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.
This commit is contained in:
committed by
GitHub
parent
a4314c189b
commit
2031e3b4a8
@@ -123,6 +123,61 @@ export const registerOpenCodeProxy = (app, deps) => {
|
||||
realpath: fs?.promises?.realpath?.bind(fs.promises),
|
||||
});
|
||||
|
||||
const hasParsedBodyValue = (body) => {
|
||||
if (body === undefined || body === null) return false;
|
||||
if (Buffer.isBuffer(body)) return body.length > 0;
|
||||
if (typeof body === 'string') return body.length > 0;
|
||||
if (Array.isArray(body)) return body.length > 0;
|
||||
if (typeof body === 'object') return Object.keys(body).length > 0;
|
||||
return true;
|
||||
};
|
||||
|
||||
const getContentType = (proxyReq, req) => {
|
||||
const value = proxyReq.getHeader?.('content-type') ?? req.headers?.['content-type'] ?? '';
|
||||
if (Array.isArray(value)) return value[0] || '';
|
||||
return String(value || '');
|
||||
};
|
||||
|
||||
const serializeUrlEncodedBody = (body) => {
|
||||
if (!body || typeof body !== 'object' || Buffer.isBuffer(body)) {
|
||||
return String(body ?? '');
|
||||
}
|
||||
|
||||
const params = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(body)) {
|
||||
if (value === undefined || value === null) continue;
|
||||
if (Array.isArray(value)) {
|
||||
for (const entry of value) {
|
||||
if (entry !== undefined && entry !== null) params.append(key, String(entry));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
params.append(key, String(value));
|
||||
}
|
||||
return params.toString();
|
||||
};
|
||||
|
||||
const serializeParsedBody = (req, proxyReq) => {
|
||||
if (req.method === 'GET' || req.method === 'HEAD') return null;
|
||||
if (req.body === undefined || req.body === null) return null;
|
||||
const originalContentLength = Number.parseInt(req.headers?.['content-length'] || '0', 10) || 0;
|
||||
if (!hasParsedBodyValue(req.body) && originalContentLength <= 0) return null;
|
||||
|
||||
const contentType = getContentType(proxyReq, req).toLowerCase();
|
||||
if (Buffer.isBuffer(req.body)) return req.body;
|
||||
if (contentType.includes('application/json')) return Buffer.from(JSON.stringify(req.body));
|
||||
if (contentType.includes('application/x-www-form-urlencoded')) return Buffer.from(serializeUrlEncodedBody(req.body));
|
||||
if (typeof req.body === 'string') return Buffer.from(req.body);
|
||||
return null;
|
||||
};
|
||||
|
||||
const replayParsedBody = (proxyReq, req) => {
|
||||
const body = serializeParsedBody(req, proxyReq);
|
||||
if (!body) return;
|
||||
proxyReq.setHeader('content-length', String(body.length));
|
||||
proxyReq.write(body);
|
||||
};
|
||||
|
||||
const normalizeProxyTarget = (candidate) => {
|
||||
if (typeof candidate !== 'string') {
|
||||
return null;
|
||||
@@ -417,7 +472,7 @@ export const registerOpenCodeProxy = (app, deps) => {
|
||||
// Dynamic target — port can change after restart
|
||||
router: () => resolveProxyTarget(),
|
||||
on: {
|
||||
proxyReq: (proxyReq) => {
|
||||
proxyReq: (proxyReq, req) => {
|
||||
// Inject OpenCode auth headers
|
||||
const authHeaders = getOpenCodeAuthHeaders();
|
||||
if (authHeaders.Authorization) {
|
||||
@@ -427,6 +482,8 @@ export const registerOpenCodeProxy = (app, deps) => {
|
||||
// Defensive: request identity encoding from upstream OpenCode.
|
||||
// This avoids compressed-body/header mismatches in multi-proxy setups.
|
||||
proxyReq.setHeader('accept-encoding', 'identity');
|
||||
|
||||
replayParsedBody(proxyReq, req);
|
||||
},
|
||||
proxyRes: (proxyRes) => {
|
||||
for (const key of Object.keys(proxyRes.headers || {})) {
|
||||
|
||||
Reference in New Issue
Block a user