-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Handle <think> tags in the base OpenAI-compatible provider #8989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
286 changes: 286 additions & 0 deletions
286
src/api/providers/__tests__/base-openai-compatible-provider.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,286 @@ | ||
| // npx vitest run api/providers/__tests__/base-openai-compatible-provider.spec.ts | ||
|
|
||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
| import OpenAI from "openai" | ||
|
|
||
| import type { ModelInfo } from "@roo-code/types" | ||
|
|
||
| import { BaseOpenAiCompatibleProvider } from "../base-openai-compatible-provider" | ||
|
|
||
| // Create mock functions | ||
| const mockCreate = vi.fn() | ||
|
|
||
| // Mock OpenAI module | ||
| vi.mock("openai", () => ({ | ||
| default: vi.fn(() => ({ | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| })), | ||
| })) | ||
|
|
||
| // Create a concrete test implementation of the abstract base class | ||
| class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-model"> { | ||
| constructor(apiKey: string) { | ||
| const testModels: Record<"test-model", ModelInfo> = { | ||
| "test-model": { | ||
| maxTokens: 4096, | ||
| contextWindow: 128000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| inputPrice: 0.5, | ||
| outputPrice: 1.5, | ||
| }, | ||
| } | ||
|
|
||
| super({ | ||
| providerName: "TestProvider", | ||
| baseURL: "https://test.example.com/v1", | ||
| defaultProviderModelId: "test-model", | ||
| providerModels: testModels, | ||
| apiKey, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| describe("BaseOpenAiCompatibleProvider", () => { | ||
| let handler: TestOpenAiCompatibleProvider | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| handler = new TestOpenAiCompatibleProvider("test-api-key") | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| describe("XmlMatcher reasoning tags", () => { | ||
| it("should handle reasoning tags (<think>) from stream", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| next: vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "<think>Let me think" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: " about this</think>" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "The answer is 42" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ done: true }), | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const stream = handler.createMessage("system prompt", []) | ||
| const chunks = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // XmlMatcher yields chunks as they're processed | ||
| expect(chunks).toEqual([ | ||
| { type: "reasoning", text: "Let me think" }, | ||
| { type: "reasoning", text: " about this" }, | ||
| { type: "text", text: "The answer is 42" }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("should handle complete <think> tag in a single chunk", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| next: vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "Regular text before " } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "<think>Complete thought</think>" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: " regular text after" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ done: true }), | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const stream = handler.createMessage("system prompt", []) | ||
| const chunks = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // When a complete tag arrives in one chunk, XmlMatcher may not parse it | ||
| // This test documents the actual behavior | ||
| expect(chunks.length).toBeGreaterThan(0) | ||
| expect(chunks[0]).toEqual({ type: "text", text: "Regular text before " }) | ||
| }) | ||
|
|
||
| it("should handle incomplete <think> tag at end of stream", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| next: vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "<think>Incomplete thought" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ done: true }), | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const stream = handler.createMessage("system prompt", []) | ||
| const chunks = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // XmlMatcher should handle incomplete tags and flush remaining content | ||
| expect(chunks.length).toBeGreaterThan(0) | ||
| expect( | ||
| chunks.some( | ||
| (c) => (c.type === "text" || c.type === "reasoning") && c.text.includes("Incomplete thought"), | ||
| ), | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it("should handle text without any <think> tags", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| next: vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "Just regular text" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: " without reasoning" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ done: true }), | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const stream = handler.createMessage("system prompt", []) | ||
| const chunks = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| expect(chunks).toEqual([ | ||
| { type: "text", text: "Just regular text" }, | ||
| { type: "text", text: " without reasoning" }, | ||
| ]) | ||
| }) | ||
|
|
||
| it("should handle <think> tags that start at beginning of stream", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| next: vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: "<think>reasoning" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: " content</think>" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { choices: [{ delta: { content: " normal text" } }] }, | ||
| }) | ||
| .mockResolvedValueOnce({ done: true }), | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const stream = handler.createMessage("system prompt", []) | ||
| const chunks = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| expect(chunks).toEqual([ | ||
| { type: "reasoning", text: "reasoning" }, | ||
| { type: "reasoning", text: " content" }, | ||
| { type: "text", text: " normal text" }, | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| describe("Basic functionality", () => { | ||
| it("should create stream with correct parameters", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| async next() { | ||
| return { done: true } | ||
| }, | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const systemPrompt = "Test system prompt" | ||
| const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Test message" }] | ||
|
|
||
| const messageGenerator = handler.createMessage(systemPrompt, messages) | ||
| await messageGenerator.next() | ||
|
|
||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| model: "test-model", | ||
| temperature: 0, | ||
| messages: expect.arrayContaining([{ role: "system", content: systemPrompt }]), | ||
| stream: true, | ||
| stream_options: { include_usage: true }, | ||
| }), | ||
| undefined, | ||
| ) | ||
| }) | ||
|
|
||
| it("should yield usage data from stream", async () => { | ||
| mockCreate.mockImplementationOnce(() => { | ||
| return { | ||
| [Symbol.asyncIterator]: () => ({ | ||
| next: vi | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| done: false, | ||
| value: { | ||
| choices: [{ delta: {} }], | ||
| usage: { prompt_tokens: 100, completion_tokens: 50 }, | ||
| }, | ||
| }) | ||
| .mockResolvedValueOnce({ done: true }), | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| const stream = handler.createMessage("system prompt", []) | ||
| const firstChunk = await stream.next() | ||
|
|
||
| expect(firstChunk.done).toBe(false) | ||
| expect(firstChunk.value).toEqual({ type: "usage", inputTokens: 100, outputTokens: 50 }) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.