[Session] Context injection — pass conversation history to work model #39

Closed
opened 2026-03-21 00:35:03 +00:00 by replit · 1 comment
Owner

What

When executing a session work request, prepend the session's prior conversation history to the prompt, and append the completed exchange to history after the response is produced.

Depends on: #38 (session_messages table)

Why

With the table in place but no injection, Timmy still cannot reference prior context. This ticket wires the stored history into the AI call so follow-ups like "now make it shorter" work naturally.

Where to change

artifacts/api-server/src/routes/sessions.tsPOST /sessions/:id/request

Step 1 — Load history before eval (line ~315)

const history = await getSessionHistory(id, 8, 4000);
// history: Array<{ role: "user" | "assistant", content: string }>

Step 2 — Change executeWork call (line ~346)

Add a conversationHistory parameter to AgentService.executeWork (and the streaming variant) so the Anthropic messages array receives all prior turns:

const workResult = await agentService.executeWork(requestText, history);

In agent.ts, build the messages array:

const messages = [
  ...history,                          // prior turns (already role/content objects)
  { role: "user", content: requestText } // current request
];

Pass messages to client.messages.create({ ..., messages }).

Step 3 — Append exchange to history after completion (line ~428, inside the DB transaction)

await tx.insert(sessionMessages).values([
  { sessionId: id, role: "user",      content: requestText,  tokenCount: Math.ceil(requestText.length  / 4) },
  { sessionId: id, role: "assistant", content: result ?? "", tokenCount: Math.ceil((result ?? "").length / 4) },
]);

Only insert on finalState === "complete" — rejected/failed requests are not persisted to history.

Relevant files

  • artifacts/api-server/src/routes/sessions.ts
  • artifacts/api-server/src/lib/agent.ts
  • packages/db/src/index.ts (getSessionHistory helper from #38)

Acceptance

  • Second request in a session correctly references content from the first
  • Timmy replies include phrases like "As I mentioned earlier..." when relevant
  • Rejected requests do not pollute history
  • Stub mode works unchanged (history injected but model still returns canned response)
## What When executing a session work request, prepend the session's prior conversation history to the prompt, and append the completed exchange to history after the response is produced. **Depends on**: #38 (session_messages table) ## Why With the table in place but no injection, Timmy still cannot reference prior context. This ticket wires the stored history into the AI call so follow-ups like "now make it shorter" work naturally. ## Where to change `artifacts/api-server/src/routes/sessions.ts` — `POST /sessions/:id/request` ### Step 1 — Load history before eval (line ~315) ```typescript const history = await getSessionHistory(id, 8, 4000); // history: Array<{ role: "user" | "assistant", content: string }> ``` ### Step 2 — Change executeWork call (line ~346) Add a `conversationHistory` parameter to `AgentService.executeWork` (and the streaming variant) so the Anthropic `messages` array receives all prior turns: ```typescript const workResult = await agentService.executeWork(requestText, history); ``` In `agent.ts`, build the messages array: ```typescript const messages = [ ...history, // prior turns (already role/content objects) { role: "user", content: requestText } // current request ]; ``` Pass `messages` to `client.messages.create({ ..., messages })`. ### Step 3 — Append exchange to history after completion (line ~428, inside the DB transaction) ```typescript await tx.insert(sessionMessages).values([ { sessionId: id, role: "user", content: requestText, tokenCount: Math.ceil(requestText.length / 4) }, { sessionId: id, role: "assistant", content: result ?? "", tokenCount: Math.ceil((result ?? "").length / 4) }, ]); ``` Only insert on `finalState === "complete"` — rejected/failed requests are not persisted to history. ## Relevant files - `artifacts/api-server/src/routes/sessions.ts` - `artifacts/api-server/src/lib/agent.ts` - `packages/db/src/index.ts` (getSessionHistory helper from #38) ## Acceptance - Second request in a session correctly references content from the first - Timmy replies include phrases like "As I mentioned earlier..." when relevant - Rejected requests do not pollute history - Stub mode works unchanged (history injected but model still returns canned response)
replit added the aibackend labels 2026-03-21 00:35:03 +00:00
claude was assigned by Rockachopa 2026-03-22 23:37:30 +00:00
Collaborator

PR #78 created.

Changes:

  • Added session_messages table + migration for storing conversation turns
  • Added getSessionHistory() helper with turn count and token budget limits
  • Wired conversation history into executeWork() and executeWorkStreaming() via optional conversationHistory parameter
  • POST /sessions/:id/request now loads history before the work call and persists user+assistant messages atomically — only on completed requests
  • Typecheck and lint pass cleanly
PR #78 created. Changes: - Added `session_messages` table + migration for storing conversation turns - Added `getSessionHistory()` helper with turn count and token budget limits - Wired conversation history into `executeWork()` and `executeWorkStreaming()` via optional `conversationHistory` parameter - `POST /sessions/:id/request` now loads history before the work call and persists user+assistant messages atomically — only on completed requests - Typecheck and lint pass cleanly
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: replit/timmy-tower#39