devai 0.5.12

Command Agent runner to accelerate production coding with genai.
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
use crate::agent::agent_options::AgentOptions;
use crate::agent::{Agent, AgentDoc};
use crate::run::{DirContext, PathResolver};
use crate::support::tomls::parse_toml;
use crate::{Error, Result};
use simple_fs::{list_files, read_to_string, SFile, SPath};
use std::collections::HashSet;
use std::path::Path;
use strsim::levenshtein;

/// - `from_cli` Used to decide which "reference dir" should be used.
///              If from cli, the current_dir will be used, otherwise, the workspace_dir
pub fn find_agent(agent_name: &str, dir_context: &DirContext, mode: PathResolver) -> Result<Agent> {
	let base_config = load_base_agent_config(dir_context)?;

	let devai_dir = dir_context.devai_dir();

	// -- For now, if end with .devai, we try to find direct
	let agent_sname = SPath::new(agent_name)?;
	if agent_sname.ext() == "devai" {
		let agent_file = dir_context.resolve_path(&agent_sname, mode)?;
		let agent_file =
			SFile::try_from(agent_file).map_err(|_| Error::CommandAgentNotFound(agent_sname.to_string()))?;
		let doc = AgentDoc::from_file(agent_file)?;
		return doc.into_agent(agent_sname, base_config);
	}

	// -- Otherwise, look in the command-agent dirs
	let dirs = devai_dir.get_agent_dirs()?;
	let dirs = dirs.iter().map(|dir| dir.path()).collect::<Vec<_>>();

	// Attempt to find the agent in the specified directories
	if let Some(agent_doc) = find_agent_doc_in_dir(agent_name, &dirs)? {
		return agent_doc.into_agent(agent_name, base_config);
	}

	// If not found, return an error with potential similar agents
	let similar_paths = find_similar_agent_paths(agent_name, &dirs)?;
	let error_msg = if !similar_paths.is_empty() {
		format!(
			"Agent '{}' not found.\nDid you mean one of these?\n{}",
			agent_name,
			similar_paths
				.iter()
				.map(agent_agent_rel_as_bullet)
				.collect::<Vec<String>>()
				.join("\n")
		)
	} else {
		let agent_rels = list_all_agent_rels(dir_context)?;
		format!(
			"Agent '{}' not found.\nHere is the list of available command agents:\n{}",
			agent_name,
			agent_rels
				.iter()
				.map(agent_agent_rel_as_bullet)
				.collect::<Vec<String>>()
				.join("\n")
		)
	};

	Err(error_msg.into())
}

/// Lists all agent files following the precedence rules (customs first, defaults second).
/// Agent files already present in a higher priority directory are not included.
pub fn list_all_agent_rels(dir_context: &DirContext) -> Result<Vec<AgentRel>> {
	let dirs = dir_context.devai_dir().get_agent_dirs()?;
	let mut agent_rels = Vec::new();

	let mut file_stems: HashSet<String> = HashSet::new();

	for dir in dirs {
		let files = list_files(&dir, Some(&["**/*.devai"]), None)?;
		for file in files.into_iter() {
			let Some(agent_rel) = AgentRel::new(&dir, &file) else {
				continue;
			};

			let rel_stem = agent_rel.rel_path_stem().to_string();
			if file_stems.contains(&rel_stem) {
				continue;
			}
			agent_rels.push(agent_rel);
			file_stems.insert(rel_stem);
		}
	}

	Ok(agent_rels)
}

/// Note: For now, needs to be public because of `exec_list`
pub fn agent_agent_rel_as_bullet(agent_rel: &AgentRel) -> String {
	let rel_stem = agent_rel.rel_path_stem();
	let initials = agent_rel.initials();
	let path = agent_rel.sfile.to_str();
	let msg = format!("- {rel_stem} ({initials})");
	let msg = format!("{msg:<37} - for '{path}'");

	msg
}

