refactor(core): simplify model JSON parsing - #2690
Merged
Merged
Conversation
EAGzzyCSL
marked this pull request as ready for review
June 17, 2026 07:12
Deploying midscene with
|
| Latest commit: |
5c9ce49
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://6091c8e3.midscene.pages.dev |
| Branch Preview URL: | https://zzy-json-repair.midscene.pages.dev |
EAGzzyCSL
force-pushed
the
zzy/json-repair
branch
3 times, most recently
from
June 22, 2026 12:11
691cf5c to
853f6f5
Compare
EAGzzyCSL
enabled auto-merge (squash)
June 23, 2026 08:30
EAGzzyCSL
force-pushed
the
zzy/json-repair
branch
from
July 3, 2026 03:10
5c9ce49 to
982093d
Compare
EAGzzyCSL
force-pushed
the
zzy/json-repair
branch
from
July 3, 2026 03:47
982093d to
4be8f04
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors @midscene/core model-response JSON parsing into a single pipeline (extract -> jsonrepair -> parse -> post-process), routes adapter parsers (Doubao, UI-TARS) through the shared implementation, and reorganizes unit tests to concentrate JSON parsing coverage in a dedicated suite.
Changes:
- Replaced the old safe/normal JSON parsing split with
parseModelResponseJson()and updated call sites to use it. - Simplified adapter-specific JSON parsing (Doubao / UI-TARS) to use the shared parser; adapter logic remains focused on post-parse coordinate handling.
- Moved/updated unit tests: removed scattered parser tests and introduced
tests/unit-test/json.test.tsas the primary coverage location.
Reviewed changes
Copilot reviewed 22 out of 23 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pnpm-lock.yaml | Lockfile update reflecting dependency changes (incl. jsonrepair bump). |
| packages/core/package.json | Bumps jsonrepair from 3.12.0 to 3.14.0. |
| packages/core/src/ai-model/service-caller/json.ts | Introduces unified JSON parsing via parseModelResponseJson() and shared trimming normalization. |
| packages/core/src/ai-model/service-caller/index.ts | Re-exports the new shared JSON parser API. |
| packages/core/src/ai-model/prompt/extraction.ts | Switches XML extraction parsing to parseModelResponseJson() with requireObject: false. |
| packages/core/src/ai-model/models/doubao.ts | Routes Doubao JSON parsing through the shared parser (removes adapter-local fallback parser code). |
| packages/core/src/ai-model/models/ui-tars/adapter.ts | Routes UI-TARS JSON parsing through the shared parser (removes adapter-local fallback parser code). |
| packages/core/src/ai-model/model-adapter/resolve.ts | Updates default/lenient JSON parser resolution to use parseModelResponseJson(). |
| packages/core/src/ai-model/models/utils/intent.ts | Adds isLocateIntent() helper for intent checks shared by model adapters. |
| packages/core/src/ai-model/models/mimo.ts | Adjusts response_format behavior to only set JSON mode for locate intent. |
| packages/core/src/ai-model/models/kimi.ts | Sets response_format: json_object for locate intent. |
| packages/core/src/ai-model/models/gpt.ts | Uses isLocateIntent() for original image detail + sets JSON mode for locate intent. |
| packages/core/src/ai-model/models/glm.ts | Sets response_format: json_object for locate intent. |
| packages/core/src/ai-model/models/qwen.ts | Adds commented reference for JSON mode behavior considerations. |
| packages/core/tests/unit-test/utils.test.ts | Removes JSON parsing tests that were moved into the dedicated JSON test file. |
| packages/core/tests/unit-test/service-caller.test.ts | Deletes the old service-caller JSON normalization test suite (coverage moved). |
| packages/core/tests/unit-test/json.test.ts | New dedicated JSON parsing test suite (extract + parse + normalization + adapter behavior hooks). |
| packages/core/tests/unit-test/model-adapter/doubao.test.ts | Updates adapter tests to match shared-parser behavior and removes adapter-local parser helper tests. |
| packages/core/tests/unit-test/model-adapter/ui-tars/adapter-json-repair.test.ts | Updates UI-TARS adapter tests to validate shared-parser behavior directly. |
| packages/core/tests/unit-test/model-adapter/mimo.test.ts | Updates expected request config (response_format changes). |
| packages/core/tests/unit-test/model-adapter/kimi.test.ts | Adds coverage for locate intent JSON mode config. |
| packages/core/tests/unit-test/model-adapter/gpt.test.ts | Adds coverage for locate intent JSON mode config. |
| packages/core/tests/unit-test/model-adapter/glm.test.ts | Adds coverage for locate intent JSON mode config. |
Files not reviewed (1)
- pnpm-lock.yaml: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
144
to
+145
| const cleanJsonString = extractJSONFromCodeBlock(raw); | ||
| const result = parseNormalJson(cleanJsonString, raw, context); | ||
| if ( | ||
| result && | ||
| typeof result === 'object' && | ||
| 'parsed' in result && | ||
| result.parsed === undefined | ||
| ) { | ||
| throw Error( | ||
| `failed to parse LLM response into JSON. Error - ${String( | ||
| result.lastError ?? 'unknown error', | ||
| )}. Response - \n ${raw}`, | ||
| ); | ||
| const requireObject = context?.requireObject ?? true; |
Comment on lines
+98
to
+119
| it('should allow top-level non-object JSON values when object validation is disabled', () => { | ||
| expect( | ||
| parseModelResponseJson('[" todo 1 ", " todo 2 "]', { | ||
| source: 'generic-object', | ||
| requireObject: false, | ||
| }), | ||
| ).toEqual(['todo 1', 'todo 2']); | ||
|
|
||
| expect( | ||
| parseModelResponseJson('" todo list "', { | ||
| source: 'generic-object', | ||
| requireObject: false, | ||
| }), | ||
| ).toBe('todo list'); | ||
|
|
||
| expect( | ||
| parseModelResponseJson('42', { | ||
| source: 'generic-object', | ||
| requireObject: false, | ||
| }), | ||
| ).toBe(42); | ||
| }); |
yuyutaotao
approved these changes
Jul 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
parseModelResponseJsonObject.json.test.tsfile.Validation
pnpm --filter @midscene/core test -- tests/unit-test/json.test.ts tests/unit-test/utils.test.ts tests/unit-test/model-adapter/doubao.test.ts tests/unit-test/model-adapter/ui-tars.test.tspnpm --filter @midscene/core test -- --coverage tests/unit-test/json.test.tsNotes