Skip to content

Commit fb2399b

Browse files
committed
Update chat response processing and clipboard parsing to support handling mixed content types
- 'relevant files' should output text "Lorem ipsum." - in this block [Fragment], the issue is that text items never make it to preview
1 parent 85156eb commit fb2399b

4 files changed

Lines changed: 100 additions & 51 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/response-processor.ts‎

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provi
3232
import { natural_sort } from '@/utils/natural-sort'
3333
import { t } from '@/i18n'
3434
import { is_truncation_line } from './utils/edit-formats/truncations'
35-
import { RelevantFileInPreview } from '@shared/types/file-in-preview'
35+
import {
36+
RelevantFileInPreview,
37+
ItemInPreview
38+
} from '@shared/types/file-in-preview'
3639
import { PreviewDecision } from './utils/preview/types'
3740
import { set_response_preview_promise_resolve } from './utils/preview/preview'
3841

@@ -116,65 +119,88 @@ export const process_chat_response = async (params: {
116119
vscode.workspace.workspaceFolders?.length == 1
117120

118121
if (params.clipboard_items.some((item) => item.type == 'relevant-files')) {
119-
const relevant_files_item = params.clipboard_items.find(
120-
(item) => item.type == 'relevant-files'
121-
) as RelevantFilesItem
122-
123122
const current_checked_files =
124123
params.workspace_provider.get_export_state().regular.checked_files
125124

126125
const workspace_roots = params.workspace_provider.get_workspace_roots()
127-
const all_paths_to_process = new Set<string>(relevant_files_item.file_paths)
128126

129127
const files_for_preview: RelevantFileInPreview[] = []
128+
const items_for_preview: ItemInPreview[] = []
130129

131-
for (const rel_path of Array.from(all_paths_to_process)) {
132-
let absolute_path: string | undefined
133-
let matched_workspace_root: string | undefined
134-
for (const root of workspace_roots) {
135-
const potential = path.join(root, rel_path)
136-
if (fs.existsSync(potential)) {
137-
absolute_path = potential
138-
matched_workspace_root = root
139-
break
140-
}
141-
}
130+
for (const item of params.clipboard_items) {
131+
if (item.type == 'relevant-files') {
132+
const relevant_files_item = item as RelevantFilesItem
133+
const all_paths_to_process = new Set<string>(
134+
relevant_files_item.file_paths
135+
)
142136

143-
if (absolute_path && matched_workspace_root) {
144-
const count =
145-
await params.workspace_provider.calculate_file_tokens(absolute_path)
137+
const local_files: RelevantFileInPreview[] = []
138+
139+
for (const rel_path of Array.from(all_paths_to_process)) {
140+
let absolute_path: string | undefined
141+
let matched_workspace_root: string | undefined
142+
for (const root of workspace_roots) {
143+
const potential = path.join(root, rel_path)
144+
if (fs.existsSync(potential)) {
145+
absolute_path = potential
146+
matched_workspace_root = root
147+
break
148+
}
149+
}
146150

147-
let workspace_name: string | undefined
148-
if (workspace_roots.length > 1) {
149-
workspace_name = params.workspace_provider.get_workspace_name(
150-
matched_workspace_root
151-
)
152-
}
151+
if (absolute_path && matched_workspace_root) {
152+
const count =
153+
await params.workspace_provider.calculate_file_tokens(
154+
absolute_path
155+
)
156+
157+
let workspace_name: string | undefined
158+
if (workspace_roots.length > 1) {
159+
workspace_name = params.workspace_provider.get_workspace_name(
160+
matched_workspace_root
161+
)
162+
}
153163

154-
let is_checked = true
155-
if (params.args?.relevant_files) {
156-
const history_file = params.args.relevant_files.find(
157-
(f) =>
158-
f.file_path === rel_path && f.workspace_name === workspace_name
159-
)
160-
if (history_file) {
161-
is_checked = history_file.is_checked
164+
let is_checked = true
165+
if (params.args?.relevant_files) {
166+
const history_file = params.args.relevant_files.find(
167+
(f) =>
168+
f.file_path === rel_path &&
169+
f.workspace_name === workspace_name
170+
)
171+
if (history_file) {
172+
is_checked = history_file.is_checked
173+
}
174+
}
175+
176+
local_files.push({
177+
type: 'relevant-file',
178+
file_path: rel_path,
179+
absolute_path: absolute_path,
180+
workspace_name,
181+
is_checked,
182+
token_count: count.total
183+
})
162184
}
163185
}
164186

165-
files_for_preview.push({
166-
type: 'relevant-file',
167-
file_path: rel_path,
168-
absolute_path: absolute_path,
169-
workspace_name,
170-
is_checked,
171-
token_count: count.total
187+
local_files.sort((a, b) => natural_sort(a.file_path, b.file_path))
188+
files_for_preview.push(...local_files)
189+
items_for_preview.push(...local_files)
190+
} else if (item.type == 'text') {
191+
items_for_preview.push({
192+
type: 'text',
193+
content: item.content
194+
})
195+
} else if (item.type == 'inline-file') {
196+
items_for_preview.push({
197+
type: 'inline-file',
198+
content: item.content,
199+
language: item.language
172200
})
173201
}
174202
}
175203

176-
files_for_preview.sort((a, b) => natural_sort(a.file_path, b.file_path))
177-
178204
const created_at_for_preview = params.args?.created_at ?? Date.now()
179205

180206
const history = params.panel_provider.response_history
@@ -207,7 +233,7 @@ export const process_chat_response = async (params: {
207233

208234
params.panel_provider.send_message({
209235
command: 'RESPONSE_PREVIEW_STARTED',
210-
items: files_for_preview,
236+
items: items_for_preview,
211237
raw_instructions: params.args?.raw_instructions,
212238
created_at: created_at_for_preview,
213239
url: params.args?.url,

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,11 +1049,15 @@ describe('clipboard-parser', () => {
10491049
is_single_root_folder_workspace: true
10501050
})
10511051

1052-
expect(result).toHaveLength(1)
1052+
expect(result).toHaveLength(2)
10531053
expect(result[0]).toMatchObject({
10541054
type: 'relevant-files',
10551055
file_paths: ['src/hello.ts', 'src/welcome.ts']
10561056
})
1057+
expect(result[1]).toMatchObject({
1058+
type: 'text',
1059+
content: 'Lorem ipsum.'
1060+
})
10571061
})
10581062
})
10591063

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const parse_response = (params: {
9595

9696
const relevant_files = parse_relevant_files({ response: params.response })
9797
if (relevant_files) {
98-
return [relevant_files]
98+
return relevant_files
9999
}
100100

101101
const processed_response = params.response.replace(/``````/g, '```\n```')

‎apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/relevant-files-parser.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { RelevantFilesItem } from '../clipboard-parser'
1+
import { RelevantFilesItem, TextItem } from '../clipboard-parser'
22

33
export const parse_relevant_files = (params: {
44
response: string
5-
}): RelevantFilesItem | null => {
5+
}): (RelevantFilesItem | TextItem)[] | null => {
66
const trimmed_response = params.response.trim()
77
const lines = trimmed_response.split('\n')
88

@@ -15,6 +15,7 @@ export const parse_relevant_files = (params: {
1515
}
1616

1717
const file_paths: string[] = []
18+
let first_non_list_item_index = -1
1819

1920
for (let i = 1; i < lines.length; i++) {
2021
const line = lines[i].trim()
@@ -29,15 +30,33 @@ export const parse_relevant_files = (params: {
2930
file_paths.push(path)
3031
}
3132
} else {
33+
first_non_list_item_index = i
3234
break
3335
}
3436
}
3537

3638
if (file_paths.length > 0) {
37-
return {
38-
type: 'relevant-files',
39-
file_paths
39+
const result: (RelevantFilesItem | TextItem)[] = [
40+
{
41+
type: 'relevant-files',
42+
file_paths
43+
}
44+
]
45+
46+
if (first_non_list_item_index !== -1) {
47+
const remainingText = lines
48+
.slice(first_non_list_item_index)
49+
.join('\n')
50+
.trim()
51+
if (remainingText) {
52+
result.push({
53+
type: 'text',
54+
content: remainingText
55+
})
56+
}
4057
}
58+
59+
return result
4160
}
4261

4362
return null

0 commit comments

Comments
 (0)