pi-langfuse 1.7.0 → 1.7.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/README.md +68 -2
- package/README_CN.md +43 -1
- package/index.ts +7 -1
- package/package.json +5 -2
- package/src/commands.ts +34 -2
- package/src/handlers/agent.ts +4 -2
- package/src/handlers/cache.ts +4 -2
- package/src/handlers/generation.ts +21 -43
- package/src/handlers/session.ts +71 -13
- package/src/handlers/tool.ts +6 -7
- package/src/langfuse.ts +76 -65
- package/src/observation.ts +27 -0
- package/src/redaction.ts +145 -10
- package/src/session-lineage.ts +98 -0
- package/src/state.ts +7 -0
- package/src/types.ts +1 -0
- package/src/utils.ts +95 -19
package/src/state.ts
CHANGED
|
@@ -13,6 +13,9 @@ export interface SessionRunState {
|
|
|
13
13
|
lastPromptStateHash?: string;
|
|
14
14
|
lastToolStateHash?: string;
|
|
15
15
|
lastActiveTools: string[];
|
|
16
|
+
parentSessionId?: string;
|
|
17
|
+
sessionKind?: "subagent" | "fork";
|
|
18
|
+
lineageResolved: boolean;
|
|
16
19
|
systemStateSequence: number;
|
|
17
20
|
seenSystemEntryIds: Set<string>;
|
|
18
21
|
seenUsageEntryIds: Set<string>;
|
|
@@ -34,6 +37,7 @@ function createSessionRunState(): SessionRunState {
|
|
|
34
37
|
tracingDisabled: false,
|
|
35
38
|
setupAttemptedThisSession: false,
|
|
36
39
|
lastActiveTools: [],
|
|
40
|
+
lineageResolved: false,
|
|
37
41
|
systemStateSequence: 0,
|
|
38
42
|
seenSystemEntryIds: new Set(),
|
|
39
43
|
seenUsageEntryIds: new Set(),
|
|
@@ -147,6 +151,9 @@ export function resetRunState(sessionId = getActiveSessionId()) {
|
|
|
147
151
|
lastPromptStateHash: previous?.lastPromptStateHash,
|
|
148
152
|
lastToolStateHash: previous?.lastToolStateHash,
|
|
149
153
|
lastActiveTools: previous?.lastActiveTools ?? [],
|
|
154
|
+
parentSessionId: previous?.parentSessionId,
|
|
155
|
+
sessionKind: previous?.sessionKind,
|
|
156
|
+
lineageResolved: previous?.lineageResolved ?? false,
|
|
150
157
|
systemStateSequence: previous?.systemStateSequence ?? 0,
|
|
151
158
|
seenSystemEntryIds: previous?.seenSystemEntryIds ?? new Set(),
|
|
152
159
|
seenUsageEntryIds: previous?.seenUsageEntryIds ?? new Set(),
|
package/src/types.ts
CHANGED
|
@@ -122,6 +122,7 @@ export interface GenerationState {
|
|
|
122
122
|
observation: LangfuseObservation;
|
|
123
123
|
requestKey: string;
|
|
124
124
|
ended: boolean;
|
|
125
|
+
errorStatus?: Pick<ObservationUpdate, "level" | "statusMessage">;
|
|
125
126
|
metadata: Record<string, unknown>;
|
|
126
127
|
modelParameters?: Record<string, string | number>;
|
|
127
128
|
ttftRecorded?: boolean;
|
package/src/utils.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { getLimits } from "./limits.js";
|
|
2
2
|
import { getUsageOptions, type UsageOptions } from "./usage-options.js";
|
|
3
3
|
import { createCapturePolicy, redactOptionsFor, type CapturePolicy } from "./capture-policy.js";
|
|
4
|
-
import { redactValue } from "./redaction.js";
|
|
4
|
+
import { hasUnredactedSensitiveJsonField, redactString, redactValue } from "./redaction.js";
|
|
5
5
|
import { state } from "./state.js";
|
|
6
|
+
import type { ObservationUpdate } from "./types.js";
|
|
6
7
|
|
|
7
8
|
export function getCapturePolicy(): CapturePolicy {
|
|
8
9
|
return state.config?.capturePolicy ?? createCapturePolicy();
|
|
@@ -39,6 +40,7 @@ export function shapePayload(
|
|
|
39
40
|
maxObjectKeys?: number;
|
|
40
41
|
redact?: boolean;
|
|
41
42
|
parseJson?: boolean;
|
|
43
|
+
parseJsonPrefix?: boolean;
|
|
42
44
|
} = {},
|
|
43
45
|
): unknown {
|
|
44
46
|
const limits = getLimits();
|
|
@@ -48,6 +50,13 @@ export function shapePayload(
|
|
|
48
50
|
const maxArrayItems = options.maxArrayItems ?? limits.maxArrayItems;
|
|
49
51
|
const maxObjectKeys = options.maxObjectKeys ?? limits.maxObjectKeys;
|
|
50
52
|
const budget = { exhausted: false, nodeCount: 0 };
|
|
53
|
+
const redactionOptions = {
|
|
54
|
+
...redactOptionsFor(getCapturePolicy()),
|
|
55
|
+
maxDepth: depth,
|
|
56
|
+
maxStringLength: maxString,
|
|
57
|
+
maxArrayItems,
|
|
58
|
+
maxObjectKeys,
|
|
59
|
+
};
|
|
51
60
|
|
|
52
61
|
function visit(item: unknown, remainingDepth: number, seen: WeakSet<object>): unknown {
|
|
53
62
|
if (budget.exhausted) {
|
|
@@ -61,15 +70,31 @@ export function shapePayload(
|
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
if (typeof item === "string") {
|
|
73
|
+
const jsonStart = options.parseJsonPrefix ? item.search(/[\[{]/) : item.search(/^[\s]*[\[{]/);
|
|
74
|
+
// Truncated JSON loses field names needed for secret redaction.
|
|
75
|
+
if (options.parseJson !== false && jsonStart >= 0 && item.length > maxString) {
|
|
76
|
+
return PAYLOAD_TOO_LARGE;
|
|
77
|
+
}
|
|
64
78
|
const truncated = truncate(item, maxString);
|
|
65
79
|
if (options.parseJson === false) {
|
|
66
|
-
return truncated;
|
|
80
|
+
return options.redact === false ? truncated : redactString(item, redactionOptions);
|
|
67
81
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
82
|
+
const candidate = jsonStart > 0 ? truncated.slice(jsonStart) : truncated;
|
|
83
|
+
const parsed = tryParseJson(candidate);
|
|
84
|
+
if (parsed === candidate) {
|
|
85
|
+
// Native tool suffixes and bracketed prefixes can make JSON unparseable.
|
|
86
|
+
if (options.redact !== false && (
|
|
87
|
+
(options.parseJsonPrefix && /[\[{]\s*"/.test(truncated)) || hasUnredactedSensitiveJsonField(truncated)
|
|
88
|
+
)) return "[unparseable error payload]";
|
|
89
|
+
return options.redact === false ? truncated : redactString(item, redactionOptions);
|
|
71
90
|
}
|
|
72
|
-
|
|
91
|
+
const shaped = visit(parsed, remainingDepth - 1, seen);
|
|
92
|
+
if (jsonStart > 0 && truncated.slice(0, jsonStart).trim()) {
|
|
93
|
+
// Pi/provider prefixes must not hide structured fields from redaction.
|
|
94
|
+
const safe = options.redact === false ? shaped : redactValue(shaped, redactionOptions);
|
|
95
|
+
return truncated.slice(0, jsonStart) + JSON.stringify(safe);
|
|
96
|
+
}
|
|
97
|
+
return shaped;
|
|
73
98
|
}
|
|
74
99
|
|
|
75
100
|
if (
|
|
@@ -106,11 +131,12 @@ export function shapePayload(
|
|
|
106
131
|
}
|
|
107
132
|
|
|
108
133
|
if (item instanceof Error) {
|
|
109
|
-
|
|
134
|
+
// The stack repeats the raw message; retain frames without that copy.
|
|
135
|
+
return visit({
|
|
110
136
|
name: item.name,
|
|
111
137
|
message: item.message,
|
|
112
|
-
stack: item.
|
|
113
|
-
};
|
|
138
|
+
stack: item.message ? item.stack?.replaceAll(item.message, "[error message]") : item.stack,
|
|
139
|
+
}, remainingDepth, seen);
|
|
114
140
|
}
|
|
115
141
|
|
|
116
142
|
if (typeof item === "object") {
|
|
@@ -140,13 +166,7 @@ export function shapePayload(
|
|
|
140
166
|
const shaped = visit(value, depth, new WeakSet<object>());
|
|
141
167
|
return options.redact === false
|
|
142
168
|
? shaped
|
|
143
|
-
: redactValue(shaped,
|
|
144
|
-
...redactOptionsFor(getCapturePolicy()),
|
|
145
|
-
maxDepth: depth,
|
|
146
|
-
maxStringLength: maxString,
|
|
147
|
-
maxArrayItems,
|
|
148
|
-
maxObjectKeys,
|
|
149
|
-
});
|
|
169
|
+
: redactValue(shaped, redactionOptions);
|
|
150
170
|
}
|
|
151
171
|
|
|
152
172
|
export function safeSerialize(value: unknown, maxLength = getLimits().maxToolPayload): string {
|
|
@@ -161,20 +181,38 @@ export function estimatePayloadBytes(value: unknown, maxLength = getLimits().max
|
|
|
161
181
|
return new TextEncoder().encode(safeSerialize(value, maxLength)).length;
|
|
162
182
|
}
|
|
163
183
|
|
|
164
|
-
export function extractTextContent(content: unknown, maxLength?: number): string | undefined {
|
|
184
|
+
export function extractTextContent(content: unknown, maxLength?: number, depth = 0): string | undefined {
|
|
165
185
|
if (typeof content === "string") {
|
|
166
186
|
return maxLength ? truncate(content, maxLength) : content;
|
|
167
187
|
}
|
|
168
188
|
|
|
169
189
|
if (!Array.isArray(content)) {
|
|
190
|
+
// Tool results arrive wrapped ({ content: [{ type: "text", text }] }, or with
|
|
191
|
+
// the payload under result/output/text). The wrapper alone stringifies to
|
|
192
|
+
// "[object Object]", so dig out the text the reader actually wants; the depth
|
|
193
|
+
// cap keeps a reference cycle from recursing forever.
|
|
194
|
+
if (content && typeof content === "object" && depth < 2) {
|
|
195
|
+
const wrapper = content as { content?: unknown; result?: unknown; output?: unknown; text?: unknown };
|
|
196
|
+
for (const nested of [wrapper.content, wrapper.result, wrapper.output, wrapper.text]) {
|
|
197
|
+
if (!nested || nested === content) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const unwrapped = extractTextContent(nested, maxLength, depth + 1);
|
|
201
|
+
if (unwrapped) {
|
|
202
|
+
return unwrapped;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
170
206
|
return undefined;
|
|
171
207
|
}
|
|
172
208
|
|
|
173
209
|
const text = content
|
|
174
210
|
.map((item) => {
|
|
175
211
|
if (!item || typeof item !== "object") return "";
|
|
176
|
-
const block = item as { type?: string; text?:
|
|
177
|
-
return block.type === "text" && block.text
|
|
212
|
+
const block = item as { type?: string; text?: unknown };
|
|
213
|
+
return block.type === "text" && block.text
|
|
214
|
+
? typeof block.text === "string" ? block.text : safeSerialize(block.text, maxLength)
|
|
215
|
+
: "";
|
|
178
216
|
})
|
|
179
217
|
.filter(Boolean)
|
|
180
218
|
.join("\n");
|
|
@@ -186,6 +224,31 @@ export function extractTextContent(content: unknown, maxLength?: number): string
|
|
|
186
224
|
return maxLength ? truncate(text, maxLength) : text;
|
|
187
225
|
}
|
|
188
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Human-readable text for an observation `statusMessage`: text blocks first, then
|
|
229
|
+
* the string, then JSON, so a structured error never lands as "[object Object]"
|
|
230
|
+
* or as a wall of escaped JSON. Shape and redact before extracting any text.
|
|
231
|
+
*/
|
|
232
|
+
export function toStatusMessage(value: unknown, maxLength = 1_000): string {
|
|
233
|
+
maxLength = Math.min(maxLength, getLimits().maxString);
|
|
234
|
+
const shaped = shapePayload(value instanceof Error ? value.message || String(value) : value, {
|
|
235
|
+
maxString: maxLength, parseJsonPrefix: true,
|
|
236
|
+
});
|
|
237
|
+
const text = extractTextContent(shaped, maxLength);
|
|
238
|
+
if (text) {
|
|
239
|
+
return text;
|
|
240
|
+
}
|
|
241
|
+
const message = (shaped as { message?: unknown } | null)?.message;
|
|
242
|
+
if (typeof message === "string" && message) {
|
|
243
|
+
return truncate(message, maxLength);
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
return truncate(JSON.stringify(shaped) ?? String(shaped), maxLength);
|
|
247
|
+
} catch {
|
|
248
|
+
return "[unserializable error]";
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
189
252
|
export function normalizeContentForLangfuse(content: unknown, api?: string): unknown {
|
|
190
253
|
if (!Array.isArray(content)) {
|
|
191
254
|
return content;
|
|
@@ -410,6 +473,19 @@ export function extractModelParameters(payload: unknown): Record<string, string
|
|
|
410
473
|
return Object.keys(params).length > 0 ? params : undefined;
|
|
411
474
|
}
|
|
412
475
|
|
|
476
|
+
export function extractGenerationStatus(message: Record<string, unknown>): ObservationUpdate {
|
|
477
|
+
const reason = message.finishReason ?? message.stopReason;
|
|
478
|
+
if (reason !== "error" && reason !== "aborted") return {};
|
|
479
|
+
|
|
480
|
+
return {
|
|
481
|
+
level: reason === "error" ? "ERROR" : "WARNING",
|
|
482
|
+
statusMessage: toStatusMessage(
|
|
483
|
+
message.errorMessage || (reason === "error" ? "Generation failed" : "Generation aborted"),
|
|
484
|
+
getLimits().maxString,
|
|
485
|
+
),
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
413
489
|
export function getMessageFromEvent(event: Record<string, unknown>): Record<string, unknown> | undefined {
|
|
414
490
|
if (event.message && typeof event.message === "object") {
|
|
415
491
|
return event.message as Record<string, unknown>;
|