forked from garrytan/gstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgstack-brain-consumer
More file actions
executable file
·201 lines (191 loc) · 5.77 KB
/
Copy pathgstack-brain-consumer
File metadata and controls
executable file
·201 lines (191 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
# gstack-brain-consumer — manage the consumer (reader) registry.
#
# DEPRECATED in v1.17.0.0. This binary targets a gbrain HTTP /ingest-repo
# endpoint that never shipped on the gbrain side. Live federation now uses
# `gbrain sources` directly via bin/gstack-gbrain-source-wireup. This file
# stays for one cycle to avoid breaking external scripts; removal in v1.18.0.0.
#
# Consumer = a reader that ingests the gstack-brain git repo as a source of
# session memory. v1 primary consumer is GBrain; later versions can register
# Codex, OpenClaw, or third-party readers.
#
# NOTE ON NAMING: internally this helper uses "consumer" (correct data-model
# term). User-facing copy and the alias `gstack-brain-reader` use "reader"
# (matches user mental model: "what's reading my brain?").
#
# Usage:
# gstack-brain-consumer add <name> --ingest-url <url> --token <token>
# gstack-brain-consumer list
# gstack-brain-consumer remove <name>
# gstack-brain-consumer test <name>
#
# Env:
# GSTACK_HOME — override ~/.gstack
set -euo pipefail
GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
CONSUMERS_FILE="$GSTACK_HOME/consumers.json"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
CONFIG_BIN="$SCRIPT_DIR/gstack-config"
ensure_file() {
mkdir -p "$GSTACK_HOME"
if [ ! -f "$CONSUMERS_FILE" ]; then
echo '{"consumers": []}' > "$CONSUMERS_FILE"
fi
}
get_remote_url() {
git -C "$GSTACK_HOME" remote get-url origin 2>/dev/null || echo ""
}
sub_add() {
local name="" url="" token=""
local positional=""
while [ $# -gt 0 ]; do
case "$1" in
--ingest-url) url="$2"; shift 2 ;;
--token) token="$2"; shift 2 ;;
--) shift; break ;;
-*) echo "Unknown flag: $1" >&2; exit 1 ;;
*) positional="$1"; shift ;;
esac
done
name="$positional"
if [ -z "$name" ] || [ -z "$url" ]; then
echo "Usage: gstack-brain-consumer add <name> --ingest-url <url> [--token <token>]" >&2
exit 1
fi
ensure_file
# Upsert in consumers.json, store token in gstack-config under `<name>_token`.
python3 - "$CONSUMERS_FILE" "$name" "$url" <<'PYEOF'
import sys, json
path, name, url = sys.argv[1:4]
try:
with open(path) as f:
data = json.load(f)
except Exception:
data = {"consumers": []}
entry = {"name": name, "ingest_url": url, "status": "unknown", "token_ref": f"{name}_token"}
cs = data.setdefault("consumers", [])
for i, c in enumerate(cs):
if c.get("name") == name:
cs[i] = entry
break
else:
cs.append(entry)
with open(path, "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
print(f"registered consumer: {name}")
PYEOF
if [ -n "$token" ]; then
"$CONFIG_BIN" set "${name}_token" "$token"
echo "token stored: gstack-config get ${name}_token to retrieve"
fi
# Attempt registration with remote (HTTP POST).
sub_test "$name"
}
sub_list() {
if [ ! -f "$CONSUMERS_FILE" ]; then
echo '{"consumers": []}'
return 0
fi
cat "$CONSUMERS_FILE"
}
sub_remove() {
local name="${1:-}"
if [ -z "$name" ]; then
echo "Usage: gstack-brain-consumer remove <name>" >&2
exit 1
fi
ensure_file
python3 - "$CONSUMERS_FILE" "$name" <<'PYEOF'
import sys, json
path, name = sys.argv[1:3]
try:
with open(path) as f:
data = json.load(f)
except Exception:
data = {"consumers": []}
before = len(data.get("consumers", []))
data["consumers"] = [c for c in data.get("consumers", []) if c.get("name") != name]
after = len(data["consumers"])
with open(path, "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
print(f"removed: {before - after} entry(ies)")
PYEOF
}
sub_test() {
local name="${1:-}"
if [ -z "$name" ]; then
echo "Usage: gstack-brain-consumer test <name>" >&2
exit 1
fi
ensure_file
# Look up the consumer by name.
local info
info=$(python3 - "$CONSUMERS_FILE" "$name" <<'PYEOF'
import sys, json
path, name = sys.argv[1:3]
try:
with open(path) as f:
data = json.load(f)
except Exception:
data = {"consumers": []}
for c in data.get("consumers", []):
if c.get("name") == name:
print(c.get("ingest_url", ""))
sys.exit(0)
sys.exit(1)
PYEOF
) || { echo "No such consumer: $name" >&2; exit 1; }
local url="$info"
local token
token=$("$CONFIG_BIN" get "${name}_token" 2>/dev/null || echo "")
if [ -z "$url" ] || [ -z "$token" ]; then
echo "consumer '$name': url or token missing; cannot test"
return 0
fi
local repo_url
repo_url=$(get_remote_url)
echo "Testing $name at ${url%/}/ingest-repo ..."
local resp
resp=$(curl -sS -X POST "${url%/}/ingest-repo" \
-H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
--data "{\"repo_url\":\"$repo_url\"}" \
-w "\n%{http_code}" 2>&1 || echo -e "\ncurl-error")
local code
code=$(echo "$resp" | tail -1)
if [ "$code" = "200" ] || [ "$code" = "201" ] || [ "$code" = "204" ]; then
echo "ok (HTTP $code)"
# Update status in consumers.json.
python3 - "$CONSUMERS_FILE" "$name" "ok" <<'PYEOF'
import sys, json
path, name, status = sys.argv[1:4]
with open(path) as f: data = json.load(f)
for c in data.get("consumers", []):
if c.get("name") == name:
c["status"] = status
with open(path, "w") as f: json.dump(data, f, indent=2); f.write("\n")
PYEOF
else
echo "failed (HTTP $code)"
python3 - "$CONSUMERS_FILE" "$name" "error" <<'PYEOF'
import sys, json
path, name, status = sys.argv[1:4]
with open(path) as f: data = json.load(f)
for c in data.get("consumers", []):
if c.get("name") == name:
c["status"] = status
with open(path, "w") as f: json.dump(data, f, indent=2); f.write("\n")
PYEOF
fi
}
case "${1:-}" in
add) shift; sub_add "$@" ;;
list) sub_list ;;
remove) shift; sub_remove "$@" ;;
test) shift; sub_test "$@" ;;
--help|-h|"") sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' ;;
*) echo "Unknown subcommand: $1" >&2; exit 1 ;;
esac