Files
junction/tests/opencode.sh
T
Hermes ef0fa43661 opencode serve als Endpunkt erster Klasse (Brücke zu /v1/)
opencode spricht kein OpenAI-Protokoll, sondern eine sitzungsorientierte API
(/api/session, /prompt, /model, /message). Ein Client soll davon nichts merken:

  kind opencode     Endpunkt wird über die Brücke bedient statt blind weitergeleitet
  auth_basic USER   HTTP Basic (opencode verlangt es, junction kannte nur Bearer)
  pass_cmd CMD      Passwort aus einem Kommando — opencode erzeugt bei jedem Start eins
  base_cmd CMD      Basis-URL aus einem Kommando — der Dienst bindet einen freien Port

Ablauf je Anfrage: Sitzung anlegen, Modell setzen, Nachrichten zu einem Prompt
zusammenfassen, auf die fertige Assistenten-Nachricht warten, Antwort in OpenAI-Form
bauen, Sitzung löschen. reasoning_content wird getrennt geführt, Token-Zahlen kommen
aus der Sitzung. /v1/models wird ebenfalls übersetzt (modelID statt id, sonst landen
Varianten wie low/high als eigene Modelle in der Liste).

Bewusste Grenzen, im README benannt: Werkzeugdefinitionen werden nicht übersetzt, und
Streaming kommt als ein Stück plus [DONE], weil die Sitzungs-API keine Token-Deltas
liefert.

Geprüft: 10 neue Prüfungen gegen einen Nachbau des Sitzungs-API (tests/opencode.sh,
tests/opencode_stub.py), 76 gesamt, keine Fehler. Am echten Dienst einmal durchgelaufen
mit einem kostenlosen Modell: /v1/models 10 Modelle, Chat 200 mit Inhalt "bereit" in 4,1 s.
2026-11-04 21:55:32 +00:00

86 lines
3.3 KiB
Bash

#!/bin/bash
# Wird von tests/run.sh eingebunden: die opencode-Brücke gegen einen Nachbau.
# Erwartet: $TMP, $PORT, ok/bad/contains, ./junction
echo "== 18. opencode serve als Endpunkt (Brücke zu /v1/) =="
OC_PORT=$((PORT+18))
OC_JUNC=$((PORT+19))
python3 tests/opencode_stub.py $OC_PORT > "$TMP/oc-stub.log" 2>&1 &
PIDS+=($!)
sleep 0.6
printf 'opencode:testpasswort\n' > "$TMP/oc.pass"
chmod +x "$TMP/oc.pass"
cat > "$TMP/oc.pass.sh" <<'SH'
#!/bin/sh
cat "$1" 2>/dev/null
SH
# Ein Skript, das das Passwort ausgibt — so wie im Betrieb opencode_password.sh.
printf '#!/bin/sh\necho opencode:testpasswort | cut -d: -f2\n' > "$TMP/oc.pass.sh"
chmod +x "$TMP/oc.pass.sh"
cat > "$TMP/oc.conf" <<CFG
listen 127.0.0.1:$OC_JUNC
endpoint oc {
base http://127.0.0.1:$OC_PORT
kind opencode
context 200000
timeout 30
idle_timeout 10
auth_basic opencode
pass_cmd "$TMP/oc.pass.sh"
}
req for "*" pass oc
CFG
./junction --config "$TMP/oc.conf" > "$TMP/oc.log" 2>&1 &
PIDS+=($!)
sleep 0.8
# 1. Modellliste kommt als OpenAI-Liste, ohne die Varianten der Modelle
R=$(curl -s -m 20 "http://127.0.0.1:$OC_JUNC/v1/models")
contains "$R" '"id":"glm-5.3-flash-free"' "the bridge lists opencode models as an OpenAI list"
case "$R" in
*'"id":"low"'*) bad "model variants leak into the list as models";;
*) ok "model variants do not leak into the list";;
esac
# 2. Chat: OpenAI rein, Antwort raus
R=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' \
-d '{"model":"glm-5.3-flash-free","messages":[{"role":"user","content":"hallo welt"}],"max_tokens":10}' \
"http://127.0.0.1:$OC_JUNC/v1/chat/completions")
contains "$R" '"object":"chat.completion"' "a chat request becomes an OpenAI completion"
contains "$R" 'ANTWORT auf: hallo welt' "the prompt reached the session and the answer came back"
contains "$R" '"reasoning_content":"kurz nachgedacht"' "reasoning is carried separately from the answer"
contains "$R" '"finish_reason":"stop"' "the finish reason is translated"
contains "$R" '"prompt_tokens":11' "the token counts are read from the session"
# 3. Das Modell kam als provider/id an
R=$(curl -s -m 20 "http://127.0.0.1:$OC_JUNC/v1/models")
case "$R" in
*'"object":"list"'*) ok "the models call goes through the bridge, not the plain proxy";;
*) bad "the models call did not go through the bridge";;
esac
# 4. Ein Systemprompt wird mitgeschickt, nicht verschluckt
R=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' \
-d '{"model":"beam-free","messages":[{"role":"system","content":"sei knapp"},{"role":"user","content":"frage"}]}' \
"http://127.0.0.1:$OC_JUNC/v1/chat/completions")
contains "$R" 'System: sei knapp' "a system prompt survives into the session"
# 5. Falsches Passwort ist ein sauberer Fehler, kein Absturz
printf '#!/bin/sh\necho falsch\n' > "$TMP/oc.bad.sh"
chmod +x "$TMP/oc.bad.sh"
sed "s|pass_cmd .*|pass_cmd \"$TMP/oc.bad.sh\"|; s|$OC_JUNC|$((PORT+20))|" "$TMP/oc.conf" > "$TMP/oc2.conf"
./junction --config "$TMP/oc2.conf" > "$TMP/oc2.log" 2>&1 &
PIDS+=($!)
sleep 0.8
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"x","messages":[{"role":"user","content":"hi"}]}' \
"http://127.0.0.1:$((PORT+20))/v1/chat/completions")
contains "$R" 'no_endpoint_available' "a rejected password fails as a routing error, not a crash"