Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/forge_config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum ProviderResponseType {
Bedrock,
Google,
OpenCode,
CommandCode,
}

/// Category of a provider.
Expand Down
25 changes: 25 additions & 0 deletions crates/forge_domain/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl ProviderId {
pub const KIMI_CODING: ProviderId = ProviderId(Cow::Borrowed("kimi_coding"));
pub const MOONSHOT: ProviderId = ProviderId(Cow::Borrowed("moonshot"));
pub const ALIBABA_TOKEN_PLAN: ProviderId = ProviderId(Cow::Borrowed("alibaba_token_plan"));
pub const COMMAND_CODE: ProviderId = ProviderId(Cow::Borrowed("command_code"));

/// Returns all built-in provider IDs
///
Expand Down Expand Up @@ -135,6 +136,7 @@ impl ProviderId {
ProviderId::KIMI_CODING,
ProviderId::MOONSHOT,
ProviderId::ALIBABA_TOKEN_PLAN,
ProviderId::COMMAND_CODE,
]
}

Expand All @@ -160,6 +162,7 @@ impl ProviderId {
"codex" => "Codex".to_string(),
"opencode_zen" => "OpenCode Zen".to_string(),
"opencode_go" => "OpenCode Go".to_string(),
"command_code" => "Command Code".to_string(),
"fireworks-ai" => "FireworksAI".to_string(),
"fireworks-ai-firepass" => "FireworksAIFirepass".to_string(),
"novita" => "Novita".to_string(),
Expand Down Expand Up @@ -235,6 +238,7 @@ impl std::str::FromStr for ProviderId {
"kimi_coding" => ProviderId::KIMI_CODING,
"moonshot" => ProviderId::MOONSHOT,
"alibaba_token_plan" => ProviderId::ALIBABA_TOKEN_PLAN,
"command_code" => ProviderId::COMMAND_CODE,
// For custom providers, use Cow::Owned to avoid memory leaks
custom => ProviderId(Cow::Owned(custom.to_string())),
};
Expand All @@ -256,6 +260,7 @@ pub enum ProviderResponse {
Bedrock,
Google,
OpenCode,
CommandCode,
}

/// Represents the source of models for a provider
Expand Down Expand Up @@ -839,6 +844,26 @@ mod tests {
assert_eq!(actual, expected);
}

#[test]
fn test_command_code_display_name() {
let actual = ProviderId::COMMAND_CODE.to_string();
let expected = "Command Code".to_string();
assert_eq!(actual, expected);
}

#[test]
fn test_command_code_in_built_in_providers() {
let built_in = ProviderId::built_in_providers();
assert!(built_in.contains(&ProviderId::COMMAND_CODE));
}

#[test]
fn test_command_code_from_str_roundtrip() {
let actual = ProviderId::from_str("command_code").unwrap();
let expected = ProviderId::COMMAND_CODE;
assert_eq!(actual, expected);
}

#[test]
fn test_io_intelligence() {
let fixture = "test_key";
Expand Down
43 changes: 42 additions & 1 deletion crates/forge_repo/src/provider/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use url::Url;

use crate::provider::anthropic::AnthropicResponseRepository;
use crate::provider::bedrock::BedrockResponseRepository;
use crate::provider::command_code::CommandCodeResponseRepository;
use crate::provider::google::GoogleResponseRepository;
use crate::provider::openai::OpenAIResponseRepository;
use crate::provider::openai_responses::OpenAIResponsesResponseRepository;
Expand Down Expand Up @@ -42,6 +43,7 @@ impl<F: EnvironmentInfra<Config = forge_config::ForgeConfig> + HttpInfra> ForgeC
BedrockResponseRepository::new(Arc::new(config.retry.unwrap_or_default()));
let google_repo = GoogleResponseRepository::new(infra.clone());
let opencode_zen_repo = OpenCodeZenResponseRepository::new(infra.clone());
let command_code_repo = CommandCodeResponseRepository::new(infra.clone());

let model_cache = Arc::new(CacacheStorage::new(
env.cache_dir().join("model_cache"),
Expand All @@ -56,6 +58,7 @@ impl<F: EnvironmentInfra<Config = forge_config::ForgeConfig> + HttpInfra> ForgeC
bedrock_repo,
google_repo,
opencode_zen_repo,
command_code_repo,
}),
model_cache,
bg_refresh: BgRefresh::default(),
Expand Down Expand Up @@ -129,6 +132,7 @@ struct ProviderRouter<F> {
bedrock_repo: BedrockResponseRepository,
google_repo: GoogleResponseRepository<F>,
opencode_zen_repo: OpenCodeZenResponseRepository<F>,
command_code_repo: CommandCodeResponseRepository<F>,
}

impl<F: HttpInfra + EnvironmentInfra<Config = forge_config::ForgeConfig> + Sync> ProviderRouter<F> {
Expand All @@ -141,7 +145,7 @@ impl<F: HttpInfra + EnvironmentInfra<Config = forge_config::ForgeConfig> + Sync>
match provider.response {
Some(ProviderResponse::OpenAI) => {
// Check if model is a Codex model
if model_id.as_str().contains("gpt-5")
if uses_responses_api(model_id.as_str())
&& (provider.id == ProviderId::OPENAI
|| provider.id == ProviderId::GITHUB_COPILOT
|| provider.id == ProviderId::CODEX)
Expand Down Expand Up @@ -171,6 +175,11 @@ impl<F: HttpInfra + EnvironmentInfra<Config = forge_config::ForgeConfig> + Sync>
.chat(model_id, context, provider)
.await
}
Some(ProviderResponse::CommandCode) => {
self.command_code_repo
.chat(model_id, context, provider)
.await
}
None => Err(anyhow::anyhow!(
"Provider response type not configured for provider: {}",
provider.id
Expand All @@ -186,6 +195,7 @@ impl<F: HttpInfra + EnvironmentInfra<Config = forge_config::ForgeConfig> + Sync>
Some(ProviderResponse::Bedrock) => self.bedrock_repo.models(provider).await,
Some(ProviderResponse::Google) => self.google_repo.models(provider).await,
Some(ProviderResponse::OpenCode) => self.opencode_zen_repo.models(provider).await,
Some(ProviderResponse::CommandCode) => self.command_code_repo.models(provider).await,
None => Err(anyhow::anyhow!(
"Provider response type not configured for provider: {}",
provider.id
Expand All @@ -194,6 +204,12 @@ impl<F: HttpInfra + EnvironmentInfra<Config = forge_config::ForgeConfig> + Sync>
}
}

/// Returns `true` when an OpenAI-compatible model must be served by the
/// Responses API rather than Chat Completions.
fn uses_responses_api(model: &str) -> bool {
model.contains("gpt-5") || model.contains("gpt-6")
}

/// Tracks abort handles for background tasks and cancels them on drop.
#[derive(Default)]
struct BgRefresh(std::sync::Mutex<Vec<AbortHandle>>);
Expand All @@ -216,3 +232,28 @@ impl Drop for BgRefresh {
}
}
}

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;

use super::*;

#[test]
fn test_uses_responses_api() {
let fixture = [
"gpt-5.6-luna",
"gpt-6-astra",
"gpt-6-sol",
"gpt-6-luna",
"gpt-6-cyber",
"gpt-6.1-sol",
"gpt-6.1-terra",
"openai/gpt-6-luna",
"gpt-4o",
];
let actual: Vec<bool> = fixture.iter().map(|m| uses_responses_api(m)).collect();
let expected = vec![true, true, true, true, true, true, true, true, false];
assert_eq!(actual, expected);
}
}
124 changes: 124 additions & 0 deletions crates/forge_repo/src/provider/command_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use std::sync::Arc;

use anyhow::Result;
use forge_app::domain::{
ChatCompletionMessage, Context as ChatContext, Model, ModelId, Provider, ProviderResponse,
ResultStream,
};
use forge_app::{EnvironmentInfra, HttpInfra};
use forge_domain::ChatRepository;
use url::Url;

use crate::provider::anthropic::AnthropicResponseRepository;
use crate::provider::openai::OpenAIResponseRepository;

/// Command Code serves multiple protocols from one host: Claude models answer
/// only on the Anthropic `/messages` route, while every other model answers on
/// `/chat/completions`. Routes by model ID and derives the endpoint from the
/// configured chat completions URL.
pub struct CommandCodeResponseRepository<F> {
openai_repo: OpenAIResponseRepository<F>,
anthropic_repo: AnthropicResponseRepository<F>,
}

impl<F: HttpInfra + EnvironmentInfra<Config = forge_config::ForgeConfig> + Sync>
CommandCodeResponseRepository<F>
{
/// # Arguments
///
/// * `infra` - Infrastructure providing environment and HTTP capabilities
pub fn new(infra: Arc<F>) -> Self {
Self {
openai_repo: OpenAIResponseRepository::new(infra.clone()),
anthropic_repo: AnthropicResponseRepository::new(infra),
}
}

pub async fn chat(
&self,
model_id: &ModelId,
context: ChatContext,
provider: Provider<Url>,
) -> ResultStream<ChatCompletionMessage, anyhow::Error> {
let adapted_provider = build_provider(&provider, model_id);
if is_anthropic_model(model_id) {
self.anthropic_repo
.chat(model_id, context, adapted_provider)
.await
} else {
self.openai_repo
.chat(model_id, context, adapted_provider)
.await
}
}

/// Fetches the model list from the OpenAI-compatible `/models` endpoint.
///
/// The endpoint reports every model the account can use, including Claude
/// models that only answer on `/messages`.
pub async fn models(&self, provider: Provider<Url>) -> Result<Vec<Model>> {
self.openai_repo.models(provider).await
}
}

/// Returns `true` when the model answers only on the Anthropic messages route.
fn is_anthropic_model(model_id: &ModelId) -> bool {
model_id.as_str().starts_with("claude-")
}

/// Rewrites the configured chat completions URL to the Anthropic messages
/// route for Claude models.
fn build_provider(provider: &Provider<Url>, model_id: &ModelId) -> Provider<Url> {
let mut new_provider = provider.clone();
if is_anthropic_model(model_id) {
new_provider.url = messages_url(&provider.url);
new_provider.response = Some(ProviderResponse::Anthropic);
}
new_provider
}

/// Derives the messages endpoint from a chat completions endpoint URL.
fn messages_url(chat_url: &Url) -> Url {
let base = chat_url
.as_str()
.trim_end_matches('/')
.trim_end_matches("/chat/completions");
Url::parse(&format!("{base}/messages")).unwrap()
}

#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;

use super::*;

#[test]
fn test_model_routing() {
let fixture = [
("claude-opus-5", true),
("claude-sonnet-5-5", true),
("gpt-6-astra", false),
("deepseek/deepseek-v4-pro", false),
("z-ai/glm-5.3-flash", false),
];

let actual: Vec<bool> = fixture
.iter()
.map(|(model_id, _)| is_anthropic_model(&ModelId::new(*model_id)))
.collect();
let expected: Vec<bool> = fixture.iter().map(|(_, expected)| *expected).collect();

assert_eq!(actual, expected);
}

#[test]
fn test_messages_url() {
let fixture =
Url::parse("https://api.commandcode.ai/provider/v1/chat/completions").unwrap();

let actual = messages_url(&fixture);

let expected = Url::parse("https://api.commandcode.ai/provider/v1/messages").unwrap();
assert_eq!(actual, expected);
}
}
1 change: 1 addition & 0 deletions crates/forge_repo/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod bedrock;
mod bedrock_cache;
mod bedrock_sanitize_ids;
mod chat;
mod command_code;
mod event;
mod google;
#[cfg(test)]
Expand Down
Loading
Loading