## What / Why This PR finishes the desktop refactor: the Tauri app is now a thin shell that launches the web server as a sidecar and loads the UI from `http://127.0.0.1:<port>`. All real backend logic lives in `packages/web/server/index.js`; desktop Rust keeps only native integrations (menu/dialog/notifications/updater/deep-link + window chrome). This unblocks: - consistent behavior across web/desktop/vscode (single backend) - simpler desktop maintenance (no duplicated Rust backend) - host switching between Local + remote instances in desktop - reliable cold-start behavior on slow machines (VSCode + desktop) ## Key changes - Desktop sidecar runtime - build pipeline to bundle web dist + `openchamber-server` sidecar (`packages/desktop/scripts/build-sidecar.mjs`) - robust local port selection (prefer saved/default, fallback to random; persisted in `~/.config/openchamber/settings.json`) - improved PATH handling so the sidecar can locate `opencode` CLI (incl `~/.opencode/bin`, overrides, common bins) - disable native right-click context menu in production builds (dev keeps it) - Desktop instance switcher (Tauri-only) - header button + modal to add/edit/delete remote hosts, set default, probe status/ping, switch back to Local escape hatch - auth gate includes host switcher so you can recover when a remote host is broken/auth-required - host list stored desktop-locally (not tied to the currently selected remote server) - Notifications - decision logic moved server-side; desktop notifications emitted via sidecar stdout and shown natively by Tauri - prevent double-notifications on desktop Local origin (UI ignores SSE notification when native path is active) - restore macOS notification sound - Updates - Tauri updater used only when viewing Local instance in desktop shell (avoid “remote web update” triggering desktop restart) - Settings persistence & UX polish - persist model favorites/recents via `/api/config/settings` (works for web + desktop; not origin-dependent) - persist per-project sidebar collapse state in `projects[].sidebarCollapsed` via `/api/config/settings` (with debounce on toggles) - macOS header sizing/traffic-lights offsets fixed (marketing macOS major injected from desktop; MultiRun header aligned) - VSCode cold-start: keep retrying provider/agent loads after connection to avoid empty UI on slow machines - misc lint/type fixes + bun.lock sync - Desktop bootstrap / resiliency - show onboarding screen when OpenCode CLI is missing (desktop Local origin), with retry hook to restart OpenCode after install ## Testing notes - Desktop (macOS): switch Local <-> remote, set default host, verify auth gate recovery, native notifications (with sound), updater gated to Local - Web: favorites/recents + per-project collapsed state persist across reload/restart - VSCode: slow startup no longer results in missing providers/agents/models
526 lines
17 KiB
TypeScript
526 lines
17 KiB
TypeScript
import React from 'react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { ButtonLarge } from '@/components/ui/button-large';
|
|
import { Input } from '@/components/ui/input';
|
|
import { toast } from '@/components/ui';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from '@/components/ui/dropdown-menu';
|
|
import { RiAddLine, RiAiAgentFill, RiAiAgentLine, RiDeleteBinLine, RiFileCopyLine, RiMore2Line, RiRobot2Line, RiRobotLine, RiRestartLine, RiEditLine } from '@remixicon/react';
|
|
import { useAgentsStore, isAgentBuiltIn, isAgentHidden, type AgentScope, type AgentDraft } from '@/stores/useAgentsStore';
|
|
import { useUIStore } from '@/stores/useUIStore';
|
|
import { useDeviceInfo } from '@/lib/device';
|
|
import { isVSCodeRuntime } from '@/lib/desktop';
|
|
import { cn } from '@/lib/utils';
|
|
import type { Agent } from '@opencode-ai/sdk/v2';
|
|
import { ScrollableOverlay } from '@/components/ui/ScrollableOverlay';
|
|
|
|
interface AgentsSidebarProps {
|
|
onItemSelect?: () => void;
|
|
}
|
|
|
|
type PermissionAction = 'allow' | 'ask' | 'deny';
|
|
type PermissionRule = { permission: string; pattern: string; action: PermissionAction };
|
|
|
|
type PermissionConfigValue = PermissionAction | Record<string, PermissionAction>;
|
|
|
|
const toPermissionRuleset = (ruleset: unknown): PermissionRule[] => {
|
|
if (!Array.isArray(ruleset)) {
|
|
return [];
|
|
}
|
|
|
|
const parsed: PermissionRule[] = [];
|
|
for (const entry of ruleset) {
|
|
if (!entry || typeof entry !== 'object') {
|
|
continue;
|
|
}
|
|
const candidate = entry as Partial<PermissionRule>;
|
|
if (typeof candidate.permission !== 'string' || typeof candidate.pattern !== 'string' || typeof candidate.action !== 'string') {
|
|
continue;
|
|
}
|
|
if (candidate.action !== 'allow' && candidate.action !== 'ask' && candidate.action !== 'deny') {
|
|
continue;
|
|
}
|
|
parsed.push({ permission: candidate.permission, pattern: candidate.pattern, action: candidate.action });
|
|
}
|
|
|
|
return parsed;
|
|
};
|
|
|
|
const normalizeRuleset = (ruleset: PermissionRule[]): PermissionRule[] => {
|
|
const map = new Map<string, PermissionRule>();
|
|
for (const rule of ruleset) {
|
|
if (!rule.permission || rule.permission === 'invalid') {
|
|
continue;
|
|
}
|
|
if (!rule.pattern) {
|
|
continue;
|
|
}
|
|
map.set(`${rule.permission}::${rule.pattern}`, rule);
|
|
}
|
|
return Array.from(map.values());
|
|
};
|
|
|
|
const rulesetToPermissionConfig = (ruleset: unknown): AgentDraft['permission'] => {
|
|
const parsed = normalizeRuleset(toPermissionRuleset(ruleset));
|
|
if (parsed.length === 0) {
|
|
return undefined;
|
|
}
|
|
|
|
const byPermission: Record<string, Record<string, PermissionAction>> = {};
|
|
for (const rule of parsed) {
|
|
if (!rule.permission) {
|
|
continue;
|
|
}
|
|
(byPermission[rule.permission] ||= {})[rule.pattern] = rule.action;
|
|
}
|
|
|
|
const result: Record<string, PermissionConfigValue> = {};
|
|
for (const [permissionName, map] of Object.entries(byPermission)) {
|
|
const patterns = Object.keys(map);
|
|
if (patterns.length === 1 && patterns[0] === '*') {
|
|
result[permissionName] = map['*'];
|
|
continue;
|
|
}
|
|
result[permissionName] = map;
|
|
}
|
|
|
|
return Object.keys(result).length > 0 ? (result as AgentDraft['permission']) : undefined;
|
|
};
|
|
|
|
export const AgentsSidebar: React.FC<AgentsSidebarProps> = ({ onItemSelect }) => {
|
|
const [renameDialogAgent, setRenameDialogAgent] = React.useState<Agent | null>(null);
|
|
const [renameNewName, setRenameNewName] = React.useState('');
|
|
|
|
const {
|
|
selectedAgentName,
|
|
agents,
|
|
setSelectedAgent,
|
|
setAgentDraft,
|
|
createAgent,
|
|
deleteAgent,
|
|
loadAgents,
|
|
} = useAgentsStore();
|
|
|
|
const { setSidebarOpen } = useUIStore();
|
|
const { isMobile } = useDeviceInfo();
|
|
|
|
const isVSCode = React.useMemo(() => isVSCodeRuntime(), []);
|
|
|
|
React.useEffect(() => {
|
|
loadAgents();
|
|
}, [loadAgents]);
|
|
|
|
const bgClass = isVSCode ? 'bg-background' : 'bg-sidebar';
|
|
|
|
const handleCreateNew = () => {
|
|
// Generate unique name
|
|
const baseName = 'new-agent';
|
|
let newName = baseName;
|
|
let counter = 1;
|
|
while (agents.some((a) => a.name === newName)) {
|
|
newName = `${baseName}-${counter}`;
|
|
counter++;
|
|
}
|
|
|
|
// Set draft and open the page for editing
|
|
setAgentDraft({ name: newName, scope: 'user' });
|
|
setSelectedAgent(newName);
|
|
onItemSelect?.();
|
|
|
|
if (isMobile) {
|
|
setSidebarOpen(false);
|
|
}
|
|
};
|
|
|
|
const handleDeleteAgent = async (agent: Agent) => {
|
|
if (isAgentBuiltIn(agent)) {
|
|
toast.error('Built-in agents cannot be deleted');
|
|
return;
|
|
}
|
|
|
|
if (window.confirm(`Are you sure you want to delete agent "${agent.name}"?`)) {
|
|
const success = await deleteAgent(agent.name);
|
|
if (success) {
|
|
toast.success(`Agent "${agent.name}" deleted successfully`);
|
|
} else {
|
|
toast.error('Failed to delete agent');
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleResetAgent = async (agent: Agent) => {
|
|
if (!isAgentBuiltIn(agent)) {
|
|
return;
|
|
}
|
|
|
|
if (window.confirm(`Are you sure you want to reset agent "${agent.name}" to its default configuration?`)) {
|
|
const success = await deleteAgent(agent.name);
|
|
if (success) {
|
|
toast.success(`Agent "${agent.name}" reset to default`);
|
|
} else {
|
|
toast.error('Failed to reset agent');
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleDuplicateAgent = (agent: Agent) => {
|
|
const baseName = agent.name;
|
|
let copyNumber = 1;
|
|
let newName = `${baseName}-copy`;
|
|
|
|
while (agents.some((a) => a.name === newName)) {
|
|
copyNumber++;
|
|
newName = `${baseName}-copy-${copyNumber}`;
|
|
}
|
|
|
|
// Set draft with prefilled values from source agent
|
|
const extAgent = agent as Agent & { scope?: AgentScope };
|
|
const modelStr = agent.model?.providerID && agent.model?.modelID
|
|
? `${agent.model.providerID}/${agent.model.modelID}`
|
|
: null;
|
|
const draftAgent = agent as Agent & { disable?: boolean };
|
|
setAgentDraft({
|
|
name: newName,
|
|
scope: extAgent.scope || 'user',
|
|
description: agent.description,
|
|
model: modelStr,
|
|
temperature: agent.temperature,
|
|
top_p: agent.topP,
|
|
prompt: agent.prompt,
|
|
mode: agent.mode,
|
|
permission: rulesetToPermissionConfig(agent.permission),
|
|
disable: draftAgent.disable,
|
|
});
|
|
setSelectedAgent(newName);
|
|
|
|
if (isMobile) {
|
|
setSidebarOpen(false);
|
|
}
|
|
};
|
|
|
|
const handleOpenRenameDialog = (agent: Agent) => {
|
|
setRenameNewName(agent.name);
|
|
setRenameDialogAgent(agent);
|
|
};
|
|
|
|
const handleRenameAgent = async () => {
|
|
if (!renameDialogAgent) return;
|
|
|
|
const sanitizedName = renameNewName.trim().replace(/\s+/g, '-');
|
|
|
|
if (!sanitizedName) {
|
|
toast.error('Agent name is required');
|
|
return;
|
|
}
|
|
|
|
if (sanitizedName === renameDialogAgent.name) {
|
|
setRenameDialogAgent(null);
|
|
return;
|
|
}
|
|
|
|
if (agents.some((a) => a.name === sanitizedName)) {
|
|
toast.error('An agent with this name already exists');
|
|
return;
|
|
}
|
|
|
|
// Create new agent with new name and all existing config
|
|
const renameModelStr = renameDialogAgent.model?.providerID && renameDialogAgent.model?.modelID
|
|
? `${renameDialogAgent.model.providerID}/${renameDialogAgent.model.modelID}`
|
|
: null;
|
|
const renameExt = renameDialogAgent as Agent & { scope?: AgentScope; disable?: boolean };
|
|
const success = await createAgent({
|
|
name: sanitizedName,
|
|
description: renameDialogAgent.description,
|
|
model: renameModelStr,
|
|
temperature: renameDialogAgent.temperature,
|
|
top_p: renameDialogAgent.topP,
|
|
prompt: renameDialogAgent.prompt,
|
|
mode: renameDialogAgent.mode,
|
|
permission: rulesetToPermissionConfig(renameDialogAgent.permission),
|
|
disable: renameExt.disable,
|
|
scope: renameExt.scope,
|
|
});
|
|
|
|
if (success) {
|
|
// Delete old agent
|
|
const deleteSuccess = await deleteAgent(renameDialogAgent.name);
|
|
if (deleteSuccess) {
|
|
toast.success(`Agent renamed to "${sanitizedName}"`);
|
|
setSelectedAgent(sanitizedName);
|
|
} else {
|
|
toast.error('Failed to remove old agent after rename');
|
|
}
|
|
} else {
|
|
toast.error('Failed to rename agent');
|
|
}
|
|
|
|
setRenameDialogAgent(null);
|
|
};
|
|
|
|
const getAgentModeIcon = (mode?: string) => {
|
|
switch (mode) {
|
|
case 'primary':
|
|
return <RiAiAgentLine className="h-3 w-3 text-primary" />;
|
|
case 'all':
|
|
return <RiAiAgentFill className="h-3 w-3 text-primary" />;
|
|
case 'subagent':
|
|
return <RiRobotLine className="h-3 w-3 text-primary" />;
|
|
default:
|
|
return null;
|
|
}
|
|
};
|
|
|
|
// Filter out hidden agents (internal agents like title, compaction, summary)
|
|
const visibleAgents = agents.filter((agent) => !isAgentHidden(agent));
|
|
const builtInAgents = visibleAgents.filter(isAgentBuiltIn);
|
|
const customAgents = visibleAgents.filter((agent) => !isAgentBuiltIn(agent));
|
|
|
|
return (
|
|
<div className={cn('flex h-full flex-col', bgClass)}>
|
|
<div className={cn('border-b px-3', isMobile ? 'mt-2 py-3' : 'py-3')}>
|
|
<div className="flex items-center justify-between gap-2">
|
|
<span className="typography-meta text-muted-foreground">Total {visibleAgents.length}</span>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon"
|
|
className="h-7 w-7 -my-1 text-muted-foreground"
|
|
onClick={handleCreateNew}
|
|
>
|
|
<RiAddLine className="size-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<ScrollableOverlay outerClassName="flex-1 min-h-0" className="space-y-1 px-3 py-2 overflow-x-hidden">
|
|
{visibleAgents.length === 0 ? (
|
|
<div className="py-12 px-4 text-center text-muted-foreground">
|
|
<RiRobot2Line className="mx-auto mb-3 h-10 w-10 opacity-50" />
|
|
<p className="typography-ui-label font-medium">No agents configured</p>
|
|
<p className="typography-meta mt-1 opacity-75">Use the + button above to create one</p>
|
|
</div>
|
|
) : (
|
|
<>
|
|
{builtInAgents.length > 0 && (
|
|
<>
|
|
<div className="px-2 pb-1.5 pt-2 text-xs font-semibold uppercase tracking-wide text-muted-foreground">
|
|
Built-in Agents
|
|
</div>
|
|
{builtInAgents.map((agent) => (
|
|
<AgentListItem
|
|
key={agent.name}
|
|
agent={agent}
|
|
isSelected={selectedAgentName === agent.name}
|
|
onSelect={() => {
|
|
setSelectedAgent(agent.name);
|
|
onItemSelect?.();
|
|
if (isMobile) {
|
|
setSidebarOpen(false);
|
|
}
|
|
}}
|
|
onReset={() => handleResetAgent(agent)}
|
|
onDuplicate={() => handleDuplicateAgent(agent)}
|
|
getAgentModeIcon={getAgentModeIcon}
|
|
/>
|
|
))}
|
|
</>
|
|
)}
|
|
|
|
{customAgents.length > 0 && (
|
|
<>
|
|
<div className="px-2 pb-1.5 pt-3 text-xs font-semibold uppercase tracking-wide text-muted-foreground">
|
|
Custom Agents
|
|
</div>
|
|
{customAgents.map((agent) => (
|
|
<AgentListItem
|
|
key={agent.name}
|
|
agent={agent}
|
|
isSelected={selectedAgentName === agent.name}
|
|
onSelect={() => {
|
|
setSelectedAgent(agent.name);
|
|
onItemSelect?.();
|
|
if (isMobile) {
|
|
setSidebarOpen(false);
|
|
}
|
|
}}
|
|
onRename={() => handleOpenRenameDialog(agent)}
|
|
onDelete={() => handleDeleteAgent(agent)}
|
|
onDuplicate={() => handleDuplicateAgent(agent)}
|
|
getAgentModeIcon={getAgentModeIcon}
|
|
/>
|
|
))}
|
|
</>
|
|
)}
|
|
</>
|
|
)}
|
|
</ScrollableOverlay>
|
|
|
|
{/* Rename Dialog */}
|
|
<Dialog open={renameDialogAgent !== null} onOpenChange={(open) => !open && setRenameDialogAgent(null)}>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>Rename Agent</DialogTitle>
|
|
<DialogDescription>
|
|
Enter a new name for the agent "@{renameDialogAgent?.name}"
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<Input
|
|
value={renameNewName}
|
|
onChange={(e) => setRenameNewName(e.target.value)}
|
|
placeholder="New agent name..."
|
|
className="text-foreground placeholder:text-muted-foreground"
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter') {
|
|
handleRenameAgent();
|
|
}
|
|
}}
|
|
/>
|
|
<DialogFooter>
|
|
<Button
|
|
variant="ghost"
|
|
onClick={() => setRenameDialogAgent(null)}
|
|
className="text-foreground hover:bg-interactive-hover hover:text-foreground"
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<ButtonLarge onClick={handleRenameAgent}>
|
|
Rename
|
|
</ButtonLarge>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
interface AgentListItemProps {
|
|
agent: Agent;
|
|
isSelected: boolean;
|
|
onSelect: () => void;
|
|
onDelete?: () => void;
|
|
onReset?: () => void;
|
|
onRename?: () => void;
|
|
onDuplicate: () => void;
|
|
getAgentModeIcon: (mode?: string) => React.ReactNode;
|
|
}
|
|
|
|
const AgentListItem: React.FC<AgentListItemProps> = ({
|
|
agent,
|
|
isSelected,
|
|
onSelect,
|
|
onDelete,
|
|
onReset,
|
|
onRename,
|
|
onDuplicate,
|
|
getAgentModeIcon,
|
|
}) => {
|
|
const extAgent = agent as Agent & { scope?: AgentScope };
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
'group relative flex items-center rounded-md px-1.5 py-1 transition-all duration-200',
|
|
isSelected ? 'bg-interactive-selection' : 'hover:bg-interactive-hover'
|
|
)}
|
|
>
|
|
<div className="flex min-w-0 flex-1 items-center">
|
|
<button
|
|
onClick={onSelect}
|
|
className="flex min-w-0 flex-1 flex-col gap-0 rounded-sm text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary/50"
|
|
tabIndex={0}
|
|
>
|
|
<div className="flex items-center gap-1.5">
|
|
<span className="typography-ui-label font-normal truncate text-foreground">
|
|
{agent.name}
|
|
</span>
|
|
{getAgentModeIcon(agent.mode)}
|
|
{(extAgent.scope || isAgentBuiltIn(agent)) && (
|
|
<span className="typography-micro text-muted-foreground bg-muted px-1 rounded flex-shrink-0 leading-none pb-px border border-border/50">
|
|
{isAgentBuiltIn(agent) ? 'system' : extAgent.scope}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{agent.description && (
|
|
<div className="typography-micro text-muted-foreground/60 truncate leading-tight">
|
|
{agent.description}
|
|
</div>
|
|
)}
|
|
</button>
|
|
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
className="h-6 w-6 flex-shrink-0 -mr-1 opacity-100 transition-opacity md:opacity-0 md:group-hover:opacity-100"
|
|
>
|
|
<RiMore2Line className="h-3.5 w-3.5" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" className="w-fit min-w-20">
|
|
{onRename && (
|
|
<DropdownMenuItem
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onRename();
|
|
}}
|
|
>
|
|
<RiEditLine className="h-4 w-4 mr-px" />
|
|
Rename
|
|
</DropdownMenuItem>
|
|
)}
|
|
|
|
<DropdownMenuItem
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onDuplicate();
|
|
}}
|
|
>
|
|
<RiFileCopyLine className="h-4 w-4 mr-px" />
|
|
Duplicate
|
|
</DropdownMenuItem>
|
|
|
|
{onReset && (
|
|
<DropdownMenuItem
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onReset();
|
|
}}
|
|
>
|
|
<RiRestartLine className="h-4 w-4 mr-px" />
|
|
Reset
|
|
</DropdownMenuItem>
|
|
)}
|
|
|
|
{onDelete && (
|
|
<DropdownMenuItem
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
onDelete();
|
|
}}
|
|
className="text-destructive focus:text-destructive"
|
|
>
|
|
<RiDeleteBinLine className="h-4 w-4 mr-px" />
|
|
Delete
|
|
</DropdownMenuItem>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|