genaicode 0.0.37 → 0.2.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 (312) hide show
  1. package/README.md +3 -1
  2. package/bin/genaicode-dev.js +11 -0
  3. package/bin/genaicode.cjs +15 -1
  4. package/dist/ai-service/ai-studio.d.ts +6 -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 +6 -0
  8. package/dist/ai-service/anthropic.js +135 -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 +132 -0
  12. package/dist/ai-service/chat-gpt.js.map +1 -0
  13. package/dist/ai-service/common.d.ts +58 -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 +48 -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 +94 -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 +77 -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 +142 -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 +30 -0
  38. package/dist/cli/cli-params.js +109 -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 +129 -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 +18 -0
  65. package/dist/files/read-files.js +66 -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 +88 -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 +6 -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-interactive-utils.d.ts +8 -0
  89. package/dist/main/codegen-interactive-utils.js +148 -0
  90. package/dist/main/codegen-interactive-utils.js.map +1 -0
  91. package/dist/main/codegen-interactive.d.ts +2 -0
  92. package/dist/main/codegen-interactive.js +68 -0
  93. package/dist/main/codegen-interactive.js.map +1 -0
  94. package/dist/main/codegen-types.d.ts +28 -0
  95. package/dist/main/codegen-types.js +2 -0
  96. package/dist/main/codegen-types.js.map +1 -0
  97. package/dist/main/codegen.d.ts +4 -0
  98. package/dist/main/codegen.js +164 -0
  99. package/dist/main/codegen.js.map +1 -0
  100. package/dist/main/codegen.test.d.ts +2 -0
  101. package/dist/main/codegen.test.js +273 -0
  102. package/dist/main/codegen.test.js.map +1 -0
  103. package/dist/main/config-lib.d.ts +16 -0
  104. package/dist/main/config-lib.js +28 -0
  105. package/dist/main/config-lib.js.map +1 -0
  106. package/dist/main/config-lib.test.d.ts +1 -0
  107. package/dist/main/config-lib.test.js +64 -0
  108. package/dist/main/config-lib.test.js.map +1 -0
  109. package/dist/main/config.d.ts +6 -0
  110. package/{src → dist}/main/config.js +28 -22
  111. package/dist/main/config.js.map +1 -0
  112. package/dist/main/interactive/codegen-interactive.d.ts +2 -0
  113. package/dist/main/interactive/codegen-interactive.js +55 -0
  114. package/dist/main/interactive/codegen-interactive.js.map +1 -0
  115. package/dist/main/interactive/codegen-interactive.test.d.ts +4 -0
  116. package/dist/main/interactive/codegen-interactive.test.js +77 -0
  117. package/dist/main/interactive/codegen-interactive.test.js.map +1 -0
  118. package/dist/main/interactive/codegen-worker.d.ts +3 -0
  119. package/dist/main/interactive/codegen-worker.js +47 -0
  120. package/dist/main/interactive/codegen-worker.js.map +1 -0
  121. package/dist/main/interactive/common.d.ts +5 -0
  122. package/dist/main/interactive/common.js +51 -0
  123. package/dist/main/interactive/common.js.map +1 -0
  124. package/dist/main/interactive/configure.d.ts +2 -0
  125. package/dist/main/interactive/configure.js +99 -0
  126. package/dist/main/interactive/configure.js.map +1 -0
  127. package/dist/main/interactive/error-handling.d.ts +4 -0
  128. package/dist/main/interactive/error-handling.js +49 -0
  129. package/dist/main/interactive/error-handling.js.map +1 -0
  130. package/dist/main/interactive/help.d.ts +1 -0
  131. package/dist/main/interactive/help.js +22 -0
  132. package/dist/main/interactive/help.js.map +1 -0
  133. package/dist/main/interactive/process-comments.d.ts +2 -0
  134. package/dist/main/interactive/process-comments.js +39 -0
  135. package/dist/main/interactive/process-comments.js.map +1 -0
  136. package/dist/main/interactive/select-ai-service.d.ts +2 -0
  137. package/dist/main/interactive/select-ai-service.js +27 -0
  138. package/dist/main/interactive/select-ai-service.js.map +1 -0
  139. package/dist/main/interactive/task-file.d.ts +2 -0
  140. package/dist/main/interactive/task-file.js +28 -0
  141. package/dist/main/interactive/task-file.js.map +1 -0
  142. package/dist/main/interactive/text-prompt.d.ts +2 -0
  143. package/dist/main/interactive/text-prompt.js +18 -0
  144. package/dist/main/interactive/text-prompt.js.map +1 -0
  145. package/dist/main/interactive/user-interrupt.d.ts +1 -0
  146. package/dist/main/interactive/user-interrupt.js +36 -0
  147. package/dist/main/interactive/user-interrupt.js.map +1 -0
  148. package/dist/main/interactive/worker-script.d.ts +1 -0
  149. package/dist/main/interactive/worker-script.js +24 -0
  150. package/dist/main/interactive/worker-script.js.map +1 -0
  151. package/dist/main/interactive/worker.d.ts +2 -0
  152. package/dist/main/interactive/worker.js +55 -0
  153. package/dist/main/interactive/worker.js.map +1 -0
  154. package/dist/prompt/function-calling-validate.d.ts +3 -0
  155. package/dist/prompt/function-calling-validate.js +33 -0
  156. package/dist/prompt/function-calling-validate.js.map +1 -0
  157. package/dist/prompt/function-calling.d.ts +5 -0
  158. package/{src → dist}/prompt/function-calling.js +25 -24
  159. package/dist/prompt/function-calling.js.map +1 -0
  160. package/dist/prompt/function-defs/ask-question.d.ts +65 -0
  161. package/dist/prompt/function-defs/ask-question.js +67 -0
  162. package/dist/prompt/function-defs/ask-question.js.map +1 -0
  163. package/dist/prompt/function-defs/codegen-summary.d.ts +63 -0
  164. package/dist/prompt/function-defs/codegen-summary.js +80 -0
  165. package/dist/prompt/function-defs/codegen-summary.js.map +1 -0
  166. package/dist/prompt/function-defs/create-directory.d.ts +21 -0
  167. package/dist/prompt/function-defs/create-directory.js +22 -0
  168. package/dist/prompt/function-defs/create-directory.js.map +1 -0
  169. package/dist/prompt/function-defs/create-file.d.ts +25 -0
  170. package/dist/prompt/function-defs/create-file.js +26 -0
  171. package/dist/prompt/function-defs/create-file.js.map +1 -0
  172. package/dist/prompt/function-defs/delete-file.d.ts +21 -0
  173. package/dist/prompt/function-defs/delete-file.js +22 -0
  174. package/dist/prompt/function-defs/delete-file.js.map +1 -0
  175. package/dist/prompt/function-defs/download-file.d.ts +25 -0
  176. package/dist/prompt/function-defs/download-file.js +26 -0
  177. package/dist/prompt/function-defs/download-file.js.map +1 -0
  178. package/dist/prompt/function-defs/explanation.d.ts +17 -0
  179. package/dist/prompt/function-defs/explanation.js +18 -0
  180. package/dist/prompt/function-defs/explanation.js.map +1 -0
  181. package/dist/prompt/function-defs/generate-image.d.ts +41 -0
  182. package/dist/prompt/function-defs/generate-image.js +42 -0
  183. package/dist/prompt/function-defs/generate-image.js.map +1 -0
  184. package/dist/prompt/function-defs/get-image-assets.d.ts +20 -0
  185. package/dist/prompt/function-defs/get-image-assets.js +21 -0
  186. package/dist/prompt/function-defs/get-image-assets.js.map +1 -0
  187. package/dist/prompt/function-defs/get-source-code.d.ts +20 -0
  188. package/dist/prompt/function-defs/get-source-code.js +21 -0
  189. package/dist/prompt/function-defs/get-source-code.js.map +1 -0
  190. package/dist/prompt/function-defs/imgly-remove-background.d.ts +25 -0
  191. package/dist/prompt/function-defs/imgly-remove-background.js +26 -0
  192. package/dist/prompt/function-defs/imgly-remove-background.js.map +1 -0
  193. package/dist/prompt/function-defs/move-file.d.ts +25 -0
  194. package/dist/prompt/function-defs/move-file.js +26 -0
  195. package/dist/prompt/function-defs/move-file.js.map +1 -0
  196. package/dist/prompt/function-defs/patch-file.d.ts +25 -0
  197. package/dist/prompt/function-defs/patch-file.js +39 -0
  198. package/dist/prompt/function-defs/patch-file.js.map +1 -0
  199. package/dist/prompt/function-defs/resize-image.d.ts +36 -0
  200. package/dist/prompt/function-defs/resize-image.js +37 -0
  201. package/dist/prompt/function-defs/resize-image.js.map +1 -0
  202. package/dist/prompt/function-defs/split-image.d.ts +57 -0
  203. package/dist/prompt/function-defs/split-image.js +46 -0
  204. package/dist/prompt/function-defs/split-image.js.map +1 -0
  205. package/dist/prompt/function-defs/update-file.d.ts +25 -0
  206. package/dist/prompt/function-defs/update-file.js +26 -0
  207. package/dist/prompt/function-defs/update-file.js.map +1 -0
  208. package/dist/prompt/limits.d.ts +3 -0
  209. package/dist/prompt/limits.js +19 -0
  210. package/dist/prompt/limits.js.map +1 -0
  211. package/dist/prompt/limits.test.d.ts +1 -0
  212. package/dist/prompt/limits.test.js +35 -0
  213. package/dist/prompt/limits.test.js.map +1 -0
  214. package/dist/prompt/prompt-codegen.d.ts +9 -0
  215. package/dist/prompt/prompt-codegen.js +90 -0
  216. package/dist/prompt/prompt-codegen.js.map +1 -0
  217. package/dist/prompt/prompt-codegen.test.d.ts +1 -0
  218. package/dist/prompt/prompt-codegen.test.js +98 -0
  219. package/dist/prompt/prompt-codegen.test.js.map +1 -0
  220. package/dist/prompt/prompt-consts.d.ts +1 -0
  221. package/{src → dist}/prompt/prompt-consts.js +1 -0
  222. package/dist/prompt/prompt-consts.js.map +1 -0
  223. package/dist/prompt/prompt-service-ask-question.test.d.ts +3 -0
  224. package/dist/prompt/prompt-service-ask-question.test.js +142 -0
  225. package/dist/prompt/prompt-service-ask-question.test.js.map +1 -0
  226. package/dist/prompt/prompt-service.d.ts +28 -0
  227. package/dist/prompt/prompt-service.js +163 -0
  228. package/dist/prompt/prompt-service.js.map +1 -0
  229. package/dist/prompt/prompt-service.test.d.ts +1 -0
  230. package/dist/prompt/prompt-service.test.js +531 -0
  231. package/dist/prompt/prompt-service.test.js.map +1 -0
  232. package/dist/prompt/steps/step-ask-question.d.ts +6 -0
  233. package/dist/prompt/steps/step-ask-question.js +82 -0
  234. package/dist/prompt/steps/step-ask-question.js.map +1 -0
  235. package/dist/prompt/steps/step-generate-image.d.ts +2 -0
  236. package/dist/prompt/steps/step-generate-image.js +29 -0
  237. package/dist/prompt/steps/step-generate-image.js.map +1 -0
  238. package/dist/prompt/steps/step-validate-recover.d.ts +13 -0
  239. package/dist/prompt/steps/step-validate-recover.js +51 -0
  240. package/dist/prompt/steps/step-validate-recover.js.map +1 -0
  241. package/dist/prompt/steps/step-verify-patch.d.ts +7 -0
  242. package/dist/prompt/steps/step-verify-patch.js +41 -0
  243. package/dist/prompt/steps/step-verify-patch.js.map +1 -0
  244. package/dist/prompt/steps/steps-types.d.ts +4 -0
  245. package/dist/prompt/steps/steps-types.js +6 -0
  246. package/dist/prompt/steps/steps-types.js.map +1 -0
  247. package/dist/prompt/systemprompt.d.ts +3 -0
  248. package/dist/prompt/systemprompt.js +47 -0
  249. package/dist/prompt/systemprompt.js.map +1 -0
  250. package/dist/prompt/systemprompt.test.d.ts +3 -0
  251. package/dist/prompt/systemprompt.test.js +46 -0
  252. package/dist/prompt/systemprompt.test.js.map +1 -0
  253. package/package.json +15 -5
  254. package/bin/genaicode.js +0 -5
  255. package/bin/vertex-monkey-patch.cjs +0 -33
  256. package/src/ai-service/anthropic.js +0 -126
  257. package/src/ai-service/chat-gpt.js +0 -126
  258. package/src/ai-service/common.js +0 -49
  259. package/src/ai-service/common.test.js +0 -107
  260. package/src/ai-service/dall-e.js +0 -50
  261. package/src/ai-service/vertex-ai-claude.js +0 -99
  262. package/src/ai-service/vertex-ai-imagen.js +0 -78
  263. package/src/ai-service/vertex-ai.js +0 -156
  264. package/src/cli/cli-options.js +0 -132
  265. package/src/cli/cli-options.test.js +0 -50
  266. package/src/cli/cli-params.js +0 -114
  267. package/src/cli/cli-params.test.js +0 -43
  268. package/src/cli/service-autodetect.js +0 -11
  269. package/src/cli/service-autodetect.test.js +0 -43
  270. package/src/cli/validate-cli-params.js +0 -137
  271. package/src/cli/validate-cli-params.test.js +0 -134
  272. package/src/files/file-utils.js +0 -7
  273. package/src/files/file-utils.test.js +0 -21
  274. package/src/files/find-files.js +0 -86
  275. package/src/files/read-files.js +0 -65
  276. package/src/files/read-files.test.js +0 -103
  277. package/src/files/temp-buffer.js +0 -13
  278. package/src/files/update-files.js +0 -205
  279. package/src/images/ensure-alpha.js +0 -5
  280. package/src/images/imgly-remove-background.js +0 -23
  281. package/src/images/resize-image.js +0 -23
  282. package/src/images/split-image.js +0 -26
  283. package/src/main/codegen.js +0 -120
  284. package/src/main/codegen.test.js +0 -335
  285. package/src/main/config-lib.js +0 -33
  286. package/src/main/config-lib.test.js +0 -80
  287. package/src/prompt/function-calling-validate.js +0 -25
  288. package/src/prompt/function-defs/ask-question.js +0 -33
  289. package/src/prompt/function-defs/codegen-summary.js +0 -87
  290. package/src/prompt/function-defs/create-directory.js +0 -21
  291. package/src/prompt/function-defs/create-file.js +0 -26
  292. package/src/prompt/function-defs/delete-file.js +0 -21
  293. package/src/prompt/function-defs/download-file.js +0 -25
  294. package/src/prompt/function-defs/explanation.js +0 -17
  295. package/src/prompt/function-defs/generate-image.js +0 -51
  296. package/src/prompt/function-defs/get-image-assets.js +0 -21
  297. package/src/prompt/function-defs/get-source-code.js +0 -21
  298. package/src/prompt/function-defs/imgly-remove-background.js +0 -25
  299. package/src/prompt/function-defs/move-file.js +0 -25
  300. package/src/prompt/function-defs/patch-file.js +0 -39
  301. package/src/prompt/function-defs/resize-image.js +0 -36
  302. package/src/prompt/function-defs/split-image.js +0 -45
  303. package/src/prompt/function-defs/update-file.js +0 -26
  304. package/src/prompt/limits.js +0 -23
  305. package/src/prompt/limits.test.js +0 -40
  306. package/src/prompt/prompt-codegen.js +0 -106
  307. package/src/prompt/prompt-codegen.test.js +0 -116
  308. package/src/prompt/prompt-service-ask-question.test.js +0 -163
  309. package/src/prompt/prompt-service.js +0 -335
  310. package/src/prompt/prompt-service.test.js +0 -602
  311. package/src/prompt/systemprompt.js +0 -39
  312. package/src/prompt/systemprompt.test.js +0 -64
