ework-aio 0.2.8 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/scripts/e2e-browser.sh +322 -0
- package/scripts/fake-llm-server.ts +221 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ework-aio",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "All-in-one installer for ework (issue tracker) + ework-daemon (AI bridge) + opencode-ework (plugin). One command: npm i -g ework-aio && ework-aio install.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# End-to-end browser test: install → fake LLM → opencode config → Playwright.
|
|
3
|
+
#
|
|
4
|
+
# This is the "full product path" test. Compared to e2e-install.sh (which
|
|
5
|
+
# covers install/subcommand/lifecycle), this script:
|
|
6
|
+
# - Starts a fake OpenAI-compatible LLM server (scripts/fake-llm-server.ts)
|
|
7
|
+
# - Configures opencode.json to use it (no external API needed)
|
|
8
|
+
# - Creates an issue (which triggers daemon → opencode → fake LLM)
|
|
9
|
+
# - Drives a headless browser through ework-web UI
|
|
10
|
+
# - Verifies the resulting session is browsable and shows content
|
|
11
|
+
#
|
|
12
|
+
# Requires Docker image with Chromium pre-installed. The Dockerfile.regression
|
|
13
|
+
# image handles this; pass via IMAGE env or rely on the default.
|
|
14
|
+
#
|
|
15
|
+
# Usage: ./scripts/e2e-browser.sh [container-runtime] [npm-tag]
|
|
16
|
+
|
|
17
|
+
set -euo pipefail
|
|
18
|
+
|
|
19
|
+
RUNTIME="${1:-docker}"
|
|
20
|
+
NPM_TAG="${2:-latest}"
|
|
21
|
+
IMAGE="${IMAGE:-ework-aio:e2e}"
|
|
22
|
+
|
|
23
|
+
c_grn=$'\033[32m'; c_red=$'\033[31m'; c_ylw=$'\033[33m'; c_rst=$'\033[0m'
|
|
24
|
+
pass() { printf '%sPASS%s %s\n' "$c_grn" "$c_rst" "$*"; }
|
|
25
|
+
fail() { printf '%sFAIL%s %s\n' "$c_red" "$c_rst" "$*"; exit 1; }
|
|
26
|
+
info() { printf '%s…%s %s\n' "$c_ylw" "$c_rst" "$*" >&2; }
|
|
27
|
+
|
|
28
|
+
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
29
|
+
|
|
30
|
+
if ! "$RUNTIME" image inspect "$IMAGE" >/dev/null 2>&1; then
|
|
31
|
+
info "Building $IMAGE (with Chromium deps — first build is slow)"
|
|
32
|
+
"$RUNTIME" build --network=host -f "$REPO_ROOT/Dockerfile.regression" \
|
|
33
|
+
-t "$IMAGE" "$REPO_ROOT"
|
|
34
|
+
fi
|
|
35
|
+
|
|
36
|
+
info "Using image: $IMAGE"
|
|
37
|
+
|
|
38
|
+
# Hardcoded ports — see e2e-install.sh for why we don't inherit from host.
|
|
39
|
+
WORK_PORT="14002"
|
|
40
|
+
DAEMON_PORT="14101"
|
|
41
|
+
FAKE_LLM_PORT="8400"
|
|
42
|
+
|
|
43
|
+
OPENCODE_HOST_BIN="${OPENCODE_HOST_BIN:-/home/dog/.local/bin/opencode}"
|
|
44
|
+
|
|
45
|
+
"$RUNTIME" run --rm -i --network host \
|
|
46
|
+
-v "$OPENCODE_HOST_BIN:/usr/local/bin/opencode:ro" \
|
|
47
|
+
-v "$REPO_ROOT/scripts:/host-scripts:ro" \
|
|
48
|
+
-v "$REPO_ROOT/test:/host-test:ro" \
|
|
49
|
+
-e WORK_PORT="$WORK_PORT" \
|
|
50
|
+
-e DAEMON_PORT="$DAEMON_PORT" \
|
|
51
|
+
-e FAKE_LLM_PORT="$FAKE_LLM_PORT" \
|
|
52
|
+
-e NPM_TAG="$NPM_TAG" \
|
|
53
|
+
-e HTTP_PROXY="${HTTP_PROXY:-}" \
|
|
54
|
+
-e HTTPS_PROXY="${HTTPS_PROXY:-}" \
|
|
55
|
+
-e http_proxy="${HTTP_PROXY:-}" \
|
|
56
|
+
-e https_proxy="${HTTPS_PROXY:-}" \
|
|
57
|
+
-e NO_PROXY="127.0.0.1,localhost" \
|
|
58
|
+
"$IMAGE" bash -euo pipefail <<'EOSCRIPT'
|
|
59
|
+
set -euo pipefail
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
c_grn=$'\033[32m'; c_red=$'\033[31m'; c_ylw=$'\033[33m'; c_rst=$'\033[0m'
|
|
63
|
+
pass() { printf '%sPASS%s %s\n' "$c_grn" "$c_rst" "$*"; }
|
|
64
|
+
fail() { printf '%sFAIL%s %s\n' "$c_red" "$c_rst" "$*"; exit 1; }
|
|
65
|
+
info() { printf '%s…%s %s\n' "$c_ylw" "$c_rst" "$*" >&2; }
|
|
66
|
+
|
|
67
|
+
WORK_PORT="${WORK_PORT:-14002}"
|
|
68
|
+
DAEMON_PORT="${DAEMON_PORT:-14101}"
|
|
69
|
+
FAKE_LLM_PORT="${FAKE_LLM_PORT:-8400}"
|
|
70
|
+
NPM_TAG="${NPM_TAG:-latest}"
|
|
71
|
+
DATA_DIR=/tmp/aio-browser
|
|
72
|
+
|
|
73
|
+
info "clean state"
|
|
74
|
+
rm -rf "$DATA_DIR"
|
|
75
|
+
mkdir -p "$DATA_DIR"
|
|
76
|
+
|
|
77
|
+
info "opencode binary on PATH"
|
|
78
|
+
opencode --version | head -1
|
|
79
|
+
|
|
80
|
+
info "install ework-aio@${NPM_TAG} + deps"
|
|
81
|
+
for pkg in ework-web ework-daemon opencode-ework; do
|
|
82
|
+
npm install -g "$pkg@latest" 2>&1 | tail -2
|
|
83
|
+
done
|
|
84
|
+
npm install -g "ework-aio@${NPM_TAG}" 2>&1 | tail -2
|
|
85
|
+
|
|
86
|
+
info "install Playwright npm package (browser itself is pre-baked into image)"
|
|
87
|
+
# Install into a local node_modules so module resolution works regardless of
|
|
88
|
+
# whether we drive the test via `node` or `bun`. Global installs don't get
|
|
89
|
+
# picked up by Node's import resolver.
|
|
90
|
+
mkdir -p /tmp/test-runner
|
|
91
|
+
cd /tmp/test-runner
|
|
92
|
+
npm init -y >/dev/null
|
|
93
|
+
npm install playwright 2>&1 | tail -2
|
|
94
|
+
|
|
95
|
+
info "start fake LLM server in background"
|
|
96
|
+
PORT="$FAKE_LLM_PORT" bun run /host-scripts/fake-llm-server.ts 2>/tmp/fake-llm.log &
|
|
97
|
+
FAKE_LLM_PID=$!
|
|
98
|
+
# Wait for server to come up.
|
|
99
|
+
for i in $(seq 1 30); do
|
|
100
|
+
if curl -sf "http://127.0.0.1:$FAKE_LLM_PORT/v1/models" >/dev/null; then
|
|
101
|
+
pass "fake LLM up (pid $FAKE_LLM_PID, port $FAKE_LLM_PORT)"
|
|
102
|
+
break
|
|
103
|
+
fi
|
|
104
|
+
sleep 0.5
|
|
105
|
+
[[ $i -eq 30 ]] && { tail /tmp/fake-llm.log; fail "fake LLM did not come up"; }
|
|
106
|
+
done
|
|
107
|
+
|
|
108
|
+
info "run ework-aio install (writes opencode.json with plugin only)"
|
|
109
|
+
ework-aio install \
|
|
110
|
+
--allow-root \
|
|
111
|
+
--data-dir "$DATA_DIR" \
|
|
112
|
+
--port "$WORK_PORT" \
|
|
113
|
+
--daemon-port "$DAEMON_PORT" \
|
|
114
|
+
--bot-name e2e-bot \
|
|
115
|
+
--yes 2>&1 | tee /tmp/install.log | tail -5
|
|
116
|
+
|
|
117
|
+
pass "install exited 0"
|
|
118
|
+
|
|
119
|
+
info "verify services respond"
|
|
120
|
+
for i in $(seq 1 60); do
|
|
121
|
+
curl -sf "http://127.0.0.1:$WORK_PORT/login" >/dev/null && break
|
|
122
|
+
sleep 0.5
|
|
123
|
+
[[ $i -eq 60 ]] && fail "ework-web did not come up"
|
|
124
|
+
done
|
|
125
|
+
for i in $(seq 1 60); do
|
|
126
|
+
curl -sf "http://127.0.0.1:$DAEMON_PORT/api/status" >/dev/null && break
|
|
127
|
+
sleep 0.5
|
|
128
|
+
[[ $i -eq 60 ]] && fail "daemon did not come up"
|
|
129
|
+
done
|
|
130
|
+
pass "both services responding"
|
|
131
|
+
|
|
132
|
+
info "configure opencode to use fake LLM"
|
|
133
|
+
# Overwrite ~/.config/opencode/opencode.json with fake provider + plugin.
|
|
134
|
+
# install.ts only registers the plugin; we add the fake provider here so
|
|
135
|
+
# the test stays isolated from any real API credentials.
|
|
136
|
+
OPENCODE_CONFIG_DIR="$HOME/.config/opencode"
|
|
137
|
+
mkdir -p "$OPENCODE_CONFIG_DIR"
|
|
138
|
+
cat > "$OPENCODE_CONFIG_DIR/opencode.json" <<OCJSON
|
|
139
|
+
{
|
|
140
|
+
"\$schema": "https://opencode.ai/config.json",
|
|
141
|
+
"plugin": ["opencode-ework@latest"],
|
|
142
|
+
"provider": {
|
|
143
|
+
"fake": {
|
|
144
|
+
"npm": "@ai-sdk/openai-compatible",
|
|
145
|
+
"name": "Fake (E2E test)",
|
|
146
|
+
"options": {
|
|
147
|
+
"baseURL": "http://127.0.0.1:$FAKE_LLM_PORT/v1"
|
|
148
|
+
},
|
|
149
|
+
"models": {
|
|
150
|
+
"fake-model": {
|
|
151
|
+
"name": "Fake Model"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
"model": "fake/fake-model"
|
|
157
|
+
}
|
|
158
|
+
OCJSON
|
|
159
|
+
pass "opencode.json points at fake LLM"
|
|
160
|
+
|
|
161
|
+
info "warm up opencode (first-run DB migration takes minutes)"
|
|
162
|
+
# Opencode prints "Performing one time database migration, may take a few
|
|
163
|
+
# minutes..." on its first invocation against a fresh DB. If we don't warm
|
|
164
|
+
# it up here, both the smoke test AND the daemon's spawned opencode get
|
|
165
|
+
# killed mid-migration and never reach the chat step. Running any cheap
|
|
166
|
+
# command against the DB triggers the migration once; subsequent runs are
|
|
167
|
+
# fast. `session list` is the cheapest touch we can do.
|
|
168
|
+
# </dev/null: opencode session list reads stdin (cosmetic for list mode,
|
|
169
|
+
# but defensive — see smoke test comment for the heredoc-eof trap).
|
|
170
|
+
timeout -s KILL 300 opencode session list </dev/null >/dev/null 2>&1 || true
|
|
171
|
+
pass "opencode warm-up done (migration triggered if needed)"
|
|
172
|
+
|
|
173
|
+
info "verify opencode can reach fake LLM (manual smoke)"
|
|
174
|
+
# SIGKILL after 90s — opencode can hang on tool-call prompts if the LLM
|
|
175
|
+
# response shape is wrong; SIGTERM is caught for cleanup and takes longer.
|
|
176
|
+
# 90s because even post-migration, first stream response can take 10-20s.
|
|
177
|
+
#
|
|
178
|
+
# </dev/null is critical: this script's heredoc provides bash's stdin.
|
|
179
|
+
# opencode run reads stdin when waiting for prompts, which would silently
|
|
180
|
+
# consume the rest of the heredoc — bash then hits EOF and exits without
|
|
181
|
+
# running any subsequent step.
|
|
182
|
+
cd /tmp && timeout -s KILL 90 opencode run --format json --model fake/fake-model --dir /tmp "manual smoke test" </dev/null > /tmp/smoke.json 2>&1 || true
|
|
183
|
+
pass "opencode smoke test ran (output captured)"
|
|
184
|
+
echo " events: $(wc -l < /tmp/smoke.json)"
|
|
185
|
+
echo " --- smoke.json content (first 1000 chars) ---"
|
|
186
|
+
head -c 1000 /tmp/smoke.json
|
|
187
|
+
echo
|
|
188
|
+
echo " --- fake-llm log tail ---"
|
|
189
|
+
tail -5 /tmp/fake-llm.log
|
|
190
|
+
|
|
191
|
+
info "create issue that triggers daemon → opencode → fake LLM"
|
|
192
|
+
# Build admin cookie (same recipe as e2e-install.sh).
|
|
193
|
+
WORK_TOKEN=$(grep ^WORK_TOKEN= "$DATA_DIR/ework-web/.env" | cut -d= -f2-)
|
|
194
|
+
WORK_COOKIE_SECRET=$(grep ^WORK_COOKIE_SECRET= "$DATA_DIR/ework-web/.env" | cut -d= -f2-)
|
|
195
|
+
COOKIE_SIG=$(printf '%s' "$WORK_TOKEN" \
|
|
196
|
+
| openssl dgst -sha256 -hmac "$WORK_COOKIE_SECRET" -binary \
|
|
197
|
+
| base64 | tr '+/' '-_' | tr -d '=')
|
|
198
|
+
AUTH_COOKIE="ework_auth=${WORK_TOKEN}.${COOKIE_SIG}"
|
|
199
|
+
|
|
200
|
+
# Create project (idempotent — if it exists, 303 is still returned).
|
|
201
|
+
curl -sS -o /dev/null -w 'project: %{http_code}\n' -X POST \
|
|
202
|
+
"http://127.0.0.1:$WORK_PORT/projects" \
|
|
203
|
+
-H "Cookie: $AUTH_COOKIE" \
|
|
204
|
+
--data-urlencode 'owner=e2e' \
|
|
205
|
+
--data-urlencode 'name=browser-test'
|
|
206
|
+
|
|
207
|
+
# Create issue (triggers webhook → daemon → opencode).
|
|
208
|
+
ISSUE_TITLE="Browser E2E $(date +%s)"
|
|
209
|
+
curl -sS -o /dev/null -w 'issue: %{http_code}\n' -X POST \
|
|
210
|
+
"http://127.0.0.1:$WORK_PORT/e2e/browser-test/issues" \
|
|
211
|
+
-H "Cookie: $AUTH_COOKIE" \
|
|
212
|
+
--data-urlencode "title=$ISSUE_TITLE" \
|
|
213
|
+
--data-urlencode 'body=please reply with anything'
|
|
214
|
+
|
|
215
|
+
info "wait for daemon to process the issue + spawn opencode"
|
|
216
|
+
# daemon polls every few seconds; opencode run takes a few seconds against
|
|
217
|
+
# fake LLM. Give it generous room.
|
|
218
|
+
for i in $(seq 1 60); do
|
|
219
|
+
ACTIVE=$(curl -sS "http://127.0.0.1:$DAEMON_PORT/api/status" | jq -r '.issues // 0')
|
|
220
|
+
if [[ "$ACTIVE" -ge 1 ]]; then
|
|
221
|
+
pass "daemon registered issue after ${i} seconds (issues=$ACTIVE)"
|
|
222
|
+
break
|
|
223
|
+
fi
|
|
224
|
+
sleep 1
|
|
225
|
+
[[ $i -eq 60 ]] && fail "daemon did not register issue"
|
|
226
|
+
done
|
|
227
|
+
|
|
228
|
+
info "wait for opencode to write a session"
|
|
229
|
+
OPENCODE_DB="$HOME/.local/share/opencode/opencode.db"
|
|
230
|
+
# Query opencode.db via bun (bun:sqlite ships with the runtime, no extra
|
|
231
|
+
# dependency to apt install). One-line JS keeps the bash flow readable.
|
|
232
|
+
sql_query() {
|
|
233
|
+
bun -e "const db=require('bun:sqlite');const d=new db.Database('$1',{readonly:true,create:false});try{process.stdout.write(JSON.stringify(d.prepare(\`$2\`).all()))}catch(e){process.stdout.write('')}" 2>/dev/null
|
|
234
|
+
}
|
|
235
|
+
SESSION_ID=""
|
|
236
|
+
# 180s window: opencode spawn + fake-LLM roundtrip + write to DB. The
|
|
237
|
+
# daemon's poller picks up the issue within ~30s, then opencode takes
|
|
238
|
+
# 5-30s depending on warm-up state.
|
|
239
|
+
for i in $(seq 1 180); do
|
|
240
|
+
ROWS=$(sql_query "$OPENCODE_DB" "SELECT id FROM session ORDER BY time_updated DESC LIMIT 1;")
|
|
241
|
+
SESSION_ID=$(echo "$ROWS" | jq -r '.[0].id // empty' 2>/dev/null || echo "")
|
|
242
|
+
if [[ -n "$SESSION_ID" ]]; then
|
|
243
|
+
pass "session created: $SESSION_ID (after ${i}s)"
|
|
244
|
+
break
|
|
245
|
+
fi
|
|
246
|
+
sleep 1
|
|
247
|
+
[[ $i -eq 180 ]] && {
|
|
248
|
+
echo "--- daemon.log (last 40) ---"
|
|
249
|
+
tail -40 "$DATA_DIR/run/daemon.log" 2>/dev/null || echo "(no daemon.log)"
|
|
250
|
+
echo "--- fake-llm log (last 10) ---"
|
|
251
|
+
tail -10 /tmp/fake-llm.log
|
|
252
|
+
fail "opencode did not write a session";
|
|
253
|
+
}
|
|
254
|
+
done
|
|
255
|
+
|
|
256
|
+
info "wait for session to have assistant content (fake LLM reply)"
|
|
257
|
+
for i in $(seq 1 30); do
|
|
258
|
+
ROWS=$(sql_query "$OPENCODE_DB" "SELECT COUNT(*) AS n FROM message WHERE session_id='${SESSION_ID}';")
|
|
259
|
+
MSG_COUNT=$(echo "$ROWS" | jq -r '.[0].n // 0' 2>/dev/null || echo 0)
|
|
260
|
+
if [[ "$MSG_COUNT" -ge 2 ]]; then
|
|
261
|
+
pass "session has $MSG_COUNT messages (user + assistant)"
|
|
262
|
+
break
|
|
263
|
+
fi
|
|
264
|
+
sleep 1
|
|
265
|
+
[[ $i -eq 30 ]] && fail "session never got assistant reply (msgs=$MSG_COUNT)";
|
|
266
|
+
done
|
|
267
|
+
|
|
268
|
+
info "verify session is browsable via awork-web"
|
|
269
|
+
# awork-web's OpencodeClient reads opencode.db and calls `opencode export`.
|
|
270
|
+
# Hit the /sessions list and the specific session page. Both require the
|
|
271
|
+
# admin auth cookie (otherwise 302 → /login).
|
|
272
|
+
SESSIONS_RESP=$(curl -sS -i -H "Cookie: $AUTH_COOKIE" "http://127.0.0.1:$WORK_PORT/sessions")
|
|
273
|
+
SESSIONS_HTML=$(echo "$SESSIONS_RESP" | tail -n +5)
|
|
274
|
+
SESSIONS_STATUS=$(echo "$SESSIONS_RESP" | head -1)
|
|
275
|
+
if ! echo "$SESSIONS_HTML" | grep -q "$SESSION_ID"; then
|
|
276
|
+
echo "--- HTTP status: $SESSIONS_STATUS ---"
|
|
277
|
+
echo "--- /sessions response (first 2000 chars of body) ---"
|
|
278
|
+
echo "$SESSIONS_HTML" | head -c 2000
|
|
279
|
+
echo
|
|
280
|
+
echo "--- sessions actually in opencode.db ---"
|
|
281
|
+
sql_query "$OPENCODE_DB" "SELECT id, time_created, time_archived FROM session ORDER BY time_created DESC LIMIT 5;" | jq . 2>/dev/null
|
|
282
|
+
echo "--- ework-web access log (last 20) ---"
|
|
283
|
+
tail -20 "$DATA_DIR/run/web-access.log" 2>/dev/null || echo "(no access log)"
|
|
284
|
+
fail "/sessions list does not contain the new session ID"
|
|
285
|
+
fi
|
|
286
|
+
pass "/sessions lists the new session"
|
|
287
|
+
|
|
288
|
+
SESSION_PAGE_HTML=$(curl -sS -H "Cookie: $AUTH_COOKIE" "http://127.0.0.1:$WORK_PORT/sessions/$SESSION_ID")
|
|
289
|
+
# Look for any marker that the fake LLM reply rendered. The reply body always
|
|
290
|
+
# starts with "E2E fake-LLM reply" so we grep for that substring.
|
|
291
|
+
echo "$SESSION_PAGE_HTML" | grep -q "E2E fake-LLM" \
|
|
292
|
+
|| fail "session page does not contain fake-LLM reply text"
|
|
293
|
+
pass "session page renders fake-LLM reply content"
|
|
294
|
+
|
|
295
|
+
info "drive headless browser through the UI"
|
|
296
|
+
# bun (not tsx) because the script imports `bun:sqlite` to peek at opencode.db.
|
|
297
|
+
cp /host-test/browser-flow.ts /tmp/test-runner/
|
|
298
|
+
WORK_PORT="$WORK_PORT" \
|
|
299
|
+
WORK_DATA_DIR="$DATA_DIR" \
|
|
300
|
+
OPENCODE_DB="$OPENCODE_DB" \
|
|
301
|
+
HEADLESS=1 \
|
|
302
|
+
bun /tmp/test-runner/browser-flow.ts
|
|
303
|
+
pass "browser flow test PASSED"
|
|
304
|
+
|
|
305
|
+
info "fake LLM log tail"
|
|
306
|
+
tail -10 /tmp/fake-llm.log
|
|
307
|
+
|
|
308
|
+
echo
|
|
309
|
+
echo "====================================================="
|
|
310
|
+
echo "BROWSER E2E PASSED"
|
|
311
|
+
echo "====================================================="
|
|
312
|
+
echo " session ID: $SESSION_ID"
|
|
313
|
+
echo " session URL: http://127.0.0.1:$WORK_PORT/sessions/$SESSION_ID"
|
|
314
|
+
echo " fake LLM log: /tmp/fake-llm.log"
|
|
315
|
+
echo " data dir: $DATA_DIR"
|
|
316
|
+
|
|
317
|
+
# Kill fake LLM (container exit also does this, but explicit is clearer).
|
|
318
|
+
kill "$FAKE_LLM_PID" 2>/dev/null || true
|
|
319
|
+
EOSCRIPT
|
|
320
|
+
|
|
321
|
+
echo
|
|
322
|
+
echo "${c_grn}BROWSER E2E COMPLETE${c_rst}"
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// Fake OpenAI-compatible LLM server for E2E tests.
|
|
3
|
+
//
|
|
4
|
+
// Listens on 127.0.0.1:PORT and responds to /v1/chat/completions with a
|
|
5
|
+
// deterministic, context-aware canned reply. Used by the docker E2E test
|
|
6
|
+
// to drive real `opencode run` against a stub LLM — opencode can't tell
|
|
7
|
+
// the difference, writes a real session to opencode.db, and awork-web can
|
|
8
|
+
// then render that session via its normal /sessions/:id route.
|
|
9
|
+
//
|
|
10
|
+
// Why not just stub the opencode binary? Because awork-web reads opencode.db
|
|
11
|
+
// directly (src/opencode.ts:98-139 listSessions) and calls `opencode export`
|
|
12
|
+
// for transcripts — stubbing the binary means re-implementing the SQLite
|
|
13
|
+
// schema, which would silently drift from real opencode. Talking to a fake
|
|
14
|
+
// LLM exercises every piece except the network call to a real model.
|
|
15
|
+
//
|
|
16
|
+
// Usage:
|
|
17
|
+
// PORT=8400 bun run scripts/fake-llm-server.ts
|
|
18
|
+
// # or just: bun run scripts/fake-llm-server.ts (defaults to 8400)
|
|
19
|
+
|
|
20
|
+
const PORT = parseInt(process.env.PORT ?? "8400", 10);
|
|
21
|
+
const HOST = process.env.HOST ?? "127.0.0.1";
|
|
22
|
+
|
|
23
|
+
const server = Bun.serve({
|
|
24
|
+
port: PORT,
|
|
25
|
+
hostname: HOST,
|
|
26
|
+
fetch(req) {
|
|
27
|
+
const url = new URL(req.url);
|
|
28
|
+
log(`${req.method} ${url.pathname}`);
|
|
29
|
+
|
|
30
|
+
if (req.method === "GET" && url.pathname === "/v1/models") {
|
|
31
|
+
return json({
|
|
32
|
+
object: "list",
|
|
33
|
+
data: [
|
|
34
|
+
{
|
|
35
|
+
id: "fake-model",
|
|
36
|
+
object: "model",
|
|
37
|
+
created: 1_700_000_000_000,
|
|
38
|
+
owned_by: "e2e-test",
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (req.method === "POST" && url.pathname === "/v1/chat/completions") {
|
|
45
|
+
return handleChatCompletion(req);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return json({ error: `not found: ${req.method} ${url.pathname}` }, 404);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
function log(msg: string): void {
|
|
53
|
+
process.stderr.write(`[fake-llm] ${new Date().toISOString()} ${msg}\n`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function json(body: unknown, status = 200): Response {
|
|
57
|
+
return new Response(JSON.stringify(body), {
|
|
58
|
+
status,
|
|
59
|
+
headers: {
|
|
60
|
+
"content-type": "application/json",
|
|
61
|
+
"access-control-allow-origin": "*",
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function handleChatCompletion(req: Request): Promise<Response> {
|
|
67
|
+
let body: any;
|
|
68
|
+
try {
|
|
69
|
+
body = await req.json();
|
|
70
|
+
} catch (err) {
|
|
71
|
+
return json({ error: `invalid JSON: ${(err as Error).message}` }, 400);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Log the full request so we can see what opencode/@ai-sdk is asking for
|
|
75
|
+
// (stream vs non-stream, tool definitions, system prompt, etc).
|
|
76
|
+
log(` body: stream=${body?.stream} model=${body?.model} msgs=${body?.messages?.length} tools=${body?.tools?.length ?? 0}`);
|
|
77
|
+
|
|
78
|
+
const messages = Array.isArray(body?.messages) ? body.messages : [];
|
|
79
|
+
const userMsgs = messages.filter((m: any) => m?.role === "user");
|
|
80
|
+
const lastUser = userMsgs[userMsgs.length - 1];
|
|
81
|
+
const userText = typeof lastUser?.content === "string"
|
|
82
|
+
? lastUser.content
|
|
83
|
+
: Array.isArray(lastUser?.content)
|
|
84
|
+
? lastUser.content.map((p: any) => p?.text ?? "").join(" ")
|
|
85
|
+
: "";
|
|
86
|
+
|
|
87
|
+
// Deterministic, context-aware reply: echo a snippet of the user's last
|
|
88
|
+
// message so the session transcript has traceable content (not just a
|
|
89
|
+
// fixed string). Truncated so the reply stays readable in the UI.
|
|
90
|
+
const snippet = userText.slice(0, 120).replace(/\s+/g, " ").trim();
|
|
91
|
+
const reply =
|
|
92
|
+
`E2E fake-LLM reply.\n\n` +
|
|
93
|
+
`You said: "${snippet}${userText.length > 120 ? "…" : ""}"\n\n` +
|
|
94
|
+
`This is a stub response from scripts/fake-llm-server.ts. ` +
|
|
95
|
+
`The real value of this test is that opencode wrote a real session row ` +
|
|
96
|
+
`to opencode.db and awork-web can render it end-to-end.`;
|
|
97
|
+
|
|
98
|
+
const promptTokens = roughTokens(JSON.stringify(messages));
|
|
99
|
+
const completionTokens = roughTokens(reply);
|
|
100
|
+
const usage = {
|
|
101
|
+
prompt_tokens: promptTokens,
|
|
102
|
+
completion_tokens: completionTokens,
|
|
103
|
+
total_tokens: promptTokens + completionTokens,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// opencode (@ai-sdk/openai-compatible) defaults to stream=true. Returning
|
|
107
|
+
// a non-streaming JSON response silently produces a 0-token session row
|
|
108
|
+
// with no assistant text — the request completes but the transcript is
|
|
109
|
+
// empty. Honor the stream flag and emit SSE chunks.
|
|
110
|
+
if (body?.stream === true) {
|
|
111
|
+
return streamResponse(body?.model ?? "fake-model", reply, usage);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Non-streaming path (used by curl sanity checks in tests).
|
|
115
|
+
return json({
|
|
116
|
+
id: `chatcmpl-fake-${crypto.randomUUID()}`,
|
|
117
|
+
object: "chat.completion",
|
|
118
|
+
created: Math.floor(Date.now() / 1000),
|
|
119
|
+
model: body?.model ?? "fake-model",
|
|
120
|
+
choices: [
|
|
121
|
+
{
|
|
122
|
+
index: 0,
|
|
123
|
+
message: { role: "assistant", content: reply },
|
|
124
|
+
finish_reason: "stop",
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
usage,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function streamResponse(model: string, reply: string, usage: any): Response {
|
|
132
|
+
const id = `chatcmpl-fake-${crypto.randomUUID()}`;
|
|
133
|
+
const created = Math.floor(Date.now() / 1000);
|
|
134
|
+
|
|
135
|
+
// Split into ~10 chunks so the streaming code path actually streams
|
|
136
|
+
// (single-chunk streams work but look unrealistic in logs).
|
|
137
|
+
const chunks: string[] = [];
|
|
138
|
+
const words = reply.split(/(\s+)/);
|
|
139
|
+
const perChunk = Math.max(1, Math.ceil(words.length / 10));
|
|
140
|
+
for (let i = 0; i < words.length; i += perChunk) {
|
|
141
|
+
chunks.push(words.slice(i, i + perChunk).join(""));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const encoder = new TextEncoder();
|
|
145
|
+
const readable = new ReadableStream({
|
|
146
|
+
start(controller) {
|
|
147
|
+
// First chunk: role + opening content.
|
|
148
|
+
controller.enqueue(
|
|
149
|
+
encoder.encode(
|
|
150
|
+
sseLine({
|
|
151
|
+
id,
|
|
152
|
+
object: "chat.completion.chunk",
|
|
153
|
+
created,
|
|
154
|
+
model,
|
|
155
|
+
choices: [{ index: 0, delta: { role: "assistant", content: chunks[0] ?? "" }, finish_reason: null }],
|
|
156
|
+
}),
|
|
157
|
+
),
|
|
158
|
+
);
|
|
159
|
+
// Subsequent chunks: content deltas only.
|
|
160
|
+
for (let i = 1; i < chunks.length; i++) {
|
|
161
|
+
controller.enqueue(
|
|
162
|
+
encoder.encode(
|
|
163
|
+
sseLine({
|
|
164
|
+
id,
|
|
165
|
+
object: "chat.completion.chunk",
|
|
166
|
+
created,
|
|
167
|
+
model,
|
|
168
|
+
choices: [{ index: 0, delta: { content: chunks[i] }, finish_reason: null }],
|
|
169
|
+
}),
|
|
170
|
+
),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
// Final chunk: empty delta + finish_reason + usage (usage must be on
|
|
174
|
+
// the final chunk when stream_options.include_usage is set; opencode
|
|
175
|
+
// sets it so the session row gets non-zero token counts).
|
|
176
|
+
controller.enqueue(
|
|
177
|
+
encoder.encode(
|
|
178
|
+
sseLine({
|
|
179
|
+
id,
|
|
180
|
+
object: "chat.completion.chunk",
|
|
181
|
+
created,
|
|
182
|
+
model,
|
|
183
|
+
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
|
|
184
|
+
usage,
|
|
185
|
+
}),
|
|
186
|
+
),
|
|
187
|
+
);
|
|
188
|
+
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
|
|
189
|
+
controller.close();
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
return new Response(readable, {
|
|
194
|
+
headers: {
|
|
195
|
+
"content-type": "text/event-stream",
|
|
196
|
+
"cache-control": "no-cache",
|
|
197
|
+
"connection": "keep-alive",
|
|
198
|
+
"access-control-allow-origin": "*",
|
|
199
|
+
},
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function sseLine(obj: any): string {
|
|
204
|
+
return `data: ${JSON.stringify(obj)}\n\n`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function roughTokens(text: string): number {
|
|
208
|
+
// Crude 4-chars-per-token heuristic. Token counts only need to be >0 for
|
|
209
|
+
// awork-web's heat bar (which divides by max-peak-token across the session).
|
|
210
|
+
// Real tokenizer isn't needed for test data.
|
|
211
|
+
return Math.max(1, Math.ceil(text.length / 4));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
process.stderr.write(
|
|
215
|
+
`[fake-llm] listening on http://${HOST}:${PORT}\n` +
|
|
216
|
+
`[fake-llm] POST /v1/chat/completions -> canned OpenAI response\n` +
|
|
217
|
+
`[fake-llm] GET /v1/models -> { fake-model }\n`,
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
// Keep stderr unbuffered so logs show up immediately in docker output.
|
|
221
|
+
process.stderr.write(`[fake-llm] ready (pid ${process.pid})\n`);
|