Skip to content

Commit ebbb4dd

Browse files
authored
fix(desktop): improve server detection & connection logic (anomalyco#7962)
1 parent 087473b commit ebbb4dd

File tree

5 files changed

+180
-198
lines changed

5 files changed

+180
-198
lines changed

packages/app/src/app.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const Loading = () => <div class="size-full flex items-center justify-center tex
3333

3434
declare global {
3535
interface Window {
36-
__OPENCODE__?: { updaterEnabled?: boolean; port?: number; serverReady?: boolean; serverUrl?: string }
36+
__OPENCODE__?: { updaterEnabled?: boolean; }
3737
}
3838
}
3939

@@ -65,19 +65,18 @@ function ServerKey(props: ParentProps) {
6565
)
6666
}
6767

68-
export function AppInterface() {
69-
const defaultServerUrl = iife(() => {
68+
export function AppInterface(props: { defaultUrl?: string }) {
69+
const defaultServerUrl = () => {
70+
if (props.defaultUrl) return props.defaultUrl;
7071
if (location.hostname.includes("opencode.ai")) return "http://localhost:4096"
71-
if (window.__OPENCODE__?.serverUrl) return window.__OPENCODE__.serverUrl
72-
if (window.__OPENCODE__?.port) return `http://127.0.0.1:${window.__OPENCODE__.port}`
7372
if (import.meta.env.DEV)
7473
return `http://${import.meta.env.VITE_OPENCODE_SERVER_HOST ?? "localhost"}:${import.meta.env.VITE_OPENCODE_SERVER_PORT ?? "4096"}`
7574

7675
return window.location.origin
77-
})
76+
};
7877

7978
return (
80-
<ServerProvider defaultUrl={defaultServerUrl}>
79+
<ServerProvider defaultUrl={defaultServerUrl()}>
8180
<ServerKey>
8281
<GlobalSDKProvider>
8382
<GlobalSyncProvider>

packages/desktop/src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "opencode-desktop"
33
version = "0.0.0"
44
description = "The open source AI coding agent"
55
authors = ["Anomaly Innovations"]
6-
edition = "2021"
6+
edition = "2024"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
99

packages/desktop/src-tauri/src/cli.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1-
use tauri::Manager;
1+
use tauri::{path::BaseDirectory, AppHandle, Manager};
2+
use tauri_plugin_shell::{process::Command, ShellExt};
23

34
const CLI_INSTALL_DIR: &str = ".opencode/bin";
45
const CLI_BINARY_NAME: &str = "opencode";
56

7+
#[derive(serde::Deserialize)]
8+
pub struct ServerConfig {
9+
pub hostname: Option<String>,
10+
pub port: Option<u32>,
11+
}
12+
13+
#[derive(serde::Deserialize)]
14+
pub struct Config {
15+
pub server: Option<ServerConfig>,
16+
}
17+
18+
pub async fn get_config(app: &AppHandle) -> Option<Config> {
19+
create_command(app, "debug config")
20+
.output()
21+
.await
22+
.inspect_err(|e| eprintln!("Failed to read OC config: {e}"))
23+
.ok()
24+
.and_then(|out| String::from_utf8(out.stdout.to_vec()).ok())
25+
.and_then(|s| serde_json::from_str::<Config>(&s).ok())
26+
}
27+
628
fn get_cli_install_path() -> Option<std::path::PathBuf> {
729
std::env::var("HOME").ok().map(|home| {
830
std::path::PathBuf::from(home)
@@ -117,3 +139,35 @@ pub fn sync_cli(app: tauri::AppHandle) -> Result<(), String> {
117139

118140
Ok(())
119141
}
142+
143+
fn get_user_shell() -> String {
144+
std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string())
145+
}
146+
147+
pub fn create_command(app: &tauri::AppHandle, args: &str) -> Command {
148+
let state_dir = app
149+
.path()
150+
.resolve("", BaseDirectory::AppLocalData)
151+
.expect("Failed to resolve app local data dir");
152+
153+
#[cfg(target_os = "windows")]
154+
return app
155+
.shell()
156+
.sidecar("opencode-cli")
157+
.unwrap()
158+
.env("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY", "true")
159+
.env("OPENCODE_CLIENT", "desktop")
160+
.env("XDG_STATE_HOME", &state_dir);
161+
162+
#[cfg(not(target_os = "windows"))]
163+
return {
164+
let sidecar = get_sidecar_path(app);
165+
let shell = get_user_shell();
166+
app.shell()
167+
.command(&shell)
168+
.env("OPENCODE_EXPERIMENTAL_ICON_DISCOVERY", "true")
169+
.env("OPENCODE_CLIENT", "desktop")
170+
.env("XDG_STATE_HOME", &state_dir)
171+
.args(["-il", "-c", &format!("\"{}\" {}", sidecar.display(), args)])
172+
};
173+
}

0 commit comments

Comments
 (0)