|
- #!/usr/bin/env bash
- # Use this repo's own knowledge base (the Agent Memory API running in docker-compose) while developing it.
- # Settings and keys come from .amk.env at the repo root (git-ignored; created when the tenant was provisioned).
- #
- # bash scripts/kb.sh context "<task>" briefing for a task (constraints, lessons, references)
- # bash scripts/kb.sh recall "<query>" [unverified] memories matching a query
- # bash scripts/kb.sh search "<query>" passages from ingested documents
- # bash scripts/kb.sh ingest <file.md|.html> [title] ingest or re-ingest a Markdown or HTML file (source kb://repo/<file>)
- # bash scripts/kb.sh learn <type> "<title>" "<content>" propose knowledge (lesson, decision, constraint, ...)
- # bash scripts/kb.sh candidates open proposals
- # bash scripts/kb.sh review <candidate_id> accept|reject [notes] owner only: accept (verified) or reject
- # bash scripts/kb.sh feedback <query_id> <result_id> useful|not_useful|incorrect|harmful
- # bash scripts/kb.sh session "<objective>" start a session; prints it (use its id below)
- # bash scripts/kb.sh event <session_id> success|failure|partial|unknown "<task>" "<problem>" "<action>" "<result>"
- # bash scripts/kb.sh consolidate [propose] group similar experiences; "propose" files draft candidates
- set -eu
- ROOT=$(cd "$(dirname "$0")/.." && pwd)
- [ -f "$ROOT/.amk.env" ] || { echo "missing $ROOT/.amk.env (provision a tenant first, see AGENTS.md)" >&2; exit 2; }
- # shellcheck disable=SC1091
- . <(tr -d '\r' < "$ROOT/.amk.env")
- py() { python -I -c "import json,sys;a=sys.argv[1:];$1" "${@:2}"; }
- call() { local key=$1 m=$2 p=$3; shift 3; curl -s -X "$m" -H "Authorization: Bearer $key" -H 'Content-Type: application/json' "$@" "$AMK_URL$p"; echo; }
- cmd=${1:-}; shift || true
- case "$cmd" in
- context)
- call "$AMK_KEY" POST /context --data-binary "$(py 'print(json.dumps({"task":a[0],"namespace_ids":[a[1]]}))' "$1" "$AMK_NAMESPACE")" ;;
- recall)
- vf=verified_only; [ "${2:-}" = unverified ] && vf=include_unverified
- call "$AMK_KEY" POST /recall --data-binary "$(py 'print(json.dumps({"query":a[0],"namespace_ids":[a[1]],"verification_filter":a[2]}))' "$1" "$AMK_NAMESPACE" "$vf")" ;;
- search)
- call "$AMK_KEY" POST /search --data-binary "$(py 'print(json.dumps({"query":a[0],"namespace_ids":[a[1]]}))' "$1" "$AMK_NAMESPACE")" ;;
- ingest)
- f=$1; rel=${f#"$ROOT"/}
- # large files: the body goes through stdin (the Windows command line is too short for it)
- fmt=markdown; case "$f" in *.html|*.htm) fmt=html ;; esac
- py 'sys.stdout.buffer.write(json.dumps({"namespace_id":a[0],"title":a[1],"source_uri":"kb://repo/"+a[2],"format":a[4],"content":open(a[3],encoding="utf-8").read()}).encode())' "$AMK_NAMESPACE" "${2:-$rel}" "$rel" "$f" "$fmt" | call "$AMK_KEY" POST /ingestion/jobs --data-binary @- ;;
- learn)
- call "$AMK_KEY" POST /learn --data-binary "$(py 'print(json.dumps({"namespace_id":a[0],"type":a[1],"title":a[2],"content":a[3]}))' "$AMK_NAMESPACE" "$1" "$2" "$3")" ;;
- candidates)
- call "$AMK_KEY" GET "/candidates?state=proposed" ;;
- review)
- call "$AMK_OWNER_KEY" POST "/candidates/$1/review" --data-binary "$(py 'print(json.dumps({"decision":a[0],"notes":a[1],"verify":a[0]=="accept"}))' "$2" "${3:-}")" ;;
- feedback)
- call "$AMK_KEY" POST /feedback --data-binary "$(py 'print(json.dumps({"query_id":a[0],"result_id":a[1],"usefulness":a[2]}))' "$1" "$2" "$3")" ;;
- session)
- call "$AMK_KEY" POST /sessions --data-binary "$(py 'print(json.dumps({"namespace_id":a[0],"objective":a[1]}))' "$AMK_NAMESPACE" "$1")" ;;
- event)
- call "$AMK_KEY" POST "/sessions/$1/events" --data-binary "$(py 'print(json.dumps({"outcome":a[0],"task":a[1],"problem":a[2],"action":a[3],"result":a[4]}))' "$2" "$3" "${4:-}" "${5:-}" "${6:-}")" ;;
- consolidate)
- call "$AMK_KEY" POST /consolidations --data-binary "$(py 'print(json.dumps({"namespace_id":a[0],"propose":a[1]=="propose"}))' "$AMK_NAMESPACE" "${1:-}")" ;;
- *)
- sed -n '2,15p' "$0"; exit 1 ;;
- esac
|