Skip to content

Commit 5d12997

Browse files
committed
refactor: move processStreamChunk and processRemainingMetricsBuffer to lib
1 parent 0ef1383 commit 5d12997

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

src/background/handlers/handle-chat-stream.ts

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
1-
import { processStreamChunk } from "@/background/handlers/process-stream-chunk"
1+
import {
2+
processRemainingMetricsBuffer,
3+
processStreamChunk
4+
} from "@/background/lib/process-stream-chunk"
25
import { safePostMessage } from "@/background/lib/utils"
3-
import type {
4-
ChromePort,
5-
OllamaChatResponse,
6-
PortStatusFunction
7-
} from "@/types"
8-
9-
const processRemainingBuffer = (
10-
buffer: string,
11-
fullText: string,
12-
port: ChromePort
13-
): void => {
14-
try {
15-
const data: OllamaChatResponse = JSON.parse(buffer.trim())
16-
if (data.done === true) {
17-
console.log("Final completion from buffer")
18-
safePostMessage(port, {
19-
done: true,
20-
content: fullText,
21-
metrics: {
22-
total_duration: data.total_duration,
23-
load_duration: data.load_duration,
24-
prompt_eval_count: data.prompt_eval_count,
25-
prompt_eval_duration: data.prompt_eval_duration,
26-
eval_count: data.eval_count,
27-
eval_duration: data.eval_duration
28-
}
29-
})
30-
}
31-
} catch (parseError) {
32-
console.warn("Failed to parse final buffer:", buffer, parseError)
33-
}
34-
}
6+
import type { ChromePort, PortStatusFunction } from "@/types"
357

368
export const handleChatStream = async (
379
response: Response,
@@ -55,13 +27,12 @@ export const handleChatStream = async (
5527
let buffer = ""
5628
let hasReceivedData = false
5729

58-
// Add timeout for stuck connections - declare timeoutId in proper scope
5930
let timeoutId: NodeJS.Timeout | null = null
6031

6132
try {
6233
timeoutId = setTimeout(() => {
6334
if (!hasReceivedData) {
64-
console.warn("No data received within 10 seconds, aborting")
35+
console.warn("No data received within 60 seconds, aborting")
6536
reader.cancel().catch(console.error)
6637
safePostMessage(port, {
6738
error: {
@@ -70,7 +41,7 @@ export const handleChatStream = async (
7041
}
7142
})
7243
}
73-
}, 30000) // 30 second timeout
44+
}, 60000)
7445

7546
while (true) {
7647
const { value, done } = await reader.read()
@@ -79,21 +50,18 @@ export const handleChatStream = async (
7950
break
8051
}
8152

82-
// Mark that we've received data
8353
if (!hasReceivedData) {
8454
hasReceivedData = true
8555
if (timeoutId) clearTimeout(timeoutId)
8656
console.log("[Handle chat stream]First data chunk received")
8757
}
8858

89-
// Check if port is still connected before processing
9059
if (isPortClosed()) {
9160
reader.cancel().catch(console.error)
9261
if (timeoutId) clearTimeout(timeoutId)
9362
break
9463
}
9564

96-
// Process the streaming data
9765
const processResult = processStreamChunk(
9866
value,
9967
decoder,
@@ -110,9 +78,8 @@ export const handleChatStream = async (
11078
}
11179
}
11280

113-
// Process any remaining data in buffer
11481
if (buffer.trim() && !isPortClosed()) {
115-
processRemainingBuffer(buffer, fullText, port)
82+
processRemainingMetricsBuffer(buffer, fullText, port)
11683
}
11784
} catch (error) {
11885
console.error("Stream processing error:", error)

src/background/handlers/process-stream-chunk.ts renamed to src/background/lib/process-stream-chunk.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,30 @@ export const processStreamChunk = (
5454

5555
return { buffer, fullText, isDone: false }
5656
}
57+
58+
export const processRemainingMetricsBuffer = (
59+
buffer: string,
60+
fullText: string,
61+
port: ChromePort
62+
): void => {
63+
try {
64+
const data: OllamaChatResponse = JSON.parse(buffer.trim())
65+
if (data.done === true) {
66+
console.log("Final completion from buffer")
67+
safePostMessage(port, {
68+
done: true,
69+
content: fullText,
70+
metrics: {
71+
total_duration: data.total_duration,
72+
load_duration: data.load_duration,
73+
prompt_eval_count: data.prompt_eval_count,
74+
prompt_eval_duration: data.prompt_eval_duration,
75+
eval_count: data.eval_count,
76+
eval_duration: data.eval_duration
77+
}
78+
})
79+
}
80+
} catch (parseError) {
81+
console.warn("Failed to parse final buffer:", buffer, parseError)
82+
}
83+
}

0 commit comments

Comments
 (0)