2026-09-12 02:34:18 +03:00
|
|
|
import { lhTool, type LhTool } from "./lh-tool.ts";
|
2026-09-11 06:44:20 +03:00
|
|
|
import {
|
|
|
|
|
askInputSchema,
|
|
|
|
|
probeQuizInputSchema,
|
|
|
|
|
quizInputObject,
|
|
|
|
|
quizInputProblems,
|
|
|
|
|
} from "../core/question-core.ts";
|
2026-09-19 14:06:07 +03:00
|
|
|
import { filterEnabledTools, type EnabledTools } from "../core/pending.ts";
|
2026-09-01 19:15:43 +03:00
|
|
|
import type { JobRecord } from "../core/contracts.ts";
|
|
|
|
|
import type { JobStore } from "../core/ports.ts";
|
2026-09-22 16:22:57 +03:00
|
|
|
import { jobTools, type DemoInput, type PdfInput, type ResearchInput, type VisualInput } from "./tools/jobs.ts";
|
2026-09-01 19:15:43 +03:00
|
|
|
import { planTools, type PlanToolContext } from "./tools/plan.ts";
|
2026-09-11 06:44:20 +03:00
|
|
|
import { probeDoneTool, type ProbeDoneContext } from "./tools/probe.ts";
|
2026-09-19 15:24:26 +03:00
|
|
|
import { readTool } from "./tools/read.ts";
|
2026-09-19 16:50:20 +03:00
|
|
|
import { lsTool } from "./tools/ls.ts";
|
|
|
|
|
import { findTool } from "./tools/find.ts";
|
|
|
|
|
import { grepTool } from "./tools/grep.ts";
|
2026-08-31 22:16:03 +03:00
|
|
|
|
|
|
|
|
export { jobTools };
|
2026-09-11 06:44:20 +03:00
|
|
|
export type { ProbeDoneContext } from "./tools/probe.ts";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
/**
|
2026-09-11 06:44:20 +03:00
|
|
|
* The agent-split tool composition (docs/probe-teach-split.md §1): the
|
|
|
|
|
* PROBER and the TEACHER are different agents with different tool sets.
|
|
|
|
|
* The question tools share a NAME but are genuinely DIFFERENT tools —
|
|
|
|
|
* the prober's quiz has NO nodeId field at all (its schema is built from
|
|
|
|
|
* quizInputBase; zod strips the key if the model tries anyway), the
|
|
|
|
|
* teacher's carries the validated position field.
|
2026-08-22 02:18:27 +03:00
|
|
|
*
|
2026-09-11 06:44:20 +03:00
|
|
|
* - Question tools (quiz / ask_user_question): CLIENT-SIDE (no `execute`).
|
|
|
|
|
* The call streams to the browser in `input-available` state; the
|
|
|
|
|
* learner answers in the dock; the client grades and sends the verdict
|
|
|
|
|
* with addToolOutput. Input schemas live in core/question-core.
|
|
|
|
|
* - probe_done (src/agent/tools/probe.ts): SERVER-SIDE seam tool.
|
|
|
|
|
* - planTools (src/agent/tools/plan.ts): SERVER-SIDE — author-once
|
|
|
|
|
* plan_init. Teacher only.
|
|
|
|
|
* - jobTools (src/agent/tools/jobs.ts): SERVER-EXECUTED dispatch. Teacher
|
|
|
|
|
* only.
|
2026-08-22 02:18:27 +03:00
|
|
|
*/
|
|
|
|
|
|
2026-09-12 02:34:18 +03:00
|
|
|
export const askUserQuestionTool: LhTool<any, any> = {
|
|
|
|
|
name: "ask_user_question",
|
|
|
|
|
clientQuestion: true,
|
2026-08-22 02:18:27 +03:00
|
|
|
description:
|
|
|
|
|
"Ask the learner a single question and pause until they answer. NOT graded — there is no correct answer. " +
|
|
|
|
|
"Use when requirements are ambiguous, preferences are needed, a decision would materially affect the lesson, " +
|
|
|
|
|
"or you need an open-ended response. Ask exactly one question per tool call; after calling, end your turn and wait.",
|
2026-09-12 02:34:18 +03:00
|
|
|
schema: askInputSchema,
|
|
|
|
|
};
|
2026-08-22 02:18:27 +03:00
|
|
|
|
2026-09-11 06:44:20 +03:00
|
|
|
// ─── the prober's question tools (no nodeId — no map exists yet) ─────────────
|
2026-08-22 02:18:27 +03:00
|
|
|
|
2026-09-12 02:34:18 +03:00
|
|
|
export const probeQuizTool: LhTool<any, any> = {
|
|
|
|
|
name: "quiz",
|
|
|
|
|
clientQuestion: true,
|
2026-09-11 06:44:20 +03:00
|
|
|
description:
|
|
|
|
|
"Ask the learner a GRADED question with a known correct answer, then instantly grade and give feedback. " +
|
|
|
|
|
"Unlike ask_user_question (which collects preferences/decisions with no right answer), quiz always has a " +
|
|
|
|
|
"correct answer supplied by you, marks the learner's selection right/wrong, reveals the correct answer, and " +
|
|
|
|
|
"can show an explanation. Use it to locate the edge of his understanding: bracket each strand the lesson will " +
|
|
|
|
|
"depend on — something he gets right (a floor), something he gets wrong or doesn't know (a ceiling). " +
|
|
|
|
|
"Options-only: single-select or multi-select, plus an automatic 'I don't know' choice so the learner can " +
|
|
|
|
|
"signal a genuine gap instead of guessing. An always-present optional note field lets the learner attach a " +
|
|
|
|
|
"free-text note to ANY answer; it reaches you only when non-empty. No free-text answers — for non-graded " +
|
|
|
|
|
"questions use ask_user_question instead. Ask exactly one question per tool call; after calling, end your " +
|
|
|
|
|
"turn and wait for the graded result.",
|
2026-09-12 02:34:18 +03:00
|
|
|
schema: probeQuizInputSchema,
|
|
|
|
|
};
|
2026-09-11 06:44:20 +03:00
|
|
|
|
2026-09-22 16:22:57 +03:00
|
|
|
/** The prober's registry context: the probe's own needs (probe_done —
|
|
|
|
|
* ProbeDoneContext) PLUS the job-dispatch wiring, since the owner ruling
|
|
|
|
|
* (2026-09-20) puts research + pdf in the probe phase's registry too. */
|
|
|
|
|
export interface ProberToolContext extends ProbeDoneContext {
|
|
|
|
|
/** Creates the job record (deduped by toolCallId). */
|
|
|
|
|
jobStore: JobStore;
|
|
|
|
|
/** Fire the background runner for a freshly created job. */
|
|
|
|
|
spawn: (job: JobRecord, input: ResearchInput | DemoInput | VisualInput | PdfInput) => void;
|
|
|
|
|
/** Search availability — the research dispatch tool's honest-disable
|
|
|
|
|
* signal (same gate as the teacher's). Absent = available. */
|
|
|
|
|
searchAvailable?: () => boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The prober's tool set: probe quiz (no nodeId) + ask + the learner-fs
|
|
|
|
|
* set + probe_done + the two phase-omnipresent job dispatch tools.
|
2026-09-19 14:06:07 +03:00
|
|
|
* `enabled` is the resolved settings seam (core/pending.ts): any tool
|
|
|
|
|
* explicitly false is dropped from the registry. probe_done stays
|
|
|
|
|
* always-on regardless — it is how the phase ENDS; a prober that can
|
|
|
|
|
* never conclude is a lockout. */
|
|
|
|
|
export function createProberTools(
|
2026-09-22 16:22:57 +03:00
|
|
|
ctx: ProberToolContext,
|
2026-09-19 14:06:07 +03:00
|
|
|
enabled?: EnabledTools,
|
|
|
|
|
): Record<string, LhTool<any, any>> {
|
|
|
|
|
// probe_done is re-added AFTER the filter, always-on by construction — it
|
|
|
|
|
// is how the phase ENDS; a prober that can never conclude is a lockout.
|
|
|
|
|
const done = probeDoneTool(ctx);
|
2026-09-22 16:22:57 +03:00
|
|
|
// The job tools build over the SAME context/gates as the teacher's
|
|
|
|
|
// (one builder, one gating semantic); the prober carries a SUBSET:
|
|
|
|
|
const jobs = jobTools({
|
|
|
|
|
chatId: ctx.chatId,
|
|
|
|
|
jobStore: ctx.jobStore,
|
|
|
|
|
spawn: ctx.spawn,
|
|
|
|
|
searchAvailable: ctx.searchAvailable,
|
|
|
|
|
});
|
2026-09-11 06:44:20 +03:00
|
|
|
return {
|
2026-09-19 14:06:07 +03:00
|
|
|
...filterEnabledTools(
|
|
|
|
|
{
|
|
|
|
|
quiz: probeQuizTool,
|
|
|
|
|
ask_user_question: askUserQuestionTool,
|
2026-09-19 16:50:20 +03:00
|
|
|
// The learner-filesystem set is always-on (user-decided 2026-09,
|
|
|
|
|
// sources v1): the prober inspects and LOCATES learner-referenced
|
|
|
|
|
// files while assessing the edge. Read-only by construction.
|
2026-09-19 15:24:26 +03:00
|
|
|
read: readTool,
|
2026-09-19 16:50:20 +03:00
|
|
|
ls: lsTool,
|
|
|
|
|
find: findTool,
|
|
|
|
|
grep: grepTool,
|
2026-09-22 16:22:57 +03:00
|
|
|
// Job dispatch in the probe phase (owner ruling, 2026-09-20):
|
|
|
|
|
// research + pdf are ALWAYS available in every phase — the
|
|
|
|
|
// prober can verify facts or read learner PDFs while assessing.
|
|
|
|
|
// demo/visual stay teach-phase-only (they need the lesson's plan
|
|
|
|
|
// context). Same gating as the teacher: toggle-filtered here,
|
|
|
|
|
// research subject to the searchAvailable gate (honestly absent
|
|
|
|
|
// when no engine is live).
|
|
|
|
|
...(jobs.research ? { research: jobs.research } : {}),
|
|
|
|
|
pdf: jobs.pdf!,
|
2026-09-19 14:06:07 +03:00
|
|
|
...done,
|
|
|
|
|
},
|
|
|
|
|
enabled,
|
|
|
|
|
),
|
|
|
|
|
...done,
|
2026-09-11 06:44:20 +03:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── the teacher's question tools (quiz carries the position field) ──────────
|
|
|
|
|
|
|
|
|
|
/** Build the teach-side quiz tool for one request (exported for tests): the authoring contract
|
|
|
|
|
* + the per-session plan-nodeId validation (chained superRefines — the
|
|
|
|
|
* grading contract is untouched). Reads the LIVE chat record, so rewrites
|
|
|
|
|
* mid-turn are honored, and with no tree yet every nodeId is rejected.
|
|
|
|
|
* Omitting nodeId is legal — an ad-hoc check that moves nothing. */
|
|
|
|
|
export function teachQuizToolFor(ctx: Pick<PlanToolContext, "chatId" | "chatStore">) {
|
|
|
|
|
const inputSchema = quizInputObject
|
2026-09-09 03:04:16 +03:00
|
|
|
.superRefine((input, add) => {
|
|
|
|
|
for (const problem of quizInputProblems(input)) {
|
|
|
|
|
add.addIssue({ code: "custom", message: problem });
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.superRefine((input, add) => {
|
|
|
|
|
if (typeof input.nodeId !== "string" || input.nodeId.length === 0) return;
|
|
|
|
|
const record = ctx.chatStore.getChat(ctx.chatId);
|
2026-09-11 06:44:20 +03:00
|
|
|
const known = record?.tree?.nodes.map((n) => n.id) ?? [];
|
2026-09-09 03:04:16 +03:00
|
|
|
if (!known.includes(input.nodeId)) {
|
|
|
|
|
add.addIssue({
|
|
|
|
|
code: "custom",
|
|
|
|
|
message:
|
2026-09-11 06:44:20 +03:00
|
|
|
`nodeId "${input.nodeId}" matches no node in this session's plan map. ` +
|
2026-09-09 03:04:16 +03:00
|
|
|
(known.length > 0
|
|
|
|
|
? `Copy one of your plan_init slugs exactly (${known.slice(0, 5).join(", ")}${known.length > 5 ? ", …" : ""}), or omit nodeId for an ad-hoc check.`
|
|
|
|
|
: "No plan map exists yet — omit nodeId (ad-hoc checks only until you call plan_init)."),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-12 02:34:18 +03:00
|
|
|
return lhTool({
|
|
|
|
|
name: "quiz",
|
|
|
|
|
clientQuestion: true,
|
2026-09-11 06:44:20 +03:00
|
|
|
description:
|
|
|
|
|
"Ask the learner a GRADED question with a known correct answer, then instantly grade and give feedback. " +
|
|
|
|
|
"Unlike ask_user_question (which collects preferences/decisions with no right answer), quiz always has a " +
|
|
|
|
|
"correct answer supplied by you, marks the learner's selection right/wrong, reveals the correct answer, and " +
|
|
|
|
|
"can show an explanation. Use it to (1) confirm a taught node actually landed, or (2) probe understanding " +
|
|
|
|
|
"whenever you're unsure. Set `nodeId` whenever the question targets a map node — the lesson's position on the " +
|
|
|
|
|
"map derives from those tags; omit it only for quick ad-hoc checks. Options-only: single-select or multi-select, " +
|
|
|
|
|
"plus an automatic 'I don't know' choice so the learner can signal a genuine gap instead of guessing. " +
|
|
|
|
|
"An always-present optional note field lets the learner attach a free-text note to ANY answer; it reaches you " +
|
|
|
|
|
"only when non-empty. No free-text answers — for non-graded questions use ask_user_question instead. " +
|
|
|
|
|
"Ask exactly one question per tool call; after calling, end your turn and wait for the graded result.",
|
2026-09-12 02:34:18 +03:00
|
|
|
schema: inputSchema,
|
2026-09-09 03:04:16 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 19:15:43 +03:00
|
|
|
export interface TeacherToolContext extends PlanToolContext {
|
|
|
|
|
/** Job-tool wiring (sub-agent dispatch): the job store + the runner's
|
|
|
|
|
* spawn seam. The composition root injects both. */
|
|
|
|
|
jobStore: JobStore;
|
|
|
|
|
/** Fire the background runner for a freshly created job. The tool input
|
|
|
|
|
* rides along — the spawn fires mid-stream, before the tool part is
|
|
|
|
|
* persisted to the transcript (a transcript scan would race it). */
|
2026-09-22 16:22:57 +03:00
|
|
|
spawn: (job: JobRecord, input: ResearchInput | DemoInput | VisualInput | PdfInput) => void;
|
2026-09-22 13:55:01 +03:00
|
|
|
/** Search availability (search port wave, 2026-09-20): the research
|
|
|
|
|
* dispatch tool's honest-disable signal — no live engine = the tool
|
|
|
|
|
* drops from this registry (JobToolContext.searchAvailable). */
|
|
|
|
|
searchAvailable?: () => boolean;
|
2026-08-22 09:06:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-09-11 06:44:20 +03:00
|
|
|
* Per-request teacher tool registry: teach questions (validated nodeId) +
|
|
|
|
|
* plan tools + job tools, all closing over the ctx.
|
2026-09-19 14:06:07 +03:00
|
|
|
*
|
|
|
|
|
* `enabled` is the resolved settings seam (core/pending.ts EnabledTools —
|
|
|
|
|
* Platform's chat-handler wiring passes it through from settings). Today
|
|
|
|
|
* only the sub-agent tools (research/demo/visual) are user-toggleable, but
|
|
|
|
|
* the filtering is GENERIC: any name explicitly false is dropped, so
|
|
|
|
|
* future toggleables (question-tool variants, …) need no change here.
|
|
|
|
|
* Absent/undefined = enabled — tests and old records see today's set.
|
2026-08-22 09:06:24 +03:00
|
|
|
*/
|
2026-09-19 14:06:07 +03:00
|
|
|
export function createTeacherTools(
|
|
|
|
|
ctx: TeacherToolContext,
|
|
|
|
|
enabled?: EnabledTools,
|
|
|
|
|
): Record<string, LhTool<any, any>> {
|
|
|
|
|
return filterEnabledTools(
|
|
|
|
|
{
|
|
|
|
|
quiz: teachQuizToolFor(ctx),
|
|
|
|
|
ask_user_question: askUserQuestionTool,
|
2026-09-19 16:50:20 +03:00
|
|
|
// The learner-filesystem set is always-on (user-decided 2026-09,
|
|
|
|
|
// sources v1): the teacher locates and reads learner-referenced
|
|
|
|
|
// files on its own terms — nothing auto-injects. Read-only by
|
|
|
|
|
// construction (fs-shared.ts).
|
2026-09-19 15:24:26 +03:00
|
|
|
read: readTool,
|
2026-09-19 16:50:20 +03:00
|
|
|
ls: lsTool,
|
|
|
|
|
find: findTool,
|
|
|
|
|
grep: grepTool,
|
2026-09-19 14:06:07 +03:00
|
|
|
...planTools(ctx),
|
2026-09-22 16:22:57 +03:00
|
|
|
// Fix wave (2026-09-20): searchAvailable was RECEIVED here but
|
|
|
|
|
// DROPPED when building the inner JobToolContext — the drop-gate
|
|
|
|
|
// always took its ?? true default. Threaded now: the live signal
|
|
|
|
|
// reaches jobTools' research gate.
|
|
|
|
|
...jobTools({ chatId: ctx.chatId, jobStore: ctx.jobStore, spawn: ctx.spawn, searchAvailable: ctx.searchAvailable }),
|
2026-09-19 14:06:07 +03:00
|
|
|
},
|
|
|
|
|
enabled,
|
|
|
|
|
);
|
2026-08-22 09:06:24 +03:00
|
|
|
}
|