Repository navigation
Expand file tree
/
Copy pathprofile.rs
More file actions
577 lines (528 loc) · 22.1 KB
/
Copy pathprofile.rs
File metadata and controls
577 lines (528 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
use std::borrow::Cow;
use std::collections::HashMap;
use std::io::Write;
use clap::Subcommand;
use crossterm::style::{
self,
};
use crossterm::{
execute,
queue,
};
use dialoguer::{
MultiSelect,
Select,
};
use eyre::Result;
use syntect::easy::HighlightLines;
use syntect::highlighting::{
Style,
ThemeSet,
};
use syntect::parsing::SyntaxSet;
use syntect::util::{
LinesWithEndings,
as_24_bit_terminal_escaped,
};
use crate::cli::agent::{
Agent,
Agents,
McpServerConfig,
create_agent,
};
use crate::cli::chat::conversation::McpServerInfo;
use crate::cli::chat::{
ChatError,
ChatSession,
ChatState,
};
use crate::database::settings::Setting;
use crate::os::Os;
use crate::theme::StyledText;
use crate::util::NullWriter;
use crate::util::paths::PathResolver;
#[deny(missing_docs)]
#[derive(Debug, PartialEq, Subcommand)]
#[command(
before_long_help = "Agents allow you to organize and manage different sets of context files for different projects or tasks.
Notes
• Launch q chat with a specific agent with --agent
• Construct an agent in the global agents directory (accessible globally) or workspace agents directory (accessible in workspace)
• See example config under global directory
• Set default agent to assume with settings by running \"q settings chat.defaultAgent agent_name\"
• Each agent maintains its own set of context and customizations"
)]
/// Subcommands for managing agents in the chat CLI
pub enum AgentSubcommand {
/// List all available agents
List,
/// Create a new agent with the specified name
Create {
/// Name of the agent to be created
#[arg(long, short)]
name: String,
/// The directory where the agent will be saved. If not provided, the agent will be saved in
/// the global agent directory
#[arg(long, short)]
directory: Option<String>,
/// The name of an agent that shall be used as the starting point for the agent creation
#[arg(long, short)]
from: Option<String>,
},
/// Edit an existing agent configuration
Edit {
/// Name of the agent to edit
#[arg(long, short)]
name: String,
},
/// Generate an agent configuration using AI
Generate {},
/// Delete the specified agent
#[command(hide = true)]
Delete {
/// Name of the agent to delete
name: String,
},
/// Switch to the specified agent
#[command(hide = true)]
Set {
/// Name of the agent to switch to
name: String,
},
/// Show agent config schema
Schema,
/// Define a default agent to use when q chat launches
SetDefault {
/// Name of the agent to set as default
#[arg(long, short)]
name: String,
},
/// Swap to a new agent at runtime
#[command(alias = "switch")]
Swap {
/// Optional name of the agent to swap to. If not provided, a selection dialog will be shown
name: Option<String>,
},
}
fn prompt_mcp_server_selection(servers: &[McpServerInfo]) -> eyre::Result<Option<Vec<&McpServerInfo>>> {
let items: Vec<String> = servers
.iter()
.map(|server| format!("{} ({})", server.name, server.config.command))
.collect();
let selections = match MultiSelect::new()
.with_prompt("Select MCP servers (use Space to toggle, Enter to confirm)")
.items(&items)
.interact_on_opt(&dialoguer::console::Term::stdout())
{
Ok(sel) => sel,
Err(dialoguer::Error::IO(ref e)) if e.kind() == std::io::ErrorKind::Interrupted => {
return Ok(None);
},
Err(e) => return Err(eyre::eyre!("Failed to get MCP server selection: {e}")),
};
let selected_servers: Vec<&McpServerInfo> = selections
.unwrap_or_default()
.iter()
.filter_map(|&i| servers.get(i))
.collect();
Ok(Some(selected_servers))
}
impl AgentSubcommand {
pub async fn execute(self, os: &mut Os, session: &mut ChatSession) -> Result<ChatState, ChatError> {
let agents = &session.conversation.agents;
macro_rules! _print_err {
($err:expr) => {
execute!(
session.stderr,
StyledText::error_fg(),
style::Print(format!("\nError: {}\n\n", $err)),
StyledText::reset(),
)?
};
}
match self {
Self::List => {
let profiles = agents.agents.values().collect::<Vec<_>>();
let active_profile = agents.get_active();
for (i, profile) in profiles.iter().enumerate() {
if active_profile.is_some_and(|p| p == *profile) {
queue!(
session.stderr,
StyledText::success_fg(),
style::Print("* "),
style::Print(&profile.name),
StyledText::reset(),
)?;
} else {
queue!(session.stderr, style::Print(" "), style::Print(&profile.name),)?;
}
if i < profiles.len().saturating_sub(1) {
queue!(session.stderr, style::Print("\n"))?;
}
}
execute!(session.stderr, style::Print("\n"))?;
},
Self::Schema => {
use schemars::schema_for;
let schema = schema_for!(Agent);
let pretty = serde_json::to_string_pretty(&schema)
.map_err(|e| ChatError::Custom(format!("Failed to convert agent schema to string: {e}").into()))?;
highlight_json(&mut session.stderr, pretty.as_str())
.map_err(|e| ChatError::Custom(format!("Error printing agent schema: {e}").into()))?;
},
Self::Create { name, directory, from } => {
let mut agents = Agents::load(os, None, true, &mut session.stderr, session.conversation.mcp_enabled)
.await
.0;
let path_with_file_name = create_agent(os, &mut agents, name.clone(), directory, from)
.await
.map_err(|e| ChatError::Custom(Cow::Owned(e.to_string())))?;
crate::util::editor::launch_editor(&path_with_file_name)
.map_err(|e| ChatError::Custom(Cow::Owned(e.to_string())))?;
let new_agent = Agent::load(
os,
&path_with_file_name,
&mut None,
session.conversation.mcp_enabled,
&mut session.stderr,
)
.await;
match new_agent {
Ok(agent) => {
session.conversation.agents.agents.insert(agent.name.clone(), agent);
},
Err(e) => {
execute!(
session.stderr,
StyledText::error_fg(),
style::Print("Error: "),
StyledText::reset(),
style::Print(&e),
style::Print("\n"),
)?;
return Err(ChatError::Custom(
format!("Post write validation failed for agent '{name}'. Malformed config detected: {e}")
.into(),
));
},
}
execute!(
session.stderr,
StyledText::success_fg(),
style::Print("Agent "),
StyledText::brand_fg(),
style::Print(name),
StyledText::success_fg(),
style::Print(" has been created successfully"),
StyledText::reset(),
style::Print("\n"),
StyledText::warning_fg(),
style::Print("Changes take effect on next launch"),
StyledText::reset(),
)?;
},
Self::Edit { name } => {
let (_agent, path_with_file_name) = Agent::get_agent_by_name(os, &name)
.await
.map_err(|e| ChatError::Custom(Cow::Owned(e.to_string())))?;
crate::util::editor::launch_editor(&path_with_file_name)
.map_err(|e| ChatError::Custom(Cow::Owned(e.to_string())))?;
let updated_agent = Agent::load(
os,
&path_with_file_name,
&mut None,
session.conversation.mcp_enabled,
&mut session.stderr,
)
.await;
match updated_agent {
Ok(agent) => {
session.conversation.agents.agents.insert(agent.name.clone(), agent);
},
Err(e) => {
execute!(
session.stderr,
StyledText::error_fg(),
style::Print("Error: "),
StyledText::reset(),
style::Print(&e),
style::Print("\n"),
)?;
return Err(ChatError::Custom(
format!("Post edit validation failed for agent '{name}'. Malformed config detected: {e}")
.into(),
));
},
}
execute!(
session.stderr,
StyledText::success_fg(),
style::Print("Agent "),
StyledText::brand_fg(),
style::Print(name),
StyledText::success_fg(),
style::Print(" has been edited successfully"),
StyledText::reset(),
style::Print("\n"),
StyledText::warning_fg(),
style::Print("Changes take effect on next launch"),
StyledText::reset(),
)?;
},
Self::Generate {} => {
let agent_name = match crate::util::input("Enter agent name: ", None) {
Ok(input) => input.trim().to_string(),
Err(_) => {
return Ok(ChatState::PromptUser {
skip_printing_tools: true,
});
},
};
let agent_description = match crate::util::input("Enter agent description: ", None) {
Ok(input) => input.trim().to_string(),
Err(_) => {
return Ok(ChatState::PromptUser {
skip_printing_tools: true,
});
},
};
let scope_options = vec!["Local (current workspace)", "Global (all workspaces)"];
let scope_selection = match Select::with_theme(&crate::util::dialoguer_theme())
.with_prompt("Agent scope")
.items(&scope_options)
.default(0)
.interact_on_opt(&dialoguer::console::Term::stdout())
{
Ok(sel) => {
let _ = crossterm::execute!(std::io::stdout(), StyledText::emphasis_fg());
sel
},
// Ctrl‑C -> Err(Interrupted)
Err(dialoguer::Error::IO(ref e)) if e.kind() == std::io::ErrorKind::Interrupted => {
return Ok(ChatState::PromptUser {
skip_printing_tools: true,
});
},
Err(e) => return Err(ChatError::Custom(format!("Failed to get scope selection: {e}").into())),
};
let scope_selection = match scope_selection {
Some(selection) => selection,
None => {
return Ok(ChatState::PromptUser {
skip_printing_tools: true,
});
},
};
let is_global = scope_selection == 1;
let mcp_servers = get_enabled_mcp_servers(os)
.await
.map_err(|e| ChatError::Custom(e.to_string().into()))?;
let selected_servers = if mcp_servers.is_empty() {
Vec::new()
} else {
match prompt_mcp_server_selection(&mcp_servers)
.map_err(|e| ChatError::Custom(e.to_string().into()))?
{
Some(servers) => servers,
None => return Ok(ChatState::default()),
}
};
let mcp_servers_json = if !selected_servers.is_empty() {
let servers: std::collections::HashMap<String, serde_json::Value> = selected_servers
.iter()
.map(|server| {
(
server.name.clone(),
serde_json::to_value(&server.config).unwrap_or_default(),
)
})
.collect();
serde_json::to_string(&servers).unwrap_or_default()
} else {
"{}".to_string()
};
use schemars::schema_for;
let schema = schema_for!(Agent);
let schema_string = serde_json::to_string_pretty(&schema)
.map_err(|e| ChatError::Custom(format!("Failed to serialize agent schema: {e}").into()))?;
return session
.generate_agent_config(
os,
&agent_name,
&agent_description,
&mcp_servers_json,
&schema_string,
is_global,
)
.await;
},
Self::Set { .. } | Self::Delete { .. } => {
// As part of the agent implementation, we are disabling the ability to
// switch / create profile after a session has started.
// TODO: perhaps revive this after we have a decision on profile create /
// switch
let global_path = if let Ok(path) = PathResolver::new(os).global().agents_dir() {
path.to_str().unwrap_or("default global agent path").to_string()
} else {
"default global agent path".to_string()
};
execute!(
session.stderr,
StyledText::warning_fg(),
style::Print(format!(
"To make changes or create agents, please do so via create the corresponding config in {}, where you would also find an example config for your reference.\nTo switch agent, launch another instance of q chat with --agent.\n\n",
global_path
)),
StyledText::reset_attributes()
)?;
},
Self::SetDefault { name } => match session.conversation.agents.agents.get(&name) {
Some(agent) => {
os.database
.settings
.set(Setting::ChatDefaultAgent, agent.name.clone())
.await
.map_err(|e| ChatError::Custom(e.to_string().into()))?;
execute!(
session.stderr,
StyledText::success_fg(),
style::Print("✓ Default agent set to '"),
style::Print(&agent.name),
style::Print("'. This will take effect the next time q chat is launched.\n"),
StyledText::reset(),
)?;
},
None => {
execute!(
session.stderr,
StyledText::error_fg(),
style::Print("Error: "),
StyledText::reset(),
style::Print(format!("No agent with name {name} found\n")),
)?;
},
},
Self::Swap { name } => {
if let Some(name) = name {
session.conversation.swap_agent(os, &mut session.stderr, &name).await?;
} else {
let labels = session
.conversation
.agents
.agents
.keys()
.map(|name| name.as_str())
.collect::<Vec<_>>();
let name = {
let idx = match Select::with_theme(&crate::util::dialoguer_theme())
.with_prompt("Choose one of the following agents")
.items(&labels)
.default(1)
.interact_on_opt(&dialoguer::console::Term::stdout())
{
Ok(sel) => {
let _ = crossterm::execute!(std::io::stdout(), StyledText::emphasis_fg());
sel
},
// Ctrl‑C -> Err(Interrupted)
Err(dialoguer::Error::IO(ref e)) if e.kind() == std::io::ErrorKind::Interrupted => None,
Err(e) => {
return Err(ChatError::Custom(
format!("Dialog has failed to make a selection {e}").into(),
));
},
};
idx.and_then(|idx| labels.get(idx).cloned().map(str::to_string))
};
if let Some(name) = name {
session.conversation.swap_agent(os, &mut session.stderr, &name).await?;
}
}
},
}
Ok(ChatState::PromptUser {
skip_printing_tools: true,
})
}
pub fn name(&self) -> &'static str {
match self {
Self::List => "list",
Self::Create { .. } => "create",
Self::Edit { .. } => "edit",
Self::Generate { .. } => "generate",
Self::Delete { .. } => "delete",
Self::Set { .. } => "set",
Self::Schema => "schema",
Self::SetDefault { .. } => "set_default",
Self::Swap { .. } => "swap",
}
}
}
fn highlight_json(output: &mut impl Write, json_str: &str) -> eyre::Result<()> {
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let syntax = ps
.find_syntax_by_extension("json")
.ok_or(eyre::eyre!("No syntax found by extension"))?;
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
for line in LinesWithEndings::from(json_str) {
let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps)?;
let escaped = as_24_bit_terminal_escaped(&ranges[..], false);
queue!(output, style::Print(escaped))?;
}
Ok(execute!(output, StyledText::reset())?)
}
/// Searches all configuration sources for MCP servers and returns a deduplicated list.
/// Priority order: Agent configs > Workspace legacy > Global legacy
pub async fn get_all_available_mcp_servers(os: &mut Os) -> Result<Vec<McpServerInfo>> {
let mut servers = HashMap::<String, McpServerInfo>::new();
// 1. Load from agent configurations (highest priority)
let mut null_writer = NullWriter;
let (agents, _) = Agents::load(os, None, true, &mut null_writer, true).await;
for (_, agent) in agents.agents {
for (server_name, server_config) in agent.mcp_servers.mcp_servers {
if !servers.values().any(|s| s.config.command == server_config.command) {
servers.insert(server_name.clone(), McpServerInfo {
name: server_name,
config: server_config,
});
}
}
}
let resolver = PathResolver::new(os);
// 2. Load from workspace legacy config (medium priority)
if let Ok(workspace_path) = resolver.workspace().mcp_config() {
if let Ok(workspace_config) = McpServerConfig::load_from_file(os, workspace_path).await {
for (server_name, server_config) in workspace_config.mcp_servers {
if !servers.values().any(|s| s.config.command == server_config.command) {
servers.insert(server_name.clone(), McpServerInfo {
name: server_name,
config: server_config,
});
}
}
}
}
// 3. Load from global legacy config (lowest priority)
if let Ok(global_path) = resolver.global().mcp_config() {
if let Ok(global_config) = McpServerConfig::load_from_file(os, global_path).await {
for (server_name, server_config) in global_config.mcp_servers {
if !servers.values().any(|s| s.config.command == server_config.command) {
servers.insert(server_name.clone(), McpServerInfo {
name: server_name,
config: server_config,
});
}
}
}
}
Ok(servers.into_values().collect())
}
/// Get only enabled MCP servers (excludes disabled ones)
pub async fn get_enabled_mcp_servers(os: &mut Os) -> Result<Vec<McpServerInfo>> {
let all_servers = get_all_available_mcp_servers(os).await?;
Ok(all_servers
.into_iter()
.filter(|server| !server.config.disabled)
.collect())
}