Files
heidi/lib/speech/capability.ts
CatoandClaude Opus 5.5 d73ac3c391 refactor(speech): the engine is @bitbaum/speechkit now — and grammar stops presenting a guess as the answer (#125)
* refactor(speech): the engine is @bitbaum/speechkit now — and grammar stops presenting a guess as the answer

Heidi's speech engine — signal measurement, the evidence rule, fluency,
hesitations, dialect markers, grammar mapping, the LanguageTool client — moved
into github:bitbaum/speechkit#v1.0.0 (dist committed, no build step; npm once
bootstrapped). 20 files leave this repo; 82 of their tests moved with them and
run there (90 in the package). Heidi keeps what is Heidi's: which measures it
CLAIMS (capability.ts), the Zurich pack's marker tests, and the product.

The package reads no environment, so the route now passes
LANGUAGETOOL_URL itself.

THE WIRED GUARD, REBUILT. It checked that a measure's FILE was reachable from
app/; once the engine is a package nothing imports its internal files. Each
measure now names its entry function and the test requires something reachable
to import it. Mutation-proven: dropping checkGrammar from the route fails it by
name. The source-link test also now checks the file EXISTS — the old one checked
only that a string looked like a path, which is how /technology linked readers
to a gate.ts that never existed. The links are real links now.

GRAMMAR SHOWED A WRONG CORRECTION. Measured on the live site after #123:
LanguageTool offers "dem Buch" FIRST and "das Buch" second for "Er hat den Buch
gelesen", and the page showed only the first — a wrong fix presented as the
answer. All alternatives now, and a line saying which one fits depends on what
the speaker meant (seven languages).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(investors): count the tests that moved, instead of overstating them

The data room claimed 111 test files; the speech engine's 11 moved into
@bitbaum/speechkit, leaving 103 here. The ratchet caught it. The figure is
now 842 across 103, plus its own row for the open-source engine (94 tests,
checkable at github.com/bitbaum/speechkit) — moved, not dropped.

Also: ADVERTISED_TEST_FILES was declared below METRICS, so the string could
not reference it and carried its own literal 111 — 'said once' was false. It
is declared above and used now.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 02:53:58 -07:00

169 lines
7.2 KiB
TypeScript

