feat(ai): Study Buddy endpoint — SSE + run history (AI-037b)#328
Merged
Conversation
POST /me/books/{id}/studybuddy runs StudyBuddyAgent on a passage and streams
its steps over SSE (step* -> done | error), persisting the run (AI-036) with
the right status (budget-exhausted keeps its transcript). GET
/me/studybuddy/runs/{id} returns a persisted run for the show-steps UI.
Rate-limited (studybuddy, 8/min), X-Accel-Buffering off, client disconnect
untraced. Panel UI is AI-038.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 6 AI-037, slice b — the reader can now run the agent and watch it work. The panel UI is AI-038.
Changes
POST /me/books/{editionId}/studybuddy— authenticated; runsStudyBuddyAgenton a highlighted passage and streams its progress over SSE: astepevent per recorded step (index / kind / payload), adoneevent with the final answer (+ iterations + cost), or a terminalerrorevent when the agent fails or exhausts its budget. The run is persisted (AI-036) on completion with the right status — a budget-exhausted run keeps its partial transcript.X-Accel-Buffering: nofor the Cloudflare tunnel; client disconnect propagates untraced (no false trace); rate-limited (studybuddy, 8/min/IP — each run is several LLM calls).GET /me/studybuddy/runs/{runId}— returns a persisted run (scoped to the requesting user) with its step transcript parsed from jsonb, for the "show steps" view.Design notes
IAgentRunWriterfrom the request services (alive for the duration of the stream), accumulates the outcome, and writes oneagent_runrow in a best-effort terminal block — a failed write never breaks the response the client already received (surfaced aspersisted:falseon the error event).Verification
StreamRunAsyncover the real agent + loop + scripted LLM: direct answer →step→done+ a persistedcompletedrun with the output; a never-terminating model → partialsteps → terminalerror+ a persistedbudget_exhaustedrun that keeps its transcript.TextStack.UnitTests(272) green; solution builds;dotnet formatclean. The real streamed run (key + corpus) is exercised on prod / AI-039 eval.🤖 Generated with Claude Code