1- import { processStreamChunk } from "@/background/handlers/process-stream-chunk"
1+ import {
2+ processRemainingMetricsBuffer ,
3+ processStreamChunk
4+ } from "@/background/lib/process-stream-chunk"
25import { 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
368export 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 )
0 commit comments