/**
* What the speaking evaluation can honestly say, per variety, derived.
*
* WHY THIS FILE EXISTS. `evidence.ts` states the rule that decides whether a
* transcript may be judged; the engine files each obey it. But a PAGE wanting
* to describe the feature — the technology page, a pitch, a sector page — had
* no way to ask "what does this amount to for the language we actually teach?"
* without a human re-deriving the answer in prose. Prose derived by hand from
* a rule is prose that goes stale the first time the rule's inputs change, and
* the input here is a vendor's word error rate, which changes every few months.
*
* So the claim is COMPUTED. A measure declares the least evidence it needs;
* the pack declares what recognition exists; the verdict falls out. Nobody
* writes "we can check your grammar" anywhere — the page reads a verdict and
* renders the label for it, in whichever language the reader is using.
*
* WHAT THIS BUYS, concretely: when Zurich German recognition improves past
* `FORM_JUDGEMENT_MAX_WER`, one number in one pack changes and the technology
* page starts saying grammar is checkable on the dialect — in seven languages,
* with no copy edit and no chance of the page and the engine disagreeing. And
* until that day, the page cannot overclaim even if somebody wants it to.
*
* THE REFUSAL IS DATA TOO. Pronunciation is listed here and marked refused, so
* the absence is a row on the page rather than a silence. §8 forbids the claim
* and this is where that shows up in the product: the one measure every
* competitor advertises is the one this engine declines to produce, stated
* next to the four it does.
*
* Pure: no I/O, no model, same input -> same output.
*/
import { evidenceFrom, type EvidenceKind, type Recognition } from "@bitbaum/speechkit";
/**
* What a measure needs before it may say anything.
*
* `signal` is the fourth level and sits below `evidence.ts`'s three: it needs
* no transcript at all, because it is measured from where the sound was and
* where it was not. That is the level that makes the feature work for a
* dialect nothing transcribes — silence, hesitation and phonation are real
* properties of an utterance, and measuring them requires understanding none
* of it.
*/
export type Needs = "signal" | EvidenceKind;
export type MeasureId = "delivery" | "fluency" | "words" | "grammar" | "pronunciation";
export type Measure = {
id: MeasureId;
needs: Needs;
/**
* Also needs a grammar service that covers the variety.
*
* Separate from `needs` because it fails independently: a variety can have
* excellent recognition and still have nobody offering a rule checker for
* it, and collapsing the two would report the wrong reason.
*/
needsGrammarService?: true;
/**
* Never produced, at any evidence level, by decision rather than by limit.
*
* §8. A pronunciation score is the claim the register bans, and it is banned
* because no amount of better recognition would make it honest: scoring an
* accent against a native ideal is a judgement about a person, not a
* measurement of an utterance. This flag is what stops a future contributor
* reading the verdicts as "not yet" and implementing it when the WER drops.
*/
refused?: true;
/**
* Where the code is, as `repo/path`, so a reader can go and check rather
* than trust. The technology page links it to GitHub.
*/
module: string;
/**
* The function the product must call for this measure to be real.
*
* `capability.wired.test.ts` requires something reachable from `app/` to
* import it. A file path could not carry that check once the engine moved
* into `@bitbaum/speechkit`: nothing in this repo imports a package's
* internal file, it imports the package's name for the thing.
*/
entry: string;
};
/** A measure's `module` as a URL a reader can open. */
export function sourceUrl(module: string): string {
const [repo, ...path] = module.split("/");
return `https://github.com/bitbaum/${repo}/blob/main/${path.join("/")}`;
}
/**
* The five, in the order a learner meets them.
*
* Delivery first because it works everywhere and is the honest core; grammar
* and pronunciation last because they are the two a buyer asks about, and the
* page should answer those questions having already shown its working.
*/
export const MEASURES: readonly Measure[] = [
{ id: "delivery", needs: "signal", module: "speechkit/src/delivery.ts", entry: "measureDelivery" },
{ id: "fluency", needs: "words", module: "speechkit/src/spoken.ts", entry: "measureSpoken" },
{ id: "words", needs: "words", module: "heidi/lib/variety/check.ts", entry: "check" },
{
id: "grammar",
needs: "words",
needsGrammarService: true,
module: "speechkit/src/languagetool.ts",
entry: "checkGrammar",
},
{ id: "pronunciation", needs: "words", refused: true, module: "—", entry: "—" },
];
/**
* What a variety offers a measure.
*
* `bridge` is the interesting value and the reason this is four-valued rather
* than a boolean. In a diglossic situation the learner has two varieties in
* play, and "we cannot check your Zurich German grammar, we can check the
* Standard German you will actually speak at the doctor's desk" is both true
* and the more useful half — but only if the product says which one it did.
*/
export type Verdict = "target" | "bridge" | "none" | "refused";
const RANK: Record<Needs, number> = { signal: 0, none: 0, "meaning-only": 1, words: 2 };
export type VarietySpeech = {
recognition: Recognition;
bridgeRecognition?: Recognition;
/** A grammar service code for the variety itself, or null if none covers it. */
grammarCode: string | null;
bridgeGrammarCode?: string | null;
};
function satisfies(measure: Measure, recognition: Recognition | undefined, grammarCode: string | null): boolean {
if (measure.needs === "signal") return true;
if (!recognition) return false;
if (RANK[evidenceFrom(recognition)] < RANK[measure.needs]) return false;
if (measure.needsGrammarService && !grammarCode) return false;
return true;
}
/**
* The verdict for one measure against one variety's capabilities.
*
* Order matters: the target variety is preferred over the bridge wherever both
* would work, because the bridge is a fallback and reporting it when the real
* thing is available would understate what the product does.
*/
export function verdictFor(measure: Measure, speech: VarietySpeech): Verdict {
if (measure.refused) return "refused";
if (satisfies(measure, speech.recognition, speech.grammarCode)) return "target";
if (satisfies(measure, speech.bridgeRecognition, speech.bridgeGrammarCode ?? null)) return "bridge";
return "none";
}
export function verdicts(speech: VarietySpeech): Array<{ id: MeasureId; verdict: Verdict }> {
return MEASURES.map((measure) => ({ id: measure.id, verdict: verdictFor(measure, speech) }));
}
/**
* Is there anything to show at all?
*
* A variety with no recognition in either direction still gets delivery, so
* this is never false today — it exists so a future pack that somehow has
* neither does not render an empty panel with a confident heading over it.
*/
export function anyMeasureAvailable(speech: VarietySpeech): boolean {
return verdicts(speech).some((v) => v.verdict === "target" || v.verdict === "bridge");
}