Skip to content

Commit d2968fb

Browse files
feat: add session agent picker and cycling commands (anomalyco#268)
1 parent c5d89be commit d2968fb

4 files changed

Lines changed: 243 additions & 5 deletions

File tree

‎packages/app/src/app/app.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,6 +2717,7 @@ export default function App() {
27172717
providers: providers(),
27182718
providerConnectedIds: providerConnectedIds(),
27192719
listAgents: listAgents,
2720+
selectedSessionAgent: selectedSessionAgent(),
27202721
setSessionAgent: setSessionAgent,
27212722
saveSession: saveSessionExport,
27222723
sessionStatusById: activeSessionStatusById(),

‎packages/app/src/app/components/session/message-list.tsx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ export default function MessageList(props: MessageListProps) {
6262
}
6363
};
6464

65+
const partToText = (part: Part) => {
66+
if (part.type === "text") {
67+
return String((part as { text?: string }).text ?? "");
68+
}
69+
if (part.type === "agent") {
70+
const name = (part as { name?: string }).name ?? "";
71+
return name ? `@${name}` : "@agent";
72+
}
73+
if (part.type === "file") {
74+
const record = part as { label?: string; path?: string; filename?: string };
75+
const label = record.label ?? record.path ?? record.filename ?? "";
76+
return label ? `@${label}` : "@file";
77+
}
78+
return "";
79+
};
80+
6581
const toggleSteps = (id: string, relatedIds: string[] = []) => {
6682
props.setExpandedStepIds((current) => {
6783
const next = new Set(current);
@@ -91,7 +107,7 @@ export default function MessageList(props: MessageListProps) {
91107
return props.developerMode;
92108
}
93109

94-
if (part.type === "text" || part.type === "tool") {
110+
if (part.type === "text" || part.type === "tool" || part.type === "agent" || part.type === "file") {
95111
return true;
96112
}
97113

@@ -308,7 +324,7 @@ export default function MessageList(props: MessageListProps) {
308324
title="Copy message"
309325
onClick={() => {
310326
const text = block.renderableParts
311-
.map((part) => ("text" in part ? (part as any).text : ""))
327+
.map((part) => partToText(part))
312328
.join("\n");
313329
handleCopy(text, block.messageId);
314330
}}

‎packages/app/src/app/pages/session.tsx‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717

1818
import {
1919
AlertTriangle,
20+
AtSign,
2021
ArrowRight,
2122
ChevronDown,
2223
HardDrive,
@@ -93,6 +94,7 @@ export type SessionViewProps = {
9394
providers: Provider[];
9495
providerConnectedIds: string[];
9596
listAgents: () => Promise<Agent[]>;
97+
selectedSessionAgent: string | null;
9698
setSessionAgent: (sessionId: string, agent: string | null) => void;
9799
saveSession: (sessionId: string) => Promise<string>;
98100
sessionStatusById: Record<string, string>;
@@ -106,17 +108,46 @@ export type SessionViewProps = {
106108
export default function SessionView(props: SessionViewProps) {
107109
let messagesEndEl: HTMLDivElement | undefined;
108110
let chatContainerEl: HTMLDivElement | undefined;
111+
let agentPickerRef: HTMLDivElement | undefined;
109112

110113
const [commandToast, setCommandToast] = createSignal<string | null>(null);
111114
const [providerAuthActionBusy, setProviderAuthActionBusy] = createSignal(false);
112115
const [renameModalOpen, setRenameModalOpen] = createSignal(false);
113116
const [renameTitle, setRenameTitle] = createSignal("");
114117
const [renameBusy, setRenameBusy] = createSignal(false);
118+
const [agentPickerOpen, setAgentPickerOpen] = createSignal(false);
119+
const [agentPickerBusy, setAgentPickerBusy] = createSignal(false);
120+
const [agentPickerReady, setAgentPickerReady] = createSignal(false);
121+
const [agentPickerError, setAgentPickerError] = createSignal<string | null>(null);
122+
const [agentOptions, setAgentOptions] = createSignal<Agent[]>([]);
115123

116124
const COMMAND_ARGS_RE = /\$(ARGUMENTS|\d+)/i;
117125

118126
const commandNeedsDetails = (command: { template: string }) => COMMAND_ARGS_RE.test(command.template);
119127

128+
const agentLabel = createMemo(() => props.selectedSessionAgent ?? "Default agent");
129+
130+
const loadAgentOptions = async (force = false) => {
131+
if (agentPickerBusy()) return agentOptions();
132+
if (agentPickerReady() && !force) return agentOptions();
133+
setAgentPickerBusy(true);
134+
setAgentPickerError(null);
135+
try {
136+
const agents = await props.listAgents();
137+
const sorted = agents.slice().sort((a, b) => a.name.localeCompare(b.name));
138+
setAgentOptions(sorted);
139+
setAgentPickerReady(true);
140+
return sorted;
141+
} catch (error) {
142+
const message = error instanceof Error ? error.message : "Failed to load agents";
143+
setAgentPickerError(message);
144+
setAgentOptions([]);
145+
return [];
146+
} finally {
147+
setAgentPickerBusy(false);
148+
}
149+
};
150+
120151
type Flyout = {
121152
id: string;
122153
rect: { top: number; left: number; width: number; height: number };
@@ -511,6 +542,64 @@ export default function SessionView(props: SessionViewProps) {
511542
return items.length > 4 ? `${preview}, ...` : preview;
512543
};
513544

545+
const openAgentPicker = () => {
546+
setAgentPickerOpen((current) => !current);
547+
if (!agentPickerReady()) {
548+
void loadAgentOptions();
549+
}
550+
};
551+
552+
const applySessionAgent = (agent: string | null) => {
553+
const sessionId = requireSessionId();
554+
if (!sessionId) return;
555+
props.setSessionAgent(sessionId, agent);
556+
setCommandToast(agent ? `Agent set to ${agent}` : "Agent cleared");
557+
};
558+
559+
const cycleAgent = async (direction: "next" | "prev") => {
560+
const sessionId = requireSessionId();
561+
if (!sessionId) return;
562+
try {
563+
const agents = await loadAgentOptions(true);
564+
if (!agents.length) {
565+
setCommandToast("No agents available");
566+
return;
567+
}
568+
const names = agents.map((agent) => agent.name);
569+
const current = props.selectedSessionAgent ?? "";
570+
const currentIndex = current ? names.findIndex((name) => name === current) : -1;
571+
let nextIndex = 0;
572+
if (currentIndex === -1) {
573+
nextIndex = direction === "next" ? 0 : names.length - 1;
574+
} else if (direction === "next") {
575+
nextIndex = (currentIndex + 1) % names.length;
576+
} else {
577+
nextIndex = (currentIndex - 1 + names.length) % names.length;
578+
}
579+
const nextAgent = names[nextIndex] ?? null;
580+
if (!nextAgent) {
581+
setCommandToast("No agents available");
582+
return;
583+
}
584+
props.setSessionAgent(sessionId, nextAgent);
585+
setCommandToast(`Agent set to ${nextAgent}`);
586+
} catch (error) {
587+
const message = error instanceof Error ? error.message : "Agent selection failed";
588+
setCommandToast(message);
589+
}
590+
};
591+
592+
createEffect(() => {
593+
if (!agentPickerOpen()) return;
594+
const handler = (event: MouseEvent) => {
595+
if (!agentPickerRef) return;
596+
if (agentPickerRef.contains(event.target as Node)) return;
597+
setAgentPickerOpen(false);
598+
};
599+
window.addEventListener("mousedown", handler);
600+
onCleanup(() => window.removeEventListener("mousedown", handler));
601+
});
602+
514603
const handleProviderAuthSelect = async (providerId: string) => {
515604
if (providerAuthActionBusy()) return;
516605
setProviderAuthActionBusy(true);
@@ -607,6 +696,11 @@ export default function SessionView(props: SessionViewProps) {
607696

608697
try {
609698
const rawArg = extractCommandArgs(props.prompt);
699+
if (/^(next|prev|previous)$/i.test(rawArg)) {
700+
await cycleAgent(/^prev/i.test(rawArg) ? "prev" : "next");
701+
clearPrompt();
702+
return;
703+
}
610704
if (/^(none|clear|default)$/i.test(rawArg)) {
611705
props.setSessionAgent(sessionId, null);
612706
setCommandToast("Agent cleared");
@@ -655,6 +749,30 @@ export default function SessionView(props: SessionViewProps) {
655749
}
656750
},
657751
},
752+
{
753+
id: "session.agent.next",
754+
title: "Next agent",
755+
category: "Session",
756+
description: "Cycle to the next agent",
757+
slash: "agent-next",
758+
scope: "session",
759+
onSelect: async () => {
760+
await cycleAgent("next");
761+
clearPrompt();
762+
},
763+
},
764+
{
765+
id: "session.agent.prev",
766+
title: "Previous agent",
767+
category: "Session",
768+
description: "Cycle to the previous agent",
769+
slash: "agent-prev",
770+
scope: "session",
771+
onSelect: async () => {
772+
await cycleAgent("prev");
773+
clearPrompt();
774+
},
775+
},
658776
{
659777
id: "session.export",
660778
title: "Export session JSON",
@@ -852,6 +970,91 @@ export default function SessionView(props: SessionViewProps) {
852970
</Show>
853971

854972
</div>
973+
<div class="relative flex items-center gap-2" ref={(el) => (agentPickerRef = el)}>
974+
<button
975+
type="button"
976+
class="flex items-center gap-2 pl-3 pr-2 py-1.5 bg-gray-2 border border-gray-6 rounded-lg hover:border-gray-7 hover:bg-gray-4 transition-all group"
977+
onClick={openAgentPicker}
978+
aria-expanded={agentPickerOpen()}
979+
>
980+
<div class="p-1 rounded bg-gray-4 text-gray-10">
981+
<AtSign size={14} />
982+
</div>
983+
<div class="flex flex-col items-start mr-2 min-w-0">
984+
<span class="text-xs font-medium text-gray-12 leading-none truncate max-w-[10rem]">
985+
{agentLabel()}
986+
</span>
987+
<span class="text-[10px] text-gray-10 font-mono leading-none">
988+
{props.selectedSessionAgent ? "Agent" : "Default"}
989+
</span>
990+
</div>
991+
<ChevronDown size={14} class="text-gray-10 group-hover:text-gray-11" />
992+
</button>
993+
994+
<Show when={agentPickerOpen()}>
995+
<div class="absolute right-0 top-full mt-2 w-72 rounded-2xl border border-gray-6 bg-gray-1/95 shadow-2xl backdrop-blur-md overflow-hidden">
996+
<div class="px-4 pt-3 pb-2 text-[10px] font-semibold uppercase tracking-[0.2em] text-gray-8 border-b border-gray-6/30">
997+
Session agent
998+
</div>
999+
<div class="max-h-64 overflow-auto p-2 space-y-1">
1000+
<button
1001+
type="button"
1002+
class={`w-full flex items-center justify-between rounded-xl px-3 py-2 text-left text-xs transition-colors ${
1003+
props.selectedSessionAgent
1004+
? "text-gray-11 hover:bg-gray-12/5"
1005+
: "bg-gray-12/10 text-gray-12"
1006+
}`}
1007+
onClick={() => {
1008+
applySessionAgent(null);
1009+
setAgentPickerOpen(false);
1010+
}}
1011+
>
1012+
<span>Default agent</span>
1013+
<Show when={!props.selectedSessionAgent}>
1014+
<span class="text-[10px] uppercase tracking-wider text-gray-9">Active</span>
1015+
</Show>
1016+
</button>
1017+
<Show
1018+
when={!agentPickerBusy()}
1019+
fallback={<div class="px-3 py-2 text-xs text-gray-9">Loading agents...</div>}
1020+
>
1021+
<Show
1022+
when={agentOptions().length}
1023+
fallback={<div class="px-3 py-2 text-xs text-gray-9">No agents available.</div>}
1024+
>
1025+
<For each={agentOptions()}>
1026+
{(agent: Agent) => (
1027+
<button
1028+
type="button"
1029+
class={`w-full flex items-center justify-between rounded-xl px-3 py-2 text-left text-xs transition-colors ${
1030+
props.selectedSessionAgent === agent.name
1031+
? "bg-gray-12/10 text-gray-12"
1032+
: "text-gray-11 hover:bg-gray-12/5"
1033+
}`}
1034+
onClick={() => {
1035+
applySessionAgent(agent.name);
1036+
setAgentPickerOpen(false);
1037+
}}
1038+
>
1039+
<span>{agent.name}</span>
1040+
<Show when={props.selectedSessionAgent === agent.name}>
1041+
<span class="text-[10px] uppercase tracking-wider text-gray-9">Active</span>
1042+
</Show>
1043+
</button>
1044+
)}
1045+
</For>
1046+
</Show>
1047+
</Show>
1048+
<Show when={agentPickerError()}>
1049+
<div class="px-3 py-2 text-xs text-red-11">{agentPickerError()}</div>
1050+
</Show>
1051+
</div>
1052+
<div class="border-t border-gray-6/40 px-4 py-2 text-[10px] text-gray-9">
1053+
Tip: use /agent-next or /agent-prev to cycle.
1054+
</div>
1055+
</div>
1056+
</Show>
1057+
</div>
8551058
</header>
8561059

8571060
<Show when={props.error}>

‎packages/app/src/app/utils/index.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,21 +417,39 @@ export function isStepPart(part: Part) {
417417
export function groupMessageParts(parts: Part[], messageId: string): MessageGroup[] {
418418
const groups: MessageGroup[] = [];
419419
const steps: Part[] = [];
420+
let textBuffer = "";
421+
422+
const flushText = () => {
423+
if (!textBuffer) return;
424+
groups.push({ kind: "text", part: { type: "text", text: textBuffer } as Part });
425+
textBuffer = "";
426+
};
420427

421428
parts.forEach((part) => {
422429
if (part.type === "text") {
423-
groups.push({ kind: "text", part });
430+
textBuffer += (part as { text?: string }).text ?? "";
431+
return;
432+
}
433+
434+
if (part.type === "agent") {
435+
const name = (part as { name?: string }).name ?? "";
436+
textBuffer += name ? `@${name}` : "@agent";
424437
return;
425438
}
426439

427-
if (isStepPart(part)) {
428-
steps.push(part);
440+
if (part.type === "file") {
441+
const record = part as { label?: string; path?: string; filename?: string };
442+
const label = record.label ?? record.path ?? record.filename ?? "";
443+
textBuffer += label ? `@${label}` : "@file";
429444
return;
430445
}
431446

447+
flushText();
432448
steps.push(part);
433449
});
434450

451+
flushText();
452+
435453
if (steps.length) {
436454
groups.push({ kind: "steps", id: `steps-${messageId}`, parts: steps });
437455
}

0 commit comments

Comments
 (0)