AgentModelBinding
CORE · type · 类型合同
Model binding accepted by the engine.
Execution scope belongs to the engine, so a bound model may not carry its own signal or execution options.
导入
ts
import type { AgentModelBinding } from '@jangts/agent-core/engine';js
/** @typedef {import('@jangts/agent-core/engine').AgentModelBinding} AgentModelBinding */其他公开入口:@jangts/agent-core、@jangts/agent-core/engine、@jangts/agent-core/types。
定义
类型: AgentModelBinding
类型表达式
ts
type AgentModelBinding = ModelRunnerOptions & { execution?: never; signal?: never };字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
provider | ChatProvider | 必需 | 见类型定义及调用场景。 |
execution | undefined | 可省略 | 见类型定义及调用场景。 |
transports | readonly ModelTransportDefinition<unknown>[] | undefined | 可省略 | 见类型定义及调用场景。 |
sink | { output?: ModelRunnerPorts["onOutput"]; } | undefined | 可省略 | 见类型定义及调用场景。 |
errors | { inputBudget?: ModelRunnerPorts["inputBudgetError"]; outputBudget?: ModelRunnerPorts["outputBudgetError"]; } | undefined | 可省略 | 见类型定义及调用场景。 |
children | ModelChildPolicy | undefined | 可省略 | 只读。见类型定义及调用场景。 |
maxInputChars | number | undefined | 可省略 | Character budget for the assembled input. |
maxSchemaChars | number | undefined | 可省略 | Character allowance for registered capability schemas. |
tokenBudget | ModelTokenBudget | undefined | 可省略 | 见类型定义及调用场景。 |
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. |
signal | undefined | 可省略 | 见类型定义及调用场景。 |
使用示例
- 一次绑定,单次多模型与跨次切换:构造时提供稳定模型配置;请求用配置 ID 选择,不反复注入 Provider。
将模型提前绑到 Agent
入门 · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { input: string; answer?: string };
const workflow = defineWorkflow<State, string>({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const agent = new AgentCoreEngine({ workflow, model: { provider } });
console.log(await agent.run({ input: '你好' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const agent = new AgentCoreEngine({ workflow, model: { provider } });
console.log(await agent.run({ input: '你好' }));增加模型目录,调用时只给 ID
配置 · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { input: string; answer?: string };
const workflow = defineWorkflow<State, string>({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 4,
models: { quick: { provider, maxCalls: 2 }, careful: { provider, maxCalls: 2 } },
});
console.log(await agent.run({ input: '简答' }));
console.log(await agent.run({ input: '仔细回答' }, { modelId: 'careful' }));
agent.switchModel('careful');
console.log(await agent.run({ input: '后续默认使用 careful' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'chat', nodes: [{
id: 'reply', description: '用绑定模型回答', startsRound: true,
async run(state, { model }) {
const { value } = await model.invoke({ input: state.input });
return { answer: value };
}, routes: [route.deliver('answer')],
}] });
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 4,
models: { quick: { provider, maxCalls: 2 }, careful: { provider, maxCalls: 2 } },
});
console.log(await agent.run({ input: '简答' }));
console.log(await agent.run({ input: '仔细回答' }, { modelId: 'careful' }));
agent.switchModel('careful');
console.log(await agent.run({ input: '后续默认使用 careful' }));