Files
openchamber/packages/ui/src/lib/desktopCurrentHost.ts
T
Bohdan Triapitsyn f4743ea060 feat(chat): work-status panel, and MCP auth and settings fixes (#2776)
Adds a work-status panel beside the transcript. Context fill, model and
cost, todos, running subagents and the permission requests blocking
them, branch and working-tree state, MCP servers, pinned messages and
context sources were scattered across the header, the composer and the
context panel — a blocked subagent was reported nowhere at all. The
panel reads them from live channels rather than persisted history, and
becomes an overlay where the chat is too narrow to seat a column.

It is on by default, including for existing installs. Because it now
carries these readouts, the desktop header and composer drop the ones it
duplicates: todo and changed-files chips, usage and MCP tabs. VS Code
and mobile keep theirs — neither hosts the panel.

Fixes MCP authorization, which was broken from the panel, invalidated by
a directory switch through a redirect URI that encoded the working
directory, and left the desktop app in the background because browsers
will not follow a custom-protocol link without a user gesture. The
settings page no longer asks the user to understand the MCP spec before
adding a server: one field takes the command or the link, with the kind
inferred and a visible override, and client-registration fields appear
only when a server actually asks for its own credentials.

Also: skills load from the panel instead of only when the composer's
slash autocomplete opens; the header button names the current instance
rather than falling through to the word "Instance" for relay hosts.

Three new optional UI settings keys, all migrated. No change to stored
MCP server configuration.
2026-08-09 19:30:25 +03:00

98 lines
3.5 KiB
TypeScript

import {
getDesktopHostApiUrl,
locationMatchesHost,
normalizeHostUrl,
redactSensitiveUrl,
type DesktopHost,
} from '@/lib/desktopHosts';
import { getRuntimeApiBaseUrl, getRuntimeKey } from '@/lib/runtime-switch';
/**
* Which configured instance the window is actually talking to.
*
* This lives outside the host switcher because the header names the same
* instance on its button. When the header carried its own copy of the matching
* rules it was missing the relay branch, so a relay instance — whose API base
* is the window origin and therefore matches no host URL — fell through to the
* word "Instance" while the switcher two clicks away named it correctly.
*/
export const LOCAL_HOST_ID = 'local';
export const buildLocalDesktopHost = (localOrigin?: string | null): DesktopHost => ({
id: LOCAL_HOST_ID,
label: 'Local',
url: localOrigin || getLocalDesktopOrigin(),
});
export const getLocalDesktopOrigin = (): string => {
if (typeof window === 'undefined') return '';
return window.__OPENCHAMBER_LOCAL_ORIGIN__ || window.location.origin;
};
export const runtimeKeyForDesktopHost = (host: DesktopHost): string => {
if (host.id === LOCAL_HOST_ID) return 'local';
return `host:${host.id}`;
};
type ResolvedDesktopHost = {
id: string;
label: string;
url: string;
};
export const resolveCurrentDesktopHost = (hosts: DesktopHost[]): ResolvedDesktopHost => {
const currentHref = typeof window === 'undefined' ? '' : window.location.href;
const localOrigin = hosts.find((host) => host.id === LOCAL_HOST_ID)?.url || getLocalDesktopOrigin();
const runtimeApiBaseUrl = getRuntimeApiBaseUrl();
const normalizedLocal = normalizeHostUrl(localOrigin) || localOrigin;
const normalizedCurrent = normalizeHostUrl(currentHref) || currentHref;
// Relay hosts share the window origin as their (virtual) API base, so URL
// matching can't distinguish them — identify the active relay host by its
// stable runtime key instead.
const activeRuntimeKey = getRuntimeKey();
const relayMatch = hosts.find((host) => host.relay && runtimeKeyForDesktopHost(host) === activeRuntimeKey);
if (relayMatch) {
return { id: relayMatch.id, label: relayMatch.label, url: relayMatch.url };
}
if (runtimeApiBaseUrl && locationMatchesHost(runtimeApiBaseUrl, localOrigin)) {
return { id: LOCAL_HOST_ID, label: 'Local', url: normalizedLocal };
}
const runtimeMatch = hosts.find((host) => (
runtimeApiBaseUrl ? locationMatchesHost(runtimeApiBaseUrl, getDesktopHostApiUrl(host)) : false
));
if (runtimeMatch) {
return {
id: runtimeMatch.id,
label: runtimeMatch.label,
url: normalizeHostUrl(getDesktopHostApiUrl(runtimeMatch)) || getDesktopHostApiUrl(runtimeMatch),
};
}
if (currentHref && locationMatchesHost(currentHref, localOrigin)) {
return { id: LOCAL_HOST_ID, label: 'Local', url: normalizedLocal };
}
const match = hosts.find((host) => (currentHref ? locationMatchesHost(currentHref, host.url) : false));
if (match) {
return { id: match.id, label: match.label, url: normalizeHostUrl(match.url) || match.url };
}
if (currentHref.startsWith('openchamber-ui://')) {
return { id: LOCAL_HOST_ID, label: 'Local', url: normalizedLocal };
}
// Nothing configured matches. Naming the address is still more use than the
// bare word "Instance"; the redaction strips anything credential-shaped.
return {
id: 'custom',
label: redactSensitiveUrl(normalizedCurrent || 'Instance'),
url: normalizedCurrent,
};
};