// region:    --- Support
/// Finds the first matching AgentDoc in the provided directories.
fn find_agent_doc_in_dir(name: &str, dirs: &[&Path]) -> Result<Option<AgentDoc>> {
	for &dir in dirs {
		let files = list_files(dir, Some(&["**/*.devai"]), None)?;

		let mut main_files: Vec<SFile> = Vec::new();

		// First try to find the full path name like `my-agent`/`ma`, `my-mod/super`/`mm/s`
		if let Some(found_file) = files.into_iter().find(|f| {
			// Note: here we capture if the f.stem is "main" so that we can quickly match it below.
			if f.stem() == "main" {
				main_files.push(f.clone())
			}
			match_agent(dir, name, f)
		}) {
			// NOTE: Because the dirs are form the DevaiDir and might not be absolute, and relative to working dir
			//       But later, need to remove from the current_dir of the DirContext, so, needs full path
			let found_file = found_file.canonicalize()?;
			let doc = AgentDoc::from_file(found_file)?;
			return Ok(Some(doc));
		}

		// if not found, then, perhaps is the just mode agent and want to have a main
		let name = format!("{name}/main");
		for sfile in main_files {
			if match_agent(dir, &name, &sfile) {
				// NOTE: See above
				let found_file = sfile.canonicalize()?;
				let doc = AgentDoc::from_file(found_file)?;
				return Ok(Some(doc));
			}
		}
	}
	Ok(None)
}

fn match_agent(base_dir: &Path, name: &str, sfile: &SFile) -> bool {
	let Some(agent_rel) = AgentRel::new(base_dir, sfile) else {
		return false;
	};
	name == agent_rel.rel_path_stem() || name == agent_rel.initials()
}

/// The structure that represent the agent and relative path
#[derive(Debug)]
pub struct AgentRel {
	sfile: SFile,
	rel_path: SPath,
}

impl AgentRel {
	fn new(base_dir: impl AsRef<Path>, sfile: &SFile) -> Option<Self> {
		let rel_path = sfile.diff(base_dir).ok()?;
		Some(AgentRel {
			rel_path,
			sfile: sfile.clone(),
		})
	}

	/// Return the rel_path (with `.devai` extension) as str
	fn to_str(&self) -> &str {
		self.rel_path.to_str()
	}

	/// Remove the `.devai` suffix
	fn rel_path_stem(&self) -> &str {
		let rel_path_str = self.to_str();
		rel_path_str.strip_suffix(".devai").unwrap_or(rel_path_str)
	}

	fn initials(&self) -> String {
		get_initials(self.to_str())
	}
}

fn get_initials(input: &str) -> String {
	input
		.split('/') // Split by '/'
		.map(|segment| {
			segment
				.split('-') // Split by '-'
				.filter_map(|part| {
					let part = part.strip_prefix("_").unwrap_or(part);
					part.chars().next()
				}) // Get the first character of each part
				.collect::<String>() // Collect into a String
		})
		.collect::<Vec<String>>() // Collect segments into a Vec
		.join("/") // Join segments with '/'
}

/// Finds the top 3 most similar agent file paths based on Levenshtein distance.
fn find_similar_agent_paths(name: &str, dirs: &[&Path]) -> Result<Vec<AgentRel>> {
	let mut candidates = Vec::new();

	for dir in dirs {
		let files = list_files(dir, Some(&["**/*.devai"]), None)?;
		for file in files {
			if let Some(agent_rel) = AgentRel::new(dir, &file) {
				candidates.push(agent_rel);
			};
		}
	}

	let mut scored_candidates: Vec<(AgentRel, usize)> = candidates
		.into_iter()
		.filter_map(|agent_rel| {
			let agent_rel_stem = agent_rel.rel_path_stem();
			let distance = levenshtein(name, agent_rel_stem);

			// note might need to change this one, seems to work ok
			const MAX_DISTANCE: usize = 5;
			if distance > MAX_DISTANCE {
				None
			} else {
				Some((agent_rel, distance))
			}
		})
		.collect();

	// Sort by ascending distance
	scored_candidates.sort_by_key(|&(_, distance)| distance);

	// Take the top 3
	let top_three = scored_candidates.into_iter().take(3).map(|(path, _)| path).collect();

	Ok(top_three)
}

