Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Offer a Google AI Studio (Gemini) key in Settings and first run (#670)

Shaikh Fardin4759d37ad230
diff --git a/crates/ai/src/detect.rs b/crates/ai/src/detect.rs
index 33a86b20..d5b65af9 100644
--- a/crates/ai/src/detect.rs
+++ b/crates/ai/src/detect.rs
@@ -31,6 +31,17 @@ pub fn env_keys(get: impl Fn(&str) -> Option<String>) -> Vec<Detected> {
out.push(Detected { suggestion: cfg(name, kind, model), reason: format!("Found {var} in your environment"), needs_key: false });
}
}
+ // A Google AI Studio key is free and the easiest one to get (#646): always offered, found or pasted.
+ let gemini = get("GEMINI_API_KEY").is_some_and(|v| !v.trim().is_empty());
+ out.push(Detected {
+ suggestion: cfg(GEMINI_NAME, BackendKind::Gemini, crate::openai::GEMINI_DEFAULT_MODEL),
+ reason: if gemini {
+ "Found GEMINI_API_KEY in your environment".into()
+ } else {
+ "Gemini: paste a free key from Google AI Studio (aistudio.google.com/apikey)".into()
+ },
+ needs_key: !gemini,
+ });
// Kimi K3.1 Pro builds through OpenRouter: with its key found, or one the person pastes.
let found = get("OPENROUTER_API_KEY").is_some_and(|v| !v.trim().is_empty());
out.push(Detected {
@@ -47,6 +58,8 @@ pub fn env_keys(get: impl Fn(&str) -> Option<String>) -> Vec<Detected> {
/// The name Kimi K3.1 Pro is added under.
pub const KIMI_NAME: &str = "Kimi K3.1 Pro (OpenRouter)";
+/// Name given to a Google AI Studio (Gemini API key) backend.
+pub const GEMINI_NAME: &str = "Gemini (Google AI Studio)";
/// Is something listening at this URL's host:port? (bounded TCP connect)
pub fn port_open(base_url: &str, timeout: Duration) -> bool {
@@ -286,15 +299,27 @@ mod tests {
#[test]
fn env_detection() {
let d = env_keys(|v| (v == "OPENROUTER_API_KEY").then(|| "k".to_string()));
- assert_eq!(d.len(), 2);
+ assert_eq!(d.len(), 3);
assert_eq!(d[0].suggestion.kind, BackendKind::Openrouter);
assert!(d[0].reason.contains("OPENROUTER_API_KEY"));
+ // Gemini is always offered; without its key it asks for one.
+ assert!(d[1].suggestion.kind == BackendKind::Gemini && d[1].needs_key);
// Kimi K3.1 Pro rides on the same key, so it needs none of its own.
- assert_eq!((d[1].suggestion.model.as_str(), d[1].needs_key), (crate::openai::KIMI_K3_1, false));
- // Without a key, Kimi K3.1 Pro is still offered, asking for one.
+ assert_eq!((d[2].suggestion.model.as_str(), d[2].needs_key), (crate::openai::KIMI_K3_1, false));
+ // Without a key, Gemini and Kimi K3.1 Pro are still offered, asking for one.
let none = env_keys(|_| Some(" ".into()));
- assert_eq!(none.len(), 1);
- assert!(none[0].needs_key && none[0].suggestion.name == KIMI_NAME);
+ assert_eq!(none.len(), 2);
+ assert!(none[0].needs_key && none[0].suggestion.name == GEMINI_NAME);
+ assert!(none[1].needs_key && none[1].suggestion.name == KIMI_NAME);
+ }
+
+ #[test]
+ fn gemini_key_in_the_environment_is_used() {
+ let d = env_keys(|v| (v == "GEMINI_API_KEY").then(|| "k".to_string()));
+ let g = d.iter().find(|x| x.suggestion.kind == BackendKind::Gemini).expect("Gemini offered");
+ assert!(!g.needs_key && g.reason.contains("GEMINI_API_KEY"));
+ assert_eq!(g.suggestion.model, crate::openai::GEMINI_DEFAULT_MODEL);
+ assert!(BackendKind::Gemini.needs_key());
}
#[test]
diff --git a/crates/ai/src/keys.rs b/crates/ai/src/keys.rs
index 40a3ca36..638f69c6 100644
--- a/crates/ai/src/keys.rs
+++ b/crates/ai/src/keys.rs
@@ -48,6 +48,7 @@ pub fn env_var(kind: BackendKind) -> Option<&'static str> {
BackendKind::Anthropic => Some("ANTHROPIC_API_KEY"),
BackendKind::Openai => Some("OPENAI_API_KEY"),
BackendKind::Openrouter => Some("OPENROUTER_API_KEY"),
+ BackendKind::Gemini => Some("GEMINI_API_KEY"),
BackendKind::OpenaiCompatible | BackendKind::AgentCli => None,
}
}
@@ -264,5 +265,6 @@ mod tests {
assert!(s.get("Claude").is_none());
assert!(s.set("x", " ").is_err());
assert_eq!(env_var(BackendKind::Openrouter), Some("OPENROUTER_API_KEY"));
+ assert_eq!(env_var(BackendKind::Gemini), Some("GEMINI_API_KEY"));
}
}
diff --git a/crates/ai/src/lib.rs b/crates/ai/src/lib.rs
index e67e1e1c..4205964e 100644
--- a/crates/ai/src/lib.rs
+++ b/crates/ai/src/lib.rs
@@ -47,6 +47,8 @@ pub enum BackendKind {
Anthropic,
Openai,
Openrouter,
+ /// Google AI Studio (a Gemini API key), through Gemini's OpenAI-compatible endpoint.
+ Gemini,
/// Ollama, LM Studio, llama.cpp, vLLM… (OpenAI-compatible HTTP).
OpenaiCompatible,
/// Claude Code, Codex, Gemini CLI, Hermes Agent, OpenCode… run as a subprocess with our MCP server.
@@ -59,6 +61,7 @@ impl BackendKind {
BackendKind::Anthropic => "Anthropic (Claude API)",
BackendKind::Openai => "OpenAI",
BackendKind::Openrouter => "OpenRouter",
+ BackendKind::Gemini => "Gemini (Google AI Studio)",
BackendKind::OpenaiCompatible => "Local / OpenAI-compatible server",
BackendKind::AgentCli => "Agent app (command line)",
}
@@ -70,13 +73,14 @@ impl BackendKind {
BackendKind::Anthropic => anthropic::MODELS,
BackendKind::Openai => &[openai::OPENAI_DEFAULT_MODEL],
BackendKind::Openrouter => openai::OPENROUTER_MODELS,
+ BackendKind::Gemini => openai::GEMINI_MODELS,
BackendKind::OpenaiCompatible => &[],
BackendKind::AgentCli => &["claude-code", "codex", "gemini-cli", "opencode", "antigravity", "hermes", "maki"],
}
}
pub fn needs_key(self) -> bool {
- matches!(self, BackendKind::Anthropic | BackendKind::Openai | BackendKind::Openrouter)
+ matches!(self, BackendKind::Anthropic | BackendKind::Openai | BackendKind::Openrouter | BackendKind::Gemini)
}
}
@@ -637,6 +641,7 @@ impl AiHub {
}
BackendKind::Openai => Box::new(openai::OpenAi::official(base, self.key_for(e)?, &c.model)),
BackendKind::Openrouter => Box::new(openai::OpenAi::openrouter(base, self.key_for(e)?, &c.model)),
+ BackendKind::Gemini => Box::new(openai::OpenAi::gemini(base, self.key_for(e)?, &c.model)),
BackendKind::OpenaiCompatible => {
let mut o = openai::OpenAi::compatible(&c.name, base.unwrap_or("http://127.0.0.1:11434/v1"), self.keys.get(&c.name), &c.model);
o.react = e.options.react_fallback.unwrap_or(false);
@@ -758,6 +763,7 @@ impl AiHub {
BackendKind::Anthropic => anthropic::Anthropic::new(base, self.key_for(&e)?, &e.config.model).list_models(t),
BackendKind::Openai => openai::OpenAi::official(base, self.key_for(&e)?, &e.config.model).list_models(t),
BackendKind::Openrouter => openai::OpenAi::openrouter(base, self.key_for(&e)?, &e.config.model).list_models(t),
+ BackendKind::Gemini => openai::OpenAi::gemini(base, self.key_for(&e)?, &e.config.model).list_models(t),
BackendKind::OpenaiCompatible => {
if e.config.ssh_host.as_deref().is_some_and(|h| !h.trim().is_empty()) {
return Err(ToolError::unavailable("Listing models on a remote server isn't supported yet.").with_remediation("Type the model name."));
@@ -859,6 +865,7 @@ fn reach(e: &BackendEntry, key: Option<String>) -> Result<u64, String> {
BackendKind::Anthropic => anthropic::Anthropic::new(base, need(key)?, &e.config.model).list_models(t).map(|_| ()).map_err(|x| x.message),
BackendKind::Openai => openai::OpenAi::official(base, need(key)?, &e.config.model).list_models(t).map(|_| ()).map_err(|x| x.message),
BackendKind::Openrouter => openai::OpenAi::openrouter(base, need(key)?, &e.config.model).list_models(t).map(|_| ()).map_err(|x| x.message),
+ BackendKind::Gemini => openai::OpenAi::gemini(base, need(key)?, &e.config.model).list_models(t).map(|_| ()).map_err(|x| x.message),
BackendKind::OpenaiCompatible | BackendKind::AgentCli if e.config.ssh_host.as_deref().is_some_and(|h| !h.trim().is_empty()) => {
let host = e.config.ssh_host.clone().unwrap_or_default();
craft_anything_platform::Process::new("ssh")
diff --git a/crates/ai/src/openai.rs b/crates/ai/src/openai.rs
index a534eb5d..54c59629 100644
--- a/crates/ai/src/openai.rs
+++ b/crates/ai/src/openai.rs
@@ -10,12 +10,17 @@ use std::collections::BTreeMap;
pub const OPENAI_BASE: &str = "https://api.openai.com/v1";
pub const OPENROUTER_BASE: &str = "https://openrouter.ai/api/v1";
+/// Gemini's OpenAI-compatible endpoint, for a Google AI Studio key.
+pub const GEMINI_BASE: &str = "https://generativelanguage.googleapis.com/v1beta/openai";
pub const OPENAI_DEFAULT_MODEL: &str = "gpt-5";
pub const OPENROUTER_DEFAULT_MODEL: &str = "anthropic/claude-opus-5-5";
/// Kimi K3.1 Pro (Moonshot AI) on OpenRouter: 1M-token context, tool calls.
pub const KIMI_K3_1: &str = "moonshotai/kimi-k3.1-pro";
/// OpenRouter models offered when adding it.
pub const OPENROUTER_MODELS: &[&str] = &[OPENROUTER_DEFAULT_MODEL, KIMI_K3_1];
+/// Gemini models offered when adding a Google AI Studio key; the live list comes from the key.
+pub const GEMINI_DEFAULT_MODEL: &str = "gemini-3.8-flash";
+pub const GEMINI_MODELS: &[&str] = &[GEMINI_DEFAULT_MODEL, "gemini-4-argon"];
/// Generic app identification for OpenRouter (no personal data).
pub const APP_REFERER: &str = "https://github.com/storytold/craft-anything";
pub const APP_TITLE: &str = "Craft Anything";
@@ -56,6 +61,11 @@ impl OpenAi {
o
}
+ /// A Google AI Studio (Gemini API) key, through Gemini's OpenAI-compatible endpoint.
+ pub fn gemini(base: Option<&str>, key: String, model: &str) -> Self {
+ Self::compatible("Gemini", base.unwrap_or(GEMINI_BASE), Some(key), if model.is_empty() { GEMINI_DEFAULT_MODEL } else { model })
+ }
+
pub fn compatible(label: &str, base: &str, key: Option<String>, model: &str) -> Self {
OpenAi {
label: label.to_string(),
@@ -664,6 +674,15 @@ mod tests {
/// #369: OpenRouter routes to an upstream provider per request, and one without tool calling
/// answered "400: Provider returned error". With tools in the request, only providers that
/// support every parameter are used; nothing else changes.
+ #[test]
+ fn gemini_uses_the_google_endpoint_with_a_bearer_key() {
+ let g = OpenAi::gemini(None, "k".into(), "");
+ assert_eq!(g.url("chat/completions"), format!("{GEMINI_BASE}/chat/completions"));
+ assert_eq!(g.model, GEMINI_DEFAULT_MODEL);
+ assert!(g.headers().contains(&("authorization", "Bearer k".to_string())));
+ assert!(!g.openrouter);
+ }
+
#[test]
fn openrouter_only_routes_a_tool_request_to_providers_that_support_tools() {
use crate::model::{Effort, ToolDef, TurnRequest};
diff --git a/crates/ui-egui/src/first_run.rs b/crates/ui-egui/src/first_run.rs
index babd523c..6ad37120 100644
--- a/crates/ui-egui/src/first_run.rs
+++ b/crates/ui-egui/src/first_run.rs
@@ -169,7 +169,7 @@ fn brain(app: &mut CraftApp, ui: &mut Ui, width: f32) {
fn kind_icon(k: BackendKind) -> &'static str {
match k {
- BackendKind::Anthropic | BackendKind::Openai | BackendKind::Openrouter => "key-round",
+ BackendKind::Anthropic | BackendKind::Openai | BackendKind::Openrouter | BackendKind::Gemini => "key-round",
BackendKind::OpenaiCompatible => "server",
BackendKind::AgentCli => "terminal",
}
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index 6e98abbc..ff6b6aa2 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -263,6 +263,10 @@ Owner: open · Status: todo
- [ ] Check #369 live with Kimi K3 and a key limited to Kimi/Moonshot models (needs the person's
OpenRouter key): that the build starts, or that the error now names the provider's reason
- [ ] A live API-key build (needs a key on the test machine)
+- [x] A Google AI Studio (Gemini API) key is its own choice in Settings → AI and first run (#646):
+ the `gemini` backend kind uses Gemini's OpenAI-compatible endpoint and `GEMINI_API_KEY`
+- [ ] Check a Gemini build live with a real Google AI Studio key (tool calls through the
+ OpenAI-compatible endpoint)
- [ ] Failure logs for the other ways an AI stops (API backends, MCP server start-up), like the
ones agent CLIs write to `logs/` in the config folder
diff --git a/docs/STATUS.md b/docs/STATUS.md
index 2112f785..90d20849 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -232,7 +232,7 @@ laptops are covered by unit tests on fake `/sys/class/drm` folders only.
own permissions. It hasn't been run as a `maki -p` builder here.
- Claude Code is fully wired as a builder; Codex works through its own flags (our tools are pre-approved for
`codex exec`, see `docs/codex.md`) but is unverified on a real install and doesn't resume; other agent programs get plain text in and out. API-key
- backends (Anthropic, OpenRouter, OpenAI, local; Kimi K3.1 Pro through OpenRouter is offered by Detect; a backend chosen as the Extractor takes a Deep-mode game's own files out into `app/Original/` after the study) build with our own loop and the Build tools,
+ backends (Anthropic, OpenRouter, OpenAI, Gemini through a Google AI Studio key, local; Kimi K3.1 Pro through OpenRouter is offered by Detect; a backend chosen as the Extractor takes a Deep-mode game's own files out into `app/Original/` after the study) build with our own loop and the Build tools,
trim and summarise long conversations and keep them in `transcript.json`; no live API build has
been run yet.
- Website checks compare the start page and up to 12 linked pages at the configured widths; the