@vercel/agent-runs 0.0.0-stage → 0.0.0

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 (129) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +77 -0
  3. package/README.md +251 -2
  4. package/dist/src/adapters/agent-transport.d.ts +9 -0
  5. package/dist/src/adapters/agent-transport.d.ts.map +1 -0
  6. package/dist/src/adapters/agent-transport.js +87 -0
  7. package/dist/src/adapters/agent-transport.js.map +1 -0
  8. package/dist/src/adapters/ai-sdk-payload.d.ts +16 -0
  9. package/dist/src/adapters/ai-sdk-payload.d.ts.map +1 -0
  10. package/dist/src/adapters/ai-sdk-payload.js +30 -0
  11. package/dist/src/adapters/ai-sdk-payload.js.map +1 -0
  12. package/dist/src/adapters/ai-sdk-telemetry.d.ts +15 -0
  13. package/dist/src/adapters/ai-sdk-telemetry.d.ts.map +1 -0
  14. package/dist/src/adapters/ai-sdk-telemetry.js +151 -0
  15. package/dist/src/adapters/ai-sdk-telemetry.js.map +1 -0
  16. package/dist/src/adapters/otel-error.d.ts +4 -0
  17. package/dist/src/adapters/otel-error.d.ts.map +1 -0
  18. package/dist/src/adapters/otel-error.js +32 -0
  19. package/dist/src/adapters/otel-error.js.map +1 -0
  20. package/dist/src/adapters/otel-ids.d.ts +20 -0
  21. package/dist/src/adapters/otel-ids.d.ts.map +1 -0
  22. package/dist/src/adapters/otel-ids.js +74 -0
  23. package/dist/src/adapters/otel-ids.js.map +1 -0
  24. package/dist/src/adapters/otel.d.ts +34 -0
  25. package/dist/src/adapters/otel.d.ts.map +1 -0
  26. package/dist/src/adapters/otel.js +147 -0
  27. package/dist/src/adapters/otel.js.map +1 -0
  28. package/dist/src/adapters/serialization.d.ts +37 -0
  29. package/dist/src/adapters/serialization.d.ts.map +1 -0
  30. package/dist/src/adapters/serialization.js +318 -0
  31. package/dist/src/adapters/serialization.js.map +1 -0
  32. package/dist/src/agent-run.d.ts +85 -0
  33. package/dist/src/agent-run.d.ts.map +1 -0
  34. package/dist/src/agent-run.js +213 -0
  35. package/dist/src/agent-run.js.map +1 -0
  36. package/dist/src/ai-sdk.d.ts +8 -0
  37. package/dist/src/ai-sdk.d.ts.map +1 -0
  38. package/dist/src/ai-sdk.js +8 -0
  39. package/dist/src/ai-sdk.js.map +1 -0
  40. package/dist/src/capture.d.ts +6 -0
  41. package/dist/src/capture.d.ts.map +1 -0
  42. package/dist/src/capture.js +9 -0
  43. package/dist/src/capture.js.map +1 -0
  44. package/dist/src/core/activation.d.ts +4 -0
  45. package/dist/src/core/activation.d.ts.map +1 -0
  46. package/dist/src/core/activation.js +31 -0
  47. package/dist/src/core/activation.js.map +1 -0
  48. package/dist/src/core/attributes.d.ts +15 -0
  49. package/dist/src/core/attributes.d.ts.map +1 -0
  50. package/dist/src/core/attributes.js +76 -0
  51. package/dist/src/core/attributes.js.map +1 -0
  52. package/dist/src/core/content-policy.d.ts +29 -0
  53. package/dist/src/core/content-policy.d.ts.map +1 -0
  54. package/dist/src/core/content-policy.js +94 -0
  55. package/dist/src/core/content-policy.js.map +1 -0
  56. package/dist/src/core/context.d.ts +4 -0
  57. package/dist/src/core/context.d.ts.map +1 -0
  58. package/dist/src/core/context.js +28 -0
  59. package/dist/src/core/context.js.map +1 -0
  60. package/dist/src/core/delegation.d.ts +14 -0
  61. package/dist/src/core/delegation.d.ts.map +1 -0
  62. package/dist/src/core/delegation.js +7 -0
  63. package/dist/src/core/delegation.js.map +1 -0
  64. package/dist/src/core/mcp.d.ts +25 -0
  65. package/dist/src/core/mcp.d.ts.map +1 -0
  66. package/dist/src/core/mcp.js +33 -0
  67. package/dist/src/core/mcp.js.map +1 -0
  68. package/dist/src/core/scopes.d.ts +60 -0
  69. package/dist/src/core/scopes.d.ts.map +1 -0
  70. package/dist/src/core/scopes.js +574 -0
  71. package/dist/src/core/scopes.js.map +1 -0
  72. package/dist/src/core/snapshot.d.ts +8 -0
  73. package/dist/src/core/snapshot.d.ts.map +1 -0
  74. package/dist/src/core/snapshot.js +207 -0
  75. package/dist/src/core/snapshot.js.map +1 -0
  76. package/dist/src/core/span-kinds.d.ts +13 -0
  77. package/dist/src/core/span-kinds.d.ts.map +1 -0
  78. package/dist/src/core/span-kinds.js +338 -0
  79. package/dist/src/core/span-kinds.js.map +1 -0
  80. package/dist/src/core/types.d.ts +298 -0
  81. package/dist/src/core/types.d.ts.map +1 -0
  82. package/dist/src/core/types.js +2 -0
  83. package/dist/src/core/types.js.map +1 -0
  84. package/dist/src/core/writer.d.ts +16 -0
  85. package/dist/src/core/writer.d.ts.map +1 -0
  86. package/dist/src/core/writer.js +77 -0
  87. package/dist/src/core/writer.js.map +1 -0
  88. package/dist/src/delegation.d.ts +3 -0
  89. package/dist/src/delegation.d.ts.map +1 -0
  90. package/dist/src/delegation.js +3 -0
  91. package/dist/src/delegation.js.map +1 -0
  92. package/dist/src/index.d.ts +7 -0
  93. package/dist/src/index.d.ts.map +1 -0
  94. package/dist/src/index.js +5 -0
  95. package/dist/src/index.js.map +1 -0
  96. package/dist/src/operations.d.ts +97 -0
  97. package/dist/src/operations.d.ts.map +1 -0
  98. package/dist/src/operations.js +185 -0
  99. package/dist/src/operations.js.map +1 -0
  100. package/dist/src/otel.d.ts +13 -0
  101. package/dist/src/otel.d.ts.map +1 -0
  102. package/dist/src/otel.js +13 -0
  103. package/dist/src/otel.js.map +1 -0
  104. package/package.json +75 -3
  105. package/src/adapters/agent-transport.ts +113 -0
  106. package/src/adapters/ai-sdk-payload.ts +44 -0
  107. package/src/adapters/ai-sdk-telemetry.ts +179 -0
  108. package/src/adapters/otel-error.ts +34 -0
  109. package/src/adapters/otel-ids.ts +82 -0
  110. package/src/adapters/otel.ts +232 -0
  111. package/src/adapters/serialization.ts +338 -0
  112. package/src/agent-run.ts +341 -0
  113. package/src/ai-sdk.ts +7 -0
  114. package/src/capture.ts +13 -0
  115. package/src/core/activation.ts +42 -0
  116. package/src/core/attributes.ts +85 -0
  117. package/src/core/content-policy.ts +116 -0
  118. package/src/core/context.ts +34 -0
  119. package/src/core/delegation.ts +20 -0
  120. package/src/core/mcp.ts +60 -0
  121. package/src/core/scopes.ts +761 -0
  122. package/src/core/snapshot.ts +245 -0
  123. package/src/core/span-kinds.ts +406 -0
  124. package/src/core/types.ts +304 -0
  125. package/src/core/writer.ts +135 -0
  126. package/src/delegation.ts +2 -0
  127. package/src/index.ts +44 -0
  128. package/src/operations.ts +342 -0
  129. package/src/otel.ts +18 -0
