use std::error::Error;
use std::num::NonZeroUsize;
use std::path::Path;
use serde_json::Value;
use crate::port::FakeFileTree;
use crate::types::frontmatter::{AgentMeta, SkillMeta};
use crate::types::hooks::HookEvent;
use crate::types::manifest::PackVersion;
use crate::types::name::{ItemName, PackName, ToolName};
use crate::types::op::answer::ValuesResult;
use crate::types::op::payload::{
PlanAction, PlanAgent, PlanFile, PlanHook, PlanInput, PlanNative, PlanNativeHook, PlanPack,
PlanSkill, ValuesInput,
};
use crate::types::op::{
ItemId, LuaType, OpPath, Operation, Origin, Proposal, Proposals, SkipReason,
};
use crate::types::pack::Pack;
use crate::types::tokens::{TextOrigin, Token, scan};
use crate::types::tree::TreePath;
pub(super) fn doc(name: &str, tail: &str) -> String {
format!("---\nname: {name}\ndescription: d\n---\n\n{tail}\n")
}
pub(super) fn load(
name: &str,
tables: &str,
files: &[(&str, &str)],
) -> Result<Pack, Box<dyn Error>> {
let manifest = format!(
"[pack]\nname = \"{name}\"\nversion = \"0.1.0\"\napi = 0\ndescription = \"d\"\n{tables}"
);
let mut tree =
FakeFileTree::new(format!("packs/{name}")).file("pack.toml", manifest.as_bytes());
for (path, text) in files {
tree = tree.file(path, text.as_bytes());
}
Pack::load(&tree).map_err(|found| {
let shown: Vec<String> = found
.into_sorted()
.iter()
.map(ToString::to_string)
.collect();
shown.join("; ").into()
})
}
pub(super) fn json_word(value: &Value) -> LuaType {
match value {
Value::Null => LuaType::Nil,
Value::Bool(_) => LuaType::Boolean,
Value::Number(_) => LuaType::Number,
Value::String(_) => LuaType::String,
Value::Array(_) => LuaType::List,
Value::Object(_) => LuaType::Table,
}
}
pub(super) fn values_input() -> Result<ValuesInput, Box<dyn Error>> {
let skill = doc("status", "{{var tmpdir}} {{var session_id}}");
let pack = load(
"core",
"",
&[
("skills/status/SKILL.md", &skill),
("agents/coder.md", &doc("coder", "x")),
],
)?;
Ok(ValuesInput::new(ToolName::new("claude")?, &[&pack]))
}
pub(super) fn values_result() -> Result<ValuesResult, Box<dyn Error>> {
let text = r#"{"agents":{"core/coder":"coder"},"root":".claude/agentstacks","skills":{"core/status":{"dir":".claude/skills/status","invoke":"/status"}},"vars":{"tmpdir":"$TMPDIR"}}"#;
let value: Value = serde_json::from_str(text)?;
let object = value.as_object().ok_or("an object")?;
ValuesResult::read(
object,
&values_input()?,
Path::new("claude/adapter.lua"),
json_word,
)
.map_err(|found| format!("{found:?}").into())
}
pub(super) fn tokens(text: &str) -> Vec<Token> {
let origin = TextOrigin::Lines {
file: "x.md".into(),
first_line: NonZeroUsize::MIN,
};
let (found, _) = scan(text, &origin);
found
.iter()
.map(|located| located.token().clone())
.collect()
}
fn call_at(tool: &str, n: usize) -> Result<Origin, Box<dyn Error>> {
Ok(Origin::Extension {
tool: ToolName::new(tool)?,
file: rel(&["adapter.lua"]),
line: NonZeroUsize::new(n).ok_or("a line starts at 1")?,
})
}
pub(super) fn cover(
proposals: &mut Proposals,
input: &PlanInput,
tool: &str,
ids: &[&str],
) -> Result<(), Box<dyn Error>> {
for (index, text) in ids.iter().enumerate() {
let op = Operation::Write {
path: OpPath::new(&format!(".{tool}/out/{text}"))?,
content: Vec::new(),
covers: vec![input.cover(1, text)?],
};
let origin = call_at(tool, index + 1)?;
proposals.propose(Proposal { op, origin })?;
}
Ok(())
}
pub(super) fn skip_all(
proposals: &mut Proposals,
input: &PlanInput,
tool: &str,
ids: &[&str],
) -> Result<(), Box<dyn Error>> {
for (index, text) in ids.iter().enumerate() {
let op = Operation::Skip {
item: input.skip_item(text)?,
reason: SkipReason::new("not for this tool")?,
};
let origin = call_at(tool, index + 1)?;
proposals.propose(Proposal { op, origin })?;
}
Ok(())
}
pub(super) fn write_path(
proposals: &mut Proposals,
tool: &str,
path: &str,
) -> Result<(), Box<dyn Error>> {
let op = Operation::Write {
path: OpPath::new(path)?,
content: Vec::new(),
covers: Vec::new(),
};
proposals.propose(Proposal {
op,
origin: call_at(tool, 1)?,
})?;
Ok(())
}
pub(super) fn record_runtime(
proposals: &mut Proposals,
tool: &str,
root: &OpPath,
) -> Result<(), Box<dyn Error>> {
let tool = ToolName::new(tool)?;
let op = Operation::Write {
path: root.join("runtime.lua"),
content: Vec::new(),
covers: Vec::new(),
};
let origin = Origin::Tool { tool: tool.clone() };
proposals
.record_core(
&tool,
Proposal { op, origin },
Path::new("agentstacks.toml"),
)
.map_err(|found| found.to_string())?;
Ok(())
}
pub(super) fn rel(parts: &[&str]) -> TreePath {
TreePath::from_parts(parts)
}
pub(super) fn lua_action() -> PlanAction {
PlanAction::Lua {
may_block: false,
command: vec!["airsl".to_owned(), "run".to_owned()],
}
}
pub(super) fn plan_input() -> Result<PlanInput, Box<dyn Error>> {
let core = PackName::new("core")?;
let status = ItemName::new("status")?;
let coder = ItemName::new("coder")?;
let claude = ToolName::new("claude")?;
let skill_text = doc("status", "body");
let skill = PlanSkill {
id: ItemId::skill(&core, &status),
name: status.clone(),
dir: OpPath::new(".claude/skills/status")?,
meta: SkillMeta::parse(Path::new("SKILL.md"), &skill_text, &status)
.map_err(|found| format!("{found:?}"))?,
files: vec![
PlanFile {
id: ItemId::skill_file(&core, &status, &rel(&["SKILL.md"])),
rel: rel(&["SKILL.md"]),
text: Some("body".to_owned()),
},
PlanFile {
id: ItemId::skill_file(&core, &status, &rel(&["logo.png"])),
rel: rel(&["logo.png"]),
text: None,
},
],
};
let agent_text = doc("coder", "body");
let agent = PlanAgent {
id: ItemId::agent(&core, &coder),
name: coder.clone(),
meta: AgentMeta::parse(Path::new("coder.md"), &agent_text, &coder)
.map_err(|found| format!("{found:?}"))?,
text: "body".to_owned(),
};
let hooks = vec![
PlanHook {
id: ItemId::hook(&core, 0),
event: HookEvent::SessionEnd,
matches: None,
agents: None,
action: lua_action(),
},
PlanHook {
id: ItemId::hook(&core, 1),
event: HookEvent::SessionEnd,
matches: None,
agents: None,
action: PlanAction::Message {
text: "hi".to_owned(),
},
},
];
let native = PlanNative {
files: vec![PlanFile {
id: ItemId::native_file(&core, &claude, &rel(&["x.md"])),
rel: rel(&["x.md"]),
text: Some("x".to_owned()),
}],
hooks: vec![PlanNativeHook {
id: ItemId::native_hook(&core, 0),
event: "Stop".to_owned(),
matches: None,
action: lua_action(),
}],
};
Ok(PlanInput {
tool: claude,
root: OpPath::new(".claude/agentstacks")?,
packs: vec![PlanPack {
name: core,
version: PackVersion::new("0.1.0")?,
skills: vec![skill],
agents: vec![agent],
hooks,
native,
}],
})
}