feat(models): BYOK all the way down — factory pins deleted, per-need refusal gates
Owner decision 2026-09-22: 'it doesn't make sense to have any default
model configured anywhere; the application is bring your own keys.'
config.ts: MODELS deleted; ModelRole becomes an explicit 9-role union;
DEFAULT_THINKING_ORDER (medium, low, high, minimal, xhigh, max) bakes
the unset-level default — first entry the model supports; explicitly
set levels keep pi's native nearest-clamp.
core/settings-resolution: the factory layer dissolves — the chain is
role override → slot → NOTHING. resolveSettings(record) takes no
factory. Empty chains resolve to null/undefined; a thinkingLevel-only
slot is inert (stored, never run). resolveConversationSpec: chat.model
override over the strong slot, null when both empty. resolveRoleSpec
spans all nine roles for the job/extraction engines.
Refusal envelope (core/settings.ts): modelsNotConfigured(missing) —
{error, code: 'models_not_configured', missing: [slot tiers]} +
ModelsNotConfiguredError with .envelope. Per-need gates, each honest:
- chat-handler: universal turn gate (new + continuing turns) returns
503 before beginTurn — no liveness, no write-ahead persist, no graph
trigger before the gate fires.
- runner.runJob: unresolvable engine lands the job as error with the
envelope wording; no phantom loop, no result steer.
- extraction: refuseIfStagesUnconfigured throws before any stage
(turn-service info-skips so coverage retries next turn; the import
route surfaces per-file code+missing); labeler-less runs skip
cluster naming with a console line and still merge.
Write side: slot entries must carry a model — level-only slot writes
are 400s (absent slot key = clear). piEngineForSpec resolves unset
thinking levels via DEFAULT_THINKING_ORDER; piEngineFor now throws
honestly; thinkingLevelFor/modelIdFor deleted; teacher-agent, pi-run,
visual-maker, pdf-reader all read engine.level.
model-catalog: defaults/slots/rolePins null when the chain is empty
(contracts nullable) — no phantom ids in payloads. Stores drop
MODEL_PINS entirely.
Tests: pin-leaners converted to explicit injection (per-chat model
pins on scripted chats — exactly what the setup flow writes; faux
engines for agent-construction tests). New coverage: the 503 gate
envelope, the runner refusal, the extraction refusal + labeler skip.
Gates: typecheck, lint, 815/815, build.
This commit is contained in:
+40
-32
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
clampThinkingLevel,
|
||||
createModels,
|
||||
getSupportedThinkingLevels,
|
||||
type Api,
|
||||
type Model,
|
||||
type Models,
|
||||
@@ -8,19 +9,18 @@ import {
|
||||
} from "@earendil-works/pi-ai";
|
||||
import { builtinProviders } from "@earendil-works/pi-ai/providers/all";
|
||||
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
||||
import { MODELS, type ModelRole } from "../../config.ts";
|
||||
import type { ChatSettings } from "../core/contracts/chat.ts";
|
||||
import { DEFAULT_THINKING_ORDER, type ModelRole } from "../../config.ts";
|
||||
|
||||
/**
|
||||
* The one engine factory every agent's model comes from (the pi-ai swap,
|
||||
* docs/pi-engine-swap.md). Model choice and thinking budget live in
|
||||
* config.ts (MODELS) — the single tuning table; this module only wires them
|
||||
* to pi-ai's OpenRouter provider.
|
||||
* docs/pi-engine-swap.md). BYOK ALL THE WAY DOWN (owner, 2026-09-22):
|
||||
* there are no factory pins — every engine resolves from a RESOLVED SPEC
|
||||
* (the settings chain's output) and the one builder here wires it to
|
||||
* pi-ai's federated catalogs.
|
||||
*
|
||||
* An "engine" is the model + the stream function the agent loop runs on —
|
||||
* the two things a test fake replaces. Production engines resolve the model
|
||||
* from pi-ai's static catalog (glm-5.3-flash and qwen3.8-max are both in
|
||||
* 0.85.1's built-ins — no overlay fetch) and stream through the same Models
|
||||
* from pi-ai's static catalogs and stream through the same Models
|
||||
* collection.
|
||||
*/
|
||||
|
||||
@@ -34,12 +34,6 @@ export interface PiEngine {
|
||||
|
||||
export type { ThinkingLevel };
|
||||
|
||||
/** The role's config thinking level (config's scale IS pi's since the
|
||||
* model-control wave — no mapping needed). */
|
||||
export function thinkingLevelFor(role: ModelRole): ThinkingLevel {
|
||||
return MODELS[role].effort;
|
||||
}
|
||||
|
||||
let collection: ReturnType<typeof createModels> | undefined;
|
||||
|
||||
/** The process-wide pi-ai Models collection — EVERY builtin provider's
|
||||
@@ -63,14 +57,26 @@ export function openRouterApiKey(): string | undefined {
|
||||
return process.env.OPENROUTER_API_KEY || undefined;
|
||||
}
|
||||
|
||||
/** A fully-resolved model pin — the shape every engine now comes from
|
||||
* (model slots wave, 2026-09-20): the settings-resolution chain's output.
|
||||
* Every field concrete; the CALLER resolved the chain (role override →
|
||||
* slot → factory pin, or the chat's conversation chain). */
|
||||
/** A fully-resolved model pin — the shape every engine now comes from:
|
||||
* the settings-resolution chain's output (role override → slot, or the
|
||||
* chat's conversation chain). thinkingLevel is OPTIONAL: unset means
|
||||
* the baked default resolves here (owner 2026-09-22) — the first entry
|
||||
* of DEFAULT_THINKING_ORDER the model supports. */
|
||||
export interface ResolvedModelSpec {
|
||||
provider: string;
|
||||
id: string;
|
||||
thinkingLevel: ThinkingLevel;
|
||||
thinkingLevel?: ThinkingLevel;
|
||||
}
|
||||
|
||||
/** The first entry of the baked default order the model supports (owner,
|
||||
* 2026-09-22). A non-reasoning model reports exactly ["off"], so the
|
||||
* order walk lands there naturally. */
|
||||
function defaultThinkingLevel(model: Model<Api>): ThinkingLevel {
|
||||
const supported = getSupportedThinkingLevels(model);
|
||||
for (const level of DEFAULT_THINKING_ORDER) {
|
||||
if (supported.includes(level)) return level;
|
||||
}
|
||||
return supported[0] ?? "off";
|
||||
}
|
||||
|
||||
/** The production engine for a RESOLVED spec (model slots wave,
|
||||
@@ -84,7 +90,12 @@ export function piEngineForSpec(spec: ResolvedModelSpec): PiEngine {
|
||||
if (!model) {
|
||||
throw new Error(`${spec.provider} catalog has no model ${spec.id}`);
|
||||
}
|
||||
const level = clampThinkingLevel(model, spec.thinkingLevel);
|
||||
// UNSET level → the baked default order (first entry the model
|
||||
// supports); SET level → pi's native nearest-clamp (model swaps keep
|
||||
// the user's intent and adapt honestly).
|
||||
const level = spec.thinkingLevel
|
||||
? clampThinkingLevel(model, spec.thinkingLevel)
|
||||
: defaultThinkingLevel(model);
|
||||
return {
|
||||
model,
|
||||
level,
|
||||
@@ -99,17 +110,14 @@ export function piEngineForSpec(spec: ResolvedModelSpec): PiEngine {
|
||||
};
|
||||
}
|
||||
|
||||
/** The production engine for one agent role — the LEGACY fallback path:
|
||||
* the chat's model-control override over config.ts's pin. Production
|
||||
* callers (chat-handler) now resolve the full chain (chat override →
|
||||
* strong slot → factory pin) and call piEngineForSpec; this stays for
|
||||
* direct config-pin fallbacks (sub-agents without an injected engine —
|
||||
* the job runner injects one — and tests). */
|
||||
export function piEngineFor(role: ModelRole, settings?: ChatSettings): PiEngine {
|
||||
const spec = MODELS[role];
|
||||
return piEngineForSpec({
|
||||
provider: settings?.model?.provider ?? spec.provider,
|
||||
id: settings?.model?.id ?? spec.model,
|
||||
thinkingLevel: settings?.thinkingLevel ?? thinkingLevelFor(role),
|
||||
});
|
||||
/** The LEGACY role entry point — BYOK made it an honest THROWER (owner,
|
||||
* 2026-09-22): no factory pins exist to fall back to, and this module
|
||||
* cannot read settings (architecture law — the agent layer never
|
||||
* imports the server layer). Production resolves the chain in
|
||||
* chat-handler and ALWAYS injects engines; reaching this means a
|
||||
* wiring bug, so it fails loudly instead of guessing a model. */
|
||||
export function piEngineFor(role: ModelRole): PiEngine {
|
||||
throw new Error(
|
||||
`no configured model for role "${role}" — set your model slots (settings → models)`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import type { Usage } from "@earendil-works/pi-ai";
|
||||
import { z } from "zod";
|
||||
import { lhTool, toPiTools } from "../lh-tool.ts";
|
||||
import type { ModelRole } from "../../../config.ts";
|
||||
import { ModelsNotConfiguredError, ROLE_SLOT, type ModelSlotName } from "../../core/settings.ts";
|
||||
import type { PiEngine } from "../models.ts";
|
||||
import { runSubagent } from "./pi-run.ts";
|
||||
import type {
|
||||
@@ -562,11 +563,12 @@ export interface ExtractionModels {
|
||||
auditor?: PiEngine;
|
||||
/** Test seam for the cluster namer (post-extraction garnish). */
|
||||
labeler?: PiEngine;
|
||||
/** Settings-resolved engine per role (model slots wave, 2026-09-20) —
|
||||
* the same slot treatment the job runner got: the role-override →
|
||||
* slot → factory-pin chain, read FRESH per stage call (a settings PUT
|
||||
* between stages lands). Absent → config pins, unchanged. */
|
||||
engineFor?: (role: ModelRole) => PiEngine;
|
||||
/** Settings-resolved engine per role — the same slot treatment the
|
||||
* job runner got, read FRESH per stage call (a settings PUT between
|
||||
* stages lands). BYOK (owner, 2026-09-22): the chain (role override →
|
||||
* slot → NOTHING) may end EMPTY → undefined; the front doors refuse
|
||||
* with ModelsNotConfiguredError before any stage runs. */
|
||||
engineFor?: (role: ModelRole) => PiEngine | undefined;
|
||||
}
|
||||
|
||||
export interface ExtractionPipelineDeps extends GraphDeps, ExtractionModels {
|
||||
@@ -615,13 +617,24 @@ export function callUsage(usage: Usage): CallUsage {
|
||||
|
||||
export function createExtractionPipeline(deps: ExtractionPipelineDeps): ExtractionPipeline {
|
||||
const { chatStore, graphStore, embedder: embed } = deps;
|
||||
// Stage-engine resolution (model slots wave, 2026-09-20): a direct test
|
||||
// value wins; otherwise the engineFor chain (role override → slot →
|
||||
// factory pin, resolved FRESH per stage call server-side) — the job
|
||||
// runner's exact treatment, closing the flagged gap where extraction
|
||||
// stayed config-pin-bound.
|
||||
// Stage-engine resolution: a direct test value wins; otherwise the
|
||||
// engineFor chain (role override → slot → nothing, resolved FRESH per
|
||||
// stage call server-side) — the job runner's exact treatment. undefined
|
||||
// = the BYOK chain came up empty; the front-door guards turn that into
|
||||
// an honest refusal.
|
||||
const stageEngine = (role: ModelRole, override?: PiEngine): PiEngine | undefined =>
|
||||
override ?? deps.engineFor?.(role);
|
||||
/** The per-need refusal guard: which slot tiers the pipeline's stages
|
||||
* found unset (test values count as configured — they ARE engines). */
|
||||
function refuseIfStagesUnconfigured(): void {
|
||||
const missing = new Set<ModelSlotName>();
|
||||
if (!stageEngine("extractor", deps.extractor)) missing.add(ROLE_SLOT.extractor);
|
||||
if (!stageEngine("auditor", deps.auditor)) missing.add(ROLE_SLOT.auditor);
|
||||
if (missing.size > 0) {
|
||||
console.info(`[extraction] refused — no model configured for: ${[...missing].join(", ")}`);
|
||||
throw new ModelsNotConfiguredError([...missing]);
|
||||
}
|
||||
}
|
||||
// THE SHARED REGISTRIES (runs() above — globalThis): every pipeline
|
||||
// instance (route entries in dev, tests) sees the same runs, the same
|
||||
// single-flight guard. No module-level state left in this factory.
|
||||
@@ -743,6 +756,10 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti
|
||||
try {
|
||||
const record = chatStore.getChat(chatId);
|
||||
if (!record) throw new Error(`Chat not found: ${chatId}`);
|
||||
// BYOK per-need gate (owner, 2026-09-22): the write channel needs
|
||||
// extractor + auditor engines; an empty chain refuses BEFORE any
|
||||
// stage runs (typed — the trigger skips, routes surface 503).
|
||||
refuseIfStagesUnconfigured();
|
||||
const covered = coverageOf(record.messages as UIMessage[], graphStore.readCoverage(), chatId);
|
||||
if (covered.uncovered.length === 0) {
|
||||
console.info(`[extraction ${chatId}] nothing uncovered — no-op`);
|
||||
@@ -811,11 +828,16 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti
|
||||
);
|
||||
// Post-extraction garnish (DECIDED 2026-09-13): name new/changed
|
||||
// clusters. Fire-and-forget — naming never fails the run.
|
||||
refreshClusterLabels({
|
||||
graphStore,
|
||||
engine: stageEngine("labeler", deps.labeler),
|
||||
signal: runningExtractions.get(chatId)?.controller.signal,
|
||||
}).catch(() => {});
|
||||
const labelerEngine = stageEngine("labeler", deps.labeler);
|
||||
if (labelerEngine) {
|
||||
refreshClusterLabels({
|
||||
graphStore,
|
||||
engine: labelerEngine,
|
||||
signal: runningExtractions.get(chatId)?.controller.signal,
|
||||
}).catch(() => {});
|
||||
} else {
|
||||
console.info(`[extraction ${chatId}] cluster naming skipped — no labeler model configured`);
|
||||
}
|
||||
return stats;
|
||||
} catch (err) {
|
||||
persistRunUsage(chatId, Math.round(performance.now() - t0), batchesDone > 0 ? total : undefined);
|
||||
@@ -908,6 +930,9 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti
|
||||
inFlight.add(id);
|
||||
const t0 = performance.now();
|
||||
try {
|
||||
// BYOK per-need gate (owner, 2026-09-22): same refusal as the chat
|
||||
// front door — no extractor/auditor engines, no run.
|
||||
refuseIfStagesUnconfigured();
|
||||
console.info(`[extraction ${id}] notes pipeline start (${input.filePath})`);
|
||||
setPhase(id, "extracting", "notes", input.filePath);
|
||||
const findings = await notesExtract(input);
|
||||
@@ -933,11 +958,16 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti
|
||||
`[extraction ${id}] merged: ${stats.nodes} nodes, ${stats.edges} edges, ${stats.events} events ` +
|
||||
`(${Math.round(performance.now() - t0)}ms total)`,
|
||||
);
|
||||
refreshClusterLabels({
|
||||
graphStore,
|
||||
engine: stageEngine("labeler", deps.labeler),
|
||||
signal: runningExtractions.get(id)?.controller.signal,
|
||||
}).catch(() => {});
|
||||
const labelerEngine = stageEngine("labeler", deps.labeler);
|
||||
if (labelerEngine) {
|
||||
refreshClusterLabels({
|
||||
graphStore,
|
||||
engine: labelerEngine,
|
||||
signal: runningExtractions.get(id)?.controller.signal,
|
||||
}).catch(() => {});
|
||||
} else {
|
||||
console.info(`[extraction ${id}] cluster naming skipped — no labeler model configured`);
|
||||
}
|
||||
return stats;
|
||||
} catch (err) {
|
||||
persistRunUsage(id, Math.round(performance.now() - t0), undefined);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { AssistantMessage, Message, ToolResultMessage, Usage } from "@earendil-works/pi-ai";
|
||||
import { z } from "zod";
|
||||
import { lhTool, piToolDeclarations, type LhTool } from "../lh-tool.ts";
|
||||
import { piEngineFor, thinkingLevelFor, type PiEngine } from "../models.ts";
|
||||
import { piEngineFor, type PiEngine, type ThinkingLevel } from "../models.ts";
|
||||
import type { PdfResult, PdfToolInput } from "../../core/contracts.ts";
|
||||
import { getHeadless, type HeadlessApi } from "./headless.ts";
|
||||
import { pdfExtract, pdfInfo, pdfProbe, pdfSearch, type PdfSearchHit } from "./pdf-ops.ts";
|
||||
@@ -181,7 +181,9 @@ export async function runPdfReaderAgent(
|
||||
const tools = createPdfTools(pdfPath);
|
||||
const toolDeclarations = piToolDeclarations(tools);
|
||||
const engine = opts.engine ?? piEngineFor("pdfReader");
|
||||
const reasoning = thinkingLevelFor("pdfReader");
|
||||
// BYOK (owner, 2026-09-22): the level rides the injected engine — no
|
||||
// factory pin to consult (engine.level is the resolved/clamped value).
|
||||
const reasoning = engine.level;
|
||||
const prompt = [
|
||||
`PDF path: ${pdfPath}`,
|
||||
`Question (what the teacher needs from this document): ${input.question || "(no question — give a structural overview)"}`,
|
||||
@@ -392,11 +394,11 @@ async function streamOnce(
|
||||
systemPrompt: string,
|
||||
messages: Message[],
|
||||
tools: ReturnType<typeof piToolDeclarations>,
|
||||
reasoning: ReturnType<typeof thinkingLevelFor>,
|
||||
reasoning: ThinkingLevel | undefined,
|
||||
abortSignal?: AbortSignal,
|
||||
): Promise<AssistantMessage> {
|
||||
const stream = engine.streamFn(engine.model, { systemPrompt, messages, tools }, {
|
||||
reasoning: reasoning === "off" ? undefined : reasoning,
|
||||
reasoning: reasoning && reasoning !== "off" ? reasoning : undefined,
|
||||
apiKey: process.env.OPENROUTER_API_KEY || undefined,
|
||||
signal: abortSignal,
|
||||
} as never);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Agent, type AgentEvent, type AgentMessage, type AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import type { AssistantMessage, ToolResultMessage, Usage } from "@earendil-works/pi-ai";
|
||||
import { openRouterApiKey, piEngineFor, thinkingLevelFor, type PiEngine } from "../models.ts";
|
||||
import { openRouterApiKey, piEngineFor, type PiEngine } from "../models.ts";
|
||||
import type { ModelRole } from "../../../config.ts";
|
||||
|
||||
/**
|
||||
@@ -47,6 +47,9 @@ export interface SubagentRunResult {
|
||||
}
|
||||
|
||||
export function subagentEngine(role: ModelRole, override?: PiEngine): PiEngine {
|
||||
// BYOK (owner, 2026-09-22): no factory pins — the override is the only
|
||||
// real path (the runner injects settings-resolved engines); a missed
|
||||
// injection hits the honest thrower in models.ts instead of a guess.
|
||||
return override ?? piEngineFor(role);
|
||||
}
|
||||
|
||||
@@ -57,7 +60,7 @@ export async function runSubagent(tools: AgentTool<any, any>[], opts: SubagentRu
|
||||
initialState: {
|
||||
systemPrompt: opts.systemPrompt,
|
||||
model: engine.model,
|
||||
thinkingLevel: thinkingLevelFor(opts.role),
|
||||
thinkingLevel: engine.level,
|
||||
tools,
|
||||
messages: opts.messages ?? [],
|
||||
},
|
||||
|
||||
@@ -20,7 +20,8 @@ import {
|
||||
type VisualToolInput,
|
||||
} from "../../core/contracts.ts";
|
||||
import { registerJobAbort, unregisterJobAbort } from "./aborts.ts";
|
||||
import { MODELS, type ModelRole } from "../../../config.ts";
|
||||
import type { ModelRole } from "../../../config.ts";
|
||||
import { ROLE_SLOT, modelsNotConfigured } from "../../core/settings.ts";
|
||||
import type { ChatStore, JobStore } from "../../core/ports.ts";
|
||||
import { runDemoBuilder } from "./demo-builder.ts";
|
||||
import { runPdfReaderAgent } from "./pdf-reader.ts";
|
||||
@@ -76,7 +77,10 @@ export interface JobRunnerDeps {
|
||||
* old gap where per-role settings overrides never reached job engines.
|
||||
* Test overrides (below) still win; absent → config pins.
|
||||
*/
|
||||
engineFor?: (role: ModelRole) => PiEngine;
|
||||
/** Settings-resolved engine per role — BYOK (owner, 2026-09-22): the
|
||||
* chain (role override → slot → NOTHING) may end empty → undefined;
|
||||
* runJob then refuses the spawn honestly instead of guessing. */
|
||||
engineFor?: (role: ModelRole) => PiEngine | undefined;
|
||||
/** The search seam (search port wave, 2026-09-20): resolves the LIVE
|
||||
* provider fresh per job — server/search.ts composition injected by
|
||||
* the wiring; null = honestly unavailable (the researcher's web_search
|
||||
@@ -114,20 +118,6 @@ const KIND_ROLE: Record<Exclude<JobKind, "graph-context">, ModelRole> = {
|
||||
pdf: "pdfReader",
|
||||
};
|
||||
|
||||
/** The config-pin model stamp for a job kind — JobUsage.model when no
|
||||
* engine was resolved (engineFor absent AND no test override). Stamps are
|
||||
* provider-qualified ("provider:id") since the model slots wave, matching
|
||||
* the price table's uniform keys. Context-injection jobs make NO model
|
||||
* call ("" — onUsage never fires for them; the kind is deliberately
|
||||
* absent from the runners below). */
|
||||
const KIND_MODEL: Record<JobKind, string> = {
|
||||
research: `${MODELS.researcher.provider}:${MODELS.researcher.model}`,
|
||||
demo: `${MODELS.demoBuilder.provider}:${MODELS.demoBuilder.model}`,
|
||||
visual: `${MODELS.visualMaker.provider}:${MODELS.visualMaker.model}`,
|
||||
pdf: `${MODELS.pdfReader.provider}:${MODELS.pdfReader.model}`,
|
||||
"graph-context": "",
|
||||
};
|
||||
|
||||
export function createJobRunner(deps: JobRunnerDeps): JobRunner {
|
||||
const { chatStore, jobStore, deliver } = deps;
|
||||
|
||||
@@ -135,10 +125,10 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
|
||||
* then writes through the job store so the poll sees progress). */
|
||||
const usageTotals = new Map<string, JobUsage>();
|
||||
|
||||
function makeLoopContext(jobId: string, kind: JobKind, engine?: PiEngine): LoopContext {
|
||||
// Provider-qualified stamp (model slots wave): the engine's actual
|
||||
// model, else the legacy config-pin id (see KIND_MODEL).
|
||||
const model = engine ? `${engine.model.provider}:${engine.model.id}` : KIND_MODEL[kind];
|
||||
function makeLoopContext(jobId: string, engine: PiEngine): LoopContext {
|
||||
// Provider-qualified stamp: the engine's actual model — the only
|
||||
// thing that ran (BYOK: no config-pin fallback stamps exist).
|
||||
const model = `${engine.model.provider}:${engine.model.id}`;
|
||||
// Real-kill wiring (2026-09 abort semantics): the controller is what
|
||||
// the abort endpoint fires (agent/subagents/aborts.ts registry); the
|
||||
// signal rides into every model call of the loop.
|
||||
@@ -171,8 +161,8 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
|
||||
// are kind-correct by construction; transcript fallbacks are cast after).
|
||||
/** The engine one job runs on: test override first (the old seam —
|
||||
* tests swap scripted engines without touching deps), then the injected
|
||||
* settings-resolved engineFor (model slots wave, 2026-09-20), else
|
||||
* undefined → the sub-agent factories fall back to config pins. */
|
||||
* settings-resolved engineFor. undefined = the BYOK chain came up
|
||||
* empty (owner, 2026-09-22) — runJob refuses below, honestly. */
|
||||
function pickEngine(kind: Exclude<JobKind, "graph-context">): PiEngine | undefined {
|
||||
const testOverride: Record<string, { current?: PiEngine } | undefined> = {
|
||||
researcher: researchModelOverride,
|
||||
@@ -185,7 +175,7 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
|
||||
|
||||
const KIND_RUNNERS: Record<
|
||||
Exclude<JobKind, "graph-context">,
|
||||
(jobId: string, engine: PiEngine | undefined, input: JobToolInput | undefined, loop: LoopContext) => Promise<void>
|
||||
(jobId: string, engine: PiEngine, input: JobToolInput | undefined, loop: LoopContext) => Promise<void>
|
||||
> = {
|
||||
research: (jobId, engine, input, loop) => runResearchJob(jobId, engine, input, loop),
|
||||
demo: (jobId, engine, input, loop) => runDemoJob(jobId, engine, input, loop),
|
||||
@@ -212,9 +202,23 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner {
|
||||
jobStore.updateJob(jobId, { status: "error", error: `unknown kind ${job.kind}`, activity: "failed" });
|
||||
return;
|
||||
}
|
||||
const engine = pickEngine(job.kind);
|
||||
if (!engine) {
|
||||
// BYOK per-need refusal (owner, 2026-09-22): this kind's tier has
|
||||
// no model — the job record surfaces the honest error (dock +
|
||||
// teacher read it), and no engine-less loop ever starts.
|
||||
const tier = ROLE_SLOT[KIND_ROLE[job.kind]];
|
||||
const envelope = modelsNotConfigured([tier]);
|
||||
console.info(`[job ${jobId}] refused — ${JSON.stringify(envelope)}`);
|
||||
jobStore.updateJob(jobId, {
|
||||
status: "error",
|
||||
error: `${envelope.error} (missing: ${tier})`,
|
||||
activity: "failed",
|
||||
});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const engine = pickEngine(job.kind);
|
||||
await run(jobId, engine, input, makeLoopContext(jobId, job.kind, engine));
|
||||
await run(jobId, engine, input, makeLoopContext(jobId, engine));
|
||||
} finally {
|
||||
unregisterJobAbort(jobId);
|
||||
// The last usage write may have been mid-flight when the loop died;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { AssistantMessage, Message, ToolResultMessage, Usage } from "@earendil-works/pi-ai";
|
||||
import { z } from "zod";
|
||||
import { lhTool, piToolDeclarations, type LhTool } from "../lh-tool.ts";
|
||||
import { piEngineFor, thinkingLevelFor, type PiEngine } from "../models.ts";
|
||||
import { piEngineFor, type PiEngine, type ThinkingLevel } from "../models.ts";
|
||||
import type { VisualResult } from "../../core/contracts.ts";
|
||||
import { getHeadless, type HeadlessApi } from "./headless.ts";
|
||||
|
||||
@@ -132,7 +132,9 @@ export async function runVisualMaker(input: VisualMakerInput, opts: VisualMakerO
|
||||
const tools = createVisualTools();
|
||||
const toolDeclarations = piToolDeclarations(tools);
|
||||
const engine = opts.engine ?? piEngineFor("visualMaker");
|
||||
const reasoning = thinkingLevelFor("visualMaker");
|
||||
// BYOK (owner, 2026-09-22): the level rides the injected engine — no
|
||||
// factory pin to consult (engine.level is the resolved/clamped value).
|
||||
const reasoning = engine.level;
|
||||
const prompt = [
|
||||
"Brief for ONE precise figure (from the teacher, verbatim — it is everything you know):",
|
||||
brief,
|
||||
@@ -300,11 +302,11 @@ async function streamOnce(
|
||||
systemPrompt: string,
|
||||
messages: Message[],
|
||||
tools: ReturnType<typeof piToolDeclarations>,
|
||||
reasoning: ReturnType<typeof thinkingLevelFor>,
|
||||
reasoning: ThinkingLevel | undefined,
|
||||
abortSignal?: AbortSignal,
|
||||
): Promise<AssistantMessage> {
|
||||
const stream = engine.streamFn(engine.model, { systemPrompt, messages, tools }, {
|
||||
reasoning: reasoning === "off" ? undefined : reasoning,
|
||||
reasoning: reasoning && reasoning !== "off" ? reasoning : undefined,
|
||||
apiKey: process.env.OPENROUTER_API_KEY || undefined,
|
||||
signal: abortSignal,
|
||||
} as never);
|
||||
|
||||
@@ -2,8 +2,7 @@ import { Agent } from "@earendil-works/pi-agent-core";
|
||||
import type { AgentEvent, AgentMessage, AgentTool } from "@earendil-works/pi-agent-core";
|
||||
import type { Usage } from "@earendil-works/pi-ai";
|
||||
import { PATHS } from "../../config.ts";
|
||||
import { MODELS } from "../../config.ts";
|
||||
import { piEngineFor, thinkingLevelFor, openRouterApiKey, type PiEngine } from "./models.ts";
|
||||
import { piEngineFor, openRouterApiKey, type PiEngine } from "./models.ts";
|
||||
import { effectivePromptSegments } from "./prompt-segments.ts";
|
||||
import {
|
||||
askUserQuestionTool,
|
||||
@@ -154,7 +153,7 @@ function baseAgentOptions(kind: AgentKind, opts: AgentBuildOptions, tools: Agent
|
||||
initialState: {
|
||||
systemPrompt,
|
||||
model: engine.model,
|
||||
thinkingLevel: thinkingLevelFor(kind),
|
||||
thinkingLevel: engine.level,
|
||||
tools,
|
||||
messages: opts.messages ?? [],
|
||||
},
|
||||
@@ -227,7 +226,3 @@ export type ProberAgent = ReturnType<typeof createProberAgent>;
|
||||
export type TeacherAgent = ReturnType<typeof createTeacherAgent>;
|
||||
export type TeachingAgent = ProberAgent | TeacherAgent;
|
||||
|
||||
/** The model id a role's engine resolves to (observability). */
|
||||
export function modelIdFor(kind: AgentKind): string {
|
||||
return MODELS[kind].model;
|
||||
}
|
||||
@@ -26,38 +26,46 @@ export interface ModelCatalogEntry {
|
||||
maxTokens: number;
|
||||
}
|
||||
|
||||
/** The conversation agents' config pin (config.ts MODELS prober/teacher —
|
||||
* one controller, so one pin): what a chat without settings runs. */
|
||||
/** The conversation agents' resolved default (BYOK, owner 2026-09-22): the
|
||||
* chat's model override over the STRONG slot — one controller, so one
|
||||
* default. thinkingLevel is optional: unset resolves from the baked
|
||||
* DEFAULT_THINKING_ORDER at engine build. */
|
||||
export interface CatalogDefaults {
|
||||
model: { provider: string; id: string };
|
||||
thinkingLevel: ThinkingLevel;
|
||||
thinkingLevel?: ThinkingLevel;
|
||||
}
|
||||
|
||||
/** One background role's RESOLVED pin (settings interlude, 2026-09-19):
|
||||
* the user's agent-models override falling back to the config.ts MODELS
|
||||
* pin — what that role actually runs today. The settings UI shows each
|
||||
* agent-role row's "Default: <this>" caption from it. */
|
||||
/** One background role's RESOLVED pin (BYOK, owner 2026-09-22): what that
|
||||
* role actually runs today — role override → tier slot → NOTHING. NULL
|
||||
* = the role's chain is empty (the per-need gates refuse its work); the
|
||||
* settings UI shows "not configured" from it. */
|
||||
export type RolePins = Record<
|
||||
BackgroundModelRole,
|
||||
{ model: { provider: string; id: string }; thinkingLevel?: ThinkingLevel }
|
||||
{ model: { provider: string; id: string }; thinkingLevel?: ThinkingLevel } | null
|
||||
>;
|
||||
|
||||
/** One slot tier's RESOLVED pin (model slots wave, 2026-09-20): the
|
||||
* strong/cheap/vision tier's concrete model — slot override falling back
|
||||
* to the factory rep pin. The settings UI captions each slot row with it
|
||||
* and the picker pre-fills from it. */
|
||||
export type SlotPins = Record<ModelSlotName, { model: { provider: string; id: string }; thinkingLevel?: ThinkingLevel }>;
|
||||
/** One slot tier's RESOLVED pin (BYOK, owner 2026-09-22): NULL = the tier
|
||||
* holds no model. The settings UI captions each slot row with it and the
|
||||
* picker pre-fills from it. */
|
||||
export type SlotPins = Record<
|
||||
ModelSlotName,
|
||||
{ model: { provider: string; id: string }; thinkingLevel?: ThinkingLevel } | null
|
||||
>;
|
||||
|
||||
/** GET /api/models response. */
|
||||
export interface ModelCatalogResponse {
|
||||
models: ModelCatalogEntry[];
|
||||
defaults: CatalogDefaults;
|
||||
/** The conversation agents' resolved default — NULL when the
|
||||
* conversation chain is empty (no factory floor; the turn gate
|
||||
* refuses with 503 models_not_configured). */
|
||||
defaults: CatalogDefaults | null;
|
||||
/** The six background roles' resolved pins (same resolution the agent
|
||||
* factories use — src/server/stores.ts effectiveSettings). */
|
||||
* factories use — src/server/stores.ts effectiveSettings); NULL per
|
||||
* role = chain empty. */
|
||||
rolePins: RolePins;
|
||||
/** The three slot tiers' resolved pins (model slots wave, 2026-09-20):
|
||||
* same resolution chain — role override → slot → factory pin — reported
|
||||
* at tier granularity; effectiveSettings().models verbatim. */
|
||||
/** The three slot tiers' resolved pins: role override → slot at tier
|
||||
* granularity; effectiveSettings().models verbatim, NULL per unset
|
||||
* tier. */
|
||||
slots: SlotPins;
|
||||
/** Providers with a PRESENT key, and which env var gates each — the
|
||||
* model slots wave's federation surface (2026-09-20): the picker lists
|
||||
|
||||
@@ -5,17 +5,17 @@
|
||||
* delta-walking of their own.
|
||||
*
|
||||
* Resolution rule (owner decision): explicit value in the record wins;
|
||||
* absent key resolves to the compiled factory default. Model-role defaults
|
||||
* come from config.ts's MODELS, threaded in as a parameter — this module
|
||||
* must stay browser-safe/pure, so it imports config TYPES only, never the
|
||||
* runtime module (config.ts pulls node:path). The composition root
|
||||
* (src/server/stores.ts) owns the glue: effectiveSettings() reads the
|
||||
* record through the store and passes MODELS here.
|
||||
* absent key resolves to the compiled factory default — EXCEPT MODELS
|
||||
* (BYOK all the way down, owner 2026-09-22): no factory pins exist, so a
|
||||
* model chain that ends empty resolves to null/undefined and whatever
|
||||
* needs it refuses honestly (503 models_not_configured). This module
|
||||
* stays browser-safe/pure, so it imports config TYPES only, never the
|
||||
* runtime module (config.ts pulls node:path).
|
||||
*
|
||||
* Deliberately NOT resolved here (see settings.ts module doc):
|
||||
* - per-chat overrides: the chat record's ChatSettings applies via
|
||||
* piEngineFor(role, chatRecord.settings) exactly as before — prober and
|
||||
* teacher stay chat-controlled, never settings-record-controlled;
|
||||
* - per-chat overrides: the chat record's ChatSettings applies via the
|
||||
* conversation chain in chat-handler (chat.model → strong slot) — prober
|
||||
* and teacher stay chat-controlled, never settings-record-controlled;
|
||||
* - tool toggles: passed through as the delta map; consumers look up by
|
||||
* tool id with absent → enabled (the honoring lives in the agent band);
|
||||
* - prompt switches: passed through; effective composition lives in
|
||||
@@ -42,27 +42,24 @@ import {
|
||||
type UserSettings,
|
||||
} from "./settings.ts";
|
||||
|
||||
/** The factory layer for model roles — config.ts's MODELS shape (types only
|
||||
* here; the runtime object is threaded in by the caller). Model slots
|
||||
* (2026-09-20): the factory map spans ALL nine roles (the strong slot's
|
||||
* factory fallback is the prober pin), while per-role OVERRIDES stay
|
||||
* background-only (the conversation pair is chat-settings-controlled). */
|
||||
export type ModelRoleSpecs = Record<ModelRole, { provider: string; model: string; effort: ThinkingLevel }>;
|
||||
|
||||
/** A fully-resolved model slot: the tier's concrete model (slot over factory
|
||||
* rep pin) — strong's factory rep is the prober pin, cheap's the researcher
|
||||
* pin, vision's the visualMaker pin (owner-settled tier mapping). */
|
||||
/** A configured model slot: the tier's concrete model, with its optional
|
||||
* thinking level. UNSET level is legal — the engine build resolves it
|
||||
* from config.ts's DEFAULT_THINKING_ORDER (first entry the model
|
||||
* supports; owner, 2026-09-22). A slot is CONFIGURED iff its model is
|
||||
* present; a level-only slot is inert (kept on disk, resolved as null). */
|
||||
export interface EffectiveSlot {
|
||||
provider: string;
|
||||
id: string;
|
||||
thinkingLevel: ThinkingLevel;
|
||||
thinkingLevel?: ThinkingLevel;
|
||||
}
|
||||
|
||||
/** A fully-resolved model pin for one background role. */
|
||||
/** A fully-resolved model pin for one role — same shape/level semantics
|
||||
* as EffectiveSlot; undefined means the role's whole chain came up
|
||||
* empty and whatever needs it refuses honestly. */
|
||||
export interface EffectiveModelSpec {
|
||||
provider: string;
|
||||
id: string;
|
||||
thinkingLevel: ThinkingLevel;
|
||||
thinkingLevel?: ThinkingLevel;
|
||||
}
|
||||
|
||||
/** The concrete appearance view — same shape as the factory's. */
|
||||
@@ -75,7 +72,7 @@ export interface EffectiveSettings {
|
||||
* ({} = no default = BYOK "no model" first boot). */
|
||||
chat: ChatSettings;
|
||||
agent: {
|
||||
models: Record<BackgroundModelRole, EffectiveModelSpec>;
|
||||
models: Record<BackgroundModelRole, EffectiveModelSpec | undefined>;
|
||||
/** Delta map as stored — consumers resolve absent key → enabled. */
|
||||
tools: Record<string, boolean>;
|
||||
/** Delta map as stored — absent key → override off. */
|
||||
@@ -85,84 +82,81 @@ export interface EffectiveSettings {
|
||||
* lives in server/search.ts, where keys are visible. */
|
||||
search: { engine?: SearchEngineId };
|
||||
};
|
||||
/** The resolved strong/cheap/vision slots (model slots wave,
|
||||
* 2026-09-20): each tier's concrete model (slot over factory rep pin)
|
||||
* — the per-role chain (override → slot → pin) is resolved into
|
||||
* agent.models; these are the tier-level views the picker captions. */
|
||||
models: Record<ModelSlotName, EffectiveSlot>;
|
||||
/** The resolved strong/cheap/vision slots: each tier's concrete model or
|
||||
* NULL when the tier holds no model (no factory floor — BYOK all the
|
||||
* way down, owner 2026-09-22). These are the tier-level views the
|
||||
* picker captions and the per-need refusal gates read. */
|
||||
models: Record<ModelSlotName, EffectiveSlot | null>;
|
||||
appearance: EffectiveAppearance;
|
||||
graph: Required<GraphSettings>;
|
||||
}
|
||||
|
||||
/** Full-chain resolution for ANY role (model slots wave, 2026-09-20):
|
||||
* per-role override (background roles only) → slot → factory pin. The
|
||||
* conversation pair (prober/teacher) resolves the same chain MINUS the
|
||||
* role override (their override is the chat's own settings — the
|
||||
* chat-handler overlays it itself). This is the engine wiring's one call. */
|
||||
export function resolveRoleSpec(
|
||||
role: ModelRole,
|
||||
record: UserSettings | null,
|
||||
factory: ModelRoleSpecs,
|
||||
): EffectiveModelSpec {
|
||||
const bg = BACKGROUND_MODEL_ROLES as readonly string[];
|
||||
const override = bg.includes(role) ? record?.agent?.models?.[role as BackgroundModelRole] : undefined;
|
||||
return resolveModelSpec(role as BackgroundModelRole, override, factory, record?.models);
|
||||
}
|
||||
|
||||
/** The factory representative pin for a slot tier — the pin the slot
|
||||
* falls back to when unset (owner-settled mapping: strong's rep is the
|
||||
* prober pin — prober and teacher share the strong tier — cheap's the
|
||||
* researcher pin, vision's the visualMaker pin). */
|
||||
export function factorySlotRep(slot: ModelSlotName, factory: ModelRoleSpecs): EffectiveSlot {
|
||||
const rep =
|
||||
slot === "strong" ? factory.prober : slot === "cheap" ? factory.researcher : factory.visualMaker;
|
||||
// The factory contract spans all nine roles (stores.ts MODEL_PINS) —
|
||||
// the rep lookup cannot miss; the cast is for the checker only.
|
||||
const r = rep as { provider: string; model: string; effort: ThinkingLevel };
|
||||
return { provider: r.provider, id: r.model, thinkingLevel: r.effort };
|
||||
}
|
||||
|
||||
/** Resolve one slot: the slot override per field, the factory rep pin per
|
||||
* absent field (model slots wave, 2026-09-20). */
|
||||
export function resolveSlot(
|
||||
slot: ModelSlotName,
|
||||
override: ModelSlot | undefined,
|
||||
factory: ModelRoleSpecs,
|
||||
): EffectiveSlot {
|
||||
const rep = factorySlotRep(slot, factory);
|
||||
/** The conversation pair's chain (BYOK, owner 2026-09-22): the chat's own
|
||||
* model override over the STRONG slot, each field independent (a bare
|
||||
* chat thinking level rides the strong slot's model). null = no model
|
||||
* anywhere — the turn gate refuses with the 503 models_not_configured
|
||||
* envelope. */
|
||||
export function resolveConversationSpec(
|
||||
chat: ChatSettings | undefined,
|
||||
strong: EffectiveSlot | null,
|
||||
): EffectiveModelSpec | null {
|
||||
const model = chat?.model ?? (strong ? { provider: strong.provider, id: strong.id } : undefined);
|
||||
if (!model) return null;
|
||||
return {
|
||||
provider: override?.model?.provider ?? rep.provider,
|
||||
id: override?.model?.id ?? rep.id,
|
||||
thinkingLevel: override?.thinkingLevel ?? rep.thinkingLevel,
|
||||
provider: model.provider,
|
||||
id: model.id,
|
||||
thinkingLevel: chat?.thinkingLevel ?? strong?.thinkingLevel,
|
||||
};
|
||||
}
|
||||
|
||||
/** Resolve one background role's spec — the model slots chain (owner
|
||||
* decision 2026-09-20): role override → SLOT → factory pin, each field
|
||||
* independent (a role can borrow only the slot's model, or only its
|
||||
* thinking budget). An absent provider coordinate anywhere resolves to
|
||||
* the factory pin's provider (the BYOK default, openrouter today) —
|
||||
* legacy ids without a provider ride the factory provider. */
|
||||
/** Full-chain resolution for ANY role (BYOK, owner 2026-09-22):
|
||||
* per-role override (background roles only) → SLOT → NOTHING. The
|
||||
* conversation pair (prober/teacher) resolves the same chain MINUS the
|
||||
* role override (their override is the chat's own settings — the
|
||||
* chat-handler overlays it itself). undefined = the chain came up
|
||||
* empty; the per-need refusal gates fire on exactly that. This is the
|
||||
* engine wiring's one call. */
|
||||
export function resolveRoleSpec(role: ModelRole, record: UserSettings | null): EffectiveModelSpec | undefined {
|
||||
const bg = BACKGROUND_MODEL_ROLES as readonly string[];
|
||||
const override = bg.includes(role) ? record?.agent?.models?.[role as BackgroundModelRole] : undefined;
|
||||
return resolveModelSpec(role, override, record?.models);
|
||||
}
|
||||
|
||||
/** Resolve one slot to its configured view, or null when the tier holds
|
||||
* no model (a thinkingLevel-only slot is inert — the level has nothing
|
||||
* to ride without the factory floor). */
|
||||
export function resolveSlot(slot: ModelSlot | undefined): EffectiveSlot | null {
|
||||
if (!slot?.model) return null;
|
||||
return { provider: slot.model.provider, id: slot.model.id, thinkingLevel: slot.thinkingLevel };
|
||||
}
|
||||
|
||||
/** Resolve one role's spec — the BYOK chain (owner 2026-09-22): role
|
||||
* override → SLOT → NOTHING, each field independent (an override may
|
||||
* borrow only the slot's model, or only its thinking budget — a level
|
||||
* with no model anywhere stays unset and the engine build resolves it
|
||||
* from DEFAULT_THINKING_ORDER). undefined = no model resolved; the
|
||||
* per-need refusal gates fire on exactly that. */
|
||||
export function resolveModelSpec(
|
||||
role: BackgroundModelRole,
|
||||
role: ModelRole,
|
||||
override: RoleModelOverride | undefined,
|
||||
factory: ModelRoleSpecs,
|
||||
slots?: Partial<Record<ModelSlotName, ModelSlot>>,
|
||||
): EffectiveModelSpec {
|
||||
// The factory contract spans all nine roles; background roles always hit.
|
||||
const spec = factory[role] as { provider: string; model: string; effort: ThinkingLevel };
|
||||
): EffectiveModelSpec | undefined {
|
||||
const slotName = ROLE_SLOT[role] as ModelSlotName | undefined;
|
||||
const slot = slotName ? slots?.[slotName] : undefined;
|
||||
const model = override?.model ?? slot?.model;
|
||||
if (!model) return undefined;
|
||||
return {
|
||||
provider: override?.model?.provider ?? slot?.model?.provider ?? spec.provider,
|
||||
id: override?.model?.id ?? slot?.model?.id ?? spec.model,
|
||||
thinkingLevel: override?.thinkingLevel ?? slot?.thinkingLevel ?? spec.effort,
|
||||
provider: model.provider,
|
||||
id: model.id,
|
||||
thinkingLevel: override?.thinkingLevel ?? slot?.thinkingLevel,
|
||||
};
|
||||
}
|
||||
|
||||
/** Resolve the whole record. `record` may be null (first boot) — every
|
||||
* field then lands on its factory default. */
|
||||
export function resolveSettings(record: UserSettings | null, factory: ModelRoleSpecs): EffectiveSettings {
|
||||
* factory-defaulted field lands on its default; MODEL fields resolve to
|
||||
* null/undefined when unset (no factory pins exist — BYOK all the way
|
||||
* down, owner 2026-09-22). */
|
||||
export function resolveSettings(record: UserSettings | null): EffectiveSettings {
|
||||
const agent = record?.agent ?? {};
|
||||
const modelsSection = record?.models ?? {};
|
||||
const appearance = record?.appearance ?? {};
|
||||
@@ -170,9 +164,9 @@ export function resolveSettings(record: UserSettings | null, factory: ModelRoleS
|
||||
const graph = record?.graph ?? {};
|
||||
const factoryAppearance = FACTORY_SETTINGS.appearance;
|
||||
const factoryMd = factoryAppearance.mdRendering;
|
||||
const models = {} as Record<BackgroundModelRole, EffectiveModelSpec>;
|
||||
const models = {} as Record<BackgroundModelRole, EffectiveModelSpec | undefined>;
|
||||
for (const role of BACKGROUND_MODEL_ROLES) {
|
||||
models[role] = resolveModelSpec(role, agent.models?.[role], factory, modelsSection);
|
||||
models[role] = resolveModelSpec(role, agent.models?.[role], modelsSection);
|
||||
}
|
||||
return {
|
||||
general: {
|
||||
@@ -186,9 +180,9 @@ export function resolveSettings(record: UserSettings | null, factory: ModelRoleS
|
||||
search: agent.search ?? {},
|
||||
},
|
||||
models: {
|
||||
strong: resolveSlot("strong", modelsSection.strong, factory),
|
||||
cheap: resolveSlot("cheap", modelsSection.cheap, factory),
|
||||
vision: resolveSlot("vision", modelsSection.vision, factory),
|
||||
strong: resolveSlot(modelsSection.strong),
|
||||
cheap: resolveSlot(modelsSection.cheap),
|
||||
vision: resolveSlot(modelsSection.vision),
|
||||
},
|
||||
appearance: {
|
||||
mode: appearance.mode ?? factoryAppearance.mode,
|
||||
|
||||
+51
-14
@@ -5,8 +5,10 @@
|
||||
*
|
||||
* DELTA SEMANTICS (owner decision, 2026-09-19): the record stores EXPLICIT
|
||||
* user choices only — ABSENT keys resolve to the factory defaults compiled
|
||||
* into this module (FACTORY_SETTINGS) and config.ts's MODELS (the compiled
|
||||
* factory layer for model roles). A software update that ADDS a knob (new
|
||||
* into this module (FACTORY_SETTINGS). MODELS are the exception (owner,
|
||||
* 2026-09-22 — BYOK all the way down): no factory pins exist, so an absent
|
||||
* model key resolves to EMPTY and the per-need gates refuse honestly. A
|
||||
* software update that ADDS a knob (new
|
||||
* tool, new role, new appearance field) therefore needs no migration: old
|
||||
* records simply lack the new keys. `version` is a PRIVATE schema counter
|
||||
* for BREAKING shape changes only (field renamed / meaning changed) —
|
||||
@@ -26,6 +28,7 @@
|
||||
*/
|
||||
|
||||
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
||||
import type { ModelRole } from "../../config.ts";
|
||||
import type { ChatSettings } from "./contracts/chat.ts";
|
||||
import type { SearchEngineId } from "./search.ts";
|
||||
|
||||
@@ -34,9 +37,8 @@ import type { SearchEngineId } from "./search.ts";
|
||||
export const CURRENT_SETTINGS_VERSION = 1;
|
||||
|
||||
/** The background-agent roles whose models the Agent settings expose.
|
||||
* Derived from config.ts's MODELS minus the conversation pair — prober and
|
||||
* teacher stay chat-settings-controlled (one controller, the model picker).
|
||||
* Keep in sync with config.ts's MODELS (tests pin the set). */
|
||||
* The conversation pair (prober/teacher) stays out — chat-controlled via
|
||||
* the strong slot (one controller, the model picker). Tests pin the set. */
|
||||
export const BACKGROUND_MODEL_ROLES = [
|
||||
"researcher",
|
||||
"demoBuilder",
|
||||
@@ -67,9 +69,44 @@ export interface RoleModelOverride {
|
||||
export const MODEL_SLOTS = ["strong", "cheap", "vision"] as const;
|
||||
export type ModelSlotName = (typeof MODEL_SLOTS)[number];
|
||||
|
||||
/** One slot's override — same per-field independence as a role override:
|
||||
* absent model keeps the tier's current model, absent thinkingLevel keeps
|
||||
* its thinking budget. */
|
||||
/** The machine-readable NOT-CONFIGURED refusal envelope (owner, 2026-09-22):
|
||||
* every per-need gate — the turn gate in chat-handler, a job spawn, a
|
||||
* notes import — refuses with this exact shape. HTTP callers get 503 +
|
||||
* this JSON; in-band consumers (the job record's error field) carry the
|
||||
* human `error` line. `missing` names the slot TIER(S) the refused
|
||||
* action needs and found unset. */
|
||||
export const MODELS_NOT_CONFIGURED_CODE = "models_not_configured" as const;
|
||||
export interface ModelsNotConfigured {
|
||||
/** Human-readable — safe to render as-is. */
|
||||
error: string;
|
||||
/** The machine discriminator the client switches on. */
|
||||
code: typeof MODELS_NOT_CONFIGURED_CODE;
|
||||
/** Which slot tiers the refused action needs ("strong" | "cheap" | "vision"). */
|
||||
missing: ModelSlotName[];
|
||||
}
|
||||
export function modelsNotConfigured(missing: ModelSlotName[]): ModelsNotConfigured {
|
||||
return {
|
||||
error: "No model configured — set up your models first",
|
||||
code: MODELS_NOT_CONFIGURED_CODE,
|
||||
missing,
|
||||
};
|
||||
}
|
||||
/** The typed throw for in-process seams (extraction pipeline): callers
|
||||
* catch it to skip honestly (trigger) or surface the 503 envelope
|
||||
* (routes) instead of logging a failure. */
|
||||
export class ModelsNotConfiguredError extends Error {
|
||||
readonly envelope: ModelsNotConfigured;
|
||||
constructor(missing: ModelSlotName[]) {
|
||||
super(modelsNotConfigured(missing).error);
|
||||
this.name = "ModelsNotConfiguredError";
|
||||
this.envelope = modelsNotConfigured(missing);
|
||||
}
|
||||
}
|
||||
|
||||
/** One slot's stored state. BYOK (owner, 2026-09-22): the WRITE side
|
||||
* requires model (a level alone can never run — no factory floor); the
|
||||
* sanitizer keeps whatever is on disk, but RESOLUTION treats a slot
|
||||
* without a model as unset (inert level, no phantom default). */
|
||||
export interface ModelSlot {
|
||||
/** The model swap: both coordinates or neither. */
|
||||
model?: { provider: string; id: string };
|
||||
@@ -99,7 +136,7 @@ export function slotsConfigured(models: ModelsSettings | undefined): boolean {
|
||||
/** Which slot a role draws from (model slots wave, 2026-09-20). Includes the
|
||||
* conversation pair and pdfReader (not BackgroundModelRoles — the slot
|
||||
* tier spans every role; only background roles surface per-role overrides). */
|
||||
export const ROLE_SLOT: Readonly<Record<string, ModelSlotName>> = {
|
||||
export const ROLE_SLOT: Readonly<Record<ModelRole, ModelSlotName>> = {
|
||||
prober: "strong",
|
||||
teacher: "strong",
|
||||
researcher: "cheap",
|
||||
@@ -201,11 +238,11 @@ export interface UserSettings {
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/** Factory defaults for everything core can pin (owner decisions 2026-09-19).
|
||||
* Model-role defaults live in config.ts's MODELS (the compiled factory
|
||||
* layer); the chat section has NO factory model (BYOK first boot doctrine
|
||||
* — "no model" until the user picks one); tools default on; prompts
|
||||
* default off — hence their absence here. */
|
||||
/** Factory defaults for everything core can pin (owner decisions
|
||||
* 2026-09-19). NO factory models exist anywhere (owner, 2026-09-22 — BYOK
|
||||
* all the way down): the chat section and every slot stay empty until the
|
||||
* user picks, and the per-need gates refuse honestly; tools default on;
|
||||
* prompts default off — hence their absence here. */
|
||||
export interface FactorySettings {
|
||||
version: number;
|
||||
general: Required<GeneralSettings>;
|
||||
|
||||
+53
-31
@@ -1,7 +1,7 @@
|
||||
import type { UIMessage } from "ai";
|
||||
import type { Usage } from "@earendil-works/pi-ai";
|
||||
import { createProberAgent, createTeacherAgent, type AgentBuildOptions, type TeachingAgent } from "../agent/teacher-agent.ts";
|
||||
import { piEngineForSpec } from "../agent/models.ts";
|
||||
import { piEngineForSpec, type PiEngine } from "../agent/models.ts";
|
||||
import { createJobRunner } from "../agent/subagents/runner.ts";
|
||||
import { createExtractionPipeline } from "../agent/subagents/extraction.ts";
|
||||
import { runGraphContextJob, type GraphContextDeps } from "../agent/subagents/graph-context.ts";
|
||||
@@ -10,9 +10,15 @@ import type { ModelRole } from "../../config.ts";
|
||||
import { claimSteers, deliverJobResult, setContextRespawn, setJobRespawn, setTurnRunner } from "./jobs.ts";
|
||||
import { recordToPiMessages } from "./pi/record-adapter.ts";
|
||||
import { createPiTurnResponse } from "./pi/ui-stream.ts";
|
||||
import { effectiveSettings, MODEL_PINS, stores } from "./stores.ts";
|
||||
import { effectiveSettings, stores } from "./stores.ts";
|
||||
import { searchAvailable, searchProvider } from "./search.ts";
|
||||
import { extractionEnabled, graphContextEnabled, resolveRoleSpec } from "../core/settings-resolution.ts";
|
||||
import {
|
||||
extractionEnabled,
|
||||
graphContextEnabled,
|
||||
resolveConversationSpec,
|
||||
resolveRoleSpec,
|
||||
} from "../core/settings-resolution.ts";
|
||||
import { modelsNotConfigured } from "../core/settings.ts";
|
||||
import { beginTurn, endTurn } from "./turn-liveness.ts";
|
||||
import { persistTurn, triggerExtraction, triggerGraphContext, withMessageIds } from "./turn-service.ts";
|
||||
import type { CallUsage } from "../core/contracts.ts";
|
||||
@@ -40,12 +46,12 @@ const jobRunner = createJobRunner({
|
||||
chatStore: stores.chatStore,
|
||||
jobStore: stores.jobStore,
|
||||
deliver: deliverJobResult,
|
||||
// Model slots wave (2026-09-20): job engines resolve the full chain —
|
||||
// role override → slot → factory pin — read FRESH per job (a settings
|
||||
// PUT between dispatch and spawn lands). Closes the old gap where
|
||||
// per-role overrides reached rolePins but never the engines.
|
||||
// BYOK (owner, 2026-09-22): job engines resolve the chain — role
|
||||
// override → slot → NOTHING — read FRESH per job (a settings PUT
|
||||
// between dispatch and spawn lands); an empty chain flows through as
|
||||
// undefined and the runner refuses the spawn honestly.
|
||||
engineFor: (role) =>
|
||||
piEngineForSpec(resolveRoleSpec(role, stores.settingsStore.readSettings(), MODEL_PINS)),
|
||||
resolveEngine(role),
|
||||
// Search port wave (2026-09-20): the researcher's web_search runs on the
|
||||
// injected port — resolved FRESH per job (key rotations land immediately).
|
||||
// null = honestly unavailable; the dispatch-level drop (below) uses the
|
||||
@@ -60,13 +66,12 @@ export const extraction = createExtractionPipeline({
|
||||
// The REAL embedder (local transformer model): post-merge embedding
|
||||
// batch — semantic clusters are computed and persisted here.
|
||||
embedder: embedNewNodes,
|
||||
// Model slots wave (2026-09-20): the extraction stages resolve the FULL
|
||||
// chain — role override → slot → factory pin — read FRESH per stage call
|
||||
// (the flagged gap: ExtractionModels deps were engine VALUES, keeping
|
||||
// extractor/auditor/labeler config-pin-bound while the job runner went
|
||||
// slot-native). Direct test values still win inside the pipeline.
|
||||
// BYOK (owner, 2026-09-22): the extraction stages resolve the chain —
|
||||
// role override → slot → NOTHING — read FRESH per stage call; an empty
|
||||
// chain flows through as undefined and the pipeline refuses the run
|
||||
// with the typed ModelsNotConfiguredError. Direct test values still win.
|
||||
engineFor: (role) =>
|
||||
piEngineForSpec(resolveRoleSpec(role, stores.settingsStore.readSettings(), MODEL_PINS)),
|
||||
resolveEngine(role),
|
||||
});
|
||||
|
||||
/** Graph context injection (vision-context-injection.md): the runner's
|
||||
@@ -97,6 +102,15 @@ setTurnRunner(async (chatId, messages) => {
|
||||
await res.arrayBuffer().catch(() => undefined);
|
||||
return res.status;
|
||||
});
|
||||
/** The settings-resolved engine for one role — BYOK (owner, 2026-09-22):
|
||||
* the chain (role override → slot → NOTHING) may end empty, and undefined
|
||||
* flows through to the per-need refusal gates (runner/extraction), read
|
||||
* FRESH so a settings PUT between dispatch and spawn lands. */
|
||||
function resolveEngine(role: ModelRole): PiEngine | undefined {
|
||||
const spec = resolveRoleSpec(role, stores.settingsStore.readSettings());
|
||||
return spec ? piEngineForSpec(spec) : undefined;
|
||||
}
|
||||
|
||||
setJobRespawn((jobId) => void jobRunner.runJob(jobId));
|
||||
setContextRespawn((jobId) => void runGraphContextJob(graphContext, jobId));
|
||||
|
||||
@@ -135,6 +149,25 @@ export async function handleChatRequest(
|
||||
return Response.json({ error: "messages must be an array" }, { status: 400 });
|
||||
}
|
||||
|
||||
// THE UNIVERSAL TURN GATE (BYOK, owner, 2026-09-22): a turn needs the
|
||||
// CONVERSATION chain — the chat's own model override over the strong
|
||||
// slot — and there is no factory floor; when both are empty the turn
|
||||
// refuses honestly with the machine-readable 503 envelope (new sessions
|
||||
// and continuing ones alike — wake-pipeline turns ride this same path).
|
||||
// Placement is deliberate: BEFORE beginTurn (no liveness effects), the
|
||||
// write-ahead persist (nothing durable changes), and every downstream
|
||||
// resolution (jobs/extraction gate per-need on their own tiers).
|
||||
const effective = effectiveSettings();
|
||||
const conversation = resolveConversationSpec(
|
||||
stores.chatStore.getChat(chatId)?.settings,
|
||||
effective.models.strong,
|
||||
);
|
||||
if (!conversation) {
|
||||
const envelope = modelsNotConfigured(["strong"]);
|
||||
console.info(`[chat ${chatId}] turn refused — ${JSON.stringify(envelope)}`);
|
||||
return Response.json(envelope, { status: 503 });
|
||||
}
|
||||
|
||||
// One turn per chat, ever. A second request while a turn runs means a
|
||||
// stale/remounted client raced a live one — its message list is outdated,
|
||||
// and letting it through would double-run the teacher and clobber the
|
||||
@@ -158,10 +191,8 @@ export async function handleChatRequest(
|
||||
) as UIMessage[];
|
||||
|
||||
// THE SETTINGS VIEW (settings-storage wave, 2026-09-19): resolved fresh
|
||||
// per turn — graph gates, tool toggles, and (via the store) the record's
|
||||
// chat defaults all read from ONE small file. No caching: a stale toggle
|
||||
// after a settings PUT would be worse than the read.
|
||||
const effective = effectiveSettings();
|
||||
// per turn (the gate above owns the read) — graph gates, tool toggles,
|
||||
// and the record's chat defaults all read from ONE small file.
|
||||
|
||||
// Write-ahead persistence: the request body IS the client's canonical
|
||||
// state, so it goes on disk BEFORE the stream starts. The learner's answer
|
||||
@@ -220,19 +251,10 @@ export async function handleChatRequest(
|
||||
// steers, usage, extraction) — only the agent differs.
|
||||
const teaching = outgoing.some((m) => m.parts.some((p) => p.type === "tool-probe_done"));
|
||||
const role: ModelRole = teaching ? "teacher" : "prober";
|
||||
// The chat's model-control settings (prober/teacher only — one controller
|
||||
// for the conversation agents; piEngineFor falls back to config on absent
|
||||
// fields and clamps the thinking level to the model's supported list).
|
||||
const settings = stores.chatStore.getChat(chatId)?.settings;
|
||||
// Model slots wave (2026-09-20): the conversation chain is the chat's
|
||||
// own override over the STRONG slot (prober + teacher's tier) over the
|
||||
// factory pin — each field independent, same discipline as the roles.
|
||||
const strong = effective.models.strong;
|
||||
const engine = piEngineForSpec({
|
||||
provider: settings?.model?.provider ?? strong.provider,
|
||||
id: settings?.model?.id ?? strong.id,
|
||||
thinkingLevel: settings?.thinkingLevel ?? strong.thinkingLevel,
|
||||
});
|
||||
// BYOK (owner, 2026-09-22): the conversation chain was resolved by the
|
||||
// turn gate above (chat.model override → strong slot → refused) — the
|
||||
// engine build is pure from here; thinking levels clamp natively.
|
||||
const engine = piEngineForSpec(conversation);
|
||||
// Provider-qualified stamp (model slots wave): matches the price table's
|
||||
// provider:id keys.
|
||||
const modelId = `${engine.model.provider}:${engine.model.id}`;
|
||||
|
||||
+66
-42
@@ -39,63 +39,77 @@ import { BACKGROUND_MODEL_ROLES, type BackgroundModelRole, type ModelSlotName, t
|
||||
/** The picker's client-safe model entry (contract: core/contracts/models.ts). */
|
||||
export type ModelDto = ModelCatalogEntry;
|
||||
|
||||
/** The conversation agents' resolved default pin (pure form — the record's
|
||||
* chat section in, CatalogDefaults out; `catalogDefaults()` below is the
|
||||
* fs wrapper). Absent fields (or no record — BYOK first boot) fall back
|
||||
* to the config pin, exactly as before. */
|
||||
export function catalogDefaultsFor(chat: ChatSettings | undefined, strong: EffectiveSlot): CatalogDefaults {
|
||||
/** The conversation agents' resolved default (pure form — the record's chat
|
||||
* section + the strong slot in, CatalogDefaults out; `catalogDefaults()`
|
||||
* below is the fs wrapper). BYOK (owner, 2026-09-22): there is no factory
|
||||
* floor — when neither the chat section nor the strong slot names a
|
||||
* model, the default is NULL and the turn gate refuses with the 503
|
||||
* models_not_configured envelope. An unset thinkingLevel resolves from
|
||||
* DEFAULT_THINKING_ORDER at engine build (reported as absent here). */
|
||||
export function catalogDefaultsFor(
|
||||
chat: ChatSettings | undefined,
|
||||
strong: EffectiveSlot | null,
|
||||
): CatalogDefaults | null {
|
||||
const model = chat?.model ?? (strong ? { provider: strong.provider, id: strong.id } : undefined);
|
||||
if (!model) return null;
|
||||
const level = chat?.thinkingLevel ?? strong?.thinkingLevel;
|
||||
return {
|
||||
model: chat?.model ?? { provider: strong.provider, id: strong.id },
|
||||
thinkingLevel: chat?.thinkingLevel ?? strong.thinkingLevel,
|
||||
model,
|
||||
...(level !== undefined ? { thinkingLevel: level } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/** The conversation agents' config pin — what a settings-less chat runs.
|
||||
* (settings-storage wave, 2026-09-19): the user's picked default rides in
|
||||
* the settings record's `chat` section; the picker's defaults row shows
|
||||
* it once chosen. */
|
||||
export function catalogDefaults(): CatalogDefaults {
|
||||
/** The conversation agents' resolved default — what a settings-less chat
|
||||
* runs. (settings-storage wave, 2026-09-19): the user's picked default
|
||||
* rides in the settings record's `chat` section; the picker's defaults
|
||||
* row shows it once chosen. */
|
||||
export function catalogDefaults(): CatalogDefaults | null {
|
||||
return catalogDefaultsFor(settingsStore.readSettings()?.chat, effectiveSettings().models.strong);
|
||||
}
|
||||
|
||||
/** Project the resolved per-role specs to the client DTO (pure — the
|
||||
* settings UI's "Default: <actual pinned model>" caption reads this).
|
||||
* thinkingLevel is always concrete in the resolved view; the contract
|
||||
* keeps it optional so the UI tolerates a future role without one. */
|
||||
export function rolePinsFor(models: Record<BackgroundModelRole, EffectiveModelSpec>): RolePins {
|
||||
* BYOK (owner, 2026-09-22): an empty chain projects to NULL — the caption
|
||||
* honestly shows "not configured", never a phantom default. */
|
||||
export function rolePinsFor(
|
||||
models: Record<BackgroundModelRole, EffectiveModelSpec | undefined>,
|
||||
): RolePins {
|
||||
const pins = {} as RolePins;
|
||||
for (const role of BACKGROUND_MODEL_ROLES) {
|
||||
const spec = models[role];
|
||||
pins[role] = {
|
||||
model: { provider: spec.provider, id: spec.id },
|
||||
thinkingLevel: spec.thinkingLevel,
|
||||
};
|
||||
pins[role] = spec
|
||||
? {
|
||||
model: { provider: spec.provider, id: spec.id },
|
||||
...(spec.thinkingLevel !== undefined ? { thinkingLevel: spec.thinkingLevel } : {}),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
return pins;
|
||||
}
|
||||
|
||||
/** The six background roles' RESOLVED pins (settings interlude,
|
||||
* 2026-09-19): the user's agent-models override falling back to the
|
||||
* config.ts MODELS pin — the same resolution the agent factories use
|
||||
* (src/server/stores.ts effectiveSettings), so the caption can never
|
||||
* drift from what actually runs. */
|
||||
/** The six background roles' RESOLVED pins: the user's agent-models
|
||||
* override → tier slot → nothing — the same resolution the agent wiring
|
||||
* uses (src/server/stores.ts effectiveSettings), so the caption can
|
||||
* never drift from what actually runs. */
|
||||
export function catalogRolePins(): RolePins {
|
||||
return rolePinsFor(effectiveSettings().agent.models);
|
||||
}
|
||||
|
||||
/** The three slot tiers' RESOLVED pins (model slots wave, 2026-09-20) —
|
||||
* effectiveSettings().models verbatim: slot over factory rep pin, the
|
||||
* same resolution the engine wiring uses, so the Models settings page's
|
||||
* captions cannot drift from what actually runs. */
|
||||
/** The three slot tiers' RESOLVED pins — effectiveSettings().models
|
||||
* verbatim: the same resolution the engine wiring uses, so the Models
|
||||
* settings page's captions cannot drift from what actually runs. NULL
|
||||
* per tier that holds no model. */
|
||||
export function catalogSlots(): SlotPins {
|
||||
const slots = effectiveSettings().models;
|
||||
const pins = {} as SlotPins;
|
||||
for (const name of ["strong", "cheap", "vision"] as const) {
|
||||
const slot: EffectiveSlot = slots[name];
|
||||
pins[name] = {
|
||||
model: { provider: slot.provider, id: slot.id },
|
||||
thinkingLevel: slot.thinkingLevel,
|
||||
};
|
||||
const slot: EffectiveSlot | null = slots[name];
|
||||
pins[name] = slot
|
||||
? {
|
||||
model: { provider: slot.provider, id: slot.id },
|
||||
...(slot.thinkingLevel !== undefined ? { thinkingLevel: slot.thinkingLevel } : {}),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
return pins;
|
||||
}
|
||||
@@ -183,11 +197,14 @@ export function validateChatSettings(settings: ChatSettings): SettingsValidation
|
||||
}
|
||||
}
|
||||
if (settings.thinkingLevel !== undefined) {
|
||||
// The conversation chain's fallback is the strong slot (model slots
|
||||
// wave, 2026-09-20) — the model a settings-less chat would run.
|
||||
// The conversation chain's fallback is the strong slot — the model
|
||||
// a settings-less chat would run. BYOK (owner, 2026-09-22): with no
|
||||
// model to check against (no chat.model, no strong slot), the level
|
||||
// rides unchecked — it clamps against whatever the chain later holds.
|
||||
const strong = effectiveSettings().models.strong;
|
||||
const provider = settings.model?.provider ?? strong.provider;
|
||||
const id = settings.model?.id ?? strong.id;
|
||||
const provider = settings.model?.provider ?? strong?.provider;
|
||||
const id = settings.model?.id ?? strong?.id;
|
||||
if (!provider || !id) return { ok: true };
|
||||
const model = piModels().getModel(provider, id);
|
||||
if (!model) return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
||||
const supported = getSupportedThinkingLevels(model);
|
||||
@@ -202,9 +219,10 @@ export function validateChatSettings(settings: ChatSettings): SettingsValidation
|
||||
return { ok: true };
|
||||
}
|
||||
/** Validate a models-section write (PUT /api/settings/models) against the
|
||||
* catalog — the same gate the chat section gets. Model swaps must name a
|
||||
* real provider+model; a thinkingLevel must be supported by the model the
|
||||
* slot would resolve to (the slot's own model, else the factory rep pin).
|
||||
* catalog — the same gate the chat section gets. A slot must NAME a real
|
||||
* provider+model (owner, 2026-09-22: no factory floor exists, so a level
|
||||
* alone can never run — the parser rejects it, this gate re-checks the
|
||||
* model); a thinkingLevel must be supported by the slot's own model.
|
||||
* keyPresent is NOT gated here (a stored pin for a provider whose key
|
||||
* lapsed stays valid — it fails honestly at stream time, and the picker
|
||||
* stops offering it). */
|
||||
@@ -230,8 +248,14 @@ function validateSlotSpec(name: ModelSlotName, slot: { model?: { provider: strin
|
||||
}
|
||||
}
|
||||
if (slot.thinkingLevel !== undefined) {
|
||||
const provider = slot.model?.provider ?? effectiveSettings().models[name].provider;
|
||||
const id = slot.model?.id ?? effectiveSettings().models[name].id;
|
||||
// The write-side parser requires slot.model (owner, 2026-09-22 — a
|
||||
// level-only slot can never run), so this fallback is tolerance for
|
||||
// future callers; with no model to check against, accept (the
|
||||
// engine build clamps whatever the chain later holds).
|
||||
const resolved = effectiveSettings().models[name];
|
||||
const provider = slot.model?.provider ?? resolved?.provider;
|
||||
const id = slot.model?.id ?? resolved?.id;
|
||||
if (!provider || !id) return { ok: true };
|
||||
const model = piModels().getModel(provider, id);
|
||||
if (!model) return { ok: false, error: `No model "${id}" in the ${provider} catalog` };
|
||||
const supported = getSupportedThinkingLevels(model);
|
||||
|
||||
@@ -309,11 +309,14 @@ export function parseModelsSettingsBody(body: unknown): SettingsBodyResult<Model
|
||||
}
|
||||
const parsed = parseRoleOverride(raw[key], key);
|
||||
if (!parsed.ok) return { ok: false, error: parsed.error };
|
||||
if (Object.keys(parsed.override).length) {
|
||||
settings[key as (typeof MODEL_SLOTS)[number]] = parsed.override;
|
||||
} else {
|
||||
return { ok: false, error: `slot "${key}" must set model or thinkingLevel (absent slot = unset)` };
|
||||
// BYOK (owner, 2026-09-22): a slot IS a model choice — no factory
|
||||
// floor exists, so a thinkingLevel alone can never run; every slot
|
||||
// write must carry the model (the level is an optional rider). An
|
||||
// absent slot key clears the tier (replace-whole section).
|
||||
if (!parsed.override.model) {
|
||||
return { ok: false, error: `slot "${key}" must set a model (thinkingLevel alone cannot run — absent slot = unset)` };
|
||||
}
|
||||
settings[key as (typeof MODEL_SLOTS)[number]] = parsed.override;
|
||||
}
|
||||
return { ok: true, settings };
|
||||
}
|
||||
|
||||
+5
-24
@@ -6,12 +6,11 @@
|
||||
* file (and the adapter implementations in src/store/).
|
||||
*/
|
||||
|
||||
import { MODELS } from "../../config.ts";
|
||||
import { chatStore } from "../store/chats.ts";
|
||||
import { graphStore } from "../store/graph.ts";
|
||||
import { jobStore } from "../store/jobs.ts";
|
||||
import { settingsStore } from "../store/settings.ts";
|
||||
import { resolveSettings, type EffectiveSettings, type ModelRoleSpecs } from "../core/settings-resolution.ts";
|
||||
import { resolveSettings, type EffectiveSettings } from "../core/settings-resolution.ts";
|
||||
/** The wired adapters. Satisfies core/graph.ts's GraphDeps structurally. */
|
||||
export const stores = {
|
||||
chatStore,
|
||||
@@ -22,32 +21,14 @@ export const stores = {
|
||||
|
||||
export type Stores = typeof stores;
|
||||
|
||||
/** config.ts's MODELS as the compiled factory layer the resolution reads —
|
||||
* ALL NINE roles since the model-slots wave (2026-09-20): the strong
|
||||
* slot's factory fallback IS the prober pin. core stays pure (it takes
|
||||
* this as a parameter, never imports config at runtime). Per-role
|
||||
* OVERRIDES stay background-only (the conversation pair is
|
||||
* chat-settings-controlled) — that split lives in core's
|
||||
* BACKGROUND_MODEL_ROLES, unchanged. */
|
||||
const MODEL_PINS: ModelRoleSpecs = {
|
||||
prober: MODELS.prober,
|
||||
teacher: MODELS.teacher,
|
||||
researcher: MODELS.researcher,
|
||||
demoBuilder: MODELS.demoBuilder,
|
||||
visualMaker: MODELS.visualMaker,
|
||||
pdfReader: MODELS.pdfReader,
|
||||
extractor: MODELS.extractor,
|
||||
auditor: MODELS.auditor,
|
||||
labeler: MODELS.labeler,
|
||||
};
|
||||
export { MODEL_PINS };
|
||||
|
||||
/**
|
||||
* The resolved view of the settings record (core/settings-resolution.ts):
|
||||
* every knob concrete, record overrides over config.ts factory pins. Read
|
||||
* every knob concrete over its factory default — EXCEPT MODELS (BYOK all
|
||||
* the way down, owner 2026-09-22): no factory pins exist, so unset model
|
||||
* slots resolve to null and per-need refusal gates fire downstream. Read
|
||||
* fresh on every call — the record is a small file and turns are rare, so
|
||||
* no caching layer (stale toggles after a settings PUT would be worse).
|
||||
*/
|
||||
export function effectiveSettings(): EffectiveSettings {
|
||||
return resolveSettings(settingsStore.readSettings(), MODEL_PINS);
|
||||
return resolveSettings(settingsStore.readSettings());
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import type { ChatStore, GraphStore } from "../core/ports.ts";
|
||||
import { EXTRACTION } from "../../config.ts";
|
||||
import type { ExtractionPipeline } from "../agent/subagents/extraction.ts";
|
||||
import { startGraphContextInjection, type GraphContextDeps } from "../agent/subagents/graph-context.ts";
|
||||
import { ModelsNotConfiguredError } from "../core/settings.ts";
|
||||
|
||||
/**
|
||||
* The canonical state write: the client owns the message list between turns
|
||||
@@ -98,6 +99,13 @@ export function triggerExtraction(
|
||||
const covered = coverageOf(record.messages as UIMessage[], deps.graphStore.readCoverage(), chatId);
|
||||
if (covered.uncoveredTokens < EXTRACTION.chunkTokens) return;
|
||||
void extraction.runExtraction(chatId).catch((err) => {
|
||||
// BYOK (owner, 2026-09-22): an unconfigured write channel SKIPS
|
||||
// (the coverage ledger keeps the messages uncovered — the next
|
||||
// trigger retries once slots are set); only real failures log.
|
||||
if (err instanceof ModelsNotConfiguredError) {
|
||||
console.info(`[chat ${chatId}] extraction skipped — ${err.envelope.error} (missing: ${err.envelope.missing.join(", ")})`);
|
||||
return;
|
||||
}
|
||||
console.error(`[chat ${chatId}] extraction pipeline failed:`, err);
|
||||
});
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user