/** * The setup report, and what it offers to do about itself. * * Everything here is pure: facts in, rows and offers out. Nothing reads a * seam, nothing writes one, and nothing decides on its own that a step should * run — {@link "./index.ts"} does the acting, this decides what is true and * what is worth offering. * * Two rules shape every line below. * * **A mark is a claim, so an unknown gets no mark.** `✓` means a surface * confirmed something, `⚠` means a surface confirmed it is missing, and `·` * means nobody established either — a Harness generation that could not be * read, a profile whose root is unreadable. The third case is common and is * not a fault, so it must not look like one; the same distinction * `providerReadiness` already draws for a credential dot. * * **A step is offered only when the seams would accept it, and the missing * piece leads.** `Choose a model` appears only once a route is registered — * `/model` answers "no provider route advertises a model" otherwise, and an * offer that opens an empty picker is worse than no offer — and when it does * appear it goes first, because by then it is the step between the reader and * a working session. * * **A healthy report offers nothing to repair.** `/model` and `/connect` are * the authorities for changing a working installation, and setup does not * present them as next steps merely because a reader asked it to look. The * steps below are keyed to warnings the report actually carries, so the * picker never opens on a screen with nothing to fix, and the flow that * printed an all-clear returns to the composer instead of offering a menu. * * **A warning and a repair are different questions.** The report shows every * warning Harness is entitled to state, including ones whose only guidance is * a shell command or a settings file; the steps answer only the warnings setup * can act on interactively. {@link hasRemediation} is the picker's gate, so a * diagnostic-only warning stays in scrollback and never forces a one-item * picker. * @module dshline/setup/model */ import { connectSelectableCount } from '../connect/index.ts' import type { ConnectCapabilities, ConnectReadiness, ConnectState } from '../connect/index.ts' import type { HarnessGeneration, SetupFacts, SetupSelection } from './harness.ts' /** * How confidently one row can be stated. * * `⚠` is deliberately not an error mark. Every state it appears on here is a * reachable, fixable configuration — no route active, no authorization seam — * and setup exists precisely to be the place a reader meets those calmly. */ export type SetupMark = '✓' | '⚠' | '·' /** One line of the report. */ export interface SetupCheck { /** The mark, or `·` where nothing was established. */ readonly mark: SetupMark /** Left column: what was checked. */ readonly name: string /** Right column: what was found. */ readonly detail: string /** Indented follow-up lines, for a finding that needs an action spelled out. */ readonly notes: readonly string[] } /** Why a launch cannot send a turn as it stands. */ export type SetupReason = /** No adapter registered any route, so nothing can be selected at all. */ | 'no-route' /** Routes exist, but nothing resolved a selection for the next model step. */ | 'no-selection' /** A selection exists, but no adapter has registered the route it names. */ | 'unregistered-selection' /** * The topology is complete and the selected route's credential is one * Harness positively reports as absent. Only `missing` reaches here: * `unknown` is never turned into a fault — see {@link readinessOf}. */ | 'credential-missing' /** Something setup can hand the reader on to. */ export type SetupStepId = /** Open `/connect`, the browser that configures and authenticates providers. */ | 'connect' /** Open `/model`, once at least one route advertises something. */ | 'model' /** Leave setup and go to the composer. */ | 'skip' /** One offered next action, already worded for a picker. */ export interface SetupStep { readonly id: SetupStepId readonly label: string readonly description: string } /** * Why this launch would reach the composer without a model it can send to. * * The topology states come from the registry and the selection ref `/model` * writes. The selected route's Connect readiness adds credential facts; a * provider diagnostic stays presentation data and does not become a validity * judgement. * * Route registration alone is NOT the question, which is what the first * version of this got wrong: a registered route is only what `/model` offers * FROM, and a launch reaches the composer with whatever `selection.current` * resolved to, which may be nothing or may name a route nothing registered. * * The selection is checked at PROVIDER granularity and no finer. Exact model * validity remains with the Harness adapter that executes the request; the * selector catalog is not an execution whitelist. * @param registered - route keys an adapter has registered, from `listProviders`. * @param selected - the selection the next model step would use, if any. * @param credential - readiness of the selected route, from Connect's own * {@link readinessOf}; omitted where the topology already decided. * @returns why setup should open, or undefined when this launch can send a turn. */ export function setupReason( registered: readonly string[], selected: { readonly provider: string } | undefined, credential?: ConnectReadiness, ): SetupReason | undefined { if (registered.length === 0) return 'no-route' if (selected === undefined) return 'no-selection' // A remembered model whose route is gone: the profile changed under a stored // default, and the composer would open on a selection no adapter serves. if (!registered.includes(selected.provider)) return 'unregistered-selection' // Topology alone says a stock first install is healthy, and it is not: the // base composition ships a default selection AND its adapter registers that // route before any credential exists, so all three checks above pass while // the first prompt would fail with MISSING_CREDENTIAL. Only a credential // Harness reports as positively absent counts; `ready` and `unknown` both // leave the launch alone. if (credential === 'missing') return 'credential-missing' return undefined } /** Whether at least one route is registered, so `/model` has something to offer. */ export function hasActiveRoute(connect: ConnectState): boolean { return connect.kind === 'ready' && connect.providers.some(row => row.state === 'active') } /** * Whether the model step is the piece that is actually missing. * * What separates "offer a model picker" from "open one": setup hands straight * into `/model` only when a route can serve it and the selection is the thing * that is absent or stale — never when a usable model is already selected. * @param facts - what one setup pass established. * @returns whether choosing a model is the next obvious step. */ export function needsModelChoice(facts: SetupFacts): boolean { if (!hasActiveRoute(facts.connect)) return false return facts.reason === 'no-selection' || facts.reason === 'unregistered-selection' } /** * Sign-ins that succeeded against a route nothing has registered. * * The failure this whole change exists for, stated as data. A credential * record and a settings profile are separate writes, so a person can finish an * account login and still have no model — and before the link existed, the * only evidence on screen was two unrelated-looking rows. * @param connect - the reading. * @returns each such sign-in as `