Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface AppSettings {
};
defaultOpenInApp?: OpenInAppId;
hiddenOpenInApps?: OpenInAppId[];
pinnedAgents?: string[];
}

function getPlatformTaskSwitchDefaults(): { next: ShortcutBinding; prev: ShortcutBinding } {
Expand Down Expand Up @@ -179,6 +180,7 @@ const DEFAULT_SETTINGS: AppSettings = {
},
defaultOpenInApp: 'terminal',
hiddenOpenInApps: [],
pinnedAgents: [],
};

function getSettingsPath(): string {
Expand Down Expand Up @@ -530,6 +532,15 @@ export function normalizeSettings(input: AppSettings): AppSettings {
out.hiddenOpenInApps = [];
}

// Pinned Agents
const rawPinned = (input as any)?.pinnedAgents;
if (Array.isArray(rawPinned)) {
const validated = rawPinned.filter((item) => typeof item === 'string');
out.pinnedAgents = [...new Set(validated)];
} else {
out.pinnedAgents = [];
}
Comment on lines +535 to +542
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing deduplication for pinnedAgents

The hiddenOpenInApps normalization immediately above uses [...new Set(validated)] to deduplicate entries. pinnedAgents skips this step, so a corrupt or manually edited settings file could contain duplicate agent IDs. Duplicates won't break rendering but will cause an agent to appear pinned even after being "unpinned" (since filter only removes it once per call).

Suggested change
// Pinned Agents
const rawPinned = (input as any)?.pinnedAgents;
if (Array.isArray(rawPinned)) {
out.pinnedAgents = rawPinned.filter((item) => typeof item === 'string');
} else {
out.pinnedAgents = [];
}
// Pinned Agents
const rawPinned = (input as any)?.pinnedAgents;
if (Array.isArray(rawPinned)) {
out.pinnedAgents = [...new Set(rawPinned.filter((item) => typeof item === 'string'))];
} else {
out.pinnedAgents = [];
}


return out;
}

