- src/agent/lh-tool.ts deleted: the zod-wrapped LhTool + toPiTool bridge (z.toJSONSchema per call, clientQuestion flag, hand-rolled result mapping) is gone. Tools are pi-agent-core native AgentTools now. - New src/agent/tool-execute.ts owns what the wrapper added: the abort guard (runWithAbortGuard: run signal + 30s per-call timeout + 250ms grace, 'Operation aborted' / timeout wording preserved), ToolInputError (the retryable '- problem at path' format), result mapping (string verbatim / object JSON / image parts, details rides the record's tool part output), and questionResult() — placeholder + terminate:true (owner Option A: belt of terminate + slim makeQuestionStopper with the clean-question rule; maxTurns deleted from the teaching turn entirely per owner). - All 11 tool files + tools.ts registries converted to TypeBox parameters (typebox@1.3.7, pinned to pi-agent-core's copy). zod refinements (quizInputProblems, askOptionProblems, live-nodeId) moved from schemas into execute — the model sees the same retryable error shape. - question-core + contracts/probe TypeBox-authored (single source shared with the UI); tool-parts parse seam runs the boundary's full chain (Convert → Check → Clean → refinement), fixing the live-smoke lockout: pi's boundary Value.Converts correctAnswer: 17 → '17' but the record keeps raw args, so a parse without Convert rejected a posed quiz (invisible pending question, turn ended). Pinned with the real smoke emission shape. - Subagents converted: demo-builder/researcher AgentTool-native, visual-maker/pdf-reader declaration-only pi-ai Tools with local execute fns, extraction submit schemas re-authored in TypeBox (owner option 2: zod defaults → Type.Optional + fill at the submit seam). - Accepted delta, flagged: TypeBox-native schemas skip pi's JSON-Schema primitive coercion fast path... (boundary still Value.Converts), and prober-quiz unknown keys ride through (Value.Clean strips at display). - Tests: 988 passing (was 967 at the recon baseline); tool-abort and read-tool rewritten against the native seams; question-core/question- tools/tool-parts/demos/web-search/graph-tools/plan-tree/probe-done/ tool-toggles converted to Value.Check/execute pins. Live smoke (PORT=3004, real model): native ls turn streams end-to-end; a real quiz pose (first attempt missing correctAnswer → tool-input-error → model retry → posed) terminates the turn cleanly and persists pending.
91 lines
4.3 KiB
TypeScript
91 lines
4.3 KiB
TypeScript
import type { AgentTool } from "@earendil-works/pi-agent-core";
|
|
import { serverExecute } from "../tool-execute.ts";
|
|
import { probeDoneInputSchema, type ProbeDoneResult } from "../../core/contracts.ts";
|
|
import type { UIMessage } from "ai";
|
|
import { findPendingToolCall } from "../../core/pending.ts";
|
|
import type { ChatStore } from "../../core/ports.ts";
|
|
import { buildProbeHandoffSteer, type ProbeHandoffSteerMessage } from "../../core/contracts.ts";
|
|
|
|
/** What the probe_done tool closes over. */
|
|
export interface ProbeDoneContext {
|
|
chatId: string;
|
|
/** The storage port (open-question + already-concluded checks). */
|
|
chatStore: ChatStore;
|
|
/** Queue the handoff steer (the wake pipeline — src/server/jobs.ts).
|
|
* Injected so this module never imports the server seam. */
|
|
deliver: (steer: ProbeHandoffSteerMessage) => void;
|
|
}
|
|
|
|
/** Does the transcript already carry a concluded probe (any state)? */
|
|
function probeConcluded(messages: UIMessage[]): boolean {
|
|
for (const m of messages) {
|
|
if (m.parts.some((p) => p.type === "tool-probe_done")) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* probe_done — the probe→planning seam (docs/probe-teach-split.md §2).
|
|
*
|
|
* SERVER-SIDE. The prober concludes the probe by authoring the learner
|
|
* assessment (the tool INPUT — it streams to the learner live and is
|
|
* durable in the transcript as the tool part itself). Three outcomes:
|
|
*
|
|
* ok:false + error — a question is still open ("resolve it first"), or
|
|
* the probe is already concluded. The refusal returns to the prober as
|
|
* a tool result; it fixes course and retries (the return-to-agent
|
|
* doctrine, same as nodeId validation).
|
|
* ok:true — the assessment is recorded, and the HANDOFF is queued: a
|
|
* steer through the wake pipeline that fires the teacher's first turn
|
|
* (plan authoring) the moment the prober's turn ends. Auto-wake is the
|
|
* user-decided trigger — the learner experiences assessment → plan as
|
|
* one continuous flow.
|
|
*
|
|
* The first probe_done in the transcript is also the construction seam:
|
|
* from that message on, every request builds the TEACHER agent.
|
|
*/
|
|
export function probeDoneTool(ctx: ProbeDoneContext): Record<string, AgentTool<any>> {
|
|
return {
|
|
probe_done: {
|
|
name: "probe_done",
|
|
label: "probe_done",
|
|
description:
|
|
"Conclude the probing phase and hand off to the teaching phase. Call this ONCE, when the probe is " +
|
|
"complete: every strand the lesson depends on is bracketed (a floor he reliably gets right AND a " +
|
|
"ceiling where it runs out), and his learning goal is concrete. The input IS the learner assessment — " +
|
|
"one strand per dependency the lesson will rest on, with your read of where his edge sits on it " +
|
|
"(floor, ceiling, misconceptions, what to build from). It streams to the learner live and becomes " +
|
|
"the handoff artifact the teacher plans from. Refused while a question is still open, and after " +
|
|
"the probe is already concluded. After it succeeds, say your closing sentence and end your turn.",
|
|
// The core-owned schema (contracts/probe.ts) — the SAME schema the UI
|
|
// parse seam (tool-parts.ts) runs, so boundary-valid ⇔ consumer-parseable.
|
|
parameters: probeDoneInputSchema,
|
|
execute: serverExecute("probe_done", probeDoneInputSchema, async (input): Promise<ProbeDoneResult> => {
|
|
const record = ctx.chatStore.getChat(ctx.chatId);
|
|
if (!record) return { ok: false, error: `Chat not found: ${ctx.chatId}` };
|
|
|
|
if (findPendingToolCall(record.messages) !== null) {
|
|
return {
|
|
ok: false,
|
|
error:
|
|
"A question is still open — resolve it (or let him answer) before concluding the probe. " +
|
|
"probe_done only fires with no question outstanding.",
|
|
};
|
|
}
|
|
if (probeConcluded(record.messages)) {
|
|
return { ok: false, error: "The probe is already concluded — probe_done fires exactly once." };
|
|
}
|
|
try {
|
|
ctx.deliver(buildProbeHandoffSteer(ctx.chatId));
|
|
} catch (err) {
|
|
console.error(`[chat ${ctx.chatId}] probe handoff queueing failed:`, err);
|
|
// The handoff is recoverable-by-design (the next client message
|
|
// constructs the teacher off the transcript seam) — record the
|
|
// assessment anyway; the wake is an optimization, not a gate.
|
|
}
|
|
return { ok: true, assessment: input.assessment };
|
|
}),
|
|
},
|
|
};
|
|
}
|