@@ -0,0 +1,342 @@
1
+ import type { LiveOperation } from "./core/scopes.js";
2
+ import type {
3
+ StepOptions,
4
+ TraceLink,
5
+ ScopeData,
6
+ ScopeTerminal,
7
+ ModelOptions,
8
+ ToolOptions,
9
+ ModelResult,
10
+ CaptureDecision,
11
+ Attributes,
12
+ TraceReference,
13
+ TraceErrorHandler,
14
+ } from "./core/types.js";
15
+
16
+ type Completion = ScopeTerminal & { errorType?: string; result?: ModelResult };
17
+ export interface Operation {
18
+ readonly type: ScopeData["type"];
19
+ readonly reference: TraceReference;
20
+ readonly parent?: TraceReference;
21
+ readonly startTimeMs: number;
22
+ readonly finished: boolean;
23
+ readonly capture: CaptureDecision;
24
+ run<T>(execute: () => T, ceiling?: CaptureDecision): T;
25
+ attributes(attributes: Attributes): void;
26
+ complete(result?: Completion): Promise<void>;
27
+ fail(error: unknown): Promise<void>;
28
+ /** Records a failure without ending the operation; completion keeps it. */
29
+ recordError(error: unknown, errorType?: string): void;
30
+ }
31
+ export interface WrappedOperation<I, H extends Operation> {
32
+ (input: I): Promise<H>;
33
+ <T>(
34
+ input: I & { describe?: (value: T) => Completion },
35
+ execute: (operation: H) => T | PromiseLike<T>,
36
+ ): Promise<T>;
37
+ }
38
+ type MemoryInput = Extract<ScopeData, { type: "memory" }>["options"];
39
+ export interface AttemptInput {
40
+ readonly stepIndex: number;
41
+ readonly attempt: number;
42
+ readonly runtimeContext?: StepOptions["runtimeContext"];
43
+ readonly channel?: StepOptions["channel"];
44
+ readonly links?: readonly TraceLink[];
45
+ }
46
+ export interface TurnOperation extends Operation {
47
+ attempt: WrappedOperation<AttemptInput, AttemptOperation>;
48
+ /** Returns an open attempt without starting one. Durable turns only. */
49
+ findAttempt(input: { stepIndex: number; attempt: number }): AttemptOperation | undefined;
50
+ /** Returns an open tool call from any attempt of this turn. Durable turns only. */
51
+ findTool(callId: string): ToolOperation | undefined;
52
+ /** Replaces the turn's links before its deferred span is exported. */
53
+ links(links: readonly TraceLink[]): void;
54
+ memory: WrappedOperation<MemoryInput, MemoryOperation>;
55
+ }
56
+ /**
57
+ * One tool call. In a durable turn, calling `tool` again with the same `callId`
58
+ * returns the same call and fills in details it did not have, such as `kind`,
59
+ * `arguments`, or an earlier `startTimeMs`.
60
+ */
61
+ export interface ToolInput extends ToolOptions {
62
+ readonly startTimeMs?: number;
63
+ }
64
+ export interface AttemptOperation extends Operation {
65
+ tool: WrappedOperation<ToolInput, ToolOperation>;
66
+ callSubAgent: WrappedOperation<{ callId: string; agentName: string }, ToolOperation>;
67
+ callRemoteAgent: WrappedOperation<{ callId: string; agentName: string }, ToolOperation>;
68
+ modelCall(input: ModelOptions, key?: string): Promise<ModelOperation>;
69
+ modelCall<T>(
70
+ input: ModelOptions,
71
+ execute: () => ModelCallReturn<T> | PromiseLike<ModelCallReturn<T>>,
72
+ ): Promise<T>;
73
+ modelStream<T>(
74
+ input: ModelOptions,
75
+ execute: () => ModelStreamReturn<T> | PromiseLike<ModelStreamReturn<T>>,
76
+ ): Promise<T>;
77
+ memory: WrappedOperation<MemoryInput, MemoryOperation>;
78
+ }
79
+ export interface ToolOperation extends Operation {
80
+ approval: WrappedOperation<{ requestId: string; request?: unknown }, ApprovalOperation>;
81
+ /** Returns an open approval without starting one. Durable turns only. */
82
+ findApproval(requestId: string): ApprovalOperation | undefined;
83
+ /** A tool call made while this tool runs, such as a connector's nested call. */
84
+ tool: WrappedOperation<ToolInput, ToolOperation>;
85
+ memory: WrappedOperation<MemoryInput, MemoryOperation>;
86
+ }
87
+ export type ModelOperation = Operation;
88
+ export type ApprovalOperation = Operation;
89
+ export type MemoryOperation = Operation;
90
+ export interface ModelCallReturn<T> extends ModelResult {
91
+ readonly result: T;
92
+ }
93
+ export interface ModelStreamReturn<T> {
94
+ readonly result: T;
95
+ readonly completion: PromiseLike<ModelResult>;
96
+ }
97
+
98
+ function report(
99
+ onError: TraceErrorHandler | undefined,
100
+ error: unknown,
101
+ phase: "start" | "complete",
102
+ ) {
103
+ try {
104
+ onError?.(error, { phase });
105
+ } catch {}
106
+ }
107
+ export function wrappedOperation<I, H extends Operation>(
108
+ start: (input: I) => Promise<H>,
109
+ fallback: Completion = { outcome: "completed" },
110
+ onError?: TraceErrorHandler,
111
+ ): WrappedOperation<I, H> {
112
+ function invoke(input: I): Promise<H>;
113
+ function invoke<T>(
114
+ input: I & { describe?: (value: T) => Completion },
115
+ execute: (operation: H) => T | PromiseLike<T>,
116
+ ): Promise<T>;
117
+ async function invoke<T>(
118
+ input: I,
119
+ execute?: (operation: H) => T | PromiseLike<T>,
120
+ ): Promise<H | T> {
121
+ if (execute === undefined) return start(input);
122
+ const handle = await start(input);
123
+ let completion = fallback;
124
+ try {
125
+ const value = await handle.run(() => execute(handle));
126
+ try {
127
+ completion =
128
+ (input as I & { describe?: (value: T) => Completion }).describe?.(value) ?? fallback;
129
+ } catch (error) {
130
+ report(onError, error, "complete");
131
+ }
132
+ return value;
133
+ } catch (error) {
134
+ completion = {
135
+ outcome: "failed",
136
+ errorType: error instanceof Error ? error.name : "_OTHER",
137
+ error,
138
+ };
139
+ throw error;
140
+ } finally {
141
+ try {
142
+ await handle.complete(completion);
143
+ } catch (error) {
144
+ report(onError, error, "complete");
145
+ }
146
+ }
147
+ }
148
+ return invoke;
149
+ }
150
+
151
+ export function operationHandle(
152
+ runtime: LiveOperation,
153
+ data: ScopeData,
154
+ onError?: TraceErrorHandler,
155
+ ): TurnOperation | AttemptOperation | ToolOperation | Operation {
156
+ const live = <T extends Operation>(handle: T): T =>
157
+ Object.defineProperties(handle, {
158
+ finished: { get: () => runtime.finished },
159
+ capture: { get: () => runtime.capture },
160
+ parent: { get: () => runtime.parent },
161
+ });
162
+ const common: Operation = {
163
+ type: runtime.type,
164
+ reference: runtime.reference,
165
+ get startTimeMs() {
166
+ return runtime.startTimeMs;
167
+ },
168
+ get parent() {
169
+ return runtime.parent;
170
+ },
171
+ get finished() {
172
+ return runtime.finished;
173
+ },
174
+ get capture() {
175
+ return runtime.capture;
176
+ },
177
+ run: (execute, ceiling) => runtime.run(execute, ceiling),
178
+ attributes: (attributes) => runtime.attributes(attributes),
179
+ complete: (result) => runtime.complete(result),
180
+ fail: (error) => runtime.fail(error),
181
+ recordError: (error, errorType) => runtime.error(error, errorType),
182
+ };
183
+ const child = async (
184
+ data: ScopeData,
185
+ key?: string,
186
+ options?: { links?: readonly TraceLink[]; startTimeMs?: number },
187
+ ) => operationHandle(await runtime.child(data, key, options), data, onError);
188
+ const tool = wrappedOperation(
189
+ async ({ startTimeMs, ...options }: ToolInput) =>
190
+ child({ type: "tool", options }, undefined, { startTimeMs }) as Promise<ToolOperation>,
191
+ undefined,
192
+ onError,
193
+ );
194
+ const found = <T>(operation: LiveOperation | undefined): T | undefined =>
195
+ operation === undefined
196
+ ? undefined
197
+ : (operationHandle(operation, operation.record().data, onError) as T);
198
+ const memory = wrappedOperation(
199
+ async (options: MemoryInput) => child({ type: "memory", options }),
200
+ undefined,
201
+ onError,
202
+ );
203
+ if (data.type === "activation")
204
+ return live({
205
+ ...common,
206
+ memory,
207
+ links: (links: readonly TraceLink[]) => runtime.update({ links }),
208
+ findAttempt: (input: { stepIndex: number; attempt: number }) =>
209
+ found<AttemptOperation>(
210
+ runtime.find({
211
+ type: "step",
212
+ options: { index: input.stepIndex, attempt: input.attempt },
213
+ }),
214
+ ),
215
+ findTool: (callId: string) => found<ToolOperation>(findTool(runtime, callId)),
216
+ attempt: wrappedOperation(
217
+ async (input: AttemptInput) =>
218
+ child(
219
+ {
220
+ type: "step",
221
+ options: {
222
+ index: input.stepIndex,
223
+ attempt: input.attempt,
224
+ runtimeContext: input.runtimeContext,
225
+ channel: input.channel,
226
+ },
227
+ },
228
+ undefined,
229
+ { links: input.links },
230
+ ) as Promise<AttemptOperation>,
231
+ undefined,
232
+ onError,
233
+ ),
234
+ });
235
+ if (data.type === "step") {
236
+ const delegation = (kind: string) =>
237
+ wrappedOperation(
238
+ async (input: { callId: string; agentName: string }) =>
239
+ child({
240
+ type: "tool",
241
+ options: { callId: input.callId, name: input.agentName, kind },
242
+ }) as Promise<ToolOperation>,
243
+ undefined,
244
+ onError,
245
+ );
246
+ const startModel = (options: ModelOptions, key?: string) =>
247
+ child({ type: "model", options }, key);
248
+ function modelCall(input: ModelOptions, key?: string): Promise<ModelOperation>;
249
+ function modelCall<T>(
250
+ input: ModelOptions,
251
+ execute: () => ModelCallReturn<T> | PromiseLike<ModelCallReturn<T>>,
252
+ ): Promise<T>;
253
+ async function modelCall<T>(
254
+ input: ModelOptions,
255
+ execute?: string | (() => ModelCallReturn<T> | PromiseLike<ModelCallReturn<T>>),
256
+ ): Promise<ModelOperation | T> {
257
+ if (typeof execute !== "function") return startModel(input, execute);
258
+ const handle = await startModel(input);
259
+ let result: ModelCallReturn<T>;
260
+ try {
261
+ result = await handle.run(execute);
262
+ } catch (error) {
263
+ try {
264
+ await handle.fail(error);
265
+ } catch (tracingError) {
266
+ report(onError, tracingError, "complete");
267
+ }
268
+ throw error;
269
+ }
270
+ try {
271
+ await handle.complete({ outcome: "completed", result });
272
+ } catch (error) {
273
+ report(onError, error, "complete");
274
+ }
275
+ return result.result;
276
+ }
277
+ return live({
278
+ ...common,
279
+ memory,
280
+ tool,
281
+ callSubAgent: delegation("subagent-call"),
282
+ callRemoteAgent: delegation("remote-agent-call"),
283
+ modelCall,
284
+ async modelStream(input, execute) {
285
+ const handle = await startModel(input);
286
+ try {
287
+ const value = await handle.run(execute);
288
+ const completion = Promise.resolve(value.completion)
289
+ .then(
290
+ (result) => handle.complete({ outcome: "completed", result }),
291
+ (error) => handle.fail(error),
292
+ )
293
+ .catch((error) => report(onError, error, "complete"));
294
+ runtime.waitUntil(completion);
295
+ return value.result;
296
+ } catch (error) {
297
+ try {
298
+ await handle.fail(error);
299
+ } catch (tracingError) {
300
+ report(onError, tracingError, "complete");
301
+ }
302
+ throw error;
303
+ }
304
+ },
305
+ } as AttemptOperation);
306
+ }
307
+ if (data.type === "tool")
308
+ return live({
309
+ ...common,
310
+ memory,
311
+ tool,
312
+ findApproval: (requestId: string) =>
313
+ found<ApprovalOperation>(
314
+ runtime.find({
315
+ type: "approval",
316
+ options: { requestId, callId: data.options.callId, toolName: data.options.name },
317
+ }),
318
+ ),
319
+ approval: wrappedOperation(
320
+ async (input: { requestId: string; request?: unknown }) =>
321
+ child({
322
+ type: "approval",
323
+ options: { ...input, callId: data.options.callId, toolName: data.options.name },
324
+ }),
325
+ { outcome: "ignored" },
326
+ onError,
327
+ ),
328
+ } as ToolOperation);
329
+ return common;
330
+ }
331
+
332
+ /** Tool calls can nest, so a call ID is searched through every open call of the turn. */
333
+ function findTool(parent: LiveOperation, callId: string): LiveOperation | undefined {
334
+ for (const next of parent.children) {
335
+ if (next.type === "tool" && next.callId === callId) return next;
336
+ if (next.type === "step" || next.type === "tool") {
337
+ const found = findTool(next, callId);
338
+ if (found !== undefined) return found;
339
+ }
340
+ }
341
+ return undefined;
342
+ }
package/src/otel.ts ADDED
@@ -0,0 +1,18 @@
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
+ export { activeTraceOperation, withErrorContent } from "./adapters/otel.js";
7
+ export { withCapture } from "./capture.js";
8
+ export {
9
+ boundedPrincipalId,
10
+ contentAttribute,
11
+ telemetryByteLength,
12
+ truncateTelemetryText,
13
+ CONTENT_ATTRIBUTE_LIMIT,
14
+ } from "./adapters/serialization.js";
15
+ export { withoutDeclinedContent, type ResolvedContentOptions } from "./core/content-policy.js";
16
+ export { mcpLifecycle, type McpLifecycle, type McpUpdate } from "./core/mcp.js";
17
+ export { invocationName } from "./core/span-kinds.js";
18
+ export { USAGE_FIELDS } from "./core/attributes.js";