@@ -1,116 +0,0 @@
1
- /*eslint-disable no-import-assign*/
2
-
3
- import { describe, it, expect, vi, beforeEach } from 'vitest';
4
- import { getCodeGenPrompt, getLintFixPrompt } from './prompt-codegen.js';
5
- import * as findFiles from '../files/find-files.js';
6
- import * as cliParams from '../cli/cli-params.js';
7
- import * as limits from './limits.js';
8
- import '../files/read-files.js';
9
-
10
- vi.mock('../files/find-files.js', () => ({
11
- rcConfig: {},
12
- getSourceFiles: vi.fn(),
13
- }));
14
- vi.mock('../files/read-files.js', () => ({
15
- getSourceCode: () => ({}),
16
- }));
17
- vi.mock('../cli/cli-params.js', () => ({
18
- requireExplanations: false,
19
- considerAllFiles: false,
20
- dependencyTree: false,
21
- explicitPrompt: false,
22
- allowFileCreate: false,
23
- allowFileDelete: false,
24
- allowDirectoryCreate: false,
25
- allowFileMove: false,
26
- verbosePrompt: false,
27
- imagen: false,
28
- vision: false,
29
- }));
30
- vi.mock('./limits.js');
31
-
32
- describe('getCodeGenPrompt', () => {
33
- beforeEach(() => {
34
- vi.resetAllMocks();
35
- });
36
-
37
- it('should generate prompt for all files when considerAllFiles is true', () => {
38
- vi.spyOn(findFiles, 'getSourceFiles').mockReturnValue(['file1.js', 'file2.js']);
39
- cliParams.considerAllFiles = true;
40
- cliParams.explicitPrompt = null;
41
- cliParams.allowFileCreate = false;
42
- cliParams.allowFileDelete = false;
43
- cliParams.allowDirectoryCreate = false;
44
- cliParams.allowFileMove = false;
45
- vi.spyOn(limits, 'verifyCodegenPromptLimit').mockImplementation(() => {});
46
-
47
- const prompt = getCodeGenPrompt();
48
-
49
- expect(prompt).toContain('You are allowed to modify all files in the application');
50
- expect(prompt).toContain('Do not create new files.');
51
- expect(prompt).toContain('Do not delete files.');
52
- expect(prompt).toContain('Do not create new directories.');
53
- expect(prompt).toContain('Do not move files.');
54
- });
55
-
56
- // Add more test cases for getCodeGenPrompt as needed
57
- });
58
-
59
- describe('getLintFixPrompt', () => {
60
- beforeEach(() => {
61
- cliParams.verbosePrompt = false;
62
- vi.spyOn(limits, 'verifyCodegenPromptLimit').mockImplementation(() => {});
63
- });
64
-
65
- it('should generate a lint fix prompt with provided command, stdout, and stderr', () => {
66
- const command = 'eslint --fix';
67
- const stdout = 'Fixed 2 errors';
68
- const stderr = '';
69
-
70
- const prompt = getLintFixPrompt(command, stdout, stderr);
71
-
72
- expect(prompt).toContain('The following lint errors were encountered after the initial code generation:');
73
- expect(prompt).toContain(`Lint command: ${command}`);
74
- expect(prompt).toContain('Lint command stdout:');
75
- expect(prompt).toContain(stdout);
76
- expect(prompt).toContain('Lint command stderr:');
77
- expect(prompt).toContain('Please suggest changes to fix these lint errors.');
78
- });
79
-
80
- it('should include stderr in the prompt when provided', () => {
81
- const command = 'eslint --fix';
82
- const stdout = '';
83
- const stderr = 'Error: Unable to resolve path';
84
-
85
- const prompt = getLintFixPrompt(command, stdout, stderr);
86
-
87
- expect(prompt).toContain('Lint command stderr:');
88
- expect(prompt).toContain(stderr);
89
- });
90
-
91
- it('should call verifyCodegenPromptLimit with the generated prompt', () => {
92
- const command = 'eslint --fix';
93
- const stdout = 'Fixed 1 error';
94
- const stderr = '';
95
-
96
- getLintFixPrompt(command, stdout, stderr);
97
-
98
- expect(limits.verifyCodegenPromptLimit).toHaveBeenCalledWith(expect.any(String));
99
- });
100
-
101
- it('should log the prompt when verbosePrompt is true', () => {
102
- cliParams.verbosePrompt = true;
103
- const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
104
-
105
- const command = 'eslint --fix';
106
- const stdout = 'Fixed 1 error';
107
- const stderr = '';
108
-
109
- getLintFixPrompt(command, stdout, stderr);
110
-
111
- expect(consoleSpy).toHaveBeenCalledWith('Lint fix prompt:');
112
- expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('The following lint errors were encountered'));
113
-
114
- consoleSpy.mockRestore();
115
- });
116
- });
@@ -1,163 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import { promptService } from './prompt-service.js';
3
- import * as vertexAi from '../ai-service/vertex-ai.js';
4
- import * as readline from 'readline';
5
- import '../cli/cli-params.js';
6
- import '../files/read-files.js';
7
- import '../files/find-files.js';
8
-
9
- vi.mock('../ai-service/vertex-ai.js', () => ({ generateContent: vi.fn() }));
10
- vi.mock('readline', () => ({
11
- createInterface: vi.fn(),
12
- }));
13
- vi.mock('../cli/cli-params.js', () => ({
14
- requireExplanations: false,
15
- considerAllFiles: false,
16
- dependencyTree: false,
17
- explicitPrompt: false,
18
- allowFileCreate: false,
19
- allowFileDelete: false,
20
- allowDirectoryCreate: false,
21
- allowFileMove: false,
22
- verbosePrompt: false,
23
- disableContextOptimization: false,
24
- vision: false,
25
- imagen: false,
26
- temperature: 0.7,
27
- cheap: false,
28
- askQuestion: true,
29
- }));
30
-
31
- // Mock find-files module
32
- vi.mock('../files/find-files.js', () => ({
33
- getSourceFiles: () => [],
34
- getImageAssetFiles: () => [],
35
- }));
36
-
37
- // Mock read-files module
38
- vi.mock('../files/read-files.js', () => ({
39
- getSourceCode: () => ({}),
40
- getImageAssets: vi.fn(() => ({})),
41
- }));
42
-
43
- vi.mock('../main/config.js', () => ({
44
- rootDir: '/mocked/root/dir',
45
- rcConfig: {
46
- rootDir: '.',
47
- extensions: ['.js', '.ts', '.tsx', '.jsx'],
48
- },
49
- }));
50
-
51
- describe('promptService with askQuestion', () => {
52
- let mockReadline;
53
-
54
- beforeEach(() => {
55
- vi.resetAllMocks();
56
- console.log = vi.fn();
57
-
58
- mockReadline = {
59
- question: vi.fn(),
60
- close: vi.fn(),
61
- };
62
- vi.spyOn(readline, 'createInterface').mockReturnValue(mockReadline);
63
- });
64
-
65
- afterEach(() => {
66
- vi.clearAllMocks();
67
- });
68
-
69
- it('should handle askQuestion when enabled', async () => {
70
- const mockAskQuestionCall = [
71
- {
72
- name: 'askQuestion',
73
- args: {
74
- content: 'Do you want to proceed with code generation?',
75
- shouldPrompt: true,
76
- promptNecessity: 80,
77
- stopCodegen: false,
78
- },
79
- },
80
- ];
81
- const mockAskQuestionCall2 = [
82
- {
83
- name: 'askQuestion',
84
- args: {
85
- content: 'Ok lets go',
86
- shouldPrompt: false,
87
- promptNecessity: 80,
88
- stopCodegen: false,
89
- },
90
- },
91
- ];
92
- const mockCodegenSummary = [
93
- {
94
- name: 'codegenSummary',
95
- args: {
96
- fileUpdates: [],
97
- contextPaths: [],
98
- explanation: 'No updates needed',
99
- },
100
- },
101
- ];
102
-
103
- vertexAi.generateContent
104
- .mockResolvedValueOnce(mockAskQuestionCall)
105
- .mockResolvedValueOnce(mockAskQuestionCall2)
106
- .mockResolvedValueOnce(mockCodegenSummary);
107
-
108
- mockReadline.question.mockImplementationOnce((_, callback) => {
109
- callback('Yes');
110
- });
111
-
112
- await promptService(vertexAi.generateContent);
113
-
114
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(3);
115
- expect(mockReadline.question).toHaveBeenCalledWith('Your answer: ', expect.any(Function));
116
- expect(console.log).toHaveBeenCalledWith('Assistant asks:', expect.any(Object));
117
- expect(console.log).toHaveBeenCalledWith('The question was answered');
118
- });
119
-
120
- it('should stop code generation when askQuestion returns stopCodegen: true', async () => {
121
- const mockAskQuestionCall = [
122
- {
123
- name: 'askQuestion',
124
- args: {
125
- content: 'Stopping code generation as requested.',
126
- shouldPrompt: false,
127
- promptNecessity: 100,
128
- stopCodegen: true,
129
- },
130
- },
131
- ];
132
-
133
- vertexAi.generateContent.mockResolvedValueOnce(mockAskQuestionCall);
134
-
135
- const result = await promptService(vertexAi.generateContent);
136
-
137
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(1);
138
- expect(result).toEqual([]);
139
- expect(console.log).toHaveBeenCalledWith('Assistant requested to stop code generation. Exiting...');
140
- });
141
-
142
- it('should proceed with code generation when askQuestion returns shouldPrompt: false', async () => {
143
- const mockAskQuestionCall = [];
144
- const mockCodegenSummary = [
145
- {
146
- name: 'codegenSummary',
147
- args: {
148
- fileUpdates: [],
149
- contextPaths: [],
150
- explanation: 'No updates needed',
151
- },
152
- },
153
- ];
154
-
155
- vertexAi.generateContent.mockResolvedValueOnce(mockAskQuestionCall).mockResolvedValueOnce(mockCodegenSummary);
156
-
157
- await promptService(vertexAi.generateContent);
158
-
159
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(2);
160
- expect(mockReadline.question).not.toHaveBeenCalled();
161
- expect(console.log).toHaveBeenCalledWith('Assistant did not ask a question. Proceeding with code generation.');
162
- });
163
- });
@@ -1,335 +0,0 @@
1
- import assert from 'node:assert';
2
- import fs from 'fs';
3
- import * as diff from 'diff';
4
- import mime from 'mime-types';
5
- import { createInterface } from 'readline';
6
-
7
- import { getSystemPrompt } from './systemprompt.js';
8
- import { getCodeGenPrompt } from './prompt-codegen.js';
9
- import { functionDefs } from './function-calling.js';
10
- import { getSourceCode, getImageAssets } from '../files/read-files.js';
11
- import { disableContextOptimization, temperature, vision, cheap, askQuestion } from '../cli/cli-params.js';
12
- import { validateFunctionCall } from './function-calling-validate.js';
13
-
14
- /** A function that communicates with model using */
15
- export async function promptService(generateContentFn, generateImageFn, codegenPrompt = getCodeGenPrompt()) {
16
- const messages = prepareMessages(codegenPrompt);
17
-
18
- // First stage: generate code generation summary, which should not take a lot of output tokens
19
- const getSourceCodeRequest = { name: 'getSourceCode' };
20
-
21
- const prompt = [
22
- { type: 'systemPrompt', systemPrompt: getSystemPrompt() },
23
- { type: 'user', text: messages.suggestSourceCode },
24
- { type: 'assistant', text: messages.requestSourceCode, functionCalls: [getSourceCodeRequest] },
25
- ];
26
-
27
- const getSourceCodeResponse = {
28
- type: 'user',
29
- functionResponses: [{ name: 'getSourceCode', content: messages.sourceCode }],
30
- cache: true,
31
- };
32
- prompt.push(getSourceCodeResponse);
33
-
34
- if (vision) {
35
- prompt.slice(-1)[0].text = messages.suggestImageAssets;
36
- prompt.push(
37
- { type: 'assistant', text: messages.requestImageAssets, functionCalls: [{ name: 'getImageAssets' }] },
38
- { type: 'user', functionResponses: [{ name: 'getImageAssets', content: messages.imageAssets }] },
39
- );
40
- }
41
-
42
- prompt.slice(-1)[0].text = messages.prompt;
43
-
44
- // New step: Allow the assistant to ask a question if --ask-question is enabled
45
- if (askQuestion) {
46
- console.log('Allowing the assistant to ask a question...');
47
- let questionAsked = false;
48
- while (!questionAsked) {
49
- const askQuestionResult = await generateContentFn(prompt, functionDefs, 'askQuestion', temperature, cheap);
50
- const askQuestionCall = askQuestionResult.find((call) => call.name === 'askQuestion');
51
- if (askQuestionCall) {
52
- console.log('Assistant asks:', askQuestionCall.args);
53
- const userAnswer = askQuestionCall.args.shouldPrompt
54
- ? await getUserInput('Your answer: ')
55
- : 'Lets proceed with code generation.';
56
- prompt.push(
57
- { type: 'assistant', functionCalls: [askQuestionCall] },
58
- {
59
- type: 'user',
60
- text: userAnswer,
61
- functionResponses: [{ name: 'askQuestion', call_id: askQuestionCall.id }],
62
- },
63
- );
64
- console.log('The question was answered');
65
- if (askQuestionCall.args.stopCodegen) {
66
- console.log('Assistant requested to stop code generation. Exiting...');
67
- return [];
68
- }
69
- if (!askQuestionCall.args.shouldPrompt) {
70
- console.log('Proceeding with code generation.');
71
- break;
72
- }
73
- } else {
74
- console.log('Assistant did not ask a question. Proceeding with code generation.');
75
- break;
76
- }
77
- }
78
- }
79
-
80
- let baseRequest = [prompt, functionDefs, 'codegenSummary', temperature, cheap];
81
- let baseResult = await generateContentFn(...baseRequest);
82
-
83
- let codegenSummaryRequest = baseResult.find((call) => call.name === 'codegenSummary');
84
-
85
- if (codegenSummaryRequest) {
86
- // Second stage: for each file request the actual code updates
87
- console.log('Received codegen summary, will collect partial updates', codegenSummaryRequest.args);
88
-
89
- baseResult = await validateAndRecoverSingleResult(baseRequest, baseResult, messages, generateContentFn);
90
- codegenSummaryRequest = baseResult.find((call) => call.name === 'codegenSummary');
91
-
92
- // Sometimes the result happens to be a string
93
- assert(Array.isArray(codegenSummaryRequest.args.fileUpdates), 'fileUpdates is not an array');
94
- assert(Array.isArray(codegenSummaryRequest.args.contextPaths), 'contextPaths is not an array');
95
-
96
- if (codegenSummaryRequest.args.contextPaths.length > 0 && !disableContextOptimization) {
97
- console.log('Optimize with context paths.');
98
- // Monkey patch the initial getSourceCode, do not send parts of source code that are consider irrelevant
99
- getSourceCodeRequest.args = {
100
- filePaths: [
101
- ...codegenSummaryRequest.args.fileUpdates.map((file) => file.path),
102
- ...codegenSummaryRequest.args.contextPaths,
103
- ],
104
- };
105
- getSourceCodeResponse.functionResponses.find((item) => item.name === 'getSourceCode').content =
106
- messages.contextSourceCode(getSourceCodeRequest.args.filePaths);
107
- }
108
-
109
- // Store the first stage response entirely in conversation history
110
- prompt.push({ type: 'assistant', functionCalls: baseResult });
111
- prompt.push({
112
- type: 'user',
113
- functionResponses: baseResult.map((call) => ({ name: call.name, call_id: call.id })),
114
- cache: true,
115
- });
116
-
117
- const result = [];
118
-
119
- for (const file of codegenSummaryRequest.args.fileUpdates) {
120
- console.log('Collecting partial update for: ' + file.path + ' using tool: ' + file.updateToolName);
121
- console.log('- Prompt:', file.prompt);
122
- console.log('- Temperature', file.temperature);
123
- console.log('- Cheap', file.cheap);
124
- if (vision) {
125
- console.log('- Context image assets', file.contextImageAssets);
126
- }
127
-
128
- // this is needed, otherwise we will get an error
129
- if (prompt.slice(-1)[0].type === 'user') {
130
- prompt.slice(-1)[0].text = file.prompt ?? messages.partialPromptTemplate(file.path);
131
- } else {
132
- prompt.push({ type: 'user', text: file.prompt ?? messages.partialPromptTemplate(file.path) });
133
- }
134
-
135
- if (vision && file.contextImageAssets) {
136
- prompt.slice(-1)[0].images = file.contextImageAssets.map((path) => ({
137
- path,
138
- base64url: fs.readFileSync(path, 'base64'),
139
- mediaType: mime.lookup(path),
140
- }));
141
- }
142
-
143
- let partialRequest = [
144
- prompt,
145
- functionDefs,
146
- file.updateToolName,
147
- file.temperature ?? temperature,
148
- file.cheap === true,
149
- ];
150
- let partialResult = await generateContentFn(...partialRequest);
151
-
152
- let getSourceCodeCall = partialResult.find((call) => call.name === 'getSourceCode');
153
- assert(!getSourceCodeCall, 'Unexpected getSourceCode: ' + JSON.stringify(getSourceCodeCall));
154
-
155
- // Handle image generation requests
156
- const generateImageCall = partialResult.find((call) => call.name === 'generateImage');
157
- if (generateImageCall) {
158
- assert(!!generateImageFn, 'Image generation requested, but a image generation service was not provided');
159
-
160
- console.log('Processing image generation request:', generateImageCall.args);
161
- try {
162
- const { prompt: imagePrompt, filePath, contextImagePath, size, cheap } = generateImageCall.args;
163
- const generatedImageUrl = await generateImageFn(imagePrompt, contextImagePath, size, cheap === true);
164
-
165
- // Add a createFile call to the result to ensure the generated image is tracked
166
- partialResult.push({
167
- name: 'downloadFile',
168
- args: {
169
- filePath: filePath,
170
- downloadUrl: generatedImageUrl,
171
- explanation: `Downloading generated image`,
172
- },
173
- });
174
- } catch (error) {
175
- console.error('Error generating image:', error);
176
- // Add an explanation about the failed image generation
177
- partialResult.push({
178
- name: 'explanation',
179
- args: {
180
- text: `Failed to generate image: ${error.message}`,
181
- },
182
- });
183
- }
184
- } else {
185
- partialResult = await validateAndRecoverSingleResult(
186
- partialRequest,
187
- partialResult,
188
- messages,
189
- generateContentFn,
190
- );
191
- }
192
-
193
- // Verify if patchFile is one of the functions called, and test if patch is valid and can be applied successfully
194
- const patchFileCall = partialResult.find((call) => call.name === 'patchFile');
195
- if (patchFileCall) {
196
- const { filePath, patch } = patchFileCall.args;
197
- console.log('Verification of patch for file:', filePath);
198
-
199
- let updatedContent;
200
- const currentContent = fs.readFileSync(filePath, 'utf-8');
201
-
202
- try {
203
- updatedContent = diff.applyPatch(currentContent, patch);
204
- } catch (e) {
205
- console.log('Error when applying patch', e);
206
- }
207
-
208
- if (!updatedContent) {
209
- console.log(`Patch could not be applied for ${filePath}. Retrying without patchFile function.`);
210
-
211
- // Rerun content generation without patchFile function
212
- partialRequest = [prompt, functionDefs, 'updateFile', file.temperature ?? temperature, file.cheap === true];
213
- partialResult = await generateContentFn(...partialRequest);
214
-
215
- partialResult = await validateAndRecoverSingleResult(
216
- partialRequest,
217
- partialResult,
218
- messages,
219
- generateContentFn,
220
- );
221
-
222
- let getSourceCodeCall = partialResult.find((call) => call.name === 'getSourceCode');
223
- assert(!getSourceCodeCall, 'Unexpected getSourceCode: ' + JSON.stringify(getSourceCodeCall));
224
- assert(!partialResult.find((call) => call.name === 'patchFile'), 'Unexpected patchFile in retry response');
225
- } else {
226
- console.log('Patch verified successfully');
227
- }
228
- }
229
-
230
- // add the code gen result to the context, as the subsequent code gen may depend on the result
231
- prompt.push(
232
- { type: 'assistant', functionCalls: partialResult },
233
- {
234
- type: 'user',
235
- functionResponses: partialResult.map((call) => ({ name: call.name, call_id: call.id })),
236
- },
237
- );
238
-
239
- result.push(...partialResult);
240
- }
241
-
242
- return result;
243
- } else {
244
- // This is unexpected, if happens probably means no code updates.
245
- console.log('Did not receive codegen summary, returning result.');
246
- return baseResult;
247
- }
248
- }
249
-
250
- async function validateAndRecoverSingleResult(
251
- [prompt, functionDefs, requiredFunctionName, temperature, cheap],
252
- result,
253
- messages,
254
- generateContentFn,
255
- ) {
256
- if (result.length !== 1) {
257
- return result;
258
- }
259
-
260
- const call = result[0];
261
- const validatorError = validateFunctionCall(call);
262
- if (validatorError) {
263
- console.log('Invalid function call', call, validatorError);
264
-
265
- prompt.push(
266
- { type: 'assistant', functionCalls: [call] },
267
- {
268
- type: 'user',
269
- text: messages.invalidFunctionCall,
270
- functionResponses: [
271
- {
272
- name: call.name,
273
- call_id: call.id,
274
- content: JSON.stringify({ args: call.args, error: validatorError }),
275
- isError: true,
276
- },
277
- ],
278
- },
279
- );
280
-
281
- console.log('Trying to recover...');
282
- if (cheap) {
283
- console.log('Disabling --cheap for recovery.');
284
- }
285
- result = await generateContentFn(prompt, functionDefs, requiredFunctionName, temperature, false);
286
- console.log('Recover result:', result);
287
-
288
- if (result.length === 1) {
289
- const recoveryError = validateFunctionCall(result[0]);
290
- assert(!recoveryError, 'Recovery failed');
291
- console.log('Recovery was sucessful');
292
- } else {
293
- console.log('Unexpected number of function calls', result);
294
- }
295
- }
296
-
297
- return result;
298
- }
299
-
300
- /**
301
- * Function to prepare messages for AI services
302
- */
303
- function prepareMessages(prompt) {
304
- return {
305
- suggestSourceCode: 'I should provide you with application source code.',
306
- requestSourceCode: 'Please provide application source code.',
307
- suggestImageAssets: 'I should also provide you with a summary of application image assets',
308
- requestImageAssets: 'Please provide summary of application image assets.',
309
- prompt:
310
- prompt +
311
- '\n Start from generating codegen summary, this summary will be used as a context to generate updates, so make sure that it contains useful information.',
312
- sourceCode: JSON.stringify(getSourceCode()),
313
- contextSourceCode: (paths) => JSON.stringify(getSourceCode(paths)),
314
- imageAssets: JSON.stringify(getImageAssets()),
315
- partialPromptTemplate(path) {
316
- return `Thank you for providing the summary, now suggest changes for the \`${path}\` file using appropriate tools.`;
317
- },
318
- invalidFunctionCall:
319
- 'Function call was invalid, please analyze the error and respond with corrected function call.',
320
- };
321
- }
322
-
323
- async function getUserInput(prompt) {
324
- const rl = createInterface({
325
- input: process.stdin,
326
- output: process.stdout,
327
- });
328
-
329
- return new Promise((resolve) => {
330
- rl.question(prompt, (answer) => {
331
- rl.close();
332
- resolve(answer);
333
- });
334
- });
335
- }