Skip to content
English

BudgetError ​

CORE · class · 运行时值

A configured allowance (rounds, model calls, attempts) is exhausted.

The optional message states which allowance ran out; code is the stable marker.

导入 ​

ts
import { BudgetError } from '@jangts/agent-core/model';
js
import { BudgetError } from '@jangts/agent-core/model';

其他公开入口:@jangts/agent-core、@jangts/agent-core/model、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/errors。

定义 ​

ts
class BudgetError

继承或实现: Error。

构造函数 ​

ts
new BudgetError(message?: string): BudgetError
参数类型传递说明
messagestring | undefined可省略—

返回: BudgetError。

构造函数 ​

ts
new BudgetError(message?: string, options?: ErrorOptions): BudgetError
参数类型传递说明
messagestring | undefined可省略—
optionsErrorOptions | undefined可省略—

返回: BudgetError。

实例属性 ​

字段类型必需说明
code"BUDGET"必需只读。见类型定义及调用场景。

使用示例 ​

  • 尝试预算与错误归属:将尝试次数、数据无效和流程合同错误分别表达,不把所有失败都转成成功正文。

只给两次业务尝试 ​

入门 · 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); // false
js
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));

接口参考、使用示例与开发指南。