@naiv/swan 0.0.1

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 (42) hide show
  1. package/SWAN-DSL.md +832 -0
  2. package/dist/SWANServer.d.ts +26 -0
  3. package/dist/SWANServer.d.ts.map +1 -0
  4. package/dist/SWANServer.js +158 -0
  5. package/dist/SWANServer.js.map +1 -0
  6. package/dist/agent-mode/agent-capability.d.ts +28 -0
  7. package/dist/agent-mode/agent-capability.d.ts.map +1 -0
  8. package/dist/agent-mode/agent-capability.js +82 -0
  9. package/dist/agent-mode/agent-capability.js.map +1 -0
  10. package/dist/agent-mode/plan-execution-and-next.d.ts +3 -0
  11. package/dist/agent-mode/plan-execution-and-next.d.ts.map +1 -0
  12. package/dist/agent-mode/plan-execution-and-next.js +33 -0
  13. package/dist/agent-mode/plan-execution-and-next.js.map +1 -0
  14. package/dist/agent-mode/plan-preparation.d.ts +11 -0
  15. package/dist/agent-mode/plan-preparation.d.ts.map +1 -0
  16. package/dist/agent-mode/plan-preparation.js +98 -0
  17. package/dist/agent-mode/plan-preparation.js.map +1 -0
  18. package/dist/command/cmd-chat.d.ts +2 -0
  19. package/dist/command/cmd-chat.d.ts.map +1 -0
  20. package/dist/command/cmd-chat.js +137 -0
  21. package/dist/command/cmd-chat.js.map +1 -0
  22. package/dist/command/cmd-run.d.ts +2 -0
  23. package/dist/command/cmd-run.d.ts.map +1 -0
  24. package/dist/command/cmd-run.js +42 -0
  25. package/dist/command/cmd-run.js.map +1 -0
  26. package/dist/exec.d.ts +3 -0
  27. package/dist/exec.d.ts.map +1 -0
  28. package/dist/exec.js +78 -0
  29. package/dist/exec.js.map +1 -0
  30. package/dist/index.d.ts +2 -0
  31. package/dist/index.d.ts.map +1 -0
  32. package/dist/index.js +41 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/prompt.d.ts +7 -0
  35. package/dist/prompt.d.ts.map +1 -0
  36. package/dist/prompt.js +90 -0
  37. package/dist/prompt.js.map +1 -0
  38. package/dist/sample-page.d.ts +3 -0
  39. package/dist/sample-page.d.ts.map +1 -0
  40. package/dist/sample-page.js +144 -0
  41. package/dist/sample-page.js.map +1 -0
  42. package/package.json +50 -0
