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