OpenClaw-Setup/openclaw-setup-copilot/scripts/install_model_budget_guard_launchd.sh

79 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
if ! command -v jq >/dev/null 2>&1; then
echo "[install-model-budget-guard] jq is required" >&2
exit 1
fi
LABEL="ai.openclaw.model-budget-guard"
PLIST_PATH="$HOME/Library/LaunchAgents/$LABEL.plist"
STAGE_DIR="${MODEL_GUARD_STAGE_DIR:-$HOME/Library/Application Support/openclaw-copilot-guard}"
STAGE_SCRIPTS_DIR="$STAGE_DIR/scripts"
STAGE_CONFIG="$STAGE_DIR/model-budget-guard.config.json"
MODEL_GUARD_CONFIG="${MODEL_GUARD_CONFIG:-$STAGE_CONFIG}"
INTERVAL_SECONDS="${INTERVAL_SECONDS:-120}"
mkdir -p "$STAGE_SCRIPTS_DIR"
cp "$ROOT_DIR/scripts/model_budget_guard.sh" "$STAGE_SCRIPTS_DIR/model_budget_guard.sh"
jq \
--arg state "$STAGE_DIR/model-budget-guard-state.json" \
'.stateFile=$state' \
"$ROOT_DIR/config/model-budget-guard.config.json" > "$STAGE_CONFIG"
chmod +x "$STAGE_SCRIPTS_DIR/model_budget_guard.sh"
cat > "$PLIST_PATH" <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>$LABEL</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>$STAGE_SCRIPTS_DIR/model_budget_guard.sh</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
<key>MODEL_GUARD_CONFIG</key>
<string>$MODEL_GUARD_CONFIG</string>
</dict>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>$INTERVAL_SECONDS</integer>
<key>StandardOutPath</key>
<string>/tmp/openclaw-model-budget-guard.log</string>
<key>StandardErrorPath</key>
<string>/tmp/openclaw-model-budget-guard.err.log</string>
</dict>
</plist>
PLIST
launchctl bootout "gui/$(id -u)/$LABEL" 2>/dev/null || true
launchctl bootstrap "gui/$(id -u)" "$PLIST_PATH"
launchctl kickstart -k "gui/$(id -u)/$LABEL"
cat <<MSG
Installed LaunchAgent:
$PLIST_PATH
Staged runtime files:
$STAGE_DIR
Check status:
launchctl print gui/$(id -u)/$LABEL
tail -f /tmp/openclaw-model-budget-guard.log /tmp/openclaw-model-budget-guard.err.log
MSG