AttemptBudget
CORE · class · 运行时值
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.
导入
ts
import { AttemptBudget } from '@jangts/agent-core/engine';js
import { AttemptBudget } from '@jangts/agent-core/engine';其他公开入口:@jangts/agent-core、@jangts/agent-core/engine、@jangts/agent-core/types。
定义
ts
class AttemptBudget构造函数
ts
new AttemptBudget(limit: number): AttemptBudget| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
limit | number | 必需 | — |
返回: AttemptBudget。
实例属性
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
limit | number | 必需 | 只读。见类型定义及调用场景。 |
used | number | 必需 | 只读。Attempts consumed so far. |
available | boolean | 必需 | 只读。Whether at least one attempt remains unspent. |
成员目录
| 成员 | 用途 |
|---|---|
take | Consume one attempt if one remains. |
成员详情
take
实例方法。
Consume one attempt if one remains.
ts
take(): booleanConsume one attempt if one remains.
无调用参数。
返回: boolean。
使用示例
- 尝试预算与错误归属:将尝试次数、数据无效和流程合同错误分别表达,不把所有失败都转成成功正文。
只给两次业务尝试
入门 · 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); // false明确额度不足与数据无效
配置 · 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));