AttemptBudget
CORE · class · Runtime value
Successful acquisitions consume attempts; a refused acquisition never grants another.
available is a snapshot of the counter, not a reservation: a caller must still call take and honor its boolean result before acting.
Import
ts
import { AttemptBudget } from '@jangts/agent-core/engine';js
import { AttemptBudget } from '@jangts/agent-core/engine';Other public entry points:@jangts/agent-core, @jangts/agent-core/engine, @jangts/agent-core/types.
Definition
ts
class AttemptBudgetConstructor
ts
new AttemptBudget(limit: number): AttemptBudget| Parameter | Type | Passing | Description |
|---|---|---|---|
limit | number | Required | — |
Returns: AttemptBudget.
Instance properties
| Field | Type | Required | Description |
|---|---|---|---|
limit | number | Required | Read-only. See the type definition and usage context. |
used | number | Required | Read-only. Attempts consumed so far. |
available | boolean | Required | Read-only. Whether at least one attempt remains unspent. |
Member index
| Member | Purpose |
|---|---|
take | Consume one attempt if one remains. |
Member details
take
Instance method.
Consume one attempt if one remains.
ts
take(): booleanConsume one attempt if one remains.
No call parameters.
Returns: boolean.
Examples
- Attempt budgets and error ownership: Express used attempts, invalid data, and workflow contract errors separately instead of turning every failure into successful prose.
Allow only two business attempts
Beginner · Node.js
ts
import { AttemptBudget } from '@jangts/agent-core/engine';
const attempts = new AttemptBudget(2);
while (attempts.take()) console.log('attempt', attempts.used);
console.log(attempts.available); // falsejs
import { AttemptBudget } from '@jangts/agent-core/engine';
const attempts = new AttemptBudget(2);
while (attempts.take())
console.log('attempt', attempts.used);
console.log(attempts.available); // falseDistinguish an exhausted quota from invalid data
Configuration · Node.js
ts
import { BudgetError, ValidationError, getErrorInfo } from '@jangts/agent-core';
function requireAnswer(answer: string) {
if (!answer.trim()) throw new ValidationError([{ code: 'ANSWER_EMPTY', message: '回答不能为空' }]);
return answer;
}
try { requireAnswer(''); }
catch (error) { console.log(getErrorInfo(error)); }
const exhausted = new BudgetError('本次业务的尝试额度已用尽');
console.log(getErrorInfo(exhausted));js
import { BudgetError, ValidationError, getErrorInfo } from '@jangts/agent-core';
function requireAnswer(answer) {
if (!answer.trim())
throw new ValidationError([{ code: 'ANSWER_EMPTY', message: '回答不能为空' }]);
return answer;
}
try {
requireAnswer('');
}
catch (error) {
console.log(getErrorInfo(error));
}
const exhausted = new BudgetError('本次业务的尝试额度已用尽');
console.log(getErrorInfo(exhausted));