-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(core): record exact model input screenshots #2856
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { Buffer } from 'node:buffer'; | ||
| import type { ModelRuntime } from '@/ai-model/models'; | ||
| import { ScreenshotItem } from '@/screenshot-item'; | ||
| import type { ExecutionTask } from '@/types'; | ||
| import { parseBase64 } from '@midscene/shared/img'; | ||
| import { sha256Hex } from '@midscene/shared/utils'; | ||
|
|
||
| const MODEL_INPUT_TIMING = 'model-input'; | ||
|
|
||
| function screenshotContentHash(imageBase64: string): string { | ||
| const { body } = parseBase64(imageBase64); | ||
| return sha256Hex(Buffer.from(body, 'base64')); | ||
| } | ||
|
|
||
| /** | ||
| * Bind a model runtime to one report task so the report retains the exact | ||
| * data-URI bytes passed to the provider after padding/cropping/resizing. | ||
| */ | ||
| export function recordModelInputsForTask( | ||
| modelRuntime: ModelRuntime, | ||
| task: ExecutionTask, | ||
| sourceScreenshot?: ScreenshotItem, | ||
| ): ModelRuntime { | ||
| return { | ||
| ...modelRuntime, | ||
| onModelInputImages: (images) => { | ||
| modelRuntime.onModelInputImages?.(images); | ||
|
|
||
| for (const [index, imageBase64] of images.entries()) { | ||
| if (!imageBase64.startsWith('data:image/')) { | ||
| continue; | ||
| } | ||
|
|
||
| const contentHash = screenshotContentHash(imageBase64); | ||
| const alreadyRecorded = task.recorder?.some( | ||
| (item) => | ||
| item.timing === MODEL_INPUT_TIMING && | ||
| item.screenshot && | ||
| screenshotContentHash(item.screenshot.base64) === contentHash, | ||
| ); | ||
| if (alreadyRecorded) { | ||
| continue; | ||
| } | ||
|
|
||
| const uiScreenshot = task.uiContext?.screenshot ?? sourceScreenshot; | ||
| const screenshot = | ||
| uiScreenshot && | ||
| screenshotContentHash(uiScreenshot.base64) === contentHash | ||
| ? uiScreenshot | ||
| : ScreenshotItem.create(imageBase64, Date.now()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an input is WebP—as explicitly exercised by the new recorder test—this creates a AGENTS.md reference: AGENTS.md:L9-L12 Useful? React with 👍 / 👎. |
||
| const recorderItem = { | ||
| type: 'screenshot' as const, | ||
| ts: Date.now(), | ||
| screenshot, | ||
| timing: MODEL_INPUT_TIMING, | ||
| description: `Model input ${index + 1} (exact bytes, sha256: ${contentHash})`, | ||
| }; | ||
| task.recorder = [...(task.recorder ?? []), recorderItem]; | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { recordModelInputsForTask } from '@/agent/model-input-recorder'; | ||
| import type { ModelRuntime } from '@/ai-model/models'; | ||
| import { ScreenshotItem } from '@/screenshot-item'; | ||
| import type { ExecutionTask } from '@/types'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const pngBase64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'; | ||
| const webpBase64 = | ||
| 'data:image/webp;base64,UklGRjQAAABXRUJQVlA4ICgAAACQAQCdASoCAAMAAMASJQBOl0AAjNAA/v4icv1difCfoP7mxzi2QwAA'; | ||
|
|
||
| function createTask(): ExecutionTask { | ||
| const screenshot = ScreenshotItem.create(pngBase64, 100); | ||
| return { | ||
| taskId: 'task-1', | ||
| type: 'Planning', | ||
| subType: 'Plan', | ||
| status: 'running', | ||
| executor: vi.fn(), | ||
| uiContext: { | ||
| screenshot, | ||
| shotSize: { width: 5, height: 1 }, | ||
| shrunkShotToLogicalRatio: 1, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('recordModelInputsForTask', () => { | ||
| it('reuses the report screenshot when the exact bytes are sent to AI', () => { | ||
| const task = createTask(); | ||
| const runtime = recordModelInputsForTask({} as ModelRuntime, task); | ||
|
|
||
| runtime.onModelInputImages?.([pngBase64]); | ||
|
|
||
| expect(task.recorder).toHaveLength(1); | ||
| expect(task.recorder?.[0].screenshot).toBe(task.uiContext?.screenshot); | ||
| expect(task.recorder?.[0].timing).toBe('model-input'); | ||
| expect(task.recorder?.[0].description).toMatch( | ||
| /^Model input 1 \(exact bytes, sha256: [a-f0-9]{64}\)$/, | ||
| ); | ||
| }); | ||
|
|
||
| it('records a transformed model image and deduplicates request retries', () => { | ||
| const task = createTask(); | ||
| const parentCallback = vi.fn(); | ||
| const parentRuntime = {} as ModelRuntime; | ||
| parentRuntime.onModelInputImages = parentCallback; | ||
| const runtime = recordModelInputsForTask(parentRuntime, task); | ||
|
|
||
| runtime.onModelInputImages?.([webpBase64]); | ||
| runtime.onModelInputImages?.([webpBase64]); | ||
|
|
||
| expect(parentCallback).toHaveBeenCalledTimes(2); | ||
| expect(task.recorder).toHaveLength(1); | ||
| expect(task.recorder?.[0].screenshot?.base64).toBe(webpBase64); | ||
| expect(task.recorder?.[0].screenshot).not.toBe(task.uiContext?.screenshot); | ||
| }); | ||
|
|
||
| it('reuses the executor context screenshot before it is bound to the task', () => { | ||
| const task = createTask(); | ||
| const sourceScreenshot = task.uiContext!.screenshot; | ||
| task.uiContext = undefined; | ||
| const runtime = recordModelInputsForTask( | ||
| {} as ModelRuntime, | ||
| task, | ||
| sourceScreenshot, | ||
| ); | ||
|
|
||
| runtime.onModelInputImages?.([pngBase64]); | ||
|
|
||
| expect(task.recorder?.[0].screenshot).toBe(sourceScreenshot); | ||
| }); | ||
|
|
||
| it('does not turn remote reference image URLs into screenshot items', () => { | ||
| const task = createTask(); | ||
| const runtime = recordModelInputsForTask({} as ModelRuntime, task); | ||
|
|
||
| runtime.onModelInputImages?.(['https://example.com/reference.png']); | ||
|
|
||
| expect(task.recorder).toBeUndefined(); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a UIObserver assertion supplies
uiContext.screenshotSequence, extraction sends every buffered frame to the model, but this lookup considers only the representative screenshot. Each earlier frame therefore receives a newScreenshotItemID, after whichrecordAndReleaseScreenshotSequenceadds the original frame objects to the same task. Because report persistence deduplicates by ID rather than content hash, multi-frame assertions persist and render every earlier screenshot twice; match againstscreenshotSequenceas well before creating a new item.Useful? React with 👍 / 👎.