operon-agents-core 0.1.0-alpha.2 → 0.1.0-alpha.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/agent.d.ts +8 -7
- package/dist/agent/compose-hooks.d.ts +10 -1
- package/dist/agent/compose-hooks.js +61 -6
- package/dist/agent/concurrency.js +1 -1
- package/dist/agent/context-report.d.ts +2 -2
- package/dist/agent/durable-state.d.ts +22 -0
- package/dist/agent/durable-state.js +54 -0
- package/dist/agent/index.d.ts +2 -2
- package/dist/agent/index.js +1 -1
- package/dist/agent/instruction-context.d.ts +6 -6
- package/dist/agent/instruction-context.js +23 -23
- package/dist/agent/profile-builtins.js +1 -1
- package/dist/agent/run-support.d.ts +1 -1
- package/dist/agent/run-support.js +6 -5
- package/dist/agent/runner.d.ts +25 -22
- package/dist/agent/runner.js +217 -121
- package/dist/agent/session.d.ts +262 -156
- package/dist/agent/session.js +590 -418
- package/dist/agent/subagent-tools.js +3 -3
- package/dist/agent/subagent.js +28 -13
- package/dist/agent/toolset.d.ts +1 -0
- package/dist/agent/toolset.js +13 -7
- package/dist/agent/workflow/journal.d.ts +2 -2
- package/dist/agent/workflow/journal.js +2 -2
- package/dist/agent/workflow/manager.d.ts +1 -1
- package/dist/agent/workflow/manager.js +1 -1
- package/dist/agent/workflow/prompt.js +2 -2
- package/dist/agent/workflow/sandbox.d.ts +1 -1
- package/dist/agent/workflow/sandbox.js +1 -1
- package/dist/agent/workflow/tool.js +30 -43
- package/dist/agent/worktree.d.ts +8 -8
- package/dist/agent/worktree.js +15 -15
- package/dist/capabilities/assembler.d.ts +29 -5
- package/dist/capabilities/assembler.js +85 -9
- package/dist/capabilities/background/code-task.d.ts +43 -0
- package/dist/capabilities/background/code-task.js +75 -0
- package/dist/capabilities/background/command-task.d.ts +9 -2
- package/dist/capabilities/background/command-task.js +22 -6
- package/dist/capabilities/background/index.d.ts +6 -1
- package/dist/capabilities/background/index.js +26 -20
- package/dist/capabilities/background/manager.d.ts +57 -1
- package/dist/capabilities/background/manager.js +210 -45
- package/dist/capabilities/background/persist-disk.d.ts +1 -1
- package/dist/capabilities/background/persist-disk.js +1 -1
- package/dist/capabilities/background/persist.js +4 -2
- package/dist/capabilities/background/projections.d.ts +16 -0
- package/dist/capabilities/background/projections.js +86 -0
- package/dist/capabilities/background/stall-watchdog.d.ts +41 -0
- package/dist/capabilities/background/stall-watchdog.js +85 -0
- package/dist/capabilities/background/task.d.ts +8 -7
- package/dist/capabilities/background/tools.js +33 -7
- package/dist/capabilities/capability-data.d.ts +52 -0
- package/dist/capabilities/capability-data.js +88 -0
- package/dist/capabilities/capability-state.d.ts +2 -4
- package/dist/capabilities/capability-state.js +5 -23
- package/dist/capabilities/capability.d.ts +203 -38
- package/dist/capabilities/compaction/index.js +10 -4
- package/dist/capabilities/compaction/service.d.ts +20 -5
- package/dist/capabilities/compaction/service.js +27 -11
- package/dist/capabilities/goal/goal-store.d.ts +76 -12
- package/dist/capabilities/goal/goal-store.js +137 -53
- package/dist/capabilities/goal/index.d.ts +1 -1
- package/dist/capabilities/goal/index.js +30 -7
- package/dist/capabilities/index.d.ts +9 -6
- package/dist/capabilities/index.js +4 -3
- package/dist/capabilities/plan/index.js +22 -5
- package/dist/capabilities/plan/plan-mode.d.ts +30 -9
- package/dist/capabilities/plan/plan-mode.js +57 -32
- package/dist/capabilities/plan/tools.js +1 -1
- package/dist/capabilities/skills/flow-provider.js +3 -3
- package/dist/capabilities/skills/index.d.ts +13 -1
- package/dist/capabilities/skills/index.js +30 -21
- package/dist/capabilities/skills/parser.d.ts +2 -2
- package/dist/capabilities/skills/parser.js +3 -3
- package/dist/capabilities/skills/registry.d.ts +5 -6
- package/dist/capabilities/skills/registry.js +6 -9
- package/dist/capabilities/skills/scanner.d.ts +3 -3
- package/dist/capabilities/skills/scanner.js +14 -14
- package/dist/capabilities/skills/service.d.ts +2 -1
- package/dist/capabilities/skills/service.js +3 -3
- package/dist/capabilities/skills/skill-tool.d.ts +2 -0
- package/dist/capabilities/skills/skill-tool.js +1 -1
- package/dist/capabilities/task/index.js +2 -1
- package/dist/capabilities/task/persist-disk.d.ts +1 -1
- package/dist/capabilities/task/persist-disk.js +1 -1
- package/dist/capabilities/task/persist.js +1 -1
- package/dist/capabilities/todo/index.js +11 -6
- package/dist/capabilities/todo/injector.js +2 -1
- package/dist/capabilities/todo/todo-store.d.ts +28 -7
- package/dist/capabilities/todo/todo-store.js +44 -11
- package/dist/capabilities/todo/tools.js +8 -3
- package/dist/capabilities/tool-provider.d.ts +3 -3
- package/dist/capabilities/user-hooks/engine.d.ts +3 -3
- package/dist/capabilities/user-hooks/engine.js +7 -7
- package/dist/capabilities/user-hooks/index.js +2 -1
- package/dist/capabilities/workflow/index.d.ts +5 -3
- package/dist/capabilities/workflow/index.js +3 -2
- package/dist/commands/index.js +34 -32
- package/dist/config/index.d.ts +1 -1
- package/dist/config/provider-manager.js +0 -28
- package/dist/config/schema.d.ts +53 -4
- package/dist/config/schema.js +53 -1
- package/dist/events/events.d.ts +43 -11
- package/dist/events/events.js +1 -0
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.js +1 -1
- package/dist/events/projection.d.ts +49 -1
- package/dist/events/projection.js +144 -19
- package/dist/events/publisher.js +2 -21
- package/dist/home.d.ts +1 -1
- package/dist/home.js +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/internal.d.ts +5 -2
- package/dist/internal.js +5 -2
- package/dist/llm/define-model.d.ts +4 -0
- package/dist/llm/define-model.js +63 -25
- package/dist/llm/index.d.ts +1 -0
- package/dist/llm/index.js +1 -0
- package/dist/llm/model.d.ts +2 -0
- package/dist/llm/provider.d.ts +17 -0
- package/dist/llm/provider.js +14 -0
- package/dist/llm/retry.js +1 -1
- package/dist/loop/context.d.ts +21 -1
- package/dist/loop/context.js +47 -9
- package/dist/loop/events.d.ts +5 -1
- package/dist/loop/index.d.ts +2 -2
- package/dist/loop/index.js +1 -1
- package/dist/loop/interruption.d.ts +9 -4
- package/dist/loop/interruption.js +11 -25
- package/dist/loop/run-turn.d.ts +16 -3
- package/dist/loop/run-turn.js +13 -4
- package/dist/loop/steer.d.ts +78 -12
- package/dist/loop/steer.js +101 -38
- package/dist/loop/tool-call.d.ts +13 -2
- package/dist/loop/tool-call.js +135 -5
- package/dist/loop/turn-step.d.ts +13 -2
- package/dist/loop/turn-step.js +29 -7
- package/dist/loop/types.d.ts +6 -0
- package/dist/mcp/auth-tool.d.ts +3 -1
- package/dist/mcp/auth-tool.js +2 -2
- package/dist/mcp/capability.d.ts +13 -0
- package/dist/mcp/capability.js +36 -17
- package/dist/mcp/config-loader.d.ts +5 -5
- package/dist/mcp/config-loader.js +17 -17
- package/dist/mcp/credentials.d.ts +35 -0
- package/dist/mcp/credentials.js +2 -0
- package/dist/mcp/filter.d.ts +2 -2
- package/dist/mcp/index.d.ts +4 -3
- package/dist/mcp/index.js +1 -1
- package/dist/mcp/manager.d.ts +108 -6
- package/dist/mcp/manager.js +262 -18
- package/dist/mcp/oauth/callback-server.d.ts +10 -1
- package/dist/mcp/oauth/callback-server.js +82 -16
- package/dist/mcp/oauth/index.d.ts +2 -2
- package/dist/mcp/oauth/provider.d.ts +18 -0
- package/dist/mcp/oauth/provider.js +31 -1
- package/dist/mcp/oauth/service.d.ts +9 -3
- package/dist/mcp/oauth/service.js +29 -4
- package/dist/mcp/sdk-transport.js +1 -1
- package/dist/mcp/server.js +1 -1
- package/dist/permission/auto-approver/prompts-data.d.ts +1 -1
- package/dist/permission/auto-approver/prompts-data.js +1 -1
- package/dist/permission/manager.d.ts +5 -3
- package/dist/permission/manager.js +7 -3
- package/dist/permission/policies.d.ts +2 -2
- package/dist/permission/policies.js +4 -4
- package/dist/plugins/fields.d.ts +14 -0
- package/dist/plugins/fields.js +30 -0
- package/dist/plugins/hooks.d.ts +2 -2
- package/dist/plugins/hooks.js +12 -12
- package/dist/plugins/index.js +1 -0
- package/dist/plugins/manager.d.ts +8 -3
- package/dist/plugins/manager.js +18 -7
- package/dist/plugins/manifest.d.ts +2 -2
- package/dist/plugins/manifest.js +101 -91
- package/dist/plugins/marketplace.js +76 -57
- package/dist/plugins/store.d.ts +3 -3
- package/dist/plugins/store.js +6 -6
- package/dist/protocol/index.d.ts +1 -1
- package/dist/store/catalog-store.d.ts +4 -1
- package/dist/store/catalog-store.js +5 -1
- package/dist/store/checkpointing-store.d.ts +41 -0
- package/dist/store/checkpointing-store.js +86 -0
- package/dist/store/disk-store.d.ts +8 -0
- package/dist/store/disk-store.js +127 -5
- package/dist/store/index.d.ts +3 -2
- package/dist/store/index.js +2 -1
- package/dist/store/log-store.d.ts +9 -1
- package/dist/store/log-store.js +68 -4
- package/dist/store/migrate.d.ts +8 -2
- package/dist/store/migrate.js +9 -2
- package/dist/store/origin.d.ts +31 -6
- package/dist/store/origin.js +0 -14
- package/dist/store/pg.d.ts +1 -0
- package/dist/store/pg.js +37 -2
- package/dist/store/redis.d.ts +1 -0
- package/dist/store/redis.js +30 -0
- package/dist/store/repository.d.ts +13 -1
- package/dist/store/repository.js +45 -2
- package/dist/store/session-catalog.d.ts +4 -0
- package/dist/store/session-catalog.js +6 -0
- package/dist/store/storage.js +3 -0
- package/dist/store/store.d.ts +97 -34
- package/dist/store/store.js +64 -10
- package/dist/telemetry/appender.d.ts +43 -0
- package/dist/telemetry/appender.js +46 -0
- package/dist/telemetry/events.d.ts +142 -0
- package/dist/telemetry/events.js +141 -0
- package/dist/telemetry/index.d.ts +10 -0
- package/dist/telemetry/index.js +6 -0
- package/dist/telemetry/projection.d.ts +23 -0
- package/dist/telemetry/projection.js +185 -0
- package/dist/telemetry/redact.d.ts +20 -0
- package/dist/telemetry/redact.js +40 -0
- package/dist/telemetry/service.d.ts +49 -0
- package/dist/telemetry/service.js +137 -0
- package/dist/testing.d.ts +58 -0
- package/dist/testing.js +123 -0
- package/dist/tool/background.d.ts +9 -6
- package/dist/tool/background.js +7 -1
- package/dist/tool/builtin/ask-user-question.js +6 -5
- package/dist/tool/builtin/bash.d.ts +6 -0
- package/dist/tool/builtin/bash.js +79 -42
- package/dist/tool/builtin/edit.js +82 -109
- package/dist/tool/builtin/glob.js +13 -13
- package/dist/tool/builtin/grep.js +13 -13
- package/dist/tool/builtin/read.js +25 -15
- package/dist/tool/builtin/write.js +49 -33
- package/dist/tool/define.d.ts +14 -8
- package/dist/tool/define.js +8 -2
- package/dist/tool/{machine-base.d.ts → environment-base.d.ts} +37 -37
- package/dist/tool/{machine-base.js → environment-base.js} +45 -119
- package/dist/tool/{machine-local.d.ts → environment-local.d.ts} +9 -8
- package/dist/tool/{machine-local.js → environment-local.js} +60 -81
- package/dist/tool/{machine-null.d.ts → environment-null.d.ts} +9 -9
- package/dist/tool/{machine-null.js → environment-null.js} +9 -9
- package/dist/tool/{machine-ssh.d.ts → environment-ssh.d.ts} +33 -18
- package/dist/tool/{machine-ssh.js → environment-ssh.js} +118 -149
- package/dist/tool/{machine.d.ts → environment.d.ts} +53 -82
- package/dist/tool/environment.js +2 -0
- package/dist/tool/file-freshness.d.ts +33 -19
- package/dist/tool/file-freshness.js +39 -30
- package/dist/tool/filesystem-tools.d.ts +1 -1
- package/dist/tool/filesystem-tools.js +1 -1
- package/dist/tool/git-context.d.ts +2 -2
- package/dist/tool/git-context.js +8 -8
- package/dist/tool/index.d.ts +12 -12
- package/dist/tool/index.js +12 -10
- package/dist/tool/policies/path-access.d.ts +4 -4
- package/dist/tool/policies/path-access.js +12 -12
- package/dist/tool/questions.d.ts +8 -0
- package/dist/tool/search/catalog.d.ts +37 -0
- package/dist/tool/search/catalog.js +182 -0
- package/dist/tool/search/deferral.d.ts +3 -2
- package/dist/tool/search/loaded.d.ts +32 -0
- package/dist/tool/search/loaded.js +96 -0
- package/dist/tool/search/search-tool.d.ts +2 -6
- package/dist/tool/search/search-tool.js +16 -15
- package/dist/tool/shell-env.js +4 -0
- package/dist/tool/support/bash-paths.js +38 -7
- package/dist/tool/support/{machine-ops.d.ts → environment-ops.d.ts} +12 -14
- package/dist/tool/support/{machine-ops.js → environment-ops.js} +21 -2
- package/dist/tool/support/git-worktree.d.ts +3 -3
- package/dist/tool/support/git-worktree.js +6 -6
- package/dist/tool/support/rg-locator.d.ts +6 -6
- package/dist/tool/support/rg-locator.js +6 -6
- package/dist/tool/support/ripgrep-run.d.ts +5 -5
- package/dist/tool/support/ripgrep-run.js +3 -3
- package/dist/tool/support/tool-path.d.ts +3 -3
- package/dist/tool/support/tool-path.js +7 -7
- package/dist/tool/types.d.ts +54 -10
- package/dist/tool/web/fetch-url.js +1 -1
- package/dist/tool/web/web-search.js +1 -1
- package/dist/tool/workspace-spec.d.ts +3 -3
- package/dist/tool/workspace-spec.js +27 -27
- package/dist/tracing/bridge.d.ts +9 -1
- package/dist/tracing/bridge.js +307 -77
- package/dist/tracing/index.d.ts +2 -2
- package/dist/tracing/otel.d.ts +28 -1
- package/dist/tracing/otel.js +150 -38
- package/dist/tracing/processor.d.ts +22 -0
- package/dist/tracing/processor.js +2 -0
- package/dist/tracing/spans.d.ts +54 -0
- package/dist/tracing/spans.js +5 -0
- package/package.json +6 -2
- package/dist/tool/machine.js +0 -15
- package/dist/tool/search/activation.d.ts +0 -13
- package/dist/tool/search/activation.js +0 -31
package/dist/tracing/bridge.js
CHANGED
|
@@ -1,144 +1,340 @@
|
|
|
1
1
|
import { Span, Trace, newSpanId, newTraceId } from "./spans.js";
|
|
2
|
+
/** Input messages a run can carry before its trace opens; more than this is not a prompt. */
|
|
3
|
+
const PENDING_PROMPTS_MAX = 8;
|
|
4
|
+
/** Cap on the tool-failure text copied into `span.error.message`. */
|
|
5
|
+
const TOOL_ERROR_MAX_CHARS = 500;
|
|
6
|
+
/** Cap on the prompt snippet that names the root span. */
|
|
7
|
+
const PROMPT_NAME_MAX_CHARS = 120;
|
|
8
|
+
/**
|
|
9
|
+
* Subscribes to a session's `AgentEvent` stream and drives a `TracingProcessor` with a span tree:
|
|
10
|
+
* agent → turn → generation | tool | message, sub-agents nested by address. With content enabled
|
|
11
|
+
* (processor or option) the spans also carry the prompt, messages, tool args/results — see
|
|
12
|
+
* `TracingContentMode`.
|
|
13
|
+
*/
|
|
2
14
|
export function eventSinkTracingBridge(sink, processor, options = {}) {
|
|
3
15
|
const now = options.now ?? (() => Date.now());
|
|
16
|
+
const content = options.content ?? processor.content ?? "none";
|
|
17
|
+
const withContent = content !== "none";
|
|
18
|
+
/** Per session: the live traces, newest first. Index 0 is the current run while its root is open. */
|
|
4
19
|
const sessions = new Map();
|
|
5
|
-
|
|
20
|
+
/** Per session: user input appended while no run is open — the prompt of the run about to start. */
|
|
21
|
+
const promptsBeforeRun = new Map();
|
|
22
|
+
const start = (st, parent, data) => {
|
|
6
23
|
const span = new Span({ traceId: st.trace.traceId, spanId: newSpanId(now()), parentId: parent?.spanId ?? null, data });
|
|
7
24
|
span.startedAt = now();
|
|
8
25
|
void Promise.resolve(processor.onSpanStart(span)).catch(() => { });
|
|
9
26
|
return span;
|
|
10
27
|
};
|
|
28
|
+
/** Report a span as ended. Keeps an end time already fixed by `settle`. */
|
|
11
29
|
const finish = (span) => {
|
|
12
|
-
if (span === undefined
|
|
30
|
+
if (span === undefined)
|
|
13
31
|
return;
|
|
14
|
-
span.endedAt
|
|
32
|
+
if (span.endedAt === null)
|
|
33
|
+
span.endedAt = now();
|
|
15
34
|
void Promise.resolve(processor.onSpanEnd(span)).catch(() => { });
|
|
16
35
|
};
|
|
36
|
+
/** Fix a span's end time now, but report it later (`finish`). */
|
|
37
|
+
const settle = (span) => {
|
|
38
|
+
if (span.endedAt === null)
|
|
39
|
+
span.endedAt = now();
|
|
40
|
+
};
|
|
41
|
+
const flushSettledGen = (st, address) => {
|
|
42
|
+
const span = st.settledGenByAddress.get(address);
|
|
43
|
+
if (span === undefined)
|
|
44
|
+
return;
|
|
45
|
+
st.settledGenByAddress.delete(address);
|
|
46
|
+
finish(span);
|
|
47
|
+
};
|
|
48
|
+
const emitPrompt = (st, parent, pending) => {
|
|
49
|
+
const origin = pending.origin?.kind;
|
|
50
|
+
const span = start(st, parent, {
|
|
51
|
+
type: "message",
|
|
52
|
+
role: "user",
|
|
53
|
+
...(origin !== undefined ? { origin } : {}),
|
|
54
|
+
content: pending.message.content,
|
|
55
|
+
});
|
|
56
|
+
finish(span);
|
|
57
|
+
};
|
|
58
|
+
const flushPendingPrompts = (st, parent) => {
|
|
59
|
+
for (const pending of st.pendingPrompts)
|
|
60
|
+
emitPrompt(st, parent, pending);
|
|
61
|
+
st.pendingPrompts = [];
|
|
62
|
+
};
|
|
63
|
+
const isRoot = (address) => !address.includes("/");
|
|
17
64
|
const parentAddress = (address) => address.split("/").slice(0, -1).join("/");
|
|
18
65
|
const currentParent = (st, address) => st.turnByAddress.get(address) ?? st.agentByAddress.get(address) ?? null;
|
|
66
|
+
/** The trace that owns `address`: the one with an open agent span there, else the current run. */
|
|
67
|
+
const traceFor = (sessionId, address) => {
|
|
68
|
+
const traces = sessions.get(sessionId);
|
|
69
|
+
if (traces === undefined)
|
|
70
|
+
return undefined;
|
|
71
|
+
const owner = traces.find((t) => t.agentByAddress.has(address));
|
|
72
|
+
if (owner !== undefined)
|
|
73
|
+
return owner;
|
|
74
|
+
const current = traces[0];
|
|
75
|
+
return current !== undefined && !current.rootEnded ? current : undefined;
|
|
76
|
+
};
|
|
77
|
+
const endTraceIfDone = (sessionId, st) => {
|
|
78
|
+
if (!st.rootEnded || st.agentByAddress.size > 0)
|
|
79
|
+
return;
|
|
80
|
+
void Promise.resolve(processor.onTraceEnd(st.trace)).catch(() => { });
|
|
81
|
+
const traces = sessions.get(sessionId);
|
|
82
|
+
if (traces === undefined)
|
|
83
|
+
return;
|
|
84
|
+
const rest = traces.filter((t) => t !== st);
|
|
85
|
+
if (rest.length === 0)
|
|
86
|
+
sessions.delete(sessionId);
|
|
87
|
+
else
|
|
88
|
+
sessions.set(sessionId, rest);
|
|
89
|
+
};
|
|
19
90
|
const handle = (event) => {
|
|
20
91
|
const { sessionId, address } = event;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
turnByAddress: new Map(),
|
|
30
|
-
genByAddress: new Map(),
|
|
31
|
-
compactionByAddress: new Map(),
|
|
32
|
-
toolById: new Map(),
|
|
33
|
-
};
|
|
34
|
-
sessions.set(sessionId, st);
|
|
35
|
-
void Promise.resolve(processor.onTraceStart(trace)).catch(() => { });
|
|
92
|
+
if (event.type === "agent.started") {
|
|
93
|
+
if (isRoot(address)) {
|
|
94
|
+
const continued = sessions.get(sessionId)?.[0];
|
|
95
|
+
if (continued !== undefined && continued.handoffPending) {
|
|
96
|
+
// Handoff target: same run, same trace, a new top-level agent span.
|
|
97
|
+
continued.handoffPending = false;
|
|
98
|
+
continued.agentByAddress.set(address, start(continued, null, { type: "agent", name: event.agent }));
|
|
99
|
+
return;
|
|
36
100
|
}
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
101
|
+
const trace = new Trace({ traceId: newTraceId(now()), name: event.agent, groupId: sessionId });
|
|
102
|
+
// The run's input was appended (and surfaced) before this event — see Runner.run. The
|
|
103
|
+
// first prompt names the root span; the messages become spans once the turn opens.
|
|
104
|
+
const pendingPrompts = withContent ? (promptsBeforeRun.get(sessionId) ?? []) : [];
|
|
105
|
+
promptsBeforeRun.delete(sessionId);
|
|
106
|
+
const prompt = pendingPrompts.map((p) => promptSnippet(p.message.content)).find((text) => text !== undefined);
|
|
107
|
+
const st = {
|
|
108
|
+
trace,
|
|
109
|
+
root: new Span({
|
|
110
|
+
traceId: trace.traceId,
|
|
111
|
+
spanId: newSpanId(now()),
|
|
112
|
+
parentId: null,
|
|
113
|
+
data: { type: "agent", name: event.agent, ...(prompt !== undefined ? { prompt } : {}) },
|
|
114
|
+
}),
|
|
115
|
+
rootEnded: false,
|
|
116
|
+
handoffPending: false,
|
|
117
|
+
agentByAddress: new Map(),
|
|
118
|
+
turnByAddress: new Map(),
|
|
119
|
+
genByAddress: new Map(),
|
|
120
|
+
settledGenByAddress: new Map(),
|
|
121
|
+
compactionByAddress: new Map(),
|
|
122
|
+
toolById: new Map(),
|
|
123
|
+
pendingPrompts,
|
|
124
|
+
};
|
|
125
|
+
void Promise.resolve(processor.onTraceStart(trace)).catch(() => { });
|
|
126
|
+
st.root.startedAt = now();
|
|
127
|
+
void Promise.resolve(processor.onSpanStart(st.root)).catch(() => { });
|
|
128
|
+
st.agentByAddress.set(address, st.root);
|
|
129
|
+
sessions.set(sessionId, [st, ...(sessions.get(sessionId) ?? [])]);
|
|
130
|
+
return;
|
|
40
131
|
}
|
|
132
|
+
// A sub-agent nests under whoever owns its parent address — the lingering trace of a
|
|
133
|
+
// finished run when it is that run's background agent spawning its own child.
|
|
134
|
+
const st = traceFor(sessionId, parentAddress(address));
|
|
135
|
+
if (st === undefined)
|
|
136
|
+
return;
|
|
137
|
+
const parent = st.agentByAddress.get(parentAddress(address)) ?? currentParent(st, parentAddress(address));
|
|
138
|
+
st.agentByAddress.set(address, start(st, parent, { type: "agent", name: event.agent }));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const st = traceFor(sessionId, address);
|
|
142
|
+
if (st === undefined) {
|
|
143
|
+
if (withContent && event.type === "message.appended" && event.message.role === "user" && isRoot(address)) {
|
|
144
|
+
const pending = promptsBeforeRun.get(sessionId) ?? [];
|
|
145
|
+
if (pending.length < PENDING_PROMPTS_MAX)
|
|
146
|
+
pending.push({ message: event.message, ...(event.origin !== undefined ? { origin: event.origin } : {}) });
|
|
147
|
+
promptsBeforeRun.set(sessionId, pending);
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
switch (event.type) {
|
|
41
152
|
case "agent.ended": {
|
|
42
|
-
|
|
43
|
-
|
|
153
|
+
flushSettledGen(st, address);
|
|
154
|
+
if (isRoot(address))
|
|
155
|
+
flushPendingPrompts(st, st.agentByAddress.get(address) ?? null);
|
|
44
156
|
finish(st.agentByAddress.get(address));
|
|
45
157
|
st.agentByAddress.delete(address);
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
158
|
+
if (isRoot(address))
|
|
159
|
+
st.rootEnded = true;
|
|
160
|
+
endTraceIfDone(sessionId, st);
|
|
50
161
|
break;
|
|
51
162
|
}
|
|
52
163
|
case "turn.started": {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
164
|
+
const turn = start(st, st.agentByAddress.get(address) ?? null, { type: "turn", turnId: event.turnId });
|
|
165
|
+
st.turnByAddress.set(address, turn);
|
|
166
|
+
if (isRoot(address))
|
|
167
|
+
flushPendingPrompts(st, turn);
|
|
56
168
|
break;
|
|
57
169
|
}
|
|
58
170
|
case "turn.ended": {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
171
|
+
flushSettledGen(st, address);
|
|
172
|
+
const span = st.turnByAddress.get(address);
|
|
173
|
+
if (span !== undefined && span.data.type === "turn") {
|
|
174
|
+
span.data = { ...span.data, reason: event.reason, ...(event.error !== undefined ? { error: event.error } : {}) };
|
|
175
|
+
if (event.reason === "failed" && span.error === null)
|
|
176
|
+
span.error = { message: event.error ?? "turn failed" };
|
|
177
|
+
}
|
|
178
|
+
finish(span);
|
|
62
179
|
st.turnByAddress.delete(address);
|
|
63
180
|
break;
|
|
64
181
|
}
|
|
182
|
+
case "turn.paused": {
|
|
183
|
+
// The run stops here: close everything open at this address so it is exported now.
|
|
184
|
+
// The turn says why it ended; the tools that suspended are marked, not failed; and the
|
|
185
|
+
// agent span closes so the trace can end. The continuation is a run of its own.
|
|
186
|
+
flushSettledGen(st, address);
|
|
187
|
+
const turn = st.turnByAddress.get(address);
|
|
188
|
+
for (const [toolCallId, tool] of [...st.toolById]) {
|
|
189
|
+
if (turn === undefined || tool.parentId !== turn.spanId)
|
|
190
|
+
continue;
|
|
191
|
+
tool.addEvent("paused", now());
|
|
192
|
+
finish(tool);
|
|
193
|
+
st.toolById.delete(toolCallId);
|
|
194
|
+
}
|
|
195
|
+
if (turn !== undefined && turn.data.type === "turn")
|
|
196
|
+
turn.data = { ...turn.data, reason: "paused" };
|
|
197
|
+
finish(turn);
|
|
198
|
+
st.turnByAddress.delete(address);
|
|
199
|
+
const agent = st.agentByAddress.get(address);
|
|
200
|
+
agent?.addEvent("paused", now(), { pending: event.pending.length });
|
|
201
|
+
finish(agent);
|
|
202
|
+
st.agentByAddress.delete(address);
|
|
203
|
+
if (isRoot(address))
|
|
204
|
+
st.rootEnded = true;
|
|
205
|
+
endTraceIfDone(sessionId, st);
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
65
208
|
case "turn.step.started": {
|
|
66
|
-
// A model generation = one step. Open the gen span here; `
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
st.genByAddress.set(address, start(st,
|
|
209
|
+
// A model generation = one step. Open the gen span here; `model.request` fills in what
|
|
210
|
+
// was sent, `turn.step.completed` fixes its end, and the assistant `message.appended`
|
|
211
|
+
// (which follows) supplies model/usage/output and reports it.
|
|
212
|
+
flushSettledGen(st, address);
|
|
213
|
+
st.genByAddress.set(address, start(st, currentParent(st, address), { type: "generation" }));
|
|
71
214
|
break;
|
|
72
215
|
}
|
|
73
|
-
case "
|
|
74
|
-
|
|
75
|
-
// transcript-only. (Also narrows `message` to AssistantMessage.)
|
|
76
|
-
if (st === undefined || event.message.role !== "assistant")
|
|
216
|
+
case "model.request": {
|
|
217
|
+
if (!withContent)
|
|
77
218
|
break;
|
|
78
219
|
const span = st.genByAddress.get(address);
|
|
79
|
-
if (span !==
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
220
|
+
if (span === undefined || span.data.type !== "generation")
|
|
221
|
+
break;
|
|
222
|
+
const inputMode = content === "full" ? "full" : "delta";
|
|
223
|
+
span.data = {
|
|
224
|
+
...span.data,
|
|
225
|
+
...(event.system !== undefined ? { system: event.system } : {}),
|
|
226
|
+
toolNames: event.toolNames,
|
|
227
|
+
...(event.params !== undefined ? { params: event.params } : {}),
|
|
228
|
+
input: inputMode === "full" ? event.messages : unansweredMessages(event.messages),
|
|
229
|
+
inputMode,
|
|
230
|
+
};
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case "turn.step.retrying": {
|
|
234
|
+
st.genByAddress.get(address)?.addEvent("retry", now(), {
|
|
235
|
+
attempt: event.attempt,
|
|
236
|
+
maxAttempts: event.maxAttempts,
|
|
237
|
+
delayMs: event.delayMs,
|
|
238
|
+
...(event.reason !== undefined ? { reason: event.reason } : {}),
|
|
239
|
+
});
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case "turn.step.reset": {
|
|
243
|
+
st.genByAddress.get(address)?.addEvent("reset", now(), {
|
|
244
|
+
discardedAttempt: event.discardedAttempt,
|
|
245
|
+
nextAttempt: event.nextAttempt,
|
|
246
|
+
...(event.reason !== undefined ? { reason: event.reason } : {}),
|
|
247
|
+
});
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
case "message.appended": {
|
|
251
|
+
const m = event.message;
|
|
252
|
+
if (m.role === "assistant") {
|
|
253
|
+
// The assistant message IS the model generation: report the gen span with its detail.
|
|
254
|
+
const span = st.settledGenByAddress.get(address) ?? st.genByAddress.get(address);
|
|
255
|
+
st.settledGenByAddress.delete(address);
|
|
256
|
+
if (span !== undefined && span.data.type === "generation") {
|
|
257
|
+
span.data = {
|
|
258
|
+
...span.data,
|
|
259
|
+
model: m.responseModel ?? m.model,
|
|
260
|
+
...(m.responseId !== undefined ? { responseId: m.responseId } : {}),
|
|
261
|
+
stopReason: m.stopReason,
|
|
262
|
+
usage: generationUsage(m.usage),
|
|
263
|
+
...(withContent ? { output: m.content } : {}),
|
|
264
|
+
};
|
|
265
|
+
if (m.errorMessage !== undefined)
|
|
266
|
+
span.error = { message: m.errorMessage };
|
|
267
|
+
finish(span);
|
|
268
|
+
}
|
|
269
|
+
st.genByAddress.delete(address);
|
|
270
|
+
}
|
|
271
|
+
else if (m.role === "user" && withContent) {
|
|
272
|
+
// A prompt entering the transcript mid-run (a steer, a follow-up drain). Tool results
|
|
273
|
+
// are skipped: the tool span has them.
|
|
274
|
+
emitPrompt(st, currentParent(st, address), { message: m, ...(event.origin !== undefined ? { origin: event.origin } : {}) });
|
|
275
|
+
// A run whose input arrived this way (a wake run) still gets a prompt-named root.
|
|
276
|
+
if (st.root.data.type === "agent" && st.root.data.prompt === undefined && isRoot(address)) {
|
|
277
|
+
const prompt = promptSnippet(m.content);
|
|
278
|
+
if (prompt !== undefined)
|
|
279
|
+
st.root.data = { ...st.root.data, prompt };
|
|
280
|
+
}
|
|
91
281
|
}
|
|
92
|
-
st.genByAddress.delete(address);
|
|
93
282
|
break;
|
|
94
283
|
}
|
|
95
284
|
case "turn.step.completed": {
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
285
|
+
// The model call is over: fix the end time and record what the step itself knows. The
|
|
286
|
+
// assistant message (model, output) is appended right after; report then — or at the
|
|
287
|
+
// next step / turn end if it never comes.
|
|
288
|
+
const span = st.genByAddress.get(address);
|
|
100
289
|
st.genByAddress.delete(address);
|
|
290
|
+
if (span === undefined || span.data.type !== "generation")
|
|
291
|
+
break;
|
|
292
|
+
settle(span);
|
|
293
|
+
span.data = {
|
|
294
|
+
...span.data,
|
|
295
|
+
...(event.usage !== undefined ? { usage: generationUsage(event.usage) } : {}),
|
|
296
|
+
...(event.finishReason !== undefined && span.data.stopReason === undefined ? { stopReason: event.finishReason } : {}),
|
|
297
|
+
};
|
|
298
|
+
st.settledGenByAddress.set(address, span);
|
|
101
299
|
break;
|
|
102
300
|
}
|
|
103
301
|
case "tool.call.started": {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
302
|
+
st.toolById.set(event.toolCallId, start(st, currentParent(st, address), {
|
|
303
|
+
type: "tool",
|
|
304
|
+
name: event.toolName,
|
|
305
|
+
toolCallId: event.toolCallId,
|
|
306
|
+
...(withContent ? { args: event.args } : {}),
|
|
307
|
+
}));
|
|
107
308
|
break;
|
|
108
309
|
}
|
|
109
310
|
case "tool.result": {
|
|
110
|
-
if (st === undefined)
|
|
111
|
-
break;
|
|
112
311
|
const span = st.toolById.get(event.toolCallId);
|
|
113
|
-
if (span !== undefined) {
|
|
312
|
+
if (span !== undefined && span.data.type === "tool") {
|
|
114
313
|
const isError = event.result.isError === true;
|
|
115
|
-
span.data = {
|
|
314
|
+
span.data = { ...span.data, isError, ...(withContent ? { result: event.result } : {}) };
|
|
116
315
|
if (isError)
|
|
117
|
-
span.error = { message:
|
|
316
|
+
span.error = { message: toolErrorMessage(event.toolName, event.result) };
|
|
118
317
|
finish(span);
|
|
119
318
|
}
|
|
120
319
|
st.toolById.delete(event.toolCallId);
|
|
121
320
|
break;
|
|
122
321
|
}
|
|
123
322
|
case "agent.handoff": {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const span = start(st,
|
|
323
|
+
// The run continues under the target agent at a new root address: keep the trace, close
|
|
324
|
+
// the source agent span (the target's `agent.started` re-roots under the same trace).
|
|
325
|
+
const span = start(st, currentParent(st, address), { type: "handoff", from: event.from, to: event.to });
|
|
127
326
|
finish(span);
|
|
128
327
|
const fromAddress = event.fromAddress ?? address;
|
|
129
328
|
finish(st.agentByAddress.get(fromAddress));
|
|
130
329
|
st.agentByAddress.delete(fromAddress);
|
|
330
|
+
st.handoffPending = true;
|
|
131
331
|
break;
|
|
132
332
|
}
|
|
133
333
|
case "compaction.started": {
|
|
134
|
-
|
|
135
|
-
break;
|
|
136
|
-
st.compactionByAddress.set(address, start(st, address, st.agentByAddress.get(address) ?? null, { type: "compaction", trigger: event.trigger }));
|
|
334
|
+
st.compactionByAddress.set(address, start(st, st.agentByAddress.get(address) ?? null, { type: "compaction", trigger: event.trigger }));
|
|
137
335
|
break;
|
|
138
336
|
}
|
|
139
337
|
case "compaction.completed": {
|
|
140
|
-
if (st === undefined)
|
|
141
|
-
break;
|
|
142
338
|
const span = st.compactionByAddress.get(address);
|
|
143
339
|
if (span !== undefined) {
|
|
144
340
|
span.data = { type: "compaction", tokensBefore: event.tokensBefore, tokensAfter: event.tokensAfter, compactedCount: event.compactedCount };
|
|
@@ -148,8 +344,6 @@ export function eventSinkTracingBridge(sink, processor, options = {}) {
|
|
|
148
344
|
break;
|
|
149
345
|
}
|
|
150
346
|
case "error": {
|
|
151
|
-
if (st === undefined)
|
|
152
|
-
break;
|
|
153
347
|
const target = currentParent(st, address);
|
|
154
348
|
if (target !== null && target.error === null)
|
|
155
349
|
target.error = { message: event.message };
|
|
@@ -168,4 +362,40 @@ export function eventSinkTracingBridge(sink, processor, options = {}) {
|
|
|
168
362
|
}
|
|
169
363
|
});
|
|
170
364
|
}
|
|
365
|
+
/** Everything after the last assistant message: what the model is being asked to answer now. */
|
|
366
|
+
function unansweredMessages(messages) {
|
|
367
|
+
for (let i = messages.length - 1; i >= 0; i -= 1) {
|
|
368
|
+
if (messages[i]?.role === "assistant")
|
|
369
|
+
return messages.slice(i + 1);
|
|
370
|
+
}
|
|
371
|
+
return messages;
|
|
372
|
+
}
|
|
373
|
+
function generationUsage(usage) {
|
|
374
|
+
return {
|
|
375
|
+
input_tokens: usage.input,
|
|
376
|
+
output_tokens: usage.output,
|
|
377
|
+
total_tokens: usage.totalTokens,
|
|
378
|
+
cache_read_tokens: usage.cacheRead,
|
|
379
|
+
cache_write_tokens: usage.cacheWrite,
|
|
380
|
+
...(usage.reasoning !== undefined ? { reasoning_tokens: usage.reasoning } : {}),
|
|
381
|
+
...(typeof usage.cost?.total === "number" ? { cost_usd: usage.cost.total } : {}),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
function toolErrorMessage(toolName, result) {
|
|
385
|
+
const text = result.content
|
|
386
|
+
.map((part) => (part.type === "text" ? part.text : ""))
|
|
387
|
+
.join("\n")
|
|
388
|
+
.trim();
|
|
389
|
+
if (text.length === 0)
|
|
390
|
+
return `Tool ${toolName} failed`;
|
|
391
|
+
return text.length > TOOL_ERROR_MAX_CHARS ? `${text.slice(0, TOOL_ERROR_MAX_CHARS)}…` : text;
|
|
392
|
+
}
|
|
393
|
+
function promptSnippet(content) {
|
|
394
|
+
const text = (typeof content === "string" ? content : content.map((part) => (part.type === "text" ? part.text : "")).join(" "))
|
|
395
|
+
.replace(/\s+/g, " ")
|
|
396
|
+
.trim();
|
|
397
|
+
if (text.length === 0)
|
|
398
|
+
return undefined;
|
|
399
|
+
return text.length > PROMPT_NAME_MAX_CHARS ? `${text.slice(0, PROMPT_NAME_MAX_CHARS)}…` : text;
|
|
400
|
+
}
|
|
171
401
|
//# sourceMappingURL=bridge.js.map
|
package/dist/tracing/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { Span, Trace, newSpanId, newTraceId } from "./spans.ts";
|
|
2
|
-
export type { SpanData, SpanType, SpanError, SpanRecord, TraceRecord, GenerationUsage } from "./spans.ts";
|
|
2
|
+
export type { SpanData, SpanType, SpanError, SpanEvent, SpanRecord, TraceRecord, GenerationUsage, GenerationInputMode, GenerationOutputPart } from "./spans.ts";
|
|
3
3
|
export { BatchTracingProcessor, ConsoleSpanExporter, consoleTracingProcessor, noopTracingProcessor, } from "./processor.ts";
|
|
4
|
-
export type { TracingProcessor, TracingExporter, BatchTracingProcessorOptions, TraceOrSpan } from "./processor.ts";
|
|
4
|
+
export type { TracingProcessor, TracingExporter, TracingContentMode, BatchTracingProcessorOptions, TraceOrSpan } from "./processor.ts";
|
|
5
5
|
export { eventSinkTracingBridge } from "./bridge.ts";
|
|
6
6
|
export type { TracingBridgeOptions } from "./bridge.ts";
|
|
7
7
|
export { OTelTracingProcessor } from "./otel.ts";
|
package/dist/tracing/otel.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type Tracer } from "@opentelemetry/api";
|
|
2
|
+
import { type RedactOptions } from "../logging/redact.ts";
|
|
2
3
|
import type { Span, Trace } from "./spans.ts";
|
|
3
|
-
import type { TracingProcessor } from "./processor.ts";
|
|
4
|
+
import type { TracingContentMode, TracingProcessor } from "./processor.ts";
|
|
4
5
|
export interface FlushableTracerProvider {
|
|
5
6
|
forceFlush?: () => Promise<void>;
|
|
6
7
|
shutdown?: () => Promise<void>;
|
|
@@ -14,6 +15,19 @@ export interface OTelTracingProcessorOptions {
|
|
|
14
15
|
readonly includeFrameworkIds?: boolean;
|
|
15
16
|
readonly recordException?: boolean;
|
|
16
17
|
readonly shutdownProvider?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Attach conversation content (system prompt, messages, tool args/results) as span attributes.
|
|
20
|
+
* Default `"none"`. See `TracingContentMode`; the bridge reads this off the processor.
|
|
21
|
+
*/
|
|
22
|
+
readonly content?: TracingContentMode;
|
|
23
|
+
/** Per-attribute cap for serialized content; longer values are cut with a marker. Default 32 000. */
|
|
24
|
+
readonly contentMaxChars?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Run content through `redactDeep` (tokens, emails, JWTs…) before it becomes an attribute.
|
|
27
|
+
* Default `false`: this processor is for a LOCAL collector where the raw prompt is the point.
|
|
28
|
+
* Set it when the exporter leaves the machine.
|
|
29
|
+
*/
|
|
30
|
+
readonly redact?: boolean | RedactOptions;
|
|
17
31
|
}
|
|
18
32
|
export declare class OTelTracingProcessor implements TracingProcessor {
|
|
19
33
|
private readonly tracer;
|
|
@@ -22,6 +36,9 @@ export declare class OTelTracingProcessor implements TracingProcessor {
|
|
|
22
36
|
private readonly includeFrameworkIds;
|
|
23
37
|
private readonly recordException;
|
|
24
38
|
private readonly shutdownProvider;
|
|
39
|
+
readonly content: TracingContentMode;
|
|
40
|
+
private readonly contentMaxChars;
|
|
41
|
+
private readonly redactOptions;
|
|
25
42
|
private readonly activeTraces;
|
|
26
43
|
private readonly activeSpans;
|
|
27
44
|
constructor(options?: OTelTracingProcessorOptions);
|
|
@@ -33,6 +50,16 @@ export declare class OTelTracingProcessor implements TracingProcessor {
|
|
|
33
50
|
shutdown(): Promise<void>;
|
|
34
51
|
private attributesFor;
|
|
35
52
|
private genAiAttributes;
|
|
53
|
+
/**
|
|
54
|
+
* Content attributes, named after the OTel GenAI semantic conventions where one exists
|
|
55
|
+
* (`gen_ai.system_instructions`, `gen_ai.input.messages`, `gen_ai.output.messages`,
|
|
56
|
+
* `gen_ai.tool.call.arguments` / `.result`). Values are JSON strings, capped at
|
|
57
|
+
* `contentMaxChars`; images are reduced to `{ type, mimeType, bytes }`.
|
|
58
|
+
*/
|
|
59
|
+
private contentAttributes;
|
|
60
|
+
private text;
|
|
61
|
+
private json;
|
|
62
|
+
private primitiveAttributes;
|
|
36
63
|
private nameFor;
|
|
37
64
|
}
|
|
38
65
|
//# sourceMappingURL=otel.d.ts.map
|