forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
362 lines (327 loc) · 11.9 KB
/
schema.ts
File metadata and controls
362 lines (327 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { z } from 'zod';
const IntegrationSchema = z
.object({
name: z.string().describe('Integration name (e.g., MailChimp, Twilio)'),
description: z.string().optional(),
source: z.string().describe('integration module source (e.g., @auto-engineer/mailchimp-integration)'),
})
.describe('External service integration configuration');
// Data flow schemas for unified architecture
export const MessageTargetSchema = z
.object({
type: z.enum(['Event', 'Command', 'State']).describe('Type of message to target'),
name: z.string().describe('Name of the specific message'),
fields: z.record(z.unknown()).optional().describe('Field selector for partial message targeting'),
})
.describe('Target message with optional field selection');
const DestinationSchema = z
.discriminatedUnion('type', [
z.object({
type: z.literal('stream'),
pattern: z.string().describe('Stream pattern with interpolation (e.g., listing-${propertyId})'),
}),
z.object({
type: z.literal('integration'),
systems: z.array(z.string()).describe('Integration names to send to'),
message: z
.object({
name: z.string(),
type: z.enum(['command', 'query', 'reaction']),
})
.optional(),
}),
z.object({
type: z.literal('database'),
collection: z.string().describe('Database collection name'),
}),
z.object({
type: z.literal('topic'),
name: z.string().describe('Message topic/queue name'),
}),
])
.describe('Destination for outbound data');
const OriginSchema = z
.discriminatedUnion('type', [
z.object({
type: z.literal('projection'),
name: z.string(),
idField: z.string().describe('Field from event used as the projection’s unique identifier'),
}),
z.object({
type: z.literal('readModel'),
name: z.string().describe('Read model name'),
}),
z.object({
type: z.literal('database'),
collection: z.string().describe('Database collection name'),
query: z.unknown().optional().describe('Optional query filter'),
}),
z.object({
type: z.literal('api'),
endpoint: z.string().describe('API endpoint URL'),
method: z.string().optional().describe('HTTP method (defaults to GET)'),
}),
z.object({
type: z.literal('integration'),
systems: z.array(z.string()),
}),
])
.describe('Origin for inbound data');
export const DataSinkSchema = z
.object({
target: MessageTargetSchema,
destination: DestinationSchema,
transform: z.string().optional().describe('Optional transformation function name'),
_additionalInstructions: z.string().optional().describe('Additional instructions'),
_withState: z
.lazy(() => DataSourceSchema)
.optional()
.describe('Optional state data source for command'),
})
.describe('Data sink configuration for outbound data flow');
export const DataSourceSchema = z
.object({
target: MessageTargetSchema,
origin: OriginSchema,
transform: z.string().optional().describe('Optional transformation function name'),
_additionalInstructions: z.string().optional().describe('Additional instructions'),
})
.describe('Data source configuration for inbound data flow');
const MessageFieldSchema = z
.object({
name: z.string(),
type: z.string().describe('Field type (e.g., string, number, Date, UUID, etc.)'),
required: z.boolean().default(true),
description: z.string().optional(),
defaultValue: z.unknown().optional().describe('Default value for optional fields'),
})
.describe('Field definition for a message');
const BaseMessageSchema = z.object({
name: z.string().describe('Message name'),
fields: z.array(MessageFieldSchema),
description: z.string().optional(),
metadata: z
.object({
version: z.number().default(1).describe('Version number for schema evolution'),
})
.optional(),
});
const CommandSchema = BaseMessageSchema.extend({
type: z.literal('command'),
}).describe('Command that triggers state changes');
const EventSchema = BaseMessageSchema.extend({
type: z.literal('event'),
source: z.enum(['internal', 'external']).default('internal'),
}).describe('Event representing something that has happened');
const StateSchema = BaseMessageSchema.extend({
type: z.literal('state'),
}).describe('State/Read Model representing a view of data');
const MessageSchema = z.discriminatedUnion('type', [CommandSchema, EventSchema, StateSchema]);
export const EventExampleSchema = z
.object({
eventRef: z.string().describe('Reference to event message by name'),
exampleData: z.record(z.unknown()).describe('Example data matching the event schema'),
})
.describe('Event example with reference and data');
export const CommandExampleSchema = z
.object({
commandRef: z.string().describe('Reference to command message by name'),
exampleData: z.record(z.unknown()).describe('Example data matching the command schema'),
})
.describe('Command example with reference and data');
const StateExampleSchema = z
.object({
stateRef: z.string().describe('Reference to state message by name'),
exampleData: z.record(z.unknown()).describe('Example data matching the state schema'),
})
.describe('State example with reference and data');
const BaseSliceSchema = z
.object({
name: z.string(),
description: z.string().optional(),
stream: z.string().optional().describe('Event stream pattern for this slice'),
via: z.array(z.string()).optional().describe('Integration names used by this slice'),
additionalInstructions: z.string().optional().describe('Additional instructions'),
})
.describe('Base properties shared by all slice types');
const ErrorExampleSchema = z
.object({
errorType: z.enum(['IllegalStateError', 'ValidationError', 'NotFoundError']).describe('Expected error'),
message: z.string().optional().describe('Optional error message'),
})
.describe('Error outcome');
const CommandSliceSchema = BaseSliceSchema.extend({
type: z.literal('command'),
client: z.object({
description: z.string(),
specs: z.array(z.string()).describe('UI specifications (should statements)'),
}),
request: z.string().describe('Command request (GraphQL, REST endpoint, or other query format)').optional(),
server: z.object({
description: z.string(),
data: z.array(DataSinkSchema).optional().describe('Data sinks for command slices'),
gwt: z.array(
z.object({
given: z.array(EventExampleSchema).optional(),
when: CommandExampleSchema,
then: z.array(z.union([EventExampleSchema, ErrorExampleSchema])),
}),
),
}),
}).describe('Command slice handling user actions and business logic');
const QuerySliceSchema = BaseSliceSchema.extend({
type: z.literal('query'),
client: z.object({
description: z.string(),
specs: z.array(z.string()).describe('UI specifications (should statements)'),
}),
request: z.string().describe('Query request (GraphQL, REST endpoint, or other query format)').optional(),
server: z.object({
description: z.string(),
data: z.array(DataSourceSchema).optional().describe('Data sources for query slices'),
gwt: z.array(
z.object({
given: z.array(EventExampleSchema).describe('Given events'),
then: z.array(StateExampleSchema).describe('Then update state'),
}),
),
}),
}).describe('Query slice for reading data and maintaining projections');
const ReactSliceSchema = BaseSliceSchema.extend({
type: z.literal('react'),
server: z.object({
description: z.string().optional(),
data: z
.array(z.union([DataSinkSchema, DataSourceSchema]))
.optional()
.describe('Data items for react slices (mix of sinks and sources)'),
gwt: z.array(
z.object({
when: z.array(EventExampleSchema).describe('When event(s) occur'),
then: z.array(CommandExampleSchema).describe('Then send command(s)'),
}),
),
}),
}).describe('React slice for automated responses to events');
const SliceSchema = z.discriminatedUnion('type', [CommandSliceSchema, QuerySliceSchema, ReactSliceSchema]);
const FlowSchema = z
.object({
name: z.string(),
description: z.string().optional(),
slices: z.array(SliceSchema),
})
.describe('Business flow containing related slices');
// Variant 1: Just flow names
const FlowNamesOnlySchema = z
.object({
name: z.string(),
description: z.string().optional(),
})
.describe('Flow with just name for initial planning');
// Variant 2: Flow with slice names
const SliceNamesOnlySchema = z
.object({
name: z.string(),
description: z.string().optional(),
type: z.enum(['command', 'query', 'react']),
})
.describe('Slice with just name and type for structure planning');
const FlowWithSliceNamesSchema = z
.object({
name: z.string(),
description: z.string().optional(),
slices: z.array(SliceNamesOnlySchema),
})
.describe('Flow with slice names for structure planning');
// Variant 3: Flow with client & server names
const ClientServerNamesSliceSchema = z
.object({
name: z.string(),
type: z.enum(['command', 'query', 'react']),
description: z.string().optional(),
client: z
.object({
description: z.string(),
})
.optional(),
server: z
.object({
description: z.string(),
})
.optional(),
})
.describe('Slice with client/server descriptions for behavior planning');
const FlowWithClientServerNamesSchema = z
.object({
name: z.string(),
description: z.string().optional(),
slices: z.array(ClientServerNamesSliceSchema),
})
.describe('Flow with client/server descriptions for behavior planning');
// Variant 4: Full specs (uses existing schemas)
// Export individual variant schemas
export const FlowNamesSchema = z
.object({
variant: z.literal('flow-names').describe('Just flow names for initial ideation'),
flows: z.array(FlowNamesOnlySchema),
})
.describe('System with just flow names for initial planning');
export const SliceNamesSchema = z
.object({
variant: z.literal('slice-names').describe('Flows with slice names for structure planning'),
flows: z.array(FlowWithSliceNamesSchema),
})
.describe('System with flow and slice names for structure planning');
export const ClientServerNamesSchema = z
.object({
variant: z.literal('client-server-names').describe('Flows with client/server descriptions'),
flows: z.array(FlowWithClientServerNamesSchema),
})
.describe('System with client/server descriptions for behavior planning');
export const SpecsSchema = z
.object({
variant: z.literal('specs').describe('Full specification with all details'),
flows: z.array(FlowSchema),
messages: z.array(MessageSchema),
integrations: z.array(IntegrationSchema).optional(),
})
.describe('Complete system specification with all implementation details');
export const AppSchema = z
.discriminatedUnion('variant', [FlowNamesSchema, SliceNamesSchema, ClientServerNamesSchema, SpecsSchema])
.describe('Progressive system definition supporting incremental co-creation');
// if (require.main === module) {
// const schemas = Object.fromEntries(
// Object.entries({
// flow: FlowSchema,
// message: MessageSchema,
// integration: IntegrationSchema,
// commandSlice: CommandSliceSchema,
// querySlice: QuerySliceSchema,
// reactSlice: ReactSliceSchema,
// }).map(([k, v]) => [
// k,
// zodToJsonSchema(v, {
// $refStrategy: 'root' as const,
// target: 'jsonSchema7' as const,
// definitionPath: 'definitions',
// name: k[0].toUpperCase() + k.slice(1),
// }),
// ]),
// );
// console.log(JSON.stringify(schemas, null, 2));
// }
// Re-export schemas for external usage
export {
StateExampleSchema,
MessageFieldSchema,
ErrorExampleSchema,
MessageSchema,
CommandSchema,
EventSchema,
StateSchema,
CommandSliceSchema,
QuerySliceSchema,
ReactSliceSchema,
SliceSchema,
FlowSchema,
};