@narumitw/pi-langfuse 0.20.0 → 0.28.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.
@@ -0,0 +1,147 @@
1
+ export const MAX_CAPTURE_BYTES = 64 * 1024;
2
+ const MAX_STRING_LENGTH = 50_000;
3
+ const MAX_COLLECTION_LENGTH = 200;
4
+ const MAX_DEPTH = 12;
5
+ const TRUNCATED = "[truncated: content budget exceeded]";
6
+ const BASE64_DATA_URI = /data:[^,\s"'`]*;base64,[^\s"'`]*/gi;
7
+ const BASE64_DATA_URI_OMITTED = "[base64 data URI omitted]";
8
+ const OPAQUE_SIGNATURE_KEYS = new Set(["thinkingSignature", "textSignature", "thoughtSignature"]);
9
+
10
+ export function sanitizeTraceValue(value: unknown): unknown {
11
+ const budget = { remaining: MAX_CAPTURE_BYTES };
12
+ const sanitized = sanitize(value, new WeakSet<object>(), 0, budget);
13
+ return serializedBytes(sanitized) <= MAX_CAPTURE_BYTES ? sanitized : TRUNCATED;
14
+ }
15
+
16
+ function sanitize(
17
+ value: unknown,
18
+ active: WeakSet<object>,
19
+ depth: number,
20
+ budget: { remaining: number },
21
+ ): unknown {
22
+ if (budget.remaining <= byteLength(TRUNCATED)) return TRUNCATED;
23
+ if (value === null || typeof value === "number" || typeof value === "boolean") {
24
+ return consume(value, budget);
25
+ }
26
+ if (typeof value === "string") {
27
+ const bounded = truncateString(value, Math.min(MAX_STRING_LENGTH, budget.remaining));
28
+ const redacted = bounded.replace(BASE64_DATA_URI, BASE64_DATA_URI_OMITTED);
29
+ return consume(redacted, budget);
30
+ }
31
+ if (typeof value === "bigint") return consume(value.toString(), budget);
32
+ if (typeof value === "undefined" || typeof value === "function" || typeof value === "symbol") {
33
+ return undefined;
34
+ }
35
+ if (value instanceof Date) {
36
+ return consume(Number.isNaN(value.getTime()) ? "[invalid date]" : value.toISOString(), budget);
37
+ }
38
+ if (value instanceof Error) {
39
+ return sanitize({ name: value.name, message: value.message }, active, depth, budget);
40
+ }
41
+ if (depth >= MAX_DEPTH) return consume("[maximum depth reached]", budget);
42
+ if (active.has(value)) return consume("[circular]", budget);
43
+ active.add(value);
44
+
45
+ let result: unknown;
46
+ if (Array.isArray(value)) {
47
+ const items: unknown[] = [];
48
+ for (const item of value.slice(0, MAX_COLLECTION_LENGTH)) {
49
+ if (budget.remaining <= byteLength(TRUNCATED)) break;
50
+ items.push(sanitize(item, active, depth + 1, budget));
51
+ }
52
+ if (items.length < value.length) items.push(`[${value.length - items.length} items omitted]`);
53
+ result = items;
54
+ } else {
55
+ const record = value as Record<string, unknown>;
56
+ const output: Record<string, unknown> = {};
57
+ const objectType = readProperty(record, "type").value;
58
+ const redactData = objectType === "image" || objectType === "base64";
59
+ let visited = 0;
60
+ let processed = 0;
61
+ let omitted = false;
62
+ try {
63
+ for (const key in record) {
64
+ if (visited >= MAX_COLLECTION_LENGTH || budget.remaining <= byteLength(TRUNCATED)) {
65
+ omitted = true;
66
+ break;
67
+ }
68
+ visited += 1;
69
+ if (!Object.hasOwn(record, key) || OPAQUE_SIGNATURE_KEYS.has(key)) continue;
70
+ const keyBudget = Math.min(MAX_STRING_LENGTH, Math.max(0, budget.remaining - 4));
71
+ if (key.length > keyBudget) {
72
+ omitted = true;
73
+ break;
74
+ }
75
+ const keyBytes = byteLength(key);
76
+ if (keyBytes > keyBudget) {
77
+ omitted = true;
78
+ break;
79
+ }
80
+ budget.remaining -= keyBytes + 4;
81
+ const property = readProperty(record, key);
82
+ if (redactData && key === "data") {
83
+ output[key] = consume("[base64 omitted]", budget);
84
+ } else if (!property.ok) output[key] = consume("[unreadable property]", budget);
85
+ else output[key] = sanitize(property.value, active, depth + 1, budget);
86
+ processed += 1;
87
+ }
88
+ } catch {
89
+ if (processed === 0) {
90
+ active.delete(value);
91
+ return consume("[unreadable object]", budget);
92
+ }
93
+ omitted = true;
94
+ }
95
+ if (omitted) output.$truncated = "additional object entries omitted";
96
+ result = output;
97
+ }
98
+ active.delete(value);
99
+ return result;
100
+ }
101
+
102
+ function readProperty(
103
+ record: Record<string, unknown>,
104
+ key: string,
105
+ ): { ok: true; value: unknown } | { ok: false; value?: undefined } {
106
+ try {
107
+ return { ok: true, value: record[key] };
108
+ } catch {
109
+ return { ok: false };
110
+ }
111
+ }
112
+
113
+ function consume<T>(value: T, budget: { remaining: number }): T | string {
114
+ const size = serializedBytes(value);
115
+ if (size > budget.remaining) {
116
+ budget.remaining -= byteLength(TRUNCATED);
117
+ return TRUNCATED;
118
+ }
119
+ budget.remaining -= size;
120
+ return value;
121
+ }
122
+
123
+ function truncateString(value: string, maxBytes: number): string {
124
+ const boundedPrefix = value.slice(0, maxBytes);
125
+ if (boundedPrefix.length === value.length && byteLength(boundedPrefix) <= maxBytes) {
126
+ return boundedPrefix;
127
+ }
128
+ const suffix = "… [truncated]";
129
+ const target = Math.max(0, maxBytes - byteLength(suffix) - 2);
130
+ let bytes = 0;
131
+ let output = "";
132
+ for (const character of boundedPrefix) {
133
+ const size = byteLength(character);
134
+ if (bytes + size > target) break;
135
+ output += character;
136
+ bytes += size;
137
+ }
138
+ return `${output}${suffix}`;
139
+ }
140
+
141
+ function serializedBytes(value: unknown): number {
142
+ return byteLength(JSON.stringify(value) ?? "null");
143
+ }
144
+
145
+ function byteLength(value: string): number {
146
+ return Buffer.byteLength(value, "utf8");
147
+ }