|
1 | | -export const build_file_context = (params: { |
2 | | - filepath: string |
3 | | - content?: string |
4 | | - is_binary?: boolean |
5 | | -}): string => { |
6 | | - const display_path = params.filepath.replace(/\\/g, '/') |
7 | | - if (params.is_binary || params.content === undefined) { |
8 | | - return `### File: \`${display_path}\`\n\nBinary file\n\n` |
| 1 | +export namespace PromptBuilder { |
| 2 | + export const build_file_context = (params: { |
| 3 | + filepath: string |
| 4 | + content?: string |
| 5 | + is_binary?: boolean |
| 6 | + }): string => { |
| 7 | + const display_path = params.filepath.replace(/\\/g, '/') |
| 8 | + if (params.is_binary || params.content === undefined) { |
| 9 | + return `### File: \`${display_path}\`\n\nBinary file\n\n` |
| 10 | + } |
| 11 | + return `### File: \`${display_path}\`\n\n\`\`\`\n${params.content}\n\`\`\`\n\n` |
9 | 12 | } |
10 | | - return `### File: \`${display_path}\`\n\n\`\`\`\n${params.content}\n\`\`\`\n\n` |
11 | | -} |
12 | 13 |
|
13 | | -export const build_prompt = (params: { |
14 | | - other_files?: string |
15 | | - recent_files?: string |
16 | | - context_text?: string |
17 | | - active_file?: { filepath: string; content: string } |
18 | | - skill_definitions?: string |
19 | | - system_instructions?: string |
20 | | - user_instructions?: string |
21 | | - separator?: boolean |
22 | | -}): { part1: string; part2: string; full_prompt: string } => { |
23 | | - let part1 = '' |
24 | | - let part2 = '' |
25 | | - let full_prompt = '' |
26 | | - |
27 | | - let active_file_context = '' |
28 | | - if (params.active_file) { |
29 | | - active_file_context = build_file_context(params.active_file) |
30 | | - } |
| 14 | + export const build_prompt = (params: { |
| 15 | + other_files?: string |
| 16 | + recent_files?: string |
| 17 | + context_text?: string |
| 18 | + active_file?: { filepath: string; content: string } |
| 19 | + skill_definitions?: string |
| 20 | + system_instructions?: string |
| 21 | + user_instructions?: string |
| 22 | + separator?: boolean |
| 23 | + }): { part1: string; part2: string; full_prompt: string } => { |
| 24 | + let part1 = '' |
| 25 | + let part2 = '' |
| 26 | + let full_prompt = '' |
31 | 27 |
|
32 | | - if (params.context_text !== undefined) { |
33 | | - if (params.context_text) { |
34 | | - full_prompt += `# Files\n\n${params.context_text}` |
| 28 | + let active_file_context = '' |
| 29 | + if (params.active_file) { |
| 30 | + active_file_context = build_file_context(params.active_file) |
35 | 31 | } |
36 | | - if (active_file_context) { |
37 | | - full_prompt += active_file_context |
| 32 | + |
| 33 | + if (params.context_text !== undefined) { |
| 34 | + if (params.context_text) { |
| 35 | + full_prompt += `# Files\n\n${params.context_text}` |
| 36 | + } |
| 37 | + if (active_file_context) { |
| 38 | + full_prompt += active_file_context |
| 39 | + } |
| 40 | + } else { |
| 41 | + part1 = `# Files\n\n${params.other_files || ''}` |
| 42 | + part2 += `${params.recent_files || ''}${active_file_context}` |
| 43 | + full_prompt = part1 + part2 |
38 | 44 | } |
39 | | - } else { |
40 | | - part1 = `# Files\n\n${params.other_files || ''}` |
41 | | - part2 += `${params.recent_files || ''}${active_file_context}` |
42 | | - full_prompt = part1 + part2 |
43 | | - } |
44 | 45 |
|
45 | | - if (params.skill_definitions) { |
46 | | - part2 += params.skill_definitions |
47 | | - full_prompt += params.skill_definitions |
48 | | - } |
| 46 | + if (params.skill_definitions) { |
| 47 | + part2 += params.skill_definitions |
| 48 | + full_prompt += params.skill_definitions |
| 49 | + } |
49 | 50 |
|
50 | | - const has_system = !!params.system_instructions |
51 | | - const has_user = !!params.user_instructions |
52 | | - const separator = params.separator !== false && has_system && has_user |
| 51 | + const has_system = !!params.system_instructions |
| 52 | + const has_user = !!params.user_instructions |
| 53 | + const separator = params.separator !== false && has_system && has_user |
53 | 54 |
|
54 | | - if (params.system_instructions) { |
55 | | - const sys = params.system_instructions.trimEnd() |
56 | | - if (sys) { |
57 | | - part2 += `${sys}\n` |
58 | | - full_prompt += `${sys}\n` |
59 | | - if (separator) { |
60 | | - part2 += `\n---\n\n` |
61 | | - full_prompt += `\n---\n\n` |
62 | | - } else { |
63 | | - part2 += `\n\n` |
64 | | - full_prompt += `\n\n` |
| 55 | + if (params.system_instructions) { |
| 56 | + const sys = params.system_instructions.trimEnd() |
| 57 | + if (sys) { |
| 58 | + part2 += `${sys}\n` |
| 59 | + full_prompt += `${sys}\n` |
| 60 | + if (separator) { |
| 61 | + part2 += `\n---\n\n` |
| 62 | + full_prompt += `\n---\n\n` |
| 63 | + } else { |
| 64 | + part2 += `\n\n` |
| 65 | + full_prompt += `\n\n` |
| 66 | + } |
65 | 67 | } |
66 | 68 | } |
67 | | - } |
68 | 69 |
|
69 | | - if (params.user_instructions) { |
70 | | - part2 += params.user_instructions |
71 | | - full_prompt += params.user_instructions |
72 | | - } |
| 70 | + if (params.user_instructions) { |
| 71 | + part2 += params.user_instructions |
| 72 | + full_prompt += params.user_instructions |
| 73 | + } |
73 | 74 |
|
74 | | - return { part1, part2, full_prompt } |
| 75 | + return { part1, part2, full_prompt } |
| 76 | + } |
75 | 77 | } |
0 commit comments