MCPcopy Index your code
hub / github.com/TanStack/ai / stringifyMetaSafely

Function stringifyMetaSafely

packages/ai/src/logger/console-logger.ts:53–82  ·  view source on GitHub ↗

* `JSON.stringify` hardened for debug payloads: circular references collapse * to `"[Circular]"`, `Error` instances expand to `name`/`message`/`stack` * (they would otherwise stringify to `{}`), and `bigint` values become * strings (they would otherwise throw). Never throws — falls back to * `St

(value: unknown)

Source from the content-addressed store, hash-verified

51 * `String(value)` and, if even that coercion throws, a placeholder.
52 */
53function stringifyMetaSafely(value: unknown): string {
54 const seen = new WeakSet<object>()
55 try {
56 return JSON.stringify(
57 value,
58 (_key, entry: unknown) => {
59 if (typeof entry === 'bigint') return entry.toString()
60 if (entry instanceof Error) {
61 return {
62 name: entry.name,
63 message: entry.message,
64 stack: entry.stack,
65 }
66 }
67 if (typeof entry === 'object' && entry !== null) {
68 if (seen.has(entry)) return '[Circular]'
69 seen.add(entry)
70 }
71 return entry
72 },
73 2,
74 )
75 } catch {
76 try {
77 return String(value)
78 } catch {
79 return '[Unserializable meta]'
80 }
81 }
82}
83
84/**
85 * Default `Logger` implementation that routes each level to the matching

Callers 1

emitMethod · 0.85

Calls 2

toStringMethod · 0.80
hasMethod · 0.80

Tested by

no test coverage detected