From 0ddf4ff30fb93f1f52ca3d481fd27911aae0793f Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Mon, 14 Sep 2026 10:33:25 -0700 Subject: [PATCH] refactor(opencode): move completion dispatch logic out of main.py into the provider package --- litellm/llms/opencode/chat/handler.py | 139 ++++++++++++++++++++++++++ litellm/main.py | 121 ++++------------------ 2 files changed, 159 insertions(+), 101 deletions(-) create mode 100644 litellm/llms/opencode/chat/handler.py diff --git a/litellm/llms/opencode/chat/handler.py b/litellm/llms/opencode/chat/handler.py new file mode 100644 index 00000000000..99f3e770f1a --- /dev/null +++ b/litellm/llms/opencode/chat/handler.py @@ -0,0 +1,139 @@ +""" +Completion dispatch for the OpenCode providers. + +Resolves the key, base URL and session header, then sends the request over the +wire format the model speaks: Anthropic Messages through the Anthropic chat +handler, which translates OpenAI input and output, or OpenAI Chat Completions. +""" + +from collections.abc import Callable, Coroutine, Mapping +from types import MappingProxyType +from typing import TYPE_CHECKING, Final + +import httpx +import tiktoken + +import litellm +from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper +from litellm.llms.anthropic.chat.handler import AnthropicChatCompletion +from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler +from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler +from litellm.llms.opencode.chat.messages_transformation import ( + OpenCodeMessagesConfig, + is_messages_model, +) +from litellm.llms.opencode.chat.transformation import OpenCodeConfig +from litellm.llms.opencode.common_utils import ( + resolve_opencode_api_base, + resolve_opencode_api_key, + with_opencode_session_header, +) +from litellm.types.llms.openai import AllMessageValues +from litellm.types.utils import ModelResponse + +if TYPE_CHECKING: + from aiohttp import ClientSession + + from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj + + +def complete_opencode( + *, + custom_llm_provider: str, + model: str, + messages: list[AllMessageValues], # mutable-ok: both handlers take the message list as-is + api_key: str | None, + api_base: str | None, + headers: Mapping[str, str] | None, + litellm_params: dict[str, object], # mutable-ok: both handlers take litellm_params as a dict + optional_params: dict[str, object], # mutable-ok: both handlers take optional_params as a dict + model_response: ModelResponse, + logging_obj: "LiteLLMLoggingObj", + acompletion: bool, + stream: bool | None, + timeout: float | str | httpx.Timeout | None, + client: HTTPHandler | AsyncHTTPHandler | None, + shared_session: "ClientSession | None", + anthropic_chat_handler: AnthropicChatCompletion, + chat_handler: BaseLLMHTTPHandler, + encoding: tiktoken.Encoding, + print_verbose: Callable[[str], object], +) -> ModelResponse | CustomStreamWrapper | Coroutine[object, object, ModelResponse | CustomStreamWrapper]: + surface: Final = "go" if custom_llm_provider == "opencode_go" else "zen" + provider: Final = f"opencode_{surface}" + resolved_key: Final = resolve_opencode_api_key(surface, api_key) + resolved_base: Final = resolve_opencode_api_base(surface, api_base) + session_headers: Final = with_opencode_session_header( + surface, + headers or litellm.headers or {}, # mutable-ok: empty dict fallback for headers + litellm_params, + ) + no_params: Final = MappingProxyType({}) + + if is_messages_model(surface, model): + # AnthropicChatCompletion builds its headers from AnthropicConfig + # directly, and that resolves a missing key from ANTHROPIC_API_KEY, so + # a missing OpenCode key has to be rejected here rather than sending a + # first-party Anthropic credential to opencode.ai. + if resolved_key is None: + raise ValueError( + f"OpenCode API key is required. Set OPENCODE_{surface.upper()}_API_KEY " + f"or OPENCODE_API_KEY, or pass api_key." + ) + return anthropic_chat_handler.completion( + model=model, + messages=messages, + api_base=OpenCodeMessagesConfig(surface=surface).get_complete_url( + api_base=resolved_base, + api_key=resolved_key, + model=model, + optional_params=no_params, + litellm_params=no_params, + ), + acompletion=acompletion, + custom_prompt_dict=litellm.custom_prompt_dict, + model_response=model_response, + print_verbose=print_verbose, + optional_params=optional_params, + litellm_params=litellm_params, + logger_fn=None, + encoding=encoding, + api_key=resolved_key, + logging_obj=logging_obj, + headers=session_headers, # Bearer is chat-arm only; the config injects x-api-key + timeout=timeout, # pyright: ignore[reportArgumentType] # the dispatch context widens timeout to str|None for every provider; narrowing it is a repo-wide gap, not opencode-specific + client=client, + custom_llm_provider=provider, + ) + + response: Final = chat_handler.completion( + model=model, + stream=stream, + messages=messages, + acompletion=acompletion, + api_base=OpenCodeConfig(surface=surface).get_complete_url( + api_base=resolved_base, + api_key=resolved_key, + model=model, + optional_params=no_params, + litellm_params=no_params, + ), + model_response=model_response, + optional_params=optional_params, + litellm_params=litellm_params, + shared_session=shared_session, + custom_llm_provider=provider, + timeout=timeout, # pyright: ignore[reportArgumentType] # the dispatch context widens timeout to str|None for every provider; narrowing it is a repo-wide gap, not opencode-specific + headers=( + {**session_headers, "Authorization": f"Bearer {resolved_key}"} # mutable-ok: handler mutates headers + if resolved_key is not None + else session_headers + ), + encoding=encoding, + api_key=resolved_key, + logging_obj=logging_obj, + client=client, + ) + logging_obj.post_call(input=messages, api_key=resolved_key, original_response=response) + + return response diff --git a/litellm/main.py b/litellm/main.py index 1a8dfed2c93..7f87b324087 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -3484,110 +3484,29 @@ def _complete_openrouter(ctx: _CompletionDispatchContext) -> _CompletionDispatch def _complete_opencode( ctx: _CompletionDispatchContext, ) -> _CompletionDispatchResult: - acompletion: Final = ctx.acompletion - api_base = ctx.api_base # rebind-ok: resolved below from module/env fallbacks - api_key = ctx.api_key # rebind-ok: resolved below from module/env fallbacks - client: Final = ctx.client - custom_llm_provider: Final = ctx.custom_llm_provider - headers: Final = ctx.headers - litellm_params: Final = ctx.litellm_params - logging: Final = ctx.logging - messages: Final = ctx.messages - model: Final = ctx.model - model_response: Final = ctx.model_response - optional_params: Final = ctx.optional_params - shared_session: Final = ctx.shared_session - stream: Final = ctx.stream - timeout: Final = ctx.timeout + from litellm.llms.opencode.chat.handler import complete_opencode - from litellm.llms.opencode.common_utils import ( - resolve_opencode_api_base, - resolve_opencode_api_key, - with_opencode_session_header, - ) - - surface: Final = "go" if custom_llm_provider == "opencode_go" else "zen" - _base_url: Final = "https://opencode.ai/zen/go/v1" if surface == "go" else "https://opencode.ai/zen/v1" - api_base = ( - resolve_opencode_api_base(surface, api_base) or _base_url - ) # rebind-ok: resolved from module/env fallbacks - - api_key = resolve_opencode_api_key(surface, api_key) # rebind-ok: resolved from module/env fallbacks - - base_headers: Final = with_opencode_session_header( - surface, - headers or litellm.headers or {}, # mutable-ok: empty dict fallback for headers - litellm_params, - ) - _headers: Final = ( - {**base_headers, "Authorization": f"Bearer {api_key}"} # mutable-ok: dict literal for request headers - if api_key is not None - else base_headers - ) - - from litellm.llms.opencode.chat.messages_transformation import is_messages_model - - if is_messages_model(surface, model): - # AnthropicChatCompletion builds its headers from AnthropicConfig - # directly, and that resolves a missing key from ANTHROPIC_API_KEY, so - # a missing OpenCode key has to be rejected here rather than sending a - # first-party Anthropic credential to opencode.ai. - if api_key is None: - raise ValueError( - f"OpenCode API key is required. Set OPENCODE_{surface.upper()}_API_KEY " - f"or OPENCODE_API_KEY, or pass api_key." - ) - messages_base: Final = api_base.rstrip("/") - messages_url: Final = ( - messages_base - if messages_base.endswith("/v1/messages") - else f"{messages_base}/messages" - if messages_base.endswith("/v1") - else f"{messages_base}/v1/messages" - ) - return anthropic_chat_completions.completion( - model=model, - messages=messages, - api_base=messages_url, - acompletion=acompletion, - custom_prompt_dict=litellm.custom_prompt_dict, - model_response=model_response, - print_verbose=print_verbose, - optional_params=optional_params, - litellm_params=litellm_params, - logger_fn=None, - encoding=_get_encoding(), - api_key=api_key, - logging_obj=logging, - headers=base_headers, # Bearer is chat-arm only; the config injects x-api-key - timeout=timeout, # pyright: ignore[reportArgumentType] # ctx.timeout is str|None-widened same as every other _complete_* dispatch handler; narrowing is a pre-existing, repo-wide gap in _CompletionDispatchContext, not opencode-specific - client=client, - custom_llm_provider=f"opencode_{surface}", - ) - - ## COMPLETION CALL (chat arm) - response = base_llm_http_handler.completion( # rebind-ok: chat arm result - model=model, - stream=stream, - messages=messages, - acompletion=acompletion, - api_base=api_base, - model_response=model_response, - optional_params=optional_params, - litellm_params=litellm_params, - shared_session=shared_session, - custom_llm_provider=f"opencode_{surface}", - timeout=timeout, # pyright: ignore[reportArgumentType] # ctx.timeout is str|None-widened same as every other _complete_* dispatch handler; narrowing is a pre-existing, repo-wide gap in _CompletionDispatchContext, not opencode-specific - headers=_headers, + return complete_opencode( + custom_llm_provider=ctx.custom_llm_provider, + model=ctx.model, + messages=ctx.messages, + api_key=ctx.api_key, + api_base=ctx.api_base, + headers=ctx.headers, + litellm_params=ctx.litellm_params, + optional_params=ctx.optional_params, + model_response=ctx.model_response, + logging_obj=ctx.logging, + acompletion=ctx.acompletion, + stream=ctx.stream, + timeout=ctx.timeout, + client=ctx.client, + shared_session=ctx.shared_session, + anthropic_chat_handler=anthropic_chat_completions, + chat_handler=base_llm_http_handler, encoding=_get_encoding(), - api_key=api_key, - logging_obj=logging, - client=client, + print_verbose=print_verbose, ) - ## LOGGING - logging.post_call(input=messages, api_key=api_key, original_response=response) - - return response def _complete_vercel_ai_gateway(