ollie/tools/agent_generate

53 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# description: Generate a JIT agent config from a base agent with custom identity text. Returns the agent name.
# Args: [-base AGENT] [-name NAME] <identity text...>
set -euo pipefail
OLLIE="${OLLIE:-$HOME/mnt/ollie}"
die() { printf 'agent_generate: %s\n' "$*" >&2; exit 1; }
base=default
name=
while [ $# -gt 0 ]; do
case $1 in
-base) base=$2; shift ;;
-name) name=$2; shift ;;
--) shift; break ;;
-*) die "unknown flag: $1" ;;
*) break ;;
esac
shift
done
[ $# -eq 0 ] && die "identity text required"
identity="$*"
name=${name:-jit-$(head -c 8 /dev/urandom | od -An -tx1 | tr -d ' \n')}
base_cfg="$OLLIE/a/${base}.json"
[ -f "$base_cfg" ] || die "base agent not found: $base"
# Read base config, insert identity as literal string after SYSTEM_PROMPT entry
python3 -c "
import json, sys
with open('$base_cfg') as f:
cfg = json.load(f)
prompt = cfg.get('prompt', [])
if not isinstance(prompt, list):
print('error: base agent uses string prompt, not array', file=sys.stderr)
sys.exit(1)
# Insert identity after the first entry (SYSTEM_PROMPT)
identity = sys.stdin.read()
prompt.insert(1, identity)
cfg['prompt'] = prompt
json.dump(cfg, sys.stdout, indent=2)
" <<< "$identity" > "$OLLIE/a/${name}.json" || die "failed to write agent config"
printf '%s\n' "$name"