genaicode 0.0.35 → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/package.json +3 -2
- package/src/ai-service/anthropic.js +45 -14
- package/src/ai-service/chat-gpt.js +34 -7
- package/src/ai-service/common.js +10 -1
- package/src/cli/cli-options.js +12 -0
- package/src/cli/cli-params.js +15 -0
- package/src/cli/validate-cli-params.js +20 -0
- package/src/files/read-files.js +8 -3
- package/src/files/read-files.test.js +1 -0
- package/src/main/codegen.test.js +1 -0
- package/src/prompt/function-calling.js +33 -440
- package/src/prompt/function-defs/ask-question.js +33 -0
- package/src/prompt/function-defs/codegen-summary.js +87 -0
- package/src/prompt/function-defs/create-directory.js +21 -0
- package/src/prompt/function-defs/create-file.js +26 -0
- package/src/prompt/function-defs/delete-file.js +21 -0
- package/src/prompt/function-defs/download-file.js +25 -0
- package/src/prompt/function-defs/explanation.js +17 -0
- package/src/prompt/function-defs/generate-image.js +51 -0
- package/src/prompt/function-defs/get-image-assets.js +21 -0
- package/src/prompt/function-defs/get-source-code.js +21 -0
- package/src/prompt/function-defs/imgly-remove-background.js +25 -0
- package/src/prompt/function-defs/move-file.js +25 -0
- package/src/prompt/function-defs/patch-file.js +39 -0
- package/src/prompt/function-defs/resize-image.js +36 -0
- package/src/prompt/function-defs/split-image.js +45 -0
- package/src/prompt/function-defs/update-file.js +26 -0
- package/src/prompt/prompt-service-ask-question.test.js +163 -0
- package/src/prompt/prompt-service.js +56 -8
- package/src/prompt/prompt-service.test.js +1 -0
- package/src/prompt/systemprompt.js +8 -1
- package/src/prompt/systemprompt.test.js +1 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for downloadFile
|
|
3
|
+
*/
|
|
4
|
+
export const downloadFile = {
|
|
5
|
+
name: 'downloadFile',
|
|
6
|
+
description: 'Download file from url, and save to file',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
filePath: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: 'The file path to save the downloaded file.',
|
|
13
|
+
},
|
|
14
|
+
downloadUrl: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'The url of the file that will be used for downloading.',
|
|
17
|
+
},
|
|
18
|
+
explanation: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'The reasoning behind downloading this image.',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
required: ['filePath', 'downloadUrl'],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for explanation
|
|
3
|
+
*/
|
|
4
|
+
export const explanation = {
|
|
5
|
+
name: 'explanation',
|
|
6
|
+
description: 'Explain the reasoning behind the suggested code changes or reasoning for lack of code changes',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
text: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: 'The explanation text',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
required: ['text'],
|
|
16
|
+
},
|
|
17
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for generateImage
|
|
3
|
+
*/
|
|
4
|
+
export const generateImage = {
|
|
5
|
+
name: 'generateImage',
|
|
6
|
+
description: 'Generate an image using AI service and save it as a file.',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
prompt: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description:
|
|
13
|
+
'The prompt that will be used to generate the image. This prompt must be detailed, it will be used by image generation model.',
|
|
14
|
+
},
|
|
15
|
+
filePath: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: 'The file path to save the generated image.',
|
|
18
|
+
},
|
|
19
|
+
contextImagePath: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description:
|
|
22
|
+
'Path to a image file that will be used as a context for image generation. It is useful if there is a need to edit an image with genAI.',
|
|
23
|
+
},
|
|
24
|
+
size: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
width: {
|
|
28
|
+
type: 'number',
|
|
29
|
+
description: 'width of the image',
|
|
30
|
+
},
|
|
31
|
+
height: {
|
|
32
|
+
type: 'number',
|
|
33
|
+
description: 'height of the image',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
required: ['width', 'height'],
|
|
37
|
+
description: 'The size of the image to generate.',
|
|
38
|
+
},
|
|
39
|
+
cheap: {
|
|
40
|
+
type: 'boolean',
|
|
41
|
+
description:
|
|
42
|
+
'true value means that the prompt will be executed with cheaper model, which work faster, but provides lower quality results, so please use it only in situation when lower quality results are acceptable for the prompt.',
|
|
43
|
+
},
|
|
44
|
+
explanation: {
|
|
45
|
+
type: 'string',
|
|
46
|
+
description: 'The explanation of the reasoning behind generating this image',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
required: ['prompt', 'filePath', 'size', 'cheap'],
|
|
50
|
+
},
|
|
51
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for getImageAssets
|
|
3
|
+
*/
|
|
4
|
+
export const getImageAssets = {
|
|
5
|
+
name: 'getImageAssets',
|
|
6
|
+
description:
|
|
7
|
+
'This function returns a map of application image assets. This map contains absolute file path, and basic metadata information. It does not contain contents. Contents must be requested using dedicated tool.',
|
|
8
|
+
parameters: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
filePaths: {
|
|
12
|
+
type: 'array',
|
|
13
|
+
description: 'An array of absolute paths of files that should be used to provided context.',
|
|
14
|
+
items: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
required: [],
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for getSourceCode
|
|
3
|
+
*/
|
|
4
|
+
export const getSourceCode = {
|
|
5
|
+
name: 'getSourceCode',
|
|
6
|
+
description:
|
|
7
|
+
'This function returns source code of the application in Map format, where absolute file path is the key, and the value is an object, where one of the properties may be the content of the file. Some keys may not provide content. This function can be called only once during the conversation, and only if suggested by the user.',
|
|
8
|
+
parameters: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
filePaths: {
|
|
12
|
+
type: 'array',
|
|
13
|
+
description: 'An array of absolute paths of files that should be used to provided context.',
|
|
14
|
+
items: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
required: [],
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for imglyRemoveBackground
|
|
3
|
+
*/
|
|
4
|
+
export const imglyRemoveBackground = {
|
|
5
|
+
name: 'imglyRemoveBackground',
|
|
6
|
+
description: 'Removes background from an image using @imgly/background-removal-node',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
inputFilePath: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: 'The file path of the input image.',
|
|
13
|
+
},
|
|
14
|
+
outputFilePath: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'The file path to save the output image with removed background.',
|
|
17
|
+
},
|
|
18
|
+
explanation: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'The explanation of the reasoning behind removing the background from this image',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
required: ['inputFilePath', 'outputFilePath'],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for moveFile
|
|
3
|
+
*/
|
|
4
|
+
export const moveFile = {
|
|
5
|
+
name: 'moveFile',
|
|
6
|
+
description: 'Move a file from one location to another',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
source: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: 'The current file path.',
|
|
13
|
+
},
|
|
14
|
+
destination: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'The new file path.',
|
|
17
|
+
},
|
|
18
|
+
explanation: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'The explanation of the reasoning behind moving this file',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
required: ['source', 'destination'],
|
|
24
|
+
},
|
|
25
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for patchFile
|
|
3
|
+
*/
|
|
4
|
+
export const patchFile = {
|
|
5
|
+
name: 'patchFile',
|
|
6
|
+
description:
|
|
7
|
+
'Partially update a file content. The file must already exists in the application source code. The function should be called only if there is a need to actually change something.',
|
|
8
|
+
parameters: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
filePath: {
|
|
12
|
+
type: 'string',
|
|
13
|
+
description: 'The file path to patch.',
|
|
14
|
+
},
|
|
15
|
+
patch: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: `Modification to the file expressed in patch format. Example patch:
|
|
18
|
+
|
|
19
|
+
\`\`\`
|
|
20
|
+
Index: filename.js
|
|
21
|
+
===================================================================
|
|
22
|
+
--- filename.js
|
|
23
|
+
+++ filename.js
|
|
24
|
+
@@ -1,2 +1,3 @@
|
|
25
|
+
line1
|
|
26
|
+
+line3
|
|
27
|
+
line2
|
|
28
|
+
\
|
|
29
|
+
\`\`\`
|
|
30
|
+
`,
|
|
31
|
+
},
|
|
32
|
+
explanation: {
|
|
33
|
+
type: 'string',
|
|
34
|
+
description: 'The explanation of the reasoning behind the suggested code changes for this file',
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
required: ['filePath', 'patch'],
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for resizeImage
|
|
3
|
+
*/
|
|
4
|
+
export const resizeImage = {
|
|
5
|
+
name: 'resizeImage',
|
|
6
|
+
description: 'Resize image to the desired size',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
filePath: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: 'The file path of the image.',
|
|
13
|
+
},
|
|
14
|
+
size: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
width: {
|
|
18
|
+
type: 'number',
|
|
19
|
+
description: 'width of the image',
|
|
20
|
+
},
|
|
21
|
+
height: {
|
|
22
|
+
type: 'number',
|
|
23
|
+
description: 'height of the image',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
required: ['width', 'height'],
|
|
27
|
+
description: 'The size of the image to generate.',
|
|
28
|
+
},
|
|
29
|
+
explanation: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: 'The explanation of the reasoning behind removing the background from this image',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
required: ['filePath', 'size'],
|
|
35
|
+
},
|
|
36
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for splitImage
|
|
3
|
+
*/
|
|
4
|
+
export const splitImage = {
|
|
5
|
+
name: 'splitImage',
|
|
6
|
+
description: 'Split an image into multiple parts and save them as separate files.',
|
|
7
|
+
parameters: {
|
|
8
|
+
type: 'object',
|
|
9
|
+
properties: {
|
|
10
|
+
inputFilePath: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
description: 'The file path of the input image to be split.',
|
|
13
|
+
},
|
|
14
|
+
parts: {
|
|
15
|
+
type: 'array',
|
|
16
|
+
items: {
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
rect: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: {
|
|
22
|
+
x: { type: 'number', description: 'The x-coordinate of the top-left corner of the rectangle.' },
|
|
23
|
+
y: { type: 'number', description: 'The y-coordinate of the top-left corner of the rectangle.' },
|
|
24
|
+
width: { type: 'number', description: 'The width of the rectangle.' },
|
|
25
|
+
height: { type: 'number', description: 'The height of the rectangle.' },
|
|
26
|
+
},
|
|
27
|
+
required: ['x', 'y', 'width', 'height'],
|
|
28
|
+
},
|
|
29
|
+
outputFilePath: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: 'The file path to save the extracted part of the image.',
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
required: ['rect', 'outputFilePath'],
|
|
35
|
+
},
|
|
36
|
+
description: 'An array of parts to extract from the image, each with a rectangle and output file path.',
|
|
37
|
+
},
|
|
38
|
+
explanation: {
|
|
39
|
+
type: 'string',
|
|
40
|
+
description: 'The explanation of the reasoning behind splitting this image',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
required: ['inputFilePath', 'parts'],
|
|
44
|
+
},
|
|
45
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Function definition for updateFile
|
|
3
|
+
*/
|
|
4
|
+
export const updateFile = {
|
|
5
|
+
name: 'updateFile',
|
|
6
|
+
description:
|
|
7
|
+
'Update a file with new content. The file must already exists in the application source code. The function should be called only if there is a need to actually change something.',
|
|
8
|
+
parameters: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
filePath: {
|
|
12
|
+
type: 'string',
|
|
13
|
+
description: 'The file path to update.',
|
|
14
|
+
},
|
|
15
|
+
newContent: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: 'The content to update the file with. Must not be empty.',
|
|
18
|
+
},
|
|
19
|
+
explanation: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: 'The explanation of the reasoning behind the suggested code changes for this file',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
required: ['filePath', 'newContent'],
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
});
|
|
@@ -2,12 +2,13 @@ import assert from 'node:assert';
|
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import * as diff from 'diff';
|
|
4
4
|
import mime from 'mime-types';
|
|
5
|
+
import { createInterface } from 'readline';
|
|
5
6
|
|
|
6
7
|
import { getSystemPrompt } from './systemprompt.js';
|
|
7
8
|
import { getCodeGenPrompt } from './prompt-codegen.js';
|
|
8
9
|
import { functionDefs } from './function-calling.js';
|
|
9
10
|
import { getSourceCode, getImageAssets } from '../files/read-files.js';
|
|
10
|
-
import { disableContextOptimization, temperature, vision, cheap } from '../cli/cli-params.js';
|
|
11
|
+
import { disableContextOptimization, temperature, vision, cheap, askQuestion } from '../cli/cli-params.js';
|
|
11
12
|
import { validateFunctionCall } from './function-calling-validate.js';
|
|
12
13
|
|
|
13
14
|
/** A function that communicates with model using */
|
|
@@ -26,24 +27,56 @@ export async function promptService(generateContentFn, generateImageFn, codegenP
|
|
|
26
27
|
const getSourceCodeResponse = {
|
|
27
28
|
type: 'user',
|
|
28
29
|
functionResponses: [{ name: 'getSourceCode', content: messages.sourceCode }],
|
|
30
|
+
cache: true,
|
|
29
31
|
};
|
|
30
32
|
prompt.push(getSourceCodeResponse);
|
|
31
33
|
|
|
32
34
|
if (vision) {
|
|
33
35
|
prompt.slice(-1)[0].text = messages.suggestImageAssets;
|
|
34
36
|
prompt.push(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
{
|
|
38
|
-
type: 'user',
|
|
39
|
-
functionResponses: [{ name: 'getImageAssets', content: messages.imageAssets }],
|
|
40
|
-
},
|
|
41
|
-
],
|
|
37
|
+
{ type: 'assistant', text: messages.requestImageAssets, functionCalls: [{ name: 'getImageAssets' }] },
|
|
38
|
+
{ type: 'user', functionResponses: [{ name: 'getImageAssets', content: messages.imageAssets }] },
|
|
42
39
|
);
|
|
43
40
|
}
|
|
44
41
|
|
|
45
42
|
prompt.slice(-1)[0].text = messages.prompt;
|
|
46
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
|
+
|
|
47
80
|
let baseRequest = [prompt, functionDefs, 'codegenSummary', temperature, cheap];
|
|
48
81
|
let baseResult = await generateContentFn(...baseRequest);
|
|
49
82
|
|
|
@@ -78,6 +111,7 @@ export async function promptService(generateContentFn, generateImageFn, codegenP
|
|
|
78
111
|
prompt.push({
|
|
79
112
|
type: 'user',
|
|
80
113
|
functionResponses: baseResult.map((call) => ({ name: call.name, call_id: call.id })),
|
|
114
|
+
cache: true,
|
|
81
115
|
});
|
|
82
116
|
|
|
83
117
|
const result = [];
|
|
@@ -285,3 +319,17 @@ function prepareMessages(prompt) {
|
|
|
285
319
|
'Function call was invalid, please analyze the error and respond with corrected function call.',
|
|
286
320
|
};
|
|
287
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
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CODEGEN_TRIGGER } from './prompt-consts.js';
|
|
2
|
-
import { verbosePrompt } from '../cli/cli-params.js';
|
|
2
|
+
import { verbosePrompt, askQuestion } from '../cli/cli-params.js';
|
|
3
3
|
import { verifySystemPromptLimit } from './limits.js';
|
|
4
4
|
import { rcConfig } from '../main/config.js';
|
|
5
5
|
|
|
@@ -21,6 +21,13 @@ export function getSystemPrompt() {
|
|
|
21
21
|
When suggesting changes always use absolute file paths.
|
|
22
22
|
`;
|
|
23
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
|
+
|
|
24
31
|
if (verbosePrompt) {
|
|
25
32
|
console.log('System prompt:');
|
|
26
33
|
console.log(systemPrompt);
|