Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(frontend): preserve numeric literals in JSON preview (#4620)

Zicheng (Axelsen) Xie865cc97fb07e
diff --git a/frontend/src/utils/filePreview.test.ts b/frontend/src/utils/filePreview.test.ts
index 2fcfeb484..9d80b5773 100644
--- a/frontend/src/utils/filePreview.test.ts
+++ b/frontend/src/utils/filePreview.test.ts
@@ -71,6 +71,35 @@ test('pretty-print helpers only rewrite canonical JSON documents', () => {
assert.equal(prettyPrintJson('not json'), 'not json')
})
+test('JSON preview preserves numeric literals instead of rounding or replacing them', () => {
+ for (const number of [
+ '9007199254740993', '-9007199254740993', '9223372036854775807',
+ '0.1234567890123456789012345', '1e400', '1e-400', '-0', '1.2300',
+ ]) {
+ assert.equal(prettyPrintJson(`{"value":${number}}`), `{\n "value": ${number}\n}`, number)
+ }
+})
+
+test('JSON preview formats nested containers without rewriting string contents', () => {
+ // Strings containing JSON punctuation and escaped quotes are indivisible tokens.
+ const ordinary = '{"items":[1,{"text":"a, b: [c] {d}","empty":[]}],"object":{},"ok":true,"nil":null}'
+ assert.equal(prettyPrintJson(ordinary), JSON.stringify(JSON.parse(ordinary), null, 2))
+ const escaped = String.raw`{"text":"comma, colon: quote\" slash\\ newline\n","id":9007199254740993}`
+ assert.equal(prettyPrintJson(escaped), String.raw`{
+ "text": "comma, colon: quote\" slash\\ newline\n",
+ "id": 9007199254740993
+}`)
+})
+
+test('JSON preview preserves top-level values and leaves invalid JSON unchanged', () => {
+ for (const value of ['9007199254740993', '1e400', '-0', 'null', 'true', '"text"', '[]', '{}']) {
+ assert.equal(prettyPrintJson(` ${value}\n`), value)
+ }
+ for (const value of [' {"id":01}', '{"id":1,}', '{"id":NaN}', '{"unfinished":', ' \n']) {
+ assert.equal(prettyPrintJson(value), value)
+ }
+})
+
test('mime and highlight maps cover skill-generated html/csv', () => {
assert.equal(getPreviewMimeType('html'), 'text/html')
assert.equal(getPreviewMimeType('csv'), 'text/csv')
diff --git a/frontend/src/utils/filePreview.ts b/frontend/src/utils/filePreview.ts
index 4991e0e67..4f7c39191 100644
--- a/frontend/src/utils/filePreview.ts
+++ b/frontend/src/utils/filePreview.ts
@@ -277,10 +277,42 @@ export function prettyPrintJson(text: string): string {
const trimmed = text.trim()
if (!trimmed) return text
try {
- return JSON.stringify(JSON.parse(trimmed), null, 2)
+ // Validate the document, but format its original tokens: serializing the
+ // parsed value rounds large IDs/decimals and turns overflowing numbers into null.
+ JSON.parse(trimmed)
} catch {
return text
}
+ const tokens = trimmed.match(/"(?:\\.|[^"\\])*"|[{}\[\],:]|[^\s{}\[\],:]+/g)!
+ const output: string[] = []
+ let depth = 0
+ const newline = () => output.push('\n', ' '.repeat(depth))
+ for (let i = 0; i < tokens.length; i++) {
+ const token = tokens[i]
+ if (token === '{' || token === '[') {
+ output.push(token)
+ const closing = token === '{' ? '}' : ']'
+ if (tokens[i + 1] === closing) {
+ output.push(closing)
+ i++
+ } else {
+ depth++
+ newline()
+ }
+ } else if (token === '}' || token === ']') {
+ depth--
+ newline()
+ output.push(token)
+ } else if (token === ',') {
+ output.push(token)
+ newline()
+ } else if (token === ':') {
+ output.push(': ')
+ } else {
+ output.push(token)
+ }
+ }
+ return output.join('')
}
/**