genaicode 0.0.35 → 0.1.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 (229) hide show
  1. package/README.md +3 -0
  2. package/bin/genaicode-dev.js +11 -0
  3. package/bin/genaicode.cjs +8 -1
  4. package/dist/ai-service/ai-studio.d.ts +5 -0
  5. package/dist/ai-service/ai-studio.js +135 -0
  6. package/dist/ai-service/ai-studio.js.map +1 -0
  7. package/dist/ai-service/anthropic.d.ts +5 -0
  8. package/dist/ai-service/anthropic.js +133 -0
  9. package/dist/ai-service/anthropic.js.map +1 -0
  10. package/dist/ai-service/chat-gpt.d.ts +5 -0
  11. package/dist/ai-service/chat-gpt.js +131 -0
  12. package/dist/ai-service/chat-gpt.js.map +1 -0
  13. package/dist/ai-service/common.d.ts +57 -0
  14. package/dist/ai-service/common.js +38 -0
  15. package/dist/ai-service/common.js.map +1 -0
  16. package/dist/ai-service/common.test.d.ts +1 -0
  17. package/dist/ai-service/common.test.js +85 -0
  18. package/dist/ai-service/common.test.js.map +1 -0
  19. package/dist/ai-service/dall-e.d.ts +14 -0
  20. package/dist/ai-service/dall-e.js +47 -0
  21. package/dist/ai-service/dall-e.js.map +1 -0
  22. package/dist/ai-service/vertex-ai-claude.d.ts +5 -0
  23. package/dist/ai-service/vertex-ai-claude.js +93 -0
  24. package/dist/ai-service/vertex-ai-claude.js.map +1 -0
  25. package/dist/ai-service/vertex-ai-imagen.d.ts +14 -0
  26. package/dist/ai-service/vertex-ai-imagen.js +74 -0
  27. package/dist/ai-service/vertex-ai-imagen.js.map +1 -0
  28. package/dist/ai-service/vertex-ai.d.ts +7 -0
  29. package/dist/ai-service/vertex-ai.js +147 -0
  30. package/dist/ai-service/vertex-ai.js.map +1 -0
  31. package/dist/cli/cli-options.d.ts +10 -0
  32. package/dist/cli/cli-options.js +124 -0
  33. package/dist/cli/cli-options.js.map +1 -0
  34. package/dist/cli/cli-options.test.d.ts +1 -0
  35. package/dist/cli/cli-options.test.js +34 -0
  36. package/dist/cli/cli-options.test.js.map +1 -0
  37. package/dist/cli/cli-params.d.ts +29 -0
  38. package/dist/cli/cli-params.js +103 -0
  39. package/dist/cli/cli-params.js.map +1 -0
  40. package/dist/cli/cli-params.test.d.ts +1 -0
  41. package/dist/cli/cli-params.test.js +38 -0
  42. package/dist/cli/cli-params.test.js.map +1 -0
  43. package/dist/cli/service-autodetect.d.ts +2 -0
  44. package/dist/cli/service-autodetect.js +17 -0
  45. package/dist/cli/service-autodetect.js.map +1 -0
  46. package/dist/cli/service-autodetect.test.d.ts +1 -0
  47. package/dist/cli/service-autodetect.test.js +38 -0
  48. package/dist/cli/service-autodetect.test.js.map +1 -0
  49. package/dist/cli/validate-cli-params.d.ts +18 -0
  50. package/dist/cli/validate-cli-params.js +128 -0
  51. package/dist/cli/validate-cli-params.js.map +1 -0
  52. package/dist/cli/validate-cli-params.test.d.ts +1 -0
  53. package/dist/cli/validate-cli-params.test.js +106 -0
  54. package/dist/cli/validate-cli-params.test.js.map +1 -0
  55. package/dist/files/file-utils.d.ts +2 -0
  56. package/dist/files/file-utils.js +7 -0
  57. package/dist/files/file-utils.js.map +1 -0
  58. package/dist/files/file-utils.test.d.ts +1 -0
  59. package/dist/files/file-utils.test.js +18 -0
  60. package/dist/files/file-utils.test.js.map +1 -0
  61. package/dist/files/find-files.d.ts +8 -0
  62. package/dist/files/find-files.js +76 -0
  63. package/dist/files/find-files.js.map +1 -0
  64. package/dist/files/read-files.d.ts +13 -0
  65. package/dist/files/read-files.js +61 -0
  66. package/dist/files/read-files.js.map +1 -0
  67. package/dist/files/read-files.test.d.ts +1 -0
  68. package/dist/files/read-files.test.js +89 -0
  69. package/dist/files/read-files.test.js.map +1 -0
  70. package/dist/files/temp-buffer.d.ts +2 -0
  71. package/dist/files/temp-buffer.js +12 -0
  72. package/dist/files/temp-buffer.js.map +1 -0
  73. package/dist/files/update-files.d.ts +5 -0
  74. package/dist/files/update-files.js +197 -0
  75. package/dist/files/update-files.js.map +1 -0
  76. package/dist/images/ensure-alpha.d.ts +1 -0
  77. package/dist/images/ensure-alpha.js +5 -0
  78. package/dist/images/ensure-alpha.js.map +1 -0
  79. package/dist/images/imgly-remove-background.d.ts +2 -0
  80. package/dist/images/imgly-remove-background.js +21 -0
  81. package/dist/images/imgly-remove-background.js.map +1 -0
  82. package/dist/images/resize-image.d.ts +19 -0
  83. package/dist/images/resize-image.js +22 -0
  84. package/dist/images/resize-image.js.map +1 -0
  85. package/dist/images/split-image.d.ts +18 -0
  86. package/dist/images/split-image.js +25 -0
  87. package/dist/images/split-image.js.map +1 -0
  88. package/dist/main/codegen.d.ts +2 -0
  89. package/dist/main/codegen.js +101 -0
  90. package/dist/main/codegen.js.map +1 -0
  91. package/dist/main/codegen.test.d.ts +2 -0
  92. package/dist/main/codegen.test.js +259 -0
  93. package/dist/main/codegen.test.js.map +1 -0
  94. package/dist/main/config-lib.d.ts +8 -0
  95. package/dist/main/config-lib.js +28 -0
  96. package/dist/main/config-lib.js.map +1 -0
  97. package/dist/main/config-lib.test.d.ts +1 -0
  98. package/dist/main/config-lib.test.js +64 -0
  99. package/dist/main/config-lib.test.js.map +1 -0
  100. package/dist/main/config.d.ts +5 -0
  101. package/{src → dist}/main/config.js +16 -22
  102. package/dist/main/config.js.map +1 -0
  103. package/dist/prompt/function-calling-validate.d.ts +3 -0
  104. package/dist/prompt/function-calling-validate.js +27 -0
  105. package/dist/prompt/function-calling-validate.js.map +1 -0
  106. package/dist/prompt/function-calling.d.ts +5 -0
  107. package/dist/prompt/function-calling.js +48 -0
  108. package/dist/prompt/function-calling.js.map +1 -0
  109. package/dist/prompt/function-defs/ask-question.d.ts +31 -0
  110. package/dist/prompt/function-defs/ask-question.js +32 -0
  111. package/dist/prompt/function-defs/ask-question.js.map +1 -0
  112. package/dist/prompt/function-defs/codegen-summary.d.ts +63 -0
  113. package/dist/prompt/function-defs/codegen-summary.js +80 -0
  114. package/dist/prompt/function-defs/codegen-summary.js.map +1 -0
  115. package/dist/prompt/function-defs/create-directory.d.ts +21 -0
  116. package/dist/prompt/function-defs/create-directory.js +22 -0
  117. package/dist/prompt/function-defs/create-directory.js.map +1 -0
  118. package/dist/prompt/function-defs/create-file.d.ts +25 -0
  119. package/dist/prompt/function-defs/create-file.js +26 -0
  120. package/dist/prompt/function-defs/create-file.js.map +1 -0
  121. package/dist/prompt/function-defs/delete-file.d.ts +21 -0
  122. package/dist/prompt/function-defs/delete-file.js +22 -0
  123. package/dist/prompt/function-defs/delete-file.js.map +1 -0
  124. package/dist/prompt/function-defs/download-file.d.ts +25 -0
  125. package/dist/prompt/function-defs/download-file.js +26 -0
  126. package/dist/prompt/function-defs/download-file.js.map +1 -0
  127. package/dist/prompt/function-defs/explanation.d.ts +17 -0
  128. package/dist/prompt/function-defs/explanation.js +18 -0
  129. package/dist/prompt/function-defs/explanation.js.map +1 -0
  130. package/dist/prompt/function-defs/generate-image.d.ts +48 -0
  131. package/dist/prompt/function-defs/generate-image.js +49 -0
  132. package/dist/prompt/function-defs/generate-image.js.map +1 -0
  133. package/dist/prompt/function-defs/get-image-assets.d.ts +20 -0
  134. package/dist/prompt/function-defs/get-image-assets.js +21 -0
  135. package/dist/prompt/function-defs/get-image-assets.js.map +1 -0
  136. package/dist/prompt/function-defs/get-source-code.d.ts +20 -0
  137. package/dist/prompt/function-defs/get-source-code.js +21 -0
  138. package/dist/prompt/function-defs/get-source-code.js.map +1 -0
  139. package/dist/prompt/function-defs/imgly-remove-background.d.ts +25 -0
  140. package/dist/prompt/function-defs/imgly-remove-background.js +26 -0
  141. package/dist/prompt/function-defs/imgly-remove-background.js.map +1 -0
  142. package/dist/prompt/function-defs/move-file.d.ts +25 -0
  143. package/dist/prompt/function-defs/move-file.js +26 -0
  144. package/dist/prompt/function-defs/move-file.js.map +1 -0
  145. package/dist/prompt/function-defs/patch-file.d.ts +25 -0
  146. package/dist/prompt/function-defs/patch-file.js +39 -0
  147. package/dist/prompt/function-defs/patch-file.js.map +1 -0
  148. package/dist/prompt/function-defs/resize-image.d.ts +36 -0
  149. package/dist/prompt/function-defs/resize-image.js +37 -0
  150. package/dist/prompt/function-defs/resize-image.js.map +1 -0
  151. package/dist/prompt/function-defs/split-image.d.ts +57 -0
  152. package/dist/prompt/function-defs/split-image.js +46 -0
  153. package/dist/prompt/function-defs/split-image.js.map +1 -0
  154. package/dist/prompt/function-defs/update-file.d.ts +25 -0
  155. package/dist/prompt/function-defs/update-file.js +26 -0
  156. package/dist/prompt/function-defs/update-file.js.map +1 -0
  157. package/dist/prompt/limits.d.ts +3 -0
  158. package/dist/prompt/limits.js +19 -0
  159. package/dist/prompt/limits.js.map +1 -0
  160. package/dist/prompt/limits.test.d.ts +1 -0
  161. package/dist/prompt/limits.test.js +35 -0
  162. package/dist/prompt/limits.test.js.map +1 -0
  163. package/dist/prompt/prompt-codegen.d.ts +4 -0
  164. package/dist/prompt/prompt-codegen.js +81 -0
  165. package/dist/prompt/prompt-codegen.js.map +1 -0
  166. package/dist/prompt/prompt-codegen.test.d.ts +1 -0
  167. package/dist/prompt/prompt-codegen.test.js +94 -0
  168. package/dist/prompt/prompt-codegen.test.js.map +1 -0
  169. package/dist/prompt/prompt-consts.d.ts +1 -0
  170. package/{src → dist}/prompt/prompt-consts.js +1 -0
  171. package/dist/prompt/prompt-consts.js.map +1 -0
  172. package/dist/prompt/prompt-service-ask-question.test.d.ts +3 -0
  173. package/dist/prompt/prompt-service-ask-question.test.js +146 -0
  174. package/dist/prompt/prompt-service-ask-question.test.js.map +1 -0
  175. package/dist/prompt/prompt-service.d.ts +13 -0
  176. package/dist/prompt/prompt-service.js +278 -0
  177. package/dist/prompt/prompt-service.js.map +1 -0
  178. package/dist/prompt/prompt-service.test.d.ts +1 -0
  179. package/dist/prompt/prompt-service.test.js +511 -0
  180. package/dist/prompt/prompt-service.test.js.map +1 -0
  181. package/dist/prompt/systemprompt.d.ts +2 -0
  182. package/dist/prompt/systemprompt.js +43 -0
  183. package/dist/prompt/systemprompt.js.map +1 -0
  184. package/dist/prompt/systemprompt.test.d.ts +2 -0
  185. package/dist/prompt/systemprompt.test.js +47 -0
  186. package/dist/prompt/systemprompt.test.js.map +1 -0
  187. package/package.json +15 -5
  188. package/bin/genaicode.js +0 -5
  189. package/src/ai-service/anthropic.js +0 -95
  190. package/src/ai-service/chat-gpt.js +0 -99
  191. package/src/ai-service/common.js +0 -40
  192. package/src/ai-service/common.test.js +0 -107
  193. package/src/ai-service/dall-e.js +0 -50
  194. package/src/ai-service/vertex-ai-claude.js +0 -99
  195. package/src/ai-service/vertex-ai-imagen.js +0 -78
  196. package/src/ai-service/vertex-ai.js +0 -156
  197. package/src/cli/cli-options.js +0 -120
  198. package/src/cli/cli-options.test.js +0 -50
  199. package/src/cli/cli-params.js +0 -99
  200. package/src/cli/cli-params.test.js +0 -43
  201. package/src/cli/service-autodetect.js +0 -11
  202. package/src/cli/service-autodetect.test.js +0 -43
  203. package/src/cli/validate-cli-params.js +0 -117
  204. package/src/cli/validate-cli-params.test.js +0 -134
  205. package/src/files/file-utils.js +0 -7
  206. package/src/files/file-utils.test.js +0 -21
  207. package/src/files/find-files.js +0 -86
  208. package/src/files/read-files.js +0 -60
  209. package/src/files/read-files.test.js +0 -102
  210. package/src/files/temp-buffer.js +0 -13
  211. package/src/files/update-files.js +0 -205
  212. package/src/images/ensure-alpha.js +0 -5
  213. package/src/images/imgly-remove-background.js +0 -23
  214. package/src/images/resize-image.js +0 -23
  215. package/src/images/split-image.js +0 -26
  216. package/src/main/codegen.js +0 -120
  217. package/src/main/codegen.test.js +0 -334
  218. package/src/main/config-lib.js +0 -33
  219. package/src/main/config-lib.test.js +0 -80
  220. package/src/prompt/function-calling-validate.js +0 -25
  221. package/src/prompt/function-calling.js +0 -454
  222. package/src/prompt/limits.js +0 -23
  223. package/src/prompt/limits.test.js +0 -40
  224. package/src/prompt/prompt-codegen.js +0 -106
  225. package/src/prompt/prompt-codegen.test.js +0 -116
  226. package/src/prompt/prompt-service.js +0 -287
  227. package/src/prompt/prompt-service.test.js +0 -601
  228. package/src/prompt/systemprompt.js +0 -32
  229. package/src/prompt/systemprompt.test.js +0 -63
