PreparedModelInteraction
CORE · interface · 类型合同
Adapter output for one stage, before the caller binds capability resolution.
导入
ts
import type { PreparedModelInteraction } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').PreparedModelInteraction} PreparedModelInteraction */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
ts
interface PreparedModelInteraction<Result>泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Result | — | unknown |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
options | Pick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls"> | 必需 | 只读。Provider options produced from the stage's tools. |
schemaChars | number | 必需 | 只读。Characters the compiled schemas add to the input, counted against the schema budget. |
instruction | string | undefined | 可省略 | 只读。Instruction text that teaches the model how to answer; absent when the format is native. |
createResponse | () => ModelResponseCollector | 必需 | Creates a fresh collector for one call; never reuse a collector across calls. |
resolve | (selection: ModelSelection, stage: string) => Result | 必需 | Resolves a validated selection into the stage result. |
成员目录
| 成员 | 用途 |
|---|---|
createResponse | Creates a fresh collector for one call; never reuse a collector across calls. |
resolve | Resolves a validated selection into the stage result. |
成员详情
createResponse
Creates a fresh collector for one call; never reuse a collector across calls.
ts
PreparedModelInteraction.createResponse(): ModelResponseCollectorCreates a fresh collector for one call; never reuse a collector across calls.
无调用参数。
返回: ModelResponseCollector<ModelSelection>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
call | { id: string; name: string; argumentChars: number; } | undefined | 可省略 | 只读。Native function call that carried the selection, when the response used one. |
calls | readonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefined | 可省略 | 只读。All native calls carried by a session response; omitted by single-selection collectors. |
outputChars | number | 必需 | 只读。Output characters accumulated so far, for output-budget accounting. |
push | (part: ModelPart) => void | 必需 | Appends one streamed part; parts arrive in provider order. |
finish | () => ModelSelection | 必需 | Finalizes the response. Throws when the accumulated content is not a valid selection. |
resolve
Resolves a validated selection into the stage result.
ts
PreparedModelInteraction.resolve(selection: ModelSelection, stage: string): ResultResolves a validated selection into the stage result.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
selection | ModelSelection | 必需 | — |
stage | string | 必需 | — |
selection 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
input | Record<string, unknown> | 必需 | 只读。Arguments exactly as produced by the model, before schema parsing. |
call | { id: string; name: string; argumentChars: number; } | undefined | 可省略 | 只读。Native function call that carried the selection, when the provider used one. |
toolId | string | 必需 | 只读。Capability the selection resolved to. |
返回: Result。
等待后的值: Awaited<Result>。
使用示例
- 模型传输与阶段投影:从 Tool 定义生成封存目录与阶段投影,原生函数和 JSON 适配复用同一 Tool 合同。
创建具名文本传输
入门 · Node.js
ts
import { ModelRunner, Support } from '@jangts/agent-core';
const text = Support.createTextTransport({ id: 'brief', options: { temperature: 0.2 } });
const runner = new ModelRunner({ provider: { async *stream() { yield '服务正常'; } }, transports: [text] });
console.log((await runner.invoke({ input: '简答服务状态', transport: text })).value);js
import { ModelRunner, Support } from '@jangts/agent-core';
const text = Support.createTextTransport({ id: 'brief', options: { temperature: 0.2 } });
const runner = new ModelRunner({ provider: { async *stream() { yield '服务正常'; } }, transports: [text] });
console.log((await runner.invoke({ input: '简答服务状态', transport: text })).value);从同源 Tool 生成两个阶段的传输
配置 · Node.js
ts
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value: unknown) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
return {};
},
});
const tools = Support.defineModelTools([
{ id: 'greet', description: '问候', inputSchema, resolve: () => 'greet' },
{ id: 'status', description: '状态查询', inputSchema, resolve: () => 'status' },
]);
const transport = Support.createStageModelTransport({ id: 'intent', tools,
stages: [{ id: 'recognize', toolIds: ['greet', 'status'] }, { id: 'status-only', toolIds: ['status'] }],
});
console.log(transport.prepare('status-only').schemaChars);js
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
parse(value) {
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
throw new TypeError('需要空对象');
return {};
},
});
const tools = Support.defineModelTools([
{ id: 'greet', description: '问候', inputSchema, resolve: () => 'greet' },
{ id: 'status', description: '状态查询', inputSchema, resolve: () => 'status' },
]);
const transport = Support.createStageModelTransport({ id: 'intent', tools,
stages: [{ id: 'recognize', toolIds: ['greet', 'status'] }, { id: 'status-only', toolIds: ['status'] }],
});
console.log(transport.prepare('status-only').schemaChars);