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,602 +0,0 @@
1
- /*eslint-disable no-import-assign*/
2
-
3
- import { describe, it, expect, beforeEach, vi } from 'vitest';
4
- import { promptService } from './prompt-service.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 cliParams from '../cli/cli-params.js';
9
- import fs from 'fs';
10
- import * as diff from 'diff';
11
- import mime from 'mime-types';
12
- import { getImageAssets } from '../files/read-files.js';
13
- import '../files/find-files.js';
14
- import * as dalleService from '../ai-service/dall-e.js';
15
-
16
- vi.mock('../ai-service/vertex-ai.js', () => ({ generateContent: vi.fn() }));
17
- vi.mock('../ai-service/chat-gpt.js', () => ({ generateContent: vi.fn() }));
18
- vi.mock('../ai-service/anthropic.js', () => ({ generateContent: vi.fn() }));
19
- vi.mock('../cli/cli-params.js', () => ({
20
- requireExplanations: false,
21
- considerAllFiles: false,
22
- dependencyTree: false,
23
- explicitPrompt: false,
24
- allowFileCreate: false,
25
- allowFileDelete: false,
26
- allowDirectoryCreate: false,
27
- allowFileMove: false,
28
- verbosePrompt: false,
29
- disableContextOptimization: false,
30
- vision: false,
31
- imagen: false,
32
- temperature: 0.7,
33
- cheap: false,
34
- askQuestion: false,
35
- }));
36
- vi.mock('fs');
37
- vi.mock('diff');
38
- vi.mock('mime-types');
39
- vi.mock('../ai-service/dall-e.js', () => ({ generateImage: vi.fn() }));
40
-
41
- // Mock find-files module
42
- vi.mock('../files/find-files.js', () => ({
43
- getSourceFiles: () => [],
44
- getImageAssetFiles: () => [],
45
- }));
46
-
47
- // Mock read-files module
48
- vi.mock('../files/read-files.js', () => ({
49
- getSourceCode: () => ({}),
50
- getImageAssets: vi.fn(() => ({})),
51
- }));
52
-
53
- vi.mock('../main/config.js', () => ({
54
- rootDir: '/mocked/root/dir',
55
- rcConfig: {
56
- rootDir: '.',
57
- extensions: ['.js', '.ts', '.tsx', '.jsx'],
58
- },
59
- }));
60
-
61
- describe('promptService', () => {
62
- beforeEach(() => {
63
- vi.resetAllMocks();
64
- cliParams.anthropic = false;
65
- cliParams.chatGpt = false;
66
- cliParams.vertexAi = false;
67
- cliParams.dryRun = false;
68
- cliParams.vision = false;
69
- cliParams.imagen = false;
70
- cliParams.disableContextOptimization = false;
71
- });
72
-
73
- it('should process the codegen summary and return the result with Vertex AI', async () => {
74
- cliParams.vertexAi = true;
75
- const mockFunctionCalls = [
76
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Hello");' } },
77
- ];
78
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
79
-
80
- const result = await promptService(vertexAi.generateContent);
81
-
82
- expect(result).toEqual(mockFunctionCalls);
83
- expect(vertexAi.generateContent).toHaveBeenCalled();
84
- });
85
-
86
- it('should process the codegen summary and return the result with ChatGPT', async () => {
87
- cliParams.chatGpt = true;
88
- const mockFunctionCalls = [{ name: 'createFile', args: { filePath: 'new.js', newContent: 'const x = 5;' } }];
89
- chatGpt.generateContent.mockResolvedValueOnce(mockFunctionCalls);
90
-
91
- const result = await promptService(chatGpt.generateContent);
92
-
93
- expect(result).toEqual(mockFunctionCalls);
94
- expect(chatGpt.generateContent).toHaveBeenCalled();
95
- });
96
-
97
- it('should process the codegen summary and return the result with Anthropic', async () => {
98
- cliParams.anthropic = true;
99
- const mockFunctionCalls = [{ name: 'deleteFile', args: { filePath: 'obsolete.js' } }];
100
- anthropic.generateContent.mockResolvedValueOnce(mockFunctionCalls);
101
-
102
- const result = await promptService(anthropic.generateContent);
103
-
104
- expect(result).toEqual(mockFunctionCalls);
105
- expect(anthropic.generateContent).toHaveBeenCalled();
106
- });
107
-
108
- it('should not update files in dry run mode', async () => {
109
- cliParams.vertexAi = true;
110
- cliParams.dryRun = true;
111
- const mockFunctionCalls = [
112
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Dry run");' } },
113
- ];
114
- vertexAi.generateContent.mockResolvedValueOnce(mockFunctionCalls);
115
-
116
- const result = await promptService(vertexAi.generateContent);
117
-
118
- expect(result).toEqual(mockFunctionCalls);
119
- expect(vertexAi.generateContent).toHaveBeenCalled();
120
- });
121
-
122
- it('should handle invalid patchFile call and retry without patchFile function', async () => {
123
- cliParams.vertexAi = true;
124
- const mockCodegenSummary = [
125
- {
126
- name: 'codegenSummary',
127
- args: {
128
- fileUpdates: [{ path: 'test.js', updateToolName: 'patchFile' }],
129
- contextPaths: [],
130
- explanation: 'Mock summary',
131
- },
132
- },
133
- ];
134
- const mockInvalidPatchCall = [
135
- {
136
- name: 'patchFile',
137
- args: {
138
- filePath: 'test.js',
139
- patch: 'invalid patch',
140
- },
141
- },
142
- ];
143
- const mockValidUpdateCall = [
144
- {
145
- name: 'updateFile',
146
- args: {
147
- filePath: 'test.js',
148
- newContent: 'console.log("Updated content");',
149
- },
150
- },
151
- ];
152
-
153
- // Mock the first call to return the codegen summary
154
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
155
- // Mock the second call to return an invalid patch
156
- vertexAi.generateContent.mockResolvedValueOnce(mockInvalidPatchCall);
157
- // Mock the third call (retry) to return a valid update
158
- vertexAi.generateContent.mockResolvedValueOnce(mockValidUpdateCall);
159
-
160
- // Mock fs.readFileSync to return some content
161
- fs.readFileSync.mockReturnValue('Original content');
162
-
163
- // Mock diff.applyPatch to fail for the invalid patch
164
- diff.applyPatch.mockReturnValue(false);
165
-
166
- const result = await promptService(vertexAi.generateContent);
167
-
168
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(3);
169
- expect(fs.readFileSync).toHaveBeenCalledWith('test.js', 'utf-8');
170
- expect(diff.applyPatch).toHaveBeenCalledWith('Original content', 'invalid patch');
171
- expect(result).toEqual(mockValidUpdateCall);
172
- });
173
-
174
- it('should include image assets when vision flag is true', async () => {
175
- cliParams.chatGpt = true;
176
- cliParams.vision = true;
177
- const mockImageAssets = {
178
- '/path/to/image1.png': { width: 100, height: 100 },
179
- '/path/to/image2.jpg': { width: 200, height: 200 },
180
- };
181
- const mockFunctionCalls = [
182
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("Vision test");' } },
183
- ];
184
-
185
- getImageAssets.mockReturnValue(mockImageAssets);
186
- chatGpt.generateContent.mockResolvedValueOnce(mockFunctionCalls);
187
-
188
- await promptService(chatGpt.generateContent);
189
-
190
- expect(chatGpt.generateContent).toHaveBeenCalled();
191
- const calls = chatGpt.generateContent.mock.calls[0];
192
- expect(calls[0]).toEqual(
193
- expect.arrayContaining([
194
- expect.objectContaining({
195
- type: 'user',
196
- text: expect.stringContaining('I should also provide you with a summary of application image assets'),
197
- }),
198
- expect.objectContaining({
199
- type: 'assistant',
200
- text: expect.stringContaining('Please provide summary of application image assets.'),
201
- }),
202
- expect.objectContaining({
203
- type: 'user',
204
- functionResponses: [{ name: 'getImageAssets', content: JSON.stringify(mockImageAssets) }],
205
- }),
206
- ]),
207
- );
208
- });
209
-
210
- it('should include image data in the prompt when processing files with vision', async () => {
211
- cliParams.chatGpt = true;
212
- cliParams.vision = true;
213
- const mockCodegenSummary = [
214
- {
215
- name: 'codegenSummary',
216
- args: {
217
- fileUpdates: [
218
- {
219
- path: 'test.js',
220
- updateToolName: 'updateFile',
221
- contextImageAssets: ['/path/to/image1.png', '/path/to/image2.jpg'],
222
- },
223
- ],
224
- contextPaths: [],
225
- explanation: 'Mock summary',
226
- },
227
- },
228
- ];
229
- const mockUpdateCall = [
230
- {
231
- name: 'updateFile',
232
- args: {
233
- filePath: 'test.js',
234
- newContent: 'console.log("Updated with vision");',
235
- },
236
- },
237
- ];
238
-
239
- chatGpt.generateContent.mockResolvedValueOnce(mockCodegenSummary);
240
- chatGpt.generateContent.mockResolvedValueOnce(mockUpdateCall);
241
-
242
- fs.readFileSync.mockImplementation((path) => `mock-base64-data-for-${path}`);
243
- mime.lookup.mockImplementation((path) => (path.endsWith('.png') ? 'image/png' : 'image/jpeg'));
244
-
245
- await promptService(chatGpt.generateContent);
246
-
247
- expect(chatGpt.generateContent).toHaveBeenCalledTimes(2);
248
- const secondCall = chatGpt.generateContent.mock.calls[1];
249
- expect(secondCall[0]).toEqual(
250
- expect.arrayContaining([
251
- expect.objectContaining({
252
- type: 'user',
253
- text: expect.stringContaining('suggest changes for the `test.js` file'),
254
- images: [
255
- {
256
- path: '/path/to/image1.png',
257
- base64url: 'mock-base64-data-for-/path/to/image1.png',
258
- mediaType: 'image/png',
259
- },
260
- {
261
- path: '/path/to/image2.jpg',
262
- base64url: 'mock-base64-data-for-/path/to/image2.jpg',
263
- mediaType: 'image/jpeg',
264
- },
265
- ],
266
- }),
267
- ]),
268
- );
269
- });
270
-
271
- it('should not include image assets when vision flag is false', async () => {
272
- cliParams.chatGpt = true;
273
- cliParams.vision = false;
274
- const mockFunctionCalls = [
275
- { name: 'updateFile', args: { filePath: 'test.js', newContent: 'console.log("No vision test");' } },
276
- ];
277
-
278
- chatGpt.generateContent.mockResolvedValueOnce(mockFunctionCalls);
279
-
280
- await promptService(chatGpt.generateContent);
281
-
282
- expect(chatGpt.generateContent).toHaveBeenCalled();
283
- const calls = chatGpt.generateContent.mock.calls[0];
284
- expect(calls[0]).not.toEqual(
285
- expect.arrayContaining([
286
- expect.objectContaining({
287
- type: 'user',
288
- text: expect.stringContaining('I should also provide you with a summary of application image assets'),
289
- }),
290
- ]),
291
- );
292
- });
293
-
294
- it('should handle context optimization', async () => {
295
- cliParams.vertexAi = true;
296
- const mockCodegenSummary = [
297
- {
298
- name: 'codegenSummary',
299
- args: {
300
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
301
- contextPaths: ['context1.js', 'context2.js'],
302
- explanation: 'Mock summary with context',
303
- },
304
- },
305
- ];
306
- const mockUpdateCall = [
307
- {
308
- name: 'updateFile',
309
- args: {
310
- filePath: 'test.js',
311
- newContent: 'console.log("Updated with context");',
312
- },
313
- },
314
- ];
315
-
316
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
317
- vertexAi.generateContent.mockResolvedValueOnce(mockUpdateCall);
318
-
319
- await promptService(vertexAi.generateContent);
320
-
321
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(2);
322
- const firstCall = vertexAi.generateContent.mock.calls[0];
323
- expect(firstCall[0]).toEqual(
324
- expect.arrayContaining([
325
- expect.objectContaining({
326
- type: 'user',
327
- functionResponses: [
328
- expect.objectContaining({
329
- name: 'getSourceCode',
330
- content: expect.any(String),
331
- }),
332
- ],
333
- }),
334
- ]),
335
- );
336
- });
337
-
338
- it('should handle disableContextOptimization flag', async () => {
339
- cliParams.vertexAi = true;
340
- cliParams.disableContextOptimization = true;
341
- const mockCodegenSummary = [
342
- {
343
- name: 'codegenSummary',
344
- args: {
345
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
346
- contextPaths: ['context1.js', 'context2.js'],
347
- explanation: 'Mock summary without context optimization',
348
- },
349
- },
350
- ];
351
- const mockUpdateCall = [
352
- {
353
- name: 'updateFile',
354
- args: {
355
- filePath: 'test.js',
356
- newContent: 'console.log("Updated without context optimization");',
357
- },
358
- },
359
- ];
360
-
361
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
362
- vertexAi.generateContent.mockResolvedValueOnce(mockUpdateCall);
363
-
364
- await promptService(vertexAi.generateContent);
365
-
366
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(2);
367
- const firstCall = vertexAi.generateContent.mock.calls[0];
368
- expect(firstCall[0]).not.toEqual(
369
- expect.arrayContaining([
370
- expect.objectContaining({
371
- type: 'user',
372
- functionResponses: [
373
- expect.objectContaining({
374
- name: 'getSourceCode',
375
- content: expect.stringContaining('context1.js'),
376
- }),
377
- ],
378
- }),
379
- ]),
380
- );
381
- });
382
-
383
- it('should handle image generation requests', async () => {
384
- cliParams.vertexAi = true;
385
- cliParams.imagen = true;
386
- const mockCodegenSummary = [
387
- {
388
- name: 'codegenSummary',
389
- args: {
390
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
391
- contextPaths: [],
392
- explanation: 'Mock summary with image generation',
393
- },
394
- },
395
- ];
396
- const mockGenerateImageCall = [
397
- {
398
- name: 'generateImage',
399
- args: {
400
- prompt: 'A test image',
401
- filePath: '/path/to/generated/image.png',
402
- size: { width: 256, height: 256 },
403
- },
404
- },
405
- ];
406
- const mockDownloadFileCall = [
407
- {
408
- name: 'downloadFile',
409
- args: {
410
- filePath: '/path/to/generated/image.png',
411
- explanation: 'Downloading generated image',
412
- downloadUrl: 'https://example.com/generated-image.png',
413
- },
414
- },
415
- ];
416
-
417
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
418
- vertexAi.generateContent.mockResolvedValueOnce(mockGenerateImageCall);
419
- dalleService.generateImage.mockResolvedValue('https://example.com/generated-image.png');
420
-
421
- const result = await promptService(vertexAi.generateContent, dalleService.generateImage);
422
-
423
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(2);
424
- expect(dalleService.generateImage).toHaveBeenCalledWith(
425
- 'A test image',
426
- undefined,
427
- { width: 256, height: 256 },
428
- false,
429
- );
430
- expect(result).toEqual(expect.arrayContaining(mockDownloadFileCall));
431
- });
432
-
433
- it('should handle image generation failure', async () => {
434
- cliParams.vertexAi = true;
435
- cliParams.imagen = true;
436
- const mockCodegenSummary = [
437
- {
438
- name: 'codegenSummary',
439
- args: {
440
- fileUpdates: [{ path: 'test.js', updateToolName: 'updateFile' }],
441
- contextPaths: [],
442
- explanation: 'Mock summary with image generation failure',
443
- },
444
- },
445
- ];
446
- const mockGenerateImageCall = [
447
- {
448
- name: 'generateImage',
449
- args: {
450
- prompt: 'A test image',
451
- filePath: '/path/to/generated/image.png',
452
- size: { width: 256, height: 256 },
453
- },
454
- },
455
- ];
456
-
457
- vertexAi.generateContent.mockResolvedValueOnce(mockCodegenSummary);
458
- vertexAi.generateContent.mockResolvedValueOnce(mockGenerateImageCall);
459
- dalleService.generateImage.mockRejectedValue(new Error('Image generation failed'));
460
-
461
- const result = await promptService(vertexAi.generateContent, dalleService.generateImage);
462
-
463
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(2);
464
- expect(dalleService.generateImage).toHaveBeenCalledWith(
465
- 'A test image',
466
- undefined,
467
- { width: 256, height: 256 },
468
- false,
469
- );
470
- expect(result).toEqual(
471
- expect.arrayContaining([
472
- expect.objectContaining({
473
- name: 'explanation',
474
- args: {
475
- text: expect.stringContaining('Failed to generate image: Image generation failed'),
476
- },
477
- }),
478
- ]),
479
- );
480
- });
481
-
482
- it('should handle unexpected response without codegen summary', async () => {
483
- cliParams.vertexAi = true;
484
- const mockUnexpectedResponse = [
485
- {
486
- name: 'updateFile',
487
- args: {
488
- filePath: 'test.js',
489
- newContent: 'console.log("Unexpected response");',
490
- },
491
- },
492
- ];
493
-
494
- vertexAi.generateContent.mockResolvedValueOnce(mockUnexpectedResponse);
495
-
496
- const result = await promptService(vertexAi.generateContent);
497
-
498
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(1);
499
- expect(result).toEqual(mockUnexpectedResponse);
500
- });
501
-
502
- describe('validateAndRecoverSingleResult', () => {
503
- it('should successfully recover from an invalid function call', async () => {
504
- cliParams.vertexAi = true;
505
- const mockInvalidCall = [
506
- {
507
- name: 'codegenSummary',
508
- args: {
509
- files: [{ path: 'test.js', updateToolName: 'patchFile' }],
510
- contextPaths: [],
511
- explanation: 'Mock summary',
512
- },
513
- },
514
- ];
515
- const mockValidCall = [
516
- {
517
- name: 'codegenSummary',
518
- args: {
519
- fileUpdates: [{ path: 'test.js', updateToolName: 'patchFile' }],
520
- contextPaths: [],
521
- explanation: 'Mock summary',
522
- },
523
- },
524
- ];
525
-
526
- vertexAi.generateContent
527
- .mockResolvedValueOnce(mockInvalidCall)
528
- .mockResolvedValueOnce(mockValidCall)
529
- .mockResolvedValueOnce([
530
- {
531
- name: 'updateFile',
532
- args: {
533
- filePath: 'test.js',
534
- newContent: 'console.log("Unexpected response");',
535
- },
536
- },
537
- ]);
538
-
539
- const result = await promptService(vertexAi.generateContent);
540
-
541
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(3);
542
- expect(result).toEqual([
543
- {
544
- args: {
545
- filePath: 'test.js',
546
- newContent: 'console.log("Unexpected response");',
547
- },
548
- name: 'updateFile',
549
- },
550
- ]);
551
- });
552
-
553
- it('should handle unsuccessful recovery', async () => {
554
- cliParams.vertexAi = true;
555
- const mockInvalidCall = [
556
- {
557
- name: 'updateFile',
558
- args: { filePath: 'test.js', invalidArg: 'This should not be here' },
559
- },
560
- ];
561
-
562
- vertexAi.generateContent
563
- .mockResolvedValueOnce([
564
- {
565
- name: 'codegenSummary',
566
- args: {
567
- fileUpdates: [{ path: 'test.js', updateToolName: 'patchFile' }],
568
- contextPaths: [],
569
- explanation: 'Mock summary',
570
- },
571
- },
572
- ])
573
- .mockResolvedValueOnce(mockInvalidCall)
574
- .mockResolvedValueOnce(mockInvalidCall); // Second call also returns invalid result
575
-
576
- await expect(promptService(vertexAi.generateContent)).rejects.toThrow('Recovery failed');
577
-
578
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(3);
579
- });
580
-
581
- it('should not attempt recovery for multiple valid function calls', async () => {
582
- cliParams.vertexAi = true;
583
- const mockValidCalls = [
584
- {
585
- name: 'updateFile',
586
- args: { filePath: 'test1.js', newContent: 'console.log("File 1");' },
587
- },
588
- {
589
- name: 'updateFile',
590
- args: { filePath: 'test2.js', newContent: 'console.log("File 2");' },
591
- },
592
- ];
593
-
594
- vertexAi.generateContent.mockResolvedValueOnce(mockValidCalls);
595
-
596
- const result = await promptService(vertexAi.generateContent);
597
-
598
- expect(vertexAi.generateContent).toHaveBeenCalledTimes(1);
599
- expect(result).toEqual(mockValidCalls);
600
- });
601
- });
602
- });
@@ -1,39 +0,0 @@
1
- import { CODEGEN_TRIGGER } from './prompt-consts.js';
2
- import { verbosePrompt, askQuestion } from '../cli/cli-params.js';
3
- import { verifySystemPromptLimit } from './limits.js';
4
- import { rcConfig } from '../main/config.js';
5
-
6
- /** Generates a system prompt */
7
- export function getSystemPrompt() {
8
- console.log('Generate system prompt');
9
-
10
- let systemPrompt = `
11
- You are a code generation assistant. I want you to help me generate code for my ideas in my application source code.
12
-
13
- You can generate new code, or modify the existing one. You will receive instructions on what is the goal of requested code modification.
14
-
15
- Instructions will be passed to you either directly via message, with a file, or using the ${CODEGEN_TRIGGER} comment in the code.
16
-
17
- You should parse my application source code and then suggest changes using appropriate tools.
18
-
19
- The root directory of my application is \`${rcConfig.rootDir}\` and you should limit the changes only to this path.
20
-
21
- When suggesting changes always use absolute file paths.
22
- `;
23
-
24
- if (askQuestion) {
25
- systemPrompt +=
26
- '\nYou have the ability to ask the user a question at the beginning of the conversation if you need more information or clarification. ' +
27
- 'Use this feature wisely to gather any crucial information that would help you better understand the task or provide more accurate code generation. ' +
28
- "To ask a question, use the 'askQuestion' function.";
29
- }
30
-
31
- if (verbosePrompt) {
32
- console.log('System prompt:');
33
- console.log(systemPrompt);
34
- }
35
-
36
- verifySystemPromptLimit(systemPrompt);
37
-
38
- return systemPrompt;
39
- }
@@ -1,64 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
2
- import { getSystemPrompt } from './systemprompt.js';
3
- import * as cliParams from '../cli/cli-params.js';
4
- import '../files/find-files.js';
5
- import '../main/config.js';
6
-
7
- vi.mock('../cli/cli-params.js', () => ({
8
- requireExplanations: false,
9
- considerAllFiles: false,
10
- dependencyTree: false,
11
- explicitPrompt: false,
12
- allowFileCreate: false,
13
- allowFileDelete: false,
14
- allowDirectoryCreate: false,
15
- allowFileMove: false,
16
- verbosePrompt: false,
17
- askQuestion: false,
18
- }));
19
-
20
- vi.mock('../files/find-files.js', () => ({
21
- getSourceFiles: vi.fn(),
22
- }));
23
-
24
- vi.mock('../main/config.js', () => ({
25
- rcConfig: { rootDir: '/mocked/root/dir' },
26
- }));
27
-
28
- describe('getSystemPrompt', () => {
29
- beforeEach(() => {
30
- vi.clearAllMocks();
31
- });
32
-
33
- it('generates correct system prompt', () => {
34
- const systemPrompt = getSystemPrompt();
35
-
36
- expect(systemPrompt).toContain(
37
- 'You are a code generation assistant. I want you to help me generate code for my ideas in my application source code.',
38
- );
39
- expect(systemPrompt).toContain('You can generate new code, or modify the existing one.');
40
- expect(systemPrompt).toContain(
41
- 'Instructions will be passed to you either directly via message, with a file, or using the ' +
42
- '@' +
43
- 'CODEGEN comment in the code.',
44
- );
45
- expect(systemPrompt).toContain(
46
- 'The root directory of my application is `/mocked/root/dir` and you should limit the changes only to this path.',
47
- );
48
- expect(systemPrompt).toContain('When suggesting changes always use absolute file paths.');
49
- });
50
-
51
- it('verifies system prompt limit', () => {
52
- vi.spyOn(cliParams, 'verbosePrompt', 'get').mockReturnValue(true);
53
- const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
54
-
55
- getSystemPrompt();
56
-
57
- expect(consoleSpy).toHaveBeenCalledWith('System prompt:');
58
- expect(consoleSpy).toHaveBeenCalledWith(
59
- expect.stringContaining('You are a code generation assistant. I want you to help me generate code for my ideas'),
60
- );
61
-
62
- consoleSpy.mockRestore();
63
- });
64
- });