@@ -27,6 +27,52 @@ export class Commands {
2727 private readonly storage : Storage ,
2828 ) { }
2929
30+ /**
31+ * Find the requested agent if specified, otherwise return the agent if there
32+ * is only one or ask the user to pick if there are multiple. Return
33+ * undefined if the user cancels.
34+ */
35+ public async maybeAskAgent ( workspace : Workspace , filter ?: string ) : Promise < WorkspaceAgent | undefined > {
36+ const agents = extractAgents ( workspace )
37+ const filteredAgents = filter ? agents . filter ( ( agent ) => agent . name === filter ) : agents
38+ if ( filteredAgents . length === 0 ) {
39+ throw new Error ( "Workspace has no matching agents" )
40+ } else if ( filteredAgents . length === 1 ) {
41+ return filteredAgents [ 0 ]
42+ } else {
43+ const quickPick = vscode . window . createQuickPick ( )
44+ quickPick . title = "Select an agent"
45+ quickPick . busy = true
46+ const agentItems : vscode . QuickPickItem [ ] = filteredAgents . map ( ( agent ) => {
47+ let icon = "$(debug-start)"
48+ if ( agent . status !== "connected" ) {
49+ icon = "$(debug-stop)"
50+ }
51+ return {
52+ alwaysShow : true ,
53+ label : `${ icon } ${ agent . name } ` ,
54+ detail : `${ agent . name } • Status: ${ agent . status } ` ,
55+ }
56+ } )
57+ quickPick . items = agentItems
58+ quickPick . busy = false
59+ quickPick . show ( )
60+
61+ const selected = await new Promise < WorkspaceAgent | undefined > ( ( resolve ) => {
62+ quickPick . onDidHide ( ( ) => resolve ( undefined ) )
63+ quickPick . onDidChangeSelection ( ( selected ) => {
64+ if ( selected . length < 1 ) {
65+ return resolve ( undefined )
66+ }
67+ const agent = filteredAgents [ quickPick . items . indexOf ( selected [ 0 ] ) ]
68+ resolve ( agent )
69+ } )
70+ } )
71+ quickPick . dispose ( )
72+ return selected
73+ }
74+ }
75+
3076 /**
3177 * Ask the user for the URL, letting them choose from a list of recent URLs or
3278 * CODER_URL or enter a new one. Undefined means the user aborted.
@@ -376,58 +422,19 @@ export class Commands {
376422 } )
377423 } )
378424 if ( ! workspace ) {
425+ // User declined to pick a workspace.
379426 return
380427 }
381428 workspaceOwner = workspace . owner_name
382429 workspaceName = workspace . name
383430
384- const agents = extractAgents ( workspace )
385-
386- if ( agents . length === 1 ) {
387- folderPath = agents [ 0 ] . expanded_directory
388- workspaceAgent = agents [ 0 ] . name
389- } else if ( agents . length > 0 ) {
390- const agentQuickPick = vscode . window . createQuickPick ( )
391- agentQuickPick . title = `Select an agent`
392-
393- agentQuickPick . busy = true
394- const lastAgents = agents
395- const agentItems : vscode . QuickPickItem [ ] = agents . map ( ( agent ) => {
396- let icon = "$(debug-start)"
397- if ( agent . status !== "connected" ) {
398- icon = "$(debug-stop)"
399- }
400- return {
401- alwaysShow : true ,
402- label : `${ icon } ${ agent . name } ` ,
403- detail : `${ agent . name } • Status: ${ agent . status } ` ,
404- }
405- } )
406- agentQuickPick . items = agentItems
407- agentQuickPick . busy = false
408- agentQuickPick . show ( )
409-
410- const agent = await new Promise < WorkspaceAgent | undefined > ( ( resolve ) => {
411- agentQuickPick . onDidHide ( ( ) => {
412- resolve ( undefined )
413- } )
414- agentQuickPick . onDidChangeSelection ( ( selected ) => {
415- if ( selected . length < 1 ) {
416- return resolve ( undefined )
417- }
418- const agent = lastAgents [ agentQuickPick . items . indexOf ( selected [ 0 ] ) ]
419- resolve ( agent )
420- } )
421- } )
422-
423- if ( agent ) {
424- folderPath = agent . expanded_directory
425- workspaceAgent = agent . name
426- } else {
427- folderPath = ""
428- workspaceAgent = ""
429- }
431+ const agent = await this . maybeAskAgent ( workspace )
432+ if ( ! agent ) {
433+ // User declined to pick an agent.
434+ return
430435 }
436+ folderPath = agent . expanded_directory
437+ workspaceAgent = agent . name
431438 } else {
432439 workspaceOwner = args [ 0 ] as string
433440 workspaceName = args [ 1 ] as string
0 commit comments