nativeFunctionInteraction
CORE · constant · 运行时值
导入
ts
import { nativeFunctionInteraction } from '@jangts/agent-core/support';js
import { nativeFunctionInteraction } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 只读。Adapter id, reported when a transport is validated or rejected. |
features | { readonly strictArguments: boolean; } | 必需 | 只读。见类型定义及调用场景。 |
成员目录
| 成员 | 用途 |
|---|---|
prepare | Compiles one stage's tools. |
成员详情
prepare
Compiles one stage's tools.
ts
nativeFunctionInteraction.prepare(tools: readonly RegisteredModelTool[]): Omit<PreparedModelInteraction, "resolve">Compiles one stage's tools.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
tools | readonly Readonly<ModelTool<unknown, unknown, any>>[] | 必需 | — |
返回: Omit<PreparedModelInteraction<unknown>, "resolve">。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
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. |
使用示例
- 模型传输与阶段投影:从 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);保留 Tool,只替换明确的交互适配
扩展 · Node.js
模型调用仍是 invoke({input,transport}) 并返回 ModelResult;切换交互不重新设计业务结果。
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: 'status', description: '查询服务状态', inputSchema, resolve: () => 'status' },
]);
const native = Support.createModelTransport({ id: 'native', tools });
const adapterId: keyof typeof Support.modelInteractionCatalog = 'json-object';
const json = Support.createModelTransport({ id: 'json', tools, adapter: Support.modelInteractionCatalog[adapterId] });
console.log(native.id, json.id);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: 'status', description: '查询服务状态', inputSchema, resolve: () => 'status' },
]);
const native = Support.createModelTransport({ id: 'native', tools });
const adapterId = 'json-object';
const json = Support.createModelTransport({ id: 'json', tools, adapter: Support.modelInteractionCatalog[adapterId] });
console.log(native.id, json.id);