@vercel/agent-runs 0.0.0-stage → 0.0.1
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/LICENSE +202 -0
- package/NOTICE +77 -0
- package/README.md +251 -2
- package/dist/src/adapters/agent-transport.d.ts +9 -0
- package/dist/src/adapters/agent-transport.d.ts.map +1 -0
- package/dist/src/adapters/agent-transport.js +87 -0
- package/dist/src/adapters/agent-transport.js.map +1 -0
- package/dist/src/adapters/ai-sdk-payload.d.ts +16 -0
- package/dist/src/adapters/ai-sdk-payload.d.ts.map +1 -0
- package/dist/src/adapters/ai-sdk-payload.js +30 -0
- package/dist/src/adapters/ai-sdk-payload.js.map +1 -0
- package/dist/src/adapters/ai-sdk-telemetry.d.ts +15 -0
- package/dist/src/adapters/ai-sdk-telemetry.d.ts.map +1 -0
- package/dist/src/adapters/ai-sdk-telemetry.js +151 -0
- package/dist/src/adapters/ai-sdk-telemetry.js.map +1 -0
- package/dist/src/adapters/otel-error.d.ts +4 -0
- package/dist/src/adapters/otel-error.d.ts.map +1 -0
- package/dist/src/adapters/otel-error.js +32 -0
- package/dist/src/adapters/otel-error.js.map +1 -0
- package/dist/src/adapters/otel-ids.d.ts +20 -0
- package/dist/src/adapters/otel-ids.d.ts.map +1 -0
- package/dist/src/adapters/otel-ids.js +74 -0
- package/dist/src/adapters/otel-ids.js.map +1 -0
- package/dist/src/adapters/otel.d.ts +34 -0
- package/dist/src/adapters/otel.d.ts.map +1 -0
- package/dist/src/adapters/otel.js +147 -0
- package/dist/src/adapters/otel.js.map +1 -0
- package/dist/src/adapters/serialization.d.ts +37 -0
- package/dist/src/adapters/serialization.d.ts.map +1 -0
- package/dist/src/adapters/serialization.js +318 -0
- package/dist/src/adapters/serialization.js.map +1 -0
- package/dist/src/agent-run.d.ts +85 -0
- package/dist/src/agent-run.d.ts.map +1 -0
- package/dist/src/agent-run.js +213 -0
- package/dist/src/agent-run.js.map +1 -0
- package/dist/src/ai-sdk.d.ts +8 -0
- package/dist/src/ai-sdk.d.ts.map +1 -0
- package/dist/src/ai-sdk.js +8 -0
- package/dist/src/ai-sdk.js.map +1 -0
- package/dist/src/capture.d.ts +6 -0
- package/dist/src/capture.d.ts.map +1 -0
- package/dist/src/capture.js +9 -0
- package/dist/src/capture.js.map +1 -0
- package/dist/src/core/activation.d.ts +4 -0
- package/dist/src/core/activation.d.ts.map +1 -0
- package/dist/src/core/activation.js +31 -0
- package/dist/src/core/activation.js.map +1 -0
- package/dist/src/core/attributes.d.ts +15 -0
- package/dist/src/core/attributes.d.ts.map +1 -0
- package/dist/src/core/attributes.js +76 -0
- package/dist/src/core/attributes.js.map +1 -0
- package/dist/src/core/content-policy.d.ts +29 -0
- package/dist/src/core/content-policy.d.ts.map +1 -0
- package/dist/src/core/content-policy.js +94 -0
- package/dist/src/core/content-policy.js.map +1 -0
- package/dist/src/core/context.d.ts +4 -0
- package/dist/src/core/context.d.ts.map +1 -0
- package/dist/src/core/context.js +28 -0
- package/dist/src/core/context.js.map +1 -0
- package/dist/src/core/delegation.d.ts +14 -0
- package/dist/src/core/delegation.d.ts.map +1 -0
- package/dist/src/core/delegation.js +7 -0
- package/dist/src/core/delegation.js.map +1 -0
- package/dist/src/core/mcp.d.ts +25 -0
- package/dist/src/core/mcp.d.ts.map +1 -0
- package/dist/src/core/mcp.js +33 -0
- package/dist/src/core/mcp.js.map +1 -0
- package/dist/src/core/scopes.d.ts +60 -0
- package/dist/src/core/scopes.d.ts.map +1 -0
- package/dist/src/core/scopes.js +574 -0
- package/dist/src/core/scopes.js.map +1 -0
- package/dist/src/core/snapshot.d.ts +8 -0
- package/dist/src/core/snapshot.d.ts.map +1 -0
- package/dist/src/core/snapshot.js +207 -0
- package/dist/src/core/snapshot.js.map +1 -0
- package/dist/src/core/span-kinds.d.ts +13 -0
- package/dist/src/core/span-kinds.d.ts.map +1 -0
- package/dist/src/core/span-kinds.js +338 -0
- package/dist/src/core/span-kinds.js.map +1 -0
- package/dist/src/core/types.d.ts +298 -0
- package/dist/src/core/types.d.ts.map +1 -0
- package/dist/src/core/types.js +2 -0
- package/dist/src/core/types.js.map +1 -0
- package/dist/src/core/writer.d.ts +16 -0
- package/dist/src/core/writer.d.ts.map +1 -0
- package/dist/src/core/writer.js +77 -0
- package/dist/src/core/writer.js.map +1 -0
- package/dist/src/delegation.d.ts +3 -0
- package/dist/src/delegation.d.ts.map +1 -0
- package/dist/src/delegation.js +3 -0
- package/dist/src/delegation.js.map +1 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/operations.d.ts +97 -0
- package/dist/src/operations.d.ts.map +1 -0
- package/dist/src/operations.js +185 -0
- package/dist/src/operations.js.map +1 -0
- package/dist/src/otel.d.ts +13 -0
- package/dist/src/otel.d.ts.map +1 -0
- package/dist/src/otel.js +13 -0
- package/dist/src/otel.js.map +1 -0
- package/package.json +75 -3
- package/src/adapters/agent-transport.ts +113 -0
- package/src/adapters/ai-sdk-payload.ts +44 -0
- package/src/adapters/ai-sdk-telemetry.ts +179 -0
- package/src/adapters/otel-error.ts +34 -0
- package/src/adapters/otel-ids.ts +82 -0
- package/src/adapters/otel.ts +232 -0
- package/src/adapters/serialization.ts +338 -0
- package/src/agent-run.ts +341 -0
- package/src/ai-sdk.ts +7 -0
- package/src/capture.ts +13 -0
- package/src/core/activation.ts +42 -0
- package/src/core/attributes.ts +85 -0
- package/src/core/content-policy.ts +116 -0
- package/src/core/context.ts +34 -0
- package/src/core/delegation.ts +20 -0
- package/src/core/mcp.ts +60 -0
- package/src/core/scopes.ts +761 -0
- package/src/core/snapshot.ts +245 -0
- package/src/core/span-kinds.ts +406 -0
- package/src/core/types.ts +304 -0
- package/src/core/writer.ts +135 -0
- package/src/delegation.ts +2 -0
- package/src/index.ts +44 -0
- package/src/operations.ts +342 -0
- package/src/otel.ts +18 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
export type AttributeValue =
|
|
2
|
+
| string
|
|
3
|
+
| number
|
|
4
|
+
| boolean
|
|
5
|
+
| (string | null | undefined)[]
|
|
6
|
+
| (number | null | undefined)[]
|
|
7
|
+
| (boolean | null | undefined)[];
|
|
8
|
+
export type Attributes = Readonly<Record<string, AttributeValue | undefined>>;
|
|
9
|
+
export type TraceJson =
|
|
10
|
+
| null
|
|
11
|
+
| boolean
|
|
12
|
+
| number
|
|
13
|
+
| string
|
|
14
|
+
| readonly TraceJson[]
|
|
15
|
+
| { readonly [key: string]: TraceJson };
|
|
16
|
+
declare const traceSnapshotBrand: unique symbol;
|
|
17
|
+
export type TraceSnapshot = TraceJson & { readonly [traceSnapshotBrand]: true };
|
|
18
|
+
export type SpanType =
|
|
19
|
+
| "activation"
|
|
20
|
+
| "step"
|
|
21
|
+
| "model"
|
|
22
|
+
| "tool"
|
|
23
|
+
| "approval"
|
|
24
|
+
| "memory"
|
|
25
|
+
| "channelRequest"
|
|
26
|
+
| "mcp";
|
|
27
|
+
export type SpanKind = "INTERNAL" | "CLIENT" | "SERVER" | "PRODUCER" | "CONSUMER";
|
|
28
|
+
|
|
29
|
+
/** Host-owned process-local context. Never stored in a checkpoint. */
|
|
30
|
+
export type ExecutionContext = object;
|
|
31
|
+
|
|
32
|
+
export interface TraceReference {
|
|
33
|
+
readonly traceId: string;
|
|
34
|
+
readonly spanId: string;
|
|
35
|
+
readonly traceFlags: number;
|
|
36
|
+
readonly isRemote?: boolean;
|
|
37
|
+
readonly tracestate?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface CaptureDecision {
|
|
41
|
+
readonly emit: boolean;
|
|
42
|
+
readonly recordInputs: boolean;
|
|
43
|
+
readonly recordOutputs: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface TraceErrorContext {
|
|
47
|
+
readonly phase: "start" | "serialize" | "complete" | "context" | "restore" | "checkpoint";
|
|
48
|
+
readonly operation?: SpanType;
|
|
49
|
+
readonly reference?: TraceReference;
|
|
50
|
+
}
|
|
51
|
+
export type TraceErrorHandler = (error: unknown, context: TraceErrorContext) => void;
|
|
52
|
+
|
|
53
|
+
export interface TraceLink {
|
|
54
|
+
readonly context: TraceReference;
|
|
55
|
+
readonly relationship: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface PreparedSpan {
|
|
59
|
+
readonly type: SpanType;
|
|
60
|
+
readonly operationId: string;
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly kind?: SpanKind;
|
|
63
|
+
readonly attributes: Attributes;
|
|
64
|
+
readonly parent?: TraceReference;
|
|
65
|
+
readonly root?: boolean;
|
|
66
|
+
readonly startTimeMs?: number;
|
|
67
|
+
readonly links?: readonly TraceLink[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface SpanWriter {
|
|
71
|
+
readonly reference: TraceReference;
|
|
72
|
+
setAttribute(key: string, value: AttributeValue): void;
|
|
73
|
+
addEvent(name: string, attributes?: Attributes, timeMs?: number): void;
|
|
74
|
+
fail(error?: unknown, errorType?: string): void;
|
|
75
|
+
setStatus(code: "UNSET" | "OK" | "ERROR"): void;
|
|
76
|
+
end(timeMs?: number): void;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Span output and context propagation used by agent tracing. */
|
|
80
|
+
export interface AgentTelemetry {
|
|
81
|
+
/** Starts a span. A `reference` requests that exact trace and span ID. */
|
|
82
|
+
startSpan(
|
|
83
|
+
span: PreparedSpan,
|
|
84
|
+
options?: { readonly reference?: TraceReference; readonly context?: ExecutionContext },
|
|
85
|
+
): SpanWriter;
|
|
86
|
+
/** Returns the operation active in the current execution context. */
|
|
87
|
+
active(): ActiveOperation | undefined;
|
|
88
|
+
/** Runs `execute` with `operation` as the active trace context. */
|
|
89
|
+
run<T>(operation: ActiveOperation, execute: () => T, context?: ExecutionContext): T;
|
|
90
|
+
/** Runs `execute` without creating spans. */
|
|
91
|
+
suppressed?<T>(execute: () => T): T;
|
|
92
|
+
/** Decides whether a durable turn's trace is recorded. Defaults to recording. */
|
|
93
|
+
samples?(traceId: string, span: PreparedSpan): boolean;
|
|
94
|
+
/** Derives stable IDs from keys. Present only when `startSpan` honors `reference`. */
|
|
95
|
+
readonly ids?: {
|
|
96
|
+
traceId(key: string): string;
|
|
97
|
+
spanId(key: string): string;
|
|
98
|
+
};
|
|
99
|
+
forceFlush(): Promise<void>;
|
|
100
|
+
shutdown(): Promise<void>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Durable storage for in-flight turns. Values are JSON; keys are chosen by the library. */
|
|
104
|
+
export interface TraceCheckpointer {
|
|
105
|
+
get(key: string): unknown | PromiseLike<unknown>;
|
|
106
|
+
set(key: string, value: TraceSnapshot): void | PromiseLike<void>;
|
|
107
|
+
delete(key: string): void | PromiseLike<void>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface ActiveOperation {
|
|
111
|
+
readonly type: SpanType;
|
|
112
|
+
readonly reference: TraceReference;
|
|
113
|
+
readonly capture: CaptureDecision;
|
|
114
|
+
readonly mcp?: import("./mcp.js").McpLifecycle;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface MappingContext {
|
|
118
|
+
readonly type: SpanType;
|
|
119
|
+
readonly operationId: string;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface OutputMapping {
|
|
123
|
+
attributes(span: MappingContext, attributes: Attributes): Attributes;
|
|
124
|
+
link(span: MappingContext, link: TraceLink): Attributes;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface RunIdentity {
|
|
128
|
+
readonly conversationId: string;
|
|
129
|
+
readonly runId: string;
|
|
130
|
+
readonly turnId: string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface FrameworkIdentity {
|
|
134
|
+
readonly name: string;
|
|
135
|
+
readonly version?: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface Usage {
|
|
139
|
+
readonly costUsd?: number;
|
|
140
|
+
readonly inputTokens?: number;
|
|
141
|
+
readonly outputTokens?: number;
|
|
142
|
+
readonly inputTokenDetails?: {
|
|
143
|
+
readonly cacheReadTokens?: number;
|
|
144
|
+
readonly cacheWriteTokens?: number;
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** `subagent-call` and `remote-agent-call` delegate to another agent. */
|
|
149
|
+
export type ToolKind = string;
|
|
150
|
+
/** Capped GenAI input messages and how many of the oldest were dropped to fit. */
|
|
151
|
+
export interface InputMessagesContent {
|
|
152
|
+
readonly json: string;
|
|
153
|
+
readonly omitted: number;
|
|
154
|
+
}
|
|
155
|
+
export interface ContentSerializer {
|
|
156
|
+
json(value: unknown): string | undefined;
|
|
157
|
+
text(value: string): string | undefined;
|
|
158
|
+
inputMessages(value: unknown): InputMessagesContent | undefined;
|
|
159
|
+
instructions(value: unknown): string | undefined;
|
|
160
|
+
toolDefinitions(value: unknown): string | undefined;
|
|
161
|
+
outputMessages(value: readonly ContentPart[], finishReason: string): string | undefined;
|
|
162
|
+
toolResults(value: readonly Record<string, unknown>[]): string | undefined;
|
|
163
|
+
}
|
|
164
|
+
export interface ModelResult {
|
|
165
|
+
readonly usage: Usage;
|
|
166
|
+
readonly responseId?: string;
|
|
167
|
+
readonly responseModelId?: string;
|
|
168
|
+
readonly finishReason: string;
|
|
169
|
+
readonly content?: readonly ContentPart[];
|
|
170
|
+
}
|
|
171
|
+
export interface ScopeIdentity extends RunIdentity {
|
|
172
|
+
readonly agentName?: string;
|
|
173
|
+
readonly framework?: FrameworkIdentity;
|
|
174
|
+
}
|
|
175
|
+
export interface TurnMetadata {
|
|
176
|
+
readonly attributes?: Attributes;
|
|
177
|
+
readonly sequence: number;
|
|
178
|
+
readonly subagent?: boolean;
|
|
179
|
+
readonly subagentName?: string;
|
|
180
|
+
readonly parentCallId?: string;
|
|
181
|
+
readonly parentRunId?: string;
|
|
182
|
+
readonly channel?: { kind?: string; origin?: string };
|
|
183
|
+
}
|
|
184
|
+
export interface StepOptions {
|
|
185
|
+
readonly index: number;
|
|
186
|
+
readonly attempt?: number;
|
|
187
|
+
readonly channel?: { kind?: string; origin?: string };
|
|
188
|
+
readonly runtimeContext?: Readonly<Record<string, unknown>>;
|
|
189
|
+
}
|
|
190
|
+
export interface ModelOptions {
|
|
191
|
+
readonly provider: string;
|
|
192
|
+
readonly modelId: string;
|
|
193
|
+
readonly messages?: readonly unknown[];
|
|
194
|
+
readonly instructions?: unknown;
|
|
195
|
+
/** Tools offered to the model, recorded as `gen_ai.tool.definitions`. */
|
|
196
|
+
readonly tools?: unknown;
|
|
197
|
+
readonly runtimeContext?: Readonly<Record<string, unknown>>;
|
|
198
|
+
}
|
|
199
|
+
export interface ToolOptions {
|
|
200
|
+
readonly callId: string;
|
|
201
|
+
readonly name: string;
|
|
202
|
+
readonly kind?: ToolKind;
|
|
203
|
+
readonly arguments?: unknown;
|
|
204
|
+
}
|
|
205
|
+
export type ScopeData =
|
|
206
|
+
| { readonly type: "activation"; readonly options: TurnMetadata }
|
|
207
|
+
| { readonly type: "step"; readonly options: StepOptions }
|
|
208
|
+
| { readonly type: "model"; readonly options: ModelOptions }
|
|
209
|
+
| { readonly type: "tool"; readonly options: ToolOptions }
|
|
210
|
+
| {
|
|
211
|
+
readonly type: "approval";
|
|
212
|
+
readonly options: {
|
|
213
|
+
requestId: string;
|
|
214
|
+
request?: unknown;
|
|
215
|
+
callId: string;
|
|
216
|
+
toolName: string;
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
| {
|
|
220
|
+
readonly type: "memory";
|
|
221
|
+
readonly options: {
|
|
222
|
+
operation: "search_memory" | "upsert_memory";
|
|
223
|
+
phase: string;
|
|
224
|
+
slot: string;
|
|
225
|
+
storeId: string;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
export interface ScopeTerminal {
|
|
229
|
+
readonly outcome?: string;
|
|
230
|
+
/** Ends the operation without recording an outcome, when the host never learned one. */
|
|
231
|
+
readonly outcomeUnknown?: boolean;
|
|
232
|
+
readonly failed?: boolean;
|
|
233
|
+
readonly error?: unknown;
|
|
234
|
+
readonly errorCode?: string;
|
|
235
|
+
readonly output?: unknown;
|
|
236
|
+
readonly response?: unknown;
|
|
237
|
+
readonly usage?: Usage;
|
|
238
|
+
readonly model?: ModelResult;
|
|
239
|
+
readonly recordCount?: number;
|
|
240
|
+
readonly records?: readonly { id?: string; content: string }[];
|
|
241
|
+
readonly endTimeMs?: number;
|
|
242
|
+
}
|
|
243
|
+
export interface ScopeRecord {
|
|
244
|
+
readonly version?: 1;
|
|
245
|
+
readonly finished?: boolean;
|
|
246
|
+
/** Write token used to detect a checkpoint changed by another process. */
|
|
247
|
+
readonly revision?: string;
|
|
248
|
+
readonly key: string;
|
|
249
|
+
readonly identity: ScopeIdentity;
|
|
250
|
+
readonly data: ScopeData;
|
|
251
|
+
readonly reference: TraceReference;
|
|
252
|
+
readonly parent?: TraceReference;
|
|
253
|
+
readonly attempt?: { readonly index: number; readonly attempt: number };
|
|
254
|
+
readonly capture: CaptureDecision;
|
|
255
|
+
readonly startTimeMs: number;
|
|
256
|
+
readonly links?: readonly TraceLink[];
|
|
257
|
+
readonly usage?: Usage;
|
|
258
|
+
readonly usageKeys?: readonly string[];
|
|
259
|
+
readonly attributes?: Attributes;
|
|
260
|
+
readonly childSequence?: number;
|
|
261
|
+
readonly children?: readonly ScopeRecord[];
|
|
262
|
+
readonly terminal?: ScopeTerminal;
|
|
263
|
+
}
|
|
264
|
+
export interface Operation {
|
|
265
|
+
child(data: ScopeData, key?: string): Promise<Operation>;
|
|
266
|
+
waitUntil(completion: Promise<void>): void;
|
|
267
|
+
readonly type: ScopeData["type"];
|
|
268
|
+
readonly reference: TraceReference;
|
|
269
|
+
readonly parent?: TraceReference;
|
|
270
|
+
readonly startTimeMs: number;
|
|
271
|
+
readonly capture: CaptureDecision;
|
|
272
|
+
readonly finished: boolean;
|
|
273
|
+
snapshot(): TraceSnapshot;
|
|
274
|
+
run<T>(execute: () => T, ceiling?: CaptureDecision): T;
|
|
275
|
+
attributes(attributes: Attributes): void;
|
|
276
|
+
update(input: { attributes?: Attributes; links?: readonly TraceLink[] }): void;
|
|
277
|
+
complete(result?: ScopeTerminal & { errorType?: string; result?: ModelResult }): Promise<void>;
|
|
278
|
+
fail(error: unknown): Promise<void>;
|
|
279
|
+
modelCall(input: ModelOptions, key?: string): Promise<Operation>;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export type ContentPart =
|
|
283
|
+
| { readonly type: "text"; readonly text: string }
|
|
284
|
+
| { readonly type: "reasoning"; readonly text: string }
|
|
285
|
+
| {
|
|
286
|
+
readonly type: "tool-call";
|
|
287
|
+
readonly callId: string;
|
|
288
|
+
readonly toolName: string;
|
|
289
|
+
readonly input: unknown;
|
|
290
|
+
}
|
|
291
|
+
| {
|
|
292
|
+
readonly type: "tool-result";
|
|
293
|
+
readonly callId: string;
|
|
294
|
+
readonly toolName: string;
|
|
295
|
+
readonly input: unknown;
|
|
296
|
+
readonly output: unknown;
|
|
297
|
+
}
|
|
298
|
+
| {
|
|
299
|
+
readonly type: "tool-error";
|
|
300
|
+
readonly callId: string;
|
|
301
|
+
readonly toolName: string;
|
|
302
|
+
readonly input: unknown;
|
|
303
|
+
readonly error: unknown;
|
|
304
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PreparedSpan,
|
|
3
|
+
SpanWriter,
|
|
4
|
+
ExecutionContext,
|
|
5
|
+
AgentTelemetry,
|
|
6
|
+
TraceReference,
|
|
7
|
+
CaptureDecision,
|
|
8
|
+
Attributes,
|
|
9
|
+
TraceErrorHandler,
|
|
10
|
+
} from "./types.js";
|
|
11
|
+
import { withoutDeclinedContent } from "./content-policy.js";
|
|
12
|
+
import { runTraceContext } from "./context.js";
|
|
13
|
+
import { intersectCapture } from "./activation.js";
|
|
14
|
+
export interface TraceOperation extends SpanWriter {
|
|
15
|
+
readonly capture: CaptureDecision;
|
|
16
|
+
readonly finished: boolean;
|
|
17
|
+
run<T>(execute: () => T): T;
|
|
18
|
+
/** Lowers what the span accepts from now on. */
|
|
19
|
+
narrow(capture: CaptureDecision): void;
|
|
20
|
+
}
|
|
21
|
+
export function createSpanWriter(input: {
|
|
22
|
+
telemetry: Pick<AgentTelemetry, "startSpan" | "run" | "suppressed">;
|
|
23
|
+
onError?: TraceErrorHandler;
|
|
24
|
+
}) {
|
|
25
|
+
function safely<T>(
|
|
26
|
+
execute: () => T,
|
|
27
|
+
phase: "start" | "complete" = "complete",
|
|
28
|
+
reference?: TraceReference,
|
|
29
|
+
): T | undefined {
|
|
30
|
+
try {
|
|
31
|
+
return execute();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
try {
|
|
34
|
+
input.onError?.(error, { phase, reference });
|
|
35
|
+
} catch {}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function start(
|
|
40
|
+
span: PreparedSpan,
|
|
41
|
+
capture: CaptureDecision,
|
|
42
|
+
host?: ExecutionContext,
|
|
43
|
+
reserved?: TraceReference,
|
|
44
|
+
): TraceOperation {
|
|
45
|
+
const attributes = (withoutDeclinedContent(span.attributes, capture) ??
|
|
46
|
+
span.attributes) as Attributes;
|
|
47
|
+
const writer = capture.emit
|
|
48
|
+
? safely(
|
|
49
|
+
() =>
|
|
50
|
+
input.telemetry.startSpan(
|
|
51
|
+
{ ...span, attributes },
|
|
52
|
+
{ reference: reserved, context: host },
|
|
53
|
+
),
|
|
54
|
+
"start",
|
|
55
|
+
reserved,
|
|
56
|
+
)
|
|
57
|
+
: undefined;
|
|
58
|
+
const reference = writer?.reference ?? {
|
|
59
|
+
traceId: "0".repeat(32),
|
|
60
|
+
spanId: "0".repeat(16),
|
|
61
|
+
traceFlags: 0,
|
|
62
|
+
};
|
|
63
|
+
let finished = false;
|
|
64
|
+
return {
|
|
65
|
+
reference,
|
|
66
|
+
get capture() {
|
|
67
|
+
return capture;
|
|
68
|
+
},
|
|
69
|
+
narrow(next) {
|
|
70
|
+
capture = intersectCapture(capture, next);
|
|
71
|
+
},
|
|
72
|
+
get finished() {
|
|
73
|
+
return finished;
|
|
74
|
+
},
|
|
75
|
+
setAttribute(key, value) {
|
|
76
|
+
const permitted = withoutDeclinedContent({ [key]: value }, capture);
|
|
77
|
+
if (!finished && (permitted === undefined || key in permitted))
|
|
78
|
+
safely(() =>
|
|
79
|
+
writer?.setAttribute(
|
|
80
|
+
key,
|
|
81
|
+
permitted === undefined ? value : (permitted[key] as typeof value),
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
},
|
|
85
|
+
addEvent(name, attributes, timeMs) {
|
|
86
|
+
if (!finished)
|
|
87
|
+
safely(() =>
|
|
88
|
+
writer?.addEvent(
|
|
89
|
+
name,
|
|
90
|
+
attributes === undefined
|
|
91
|
+
? undefined
|
|
92
|
+
: ((withoutDeclinedContent(attributes, capture) ?? attributes) as Attributes),
|
|
93
|
+
timeMs,
|
|
94
|
+
),
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
fail(error, type) {
|
|
98
|
+
if (!finished)
|
|
99
|
+
safely(() =>
|
|
100
|
+
writer?.fail(
|
|
101
|
+
capture.recordOutputs ? error : undefined,
|
|
102
|
+
type ?? (error instanceof Error ? error.name : undefined),
|
|
103
|
+
),
|
|
104
|
+
);
|
|
105
|
+
},
|
|
106
|
+
setStatus(code) {
|
|
107
|
+
if (!finished) safely(() => writer?.setStatus(code));
|
|
108
|
+
},
|
|
109
|
+
end(timeMs) {
|
|
110
|
+
if (finished) return;
|
|
111
|
+
finished = true;
|
|
112
|
+
safely(() => writer?.end(timeMs));
|
|
113
|
+
},
|
|
114
|
+
run(execute) {
|
|
115
|
+
return finished
|
|
116
|
+
? execute()
|
|
117
|
+
: runTraceContext(
|
|
118
|
+
input.telemetry,
|
|
119
|
+
{ type: span.type, reference, capture },
|
|
120
|
+
execute,
|
|
121
|
+
host,
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
start,
|
|
128
|
+
startReserved: (
|
|
129
|
+
span: PreparedSpan,
|
|
130
|
+
reference: TraceReference,
|
|
131
|
+
capture: CaptureDecision,
|
|
132
|
+
host?: ExecutionContext,
|
|
133
|
+
) => start(span, capture, host, reference),
|
|
134
|
+
};
|
|
135
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createAgentRun,
|
|
3
|
+
type AgentRun,
|
|
4
|
+
type AgentRunOptions,
|
|
5
|
+
type AgentMemoryTracing,
|
|
6
|
+
type TurnInput,
|
|
7
|
+
type ResumeInput,
|
|
8
|
+
} from "./agent-run.js";
|
|
9
|
+
export { otelTelemetry, type OtelTelemetryOptions } from "./adapters/otel.js";
|
|
10
|
+
export { AgentSpanIdGenerator } from "./adapters/otel-ids.js";
|
|
11
|
+
export type {
|
|
12
|
+
Operation,
|
|
13
|
+
TurnOperation,
|
|
14
|
+
AttemptInput,
|
|
15
|
+
AttemptOperation,
|
|
16
|
+
ToolInput,
|
|
17
|
+
ModelOperation,
|
|
18
|
+
ToolOperation,
|
|
19
|
+
ApprovalOperation,
|
|
20
|
+
MemoryOperation,
|
|
21
|
+
ModelCallReturn,
|
|
22
|
+
ModelStreamReturn,
|
|
23
|
+
} from "./operations.js";
|
|
24
|
+
export type {
|
|
25
|
+
ActiveOperation,
|
|
26
|
+
AgentTelemetry,
|
|
27
|
+
Attributes,
|
|
28
|
+
CaptureDecision,
|
|
29
|
+
ContentPart,
|
|
30
|
+
ContentSerializer,
|
|
31
|
+
ExecutionContext,
|
|
32
|
+
MappingContext,
|
|
33
|
+
OutputMapping,
|
|
34
|
+
PreparedSpan,
|
|
35
|
+
SpanWriter,
|
|
36
|
+
TraceCheckpointer,
|
|
37
|
+
TraceErrorContext,
|
|
38
|
+
TraceErrorHandler,
|
|
39
|
+
TraceLink,
|
|
40
|
+
TraceReference,
|
|
41
|
+
TraceSnapshot,
|
|
42
|
+
Usage,
|
|
43
|
+
} from "./core/types.js";
|
|
44
|
+
export { currentCapture } from "./capture.js";
|