fix(prompter): align frontend API client with the backend session contract

The Prompter frontend and backend were built against mismatched contracts:
- createSession POSTed no body (the endpoint requires one) and read a
  `session_id` field the response doesn't have (it is `id`), so every session
  create returned 422.
- sendMessage hit `/sessions/{id}/chat` with `{message}` and expected a
  `{reply, draft}` object, but the session endpoint is `/sessions/{id}/messages`
  with `{content}` and returns the full message list.
- API errors rendered as "[object Object]" because getErrorMessage String()'d a
  FastAPI validation array.

Point createSession/sendMessage at the real endpoints, adapt the message-list
response (latest assistant message is the reply; fetch the structured draft when
the reply signals readiness), and make getErrorMessage format string /
validation-array / structured-object details into a readable line.
This commit is contained in:
Renn F
2026-06-08 16:17:41 +02:00
parent b42c13da89
commit f2df7fc30b
2 changed files with 91 additions and 12 deletions
+52 -8
View File
@@ -25,6 +25,32 @@ export interface CreateSessionResponse {
session_id: string;
}
// A message record as returned by the backend (PrompterMessageResponse).
interface BackendMessage {
id: string;
session_id: string;
role: "user" | "assistant";
content: string;
created_at: string;
}
// Mirrors the backend's draft-ready signal phrases (services/prompter.py).
// If the backend list ever drifts, the draft simply doesn't auto-surface (the
// user can keep chatting) — it never breaks the conversation flow.
const DRAFT_READY_SIGNALS = [
"i have enough information",
"ready to generate a draft",
"ready to draft",
"i can now draft",
"draft_ready=true",
"draft ready",
];
function replyLooksDraftReady(reply: string): boolean {
const lower = reply.toLowerCase();
return DRAFT_READY_SIGNALS.some((s) => lower.includes(s));
}
// ---------------------------------------------------------------------------
// API functions
// ---------------------------------------------------------------------------
@@ -32,29 +58,47 @@ export interface CreateSessionResponse {
export const prompterApi = {
/**
* Create a new prompter session, returning a session ID.
* The endpoint requires a JSON body (optional `context`), so send `{}`, and
* map the backend's `id` field onto our `session_id`.
*/
createSession: async (): Promise<CreateSessionResponse> => {
const { data } = await api.post<CreateSessionResponse>("/prompter/sessions");
return data;
const { data } = await api.post<{ id: string }>("/prompter/sessions", {});
return { session_id: data.id };
},
/**
* Send a chat message in an existing session.
* Returns the assistant reply and, if ready, a draft task proposal.
* Send a chat message in an existing session. The backend appends the user
* message, replies, and returns the full message list. We surface the latest
* assistant message as the reply and, when it signals readiness, fetch the
* structured draft.
*/
sendMessage: async (
sessionId: string,
message: string
): Promise<ChatResponse> => {
const { data } = await api.post<ChatResponse>(
`/prompter/sessions/${sessionId}/chat`,
{ message }
const { data: messages } = await api.post<BackendMessage[]>(
`/prompter/sessions/${sessionId}/messages`,
{ content: message }
);
return data;
const lastAssistant = [...messages]
.reverse()
.find((m) => m.role === "assistant");
const reply = lastAssistant?.content ?? "";
let draft: DraftProposal | null = null;
if (replyLooksDraftReady(reply)) {
try {
draft = await prompterApi.getDraft(sessionId);
} catch {
draft = null;
}
}
return { reply, draft, session_id: sessionId };
},
/**
* Fetch the current draft for a session (if the LLM has produced one).
* The backend returns a TaskDraftResponse whose `draft` field holds the task.
*/
getDraft: async (sessionId: string): Promise<DraftProposal | null> => {
const { data } = await api.get<{ draft: DraftProposal | null }>(