-
Notifications
You must be signed in to change notification settings - Fork 74
feat(opencode-go): add Opencode Go as a first-class provider (#172) #319
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
edelauna
merged 8 commits into
Zoo-Code-Org:main
from
proyectoauraorg:feat/172-opencode-go
May 27, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
12bf8a1
feat(opencode-go): add Opencode Go as a first-class provider (#172)
proyectoauraorg 60abf37
fix(opencode-go): green CI and address review nits (#172)
proyectoauraorg f2a4a0b
test(opencode-go): cover OpencodeGoHandler streaming and completeProm…
proyectoauraorg 4a4d662
fix(opencode-go): address CodeRabbit review — defensive validation, s…
proyectoauraorg 8913414
fix(opencode-go): omit price from fallback model info
proyectoauraorg 2548179
Update src/api/providers/fetchers/opencode-go.ts
edelauna baf3dfa
Update src/api/providers/fetchers/__tests__/opencode-go.spec.ts
edelauna 1df1c32
Merge branch 'main' into feat/172-opencode-go
navedmerchant 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
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,22 @@ | ||
| import type { ModelInfo } from "../model.js" | ||
|
|
||
| // Opencode "Go" plan — OpenAI-compatible gateway. | ||
| // https://opencode.ai/docs/go/ · base URL: https://opencode.ai/zen/go/v1 | ||
| // | ||
| // The full model list (and metadata) is fetched dynamically from | ||
| // `https://opencode.ai/zen/go/v1/models`, so models can be switched on the fly. | ||
| // The values below are only a fallback used before the live list resolves. | ||
| export const opencodeGoDefaultModelId = "glm-5.1" | ||
|
|
||
| export const opencodeGoDefaultModelInfo: ModelInfo = { | ||
| maxTokens: 32_768, | ||
| contextWindow: 200_000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| // Pricing is intentionally omitted: ModelInfoView renders a `0` field as "$0.00 / 1M tokens" | ||
| // (implying the service is free), so we leave it unknown — consistent with the dynamically | ||
| // fetched models, which also leave price fields absent. See PR #319 review. | ||
| description: "Opencode Go plan model. Available models and metadata are resolved dynamically from /v1/models.", | ||
| } | ||
|
|
||
| export const OPENCODE_GO_DEFAULT_TEMPERATURE = 0 |
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,177 @@ | ||
| // npx vitest run src/api/providers/__tests__/opencode-go.spec.ts | ||
|
|
||
| // Mock vscode first to avoid import errors | ||
| vitest.mock("vscode", () => ({})) | ||
|
|
||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
| import OpenAI from "openai" | ||
|
|
||
| import { opencodeGoDefaultModelId } from "@roo-code/types" | ||
|
|
||
| import { OpencodeGoHandler } from "../opencode-go" | ||
| import { ApiHandlerOptions } from "../../../shared/api" | ||
|
|
||
| vitest.mock("openai") | ||
| vitest.mock("delay", () => ({ default: vitest.fn(() => Promise.resolve()) })) | ||
| vitest.mock("../fetchers/modelCache", () => ({ | ||
| getModels: vitest.fn().mockImplementation(() => | ||
| Promise.resolve({ | ||
| "glm-5.1": { | ||
| maxTokens: 32768, | ||
| contextWindow: 200000, | ||
| supportsImages: false, | ||
| supportsPromptCache: false, | ||
| description: "GLM 5.1", | ||
| }, | ||
| }), | ||
| ), | ||
| getModelsFromCache: vitest.fn().mockReturnValue(undefined), | ||
| })) | ||
|
|
||
| const mockCreate = vitest.fn() | ||
|
|
||
| ;(OpenAI as any).mockImplementation(() => ({ | ||
| chat: { completions: { create: mockCreate } }, | ||
| })) | ||
|
|
||
| describe("OpencodeGoHandler", () => { | ||
| const mockOptions: ApiHandlerOptions = { | ||
| opencodeGoApiKey: "test-key", | ||
| opencodeGoModelId: "glm-5.1", | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vitest.clearAllMocks() | ||
| mockCreate.mockClear() | ||
| }) | ||
|
|
||
| it("initializes the OpenAI client with the Opencode Go base URL and key", () => { | ||
| const handler = new OpencodeGoHandler(mockOptions) | ||
| expect(handler).toBeInstanceOf(OpencodeGoHandler) | ||
| expect(OpenAI).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| baseURL: "https://opencode.ai/zen/go/v1", | ||
| apiKey: "test-key", | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| describe("fetchModel", () => { | ||
| it("returns the configured model info", async () => { | ||
| const handler = new OpencodeGoHandler(mockOptions) | ||
| const result = await handler.fetchModel() | ||
| expect(result.id).toBe("glm-5.1") | ||
| expect(result.info.maxTokens).toBe(32768) | ||
| expect(result.info.contextWindow).toBe(200000) | ||
| expect(result.info.supportsPromptCache).toBe(false) | ||
| }) | ||
|
|
||
| it("falls back to the default model id when none is configured", async () => { | ||
| const handler = new OpencodeGoHandler({ opencodeGoApiKey: "test-key" }) | ||
| const result = await handler.fetchModel() | ||
| expect(result.id).toBe(opencodeGoDefaultModelId) | ||
| }) | ||
| }) | ||
|
|
||
| describe("createMessage", () => { | ||
| beforeEach(() => { | ||
| mockCreate.mockImplementation(async () => ({ | ||
| [Symbol.asyncIterator]: async function* () { | ||
| yield { | ||
| choices: [ | ||
| { | ||
| delta: { | ||
| content: "Hello", | ||
| reasoning_content: "thinking…", | ||
| tool_calls: [ | ||
| { | ||
| index: 0, | ||
| id: "call_1", | ||
| function: { name: "read_file", arguments: '{"path":' }, | ||
| }, | ||
| ], | ||
| }, | ||
| index: 0, | ||
| }, | ||
| ], | ||
| usage: null, | ||
| } | ||
| yield { | ||
| choices: [{ delta: {}, index: 0 }], | ||
| usage: { | ||
| prompt_tokens: 12, | ||
| completion_tokens: 7, | ||
| total_tokens: 19, | ||
| prompt_tokens_details: { cached_tokens: 4 }, | ||
| }, | ||
| } | ||
| }, | ||
| })) | ||
| }) | ||
|
|
||
| it("streams text, reasoning, tool-call and usage chunks", async () => { | ||
| const handler = new OpencodeGoHandler(mockOptions) | ||
| const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] | ||
|
|
||
| const chunks = [] | ||
| for await (const chunk of handler.createMessage("You are helpful.", messages)) { | ||
| chunks.push(chunk) | ||
| } | ||
|
|
||
| expect(chunks).toContainEqual({ type: "text", text: "Hello" }) | ||
| expect(chunks).toContainEqual({ type: "reasoning", text: "thinking…" }) | ||
| expect(chunks).toContainEqual({ | ||
| type: "tool_call_partial", | ||
| index: 0, | ||
| id: "call_1", | ||
| name: "read_file", | ||
| arguments: '{"path":', | ||
| }) | ||
| expect(chunks).toContainEqual({ | ||
| type: "usage", | ||
| inputTokens: 12, | ||
| outputTokens: 7, | ||
| cacheReadTokens: 4, | ||
| }) | ||
| }) | ||
|
|
||
| it("requests a streaming completion with usage included", async () => { | ||
| const handler = new OpencodeGoHandler(mockOptions) | ||
| const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] | ||
| for await (const _chunk of handler.createMessage("sys", messages)) { | ||
| void _chunk // drain | ||
| } | ||
|
|
||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| model: "glm-5.1", | ||
| stream: true, | ||
| stream_options: { include_usage: true }, | ||
| max_completion_tokens: 32768, | ||
| temperature: expect.any(Number), | ||
| }), | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("completePrompt", () => { | ||
| it("returns the message content for a non-streaming completion", async () => { | ||
| mockCreate.mockResolvedValue({ choices: [{ message: { content: "the answer" } }] }) | ||
| const handler = new OpencodeGoHandler(mockOptions) | ||
| expect(await handler.completePrompt("ping")).toBe("the answer") | ||
| expect(mockCreate).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| model: "glm-5.1", | ||
| stream: false, | ||
| max_completion_tokens: 32768, | ||
| }), | ||
| ) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it("wraps errors with an Opencode Go-specific message", async () => { | ||
| mockCreate.mockRejectedValue(new Error("boom")) | ||
| const handler = new OpencodeGoHandler(mockOptions) | ||
| await expect(handler.completePrompt("ping")).rejects.toThrow("Opencode Go completion error: boom") | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.