Skip to content

Commit f80ba78

Browse files
committed
Improve LLM response tag parsing using robust substring extraction
- check robustness of parsing long 'replacement' blocks └── apps └── editor └── src ├── commands │ └── code-at-cursor-command │ ├── code-at-cursor-commands.ts │ └── utils │ ├── perform-code-at-cursor.ts │ └── show-ghost-text.ts └── utils ├── changes-integration │ └── search-replace-processor │ └── apply-search-replace-to-content │ └── apply-search-replace-to-content.ts └── send-llm-message.ts
1 parent ab55d73 commit f80ba78

2 files changed

Lines changed: 41 additions & 25 deletions

File tree

‎apps/editor/src/commands/code-at-cursor-command/utils/perform-code-at-cursor.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,22 @@ export const perform_code_at_cursor = async (params: {
210210
)
211211

212212
if (completion_result) {
213-
const match = completion_result.response.match(
214-
/<replacement>([\s\S]*?)<\/replacement>/i
215-
)
216-
if (match && match[1]) {
217-
let decoded_completion = he.decode(match[1].trim())
213+
const response_text = completion_result.response
214+
const start_match = response_text.match(/<replacement>/i)
215+
216+
if (start_match) {
217+
const content_start = start_match.index! + start_match[0].length
218+
const remaining_text = response_text.substring(content_start)
219+
const end_match = remaining_text.match(/<\/replacement>/i)
220+
221+
let extracted_content = ''
222+
if (end_match) {
223+
extracted_content = remaining_text.substring(0, end_match.index)
224+
} else {
225+
extracted_content = remaining_text
226+
}
227+
228+
let decoded_completion = he.decode(extracted_content.trim())
218229

219230
if (decoded_completion.startsWith('```')) {
220231
const first_newline = decoded_completion.indexOf('\n')

‎apps/editor/src/utils/send-llm-message.ts‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -336,28 +336,33 @@ export const send_llm_message = async (params: {
336336
}
337337
}
338338

339-
const thoughts_match = full_response.match(
340-
/<(?:think|thought)>([\s\S]*?)<\/(?:think|thought)>/
341-
)
342-
let thoughts = thoughts_match ? thoughts_match[1].trim() : undefined
339+
let thoughts: string | undefined = undefined
343340
let final_content = content_for_client
344341

345-
if (!thoughts_match) {
346-
const end_match = full_response.match(/<\/(?:think|thought)>/)
347-
if (end_match) {
348-
const start_match = full_response.match(/<(?:think|thought)>/)
349-
if (!start_match) {
350-
thoughts = full_response.substring(0, end_match.index).trim()
351-
final_content = full_response
352-
.substring(end_match.index! + end_match[0].length)
353-
.trim()
354-
Logger.info({
355-
function_name: 'send_llm_message',
356-
message:
357-
'Detected closing tag without opening tag, stripped content before '
358-
})
359-
}
360-
}
342+
const start_match = full_response.match(/<(?:think|thought)>/)
343+
const end_match = full_response.match(/<\/(?:think|thought)>/)
344+
345+
if (start_match && end_match && end_match.index! > start_match.index!) {
346+
const content_start = start_match.index! + start_match[0].length
347+
thoughts = full_response
348+
.substring(content_start, end_match.index!)
349+
.trim()
350+
} else if (start_match && !end_match) {
351+
const content_start = start_match.index! + start_match[0].length
352+
thoughts = full_response.substring(content_start).trim()
353+
} else if (
354+
end_match &&
355+
(!start_match || start_match.index! > end_match.index!)
356+
) {
357+
thoughts = full_response.substring(0, end_match.index!).trim()
358+
final_content = full_response
359+
.substring(end_match.index! + end_match[0].length)
360+
.trim()
361+
Logger.info({
362+
function_name: 'send_llm_message',
363+
message:
364+
'Detected closing tag without opening tag (or before opening tag), stripped content before'
365+
})
361366
}
362367

363368
Logger.info({

0 commit comments

Comments
 (0)