[PR #10161] fix(desktop): support browser-only dev mode #13359

Closed
opened 2026-02-16 18:18:13 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/10161

State: closed
Merged: No


Description

Fixes #10160

The desktop package README documents a web-only development mode (bun run --cwd packages/desktop dev) but the code was calling Tauri APIs unconditionally, causing crashes when running in a browser.

Changes

New Files

  • src/utils/platform.ts - Centralized Tauri context detection utilities

    • isTauri constant to detect Tauri vs browser context
    • getOSType() helper for safe OS type detection
    • safeInvoke() helper for safe Tauri command invocation
  • src/utils/constants.ts - Server configuration constants

    • DEFAULT_SERVER_PORT and DEFAULT_SERVER_URL
  • src/utils/server-discovery.ts - Server URL resolution

    • getServerUrl() for configurable server URL via env var

Refactored Files

  • src/index.tsx - Updated to use platform utilities

    • Replaced inline isTauri check with import
    • Replaced ostype() calls with getOSType()
    • Simplified OS type detection logic
    • Markdown parser fallback: When not in Tauri, parseMarkdown returns undefined so MarkedProvider uses the JS marked library instead of the native Rust parser
  • src/menu.ts - Updated to use platform utilities

    • Replaced inline isTauri check with import
    • Replaced ostype() call with getOSType()
  • src/webview-zoom.ts - Updated to use platform utilities

    • Replaced inline isTauri check with import
    • Replaced ostype() call with getOSType()
  • vite.config.ts - Added VITE_OPENCODE_SERVER_URL env var exposure

Benefits

  1. Enables documented workflow - Browser-only dev mode now works as documented
  2. DRY principle - Single source of truth for Tauri detection
  3. Type safety - Proper TypeScript types for all helpers
  4. Maintainability - Easier to update Tauri detection logic in one place
  5. Consistency - All files use the same detection mechanism
  6. Markdown rendering - Works in browser mode via JS parser fallback

Usage

  • Default: Server on port 4096 (auto-detected)
  • Custom: VITE_OPENCODE_SERVER_URL=http://127.0.0.1:PORT bun run dev

Testing

Tested in both modes:

  • Browser mode: bun run --cwd packages/desktop devhttp://localhost:1420
  • Tauri mode: bun run --cwd packages/desktop tauri dev
  • Markdown rendering works in browser mode (JS parser fallback)

Before (Browser Mode)

Multiple crashes:

TypeError: Cannot read properties of undefined (reading 'os_type')
TypeError: Cannot read properties of undefined (reading 'invoke')

After (Browser Mode)

Loads successfully
Connects to backend server
All features gracefully degrade when Tauri APIs unavailable
Markdown renders correctly using JS parser

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/10161 **State:** closed **Merged:** No --- ## Description Fixes #10160 The desktop package README documents a web-only development mode (`bun run --cwd packages/desktop dev`) but the code was calling Tauri APIs unconditionally, causing crashes when running in a browser. ## Changes ### New Files - **`src/utils/platform.ts`** - Centralized Tauri context detection utilities - `isTauri` constant to detect Tauri vs browser context - `getOSType()` helper for safe OS type detection - `safeInvoke()` helper for safe Tauri command invocation - **`src/utils/constants.ts`** - Server configuration constants - `DEFAULT_SERVER_PORT` and `DEFAULT_SERVER_URL` - **`src/utils/server-discovery.ts`** - Server URL resolution - `getServerUrl()` for configurable server URL via env var ### Refactored Files - **`src/index.tsx`** - Updated to use platform utilities - Replaced inline `isTauri` check with import - Replaced `ostype()` calls with `getOSType()` - Simplified OS type detection logic - **Markdown parser fallback**: When not in Tauri, `parseMarkdown` returns `undefined` so `MarkedProvider` uses the JS `marked` library instead of the native Rust parser - **`src/menu.ts`** - Updated to use platform utilities - Replaced inline `isTauri` check with import - Replaced `ostype()` call with `getOSType()` - **`src/webview-zoom.ts`** - Updated to use platform utilities - Replaced inline `isTauri` check with import - Replaced `ostype()` call with `getOSType()` - **`vite.config.ts`** - Added `VITE_OPENCODE_SERVER_URL` env var exposure ## Benefits 1. **Enables documented workflow** - Browser-only dev mode now works as documented 2. **DRY principle** - Single source of truth for Tauri detection 3. **Type safety** - Proper TypeScript types for all helpers 4. **Maintainability** - Easier to update Tauri detection logic in one place 5. **Consistency** - All files use the same detection mechanism 6. **Markdown rendering** - Works in browser mode via JS parser fallback ## Usage - Default: Server on port 4096 (auto-detected) - Custom: `VITE_OPENCODE_SERVER_URL=http://127.0.0.1:PORT bun run dev` ## Testing Tested in both modes: - ✅ Browser mode: `bun run --cwd packages/desktop dev` → http://localhost:1420 - ✅ Tauri mode: `bun run --cwd packages/desktop tauri dev` - ✅ Markdown rendering works in browser mode (JS parser fallback) ## Before (Browser Mode) Multiple crashes: ``` TypeError: Cannot read properties of undefined (reading 'os_type') TypeError: Cannot read properties of undefined (reading 'invoke') ``` ## After (Browser Mode) ✅ Loads successfully ✅ Connects to backend server ✅ All features gracefully degrade when Tauri APIs unavailable ✅ Markdown renders correctly using JS parser
yindo added the pull-request label 2026-02-16 18:18:13 -05:00
yindo closed this issue 2026-02-16 18:18:13 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13359