Skip to content

Commit e94bd8b

Browse files
committed
Include active file context in voice input requests
- during recording, watch active editors. Every one opened should be included in the constructed prompt. Dont use prompt builder. - in `apps/editor/src/views/prompt/backend/message-handlers/handle-voice-input.ts`, when including some files, to cli add 'preloaded_context' requirement - when attaching it, make sure it's first in the list - [Changes] opened editors tracking should be inside the handler └── apps └── editor └── src ├── constants │ └── instructions.ts ├── i18n │ └── translations │ └── views │ └── prompt │ └── handlers │ └── handle-voice-input.ts ├── utils │ └── prompt-builder.ts └── views └── prompt └── backend └── message-handlers ├── handle-autofill.ts ├── handle-voice-input.ts └── utils └── build-prompt-payload.ts
1 parent ae8abfc commit e94bd8b

1 file changed

Lines changed: 80 additions & 2 deletions

File tree

‎apps/editor/src/views/prompt/backend/message-handlers/handle-voice-input.ts‎

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,39 @@ import { invoke_agentic_cli } from '@/utils/agentic-cli-invocation'
2828

2929
const MIN_RECORDING_DURATION = 1000
3030

31+
const voice_input_tracked_files = new Map<string, string>()
32+
let active_editor_listener: vscode.Disposable | null = null
33+
3134
const start_recording = (prompt_view_provider: PromptViewProvider) => {
3235
prompt_view_provider.audio_chunks = []
3336
prompt_view_provider.recording_start_time = Date.now()
37+
voice_input_tracked_files.clear()
38+
39+
const active_editor = vscode.window.activeTextEditor
40+
if (active_editor && prompt_view_provider.currently_open_file_path) {
41+
voice_input_tracked_files.set(
42+
active_editor.document.uri.fsPath,
43+
prompt_view_provider.currently_open_file_path
44+
)
45+
}
46+
47+
active_editor_listener = vscode.window.onDidChangeActiveTextEditor(
48+
(editor) => {
49+
setTimeout(() => {
50+
if (
51+
editor &&
52+
editor === vscode.window.activeTextEditor &&
53+
prompt_view_provider.currently_open_file_path
54+
) {
55+
voice_input_tracked_files.set(
56+
editor.document.uri.fsPath,
57+
prompt_view_provider.currently_open_file_path
58+
)
59+
}
60+
}, 150)
61+
}
62+
)
63+
3464
try {
3565
prompt_view_provider.recording_process = spawn('rec', [
3666
'-q',
@@ -98,17 +128,32 @@ const start_recording = (prompt_view_provider: PromptViewProvider) => {
98128
is_recording: false
99129
})
100130
prompt_view_provider.recording_process = null
131+
132+
if (active_editor_listener) {
133+
active_editor_listener.dispose()
134+
active_editor_listener = null
135+
}
101136
})
102137
} catch (error) {
103138
Logger.error({
104139
function_name: 'start_recording',
105140
message: 'Failed to start recording',
106141
data: { error }
107142
})
143+
144+
if (active_editor_listener) {
145+
active_editor_listener.dispose()
146+
active_editor_listener = null
147+
}
108148
}
109149
}
110150

111151
const stop_recording = async (prompt_view_provider: PromptViewProvider) => {
152+
if (active_editor_listener) {
153+
active_editor_listener.dispose()
154+
active_editor_listener = null
155+
}
156+
112157
if (prompt_view_provider.recording_process) {
113158
prompt_view_provider.recording_process.kill()
114159
prompt_view_provider.recording_process = null
@@ -211,14 +256,47 @@ const stop_recording = async (prompt_view_provider: PromptViewProvider) => {
211256

212257
show_action_quick_pick = false
213258

259+
let files_context = ''
260+
if (voice_input_tracked_files.size > 0) {
261+
files_context = '# Files\n\n'
262+
for (const [
263+
file_path,
264+
display_path
265+
] of voice_input_tracked_files.entries()) {
266+
try {
267+
const document = await vscode.workspace.openTextDocument(
268+
vscode.Uri.file(file_path)
269+
)
270+
const content = document.getText()
271+
const backticks = content.includes('```') ? '````' : '```'
272+
files_context += `### File: \`${display_path}\`\n\n${backticks}\n${content}\n${backticks}\n\n`
273+
} catch (error) {
274+
Logger.warn({
275+
function_name: 'stop_recording',
276+
message: 'Failed to read tracked file',
277+
data: { file_path, error }
278+
})
279+
}
280+
}
281+
}
282+
214283
if (current_action == 'invoke-agent') {
215284
const temp_audio_path = path.join(
216285
os.tmpdir(),
217286
`cwc-voice-input-${Date.now()}.wav`
218287
)
219288
await fs.promises.writeFile(temp_audio_path, audio_buffer)
220289

221-
const cli_prompt = `# Task\n\n${voice_input_task_instructions}\n\nAudio file: \`${temp_audio_path.replace(/\\/g, '/')}\`\n\n# Output formatting\n\n${voice_input_output_formatting}\n\n# Requirements\n\n- ${cli_edit_ask_requirements.disable_tool_calling}\n- ${cli_edit_ask_requirements.exception_read_audio}`
290+
const requirements = [
291+
cli_edit_ask_requirements.disable_tool_calling,
292+
cli_edit_ask_requirements.exception_read_audio
293+
]
294+
if (files_context) {
295+
requirements.unshift(cli_edit_ask_requirements.preloaded_context)
296+
}
297+
const req_str = requirements.map((r) => `- ${r}`).join('\n')
298+
299+
const cli_prompt = `${files_context}# Task\n\n${voice_input_task_instructions}\n\nAudio file: \`${temp_audio_path.replace(/\\/g, '/')}\`\n\n# Output formatting\n\n${voice_input_output_formatting}\n\n# Requirements\n\n${req_str}`
222300

223301
const invoke_cli_result = await invoke_agentic_cli({
224302
workspace_provider: prompt_view_provider.workspace_provider,
@@ -302,7 +380,7 @@ const stop_recording = async (prompt_view_provider: PromptViewProvider) => {
302380
content: [
303381
{
304382
type: 'text',
305-
text: `# Task\n\n${voice_input_task_instructions}\n\n# Output formatting\n\n${voice_input_output_formatting}`
383+
text: `${files_context}# Task\n\n${voice_input_task_instructions}\n\n# Output formatting\n\n${voice_input_output_formatting}`
306384
},
307385
{
308386
type: 'input_audio',

0 commit comments

Comments
 (0)