Skip to content
中文

ContextRecord ​

CORE · type · Type contract

One journal entry: a model call, a tool request or a tool result.

The three variants are distinguished by type; value/input stay unknown because their domain meaning belongs to the App.

Import ​

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

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/context, @jangts/agent-core/journal.

Definition ​

Type: ContextRecord

Type expression
ts
type ContextRecord = RecordIdentity &
  /** A model call was made for `stageId`; `value` is what the call produced. */
  (
    | { readonly type: 'model'; readonly stageId: string; readonly value: unknown }
    /** A tool call was requested; `input` is the raw request payload. */
    | { readonly type: 'tool'; readonly toolId: string; readonly input: unknown }
    /** A tool call settled; `value` carries the result or the failure detail. */
    | {
        readonly type: 'tool-result';
        readonly toolId: string;
        readonly status: 'success' | 'error' | 'cancelled';
        readonly value: unknown;
      }
  );

Union branch structure ​

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

Branch 1
FieldTypeRequiredDescription
runIdstring | undefinedOptionalRead-only. Root run this invocation belongs to, when the host runs inside a diagnostics scope.
spanIdstring | undefinedOptionalRead-only. Diagnostics span of this invocation.
parentSpanIdstring | undefinedOptionalRead-only. Span that caused this invocation; absent for a root invocation.
parentInvocationIdstring | undefinedOptionalRead-only. Enclosing invocation when one call triggers another.
invocationIdstringRequiredRead-only. Opaque per-invocation id shared by receipt, events and diagnostic records. Invocation that produced the record.
startedAtMsnumberRequiredRead-only. Start instant in epoch milliseconds.
durationMsnumberRequiredRead-only. Elapsed milliseconds from start to completion.
producerSequencenumberRequiredRead-only. Monotonic order within this journal; starts at 1 and never repeats.
occurredAtMsnumberRequiredRead-only. Instant the record was written, in epoch milliseconds.
type"model"RequiredRead-only. See the type definition and usage context.
stageIdstringRequiredRead-only. See the type definition and usage context.
valueunknownRequiredRead-only. See the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
runIdstring | undefinedOptionalRead-only. Root run this invocation belongs to, when the host runs inside a diagnostics scope.
spanIdstring | undefinedOptionalRead-only. Diagnostics span of this invocation.
parentSpanIdstring | undefinedOptionalRead-only. Span that caused this invocation; absent for a root invocation.
parentInvocationIdstring | undefinedOptionalRead-only. Enclosing invocation when one call triggers another.
invocationIdstringRequiredRead-only. Opaque per-invocation id shared by receipt, events and diagnostic records. Invocation that produced the record.
startedAtMsnumberRequiredRead-only. Start instant in epoch milliseconds.
durationMsnumberRequiredRead-only. Elapsed milliseconds from start to completion.
producerSequencenumberRequiredRead-only. Monotonic order within this journal; starts at 1 and never repeats.
occurredAtMsnumberRequiredRead-only. Instant the record was written, in epoch milliseconds.
type"tool"RequiredRead-only. See the type definition and usage context.
toolIdstringRequiredRead-only. See the type definition and usage context.
inputunknownRequiredRead-only. See the type definition and usage context.
Branch 3
FieldTypeRequiredDescription
runIdstring | undefinedOptionalRead-only. Root run this invocation belongs to, when the host runs inside a diagnostics scope.
spanIdstring | undefinedOptionalRead-only. Diagnostics span of this invocation.
parentSpanIdstring | undefinedOptionalRead-only. Span that caused this invocation; absent for a root invocation.
parentInvocationIdstring | undefinedOptionalRead-only. Enclosing invocation when one call triggers another.
invocationIdstringRequiredRead-only. Opaque per-invocation id shared by receipt, events and diagnostic records. Invocation that produced the record.
startedAtMsnumberRequiredRead-only. Start instant in epoch milliseconds.
durationMsnumberRequiredRead-only. Elapsed milliseconds from start to completion.
producerSequencenumberRequiredRead-only. Monotonic order within this journal; starts at 1 and never repeats.
occurredAtMsnumberRequiredRead-only. Instant the record was written, in epoch milliseconds.
type"tool-result"RequiredRead-only. See the type definition and usage context.
toolIdstringRequiredRead-only. See the type definition and usage context.
status"error" | "success" | "cancelled"RequiredRead-only. See the type definition and usage context.
valueunknownRequiredRead-only. See the type definition and usage context.

Examples ​

Read the model receipt already recorded by the Runner ​

Beginner · Node.js

ts
import { ModelRunner } from '@jangts/agent-core';
const runner = new ModelRunner({ provider: { async *stream() { yield '服务运行正常'; } } });
const result = await runner.invoke('查询服务状态');
console.log(result.invocation.invocationId);
console.log(runner.journal.snapshot());
js
import { ModelRunner } from '@jangts/agent-core';
const runner = new ModelRunner({ provider: { async *stream() { yield '服务运行正常'; } } });
const result = await runner.invoke('查询服务状态');
console.log(result.invocation.invocationId);
console.log(runner.journal.snapshot());

An App adapter records the local tool it actually executes ​

Configuration · Node.js

ts
import { ContextJournal } from '@jangts/agent-core/journal';
const journal = new ContextJournal();
const invocationId = journal.beginTool({ toolId: 'get_status', input: {} });
// 此处由 App 实际执行本地读取;不是模型提供的回执。
const status = '服务运行正常';
journal.finishTool({ invocationId, status: 'success', value: status });
console.log(journal.snapshot());
js
import { ContextJournal } from '@jangts/agent-core/journal';
const journal = new ContextJournal();
const invocationId = journal.beginTool({ toolId: 'get_status', input: {} });
// 此处由 App 实际执行本地读取;不是模型提供的回执。
const status = '服务运行正常';
journal.finishTool({ invocationId, status: 'success', value: status });
console.log(journal.snapshot());

API reference, usage examples and developer guides.