Files
openchamber/packages/ui/src/lib/execCommands.ts
T
Bohdan Triapitsyn 2031e3b4a8 Decouple bundled UI from runtime API and add remote instance tooling (#1228)
Add a packaged-client runtime boundary so the shared UI can talk to local,
desktop, remote, and VS Code runtimes through the right transport instead of
assuming one same-origin web server.

Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and
runtime URL helpers, while keeping official OpenCode traffic on the SDK path.
Support runtime switching, remote host selection, desktop client credentials,
and headless connection links for pairing packaged clients with remote
OpenChamber servers.

Harden the new auth model by moving long-lived client tokens out of browser
URLs, introducing short-lived scoped URL tokens for browser-owned transports,
restricting URL-token access to explicit readable/realtime routes, and making
client-token management session-scoped or self-scoped as appropriate.

Update browser-owned assets and preview proxy flows to work with the split
runtime model, including authenticated project icons, preview token propagation,
CSP-safe preview bridge injection, and preview proxy auth that survives
short-lived URL-token expiry.

Tighten Electron security boundaries for packaged clients by gating privileged
preload state to trusted origins and requiring explicit confirmation before
connect deep-links import or switch remote runtimes.

Also refresh agent guidance and project skills so future runtime/API, auth,
preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new
architecture.
2026-06-02 00:43:05 +03:00

59 lines
1.8 KiB
TypeScript

import type { CommandExecResult, FilesAPI } from '@/lib/api/types';
import { getRegisteredRuntimeAPIs } from '@/contexts/runtimeAPIRegistry';
import { runtimeFetch } from '@/lib/runtime-fetch';
type ExecResult = { success: boolean; results: CommandExecResult[] };
const DEFAULT_BASE_URL = import.meta.env.VITE_OPENCODE_URL || '/api';
const getBaseUrl = (): string => {
if (typeof DEFAULT_BASE_URL === 'string' && DEFAULT_BASE_URL.startsWith('/')) {
return DEFAULT_BASE_URL;
}
return DEFAULT_BASE_URL;
};
function getRuntimeFilesAPI(): FilesAPI | null {
const apis = getRegisteredRuntimeAPIs();
if (apis?.files) {
return apis.files;
}
return null;
}
export async function execCommands(commands: string[], cwd: string): Promise<ExecResult> {
const runtimeFiles = getRuntimeFilesAPI();
if (runtimeFiles?.execCommands) {
return runtimeFiles.execCommands(commands, cwd);
}
const response = await runtimeFetch(`${getBaseUrl()}/fs/exec`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ commands, cwd, background: false }),
});
if (!response.ok) {
const error = await response.json().catch(() => ({ error: response.statusText }));
throw new Error((error as { error?: string }).error || 'Command exec failed');
}
const payload = (await response.json().catch(() => null)) as
| { success?: boolean; results?: CommandExecResult[] }
| null;
return {
success: Boolean(payload?.success),
results: Array.isArray(payload?.results) ? payload!.results! : [],
};
}
export async function execCommand(command: string, cwd: string): Promise<CommandExecResult> {
const result = await execCommands([command], cwd);
const first = result.results[0];
if (!first) {
return { command, success: result.success };
}
return first;
}