Skip to content
中文

DiagnosticContext ​

CORE · interface · Type contract

Recording scope bound to one execution identity.

A child scope inherits the sink and observer of its parent; only identity changes.

Import ​

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

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

Definition ​

ts
interface DiagnosticContext

Field structure ​

FieldTypeRequiredDescription
identityReadonly<DiagnosticIdentity> | undefinedOptionalRead-only. Identity of the current scope, when the scope is bound to an execution.
enabled(event: CoreEventName, content?: "metadata" | "content") => booleanRequiredCheap pre-check so callers can skip building expensive payloads.
emit<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => voidRequiredRecords one fact.
deliver<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>) => Promise<void>RequiredOrdered content delivery uses the same fact, while consumer failures propagate.
child(spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContextRequiredDerives a child span; fields fills values the child knows but the parent does not.
withObserver(observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContextRequiredBind live observation without changing the log destination or request identity.

Member index ​

MemberPurpose
enabledCheap pre-check so callers can skip building expensive payloads.
emitRecords one fact.
deliverOrdered content delivery uses the same fact, while consumer failures propagate.
childDerives a child span; fields fills values the child knows but the parent does not.
withObserverBind live observation without changing the log destination or request identity.

Member details ​

enabled ​

Cheap pre-check so callers can skip building expensive payloads.

ts
DiagnosticContext.enabled(event: CoreEventName, content?: "metadata" | "content"): boolean

Cheap pre-check so callers can skip building expensive payloads.

ParameterTypePassingDescription
eventkeyof CoreEventPayloadsRequired—
content"metadata" | "content" | undefinedOptional—

Returns: boolean.

emit ​

Records one fact.

ts
DiagnosticContext.emit<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput): void

Records one fact.

Type parameterConstraintDefault type
Kkeyof CoreEventPayloads—
ParameterTypePassingDescription
eventKRequired—
payload() => CoreEventPayloads[K]Required—
debugDebugStepsOutput | undefinedOptional—

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

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
usedModelboolean | undefinedOptionalRead-only. Model use reported by this handler; omitted when unknown.
recordedSubStepsreadonly Readonly<RecordedSubStep>[] | undefinedOptionalRead-only. Runtime-owned immutable snapshot, committed when a logger writes or fixes a step.

Returns: void.

deliver ​

Ordered content delivery uses the same fact, while consumer failures propagate.

ts
DiagnosticContext.deliver<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>): Promise<void>

Ordered content delivery uses the same fact, while consumer failures propagate.

