The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | ProviderApprovalOption, |
| 3 | ProviderUserInputAnswers, |
| 4 | UserInputQuestion, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Predicate from "effect/Predicate"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import type * as EffectAcpSchema from "effect-acp/compat"; |
| 11 | import * as AcpWireSchema from "effect-acp/schema-v1"; |
| 12 | |
| 13 | import type { AcpToolCallState } from "@t3tools/provider-acp/server/runtimeModel"; |
| 14 | |
| 15 | import { |
| 16 | ANTIGRAVITY_EDIT_DIFF_OMITTED, |
| 17 | antigravityEditPatch, |
| 18 | isAntigravitySnippetPatch, |
| 19 | readAntigravityPreEditFile, |
| 20 | } from "./AntigravityEditLocation.ts"; |
| 21 | |
| 22 | const TOOL_TEXT_LIMIT = 8_000; |
| 23 | const TOOL_TEXT_TRUNCATED = "[Earlier output truncated]\n\n"; |
| 24 | const QUESTION_LABEL_LIMIT = 512; |
| 25 | |
| 26 | const NativeToolFields = Schema.Struct({ |
| 27 | command: Schema.optional(Schema.String), |
| 28 | CommandLine: Schema.optional(Schema.String), |
| 29 | command_line: Schema.optional(Schema.String), |
| 30 | commandLine: Schema.optional(Schema.String), |
| 31 | cwd: Schema.optional(Schema.String), |
| 32 | Cwd: Schema.optional(Schema.String), |
| 33 | WorkingDirectory: Schema.optional(Schema.String), |
| 34 | working_dir: Schema.optional(Schema.String), |
| 35 | workingDir: Schema.optional(Schema.String), |
| 36 | combinedOutput: Schema.optional(Schema.String), |
| 37 | combined_output: Schema.optional(Schema.String), |
| 38 | exitCode: Schema.optional(Schema.Int), |
| 39 | exit_code: Schema.optional(Schema.Int), |
| 40 | imagePath: Schema.optional(Schema.String), |
| 41 | }); |
| 42 | const decodeNativeToolFields = Schema.decodeUnknownOption(NativeToolFields); |
| 43 | const decodeSingleAnswer = Schema.decodeUnknownOption( |
| 44 | Schema.Union([Schema.String, Schema.Tuple([Schema.String])]), |
| 45 | ); |
| 46 | const decodeToolCallContent = Schema.decodeUnknownOption(AcpWireSchema.ToolCallContent); |
| 47 | |
| 48 | // agy keeps its own `schedule` call in progress until the wake fires, then replies by itself. |
| 49 | const ANTIGRAVITY_SCHEDULE_TITLES = new Set(["Run schedule", "Running schedule"]); |
| 50 | |
| 51 | /** |
| 52 | * The agent goes on running a command or a scheduled wake after its prompt ends, and replies by |
| 53 | * itself when it finishes. A daemon may never finish. |
| 54 | */ |
| 55 | export function antigravityToolOutlivesPrompt(toolCall: AcpToolCallState): boolean { |
| 56 | if (toolCall.title !== undefined && ANTIGRAVITY_SCHEDULE_TITLES.has(toolCall.title)) return true; |
| 57 | const input = toolCall.data.rawInput; |
| 58 | return toolCall.kind === "execute" && !(Predicate.isObject(input) && input.IsDaemon === true); |
| 59 | } |
| 60 | |
| 61 | /** Native questions share the permission method, but their choices are not approvals. */ |
| 62 | export function isAntigravityUserInputRequest( |
| 63 | request: EffectAcpSchema.RequestPermissionRequest, |
| 64 | ): boolean { |
| 65 | return request.toolCall.toolCallId.startsWith("interaction_"); |
| 66 | } |
| 67 | |
| 68 | /** Copy truncated text so V8 cannot retain the original large string. */ |
| 69 | const SECURITY_WARNING_META_KEY = "agy.security.warning"; |
| 70 | const WARNING_TEXT_LIMIT = 512; |
| 71 | const decodeSecurityWarning = Schema.decodeUnknownOption( |
| 72 | Schema.Struct({ |
| 73 | title: Schema.optional(Schema.String), |
| 74 | message: Schema.optional(Schema.String), |
| 75 | }), |
| 76 | ); |
| 77 | |
| 78 | /** Expose shell/web Allow Always _meta warnings as option text for both clients. */ |
| 79 | function antigravitySecurityWarning(option: EffectAcpSchema.PermissionOption): string | undefined { |
| 80 | const meta = option._meta; |
| 81 | if (!Predicate.isObject(meta)) return undefined; |
| 82 | const warning = Option.getOrUndefined(decodeSecurityWarning(meta[SECURITY_WARNING_META_KEY])); |
| 83 | const text = warning?.message?.trim() || warning?.title?.trim(); |
| 84 | if (!text) return undefined; |
| 85 | return text.length > WARNING_TEXT_LIMIT |
| 86 | ? copyBoundedText(`${text.slice(0, WARNING_TEXT_LIMIT - 3)}...`) |
| 87 | : text; |
| 88 | } |
| 89 | |
| 90 | export function antigravityApprovalOptions( |
| 91 | request: EffectAcpSchema.RequestPermissionRequest, |
| 92 | ): ReadonlyArray<ProviderApprovalOption> { |
| 93 | if (isAntigravityUserInputRequest(request)) return []; |
| 94 | const options: ProviderApprovalOption[] = []; |
| 95 | const optionWithKind = (kind: EffectAcpSchema.PermissionOption["kind"]) => |
| 96 | request.options.find((entry) => entry.kind === kind && entry.optionId.trim()); |
| 97 | const once = optionWithKind("allow_once"); |
| 98 | if (once) { |
| 99 | options.push({ decision: "accept", label: "Allow once" }); |
| 100 | } |
| 101 | const always = optionWithKind("allow_always"); |
| 102 | if (always) { |
| 103 | const warning = antigravitySecurityWarning(always); |
| 104 | options.push({ |
| 105 | decision: "acceptForSession", |
| 106 | label: "Allow for this thread", |
| 107 | ...(warning ? { warning } : {}), |
| 108 | }); |
| 109 | } |
| 110 | if (optionWithKind("reject_once")) { |
| 111 | options.push({ decision: "decline", label: "Deny" }); |
| 112 | } |
| 113 | options.push({ decision: "cancel", label: "Cancel" }); |
| 114 | return options; |
| 115 | } |
| 116 | |
| 117 | function copyBoundedText(text: string): string { |
| 118 | return Buffer.from(text, "utf16le").toString("utf16le"); |
| 119 | } |
| 120 | |
| 121 | function questionLabel(option: EffectAcpSchema.PermissionOption): string { |
| 122 | const label = option.name.trim() || option.optionId; |
| 123 | return label.length > QUESTION_LABEL_LIMIT |
| 124 | ? copyBoundedText(`${label.slice(0, QUESTION_LABEL_LIMIT - 3)}...`) |
| 125 | : label; |
| 126 | } |
| 127 | |
| 128 | export function extractAntigravityUserInputQuestion( |
| 129 | request: EffectAcpSchema.RequestPermissionRequest, |
| 130 | ): UserInputQuestion | undefined { |
| 131 | if (!isAntigravityUserInputRequest(request) || request.options.length === 0) { |
| 132 | return undefined; |
| 133 | } |
| 134 | const ids = new Set<string>(); |
| 135 | for (const option of request.options) { |
| 136 | if (!option.optionId.trim() || ids.has(option.optionId)) { |
| 137 | return undefined; |
| 138 | } |
| 139 | ids.add(option.optionId); |
| 140 | } |
| 141 | const question = request.toolCall.title?.trim() || "Choose an option."; |
| 142 | return { |
| 143 | id: request.toolCall.toolCallId, |
| 144 | header: "Question", |
| 145 | question: |
| 146 | question.length > TOOL_TEXT_LIMIT |
| 147 | ? copyBoundedText(`${question.slice(0, TOOL_TEXT_LIMIT - 3)}...`) |
| 148 | : question, |
| 149 | multiSelect: false, |
| 150 | allowCustomAnswer: false, |
| 151 | options: request.options.map((option) => ({ |
| 152 | value: option.optionId, |
| 153 | label: questionLabel(option), |
| 154 | description: questionLabel(option), |
| 155 | })), |
| 156 | }; |
| 157 | } |
| 158 | |
| 159 | export function makeAntigravityUserInputResponse( |
| 160 | request: EffectAcpSchema.RequestPermissionRequest, |
| 161 | answers: ProviderUserInputAnswers, |
| 162 | ): EffectAcpSchema.RequestPermissionResponse | undefined { |
| 163 | if (extractAntigravityUserInputQuestion(request) === undefined) { |
| 164 | return undefined; |
| 165 | } |
| 166 | const answer = Option.getOrUndefined(decodeSingleAnswer(answers[request.toolCall.toolCallId])); |
| 167 | const value = typeof answer === "string" ? answer : answer?.[0]; |
| 168 | if (value === undefined) { |
| 169 | return undefined; |
| 170 | } |
| 171 | const exact = request.options.find((option) => option.optionId === value); |
| 172 | if (exact) { |
| 173 | return { outcome: { outcome: "selected", optionId: exact.optionId } }; |
| 174 | } |
| 175 | const matchingLabels = request.options.filter((option) => questionLabel(option) === value); |
| 176 | const option = matchingLabels.length === 1 ? matchingLabels[0] : undefined; |
| 177 | return option ? { outcome: { outcome: "selected", optionId: option.optionId } } : undefined; |
| 178 | } |
| 179 | |
| 180 | function boundText(text: string, limit = TOOL_TEXT_LIMIT): string { |
| 181 | return text.length <= limit |
| 182 | ? text |
| 183 | : copyBoundedText(`${TOOL_TEXT_TRUNCATED}${text.slice(-limit)}`); |
| 184 | } |
| 185 | |
| 186 | function boundCommandText(text: string, limit = TOOL_TEXT_LIMIT): string { |
| 187 | if ( |
| 188 | text.length <= limit || |
| 189 | (text.length <= limit + TOOL_TEXT_TRUNCATED.length && text.startsWith(TOOL_TEXT_TRUNCATED)) |
| 190 | ) { |
| 191 | return text; |
| 192 | } |
| 193 | const tail = text.slice(-limit).replace(/^[\uDC00-\uDFFF]/u, ""); |
| 194 | return copyBoundedText(`${TOOL_TEXT_TRUNCATED}${tail}`); |
| 195 | } |
| 196 | |
| 197 | const COMMAND_TEXT_FIELDS = new Set([ |
| 198 | "CommandLine", |
| 199 | "command_line", |
| 200 | "commandLine", |
| 201 | "command", |
| 202 | "aggregatedOutput", |
| 203 | "combinedOutput", |
| 204 | "combined_output", |
| 205 | "output", |
| 206 | "text", |
| 207 | ]); |
| 208 | |
| 209 | /** Keeps the tail of long tool text, as every bounded tool payload does. */ |
| 210 | export { boundText as boundAntigravityToolText }; |
| 211 | |
| 212 | /** True when bounding cut some of this text. */ |
| 213 | export const isCutAntigravityToolText = (text: string) => text.includes(TOOL_TEXT_TRUNCATED); |
| 214 | |
| 215 | interface ToolPayloadBudget { |
| 216 | nodes: number; |
| 217 | text: number; |
| 218 | } |
| 219 | |
| 220 | function sanitizeToolValue( |
| 221 | value: unknown, |
| 222 | budget: ToolPayloadBudget, |
| 223 | depth: number, |
| 224 | field?: string, |
| 225 | ): unknown { |
| 226 | if (depth > 12 || budget.nodes-- <= 0) { |
| 227 | return undefined; |
| 228 | } |
| 229 | if (typeof value === "string") { |
| 230 | if (/^data:image\//i.test(value) || budget.text <= 0) { |
| 231 | return undefined; |
| 232 | } |
| 233 | const bound = |
| 234 | field !== undefined && COMMAND_TEXT_FIELDS.has(field) ? boundCommandText : boundText; |
| 235 | const text = bound(value, Math.min(TOOL_TEXT_LIMIT, budget.text)); |
| 236 | budget.text -= text.length; |
| 237 | return text; |
| 238 | } |
| 239 | if (Array.isArray(value)) { |
| 240 | const result: unknown[] = []; |
| 241 | for (const entry of value) { |
| 242 | if (budget.nodes <= 0) break; |
| 243 | const sanitized = sanitizeToolValue(entry, budget, depth + 1); |
| 244 | if (sanitized !== undefined) result.push(sanitized); |
| 245 | } |
| 246 | return result; |
| 247 | } |
| 248 | if (!Predicate.isObject(value)) { |
| 249 | return value; |
| 250 | } |
| 251 | const entries: Array<readonly [string, unknown]> = []; |
| 252 | for (const [key, entry] of Object.entries(value)) { |
| 253 | if (budget.nodes <= 0) break; |
| 254 | if ( |
| 255 | (value.type === "image" && (key === "data" || key === "blob")) || |
| 256 | (key === "blob" && |
| 257 | typeof value.mimeType === "string" && |
| 258 | value.mimeType.startsWith("image/")) || |
| 259 | ((key === "formatted_output" || key === "formattedOutput") && |
| 260 | (entry === value.combinedOutput || entry === value.combined_output)) |
| 261 | ) { |
| 262 | continue; |
| 263 | } |
| 264 | const sanitized = sanitizeToolValue(entry, budget, depth + 1, key); |
| 265 | if (sanitized !== undefined) entries.push([key, sanitized]); |
| 266 | } |
| 267 | return Object.fromEntries(entries); |
| 268 | } |
| 269 | |
| 270 | export function sanitizeAntigravityToolPayload(payload: unknown): unknown { |
| 271 | return sanitizeToolValue(payload, { nodes: 512, text: 64_000 }, 0); |
| 272 | } |
| 273 | const isJsonRecord = Schema.is(Schema.Record(Schema.String, Schema.Json)); |
| 274 | |
| 275 | const ANTIGRAVITY_CLIENT_READ_TITLES = new Set([ |
| 276 | "Running client_view_file", |
| 277 | "Run client_view_file?", |
| 278 | ]); |
| 279 | |
| 280 | function antigravityClientReadPath(update: { |
| 281 | readonly title?: string | null; |
| 282 | readonly locations?: ReadonlyArray<{ readonly path: string }> | null; |
| 283 | readonly rawInput?: unknown; |
| 284 | }): string | undefined { |
| 285 | if (!update.title || !ANTIGRAVITY_CLIENT_READ_TITLES.has(update.title)) return undefined; |
| 286 | const target = |
| 287 | update.locations?.[0]?.path ?? |
| 288 | (Predicate.isObject(update.rawInput) ? update.rawInput.absolute_path : undefined); |
| 289 | return typeof target === "string" && target ? target : undefined; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * The agent never completes a subagent's `client_view_file` call, then fails every open call |
| 294 | * with the next unrelated step error. Report reads T3 served as completed instead. |
| 295 | */ |
| 296 | export function makeAntigravityReadRepair() { |
| 297 | type Outcome = "served" | "refused"; |
| 298 | const openReads = new Map< |
| 299 | string, |
| 300 | { readonly path: string; readonly sessionId: string; outcome?: Outcome } |
| 301 | >(); |
| 302 | // The agent runs a client tool in a background task, so its request can precede the announcement. |
| 303 | const earlyOutcomes: Array<{ readonly path: string; readonly outcome: Outcome }> = []; |
| 304 | return { |
| 305 | /** Earlier turns' rows are already settled, and their unanswered reads must not take credit. */ |
| 306 | reset: () => { |
| 307 | openReads.clear(); |
| 308 | earlyOutcomes.length = 0; |
| 309 | }, |
| 310 | /** |
| 311 | * Each `fs/read_text_file` request answers the oldest open read of its path, in order. |
| 312 | * Returns the call a served read completes, so T3 can report it without waiting. |
| 313 | */ |
| 314 | attempted: (path: string, served: boolean) => { |
| 315 | const outcome = served ? "served" : "refused"; |
| 316 | for (const [toolCallId, read] of openReads) { |
| 317 | if (read.path !== path || read.outcome !== undefined) continue; |
| 318 | read.outcome = outcome; |
| 319 | return served ? { sessionId: read.sessionId, toolCallId } : undefined; |
| 320 | } |
| 321 | earlyOutcomes.push({ path, outcome }); |
| 322 | return undefined; |
| 323 | }, |
| 324 | repair: ( |
| 325 | notification: EffectAcpSchema.SessionNotification, |
| 326 | ): EffectAcpSchema.SessionNotification => { |
| 327 | const update = notification.update; |
| 328 | if (update.sessionUpdate !== "tool_call" && update.sessionUpdate !== "tool_call_update") { |
| 329 | return notification; |
| 330 | } |
| 331 | const path = antigravityClientReadPath(update); |
| 332 | if (path !== undefined && !openReads.has(update.toolCallId)) { |
| 333 | const early = earlyOutcomes.findIndex((entry) => entry.path === path); |
| 334 | const outcome = early === -1 ? undefined : earlyOutcomes.splice(early, 1)[0]?.outcome; |
| 335 | const sessionId = notification.sessionId; |
| 336 | openReads.set( |
| 337 | update.toolCallId, |
| 338 | outcome === undefined ? { path, sessionId } : { path, sessionId, outcome }, |
| 339 | ); |
| 340 | } |
| 341 | const read = openReads.get(update.toolCallId); |
| 342 | if (update.status === "pending" || update.status === "in_progress") { |
| 343 | return read?.outcome === "served" |
| 344 | ? { ...notification, update: { ...update, status: "completed" } } |
| 345 | : notification; |
| 346 | } |
| 347 | if (update.status !== "completed" && update.status !== "failed") return notification; |
| 348 | openReads.delete(update.toolCallId); |
| 349 | if (update.status === "completed" || read?.outcome !== "served") return notification; |
| 350 | const { rawOutput: _stepError, ...rest } = update; |
| 351 | return { ...notification, update: { ...rest, status: "completed" } }; |
| 352 | }, |
| 353 | }; |
| 354 | } |
| 355 | |
| 356 | export function normalizeAntigravitySessionUpdate( |
| 357 | notification: EffectAcpSchema.SessionNotification, |
| 358 | ): EffectAcpSchema.SessionNotification { |
| 359 | const update = notification.update; |
| 360 | if (update.sessionUpdate !== "tool_call" && update.sessionUpdate !== "tool_call_update") { |
| 361 | return notification; |
| 362 | } |
| 363 | const contentBudget = { nodes: 512, text: 32_000 }; |
| 364 | const content = update.content?.flatMap((entry) => { |
| 365 | const decoded = Option.getOrUndefined( |
| 366 | decodeToolCallContent(sanitizeToolValue(entry, contentBudget, 0)), |
| 367 | ); |
| 368 | return decoded === undefined ? [] : [decoded]; |
| 369 | }); |
| 370 | const meta = sanitizeAntigravityToolPayload(update._meta); |
| 371 | return { |
| 372 | ...notification, |
| 373 | update: { |
| 374 | ...update, |
| 375 | ...(typeof update.title === "string" ? { title: boundText(update.title) } : {}), |
| 376 | ...(update.rawInput !== undefined |
| 377 | ? { rawInput: sanitizeAntigravityToolPayload(update.rawInput) } |
| 378 | : {}), |
| 379 | ...(update.rawOutput !== undefined |
| 380 | ? { rawOutput: sanitizeAntigravityToolPayload(update.rawOutput) } |
| 381 | : {}), |
| 382 | ...(update.content !== undefined ? { content: content ?? [] } : {}), |
| 383 | ...(update._meta !== undefined ? { _meta: isJsonRecord(meta) ? meta : null } : {}), |
| 384 | }, |
| 385 | }; |
| 386 | } |
| 387 | |
| 388 | function localImagePath(imagePath: string | undefined): string | undefined { |
| 389 | if (!imagePath || imagePath.length > TOOL_TEXT_LIMIT) { |
| 390 | return undefined; |
| 391 | } |
| 392 | const path = imagePath.trim(); |
| 393 | if (!isWorkspaceImagePreviewPath(path)) { |
| 394 | return undefined; |
| 395 | } |
| 396 | if (/^file:\/\//i.test(path)) { |
| 397 | try { |
| 398 | const url = new URL(path); |
| 399 | if (url.hostname && url.hostname !== "localhost") return undefined; |
| 400 | const pathname = decodeURIComponent(url.pathname); |
| 401 | return /^\/[a-z]:\//i.test(pathname) ? pathname.slice(1) : pathname; |
| 402 | } catch { |
| 403 | return undefined; |
| 404 | } |
| 405 | } |
| 406 | return /^[a-z][a-z\d+.-]*:/i.test(path) && !/^[a-z]:[\\/]/i.test(path) ? undefined : path; |
| 407 | } |
| 408 | |
| 409 | function hasTruncatedText(value: unknown): boolean { |
| 410 | if (typeof value === "string") return value.startsWith(TOOL_TEXT_TRUNCATED); |
| 411 | return Predicate.isObject(value) && Object.values(value).some(hasTruncatedText); |
| 412 | } |
| 413 | |
| 414 | /** The ACP adapter renders a diff's `patch.text` ahead of its bare old/new snippets. */ |
| 415 | function withAntigravityEditPatches( |
| 416 | content: ReadonlyArray<unknown>, |
| 417 | data: Record<string, unknown>, |
| 418 | readPreEditFile: ((path: string) => string | undefined) | undefined, |
| 419 | ) { |
| 420 | return content.map((entry) => { |
| 421 | if (!Predicate.isObject(entry) || entry.type !== "diff" || typeof entry.path !== "string") |
| 422 | return entry; |
| 423 | // Keep the first patch: once the edit runs, the file no longer holds the old text. A later |
| 424 | // update's shared text budget can cut or drop it, and a cut patch would misplace the hunk. |
| 425 | if (entry.patch !== undefined) { |
| 426 | const patch: unknown = entry.patch; |
| 427 | const cut = |
| 428 | Predicate.isObject(patch) && |
| 429 | patch.format !== "omitted" && |
| 430 | (typeof patch.text !== "string" || patch.text.startsWith(TOOL_TEXT_TRUNCATED)); |
| 431 | return cut ? { ...entry, patch: { format: "omitted" } } : entry; |
| 432 | } |
| 433 | // Bounding keeps only the tail of long text, so a cut snippet cannot be located and a cut |
| 434 | // old or new text would render the cut as file content. |
| 435 | if ([data.rawInput, entry.oldText, entry.newText].some(hasTruncatedText)) |
| 436 | return { ...entry, patch: { format: "omitted" } }; |
| 437 | const text = antigravityEditPatch({ |
| 438 | path: entry.path, |
| 439 | rawInput: data.rawInput, |
| 440 | locations: data.locations, |
| 441 | readPreEditFile, |
| 442 | }); |
| 443 | // A truncated patch would no longer parse, and bare snippets would render at line 1. |
| 444 | if ( |
| 445 | text === ANTIGRAVITY_EDIT_DIFF_OMITTED || |
| 446 | (text !== undefined && text.length > TOOL_TEXT_LIMIT) |
| 447 | ) |
| 448 | return { ...entry, patch: { format: "omitted" } }; |
| 449 | if (text === undefined) return entry; |
| 450 | return { |
| 451 | ...entry, |
| 452 | patch: isAntigravitySnippetPatch(text) ? { format: "snippet", text } : { text }, |
| 453 | }; |
| 454 | }); |
| 455 | } |
| 456 | |
| 457 | export function normalizeAntigravityToolCall( |
| 458 | toolCall: AcpToolCallState, |
| 459 | readPreEditFile = readAntigravityPreEditFile, |
| 460 | ): AcpToolCallState { |
| 461 | const input = Option.getOrUndefined(decodeNativeToolFields(toolCall.data.rawInput)); |
| 462 | const output = Option.getOrUndefined(decodeNativeToolFields(toolCall.data.rawOutput)); |
| 463 | const nativeCommand = |
| 464 | input?.CommandLine ?? |
| 465 | input?.command_line ?? |
| 466 | input?.commandLine ?? |
| 467 | input?.command ?? |
| 468 | output?.commandLine ?? |
| 469 | output?.command_line ?? |
| 470 | toolCall.command; |
| 471 | const command = nativeCommand?.trim() ? boundCommandText(nativeCommand.trim()) : undefined; |
| 472 | const nativeCwd = |
| 473 | input?.Cwd ?? |
| 474 | input?.WorkingDirectory ?? |
| 475 | input?.working_dir ?? |
| 476 | input?.workingDir ?? |
| 477 | input?.cwd ?? |
| 478 | output?.workingDir ?? |
| 479 | output?.working_dir; |
| 480 | const cwd = nativeCwd?.trim() ? boundText(nativeCwd.trim()) : undefined; |
| 481 | const nativeOutput = output?.combinedOutput ?? output?.combined_output; |
| 482 | const aggregatedOutput = nativeOutput === undefined ? undefined : boundCommandText(nativeOutput); |
| 483 | const exitCode = output?.exitCode ?? output?.exit_code; |
| 484 | const imagePath = localImagePath(output?.imagePath); |
| 485 | const sanitizedData = sanitizeAntigravityToolPayload(toolCall.data); |
| 486 | const data: Record<string, unknown> = Predicate.isObject(sanitizedData) ? sanitizedData : {}; |
| 487 | const kind = toolCall.kind ?? (command !== undefined ? "execute" : undefined); |
| 488 | if (kind !== undefined) data.kind = kind; |
| 489 | if (command !== undefined) data.command = command; |
| 490 | if (cwd !== undefined) data.cwd = cwd; |
| 491 | if (imagePath !== undefined) data.imagePath = imagePath; |
| 492 | if (kind === "edit") { |
| 493 | const content = Array.isArray(data.content) ? data.content : []; |
| 494 | // Full-access edits report only a path; without a hunk the checkpoint diff is the review. |
| 495 | data.content = content.some((entry) => Predicate.isObject(entry) && entry.type === "diff") |
| 496 | ? withAntigravityEditPatches( |
| 497 | content, |
| 498 | data, |
| 499 | toolCall.status === "pending" ? readPreEditFile : undefined, |
| 500 | ) |
| 501 | : [...content, { type: "diff", patch: { format: "omitted" } }]; |
| 502 | } |
| 503 | if (kind === "execute") { |
| 504 | data.item = { |
| 505 | ...(Predicate.isObject(data.item) ? data.item : {}), |
| 506 | ...(command !== undefined ? { command } : {}), |
| 507 | ...(cwd !== undefined ? { cwd } : {}), |
| 508 | ...(aggregatedOutput !== undefined ? { aggregatedOutput } : {}), |
| 509 | ...(exitCode !== undefined ? { exitCode } : {}), |
| 510 | }; |
| 511 | } |
| 512 | return { |
| 513 | ...toolCall, |
| 514 | ...(kind !== undefined ? { kind } : {}), |
| 515 | ...(command !== undefined ? { command } : {}), |
| 516 | ...(toolCall.title !== undefined ? { title: boundText(toolCall.title) } : {}), |
| 517 | ...(command !== undefined |
| 518 | ? { detail: command } |
| 519 | : toolCall.detail !== undefined |
| 520 | ? { detail: boundText(toolCall.detail) } |
| 521 | : {}), |
| 522 | data, |
| 523 | }; |
| 524 | } |
| 525 | |
| 526 | const ANTIGRAVITY_SUBAGENT_TITLES = new Set([ |
| 527 | "Running start_subagent", |
| 528 | "Run start_subagent?", |
| 529 | "Running invoke_subagent", |
| 530 | "Run invoke_subagent?", |
| 531 | // agy 1.5 auto-approved calls. |
| 532 | "Run invoke_subagent", |
| 533 | ]); |
| 534 | |
| 535 | /** The ACP agent reports subagent calls as ordinary tools without child IDs or models. */ |
| 536 | export function classifyAntigravitySubagentToolCall( |
| 537 | toolCall: AcpToolCallState, |
| 538 | rawPayload?: unknown, |
| 539 | ): "subagent" | "mcp" | undefined { |
| 540 | if ( |
| 541 | (toolCall.kind !== undefined && toolCall.kind !== "other") || |
| 542 | toolCall.title === undefined || |
| 543 | !ANTIGRAVITY_SUBAGENT_TITLES.has(toolCall.title) |
| 544 | ) |
| 545 | return undefined; |
| 546 | const update = Predicate.isObject(rawPayload) ? rawPayload.update : undefined; |
| 547 | const meta = Predicate.isObject(update) ? update._meta : toolCall.data.meta; |
| 548 | return Predicate.isObject(meta) && meta.is_mcp_tool_call === true ? "mcp" : "subagent"; |
| 549 | } |
| 550 | |
| 551 | export function antigravitySubagentOutput(toolCall: AcpToolCallState): string | undefined { |
| 552 | const output = toolCall.data.rawOutput; |
| 553 | return typeof output === "string" && output.trim() ? boundText(output.trim()) : undefined; |
| 554 | } |