Refactor runtime around isolated bot instances

This commit is contained in:
zenord
2026-08-16 23:45:09 +08:00
parent 914579a94c
commit f9fb4ef776
51 changed files with 2355 additions and 1459 deletions
+27 -132
View File
@@ -2,31 +2,31 @@
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GORI_AGENT_HOME="${GORI_AGENT_HOME:-$ROOT_DIR}"
STATE_DIR="$GORI_AGENT_HOME/state"
LOG_DIR="$GORI_AGENT_HOME/logs"
CONFIG_FILE="${GORI_GATEWAY_CONFIG:-$ROOT_DIR/config.json}"
PID_FILE="$STATE_DIR/gori-agent.pid"
LOG_FILE="$LOG_DIR/gori-agent.log"
usage() {
cat <<EOF
Usage: $0 <command> [--config path]
cat <<'EOF'
Usage: gori-agent <command> [arguments]
Commands:
setup Run interactive setup
start Start gateway in foreground
start-daemon Start gateway in background
stop Stop background gateway
restart Restart background gateway
status Show gateway status
doctor Check configuration, roles, and ACP backends
logs Follow gateway log
discover-backends List detected ACP backends
discover-agents Deprecated alias for discover-backends
Instance commands:
instance init <bot-id>
instance start <bot-id>
instance stop <bot-id>
instance restart <bot-id>
instance status <bot-id>
instance logs <bot-id>
instance doctor <bot-id>
instance list
Environment:
GORI_GATEWAY_CONFIG Config path, default: $CONFIG_FILE
Debug commands:
setup [--config path]
discover-backends [--json]
start --config path
status --config path
doctor --config path
print feishu --config path
Instances live under ${GORI_AGENT_ROOT:-$HOME/.gori-agent}/instances/<bot-id>.
config.example.json is documentation only and is never used as runtime config.
EOF
}
@@ -37,122 +37,17 @@ ensure_build() {
fi
}
ensure_state() {
mkdir -p "$STATE_DIR" "$LOG_DIR"
}
is_running() {
[[ -f "$PID_FILE" ]] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null
}
start_daemon() {
ensure_build
ensure_state
if is_running; then
echo "gori-agent already running, pid $(cat "$PID_FILE")"
exit 0
fi
nohup node "$ROOT_DIR/dist/cli.js" start --config "$CONFIG_FILE" >>"$LOG_FILE" 2>&1 &
echo $! >"$PID_FILE"
echo "gori-agent started, pid $(cat "$PID_FILE")"
echo "log: $LOG_FILE"
}
stop_daemon() {
if ! is_running; then
rm -f "$PID_FILE"
echo "gori-agent is not running"
return 0
fi
local pid
pid="$(cat "$PID_FILE")"
kill "$pid"
local waited=0
while kill -0 "$pid" 2>/dev/null && [[ $waited -lt 100 ]]; do
sleep 0.1
waited=$((waited + 1))
done
if kill -0 "$pid" 2>/dev/null; then
echo "gori-agent did not stop gracefully within 10 seconds, pid $pid" >&2
return 1
fi
rm -f "$PID_FILE"
echo "gori-agent stopped, pid $pid"
}
if [[ $# -lt 1 ]]; then
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
COMMAND="$1"
shift
while [[ $# -gt 0 ]]; do
case "$1" in
--config)
CONFIG_FILE="$2"
shift 2
;;
--help|-h)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
case "$COMMAND" in
setup)
ensure_build
exec node "$ROOT_DIR/dist/cli.js" setup --config "$CONFIG_FILE"
;;
start)
ensure_build
exec node "$ROOT_DIR/dist/cli.js" start --config "$CONFIG_FILE"
;;
start-daemon)
start_daemon
;;
stop)
stop_daemon
;;
restart)
stop_daemon
start_daemon
;;
status)
ensure_build
if is_running; then
echo "gori-agent process: running, pid $(cat "$PID_FILE")"
else
echo "gori-agent process: not running"
fi
exec node "$ROOT_DIR/dist/cli.js" status --config "$CONFIG_FILE"
;;
doctor)
ensure_build
exec node "$ROOT_DIR/dist/cli.js" doctor --config "$CONFIG_FILE"
;;
discover-backends|discover-agents)
ensure_build
exec node "$ROOT_DIR/dist/cli.js" "$COMMAND" --config "$CONFIG_FILE"
;;
logs)
ensure_state
touch "$LOG_FILE"
exec tail -f "$LOG_FILE"
;;
case "$1" in
--help|-h|help)
usage
;;
*)
echo "Unknown command: $COMMAND" >&2
usage >&2
exit 1
exit 0
;;
esac
ensure_build
exec node "$ROOT_DIR/dist/cli.js" "$@"