Open History to see the last commit
1import { Effect, FileSystem, Path } from "effect";
2import {
3 type ClientOrchestrationCommand,
4 type OrchestrationCommand,
5 OrchestrationDispatchCommandError,
6 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
7} from "@t3tools/contracts";
8
9import { createAttachmentId, resolveAttachmentPath } from "../attachmentStore";
10import { ServerConfig } from "../config";
11import { parseBase64DataUrl } from "../imageMime";
12import { WorkspacePaths } from "../workspace/Services/WorkspacePaths";
13
14export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
15 Effect.gen(function* () {
16 const fileSystem = yield* FileSystem.FileSystem;
17 const path = yield* Path.Path;
18 const serverConfig = yield* ServerConfig;
19 const workspacePaths = yield* WorkspacePaths;
20
21 const normalizeProjectWorkspaceRoot = (workspaceRoot: string) =>
22 workspacePaths.normalizeWorkspaceRoot(workspaceRoot).pipe(
23 Effect.mapError(
24 (cause) =>
25 new OrchestrationDispatchCommandError({
26 message: cause.message,
27 }),
28 ),
29 );
30
31 if (command.type === "project.create") {
32 return {
33 ...command,
34 workspaceRoot: yield* normalizeProjectWorkspaceRoot(command.workspaceRoot),
35 } satisfies OrchestrationCommand;
36 }
37
38 if (command.type === "project.meta.update" && command.workspaceRoot !== undefined) {
39 return {
40 ...command,
41 workspaceRoot: yield* normalizeProjectWorkspaceRoot(command.workspaceRoot),
42 } satisfies OrchestrationCommand;
43 }
44
45 if (command.type !== "thread.turn.start") {
46 return command as OrchestrationCommand;
47 }
48
49 const normalizedAttachments = yield* Effect.forEach(
50 command.message.attachments,
51 (attachment) =>
52 Effect.gen(function* () {
53 const parsed = parseBase64DataUrl(attachment.dataUrl);
54 if (!parsed || !parsed.mimeType.startsWith("image/")) {
55 return yield* new OrchestrationDispatchCommandError({
56 message: `Invalid image attachment payload for '${attachment.name}'.`,
57 });
58 }
59
60 const bytes = Buffer.from(parsed.base64, "base64");
61 if (bytes.byteLength === 0 || bytes.byteLength > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
62 return yield* new OrchestrationDispatchCommandError({
63 message: `Image attachment '${attachment.name}' is empty or too large.`,
64 });
65 }
66
67 const attachmentId = createAttachmentId(command.threadId);
68 if (!attachmentId) {
69 return yield* new OrchestrationDispatchCommandError({
70 message: "Failed to create a safe attachment id.",
71 });
72 }
73
74 const persistedAttachment = {
75 type: "image" as const,
76 id: attachmentId,
77 name: attachment.name,
78 mimeType: parsed.mimeType.toLowerCase(),
79 sizeBytes: bytes.byteLength,
80 };
81
82 const attachmentPath = resolveAttachmentPath({
83 attachmentsDir: serverConfig.attachmentsDir,
84 attachment: persistedAttachment,
85 });
86 if (!attachmentPath) {
87 return yield* new OrchestrationDispatchCommandError({
88 message: `Failed to resolve persisted path for '${attachment.name}'.`,
89 });
90 }
91
92 yield* fileSystem.makeDirectory(path.dirname(attachmentPath), { recursive: true }).pipe(
93 Effect.mapError(
94 () =>
95 new OrchestrationDispatchCommandError({
96 message: `Failed to create attachment directory for '${attachment.name}'.`,
97 }),
98 ),
99 );
100 yield* fileSystem.writeFile(attachmentPath, bytes).pipe(
101 Effect.mapError(
102 () =>
103 new OrchestrationDispatchCommandError({
104 message: `Failed to persist attachment '${attachment.name}'.`,
105 }),
106 ),
107 );
108
109 return persistedAttachment;
110 }),
111 { concurrency: 1 },
112 );
113
114 return {
115 ...command,
116 message: {
117 ...command.message,
118 attachments: normalizedAttachments,
119 },
120 } satisfies OrchestrationCommand;
121 });