Skip to content

Commit 2b95396

Browse files
committed
Handle <think> tags in the base OpenAI-compatible provider
1 parent d863010 commit 2b95396

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/api/providers/base-openai-compatible-provider.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import OpenAI from "openai"
44
import type { ModelInfo } from "@roo-code/types"
55

66
import { type ApiHandlerOptions, getModelMaxOutputTokens } from "../../shared/api"
7+
import { XmlMatcher } from "../../utils/xml-matcher"
78
import { ApiStream } from "../transform/stream"
89
import { convertToOpenAiMessages } from "../transform/openai-format"
910

@@ -105,13 +106,21 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
105106
): ApiStream {
106107
const stream = await this.createStream(systemPrompt, messages, metadata)
107108

109+
const matcher = new XmlMatcher(
110+
"think",
111+
(chunk) =>
112+
({
113+
type: chunk.matched ? "reasoning" : "text",
114+
text: chunk.data,
115+
}) as const,
116+
)
117+
108118
for await (const chunk of stream) {
109119
const delta = chunk.choices[0]?.delta
110120

111121
if (delta?.content) {
112-
yield {
113-
type: "text",
114-
text: delta.content,
122+
for (const processedChunk of matcher.update(delta.content)) {
123+
yield processedChunk
115124
}
116125
}
117126

@@ -127,6 +136,11 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
127136
}
128137
}
129138
}
139+
140+
// Process any remaining content
141+
for (const processedChunk of matcher.final()) {
142+
yield processedChunk
143+
}
130144
}
131145

132146
async completePrompt(prompt: string): Promise<string> {

0 commit comments

Comments
 (0)