瀏覽代碼

Fix crash when delta in openai api handling could be empty in some cases.

Fixes #641
Piotr Rogowski 11 月之前
父節點
當前提交
60cb550db7
共有 1 個文件被更改,包括 4 次插入2 次删除
  1. 4 2
      src/api/providers/openai.ts

+ 4 - 2
src/api/providers/openai.ts

@@ -62,13 +62,15 @@ export class OpenAiHandler implements ApiHandler, SingleCompletionHandler {
 			const stream = await this.client.chat.completions.create(requestOptions)
 			const stream = await this.client.chat.completions.create(requestOptions)
 
 
 			for await (const chunk of stream) {
 			for await (const chunk of stream) {
-				const delta = chunk.choices[0]?.delta
-				if (delta?.content) {
+				const delta = chunk.choices[0]?.delta ?? {}
+
+				if (delta.content) {
 					yield {
 					yield {
 						type: "text",
 						type: "text",
 						text: delta.content,
 						text: delta.content,
 					}
 					}
 				}
 				}
+
 				if ("reasoning_content" in delta && delta.reasoning_content) {
 				if ("reasoning_content" in delta && delta.reasoning_content) {
 					yield {
 					yield {
 						type: "reasoning",
 						type: "reasoning",