@@ -1,26 +0,0 @@
1
- import sharp from 'sharp';
2
-
3
- /**
4
- * This function opens the input file, and splits its into parts, extracting parts of the input image, and saving to outputFilePaths
5
- *
6
- * @param {string} inputFilePath
7
- * @param {Array<{rect: {x: number, y: number, width: number, height: number}, outputFilePath: string}>} parts
8
- */
9
- export async function splitImage(inputFilePath, parts) {
10
- try {
11
- // Process each part
12
- for (const part of parts) {
13
- const { rect, outputFilePath } = part;
14
- const { x, y, width, height } = rect;
15
-
16
- // Extract the specified rectangle from the input image
17
- console.log('Extracting', rect, outputFilePath);
18
- await sharp(inputFilePath).extract({ left: x, top: y, width, height }).toFile(outputFilePath);
19
- }
20
-
21
- console.log('Image splitting completed successfully.');
22
- } catch (error) {
23
- console.error('Error splitting image:', error);
24
- throw error;
25
- }
26
- }
@@ -1,120 +0,0 @@
1
- import assert from 'node:assert';
2
- import { exec } from 'child_process';
3
- import util from 'util';
4
-
5
- import {
6
- dryRun,
7
- chatGpt,
8
- anthropic,
9
- vertexAi,
10
- vertexAiClaude,
11
- disableInitialLint,
12
- helpRequested,
13
- imagen,
14
- } from '../cli/cli-params.js';
15
- import { validateCliParams } from '../cli/validate-cli-params.js';
16
- import { generateContent as generateContentVertexAi } from '../ai-service/vertex-ai.js';
17
- import { generateContent as generateContentGPT } from '../ai-service/chat-gpt.js';
18
- import { generateContent as generateContentAnthropic } from '../ai-service/anthropic.js';
19
- import { generateContent as generateContentVertexAiClaude } from '../ai-service/vertex-ai-claude.js';
20
- import { generateImage as generateImageDallE } from '../ai-service/dall-e.js';
21
- import { generateImage as generateImageVertexAi } from '../ai-service/vertex-ai-imagen.js';
22
-
23
- import { promptService } from '../prompt/prompt-service.js';
24
- import { updateFiles } from '../files/update-files.js';
25
- import { rcConfig } from '../main/config.js';
26
- import { getLintFixPrompt } from '../prompt/prompt-codegen.js';
27
- import { printHelpMessage } from '../cli/cli-options.js';
28
-
29
- const execPromise = util.promisify(exec);
30
-
31
- /** Executes codegen */
32
- export async function runCodegen() {
33
- // Print to console the received parameters
34
- console.log(`Received parameters: ${process.argv.slice(2).join(' ')}`);
35
-
36
- validateCliParams();
37
-
38
- // Handle --help option
39
- if (helpRequested) {
40
- printHelpMessage();
41
- return;
42
- }
43
-
44
- if (rcConfig.lintCommand && !disableInitialLint) {
45
- try {
46
- console.log(`Executing lint command: ${rcConfig.lintCommand}`);
47
- await execPromise(rcConfig.lintCommand);
48
- console.log('Lint command executed successfully');
49
- } catch (error) {
50
- console.log(
51
- 'Lint command failed. Aborting codegen, please fix lint issues before running codegen, or use --disable-initial-lint',
52
- );
53
- console.log('Lint errors:', error.stdout, error.stderr);
54
- process.exit(1);
55
- }
56
- } else if (rcConfig.lintCommand && disableInitialLint) {
57
- console.log('Initial lint was skipped.');
58
- }
59
-
60
- const generateContent = vertexAiClaude
61
- ? generateContentVertexAiClaude
62
- : vertexAi
63
- ? generateContentVertexAi
64
- : anthropic
65
- ? generateContentAnthropic
66
- : chatGpt
67
- ? generateContentGPT
68
- : assert(false, 'Please specify which AI service should be used');
69
-
70
- const generateImage =
71
- imagen === 'vertex-ai' ? generateImageVertexAi : imagen === 'dall-e' ? generateImageDallE : undefined;
72
-
73
- console.log('Generating response');
74
- let functionCalls = await promptService(generateContent, generateImage);
75
- console.log('Received function calls:', functionCalls);
76
-
77
- if (dryRun) {
78
- console.log('Dry run mode, not updating files');
79
- } else {
80
- console.log('Update files');
81
- await updateFiles(functionCalls.filter((call) => call.name !== 'explanation' && call.name !== 'getSourceCode'));
82
- console.log('Initial updates applied');
83
-
84
- // Check if lintCommand is specified in .genaicoderc
85
- if (rcConfig.lintCommand) {
86
- try {
87
- console.log(`Executing lint command: ${rcConfig.lintCommand}`);
88
- await execPromise(rcConfig.lintCommand);
89
- console.log('Lint command executed successfully');
90
- } catch (error) {
91
- console.log('Lint command failed. Attempting to fix issues...');
92
-
93
- // Prepare the lint error output for the second pass
94
- const lintErrorPrompt = getLintFixPrompt(rcConfig.lintCommand, error.stdout, error.stderr);
95
-
96
- console.log('Generating response for lint fixes');
97
- const lintFixFunctionCalls = await promptService(generateContent, generateImage, lintErrorPrompt);
98
-
99
- console.log('Received function calls for lint fixes:', lintFixFunctionCalls);
100
-
101
- console.log('Applying lint fixes');
102
- updateFiles(
103
- lintFixFunctionCalls.filter((call) => call.name !== 'explanation' && call.name !== 'getSourceCode'),
104
- );
105
-
106
- // Run lint command again to verify fixes
107
- try {
108
- console.log(`Re-running lint command: ${rcConfig.lintCommand}`);
109
- await execPromise(rcConfig.lintCommand);
110
- console.log('Lint command executed successfully after fixes');
111
- } catch (secondLintError) {
112
- console.log('Lint command still failing after fixes. Manual intervention may be required.');
113
- console.log('Lint errors:', secondLintError.stdout, secondLintError.stderr);
114
- }
115
- }
116
- }
117
-
118
- console.log('Done!');
119
- }
120
- }
@@ -1,334 +0,0 @@
1
- /*eslint-disable no-import-assign*/
2
-
3
- import { describe, it, expect, beforeEach, vi } from 'vitest';
4
- import { runCodegen } from './codegen.js';
5
- import * as vertexAi from '../ai-service/vertex-ai.js';
6
- import * as chatGpt from '../ai-service/chat-gpt.js';
7
- import * as anthropic from '../ai-service/anthropic.js';
8
- import * as vertexAiClaude from '../ai-service/vertex-ai-claude.js';
9
- import * as updateFiles from '../files/update-files.js';
10
- import '../files/find-files.js';
11
- import * as cliParams from '../cli/cli-params.js';
12
- import * as cliOptions from '../cli/cli-options.js';
13
- import * as vertexAiImagen from '../ai-service/vertex-ai-imagen.js';
14
- import * as dallE from '../ai-service/dall-e.js';
15
- import './config.js';
16
-
17
- vi.mock('../ai-service/vertex-ai.js', () => ({ generateContent: vi.fn() }));
18
- vi.mock('../ai-service/chat-gpt.js', () => ({ generateContent: vi.fn() }));
19
- vi.mock('../ai-service/anthropic.js', () => ({ generateContent: vi.fn() }));
20
- vi.mock('../ai-service/vertex-ai-claude.js', () => ({ generateContent: vi.fn() }));
21
- vi.mock('../files/update-files.js');
22
- vi.mock('../cli/cli-params.js', () => ({
23
- requireExplanations: false,
24
- considerAllFiles: false,
25
- dependencyTree: false,
26
- explicitPrompt: false,
27
- allowFileCreate: false,
28
- allowFileDelete: false,
29
- allowDirectoryCreate: false,
30
- allowFileMove: false,
31
- verbosePrompt: false,
32
- vertexAiClaude: false,
33
- helpRequested: false,
34
- vision: false,
35
- imagen: false,
36
- temperature: 0.7,
37
- cheap: false,
38
- taskFile: undefined,
39
- disableInitialLint: undefined,
40
- }));
41
- vi.mock('../files/find-files.js', () => ({
42
- getSourceFiles: () => [],
43
- getImageAssetFiles: () => [],
44
- }));
45
- vi.mock('../cli/cli-options.js', () => ({
46
- printHelpMessage: vi.fn(),
47
- }));
48
- vi.mock('../ai-service/vertex-ai-imagen.js', () => ({ generateImage: vi.fn() }));
49
- vi.mock('../ai-service/dall-e.js', () => ({ generateImage: vi.fn() }));
50
- vi.mock('./config.js', () => ({
51
- rootDir: '/mocked/root/dir',
52
- rcConfig: {
53
- rootDir: '.',
54
- extensions: ['.js', '.ts', '.tsx', '.jsx'],
55
- },
56
- }));
57
-
58
- describe('runCodegen', () => {
59
- beforeEach(() => {
60
- vi.resetAllMocks();
61
- cliParams.anthropic = false;
62
- cliParams.chatGpt = false;
63
- cliParams.vertexAi = false;
64
- cliParams.vertexAiClaude = false;
65
- cliParams.dryRun = false;
66
- cliParams.helpRequested = false;
67
- cliParams.vision = false;
68
- cliParams.imagen = false;
69
- });
70
-
71
- it('should run codegen with Vertex AI by default', async () => {
72
- cliParams.vertexAi = true;
73
-
74
- const mockFunctionCalls = [
75
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Hello");' } },
76
- ];
77
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
78
-
79
- await runCodegen();
80
-
81
- expect(vertexAi.generateContent).toHaveBeenCalled();
82
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
83
- });
84
-
85
- it('should run codegen with ChatGPT when chatGpt flag is true', async () => {
86
- cliParams.chatGpt = true;
87
-
88
- const mockFunctionCalls = [{ name: 'createFile', args: { filePath: 'new.js', newContent: 'const x = 5;' } }];
89
- chatGpt.generateContent.mockResolvedValueOnce(mockFunctionCalls);
90
-
91
- await runCodegen();
92
-
93
- expect(chatGpt.generateContent).toHaveBeenCalled();
94
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
95
- });
96
-
97
- it('should run codegen with Anthropic when anthropic flag is true', async () => {
98
- cliParams.anthropic = true;
99
-
100
- const mockFunctionCalls = [{ name: 'deleteFile', args: { filePath: 'obsolete.js' } }];
101
- anthropic.generateContent.mockResolvedValueOnce(mockFunctionCalls);
102
-
103
- await runCodegen();
104
-
105
- expect(anthropic.generateContent).toHaveBeenCalled();
106
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
107
- });
108
-
109
- it('should not update files in dry run mode', async () => {
110
- cliParams.vertexAi = true;
111
- cliParams.dryRun = true;
112
-
113
- const mockFunctionCalls = [
114
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Dry run");' } },
115
- ];
116
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
117
-
118
- await runCodegen();
119
-
120
- expect(vertexAi.generateContent).toHaveBeenCalled();
121
- expect(updateFiles.updateFiles).not.toHaveBeenCalled();
122
- });
123
-
124
- it('should run codegen with Vertex AI Claude when vertexAiClaude flag is true', async () => {
125
- cliParams.vertexAiClaude = true;
126
-
127
- const mockFunctionCalls = [
128
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Hello from Claude");' } },
129
- ];
130
- vertexAiClaude.generateContent.mockResolvedValueOnce(mockFunctionCalls);
131
-
132
- await runCodegen();
133
-
134
- expect(vertexAiClaude.generateContent).toHaveBeenCalled();
135
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
136
- });
137
-
138
- it('should pass the temperature parameter to the AI service', async () => {
139
- cliParams.vertexAi = true;
140
- cliParams.temperature = 0.5;
141
-
142
- const mockFunctionCalls = [
143
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Temperature test");' } },
144
- ];
145
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
146
-
147
- await runCodegen();
148
-
149
- expect(vertexAi.generateContent).toHaveBeenCalledWith(
150
- expect.anything(),
151
- expect.anything(),
152
- expect.anything(),
153
- 0.5,
154
- false,
155
- );
156
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
157
- });
158
-
159
- it('should print help message and not run codegen when --help option is provided', async () => {
160
- cliParams.helpRequested = true;
161
-
162
- await runCodegen();
163
-
164
- expect(cliOptions.printHelpMessage).toHaveBeenCalled();
165
- expect(vertexAi.generateContent).not.toHaveBeenCalled();
166
- expect(chatGpt.generateContent).not.toHaveBeenCalled();
167
- expect(anthropic.generateContent).not.toHaveBeenCalled();
168
- expect(vertexAiClaude.generateContent).not.toHaveBeenCalled();
169
- expect(updateFiles.updateFiles).not.toHaveBeenCalled();
170
- });
171
-
172
- it('should run codegen with vision when vision flag is true', async () => {
173
- cliParams.chatGpt = true;
174
- cliParams.vision = true;
175
-
176
- const mockFunctionCalls = [
177
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Vision test");' } },
178
- ];
179
- chatGpt.generateContent.mockResolvedValueOnce(mockFunctionCalls);
180
-
181
- await runCodegen();
182
-
183
- expect(chatGpt.generateContent).toHaveBeenCalled();
184
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
185
- // Check if the generateContent function was called with the correct parameters
186
- expect(chatGpt.generateContent.mock.calls[0][0]).toEqual(
187
- expect.arrayContaining([
188
- expect.objectContaining({
189
- type: 'user',
190
- text: expect.stringContaining('I should also provide you with a summary of application image assets'),
191
- }),
192
- expect.objectContaining({
193
- type: 'assistant',
194
- text: expect.stringContaining('Please provide summary of application image assets.'),
195
- }),
196
- expect.objectContaining({
197
- type: 'user',
198
- functionResponses: [{ name: 'getImageAssets', content: expect.any(String) }],
199
- }),
200
- ]),
201
- );
202
- });
203
-
204
- it('should use Vertex AI Imagen when imagen flag is set to vertex-ai', async () => {
205
- cliParams.imagen = 'vertex-ai';
206
- cliParams.vertexAi = true;
207
-
208
- const mockCodegenSummary = [
209
- {
210
- name: 'codegenSummary',
211
- args: {
212
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
213
- contextPaths: [],
214
- explanation: 'Mock summary with image generation failure',
215
- },
216
- },
217
- ];
218
- const mockFunctionCalls = [
219
- { name: 'generateImage', args: { prompt: 'A beautiful landscape', filePath: 'landscape.png', size: '512x512' } },
220
- ];
221
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
222
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
223
- vertexAiImagen.generateImage.mockResolvedValueOnce('mocked-image-data');
224
-
225
- await runCodegen();
226
-
227
- expect(vertexAi.generateContent).toHaveBeenCalled();
228
- expect(vertexAiImagen.generateImage).toHaveBeenCalledWith('A beautiful landscape', undefined, '512x512', false);
229
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
230
- });
231
-
232
- it('should use DALL-E when imagen flag is set to dall-e', async () => {
233
- cliParams.imagen = 'dall-e';
234
- cliParams.chatGpt = true;
235
-
236
- const mockCodegenSummary = [
237
- {
238
- name: 'codegenSummary',
239
- args: {
240
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
241
- contextPaths: [],
242
- explanation: 'Mock summary with image generation failure',
243
- },
244
- },
245
- ];
246
- const mockFunctionCalls = [
247
- { name: 'generateImage', args: { prompt: 'A futuristic city', filePath: 'city.png', size: '1024x1024' } },
248
- ];
249
- chatGpt.generateContent.mockResolvedValueOnce(mockCodegenSummary);
250
- chatGpt.generateContent.mockResolvedValueOnce(mockFunctionCalls);
251
- dallE.generateImage.mockResolvedValueOnce('mocked-image-data');
252
-
253
- await runCodegen();
254
-
255
- expect(chatGpt.generateContent).toHaveBeenCalled();
256
- expect(dallE.generateImage).toHaveBeenCalledWith('A futuristic city', undefined, '1024x1024', false);
257
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
258
- });
259
-
260
- it('should throw an error when imagen flag is set but no AI service is specified', async () => {
261
- cliParams.imagen = 'vertex-ai';
262
-
263
- await expect(runCodegen()).rejects.toThrow('Please specify which AI service should be used');
264
- });
265
-
266
- it('should pass the cheap parameter to the AI service when --cheap flag is true', async () => {
267
- cliParams.vertexAi = true;
268
- cliParams.cheap = true;
269
-
270
- const mockFunctionCalls = [
271
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Cheap test");' } },
272
- ];
273
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
274
-
275
- await runCodegen();
276
-
277
- expect(vertexAi.generateContent).toHaveBeenCalledWith(
278
- expect.anything(),
279
- expect.anything(),
280
- expect.anything(),
281
- expect.anything(),
282
- true,
283
- );
284
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
285
- });
286
-
287
- it('should pass the cheap parameter to the image generation service when --cheap flag is true', async () => {
288
- cliParams.imagen = 'vertex-ai';
289
- cliParams.vertexAi = true;
290
- cliParams.cheap = true;
291
-
292
- const mockCodegenSummary = [
293
- {
294
- name: 'codegenSummary',
295
- args: {
296
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
297
- contextPaths: [],
298
- explanation: 'Mock summary with cheap image generation',
299
- },
300
- },
301
- ];
302
- const mockFunctionCalls = [
303
- {
304
- name: 'generateImage',
305
- args: {
306
- prompt: 'A simple landscape',
307
- filePath: 'landscape.png',
308
- size: { width: 256, height: 256 },
309
- cheap: true,
310
- },
311
- },
312
- ];
313
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
314
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
315
- vertexAiImagen.generateImage.mockResolvedValueOnce('mocked-cheap-image-data');
316
-
317
- await runCodegen();
318
-
319
- expect(vertexAi.generateContent).toHaveBeenCalledWith(
320
- expect.anything(),
321
- expect.anything(),
322
- expect.anything(),
323
- expect.anything(),
324
- true,
325
- );
326
- expect(vertexAiImagen.generateImage).toHaveBeenCalledWith(
327
- 'A simple landscape',
328
- undefined,
329
- { width: 256, height: 256 },
330
- true,
331
- );
332
- expect(updateFiles.updateFiles).toHaveBeenCalledWith(mockFunctionCalls);
333
- });
334
- });
@@ -1,33 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import assert from 'node:assert';
4
-
5
- import { isAncestorDirectory } from '../files/file-utils.js';
6
-
7
- // This file contains project codegen configuration
8
- const CODEGENRC_FILENAME = '.genaicoderc';
9
-
10
- // Find .genaicoderc file
11
- export function findRcFile() {
12
- let rcFilePath = process.cwd();
13
- while (!fs.existsSync(path.join(rcFilePath, CODEGENRC_FILENAME))) {
14
- const parentDir = path.dirname(rcFilePath);
15
- if (parentDir === rcFilePath) {
16
- throw new Error(`${CODEGENRC_FILENAME} not found in any parent directory`);
17
- }
18
- rcFilePath = parentDir;
19
- }
20
- return path.join(rcFilePath, CODEGENRC_FILENAME);
21
- }
22
-
23
- // Read and parse .genaicoderc file
24
- export function parseRcFile(rcFilePath) {
25
- assert(fs.existsSync(rcFilePath), `${CODEGENRC_FILENAME} not found`);
26
- const rcConfig = JSON.parse(fs.readFileSync(rcFilePath, 'utf-8'));
27
- assert(rcConfig.rootDir, 'Root dir not configured');
28
-
29
- const rootDir = path.resolve(path.dirname(rcFilePath), rcConfig.rootDir);
30
- assert(isAncestorDirectory(path.dirname(rcFilePath), rootDir), 'Root dir is not located inside project directory');
31
-
32
- return { ...rcConfig, rootDir };
33
- }
@@ -1,80 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { isAncestorDirectory } from '../files/file-utils.js';
5
- import { findRcFile, parseRcFile } from './config-lib';
6
-
7
- vi.mock('fs');
8
- vi.mock('path');
9
- vi.mock('../files/file-utils.js');
10
-
11
- const CODEGENRC_FILENAME = '.genaicoderc';
12
-
13
- // Mock data
14
- const mockRcContent = JSON.stringify({ rootDir: 'src' });
15
- const mockRootDir = '/project/src';
16
- const mockRcFilePath = `/project/${CODEGENRC_FILENAME}`;
17
-
18
- // Helper function to mock fs.existsSync
19
- function mockExistsSync(paths) {
20
- return (p) => paths.includes(p);
21
- }
22
-
23
- // Tests
24
-
25
- // Test for findRcFile
26
- describe('findRcFile', () => {
27
- it('should find .genaicoderc in the current or parent directories', () => {
28
- fs.existsSync.mockImplementation(mockExistsSync([mockRcFilePath]));
29
- path.dirname.mockImplementation((p) => (p === '/project' ? '/' : '/project'));
30
- path.join.mockImplementation((...args) => args.join('/'));
31
-
32
- const result = findRcFile();
33
- expect(result).toBe(mockRcFilePath);
34
- });
35
-
36
- it('should throw an error if .genaicoderc is not found', () => {
37
- fs.existsSync.mockReturnValue(false);
38
- path.dirname.mockImplementation((p) => (p === '/' ? '/' : '/project'));
39
-
40
- expect(() => findRcFile()).toThrowError(`${CODEGENRC_FILENAME} not found in any parent directory`);
41
- });
42
- });
43
-
44
- // Test for parseRcFile
45
- describe('parseRcFile', () => {
46
- it('should parse .genaicoderc and return config with rootDir', () => {
47
- fs.existsSync.mockReturnValue(true);
48
- fs.readFileSync.mockReturnValue(mockRcContent);
49
- path.resolve.mockImplementation((...args) => args.join('/'));
50
- path.dirname.mockReturnValue('/project');
51
- isAncestorDirectory.mockReturnValue(true);
52
-
53
- const result = parseRcFile(mockRcFilePath);
54
- expect(result).toEqual({ ...JSON.parse(mockRcContent), rootDir: mockRootDir });
55
- });
56
-
57
- it('should throw an error if .genaicoderc is missing', () => {
58
- fs.existsSync.mockReturnValue(false);
59
-
60
- expect(() => parseRcFile(mockRcFilePath)).toThrowError(`${CODEGENRC_FILENAME} not found`);
61
- });
62
-
63
- it('should throw an error if rootDir is not configured', () => {
64
- const invalidContent = JSON.stringify({});
65
- fs.existsSync.mockReturnValue(true);
66
- fs.readFileSync.mockReturnValue(invalidContent);
67
-
68
- expect(() => parseRcFile(mockRcFilePath)).toThrowError('Root dir not configured');
69
- });
70
-
71
- it('should throw an error if rootDir is not located inside project directory', () => {
72
- fs.existsSync.mockReturnValue(true);
73
- fs.readFileSync.mockReturnValue(mockRcContent);
74
- path.resolve.mockImplementation((...args) => args.join('/'));
75
- path.dirname.mockReturnValue('/project');
76
- isAncestorDirectory.mockReturnValue(false);
77
-
78
- expect(() => parseRcFile(mockRcFilePath)).toThrowError('Root dir is not located inside project directory');
79
- });
80
- });
@@ -1,25 +0,0 @@
1
- import { Validator } from 'jsonschema';
2
- import { functionDefs } from './function-calling.js';
3
-
4
- export function validateFunctionCall(call) {
5
- const validator = new Validator();
6
- const functionDef = functionDefs.find((def) => def.name === call.name);
7
-
8
- if (!functionDef) {
9
- return {
10
- errors: [
11
- {
12
- message: `Function "${call.name}" is not defined.`,
13
- },
14
- ],
15
- };
16
- }
17
-
18
- const validationResult = validator.validate(call.args, functionDef.parameters);
19
-
20
- if (validationResult.errors.length > 0) {
21
- return validationResult;
22
- }
23
-
24
- return undefined;
25
- }