Skip to content

Commit 321c43f

Browse files
committed
Remove async/await from stream chunk processing and add type safety for content extraction
1 parent d9f64d2 commit 321c43f

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

‎packages/vscode/src/utils/make-api-request.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ type ThinkingStreamCallback = (text: string) => void
99
const DATA_PREFIX = 'data: '
1010
const DONE_TOKEN = '[DONE]'
1111

12-
const process_stream_chunk = async (
12+
const process_stream_chunk = (
1313
chunk: string,
1414
buffer: string
15-
): Promise<{
15+
): {
1616
updated_buffer: string
1717
new_content: string
18-
}> => {
18+
} => {
1919
let updated_buffer = buffer
2020
let new_content = ''
2121
try {
@@ -33,8 +33,9 @@ const process_stream_chunk = async (
3333
if (!json_string || json_string == DONE_TOKEN) continue
3434

3535
const json_data = JSON.parse(json_string)
36-
if (json_data.choices?.[0]?.delta?.content) {
37-
new_content += json_data.choices[0].delta.content
36+
const content = json_data.choices?.[0]?.delta?.content
37+
if (typeof content == 'string') {
38+
new_content += content
3839
}
3940
} catch (parse_error) {
4041
Logger.warn({
@@ -262,7 +263,7 @@ export const make_api_request = async (params: {
262263
let stream_closed = false
263264

264265
response.data.on('data', async (chunk: string) => {
265-
const { updated_buffer, new_content } = await process_stream_chunk(
266+
const { updated_buffer, new_content } = process_stream_chunk(
266267
chunk,
267268
buffer
268269
)
@@ -293,8 +294,9 @@ export const make_api_request = async (params: {
293294
.trim()
294295
if (json_string && json_string !== DONE_TOKEN) {
295296
const json_data = JSON.parse(json_string)
296-
if (json_data.choices?.[0]?.delta?.content) {
297-
process_content(json_data.choices[0].delta.content)
297+
const content = json_data.choices?.[0]?.delta?.content
298+
if (typeof content === 'string') {
299+
process_content(content)
298300
}
299301
}
300302
}

0 commit comments

Comments
 (0)