Skip to content

Commit 0f7390a

Browse files
JessYan0913louisgv
andauthored
refactor: 调整推理块和文本块的处理顺序以优化流式响应 (#125)
Co-authored-by: L <[email protected]>
1 parent 3b83be2 commit 0f7390a

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/chat/index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -507,22 +507,6 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
507507

508508
const delta = choice.delta;
509509

510-
if (delta.content != null) {
511-
if (!textStarted) {
512-
textId = openrouterResponseId || generateId();
513-
controller.enqueue({
514-
type: 'text-start',
515-
id: textId,
516-
});
517-
textStarted = true;
518-
}
519-
controller.enqueue({
520-
type: 'text-delta',
521-
delta: delta.content,
522-
id: textId || generateId(),
523-
});
524-
}
525-
526510
const emitReasoningChunk = (chunkText: string) => {
527511
if (!reasoningStarted) {
528512
reasoningId = openrouterResponseId || generateId();
@@ -567,11 +551,26 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
567551
}
568552
}
569553
}
570-
}
571-
else if (delta.reasoning != null) {
554+
} else if (delta.reasoning != null) {
572555
emitReasoningChunk(delta.reasoning);
573556
}
574-
557+
558+
if (delta.content != null) {
559+
if (!textStarted) {
560+
textId = openrouterResponseId || generateId();
561+
controller.enqueue({
562+
type: 'text-start',
563+
id: textId,
564+
});
565+
textStarted = true;
566+
}
567+
controller.enqueue({
568+
type: 'text-delta',
569+
delta: delta.content,
570+
id: textId || generateId(),
571+
});
572+
}
573+
575574
if (delta.tool_calls != null) {
576575
for (const toolCallDelta of delta.tool_calls) {
577576
const index = toolCallDelta.index ?? toolCalls.length - 1;

0 commit comments

Comments
 (0)