Files
oh-my-pi/packages/utils/src/json.ts
T
can1357 531880c620 feat: improved json serialization for bigint values
- Introduced `stringifyJson` helper to preserve bigint precision by serializing them as decimal strings.
- Replaced native `JSON.stringify` across compaction and session management modules to prevent serialization errors when handling bigint values in tool arguments.
- Added regression tests in `agent` and `coding-agent` packages to ensure bigint tool arguments remain intact through compaction and persistence flows.
2026-07-11 00:12:29 +02:00

24 lines
826 B
TypeScript

/**
* Try to parse JSON, returning null on failure.
*/
export function tryParseJson<T = unknown>(content: string): T | null {
try {
return JSON.parse(content) as T;
} catch {
return null;
}
}
/**
* Serialize JSON while preserving bigint precision as decimal strings.
*
* Tool arguments normally arrive from JSON providers, but extension hooks and
* host integrations can supply JavaScript bigint values. Native
* `JSON.stringify` throws for those values, which makes otherwise valid agent
* history impossible to persist, replay, or compact. A decimal string is the
* only lossless JSON representation.
*/
export function stringifyJson(value: unknown, space?: string | number): string | undefined {
return JSON.stringify(value, (_key, item) => (typeof item === "bigint" ? item.toString() : item), space);
}