Skip to content
English

ModelBinding ​

KIT · interface · 类型合同

App-defined I/O. Registered literal IDs determine input and output types.

导入 ​

ts
import type { ModelBinding } from '@jangts/agent-core/kits/inference';
js
/** @typedef {import('@jangts/agent-core/kits/inference').ModelBinding} ModelBinding */

其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits、@jangts/agent-core/kits/inference。

定义 ​

ts
interface ModelBinding<Input, Output>

泛型参数 ​

类型参数约束默认类型
Input——
Output——

字段结构 ​

字段类型必需说明
idstring必需只读。Catalog key used in requests; must be unique within one ModelCalls instance.
identitystring必需只读。Non-blank implementation identity (provider plus model name) reported in diagnostics.
invoke(input: Input, context: ModelInvocationContext) => MaybePromise<Output>必需Performs the provider call. May be async; the Kit applies the caller's signal.
assertInput((input: Input) => void) | undefined可省略Optional synchronous assertion run before the budget is consumed or the call starts.
assertOutput((output: Output) => void) | undefined可省略Optional synchronous assertion run after the call, before the result is reported.

成员目录 ​

成员用途
invokePerforms the provider call. May be async; the Kit applies the caller's signal.

成员详情 ​

invoke ​

Performs the provider call. May be async; the Kit applies the caller's signal.

ts
ModelBinding.invoke(input: Input, context: ModelInvocationContext): MaybePromise<Output>

Performs the provider call. May be async; the Kit applies the caller's signal.

参数类型传递说明
inputInput必需—
contextModelInvocationContext必需—
context 的字段
字段类型必需说明
attemptBudgetPick<AttemptBudget, "take"> | undefined可省略只读。Shared attempt budget; a refusal fails the call with BudgetError before invoking.
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the enclosing call, when recording is enabled.

返回: MaybePromise<Output>。

等待后的值: Awaited<Output>。

使用示例 ​

先绑定一个离线演示向量算法 ​

入门 · Node.js

算法只演示 I/O 绑定,不提供真实语言向量质量。

ts
import { ModelCalls } from '@jangts/agent-core/kits/inference';
const models = new ModelCalls([
  { id: 'demo-vector', identity: 'length-direction-v1', invoke: (text: string) => [text.length, 1] },
] as const);
const result = await models.invoke({ modelId: 'demo-vector', input: 'Agent Core', timeoutMs: 1000 }, { signal: new AbortController().signal });
console.log(result.value, result.invocation.modelId);
js
import { ModelCalls } from '@jangts/agent-core/kits/inference';
const models = new ModelCalls([
    { id: 'demo-vector', identity: 'length-direction-v1', invoke: (text) => [text.length, 1] },
]);
const result = await models.invoke({ modelId: 'demo-vector', input: 'Agent Core', timeoutMs: 1000 }, { signal: new AbortController().signal });
console.log(result.value, result.invocation.modelId);

在同一 invoke 上增加输入输出检查和取消 ​

配置 · Node.js

ts
import { ModelCalls } from '@jangts/agent-core/kits/inference';
const models = new ModelCalls([
  { id: 'demo-vector', identity: 'length-direction-v1',
    assertInput(text: string) { if (!text.trim()) throw new Error('文本为空'); },
    invoke(text: string, context) { context.signal.throwIfAborted(); return [text.length, 1]; },
    assertOutput(vector: number[]) { if (vector.length !== 2 || vector.some(value => !Number.isFinite(value))) throw new Error('向量无效'); },
  },
] as const);
const controller = new AbortController();
const result = await models.invoke({ modelId: 'demo-vector', input: 'Agent Core', timeoutMs: 1000 }, { signal: controller.signal });
console.log(models.identity('demo-vector'), result.value);
js
import { ModelCalls } from '@jangts/agent-core/kits/inference';
const models = new ModelCalls([
    { id: 'demo-vector', identity: 'length-direction-v1',
        assertInput(text) { if (!text.trim())
            throw new Error('文本为空'); },
        invoke(text, context) { context.signal.throwIfAborted(); return [text.length, 1]; },
        assertOutput(vector) { if (vector.length !== 2 || vector.some(value => !Number.isFinite(value)))
            throw new Error('向量无效'); },
    },
]);
const controller = new AbortController();
const result = await models.invoke({ modelId: 'demo-vector', input: 'Agent Core', timeoutMs: 1000 }, { signal: controller.signal });
console.log(models.identity('demo-vector'), result.value);

相关策略源码 ​

相关定义 ​

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