Skip to content
English

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

字段结构 ​

字段类型必需说明
optionsPick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls">必需只读。Provider options produced from the stage's tools.
schemaCharsnumber必需只读。Characters the compiled schemas add to the input, counted against the schema budget.
instructionstring | 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.

成员目录 ​

成员用途
createResponseCreates a fresh collector for one call; never reuse a collector across calls.
resolveResolves a validated selection into the stage result.

成员详情 ​

createResponse ​

Creates a fresh collector for one call; never reuse a collector across calls.

ts
PreparedModelInteraction.createResponse(): ModelResponseCollector

Creates 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.
callsreadonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefined可省略只读。All native calls carried by a session response; omitted by single-selection collectors.
outputCharsnumber必需只读。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): Result

Resolves a validated selection into the stage result.

参数类型传递说明
selectionModelSelection必需—
stagestring必需—
selection 的字段
字段类型必需说明
inputRecord<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.
toolIdstring必需只读。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);

相关策略源码 ​

相关定义 ​

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