(
rawPaths: string[],
uploadCtx: { replBridgeEnabled: boolean; signal?: AbortSignal },
)
| 61 | } |
| 62 | |
| 63 | export async function resolveAttachments( |
| 64 | rawPaths: string[], |
| 65 | uploadCtx: { replBridgeEnabled: boolean; signal?: AbortSignal }, |
| 66 | ): Promise<ResolvedAttachment[]> { |
| 67 | // Stat serially (local, fast) to keep ordering deterministic, then upload |
| 68 | // in parallel (network, slow). Upload failures resolve undefined — the |
| 69 | // attachment still carries {path, size, isImage} for local renderers. |
| 70 | const stated: ResolvedAttachment[] = [] |
| 71 | for (const rawPath of rawPaths) { |
| 72 | const fullPath = expandPath(rawPath) |
| 73 | // Single stat — we need size, so this is the operation, not a guard. |
| 74 | // validateInput ran before us, but the file could have moved since |
| 75 | // (TOCTOU); if it did, let the error propagate so the model sees it. |
| 76 | const stats = await stat(fullPath) |
| 77 | stated.push({ |
| 78 | path: fullPath, |
| 79 | size: stats.size, |
| 80 | isImage: IMAGE_EXTENSION_REGEX.test(fullPath), |
| 81 | }) |
| 82 | } |
| 83 | // Dynamic import inside the feature() guard so upload.ts (axios, crypto, |
| 84 | // zod, auth utils, MIME map) is fully eliminated from non-BRIDGE_MODE |
| 85 | // builds. A static import would force module-scope evaluation regardless |
| 86 | // of the guard inside uploadBriefAttachment — CLAUDE.md: "helpers defined |
| 87 | // outside remain in the build even if never called". |
| 88 | if (feature('BRIDGE_MODE')) { |
| 89 | // Headless/SDK callers never set appState.replBridgeEnabled (only the TTY |
| 90 | // REPL does, at main.tsx init). CLAUDE_CODE_BRIEF_UPLOAD lets a host that |
| 91 | // runs the CLI as a subprocess opt in — e.g. the cowork desktop bridge, |
| 92 | // which already passes CLAUDE_CODE_OAUTH_TOKEN for auth. |
| 93 | const shouldUpload = |
| 94 | uploadCtx.replBridgeEnabled || |
| 95 | isEnvTruthy(process.env.CLAUDE_CODE_BRIEF_UPLOAD) |
| 96 | const { uploadBriefAttachment } = await import('./upload.js') |
| 97 | const uuids = await Promise.all( |
| 98 | stated.map(a => |
| 99 | uploadBriefAttachment(a.path, a.size, { |
| 100 | replBridgeEnabled: shouldUpload, |
| 101 | signal: uploadCtx.signal, |
| 102 | }), |
| 103 | ), |
| 104 | ) |
| 105 | return stated.map((a, i) => |
| 106 | uuids[i] === undefined ? a : { ...a, file_uuid: uuids[i] }, |
| 107 | ) |
| 108 | } |
| 109 | return stated |
| 110 | } |
| 111 |
no test coverage detected