@narumitw/pi-otel 0.0.0-stage → 0.55.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.
- package/CHANGELOG.md +170 -0
- package/LICENSE +21 -0
- package/README.md +371 -2
- package/dist/chunks/chunk-IXR5MVGN.ts +156 -0
- package/dist/chunks/chunk-IXR5MVGN.ts.map +7 -0
- package/dist/chunks/runtime-KWIFBNQ2.ts +404 -0
- package/dist/chunks/runtime-KWIFBNQ2.ts.map +7 -0
- package/dist/config.d.ts +36 -0
- package/dist/durable-projection.d.ts +9 -0
- package/dist/durable-recorder.d.ts +57 -0
- package/dist/durable-state.d.ts +43 -0
- package/dist/durable.d.ts +25 -0
- package/dist/durable.js +897 -0
- package/dist/durable.js.map +7 -0
- package/dist/git.d.ts +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1736 -0
- package/dist/index.js.map +7 -0
- package/dist/index.ts +1878 -0
- package/dist/index.ts.map +7 -0
- package/dist/library-chunks/chunk-BBL32ZV5.js +84 -0
- package/dist/library-chunks/chunk-BBL32ZV5.js.map +7 -0
- package/dist/library-chunks/chunk-ROM6FIM7.js +78 -0
- package/dist/library-chunks/chunk-ROM6FIM7.js.map +7 -0
- package/dist/library-chunks/chunk-ZCEZBO2T.js +158 -0
- package/dist/library-chunks/chunk-ZCEZBO2T.js.map +7 -0
- package/dist/library-chunks/runtime-7TSAKORH.js +406 -0
- package/dist/library-chunks/runtime-7TSAKORH.js.map +7 -0
- package/dist/otel.d.ts +13 -0
- package/dist/otlp-settings.d.ts +9 -0
- package/dist/pi-session.d.ts +43 -0
- package/dist/public-api.d.ts +2 -0
- package/dist/runtime-api.d.ts +6 -0
- package/dist/runtime-core.d.ts +26 -0
- package/dist/runtime-defaults.d.ts +1 -0
- package/dist/runtime.d.ts +26 -0
- package/dist/sanitizer.d.ts +2 -0
- package/dist/tracing.d.ts +199 -0
- package/docs/durable.md +128 -0
- package/package.json +87 -4
- package/src/config.ts +330 -0
- package/src/durable-projection.ts +112 -0
- package/src/durable-recorder.ts +539 -0
- package/src/durable-state.ts +135 -0
- package/src/durable.ts +364 -0
- package/src/git.ts +64 -0
- package/src/index.ts +12 -0
- package/src/otel.ts +397 -0
- package/src/otlp-settings.ts +80 -0
- package/src/pi-session.ts +570 -0
- package/src/public-api.ts +13 -0
- package/src/runtime-api.ts +16 -0
- package/src/runtime-core.ts +114 -0
- package/src/runtime-defaults.ts +1 -0
- package/src/runtime.ts +509 -0
- package/src/sanitizer.ts +157 -0
- package/src/tracing.ts +946 -0
package/src/tracing.ts
ADDED
|
@@ -0,0 +1,946 @@
|
|
|
1
|
+
import { sanitizeTraceValue } from "./sanitizer.js";
|
|
2
|
+
|
|
3
|
+
export { MAX_CAPTURE_BYTES, sanitizeTraceValue } from "./sanitizer.js";
|
|
4
|
+
|
|
5
|
+
export const TRACE_SCHEMA_VERSION = "2";
|
|
6
|
+
const MAX_HEADER_VALUE_LENGTH = 1_024;
|
|
7
|
+
const CONTENT_DISABLED = "[content capture disabled]";
|
|
8
|
+
const ALLOWED_RESPONSE_HEADERS = new Set([
|
|
9
|
+
"anthropic-ratelimit-requests-limit",
|
|
10
|
+
"anthropic-ratelimit-requests-remaining",
|
|
11
|
+
"anthropic-ratelimit-requests-reset",
|
|
12
|
+
"anthropic-ratelimit-tokens-limit",
|
|
13
|
+
"anthropic-ratelimit-tokens-remaining",
|
|
14
|
+
"anthropic-ratelimit-tokens-reset",
|
|
15
|
+
"cf-ray",
|
|
16
|
+
"openai-request-id",
|
|
17
|
+
"request-id",
|
|
18
|
+
"retry-after",
|
|
19
|
+
"x-amzn-requestid",
|
|
20
|
+
"x-request-id",
|
|
21
|
+
"x-ratelimit-limit-requests",
|
|
22
|
+
"x-ratelimit-limit-tokens",
|
|
23
|
+
"x-ratelimit-remaining-requests",
|
|
24
|
+
"x-ratelimit-remaining-tokens",
|
|
25
|
+
"x-ratelimit-reset-requests",
|
|
26
|
+
"x-ratelimit-reset-tokens",
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
export interface ObservationAttributes {
|
|
30
|
+
input?: unknown;
|
|
31
|
+
output?: unknown;
|
|
32
|
+
metadata?: Record<string, unknown>;
|
|
33
|
+
level?: "DEBUG" | "DEFAULT" | "WARNING" | "ERROR";
|
|
34
|
+
statusMessage?: string;
|
|
35
|
+
completionStartTime?: Date;
|
|
36
|
+
model?: string;
|
|
37
|
+
modelParameters?: Record<string, string | number>;
|
|
38
|
+
usageDetails?: Record<string, number>;
|
|
39
|
+
costDetails?: Record<string, number>;
|
|
40
|
+
version?: string;
|
|
41
|
+
name?: string;
|
|
42
|
+
sessionId?: string;
|
|
43
|
+
userId?: string;
|
|
44
|
+
tags?: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface Observation {
|
|
48
|
+
readonly traceId?: string;
|
|
49
|
+
update(attributes: ObservationAttributes): Observation;
|
|
50
|
+
updateTrace?(attributes: ObservationAttributes): Observation;
|
|
51
|
+
end(endTime?: number): Observation;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type ObservationType = "agent" | "generation" | "span" | "tool";
|
|
55
|
+
|
|
56
|
+
export interface TraceBackend {
|
|
57
|
+
start(
|
|
58
|
+
name: string,
|
|
59
|
+
attributes: ObservationAttributes,
|
|
60
|
+
options: { asType: ObservationType; parent?: Observation; startTime?: Date },
|
|
61
|
+
): Observation;
|
|
62
|
+
forceFlush(): Promise<void>;
|
|
63
|
+
shutdown(): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface TraceRecorderOptions {
|
|
67
|
+
sessionId: string;
|
|
68
|
+
userId?: string;
|
|
69
|
+
cwd: string;
|
|
70
|
+
mode: string;
|
|
71
|
+
captureContent: boolean;
|
|
72
|
+
traceName?: string;
|
|
73
|
+
tags?: readonly string[];
|
|
74
|
+
metadata?: Readonly<Record<string, unknown>>;
|
|
75
|
+
onTraceId?: (traceId: string) => void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface ModelDescriptor {
|
|
79
|
+
provider?: string;
|
|
80
|
+
id?: string;
|
|
81
|
+
api?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface GitMetadata {
|
|
85
|
+
branch?: string;
|
|
86
|
+
commit?: string;
|
|
87
|
+
detached: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface ContextSnapshot {
|
|
91
|
+
leafId?: string | null;
|
|
92
|
+
contextUsage?: {
|
|
93
|
+
tokens: number | null;
|
|
94
|
+
contextWindow: number;
|
|
95
|
+
percent: number | null;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface BeginAgentInput {
|
|
100
|
+
prompt: unknown;
|
|
101
|
+
images?: unknown;
|
|
102
|
+
model?: ModelDescriptor;
|
|
103
|
+
git?: GitMetadata;
|
|
104
|
+
snapshot?: ContextSnapshot;
|
|
105
|
+
requestId?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
interface AttemptInput {
|
|
109
|
+
reason?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
interface GenerationInput {
|
|
113
|
+
startedAt?: number;
|
|
114
|
+
payload?: unknown;
|
|
115
|
+
payloadStage?: "before_provider_request";
|
|
116
|
+
model?: ModelDescriptor;
|
|
117
|
+
thinkingLevel?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface UsageCost {
|
|
121
|
+
input?: number;
|
|
122
|
+
output?: number;
|
|
123
|
+
cacheRead?: number;
|
|
124
|
+
cacheWrite?: number;
|
|
125
|
+
total?: number;
|
|
126
|
+
totalFromProvider?: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
interface AssistantMessage {
|
|
130
|
+
role: string;
|
|
131
|
+
content?: unknown;
|
|
132
|
+
provider?: string;
|
|
133
|
+
api?: string;
|
|
134
|
+
model?: string;
|
|
135
|
+
responseModel?: string;
|
|
136
|
+
responseId?: string;
|
|
137
|
+
usage?: {
|
|
138
|
+
input?: number;
|
|
139
|
+
output?: number;
|
|
140
|
+
cacheRead?: number;
|
|
141
|
+
cacheWrite?: number;
|
|
142
|
+
cacheWrite1h?: number;
|
|
143
|
+
reasoning?: number;
|
|
144
|
+
totalTokens?: number;
|
|
145
|
+
cost?: UsageCost;
|
|
146
|
+
};
|
|
147
|
+
stopReason?: string;
|
|
148
|
+
errorMessage?: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface ToolResult {
|
|
152
|
+
content?: unknown;
|
|
153
|
+
details?: unknown;
|
|
154
|
+
isError?: boolean;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface TurnResult {
|
|
158
|
+
message: { role: string; stopReason?: string; errorMessage?: string };
|
|
159
|
+
toolResultCount: number;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface CompactionStart {
|
|
163
|
+
reason: string;
|
|
164
|
+
willRetry: boolean;
|
|
165
|
+
tokensBefore?: number;
|
|
166
|
+
messagesToSummarize: number;
|
|
167
|
+
turnPrefixMessages: number;
|
|
168
|
+
branchEntries: number;
|
|
169
|
+
isSplitTurn: boolean;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface CompactionFinish {
|
|
173
|
+
reason: string;
|
|
174
|
+
willRetry: boolean;
|
|
175
|
+
fromExtension: boolean;
|
|
176
|
+
tokensBefore?: number;
|
|
177
|
+
details?: unknown;
|
|
178
|
+
usage?: {
|
|
179
|
+
input?: number;
|
|
180
|
+
output?: number;
|
|
181
|
+
cacheRead?: number;
|
|
182
|
+
cacheWrite?: number;
|
|
183
|
+
totalTokens?: number;
|
|
184
|
+
cost?: UsageCost;
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type Outcome = "success" | "error" | "aborted" | "length" | "interrupted";
|
|
189
|
+
|
|
190
|
+
interface GenerationState {
|
|
191
|
+
observation: Observation;
|
|
192
|
+
endTime?: number;
|
|
193
|
+
completionStartTime?: number;
|
|
194
|
+
requestMetadata: Record<string, unknown>;
|
|
195
|
+
statuses: number[];
|
|
196
|
+
headers: Record<string, string>;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
interface ToolState {
|
|
200
|
+
observation: Observation;
|
|
201
|
+
startedAt: number;
|
|
202
|
+
progressUpdates: number;
|
|
203
|
+
firstProgressAt?: number;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface CompactionState {
|
|
207
|
+
observation: Observation;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
interface Counters {
|
|
211
|
+
attempts: number;
|
|
212
|
+
turns: number;
|
|
213
|
+
generations: number;
|
|
214
|
+
tools: number;
|
|
215
|
+
toolErrors: number;
|
|
216
|
+
compactions: number;
|
|
217
|
+
recoveredErrors: number;
|
|
218
|
+
failedAttempts: number;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export class TraceRecorder {
|
|
222
|
+
private root: Observation | undefined;
|
|
223
|
+
private attempt: Observation | undefined;
|
|
224
|
+
private attemptIndex: number | undefined;
|
|
225
|
+
private turn: Observation | undefined;
|
|
226
|
+
private turnIndex: number | undefined;
|
|
227
|
+
private generation: GenerationState | undefined;
|
|
228
|
+
private readonly tools = new Map<string, ToolState>();
|
|
229
|
+
private readonly duplicateToolIds = new Set<string>();
|
|
230
|
+
private compaction: CompactionState | undefined;
|
|
231
|
+
private lastOutput: unknown;
|
|
232
|
+
private lastAssistant: AssistantMessage | undefined;
|
|
233
|
+
private lastAttemptOutcome: Outcome | undefined;
|
|
234
|
+
private unresolvedToolErrors = 0;
|
|
235
|
+
private startSnapshot: ContextSnapshot | undefined;
|
|
236
|
+
private counters: Counters = emptyCounters();
|
|
237
|
+
|
|
238
|
+
constructor(
|
|
239
|
+
private readonly backend: TraceBackend,
|
|
240
|
+
private readonly context: TraceRecorderOptions,
|
|
241
|
+
) {}
|
|
242
|
+
|
|
243
|
+
private startObservation(
|
|
244
|
+
name: string,
|
|
245
|
+
attributes: ObservationAttributes,
|
|
246
|
+
options: { asType: ObservationType; parent?: Observation; startTime?: Date },
|
|
247
|
+
): Observation {
|
|
248
|
+
const { sessionId, userId } = this.context;
|
|
249
|
+
return this.backend.start(name, { ...attributes, sessionId, ...(userId ? { userId } : {}) }, options);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
hasActiveTrace(): boolean {
|
|
253
|
+
return this.root !== undefined;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
hasActiveAttempt(): boolean {
|
|
257
|
+
return this.attempt !== undefined;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
beginAgent(input: BeginAgentInput): void {
|
|
261
|
+
if (this.root) this.closeActiveTrace("Interrupted by a new Pi run.", undefined, "interrupted");
|
|
262
|
+
|
|
263
|
+
this.lastOutput = undefined;
|
|
264
|
+
this.lastAssistant = undefined;
|
|
265
|
+
this.lastAttemptOutcome = undefined;
|
|
266
|
+
this.unresolvedToolErrors = 0;
|
|
267
|
+
this.duplicateToolIds.clear();
|
|
268
|
+
this.startSnapshot = input.snapshot;
|
|
269
|
+
this.counters = emptyCounters();
|
|
270
|
+
const traceInput = this.capture({
|
|
271
|
+
prompt: input.prompt,
|
|
272
|
+
...(hasItems(input.images) ? { images: input.images } : {}),
|
|
273
|
+
});
|
|
274
|
+
const metadata: Record<string, unknown> = {
|
|
275
|
+
...customTraceMetadata(this.context.metadata),
|
|
276
|
+
"pi.cwd": this.context.cwd,
|
|
277
|
+
...(input.git?.branch ? { "pi.git.branch": input.git.branch } : {}),
|
|
278
|
+
...(input.git?.commit ? { "pi.git.commit": input.git.commit } : {}),
|
|
279
|
+
...(input.git ? { "pi.git.detached": input.git.detached } : {}),
|
|
280
|
+
"pi.mode": this.context.mode,
|
|
281
|
+
...(input.model?.id ? { "pi.model": input.model.id } : {}),
|
|
282
|
+
...(input.model?.provider ? { "pi.provider": input.model.provider } : {}),
|
|
283
|
+
...(input.requestId ? { "pi.request.id": input.requestId } : {}),
|
|
284
|
+
"pi.session.id": this.context.sessionId,
|
|
285
|
+
"pi.trace.schema_version": Number(TRACE_SCHEMA_VERSION),
|
|
286
|
+
...snapshotMetadata("start", input.snapshot),
|
|
287
|
+
};
|
|
288
|
+
const attributes = { input: traceInput, metadata, version: TRACE_SCHEMA_VERSION };
|
|
289
|
+
const gitTag = input.git?.branch ? `branch:${input.git.branch}` : input.git?.detached ? "git:detached" : undefined;
|
|
290
|
+
|
|
291
|
+
this.root = this.startObservation("pi.agent", attributes, { asType: "agent" });
|
|
292
|
+
this.root.updateTrace?.({
|
|
293
|
+
name: normalizeTraceName(this.context.traceName),
|
|
294
|
+
sessionId: this.context.sessionId,
|
|
295
|
+
...(this.context.userId ? { userId: this.context.userId } : {}),
|
|
296
|
+
version: TRACE_SCHEMA_VERSION,
|
|
297
|
+
input: traceInput,
|
|
298
|
+
metadata,
|
|
299
|
+
tags: traceTags(this.context.tags, gitTag),
|
|
300
|
+
});
|
|
301
|
+
const traceId = this.root.traceId;
|
|
302
|
+
if (traceId && this.context.onTraceId) {
|
|
303
|
+
try {
|
|
304
|
+
void Promise.resolve(this.context.onTraceId(traceId)).catch(() => undefined);
|
|
305
|
+
} catch {
|
|
306
|
+
// Host callbacks must not corrupt observation lifecycle state.
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
beginAttempt(input: AttemptInput = {}): void {
|
|
312
|
+
if (!this.root) return;
|
|
313
|
+
if (this.attempt) {
|
|
314
|
+
this.closeAttempt(undefined, "Interrupted by the next Pi attempt.", "interrupted");
|
|
315
|
+
}
|
|
316
|
+
const index = this.counters.attempts;
|
|
317
|
+
this.counters.attempts += 1;
|
|
318
|
+
this.lastAssistant = undefined;
|
|
319
|
+
this.unresolvedToolErrors = 0;
|
|
320
|
+
this.attemptIndex = index;
|
|
321
|
+
this.attempt = this.startObservation(
|
|
322
|
+
"pi.attempt",
|
|
323
|
+
{
|
|
324
|
+
metadata: {
|
|
325
|
+
"pi.attempt.index": index,
|
|
326
|
+
...(input.reason ? { "pi.attempt.reason": input.reason } : {}),
|
|
327
|
+
},
|
|
328
|
+
version: TRACE_SCHEMA_VERSION,
|
|
329
|
+
},
|
|
330
|
+
{ asType: "span", parent: this.root },
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
finishAttempt(message?: AssistantMessage): void {
|
|
335
|
+
if (message?.role === "assistant") this.finishAssistant(message);
|
|
336
|
+
if (!this.attempt) return;
|
|
337
|
+
const outcome = this.unresolvedToolErrors > 0 ? "error" : classifyOutcome(message);
|
|
338
|
+
this.closeAttempt(
|
|
339
|
+
message,
|
|
340
|
+
this.unresolvedToolErrors > 0 ? "Pi attempt ended after a tool failure." : undefined,
|
|
341
|
+
outcome,
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
beginTurn(turnIndex: number): void {
|
|
346
|
+
if (!this.root) return;
|
|
347
|
+
if (this.turn) this.closeTurn("Interrupted by the next Pi turn.", "ERROR");
|
|
348
|
+
this.counters.turns += 1;
|
|
349
|
+
this.turnIndex = turnIndex;
|
|
350
|
+
this.turn = this.startObservation(
|
|
351
|
+
"pi.turn",
|
|
352
|
+
{ metadata: { "pi.turn.index": turnIndex }, version: TRACE_SCHEMA_VERSION },
|
|
353
|
+
{ asType: "span", parent: this.attempt ?? this.root },
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
finishTurn(turnIndex: number, result: TurnResult): void {
|
|
358
|
+
if (!this.turn) return;
|
|
359
|
+
this.closeGeneration("Turn ended without a finalized assistant message.");
|
|
360
|
+
this.closeTools("Tool span ended when the Pi turn finished.");
|
|
361
|
+
|
|
362
|
+
const mismatched = this.turnIndex !== turnIndex;
|
|
363
|
+
const outcome = classifyOutcome(result.message);
|
|
364
|
+
this.turn.update({
|
|
365
|
+
metadata: {
|
|
366
|
+
"pi.turn.index": this.turnIndex ?? turnIndex,
|
|
367
|
+
"pi.turn.tool_result_count": result.toolResultCount,
|
|
368
|
+
...(result.message.stopReason ? { "pi.turn.stop_reason": result.message.stopReason } : {}),
|
|
369
|
+
},
|
|
370
|
+
...(mismatched && outcome === "success"
|
|
371
|
+
? {
|
|
372
|
+
level: "WARNING" as const,
|
|
373
|
+
statusMessage: `Turn ${turnIndex} ended while turn ${this.turnIndex} was active.`,
|
|
374
|
+
}
|
|
375
|
+
: severityForOutcome(outcome, this.captureErrorMessage(result.message.errorMessage))),
|
|
376
|
+
});
|
|
377
|
+
this.turn.end();
|
|
378
|
+
this.turn = undefined;
|
|
379
|
+
this.turnIndex = undefined;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
beginGeneration(input: GenerationInput = {}): void {
|
|
383
|
+
if (!this.root) return;
|
|
384
|
+
this.closeGeneration("Interrupted by the next provider request.");
|
|
385
|
+
if (this.unresolvedToolErrors > 0) {
|
|
386
|
+
this.counters.recoveredErrors += this.unresolvedToolErrors;
|
|
387
|
+
this.unresolvedToolErrors = 0;
|
|
388
|
+
}
|
|
389
|
+
this.counters.generations += 1;
|
|
390
|
+
const requestMetadata: Record<string, unknown> = {
|
|
391
|
+
...(input.payloadStage ? { "pi.request.payload_stage": input.payloadStage } : {}),
|
|
392
|
+
...(input.model?.provider ? { "pi.request.provider": input.model.provider } : {}),
|
|
393
|
+
...(input.model?.id ? { "pi.request.model": input.model.id } : {}),
|
|
394
|
+
...(input.model?.api ? { "pi.request.api": input.model.api } : {}),
|
|
395
|
+
...(input.thinkingLevel ? { "pi.request.thinking_level": input.thinkingLevel } : {}),
|
|
396
|
+
};
|
|
397
|
+
const observation = this.startObservation(
|
|
398
|
+
"pi.llm",
|
|
399
|
+
{
|
|
400
|
+
...(input.payload !== undefined ? { input: this.capture(input.payload) } : {}),
|
|
401
|
+
...(input.model?.id ? { model: input.model.id } : {}),
|
|
402
|
+
...(input.thinkingLevel ? { modelParameters: { thinking_level: input.thinkingLevel } } : {}),
|
|
403
|
+
...(Object.keys(requestMetadata).length > 0 ? { metadata: requestMetadata } : {}),
|
|
404
|
+
version: TRACE_SCHEMA_VERSION,
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
asType: "generation",
|
|
408
|
+
parent: this.turn ?? this.attempt ?? this.root,
|
|
409
|
+
...(input.startedAt === undefined ? {} : { startTime: new Date(input.startedAt) }),
|
|
410
|
+
},
|
|
411
|
+
);
|
|
412
|
+
this.generation = {
|
|
413
|
+
observation,
|
|
414
|
+
requestMetadata,
|
|
415
|
+
statuses: [],
|
|
416
|
+
headers: {},
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
recordProviderResponse(status: number, headers: Record<string, string> = {}): void {
|
|
421
|
+
if (!this.generation) return;
|
|
422
|
+
this.generation.statuses.push(status);
|
|
423
|
+
Object.assign(this.generation.headers, allowlistedResponseHeaders(headers));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
markGenerationFirstOutput(timestamp = Date.now()): void {
|
|
427
|
+
if (this.generation && this.generation.completionStartTime === undefined) {
|
|
428
|
+
this.generation.completionStartTime = timestamp;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
markGenerationEnd(endTime = Date.now()): void {
|
|
433
|
+
if (this.generation && this.generation.endTime === undefined) {
|
|
434
|
+
this.generation.endTime = endTime;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
finishAssistant(message: AssistantMessage): void {
|
|
439
|
+
if (message.role !== "assistant") return;
|
|
440
|
+
this.lastAssistant = message;
|
|
441
|
+
this.lastOutput = this.capture(message.content);
|
|
442
|
+
if (!this.generation) return;
|
|
443
|
+
|
|
444
|
+
const state = this.generation;
|
|
445
|
+
const usageDetails = numericRecord({
|
|
446
|
+
input: message.usage?.input,
|
|
447
|
+
output: message.usage?.output,
|
|
448
|
+
cache_read_input_tokens: message.usage?.cacheRead,
|
|
449
|
+
cache_creation_input_tokens: message.usage?.cacheWrite,
|
|
450
|
+
total: message.usage?.totalTokens,
|
|
451
|
+
});
|
|
452
|
+
const costDetails = knownCostDetails(message.usage?.cost);
|
|
453
|
+
const outcome = classifyOutcome(message);
|
|
454
|
+
const responseModel = message.responseModel ?? message.model;
|
|
455
|
+
const httpMetadata = generationHttpMetadata(state);
|
|
456
|
+
const recoveredStatuses = state.statuses.filter((status) => status >= 400).length;
|
|
457
|
+
if (outcome === "success" && recoveredStatuses > 0) {
|
|
458
|
+
this.counters.recoveredErrors += recoveredStatuses;
|
|
459
|
+
}
|
|
460
|
+
state.observation.update({
|
|
461
|
+
output: this.lastOutput,
|
|
462
|
+
...(state.completionStartTime !== undefined ? { completionStartTime: new Date(state.completionStartTime) } : {}),
|
|
463
|
+
...(responseModel ? { model: responseModel } : {}),
|
|
464
|
+
...(Object.keys(usageDetails).length > 0 ? { usageDetails } : {}),
|
|
465
|
+
...(Object.keys(costDetails).length > 0 ? { costDetails } : {}),
|
|
466
|
+
metadata: {
|
|
467
|
+
...state.requestMetadata,
|
|
468
|
+
...httpMetadata,
|
|
469
|
+
...(message.provider ? { "pi.provider": message.provider, "pi.response.provider": message.provider } : {}),
|
|
470
|
+
...(message.api ? { "pi.response.api": message.api } : {}),
|
|
471
|
+
...(responseModel ? { "pi.response.model": responseModel } : {}),
|
|
472
|
+
...(message.responseId ? { "pi.response.id": message.responseId } : {}),
|
|
473
|
+
...(message.responseModel && message.model && message.responseModel !== message.model
|
|
474
|
+
? { "pi.requested_model": message.model }
|
|
475
|
+
: {}),
|
|
476
|
+
...(message.stopReason ? { "pi.stop_reason": message.stopReason } : {}),
|
|
477
|
+
...(typeof message.usage?.cost?.totalFromProvider === "boolean"
|
|
478
|
+
? { "pi.usage.cost.total_from_provider": message.usage.cost.totalFromProvider }
|
|
479
|
+
: {}),
|
|
480
|
+
...(finiteNumber(message.usage?.reasoning) ? { "pi.usage.reasoning_tokens": message.usage.reasoning } : {}),
|
|
481
|
+
...(finiteNumber(message.usage?.cacheWrite1h)
|
|
482
|
+
? { "pi.usage.cache_write_1h_tokens": message.usage.cacheWrite1h }
|
|
483
|
+
: {}),
|
|
484
|
+
},
|
|
485
|
+
...severityForOutcome(outcome, this.captureErrorMessage(message.errorMessage)),
|
|
486
|
+
});
|
|
487
|
+
state.observation.end(state.endTime);
|
|
488
|
+
this.generation = undefined;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
beginTool(toolCallId: string, toolName: string, args?: unknown, startedAt = Date.now()): void {
|
|
492
|
+
if (!this.root) return;
|
|
493
|
+
this.counters.tools += 1;
|
|
494
|
+
if (this.duplicateToolIds.has(toolCallId)) {
|
|
495
|
+
this.counters.toolErrors += 1;
|
|
496
|
+
const duplicate = this.startToolObservation(toolCallId, toolName, args);
|
|
497
|
+
duplicate.update({
|
|
498
|
+
level: "ERROR",
|
|
499
|
+
statusMessage: "Duplicate tool call ID cannot be correlated safely.",
|
|
500
|
+
});
|
|
501
|
+
duplicate.end();
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const existing = this.tools.get(toolCallId);
|
|
506
|
+
if (existing) {
|
|
507
|
+
existing.observation.update({
|
|
508
|
+
level: "ERROR",
|
|
509
|
+
statusMessage: "Duplicate tool execution started.",
|
|
510
|
+
});
|
|
511
|
+
existing.observation.end();
|
|
512
|
+
this.tools.delete(toolCallId);
|
|
513
|
+
this.counters.toolErrors += 2;
|
|
514
|
+
const duplicate = this.startToolObservation(toolCallId, toolName, args);
|
|
515
|
+
duplicate.update({
|
|
516
|
+
level: "ERROR",
|
|
517
|
+
statusMessage: "Duplicate tool call ID cannot be correlated safely.",
|
|
518
|
+
});
|
|
519
|
+
duplicate.end();
|
|
520
|
+
this.duplicateToolIds.add(toolCallId);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
this.tools.set(toolCallId, {
|
|
525
|
+
observation: this.startToolObservation(toolCallId, toolName, args),
|
|
526
|
+
startedAt,
|
|
527
|
+
progressUpdates: 0,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
recordToolInput(toolCallId: string, input: unknown): void {
|
|
532
|
+
if (input === undefined || this.duplicateToolIds.has(toolCallId)) return;
|
|
533
|
+
this.tools.get(toolCallId)?.observation.update({ input: this.capture(input) });
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
recordToolProgress(toolCallId: string, timestamp = Date.now()): void {
|
|
537
|
+
const tool = this.tools.get(toolCallId);
|
|
538
|
+
if (!tool) return;
|
|
539
|
+
tool.progressUpdates += 1;
|
|
540
|
+
tool.firstProgressAt ??= timestamp;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
finishTool(toolCallId: string, result: ToolResult): void {
|
|
544
|
+
if (this.duplicateToolIds.has(toolCallId)) return;
|
|
545
|
+
const tool = this.tools.get(toolCallId);
|
|
546
|
+
if (!tool) return;
|
|
547
|
+
if (result.isError) {
|
|
548
|
+
this.counters.toolErrors += 1;
|
|
549
|
+
this.unresolvedToolErrors += 1;
|
|
550
|
+
}
|
|
551
|
+
tool.observation.update({
|
|
552
|
+
output: this.capture({ content: result.content, details: result.details }),
|
|
553
|
+
metadata: {
|
|
554
|
+
"pi.tool.progress_update_count": tool.progressUpdates,
|
|
555
|
+
...(tool.firstProgressAt !== undefined
|
|
556
|
+
? {
|
|
557
|
+
"pi.tool.time_to_first_progress_ms": Math.max(0, tool.firstProgressAt - tool.startedAt),
|
|
558
|
+
}
|
|
559
|
+
: {}),
|
|
560
|
+
},
|
|
561
|
+
...(result.isError ? { level: "ERROR" as const, statusMessage: "Pi tool execution failed." } : {}),
|
|
562
|
+
});
|
|
563
|
+
tool.observation.end();
|
|
564
|
+
this.tools.delete(toolCallId);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
beginCompaction(input: CompactionStart): void {
|
|
568
|
+
if (!this.root) return;
|
|
569
|
+
this.closeCompaction("Interrupted by another Pi compaction.");
|
|
570
|
+
this.counters.compactions += 1;
|
|
571
|
+
this.compaction = {
|
|
572
|
+
observation: this.startObservation(
|
|
573
|
+
"pi.compaction",
|
|
574
|
+
{
|
|
575
|
+
metadata: {
|
|
576
|
+
"pi.compaction.reason": input.reason,
|
|
577
|
+
"pi.compaction.will_retry": input.willRetry,
|
|
578
|
+
...(finiteNumber(input.tokensBefore) ? { "pi.compaction.tokens_before": input.tokensBefore } : {}),
|
|
579
|
+
"pi.compaction.messages_to_summarize": input.messagesToSummarize,
|
|
580
|
+
"pi.compaction.turn_prefix_messages": input.turnPrefixMessages,
|
|
581
|
+
"pi.compaction.branch_entries": input.branchEntries,
|
|
582
|
+
"pi.compaction.is_split_turn": input.isSplitTurn,
|
|
583
|
+
},
|
|
584
|
+
version: TRACE_SCHEMA_VERSION,
|
|
585
|
+
},
|
|
586
|
+
{ asType: "span", parent: this.root },
|
|
587
|
+
),
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
finishCompaction(input: CompactionFinish): void {
|
|
592
|
+
if (!this.compaction) return;
|
|
593
|
+
const details = structuralCompactionDetails(input.details);
|
|
594
|
+
const usage = numericRecord({
|
|
595
|
+
input: input.usage?.input,
|
|
596
|
+
output: input.usage?.output,
|
|
597
|
+
cache_read: input.usage?.cacheRead,
|
|
598
|
+
cache_write: input.usage?.cacheWrite,
|
|
599
|
+
total: input.usage?.totalTokens,
|
|
600
|
+
});
|
|
601
|
+
const cost = knownCostDetails(input.usage?.cost);
|
|
602
|
+
this.compaction.observation.update({
|
|
603
|
+
metadata: {
|
|
604
|
+
"pi.compaction.reason": input.reason,
|
|
605
|
+
"pi.compaction.will_retry": input.willRetry,
|
|
606
|
+
"pi.compaction.from_extension": input.fromExtension,
|
|
607
|
+
...(finiteNumber(input.tokensBefore) ? { "pi.compaction.tokens_before": input.tokensBefore } : {}),
|
|
608
|
+
...details,
|
|
609
|
+
...prefixRecord("pi.compaction.usage", usage),
|
|
610
|
+
...prefixRecord("pi.compaction.cost", cost),
|
|
611
|
+
...(typeof input.usage?.cost?.totalFromProvider === "boolean"
|
|
612
|
+
? { "pi.compaction.cost.total_from_provider": input.usage.cost.totalFromProvider }
|
|
613
|
+
: {}),
|
|
614
|
+
},
|
|
615
|
+
});
|
|
616
|
+
this.compaction.observation.end();
|
|
617
|
+
this.compaction = undefined;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
settle(snapshot?: ContextSnapshot): void {
|
|
621
|
+
this.closeActiveTrace(undefined, snapshot);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
interrupt(statusMessage: string, snapshot?: ContextSnapshot): void {
|
|
625
|
+
this.closeActiveTrace(statusMessage, snapshot, "interrupted");
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
dispose(statusMessage = "Pi session ended before the active trace settled.", snapshot?: ContextSnapshot): void {
|
|
629
|
+
this.closeActiveTrace(statusMessage, snapshot, "interrupted");
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
private closeActiveTrace(statusMessage?: string, snapshot?: ContextSnapshot, forcedOutcome?: Outcome): void {
|
|
633
|
+
this.closeTurn(statusMessage ?? "Pi turn ended when the agent run settled.");
|
|
634
|
+
this.closeAttempt(undefined, statusMessage, forcedOutcome ?? "interrupted");
|
|
635
|
+
this.closeCompaction(statusMessage ?? "Pi compaction ended when the agent run settled.");
|
|
636
|
+
|
|
637
|
+
if (!this.root) return;
|
|
638
|
+
const baseOutcome = forcedOutcome ?? this.lastAttemptOutcome ?? classifyOutcome(this.lastAssistant);
|
|
639
|
+
const recoveredErrorCount =
|
|
640
|
+
this.counters.recoveredErrors + (baseOutcome === "success" ? this.counters.failedAttempts : 0);
|
|
641
|
+
const traceOutcome = baseOutcome === "success" && recoveredErrorCount > 0 ? "recovered_success" : baseOutcome;
|
|
642
|
+
const finalMetadata: Record<string, unknown> = {
|
|
643
|
+
"pi.trace.schema_version": Number(TRACE_SCHEMA_VERSION),
|
|
644
|
+
"pi.trace.outcome": traceOutcome,
|
|
645
|
+
...(this.lastAssistant?.stopReason ? { "pi.trace.stop_reason": this.lastAssistant.stopReason } : {}),
|
|
646
|
+
"pi.trace.attempt_count": this.counters.attempts,
|
|
647
|
+
"pi.trace.turn_count": this.counters.turns,
|
|
648
|
+
"pi.trace.generation_count": this.counters.generations,
|
|
649
|
+
"pi.trace.tool_count": this.counters.tools,
|
|
650
|
+
"pi.trace.tool_error_count": this.counters.toolErrors,
|
|
651
|
+
"pi.trace.compaction_count": this.counters.compactions,
|
|
652
|
+
"pi.trace.recovered_error_count": recoveredErrorCount,
|
|
653
|
+
...snapshotMetadata("start", this.startSnapshot),
|
|
654
|
+
...snapshotMetadata("end", snapshot),
|
|
655
|
+
};
|
|
656
|
+
const finalAttributes: ObservationAttributes = {
|
|
657
|
+
...(this.lastOutput !== undefined ? { output: this.lastOutput } : {}),
|
|
658
|
+
metadata: finalMetadata,
|
|
659
|
+
...severityForOutcome(baseOutcome, statusMessage ?? this.captureErrorMessage(this.lastAssistant?.errorMessage)),
|
|
660
|
+
};
|
|
661
|
+
this.root.update(finalAttributes);
|
|
662
|
+
this.root.updateTrace?.({
|
|
663
|
+
...(this.lastOutput !== undefined ? { output: this.lastOutput } : {}),
|
|
664
|
+
metadata: finalMetadata,
|
|
665
|
+
version: TRACE_SCHEMA_VERSION,
|
|
666
|
+
});
|
|
667
|
+
this.root.end();
|
|
668
|
+
this.root = undefined;
|
|
669
|
+
this.lastOutput = undefined;
|
|
670
|
+
this.lastAssistant = undefined;
|
|
671
|
+
this.lastAttemptOutcome = undefined;
|
|
672
|
+
this.unresolvedToolErrors = 0;
|
|
673
|
+
this.duplicateToolIds.clear();
|
|
674
|
+
this.startSnapshot = undefined;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
private closeAttempt(message?: AssistantMessage, statusMessage?: string, forcedOutcome?: Outcome): void {
|
|
678
|
+
if (!this.attempt) return;
|
|
679
|
+
this.closeTurn(statusMessage ?? "Pi turn ended when the attempt finished.");
|
|
680
|
+
const outcome = forcedOutcome ?? classifyOutcome(message);
|
|
681
|
+
if (outcome === "error") this.counters.failedAttempts += 1;
|
|
682
|
+
this.lastAttemptOutcome = outcome;
|
|
683
|
+
this.attempt.update({
|
|
684
|
+
...(message?.content !== undefined ? { output: this.capture(message.content) } : {}),
|
|
685
|
+
metadata: {
|
|
686
|
+
"pi.attempt.index": this.attemptIndex ?? Math.max(0, this.counters.attempts - 1),
|
|
687
|
+
"pi.attempt.outcome": outcome,
|
|
688
|
+
...(message?.stopReason ? { "pi.attempt.stop_reason": message.stopReason } : {}),
|
|
689
|
+
},
|
|
690
|
+
...severityForOutcome(outcome, statusMessage ?? this.captureErrorMessage(message?.errorMessage)),
|
|
691
|
+
});
|
|
692
|
+
this.attempt.end();
|
|
693
|
+
this.attempt = undefined;
|
|
694
|
+
this.attemptIndex = undefined;
|
|
695
|
+
this.unresolvedToolErrors = 0;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
private startToolObservation(toolCallId: string, toolName: string, args?: unknown): Observation {
|
|
699
|
+
return this.startObservation(
|
|
700
|
+
`pi.tool.${toolName}`,
|
|
701
|
+
{
|
|
702
|
+
...(args !== undefined ? { input: this.capture(args) } : {}),
|
|
703
|
+
metadata: { "pi.tool.call_id": toolCallId, "pi.tool.name": toolName },
|
|
704
|
+
version: TRACE_SCHEMA_VERSION,
|
|
705
|
+
},
|
|
706
|
+
{ asType: "tool", parent: this.turn ?? this.attempt ?? this.root },
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
private closeTurn(statusMessage: string, generationLevel: "ERROR" | "WARNING" = "WARNING"): void {
|
|
711
|
+
this.closeGeneration(statusMessage, generationLevel);
|
|
712
|
+
this.closeTools(statusMessage);
|
|
713
|
+
if (!this.turn) return;
|
|
714
|
+
this.turn.update({ level: "WARNING", statusMessage });
|
|
715
|
+
this.turn.end();
|
|
716
|
+
this.turn = undefined;
|
|
717
|
+
this.turnIndex = undefined;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
private closeTools(statusMessage: string): void {
|
|
721
|
+
for (const tool of this.tools.values()) {
|
|
722
|
+
tool.observation.update({
|
|
723
|
+
level: "WARNING",
|
|
724
|
+
statusMessage,
|
|
725
|
+
metadata: {
|
|
726
|
+
"pi.tool.progress_update_count": tool.progressUpdates,
|
|
727
|
+
...(tool.firstProgressAt !== undefined
|
|
728
|
+
? {
|
|
729
|
+
"pi.tool.time_to_first_progress_ms": Math.max(0, tool.firstProgressAt - tool.startedAt),
|
|
730
|
+
}
|
|
731
|
+
: {}),
|
|
732
|
+
},
|
|
733
|
+
});
|
|
734
|
+
tool.observation.end();
|
|
735
|
+
}
|
|
736
|
+
this.tools.clear();
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
private closeGeneration(statusMessage: string, level: "ERROR" | "WARNING" = "ERROR"): void {
|
|
740
|
+
if (!this.generation) return;
|
|
741
|
+
this.generation.observation.update({
|
|
742
|
+
level,
|
|
743
|
+
statusMessage,
|
|
744
|
+
metadata: {
|
|
745
|
+
...this.generation.requestMetadata,
|
|
746
|
+
...generationHttpMetadata(this.generation),
|
|
747
|
+
},
|
|
748
|
+
});
|
|
749
|
+
this.generation.observation.end(this.generation.endTime);
|
|
750
|
+
this.generation = undefined;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
private closeCompaction(statusMessage: string): void {
|
|
754
|
+
if (!this.compaction) return;
|
|
755
|
+
this.compaction.observation.update({ level: "WARNING", statusMessage });
|
|
756
|
+
this.compaction.observation.end();
|
|
757
|
+
this.compaction = undefined;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
private captureErrorMessage(message: string | undefined): string | undefined {
|
|
761
|
+
// Provider error text can include request or response content. Keep the outcome, not the text, when capture is off.
|
|
762
|
+
if (!this.context.captureContent || typeof message !== "string") return undefined;
|
|
763
|
+
return String(sanitizeTraceValue(message));
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
private capture(value: unknown): unknown {
|
|
767
|
+
return this.context.captureContent ? sanitizeTraceValue(value) : CONTENT_DISABLED;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
function normalizeTraceName(value: string | undefined): string {
|
|
772
|
+
return typeof value === "string" && value.trim() ? value.trim() : "pi.trace";
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function traceTags(customTags: readonly string[] | undefined, gitTag: string | undefined): string[] {
|
|
776
|
+
const tags = ["pi", ...(gitTag ? [gitTag] : []), ...(customTags ?? [])];
|
|
777
|
+
return [...new Set(tags.map(sanitizeTraceTag).filter((tag): tag is string => tag !== undefined))];
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function sanitizeTraceTag(value: unknown): string | undefined {
|
|
781
|
+
if (typeof value !== "string" || !value.trim()) return undefined;
|
|
782
|
+
const sanitized = sanitizeTraceValue(value.trim());
|
|
783
|
+
return typeof sanitized === "string" && sanitized.trim() ? sanitized.trim() : undefined;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function customTraceMetadata(metadata: Readonly<Record<string, unknown>> | undefined): Record<string, unknown> {
|
|
787
|
+
if (!metadata) return {};
|
|
788
|
+
const sanitized = sanitizeTraceValue(metadata);
|
|
789
|
+
if (!sanitized || typeof sanitized !== "object" || Array.isArray(sanitized)) return {};
|
|
790
|
+
return Object.fromEntries(Object.entries(sanitized).filter(([key]) => !key.startsWith("pi.")));
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
function generationHttpMetadata(state: GenerationState): Record<string, unknown> {
|
|
794
|
+
const finalStatus = state.statuses.at(-1);
|
|
795
|
+
return {
|
|
796
|
+
...(state.statuses.length > 0
|
|
797
|
+
? {
|
|
798
|
+
"http.response.status_codes": [...state.statuses],
|
|
799
|
+
"http.response.status_code": finalStatus,
|
|
800
|
+
"http.response.attempt_count": state.statuses.length,
|
|
801
|
+
"http.response.retry_count": Math.max(0, state.statuses.length - 1),
|
|
802
|
+
}
|
|
803
|
+
: {}),
|
|
804
|
+
...(Object.keys(state.headers).length > 0 ? { "http.response.headers": { ...state.headers } } : {}),
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
function allowlistedResponseHeaders(headers: Record<string, string>): Record<string, string> {
|
|
809
|
+
const output: Record<string, string> = {};
|
|
810
|
+
for (const [rawName, rawValue] of Object.entries(headers)) {
|
|
811
|
+
const name = rawName.toLowerCase();
|
|
812
|
+
if (!ALLOWED_RESPONSE_HEADERS.has(name) || typeof rawValue !== "string") continue;
|
|
813
|
+
output[name] = truncateString(rawValue, MAX_HEADER_VALUE_LENGTH);
|
|
814
|
+
}
|
|
815
|
+
return output;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
function structuralCompactionDetails(details: unknown): Record<string, unknown> {
|
|
819
|
+
if (!details || typeof details !== "object" || Array.isArray(details)) return {};
|
|
820
|
+
const record = details as Record<string, unknown>;
|
|
821
|
+
return {
|
|
822
|
+
...(Array.isArray(record.readFiles) ? { "pi.compaction.read_file_count": record.readFiles.length } : {}),
|
|
823
|
+
...(Array.isArray(record.modifiedFiles)
|
|
824
|
+
? { "pi.compaction.modified_file_count": record.modifiedFiles.length }
|
|
825
|
+
: {}),
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
function snapshotMetadata(prefix: string, snapshot?: ContextSnapshot): Record<string, unknown> {
|
|
830
|
+
if (!snapshot) return {};
|
|
831
|
+
return {
|
|
832
|
+
...(snapshot.leafId ? { [`pi.trace.${prefix}_leaf_id`]: snapshot.leafId } : {}),
|
|
833
|
+
...(snapshot.contextUsage
|
|
834
|
+
? {
|
|
835
|
+
...(finiteNumber(snapshot.contextUsage.tokens)
|
|
836
|
+
? { [`pi.trace.${prefix}_context_tokens`]: snapshot.contextUsage.tokens }
|
|
837
|
+
: {}),
|
|
838
|
+
[`pi.trace.${prefix}_context_window`]: snapshot.contextUsage.contextWindow,
|
|
839
|
+
...(finiteNumber(snapshot.contextUsage.percent)
|
|
840
|
+
? { [`pi.trace.${prefix}_context_percent`]: snapshot.contextUsage.percent }
|
|
841
|
+
: {}),
|
|
842
|
+
}
|
|
843
|
+
: {}),
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
function classifyOutcome(message?: { role?: string; stopReason?: string; errorMessage?: string }): Outcome {
|
|
848
|
+
if (message?.role !== "assistant") return "interrupted";
|
|
849
|
+
if (message.stopReason === "error" || message.errorMessage) return "error";
|
|
850
|
+
if (message.stopReason === "aborted") return "aborted";
|
|
851
|
+
if (message.stopReason === "length") return "length";
|
|
852
|
+
return "success";
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
function severityForOutcome(
|
|
856
|
+
outcome: Outcome,
|
|
857
|
+
statusMessage?: string,
|
|
858
|
+
): Pick<ObservationAttributes, "level" | "statusMessage"> {
|
|
859
|
+
if (outcome === "success") return {};
|
|
860
|
+
if (outcome === "error") {
|
|
861
|
+
return {
|
|
862
|
+
level: "ERROR",
|
|
863
|
+
statusMessage: statusMessage ?? "The Pi operation failed.",
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
return {
|
|
867
|
+
level: "WARNING",
|
|
868
|
+
statusMessage:
|
|
869
|
+
statusMessage ??
|
|
870
|
+
(outcome === "aborted"
|
|
871
|
+
? "The Pi operation was aborted."
|
|
872
|
+
: outcome === "length"
|
|
873
|
+
? "The model reached its output limit."
|
|
874
|
+
: "The Pi operation was interrupted."),
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function prefixRecord(prefix: string, values: Record<string, number>): Record<string, number> {
|
|
879
|
+
return Object.fromEntries(Object.entries(values).map(([key, value]) => [`${prefix}.${key}`, value]));
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
function truncateString(value: string, maxBytes: number): string {
|
|
883
|
+
const boundedPrefix = value.slice(0, maxBytes);
|
|
884
|
+
if (boundedPrefix.length === value.length && byteLength(boundedPrefix) <= maxBytes) {
|
|
885
|
+
return boundedPrefix;
|
|
886
|
+
}
|
|
887
|
+
const suffix = "… [truncated]";
|
|
888
|
+
const target = Math.max(0, maxBytes - byteLength(suffix) - 2);
|
|
889
|
+
let bytes = 0;
|
|
890
|
+
let output = "";
|
|
891
|
+
for (const character of boundedPrefix) {
|
|
892
|
+
const size = byteLength(character);
|
|
893
|
+
if (bytes + size > target) break;
|
|
894
|
+
output += character;
|
|
895
|
+
bytes += size;
|
|
896
|
+
}
|
|
897
|
+
return `${output}${suffix}`;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
function byteLength(value: string): number {
|
|
901
|
+
return Buffer.byteLength(value, "utf8");
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function numericRecord(values: Record<string, number | undefined>): Record<string, number> {
|
|
905
|
+
return Object.fromEntries(
|
|
906
|
+
Object.entries(values).filter((entry): entry is [string, number] => finiteNumber(entry[1])),
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
function knownCostDetails(cost: UsageCost | undefined): Record<string, number> {
|
|
911
|
+
const values = {
|
|
912
|
+
input: cost?.input,
|
|
913
|
+
output: cost?.output,
|
|
914
|
+
cache_read: cost?.cacheRead,
|
|
915
|
+
cache_write: cost?.cacheWrite,
|
|
916
|
+
total: cost?.total,
|
|
917
|
+
};
|
|
918
|
+
return Object.fromEntries(
|
|
919
|
+
Object.entries(values).filter(
|
|
920
|
+
(entry): entry is [string, number] =>
|
|
921
|
+
finiteNumber(entry[1]) &&
|
|
922
|
+
(entry[1] > 0 || (entry[0] === "total" && entry[1] === 0 && cost?.totalFromProvider === true)),
|
|
923
|
+
),
|
|
924
|
+
);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function finiteNumber(value: unknown): value is number {
|
|
928
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function hasItems(value: unknown): boolean {
|
|
932
|
+
return Array.isArray(value) ? value.length > 0 : value !== undefined;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
function emptyCounters(): Counters {
|
|
936
|
+
return {
|
|
937
|
+
attempts: 0,
|
|
938
|
+
turns: 0,
|
|
939
|
+
generations: 0,
|
|
940
|
+
tools: 0,
|
|
941
|
+
toolErrors: 0,
|
|
942
|
+
compactions: 0,
|
|
943
|
+
recoveredErrors: 0,
|
|
944
|
+
failedAttempts: 0,
|
|
945
|
+
};
|
|
946
|
+
}
|