/// Loads the base agent configuration.
pub fn load_base_agent_config(dir_context: &DirContext) -> Result<AgentOptions> {
	let config_path = dir_context.devai_dir().get_config_toml_path()?;
	let config_content = read_to_string(&config_path)?;
	let config_value = parse_toml(&config_content)?;

	let options = AgentOptions::from_config_value(config_value).map_err(|err| Error::Config {
		path: config_path.to_string(),
		reason: err.to_string(),
	})?;
	Ok(options)
}

// endregion: --- Support

// region:    --- Tests

#[cfg(test)]
mod tests {
	use super::*;
	use crate::_test_support::{run_test_agent, SANDBOX_01_DIR};
	use crate::run::Runtime;
	use simple_fs::ensure_dir;
	use value_ext::JsonValueExt;

	type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>; // For tests.

	#[test]
	fn test_get_initials() -> Result<()> {
		let test_cases: &[(&str, &str)] = &[
			("proof-read", "pr"),
			("proof-comment", "pc"),
			("proof", "p"),
			("_proof-comment", "pc"),
			("proof-_comment-read", "pcr"),
			("_proof-_comment-_read", "pcr"),
			("a-b-c", "abc"),
			("hello/world", "h/w"),
			("hello/big-world", "h/bw"),
			("nice-hello/big-world", "nh/bw"),
			("nice-hello/_big-world", "nh/bw"),
		];

		for &(content, expected) in test_cases {
			assert_eq!(get_initials(content), expected);
		}

		Ok(())
	}