Expand Down
168 changes: 114 additions & 54 deletions src/renderer/components/MultiAgentDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { rpc } from '@/lib/rpc';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select';
import { TooltipProvider, Tooltip, TooltipTrigger, TooltipContent } from './ui/tooltip';
import { Info, ExternalLink } from 'lucide-react';
import { Info, ExternalLink, Pin } from 'lucide-react';
import { type Agent } from '../types';
import { type AgentRun } from '../types/chat';
import { agentConfig } from '../lib/agentConfig';
Expand All @@ -24,8 +25,51 @@ export const MultiAgentDropdown: React.FC<MultiAgentDropdownProps> = ({
className = '',
disabledAgents = [],
}) => {
// Use agentConfig order directly (already properly ordered)
const sortedAgents = Object.entries(agentConfig);
// Setup state for pinned agents (persisted via rpc.appSettings)
const [pinnedAgents, setPinnedAgents] = useState<string[]>([]);

useEffect(() => {
let cancel = false;
rpc.appSettings.get().then((settings) => {
if (!cancel && settings?.pinnedAgents) {
setPinnedAgents(settings.pinnedAgents);
}
});
return () => {
cancel = true;
};
}, []);

// handle pinning/unpinning
const togglePin = async (agentId: string, e: React.MouseEvent) => {
e.stopPropagation();

const previousState = [...pinnedAgents];

const next = pinnedAgents.includes(agentId)
? pinnedAgents.filter((a) => a !== agentId)
: [...pinnedAgents, agentId];
setPinnedAgents(next);

try {
await rpc.appSettings.update({ pinnedAgents: next });
} catch (error) {
console.error('Failed to save pinned agents', error);
// If the save fails, roll the UI back
setPinnedAgents(previousState);
}
Comment on lines +52 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No rollback on settings save failure

setPinnedAgents(next) is called optimistically before the async rpc.appSettings.update call. If the update fails, the UI reflects the pin as toggled but the setting is never persisted — on the next app launch (or the next time this component mounts) the pin will silently disappear, leaving the user confused. The state should be rolled back when the call fails.

Suggested change
setPinnedAgents(next);
try {
await rpc.appSettings.update({ pinnedAgents: next });
} catch (error) {
console.error('Failed to save pinned agents', error);
}
setPinnedAgents(next);
try {
await rpc.appSettings.update({ pinnedAgents: next });
} catch (error) {
console.error('Failed to save pinned agents', error);
setPinnedAgents(pinnedAgents); // rollback optimistic update
}

};

// Dynamically sort agents
const sortedAgents = React.useMemo(() => {
return Object.entries(agentConfig).sort(([keyA], [keyB]) => {
const aPinned = pinnedAgents.includes(keyA);
const bPinned = pinnedAgents.includes(keyB);
if (aPinned && !bPinned) return -1;
if (!aPinned && bPinned) return 1;
return 0;
});
}, [pinnedAgents]);
const [open, setOpen] = useState(false);
const [hoveredAgent, setHoveredAgent] = useState<Agent | null>(null);
const [runsSelectOpenFor, setRunsSelectOpenFor] = useState<Agent | null>(null);
Expand Down Expand Up @@ -130,7 +174,7 @@ export const MultiAgentDropdown: React.FC<MultiAgentDropdownProps> = ({
}}
>
<div
className="flex h-8 cursor-pointer items-center justify-between rounded-sm px-2 hover:bg-accent"
className="group flex h-8 cursor-pointer items-center justify-between rounded-sm px-2 hover:bg-accent"
onClick={() => handleRowClick(agent)}
>
<div className="flex flex-1 items-center gap-2">
Expand All @@ -154,56 +198,72 @@ export const MultiAgentDropdown: React.FC<MultiAgentDropdownProps> = ({
/>
<span className="text-sm">{config.name}</span>
</div>
{isSelected && (
<div className="flex items-center gap-1">
<Select
value={String(getAgentRuns(agent))}
onValueChange={(v) => updateRuns(agent, parseInt(v, 10))}
onOpenChange={(isSelectOpen) => {
setRunsSelectOpenFor(isSelectOpen ? agent : null);
}}
>
<SelectTrigger
className="h-6 w-auto gap-1 border-none bg-transparent p-0 text-sm shadow-none"
title="Run up to 4 instances of this agent to compare different solutions"
>
<SelectValue />
</SelectTrigger>
<SelectContent side="right" className="z-[1001] min-w-[4rem]">
{[1, 2, 3, 4].map((n) => (
<SelectItem key={n} value={String(n)}>
{n}x
</SelectItem>
))}
</SelectContent>
</Select>
<Tooltip>
<TooltipTrigger asChild>
<Info className="h-3 w-3 opacity-40 hover:opacity-60" />
</TooltipTrigger>
<TooltipContent
side="top"
className="z-[10000] max-w-xs"
style={{ zIndex: 10000 }}
<div className="flex items-center gap-2">
{isSelected && (
<div className="flex items-center gap-1">
<Select
value={String(getAgentRuns(agent))}
onValueChange={(v) => updateRuns(agent, parseInt(v, 10))}
onOpenChange={(isSelectOpen) => {
setRunsSelectOpenFor(isSelectOpen ? agent : null);
}}
>
<p>
Run up to {MAX_RUNS} instances of this agent to compare different
solutions.{' '}
<a
href="https://docs.emdash.sh/best-of-n"
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-0.5 underline hover:opacity-70"
onClick={(e) => e.stopPropagation()}
>
Docs
<ExternalLink className="h-3 w-3" />
</a>
</p>
</TooltipContent>
</Tooltip>
</div>
)}
<SelectTrigger
className="h-6 w-auto gap-1 border-none bg-transparent p-0 text-sm shadow-none"
title="Run up to 4 instances of this agent to compare different solutions"
>
<SelectValue />
</SelectTrigger>
<SelectContent side="right" className="z-[1001] min-w-[4rem]">
{[1, 2, 3, 4].map((n) => (
<SelectItem key={n} value={String(n)}>
{n}x
</SelectItem>
))}
</SelectContent>
</Select>
<Tooltip>
<TooltipTrigger asChild>
<Info className="h-3 w-3 opacity-40 hover:opacity-60" />
</TooltipTrigger>
<TooltipContent
side="top"
className="z-[10000] max-w-xs"
style={{ zIndex: 10000 }}
>
<p>
Run up to {MAX_RUNS} instances of this agent to compare different
solutions.{' '}
<a
href="https://docs.emdash.sh/best-of-n"
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-0.5 underline hover:opacity-70"
onClick={(e) => e.stopPropagation()}
>
Docs
<ExternalLink className="h-3 w-3" />
</a>
</p>
</TooltipContent>
</Tooltip>
</div>
)}
<button
onClick={(e) => togglePin(key, e)}
className={`flex items-center justify-center rounded p-1 transition-opacity hover:bg-muted-foreground/20 ${
pinnedAgents.includes(key)
? 'text-foreground opacity-100' // Always visible if pinned
: 'text-muted-foreground opacity-0 group-hover:opacity-100' // Visible on hover if unpinned
}`}
title={pinnedAgents.includes(key) ? 'Unpin agent' : 'Pin agent to top'}
>
<Pin
className="h-3.5 w-3.5"
fill={pinnedAgents.includes(key) ? 'currentColor' : 'none'} // Fills the icon when pinned
/>
</button>
</div>
</div>
</AgentTooltipRow>
) : (
Expand Down