Skip to content

feat(chat): run pi behind /api/chat with an RPC bridge in the pod - #34

Open
jbouder wants to merge 6 commits into
mainfrom
chat-pi-bridge
Open

jbouder wants to merge 6 commits into
mainfrom
chat-pi-bridge

Conversation

@jbouder

@jbouder jbouder commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #28.

What changed

  • Bridge (internal/chatbridge, javo chat-bridge): runs in a chat pod and starts pi --mode rpc for each active conversation, at most four at once. Pi processes idle for 10 minutes are stopped. It reads pi's output split on line feeds only.
    • Translates pi's events into the AI SDK UI message stream: text, reasoning, tool calls (dynamic: true), steps, and a finish with token counts in the message metadata.
    • Tracks parts by contentIndex. Part ids carry the reply's id, so pi's short tool ids stay unique.
    • Drops the system prompt and pi's repeated usage blocks.
    • Reads history back from pi with get_messages. Stop sends abort.
    • Refuses a message while a reply streams. Answers extension dialogs as canceled.
    • Logs a request's route and conversation id, never a prompt or a reply.
  • Template chat (kind chat, private, agents environment):
    • chat-start.sh runs pi-setup.mjs, then the bridge. The model key and API token stay in the helper sidecar.
    • Conversations are kept in ~/.pi/agent/chat-sessions, apart from the terminal's sessions.
    • The app proxy sets X-Javo-App-Proxy and the bridge refuses it, so the app's own address never opens chat. Chat is reached only through /api/chat.
    • Added to the Helm values and the Docker Compose template mounts.
  • Gateway /api/chat: list conversations, load one, send a message and stream the reply, stop a reply.
    • Finds the caller's running chat app among their own apps. The request never names an app or an owner.
    • Refuses an app's or a job's own token.
    • JSON answers from the pod are re-encoded from known fields.
    • The stream is passed on one event at a time, each a JSON object of a known chunk type. A stream that breaks off ends with an error and a finish.
    • Chat counts as use of the app for "Stop when idle".
    • The OpenAPI document and UI client are regenerated.
  • Docs: how chat reaches pi (architecture), the pi chat template (using apps), and a note in the admin AI page.

Why

