-
-
Notifications
You must be signed in to change notification settings - Fork 11.3k
feat(opencode): add OpenCode Zen and Go providers that send x-opencode-session #39549
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
Open
codedoga
wants to merge
5
commits into
BerriAI:litellm_internal_staging
Choose a base branch
from
codedoga:litellm_opencode_provider
base: litellm_internal_staging
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.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1853181
feat(opencode): add OpenCode Zen and Go providers that send x-opencod…
codedoga c56dc47
feat(opencode): route each model to the endpoint OpenCode serves it on
codedoga 19d241a
test(opencode): cover the responses configs and the remaining provide…
codedoga 4c3cf43
fix(opencode): declare cache minimums and the Gemini route the cost m…
codedoga 1cc5371
fix(opencode): inherit the request-shape flags the Claude re-export g…
codedoga 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
Empty file.
Empty file.
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,76 @@ | ||
| """ | ||
| Translate from OpenAI's `/v1/chat/completions` to OpenCode Zen's and OpenCode Go's. | ||
|
|
||
| Both surfaces are OpenAI-compatible, so only the default base URL, the credential lookup and | ||
| the required `x-opencode-session` header differ from stock OpenAI. | ||
| """ | ||
|
|
||
| from collections.abc import Mapping, Sequence | ||
| from typing import ClassVar, Final | ||
|
|
||
| from litellm.llms.openai_like.chat.transformation import OpenAILikeChatConfig | ||
| from litellm.secret_managers.main import get_secret_str | ||
| from litellm.types.llms.openai import AllMessageValues | ||
|
|
||
| from ..common_utils import ( | ||
| OPENCODE_SESSION_HEADER, | ||
| has_opencode_session_header, | ||
| resolve_opencode_session_id, | ||
| ) | ||
|
|
||
|
|
||
| class OpenCodeChatConfig(OpenAILikeChatConfig): | ||
| _provider: ClassVar[str] | ||
| _default_api_base: ClassVar[str] | ||
| _api_base_env_var: ClassVar[str] | ||
|
|
||
| @property | ||
| def custom_llm_provider(self) -> str | None: | ||
| return self._provider | ||
|
|
||
| def _get_openai_compatible_provider_info( | ||
| self, api_base: str | None, api_key: str | None | ||
| ) -> tuple[str | None, str | None]: | ||
| resolved_api_base: Final = api_base or get_secret_str(self._api_base_env_var) or self._default_api_base | ||
| resolved_api_key: Final = ( | ||
| api_key or get_secret_str("OPENCODE_API_KEY") or get_secret_str("OPENCODE_ZEN_API_KEY") | ||
| ) | ||
| return resolved_api_base, resolved_api_key | ||
|
|
||
| def validate_environment( | ||
| self, | ||
| headers: Mapping[str, object], | ||
| model: str, | ||
| messages: Sequence[AllMessageValues], | ||
| optional_params: Mapping[str, object], | ||
| litellm_params: Mapping[str, object], | ||
| api_key: str | None = None, | ||
| api_base: str | None = None, | ||
| ) -> dict: # mutable-ok: return type is fixed by BaseConfig.validate_environment, whose callers mutate it | ||
| base_headers: Final[Mapping[str, object]] = super().validate_environment( | ||
| headers=dict(headers), # mutable-ok: BaseConfig.validate_environment only accepts mutable dicts | ||
| model=model, | ||
| messages=list(messages), # mutable-ok: BaseConfig.validate_environment only accepts a mutable list | ||
| optional_params=dict(optional_params), # mutable-ok: as above | ||
| litellm_params=dict(litellm_params), # mutable-ok: as above | ||
| api_key=api_key, | ||
| api_base=api_base, | ||
| ) | ||
| session_id: Final = ( | ||
| None if has_opencode_session_header(base_headers) else resolve_opencode_session_id(litellm_params) | ||
| ) | ||
| if session_id is None: | ||
| return dict(base_headers) | ||
| return {**base_headers, OPENCODE_SESSION_HEADER: session_id} | ||
|
|
||
|
|
||
| class OpenCodeZenChatConfig(OpenCodeChatConfig): | ||
| _provider: ClassVar[str] = "opencode" | ||
| _default_api_base: ClassVar[str] = "https://opencode.ai/zen/v1" | ||
| _api_base_env_var: ClassVar[str] = "OPENCODE_API_BASE" | ||
|
|
||
|
|
||
| class OpenCodeGoChatConfig(OpenCodeChatConfig): | ||
| _provider: ClassVar[str] = "opencode_go" | ||
| _default_api_base: ClassVar[str] = "https://opencode.ai/zen/go/v1" | ||
| _api_base_env_var: ClassVar[str] = "OPENCODE_GO_API_BASE" |
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,30 @@ | ||
| """ | ||
| Shared helpers for OpenCode's managed inference APIs (Zen and Go). | ||
| """ | ||
|
|
||
| from collections.abc import Iterable, Mapping | ||
| from typing import Final | ||
|
|
||
| OPENCODE_SESSION_HEADER: Final = "x-opencode-session" | ||
|
|
||
|
|
||
| def resolve_opencode_session_id(litellm_params: Mapping[str, object]) -> str | None: | ||
| """ | ||
| OpenCode requires a stable per-conversation id on every inference request so it can pin a | ||
| conversation to one upstream and keep that upstream's prompt cache warm. LiteLLM keeps no | ||
| conversation state of its own, so a caller-supplied session id is the only value that is | ||
| genuinely stable across turns; the per-request ids are a last resort that keeps the request | ||
| accepted even though it forfeits cache affinity. | ||
| """ | ||
| metadata: Final = litellm_params.get("metadata") | ||
| candidates: Final = ( | ||
| litellm_params.get("litellm_session_id"), | ||
| metadata.get("session_id") if isinstance(metadata, Mapping) else None, | ||
| litellm_params.get("litellm_trace_id"), | ||
| litellm_params.get("litellm_call_id"), | ||
| ) | ||
| return next((candidate for candidate in candidates if isinstance(candidate, str) and candidate), None) | ||
|
|
||
|
|
||
| def has_opencode_session_header(header_names: Iterable[str]) -> bool: | ||
| return any(name.lower() == OPENCODE_SESSION_HEADER for name in header_names) |
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.
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.