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