chimera-opencode 0.2.1

OpenCode/OpenAI-compatible REST backend for the chimera AI agent SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::path::{Path, PathBuf};

use bon::Builder;

use chimera_core::*;

use crate::agent_session::OpenCodeAgentSession;
use crate::config::{OpenCodeConfig, OpenCodeExecutionMode, OpenCodeProvider};
use crate::session::OpenCodeSession;

#[derive(Debug, Clone, Builder)]
pub struct OpenCodeBackend {
    #[builder(into)]
    pub(crate) api_key: Option<String>,

    pub(crate) provider: Option<OpenCodeProvider>,

    /// Path to the `opencode` CLI binary used by agent mode.
    pub(crate) binary_path: Option<PathBuf>,
}

impl OpenCodeBackend {
    /// Create a backend, reading API key from env vars or ~/.local/share/opencode/auth.json.
    ///
    /// Priority:
    /// 1. `OPENCODE_ZEN_KEY` or `OPENCODE_API_KEY` env vars (Zen provider)
    /// 2. `OPENCODE_GO_API_KEY` env var (Go provider)
    /// 3. `OPENROUTER_API_KEY` env var (OpenRouter direct provider)
    /// 4. `auth.json` provider records (`opencode` or legacy `opencode-go`)
    pub fn from_env() -> Self {
        from_env_with(|key| std::env::var(key).ok(), load_auth_json_api_key)
    }
}

fn from_env_with(
    get_env: impl Fn(&str) -> Option<String>,
    load_auth_json_key: impl Fn() -> Option<(String, Option<OpenCodeProvider>)>,
) -> OpenCodeBackend {
    // Zen env vars
    if let Some(key) = get_env("OPENCODE_ZEN_KEY")
        .filter(|s| !s.is_empty())
        .or_else(|| get_env("OPENCODE_API_KEY").filter(|s| !s.is_empty()))
    {
        return OpenCodeBackend {
            api_key: Some(key),
            provider: None,
            binary_path: None,
        };
    }
    // Go env var
    if let Some(key) = get_env("OPENCODE_GO_API_KEY").filter(|s| !s.is_empty()) {
        return OpenCodeBackend {
            api_key: Some(key),
            provider: Some(crate::config::OpenCodeProvider::Go),
            binary_path: None,
        };
    }
    // OpenRouter env var
    if let Some(key) = get_env("OPENROUTER_API_KEY").filter(|s| !s.is_empty()) {
        return OpenCodeBackend {
            api_key: Some(key),
            provider: Some(crate::config::OpenCodeProvider::Direct {
                base_url: "https://openrouter.ai/api/v1".into(),
            }),
            binary_path: None,
        };
    }
    // auth.json fallback
    if let Some((key, provider)) = load_auth_json_key() {
        return OpenCodeBackend {
            api_key: Some(key),
            provider,
            binary_path: None,
        };
    }
    OpenCodeBackend {
        api_key: None,
        provider: None,
        binary_path: None,
    }
}

impl OpenCodeBackend {
    /// Locate the `opencode` binary on PATH or common install locations.
    pub fn from_path() -> Result<Self> {
        let binary_path = find_opencode_binary()?;
        Ok(Self {
            api_key: None,
            provider: None,
            binary_path: Some(binary_path),
        })
    }
}

fn find_opencode_binary() -> Result<PathBuf> {
    if let Ok(path) = std::env::var("CHIMERA_OPENCODE_BINARY") {
        let path = PathBuf::from(path);
        if path.exists() {
            return Ok(path);
        }
    }

    let home = std::env::var("HOME").unwrap_or_default();

    // Prefer the installed binary. A local source checkout is only safe when
    // its workspace dependencies are installed; otherwise `bun run` exits at
    // startup before the server is ready.
    if let Ok(path) = which_opencode() {
        return Ok(path);
    }

    let source_candidates: &[&str] = &[
        "~/projects/opencode/packages/opencode",
        "~/opencode/packages/opencode",
    ];
    for raw in source_candidates {
        let expanded = raw.replacen("~", &home, 1);
        let path = PathBuf::from(&expanded);
        if is_runnable_opencode_source(&path) {
            return Ok(path);
        }
    }

    // Common install locations.
    let binary_candidates: &[&str] = &[
        "~/.npm-global/bin/opencode",
        "~/.local/bin/opencode",
        "/usr/local/bin/opencode",
        "~/.yarn/bin/opencode",
        "~/.bun/bin/opencode",
    ];
    for raw in binary_candidates {
        let expanded = raw.replacen("~", &home, 1);
        let path = PathBuf::from(&expanded);
        if path.exists() {
            return Ok(path);
        }
    }
    Err(AgentError::BinaryNotFound {
        binary: "opencode".into(),
        searched: source_candidates
            .iter()
            .chain(binary_candidates.iter())
            .map(PathBuf::from)
            .collect(),
    })
}

