feat: export loadAgents and backward-compat agentsJson from flake
This commit is contained in:
0
.sisyphus/evidence/task-6-bridge-diff.txt
Normal file
0
.sisyphus/evidence/task-6-bridge-diff.txt
Normal file
1
.sisyphus/evidence/task-6-loadagents.json
Normal file
1
.sisyphus/evidence/task-6-loadagents.json
Normal file
File diff suppressed because one or more lines are too long
160
flake.nix
160
flake.nix
@@ -3,13 +3,14 @@
|
||||
|
||||
inputs = {nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";};
|
||||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
outputs = {
|
||||
self,
|
||||
nixpkgs,
|
||||
}: let
|
||||
supportedSystems = ["x86_64-linux" "aarch64-linux" "aarch64-darwin"];
|
||||
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||
inherit (nixpkgs) lib;
|
||||
in {
|
||||
|
||||
# ── Skill composition library ──────────────────────────────────
|
||||
#
|
||||
# Merges custom skills with external skills.sh sources into a
|
||||
@@ -49,15 +50,13 @@
|
||||
# Custom skills always take priority over external ones.
|
||||
# Among external sources, earlier entries in the list take priority.
|
||||
|
||||
lib.mkOpencodeSkills =
|
||||
{ pkgs
|
||||
, customSkills ? null
|
||||
, externalSkills ? []
|
||||
}:
|
||||
let
|
||||
lib.mkOpencodeSkills = {
|
||||
pkgs,
|
||||
customSkills ? null,
|
||||
externalSkills ? [],
|
||||
}: let
|
||||
# Resolve a single external source into a list of { name, path } entries.
|
||||
resolveExternal = entry:
|
||||
let
|
||||
resolveExternal = entry: let
|
||||
skillsRoot = "${entry.src}/${entry.skillsDir or "skills"}";
|
||||
# List skill subdirectories (each must contain SKILL.md).
|
||||
allSkillDirs = lib.pipe (builtins.readDir skillsRoot) [
|
||||
@@ -69,7 +68,11 @@
|
||||
then builtins.filter (name: builtins.elem name entry.selectSkills) allSkillDirs
|
||||
else allSkillDirs;
|
||||
in
|
||||
map (name: { inherit name; path = "${skillsRoot}/${name}"; }) selected;
|
||||
map (name: {
|
||||
inherit name;
|
||||
path = "${skillsRoot}/${name}";
|
||||
})
|
||||
selected;
|
||||
|
||||
# Collect all external skills, flattened.
|
||||
allExternal = lib.concatMap resolveExternal externalSkills;
|
||||
@@ -82,46 +85,149 @@
|
||||
|
||||
# Filter out external skills that collide with custom ones.
|
||||
# Among externals, keep first occurrence (earlier sources win).
|
||||
filterExternals = externals:
|
||||
let
|
||||
filterExternals = externals: let
|
||||
go = acc: remaining:
|
||||
if remaining == []
|
||||
then acc.result
|
||||
else
|
||||
let
|
||||
else let
|
||||
head = builtins.head remaining;
|
||||
tail = builtins.tail remaining;
|
||||
isDuplicate = builtins.elem head.name acc.seen;
|
||||
in
|
||||
if isDuplicate
|
||||
then go acc tail
|
||||
else go {
|
||||
else
|
||||
go {
|
||||
seen = acc.seen ++ [head.name];
|
||||
result = acc.result ++ [head];
|
||||
} tail;
|
||||
}
|
||||
tail;
|
||||
in
|
||||
go { seen = customSkillNames; result = []; } externals;
|
||||
go {
|
||||
seen = customSkillNames;
|
||||
result = [];
|
||||
}
|
||||
externals;
|
||||
|
||||
filteredExternal = filterExternals allExternal;
|
||||
|
||||
# Build a linkFarm entry for each external skill.
|
||||
externalLinks = map (skill: {
|
||||
externalLinks =
|
||||
map (skill: {
|
||||
name = skill.name;
|
||||
path = skill.path;
|
||||
}) filteredExternal;
|
||||
})
|
||||
filteredExternal;
|
||||
|
||||
# Build a linkFarm entry for each custom skill.
|
||||
customLinks =
|
||||
if customSkills != null
|
||||
then map (name: {
|
||||
then
|
||||
map (name: {
|
||||
inherit name;
|
||||
path = "${customSkills}/${name}";
|
||||
}) customSkillNames
|
||||
})
|
||||
customSkillNames
|
||||
else [];
|
||||
|
||||
in
|
||||
pkgs.linkFarm "opencode-skills" (customLinks ++ externalLinks);
|
||||
|
||||
# ── Agent loader ───────────────────────────────────────────────
|
||||
#
|
||||
# Reads all canonical agents/*/agent.toml + agents/*/system-prompt.md
|
||||
# files and returns an attrset keyed by agent slug.
|
||||
#
|
||||
# Each value has all fields from agent.toml plus:
|
||||
# systemPrompt — full content of system-prompt.md
|
||||
#
|
||||
# Usage:
|
||||
# inputs.agents.lib.loadAgents.chiron.description
|
||||
# inputs.agents.lib.loadAgents.chiron.systemPrompt
|
||||
|
||||
lib.loadAgents = let
|
||||
agentDirs = builtins.attrNames (
|
||||
lib.filterAttrs (_: t: t == "directory") (builtins.readDir ./agents)
|
||||
);
|
||||
isAgentDir = name: builtins.pathExists ./agents/${name}/agent.toml;
|
||||
loadAgent = name:
|
||||
(builtins.fromTOML (builtins.readFile ./agents/${name}/agent.toml))
|
||||
// {systemPrompt = builtins.readFile ./agents/${name}/system-prompt.md;};
|
||||
in
|
||||
builtins.listToAttrs (
|
||||
map (name: {
|
||||
inherit name;
|
||||
value = loadAgent name;
|
||||
})
|
||||
(builtins.filter isAgentDir agentDirs)
|
||||
);
|
||||
|
||||
# ── Backward-compat agents.json bridge ────────────────────────
|
||||
#
|
||||
# Produces an attrset semantically equivalent to agents/agents.json,
|
||||
# keyed by display name (e.g. "Chiron (Assistant)").
|
||||
#
|
||||
# Suitable for embedding into opencode config.json via home-manager:
|
||||
# programs.opencode.settings.agent = inputs.agents.lib.agentsJson;
|
||||
#
|
||||
# Shape per agent:
|
||||
# description — agent purpose string
|
||||
# mode — "primary" | "subagent"
|
||||
# model — LLM model ID (fixed: "zai-coding-plan/glm-5")
|
||||
# permission — reconstructed permission object
|
||||
# prompt — "{file:./prompts/<slug>.txt}"
|
||||
|
||||
lib.agentsJson = let
|
||||
model = "zai-coding-plan/glm-5";
|
||||
|
||||
# Convert a single permission section from canonical TOML two-level
|
||||
# (intent + rules[]) into the JSON nested object shape.
|
||||
# intent-only → simple string
|
||||
# intent+rules → { "*": intent, pattern: action, ... }
|
||||
renderPermSection = section:
|
||||
if !(section ? rules) || section.rules == []
|
||||
then section.intent
|
||||
else let
|
||||
# Parse "pattern:action" — split on first colon only.
|
||||
parseRule = ruleStr: let
|
||||
colonIdx = lib.strings.stringLength (
|
||||
builtins.head (lib.strings.splitString ":" ruleStr)
|
||||
);
|
||||
pattern = builtins.substring 0 colonIdx ruleStr;
|
||||
action = builtins.substring (colonIdx + 1) (lib.strings.stringLength ruleStr) ruleStr;
|
||||
in {
|
||||
name = pattern;
|
||||
value = action;
|
||||
};
|
||||
ruleAttrs = builtins.listToAttrs (map parseRule section.rules);
|
||||
in
|
||||
{"*" = section.intent;} // ruleAttrs;
|
||||
|
||||
# Convert canonical permissions attrset to JSON permission object.
|
||||
renderPermissions = perms:
|
||||
builtins.mapAttrs (_: renderPermSection) perms;
|
||||
|
||||
# Build one agent entry in the agentsJson shape.
|
||||
renderAgent = slug: agent: {
|
||||
description = agent.description + ".";
|
||||
mode = agent.mode;
|
||||
model = model;
|
||||
permission = renderPermissions agent.permissions;
|
||||
prompt = "{file:./prompts/${slug}.txt}";
|
||||
};
|
||||
|
||||
agents = self.lib.loadAgents;
|
||||
in
|
||||
builtins.listToAttrs (
|
||||
map
|
||||
(slug: let
|
||||
agent = builtins.getAttr slug agents;
|
||||
in {
|
||||
name = agent.display_name;
|
||||
value = renderAgent slug agent;
|
||||
})
|
||||
(builtins.attrNames agents)
|
||||
);
|
||||
|
||||
# ── Composable runtime ─────────────────────────────────────────
|
||||
#
|
||||
# Runtime dependencies for skill scripts (Python packages, system
|
||||
@@ -133,8 +239,7 @@
|
||||
# packages = [ inputs.agents.packages.${system}.skills-runtime ];
|
||||
# };
|
||||
|
||||
packages = forAllSystems (system:
|
||||
let
|
||||
packages = forAllSystems (system: let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
|
||||
pythonEnv = pkgs.python3.withPackages (ps:
|
||||
@@ -170,8 +275,7 @@
|
||||
|
||||
# ── Dev shell ──────────────────────────────────────────────────
|
||||
|
||||
devShells = forAllSystems (system:
|
||||
let
|
||||
devShells = forAllSystems (system: let
|
||||
pkgs = nixpkgs.legacyPackages.${system};
|
||||
in {
|
||||
default = pkgs.mkShell {
|
||||
|
||||
Reference in New Issue
Block a user