	#[tokio::test]
	async fn test_find_command_agent_direct_and_validate_ctx() -> Result<()> {
		// -- Setup & Fixtures
		let runtime = Runtime::new_test_runtime_sandbox_01()?;
		let fx_agent_name = "./agent-script/agent-ctx-reflect.devai";

		// -- Exec
		let agent = find_agent(fx_agent_name, runtime.dir_context(), PathResolver::CurrentDir)?;
		let res = run_test_agent(&runtime, &agent).await?;

		// -- Check
		// workspace_dir
		let workspace_dir = res.x_get_as::<&str>("WORKSPACE_DIR")?;
		assert!(Path::new(workspace_dir).is_absolute(), "workspace_dir must be absolute");
		assert!(
			workspace_dir.ends_with("tests-data/sandbox-01"),
			"WORKSPACE_DIR must end with 'tests-data/sandbox-01'"
		);

		// devai dir
		assert_eq!(res.x_get_as::<&str>("DEVAI_DIR")?, "./.devai");

		assert_eq!(res.x_get_as::<&str>("AGENT_NAME")?, fx_agent_name);
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_PATH")?, fx_agent_name);
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_DIR")?, "./agent-script");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_NAME")?, "agent-ctx-reflect.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_STEM")?, "agent-ctx-reflect");

		Ok(())
	}

	#[tokio::test]
	async fn test_find_command_agent_custom_and_validate_ctx() -> Result<()> {
		// -- Setup & Fixtures
		// TODO: Probably need to run the init in sandbox_01
		let runtime = Runtime::new_test_runtime_sandbox_01()?;
		ensure_dir("tests-data/sandbox-01/.devai/custom/agent")?;
		std::fs::copy(
			Path::new(SANDBOX_01_DIR).join("agent-script/agent-ctx-reflect.devai"),
			"tests-data/sandbox-01/.devai/custom/agent/command-ctx-reflect.devai",
		)?;
		// -- Exec
		let agent = find_agent("command-ctx-reflect", runtime.dir_context(), PathResolver::CurrentDir)?;
		let res = run_test_agent(&runtime, &agent).await?;

		// -- Check
		// workspace_dir
		let workspace_dir = res.x_get_as::<&str>("WORKSPACE_DIR")?;
		assert!(Path::new(workspace_dir).is_absolute(), "workspace_dir must be absolute");
		assert!(
			workspace_dir.ends_with("tests-data/sandbox-01"),
			"WORKSPACE_DIR must end with 'tests-data/sandbox-01'"
		);

		// devai dir
		assert_eq!(res.x_get_as::<&str>("DEVAI_DIR")?, "./.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_NAME")?, "command-ctx-reflect");
		assert_eq!(
			res.x_get_as::<&str>("AGENT_FILE_PATH")?,
			"./.devai/custom/agent/command-ctx-reflect.devai"
		);
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_DIR")?, "./.devai/custom/agent");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_NAME")?, "command-ctx-reflect.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_STEM")?, "command-ctx-reflect");

		Ok(())
	}

	#[tokio::test]
	async fn test_find_command_agent_nested_full_name() -> Result<()> {
		// -- Setup & Fixtures
		// TODO: Probably need to run the init in sandbox_01
		let runtime = Runtime::new_test_runtime_sandbox_01()?;
		ensure_dir("tests-data/sandbox-01/.devai/custom/agent/sub-dir/")?;
		std::fs::copy(
			Path::new(SANDBOX_01_DIR).join("agent-script/agent-ctx-reflect.devai"),
			"tests-data/sandbox-01/.devai/custom/agent/sub-dir/sub-agent.devai",
		)?;
		// -- Exec
		let agent = find_agent("sub-dir/sub-agent", runtime.dir_context(), PathResolver::CurrentDir)?;
		let res = run_test_agent(&runtime, &agent).await?;

		// -- Check
		// workspace_dir
		let workspace_dir = res.x_get_as::<&str>("WORKSPACE_DIR")?;
		assert!(Path::new(workspace_dir).is_absolute(), "workspace_dir must be absolute");
		assert!(
			workspace_dir.ends_with("tests-data/sandbox-01"),
			"WORKSPACE_DIR must end with 'tests-data/sandbox-01'"
		);

		// devai dir
		assert_eq!(res.x_get_as::<&str>("DEVAI_DIR")?, "./.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_NAME")?, "sub-dir/sub-agent");
		assert_eq!(
			res.x_get_as::<&str>("AGENT_FILE_PATH")?,
			"./.devai/custom/agent/sub-dir/sub-agent.devai"
		);
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_DIR")?, "./.devai/custom/agent/sub-dir");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_NAME")?, "sub-agent.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_STEM")?, "sub-agent");

		Ok(())
	}

	#[tokio::test]
	async fn test_find_command_agent_nested_main() -> Result<()> {
		// -- Setup & Fixtures
		// TODO: Probably need to run the init in sandbox_01
		let runtime = Runtime::new_test_runtime_sandbox_01()?;
		ensure_dir("tests-data/sandbox-01/.devai/custom/agent/mod-agent/")?;
		std::fs::copy(
			Path::new(SANDBOX_01_DIR).join("agent-script/agent-ctx-reflect.devai"),
			"tests-data/sandbox-01/.devai/custom/agent/mod-agent/main.devai",
		)?;
		// -- Exec
		let agent = find_agent("mod-agent", runtime.dir_context(), PathResolver::CurrentDir)?;
		let res = run_test_agent(&runtime, &agent).await?;

		// -- Check
		// workspace_dir
		let workspace_dir = res.x_get_as::<&str>("WORKSPACE_DIR")?;
		assert!(Path::new(workspace_dir).is_absolute(), "workspace_dir must be absolute");
		assert!(
			workspace_dir.ends_with("tests-data/sandbox-01"),
			"WORKSPACE_DIR must end with 'tests-data/sandbox-01'"
		);

		// devai dir
		assert_eq!(res.x_get_as::<&str>("DEVAI_DIR")?, "./.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_NAME")?, "mod-agent");
		assert_eq!(
			res.x_get_as::<&str>("AGENT_FILE_PATH")?,
			"./.devai/custom/agent/mod-agent/main.devai"
		);
		assert_eq!(
			res.x_get_as::<&str>("AGENT_FILE_DIR")?,
			"./.devai/custom/agent/mod-agent"
		);
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_NAME")?, "main.devai");
		assert_eq!(res.x_get_as::<&str>("AGENT_FILE_STEM")?, "main");

		Ok(())
	}
}

// endregion: --- Tests