Skip to content
中文

ModelCalls ​

KIT · class · Runtime value

Frozen catalog of model bindings plus a checked invocation entry point.

Registration happens once in the constructor; the catalog is never mutated afterwards, so a caller cannot swap a provider in the middle of a run.

Import ​

ts
import { ModelCalls } from '@jangts/agent-core/kits/inference';
js
import { ModelCalls } from '@jangts/agent-core/kits/inference';

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

Definition ​

ts
class ModelCalls<Input, Output, Bindings>

Type parameters ​

Type parameterConstraintDefault type
Input—unknown
Output—unknown
Bindingsreadonly ModelBinding<any, any>[]readonly ModelBinding<Input, Output>[]

Constructor ​

ts
new ModelCalls<Input = unknown, Output = unknown, const Bindings extends readonly ModelBinding<any, any>[] = readonly ModelBinding<Input, Output>[]>(bindings: Bindings): ModelCalls<Input, Output, Bindings>
ParameterTypePassingDescription
bindingsBindingsRequired—

Returns: ModelCalls<Input, Output, Bindings>.

Instance properties ​

FieldTypeRequiredDescription
definitionsreadonly Readonly<Pick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>>[]RequiredRead-only. Registered bindings in declaration order, with methods already bound to their owner.

Member index ​

MemberPurpose
idsRegistered binding ids in declaration order.
hasWhether a binding id is registered.
getRegistered binding for id, or undefined when it is absent.
requireRegistered binding for id.
valuesIterate the registered bindings.
entriesIterate [id, binding] pairs.
identityImplementation identity of one binding.
invokeInvoke one registered binding under the caller's signal, timeout and attempt budget. Order matters: the binding id is resolved, input is asserted, the budget take happens before the provider call, and the output assertion runs before a result is reported. Any failure emits a diagnostic and rethrows unchanged, so callers see the provider's own error.

Member details ​

ids ​

Instance method.

Registered binding ids in declaration order.

ts
ids(): readonly string[]

Registered binding ids in declaration order.

No call parameters.

Returns: readonly string[].

has ​

Instance method.

Whether a binding id is registered.

ts
has(id: string): boolean

Whether a binding id is registered.

ParameterTypePassingDescription
idstringRequired—

Returns: boolean.

get ​

Instance method.

Registered binding for id, or undefined when it is absent.

ts
get(id: string): RegisteredBinding<Bindings[number]> | undefined

Registered binding for id, or undefined when it is absent.

ParameterTypePassingDescription
idstringRequired—

Returns: Readonly<Pick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>> \| undefined.

Optional shapes of Return value: choose one shape; required fields for each branch are listed separately.

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
idPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["id"]RequiredRead-only. Catalog key used in requests; must be unique within one ModelCalls instance.
identityPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["identity"]RequiredRead-only. Non-blank implementation identity (provider plus model name) reported in diagnostics.
invokePick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["invoke"]RequiredPerforms the provider call. May be async; the Kit applies the caller's signal.
assertInputPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["assertInput"] | undefinedOptionalOptional synchronous assertion run before the budget is consumed or the call starts.
assertOutputPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["assertOutput"] | undefinedOptionalOptional synchronous assertion run after the call, before the result is reported.

require ​

Instance method.

Registered binding for id.

ts
require(id: string): RegisteredBinding<Bindings[number]>

Registered binding for id.

ParameterTypePassingDescription
idstringRequired—

Returns: Readonly<Pick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>>.

Return value structure
FieldTypeRequiredDescription
idPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["id"]RequiredRead-only. Catalog key used in requests; must be unique within one ModelCalls instance.
identityPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["identity"]RequiredRead-only. Non-blank implementation identity (provider plus model name) reported in diagnostics.
invokePick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["invoke"]RequiredPerforms the provider call. May be async; the Kit applies the caller's signal.
assertInputPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["assertInput"] | undefinedOptionalOptional synchronous assertion run before the budget is consumed or the call starts.
assertOutputPick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>["assertOutput"] | undefinedOptionalOptional synchronous assertion run after the call, before the result is reported.

values ​

Instance method.

Iterate the registered bindings.

ts
values(): IterableIterator<RegisteredBinding<Bindings[number]>>

Iterate the registered bindings.

No call parameters.

Returns: IterableIterator<Readonly<Pick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>>>.

entries ​

Instance method.

Iterate [id, binding] pairs.

ts
entries(): IterableIterator<[string, RegisteredBinding<Bindings[number]>]>

Iterate [id, binding] pairs.

No call parameters.

Returns: IterableIterator<[string, Readonly<Pick<Bindings[number], Extract<keyof Bindings[number], keyof ModelBinding<any, any>>>>]>.

identity ​

Instance method.

Implementation identity of one binding.

ts
identity(id: string): string

Implementation identity of one binding.

ParameterTypePassingDescription
idstringRequired—

Returns: string.

invoke ​

Instance method.

Invoke one registered binding under the caller's signal, timeout and attempt budget.

Order matters: the binding id is resolved, input is asserted, the budget take happens before the provider call, and the output assertion runs before a result is reported. Any failure emits a diagnostic and rethrows unchanged, so callers see the provider's own error.

ts
invoke<const Id extends string>(request: ModelInvocationRequest<InputFor<Bindings, Id>> & { readonly modelId: Id; }, context: ModelInvocationContext): Promise<ModelResult<OutputFor<Bindings, Id>>>

Invoke one registered binding under the caller's signal, timeout and attempt budget.

Order matters: the binding id is resolved, input is asserted, the budget take happens before the provider call, and the output assertion runs before a result is reported. Any failure emits a diagnostic and rethrows unchanged, so callers see the provider's own error.

Type parameterConstraintDefault type
Idstring—
ParameterTypePassingDescription
requestModelInvocationRequest<InputFor<Bindings, Id>> & { readonly modelId: Id; }Required—
contextModelInvocationContextRequired—
Fields of request
FieldTypeRequiredDescription
modelIdIdRequiredRead-only. Registered binding id; unknown ids fail before any provider work happens.
inputInputFor<Bindings, Id>RequiredRead-only. Input handed to the binding unchanged; Core never clones App objects here.
timeoutMsnumberRequiredRead-only. Per-call timeout in milliseconds, 1..2147483647 (the platform timer ceiling).
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: Promise<ModelResult<OutputFor<Bindings, Id>>>.

Awaited value: ModelResult<OutputFor<Bindings, Id>>.

Return value structure
FieldTypeRequiredDescription
valueOutputFor<Bindings, Id>RequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.

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.