Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

feat(llm): add OpenCode Zen/Go provider

Pedro Cardoso56de9d17018a
diff --git a/crates/ai-memory-llm/src/factory.rs b/crates/ai-memory-llm/src/factory.rs
index 56720a88..59a59f7c 100644
--- a/crates/ai-memory-llm/src/factory.rs
+++ b/crates/ai-memory-llm/src/factory.rs
@@ -13,6 +13,7 @@ use crate::GeminiProvider;
use crate::OpenAiCompatProvider;
use crate::OpenAiOAuthProvider;
use crate::OpenAiProvider;
+use crate::OpenCodeProvider;
use crate::auth::{AuthRequirement, ProviderAuth};
use crate::embedding::{Embedder, OpenAiEmbedder, VoyageEmbedder};
use crate::error::{LlmError, LlmResult};
@@ -36,6 +37,8 @@ pub enum ProviderChoice {
Copilot,
/// Anthropic Messages API via a Claude-subscription OAuth token.
AnthropicOAuth,
+ /// OpenCode Zen/Go cloud API (OpenAI-compatible endpoint).
+ OpenCode,
}
impl ProviderChoice {
@@ -50,6 +53,7 @@ impl ProviderChoice {
Self::OpenAiOAuth => "openai-oauth",
Self::Copilot => "copilot",
Self::AnthropicOAuth => "anthropic-oauth",
+ Self::OpenCode => "opencode",
}
}
@@ -72,6 +76,9 @@ impl ProviderChoice {
Self::OpenAiOAuth => AuthRequirement::OpenAiOAuthToken,
Self::Copilot => AuthRequirement::CopilotToken,
Self::AnthropicOAuth => AuthRequirement::AnthropicOAuthToken,
+ Self::OpenCode => AuthRequirement::RequiredApiKey {
+ env_var: "OPENCODE_API_KEY",
+ },
}
}
}
@@ -226,6 +233,10 @@ pub fn build_provider(config: ProviderConfig) -> LlmResult<Arc<dyn LlmProvider>>
}
Ok(Arc::new(provider))
}
+ ProviderChoice::OpenCode => {
+ let key = config.auth.require_api_key()?;
+ Ok(Arc::new(OpenCodeProvider::new(key, config.model)?))
+ }
}
}
diff --git a/crates/ai-memory-llm/src/lib.rs b/crates/ai-memory-llm/src/lib.rs
index cd089fe9..b4f98dad 100644
--- a/crates/ai-memory-llm/src/lib.rs
+++ b/crates/ai-memory-llm/src/lib.rs
@@ -39,6 +39,7 @@ pub mod oidc;
pub mod openai;
pub mod openai_compat;
pub mod openai_oauth;
+pub mod opencode;
pub mod provider;
pub mod types;
@@ -70,6 +71,7 @@ pub use oidc::{
};
pub use openai::OpenAiProvider;
pub use openai_compat::OpenAiCompatProvider;
+pub use opencode::{OPENCODE_DEFAULT_MODEL, OPENCODE_ZEN_BASE_URL, OpenCodeProvider};
pub use openai_oauth::{
CODEX_CLIENT_ID, CODEX_RESPONSES_URL, OPENAI_OAUTH_AUTH_URL, OPENAI_OAUTH_ISSUER,
OPENAI_OAUTH_TOKEN_URL, OpenAiOAuthProvider, OpenAiOAuthToken, OpenAiOAuthTokenResponse,
diff --git a/crates/ai-memory-llm/src/opencode.rs b/crates/ai-memory-llm/src/opencode.rs
new file mode 100644
index 00000000..0d2c668a
--- /dev/null
+++ b/crates/ai-memory-llm/src/opencode.rs
@@ -0,0 +1,63 @@
+//! OpenCode Zen/Go provider.
+//!
+//! Thin wrapper around [`OpenAiCompatProvider`] that bakes in the OpenCode
+//! Zen API base URL (`https://opencode.ai/zen/go/v1`) and names the provider
+//! `"opencode"`. Accepts an `sk-...` API key from `OPENCODE_API_KEY`.
+
+use async_trait::async_trait;
+use secrecy::SecretString;
+
+use crate::error::LlmResult;
+use crate::openai_compat::OpenAiCompatProvider;
+use crate::provider::LlmProvider;
+use crate::types::{ChatRequest, ChatResponse};
+
+/// Public OpenCode Zen/Go OpenAI-compatible base URL.
+pub const OPENCODE_ZEN_BASE_URL: &str = "https://opencode.ai/zen/go/v1";
+
+/// Default model when `AI_MEMORY_LLM_MODEL` is not set.
+pub const OPENCODE_DEFAULT_MODEL: &str = "claude-sonnet-4-6";
+
+/// OpenCode Zen/Go LLM provider.
+///
+/// Routes through `https://opencode.ai/zen/go/v1` using the OpenAI chat
+/// completions wire format. Authenticate with the `sk-...` key obtained
+/// from <https://opencode.ai/auth>.
+pub struct OpenCodeProvider {
+ inner: OpenAiCompatProvider,
+}
+
+impl OpenCodeProvider {
+ /// Construct an OpenCode Zen/Go provider.
+ ///
+ /// # Errors
+ /// Returns a `reqwest::Error` if the HTTP client cannot be built.
+ pub fn new(api_key: SecretString, model: impl Into<String>) -> LlmResult<Self> {
+ let inner =
+ OpenAiCompatProvider::new(OPENCODE_ZEN_BASE_URL, Some(api_key), model.into())?;
+ Ok(Self { inner })
+ }
+}
+
+#[async_trait]
+impl LlmProvider for OpenCodeProvider {
+ fn name(&self) -> &'static str {
+ "opencode"
+ }
+
+ fn model(&self) -> &str {
+ self.inner.model()
+ }
+
+ async fn complete(&self, request: ChatRequest) -> LlmResult<ChatResponse> {
+ self.inner.complete(request).await
+ }
+
+ async fn complete_structured_raw(
+ &self,
+ request: ChatRequest,
+ schema: serde_json::Value,
+ ) -> LlmResult<serde_json::Value> {
+ self.inner.complete_structured_raw(request, schema).await
+ }
+}