-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext.js
More file actions
45 lines (37 loc) · 1.16 KB
/
context.js
File metadata and controls
45 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import fs from 'fs';
import path from 'path';
import { memoize } from './utils.js';
import { getCwd } from './persistent_shell.js';
const STYLE_PROMPT =
'The codebase follows strict style guidelines shown below. All code changes must strictly adhere to these guidelines to maintain consistency and quality.';
export const getCodeStyle = memoize(() => {
const styles = [];
let currentDir = getCwd();
while (currentDir !== path.parse(currentDir).root) {
const stylePath = path.join(currentDir, 'KODING.md');
if (fs.existsSync(stylePath)) {
styles.push(
`Contents of ${stylePath}:\n\n${fs.readFileSync(stylePath, 'utf-8')}`
);
}
currentDir = path.dirname(currentDir);
}
if (styles.length === 0) {
return '';
}
return `${STYLE_PROMPT}\n\n${styles.reverse().join('\n\n')}`;
});
export function formatSystemPromptWithContext(
systemPrompt
) {
const context = {
codeStyle: getCodeStyle(),
}
return [
...systemPrompt,
`\nAs you answer the user's questions, you can use the following context:\n`,
...Object.entries(context).map(
([key, value]) => `<context name="${key}">${value}</context>`,
),
]
}