feat(coach): surface per-turn token usage on the chat stream

Carry an additive usage envelope on the SSE done frame
({ totalTokens, model }) and persist tokensUsed + model onto
coach_messages (migration 0182, nullable, no backfill) so the
per-message token footer survives a conversation reload. Add an
optional reasoning frame the client can render inside the thinking
disclosure when a reasoning-capable provider emits a summary; providers
without reasoning emit none. Extend CoachMessageDTO and regenerate the
OpenAPI contract. Server-authoritative: clients display the count, never
recompute it.
This commit is contained in:
Marc-André Bombeck committed 2026-06-20 11:58:58 +02:00
1 parent a5b3715a46
commit f46e9d3bb8
9 files changed
+242 -29

No files matched your search

@@ -0,0 +1,14 @@
-- v1.18.9 — per-message Coach token usage + model.
--
-- The streaming chat endpoint already computes the per-turn token count
-- (`result.tokensUsed`) and the model that produced the reply, but until
-- now neither reached the client. Two additive nullable columns let the
-- quiet per-message token footer survive a conversation reload: the live
-- turn paints from the `done.usage` SSE frame, persisted history paints
-- from these columns.
--
-- Both NULL by default. No backfill: existing rows (and every user turn
-- or refusal, which never carries a token count) simply render no footer.
ALTER TABLE "coach_messages"
ADD COLUMN "tokens_used" INTEGER,
ADD COLUMN "model" TEXT;
+5
View File
@@ -3643,6 +3643,11 @@ model CoachMessage {
metricSourceJson String? @map("metric_source_json")
providerType String? @map("provider_type")
promptVersion String? @map("prompt_version")
// v1.18.9 — per-turn token usage + model, persisted so the quiet
// per-message token footer survives a conversation reload. Both
// nullable: user turns, refusals, and pre-feature rows carry NULL.
tokensUsed Int? @map("tokens_used")
model String?
createdAt DateTime @default(now()) @map("created_at")
conversation CoachConversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)