|
| 1 | +import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider' |
| 2 | +import * as vscode from 'vscode' |
| 3 | +import { build_prompt_payload } from './utils/build-prompt-payload' |
| 4 | +import { |
| 5 | + EDIT_FORMAT_INSTRUCTIONS_WHOLE, |
| 6 | + EDIT_FORMAT_INSTRUCTIONS_TRUNCATED, |
| 7 | + EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE, |
| 8 | + EDIT_FORMAT_INSTRUCTIONS_DIFF |
| 9 | +} from '@/constants/edit-format-instructions' |
| 10 | +import { PromptBuilder } from '@/utils/prompt-builder' |
| 11 | +import * as os from 'os' |
| 12 | +import * as path from 'path' |
| 13 | +import * as fs from 'fs' |
| 14 | +import * as crypto from 'crypto' |
| 15 | + |
| 16 | +export const handle_preview_prompt = async (params: { |
| 17 | + prompt_view_provider: PromptViewProvider |
| 18 | +}): Promise<void> => { |
| 19 | + const { |
| 20 | + other_files, |
| 21 | + recent_files, |
| 22 | + processed_instructions, |
| 23 | + skill_definitions |
| 24 | + } = await build_prompt_payload({ |
| 25 | + prompt_view_provider: params.prompt_view_provider, |
| 26 | + remove_images: true |
| 27 | + }) |
| 28 | + |
| 29 | + let formatted_system_instructions = '' |
| 30 | + const user_instructions = processed_instructions |
| 31 | + |
| 32 | + if (params.prompt_view_provider.prompt_type == 'edit-files') { |
| 33 | + const edit_format = params.prompt_view_provider.edit_format |
| 34 | + const edit_format_instructions = { |
| 35 | + whole: EDIT_FORMAT_INSTRUCTIONS_WHOLE, |
| 36 | + truncated: EDIT_FORMAT_INSTRUCTIONS_TRUNCATED, |
| 37 | + 'search-replace': EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE, |
| 38 | + diff: EDIT_FORMAT_INSTRUCTIONS_DIFF |
| 39 | + }[edit_format] |
| 40 | + if (edit_format_instructions) { |
| 41 | + formatted_system_instructions = `# Output formatting\n\n${edit_format_instructions}` |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + const { full_prompt: text } = PromptBuilder.build_prompt({ |
| 46 | + other_files, |
| 47 | + recent_files, |
| 48 | + skill_definitions, |
| 49 | + system_instructions: formatted_system_instructions, |
| 50 | + user_instructions, |
| 51 | + separator: true |
| 52 | + }) |
| 53 | + |
| 54 | + const hash = crypto.createHash('md5').update(`${Date.now()}`).digest('hex') |
| 55 | + const temp_file_path = path.join(os.tmpdir(), `cwc-prompt-${hash}.md`) |
| 56 | + |
| 57 | + try { |
| 58 | + await fs.promises.writeFile(temp_file_path, text, 'utf8') |
| 59 | + } catch (error) { |
| 60 | + vscode.window.showErrorMessage( |
| 61 | + 'Failed to create temporary file for preview.' |
| 62 | + ) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const document = await vscode.workspace.openTextDocument( |
| 68 | + vscode.Uri.file(temp_file_path) |
| 69 | + ) |
| 70 | + await vscode.window.showTextDocument(document, { preview: false }) |
| 71 | + } catch (error: any) { |
| 72 | + vscode.window.showErrorMessage( |
| 73 | + `Failed to open view: ${error.message || 'Unknown error'}` |
| 74 | + ) |
| 75 | + } |
| 76 | +} |
0 commit comments