@kapeta/local-cluster-service 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/cjs/index.js +2 -0
  3. package/dist/cjs/src/codeGeneratorManager.d.ts +1 -0
  4. package/dist/cjs/src/codeGeneratorManager.js +12 -6
  5. package/dist/cjs/src/middleware/cors.d.ts +1 -0
  6. package/dist/cjs/src/middleware/kapeta.d.ts +1 -0
  7. package/dist/cjs/src/middleware/stringBody.d.ts +1 -0
  8. package/dist/cjs/src/storm/codegen.d.ts +40 -0
  9. package/dist/cjs/src/storm/codegen.js +210 -0
  10. package/dist/cjs/src/storm/event-parser.d.ts +70 -0
  11. package/dist/cjs/src/storm/event-parser.js +522 -0
  12. package/dist/cjs/src/storm/events.d.ts +126 -0
  13. package/dist/cjs/src/storm/events.js +6 -0
  14. package/dist/cjs/src/storm/routes.d.ts +7 -0
  15. package/dist/cjs/src/storm/routes.js +87 -0
  16. package/dist/cjs/src/storm/stormClient.d.ts +13 -0
  17. package/dist/cjs/src/storm/stormClient.js +80 -0
  18. package/dist/cjs/src/storm/stream.d.ts +45 -0
  19. package/dist/cjs/src/storm/stream.js +57 -0
  20. package/dist/esm/index.js +2 -0
  21. package/dist/esm/src/codeGeneratorManager.d.ts +1 -0
  22. package/dist/esm/src/codeGeneratorManager.js +12 -6
  23. package/dist/esm/src/middleware/cors.d.ts +1 -0
  24. package/dist/esm/src/middleware/kapeta.d.ts +1 -0
  25. package/dist/esm/src/middleware/stringBody.d.ts +1 -0
  26. package/dist/esm/src/storm/codegen.d.ts +40 -0
  27. package/dist/esm/src/storm/codegen.js +210 -0
  28. package/dist/esm/src/storm/event-parser.d.ts +70 -0
  29. package/dist/esm/src/storm/event-parser.js +522 -0
  30. package/dist/esm/src/storm/events.d.ts +126 -0
  31. package/dist/esm/src/storm/events.js +6 -0
  32. package/dist/esm/src/storm/routes.d.ts +7 -0
  33. package/dist/esm/src/storm/routes.js +87 -0
  34. package/dist/esm/src/storm/stormClient.d.ts +13 -0
  35. package/dist/esm/src/storm/stormClient.js +80 -0
  36. package/dist/esm/src/storm/stream.d.ts +45 -0
  37. package/dist/esm/src/storm/stream.js +57 -0
  38. package/index.ts +2 -0
  39. package/package.json +3 -3
  40. package/src/codeGeneratorManager.ts +17 -8
  41. package/src/storm/codegen.ts +266 -0
  42. package/src/storm/event-parser.ts +668 -0
  43. package/src/storm/events.ts +168 -0
  44. package/src/storm/routes.ts +111 -0
  45. package/src/storm/stormClient.ts +106 -0
  46. package/src/storm/stream.ts +96 -0
  47. package/src/utils/BlockInstanceRunner.ts +4 -2
