Skip to content

Commit c658229

Browse files
committed
tweak partial json parsing logic
1 parent 50d1378 commit c658229

File tree

3 files changed

+26
-41
lines changed

3 files changed

+26
-41
lines changed

common/src/util/__tests__/partial-json-delta.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ describe('getPartialJsonDelta', () => {
223223
expect(result.delta).toEqual({ name: 'test', value: 42 })
224224
expect(result.result).toEqual({ name: 'test', value: 42 })
225225
expect(result.lastParam.key).toBe('value')
226-
expect(result.lastParam.complete).toBe(false)
226+
expect(result.lastParam.complete).toBe(true)
227227
})
228228

229229
it('should detect completion of partial string value', () => {
@@ -245,7 +245,7 @@ describe('getPartialJsonDelta', () => {
245245
expect(result.delta).toEqual({ value: 42 })
246246
expect(result.result).toEqual({ name: 'test', value: 42 })
247247
expect(result.lastParam.key).toBe('value')
248-
expect(result.lastParam.complete).toBe(false)
248+
expect(result.lastParam.complete).toBe(true)
249249
})
250250

251251
it('should detect new property being added', () => {
@@ -256,7 +256,7 @@ describe('getPartialJsonDelta', () => {
256256
expect(result.delta).toEqual({ value: 100 })
257257
expect(result.result).toEqual({ name: 'test', value: 100 })
258258
expect(result.lastParam.key).toBe('value')
259-
expect(result.lastParam.complete).toBe(false)
259+
expect(result.lastParam.complete).toBe(true)
260260
})
261261
})
262262

@@ -410,7 +410,7 @@ describe('getPartialJsonDelta', () => {
410410
const result = getPartialJsonDelta(content, previous)
411411

412412
expect(result.lastParam.key).toBe('value')
413-
expect(result.lastParam.complete).toBe(false)
413+
expect(result.lastParam.complete).toBe(true)
414414
})
415415

416416
it('should indicate incomplete last parameter', () => {

common/src/util/partial-json-delta.ts

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,6 @@ export function parsePartialJsonObjectSingle(content: string): {
4141
return { lastParamComplete: true, params: {} }
4242
}
4343

44-
export function parsePartialJsonObject(
45-
content: string,
46-
previous: string
47-
): {
48-
lastParamComplete: boolean
49-
params: any
50-
prevParams: any
51-
} {
52-
const { lastParamComplete, params } = parsePartialJsonObjectSingle(content)
53-
const lastParam = Object.entries(params)[-1]
54-
55-
const { lastParamComplete: prevLastParamComplete, params: prevParams } =
56-
parsePartialJsonObjectSingle(previous)
57-
const prevLastParam = Object.entries(prevParams)[-1]
58-
59-
return {
60-
lastParamComplete:
61-
lastParam === prevLastParam
62-
? lastParamComplete && !prevLastParamComplete
63-
: lastParamComplete,
64-
params,
65-
prevParams,
66-
}
67-
}
68-
6944
export function getPartialJsonDelta(
7045
content: string,
7146
previous: string
@@ -79,18 +54,21 @@ export function getPartialJsonDelta(
7954
`Content must be previous content plus new content. Content ${JSON.stringify(content)} does not start with previous content ${JSON.stringify(previous)}`
8055
)
8156
}
82-
const {
83-
lastParamComplete,
84-
params: current,
85-
prevParams,
86-
} = parsePartialJsonObject(content, previous)
57+
const { lastParamComplete, params } = parsePartialJsonObjectSingle(content)
58+
const lastParam = Object.keys(params).pop()
8759

88-
const entries = Object.entries(current)
89-
const lastKey = (entries[entries.length - 1] ?? [undefined])[0]
60+
const { lastParamComplete: prevLastParamComplete, params: prevParams } =
61+
parsePartialJsonObjectSingle(previous)
62+
const prevLastParam = Object.keys(prevParams).pop()
63+
64+
const entries = Object.entries(params)
9065

9166
const delta: Record<string, any> = {}
92-
for (const [key, value] of Object.entries(current)) {
67+
for (const [key, value] of entries) {
9368
if (prevParams[key] === value) {
69+
if (prevLastParam === key && !prevLastParamComplete) {
70+
delta[key] = ''
71+
}
9472
continue
9573
}
9674
if (typeof value === 'string') {
@@ -102,10 +80,13 @@ export function getPartialJsonDelta(
10280

10381
return {
10482
delta,
105-
result: current,
83+
result: params,
10684
lastParam: {
107-
key: lastKey,
108-
complete: lastParamComplete,
85+
key: lastParam,
86+
complete:
87+
prevLastParam === lastParam
88+
? lastParamComplete && !prevLastParamComplete
89+
: lastParamComplete,
10990
},
11091
}
11192
}

npm-app/src/utils/xml-stream-parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ export function createXMLStreamParser(
6666
result,
6767
lastParam: { key: lastKey, complete: lastComplete },
6868
} = getPartialJsonDelta(paramsContent, prevParamsContent)
69-
if (lastKey === toolNameParam && lastComplete) {
69+
if (
70+
lastKey === toolNameParam &&
71+
lastComplete &&
72+
delta[lastKey] === undefined
73+
) {
7074
delta[lastKey] = ''
7175
}
7276
for (const [key, value] of Object.entries(delta)) {

0 commit comments

Comments
 (0)