Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Normalizer.ts

142 lines4.8 KB

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