@@ -17,6 +17,7 @@ import type {
1717
1818import {
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 = {
106108export 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 = / \$ ( A R G U M E N T S | \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 ( / ^ ( n e x t | p r e v | p r e v i o u s ) $ / i. test ( rawArg ) ) {
700+ await cycleAgent ( / ^ p r e v / i. test ( rawArg ) ? "prev" : "next" ) ;
701+ clearPrompt ( ) ;
702+ return ;
703+ }
610704 if ( / ^ ( n o n e | c l e a r | d e f a u l t ) $ / 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 } >
0 commit comments