fix: am ai avec aichat >= 0.30 — rôles index.yaml + agents.txt, contexte filtré, generation --code (v1.1.5)
- roles.rs: aichat 0.30 a remplace les agents markdown agents/<name>.md
par functions/agents/<name>/index.yaml + registre functions/agents.txt;
generate_all ecrit les DEUX formats (compat <= 0.29 et >= 0.30)
- ai_cmd.rs: role_args detecte la definition index.yaml en premier
('Unknown agent am-copilot' corrige) ; contexte par defaut (dossier
courant) filtre par am (.git, target, node_modules, binaires non-UTF-8,
.env*, > 64 KiB, budget 128 KiB, 40 fichiers max) — aichat ne filtre
rien et echouait sur .git/index ; exec mode utilise le role am-do
par defaut (commande brute)
- shell_ai.rs: generation via --code au lieu de --execute --dry-run
(aichat 0.30: --dry-run echo la requete sans appeler l'API)
- version 1.1.5, man pages et ROADMAP maj
This commit is contained in:
+120
-23
@@ -1,12 +1,16 @@
|
||||
//! am roles — the aichat copilot layer (épique v1.1.0, F3).
|
||||
//!
|
||||
//! Generates aichat agent files (`~/.config/aichat/agents/am-*.md` — or
|
||||
//! `%APPDATA%\aichat\agents\` on Windows) that turn `am ai` into a
|
||||
//! Generates aichat agent definitions that turn `am ai` into a
|
||||
//! contextual copilot: guide & catalog, secured OS operator, dev helper,
|
||||
//! raw execution, analyst, orchestrator. Each prompt is bilingual (French
|
||||
//! by default, English supported) and instructs the model to use the
|
||||
//! stable `--json` contracts of `am` to inspect the environment.
|
||||
//!
|
||||
//! Two layouts are written for compatibility with every aichat version:
|
||||
//! - aichat >= 0.30 : `functions/agents/<name>/index.yaml` (definition)
|
||||
//! + `functions/agents.txt` (registry), under the aichat config dir;
|
||||
//! - aichat <= 0.29 : legacy markdown agents `agents/<name>.md`.
|
||||
//!
|
||||
//! The aichat config file is only created when it does not exist yet —
|
||||
//! an existing configuration is never touched (respect of the user setup).
|
||||
|
||||
@@ -24,20 +28,20 @@ pub const ROLE_NAMES: &[&str] = &[
|
||||
"am-orchestrator",
|
||||
];
|
||||
|
||||
/// Directory where aichat stores its agent definitions. Candidates are
|
||||
/// probed in order: an existing aichat config wins, otherwise the platform
|
||||
/// default (XDG on unix, %APPDATA% on Windows).
|
||||
pub fn aichat_agents_dir() -> PathBuf {
|
||||
/// Base config directory of aichat. Candidates are probed in order: an
|
||||
/// existing aichat config wins, otherwise the platform default (XDG on
|
||||
/// unix, %APPDATA% on Windows).
|
||||
pub fn aichat_config_dir() -> PathBuf {
|
||||
let mut candidates: Vec<PathBuf> = Vec::new();
|
||||
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
|
||||
candidates.push(PathBuf::from(xdg).join("aichat").join("agents"));
|
||||
candidates.push(PathBuf::from(xdg).join("aichat"));
|
||||
}
|
||||
if let Some(home) = crate::config::home_dir() {
|
||||
candidates.push(home.join(".config").join("aichat").join("agents"));
|
||||
candidates.push(home.join(".config").join("aichat"));
|
||||
}
|
||||
if cfg!(windows) {
|
||||
if let Ok(appdata) = std::env::var("APPDATA") {
|
||||
candidates.push(PathBuf::from(appdata).join("aichat").join("agents"));
|
||||
candidates.push(PathBuf::from(appdata).join("aichat"));
|
||||
}
|
||||
}
|
||||
for c in &candidates {
|
||||
@@ -48,19 +52,38 @@ pub fn aichat_agents_dir() -> PathBuf {
|
||||
// No existing aichat config: platform default.
|
||||
if cfg!(windows) {
|
||||
if let Ok(appdata) = std::env::var("APPDATA") {
|
||||
return PathBuf::from(appdata).join("aichat").join("agents");
|
||||
return PathBuf::from(appdata).join("aichat");
|
||||
}
|
||||
}
|
||||
crate::config::home_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join(".config")
|
||||
.join("aichat")
|
||||
.join("agents")
|
||||
}
|
||||
|
||||
/// Legacy agents directory (aichat <= 0.29): `agents/<name>.md` files
|
||||
/// with frontmatter. aichat >= 0.30 reads the new definitions instead
|
||||
/// (see [`aichat_agent_def_dir`]); the .md files are still written so
|
||||
/// older aichat versions keep working.
|
||||
pub fn aichat_agents_dir() -> PathBuf {
|
||||
aichat_config_dir().join("agents")
|
||||
}
|
||||
|
||||
/// Functions directory (aichat >= 0.30): agent definitions live in
|
||||
/// `functions/agents/<name>/index.yaml` and the agent registry in
|
||||
/// `functions/agents.txt`.
|
||||
pub fn aichat_functions_dir() -> PathBuf {
|
||||
aichat_config_dir().join("functions")
|
||||
}
|
||||
|
||||
/// Directory of one agent definition (aichat >= 0.30).
|
||||
pub fn aichat_agent_def_dir(name: &str) -> PathBuf {
|
||||
aichat_functions_dir().join("agents").join(name)
|
||||
}
|
||||
|
||||
/// Path of the aichat configuration file (created only if absent).
|
||||
pub fn aichat_config_path() -> PathBuf {
|
||||
aichat_agents_dir().parent().unwrap().join("config.yaml")
|
||||
aichat_config_dir().join("config.yaml")
|
||||
}
|
||||
|
||||
/// The list of `am` commands, embedded in the copilot prompt so the model
|
||||
@@ -99,8 +122,8 @@ fn frontmatter(name: &str, description: &str, tools: &[&str]) -> String {
|
||||
/// The bilingual instruction footer shared by every role.
|
||||
const LANG_FOOTER: &str = "\n\n## Langue / Language\nRéponds en français par défaut, dans la langue de la demande si une autre est utilisée.\nAnswer in French by default, or in the language of the request when it differs.\n";
|
||||
|
||||
/// Build the markdown content of one role.
|
||||
pub fn role_content(name: &str) -> Option<String> {
|
||||
/// The (description, tools, body) of one role.
|
||||
fn role_parts(name: &str) -> Option<(&'static str, &'static [&'static str], String)> {
|
||||
let (desc, tools, body) = match name {
|
||||
"am-copilot" => (
|
||||
"Copilot de agent-manager : guide, catalogue, dépannage",
|
||||
@@ -181,25 +204,76 @@ pub fn role_content(name: &str) -> Option<String> {
|
||||
),
|
||||
_ => return None,
|
||||
};
|
||||
let content = format!(
|
||||
Some((desc, tools, body))
|
||||
}
|
||||
|
||||
/// The aichat >= 0.30 agent definition (`index.yaml`): the subset of
|
||||
/// aichat's AgentDefinition that am generates.
|
||||
#[derive(serde::Serialize)]
|
||||
struct AgentDefinition {
|
||||
name: String,
|
||||
description: String,
|
||||
version: String,
|
||||
instructions: String,
|
||||
}
|
||||
|
||||
/// Build the legacy markdown content of one role (aichat <= 0.29).
|
||||
pub fn role_content(name: &str) -> Option<String> {
|
||||
let (desc, tools, body) = role_parts(name)?;
|
||||
Some(format!(
|
||||
"{}{}{}",
|
||||
frontmatter(name, desc, tools),
|
||||
body,
|
||||
LANG_FOOTER
|
||||
);
|
||||
Some(content)
|
||||
))
|
||||
}
|
||||
|
||||
/// Write every am-* role into the aichat agents directory (creates the
|
||||
/// directory when missing). Existing files with the same name are
|
||||
/// overwritten — they are generated artifacts of am.
|
||||
/// The aichat >= 0.30 `index.yaml` definition of one role.
|
||||
pub fn role_definition(name: &str) -> Option<String> {
|
||||
let (desc, _tools, body) = role_parts(name)?;
|
||||
let def = AgentDefinition {
|
||||
name: name.to_string(),
|
||||
description: desc.to_string(),
|
||||
version: "1.0.0".to_string(),
|
||||
instructions: format!("{body}{LANG_FOOTER}"),
|
||||
};
|
||||
serde_yaml::to_string(&def).ok()
|
||||
}
|
||||
|
||||
/// Write every am-* role into the aichat agent stores (creates the
|
||||
/// directories when missing). Two layouts are written so any aichat
|
||||
/// version works:
|
||||
/// - aichat >= 0.30 : `functions/agents/<name>/index.yaml` (definition)
|
||||
/// + `functions/agents.txt` (registry);
|
||||
/// - aichat <= 0.29 : legacy markdown `agents/<name>.md`.
|
||||
/// Existing files with the same name are overwritten — they are generated
|
||||
/// artifacts of am.
|
||||
pub fn generate_all(_app: &App) -> Result<Vec<PathBuf>> {
|
||||
let dir = aichat_agents_dir();
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
let mut written = Vec::new();
|
||||
|
||||
// New layout (aichat >= 0.30): index.yaml definitions + agents.txt.
|
||||
let funcs_dir = aichat_functions_dir();
|
||||
let agents_dir = funcs_dir.join("agents");
|
||||
std::fs::create_dir_all(&agents_dir)?;
|
||||
for name in ROLE_NAMES {
|
||||
let def = role_definition(name).expect("known role");
|
||||
let def_path = agents_dir.join(name).join("index.yaml");
|
||||
std::fs::create_dir_all(def_path.parent().expect("parent dir"))?;
|
||||
std::fs::write(&def_path, def)?;
|
||||
written.push(def_path);
|
||||
}
|
||||
let list_path = funcs_dir.join("agents.txt");
|
||||
let mut list = String::from("# generated by `am setup` — aichat >= 0.30 agent registry\n");
|
||||
list.push_str(&format!("{}\n", ROLE_NAMES.join("\n")));
|
||||
std::fs::write(&list_path, list)?;
|
||||
written.push(list_path);
|
||||
|
||||
// Legacy layout (aichat <= 0.29): markdown agents in agents/.
|
||||
let legacy_dir = aichat_agents_dir();
|
||||
std::fs::create_dir_all(&legacy_dir)?;
|
||||
for name in ROLE_NAMES {
|
||||
let content = role_content(name).expect("known role");
|
||||
let path = dir.join(format!("{name}.md"));
|
||||
let path = legacy_dir.join(format!("{name}.md"));
|
||||
std::fs::write(&path, content)?;
|
||||
written.push(path);
|
||||
}
|
||||
@@ -266,6 +340,29 @@ mod tests {
|
||||
assert!(copilot.contains("setup"), "command list must include setup");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn role_definition_is_valid_index_yaml() {
|
||||
for name in ROLE_NAMES {
|
||||
let def = role_definition(name).expect("role");
|
||||
assert!(def.starts_with("name:"), "{name}:\n{def}");
|
||||
assert!(def.contains("description:"), "{name}");
|
||||
assert!(def.contains("instructions:"), "{name}");
|
||||
assert!(def.contains("Tu es"), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_def_dir_targets_functions_layout() {
|
||||
let s = aichat_agent_def_dir("am-copilot")
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
assert!(s.contains("functions"), "{s}");
|
||||
assert!(s.ends_with("am-copilot"), "{s}");
|
||||
// The registry lives next to the definitions.
|
||||
let registry = aichat_functions_dir().join("agents.txt");
|
||||
assert!(registry.to_string_lossy().ends_with("agents.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agents_dir_prefers_existing_aichat_config() {
|
||||
// With a home dir, the unix candidate is ~/.config/aichat/agents.
|
||||
|
||||
Reference in New Issue
Block a user