-
Notifications
You must be signed in to change notification settings - Fork 46
feat(code): add opencode #2962
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
Draft
adboio
wants to merge
1
commit into
main
Choose a base branch
from
06-27-feat_code_add_opencode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat(code): add opencode #2962
Changes from all commits
Commits
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,106 @@ | ||
| import type { SessionConfigOption } from "@agentclientprotocol/sdk"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| formatOpencodeModelName, | ||
| modelIdFromConfigOptions, | ||
| normalizeOpencodeConfigOptions, | ||
| } from "./models"; | ||
|
|
||
| const modelSelect = ( | ||
| options: unknown, | ||
| currentValue = "posthog/@cf/zai-org/glm-5.2", | ||
| ): SessionConfigOption => | ||
| ({ | ||
| id: "model", | ||
| name: "Model", | ||
| type: "select", | ||
| category: "model", | ||
| currentValue, | ||
| options, | ||
| }) as unknown as SessionConfigOption; | ||
|
|
||
| describe("formatOpencodeModelName", () => { | ||
| it("strips the posthog/ provider prefix and takes the final path segment", () => { | ||
| expect(formatOpencodeModelName("posthog/@cf/zai-org/glm-5.2")).toBe( | ||
| "glm-5.2", | ||
| ); | ||
| expect(formatOpencodeModelName("@cf/zai-org/glm-5.2")).toBe("glm-5.2"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("modelIdFromConfigOptions", () => { | ||
| it("returns the model option's currentValue", () => { | ||
| expect(modelIdFromConfigOptions([modelSelect([])])).toBe( | ||
| "posthog/@cf/zai-org/glm-5.2", | ||
| ); | ||
| }); | ||
|
|
||
| it("returns undefined when there is no model option", () => { | ||
| expect(modelIdFromConfigOptions([])).toBeUndefined(); | ||
| expect(modelIdFromConfigOptions(undefined)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("normalizeOpencodeConfigOptions", () => { | ||
| it("keeps only posthog/* models and cleans their names (flat)", () => { | ||
| const result = normalizeOpencodeConfigOptions([ | ||
| modelSelect([ | ||
| { value: "openai/gpt-5", name: "OpenAI/GPT-5" }, | ||
| { | ||
| value: "anthropic/claude-opus-4-8", | ||
| name: "Anthropic/Claude Opus 4.8", | ||
| }, | ||
| { | ||
| value: "posthog/@cf/zai-org/glm-5.2", | ||
| name: "PostHog Gateway/GLM 5.2", | ||
| }, | ||
| ]), | ||
| ]); | ||
| const model = result?.find((o) => o.category === "model"); | ||
| expect((model as { options: unknown }).options).toEqual([ | ||
| { value: "posthog/@cf/zai-org/glm-5.2", name: "glm-5.2" }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("filters grouped options and drops empty groups", () => { | ||
| const result = normalizeOpencodeConfigOptions([ | ||
| modelSelect([ | ||
| { | ||
| group: "openai", | ||
| name: "OpenAI", | ||
| options: [{ value: "openai/gpt-5", name: "GPT-5" }], | ||
| }, | ||
| { | ||
| group: "posthog", | ||
| name: "PostHog", | ||
| options: [{ value: "posthog/@cf/zai-org/glm-5.2", name: "GLM 5.2" }], | ||
| }, | ||
| ]), | ||
| ]); | ||
| const model = result?.find((o) => o.category === "model"); | ||
| expect((model as { options: unknown }).options).toEqual([ | ||
| { | ||
| group: "posthog", | ||
| name: "PostHog", | ||
| options: [{ value: "posthog/@cf/zai-org/glm-5.2", name: "glm-5.2" }], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("leaves non-model options untouched", () => { | ||
| const modeOption = { | ||
| id: "mode", | ||
| name: "Mode", | ||
| type: "select", | ||
| category: "mode", | ||
| currentValue: "auto", | ||
| options: [{ value: "auto", name: "Auto" }], | ||
| } as unknown as SessionConfigOption; | ||
| expect(normalizeOpencodeConfigOptions([modeOption])).toEqual([modeOption]); | ||
| }); | ||
|
|
||
| it("returns null/undefined input unchanged", () => { | ||
| expect(normalizeOpencodeConfigOptions(null)).toBeNull(); | ||
| expect(normalizeOpencodeConfigOptions(undefined)).toBeUndefined(); | ||
| }); | ||
| }); |
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,73 @@ | ||
| import type { | ||
| SessionConfigOption, | ||
| SessionConfigSelectGroup, | ||
| SessionConfigSelectOption, | ||
| } from "@agentclientprotocol/sdk"; | ||
|
|
||
| // All gateway models are registered in the generated opencode.json under this | ||
| // provider key, so opencode surfaces them as `posthog/<modelId>` in its config | ||
| // options. We keep only those — opencode otherwise lists its ~80 built-in models | ||
| // (gpt-*, claude-*, embeddings, image) which we don't want in the Code picker. | ||
| export const OPENCODE_PROVIDER_PREFIX = "posthog/"; | ||
|
|
||
| export function formatOpencodeModelName(value: string): string { | ||
| const withoutProvider = value.startsWith(OPENCODE_PROVIDER_PREFIX) | ||
| ? value.slice(OPENCODE_PROVIDER_PREFIX.length) | ||
| : value; | ||
| // GLM ids are slash-paths ("@cf/zai-org/glm-5.2") — take the final segment. | ||
| return (withoutProvider.split("/").pop() ?? withoutProvider).toLowerCase(); | ||
| } | ||
|
|
||
| export function modelIdFromConfigOptions( | ||
| configOptions: SessionConfigOption[] | null | undefined, | ||
| ): string | undefined { | ||
| const modelOption = configOptions?.find((o) => o.category === "model"); | ||
| return typeof modelOption?.currentValue === "string" | ||
| ? modelOption.currentValue | ||
| : undefined; | ||
| } | ||
|
|
||
| function isPosthogModel(opt: SessionConfigSelectOption): boolean { | ||
| return opt.value.startsWith(OPENCODE_PROVIDER_PREFIX); | ||
| } | ||
|
|
||
| /** | ||
| * Restrict the model picker to our gateway provider and give each entry a clean | ||
| * label. Filters opencode's full built-in catalogue down to `posthog/*`. | ||
| */ | ||
| export function normalizeOpencodeConfigOptions( | ||
| configOptions: SessionConfigOption[] | null | undefined, | ||
| ): SessionConfigOption[] | null | undefined { | ||
| if (!configOptions) return configOptions; | ||
|
|
||
| const formatOption = ( | ||
| opt: SessionConfigSelectOption, | ||
| ): SessionConfigSelectOption => ({ | ||
| ...opt, | ||
| name: formatOpencodeModelName(opt.value), | ||
| }); | ||
|
|
||
| return configOptions.map((option) => { | ||
| if (option.category !== "model" || option.type !== "select") return option; | ||
| const options = option.options; | ||
| if (options.length === 0) return option; | ||
| const isGroup = "group" in options[0]; | ||
|
|
||
| if (isGroup) { | ||
| const groups = (options as SessionConfigSelectGroup[]) | ||
| .map((group) => ({ | ||
| ...group, | ||
| options: group.options.filter(isPosthogModel).map(formatOption), | ||
| })) | ||
| .filter((group) => group.options.length > 0); | ||
| return { ...option, options: groups } as SessionConfigOption; | ||
| } | ||
|
|
||
| return { | ||
| ...option, | ||
| options: (options as SessionConfigSelectOption[]) | ||
| .filter(isPosthogModel) | ||
| .map(formatOption), | ||
| } as SessionConfigOption; | ||
| }); | ||
| } | ||
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.
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.
isGroupflag is set by inspecting onlyoptions[0]. If opencode ever returns a mixed-shape array — even one flat option following a group — all entries after the first will be cast to the wrong type. In the flat branch,isPosthogModel(opt)accessesopt.valuewhich isundefinedfor group objects, throwing aTypeError. UsingArray.prototype.everygives a safe exhaustive guard at negligible cost.