@@ -0,0 +1,26 @@
1
+ import { Express } from 'express';
2
+ import { Program } from '@naiv/swan-dsl';
3
+ export interface SystemParam {
4
+ web_prefix?: string;
5
+ port?: number;
6
+ beforeStart?(): Promise<void>;
7
+ swan_content_string: string;
8
+ swan_result: Program;
9
+ llm_model?: string;
10
+ }
11
+ export interface ServerConstructorParam {
12
+ noCors?: boolean;
13
+ noTrustProxy?: boolean;
14
+ swanCwd: string;
15
+ llm_model?: string;
16
+ }
17
+ export declare class SWANServer {
18
+ express: Express;
19
+ private server_instance;
20
+ private constructor_param;
21
+ private server_param;
22
+ constructor(param?: ServerConstructorParam);
23
+ private getPageContentAndUtility;
24
+ run(param: SystemParam): Promise<SWANServer>;
25
+ }
26
+ //# sourceMappingURL=SWANServer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SWANServer.d.ts","sourceRoot":"","sources":["../SWANServer.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,OAAO,EAA6B,MAAM,SAAS,CAAC;AAGtE,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AASzC,MAAM,WAAW,WAAW;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IAC7B,mBAAmB,EAAE,MAAM,CAAA;IAC3B,WAAW,EAAE,OAAO,CAAA;IACpB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,sBAAsB;IACrC,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,UAAU;IACd,OAAO,EAAE,OAAO,CAAa;IACpC,OAAO,CAAC,eAAe,CAAoE;IAC3F,OAAO,CAAC,iBAAiB,CAAqC;IAC9D,OAAO,CAAC,YAAY,CAA0B;gBAElC,KAAK,CAAC,EAAE,sBAAsB;YAI5B,wBAAwB;IAkBzB,GAAG,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;CAyF1D"}
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.SWANServer = void 0;
40
+ const express_1 = __importStar(require("express"));
41
+ const cors_1 = __importDefault(require("cors"));
42
+ const sample_page_1 = require("./sample-page");
43
+ const react_bundler_1 = require("@naiv/react-bundler");
44
+ const plan_preparation_1 = require("./agent-mode/plan-preparation");
45
+ const path_1 = __importDefault(require("path"));
46
+ const plan_execution_and_next_1 = require("./agent-mode/plan-execution-and-next");
47
+ const fs_1 = __importDefault(require("fs"));
48
+ const agent_capability_1 = require("./agent-mode/agent-capability");
49
+ class SWANServer {
50
+ express = (0, express_1.default)();
51
+ server_instance;
52
+ constructor_param;
53
+ server_param;
54
+ constructor(param) {
55
+ this.constructor_param = param;
56
+ }
57
+ async getPageContentAndUtility(page_name, relative_implementation_folder_path, relative_utility_path, cwd) {
58
+ const list_dir = [
59
+ ...(0, agent_capability_1.ListDir)(relative_implementation_folder_path, cwd),
60
+ ...(0, agent_capability_1.ListDir)(relative_utility_path, cwd)
61
+ ];
62
+ const page_file_index = list_dir.findIndex(f => f == `original-implementation/${page_name}/index.tsx`);
63
+ if (page_file_index == -1) {
64
+ throw new Error(`no main content file on implementation page ${page_name}`);
65
+ }
66
+ return {
67
+ page_content: await fs_1.default.promises.readFile(path_1.default.resolve(cwd, list_dir[page_file_index]), 'utf-8'),
68
+ plugins: await Promise.all(list_dir.filter((_, i) => i !== page_file_index).map(async (f) => ({
69
+ path: `./${f}`,
70
+ content: await fs_1.default.promises.readFile(path_1.default.resolve(cwd, f), 'utf-8')
71
+ })))
72
+ };
73
+ }
74
+ async run(param) {
75
+ this.server_param = param;
76
+ if (!this.constructor_param?.noCors) {
77
+ this.express.use((0, cors_1.default)());
78
+ }
79
+ this.express.use(express_1.default.json({ limit: '5mb' }));
80
+ if (!this.constructor_param?.noTrustProxy) {
81
+ this.express.set('trust proxy', true);
82
+ }
83
+ if (param.beforeStart) {
84
+ await param.beforeStart();
85
+ }
86
+ const cwd = this.constructor_param?.swanCwd || '.';
87
+ const transpiled_implementation_folder_path = path_1.default.resolve(cwd, './__implementation__');
88
+ const port = param?.port ?? process.env.PORT ?? 5349;
89
+ this.express.get('/__page_status__', async (req, res) => {
90
+ if (!req.query.name && !res.headersSent) {
91
+ res.status(400).send('');
92
+ return;
93
+ }
94
+ const page_name = req.query.name;
95
+ const transpiled_page_file_path = path_1.default.resolve(transpiled_implementation_folder_path, `${page_name}.html`);
96
+ if (fs_1.default.existsSync(transpiled_page_file_path)) {
97
+ res.type('html').send(await fs_1.default.promises.readFile(transpiled_page_file_path, 'utf-8'));
98
+ if (!res.headersSent) {
99
+ res.status(200).send('');
100
+ }
101
+ return;
102
+ }
103
+ if (!res.headersSent) {
104
+ res.status(503).send('');
105
+ }
106
+ });
107
+ const router = (0, express_1.Router)();
108
+ for (const page of (this.server_param?.swan_result.pages || [])) {
109
+ const page_path = this.server_param?.swan_result.app.entry == page.name ? '/' : `/${page.name}`;
110
+ console.log(`${page_path}: ${page.name}`);
111
+ const relative_implementation_path = `original-implementation/${page.name}`;
112
+ const realtive_utility_path = `utility`;
113
+ const transpiled_page_file_path = path_1.default.resolve(transpiled_implementation_folder_path, `${page.name}.html`);
114
+ router.get(page_path, async (req, res) => {
115
+ if (fs_1.default.existsSync(transpiled_page_file_path)) {
116
+ res.type('html').send(await fs_1.default.promises.readFile(transpiled_page_file_path, 'utf-8'));
117
+ return;
118
+ }
119
+ const page_source = await (0, react_bundler_1.bundleHTML)((0, sample_page_1.getEntryPointCode)(), [{
120
+ path: 'App.tsx',
121
+ content: (0, sample_page_1.getLoadingPage)(page.name)
122
+ }], { node_modules_base_path: this.constructor_param?.swanCwd });
123
+ res.type('html').send(page_source);
124
+ // Start generating
125
+ let impl_plan = await (0, plan_preparation_1.generateFirstImplementationPlan)({
126
+ page_name: page.name,
127
+ target_implementation_folder_path: relative_implementation_path,
128
+ blueprint: this.server_param?.swan_content_string || ''
129
+ }, this.constructor_param.llm_model);
130
+ while (impl_plan.instruction.instruction != 'finished') {
131
+ impl_plan = await (0, plan_execution_and_next_1.executePlanAndGetNextPlan)(page.name, impl_plan, cwd);
132
+ }
133
+ console.log('Finished');
134
+ try {
135
+ const implementation_files = await this.getPageContentAndUtility(page.name, relative_implementation_path, realtive_utility_path, cwd);
136
+ const result_html = await (0, react_bundler_1.bundleHTML)((0, sample_page_1.getEntryPointCode)(`./${relative_implementation_path}/index.tsx`), [{
137
+ path: `./${relative_implementation_path}/index.tsx`,
138
+ content: implementation_files.page_content
139
+ }, ...implementation_files.plugins], {
140
+ node_modules_base_path: this.constructor_param?.swanCwd
141
+ });
142
+ await fs_1.default.promises.writeFile(transpiled_page_file_path, result_html);
143
+ console.log(`Transpiled file created`);
144
+ }
145
+ catch (err) {
146
+ console.error(err);
147
+ }
148
+ });
149
+ }
150
+ this.express.use((this.server_param.web_prefix || '/'), router);
151
+ this.server_instance = this.express.listen(port, () => {
152
+ console.log(`\n⚡️[server]: Server is running at http://localhost:${port}`);
153
+ });
154
+ return this;
155
+ }
156
+ }
157
+ exports.SWANServer = SWANServer;
158
+ //# sourceMappingURL=SWANServer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SWANServer.js","sourceRoot":"","sources":["../SWANServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mDAAsE;AACtE,gDAAwB;AAGxB,+CAAkE;AAClE,uDAA6D;AAC7D,oEAAgF;AAChF,gDAAwB;AACxB,kFAAiF;AACjF,4CAAoB;AACpB,oEAAwD;AAkBxD,MAAa,UAAU;IACd,OAAO,GAAY,IAAA,iBAAO,GAAE,CAAC;IAC5B,eAAe,CAAoE;IACnF,iBAAiB,CAAqC;IACtD,YAAY,CAA0B;IAE9C,YAAY,KAA8B;QACxC,IAAI,CAAC,iBAAiB,GAAG,KAAK,CAAC;IACjC,CAAC;IAEO,KAAK,CAAC,wBAAwB,CAAC,SAAiB,EAAE,mCAA2C,EAAE,qBAA6B,EAAE,GAAW;QAC/I,MAAM,QAAQ,GAAG;Y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C,EAAE;wBACnC,sBAAsB,EAAE,IAAI,CAAC,iBAAiB,EAAE,OAAO;qBACxD,CAAC,CAAC;oBACH,MAAM,YAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,yBAAyB,EAAE,WAAW,CAAC,CAAC;oBACpE,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAC;gBACzC,CAAC;gBAAC,OAAO,GAAQ,EAAE,CAAC;oBAClB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACrB,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QACD,IAAI,CAAC,OAAQ,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU,IAAI,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;QACjE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;YACpD,OAAO,CAAC,GAAG,CAAC,uDAAuD,IAAI,EAAE,CAAC,CAAC;QAC7E,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AArHD,gCAqHC"}
@@ -0,0 +1,28 @@
1
+ export interface AgentCapListFiles {
2
+ instruction: 'list-file';
3
+ description: string;
4
+ }
5
+ export interface AgentCapWriteFile {
6
+ instruction: 'write-file';
7
+ filename: string;
8
+ content: string;
9
+ description: string;
10
+ }
11
+ export interface AgentCapReadFile {
12
+ instruction: 'read-file';
13
+ filename: string;
14
+ description: string;
15
+ }
16
+ export interface AgentCapRemoveFile {
17
+ instruction: 'remove-file';
18
+ filename: string;
19
+ description: string;
20
+ }
21
+ export interface AgentCapFinished {
22
+ instruction: 'finished';
23
+ }
24
+ export type AgentCap = AgentCapListFiles | AgentCapReadFile | AgentCapRemoveFile | AgentCapWriteFile | AgentCapFinished;
25
+ export declare const utility_relative_dir_path = "utility";
26
+ export declare function executeAgentCapability(page_name: string, cap: AgentCap, cwd: string): Promise<string>;
27
+ export declare function ListDir(target_path: string, cwd: string): string[];
28
+ //# sourceMappingURL=agent-capability.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-capability.d.ts","sourceRoot":"","sources":["../../agent-mode/agent-capability.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,WAAW,CAAA;IACxB,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,YAAY,CAAA;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,WAAW,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,aAAa,CAAA;IAC1B,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,UAAU,CAAA;CACxB;AAED,MAAM,MAAM,QAAQ,GAAG,iBAAiB,GACpC,gBAAgB,GAChB,kBAAkB,GAClB,iBAAiB,GACjB,gBAAgB,CAAC;AAErB,eAAO,MAAM,yBAAyB,YAAY,CAAC;AAEnD,wBAAsB,sBAAsB,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA6C3G;AAED,wBAAgB,OAAO,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CA6BlE"}
@@ -0,0 +1,82 @@
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.utility_relative_dir_path = void 0;
7
+ exports.executeAgentCapability = executeAgentCapability;
8
+ exports.ListDir = ListDir;
9
+ const path_1 = __importDefault(require("path"));
10
+ const fs_1 = __importDefault(require("fs"));
11
+ exports.utility_relative_dir_path = `utility`;
12
+ async function executeAgentCapability(page_name, cap, cwd) {
13
+ console.log(`execute ${cap.instruction} - ${cap.description || ''}`);
14
+ const page_relative_dir_path = `original-implementation/${page_name}`;
15
+ const utility_abs_dir_path = path_1.default.resolve(cwd, exports.utility_relative_dir_path);
16
+ const page_abs_dir_path = path_1.default.resolve(cwd, page_relative_dir_path);
17
+ switch (cap.instruction) {
18
+ case "read-file":
19
+ case "remove-file":
20
+ case "write-file":
21
+ if (!cap.filename.startsWith(exports.utility_relative_dir_path) && !cap.filename.startsWith(page_relative_dir_path)) {
22
+ throw new Error(`You are not allowed to do action on file outside folders: ${exports.utility_relative_dir_path} or ${page_relative_dir_path}`);
23
+ }
24
+ case "list-file":
25
+ case "finished":
26
+ }
27
+ if (!fs_1.default.existsSync(utility_abs_dir_path)) {
28
+ await fs_1.default.promises.mkdir(utility_abs_dir_path);
29
+ }
30
+ if (!fs_1.default.existsSync(page_abs_dir_path)) {
31
+ await fs_1.default.promises.mkdir(page_abs_dir_path);
32
+ }
33
+ switch (cap.instruction) {
34
+ case "list-file":
35
+ try {
36
+ const list_dir = [
37
+ ...ListDir(utility_abs_dir_path, cwd),
38
+ ...ListDir(page_abs_dir_path, cwd)
39
+ ];
40
+ return list_dir.length == 0 ? 'Empty directory' : list_dir.join('\n');
41
+ }
42
+ catch (err) {
43
+ throw new Error(err.toString().replace(new RegExp(cwd, 'g'), ''));
44
+ }
45
+ case "read-file":
46
+ return await fs_1.default.promises.readFile(path_1.default.resolve(cwd, cap.filename), 'utf-8');
47
+ case "remove-file":
48
+ await fs_1.default.promises.rm(path_1.default.resolve(cwd, cap.filename), { force: true });
49
+ return `File ${cap.filename} deleted.`;
50
+ case "write-file":
51
+ await fs_1.default.promises.writeFile(path_1.default.resolve(cwd, cap.filename), cap.content);
52
+ return `File ${cap.filename} successfully created.`;
53
+ case "finished":
54
+ return 'Finished.';
55
+ }
56
+ }
57
+ function ListDir(target_path, cwd) {
58
+ function getAllFilesRecursive(dirPath, excludedFolders = new Set()) {
59
+ let filesList = [];
60
+ // Check if the current directory should be excluded
61
+ const folderName = path_1.default.basename(dirPath);
62
+ if (excludedFolders.has(folderName)) {
63
+ return filesList; // Skip this directory and all its contents
64
+ }
65
+ const files = fs_1.default.readdirSync(dirPath, { withFileTypes: true });
66
+ for (const file of files) {
67
+ const fullPath = path_1.default.join(dirPath, file.name);
68
+ if (file.isDirectory()) {
69
+ // Recursively call the function for subdirectories
70
+ filesList = filesList.concat(getAllFilesRecursive(fullPath, excludedFolders));
71
+ }
72
+ else {
73
+ // Add the file to the list
74
+ filesList.push(fullPath.replace(/\"/g, ''));
75
+ }
76
+ }
77
+ return filesList;
78
+ }
79
+ const foldersToExclude = new Set(['node_modules', '.git', '.cache']);
80
+ return getAllFilesRecursive(path_1.default.resolve(cwd, target_path), foldersToExclude).map(p => p.replace(cwd.endsWith('/') ? cwd : (cwd + '/'), ''));
81
+ }
82
+ //# sourceMappingURL=agent-capability.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-capability.js","sourceRoot":"","sources":["../../agent-mode/agent-capability.ts"],"names":[],"mappings":";;;;;;AAuCA,wDA6CC;AAED,0BA6BC;AAnHD,gDAAuB;AACvB,4CAAoB;AAoCP,QAAA,yBAAyB,GAAG,SAAS,CAAC;AAE5C,KAAK,UAAU,sBAAsB,CAAC,SAAiB,EAAE,GAAa,EAAE,GAAW;IACxF,OAAO,CAAC,GAAG,CAAC,WAAW,GAAG,CAAC,WAAW,MAAO,GAAW,CAAC,WAAW,IAAI,EAAE,EAAE,CAAC,CAAC;IAC9E,MAAM,sBAAsB,GAAG,2BAA2B,SAAS,EAAE,CAAC;IACtE,MAAM,oBAAoB,GAAG,cAAI,CAAC,OAAO,CAAC,GAAG,EAA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E,CAAC,CAAC;QAE/D,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,QAAQ,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YAE/C,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;gBACvB,mDAAmD;gBACnD,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,oBAAoB,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC;YAChF,CAAC;iBAAM,CAAC;gBACN,2BAA2B;gBAC3B,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC;YAC9C,CAAC;QACH,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,cAAc,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IACrE,OAAO,oBAAoB,CAAC,cAAI,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,CAAC,EAAE,gBAAgB,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAC/I,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { ImplPlanResponse } from "./plan-preparation";
2
+ export declare function executePlanAndGetNextPlan(page_name: string, impl: ImplPlanResponse, cwd: string): Promise<ImplPlanResponse>;
3
+ //# sourceMappingURL=plan-execution-and-next.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-execution-and-next.d.ts","sourceRoot":"","sources":["../../agent-mode/plan-execution-and-next.ts"],"names":[],"mappings":"AACA,OAAO,EAA0B,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE9E,wBAAsB,yBAAyB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAwBjI"}
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executePlanAndGetNextPlan = executePlanAndGetNextPlan;
4
+ const agent_capability_1 = require("./agent-capability");
5
+ const plan_preparation_1 = require("./plan-preparation");
6
+ async function executePlanAndGetNextPlan(page_name, impl, cwd) {
7
+ let res = 'error please fix your latest plan';
8
+ while (true) {
9
+ try {
10
+ const execution_result = await (0, agent_capability_1.executeAgentCapability)(page_name, impl.instruction, cwd);
11
+ res = await impl.llm.ask(execution_result, impl.session.id);
12
+ break;
13
+ }
14
+ catch (err) {
15
+ const error = `There is an error when executing the plan: ${err.toString()}`;
16
+ res = await impl.llm.ask(error, impl.session.id);
17
+ }
18
+ }
19
+ while (true) {
20
+ try {
21
+ return {
22
+ instruction: JSON.parse((0, plan_preparation_1.extractCodeBlocksAgent)(res)),
23
+ llm: impl.llm,
24
+ session: impl.session
25
+ };
26
+ }
27
+ catch (err) {
28
+ const error = `There is an error on your plan response JSON format: ${err.toString()}`;
29
+ res = await impl.llm.ask(error, impl.session.id);
30
+ }
31
+ }
32
+ }
33
+ //# sourceMappingURL=plan-execution-and-next.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-execution-and-next.js","sourceRoot":"","sources":["../../agent-mode/plan-execution-and-next.ts"],"names":[],"mappings":";;AAGA,8DAwBC;AA3BD,yDAAsE;AACtE,yDAA8E;AAEvE,KAAK,UAAU,yBAAyB,CAAC,SAAiB,EAAE,IAAsB,EAAE,GAAW;IACpG,IAAI,GAAG,GAAG,mCAAmC,CAAC;IAC9C,OAAO,IAAI,EAAE,CAAC;QACZ,IAAI,CAAC;YACH,MAAM,gBAAgB,GAAG,MAAM,IAAA,yCAAsB,EAAC,SAAS,EAAE,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;YACxF,GAAG,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;YAC5D,MAAM;QACR,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,MAAM,KAAK,GAAG,8CAA8C,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC;YAC7E,GAAG,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;IACD,OAAO,IAAI,EAAE,CAAC;QACZ,IAAI,CAAC;YACH,OAAO;gBACL,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,IAAA,yCAAsB,EAAC,GAAG,CAAC,CAAa;gBAChE,GAAG,EAAE,IAAI,CAAC,GAAG;gBACb,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC;QACJ,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAClB,MAAM,KAAK,GAAG,wDAAwD,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC;YACvF,GAAG,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACnD,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { ChatGPTLLM, LLMRunner } from "@graf-research/llm-runner";
2
+ import { AgentCap } from "./agent-capability";
3
+ import { ImplPromptParam } from "../prompt";
4
+ export interface ImplPlanResponse {
5
+ instruction: AgentCap;
6
+ llm: ChatGPTLLM;
7
+ session: LLMRunner.ChatSession;
8
+ }
9
+ export declare function generateFirstImplementationPlan(param: ImplPromptParam, _llm_model?: string): Promise<ImplPlanResponse>;
10
+ export declare function extractCodeBlocksAgent(text: string): string;
11
+ //# sourceMappingURL=plan-preparation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-preparation.d.ts","sourceRoot":"","sources":["../../agent-mode/plan-preparation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAAE,QAAQ,EAA6B,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAiB,eAAe,EAAE,MAAM,WAAW,CAAC;AAE3D,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,QAAQ,CAAA;IACrB,GAAG,EAAE,UAAU,CAAA;IACf,OAAO,EAAE,SAAS,CAAC,WAAW,CAAA;CAC/B;AAED,wBAAsB,+BAA+B,CAAC,KAAK,EAAE,eAAe,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAsE5H;AAcD,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,UAUlD"}
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateFirstImplementationPlan = generateFirstImplementationPlan;
4
+ exports.extractCodeBlocksAgent = extractCodeBlocksAgent;
5
+ const llm_runner_1 = require("@graf-research/llm-runner");
6
+ const agent_capability_1 = require("./agent-capability");
7
+ const prompt_1 = require("../prompt");
8
+ async function generateFirstImplementationPlan(param, _llm_model) {
9
+ const secret_key = process.env.SK || '';
10
+ const llm_model = _llm_model ?? 'google/gemini-3-flash-preview';
11
+ const llm_endpoint = 'https://openrouter.ai/api/v1';
12
+ const llm = new llm_runner_1.ChatGPTLLM(secret_key, llm_model, undefined, llm_endpoint);
13
+ const session = llm.chat_session_manager.newSession();
14
+ session.list_message.push({
15
+ role: 'user',
16
+ content: [
17
+ `## Your main task`,
18
+ `Implement page "${param.page_name}" by creating first plan to execute the task, follow implementation guides below.`,
19
+ '',
20
+ '## Guide: How Write Plans',
21
+ 'Every plan has structure (typescript syntax) `AgenCap`, response with only single instruction one by one, after user response, give another instruction separately, until "finished" instruction. Remember "finished" instruction is separated instruction.',
22
+ '',
23
+ '```ts',
24
+ "export interface AgentCapListFiles { instruction: 'list-file', description: string }",
25
+ "export interface AgentCapWriteFile { instruction: 'write-file', filename: string, content: string, description: string }",
26
+ "export interface AgentCapReadFile { instruction: 'read-file', filename: string, description: string }",
27
+ "export interface AgentCapRemoveFile { instruction: 'remove-file', filename: string, description: string }",
28
+ "export interface AgentCapFinished { instruction: 'finished' }",
29
+ "export type AgentCap = AgentCapListFiles | AgentCapReadFile | AgentCapRemoveFile | AgentCapWriteFile | AgentCapFinished;",
30
+ '```',
31
+ '',
32
+ '## Guide: How to Write Main Code',
33
+ (0, prompt_1.getImplPrompt)(param),
34
+ '',
35
+ '## Output File/Files Location',
36
+ `Your implementation page and its component (if any components) must be created under folder ${param.target_implementation_folder_path}`,
37
+ `The main page must have no-dependency props and located on file ${param.target_implementation_folder_path}/index.tsx`,
38
+ 'The rest of component can be create with any file name and component name within the same folder.',
39
+ '',
40
+ '## Utility File Guide',
41
+ 'If you are about to use environment variable or constants that have a chance for globally usage you should define the real value by yourself (dont give mockup create real value like "your-example-key") like example jwt secret key. Put the value on utility as a const so it can globally accessbile',
42
+ 'Before write a utility function make sure it isnt redundant check list utility first',
43
+ `Utility file must be created under folder ${agent_capability_1.utility_relative_dir_path}`,
44
+ '',
45
+ '## Example Response',
46
+ '',
47
+ 'Example #1',
48
+ '```json',
49
+ '{',
50
+ ' "instruction": "list-file",',
51
+ ' "description": "show list directory utility and I will decide next instruction based on list dir result"',
52
+ '}',
53
+ '```',
54
+ '',
55
+ 'Example #2',
56
+ '```json',
57
+ '{',
58
+ ' "instruction": "write-file",',
59
+ ` "filename": "${param.target_implementation_folder_path}/index.tsx",`,
60
+ ' "content": "```ts\nimport React, { useState, useEffect } from \'react\';\n\nexport default function ${param.page_name}Page = () => {\n const [somestate, setSomeState] = useState<number>(0);\n ...\n```",',
61
+ ` "description": "create page index.tsx for ..."`,
62
+ '}',
63
+ '```',
64
+ '',
65
+ '## Response Format',
66
+ 'Your response format must be json code-fenced begin with ```json and type structure `AgentCap` like above without free text or any additional text, only json format. Content code inside AgentCap.content must be in typescript format without codefence',
67
+ ].join('\n')
68
+ });
69
+ // console.log(session.list_message[0]?.content);
70
+ // throw new Error(``)
71
+ return {
72
+ instruction: await runLoop(llm, session.id),
73
+ llm,
74
+ session
75
+ };
76
+ }
77
+ async function runLoop(llm, session_id, error_message) {
78
+ const response = await llm.ask(error_message ?? `write plan for task described above!`, session_id);
79
+ try {
80
+ const x = JSON.parse(extractCodeBlocksAgent(response));
81
+ return x;
82
+ }
83
+ catch (err) {
84
+ const error = `There is an error on your plan response JSON format: ${err.toString()}`;
85
+ console.log('ERR1', { error });
86
+ return await runLoop(llm, session_id, error);
87
+ }
88
+ }
89
+ function extractCodeBlocksAgent(text) {
90
+ return text.startsWith('```') ?
91
+ (/```(?:json)\b/i.test(text)
92
+ ? [...text.matchAll(/```(?:json)\s*([\s\S]*?)```/gi)]
93
+ .map(m => m[1])
94
+ .join('\n')
95
+ : '')
96
+ : text;
97
+ }
98
+ //# sourceMappingURL=plan-preparation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-preparation.js","sourceRoot":"","sources":["../../agent-mode/plan-preparation.ts"],"names":[],"mappings":";;AAUA,0EAsEC;AAcD,wDAUC;AAxGD,0DAAkE;AAClE,yDAAyE;AACzE,sCAA2D;AAQpD,KAAK,UAAU,+BAA+B,CAAC,KAAsB,EAAE,UAAmB;IAC/F,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,EAAY,IAAI,EAAE,CAAC;IAClD,MAAM,SAAS,GAAG,UAAU,IAAI,+BAA+B,CAAC;IAChE,MAAM,YAAY,GAAG,8BAA8B,CAAC;IACpD,MAAM,GAAG,GAAe,IAAI,uBAAU,CAAC,UAAU,EAAE,SAAgB,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IAC9F,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AAM,QAAQ,GAAW,MAAM,GAAG,CAAC,GAAG,CAAC,aAAa,IAAI,sCAAsC,EAAE,UAAU,CAAC,CAAC;IAC5G,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAa,CAAC;QACnE,OAAO,CAAC,CAAC;IACX,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,MAAM,KAAK,GAAG,wDAAwD,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC;QACvF,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QAC/B,OAAO,MAAM,OAAO,CAAC,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED,SAAgB,sBAAsB,CAAC,IAAY;IACjD,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAC7B,CACE,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC;YAC3B,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,+BAA+B,CAAC,CAAC;iBAClD,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;iBACd,IAAI,CAAC,IAAI,CAAC;YACb,CAAC,CAAC,EAAE,CACL;QACD,CAAC,CAAC,IAAI,CAAC;AACX,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function cmdChat(_llm_model?: string): Promise<void>;
2
+ //# sourceMappingURL=cmd-chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cmd-chat.d.ts","sourceRoot":"","sources":["../../command/cmd-chat.ts"],"names":[],"mappings":"AAQA,wBAAsB,OAAO,CAAC,UAAU,CAAC,EAAE,MAAM,iBA6DhD"}
@@ -0,0 +1,137 @@
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.cmdChat = cmdChat;
7
+ const enquirer_1 = require("enquirer");
8
+ const listr2_1 = require("listr2");
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const llm_runner_1 = require("@graf-research/llm-runner");
12
+ const process_1 = require("process");
13
+ const swan_dsl_1 = require("@naiv/swan-dsl");
14
+ async function cmdChat(_llm_model) {
15
+ const filename = `web-specification.swan`;
16
+ const file_abs_path = path_1.default.resolve((0, process_1.cwd)(), filename);
17
+ let code = '';
18
+ if (fs_1.default.existsSync(file_abs_path)) {
19
+ code = await fs_1.default.promises.readFile(file_abs_path, 'utf-8');
20
+ console.warn(`File ${file_abs_path} is exist, this file will be modified by your instruction.`);
21
+ }
22
+ const answers = await (0, enquirer_1.prompt)([{
23
+ type: "input",
24
+ name: "prompt",
25
+ message: `Explain briefly your website`,
26
+ }]);
27
+ let success_compiled = false;
28
+ const secret_key = process.env.SK || '';
29
+ const llm_model = _llm_model ?? 'google/gemini-3-flash-preview';
30
+ const llm_endpoint = 'https://openrouter.ai/api/v1';
31
+ const llm = new llm_runner_1.ChatGPTLLM(secret_key, llm_model, undefined, llm_endpoint);
32
+ let attempt = 0;
33
+ let error_message = '';
34
+ let stime = performance.now();
35
+ do {
36
+ await new listr2_1.Listr([{
37
+ title: attempt > 0 ? "Fixing error specification swan DSL" : code ? "Updating existing swan file" : "Generating website specification",
38
+ rendererOptions: {
39
+ persistentOutput: true,
40
+ },
41
+ task: async () => {
42
+ const prompt = await generatePrompt(answers.prompt, code, error_message);
43
+ const response = await llm.askNoContext(prompt);
44
+ code = extractCodeBlocks(response);
45
+ try {
46
+ success_compiled = Boolean((0, swan_dsl_1.parse)(code));
47
+ }
48
+ catch (err) {
49
+ error_message = err?.message || '';
50
+ console.log(error_message);
51
+ }
52
+ }
53
+ }]).run();
54
+ attempt++;
55
+ } while (!success_compiled);
56
+ let ftime = performance.now();
57
+ let elapsed_time = (ftime - stime) / 1000;
58
+ await new listr2_1.Listr([{
59
+ title: "Writing specification file",
60
+ rendererOptions: {
61
+ persistentOutput: true,
62
+ },
63
+ task: async () => {
64
+ await fs_1.default.promises.writeFile(file_abs_path, code);
65
+ }
66
+ }, {
67
+ title: `Successfully created 'web-specification.swan' (${elapsed_time.toFixed(2)}s)`,
68
+ rendererOptions: {
69
+ persistentOutput: true,
70
+ },
71
+ task: async () => {
72
+ await fs_1.default.promises.writeFile(file_abs_path, code);
73
+ }
74
+ }]).run();
75
+ }
76
+ async function generatePrompt(instruction, generated_code, error_message) {
77
+ const swan_instruction = await fs_1.default.promises.readFile(path_1.default.resolve(__dirname, '../../SWAN-DSL.md'), 'utf-8');
78
+ if (error_message && generated_code) {
79
+ return `
80
+ Read this SWAN DSL carefully
81
+
82
+ ${swan_instruction}
83
+
84
+ --
85
+
86
+ An LLM generated this code on the context "${instruction}".
87
+
88
+ \`\`\`swan
89
+ ${generated_code}
90
+ \`\`\`
91
+
92
+ but there is an error: ${error_message}.
93
+
94
+ Your task: fix the error and create swan dsl code in a single file based on the context "${instruction}".
95
+
96
+ please return only swan code with code fence block without any other strings but swan code! your code fence block must starts with "\`\`\`swan" and ends with "\`\`\`"
97
+ `.trim();
98
+ }
99
+ if (generated_code) {
100
+ return `
101
+ Read this SWAN DSL carefully
102
+
103
+ ${swan_instruction}
104
+
105
+ --
106
+
107
+ Your task: modify existing swan dsl code below based on the context "${instruction}".
108
+
109
+ Existing swan design
110
+
111
+ \`\`\`swan
112
+ ${generated_code}
113
+ \`\`\`
114
+
115
+ please return only swan code with code fence block without any other strings but swan code! your code fence block must starts with "\`\`\`swan" and ends with "\`\`\`"
116
+ `.trim();
117
+ }
118
+ return `
119
+ Read this SWAN DSL carefully
120
+
121
+ ${swan_instruction}
122
+
123
+ --
124
+
125
+ Your task: create swan dsl code in a single file based on the context "${instruction}".
126
+
127
+ please return only swan code with code fence block without any other strings but swan code! your code fence block must starts with "\`\`\`swan" and ends with "\`\`\`"
128
+ `.trim();
129
+ }
130
+ function extractCodeBlocks(text) {
131
+ return /```(?:swan\-dsl|swan|dsl)\b/i.test(text)
132
+ ? [...text.matchAll(/```(?:swan\-dsl|swan|dsl)\s*([\s\S]*?)```/gi)]
133
+ .map(m => m[1])
134
+ .join('\n')
135
+ : '';
136
+ }
137
+ //# sourceMappingURL=cmd-chat.js.map