@vercel/agent-runs 1.0.1 → 1.0.2

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.
Files changed (102) hide show
  1. package/CHANGELOG.md +3 -7
  2. package/dist/src/adapters/agent-transport.d.ts.map +1 -1
  3. package/dist/src/adapters/agent-transport.js +0 -1
  4. package/dist/src/adapters/agent-transport.js.map +1 -1
  5. package/dist/src/adapters/ai-sdk-payload.d.ts +2 -0
  6. package/dist/src/adapters/ai-sdk-payload.d.ts.map +1 -1
  7. package/dist/src/adapters/ai-sdk-payload.js +2 -0
  8. package/dist/src/adapters/ai-sdk-payload.js.map +1 -1
  9. package/dist/src/adapters/ai-sdk-telemetry.d.ts +1 -0
  10. package/dist/src/adapters/ai-sdk-telemetry.d.ts.map +1 -1
  11. package/dist/src/adapters/ai-sdk-telemetry.js +0 -1
  12. package/dist/src/adapters/ai-sdk-telemetry.js.map +1 -1
  13. package/dist/src/adapters/otel-error.d.ts +0 -1
  14. package/dist/src/adapters/otel-error.d.ts.map +1 -1
  15. package/dist/src/adapters/otel-error.js +0 -2
  16. package/dist/src/adapters/otel-error.js.map +1 -1
  17. package/dist/src/adapters/otel.d.ts +7 -0
  18. package/dist/src/adapters/otel.d.ts.map +1 -1
  19. package/dist/src/adapters/otel.js +6 -5
  20. package/dist/src/adapters/otel.js.map +1 -1
  21. package/dist/src/adapters/serialization.d.ts +3 -12
  22. package/dist/src/adapters/serialization.d.ts.map +1 -1
  23. package/dist/src/adapters/serialization.js +3 -19
  24. package/dist/src/adapters/serialization.js.map +1 -1
  25. package/dist/src/agent-run.d.ts +14 -0
  26. package/dist/src/agent-run.d.ts.map +1 -1
  27. package/dist/src/agent-run.js +4 -13
  28. package/dist/src/agent-run.js.map +1 -1
  29. package/dist/src/ai-sdk.d.ts +0 -5
  30. package/dist/src/ai-sdk.d.ts.map +1 -1
  31. package/dist/src/ai-sdk.js +0 -5
  32. package/dist/src/ai-sdk.js.map +1 -1
  33. package/dist/src/capture.d.ts +2 -0
  34. package/dist/src/capture.d.ts.map +1 -1
  35. package/dist/src/capture.js +2 -0
  36. package/dist/src/capture.js.map +1 -1
  37. package/dist/src/core/attributes.d.ts +1 -1
  38. package/dist/src/core/attributes.d.ts.map +1 -1
  39. package/dist/src/core/attributes.js +1 -1
  40. package/dist/src/core/attributes.js.map +1 -1
  41. package/dist/src/core/content-policy.d.ts +0 -21
  42. package/dist/src/core/content-policy.d.ts.map +1 -1
  43. package/dist/src/core/content-policy.js +0 -28
  44. package/dist/src/core/content-policy.js.map +1 -1
  45. package/dist/src/core/context.d.ts +0 -1
  46. package/dist/src/core/context.d.ts.map +1 -1
  47. package/dist/src/core/context.js +0 -1
  48. package/dist/src/core/context.js.map +1 -1
  49. package/dist/src/core/delegation.d.ts +3 -0
  50. package/dist/src/core/delegation.d.ts.map +1 -1
  51. package/dist/src/core/delegation.js +2 -0
  52. package/dist/src/core/delegation.js.map +1 -1
  53. package/dist/src/core/mcp.d.ts +3 -0
  54. package/dist/src/core/mcp.d.ts.map +1 -1
  55. package/dist/src/core/mcp.js +1 -0
  56. package/dist/src/core/mcp.js.map +1 -1
  57. package/dist/src/core/scopes.d.ts +0 -11
  58. package/dist/src/core/scopes.d.ts.map +1 -1
  59. package/dist/src/core/scopes.js +0 -13
  60. package/dist/src/core/scopes.js.map +1 -1
  61. package/dist/src/core/snapshot.d.ts.map +1 -1
  62. package/dist/src/core/snapshot.js +0 -21
  63. package/dist/src/core/snapshot.js.map +1 -1
  64. package/dist/src/core/span-kinds.d.ts +1 -0
  65. package/dist/src/core/span-kinds.d.ts.map +1 -1
  66. package/dist/src/core/span-kinds.js +1 -7
  67. package/dist/src/core/span-kinds.js.map +1 -1
  68. package/dist/src/core/types.d.ts +15 -1
  69. package/dist/src/core/types.d.ts.map +1 -1
  70. package/dist/src/core/writer.d.ts +0 -1
  71. package/dist/src/core/writer.d.ts.map +1 -1
  72. package/dist/src/core/writer.js.map +1 -1
  73. package/dist/src/operations.d.ts +14 -0
  74. package/dist/src/operations.d.ts.map +1 -1
  75. package/dist/src/operations.js +0 -1
  76. package/dist/src/operations.js.map +1 -1
  77. package/dist/src/otel.d.ts +0 -5
  78. package/dist/src/otel.d.ts.map +1 -1
  79. package/dist/src/otel.js +0 -5
  80. package/dist/src/otel.js.map +1 -1
  81. package/package.json +1 -1
  82. package/src/adapters/agent-transport.ts +0 -1
  83. package/src/adapters/ai-sdk-payload.ts +2 -0
  84. package/src/adapters/ai-sdk-telemetry.ts +1 -1
  85. package/src/adapters/otel-error.ts +0 -2
  86. package/src/adapters/otel.ts +7 -5
  87. package/src/adapters/serialization.ts +3 -19
  88. package/src/agent-run.ts +14 -21
  89. package/src/ai-sdk.ts +0 -5
  90. package/src/capture.ts +2 -0
  91. package/src/core/attributes.ts +1 -1
  92. package/src/core/content-policy.ts +0 -29
  93. package/src/core/context.ts +0 -1
  94. package/src/core/delegation.ts +3 -0
  95. package/src/core/mcp.ts +3 -0
  96. package/src/core/scopes.ts +0 -23
  97. package/src/core/snapshot.ts +0 -21
  98. package/src/core/span-kinds.ts +1 -7
  99. package/src/core/types.ts +15 -1
  100. package/src/core/writer.ts +0 -1
  101. package/src/operations.ts +14 -1
  102. package/src/otel.ts +0 -5
