Skip to content

Commit b7d5a9e

Browse files
committed
Refactor the apply chat response command to accept response text directly as an argument, deprecating clipboard reliance
1 parent 0fa7390 commit b7d5a9e

5 files changed

Lines changed: 74 additions & 68 deletions

File tree

packages/vscode/src/commands/apply-chat-response-command/apply-chat-response-command.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode'
22
import * as fs from 'fs'
33
import {
4-
parse_clipboard_content,
4+
parse_response,
55
ClipboardFile
66
} from './utils/clipboard-parser'
77
import { LAST_APPLIED_CHANGES_STATE_KEY } from '../../constants/state-keys'
@@ -274,11 +274,17 @@ async function get_file_refactoring_config(
274274
export function apply_chat_response_command(context: vscode.ExtensionContext) {
275275
return vscode.commands.registerCommand(
276276
'codeWebChat.applyChatResponse',
277-
async () => {
278-
const clipboard_text = await vscode.env.clipboard.readText()
277+
async (args?: { response?: string }) => {
278+
let response = args?.response
279279

280-
if (!clipboard_text) {
281-
vscode.window.showErrorMessage('Clipboard is empty.')
280+
if (!response) {
281+
response = await vscode.env.clipboard.readText()
282+
}
283+
284+
if (!response) {
285+
vscode.window.showErrorMessage(
286+
'No response text provided and clipboard is empty.'
287+
)
282288
Logger.warn({
283289
function_name: 'apply_chat_response_command',
284290
message: 'Clipboard is empty.'
@@ -291,8 +297,8 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
291297
vscode.workspace.workspaceFolders?.length == 1
292298

293299
// Parse clipboard content which can now contain either files or patches
294-
const clipboard_content = parse_clipboard_content(
295-
clipboard_text,
300+
const clipboard_content = parse_response(
301+
response,
296302
is_single_root_folder_workspace
297303
)
298304

@@ -398,7 +404,7 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
398404
endpoint_url,
399405
api_key: provider.api_key,
400406
model: file_refactoring_config.model,
401-
clipboard_text: failed_patches_as_code_blocks,
407+
response: failed_patches_as_code_blocks,
402408
context: context,
403409
is_single_root_folder_workspace
404410
})
@@ -512,7 +518,7 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
512518
endpoint_url,
513519
api_key: provider.api_key,
514520
model: file_refactoring_config.model,
515-
clipboard_text: all_patches_text,
521+
response: all_patches_text,
516522
context: context,
517523
is_single_root_folder_workspace
518524
}
@@ -645,7 +651,7 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
645651
endpoint_url,
646652
api_key: provider.api_key,
647653
model: file_refactoring_config.model,
648-
clipboard_text,
654+
response,
649655
context: context,
650656
is_single_root_folder_workspace
651657
})
@@ -753,7 +759,7 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
753759
endpoint_url,
754760
api_key: provider.api_key,
755761
model: file_refactoring_config.model,
756-
clipboard_text,
762+
response,
757763
context: context,
758764
is_single_root_folder_workspace
759765
})

packages/vscode/src/commands/apply-chat-response-command/handlers/intelligent-update-handler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { cleanup_api_response } from '../../../helpers/cleanup-api-response'
77
import { get_refactoring_instruction } from '../../../constants/instructions'
88
import {
99
ClipboardFile,
10-
parse_clipboard_multiple_files
10+
parse_multiple_files
1111
} from '../utils/clipboard-parser'
1212
import {
1313
sanitize_file_name,
@@ -144,7 +144,7 @@ export async function handle_intelligent_update(params: {
144144
endpoint_url: string
145145
api_key: string
146146
model: string
147-
clipboard_text: string
147+
response: string
148148
context: vscode.ExtensionContext
149149
is_single_root_folder_workspace: boolean
150150
}): Promise<OriginalFileState[] | null> {
@@ -173,8 +173,8 @@ export async function handle_intelligent_update(params: {
173173
message: 'Processing multiple files'
174174
})
175175
// Handle multiple files with AI processing ('Intelligent update' mode)
176-
const raw_files = parse_clipboard_multiple_files({
177-
clipboard_text: params.clipboard_text,
176+
const raw_files = parse_multiple_files({
177+
response: params.response,
178178
is_single_root_folder_workspace: params.is_single_root_folder_workspace
179179
})
180180

packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2-
parse_clipboard_multiple_files,
2+
parse_multiple_files,
33
parse_file_content_only,
4-
parse_clipboard_content
4+
parse_response
55
} from './clipboard-parser'
66
import * as fs from 'fs'
77
import * as path from 'path'
@@ -17,8 +17,8 @@ describe('clipboard-parser', () => {
1717
describe('parse_clipboard_multiple_files', () => {
1818
it('should parse comment filename format', () => {
1919
const text = load_clipboard_text('comment-filename.txt')
20-
const result = parse_clipboard_multiple_files({
21-
clipboard_text: text,
20+
const result = parse_multiple_files({
21+
response: text,
2222
is_single_root_folder_workspace: true
2323
})
2424

@@ -31,8 +31,8 @@ describe('clipboard-parser', () => {
3131

3232
it('should parse file-xml format', () => {
3333
const text = load_clipboard_text('file-xml.txt')
34-
const result = parse_clipboard_multiple_files({
35-
clipboard_text: text,
34+
const result = parse_multiple_files({
35+
response: text,
3636
is_single_root_folder_workspace: true
3737
})
3838

@@ -43,8 +43,8 @@ describe('clipboard-parser', () => {
4343

4444
it('should parse file-xml format with CDATA', () => {
4545
const text = load_clipboard_text('file-xml-with-cdata.txt')
46-
const result = parse_clipboard_multiple_files({
47-
clipboard_text: text,
46+
const result = parse_multiple_files({
47+
response: text,
4848
is_single_root_folder_workspace: true
4949
})
5050

@@ -55,8 +55,8 @@ describe('clipboard-parser', () => {
5555

5656
it('should parse html comment filename format', () => {
5757
const text = load_clipboard_text('html-comment-style.txt')
58-
const result = parse_clipboard_multiple_files({
59-
clipboard_text: text,
58+
const result = parse_multiple_files({
59+
response: text,
6060
is_single_root_folder_workspace: true
6161
})
6262

@@ -67,8 +67,8 @@ describe('clipboard-parser', () => {
6767

6868
it('should handle workspace prefixes', () => {
6969
const text = load_clipboard_text('with-workspace-prefix.txt')
70-
const result = parse_clipboard_multiple_files({
71-
clipboard_text: text,
70+
const result = parse_multiple_files({
71+
response: text,
7272
is_single_root_folder_workspace: false
7373
})
7474

@@ -80,8 +80,8 @@ describe('clipboard-parser', () => {
8080

8181
it('should ignore workspace prefixes when has_single_root=true', () => {
8282
const text = load_clipboard_text('with-workspace-prefix.txt')
83-
const result = parse_clipboard_multiple_files({
84-
clipboard_text: text,
83+
const result = parse_multiple_files({
84+
response: text,
8585
is_single_root_folder_workspace: true
8686
})
8787

@@ -92,8 +92,8 @@ describe('clipboard-parser', () => {
9292

9393
it('should merge content for duplicate files', () => {
9494
const text = load_clipboard_text('duplicate-files.txt')
95-
const result = parse_clipboard_multiple_files({
96-
clipboard_text: text,
95+
const result = parse_multiple_files({
96+
response: text,
9797
is_single_root_folder_workspace: true
9898
})
9999

@@ -104,8 +104,8 @@ describe('clipboard-parser', () => {
104104

105105
it('should ignore files without real code', () => {
106106
const text = load_clipboard_text('empty-file.txt')
107-
const result = parse_clipboard_multiple_files({
108-
clipboard_text: text,
107+
const result = parse_multiple_files({
108+
response: text,
109109
is_single_root_folder_workspace: true
110110
})
111111

@@ -123,8 +123,8 @@ describe('clipboard-parser', () => {
123123

124124
it('should parse paths with backslashes', () => {
125125
const text = load_clipboard_text('backslash-paths.txt')
126-
const result = parse_clipboard_multiple_files({
127-
clipboard_text: text,
126+
const result = parse_multiple_files({
127+
response: text,
128128
is_single_root_folder_workspace: true
129129
})
130130

@@ -140,7 +140,7 @@ describe('clipboard-parser', () => {
140140
it('should parse file content without code blocks', () => {
141141
const text = load_clipboard_text('file-content-only.txt')
142142
const result = parse_file_content_only({
143-
clipboard_text: text,
143+
response: text,
144144
is_single_root_folder_workspace: true
145145
})
146146

@@ -154,7 +154,7 @@ describe('clipboard-parser', () => {
154154
it('should return null for invalid file content format', () => {
155155
const text = 'This is just regular text without a file path'
156156
const result = parse_file_content_only({
157-
clipboard_text: text,
157+
response: text,
158158
is_single_root_folder_workspace: true
159159
})
160160

@@ -165,7 +165,7 @@ describe('clipboard-parser', () => {
165165
describe('parse_clipboard_content', () => {
166166
it('should parse direct diff format in variant a', () => {
167167
const text = load_clipboard_text('diff-direct-variant-a.txt')
168-
const result = parse_clipboard_content(text, true)
168+
const result = parse_response(text, true)
169169

170170
expect(result.type).toBe('patches')
171171
expect(result.patches).toHaveLength(1)
@@ -181,7 +181,7 @@ describe('clipboard-parser', () => {
181181

182182
it('should parse direct diff format in variant b', () => {
183183
const text = load_clipboard_text('diff-direct-variant-b.txt')
184-
const result = parse_clipboard_content(text, true)
184+
const result = parse_response(text, true)
185185

186186
expect(result.type).toBe('patches')
187187
expect(result.patches).toHaveLength(1)
@@ -197,7 +197,7 @@ describe('clipboard-parser', () => {
197197

198198
it('should parse direct diff format in variant c', () => {
199199
const text = load_clipboard_text('diff-direct-variant-c.txt')
200-
const result = parse_clipboard_content(text, true)
200+
const result = parse_response(text, true)
201201

202202
expect(result.type).toBe('patches')
203203
expect(result.patches).toHaveLength(1)
@@ -213,7 +213,7 @@ describe('clipboard-parser', () => {
213213

214214
it('should parse direct diff format in variant d', () => {
215215
const text = load_clipboard_text('diff-direct-variant-d.txt')
216-
const result = parse_clipboard_content(text, true)
216+
const result = parse_response(text, true)
217217

218218
expect(result.type).toBe('patches')
219219
expect(result.patches).toHaveLength(1)
@@ -229,7 +229,7 @@ describe('clipboard-parser', () => {
229229

230230
it('should parse direct diff format in variant e', () => {
231231
const text = load_clipboard_text('diff-direct-variant-e.txt')
232-
const result = parse_clipboard_content(text, true)
232+
const result = parse_response(text, true)
233233

234234
expect(result.type).toBe('patches')
235235
expect(result.patches).toHaveLength(1)
@@ -245,7 +245,7 @@ describe('clipboard-parser', () => {
245245

246246
it('should parse direct diff format in variant f', () => {
247247
const text = load_clipboard_text('diff-direct-variant-f.txt')
248-
const result = parse_clipboard_content(text, true)
248+
const result = parse_response(text, true)
249249

250250
expect(result.type).toBe('patches')
251251
expect(result.patches).toHaveLength(1)
@@ -261,7 +261,7 @@ describe('clipboard-parser', () => {
261261

262262
it('should parse multiple diff files format in variant a', () => {
263263
const text = load_clipboard_text('diff-multiple-files-variant-a.txt')
264-
const result = parse_clipboard_content(text, true)
264+
const result = parse_response(text, true)
265265

266266
expect(result.type).toBe('patches')
267267
expect(result.patches).toHaveLength(2)
@@ -287,7 +287,7 @@ describe('clipboard-parser', () => {
287287

288288
it('should parse multiple diff files format in variant b', () => {
289289
const text = load_clipboard_text('diff-multiple-files-variant-b.txt')
290-
const result = parse_clipboard_content(text, true)
290+
const result = parse_response(text, true)
291291

292292
expect(result.type).toBe('patches')
293293
expect(result.patches).toHaveLength(2)
@@ -313,7 +313,7 @@ describe('clipboard-parser', () => {
313313

314314
it('should parse multiple diff files format in variant c', () => {
315315
const text = load_clipboard_text('diff-multiple-files-variant-c.txt')
316-
const result = parse_clipboard_content(text, true)
316+
const result = parse_response(text, true)
317317

318318
expect(result.type).toBe('patches')
319319
expect(result.patches).toHaveLength(2)
@@ -339,7 +339,7 @@ describe('clipboard-parser', () => {
339339

340340
it('should parse multiple diff files format in variant d', () => {
341341
const text = load_clipboard_text('diff-multiple-files-variant-d.txt')
342-
const result = parse_clipboard_content(text, true)
342+
const result = parse_response(text, true)
343343

344344
expect(result.type).toBe('patches')
345345
expect(result.patches).toHaveLength(2)
@@ -365,7 +365,7 @@ describe('clipboard-parser', () => {
365365

366366
it('should parse multiple diff files format in variant e', () => {
367367
const text = load_clipboard_text('diff-multiple-files-variant-e.txt')
368-
const result = parse_clipboard_content(text, true)
368+
const result = parse_response(text, true)
369369

370370
expect(result.type).toBe('patches')
371371
expect(result.patches).toHaveLength(2)
@@ -391,7 +391,7 @@ describe('clipboard-parser', () => {
391391

392392
it('should parse multiple diff files format in variant f', () => {
393393
const text = load_clipboard_text('diff-multiple-files-variant-f.txt')
394-
const result = parse_clipboard_content(text, true)
394+
const result = parse_response(text, true)
395395

396396
expect(result.type).toBe('patches')
397397
expect(result.patches).toHaveLength(2)
@@ -417,7 +417,7 @@ describe('clipboard-parser', () => {
417417

418418
it('should parse multiple diff files format in variant g', () => {
419419
const text = load_clipboard_text('diff-multiple-files-variant-g.txt')
420-
const result = parse_clipboard_content(text, true)
420+
const result = parse_response(text, true)
421421

422422
expect(result.type).toBe('patches')
423423
expect(result.patches).toHaveLength(2)
@@ -443,7 +443,7 @@ describe('clipboard-parser', () => {
443443

444444
it('should parse multiple diff files format in variant h', () => {
445445
const text = load_clipboard_text('diff-multiple-files-variant-h.txt')
446-
const result = parse_clipboard_content(text, true)
446+
const result = parse_response(text, true)
447447

448448
expect(result.type).toBe('patches')
449449
expect(result.patches).toHaveLength(2)

0 commit comments

Comments
 (0)