Files
substrata/scripts/db/015-chat-review.sql
CatoandClaude 757d1b3a1d feat(chat): the answer feedback loop — thumbs, transcript export, nightly review (#198)
Substrata was the product whose Ask answered "nothing in our database" with
no way forward, and nobody knew until the owner said so. From here every
turn is in the loop:

- chatkit 0.14: thumbs under every answer (ChatThread onFeedback); a thumb
  down asks why. POST /api/chat/feedback (zod, same-origin, rate limited)
  keeps it in chat_feedback.
- chat_turns: /api/chat logs the question and, when the loop ends, the
  answer with the status and tool labels the reader watched, the page it was
  asked from and whether it failed. Ids are random uuids the browser mints
  per thread and per answer; no reader, no IP, no key.
- GET /api/chat/review-export: bearer CHAT_REVIEW_TOKEN (constant-time; 401
  otherwise, 503 while unset), {product, scope, transcripts} in chatkit's
  ChatTranscript shape — at most 50 conversations since `since`, 30 turns
  each, 4,000 characters a turn, with their feedback. The scope is built
  from the system prompt's role line and the tool registry, never typed
  twice.
- scripts/db/015-chat-review.sql, .env.example, docs/INFRASTRUCTURE.md
  "Chat review", test/chat-review.test.ts.


Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-22 19:02:13 +02:00

41 lines
1.9 KiB
PL/PgSQL

BEGIN;
-- The chat-review loop (docs/INFRASTRUCTURE.md, "Chat review"). Every Ask
-- turn is kept so a nightly judge in Loki can read a day of conversations
-- and say what the reader most likely wanted, what Substrata could have
-- given with the tools it has, and what it gave. Nothing here names a
-- person: the ids are random, minted by the reader's browser for the thread
-- and by the server for each turn; no reader, no IP, no key.
CREATE TABLE IF NOT EXISTS chat_turns (
id uuid PRIMARY KEY,
conversation_id uuid NOT NULL,
role text NOT NULL CHECK (role IN ('user', 'assistant')),
content text NOT NULL,
-- The status and tool labels the reader saw while the answer was written.
steps jsonb NOT NULL DEFAULT '[]',
-- The page the question was asked from ('/chat', '/markets/asml').
surface text NOT NULL DEFAULT '/chat',
failed boolean NOT NULL DEFAULT false,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS chat_turns_conversation ON chat_turns (conversation_id, created_at);
CREATE INDEX IF NOT EXISTS chat_turns_created ON chat_turns (created_at);
-- A thumb under an answer (chatkit's AnswerFeedback): up or down, a reason
-- from chatkit's FEEDBACK_REASONS, and the reader's own words if any.
CREATE TABLE IF NOT EXISTS chat_feedback (
id bigserial PRIMARY KEY,
conversation_id uuid NOT NULL,
message_id uuid NOT NULL,
verdict text NOT NULL CHECK (verdict IN ('up', 'down')),
reason text,
text text,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS chat_feedback_conversation ON chat_feedback (conversation_id, created_at);
COMMENT ON TABLE chat_turns IS 'Every Ask turn (question, answer, steps, surface) for the nightly chat review; random ids only, no reader.';
COMMENT ON TABLE chat_feedback IS 'Thumbs under Ask answers (chatkit AnswerFeedback), exported with the conversation for review.';
COMMIT;