fix: validate prompt API payloads

Fixes #11
This commit is contained in:
Federico Jaramillo Martinez
2026-06-05 11:09:15 +02:00
parent 3330a5a7fa
commit 82ba2e0490
7 changed files with 110 additions and 9 deletions
+7 -2
View File
@@ -2,6 +2,11 @@ import type { FastifyInstance } from "fastify";
import type { SessionEventHub } from "../realtime/sessionEventHub.js";
import type { PiSessionService } from "./piSessionService.js";
interface PromptRequestBody {
text?: unknown;
streamingBehavior?: unknown;
}
export function registerSessionRoutes(app: FastifyInstance, sessions: PiSessionService, eventHub: SessionEventHub, prefix = ""): void {
app.get<{ Querystring: { cwd?: string } }>(`${prefix}/sessions`, async (request, reply) => {
if (request.query.cwd === undefined || request.query.cwd === "") return reply.code(400).send({ error: "cwd query parameter is required" });
@@ -89,9 +94,9 @@ export function registerSessionRoutes(app: FastifyInstance, sessions: PiSessionS
}
});
app.post<{ Params: { sessionId: string }; Body: { text: string; streamingBehavior?: "steer" | "followUp" } }>(`${prefix}/sessions/:sessionId/prompt`, async (request, reply) => {
app.post<{ Params: { sessionId: string }; Body: PromptRequestBody | undefined }>(`${prefix}/sessions/:sessionId/prompt`, async (request, reply) => {
try {
await sessions.prompt(request.params.sessionId, request.body.text, request.body.streamingBehavior);
await sessions.prompt(request.params.sessionId, request.body?.text, request.body?.streamingBehavior);
return { accepted: true };
} catch (error) {
return reply.code(400).send({ error: error instanceof Error ? error.message : String(error) });