The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import { |
| 4 | ANTIGRAVITY_DEFAULT_MODEL, |
| 5 | PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, |
| 6 | type ChatAttachment, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FileSystem from "effect/FileSystem"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import * as EffectAcpErrors from "effect-acp/errors"; |
| 13 | import type * as EffectAcpSchema from "effect-acp/compat"; |
| 14 | |
| 15 | import { resolveAttachmentPath } from "../../attachmentStore.ts"; |
| 16 | import { |
| 17 | applyAntigravityAcpModelSelection, |
| 18 | antigravityClientInfo, |
| 19 | antigravityPermissionDisposition, |
| 20 | antigravitySessionMeta, |
| 21 | antigravitySessionMode, |
| 22 | buildAntigravityPrompt, |
| 23 | } from "./AntigravityAcpSupport.ts"; |
| 24 | |
| 25 | const modelConfig = { |
| 26 | id: "model", |
| 27 | name: "Model", |
| 28 | type: "select", |
| 29 | currentValue: "gemini-default", |
| 30 | options: [ |
| 31 | { value: "gemini-default", name: "Gemini default" }, |
| 32 | { value: "gemini-saved", name: "Gemini saved" }, |
| 33 | ], |
| 34 | } satisfies EffectAcpSchema.SessionConfigOption; |
| 35 | |
| 36 | function makeModelRuntime( |
| 37 | configOptions: ReadonlyArray<EffectAcpSchema.SessionConfigOption> = [modelConfig], |
| 38 | failure?: EffectAcpErrors.AcpError, |
| 39 | ) { |
| 40 | const selections: string[] = []; |
| 41 | const setModel = Effect.fn("AntigravityAcpSupportTest.setModel")(function* (model: string) { |
| 42 | if (failure) return yield* failure; |
| 43 | selections.push(model); |
| 44 | }); |
| 45 | const setConfigOption = Effect.fn("AntigravityAcpSupportTest.setConfigOption")(function* ( |
| 46 | configId: string, |
| 47 | value: string | boolean, |
| 48 | options?: { readonly allowUnlistedValue?: boolean }, |
| 49 | ) { |
| 50 | selections.push( |
| 51 | `${configId}=${String(value)}${options?.allowUnlistedValue === true ? " (unlisted)" : ""}`, |
| 52 | ); |
| 53 | return { configOptions }; |
| 54 | }); |
| 55 | return { |
| 56 | runtime: { getConfigOptions: Effect.succeed(configOptions), setModel, setConfigOption }, |
| 57 | selections, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | describe("Antigravity release gates", () => { |
| 62 | it.each([ |
| 63 | { version: "1.5.0", ownName: false, advertisedAppend: false }, |
| 64 | { version: "1.5.1", ownName: true, advertisedAppend: false }, |
| 65 | { version: "1.5.7", ownName: true, advertisedAppend: true }, |
| 66 | { version: undefined, ownName: false, advertisedAppend: false }, |
| 67 | { version: "unknown", ownName: false, advertisedAppend: false }, |
| 68 | ])( |
| 69 | "$version: advertised system append $advertisedAppend, own client name $ownName", |
| 70 | ({ version, ownName, advertisedAppend }) => { |
| 71 | const initialize = { |
| 72 | protocolVersion: 1, |
| 73 | ...(advertisedAppend |
| 74 | ? { agentCapabilities: { _meta: { antigravity: { systemPrompt: { append: true } } } } } |
| 75 | : {}), |
| 76 | ...(version === undefined |
| 77 | ? {} |
| 78 | : { agentInfo: { name: "Antigravity ACP Server", version } }), |
| 79 | }; |
| 80 | // Helpers send no append. |
| 81 | expect(antigravitySessionMeta(initialize)).toBeUndefined(); |
| 82 | expect(antigravitySessionMeta(initialize, "<pull_request_linking/>")).toEqual( |
| 83 | advertisedAppend |
| 84 | ? { antigravity: { systemPrompt: { append: "<pull_request_linking/>" } } } |
| 85 | : undefined, |
| 86 | ); |
| 87 | expect(antigravityClientInfo(version).name).toBe(ownName ? "t3-code" : "zed"); |
| 88 | }, |
| 89 | ); |
| 90 | }); |
| 91 | |
| 92 | describe("Antigravity Auto", () => { |
| 93 | it.each([ |
| 94 | { offered: ["default", "accept-edits", "auto", "plan"], expected: "auto" }, |
| 95 | { offered: ["default", "accept-edits", "plan"], expected: "default" }, |
| 96 | ])("runs as $expected when the session offers $offered", ({ offered, expected }) => { |
| 97 | const modeState = { |
| 98 | currentModeId: "default", |
| 99 | availableModes: offered.map((id) => ({ id, name: id })), |
| 100 | }; |
| 101 | expect(antigravitySessionMode("auto", modeState)).toBe(expected); |
| 102 | }); |
| 103 | |
| 104 | it.each(["execute", "read"] as const)("asks about the %s requests agy's Auto sends", (kind) => { |
| 105 | const request = { |
| 106 | sessionId: "session-1", |
| 107 | toolCall: { toolCallId: "call-1", title: "Tool call", kind }, |
| 108 | options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }], |
| 109 | } satisfies EffectAcpSchema.RequestPermissionRequest; |
| 110 | expect(antigravityPermissionDisposition({ runtimeMode: "auto", cwd: null }, request)).toBe( |
| 111 | "ask", |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | it.each([{ type: "readOnly" }, { type: "workspaceWrite", writableRoots: [] }])( |
| 116 | "keeps the $type sandbox's denials in Auto", |
| 117 | (sandboxPolicy) => { |
| 118 | const request = { |
| 119 | sessionId: "session-1", |
| 120 | toolCall: { |
| 121 | toolCallId: "call-1", |
| 122 | title: "Edit", |
| 123 | kind: "edit", |
| 124 | locations: [{ path: "/outside-workspace/file.txt" }], |
| 125 | }, |
| 126 | options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }], |
| 127 | } satisfies EffectAcpSchema.RequestPermissionRequest; |
| 128 | expect( |
| 129 | antigravityPermissionDisposition( |
| 130 | { runtimeMode: "auto", cwd: "/workspace", sandboxPolicy }, |
| 131 | request, |
| 132 | ), |
| 133 | ).toBe("deny"); |
| 134 | }, |
| 135 | ); |
| 136 | }); |
| 137 | |
| 138 | describe("applyAntigravityAcpModelSelection", () => { |
| 139 | it.effect("restores the saved model instead of the cold-resume default", () => |
| 140 | Effect.gen(function* () { |
| 141 | const { runtime, selections } = makeModelRuntime(); |
| 142 | const model = yield* applyAntigravityAcpModelSelection({ |
| 143 | runtime, |
| 144 | model: "gemini-saved", |
| 145 | mapError: (cause) => cause, |
| 146 | }); |
| 147 | |
| 148 | expect(model).toBe("gemini-saved"); |
| 149 | expect(selections).toEqual(["gemini-saved"]); |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | it.effect.each([undefined, null, ANTIGRAVITY_DEFAULT_MODEL])( |
| 154 | "uses the native default for %s without sending an internal model ID", |
| 155 | (requestedModel) => |
| 156 | Effect.gen(function* () { |
| 157 | const { runtime, selections } = makeModelRuntime(); |
| 158 | const model = yield* applyAntigravityAcpModelSelection({ |
| 159 | runtime, |
| 160 | model: requestedModel, |
| 161 | mapError: (cause) => cause, |
| 162 | }); |
| 163 | |
| 164 | expect(model).toBe("gemini-default"); |
| 165 | expect(selections).toEqual([]); |
| 166 | }), |
| 167 | ); |
| 168 | |
| 169 | it.effect("selects the manifest default for the alias when the account offers it", () => |
| 170 | Effect.gen(function* () { |
| 171 | const { runtime, selections } = makeModelRuntime(); |
| 172 | const model = yield* applyAntigravityAcpModelSelection({ |
| 173 | runtime, |
| 174 | model: ANTIGRAVITY_DEFAULT_MODEL, |
| 175 | defaultModel: "gemini-saved", |
| 176 | mapError: (cause) => cause, |
| 177 | }); |
| 178 | expect(model).toBe("gemini-saved"); |
| 179 | expect(selections).toEqual(["gemini-saved"]); |
| 180 | |
| 181 | const { runtime: other, selections: otherSelections } = makeModelRuntime(); |
| 182 | const fallback = yield* applyAntigravityAcpModelSelection({ |
| 183 | runtime: other, |
| 184 | model: ANTIGRAVITY_DEFAULT_MODEL, |
| 185 | defaultModel: "gemini-not-offered", |
| 186 | mapError: (cause) => cause, |
| 187 | }); |
| 188 | expect(fallback).toBe("gemini-default"); |
| 189 | expect(otherSelections).toEqual([]); |
| 190 | }), |
| 191 | ); |
| 192 | |
| 193 | it.effect.each(["gemini-removed", "Gemini saved", "gemini-saved[reasoning=high]"])( |
| 194 | "rejects unavailable or non-native model ID %s without selecting a fallback", |
| 195 | (model) => |
| 196 | Effect.gen(function* () { |
| 197 | const { runtime, selections } = makeModelRuntime(); |
| 198 | const error = yield* applyAntigravityAcpModelSelection({ |
| 199 | runtime, |
| 200 | model, |
| 201 | mapError: (cause) => cause, |
| 202 | }).pipe(Effect.flip); |
| 203 | |
| 204 | expect(error).toMatchObject({ |
| 205 | _tag: "AcpRequestError", |
| 206 | code: -32602, |
| 207 | errorMessage: expect.stringContaining(`'${model}' is unavailable`), |
| 208 | }); |
| 209 | expect(selections).toEqual([]); |
| 210 | }), |
| 211 | ); |
| 212 | |
| 213 | it.effect("accepts exact model IDs from grouped native options", () => |
| 214 | Effect.gen(function* () { |
| 215 | const { runtime, selections } = makeModelRuntime([ |
| 216 | { |
| 217 | ...modelConfig, |
| 218 | options: [{ groupId: "gemini", name: "Gemini", options: modelConfig.options }], |
| 219 | }, |
| 220 | ]); |
| 221 | const model = yield* applyAntigravityAcpModelSelection({ |
| 222 | runtime, |
| 223 | model: "gemini-saved", |
| 224 | mapError: (cause) => cause, |
| 225 | }); |
| 226 | |
| 227 | expect(model).toBe("gemini-saved"); |
| 228 | expect(selections).toEqual(["gemini-saved"]); |
| 229 | }), |
| 230 | ); |
| 231 | |
| 232 | it.effect("passes a model ID saved on runtime 1.3 to the agent as its 1.4 family", () => |
| 233 | Effect.gen(function* () { |
| 234 | // The cold-resume model has no thinking option, so the level is unknown locally. |
| 235 | const { runtime, selections } = makeModelRuntime([ |
| 236 | { |
| 237 | ...modelConfig, |
| 238 | currentValue: "gpt-oss-120b-medium", |
| 239 | options: [ |
| 240 | { value: "gemini-3.8-flash", name: "Gemini 3.8 Flash" }, |
| 241 | { value: "gpt-oss-120b-medium", name: "GPT-OSS 120B (Medium)" }, |
| 242 | ], |
| 243 | }, |
| 244 | ]); |
| 245 | const model = yield* applyAntigravityAcpModelSelection({ |
| 246 | runtime, |
| 247 | model: "gemini-3.8-flash-low", |
| 248 | mapError: (cause) => cause, |
| 249 | }); |
| 250 | |
| 251 | expect(model).toBe("gemini-3.8-flash"); |
| 252 | expect(selections).toEqual(["model=gemini-3.8-flash-low (unlisted)"]); |
| 253 | }), |
| 254 | ); |
| 255 | }); |
| 256 | |
| 257 | const imageAttachment = { |
| 258 | type: "image", |
| 259 | id: "thread-00000000-0000-4000-8000-000000000001", |
| 260 | name: "screen.png", |
| 261 | mimeType: "image/png", |
| 262 | sizeBytes: 1, |
| 263 | } satisfies ChatAttachment; |
| 264 | |
| 265 | const textAttachment = { |
| 266 | type: "file", |
| 267 | id: "thread-00000000-0000-4000-8000-000000000002-tsx", |
| 268 | name: "example.tsx", |
| 269 | mimeType: "application/octet-stream", |
| 270 | sizeBytes: 1, |
| 271 | } satisfies ChatAttachment; |
| 272 | |
| 273 | const pdfAttachment = { |
| 274 | type: "file", |
| 275 | id: "thread-00000000-0000-4000-8000-000000000003-pdf", |
| 276 | name: "report.pdf", |
| 277 | mimeType: "application/pdf", |
| 278 | sizeBytes: 1, |
| 279 | } satisfies ChatAttachment; |
| 280 | |
| 281 | const makeAttachmentFixture = Effect.fn("AntigravityAcpSupportTest.makeAttachmentFixture")( |
| 282 | function* () { |
| 283 | const fs = yield* FileSystem.FileSystem; |
| 284 | const path = yield* Path.Path; |
| 285 | const attachmentsDir = yield* fs.makeTempDirectoryScoped({ |
| 286 | prefix: "t3-antigravity-attachments-", |
| 287 | }); |
| 288 | const write = Effect.fn("AntigravityAcpSupportTest.writeAttachment")(function* ( |
| 289 | attachment: ChatAttachment, |
| 290 | content: string | Uint8Array, |
| 291 | ) { |
| 292 | const filePath = resolveAttachmentPath({ attachmentsDir, attachment }); |
| 293 | if (filePath === null) throw new Error("Invalid test attachment path."); |
| 294 | if (typeof content === "string") yield* fs.writeFileString(filePath, content); |
| 295 | else yield* fs.writeFile(filePath, content); |
| 296 | return { filePath, uri: (yield* path.toFileUrl(filePath)).href }; |
| 297 | }); |
| 298 | const resolve = (attachment: ChatAttachment) => |
| 299 | resolveAttachmentPath({ attachmentsDir, attachment }); |
| 300 | return { fs, attachmentsDir, resolve, write }; |
| 301 | }, |
| 302 | ); |
| 303 | |
| 304 | it.layer(NodeServices.layer)("buildAntigravityPrompt", (it) => { |
| 305 | it.effect("sends image bytes as native image content alongside the user prompt", () => |
| 306 | Effect.gen(function* () { |
| 307 | const fixture = yield* makeAttachmentFixture(); |
| 308 | const bytes = Buffer.from( |
| 309 | "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jY9kAAAAASUVORK5CYII=", |
| 310 | "base64", |
| 311 | ); |
| 312 | yield* fixture.write(imageAttachment, bytes); |
| 313 | const prompt = yield* buildAntigravityPrompt({ |
| 314 | input: " Explain this image. ", |
| 315 | attachments: [imageAttachment], |
| 316 | resolveAttachmentPath: fixture.resolve, |
| 317 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 318 | }); |
| 319 | |
| 320 | expect(prompt).toEqual([ |
| 321 | { type: "text", text: "Explain this image." }, |
| 322 | { type: "image", data: bytes.toString("base64"), mimeType: "image/png" }, |
| 323 | ]); |
| 324 | }), |
| 325 | ); |
| 326 | |
| 327 | it.effect.each(["utf8", "utf16le", "utf16be"] as const)( |
| 328 | "embeds %s code from the selected environment and keeps the upload in place", |
| 329 | (encoding) => |
| 330 | Effect.gen(function* () { |
| 331 | const selectedEnvironment = yield* makeAttachmentFixture(); |
| 332 | const otherEnvironment = yield* makeAttachmentFixture(); |
| 333 | const source = ' const name = "caf\u00e9 🚀";\r\n'; |
| 334 | const bytes = |
| 335 | encoding === "utf8" ? Buffer.from(source) : Buffer.from("\ufeff" + source, "utf16le"); |
| 336 | if (encoding === "utf16be") bytes.swap16(); |
| 337 | const upload = yield* selectedEnvironment.write(textAttachment, bytes); |
| 338 | yield* otherEnvironment.write(textAttachment, "Different environment."); |
| 339 | const prompt = yield* buildAntigravityPrompt({ |
| 340 | input: undefined, |
| 341 | attachments: [textAttachment], |
| 342 | resolveAttachmentPath: selectedEnvironment.resolve, |
| 343 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 344 | }); |
| 345 | |
| 346 | expect(prompt).toEqual([ |
| 347 | { |
| 348 | type: "resource", |
| 349 | resource: { uri: upload.uri, mimeType: "application/octet-stream", text: source }, |
| 350 | }, |
| 351 | ]); |
| 352 | expect(Buffer.from(yield* selectedEnvironment.fs.readFile(upload.filePath))).toEqual(bytes); |
| 353 | expect( |
| 354 | yield* selectedEnvironment.fs.readDirectory(selectedEnvironment.attachmentsDir), |
| 355 | ).toHaveLength(1); |
| 356 | }), |
| 357 | ); |
| 358 | |
| 359 | it.effect("keeps folded clipboard text out of native context", () => |
| 360 | Effect.gen(function* () { |
| 361 | const fixture = yield* makeAttachmentFixture(); |
| 362 | const pastedText = { |
| 363 | ...textAttachment, |
| 364 | name: "pasted-text.txt", |
| 365 | mimeType: "text/plain", |
| 366 | source: { _tag: "pasted-text" as const }, |
| 367 | } satisfies ChatAttachment; |
| 368 | yield* fixture.write(pastedText, "A very large crash report"); |
| 369 | |
| 370 | const prompt = yield* buildAntigravityPrompt({ |
| 371 | input: "Inspect the pasted text only as needed.", |
| 372 | attachments: [pastedText], |
| 373 | resolveAttachmentPath: fixture.resolve, |
| 374 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 375 | }); |
| 376 | |
| 377 | expect(prompt).toEqual([{ type: "text", text: "Inspect the pasted text only as needed." }]); |
| 378 | }), |
| 379 | ); |
| 380 | |
| 381 | it.effect.each([ |
| 382 | { name: "archive.zip", mimeType: "application/zip" }, |
| 383 | { name: "clip.mp4", mimeType: "video/mp4" }, |
| 384 | { name: "recording.aiff", mimeType: "audio/aiff" }, |
| 385 | { name: "large.pdf", mimeType: "application/pdf", sizeBytes: 75_000_000 }, |
| 386 | { name: "large.txt", mimeType: "text/plain", sizeBytes: 1024 * 1024 + 1 }, |
| 387 | { name: "large.wav", mimeType: "audio/wav", sizeBytes: 20 * 1024 * 1024 + 1 }, |
| 388 | ])("keeps $name as a file path without reading or spending the native media budget", (file) => |
| 389 | Effect.gen(function* () { |
| 390 | const fixture = yield* makeAttachmentFixture(); |
| 391 | const attachment = { ...textAttachment, sizeBytes: 50 * 1024 * 1024, ...file }; |
| 392 | const upload = yield* fixture.write(attachment, ""); |
| 393 | yield* fixture.fs.truncate(upload.filePath, attachment.sizeBytes); |
| 394 | const pdf = yield* fixture.write(pdfAttachment, ""); |
| 395 | yield* fixture.fs.truncate(pdf.filePath, 50 * 1024 * 1024); |
| 396 | const input = `Inspect the file at ${upload.filePath}`; |
| 397 | const prompt = yield* buildAntigravityPrompt({ |
| 398 | input, |
| 399 | attachments: [attachment, pdfAttachment], |
| 400 | resolveAttachmentPath: fixture.resolve, |
| 401 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 402 | }).pipe( |
| 403 | Effect.provideService(FileSystem.FileSystem, { |
| 404 | ...fixture.fs, |
| 405 | stream: () => { |
| 406 | throw new Error("Path attachments must not be read into the prompt"); |
| 407 | }, |
| 408 | }), |
| 409 | ); |
| 410 | expect(prompt).toEqual([ |
| 411 | { type: "text", text: input }, |
| 412 | { |
| 413 | type: "resource_link", |
| 414 | uri: pdf.uri, |
| 415 | name: "report.pdf", |
| 416 | mimeType: "application/pdf", |
| 417 | size: 50 * 1024 * 1024, |
| 418 | }, |
| 419 | ]); |
| 420 | }), |
| 421 | ); |
| 422 | |
| 423 | it.effect.each([undefined, {}, { image: false, audio: false, embeddedContext: false }])( |
| 424 | "keeps attachments path-only when native prompt capabilities are absent: %s", |
| 425 | (promptCapabilities) => |
| 426 | Effect.gen(function* () { |
| 427 | const fixture = yield* makeAttachmentFixture(); |
| 428 | const audio = { |
| 429 | ...textAttachment, |
| 430 | id: "unsupported-audio", |
| 431 | name: "audio.wav", |
| 432 | mimeType: "audio/wav", |
| 433 | }; |
| 434 | const gifAttachment = { |
| 435 | ...imageAttachment, |
| 436 | id: "unsupported-gif", |
| 437 | name: "animation.gif", |
| 438 | mimeType: "image/gif", |
| 439 | }; |
| 440 | const gif = yield* fixture.write(gifAttachment, "GIF89a"); |
| 441 | const image = yield* fixture.write(imageAttachment, "pixels"); |
| 442 | const pdf = yield* fixture.write(pdfAttachment, "%PDF-1.7"); |
| 443 | const text = yield* fixture.write(textAttachment, "hello"); |
| 444 | const sound = yield* fixture.write(audio, "RIFF"); |
| 445 | const input = `Read ${image.filePath}, ${gif.filePath}, ${pdf.filePath}, ${text.filePath}, and ${sound.filePath}`; |
| 446 | const prompt = yield* buildAntigravityPrompt({ |
| 447 | input, |
| 448 | attachments: [imageAttachment, gifAttachment, pdfAttachment, textAttachment, audio], |
| 449 | resolveAttachmentPath: fixture.resolve, |
| 450 | promptCapabilities, |
| 451 | }).pipe( |
| 452 | Effect.provideService(FileSystem.FileSystem, { |
| 453 | ...fixture.fs, |
| 454 | stream: () => |
| 455 | Effect.die("Unnegotiated attachments must not be read").pipe(Stream.fromEffect), |
| 456 | }), |
| 457 | ); |
| 458 | expect(prompt).toEqual([{ type: "text", text: input }]); |
| 459 | expect( |
| 460 | yield* buildAntigravityPrompt({ |
| 461 | input: "Continue", |
| 462 | attachments: [], |
| 463 | resolveAttachmentPath: fixture.resolve, |
| 464 | promptCapabilities, |
| 465 | }), |
| 466 | ).toEqual([{ type: "text", text: "Continue" }]); |
| 467 | }), |
| 468 | ); |
| 469 | |
| 470 | it.effect("sends supported audio files as native audio content", () => |
| 471 | Effect.gen(function* () { |
| 472 | const fixture = yield* makeAttachmentFixture(); |
| 473 | const audioAttachment = { |
| 474 | ...textAttachment, |
| 475 | id: "recording-1", |
| 476 | name: "recording.wav", |
| 477 | mimeType: "audio/wav", |
| 478 | } satisfies ChatAttachment; |
| 479 | const bytes = Buffer.from("RIFF....WAVEfmt ", "latin1"); |
| 480 | yield* fixture.write(audioAttachment, bytes); |
| 481 | const prompt = yield* buildAntigravityPrompt({ |
| 482 | input: "Transcribe this.", |
| 483 | attachments: [audioAttachment], |
| 484 | resolveAttachmentPath: fixture.resolve, |
| 485 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 486 | }); |
| 487 | |
| 488 | expect(prompt).toEqual([ |
| 489 | { type: "text", text: "Transcribe this." }, |
| 490 | { type: "audio", data: bytes.toString("base64"), mimeType: "audio/wav" }, |
| 491 | ]); |
| 492 | }), |
| 493 | ); |
| 494 | |
| 495 | it.effect.each([{ image: true }])( |
| 496 | "keeps unsupported image formats path-only with capabilities %s", |
| 497 | (promptCapabilities) => |
| 498 | Effect.gen(function* () { |
| 499 | const fixture = yield* makeAttachmentFixture(); |
| 500 | const attachment = { ...imageAttachment, name: "animation.gif", mimeType: "image/gif" }; |
| 501 | const upload = yield* fixture.write(attachment, "GIF89a"); |
| 502 | const input = `Analyze the image at ${upload.filePath}`; |
| 503 | const prompt = yield* buildAntigravityPrompt({ |
| 504 | input, |
| 505 | attachments: [attachment], |
| 506 | resolveAttachmentPath: fixture.resolve, |
| 507 | promptCapabilities, |
| 508 | }).pipe( |
| 509 | Effect.provideService(FileSystem.FileSystem, { |
| 510 | ...fixture.fs, |
| 511 | stream: () => Effect.die("Unsupported images must not be read").pipe(Stream.fromEffect), |
| 512 | }), |
| 513 | ); |
| 514 | |
| 515 | expect(prompt).toEqual([{ type: "text", text: input }]); |
| 516 | }), |
| 517 | ); |
| 518 | |
| 519 | it.effect("rejects oversized images using file size instead of upload metadata", () => |
| 520 | Effect.gen(function* () { |
| 521 | const fixture = yield* makeAttachmentFixture(); |
| 522 | const upload = yield* fixture.write(imageAttachment, ""); |
| 523 | yield* fixture.fs.truncate(upload.filePath, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1); |
| 524 | const error = yield* buildAntigravityPrompt({ |
| 525 | input: "Read this attachment.", |
| 526 | attachments: [imageAttachment], |
| 527 | resolveAttachmentPath: fixture.resolve, |
| 528 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 529 | }).pipe(Effect.flip); |
| 530 | expect(error).toMatchObject({ |
| 531 | _tag: "AcpRequestError", |
| 532 | code: -32602, |
| 533 | errorMessage: expect.stringContaining("'screen.png' is too large"), |
| 534 | }); |
| 535 | }), |
| 536 | ); |
| 537 | |
| 538 | it.effect("keeps PDF overflow on the file-path route", () => |
| 539 | Effect.gen(function* () { |
| 540 | const fixture = yield* makeAttachmentFixture(); |
| 541 | const secondAttachment = { |
| 542 | ...pdfAttachment, |
| 543 | id: `${pdfAttachment.id}-second`, |
| 544 | name: "second.pdf", |
| 545 | }; |
| 546 | const first = yield* fixture.write(pdfAttachment, ""); |
| 547 | const second = yield* fixture.write(secondAttachment, ""); |
| 548 | yield* fixture.fs.truncate(first.filePath, (50 * 1024 * 1024) / 2); |
| 549 | yield* fixture.fs.truncate(second.filePath, (50 * 1024 * 1024) / 2); |
| 550 | const input = { |
| 551 | input: `Read ${first.filePath} and ${second.filePath}`, |
| 552 | attachments: [pdfAttachment, secondAttachment], |
| 553 | resolveAttachmentPath: fixture.resolve, |
| 554 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 555 | }; |
| 556 | const prompt = yield* buildAntigravityPrompt(input); |
| 557 | expect(prompt).toEqual([ |
| 558 | { type: "text", text: input.input }, |
| 559 | { |
| 560 | type: "resource_link", |
| 561 | uri: first.uri, |
| 562 | name: "report.pdf", |
| 563 | mimeType: "application/pdf", |
| 564 | size: (50 * 1024 * 1024) / 2, |
| 565 | }, |
| 566 | { |
| 567 | type: "resource_link", |
| 568 | uri: second.uri, |
| 569 | name: "second.pdf", |
| 570 | mimeType: "application/pdf", |
| 571 | size: (50 * 1024 * 1024) / 2, |
| 572 | }, |
| 573 | ]); |
| 574 | |
| 575 | yield* fixture.fs.truncate(second.filePath, (50 * 1024 * 1024) / 2 + 1); |
| 576 | expect(yield* buildAntigravityPrompt(input)).toEqual([ |
| 577 | { type: "text", text: input.input }, |
| 578 | { |
| 579 | type: "resource_link", |
| 580 | uri: first.uri, |
| 581 | name: "report.pdf", |
| 582 | mimeType: "application/pdf", |
| 583 | size: (50 * 1024 * 1024) / 2, |
| 584 | }, |
| 585 | ]); |
| 586 | }), |
| 587 | ); |
| 588 | |
| 589 | it.effect("still rejects images when the native budget is full", () => |
| 590 | Effect.gen(function* () { |
| 591 | const fixture = yield* makeAttachmentFixture(); |
| 592 | const pdf = yield* fixture.write(pdfAttachment, ""); |
| 593 | yield* fixture.fs.truncate(pdf.filePath, 50 * 1024 * 1024); |
| 594 | yield* fixture.write(imageAttachment, new Uint8Array([1])); |
| 595 | const error = yield* buildAntigravityPrompt({ |
| 596 | input: "Inspect both attachments.", |
| 597 | attachments: [pdfAttachment, imageAttachment], |
| 598 | resolveAttachmentPath: fixture.resolve, |
| 599 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 600 | }).pipe(Effect.flip); |
| 601 | expect(error).toMatchObject({ |
| 602 | _tag: "AcpRequestError", |
| 603 | errorMessage: expect.stringContaining("'screen.png' is too large"), |
| 604 | }); |
| 605 | }), |
| 606 | ); |
| 607 | |
| 608 | it.effect("rejects aggregate overflow when a text upload grows after its size check", () => |
| 609 | Effect.gen(function* () { |
| 610 | const fixture = yield* makeAttachmentFixture(); |
| 611 | const pdf = yield* fixture.write(pdfAttachment, ""); |
| 612 | const text = yield* fixture.write(textAttachment, "a"); |
| 613 | yield* fixture.fs.truncate(pdf.filePath, 50 * 1024 * 1024 - 1); |
| 614 | const error = yield* buildAntigravityPrompt({ |
| 615 | input: undefined, |
| 616 | attachments: [pdfAttachment, textAttachment], |
| 617 | resolveAttachmentPath: fixture.resolve, |
| 618 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 619 | }).pipe( |
| 620 | Effect.provideService(FileSystem.FileSystem, { |
| 621 | ...fixture.fs, |
| 622 | stat: Effect.fn("AntigravityAcpSupportTest.growAfterStat")(function* (filePath: string) { |
| 623 | const info = yield* fixture.fs.stat(filePath); |
| 624 | if (filePath === text.filePath) { |
| 625 | yield* fixture.fs.writeFileString(filePath, "ab"); |
| 626 | } |
| 627 | return info; |
| 628 | }), |
| 629 | }), |
| 630 | Effect.flip, |
| 631 | ); |
| 632 | |
| 633 | expect(error).toMatchObject({ |
| 634 | _tag: "AcpRequestError", |
| 635 | code: -32602, |
| 636 | errorMessage: "Attachment 'example.tsx' changed while being read and is too large.", |
| 637 | }); |
| 638 | }), |
| 639 | ); |
| 640 | |
| 641 | it.effect.each([ |
| 642 | { bytes: new Uint8Array([0x80]), message: "is not a UTF-8 text file" }, |
| 643 | { bytes: new Uint8Array([0xff, 0xfe, 0x61]), message: "is not a UTF-16LE text file" }, |
| 644 | { bytes: new Uint8Array([0xfe, 0xff, 0x00, 0x00]), message: "contains binary data" }, |
| 645 | { bytes: new Uint8Array([0x61, 0, 0x62]), message: "contains binary data" }, |
| 646 | ])("rejects binary data disguised as code: $message", ({ bytes, message }) => |
| 647 | Effect.gen(function* () { |
| 648 | const fixture = yield* makeAttachmentFixture(); |
| 649 | yield* fixture.write(textAttachment, bytes); |
| 650 | const error = yield* buildAntigravityPrompt({ |
| 651 | input: undefined, |
| 652 | attachments: [textAttachment], |
| 653 | resolveAttachmentPath: fixture.resolve, |
| 654 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 655 | }).pipe(Effect.flip); |
| 656 | |
| 657 | expect(error).toMatchObject({ |
| 658 | _tag: "AcpRequestError", |
| 659 | code: -32602, |
| 660 | errorMessage: expect.stringContaining(message), |
| 661 | }); |
| 662 | }), |
| 663 | ); |
| 664 | |
| 665 | it.effect("reports a missing upload instead of sending only the remaining text", () => |
| 666 | Effect.gen(function* () { |
| 667 | const fixture = yield* makeAttachmentFixture(); |
| 668 | const error = yield* buildAntigravityPrompt({ |
| 669 | input: "Read this image.", |
| 670 | attachments: [imageAttachment], |
| 671 | resolveAttachmentPath: fixture.resolve, |
| 672 | promptCapabilities: { image: true, audio: true, embeddedContext: true }, |
| 673 | }).pipe(Effect.flip); |
| 674 | |
| 675 | expect(error).toMatchObject({ |
| 676 | _tag: "AcpRequestError", |
| 677 | code: -32602, |
| 678 | errorMessage: "Could not read attachment 'screen.png'.", |
| 679 | }); |
| 680 | }), |
| 681 | ); |
| 682 | }); |