Skip to content

Commit 710f606

Browse files
committed
Fix ragIndexFilter key in search tool
1 parent dd3eea8 commit 710f606

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

src/server/util/azure/ResponsesAPI.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ export class ResponsesClient {
5353
? [
5454
createFileSearchTool({
5555
vectorStoreId: ragIndex.metadata.azureVectorStoreId,
56-
filters: ragIndex.metadata.ragIndexFilterValue
57-
? { key: 'ragIndexFilterValue', value: ragIndex.metadata.ragIndexFilterValue, type: 'eq' }
58-
: undefined,
56+
filters: ragIndex.metadata.ragIndexFilterValue ? { key: 'ragIndexFilter', value: ragIndex.metadata.ragIndexFilterValue, type: 'eq' } : undefined,
5957
}),
6058
]
6159
: [] // needs to retrun empty array for null

0 commit comments

Comments
 (0)