Open History to see the last commit
1import { McpAttachmentInput } from "./input.ts";
2import {
3 AttachmentCreateUploadUrlInput,
4 AttachmentCreateUploadUrlResult,
5 AttachmentDeleteInput,
6 MessageId,
7 RunId,
8 ThreadId,
9 OrchestrationV2RunStatus,
10 OrchestratorMcpFailure,
11} from "@t3tools/contracts";
12import * as SecureRandom from "effect/crypto/SecureRandom";
13import * as FileSystem from "effect/FileSystem";
14import * as Schema from "effect/Schema";
15import { Tool, Toolkit } from "effect/ai";
16import * as ServerSecretStore from "../../../auth/ServerSecretStore.ts";
17import * as ServerConfig from "../../../config.ts";
18import * as DocumentText from "../../../provider/DocumentText.ts";
19import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
20import * as McpInvocationContext from "../../McpInvocationContext.ts";
21
22const shared = {
23 failure: OrchestratorMcpFailure,
24 failureMode: "return" as const,
25 dependencies: [
26 McpInvocationContext.McpInvocationContext,
27 ThreadManagementService.ThreadManagementService,
28 ServerConfig.ServerConfig,
29 ServerSecretStore.ServerSecretStore,
30 FileSystem.FileSystem,
31 SecureRandom.SecureRandom,
32 ],
33};
34const AttachmentUploadTool = Tool.make("t3_attachment_prepare_upload", {
35 ...shared,
36 description:
37 "Get an upload URL. POST the file's exact bytes to `uploadUrl` (else this server's `relativeUrl`), then send via `t3_thread_send_attachments` with the returned `attachmentId`.",
38 parameters: Schema.Struct({ upload: AttachmentCreateUploadUrlInput }),
39 success: Schema.Struct({
40 ...AttachmentCreateUploadUrlResult.fields,
41 uploadUrl: Schema.optionalKey(Schema.String),
42 }),
43}).annotate(Tool.Destructive, true);
44const AttachmentDiscardTool = Tool.make("t3_attachment_discard", {
45 ...shared,
46 description: "Discard a pending upload (not a sent attachment).",
47 parameters: AttachmentDeleteInput,
48 success: Schema.Struct({}),
49}).annotate(Tool.Destructive, true);
50const AttachmentSendTool = Tool.make("t3_thread_send_attachments", {
51 ...shared,
52 dependencies: [...shared.dependencies, DocumentText.DocumentText],
53 description:
54 "Send uploaded attachments as a new message to a thread. No retry key. Target modes cannot exceed yours. Success does not mean the provider read them or finished.",
55 parameters: Schema.Struct({
56 threadId: Schema.optional(ThreadId),
57 message: Schema.optional(Schema.String.check(Schema.isMaxLength(120000))),
58 attachments: Schema.Array(McpAttachmentInput).check(
59 Schema.isMinLength(1),
60 Schema.isMaxLength(8),
61 ),
62 }),
63 success: Schema.Struct({
64 threadId: ThreadId,
65 messageId: MessageId,
66 runId: RunId,
67 status: OrchestrationV2RunStatus,
68 }),
69})
70 .annotate(Tool.Destructive, true)
71 .annotate(Tool.OpenWorld, true);
72export const AttachmentToolkit = Toolkit.make(
73 AttachmentUploadTool,
74 AttachmentDiscardTool,
75 AttachmentSendTool,
76);