尝试预算与错误归属
将尝试次数、数据无效和流程合同错误分别表达,不把所有失败都转成成功正文。
AttemptBudget 的 take 成功才消耗一次;额度不足返回 false,不再发放尝试。它是局部重试策略的计数器,不自动控制模型次数或全局截止时间。Engine 的轮次、ModelRunner 的调用额度和 App 的业务尝试属于不同计量。
BudgetError 表示预算拒绝,ValidationError 携带 issues;WorkflowContractError 表示节点/路由/结果违反运行合同。getErrorInfo 统一提取代码和可重试信息,但不意味着每种错误都应重试。模型解析错误、工具授权错误、持久化失败各自交还拥有该边界的 App handler。
由简到繁的同业务示例
只给两次业务尝试
入门 · 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));