ollie/tools/subagent_generate

61 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# description: Generate a JIT subagent config. No args prints the JSON template. With JSON on stdin, creates the config and prints the agent name.
# Args: none
# Stdin: JSON with structured fields (role, roster, shared_task, protocol, constraints, result_path) or legacy "prompt"
# Output: template (no stdin) or agent name
set -euo pipefail
OLLIE="${OLLIE:-$HOME/mnt/ollie}"
die() { printf 'agent_generate: %s\n' "$*" >&2; exit 1; }
if [ -t 0 ] && [ $# -eq 0 ]; then
printf '{\n "name": "",\n "role": "",\n "roster": "",\n "shared_task": "",\n "protocol": "",\n "constraints": [],\n "prompt": ""\n}\n'
exit 0
fi
cfg=$(cat) || die "failed to read stdin"
[ -z "$cfg" ] && die "empty input"
name=$(printf '%s' "$cfg" | python3 -c "
import json, sys, secrets
raw = json.load(sys.stdin)
name = raw.get('name', '') or 'jit-' + secrets.token_hex(8)
role = raw.get('role', '')
roster = raw.get('roster', '')
shared_task = raw.get('shared_task', '')
protocol = raw.get('protocol', '')
constraints = raw.get('constraints', [])
legacy = raw.get('prompt', '')
if not role and not legacy:
print('error: role or prompt required', file=sys.stderr)
sys.exit(1)
if role:
parts = ['## Role', role]
if roster: parts += ['## Roster', roster]
if shared_task: parts += ['## Shared Task', shared_task]
if protocol: parts += ['## Coordination Protocol', protocol]
if constraints: parts += ['## Constraints', '\n'.join('- ' + c for c in constraints)]
identity = '\n\n'.join(parts)
else:
identity = legacy
base = sys.argv[1]
with open(base) as f:
cfg = json.load(f)
cfg['prompt'] = ['\$OLLIE/x/prime SYSTEM_PROMPT', 'echo ' + json.dumps(identity)]
out = sys.argv[2] + '/' + name + '.json'
with open(out, 'w') as f:
json.dump(cfg, f, indent=2)
print(name)
" "$OLLIE/a/default.json" "$OLLIE/a") || die "failed to generate agent config"
printf '%s\n' "$name"