|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { generateId } from '@sim/utils/id' |
| 3 | +import { NextRequest } from 'next/server' |
| 4 | +import { v2CopyFileItemsContract } from '@/lib/api/contracts/v2/file-copy' |
| 5 | +import { V2_PARSE_DEFAULTS, v2InvalidBodyResponse } from '@/lib/api/server/routes/v2-json-route' |
| 6 | +import { parseRequest } from '@/lib/api/server/validation' |
| 7 | +import { getInternalApiBaseUrl } from '@/lib/core/utils/urls' |
| 8 | +import { createFileCopyCliTransport } from '@/lib/mothership/agent-cli/file-copy-transport' |
| 9 | +import { createResourceEffectTransport } from '@/lib/mothership/agent-cli/resource-effects' |
| 10 | +import { createCopilotResourceAdmission } from '@/lib/mothership/auth/application-delegation' |
| 11 | +import type { ResourceChange } from '@/lib/mothership/generated/resources' |
| 12 | +import { |
| 13 | + type readSandboxResourceScope, |
| 14 | + recordSandboxResourceEffects, |
| 15 | +} from '@/lib/mothership/tools/sandbox-resources' |
| 16 | + |
| 17 | +const logger = createLogger('SandboxFileCopy') |
| 18 | + |
| 19 | +/** A live callback delegates the exact pair; the shared operation owns both authorization and durable provenance. */ |
| 20 | +export async function proxySandboxFileCopyRequest( |
| 21 | + request: Request, |
| 22 | + token: string, |
| 23 | + scope: NonNullable<Awaited<ReturnType<typeof readSandboxResourceScope>>> |
| 24 | +): Promise<Response> { |
| 25 | + if ( |
| 26 | + scope.fileOwnerProtocolVersion !== 1 || |
| 27 | + request.headers.has('x-mothership-file-owner') || |
| 28 | + request.headers.has('x-mothership-workspace-id') |
| 29 | + ) |
| 30 | + return Response.json( |
| 31 | + { error: 'Paired file owner routing is unavailable or contradictory' }, |
| 32 | + { status: 403 } |
| 33 | + ) |
| 34 | + if (request.method !== 'POST') |
| 35 | + return Response.json({ error: 'Method not allowed' }, { status: 405 }) |
| 36 | + const parsed = await parseRequest( |
| 37 | + v2CopyFileItemsContract, |
| 38 | + new NextRequest(request.clone()), |
| 39 | + {}, |
| 40 | + { |
| 41 | + ...V2_PARSE_DEFAULTS, |
| 42 | + invalidJsonResponse: () => v2InvalidBodyResponse(request), |
| 43 | + } |
| 44 | + ) |
| 45 | + if (!parsed.success) return parsed.response |
| 46 | + const endpoint = getInternalApiBaseUrl() |
| 47 | + const transport = createFileCopyCliTransport( |
| 48 | + endpoint, |
| 49 | + { |
| 50 | + userId: scope.userId, |
| 51 | + chatId: scope.chatId, |
| 52 | + toolCallId: scope.toolCallId, |
| 53 | + copilotToolExecution: true, |
| 54 | + copilotResourceAdmission: createCopilotResourceAdmission({ |
| 55 | + userId: scope.userId, |
| 56 | + invocation: { kind: 'chat', chatId: scope.chatId }, |
| 57 | + }), |
| 58 | + }, |
| 59 | + parsed.data.body |
| 60 | + ) |
| 61 | + const forwarded = new Request(`${endpoint.replace(/\/$/, '')}${v2CopyFileItemsContract.path}`, { |
| 62 | + method: 'POST', |
| 63 | + headers: { 'content-type': 'application/json' }, |
| 64 | + body: JSON.stringify(parsed.data.body), |
| 65 | + signal: request.signal, |
| 66 | + }) |
| 67 | + const effects: ResourceChange[] = [] |
| 68 | + let completed: Response | undefined |
| 69 | + const dispatch: typeof fetch = async (input, init) => { |
| 70 | + completed = await transport(input, init) |
| 71 | + return completed |
| 72 | + } |
| 73 | + let response: Response |
| 74 | + try { |
| 75 | + response = await createResourceEffectTransport(endpoint, dispatch, effects)(forwarded) |
| 76 | + } catch (error) { |
| 77 | + if (!completed) throw error |
| 78 | + response = completed |
| 79 | + logger.warn('Copy callback effect projection failed after API completion', { |
| 80 | + toolCallId: scope.toolCallId, |
| 81 | + }) |
| 82 | + } |
| 83 | + const requestId = generateId() |
| 84 | + await recordSandboxResourceEffects( |
| 85 | + token, |
| 86 | + scope, |
| 87 | + effects.map((effect, index) => ({ |
| 88 | + ...effect, |
| 89 | + effectId: `${scope.runId}:${scope.toolCallId}:${requestId}:${index}`, |
| 90 | + })) |
| 91 | + ) |
| 92 | + return response |
| 93 | +} |
0 commit comments