@narumitw/pi-langfuse 0.50.5 → 0.50.6

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