This commit was merged in pull request #78.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Router, type Request, type Response } from "express";
|
||||
import { randomBytes, randomUUID, createHash } from "crypto";
|
||||
import { db, sessions, sessionRequests, type Session } from "@workspace/db";
|
||||
import { db, sessions, sessionRequests, sessionMessages, getSessionHistory, type Session } from "@workspace/db";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { lnbitsService } from "../lib/lnbits.js";
|
||||
import { sessionsLimiter } from "../lib/rate-limiter.js";
|
||||
@@ -312,6 +312,9 @@ router.post("/sessions/:id/request", async (req: Request, res: Response) => {
|
||||
const requestId = randomUUID();
|
||||
const btcPriceUsd = await getBtcPriceUsd();
|
||||
|
||||
// Load conversation history for context injection
|
||||
const history = await getSessionHistory(id, 8, 4000);
|
||||
|
||||
// Eval phase
|
||||
const evalResult = await agentService.evaluateRequest(requestText);
|
||||
const evalCostUsd = pricingService.calculateActualCostUsd(
|
||||
@@ -343,7 +346,7 @@ router.post("/sessions/:id/request", async (req: Request, res: Response) => {
|
||||
|
||||
if (evalResult.accepted) {
|
||||
try {
|
||||
const workResult = await agentService.executeWork(requestText);
|
||||
const workResult = await agentService.executeWork(requestText, history);
|
||||
workInputTokens = workResult.inputTokens;
|
||||
workOutputTokens = workResult.outputTokens;
|
||||
workCostUsd = pricingService.calculateActualCostUsd(
|
||||
@@ -452,6 +455,14 @@ router.post("/sessions/:id/request", async (req: Request, res: Response) => {
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(sessions.id, id));
|
||||
|
||||
// Persist conversation history only for completed requests
|
||||
if (finalState === "complete") {
|
||||
await tx.insert(sessionMessages).values([
|
||||
{ sessionId: id, role: "user" as const, content: requestText, tokenCount: Math.ceil(requestText.length / 4) },
|
||||
{ sessionId: id, role: "assistant" as const, content: result ?? "", tokenCount: Math.ceil((result ?? "").length / 4) },
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
// ── Trust scoring ────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user