Skip to content
中文

createDiagnostics ​

CORE · function · Runtime value

Configure the optional log consumer of the shared execution fact source.

Import ​

ts
import { createDiagnostics } from '@jangts/agent-core/diagnostics';
js
import { createDiagnostics } from '@jangts/agent-core/diagnostics';

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

Definition ​

Call ​

ts
createDiagnostics(options?: CreateDiagnosticsOptions): DiagnosticContext

Configure the optional log consumer of the shared execution fact source.

ParameterTypePassingDescription
optionsCreateDiagnosticsOptionsOptional—
Fields of options
FieldTypeRequiredDescription
outputCoreLogOutput | undefinedOptionalRead-only. Log destination; omission disables persistence while observation can still work.
observeExecutionObserver | undefinedOptionalRead-only. Live observer of the same facts, independent of output.
runIdstring | undefinedOptionalRead-only. Root run id; generated when omitted.
spanIdstring | undefinedOptionalRead-only. Root span id; generated when omitted.

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

Record an async plan first, then fill in the result ​

Configuration · Node.js

A scoped logger binds the group and only fixes its own writes; readAll exposes the full ledger. Every fix appends an independent record linked to its original write, without logger submission.

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { result?: string };
const workflow = defineWorkflow<State, string>({ id: 'prepare', nodes: [{
  id: 'prepare', description: '准备资料', async run(_, context) {
    const subs = context.createSubFlowLogger();
    const read = subs.fork('material-read');
    const id = read.write({ name: '读取资料', kind: 'read', type: 'material.read' });
    read.fix(id, { status: 'running' });
    const result = await Promise.resolve('已准备');
    read.fix(id, { status: 'success', usedModel: false, payload: { result } });
    context.report({ usedModel: false });
    console.log(read.readAll());
    return { result };
  }, routes: [route.deliver('result')],
}] });
const agent = new AgentCoreEngine({ workflow, execution: { observe: {
  accepts: ({ event, content }) => content === 'metadata' || event === 'node.progress',
  event(event) { if (event.type === 'node.progress') console.log(event.payload.subStep, event.subSteps); },
} } });
console.log(await agent.run({}));
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({ id: 'prepare', nodes: [{
            id: 'prepare', description: '准备资料', async run(_, context) {
                const subs = context.createSubFlowLogger();
                const read = subs.fork('material-read');
                const id = read.write({ name: '读取资料', kind: 'read', type: 'material.read' });
                read.fix(id, { status: 'running' });
                const result = await Promise.resolve('已准备');
                read.fix(id, { status: 'success', usedModel: false, payload: { result } });
                context.report({ usedModel: false });
                console.log(read.readAll());
                return { result };
            }, routes: [route.deliver('result')],
        }] });
const agent = new AgentCoreEngine({ workflow, execution: { observe: {
            accepts: ({ event, content }) => content === 'metadata' || event === 'node.progress',
            event(event) { if (event.type === 'node.progress')
                console.log(event.payload.subStep, event.subSteps); },
        } } });
console.log(await agent.run({}));

API reference, usage examples and developer guides.