From 0a147bd8a551ec14905849c902449d9b098e7cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Tue, 21 Jul 2026 13:16:20 -0400 Subject: [PATCH] Add Together AI platform + bare-array listing tolerance (provider 9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 12th registry row: id "together", TOGETHER_API_KEY > auth.json "together" scope, https://api.together.xyz/v1 with KIGI_TOGETHER_BASE_URL override, Bearer, ChatCompletions + Passthrough, enrichment-backed metadata (models_dev_id togetherai) with the tool-calling listing restriction (Together's listing mixes chat/embedding/rerank/image types). Together's GET /v1/models returns a BARE JSON ARRAY, not the OpenAI {object:list,data:[]} envelope. New shared parser parse_openai_listing tolerates both shapes (sniffs the top-level [ vs { for accurate diagnostics); every OpenAI-listing provider now routes through it, with byte-equivalent envelope behavior (verified: Groq/Google/OpenRouter unchanged) and non-silent errors. Together's org/Model ids match the togetherai snapshot keys exactly (verified), so restrict_to_enriched keeps the ~27 tool-calling models without the id-shape trap. Review: ship-ready, shared-parser change proven strictly-additive and safe, registry/e2e/dialect all correct. Backlog logged: kigi ignores the wire per-model field, so a live Together chat model absent from models.dev is dropped until indexed — a future wire- filter would unlock the fresh full catalog. --- crates/codegen/kigi-models/src/lib.rs | 75 ++++++++++++- .../kigi-shell/src/agent/auth_method.rs | 12 ++- .../kigi-shell/src/agent/models_fetch.rs | 100 +++++++++++++++++- crates/codegen/kigi-tui/src/app/app_view.rs | 13 ++- 4 files changed, 190 insertions(+), 10 deletions(-) diff --git a/crates/codegen/kigi-models/src/lib.rs b/crates/codegen/kigi-models/src/lib.rs index c4a4f9a..bae7f56 100644 --- a/crates/codegen/kigi-models/src/lib.rs +++ b/crates/codegen/kigi-models/src/lib.rs @@ -466,6 +466,36 @@ const OPENROUTER_SPEC: PlatformSpec = PlatformSpec { strip_listing_id_prefix: None, }; +/// Base-URL override for Together AI (dev/test escape hatch). +pub const TOGETHER_BASE_URL_ENV: &str = "KIGI_TOGETHER_BASE_URL"; + +const TOGETHER_SPEC: PlatformSpec = PlatformSpec { + id: "together", + display_name: "Together AI", + base_url: BaseUrlSource::EnvOr { + env: TOGETHER_BASE_URL_ENV, + default: "https://api.together.xyz/v1", + }, + uses_oauth: false, + allowed_model_prefixes: None, + api_key_envs: &["TOGETHER_API_KEY"], + vendor: "Together", + console_host: Some("api.together.xyz"), + login_label: Some("Together AI (API key)"), + models_dev_id: Some("togetherai"), + wire_serves_metadata: false, + wire_api: PlatformWireApi::ChatCompletions, + // Together's /v1/models is a BARE JSON array (parse_openai_listing is + // tolerant), and it mixes chat/embedding/rerank/image types; keep + // tool-calling enrichment-known chat models only. + listing: ListingDialect::OpenAi, + chat_compat: PlatformChatCompat::Passthrough, + key_header: PlatformKeyHeader::Bearer, + key_validation_path: None, + strip_listing_id_prefix: None, + restrict_to_enriched: true, +}; + /// The platform registry. Platforms are compiled-in spec rows; there is no /// dynamic provider registration (PRD F2). #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -492,12 +522,14 @@ pub enum PlatformId { Google, /// OpenRouter meta-provider (API key, wire-served metadata). OpenRouter, + /// Together AI platform API (API key, bare-array listing). + Together, } impl PlatformId { /// All platforms, in catalog precedence order: the subscription channel /// first so "default model = first list item" favors it when present. - pub const ALL: [PlatformId; 11] = [ + pub const ALL: [PlatformId; 12] = [ Self::KimiCode, Self::MoonshotCn, Self::MoonshotAi, @@ -509,6 +541,7 @@ impl PlatformId { Self::Fireworks, Self::Google, Self::OpenRouter, + Self::Together, ]; /// The registry row backing this platform (single source of per-platform @@ -526,6 +559,7 @@ impl PlatformId { Self::Fireworks => &FIREWORKS_SPEC, Self::Google => &GOOGLE_SPEC, Self::OpenRouter => &OPENROUTER_SPEC, + Self::Together => &TOGETHER_SPEC, } } @@ -740,6 +774,20 @@ pub struct WireModelsResponse { pub data: Vec, } +/// Parse an OpenAI-shape `/models` listing, tolerant of BOTH the standard +/// envelope `{object:"list", data:[...]}` and a bare top-level array `[...]` +/// (Together AI serves the bare-array form). +pub fn parse_openai_listing(json: &str) -> Result, serde_json::Error> { + // Sniff the top-level shape so a malformed body yields the diagnostic for + // the shape it actually is (a broken bare-array element reports the + // element error, not a misleading "expected the envelope object"). + if json.trim_start().starts_with('[') { + serde_json::from_str::>(json) + } else { + Ok(serde_json::from_str::(json)?.data) + } +} + // ── Anthropic listing adapter (ListingDialect::Anthropic) ─────────────────── #[derive(serde::Deserialize)] @@ -1173,6 +1221,28 @@ mod tests { ); } + /// The OpenAI listing parser accepts both the standard envelope and a + /// bare top-level array (Together AI serves the bare form). + #[test] + fn openai_listing_parse_accepts_envelope_and_bare_array() { + let envelope = r#"{"object":"list","data":[ + {"id":"a","context_length":1000},{"id":"b"}]}"#; + let bare = r#"[{"id":"a","context_length":1000},{"id":"b"}]"#; + for (label, json) in [("envelope", envelope), ("bare array", bare)] { + let models = + parse_openai_listing(json).unwrap_or_else(|e| panic!("{label} must parse: {e}")); + assert_eq!( + models.iter().map(|m| m.id.as_str()).collect::>(), + vec!["a", "b"], + "{label}" + ); + assert_eq!(models[0].context_length, 1000); + } + // Neither shape → error (not a silent empty list). + assert!(parse_openai_listing("\"not a list\"").is_err()); + assert!(parse_openai_listing("{").is_err()); + } + /// OpenRouter's `/models` is public, so its key validation must target /// an auth-requiring endpoint; every other platform validates against /// the default `/models`. @@ -1239,9 +1309,10 @@ mod tests { PlatformId::Fireworks => 8, PlatformId::Google => 9, PlatformId::OpenRouter => 10, + PlatformId::Together => 11, } } - const VARIANT_COUNT: usize = 11; // update together with `ordinal` + const VARIANT_COUNT: usize = 12; // update together with `ordinal` let mut seen: Vec = PlatformId::ALL.iter().map(|&p| ordinal(p)).collect(); seen.sort_unstable(); seen.dedup(); diff --git a/crates/codegen/kigi-shell/src/agent/auth_method.rs b/crates/codegen/kigi-shell/src/agent/auth_method.rs index a9b041c..a41addb 100644 --- a/crates/codegen/kigi-shell/src/agent/auth_method.rs +++ b/crates/codegen/kigi-shell/src/agent/auth_method.rs @@ -603,7 +603,8 @@ mod tests { "mistral", "fireworks", "google", - "openrouter" + "openrouter", + "together" ] ); assert_eq!(default_id(&built), Some(XAI_API_KEY_METHOD_ID)); @@ -637,7 +638,8 @@ mod tests { "mistral", "fireworks", "google", - "openrouter" + "openrouter", + "together" ] ); assert_eq!(default_id(&built), Some(CACHED_TOKEN_AUTH_METHOD_ID)); @@ -664,7 +666,8 @@ mod tests { "mistral", "fireworks", "google", - "openrouter" + "openrouter", + "together" ] ); assert_eq!(default_id(&built), Some(CACHED_TOKEN_AUTH_METHOD_ID)); @@ -694,7 +697,8 @@ mod tests { "mistral", "fireworks", "google", - "openrouter" + "openrouter", + "together" ] ); assert_eq!(default_id(&built), None); diff --git a/crates/codegen/kigi-shell/src/agent/models_fetch.rs b/crates/codegen/kigi-shell/src/agent/models_fetch.rs index 6491b5a..15e5b81 100644 --- a/crates/codegen/kigi-shell/src/agent/models_fetch.rs +++ b/crates/codegen/kigi-shell/src/agent/models_fetch.rs @@ -297,7 +297,13 @@ fn fetch_one_platform_models( .map(|s| s.to_string()); let data = match platform.listing() { kigi_models::ListingDialect::OpenAi => { - response.json::()?.data + // Tolerant of both the {data:[...]} envelope and a bare array + // (Together AI serves the bare form). + let body = response.text()?; + kigi_models::parse_openai_listing(&body).map_err(|e| BackendError::RequestFailed { + status: 200, + body: format!("openai listing parse failed: {e}"), + })? } kigi_models::ListingDialect::Anthropic => { let body = response.text()?; @@ -1597,6 +1603,98 @@ mod tests { ); } + /// Together-cycle e2e: the listing is a BARE JSON ARRAY (no {data:[]} + /// envelope) — parse_openai_listing tolerates it. models.dev enrichment + /// (matching org/Model keys) supplies context + the tool-calling + /// restriction drops non-chat models; Passthrough dialect; slashed id. + #[tokio::test(flavor = "multi_thread")] + #[serial_test::serial] + async fn together_bare_array_listing_enriches_restricts_and_maps_passthrough() { + let platform_server = wiremock::MockServer::start().await; + wiremock::Mock::given(wiremock::matchers::method("GET")) + .and(wiremock::matchers::path("/models")) + .and(wiremock::matchers::header("Authorization", "Bearer tg-1")) + .respond_with(wiremock::ResponseTemplate::new(200).set_body_json( + // BARE ARRAY, not {object:list,data:[]}. + serde_json::json!([ + { "id": "Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8", "object": "model", + "type": "chat", "context_length": 262144 }, + { "id": "togethercomputer/m2-bert-80M-8k-retrieval", "object": "model", + "type": "embedding" } + ]), + )) + .expect(1) + .mount(&platform_server) + .await; + let modelsdev_server = wiremock::MockServer::start().await; + wiremock::Mock::given(wiremock::matchers::method("GET")) + .and(wiremock::matchers::path("/api.json")) + .respond_with(wiremock::ResponseTemplate::new(200).set_body_json( + serde_json::json!({ "togetherai": { "models": { + "Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8": { + "limit": {"context": 262144, "output": 32768}, + "tool_call": true + }, + "togethercomputer/m2-bert-80M-8k-retrieval": { "limit": {"context": 8192} } + }}}), + )) + .expect(1) + .mount(&modelsdev_server) + .await; + let cache_dir = tempfile::tempdir().unwrap(); + let _base = kigi_test_support::EnvGuard::set( + kigi_models::TOGETHER_BASE_URL_ENV, + platform_server.uri(), + ); + let _mdev = kigi_test_support::EnvGuard::set( + crate::agent::enrichment_fetch::MODELS_DEV_URL_ENV, + format!("{}/api.json", modelsdev_server.uri()), + ); + let _mdev_cache = kigi_test_support::EnvGuard::set( + crate::agent::enrichment_fetch::MODELS_DEV_CACHE_DIR_ENV, + cache_dir.path(), + ); + let endpoints = crate::agent::config::EndpointsConfig::default(); + let keys = crate::agent::models::PlatformApiKeys::test_single( + kigi_models::PlatformId::Together, + "tg-1", + ); + let result = tokio::task::spawn_blocking(move || { + fetch_platform_models_blocking(&endpoints, None, &keys) + }) + .await + .unwrap() + .expect("fetch must succeed"); + assert_eq!( + result + .models + .iter() + .map(|m| m.id.as_deref().unwrap_or_default()) + .collect::>(), + vec!["together/Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8"], + "bare array parsed; embedding (not tool-calling) dropped; slashed id in key" + ); + let entry = &result.models[0]; + assert_eq!(entry.context_window.get(), 262_144); + assert_eq!(entry.max_completion_tokens, Some(32_768)); + assert_eq!( + entry.model, "Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8", + "the native slashed id rides the wire" + ); + let model_entry = crate::agent::config::ModelEntry::from_config_entry(entry); + let creds = crate::agent::config::ResolvedCredentials { + api_key: Some("tg-1".into()), + base_url: entry.base_url.clone(), + auth_type: kigi_chat_state::AuthType::ApiKey, + auth_scheme: Default::default(), + }; + let cfg = crate::agent::config::sampling_config_for_model(&model_entry, creds, None); + assert_eq!( + cfg.chat_compat, + kigi_sampling_types::ChatCompat::Passthrough + ); + } + #[test] fn get_env_keys_parses_strings_and_rejects_non_strings() { use crate::agent::config::EnvKeys; diff --git a/crates/codegen/kigi-tui/src/app/app_view.rs b/crates/codegen/kigi-tui/src/app/app_view.rs index d625dd3..f19ad4e 100644 --- a/crates/codegen/kigi-tui/src/app/app_view.rs +++ b/crates/codegen/kigi-tui/src/app/app_view.rs @@ -6894,7 +6894,7 @@ pub(crate) mod tests { #[test] fn pending_menu_items_lists_interactive_methods_plus_quit() { let items = pending_menu_items(&fresh_user_auth_methods(), None); - assert_eq!(items.len(), 12, "11 login rows + Quit, got {items:?}"); + assert_eq!(items.len(), 13, "12 login rows + Quit, got {items:?}"); assert!( matches!(&items[0], PendingMenuItem::Login { label } if label == "Kimi Code (OAuth)"), "row 0 must be the OAuth login, got {:?}", @@ -6971,7 +6971,14 @@ pub(crate) mod tests { label: "OpenRouter (API key)".into(), } ); - assert_eq!(items[11], PendingMenuItem::Quit); + assert_eq!( + items[11], + PendingMenuItem::ApiKey { + target: PlatformLogin(kigi_shell::models::PlatformId::Together), + label: "Together AI (API key)".into(), + } + ); + assert_eq!(items[12], PendingMenuItem::Quit); // The non-interactive methods must never appear as rows. let byok = kigi_shell::agent::auth_method::build_auth_methods( kigi_shell::agent::auth_method::AuthMethodsBuildInputs { @@ -6982,7 +6989,7 @@ pub(crate) mod tests { ); assert_eq!( pending_menu_items(&byok.methods, None).len(), - 12, + 13, "xai.api_key / cached_token must not add rows" ); }