Skip to content
中文

ModelBinding ​

KIT · interface · Type contract

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

Import ​

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

Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits, @jangts/agent-core/kits/inference.

Definition ​

ts
interface ModelBinding<Input, Output>

Type parameters ​

Type parameterConstraintDefault type
Input——
Output——

Field structure ​

FieldTypeRequiredDescription
idstringRequiredRead-only. Catalog key used in requests; must be unique within one ModelCalls instance.
identitystringRequiredRead-only. Non-blank implementation identity (provider plus model name) reported in diagnostics.
invoke(input: Input, context: ModelInvocationContext) => MaybePromise<Output>RequiredPerforms the provider call. May be async; the Kit applies the caller's signal.
assertInput((input: Input) => void) | undefinedOptionalOptional synchronous assertion run before the budget is consumed or the call starts.
assertOutput((output: Output) => void) | undefinedOptionalOptional synchronous assertion run after the call, before the result is reported.

Member index ​

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

Member details ​

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.

ParameterTypePassingDescription
inputInputRequired—
contextModelInvocationContextRequired—
Fields of context
FieldTypeRequiredDescription
attemptBudgetPick<AttemptBudget, "take"> | undefinedOptionalRead-only. Shared attempt budget; a refusal fails the call with BudgetError before invoking.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: MaybePromise<Output>.

Awaited value: Awaited<Output>.

Examples ​

First bind an offline demonstration vector algorithm ​

Beginner · Node.js

The algorithm only demonstrates I/O binding and does not provide real language embedding quality.

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);

Add input and output checks plus cancellation to the same invoke ​

Configuration · 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);

API reference, usage examples and developer guides.