Skip to content
中文

Bind once, multiple models per run, and switching across runs ​

Provide stable model configuration at construction; requests select by configuration ID instead of injecting a Provider repeatedly.

A single model can be supplied as the model configuration at construction, or bound once later with bindModel. For multiple models, register configurations in models and select the default with the model string; with multiple models the default cannot be guessed. Unknown model IDs are rejected before the call.

switchModel changes the default selection for later runs; run(state,{modelId}) overrides only the current run, and node model.invoke({modelId}) overrides only that model call. A single Workflow can draft with a fast model and then review with another. Multiple models share the maxModelCalls request budget, each model still has its own local limits, and switching does not refresh ancestor quota.

Model configuration and selection are bound on the Agent, and the Workflow uses them through the model port in its nodes.

Progressive examples for the same business case ​

Bind the model to the Agent in advance ​

Beginner · 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: '你好' }));

Add a model catalog and supply only the ID at call time ​

Configuration · 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' }));

Call two bound models within the same task ​

Extension · Node.js

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { input: string; answer?: string };
const provider = {
  async *stream(messages: readonly { content: string }[]) {
    yield '收到:' + messages.at(-1)!.content;
  },
};
const workflow = defineWorkflow<State, string>({ id: 'draft-review', nodes: [{
  id: 'reply', description: '草拟后复核', startsRound: true,
  async run(state, { model }) {
    const draft = await model.invoke({ input: state.input, modelId: 'quick' });
    const reviewed = await model.invoke({ input: '复核这份草稿:' + draft.value, modelId: 'careful' });
    return { answer: reviewed.value };
  }, routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 2,
  models: { quick: { provider }, careful: { provider } },
});
console.log(await agent.run({ input: '解释服务状态' }));
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const provider = {
    async *stream(messages) {
        yield '收到:' + messages.at(-1).content;
    },
};
const workflow = defineWorkflow({ id: 'draft-review', nodes: [{
            id: 'reply', description: '草拟后复核', startsRound: true,
            async run(state, { model }) {
                const draft = await model.invoke({ input: state.input, modelId: 'quick' });
                const reviewed = await model.invoke({ input: '复核这份草稿:' + draft.value, modelId: 'careful' });
                return { answer: reviewed.value };
            }, routes: [route.deliver('answer')],
        }] });
const agent = new AgentCoreEngine({ workflow, model: 'quick', maxModelCalls: 2,
    models: { quick: { provider }, careful: { provider } },
});
console.log(await agent.run({ input: '解释服务状态' }));

API reference, usage examples and developer guides.