feat(providers): add xAI (Grok) + migrate house BYOK env to KIGI_API_KEY
13th provider (16th registry variant). Also reconciles a naming collision the matrix flagged: this fork is house-branded "xai" (cf. xai.dev metadata, KIGI_CODE_XAI_API_KEY legacy env), so XAI_API_KEY + method xai.api_key were the GENERIC house BYOK, not x.ai/Grok. The provider table wants xai/XAI_API_KEY for Grok. Resolution (user-approved): XAI_API_KEY now keys the x.ai/Grok provider; the house BYOK primary env moves to KIGI_API_KEY, keeping XAI_API_KEY and KIGI_CODE_XAI_API_KEY as back-compat fallbacks (read_xai_api_key_env checks KIGI_API_KEY first). The xai.api_key method id is unchanged (persisted-session compat); the platform method id is the bare "xai", distinct from it. xAI spec: api.x.ai/v1, Bearer, OpenAI listing + ChatCompletions, Passthrough (docs confirm stream_options.include_usage accepted). /v1/models is minimal (ids only) and requires auth, so it doubles as the key validator (401 on bad key, no override) and metadata comes from models.dev enrichment. Live ids match the models.dev "xai" keys byte-for-byte, so restrict_to_enriched keeps the 5 tool-calling chat models (grok-4.5/4.3/4.20-0309-*/build-0.1) and drops the grok-imagine-* generators + the non-tool multi-agent model. Snapshot regenerated to include the xai provider (was stale; gen script already listed it in TARGETS). Env migration is comprehensive to avoid keying the xai platform (which would trigger a live api.x.ai fetch) or leaving house-key reads stranded: routed the trace CLI resolver + acp_agent/auth.json bridge + paste-key ext handler through the new primary; moved all leader/pager/e2e harness setters to KIGI_API_KEY; made every house-key isolation test unset KIGI_API_KEY too; updated user-facing hints to name KIGI_API_KEY. Tests: e2e proves enrichment-supplied context (wire carries none), non-vacuous tool_call restriction, bare-id round-trip under xai/, Passthrough; validation tests hit /models (401 reject, 200 accept); house_env_var_takes_precedence_over_xai pins the new precedence. Registry at 16; picker 17 rows; 4 auth arrays + xai. Review (16 findings, all fixed): caught a missed else-branch env clear in the paste-key handler (would leak the house key past a clear) and a non-hermetic credential-priority test; both fixed.
This commit is contained in:
1 parent
193d16f6d5
commit
ebf11057f8
25 files changed
+297
-56
No files matched your search
@@ -76,7 +76,7 @@ pub fn map_sampling_err_to_acp(err: SamplingError) -> acp::Error {
|
||||
&& crate::agent::auth_method::has_xai_api_key_env()
|
||||
{
|
||||
format!(
|
||||
"{message}\n\nYou have an API key set (XAI_API_KEY). \
|
||||
"{message}\n\nYou have an API key set (KIGI_API_KEY). \
|
||||
Your cached OAuth session is being used instead. \
|
||||
To use your API key, run `kigi logout` or type /logout in the TUI."
|
||||
)
|
||||
@@ -441,24 +441,32 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// Helper: run a closure with XAI_API_KEY temporarily set (or cleared).
|
||||
/// Cleans up even if the closure panics.
|
||||
/// Helper: run a closure with the house BYOK key (KIGI_API_KEY) temporarily
|
||||
/// set (or cleared). Clears every house-key env (KIGI_API_KEY plus the
|
||||
/// back-compat XAI_API_KEY / KIGI_CODE_XAI_API_KEY) so the "no key" case is
|
||||
/// hermetic. Cleans up even if the closure panics.
|
||||
fn with_api_key_env<F: FnOnce()>(key: Option<&str>, f: F) {
|
||||
let prev_house = std::env::var("KIGI_API_KEY").ok();
|
||||
let prev = std::env::var("XAI_API_KEY").ok();
|
||||
let prev_legacy = std::env::var("KIGI_CODE_XAI_API_KEY").ok();
|
||||
// SAFETY: serial_test ensures no concurrent env mutation.
|
||||
unsafe {
|
||||
std::env::remove_var("KIGI_API_KEY");
|
||||
std::env::remove_var("XAI_API_KEY");
|
||||
std::env::remove_var("KIGI_CODE_XAI_API_KEY");
|
||||
if let Some(k) = key {
|
||||
std::env::set_var("XAI_API_KEY", k);
|
||||
std::env::set_var("KIGI_API_KEY", k);
|
||||
}
|
||||
}
|
||||
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
|
||||
// Restore original state.
|
||||
unsafe {
|
||||
std::env::remove_var("KIGI_API_KEY");
|
||||
std::env::remove_var("XAI_API_KEY");
|
||||
std::env::remove_var("KIGI_CODE_XAI_API_KEY");
|
||||
if let Some(v) = prev_house {
|
||||
std::env::set_var("KIGI_API_KEY", v);
|
||||
}
|
||||
if let Some(v) = prev {
|
||||
std::env::set_var("XAI_API_KEY", v);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user