|
1 | | -import { type ChatAttachment, MessageId, OrchestratorMcpFailure } from "@t3tools/contracts"; |
| 1 | +import { |
| 2 | + ATTACHMENT_UPLOAD_URL_TTL_MS, |
| 3 | + type ChatAttachment, |
| 4 | + MessageId, |
| 5 | + OrchestratorMcpFailure, |
| 6 | +} from "@t3tools/contracts"; |
| 7 | +import * as Clock from "effect/Clock"; |
2 | 8 | import * as Effect from "effect/Effect"; |
| 9 | +import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
3 | 10 | import * as Upload from "../../../assets/AttachmentUpload.ts"; |
4 | 11 | import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts"; |
5 | 12 | import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts"; |
@@ -27,53 +34,94 @@ export function resolveAttachmentReferences( |
27 | 34 | }); |
28 | 35 | } |
29 | 36 |
|
30 | | -export const layer = McpToolAccess.toLayer(AttachmentToolkit, { |
31 | | - t3_attachment_prepare_upload: McpToolAccess.writes((input) => |
32 | | - Upload.issueAttachmentUploadUrl(input.upload).pipe(Effect.mapError(unavailable)), |
33 | | - ), |
34 | | - t3_attachment_discard: McpToolAccess.writes((input) => |
35 | | - Upload.deletePendingAttachment(input.attachmentId).pipe(Effect.as({})), |
36 | | - ), |
37 | | - t3_thread_send_attachments: McpToolAccess.writesThreads( |
38 | | - (input) => [input.threadId], |
39 | | - (input) => |
40 | | - Effect.gen(function* () { |
41 | | - const { caller, projection } = yield* readThread(input.threadId, ["messages"]); |
42 | | - if (projection.thread.archivedAt !== null) |
43 | | - return yield* new OrchestratorMcpFailure({ |
44 | | - code: "invalid_request", |
45 | | - message: "Unarchive the target thread before sending attachments.", |
46 | | - }); |
47 | | - const attachments = yield* resolveAttachmentReferences( |
48 | | - input.attachments, |
49 | | - projection.messages.flatMap((message) => message.attachments), |
50 | | - ); |
51 | | - const commandId = yield* newCommandId(); |
52 | | - const messageId = MessageId.make(commandId); |
53 | | - const result = yield* ThreadMessageIntake.sendToThread({ |
54 | | - projectId: projection.thread.projectId, |
55 | | - threadId: projection.thread.id, |
56 | | - commandId, |
57 | | - messageId, |
58 | | - ...(caller === undefined ? {} : { senderThreadId: caller.id }), |
59 | | - text: input.message ?? "", |
60 | | - attachments, |
61 | | - mode: "auto", |
62 | | - createdBy: "agent", |
63 | | - creationSource: "mcp", |
64 | | - }).pipe( |
65 | | - Effect.mapError((error) => |
66 | | - error._tag === "AttachmentClaimError" |
67 | | - ? new OrchestratorMcpFailure({ code: "orchestration_error", message: error.message }) |
68 | | - : unavailable(), |
69 | | - ), |
70 | | - ); |
71 | | - return { |
72 | | - threadId: projection.thread.id, |
73 | | - messageId, |
74 | | - runId: result.run.id, |
75 | | - status: result.run.status, |
76 | | - }; |
77 | | - }), |
78 | | - ), |
79 | | -}); |
| 37 | +/** |
| 38 | + * As long as the pending file can live. The sweep counts its 24 hours from when |
| 39 | + * the upload finishes, which can be up to the upload URL's lifetime after issue. |
| 40 | + */ |
| 41 | +const UPLOAD_OWNER_TTL_MS = 24 * 60 * 60 * 1000 + ATTACHMENT_UPLOAD_URL_TTL_MS; |
| 42 | + |
| 43 | +/** A thread owns its uploads across provider sessions; an outside client per MCP session. */ |
| 44 | +const uploadOwner = McpInvocationContext.McpInvocationContext.pipe( |
| 45 | + Effect.map((scope) => scope.thread?.threadId ?? scope.requestNamespace), |
| 46 | +); |
| 47 | + |
| 48 | +export const layer = McpToolAccess.toLayer( |
| 49 | + AttachmentToolkit, |
| 50 | + Effect.sync(() => { |
| 51 | + // Which caller prepared each pending upload, so only that caller can discard it. |
| 52 | + const uploadOwners = new Map<string, { readonly owner: string; readonly issuedAt: number }>(); |
| 53 | + return { |
| 54 | + t3_attachment_prepare_upload: McpToolAccess.writes((input) => |
| 55 | + Effect.gen(function* () { |
| 56 | + const result = yield* Upload.issueAttachmentUploadUrl(input.upload).pipe( |
| 57 | + Effect.mapError(unavailable), |
| 58 | + ); |
| 59 | + const now = yield* Clock.currentTimeMillis; |
| 60 | + for (const [id, entry] of uploadOwners) { |
| 61 | + if (now - entry.issuedAt > UPLOAD_OWNER_TTL_MS) uploadOwners.delete(id); |
| 62 | + } |
| 63 | + uploadOwners.set(result.attachmentId, { owner: yield* uploadOwner, issuedAt: now }); |
| 64 | + return result; |
| 65 | + }), |
| 66 | + ), |
| 67 | + t3_attachment_discard: McpToolAccess.writes((input) => |
| 68 | + Effect.gen(function* () { |
| 69 | + if (uploadOwners.get(input.attachmentId)?.owner !== (yield* uploadOwner)) { |
| 70 | + return yield* new OrchestratorMcpFailure({ |
| 71 | + code: "invalid_request", |
| 72 | + message: "Only the caller that prepared a pending upload can discard it.", |
| 73 | + }); |
| 74 | + } |
| 75 | + yield* Upload.deletePendingAttachment(input.attachmentId); |
| 76 | + uploadOwners.delete(input.attachmentId); |
| 77 | + return {}; |
| 78 | + }), |
| 79 | + ), |
| 80 | + t3_thread_send_attachments: McpToolAccess.writesThreads( |
| 81 | + (input) => [input.threadId], |
| 82 | + (input) => |
| 83 | + Effect.gen(function* () { |
| 84 | + const { caller, projection } = yield* readThread(input.threadId, ["messages"]); |
| 85 | + if (projection.thread.archivedAt !== null) |
| 86 | + return yield* new OrchestratorMcpFailure({ |
| 87 | + code: "invalid_request", |
| 88 | + message: "Unarchive the target thread before sending attachments.", |
| 89 | + }); |
| 90 | + const attachments = yield* resolveAttachmentReferences( |
| 91 | + input.attachments, |
| 92 | + projection.messages.flatMap((message) => message.attachments), |
| 93 | + ); |
| 94 | + const commandId = yield* newCommandId(); |
| 95 | + const messageId = MessageId.make(commandId); |
| 96 | + const result = yield* ThreadMessageIntake.sendToThread({ |
| 97 | + projectId: projection.thread.projectId, |
| 98 | + threadId: projection.thread.id, |
| 99 | + commandId, |
| 100 | + messageId, |
| 101 | + ...(caller === undefined ? {} : { senderThreadId: caller.id }), |
| 102 | + text: input.message ?? "", |
| 103 | + attachments, |
| 104 | + mode: "auto", |
| 105 | + createdBy: "agent", |
| 106 | + creationSource: "mcp", |
| 107 | + }).pipe( |
| 108 | + Effect.mapError((error) => |
| 109 | + error._tag === "AttachmentClaimError" |
| 110 | + ? new OrchestratorMcpFailure({ |
| 111 | + code: "orchestration_error", |
| 112 | + message: error.message, |
| 113 | + }) |
| 114 | + : unavailable(), |
| 115 | + ), |
| 116 | + ); |
| 117 | + return { |
| 118 | + threadId: projection.thread.id, |
| 119 | + messageId, |
| 120 | + runId: result.run.id, |
| 121 | + status: result.run.status, |
| 122 | + }; |
| 123 | + }), |
| 124 | + ), |
| 125 | + }; |
| 126 | + }), |
| 127 | +); |
0 commit comments