forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: add openai-compatible provider support for token usage display #8544
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
Closed
+149
−4
Closed
Changes from all commits
Commits
Show all changes
2 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
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
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,140 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { buildApiHandler } from "../../index" | ||
| import { OpenAiHandler } from "../openai" | ||
|
|
||
| vi.mock("openai", () => { | ||
| const mockCreate = vi.fn() | ||
| return { | ||
| default: vi.fn().mockImplementation(() => ({ | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| })), | ||
| OpenAI: vi.fn().mockImplementation(() => ({ | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| })), | ||
| AzureOpenAI: vi.fn().mockImplementation(() => ({ | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| })), | ||
| } | ||
| }) | ||
|
|
||
| describe("OpenAI Compatible Provider", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it("should create OpenAiHandler when apiProvider is 'openai-compatible'", () => { | ||
| const handler = buildApiHandler({ | ||
| apiProvider: "openai-compatible", | ||
| openAiApiKey: "test-key", | ||
| openAiBaseUrl: "https://api.example.com/v1", | ||
| openAiModelId: "test-model", | ||
| }) | ||
|
|
||
| expect(handler).toBeInstanceOf(OpenAiHandler) | ||
| }) | ||
|
|
||
| it("should handle token usage correctly for openai-compatible provider", async () => { | ||
| const mockStream = { | ||
| async *[Symbol.asyncIterator]() { | ||
| yield { | ||
| choices: [{ delta: { content: "Hello" } }], | ||
| } | ||
| yield { | ||
| choices: [{ delta: { content: " world" } }], | ||
| } | ||
| yield { | ||
| choices: [{ delta: {} }], | ||
| usage: { | ||
| prompt_tokens: 10, | ||
| completion_tokens: 5, | ||
| total_tokens: 15, | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| const OpenAI = (await import("openai")).default | ||
| const mockCreate = vi.fn().mockResolvedValue(mockStream) | ||
| ;(OpenAI as any).mockImplementation(() => ({ | ||
| chat: { | ||
| completions: { | ||
| create: mockCreate, | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| const handler = buildApiHandler({ | ||
| apiProvider: "openai-compatible", | ||
| openAiApiKey: "test-key", | ||
| openAiBaseUrl: "https://api.example.com/v1", | ||
| openAiModelId: "test-model", | ||
| }) | ||
|
|
||
| const messages = [{ role: "user" as const, content: "Test message" }] | ||
| const stream = handler.createMessage("System prompt", messages) | ||
|
|
||
| const chunks = [] | ||
| for await (const chunk of stream) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| // Check that we got text chunks | ||
| const textChunks = chunks.filter((c) => c.type === "text") | ||
| expect(textChunks).toHaveLength(2) | ||
| expect(textChunks[0].text).toBe("Hello") | ||
| expect(textChunks[1].text).toBe(" world") | ||
|
|
||
| // Check that we got usage data | ||
| const usageChunk = chunks.find((c) => c.type === "usage") | ||
| expect(usageChunk).toBeDefined() | ||
| expect(usageChunk).toEqual({ | ||
| type: "usage", | ||
| inputTokens: 10, | ||
| outputTokens: 5, | ||
| }) | ||
| }) | ||
|
|
||
| it("should use the same configuration as openai provider", () => { | ||
| const config = { | ||
| openAiApiKey: "test-key", | ||
| openAiBaseUrl: "https://api.example.com/v1", | ||
| openAiModelId: "test-model", | ||
| openAiCustomModelInfo: { | ||
| maxTokens: 4096, | ||
| contextWindow: 8192, | ||
| supportsPromptCache: false, | ||
| inputPrice: 0.001, | ||
| outputPrice: 0.002, | ||
| }, | ||
| } | ||
|
|
||
| const openaiHandler = buildApiHandler({ | ||
| apiProvider: "openai", | ||
| ...config, | ||
| }) | ||
|
|
||
| const openaiCompatibleHandler = buildApiHandler({ | ||
| apiProvider: "openai-compatible", | ||
| ...config, | ||
| }) | ||
|
|
||
| // Both should be instances of OpenAiHandler | ||
| expect(openaiHandler).toBeInstanceOf(OpenAiHandler) | ||
| expect(openaiCompatibleHandler).toBeInstanceOf(OpenAiHandler) | ||
|
|
||
| // Both should have the same model configuration | ||
| expect(openaiHandler.getModel()).toEqual(openaiCompatibleHandler.getModel()) | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,7 +255,8 @@ function getSelectedModel({ | |
| const info = mistralModels[id as keyof typeof mistralModels] | ||
| return { id, info } | ||
| } | ||
| case "openai": { | ||
| case "openai": | ||
| case "openai-compatible": { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Add unit test coverage for the new 'openai-compatible' branch here to prevent regressions. A small test asserting the hook returns the configured openAiModelId and custom model info would lock in the behavior. |
||
| const id = apiConfiguration.openAiModelId ?? "" | ||
| const info = apiConfiguration?.openAiCustomModelInfo ?? openAiModelInfoSaneDefaults | ||
| return { id, info } | ||
|
|
@@ -360,7 +361,7 @@ function getSelectedModel({ | |
| // case "human-relay": | ||
| // case "fake-ai": | ||
| default: { | ||
| provider satisfies "anthropic" | "gemini-cli" | "qwen-code" | "human-relay" | "fake-ai" | ||
| provider satisfies "anthropic" | "gemini-cli" | "human-relay" | "fake-ai" | ||
| const id = apiConfiguration.apiModelId ?? anthropicDefaultModelId | ||
| const baseInfo = anthropicModels[id as keyof typeof anthropicModels] | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] Consider adding an allow-list test case for 'openai-compatible' to validate that model gating (allowAll vs. explicit models) works identically to 'openai'. This will help catch configuration regressions.