@@ -40,7 +40,6 @@ interface Construction extends Partial<ScopeRecord> {
40
40
  readonly key: string;
41
41
  readonly deferred?: boolean;
42
42
  readonly executionContext?: ExecutionContext;
43
- /** Applies sampling to a host-supplied root reference. */
44
43
  readonly sample?: boolean;
45
44
  }
46
45
  export interface ChildOptions {
@@ -49,45 +48,33 @@ export interface ChildOptions {
49
48
  }
50
49
  export interface LiveOperation extends Operation {
51
50
  child(data: ScopeData, key?: string, options?: ChildOptions): Promise<LiveOperation>;
52
- /** Returns an unsettled child with this identity, without starting one. */
53
51
  find(data: ScopeData): LiveOperation | undefined;
54
- /** Unsettled children, in start order. */
55
52
  readonly children: readonly LiveOperation[];
56
53
  usage(usage: Usage, key?: string): Promise<void>;
57
- /** Carries a model call's messages up to its turn: the first input and the latest output. */
58
54
  turnContent(content: { readonly input?: string; readonly output?: string }): void;
59
55
  error(error?: unknown, type?: string): void;
60
- /** Replaces the host context for this operation and its descendants. */
61
56
  useContext(context: ExecutionContext): void;
62
- /** Fills in a deferred operation's details from a later call with the same identity. */
63
57
  refine(data: ScopeData, startTimeMs?: number): void;
64
- /** Lowers capture for this operation and its children, and drops retained content it no longer allows. */
65
58
  narrow(capture: CaptureDecision): void;
66
- /** Completes this operation, or closes the open descendants of a finished one. */
67
59
  release(result: ScopeTerminal): Promise<void>;
68
- /** Finished, with no unfinished descendants. */
69
60
  readonly settled: boolean;
70
61
  readonly callId?: string;
71
62
  readonly mcp: McpLifecycle;
72
- /** Present on tool operations only. */
73
63
  readonly memory?: MemoryToolLifecycle;
74
64
  record(): ScopeRecord;
75
65
  }
76
66
 
77
- /** Durable trees reserve stable IDs and defer spans whose lifetime can cross processes. */
78
67
  const DEFERRED_WHEN_DURABLE: ReadonlySet<ScopeData["type"]> = new Set([
79
68
  "activation",
80
69
  "tool",
81
70
  "approval",
82
71
  ]);
83
- /** Durable work a successful attempt or turn leaves open, such as a tool awaiting approval. */
84
72
  const OUTLIVES_ATTEMPT: ReadonlySet<ScopeData["type"]> = new Set(["tool", "approval"]);
85
73
 
86
74
  function segment(value: string | number): string {
87
75
  return encodeURIComponent(String(value));
88
76
  }
89
77
 
90
- /** Children with a stable identity hydrate on re-entry; the rest are numbered. */
91
78
  function semanticKey(data: ScopeData): string | undefined {
92
79
  switch (data.type) {
93
80
  case "step":
@@ -144,7 +131,6 @@ export function createScopes(input: {
144
131
  ): TraceReference | undefined {
145
132
  const ids = telemetry.ids;
146
133
  if (!input.durable || ids === undefined) return undefined;
147
- // Re-executed model calls, decisions, and memory reads are new work and get new IDs.
148
134
  if (data.type === "model" || data.type === "decision" || data.type === "memory")
149
135
  return undefined;
150
136
  if (data.type === "activation" && parent !== undefined)
@@ -225,7 +211,6 @@ export function createScopes(input: {
225
211
  }
226
212
  const key = binding.key;
227
213
  let startTimeMs = binding.startTimeMs ?? Date.now();
228
- // A restored record keeps its saved parent; a nested activation names its own.
229
214
  const parentReference = binding.parent ?? parent?.reference;
230
215
  if (capture.emit) capture = intersectCapture(capture, telemetry.active()?.capture);
231
216
  let actualCapture = capture;
@@ -264,7 +249,6 @@ export function createScopes(input: {
264
249
  let host = binding.executionContext;
265
250
  let reference =
266
251
  binding.reference ?? reserve(data, key, parentReference, prepared, actualCapture);
267
- // A nested turn joins its parent's sampling decision; a root turn asks the sampler.
268
252
  if (binding.sample === true && reference !== undefined)
269
253
  reference = {
270
254
  ...reference,
@@ -289,7 +273,6 @@ export function createScopes(input: {
289
273
  recordOutputs: actualCapture.emit && sampled && actualCapture.recordOutputs,
290
274
  };
291
275
  actualData = capturedData(actualData, actualCapture);
292
- // A turn's messages are its steps' model calls, not ones it makes outside them, such as a summary.
293
276
  const turnContentParent = parent?.type === "step" ? parent : undefined;
294
277
  let finished = binding.finished ?? false;
295
278
  let terminalResult = binding.terminal;
@@ -305,7 +288,6 @@ export function createScopes(input: {
305
288
  void onChange();
306
289
  }
307
290
 
308
- /** Content arriving now is held to any narrower run ceiling in force. */
309
291
  function current(): CaptureDecision {
310
292
  return intersectCapture(actualCapture, telemetry.active()?.capture);
311
293
  }
@@ -373,7 +355,6 @@ export function createScopes(input: {
373
355
  if (finished) return;
374
356
  const capture = current();
375
357
  if (!capture.recordOutputs) result = { ...result, error: undefined };
376
- // A call whose work already failed cannot finish as completed.
377
358
  if (terminalResult?.failed)
378
359
  result = {
379
360
  ...result,
@@ -462,7 +443,6 @@ export function createScopes(input: {
462
443
  options: {
463
444
  ...known,
464
445
  kind: known.kind ?? next.options.kind,
465
- // Earlier arguments were recorded with consent; new ones need it now.
466
446
  arguments:
467
447
  known.arguments ??
468
448
  (current().recordInputs ? snapshotContent(next.options.arguments) : undefined),
@@ -597,7 +577,6 @@ export function createScopes(input: {
597
577
  },
598
578
  run(execute, ceiling) {
599
579
  if (finished) throw new Error("The trace scope has already finished.");
600
- // The ceiling governs work inside this run, not what the operation already recorded.
601
580
  const scoped = intersectCapture(
602
581
  intersectCapture(actualCapture, ceiling),
603
582
  telemetry.active()?.capture,
@@ -709,7 +688,6 @@ export function createScopes(input: {
709
688
  }
710
689
 
711
690
  return {
712
- /** Starts a turn tree. Durable turns reserve their IDs from `key`. */
713
691
  start(facts: {
714
692
  identity: ScopeIdentity;
715
693
  key: string;
@@ -740,7 +718,6 @@ export function createScopes(input: {
740
718
  facts.onChange ?? (() => Promise.resolve()),
741
719
  );
742
720
  },
743
- /** Rebuilds a tree from a checkpoint, or returns `undefined` for an unusable one. */
744
721
  async restore(
745
722
  snapshot: unknown,
746
723
  options: {
@@ -14,7 +14,6 @@ function byteLength(value: string): number {
14
14
  return encoder.encode(value).length;
15
15
  }
16
16
 
17
- /** Text of a serialized GenAI message's text parts, the truncatable content. */
18
17
  function messageText(parts: unknown): string {
19
18
  if (!Array.isArray(parts)) return "";
20
19
  let text = "";
@@ -27,14 +26,6 @@ function messageText(parts: unknown): string {
27
26
  return text;
28
27
  }
29
28
 
30
- /**
31
- * Shrinks a serialized GenAI message array to `budgetBytes` while keeping it
32
- * valid JSON: the newest message's text is truncated with the library's
33
- * truncation marker, mirroring how the serializer caps oversized prompts.
34
- * The cut is binary searched, so an attribute far over the budget costs a
35
- * logarithmic number of encodes instead of re-encoding a near-full candidate
36
- * at every step.
37
- */
38
29
  function fitTurnContent(input: string, budgetBytes: number): string | undefined {
39
30
  if (budgetBytes <= 0) return undefined;
40
31
  if (byteLength(input) <= budgetBytes) return input;
@@ -79,12 +70,6 @@ function fitTurnContent(input: string, budgetBytes: number): string | undefined
79
70
  return byteLength(minimal) <= budgetBytes ? minimal : undefined;
80
71
  }
81
72
 
82
- /**
83
- * Adds truncated copies of the turn's content attributes back into a stripped
84
- * checkpoint, sized so the whole record stays within the snapshot budgets. A
85
- * copy that cannot fit small enough is left out; the capture-policy attributes
86
- * stay truthful either way.
87
- */
88
73
  function withFittedTurnContent(
89
74
  attributes: Record<string, unknown>,
90
75
  original: Readonly<Record<string, unknown>>,
@@ -103,8 +88,6 @@ function withFittedTurnContent(
103
88
  );
104
89
  while (budget > 0) {
105
90
  const truncated = fitTurnContent(value, budget);
106
- // Shrinking the budget cannot help when even the minimal form does not
107
- // fit or the copy is not truncatable JSON.
108
91
  if (truncated === undefined) break;
109
92
  const candidate = { ...fitted, [key]: truncated };
110
93
  if (nodeBytes({ ...record, attributes: candidate } as ScopeRecord) <= SNAPSHOT_BYTES) {
@@ -157,7 +140,6 @@ export function boundedSerializer(
157
140
  };
158
141
  }
159
142
 
160
- /** Each node is capped on its own; settled children leave the tree. */
161
143
  function nodeBytes(record: ScopeRecord): number {
162
144
  return new TextEncoder().encode(JSON.stringify({ ...record, children: undefined })).length;
163
145
  }
@@ -330,9 +312,6 @@ export function snapshotRecord(record: ScopeRecord, serializer: ContentSerialize
330
312
  structural = { ...structural, options: { ...structural.options, runtimeContext: undefined } };
331
313
  if (structural.type === "model")
332
314
  structural = { ...structural, options: { ...structural.options, runtimeContext: undefined } };
333
- // A size-driven strip is not a capture-policy decline: the policy
334
- // attributes keep the record's real decision, and the turn's content
335
- // attributes stay as truncated copies when they fit the node budget.
336
315
  const original = record.attributes ?? {};
337
316
  const attributes = withFittedTurnContent(
338
317
  preservingCapturePolicy(withoutDeclinedContent(original, declined) ?? {}, original),
@@ -5,6 +5,7 @@ import {
5
5
  namingAttributes,
6
6
  runtimeContextAttributes,
7
7
  } from "./attributes.js";
8
+ /** Returns the invocation span name for an agent: `invoke_agent <name>`, or `invoke_agent` when the agent is anonymous. */
8
9
  export const invocationName = (name?: string) =>
9
10
  name === undefined ? "invoke_agent" : `invoke_agent ${name}`;
10
11
  const modelName = (name: string) => `chat ${name}`;
@@ -153,7 +154,6 @@ const kinds: { [K in ScopeData["type"]]: Kind<K> } = {
153
154
  },
154
155
  },
155
156
  model: {
156
- // A turn makes model calls outside its steps, such as a context compaction's summary request.
157
157
  parents: ["step", "activation"],
158
158
  prepare({ options }, context) {
159
159
  const inputMessages =
@@ -249,7 +249,6 @@ const kinds: { [K in ScopeData["type"]]: Kind<K> } = {
249
249
  attributes: {
250
250
  ...context.attributes,
251
251
  ...frameworkAttributes(context.identity.framework),
252
- // A wait the turn holds belongs to no step.
253
252
  ...(options.callId === undefined
254
253
  ? { "agent.turn.id": context.attempt.turnId }
255
254
  : attemptAttributes(context)),
@@ -283,7 +282,6 @@ const kinds: { [K in ScopeData["type"]]: Kind<K> } = {
283
282
  decision: {
284
283
  parents: ["activation", "step", "tool"],
285
284
  prepare({ options }, context) {
286
- // A decision made outside any run has no run identity to record.
287
285
  const inRun = context.identity.runId !== "";
288
286
  return {
289
287
  name: decisionName(options.modelId),
@@ -291,8 +289,6 @@ const kinds: { [K in ScopeData["type"]]: Kind<K> } = {
291
289
  attributes: {
292
290
  ...(inRun ? context.attributes : undefined),
293
291
  ...namingAttributes(decisionName(options.modelId), "decision"),
294
- // The turn's own span is written when the turn ends; the turn ID lets a running turn
295
- // claim a decision made before its first step.
296
292
  "agent.turn.id": inRun ? context.identity.turnId || undefined : undefined,
297
293
  "agent.decision.purpose": options.purpose,
298
294
  "gen_ai.agent.name": context.identity.agentName,
@@ -303,7 +299,6 @@ const kinds: { [K in ScopeData["type"]]: Kind<K> } = {
303
299
  };
304
300
  },
305
301
  complete(span, _data, result, context) {
306
- // Per-question results, refusals included, are opt-in output content.
307
302
  if (!context.capture.recordOutputs || !Array.isArray(result.output) || !result.output.length)
308
303
  return;
309
304
  applyAttributes(span, {
@@ -379,7 +374,6 @@ export function completeScope(
379
374
  serializer: ContentSerializer,
380
375
  ): void {
381
376
  const outcome = result.outcome ?? (result.failed ? "failed" : "completed");
382
- // A failure is always recorded; otherwise an unknown outcome leaves the span without one.
383
377
  const known = result.outcomeUnknown !== true || result.failed === true;
384
378
  if ((data.type === "activation" || data.type === "approval") && known)
385
379
  applyAttributes(span, {
package/src/core/types.ts CHANGED
@@ -5,6 +5,7 @@ export type AttributeValue =
5
5
  | (string | null | undefined)[]
6
6
  | (number | null | undefined)[]
7
7
  | (boolean | null | undefined)[];
8
+ /** Span attributes keyed by name. Entries with an `undefined` value are not recorded. */
8
9
  export type Attributes = Readonly<Record<string, AttributeValue | undefined>>;
9
10
  export type TraceJson =
10
11
  | null
@@ -14,6 +15,7 @@ export type TraceJson =
14
15
  | readonly TraceJson[]
15
16
  | { readonly [key: string]: TraceJson };
16
17
  declare const traceSnapshotBrand: unique symbol;
18
+ /** A JSON snapshot of an operation's trace state, as stored by a {@link TraceCheckpointer}. */
17
19
  export type TraceSnapshot = TraceJson & { readonly [traceSnapshotBrand]: true };
18
20
  export type SpanType =
19
21
  | "activation"
@@ -30,6 +32,7 @@ export type SpanKind = "INTERNAL" | "CLIENT" | "SERVER" | "PRODUCER" | "CONSUMER
30
32
  /** Host-owned process-local context. Never stored in a checkpoint. */
31
33
  export type ExecutionContext = object;
32
34
 
35
+ /** Identifies a span: its trace ID, span ID, trace flags, and optional trace state, as in W3C trace context. */
33
36
  export interface TraceReference {
34
37
  readonly traceId: string;
35
38
  readonly spanId: string;
@@ -38,24 +41,29 @@ export interface TraceReference {
38
41
  readonly tracestate?: string;
39
42
  }
40
43
 
44
+ /** Whether an operation emits spans and whether it records input and output content. Child operations cannot exceed their parent's decision. */
41
45
  export interface CaptureDecision {
42
46
  readonly emit: boolean;
43
47
  readonly recordInputs: boolean;
44
48
  readonly recordOutputs: boolean;
45
49
  }
46
50
 
51
+ /** Where a tracing failure happened: the phase, and the operation type and span when known. */
47
52
  export interface TraceErrorContext {
48
53
  readonly phase: "start" | "serialize" | "complete" | "context" | "restore" | "checkpoint";
49
54
  readonly operation?: SpanType;
50
55
  readonly reference?: TraceReference;
51
56
  }
57
+ /** Receives tracing failures with their context. Tracing errors are reported here instead of being thrown into the traced work. */
52
58
  export type TraceErrorHandler = (error: unknown, context: TraceErrorContext) => void;
53
59
 
60
+ /** A link from a span to another span, with the relationship it records. */
54
61
  export interface TraceLink {
55
62
  readonly context: TraceReference;
56
63
  readonly relationship: string;
57
64
  }
58
65
 
66
+ /** A span ready for an {@link AgentTelemetry} backend to start: its name, kind, attributes, parent, links, and start time. */
59
67
  export interface PreparedSpan {
60
68
  readonly type: SpanType;
61
69
  readonly operationId: string;
@@ -68,6 +76,7 @@ export interface PreparedSpan {
68
76
  readonly links?: readonly TraceLink[];
69
77
  }
70
78
 
79
+ /** A started span that an {@link AgentTelemetry} backend returns for recording attributes, events, errors, and status before it ends. */
71
80
  export interface SpanWriter {
72
81
  readonly reference: TraceReference;
73
82
  setAttribute(key: string, value: AttributeValue): void;
@@ -113,6 +122,7 @@ export interface TraceCheckpointer {
113
122
  delete(key: string): void | PromiseLike<void>;
114
123
  }
115
124
 
125
+ /** The agent tracing operation active in an execution context, with its span reference, capture decision, and optional MCP and memory recorders. */
116
126
  export interface ActiveOperation {
117
127
  readonly type: SpanType;
118
128
  readonly reference: TraceReference;
@@ -121,11 +131,13 @@ export interface ActiveOperation {
121
131
  readonly memory?: import("./memory.js").MemoryToolLifecycle;
122
132
  }
123
133
 
134
+ /** The operation an {@link OutputMapping} is mapping attributes for. */
124
135
  export interface MappingContext {
125
136
  readonly type: SpanType;
126
137
  readonly operationId: string;
127
138
  }
128
139
 
140
+ /** Rewrites span attributes and link attributes before they are written, for example to add backend-specific attributes. */
129
141
  export interface OutputMapping {
130
142
  attributes(span: MappingContext, attributes: Attributes): Attributes;
131
143
  link(span: MappingContext, link: TraceLink): Attributes;
@@ -142,6 +154,7 @@ export interface FrameworkIdentity {
142
154
  readonly version?: string;
143
155
  }
144
156
 
157
+ /** Cost and token usage of a model call or operation. */
145
158
  export interface Usage {
146
159
  readonly costUsd?: number;
147
160
  readonly inputTokens?: number;
@@ -159,6 +172,7 @@ export interface InputMessagesContent {
159
172
  readonly json: string;
160
173
  readonly omitted: number;
161
174
  }
175
+ /** Serializes captured payloads such as messages, instructions, tool definitions, and tool results into span attribute strings, returning `undefined` to omit one. */
162
176
  export interface ContentSerializer {
163
177
  json(value: unknown): string | undefined;
164
178
  text(value: string): string | undefined;
@@ -263,7 +277,6 @@ export interface ScopeTerminal {
263
277
  export interface ScopeRecord {
264
278
  readonly version?: 1;
265
279
  readonly finished?: boolean;
266
- /** Write token used to detect a checkpoint changed by another process. */
267
280
  readonly revision?: string;
268
281
  readonly key: string;
269
282
  readonly identity: ScopeIdentity;
@@ -299,6 +312,7 @@ export interface Operation {
299
312
  modelCall(input: ModelOptions, key?: string): Promise<Operation>;
300
313
  }
301
314
 
315
+ /** One part of model output: text, reasoning, or a tool call, result, or error. */
302
316
  export type ContentPart =
303
317
  | { readonly type: "text"; readonly text: string }
304
318
  | { readonly type: "reasoning"; readonly text: string }
@@ -15,7 +15,6 @@ export interface TraceOperation extends SpanWriter {
15
15
  readonly capture: CaptureDecision;
16
16
  readonly finished: boolean;
17
17
  run<T>(execute: () => T): T;
18
- /** Lowers what the span accepts from now on. */
19
18
  narrow(capture: CaptureDecision): void;
20
19
  }
21
20
  export function createSpanWriter(input: {
package/src/operations.ts CHANGED
@@ -14,6 +14,10 @@ import type {
14
14
  } from "./core/types.js";
15
15
 
16
16
  type Completion = ScopeTerminal & { errorType?: string; result?: ModelResult };
17
+ /**
18
+ * A handle to one traced operation, such as a turn, attempt, model call, or tool call.
19
+ * Use it to run work inside the operation's span, add attributes, and complete or fail it.
20
+ */
17
21
  export interface Operation {
18
22
  readonly type: ScopeData["type"];
19
23
  readonly reference: TraceReference;
@@ -36,6 +40,7 @@ export interface WrappedOperation<I, H extends Operation> {
36
40
  ): Promise<T>;
37
41
  }
38
42
  type MemoryInput = Extract<ScopeData, { type: "memory" }>["options"];
43
+ /** Identifies one attempt of a turn's step by step index and attempt number, with optional runtime context, channel, and links. */
39
44
  export interface AttemptInput {
40
45
  readonly stepIndex: number;
41
46
  readonly attempt: number;
@@ -43,6 +48,7 @@ export interface AttemptInput {
43
48
  readonly channel?: StepOptions["channel"];
44
49
  readonly links?: readonly TraceLink[];
45
50
  }
51
+ /** A traced agent turn. Start attempts, memory calls, and turn-level approvals under it. */
46
52
  export interface TurnOperation extends Operation {
47
53
  attempt: WrappedOperation<AttemptInput, AttemptOperation>;
48
54
  /** Returns an open attempt without starting one. Durable turns only. */
@@ -74,6 +80,7 @@ export interface ApprovalInput {
74
80
  export interface ToolInput extends ToolOptions {
75
81
  readonly startTimeMs?: number;
76
82
  }
83
+ /** A traced attempt of one turn step. Start model calls, model streams, tool calls, agent delegations, and memory calls under it. */
77
84
  export interface AttemptOperation extends Operation {
78
85
  tool: WrappedOperation<ToolInput, ToolOperation>;
79
86
  callSubAgent: WrappedOperation<{ callId: string; agentName: string }, ToolOperation>;
@@ -89,6 +96,7 @@ export interface AttemptOperation extends Operation {
89
96
  ): Promise<T>;
90
97
  memory: WrappedOperation<MemoryInput, MemoryOperation>;
91
98
  }
99
+ /** A traced tool call. Start approvals, nested tool calls, and memory calls under it. */
92
100
  export interface ToolOperation extends Operation {
93
101
  approval: WrappedOperation<ApprovalInput, ApprovalOperation>;
94
102
  /** Returns an open approval without starting one. Durable turns only. */
@@ -97,13 +105,19 @@ export interface ToolOperation extends Operation {
97
105
  tool: WrappedOperation<ToolInput, ToolOperation>;
98
106
  memory: WrappedOperation<MemoryInput, MemoryOperation>;
99
107
  }
108
+ /** A traced model call, as returned by `AttemptOperation.modelCall` without a callback. */
100
109
  export type ModelOperation = Operation;
110
+ /** A traced wait on a person, such as a tool approval or a sign-in. */
101
111
  export type ApprovalOperation = Operation;
112
+ /** A traced memory search or write. */
102
113
  export type MemoryOperation = Operation;
114
+ /** A traced call to a decision model, such as choosing a model for a turn. */
103
115
  export type DecisionOperation = Operation;
116
+ /** What a `modelCall` callback returns: the call's `result` with its finish reason, usage, and optional response metadata and content. */
104
117
  export interface ModelCallReturn<T> extends ModelResult {
105
118
  readonly result: T;
106
119
  }
120
+ /** What a `modelStream` callback returns: the stream as `result`, returned unchanged, and a `completion` promise that settles with the model result when the stream ends. */
107
121
  export interface ModelStreamReturn<T> {
108
122
  readonly result: T;
109
123
  readonly completion: PromiseLike<ModelResult>;
@@ -340,7 +354,6 @@ export function operationHandle(
340
354
  return common;
341
355
  }
342
356
 
343
- /** Tool calls can nest, so a call ID is searched through every open call of the turn. */
344
357
  function findTool(parent: LiveOperation, callId: string): LiveOperation | undefined {
345
358
  for (const next of parent.children) {
346
359
  if (next.type === "tool" && next.callId === callId) return next;
package/src/otel.ts CHANGED
@@ -1,8 +1,3 @@
1
- /**
2
- * OpenTelemetry plumbing for hosts that run their own spans beside agent
3
- * tracing: active-operation lookup, capture context, content bounds, and MCP
4
- * enrichment. Agent tracing itself needs only the package root.
5
- */
6
1
  export { activeTraceOperation, withErrorContent } from "./adapters/otel.js";
7
2
  export { withCapture } from "./capture.js";
8
3
  export {