forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
28 lines (24 loc) · 839 Bytes
/
utils.ts
File metadata and controls
28 lines (24 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { handleSSEStream, type StreamHandler } from '@opentiny/tiny-robot-kit'
export const chatStream = async (requestOpts: any, handler: StreamHandler, headers = {}) => {
try {
const { requestData, requestUrl } = requestOpts
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'text/event-stream',
...headers
},
body: JSON.stringify(requestData)
}
const response = await fetch(requestUrl, requestOptions)
if (!response.ok) {
const errorText = await response.text()
throw new Error(`HTTP error! status: ${response.status}, details: ${errorText}`)
}
await handleSSEStream(response, handler)
} catch (error: unknown) {
const logger = console
logger.error('Error in chatStream:', error)
}
}