/** * Tiny Clef smoke check. Default is dry (settings + choose-transition + * run-toward-goal short-circuit path). Pass `{ live: true }` for one cheap * noul evaluation (and multi-event / multi-branch when billing works). * * @param input.live - When true, call Clef with a trivial greeting noul question. * @returns Setup status, dry checks, or live results without secrets. * * @example * import smokeTest from 'kody:@kody/clef/smoke-test' * const dry = await smokeTest() * const live = await smokeTest({ live: true }) */ import evaluate from './evaluate.ts' import chooseTransition from './choose-transition.ts' import runTowardGoal from './run-toward-goal.ts' import buildChoiceFromEvents from './build-choice-from-events.ts' import { missingSetup, secretsNewUrl } from './resolve.ts' import { SmokeTestInputSchema, formatSchemaError, parse } from './schema.ts' import clefSettings from './settings.ts' import type { DeclarativeMachine } from './types.ts' export type SmokeTestInput = { live?: boolean } const LINEAR_MACHINE: DeclarativeMachine = { id: 'smoke-linear', initial: 'start', states: { start: { on: { NEXT: 'middle' } }, middle: { description: 'Halfway', on: { FINISH: { target: 'done', description: 'Finish the path' } }, }, done: { type: 'final', description: 'Goal reached' }, }, } const BRANCH_MACHINE: DeclarativeMachine = { id: 'smoke-branch', initial: 'idle', states: { idle: { on: { GO_LEFT: { target: 'left', description: 'Take the left branch' }, GO_RIGHT: { target: 'right', description: 'Take the right branch' }, }, }, left: { description: 'Left branch', on: { DONE: 'done' }, }, right: { description: 'Right branch', on: { DONE: 'done' }, }, done: { type: 'final', description: 'Finished' }, }, } /** * Tiny Clef smoke check. Default is dry (settings + choose-transition + * run-toward-goal short-circuit path). Pass `{ live: true }` for one cheap * noul evaluation (and multi-event / multi-branch when billing works). * * @param input.live - When true, call Clef with a trivial greeting noul question. * @returns Setup status, dry checks, or live results without secrets. * * @example * import smokeTest from 'kody:@kody/clef/smoke-test' * const dry = await smokeTest() * const live = await smokeTest({ live: true }) */ export default async function smokeTest(input: SmokeTestInput = {}) { try { input = parse(SmokeTestInputSchema, input ?? {}) as SmokeTestInput } catch (error) { return { mode: 'dry' as const, ok: false as const, error: formatSchemaError(error), } } const settingsRaw = await clefSettings() if ( settingsRaw && typeof settingsRaw === 'object' && 'ok' in settingsRaw && (settingsRaw as { ok?: boolean }).ok === false ) { return { mode: 'dry' as const, ok: false as const, error: String((settingsRaw as { error?: string }).error || 'settings failed'), } } const settings = settingsRaw as { accountId?: string gatewayId?: string model?: string sources?: unknown } const ready = Boolean(settings.accountId) // Dry choose-transition: zero / single / multi shape (no CF for zero+single; // multi build is pure via build-choice-from-events). const zero = await chooseTransition({ state: { value: 'idle' }, goal: 'Do something', events: {}, }) const single = await chooseTransition({ state: { value: 'idle' }, goal: 'Advance', events: { NEXT: 'Go to the next state' }, }) const built = buildChoiceFromEvents({ RETRY: 'Retry the step', ESCALATE: 'Hand off to a human', }) const chooseTransitionDry = { zeroEvents: { ok: zero.ok, hasError: !zero.ok && typeof zero.error === 'string', }, singleEventShortCircuit: { ok: single.ok, event: single.ok ? single.event : undefined, confidence: single.ok ? single.confidence : undefined, shortCircuited: single.ok ? single.shortCircuited === true : false, }, multiEventChoiceShape: { eventIds: built.eventIds, hasChoiceQuestion: built.questions.transition?.type === 'choice' && Object.keys(built.criteria).length === 2, }, } // Dry run-toward-goal: linear one-path machine → every step short-circuits. const linear = await runTowardGoal({ machine: LINEAR_MACHINE, goal: 'Reach the done state', }) const runTowardGoalDry = { linearShortCircuit: { ok: linear.ok, finalState: linear.finalState, stoppedReason: linear.stoppedReason, pathLength: linear.path.length, allShortCircuited: linear.path.every((s) => s.shortCircuited === true), path: linear.path.map((s) => ({ state: s.state, event: s.event })), }, } if (!input.live) { return { mode: 'dry' as const, ready, settings: { accountIdSet: ready, gatewayId: settings.gatewayId, model: settings.model, sources: settings.sources, }, chooseTransition: chooseTransitionDry, runTowardGoal: runTowardGoalDry, setup: ready ? undefined : missingSetup(''), secretsNewUrl, } } if (!ready) { return { mode: 'live' as const, ok: false as const, error: 'cloudflareAccountId is not set', chooseTransition: chooseTransitionDry, runTowardGoal: runTowardGoalDry, setup: missingSetup(''), secretsNewUrl, } } const result = await evaluate({ state: 'hello', questions: { greeting: { type: 'noul', instructions: 'Is this a greeting?', criteria: { true: 'A greeting or hello', false: 'Not a greeting', }, }, }, }) if (!result.ok) { return { mode: 'live' as const, ok: false as const, status: result.status, error: result.error, chooseTransition: chooseTransitionDry, runTowardGoal: { ...runTowardGoalDry, liveMultiBranchSkipped: result.status === 402 ? 'HTTP 402 — dry linear short-circuit only; multi-branch needs billing' : 'live evaluate failed; skipped multi-branch run-toward-goal', }, setup: result.setup, secretsNewUrl, billingNote: result.status === 402 ? 'HTTP 402 — Workers AI / AI Gateway billing needs credits (or Workers AI enabled); dry choose-transition and run-toward-goal checks still passed. Clef bills via Workers AI (not TypeSafe BYOK).' : undefined, } } const liveChoose = await chooseTransition({ state: { value: 'awaitingPayment', context: { retries: 1 } }, goal: 'Collect payment or escalate if stuck', events: { RETRY: 'Retry the payment charge', ESCALATE: 'Notify a human operator', }, }) const liveRun = await runTowardGoal({ machine: BRANCH_MACHINE, goal: 'Prefer the left branch and finish', maxSteps: 5, }) return { mode: 'live' as const, ok: true as const, status: result.status, model: result.model, answers: result.answers, usage: result.usage, meta: result.meta, chooseTransition: { ...chooseTransitionDry, liveMultiEvent: liveChoose.ok ? { ok: true as const, event: liveChoose.event, confidence: liveChoose.confidence, shortCircuited: liveChoose.shortCircuited === true, } : { ok: false as const, error: liveChoose.error, status: liveChoose.status, }, }, runTowardGoal: { ...runTowardGoalDry, liveMultiBranch: liveRun.ok ? { ok: true as const, finalState: liveRun.finalState, stoppedReason: liveRun.stoppedReason, path: liveRun.path.map((s) => ({ state: s.state, event: s.event, shortCircuited: s.shortCircuited === true, })), } : { ok: false as const, error: liveRun.error, status: liveRun.status, finalState: liveRun.finalState, stoppedReason: liveRun.stoppedReason, pathLength: liveRun.path.length, }, }, } }