@@ -0,0 +1,266 @@
1
+ /**
2
+ * Copyright 2023 Kapeta Inc.
3
+ * SPDX-License-Identifier: BUSL-1.1
4
+ */
5
+
6
+ import { Definition } from '@kapeta/local-cluster-config';
7
+ import { AIFileTypes, BlockCodeGenerator, GeneratedFile, GeneratedResult } from '@kapeta/codegen';
8
+ import { BlockDefinition } from '@kapeta/schemas';
9
+ import { codeGeneratorManager } from '../codeGeneratorManager';
10
+ import { STORM_ID, stormClient } from './stormClient';
11
+ import { ScreenTemplate, StormEvent, StormEventFile, StormEventScreen } from './events';
12
+ import { BlockDefinitionInfo } from './event-parser';
13
+ import { ConversationItem, StormFileImplementationPrompt, StormFileInfo, StormStream } from './stream';
14
+ import { KapetaURI } from '@kapeta/nodejs-utils';
15
+
16
+ type ImplementationGenerator = (
17
+ prompt: StormFileImplementationPrompt,
18
+ history?: ConversationItem[]
19
+ ) => Promise<StormStream>;
20
+
21
+ export class StormCodegen {
22
+ private readonly userPrompt: string;
23
+ private readonly blocks: BlockDefinitionInfo[];
24
+ private readonly out = new StormStream();
25
+ private readonly events: StormEvent[];
26
+
27
+ constructor(userPrompt: string, blocks: BlockDefinitionInfo[], events: StormEvent[]) {
28
+ this.userPrompt = userPrompt;
29
+ this.blocks = blocks;
30
+ this.events = events;
31
+ }
32
+
33
+ public async process() {
34
+ for (const block of this.blocks) {
35
+ await this.processBlockCode(block);
36
+ }
37
+
38
+ this.out.end();
39
+ }
40
+
41
+ public getStream() {
42
+ return this.out;
43
+ }
44
+
45
+ private handleTemplateFileOutput(blockUri: KapetaURI, template: StormFileInfo, data: StormEvent) {
46
+ switch (data.type) {
47
+ case 'FILE':
48
+ template.filename = data.payload.filename;
49
+ template.content = data.payload.content;
50
+ return this.handleFileOutput(blockUri, data);
51
+ }
52
+ }
53
+
54
+ private handleUiOutput(blockUri: KapetaURI, data: StormEvent) {
55
+ switch (data.type) {
56
+ case 'SCREEN':
57
+ this.out.emit('data', {
58
+ type: 'SCREEN',
59
+ reason: data.reason,
60
+ created: Date.now(),
61
+ payload: {
62
+ ...data.payload,
63
+ blockName: blockUri.toNormalizedString(),
64
+ },
65
+ });
66
+ case 'FILE':
67
+ return this.handleFileOutput(blockUri, data);
68
+ }
69
+ }
70
+
71
+ private handleFileOutput(blockUri: KapetaURI, data: StormEvent) {
72
+ switch (data.type) {
73
+ case 'FILE':
74
+ this.emitFile(blockUri, data.payload.filename, data.payload.content, data.reason);
75
+ return {
76
+ type: 'FILE',
77
+ created: Date.now(),
78
+ payload: {
79
+ filename: data.payload.filename,
80
+ content: data.payload.content,
81
+ },
82
+ } as StormEventFile;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Generates the code for a block and sends it to the AI
88
+ */
89
+ private async processBlockCode(block: BlockDefinitionInfo) {
90
+ // Generate the code for the block using the standard codegen templates
91
+ const generatedResult = await this.generateBlock(block.content);
92
+ if (!generatedResult) {
93
+ return;
94
+ }
95
+
96
+ const allFiles = this.toStormFiles(generatedResult);
97
+
98
+ // Send all the non-ai files to the stream
99
+ this.emitFiles(block.uri, allFiles);
100
+
101
+ const relevantFiles: StormFileInfo[] = allFiles.filter(
102
+ (file) => file.type !== AIFileTypes.IGNORE && file.type !== AIFileTypes.WEB_SCREEN
103
+ );
104
+ const uiTemplates: StormFileInfo[] = allFiles.filter((file) => file.type === AIFileTypes.WEB_SCREEN);
105
+ if (uiTemplates.length > 0) {
106
+ const uiStream = await stormClient.createUIImplementation({
107
+ events: this.events,
108
+ templates: uiTemplates,
109
+ context: relevantFiles,
110
+ blockName: block.aiName,
111
+ prompt: this.userPrompt,
112
+ });
113
+
114
+ uiStream.on('data', (evt) => {
115
+ this.handleUiOutput(block.uri, evt);
116
+ });
117
+
118
+ await uiStream.waitForDone();
119
+ }
120
+
121
+ // Gather the context files for implementation. These will be all be passed to the AI
122
+ const contextFiles: StormFileInfo[] = relevantFiles.filter(
123
+ (file) => ![AIFileTypes.SERVICE, AIFileTypes.WEB_SCREEN].includes(file.type)
124
+ );
125
+
126
+ // Send the service and UI templates to the AI. These will be send one-by-one in addition to the context files
127
+ const serviceFiles: StormFileInfo[] = allFiles.filter((file) => file.type === AIFileTypes.SERVICE);
128
+ if (serviceFiles.length > 0) {
129
+ await this.processTemplates(
130
+ block.uri,
131
+ stormClient.createServiceImplementation.bind(stormClient),
132
+ serviceFiles,
133
+ contextFiles
134
+ );
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Emits the text-based files to the stream
140
+ */
141
+ private emitFiles(uri: KapetaURI, files: StormFileInfo[]) {
142
+ files.forEach((file) => {
143
+ if (!file.content || typeof file.content !== 'string') {
144
+ return;
145
+ }
146
+
147
+ if (file.type === AIFileTypes.SERVICE) {
148
+ // Don't send the service files to the stream yet
149
+ // They will need to be implemented by the AI
150
+ return;
151
+ }
152
+
153
+ if (file.type === AIFileTypes.WEB_SCREEN) {
154
+ // Don't send the web screen files to the stream yet
155
+ // They will need to be implemented by the AI
156
+ return;
157
+ }
158
+
159
+ this.emitFile(uri, file.filename, file.content);
160
+ });
161
+ }
162
+
163
+ private emitFile(uri: KapetaURI, filename: string, content: string, reason: string = 'File generated') {
164
+ this.out.emit('data', {
165
+ type: 'FILE',
166
+ reason,
167
+ created: Date.now(),
168
+ payload: {
169
+ filename: filename,
170
+ content: content,
171
+ blockRef: uri.toNormalizedString(),
172
+ },
173
+ } satisfies StormEventFile);
174
+ }
175
+
176
+ /**
177
+ * Sends the template to the AI and processes the response
178
+ */
179
+ private async processTemplates(
180
+ blockUri: KapetaURI,
181
+ generator: ImplementationGenerator,
182
+ templates: StormFileInfo[],
183
+ contextFiles: StormFileInfo[]
184
+ ) {
185
+ const promises = templates.map(async (templateFile) => {
186
+ const stream = await generator({
187
+ context: contextFiles,
188
+ template: templateFile,
189
+ prompt: this.userPrompt,
190
+ });
191
+
192
+ const files: StormEventFile[] = [];
193
+
194
+ stream.on('data', (evt) => {
195
+ const file = this.handleTemplateFileOutput(blockUri, templateFile, evt);
196
+ if (file) {
197
+ files.push(file);
198
+ }
199
+ });
200
+
201
+ await stream.waitForDone();
202
+ return files;
203
+ });
204
+
205
+ const fileChunks = await Promise.all(promises);
206
+
207
+ return fileChunks.flat();
208
+ }
209
+
210
+ /**
211
+ * Converts the generated files to a format that can be sent to the AI
212
+ */
213
+ private toStormFiles(generatedResult: GeneratedResult) {
214
+ const allFiles: StormFileInfo[] = generatedResult.files.map((file: GeneratedFile) => {
215
+ if (!file.content) {
216
+ return {
217
+ ...file,
218
+ type: AIFileTypes.IGNORE,
219
+ };
220
+ }
221
+ if (typeof file.content !== 'string') {
222
+ return {
223
+ ...file,
224
+ type: AIFileTypes.IGNORE,
225
+ };
226
+ }
227
+
228
+ const rx = /\/\/AI-TYPE:([a-z0-9- _]+)\n/gi;
229
+
230
+ const match = rx.exec(file.content);
231
+ if (!match) {
232
+ return {
233
+ ...file,
234
+ type: AIFileTypes.IGNORE,
235
+ };
236
+ }
237
+ const type = match[1].trim() as AIFileTypes;
238
+ file.content = file.content.replace(rx, '');
239
+
240
+ return {
241
+ ...file,
242
+ type,
243
+ };
244
+ });
245
+ return allFiles;
246
+ }
247
+
248
+ /**
249
+ * Generates the code using codegen for a given block.
250
+ */
251
+ private async generateBlock(yamlContent: Definition) {
252
+ if (!yamlContent.spec.target?.kind) {
253
+ //Not all block types have targets
254
+ return;
255
+ }
256
+
257
+ if (!(await codeGeneratorManager.ensureTarget(yamlContent.spec.target?.kind))) {
258
+ return;
259
+ }
260
+
261
+ const codeGenerator = new BlockCodeGenerator(yamlContent as BlockDefinition);
262
+ codeGenerator.withOption('AIContext', STORM_ID);
263
+
264
+ return codeGenerator.generate();
265
+ }
266
+ }