Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): add stringify helper and make AI-tracing serializers safe#22163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b6cc26f698e16b8dab3c04ab1ca0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,6 +3,31 @@ import { stringifyValue } from './normalize'; | ||
| export { escapeStringForRegex } from '../vendor/escapeStringForRegex'; | ||
| /** | ||
| * Coerce a value to a string without ever throwing. Strings pass through unchanged (so an | ||
| * already-serialized value isn't double-encoded and a plain string isn't wrapped in quotes); | ||
| * anything else is `JSON.stringify`-ed, falling back to `fallback` if that throws (e.g. on | ||
| * circular references or `BigInt`). Returns `undefined` for values `JSON.stringify` itself drops | ||
| * (top-level `undefined`, functions, symbols), matching `JSON.stringify`'s own runtime behavior. | ||
| * | ||
| * @param value the value to stringify | ||
| * @param fallback returned when serialization throws, or, if a function, called with `value` to | ||
| * produce the fallback. Defaults to `'[unserializable]'`. | ||
| */ | ||
| export function stringify( | ||
| value: unknown, | ||
| fallback: string | ((value: unknown) => string) = '[unserializable]', | ||
| ): string | undefined { | ||
| if (typeof value === 'string') { | ||
| return value; | ||
| } | ||
| try { | ||
| return JSON.stringify(value); | ||
| } catch { | ||
| return typeof fallback === 'function' ? fallback(value) : fallback; | ||
| } | ||
| } | ||
logaretm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. logaretm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * Truncates given string to the maximum characters count | ||
| * | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { truncateGenAiMessages, truncateGenAiStringInput } from '../../../src/tracing/ai/messageTruncation'; | ||
| import { getTruncatedJsonString } from '../../../src/tracing/ai/utils'; | ||
| describe('message truncation utilities', () => { | ||
| describe('truncateGenAiMessages', () => { | ||
| @@ -610,3 +611,18 @@ describe('message truncation utilities', () => { | ||
| }); | ||
| }); | ||
| }); | ||
| describe('getTruncatedJsonString', () => { | ||
| it('returns a fallback instead of throwing on circular references', () => { | ||
| const circular: Record<string, unknown> = { role: 'user', content: 'hi' }; | ||
| circular.self = circular; | ||
| expect(getTruncatedJsonString(circular)).toBe('[unserializable]'); | ||
| expect(getTruncatedJsonString([circular])).toBe('[unserializable]'); | ||
| }); | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| it('serializes normal values as before', () => { | ||
| expect(getTruncatedJsonString('hello')).toBe('hello'); | ||
| expect(getTruncatedJsonString({ a: 1 })).toBe('{"a":1}'); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.