Skip to content
English

createModelStrategy ​

SUPPORT · function · 运行时值

Compose runner options in precedence order: budget preset, selected model, declared reasoning, exact budget, then explicit Core fields.

The result is validated through the same Core binding rules but creates no runner and spends no calls.

导入 ​

ts
import { createModelStrategy } from '@jangts/agent-core/support';
js
import { createModelStrategy } from '@jangts/agent-core/support';

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

定义 ​

泛型参数 ​

类型参数约束默认类型
OptionsModelStrategyOptions—

调用 ​

ts
createModelStrategy<const Options extends ModelStrategyOptions>(options: Options): StrategyResult<Options>

Compose runner options in precedence order: budget preset, selected model, declared reasoning, exact budget, then explicit Core fields.

The result is validated through the same Core binding rules but creates no runner and spends no calls.

类型参数约束默认类型
OptionsModelStrategyOptions<readonly ModelTransportDefinition<unknown>[], ExecutionOptions>—
参数类型传递说明
optionsOptions必需—

返回: StrategyResult<Options>。

返回值结构
字段类型必需说明
maxOutputTokensnumber | undefined可省略Default output reservation; invoke can override it for one call.
maxCallsnumber | undefined可省略Calls allowed for the whole request, including descendant runners' reservations.
callTimeoutMsnumber | undefined可省略Deadline for one provider call, in milliseconds.
maxOutputCharsnumber | undefined可省略Cap on streamed output characters; exceeding it fails the call.
usageboolean | undefined可省略Request and record provider usage for each call.
childrenModelChildPolicy | undefined可省略只读。见类型定义及调用场景。
maxInputCharsnumber | undefined可省略Character budget for the assembled input.
maxSchemaCharsnumber | undefined可省略Character allowance for registered capability schemas.
tokenBudgetModelTokenBudget | undefined可省略见类型定义及调用场景。
providerChatProvider必需见类型定义及调用场景。
transportsTransportsFrom<Options, TransportsFrom<SelectedReasoning<Options>, TransportsFrom<SelectedModel<Options>, readonly []>>> | undefined可省略见类型定义及调用场景。
sink{ output?: ModelRunnerPorts["onOutput"]; } | undefined可省略见类型定义及调用场景。
errors{ inputBudget?: ModelRunnerPorts["inputBudgetError"]; outputBudget?: ModelRunnerPorts["outputBudgetError"]; } | undefined可省略见类型定义及调用场景。
executionExecutionFrom<Options, ExecutionFrom<SelectedReasoning<Options>, ExecutionFrom<SelectedModel<Options>, undefined>>> | undefined可省略见类型定义及调用场景。

使用示例 ​

短任务使用小预算,增加需求仍沿用同一 Core 构造 ​

入门 · Node.js

ts
import { AgentCoreEngine, ModelRunner, Support } from '@jangts/agent-core';
const provider = { async *stream() { yield '服务在线。'; } };
const config = Support.createModelStrategy({ model: { provider }, budget: 'small' });
const model = new ModelRunner(config);
console.log((await model.invoke('服务状态?')).value);
const agent = new AgentCoreEngine({ model: config });
console.log(agent.branch.agentId);
js
import { AgentCoreEngine, ModelRunner, Support } from '@jangts/agent-core';
const provider = { async *stream() { yield '服务在线。'; } };
const config = Support.createModelStrategy({ model: { provider }, budget: 'small' });
const model = new ModelRunner(config);
console.log((await model.invoke('服务状态?')).value);
const agent = new AgentCoreEngine({ model: config });
console.log(agent.branch.agentId);

已有模型用 ID 代指,推理方案显式登记 ​

配置 · Node.js

示例 Provider 完全离线。实际供应商的推理参数由各 Provider 的显式配置实现。

ts
import { ModelRunner, Support } from '@jangts/agent-core';
const fast = { async *stream() { yield '简短回答'; } };
const reasoned = { async *stream() { yield '经过复核的回答'; } };
const models = { fast: { provider: fast }, thorough: { provider: reasoned } };
const config = Support.createModelStrategy({ model: 'fast', models, budget: 'small',
  reasoning: 'high', reasoningOptions: { high: { provider: reasoned } },
  maxCalls: 1, maxOutputTokens: 1200,
});
console.log((await new ModelRunner(config).invoke('检查资料')).value);
js
import { ModelRunner, Support } from '@jangts/agent-core';
const fast = { async *stream() { yield '简短回答'; } };
const reasoned = { async *stream() { yield '经过复核的回答'; } };
const models = { fast: { provider: fast }, thorough: { provider: reasoned } };
const config = Support.createModelStrategy({ model: 'fast', models, budget: 'small',
    reasoning: 'high', reasoningOptions: { high: { provider: reasoned } },
    maxCalls: 1, maxOutputTokens: 1200,
});
console.log((await new ModelRunner(config).invoke('检查资料')).value);

自定义预算和原生 Core 端口保持有效 ​

扩展 · Node.js

计数器仅为演示。生产环境注入与实际模型匹配的计数器,不用字符估算冒充真实 token 数。

ts
import { ModelRunner, Support } from '@jangts/agent-core';
const provider = { async *stream() { yield 'done'; } };
const transport = Support.createTextTransport({ id: 'app:text' });
const config = Support.createModelStrategy({ model: { provider, transports: [transport] },
  budget: { maxCalls: 3, maxInputChars: 24000, callTimeoutMs: 20000 },
  tokenBudget: { counterId: 'app-counter', contextWindowTokens: 4000,
    countTokens({ messages }) { return messages.reduce((sum, message) => sum + message.content.length, 0); } },
});
const result = await new ModelRunner(config).invoke({ input: 'hello', transport });
console.log(result.value);
js
import { ModelRunner, Support } from '@jangts/agent-core';
const provider = { async *stream() { yield 'done'; } };
const transport = Support.createTextTransport({ id: 'app:text' });
const config = Support.createModelStrategy({ model: { provider, transports: [transport] },
    budget: { maxCalls: 3, maxInputChars: 24000, callTimeoutMs: 20000 },
    tokenBudget: { counterId: 'app-counter', contextWindowTokens: 4000,
        countTokens({ messages }) { return messages.reduce((sum, message) => sum + message.content.length, 0); } },
});
const result = await new ModelRunner(config).invoke({ input: 'hello', transport });
console.log(result.value);

相关策略源码 ​

相关定义 ​

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