| ... | @@ -1,8 +1,10 @@ | ... | @@ -1,8 +1,10 @@ |
| | 1 | import * as DateTime from "effect/DateTime"; |
| 1 | import * as Effect from "effect/Effect"; | 2 | import * as Effect from "effect/Effect"; |
| 2 | import * as FileSystem from "effect/FileSystem"; | 3 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Path from "effect/Path"; | 4 | import * as Path from "effect/Path"; |
| 4 | import { | 5 | import { |
| 5 | type ClientOrchestrationCommand, | 6 | type ClientOrchestrationCommand, |
| | 7 | type IsoDateTime, |
| 6 | type OrchestrationCommand, | 8 | type OrchestrationCommand, |
| 7 | OrchestrationDispatchCommandError, | 9 | OrchestrationDispatchCommandError, |
| 8 | PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, | 10 | PROVIDER_SEND_TURN_MAX_IMAGE_BYTES, |
| ... | @@ -13,8 +15,38 @@ import { ServerConfig } from "../config.ts"; | ... | @@ -13,8 +15,38 @@ import { ServerConfig } from "../config.ts"; |
| 13 | import { parseBase64DataUrl } from "../imageMime.ts"; | 15 | import { parseBase64DataUrl } from "../imageMime.ts"; |
| 14 | import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; | 16 | import * as WorkspacePaths from "../workspace/WorkspacePaths.ts"; |
| 15 | | 17 | |
| | 18 | export const canonicalizeClientCommandTimestamps = ( |
| | 19 | command: ClientOrchestrationCommand, |
| | 20 | receivedAt: IsoDateTime, |
| | 21 | ): ClientOrchestrationCommand => { |
| | 22 | const canonicalCommand = |
| | 23 | "createdAt" in command |
| | 24 | ? { |
| | 25 | ...command, |
| | 26 | createdAt: receivedAt, |
| | 27 | } |
| | 28 | : command; |
| | 29 | |
| | 30 | if (canonicalCommand.type !== "thread.turn.start" || !canonicalCommand.bootstrap?.createThread) { |
| | 31 | return canonicalCommand; |
| | 32 | } |
| | 33 | |
| | 34 | return { |
| | 35 | ...canonicalCommand, |
| | 36 | bootstrap: { |
| | 37 | ...canonicalCommand.bootstrap, |
| | 38 | createThread: { |
| | 39 | ...canonicalCommand.bootstrap.createThread, |
| | 40 | createdAt: receivedAt, |
| | 41 | }, |
| | 42 | }, |
| | 43 | }; |
| | 44 | }; |
| | 45 | |
| 16 | export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => | 46 | export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => |
| 17 | Effect.gen(function* () { | 47 | Effect.gen(function* () { |
| | 48 | const receivedAt = DateTime.formatIso(yield* DateTime.now); |
| | 49 | const canonicalCommand = canonicalizeClientCommandTimestamps(command, receivedAt); |
| 18 | const fileSystem = yield* FileSystem.FileSystem; | 50 | const fileSystem = yield* FileSystem.FileSystem; |
| 19 | const path = yield* Path.Path; | 51 | const path = yield* Path.Path; |
| 20 | const serverConfig = yield* ServerConfig; | 52 | const serverConfig = yield* ServerConfig; |
| ... | @@ -47,30 +79,33 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => | ... | @@ -47,30 +79,33 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => |
| 47 | ), | 79 | ), |
| 48 | ); | 80 | ); |
| 49 | | 81 | |
| 50 | if (command.type === "project.create") { | 82 | if (canonicalCommand.type === "project.create") { |
| 51 | return { | 83 | return { |
| 52 | ...command, | 84 | ...canonicalCommand, |
| 53 | workspaceRoot: yield* normalizeProjectWorkspaceRootForCreate( | 85 | workspaceRoot: yield* normalizeProjectWorkspaceRootForCreate( |
| 54 | command.workspaceRoot, | 86 | canonicalCommand.workspaceRoot, |
| 55 | command.createWorkspaceRootIfMissing, | 87 | canonicalCommand.createWorkspaceRootIfMissing, |
| 56 | ), | 88 | ), |
| 57 | createWorkspaceRootIfMissing: command.createWorkspaceRootIfMissing === true, | 89 | createWorkspaceRootIfMissing: canonicalCommand.createWorkspaceRootIfMissing === true, |
| 58 | } satisfies OrchestrationCommand; | 90 | } satisfies OrchestrationCommand; |
| 59 | } | 91 | } |
| 60 | | 92 | |
| 61 | if (command.type === "project.meta.update" && command.workspaceRoot !== undefined) { | 93 | if ( |
| | 94 | canonicalCommand.type === "project.meta.update" && |
| | 95 | canonicalCommand.workspaceRoot !== undefined |
| | 96 | ) { |
| 62 | return { | 97 | return { |
| 63 | ...command, | 98 | ...canonicalCommand, |
| 64 | workspaceRoot: yield* normalizeProjectWorkspaceRoot(command.workspaceRoot), | 99 | workspaceRoot: yield* normalizeProjectWorkspaceRoot(canonicalCommand.workspaceRoot), |
| 65 | } satisfies OrchestrationCommand; | 100 | } satisfies OrchestrationCommand; |
| 66 | } | 101 | } |
| 67 | | 102 | |
| 68 | if (command.type !== "thread.turn.start") { | 103 | if (canonicalCommand.type !== "thread.turn.start") { |
| 69 | return command as OrchestrationCommand; | 104 | return canonicalCommand as OrchestrationCommand; |
| 70 | } | 105 | } |
| 71 | | 106 | |
| 72 | const normalizedAttachments = yield* Effect.forEach( | 107 | const normalizedAttachments = yield* Effect.forEach( |
| 73 | command.message.attachments, | 108 | canonicalCommand.message.attachments, |
| 74 | (attachment) => | 109 | (attachment) => |
| 75 | Effect.gen(function* () { | 110 | Effect.gen(function* () { |
| 76 | const parsed = parseBase64DataUrl(attachment.dataUrl); | 111 | const parsed = parseBase64DataUrl(attachment.dataUrl); |
| ... | @@ -87,7 +122,7 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => | ... | @@ -87,7 +122,7 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => |
| 87 | }); | 122 | }); |
| 88 | } | 123 | } |
| 89 | | 124 | |
| 90 | const attachmentId = createAttachmentId(command.threadId); | 125 | const attachmentId = createAttachmentId(canonicalCommand.threadId); |
| 91 | if (!attachmentId) { | 126 | if (!attachmentId) { |
| 92 | return yield* new OrchestrationDispatchCommandError({ | 127 | return yield* new OrchestrationDispatchCommandError({ |
| 93 | message: "Failed to create a safe attachment id.", | 128 | message: "Failed to create a safe attachment id.", |
| ... | @@ -135,9 +170,9 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => | ... | @@ -135,9 +170,9 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) => |
| 135 | ); | 170 | ); |
| 136 | | 171 | |
| 137 | return { | 172 | return { |
| 138 | ...command, | 173 | ...canonicalCommand, |
| 139 | message: { | 174 | message: { |
| 140 | ...command.message, | 175 | ...canonicalCommand.message, |
| 141 | attachments: normalizedAttachments, | 176 | attachments: normalizedAttachments, |
| 142 | }, | 177 | }, |
| 143 | } satisfies OrchestrationCommand; | 178 | } satisfies OrchestrationCommand; |