zot/examples/rpc/shell/prompt.sh
patriceckhart a670442c9e feat: zotcore SDK + zot rpc subprocess protocol
two new ways to embed the zot agent runtime in third-party apps:

1. pkg/zotcore - public Go SDK
   - Runtime type: New(Config), Prompt(ctx,text,imgs)->chan Event,
     Cancel, Compact, SetModel, State, Messages, Cost, ListModels,
     Close. Concurrent-safe; one prompt at a time per Runtime,
     ErrBusy if you try to overlap. Spawn multiple Runtimes for
     multiple projects.
   - Public types mirror the JSON-RPC wire schema 1:1 so consumers
     can share parsing code with the out-of-process clients.
   - Internal core/agent/provider stay internal; SDK is a thin
     facade that exposes only what's stable.

2. zot rpc subcommand - newline-delimited JSON on stdin/stdout
   - 'zot rpc' (or 'zot --rpc') turns the agent runtime into a
     subprocess that any language can drive via pipes.
   - Commands: hello, prompt, abort, compact, get_state,
     get_messages, clear, set_model, get_models, ping. Each
     optionally carries an id; the matching response echoes it.
   - Stream notifications: turn_start, user_message,
     assistant_start, text_delta, tool_call, tool_progress,
     tool_result, assistant_message, usage, turn_end, done,
     error, compact_done. Same shape as the existing --json mode
     events (modes.EventToJSON / ContentToJSON were exported
     for reuse).
   - Auth: optional ZOTCORE_RPC_TOKEN env var; first command
     must be hello {token: ...} when set. Without the env var
     the spawning process is implicitly trusted.
   - Concurrency: one prompt or compact at a time per process,
     enforced by a turnMu mutex. abort fires immediately
     regardless. Stdin close exits the process.

3. docs/rpc.md - full schema reference
4. examples/rpc/{python,node,shell,go} - reference clients
5. examples/sdk - in-process Go embedding example
6. README updated with a new modes entry and an embedding section
2026-04-19 12:26:48 +02:00

51 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# Minimal shell client for the `zot rpc` JSON protocol.
#
# Usage:
# ./prompt.sh "fix the failing test"
#
# Sends one prompt, streams assistant text deltas to stdout, exits
# when the turn finishes. Requires `jq`.
set -euo pipefail
if [ $# -lt 1 ]; then
echo "usage: $0 <prompt>" >&2
exit 2
fi
prompt="$*"
# Build the prompt command frame with jq so quotes are escaped properly.
cmd=$(jq -nc --arg msg "$prompt" '{id:"1",type:"prompt",message:$msg}')
# Pipe the command into zot rpc; pipe its stdout through jq to react
# to events. The trailing `cat` keeps the input pipe open until the
# subprocess exits on its own (after `done`).
{
if [ -n "${ZOTCORE_RPC_TOKEN:-}" ]; then
jq -nc --arg t "$ZOTCORE_RPC_TOKEN" '{id:"0",type:"hello",token:$t}'
fi
echo "$cmd"
# Block here so stdin stays open until zot exits.
cat
} | zot rpc | while IFS= read -r line; do
type=$(echo "$line" | jq -r '.type // empty')
case "$type" in
text_delta)
echo "$line" | jq -rj '.delta // ""'
;;
tool_call)
name=$(echo "$line" | jq -r '.name // ""')
echo >&2 ""
echo >&2 "[tool] $name"
;;
error)
echo >&2 "[error] $(echo "$line" | jq -r '.message // ""')"
exit 1
;;
done)
echo
exit 0
;;
esac
done