|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { buildInboundMediaNoteProjection } from "../../../../src/auto-reply/media-note.js"; |
| 5 | +import { resolveCurrentTurnImages } from "../../../../src/auto-reply/reply/current-turn-images.js"; |
| 6 | +import type { MsgContext } from "../../../../src/auto-reply/templating.js"; |
| 7 | +import type { OpenClawConfig } from "../../../../src/config/types.js"; |
| 8 | +import { applyMediaUnderstanding } from "../../../../src/media-understanding/apply.js"; |
| 9 | +import type { MediaUnderstandingProvider } from "../../../../src/media-understanding/types.js"; |
| 10 | +import { createSolidPngBuffer } from "../../../helpers/image-fixtures.js"; |
| 11 | +import { useAutoCleanupTempDirTracker } from "../../../helpers/temp-dir.js"; |
| 12 | + |
| 13 | +const ACTIVE_PROVIDER = "openai"; |
| 14 | +const ACTIVE_VISION_MODEL = "gpt-4.1"; |
| 15 | +const PRIMARY_PROVIDER = "qa-vision-primary"; |
| 16 | +const PRIMARY_MODEL = "primary-vision"; |
| 17 | +const FALLBACK_PROVIDER = "qa-vision-fallback"; |
| 18 | +const FALLBACK_MODEL = "fallback-vision"; |
| 19 | +const SUMMARY_TEXT = "QA_VISION_FALLBACK_SUMMARY"; |
| 20 | + |
| 21 | +const preparedVisionCatalog = vi.hoisted(() => [ |
| 22 | + { |
| 23 | + id: "gpt-4.1", |
| 24 | + name: "GPT-4.1", |
| 25 | + provider: "openai", |
| 26 | + input: ["text", "image"] as const, |
| 27 | + }, |
| 28 | +]); |
| 29 | + |
| 30 | +vi.mock("../../../../src/agents/prepared-model-catalog.js", () => ({ |
| 31 | + loadPreparedModelCatalog: vi.fn(async () => preparedVisionCatalog), |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("../../../../src/plugins/capability-provider-runtime.js", () => ({ |
| 35 | + resolvePluginCapabilityProviders: () => [], |
| 36 | +})); |
| 37 | + |
| 38 | +vi.mock("../../../../src/media-understanding/image-runtime.js", () => ({ |
| 39 | + describeImageWithModel: vi.fn(), |
| 40 | + describeImagesWithModel: vi.fn(), |
| 41 | +})); |
| 42 | + |
| 43 | +function createImageContext(imagePath: string, workspaceDir: string): MsgContext { |
| 44 | + const body = "describe the attached image"; |
| 45 | + return { |
| 46 | + Body: body, |
| 47 | + CommandBody: body, |
| 48 | + RawBody: body, |
| 49 | + media: [ |
| 50 | + { |
| 51 | + path: imagePath, |
| 52 | + contentType: "image/png", |
| 53 | + kind: "image", |
| 54 | + workspaceDir, |
| 55 | + }, |
| 56 | + ], |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function configuredProvider(model: string) { |
| 61 | + return { |
| 62 | + apiKey: "test-key", // pragma: allowlist secret |
| 63 | + baseUrl: "https://example.invalid/v1", |
| 64 | + models: [{ id: model, name: model, input: ["text", "image"] }], |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function imageDecision(ctx: MsgContext) { |
| 69 | + const decision = ctx.MediaUnderstandingDecisions?.find( |
| 70 | + (candidate) => candidate.capability === "image", |
| 71 | + ); |
| 72 | + if (!decision) { |
| 73 | + throw new Error("expected an image media-understanding decision"); |
| 74 | + } |
| 75 | + return decision; |
| 76 | +} |
| 77 | + |
| 78 | +function occurrenceCount(value: string | undefined, needle: string): number { |
| 79 | + if (value === undefined) { |
| 80 | + return 0; |
| 81 | + } |
| 82 | + return value.split(needle).length - 1; |
| 83 | +} |
| 84 | + |
| 85 | +describe("core vision routing product proof", () => { |
| 86 | + const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 87 | + |
| 88 | + it("bypasses summarization and sends the real image to an active vision model", async () => { |
| 89 | + const workspaceDir = await fs.realpath(tempDirs.make("openclaw-vision-native-")); |
| 90 | + const imagePath = path.join(workspaceDir, "native.png"); |
| 91 | + const imageBytes = createSolidPngBuffer(2, 2, { r: 24, g: 96, b: 208 }); |
| 92 | + await fs.writeFile(imagePath, imageBytes); |
| 93 | + const ctx = createImageContext(imagePath, workspaceDir); |
| 94 | + let summarizerCalls = 0; |
| 95 | + const cfg = { |
| 96 | + agents: { |
| 97 | + defaults: { |
| 98 | + imageModel: { |
| 99 | + primary: `${FALLBACK_PROVIDER}/${FALLBACK_MODEL}`, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + models: { |
| 104 | + providers: { |
| 105 | + [ACTIVE_PROVIDER]: configuredProvider(ACTIVE_VISION_MODEL), |
| 106 | + [FALLBACK_PROVIDER]: configuredProvider(FALLBACK_MODEL), |
| 107 | + }, |
| 108 | + }, |
| 109 | + } as unknown as OpenClawConfig; |
| 110 | + const providers = { |
| 111 | + [FALLBACK_PROVIDER]: { |
| 112 | + id: FALLBACK_PROVIDER, |
| 113 | + capabilities: ["image"], |
| 114 | + describeImage: async () => { |
| 115 | + summarizerCalls += 1; |
| 116 | + return { text: SUMMARY_TEXT, model: FALLBACK_MODEL }; |
| 117 | + }, |
| 118 | + } satisfies MediaUnderstandingProvider, |
| 119 | + }; |
| 120 | + |
| 121 | + const applied = await applyMediaUnderstanding({ |
| 122 | + ctx, |
| 123 | + cfg, |
| 124 | + agentId: "qa-vision-native", |
| 125 | + agentDir: workspaceDir, |
| 126 | + workspaceDir, |
| 127 | + providers, |
| 128 | + activeModel: { provider: ACTIVE_PROVIDER, model: ACTIVE_VISION_MODEL }, |
| 129 | + }); |
| 130 | + |
| 131 | + const decision = imageDecision(ctx); |
| 132 | + expect(decision.outcome).toBe("skipped"); |
| 133 | + expect(decision.attachments).toEqual([ |
| 134 | + { |
| 135 | + attachmentIndex: 0, |
| 136 | + attempts: [ |
| 137 | + { |
| 138 | + type: "provider", |
| 139 | + provider: ACTIVE_PROVIDER, |
| 140 | + model: ACTIVE_VISION_MODEL, |
| 141 | + outcome: "skipped", |
| 142 | + reason: "primary model supports vision natively", |
| 143 | + }, |
| 144 | + ], |
| 145 | + chosen: { |
| 146 | + type: "provider", |
| 147 | + provider: ACTIVE_PROVIDER, |
| 148 | + model: ACTIVE_VISION_MODEL, |
| 149 | + outcome: "skipped", |
| 150 | + reason: "primary model supports vision natively", |
| 151 | + }, |
| 152 | + }, |
| 153 | + ]); |
| 154 | + expect(applied.appliedImage).toBe(false); |
| 155 | + expect(applied.outputs).toHaveLength(0); |
| 156 | + expect(summarizerCalls).toBe(0); |
| 157 | + expect(ctx.Body).not.toContain(SUMMARY_TEXT); |
| 158 | + expect(ctx.Body).not.toContain("[Image]"); |
| 159 | + |
| 160 | + const projection = buildInboundMediaNoteProjection(ctx); |
| 161 | + expect(projection.media[0]).not.toHaveProperty("hydrationSuppressed"); |
| 162 | + |
| 163 | + const currentTurnImages = await resolveCurrentTurnImages({ ctx, cfg }); |
| 164 | + expect(currentTurnImages).toEqual({ |
| 165 | + images: [ |
| 166 | + { |
| 167 | + type: "image", |
| 168 | + data: imageBytes.toString("base64"), |
| 169 | + mimeType: "image/png", |
| 170 | + }, |
| 171 | + ], |
| 172 | + imageOrder: ["inline"], |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it("falls back in order, inserts one summary, and suppresses the described raw image", async () => { |
| 177 | + const workspaceDir = await fs.realpath(tempDirs.make("openclaw-vision-fallback-")); |
| 178 | + const imagePath = path.join(workspaceDir, "fallback.png"); |
| 179 | + await fs.writeFile(imagePath, createSolidPngBuffer(2, 2, { r: 208, g: 64, b: 24 })); |
| 180 | + const ctx = createImageContext(imagePath, workspaceDir); |
| 181 | + const providerCalls: string[] = []; |
| 182 | + const cfg = { |
| 183 | + tools: { |
| 184 | + media: { |
| 185 | + models: [ |
| 186 | + { |
| 187 | + provider: PRIMARY_PROVIDER, |
| 188 | + model: PRIMARY_MODEL, |
| 189 | + capabilities: ["image"], |
| 190 | + }, |
| 191 | + { |
| 192 | + provider: FALLBACK_PROVIDER, |
| 193 | + model: FALLBACK_MODEL, |
| 194 | + capabilities: ["image"], |
| 195 | + }, |
| 196 | + ], |
| 197 | + }, |
| 198 | + }, |
| 199 | + models: { |
| 200 | + providers: { |
| 201 | + [PRIMARY_PROVIDER]: configuredProvider(PRIMARY_MODEL), |
| 202 | + [FALLBACK_PROVIDER]: configuredProvider(FALLBACK_MODEL), |
| 203 | + }, |
| 204 | + }, |
| 205 | + } as unknown as OpenClawConfig; |
| 206 | + const providers = { |
| 207 | + [PRIMARY_PROVIDER]: { |
| 208 | + id: PRIMARY_PROVIDER, |
| 209 | + capabilities: ["image"], |
| 210 | + describeImage: async () => { |
| 211 | + providerCalls.push(PRIMARY_PROVIDER); |
| 212 | + throw new Error("primary vision unavailable"); |
| 213 | + }, |
| 214 | + } satisfies MediaUnderstandingProvider, |
| 215 | + [FALLBACK_PROVIDER]: { |
| 216 | + id: FALLBACK_PROVIDER, |
| 217 | + capabilities: ["image"], |
| 218 | + describeImage: async (request) => { |
| 219 | + providerCalls.push(FALLBACK_PROVIDER); |
| 220 | + return { text: SUMMARY_TEXT, model: request.model }; |
| 221 | + }, |
| 222 | + } satisfies MediaUnderstandingProvider, |
| 223 | + }; |
| 224 | + |
| 225 | + const applied = await applyMediaUnderstanding({ |
| 226 | + ctx, |
| 227 | + cfg, |
| 228 | + agentId: "qa-vision-fallback", |
| 229 | + agentDir: workspaceDir, |
| 230 | + workspaceDir, |
| 231 | + providers, |
| 232 | + }); |
| 233 | + |
| 234 | + const decision = imageDecision(ctx); |
| 235 | + expect(providerCalls).toEqual([PRIMARY_PROVIDER, FALLBACK_PROVIDER]); |
| 236 | + expect(decision.outcome).toBe("success"); |
| 237 | + expect(decision.attachments[0]?.attempts).toEqual([ |
| 238 | + expect.objectContaining({ |
| 239 | + type: "provider", |
| 240 | + provider: PRIMARY_PROVIDER, |
| 241 | + model: PRIMARY_MODEL, |
| 242 | + outcome: "failed", |
| 243 | + reason: expect.stringContaining("primary vision unavailable"), |
| 244 | + }), |
| 245 | + { |
| 246 | + type: "provider", |
| 247 | + provider: FALLBACK_PROVIDER, |
| 248 | + model: FALLBACK_MODEL, |
| 249 | + outcome: "success", |
| 250 | + }, |
| 251 | + ]); |
| 252 | + expect(decision.attachments[0]?.chosen).toEqual({ |
| 253 | + type: "provider", |
| 254 | + provider: FALLBACK_PROVIDER, |
| 255 | + model: FALLBACK_MODEL, |
| 256 | + outcome: "success", |
| 257 | + }); |
| 258 | + expect(applied.outputs).toEqual([ |
| 259 | + { |
| 260 | + kind: "image.description", |
| 261 | + attachmentIndex: 0, |
| 262 | + text: SUMMARY_TEXT, |
| 263 | + provider: FALLBACK_PROVIDER, |
| 264 | + model: FALLBACK_MODEL, |
| 265 | + }, |
| 266 | + ]); |
| 267 | + expect(applied.appliedImage).toBe(true); |
| 268 | + expect(ctx.MediaUnderstanding).toEqual(applied.outputs); |
| 269 | + expect(occurrenceCount(ctx.Body, SUMMARY_TEXT)).toBe(1); |
| 270 | + |
| 271 | + const projection = buildInboundMediaNoteProjection(ctx); |
| 272 | + expect(projection.media).toEqual([ |
| 273 | + expect.objectContaining({ |
| 274 | + path: imagePath, |
| 275 | + contentType: "image/png", |
| 276 | + hydrationSuppressed: true, |
| 277 | + }), |
| 278 | + ]); |
| 279 | + await expect(resolveCurrentTurnImages({ ctx, cfg })).resolves.toEqual({}); |
| 280 | + }); |
| 281 | +}); |
0 commit comments