fix: Improve stream error handling and parsing (#5807)
* fix: Enhance stream error handling and parsing This commit improves the robustness of stream processing in the llamacpp-extension. - Adds explicit handling for 'error:' prefixed lines in the stream, parsing the contained JSON error and throwing an appropriate JavaScript Error. - Centralizes JSON parsing of 'data:' and 'error:' lines, ensuring consistent error propagation by re-throwing parsing exceptions. - Ensures the async iterator terminates correctly upon encountering stream errors or malformed JSON. * Address bot comments and cleanup
This commit is contained in:
parent
59ad2eb784
commit
8f1a36c8e3
@ -875,7 +875,6 @@ export default class llamacpp_extension extends AIEngine {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Store the session info for later use
|
// Store the session info for later use
|
||||||
console.log(sInfo)
|
|
||||||
this.activeSessions.set(sInfo.pid, sInfo)
|
this.activeSessions.set(sInfo.pid, sInfo)
|
||||||
await this.waitForModelLoad(sInfo)
|
await this.waitForModelLoad(sInfo)
|
||||||
|
|
||||||
@ -950,6 +949,7 @@ export default class llamacpp_extension extends AIEngine {
|
|||||||
const reader = response.body.getReader()
|
const reader = response.body.getReader()
|
||||||
const decoder = new TextDecoder('utf-8')
|
const decoder = new TextDecoder('utf-8')
|
||||||
let buffer = ''
|
let buffer = ''
|
||||||
|
let jsonStr = ''
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
const { done, value } = await reader.read()
|
const { done, value } = await reader.read()
|
||||||
@ -971,13 +971,23 @@ export default class llamacpp_extension extends AIEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (trimmedLine.startsWith('data: ')) {
|
if (trimmedLine.startsWith('data: ')) {
|
||||||
const jsonStr = trimmedLine.slice(6)
|
jsonStr = trimmedLine.slice(6)
|
||||||
try {
|
} else if (trimmedLine.startsWith('error: ')) {
|
||||||
const chunk = JSON.parse(jsonStr) as chatCompletionChunk
|
jsonStr = trimmedLine.slice(7)
|
||||||
yield chunk
|
const error = JSON.parse(jsonStr)
|
||||||
} catch (e) {
|
throw new Error(error.message)
|
||||||
console.error('Error parsing JSON from stream:', e)
|
} else {
|
||||||
}
|
// it should not normally reach here
|
||||||
|
throw new Error('Malformed chunk')
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(jsonStr)
|
||||||
|
const chunk = data as chatCompletionChunk
|
||||||
|
yield chunk
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error parsing JSON from stream or server error:', e)
|
||||||
|
// re‑throw so the async iterator terminates with an error
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1004,7 +1014,6 @@ export default class llamacpp_extension extends AIEngine {
|
|||||||
const result = await invoke<boolean>('is_process_running', {
|
const result = await invoke<boolean>('is_process_running', {
|
||||||
pid: sessionInfo.pid,
|
pid: sessionInfo.pid,
|
||||||
})
|
})
|
||||||
console.log(`is_process_running result: ${result}`)
|
|
||||||
if (result) {
|
if (result) {
|
||||||
try {
|
try {
|
||||||
await fetch(`http://localhost:${sessionInfo.port}/health`)
|
await fetch(`http://localhost:${sessionInfo.port}/health`)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user