Type parameterConstraintDefault type
Kkeyof CoreEventPayloads—
ParameterTypePassingDescription
eventKRequired—
payload() => CoreEventPayloads[K]Required—
sink(event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>Required—

Returns: Promise<void>.

Awaited value: void.

child ​

Derives a child span; fields fills values the child knows but the parent does not.

ts
DiagnosticContext.child(spanId: string, fields?: Readonly<DiagnosticSpanFields>): DiagnosticContext

Derives a child span; fields fills values the child knows but the parent does not.

ParameterTypePassingDescription
spanIdstringRequired—
fieldsReadonly<DiagnosticSpanFields> | undefinedOptional—

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

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
parentInvocationIdstring | undefinedOptionalRead-only. Enclosing invocation, when one invocation triggers another.
invocationIdstring | undefinedOptionalRead-only. Invocation that produced the fact, when the fact belongs to one.
nodeRole"invocation" | "workflow-node" | "workflow-boundary" | "loop-step" | undefinedOptionalRead-only. Role the emitting span plays in the execution graph.
agentIdstring | undefinedOptionalRead-only. App-owned agent identity.
branchIdstring | undefinedOptionalRead-only. App-owned branch identity.
agentVersionIdstring | undefinedOptionalRead-only. App-owned immutable version identity of the agent definition.
parentBranchIdstring | undefinedOptionalRead-only. Branch this branch was forked from, when applicable.
parentAgentVersionIdstring | undefinedOptionalRead-only. Version of the parent branch at the fork point.
forkPointIdstring | undefinedOptionalRead-only. App-owned fork marker shared by the forked branch and its origin.
workflowInvocationIdstring | undefinedOptionalRead-only. Workflow invocation the fact belongs to.
workflowNodeIdstring | undefinedOptionalRead-only. Workflow node invocation the fact belongs to.
definitionKindstring | undefinedOptionalRead-only. Kind of the workflow/agent definition that dispatched the node.
definitionIdstring | undefinedOptionalRead-only. Id of that definition.
definitionRevisionstring | undefinedOptionalRead-only. Revision of that definition; distinguishes executions of different definition states.
nodeIdstring | undefinedOptionalRead-only. Node id inside the workflow.
nodeNamestring | undefinedOptionalRead-only. Human-readable node name.
nodeKindstring | undefinedOptionalRead-only. Node kind as declared by the App.
nodeDescriptionstring | undefinedOptionalRead-only. Optional node description for diagnostics display.
stageIdstring | undefinedOptionalRead-only. Model stage that produced the fact.
modelIdstring | undefinedOptionalRead-only. Model binding used, when the fact concerns a model call.

Returns: DiagnosticContext.

Return value structure
FieldTypeRequiredDescription
identityReadonly<DiagnosticIdentity> | undefinedOptionalRead-only. Identity of the current scope, when the scope is bound to an execution.
enabled(event: CoreEventName, content?: "metadata" | "content") => booleanRequiredCheap pre-check so callers can skip building expensive payloads.
emit<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => voidRequiredRecords one fact.
deliver<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>) => Promise<void>RequiredOrdered content delivery uses the same fact, while consumer failures propagate.
child(spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContextRequiredDerives a child span; fields fills values the child knows but the parent does not.
withObserver(observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContextRequiredBind live observation without changing the log destination or request identity.

withObserver ​

Bind live observation without changing the log destination or request identity.

ts
DiagnosticContext.withObserver(observer: import("./execution/events.js").ExecutionObserver): DiagnosticContext

Bind live observation without changing the log destination or request identity.

ParameterTypePassingDescription
observerExecutionObserverRequired—
Fields of observer
FieldTypeRequiredDescription
accepts((descriptor: Readonly<ExecutionEventDescriptor>) => boolean) | undefinedOptionalMetadata-only by default. Content requires an explicit accepts decision.
event(event: Readonly<CoreEvent>) => void | Promise<void>RequiredHandles one fact; a rejected promise propagates to the emitting call.

Returns: DiagnosticContext.

Return value structure
FieldTypeRequiredDescription
identityReadonly<DiagnosticIdentity> | undefinedOptionalRead-only. Identity of the current scope, when the scope is bound to an execution.
enabled(event: CoreEventName, content?: "metadata" | "content") => booleanRequiredCheap pre-check so callers can skip building expensive payloads.
emit<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => voidRequiredRecords one fact.
deliver<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], sink: (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void>) => Promise<void>RequiredOrdered content delivery uses the same fact, while consumer failures propagate.
child(spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContextRequiredDerives a child span; fields fills values the child knows but the parent does not.
withObserver(observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContextRequiredBind live observation without changing the log destination or request identity.

Examples ​

  • Process observation and node reports: The App subscribes to Core execution facts; nodes report their own usedModel and use an instance sub-logger to record intent first and then results, while the Loop owns identity and logging.

Observe the nodes and model events a task passes through ​

Beginner · Node.js

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';

type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({
  id: 'greeting',
  nodes: [{
    id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
    run: state => ({ answer: '你好,' + state.name }),
    routes: [route.deliver('answer')],
  }],
});
const agent = new AgentCoreEngine({ workflow, execution: {
  observe: { event(event) { console.log(event.type, event.eventId); } },
} });
console.log(await agent.run({ name: 'Ivan' }));
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({
    id: 'greeting',
    nodes: [{
            id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
            run: state => ({ answer: '你好,' + state.name }),
            routes: [route.deliver('answer')],
        }],
});
const agent = new AgentCoreEngine({ workflow, execution: {
        observe: { event(event) { console.log(event.type, event.eventId); } },
    } });
console.log(await agent.run({ name: 'Ivan' }));

Report a business stage while the node executes ​

Configuration · Node.js

The node only reports facts it knows; Core owns node identity and execution association.

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({ id: 'greeting', nodes: [{
  id: 'greet', description: '生成问候', startsRound: true,
  run(state, context) {
    context.report({ payload: { phase: 'formatting' } });
    return { answer: '你好,' + state.name };
  }, routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow, execution: {
  observe: { event(event) { console.log(event.type); } },
} });
console.log(await agent.run({ name: 'Ivan' }));
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'greeting', nodes: [{
            id: 'greet', description: '生成问候', startsRound: true,
            run(state, context) {
                context.report({ payload: { phase: 'formatting' } });
                return { answer: '你好,' + state.name };
            }, routes: [route.deliver('answer')],
        }] });
const agent = new AgentCoreEngine({ workflow, execution: {
        observe: { event(event) { console.log(event.type); } },
    } });
console.log(await agent.run({ name: 'Ivan' }));

API reference, usage examples and developer guides.