|
| 1 | +import { useState } from 'react' |
| 2 | +import type { FileSearchCompletedData, ResponseStreamEventData } from '../../../shared/types' |
| 3 | +import type { Message } from '../../types' |
| 4 | + |
| 5 | +export const useChatStream = ({ |
| 6 | + onFileSearchComplete, |
| 7 | + onComplete, |
| 8 | + onError, |
| 9 | +}: { |
| 10 | + onFileSearchComplete: (data: FileSearchCompletedData) => void |
| 11 | + onComplete: ({ previousResponseId, message }: { previousResponseId: string | undefined; message: Message }) => void |
| 12 | + onError: (error: string) => void |
| 13 | +}) => { |
| 14 | + const [completion, setCompletion] = useState('') |
| 15 | + const [isStreaming, setIsStreaming] = useState(false) |
| 16 | + const [isFileSearching, setIsFileSearching] = useState(false) |
| 17 | + const [streamController, setStreamController] = useState<AbortController>() |
| 18 | + |
| 19 | + const decoder = new TextDecoder() |
| 20 | + |
| 21 | + const processStream = async (stream: ReadableStream) => { |
| 22 | + let content = '' |
| 23 | + let error = '' |
| 24 | + let fileSearch: FileSearchCompletedData | undefined |
| 25 | + let previousResponseId: string | undefined |
| 26 | + |
| 27 | + setIsStreaming(true) |
| 28 | + |
| 29 | + try { |
| 30 | + const reader = stream.getReader() |
| 31 | + |
| 32 | + while (true) { |
| 33 | + const { value, done } = await reader.read() |
| 34 | + if (done) break |
| 35 | + |
| 36 | + const data = decoder.decode(value) |
| 37 | + |
| 38 | + let accumulatedChunk = '' |
| 39 | + for (const chunk of data.split('\n')) { |
| 40 | + if (!chunk || chunk.trim().length === 0) continue |
| 41 | + |
| 42 | + let parsedChunk: ResponseStreamEventData | undefined |
| 43 | + try { |
| 44 | + parsedChunk = JSON.parse(chunk) |
| 45 | + } catch (e: any) { |
| 46 | + console.error('Error', e) |
| 47 | + console.error('Could not parse the chunk:', chunk) |
| 48 | + accumulatedChunk += chunk |
| 49 | + |
| 50 | + try { |
| 51 | + parsedChunk = JSON.parse(accumulatedChunk) |
| 52 | + accumulatedChunk = '' |
| 53 | + } catch (e: any) { |
| 54 | + console.error('Error', e) |
| 55 | + console.error('Could not parse the accumulated chunk:', accumulatedChunk) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (!parsedChunk) continue |
| 60 | + |
| 61 | + switch (parsedChunk.type) { |
| 62 | + case 'writing': |
| 63 | + setCompletion((prev) => prev + parsedChunk.text) |
| 64 | + content += parsedChunk.text |
| 65 | + break |
| 66 | + |
| 67 | + case 'annotation': |
| 68 | + console.log('Received annotation:', parsedChunk.annotation) |
| 69 | + break |
| 70 | + |
| 71 | + case 'fileSearchStarted': |
| 72 | + setIsFileSearching(true) |
| 73 | + break |
| 74 | + |
| 75 | + case 'fileSearchDone': |
| 76 | + fileSearch = parsedChunk.fileSearch |
| 77 | + onFileSearchComplete(parsedChunk.fileSearch) |
| 78 | + setIsFileSearching(false) |
| 79 | + break |
| 80 | + |
| 81 | + case 'error': |
| 82 | + error += parsedChunk.error |
| 83 | + break |
| 84 | + |
| 85 | + case 'complete': |
| 86 | + previousResponseId = parsedChunk.prevResponseId |
| 87 | + break |
| 88 | + |
| 89 | + default: |
| 90 | + break |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } catch (err: any) { |
| 95 | + error += '\nResponse stream was interrupted' |
| 96 | + onError(err) |
| 97 | + } finally { |
| 98 | + setStreamController(undefined) |
| 99 | + setCompletion('') |
| 100 | + setIsFileSearching(false) |
| 101 | + setIsStreaming(false) |
| 102 | + |
| 103 | + onComplete({ |
| 104 | + previousResponseId, |
| 105 | + message: { |
| 106 | + role: 'assistant', |
| 107 | + content, |
| 108 | + error: error.length > 0 ? error : undefined, |
| 109 | + fileSearchResult: fileSearch, |
| 110 | + }, |
| 111 | + }) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return { |
| 116 | + processStream, |
| 117 | + completion, |
| 118 | + isStreaming, |
| 119 | + isFileSearching, |
| 120 | + streamController, |
| 121 | + } |
| 122 | +} |
0 commit comments