fn is_runnable_opencode_source(path: &Path) -> bool {
    path.join("src/index.ts").exists() && find_ancestor_node_module(path, "@opencode-ai/core")
}

fn find_ancestor_node_module(path: &Path, module: &str) -> bool {
    path.ancestors()
        .any(|ancestor| ancestor.join("node_modules").join(module).exists())
}

/// Load an OpenCode API key from ~/.local/share/opencode/auth.json.
///
/// Current installs write typed provider records such as:
/// `{ "opencode": { "type": "api", "key": "sk-..." } }`
/// Older installs may have used `opencode-go` or omitted the `type` field.
fn load_auth_json_api_key() -> Option<(String, Option<OpenCodeProvider>)> {
    let home = std::env::var("HOME").ok()?;
    let path = std::path::Path::new(&home).join(".local/share/opencode/auth.json");
    let text = std::fs::read_to_string(&path).ok()?;
    let v: serde_json::Value = serde_json::from_str(&text).ok()?;

    parse_auth_json_api_key(&v)
}

fn parse_auth_json_api_key(v: &serde_json::Value) -> Option<(String, Option<OpenCodeProvider>)> {
    for (provider_key, provider) in [
        ("opencode", None),
        ("opencode-go", Some(crate::config::OpenCodeProvider::Go)),
    ] {
        let entry = &v[provider_key];
        let Some(key) = entry.get("key").and_then(|value| value.as_str()) else {
            continue;
        };
        let key = key.trim();
        let entry_type = entry.get("type").and_then(|value| value.as_str());
        if key.is_empty() {
            continue;
        }
        if entry_type.is_none() || entry_type == Some("api") {
            return Some((key.to_string(), provider));
        }
    }

    None
}

fn which_opencode() -> std::result::Result<PathBuf, ()> {
    let path_var = std::env::var("PATH").map_err(|_| ())?;
    for dir in std::env::split_paths(&path_var) {
        let candidate = dir.join("opencode");
        if candidate.exists() {
            return Ok(candidate);
        }
    }
    Err(())
}

impl Backend for OpenCodeBackend {
    type Config = OpenCodeConfig;
    type Session = OpenCodeSession;

    fn name(&self) -> &'static str {
        "opencode"
    }

    fn capabilities(&self) -> BackendCapabilities {
        BackendCapabilities {
            input_images: CapabilitySupport::Unsupported,
            output_schema: CapabilitySupport::ConfigDependent,
            interrupt: CapabilitySupport::ConfigDependent,
            resume: ResumeSemantics::Stateful,
            mcp: McpSupport::MergeAndExplicitOnly,
            tool_call_events: CapabilitySupport::Supported,
        }
    }

    async fn session(&self, config: SessionConfig<OpenCodeConfig>) -> Result<OpenCodeSession> {
        if config.backend.execution_mode == OpenCodeExecutionMode::Agent {
            let binary_path = match self.binary_path.clone() {
                Some(p) => p,
                None => find_opencode_binary()?,
            };
            let agent = OpenCodeAgentSession::new(binary_path, config).await?;
            return Ok(OpenCodeSession::Agent(Box::new(agent)));
        }

        validate_http_config(&config.backend)?;

        let api_key = config
            .backend
            .api_key
            .clone()
            .or_else(|| self.api_key.clone());
        let provider = self
            .provider
            .clone()
            .unwrap_or_else(|| config.backend.provider.clone());

        Ok(OpenCodeSession::Http(Box::new(
            crate::session::HttpSession::new(api_key, provider, config),
        )))
    }

    async fn resume(
        &self,
        session_id: &str,
        config: SessionConfig<OpenCodeConfig>,
    ) -> Result<OpenCodeSession> {
        if config.backend.execution_mode == OpenCodeExecutionMode::Agent {
            let binary_path = match self.binary_path.clone() {
                Some(p) => p,
                None => find_opencode_binary()?,
            };
            let agent = OpenCodeAgentSession::resume(binary_path, config, session_id).await?;
            return Ok(OpenCodeSession::Agent(Box::new(agent)));
        }

        validate_http_config(&config.backend)?;

        let api_key = config
            .backend
            .api_key
            .clone()
            .or_else(|| self.api_key.clone());
        let provider = self
            .provider
            .clone()
            .unwrap_or_else(|| config.backend.provider.clone());

        Ok(OpenCodeSession::Http(Box::new(
            crate::session::HttpSession::new(api_key, provider, config)
                .with_session_id(session_id.to_owned()),
        )))
    }
}

