-
Notifications
You must be signed in to change notification settings - Fork 802
support local image #1703
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
Open
quanru
wants to merge
34
commits into
main
Choose a base branch
from
feat/local-imgs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
support local image #1703
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
2fee00d
feat(report): implement directory-based report format with separate i…
quanru 05633d4
feat(report): extract base64 images to separate script tags in direct…
quanru 2814154
feat(api): add useDirectoryReport option for directory-based report f…
quanru 04eacc8
refactor(report): streamline image handling and extraction processes
quanru 68d3869
refactor(tests): update comments to English for directory report form…
quanru e93cad0
refactor(pixi-loader): remove unused file protocol checks and image l…
quanru f65c8ab
Revert "feat(components): replace anchor tags with Link components fo…
quanru b4ac5bc
feat(agent): clear base64 from dump memory after report generation to…
quanru 4dc41b1
refactor(agent, utils): enhance report generation and image processin…
quanru 6693288
refactor(api): clarify useDirectoryReport note regarding CORS restric…
quanru 6d137e1
refactor(utils): enhance base64 extraction error handling and sanitiz…
quanru a856dd5
refactor(utils): simplify error handling in extractBase64ToScriptTags…
quanru d223720
docs(api): update useDirectoryReport note to suggest using npx http-s…
quanru cce11cd
refactor(agent, utils, tests): optimize report generation and memory …
quanru c924e3a
feat(core): implement ScreenshotRegistry for efficient screenshot man…
quanru 001223d
refactor(agent, utils): optimize screenshot handling to reduce memory…
quanru f290380
feat(agent): enhance screenshot handling by integrating screenshot re…
quanru 290c214
Update packages/core/src/utils.ts
quanru 82d3d27
Update apps/report/src/App.tsx
quanru 9a4f70f
Update packages/core/tests/unit-test/screenshot-registry.test.ts
quanru a060183
Update packages/core/src/utils.ts
quanru 19a1234
fix(screenshot-registry): prevent ID gaps by incrementing counter aft…
quanru b1425de
test(screenshot-registry): add integration tests for report generatio…
quanru 428dc56
feat(screenshot-registry): enhance image handling and restore referen…
quanru f39fb28
feat(core): introduce ScreenshotItem class for handling screenshot data
quanru f90e539
fix(playground): enhance image reference restoration and update test …
quanru c2672a9
fix(api): improve directory report instructions and cleanup memory us…
quanru b267964
fix(utils): enhance filename sanitization by trimming, limiting lengt…
quanru 30075bf
Update packages/core/src/screenshot-item.ts
quanru 46ff26b
fix(core): improve error handling in ScreenshotRegistry and Screensho…
quanru b97a0d0
fix(agent): remove unnecessary screenshot registry cleanup after repo…
quanru 725d75e
fix(chore): implement type guard for ScreenshotItem validation and im…
quanru 9f83442
fix(utils): remove legacy base64 image handling and streamline image …
quanru 41ccfb0
refactor(screenshot-registry): remove legacy image reference handling…
quanru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { antiEscapeScriptTag } from '@midscene/shared/utils'; | ||
|
|
||
| // Constants matching backend definitions in packages/core/src/utils.ts | ||
| const IMAGE_SCRIPT_TYPE = 'midscene-image'; | ||
|
|
||
| /** Map of image ID to base64 data string, loaded from script tags */ | ||
| type ImageIdToBase64Map = Record<string, string>; | ||
|
|
||
| /** | ||
| * Load all image script tags into a map. | ||
| * These are base64 images that were extracted from dump JSON during report generation. | ||
| * | ||
| * @returns Map of image IDs to their base64 data | ||
| */ | ||
| export function loadImageMap(): ImageIdToBase64Map { | ||
| const scripts = document.querySelectorAll( | ||
| `script[type="${IMAGE_SCRIPT_TYPE}"]`, | ||
| ); | ||
| const map: ImageIdToBase64Map = {}; | ||
|
|
||
| scripts.forEach((script) => { | ||
| const id = script.getAttribute('data-id'); | ||
| if (id && script.textContent) { | ||
| map[id] = antiEscapeScriptTag(script.textContent.trim()); | ||
| } | ||
| }); | ||
|
|
||
| return map; | ||
| } | ||
|
|
||
| /** | ||
| * Recursively restore image references in parsed data. | ||
| * Handles { $screenshot: "id" } format. | ||
| * | ||
| * @param data - The parsed JSON data with image references | ||
| * @param imageMap - Map of image IDs to base64 data | ||
| * @returns Data with image references restored to base64 | ||
| */ | ||
| export function restoreImageReferences<T>( | ||
| data: T, | ||
| imageMap: ImageIdToBase64Map, | ||
| ): T { | ||
| if (typeof data === 'string') { | ||
| return data; | ||
| } | ||
|
|
||
| if (Array.isArray(data)) { | ||
| return data.map((item) => restoreImageReferences(item, imageMap)) as T; | ||
| } | ||
|
|
||
| if (typeof data === 'object' && data !== null) { | ||
| // Handle { $screenshot: ... } format (including empty/undefined values) | ||
| if ('$screenshot' in data) { | ||
| const screenshot = (data as { $screenshot: unknown }).$screenshot; | ||
|
|
||
| // Handle undefined or null | ||
| if (screenshot === undefined || screenshot === null) { | ||
| return '' as T; | ||
| } | ||
|
|
||
| // Handle non-string values | ||
| if (typeof screenshot !== 'string') { | ||
| console.warn('Invalid $screenshot value type:', typeof screenshot); | ||
| return '' as T; | ||
| } | ||
|
|
||
| // Handle empty string | ||
| if (screenshot.length === 0) { | ||
| return '' as T; | ||
| } | ||
|
|
||
| // Check if it's already base64 data | ||
| if (screenshot.startsWith('data:image/')) { | ||
| return screenshot as T; | ||
| } | ||
|
|
||
| // Check if it's a file path (for directory-based reports) | ||
| if (screenshot.startsWith('./') || screenshot.startsWith('/')) { | ||
| return screenshot as T; | ||
| } | ||
|
|
||
| // It's an ID, look up in imageMap | ||
| const base64 = imageMap[screenshot]; | ||
| if (base64) { | ||
| return base64 as T; | ||
| } | ||
|
|
||
| // Fallback: return the value as-is (could be a placeholder) | ||
| if (Object.keys(imageMap).length > 0) { | ||
| const availableIds = Object.keys(imageMap).join(', '); | ||
| console.warn( | ||
| `Image not found for ID: ${screenshot}. Available IDs: ${availableIds}`, | ||
| ); | ||
| } | ||
| return screenshot as T; | ||
| } | ||
|
|
||
| const result: Record<string, unknown> = {}; | ||
| for (const [key, value] of Object.entries(data)) { | ||
| result[key] = restoreImageReferences(value, imageMap); | ||
| } | ||
| return result as T; | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| export { IMAGE_SCRIPT_TYPE }; |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.