DiagnosticContext
CORE · interface · 类型合同
Recording scope bound to one execution identity.
A child scope inherits the sink and observer of its parent; only identity changes.
导入
import type { DiagnosticContext } from '@jangts/agent-core/diagnostics';/** @typedef {import('@jangts/agent-core/diagnostics').DiagnosticContext} DiagnosticContext */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/diagnostics。
定义
interface DiagnosticContext字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
identity | Readonly<DiagnosticIdentity> | undefined | 可省略 | 只读。Identity of the current scope, when the scope is bound to an execution. |
enabled | (event: CoreEventName, content?: "metadata" | "content") => boolean | 必需 | Cheap pre-check so callers can skip building expensive payloads. |
emit | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => void | 必需 | Records one fact. |
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. |
child | (spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContext | 必需 | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | (observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContext | 必需 | Bind live observation without changing the log destination or request identity. |
成员目录
| 成员 | 用途 |
|---|---|
enabled | Cheap pre-check so callers can skip building expensive payloads. |
emit | Records one fact. |
deliver | Ordered content delivery uses the same fact, while consumer failures propagate. |
child | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | Bind live observation without changing the log destination or request identity. |
成员详情
enabled
Cheap pre-check so callers can skip building expensive payloads.
DiagnosticContext.enabled(event: CoreEventName, content?: "metadata" | "content"): booleanCheap pre-check so callers can skip building expensive payloads.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
event | keyof CoreEventPayloads | 必需 | — |
content | "metadata" | "content" | undefined | 可省略 | — |
返回: boolean。
emit
Records one fact.
DiagnosticContext.emit<K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput): voidRecords one fact.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
K | keyof CoreEventPayloads | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
event | K | 必需 | — |
payload | () => CoreEventPayloads[K] | 必需 | — |
debug | DebugStepsOutput | undefined | 可省略 | — |
参数 debug的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
undefined
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
usedModel | boolean | undefined | 可省略 | 只读。Model use reported by this handler; omitted when unknown. |
recordedSubSteps | readonly Readonly<RecordedSubStep>[] | undefined | 可省略 | 只读。Runtime-owned immutable snapshot, committed when a logger writes or fixes a step. |
返回: void。
deliver
Ordered content delivery uses the same fact, while consumer failures propagate.
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.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
K | keyof CoreEventPayloads | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
event | K | 必需 | — |
payload | () => CoreEventPayloads[K] | 必需 | — |
sink | (event: Extract<CoreEvent, { type: K; }>) => void | Promise<void> | 必需 | — |
返回: Promise<void>。
等待后的值: void。
child
Derives a child span; fields fills values the child knows but the parent does not.
DiagnosticContext.child(spanId: string, fields?: Readonly<DiagnosticSpanFields>): DiagnosticContextDerives a child span; fields fills values the child knows but the parent does not.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
spanId | string | 必需 | — |
fields | Readonly<DiagnosticSpanFields> | undefined | 可省略 | — |
参数 fields的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
undefined
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
parentInvocationId | string | undefined | 可省略 | 只读。Enclosing invocation, when one invocation triggers another. |
invocationId | string | undefined | 可省略 | 只读。Invocation that produced the fact, when the fact belongs to one. |
nodeRole | "invocation" | "workflow-node" | "workflow-boundary" | "loop-step" | undefined | 可省略 | 只读。Role the emitting span plays in the execution graph. |
agentId | string | undefined | 可省略 | 只读。App-owned agent identity. |
branchId | string | undefined | 可省略 | 只读。App-owned branch identity. |
agentVersionId | string | undefined | 可省略 | 只读。App-owned immutable version identity of the agent definition. |
parentBranchId | string | undefined | 可省略 | 只读。Branch this branch was forked from, when applicable. |
parentAgentVersionId | string | undefined | 可省略 | 只读。Version of the parent branch at the fork point. |
forkPointId | string | undefined | 可省略 | 只读。App-owned fork marker shared by the forked branch and its origin. |
workflowInvocationId | string | undefined | 可省略 | 只读。Workflow invocation the fact belongs to. |
workflowNodeId | string | undefined | 可省略 | 只读。Workflow node invocation the fact belongs to. |
definitionKind | string | undefined | 可省略 | 只读。Kind of the workflow/agent definition that dispatched the node. |
definitionId | string | undefined | 可省略 | 只读。Id of that definition. |
definitionRevision | string | undefined | 可省略 | 只读。Revision of that definition; distinguishes executions of different definition states. |
nodeId | string | undefined | 可省略 | 只读。Node id inside the workflow. |
nodeName | string | undefined | 可省略 | 只读。Human-readable node name. |
nodeKind | string | undefined | 可省略 | 只读。Node kind as declared by the App. |
nodeDescription | string | undefined | 可省略 | 只读。Optional node description for diagnostics display. |
stageId | string | undefined | 可省略 | 只读。Model stage that produced the fact. |
modelId | string | undefined | 可省略 | 只读。Model binding used, when the fact concerns a model call. |
返回: DiagnosticContext。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
identity | Readonly<DiagnosticIdentity> | undefined | 可省略 | 只读。Identity of the current scope, when the scope is bound to an execution. |
enabled | (event: CoreEventName, content?: "metadata" | "content") => boolean | 必需 | Cheap pre-check so callers can skip building expensive payloads. |
emit | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => void | 必需 | Records one fact. |
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. |
child | (spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContext | 必需 | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | (observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContext | 必需 | Bind live observation without changing the log destination or request identity. |
withObserver
Bind live observation without changing the log destination or request identity.
DiagnosticContext.withObserver(observer: import("./execution/events.js").ExecutionObserver): DiagnosticContextBind live observation without changing the log destination or request identity.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
observer | ExecutionObserver | 必需 | — |
observer 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
accepts | ((descriptor: Readonly<ExecutionEventDescriptor>) => boolean) | undefined | 可省略 | Metadata-only by default. Content requires an explicit accepts decision. |
event | (event: Readonly<CoreEvent>) => void | Promise<void> | 必需 | Handles one fact; a rejected promise propagates to the emitting call. |
返回: DiagnosticContext。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
identity | Readonly<DiagnosticIdentity> | undefined | 可省略 | 只读。Identity of the current scope, when the scope is bound to an execution. |
enabled | (event: CoreEventName, content?: "metadata" | "content") => boolean | 必需 | Cheap pre-check so callers can skip building expensive payloads. |
emit | <K extends CoreEventName>(event: K, payload: () => CoreEventPayloads[K], debug?: DebugStepsOutput) => void | 必需 | Records one fact. |
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. |
child | (spanId: string, fields?: Readonly<DiagnosticSpanFields>) => DiagnosticContext | 必需 | Derives a child span; fields fills values the child knows but the parent does not. |
withObserver | (observer: import("./execution/events.js").ExecutionObserver) => DiagnosticContext | 必需 | Bind live observation without changing the log destination or request identity. |
使用示例
- 过程监听与节点报告:App 订阅 Core 执行事实;节点报告自身 usedModel,并用实例式子日志先记意图、再补结果,Loop 负责身份和日志。
监听任务经过的节点与模型事件
入门 · Node.js
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' }));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' }));节点执行时报告业务阶段
配置 · Node.js
节点只报告它知道的事实;Core 负责节点身份和执行关联。
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' }));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' }));