fn validate_http_config(config: &OpenCodeConfig) -> Result<()> {
    if !config.mcp_servers.is_empty()
        || config.variant.is_some()
        || config.agent_base_url.is_some()
        || config.session_title.is_some()
    {
        return Err(AgentError::Other {
            message: "opencode agent-only options require execution_mode = agent".into(),
            source: None,
        });
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn backend_name() {
        let backend = OpenCodeBackend::from_env();
        assert_eq!(backend.name(), "opencode");
        assert_eq!(
            backend.capabilities().output_schema,
            CapabilitySupport::ConfigDependent
        );
    }

    #[tokio::test]
    async fn session_merges_api_key() {
        let backend = OpenCodeBackend::builder().api_key("backend-key").build();
        let config = SessionConfig::builder()
            .backend(OpenCodeConfig::default())
            .build();

        let session = backend.session(config).await.unwrap();
        let crate::session::OpenCodeSession::Http(http) = session else {
            panic!("expected Http session");
        };
        assert_eq!(http.api_key(), Some("backend-key"));
    }

    #[tokio::test]
    async fn session_config_overrides_backend_key() {
        let backend = OpenCodeBackend::builder().api_key("backend-key").build();
        let config = SessionConfig::builder()
            .backend(OpenCodeConfig::builder().api_key("session-key").build())
            .build();

        let session = backend.session(config).await.unwrap();
        let crate::session::OpenCodeSession::Http(http) = session else {
            panic!("expected Http session");
        };
        assert_eq!(http.api_key(), Some("session-key"));
    }

    #[test]
    fn from_env_prefers_openrouter_key_when_present() {
        let key = "test-openrouter-key";
        let backend = from_env_with(
            |env_key| (env_key == "OPENROUTER_API_KEY").then(|| key.to_string()),
            || None,
        );
        assert_eq!(backend.api_key.as_deref(), Some(key));
        assert!(matches!(
            backend.provider,
            Some(OpenCodeProvider::Direct { ref base_url })
                if base_url == "https://openrouter.ai/api/v1"
        ));
    }

    #[tokio::test]
    async fn session_rejects_variant_in_http_mode() {
        let backend = OpenCodeBackend::builder().build();
        let config = SessionConfig::builder()
            .backend(OpenCodeConfig::builder().variant("medium").build())
            .build();

        let err = match backend.session(config).await {
            Ok(_) => panic!("expected variant-in-http-mode session to fail"),
            Err(err) => err,
        };
        assert!(
            err.to_string()
                .contains("agent-only options require execution_mode = agent")
        );
    }

    #[tokio::test]
    async fn resume_sets_session_id() {
        let backend = OpenCodeBackend::builder().build();
        let config = SessionConfig::builder()
            .backend(OpenCodeConfig::default())
            .build();

        let session = backend.resume("sess-abc", config).await.unwrap();
        assert_eq!(session.session_id(), Some("sess-abc"));
    }

    #[test]
    fn auth_json_prefers_typed_opencode_record() {
        let parsed = parse_auth_json_api_key(&serde_json::json!({
            "opencode": { "type": "api", "key": "typed-key" },
            "opencode-go": { "type": "api", "key": "go-key" }
        }));

        let (key, provider) = parsed.expect("expected auth fallback");
        assert_eq!(key, "typed-key");
        assert!(provider.is_none());
    }

    #[test]
    fn auth_json_can_select_legacy_go_provider() {
        let parsed = parse_auth_json_api_key(&serde_json::json!({
            "opencode-go": { "key": "go-key" }
        }));

        let (key, provider) = parsed.expect("expected auth fallback");
        assert_eq!(key, "go-key");
        assert!(matches!(provider, Some(OpenCodeProvider::Go)));
    }

    #[test]
    fn auth_json_ignores_non_api_records_for_fallback() {
        let parsed = parse_auth_json_api_key(&serde_json::json!({
            "opencode": { "type": "oauth", "access": "x", "refresh": "y", "expires": 1 }
        }));

        assert!(parsed.is_none());
    }

    #[test]
    fn opencode_source_is_not_runnable_without_workspace_deps() {
        let temp = tempfile::tempdir().unwrap();
        let package = temp.path().join("packages/opencode");
        std::fs::create_dir_all(package.join("src")).unwrap();
        std::fs::write(package.join("src/index.ts"), "").unwrap();

        assert!(!is_runnable_opencode_source(&package));
    }

    #[test]
    fn opencode_source_is_runnable_when_workspace_deps_exist() {
        let temp = tempfile::tempdir().unwrap();
        let package = temp.path().join("packages/opencode");
        std::fs::create_dir_all(package.join("src")).unwrap();
        std::fs::write(package.join("src/index.ts"), "").unwrap();
        std::fs::create_dir_all(temp.path().join("node_modules/@opencode-ai/core")).unwrap();

        assert!(is_runnable_opencode_source(&package));
    }

    #[tokio::test]
    async fn resume_rejects_variant_in_http_mode() {
        let backend = OpenCodeBackend::builder().build();
        let config = SessionConfig::builder()
            .backend(OpenCodeConfig::builder().variant("medium").build())
            .build();

        let err = match backend.resume("sess-abc", config).await {
            Ok(_) => panic!("expected variant-in-http-mode resume to fail"),
            Err(err) => err,
        };
        assert!(
            err.to_string()
                .contains("agent-only options require execution_mode = agent")
        );
    }
}