ModelInvocationContext
KIT · interface · 类型合同
One take consumes one binding invocation attempt, independent of batch size or chat tokens.
导入
ts
import type { ModelInvocationContext } from '@jangts/agent-core/kits/inference';js
/** @typedef {import('@jangts/agent-core/kits/inference').ModelInvocationContext} ModelInvocationContext */其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits、@jangts/agent-core/kits/inference。
定义
ts
interface ModelInvocationContext继承或实现: ExecutionContext。
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
attemptBudget | Pick<AttemptBudget, "take"> | undefined | 可省略 | 只读。Shared attempt budget; a refusal fails the call with BudgetError before invoking. |
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the enclosing call, when recording is enabled. |
使用示例
- ModelCalls:外部推理绑定:为 embedding 等 App 自定义 I/O 绑定身份、校验、取消与统一调用回执。
先绑定一个离线演示向量算法
入门 · 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);