Spaces:
Sleeping
Sleeping
Download web/src/lib/run.ts from TBM99/CoverageCheck: direct link, hf CLI and curl.
- Browser
- Download file 4.87 kB
-
https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/web/src/lib/run.ts
- Command line
-
hf download hf://spaces/TBM99/CoverageCheck/web/src/lib/run.ts
-
curl -L -o run.ts https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/web/src/lib/run.ts
4.87 kB
| // What the Ask page shows, as a pure function of the events seen so far (live or a | |
| // recorded replay go through the same reducer, so they look the same). | |
| import type { AnswerEvent, AskEvent, Passage, StepDetail, SystemId } from './types' | |
| export type StepStatus = 'pending' | 'running' | 'done' | 'skipped' | |
| export type StepView = { | |
| name: string | |
| status: StepStatus | |
| ms: number | null | |
| detail: StepDetail | null | |
| } | |
| export type RunState = { | |
| phase: 'idle' | 'running' | 'done' | 'error' | |
| system: SystemId | null | |
| day: string | null | |
| live: boolean // System C reports each step; A and B are one call (no progress midway) | |
| steps: StepView[] | |
| evidence: Passage[] | null | |
| answer: AnswerEvent | null | |
| error: string | null | |
| rewrite: { question: string; changed: boolean } | null // a follow-up, as understood | |
| } | |
| export const idle: RunState = { | |
| phase: 'idle', | |
| system: null, | |
| day: null, | |
| live: false, | |
| steps: [], | |
| evidence: null, | |
| answer: null, | |
| error: null, | |
| rewrite: null, | |
| } | |
| /** A run starting before any event (the request is on its way). */ | |
| export function starting(system: SystemId): RunState { | |
| return { ...idle, phase: 'running', system } | |
| } | |
| /** The pending step after the last finished one starts running (C: one node at a time). */ | |
| function advance(steps: StepView[]): StepView[] { | |
| const next = steps.findIndex((s) => s.status === 'pending') | |
| return steps.map((s, i) => (i === next ? { ...s, status: 'running' } : s)) | |
| } | |
| function skip(steps: StepView[], names: string[]): StepView[] { | |
| return steps.map((s) => | |
| names.includes(s.name) && s.status !== 'done' ? { ...s, status: 'skipped' } : s, | |
| ) | |
| } | |
| /** C's conditional steps, known as soon as the step before them reports: the second | |
| * search runs only when the judge found a fact no passage states; the second judge | |
| * only when that search found new passages. */ | |
| function skipped(name: string, detail: StepDetail): string[] { | |
| if (name === 'judge' && detail.sufficient) return ['research', 'judge_new'] | |
| if (name === 'research' && detail.new_candidates === 0) return ['judge_new'] | |
| return [] | |
| } | |
| export function reduce(state: RunState, event: AskEvent): RunState { | |
| // a finished run stays finished (9b audit: an error after the answer flipped it) | |
| if ((state.phase === 'done' || state.phase === 'error') && event.type !== 'start') return state | |
| switch (event.type) { | |
| case 'start': { | |
| const live = event.system === 'c' | |
| const steps = event.steps.map((name) => ({ | |
| name, | |
| // A and B run their steps inside one call: all "running" until the answer | |
| status: (live ? 'pending' : 'running') as StepStatus, | |
| ms: null, | |
| detail: null, | |
| })) | |
| return { ...idle, phase: 'running', system: event.system, day: event.day, live, steps: live ? advance(steps) : steps } | |
| } | |
| case 'step': { | |
| const at = state.steps.findIndex((s) => s.name === event.name) | |
| if (at < 0) return state | |
| let steps = state.steps.map((s, i): StepView => { | |
| if (i === at) return { ...s, status: 'done', ms: event.ms, detail: event.detail } | |
| // a step before this one that never reported did not run (e.g. no candidates) | |
| if (i < at && s.status !== 'done') return { ...s, status: 'skipped' } | |
| return s.status === 'running' ? { ...s, status: 'pending' } : s | |
| }) | |
| steps = advance(skip(steps, skipped(event.name, event.detail))) | |
| return { ...state, steps } | |
| } | |
| case 'rewrite': | |
| return { ...state, rewrite: { question: event.question, changed: event.changed } } | |
| case 'evidence': | |
| return { ...state, evidence: event.passages } | |
| case 'answer': { | |
| const steps = state.steps.map((s): StepView => { | |
| if (s.status === 'done' || s.status === 'skipped') return s | |
| // A/B: their steps all ran inside the call; C: one never reported did not run | |
| return { ...s, status: state.live ? 'skipped' : 'done' } | |
| }) | |
| return { ...state, phase: 'done', steps, answer: event } | |
| } | |
| case 'error': { | |
| const steps = state.steps.map((s): StepView => (s.status === 'running' ? { ...s, status: 'pending' } : s)) | |
| return { ...state, phase: 'error', steps, error: event.message } | |
| } | |
| } | |
| } | |
| export const ENDED = 'The connection closed before an answer arrived. Please try again.' | |
| /** The stream closed: a run still "running" then never got its answer or error (a | |
| * server restart, a proxy closing the connection), so it ends in an error instead of | |
| * spinning forever (9b audit). A finished run is unchanged. */ | |
| export function ended(state: RunState): RunState { | |
| return state.phase === 'running' ? reduce(state, { type: 'error', kind: 'ended', message: ENDED }) : state | |
| } | |
| /** The step running now (for its live timer), if any. */ | |
| export function running(state: RunState): StepView | undefined { | |
| return state.steps.find((s) => s.status === 'running') | |
| } | |