Pi only ran as a terminal. The chat UI (#29) needs an HTTP endpoint that streams what useChat reads, routed to a pi that runs as the signed-in person.

Differences from the issue

  • Conversation ids: the caller picks the id, a lower-case UUID. pi --session-id opens that conversation or creates it.
  • Where conversations are saved: ~/.pi/agent/chat-sessions, not pi's default folder for the working directory. Otherwise pi --continue in the terminal would reopen the latest chat.
  • No automatic chat app yet: the gateway does not create one. For now a person creates it from the "pi chat" template, which shows in the template list. In Chat is Home, with the current Home moved to the Apps page #29 the chat Home creates or starts it on the first message. The app's own address answers 403 by design, since chat is reached only through /api/chat.

How to test

go test ./internal/chatbridge/ ./internal/gateway/ -run 'Chat|Reply|Stop|History|Stream|Conversation|Dialog|Pi' -race
node --test pod/files/agents/chat-start.test.mjs

On a cluster (make up with an agent model configured), create a pi chat app, then:

id=$(uuidgen | tr A-Z a-z)
curl -N -H "Authorization: Bearer $JAVO_TOKEN" -H 'Content-Type: application/json' \
  -d '{"text":"What is in this folder?"}' "$JAVO_URL/api/chat/conversations/$id/messages"
curl -H "Authorization: Bearer $JAVO_TOKEN" "$JAVO_URL/api/chat/conversations/$id"

What was checked

  • Translator tests replay the pi 1.4.0 output recorded in Spike: pi in RPC mode, and the chat UI stack under the UI's page policy #26: text, reasoning, tool calls that succeed and fail, a stopped reply, two tool calls in one message, and text ending after a tool call starts.
    • Every stream is checked against the AI SDK's chunk rules (ai 7).
    • The recordings have no failed tool call, so that test marks one recorded result as an error.
  • Bridge tests with a fake pi over pipes: streaming, busy refusal, stop, list and reload after a restart, the app proxy refused, dialogs canceled, pi exiting mid-reply, idle and over-limit processes stopped, and no prompt or reply in the log.
  • Gateway tests with fake pods: one person never reaches another's pod, app tokens are refused, requests are validated, pod errors map to the right problems, the stream is filtered, a broken-off stream is ended, and history is filtered.
  • The existing test that coding-agent containers hold no key or token now covers the chat template. On a host the bridge listens on loopback.
  • Manual run with real pi 1.4.0, the real start script and the real bridge against a fake OpenAI-compatible model:
    • Tool calls, including a failing one, a second step with text, and token counts.
    • History matched the stream.
    • Stop, a refused second message, and a list and reload after a restart.
    • The app-proxy header was refused, and the log held no prompt text.
  • The UI type check and all 918 UI tests pass, and so do make test-local and the pod script tests.

Not checked

  • Any executor through Tapas: Kubernetes, Docker or process. That includes the pod setup (javo at /opt/javo-env, pi on PATH, the agent lock) and a signed-in curl through the real gateway. The issue asks for the Docker and process results to be recorded.
  • A real model. The fake model sends no reasoning.
  • "Stop when idle" left alone during a chat. Activity is recorded but not tested.
  • The backup tests (no restic here) and the shell prompt tests (macOS bash 3.2) fail on this Mac, the same as on clean main.

Follow-ups

🤖 Generated with Claude Code

jbouder and others added 4 commits November 30, 2026 18:31
javo chat-bridge runs in a chat pod. It starts pi --mode rpc for each
active conversation (at most four, idle ones stopped after 10 minutes),
reads pi's output split on line feeds only, and turns pi's events into
the AI SDK's UI message stream: text, reasoning, tool calls and their
results, steps, and a finish with token counts.

Parts are tracked by pi's contentIndex, since text and thinking can end
after a tool call of the same message starts. Part ids carry the reply's
id, so pi's short tool ids stay unique. The system prompt and pi's
repeated usage blocks are dropped. History is read back from pi with
get_messages. Stop sends abort. A message while a reply streams is
refused. Extension dialogs are answered as canceled. Requests the app
proxy forwards are refused, and no prompt or reply is logged.

The tests replay pi 1.4.0 output recorded in the spike (#26).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A private agents template of kind chat, on the agents environment, that
runs javo chat-bridge in place of ttyd. chat-start.sh sets pi's model up
with pi-setup.mjs, as the pi template does, then starts the bridge. The
model key and API token stay in the helper sidecar. Conversations are
kept in ~/.pi/agent/chat-sessions, apart from the terminal's sessions,
so pi --continue in the terminal never opens a chat.

The app proxy sets X-Javo-App-Proxy on what it forwards to the app, and
the bridge refuses it. On a host the bridge listens on loopback only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
List conversations, load one, send a message and stream the reply, and
stop a reply. The gateway finds the caller's running chat app among
their own apps; the request names a conversation, never an app or an
owner. An app's or a job's own token is refused.

The pod runs code as its owner, so its answers are checked: JSON answers
are re-encoded from known fields, and the reply stream is passed on one
event at a time, each a JSON object of a known chunk type, with the
gateway's own content type. A stream that breaks off ends with an error
and a finish. Chat counts as use of the app for "Stop when idle".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jbouder and others added 2 commits November 30, 2026 21:36
readEvent read each line with ReadString and checked maxEvent only after
it returned, so a chat pod that never sent a line feed grew the gateway's
memory without limit. Read with ReadSlice and check the size per chunk,
as the bridge's readLine does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QW4EexUmtM5t6pBngqzst3
@jbouder
jbouder requested a review from aktech December 1, 2026 02:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run pi behind a chat endpoint: RPC bridge in the pod and gateway routes

1 participant