@kapeta/local-cluster-service 0.68.0 → 0.69.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [0.69.0](https://github.com/kapetacom/local-cluster-service/compare/v0.68.0...v0.69.0) (2024-09-03)
2
+
3
+
4
+ ### Features
5
+
6
+ * adding create-system api ([d35545a](https://github.com/kapetacom/local-cluster-service/commit/d35545a3f35916116eca2cd116e1e9b923f1a8c2))
7
+
1
8
  # [0.68.0](https://github.com/kapetacom/local-cluster-service/compare/v0.67.5...v0.68.0) (2024-09-03)
2
9
 
3
10
 
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  const express_promise_router_1 = __importDefault(require("express-promise-router"));
11
11
  const fs_extra_1 = __importDefault(require("fs-extra"));
12
12
  const path_1 = __importDefault(require("path"));
13
+ const node_os_1 = __importDefault(require("node:os"));
13
14
  const lodash_1 = __importDefault(require("lodash"));
14
15
  const cors_1 = require("../middleware/cors");
15
16
  const stringBody_1 = require("../middleware/stringBody");
@@ -24,6 +25,7 @@ const UIServer_1 = require("./UIServer");
24
25
  const crypto_1 = require("crypto");
25
26
  const PageGenerator_1 = require("./PageGenerator");
26
27
  const PromiseQueue_1 = require("./PromiseQueue");
28
+ const utils_1 = require("./utils");
27
29
  const UI_SERVERS = {};
28
30
  const router = (0, express_promise_router_1.default)();
29
31
  router.use('/', cors_1.corsHandler);
@@ -59,6 +61,20 @@ function convertPageEvent(screenData, innerConversationId, mainConversationId) {
59
61
  router.all('/ui/:systemId/serve/:method/*', async (req, res) => {
60
62
  (0, page_utils_1.readPageFromDisk)(req.params.systemId, req.params[0], req.params.method, res);
61
63
  });
64
+ router.post('/ui/create-system/:systemId', async (req, res) => {
65
+ const systemId = req.params.systemId;
66
+ const srcDir = (0, page_utils_1.getSystemBaseDir)(systemId);
67
+ const destDir = path_1.default.join(node_os_1.default.tmpdir(), 'ai-systems-impl', systemId);
68
+ await (0, utils_1.copyDirectory)(srcDir, destDir, async (fileName, content) => {
69
+ const result = await stormClient_1.stormClient.implementAPIClients({
70
+ content: content,
71
+ fileName: fileName,
72
+ });
73
+ return result;
74
+ });
75
+ res.end();
76
+ return;
77
+ });
62
78
  router.post('/ui/screen', async (req, res) => {
63
79
  try {
64
80
  const conversationId = req.headers[stormClient_1.ConversationIdHeader.toLowerCase()];
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ConversationItem, StormFileImplementationPrompt, StormStream, StormUIImplementationPrompt, StormUIListPrompt } from './stream';
2
+ import { ConversationItem, ImplementAPIClientsRequest, StormFileImplementationPrompt, StormStream, StormUIImplementationPrompt, StormUIListPrompt } from './stream';
3
3
  import { Page, StormEventPageUrl } from './events';
4
4
  export declare const STORM_ID = "storm";
5
5
  export declare const ConversationIdHeader = "Conversation-Id";
@@ -70,6 +70,7 @@ declare class StormClient {
70
70
  getVoteUIPage(topic: string, conversationId: string, mainConversationId?: string): Promise<{
71
71
  vote: -1 | 0 | 1;
72
72
  }>;
73
+ implementAPIClients(prompt: ImplementAPIClientsRequest): Promise<string>;
73
74
  classifyUIReferences(prompt: string, conversationId?: string): Promise<StormStream>;
74
75
  editPages(prompt: UIPageEditPrompt, conversationId?: string): Promise<StormStream>;
75
76
  listScreens(prompt: StormUIListPrompt, conversationId?: string): Promise<StormStream>;
@@ -137,6 +137,18 @@ class StormClient {
137
137
  });
138
138
  return response.json();
139
139
  }
140
+ async implementAPIClients(prompt) {
141
+ const u = `${this._baseUrl}/v2/ui/implement-api-clients`;
142
+ const response = await fetch(u, {
143
+ method: 'POST',
144
+ body: JSON.stringify({
145
+ fileName: prompt.fileName,
146
+ content: prompt.content,
147
+ }),
148
+ });
149
+ const data = await response.text();
150
+ return data;
151
+ }
140
152
  classifyUIReferences(prompt, conversationId) {
141
153
  return this.send('/v2/ui/references', {
142
154
  prompt: prompt,
@@ -72,3 +72,7 @@ export interface StormUIListPrompt {
72
72
  blockName: string;
73
73
  prompt: string;
74
74
  }
75
+ export interface ImplementAPIClientsRequest {
76
+ fileName: string;
77
+ content: string;
78
+ }
@@ -0,0 +1 @@
1
+ export declare function copyDirectory(src: string, dest: string, modifyHtml: (fileName: string, content: string) => Promise<string>): Promise<void>;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.copyDirectory = void 0;
7
+ /**
8
+ * Copyright 2023 Kapeta Inc.
9
+ * SPDX-License-Identifier: BUSL-1.1
10
+ */
11
+ const fs_extra_1 = __importDefault(require("fs-extra"));
12
+ const path_1 = __importDefault(require("path"));
13
+ async function copyDirectory(src, dest, modifyHtml) {
14
+ await fs_extra_1.default.promises.mkdir(dest, { recursive: true });
15
+ const entries = await fs_extra_1.default.promises.readdir(src, { withFileTypes: true });
16
+ for (const entry of entries) {
17
+ const srcPath = path_1.default.join(src, entry.name);
18
+ const destPath = path_1.default.join(dest, entry.name);
19
+ if (entry.isDirectory()) {
20
+ await copyDirectory(srcPath, destPath, modifyHtml);
21
+ }
22
+ else if (entry.isFile()) {
23
+ let content = await fs_extra_1.default.promises.readFile(srcPath, 'utf-8');
24
+ if (path_1.default.extname(srcPath) === '.html') {
25
+ content = await modifyHtml(srcPath, content);
26
+ }
27
+ await fs_extra_1.default.promises.writeFile(destPath, content, 'utf-8');
28
+ }
29
+ }
30
+ }
31
+ exports.copyDirectory = copyDirectory;
@@ -10,6 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  const express_promise_router_1 = __importDefault(require("express-promise-router"));
11
11
  const fs_extra_1 = __importDefault(require("fs-extra"));
12
12
  const path_1 = __importDefault(require("path"));
13
+ const node_os_1 = __importDefault(require("node:os"));
13
14
  const lodash_1 = __importDefault(require("lodash"));
14
15
  const cors_1 = require("../middleware/cors");
15
16
  const stringBody_1 = require("../middleware/stringBody");
@@ -24,6 +25,7 @@ const UIServer_1 = require("./UIServer");
24
25
  const crypto_1 = require("crypto");
25
26
  const PageGenerator_1 = require("./PageGenerator");
26
27
  const PromiseQueue_1 = require("./PromiseQueue");
28
+ const utils_1 = require("./utils");
27
29
  const UI_SERVERS = {};
28
30
  const router = (0, express_promise_router_1.default)();
29
31
  router.use('/', cors_1.corsHandler);
@@ -59,6 +61,20 @@ function convertPageEvent(screenData, innerConversationId, mainConversationId) {
59
61
  router.all('/ui/:systemId/serve/:method/*', async (req, res) => {
60
62
  (0, page_utils_1.readPageFromDisk)(req.params.systemId, req.params[0], req.params.method, res);
61
63
  });
64
+ router.post('/ui/create-system/:systemId', async (req, res) => {
65
+ const systemId = req.params.systemId;
66
+ const srcDir = (0, page_utils_1.getSystemBaseDir)(systemId);
67
+ const destDir = path_1.default.join(node_os_1.default.tmpdir(), 'ai-systems-impl', systemId);
68
+ await (0, utils_1.copyDirectory)(srcDir, destDir, async (fileName, content) => {
69
+ const result = await stormClient_1.stormClient.implementAPIClients({
70
+ content: content,
71
+ fileName: fileName,
72
+ });
73
+ return result;
74
+ });
75
+ res.end();
76
+ return;
77
+ });
62
78
  router.post('/ui/screen', async (req, res) => {
63
79
  try {
64
80
  const conversationId = req.headers[stormClient_1.ConversationIdHeader.toLowerCase()];
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { ConversationItem, StormFileImplementationPrompt, StormStream, StormUIImplementationPrompt, StormUIListPrompt } from './stream';
2
+ import { ConversationItem, ImplementAPIClientsRequest, StormFileImplementationPrompt, StormStream, StormUIImplementationPrompt, StormUIListPrompt } from './stream';
3
3
  import { Page, StormEventPageUrl } from './events';
4
4
  export declare const STORM_ID = "storm";
5
5
  export declare const ConversationIdHeader = "Conversation-Id";
@@ -70,6 +70,7 @@ declare class StormClient {
70
70
  getVoteUIPage(topic: string, conversationId: string, mainConversationId?: string): Promise<{
71
71
  vote: -1 | 0 | 1;
72
72
  }>;
73
+ implementAPIClients(prompt: ImplementAPIClientsRequest): Promise<string>;
73
74
  classifyUIReferences(prompt: string, conversationId?: string): Promise<StormStream>;
74
75
  editPages(prompt: UIPageEditPrompt, conversationId?: string): Promise<StormStream>;
75
76
  listScreens(prompt: StormUIListPrompt, conversationId?: string): Promise<StormStream>;
@@ -137,6 +137,18 @@ class StormClient {
137
137
  });
138
138
  return response.json();
139
139
  }
140
+ async implementAPIClients(prompt) {
141
+ const u = `${this._baseUrl}/v2/ui/implement-api-clients`;
142
+ const response = await fetch(u, {
143
+ method: 'POST',
144
+ body: JSON.stringify({
145
+ fileName: prompt.fileName,
146
+ content: prompt.content,
147
+ }),
148
+ });
149
+ const data = await response.text();
150
+ return data;
151
+ }
140
152
  classifyUIReferences(prompt, conversationId) {
141
153
  return this.send('/v2/ui/references', {
142
154
  prompt: prompt,
@@ -72,3 +72,7 @@ export interface StormUIListPrompt {
72
72
  blockName: string;
73
73
  prompt: string;
74
74
  }
75
+ export interface ImplementAPIClientsRequest {
76
+ fileName: string;
77
+ content: string;
78
+ }
@@ -0,0 +1 @@
1
+ export declare function copyDirectory(src: string, dest: string, modifyHtml: (fileName: string, content: string) => Promise<string>): Promise<void>;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.copyDirectory = void 0;
7
+ /**
8
+ * Copyright 2023 Kapeta Inc.
9
+ * SPDX-License-Identifier: BUSL-1.1
10
+ */
11
+ const fs_extra_1 = __importDefault(require("fs-extra"));
12
+ const path_1 = __importDefault(require("path"));
13
+ async function copyDirectory(src, dest, modifyHtml) {
14
+ await fs_extra_1.default.promises.mkdir(dest, { recursive: true });
15
+ const entries = await fs_extra_1.default.promises.readdir(src, { withFileTypes: true });
16
+ for (const entry of entries) {
17
+ const srcPath = path_1.default.join(src, entry.name);
18
+ const destPath = path_1.default.join(dest, entry.name);
19
+ if (entry.isDirectory()) {
20
+ await copyDirectory(srcPath, destPath, modifyHtml);
21
+ }
22
+ else if (entry.isFile()) {
23
+ let content = await fs_extra_1.default.promises.readFile(srcPath, 'utf-8');
24
+ if (path_1.default.extname(srcPath) === '.html') {
25
+ content = await modifyHtml(srcPath, content);
26
+ }
27
+ await fs_extra_1.default.promises.writeFile(destPath, content, 'utf-8');
28
+ }
29
+ }
30
+ }
31
+ exports.copyDirectory = copyDirectory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapeta/local-cluster-service",
3
- "version": "0.68.0",
3
+ "version": "0.69.0",
4
4
  "description": "Manages configuration, ports and service discovery for locally running Kapeta systems",
5
5
  "type": "commonjs",
6
6
  "exports": {
@@ -7,6 +7,7 @@ import Router from 'express-promise-router';
7
7
  import FS from 'fs-extra';
8
8
  import { Response } from 'express';
9
9
  import Path from 'path';
10
+ import os from 'node:os';
10
11
  import _ from 'lodash';
11
12
  import { corsHandler } from '../middleware/cors';
12
13
  import { stringBody } from '../middleware/stringBody';
@@ -34,11 +35,12 @@ import {
34
35
  import { StormCodegen } from './codegen';
35
36
  import { assetManager } from '../assetManager';
36
37
  import uuid from 'node-uuid';
37
- import { readPageFromDisk, SystemIdHeader, writeAssetToDisk, writeImageToDisk, writePageToDisk } from './page-utils';
38
+ import { getSystemBaseDir, readPageFromDisk, resolveReadPath, SystemIdHeader, writeAssetToDisk, writeImageToDisk, writePageToDisk } from './page-utils';
38
39
  import { UIServer } from './UIServer';
39
40
  import { randomUUID } from 'crypto';
40
41
  import { ImagePrompt, PageQueue } from './PageGenerator';
41
42
  import { createFuture } from './PromiseQueue';
43
+ import { copyDirectory } from './utils';
42
44
 
43
45
  const UI_SERVERS: { [key: string]: UIServer } = {};
44
46
  const router = Router();
@@ -81,6 +83,25 @@ router.all('/ui/:systemId/serve/:method/*', async (req: KapetaBodyRequest, res:
81
83
  readPageFromDisk(req.params.systemId, req.params[0], req.params.method, res);
82
84
  });
83
85
 
86
+ router.post('/ui/create-system/:systemId', async (req: KapetaBodyRequest, res: Response) => {
87
+ const systemId = req.params.systemId as string;
88
+ const srcDir = getSystemBaseDir(systemId);
89
+ const destDir = Path.join(os.tmpdir(), 'ai-systems-impl', systemId);
90
+
91
+ await copyDirectory(srcDir, destDir, async (fileName, content) => {
92
+ const result = await stormClient.implementAPIClients({
93
+ content: content,
94
+ fileName: fileName,
95
+ });
96
+ return result;
97
+ });
98
+
99
+ res.end();
100
+ return;
101
+ });
102
+
103
+
104
+
84
105
  router.post('/ui/screen', async (req: KapetaBodyRequest, res: Response) => {
85
106
  try {
86
107
  const conversationId = req.headers[ConversationIdHeader.toLowerCase()] as string | undefined;
@@ -8,13 +8,13 @@ import readLine from 'node:readline/promises';
8
8
  import { Readable } from 'node:stream';
9
9
  import {
10
10
  ConversationItem,
11
+ ImplementAPIClientsRequest,
11
12
  StormContextRequest,
12
13
  StormFileImplementationPrompt,
13
14
  StormStream,
14
15
  StormUIImplementationPrompt,
15
16
  StormUIListPrompt,
16
17
  } from './stream';
17
- import { getRawAsset } from 'node:sea';
18
18
  import { Page, StormEventPageUrl } from './events';
19
19
 
20
20
  export const STORM_ID = 'storm';
@@ -242,6 +242,19 @@ class StormClient {
242
242
  return response.json() as Promise<{ vote: -1 | 0 | 1 }>;
243
243
  }
244
244
 
245
+ public async implementAPIClients(prompt: ImplementAPIClientsRequest) {
246
+ const u = `${this._baseUrl}/v2/ui/implement-api-clients`;
247
+ const response = await fetch(u, {
248
+ method: 'POST',
249
+ body: JSON.stringify({
250
+ fileName: prompt.fileName,
251
+ content: prompt.content,
252
+ }),
253
+ });
254
+ const data = await response.text();
255
+ return data;
256
+ }
257
+
245
258
  public classifyUIReferences(prompt: string, conversationId?: string) {
246
259
  return this.send('/v2/ui/references', {
247
260
  prompt: prompt,
@@ -142,3 +142,8 @@ export interface StormUIListPrompt {
142
142
  blockName: string;
143
143
  prompt: string;
144
144
  }
145
+
146
+ export interface ImplementAPIClientsRequest {
147
+ fileName: string;
148
+ content: string
149
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Copyright 2023 Kapeta Inc.
3
+ * SPDX-License-Identifier: BUSL-1.1
4
+ */
5
+ import FS from 'fs-extra';
6
+ import Path from 'path';
7
+
8
+ export async function copyDirectory(src: string, dest: string, modifyHtml: (fileName: string, content: string) => Promise<string>): Promise<void> {
9
+ await FS.promises.mkdir(dest, { recursive: true });
10
+ const entries = await FS.promises.readdir(src, { withFileTypes: true });
11
+
12
+ for (const entry of entries) {
13
+ const srcPath = Path.join(src, entry.name);
14
+ const destPath = Path.join(dest, entry.name);
15
+
16
+ if (entry.isDirectory()) {
17
+ await copyDirectory(srcPath, destPath, modifyHtml);
18
+ } else if (entry.isFile()) {
19
+ let content = await FS.promises.readFile(srcPath, 'utf-8');
20
+
21
+ if (Path.extname(srcPath) === '.html') {
22
+ content = await modifyHtml(srcPath, content);
23
+ }
24
+
25
+ await FS.promises.writeFile(destPath, content, 'utf-8');
26
+ }
27
+ }
28
+ }