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。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Options | ModelStrategyOptions | — |
调用
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.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Options | ModelStrategyOptions<readonly ModelTransportDefinition<unknown>[], ExecutionOptions> | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | Options | 必需 | — |
返回: StrategyResult<Options>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
maxOutputTokens | number | undefined | 可省略 | Default output reservation; invoke can override it for one call. |
maxCalls | number | undefined | 可省略 | Calls allowed for the whole request, including descendant runners' reservations. |
callTimeoutMs | number | undefined | 可省略 | Deadline for one provider call, in milliseconds. |
maxOutputChars | number | undefined | 可省略 | Cap on streamed output characters; exceeding it fails the call. |
usage | boolean | undefined | 可省略 | Request and record provider usage for each call. |
children | ModelChildPolicy | undefined | 可省略 | 只读。见类型定义及调用场景。 |
maxInputChars | number | undefined | 可省略 | Character budget for the assembled input. |
maxSchemaChars | number | undefined | 可省略 | Character allowance for registered capability schemas. |
tokenBudget | ModelTokenBudget | undefined | 可省略 | 见类型定义及调用场景。 |
provider | ChatProvider | 必需 | 见类型定义及调用场景。 |
transports | TransportsFrom<Options, TransportsFrom<SelectedReasoning<Options>, TransportsFrom<SelectedModel<Options>, readonly []>>> | undefined | 可省略 | 见类型定义及调用场景。 |
sink | { output?: ModelRunnerPorts["onOutput"]; } | undefined | 可省略 | 见类型定义及调用场景。 |
errors | { inputBudget?: ModelRunnerPorts["inputBudgetError"]; outputBudget?: ModelRunnerPorts["outputBudgetError"]; } | undefined | 可省略 | 见类型定义及调用场景。 |
execution | ExecutionFrom<Options, ExecutionFrom<SelectedReasoning<Options>, ExecutionFrom<SelectedModel<Options>, undefined>>> | undefined | 可省略 | 见类型定义及调用场景。 |
使用示例
- 模型策略:显式选择与推荐预算:从已登记的模型和推理配置中选择,以本地预算档位补全 Core 设置,并保留逐字段精确覆盖。
短任务使用小预算,增加需求仍沿用同一 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);