1+ import * as DateTime from "effect/DateTime" ;
12import * as Effect from "effect/Effect" ;
23import * as FileSystem from "effect/FileSystem" ;
34import * as Path from "effect/Path" ;
45import {
56 type ClientOrchestrationCommand ,
7+ type IsoDateTime ,
68 type OrchestrationCommand ,
79 OrchestrationDispatchCommandError ,
810 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ,
@@ -13,8 +15,38 @@ import { ServerConfig } from "../config.ts";
1315import { parseBase64DataUrl } from "../imageMime.ts" ;
1416import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
1517
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+
1646export const normalizeDispatchCommand = ( command : ClientOrchestrationCommand ) =>
1747 Effect . gen ( function * ( ) {
48+ const receivedAt = DateTime . formatIso ( yield * DateTime . now ) ;
49+ const canonicalCommand = canonicalizeClientCommandTimestamps ( command , receivedAt ) ;
1850 const fileSystem = yield * FileSystem . FileSystem ;
1951 const path = yield * Path . Path ;
2052 const serverConfig = yield * ServerConfig ;
@@ -47,30 +79,33 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
4779 ) ,
4880 ) ;
4981
50- if ( command . type === "project.create" ) {
82+ if ( canonicalCommand . type === "project.create" ) {
5183 return {
52- ...command ,
84+ ...canonicalCommand ,
5385 workspaceRoot : yield * normalizeProjectWorkspaceRootForCreate (
54- command . workspaceRoot ,
55- command . createWorkspaceRootIfMissing ,
86+ canonicalCommand . workspaceRoot ,
87+ canonicalCommand . createWorkspaceRootIfMissing ,
5688 ) ,
57- createWorkspaceRootIfMissing : command . createWorkspaceRootIfMissing === true ,
89+ createWorkspaceRootIfMissing : canonicalCommand . createWorkspaceRootIfMissing === true ,
5890 } satisfies OrchestrationCommand ;
5991 }
6092
61- if ( command . type === "project.meta.update" && command . workspaceRoot !== undefined ) {
93+ if (
94+ canonicalCommand . type === "project.meta.update" &&
95+ canonicalCommand . workspaceRoot !== undefined
96+ ) {
6297 return {
63- ...command ,
64- workspaceRoot : yield * normalizeProjectWorkspaceRoot ( command . workspaceRoot ) ,
98+ ...canonicalCommand ,
99+ workspaceRoot : yield * normalizeProjectWorkspaceRoot ( canonicalCommand . workspaceRoot ) ,
65100 } satisfies OrchestrationCommand ;
66101 }
67102
68- if ( command . type !== "thread.turn.start" ) {
69- return command as OrchestrationCommand ;
103+ if ( canonicalCommand . type !== "thread.turn.start" ) {
104+ return canonicalCommand as OrchestrationCommand ;
70105 }
71106
72107 const normalizedAttachments = yield * Effect . forEach (
73- command . message . attachments ,
108+ canonicalCommand . message . attachments ,
74109 ( attachment ) =>
75110 Effect . gen ( function * ( ) {
76111 const parsed = parseBase64DataUrl ( attachment . dataUrl ) ;
@@ -87,7 +122,7 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
87122 } ) ;
88123 }
89124
90- const attachmentId = createAttachmentId ( command . threadId ) ;
125+ const attachmentId = createAttachmentId ( canonicalCommand . threadId ) ;
91126 if ( ! attachmentId ) {
92127 return yield * new OrchestrationDispatchCommandError ( {
93128 message : "Failed to create a safe attachment id." ,
@@ -135,9 +170,9 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
135170 ) ;
136171
137172 return {
138- ...command ,
173+ ...canonicalCommand ,
139174 message : {
140- ...command . message ,
175+ ...canonicalCommand . message ,
141176 attachments : normalizedAttachments ,
142177 } ,
143178 } satisfies OrchestrationCommand ;
0 commit comments