Wave B: the native message record — input events, projection, adapter death
The chat record is now pi-native agent history (LhMessage[]), the AI SDK's
UIMessage transcript contract is gone from the server, and every client turn
arrives as ONE explicit input event.
Core (new):
- src/core/native-messages.ts — the LhMessage taxonomy (user/assistant/
toolResult + lhMarker customs: verdict, probe-handoff, subagent-steer,
subagent-abort, plan-block), constructors, and toLlmMessages — the single
model-facing seam (convertToLlm): strips ids, flattens markers to user
text, drops UI-only plan-blocks, synthesizes INTERRUPTED results for
dangling aborted calls, never throws.
- src/core/projection.ts — projectNative: the one-way native → UIMessage
projection (consecutive assistant+toolResult runs group into one
assistant view message; toolResults attach backward by toolCallId;
markers project to data parts; plan-blocks splice after the last
successful plan_init). Every existing consumer reads through it.
- src/core/input-events.ts — the ChatInput union (text/answer/verdict/
continue), parseChatInput validation, and inputAppends (answer targets
the dangling question call or 400s; verdict = optional note user
message + native verdict marker).
Server:
- chat-handler.ts — the turn root takes {chatId, input}; write-ahead
persists input appends + claimed steers before the turn; claimSteers
coalesces the whole queue with the handler's steerDelivered filter as
the dedupe seam; teacher-vs-prober branch on hasProbeDone.
- turn-service.ts — persistTurn stamps ids, drops clean question
placeholder toolResults (the record ends at the unanswered call),
emits plan-block markers idempotently (plan-blocks.ts rewrite), and
persists abort partials.
- ui-stream.ts — PiTurnSpec takes the native base; the wire dialect is
unchanged. jobs.ts steers are native markers (steer_<jobId> dedup ids);
waitForIdle reads danglingQuestionOf. teacher-agent.ts wires
convertToLlm: toLlmMessages. record-adapter.ts DELETED wholesale.
- GET /api/chats/[id] serves messages: projectNative(record.messages).
Client:
- chat-transport.ts — the 409 retry is IDENTITY (re-POST the same event
body after the turn settles — the event was never persisted
pre-acceptance; the merge logic is gone). The transport consumes the
stashed input event via prepareSendMessagesRequest and throws loudly on
a missing stash.
- chat.tsx — every send call site stashes exactly one ChatInput event
(text, answer, verdict, continue) before invoking the SDK send.
Tests: full native migration (chat-flow rewritten to input events and
native record pins; jobs/extraction/demos/graph/usage/plan-blocks/
probe-done/reattach-transport converted; record-adapter.test.ts deleted;
chat-transport tests pin the identity retry). 982/982, typecheck, lint,
build all green.
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* THE INPUT-EVENTS CONTRACT (Wave B, owner ruling 2026-09-26: "input
|
||||
* events now, in the same pass"): POST /api/chat carries `{chatId, input}`
|
||||
* — ONE explicit event, never a client transcript. The server owns the
|
||||
* record; a send tells it what HAPPENED, not what the client's list looks
|
||||
* like. The 409 merge-retry died with the full-list body (an input event
|
||||
* retries idempotently: nothing was appended when a POST is refused).
|
||||
*
|
||||
* The four events (the complete client-originated input taxonomy):
|
||||
* text — a learner-authored user message (composer send, initial
|
||||
* topic, the demo error-fix loop)
|
||||
* answer — the pending question's graded result (QuestionAnswerData,
|
||||
* built client-side exactly as today; a dismissal is an answer
|
||||
* carrying its flag). Server-side this becomes the call's
|
||||
* native tool result — fork 1's normal agent history.
|
||||
* verdict — the plan verdict (accept/decline); an optional note is the
|
||||
* learner's own real user message, sent first, exactly the
|
||||
* two-real-messages scheme.
|
||||
* continue — run the next turn on the current record (mount
|
||||
* continuations, job-settle re-syncs: the record ends at an
|
||||
* answered question with no teacher reaction yet).
|
||||
*
|
||||
* Server-originated delivery (the wake pipeline's steers) is NOT a client
|
||||
* input — the handler accepts it on the same path (TurnInput) so every
|
||||
* turn rides one composition.
|
||||
*/
|
||||
|
||||
import type { QuestionAnswerData } from "./question-core.ts";
|
||||
import type { LhMessage } from "./native-messages.ts";
|
||||
import {
|
||||
danglingQuestionOf,
|
||||
newAnswerResult,
|
||||
newUserMessage,
|
||||
newVerdictMessage,
|
||||
} from "./native-messages.ts";
|
||||
import { QUESTION_TOOLS } from "./pending.ts";
|
||||
|
||||
/** One client-originated input event (the POST body's `input`). */
|
||||
export type ChatInput =
|
||||
| { type: "text"; text: string }
|
||||
| { type: "answer"; toolCallId: string; answer: QuestionAnswerData }
|
||||
| { type: "verdict"; accepted: boolean; note?: string }
|
||||
| { type: "continue" };
|
||||
|
||||
/** A wake-pipeline steer delivery — same turn composition, server-origin. */
|
||||
export type SteerInput = { type: "steer"; steer: LhMessage };
|
||||
|
||||
/** Every input the turn composition accepts. */
|
||||
export type TurnInput = ChatInput | SteerInput;
|
||||
|
||||
/** A malformed input event (the honest 400 — the orphan-trap discipline). */
|
||||
export class ChatInputError extends Error {}
|
||||
|
||||
/** Parse and validate an input event off a request body. */
|
||||
export function parseChatInput(input: unknown): ChatInput {
|
||||
if (typeof input !== "object" || input === null) throw new ChatInputError("input must be an object");
|
||||
const i = input as Record<string, unknown>;
|
||||
switch (i.type) {
|
||||
case "text":
|
||||
if (typeof i.text !== "string" || (i.text as string).trim().length === 0) {
|
||||
throw new ChatInputError("text input requires non-empty text");
|
||||
}
|
||||
return { type: "text", text: i.text };
|
||||
case "answer":
|
||||
if (typeof i.toolCallId !== "string" || i.toolCallId.length === 0) {
|
||||
throw new ChatInputError("answer input requires toolCallId");
|
||||
}
|
||||
if (typeof i.answer !== "object" || i.answer === null) {
|
||||
throw new ChatInputError("answer input requires the graded answer object");
|
||||
}
|
||||
return { type: "answer", toolCallId: i.toolCallId, answer: i.answer as QuestionAnswerData };
|
||||
case "verdict":
|
||||
if (typeof i.accepted !== "boolean") throw new ChatInputError("verdict input requires accepted");
|
||||
if (i.note !== undefined && typeof i.note !== "string") {
|
||||
throw new ChatInputError("verdict note must be a string");
|
||||
}
|
||||
return { type: "verdict", accepted: i.accepted, note: i.note as string | undefined };
|
||||
case "continue":
|
||||
return { type: "continue" };
|
||||
default:
|
||||
throw new ChatInputError(`unknown input type: ${String(i.type)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The native appends an input event makes to the record (pure). The caller
|
||||
* persists them write-ahead; the agent's context is the record plus these.
|
||||
*
|
||||
* Answer validation is the orphan-400 discipline: the call must be a
|
||||
* DANGLING QUESTION call in the record (posed, never answered) — anything
|
||||
* else 400s honestly instead of silently interrupting a question or
|
||||
* double-answering.
|
||||
*/
|
||||
export function inputAppends(record: { messages: LhMessage[] }, input: ChatInput): LhMessage[] {
|
||||
switch (input.type) {
|
||||
case "text":
|
||||
return [newUserMessage(input.text)];
|
||||
case "answer": {
|
||||
const pending = danglingQuestionOf(record.messages);
|
||||
if (!pending || pending.toolCallId !== input.toolCallId) {
|
||||
throw new ChatInputError("answer input targets no pending question call");
|
||||
}
|
||||
return [newAnswerResult(input.toolCallId, pending.tool, input.answer)];
|
||||
}
|
||||
case "verdict": {
|
||||
const appends: LhMessage[] = [];
|
||||
if (input.note && input.note.trim().length > 0) {
|
||||
appends.push(newUserMessage(input.note));
|
||||
}
|
||||
appends.push(newVerdictMessage(input.accepted, new Date().toISOString()));
|
||||
return appends;
|
||||
}
|
||||
case "continue":
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** Is this tool name a question tool (dock-owned, answer-gated)? */
|
||||
export function isQuestionTool(name: string): boolean {
|
||||
return (QUESTION_TOOLS as readonly string[]).includes(name);
|
||||
}
|
||||
Reference in New Issue
Block a user