/** * Shared Cloudflare Clef question and answer TypeScript types. * Import when you need typed `questions` / `answers` shapes without calling the model. */ /** Boolean probability question (System One `noul`). */ export type NoulQuestion = { type: 'noul' instructions: string criteria?: { true?: string false?: string } } /** Single-choice question with a criteria map of option → description. */ export type ChoiceQuestion = { type: 'choice' instructions: string criteria: Record } /** Ordered rubric score question with a criteria array. */ export type ScoreQuestion = { type: 'score' instructions: string criteria: string[] } export type Question = NoulQuestion | ChoiceQuestion | ScoreQuestion export type Questions = Record export type ClefState = string | Record | unknown[] export type NoulAnswer = { type: 'noul' noul: number } export type ChoiceAnswer = { type: 'choice' choice: string confidence: number probabilities: Record } export type ScoreAnswer = { type: 'score' score: number confidence: number legend: Record probabilities: Record } export type Answer = NoulAnswer | ChoiceAnswer | ScoreAnswer export type ClefUsage = { input_tokens?: number output_tokens?: number [key: string]: unknown } /** Parsed model body returned by Cloudflare Clef. */ export type ClefResult = { model: string answers: Record usage?: ClefUsage } export type EvaluateInput = { state: ClefState questions: Questions /** Override Cloudflare account id for this call. */ accountId?: string /** Override AI Gateway id (default `kody` / package settings). */ gatewayId?: string /** * Override model id. Accepts short `clef` / `clef-flash` or full Workers AI * ids `@cf/cloudflare/clef` / `@cf/cloudflare/clef-flash`. * Default: `@cf/cloudflare/clef-flash`. */ model?: string } export type EvaluateSuccess = { ok: true status: number answers: Record model: string usage?: ClefUsage /** Gateway / account ids used (never secrets). */ meta: { accountIdSet: boolean gatewayId: string model: string bodyModel: 'clef' | 'clef-flash' } } export type EvaluateFailure = { ok: false status?: number error: string setup?: { secretsNewUrl?: string settingsHint?: string } } export type EvaluateResult = EvaluateSuccess | EvaluateFailure export type ClefRequestInput = { state: ClefState questions: Questions accountId?: string gatewayId?: string model?: string /** Extra headers for progressive disclosure (never log secrets). */ headers?: Record } export type ClefRequestResult = { ok: boolean status: number /** Raw Cloudflare `/ai/run/@cf/...` JSON (or text fallback). */ data: unknown error?: string setup?: { secretsNewUrl?: string settingsHint?: string } } /** Enabled events for `./choose-transition` / `./build-choice-from-events`. */ export type TransitionEventListItem = { id: string description: string } export type TransitionEvents = | Record | Array /** * Input for `./choose-transition`. * Pass a serializable machine snapshot (string, `{ value, context? }`, etc.) — * no XState runtime dependency required. */ export type ChooseTransitionInput = { /** Current machine/agent state snapshot. */ state: ClefState /** Desired outcome / what we are trying to achieve. */ goal: string /** Enabled events as Choices (id → description, or `{ id, description }[]`). */ events: TransitionEvents /** Optional extra context merged into the evaluate state blob. */ context?: unknown accountId?: string gatewayId?: string model?: string } export type ChooseTransitionSuccess = { ok: true /** Chosen enabled event id. */ event: string confidence: number probabilities: Record answers: Record model: string usage?: ClefUsage /** True when a single enabled event skipped the Clef call (confidence 1). */ shortCircuited?: boolean meta?: { accountIdSet: boolean gatewayId: string model: string bodyModel: 'clef' | 'clef-flash' } } export type ChooseTransitionFailure = { ok: false error: string status?: number answers?: Record model?: string usage?: ClefUsage setup?: { secretsNewUrl?: string settingsHint?: string } } export type ChooseTransitionResult = ChooseTransitionSuccess | ChooseTransitionFailure /** Default stored model: full Workers AI id. Body uses short `clef-flash`. */ export const DEFAULT_CLEF_MODEL = '@cf/cloudflare/clef-flash' export const DEFAULT_GATEWAY_ID = 'kody' /** * Type metadata overview for Clef question/answer shapes. * Prefer importing named types from this module; call this for a safe inventory. * * @returns Type inventory for agents (no secrets). * * @example * import describeClefTypes from 'kody:@kody/clef/types' * const meta = await describeClefTypes() */ export default async function describeClefTypes() { return { packageId: 'clef', exportName: './types', description: 'Cloudflare Clef question/answer types: noul, choice, score; evaluate, choose-transition, and run-toward-goal (declarative machine) shapes.', questionTypes: ['noul', 'choice', 'score'], defaultModel: DEFAULT_CLEF_MODEL, defaultBodyModel: 'clef-flash', defaultGatewayId: DEFAULT_GATEWAY_ID, models: { flash: { fullId: '@cf/cloudflare/clef-flash', bodyModel: 'clef-flash', size: '9B', note: 'Prefer for latency; package default', }, precision: { fullId: '@cf/cloudflare/clef', bodyModel: 'clef', size: '27B', note: 'Highest-precision decisions', }, }, } } /** Transition target: bare state name or `{ target, description? }`. */ export type DeclarativeTransition = | string | { target: string description?: string } /** One state node in a declarative machine (no XState runtime). */ export type DeclarativeStateNode = { description?: string type?: 'final' on?: Record } /** * Declarative machine shape for `./run-toward-goal`. * Chat authors this; the runner walks it toward a goal via `./choose-transition`. */ export type DeclarativeMachine = { id?: string initial: string states: Record } export type RunTowardGoalInput = { /** Declarative machine (`initial` + `states` with `on` transitions). */ machine: DeclarativeMachine /** Desired outcome / what we are trying to achieve. */ goal: string /** Optional context passed into choose-transition state snapshots. */ context?: unknown /** Max transitions before stopping (default 20). */ maxSteps?: number accountId?: string gatewayId?: string model?: string } export type RunTowardGoalPathStep = { /** State we were in when the event fired. */ state: string /** Chosen enabled event id. */ event: string confidence?: number shortCircuited?: boolean } export type RunTowardGoalStoppedReason = | 'final' | 'noEvents' | 'cycle' | 'maxSteps' | 'error' export type RunTowardGoalSuccess = { ok: true path: RunTowardGoalPathStep[] finalState: string stoppedReason: Exclude } export type RunTowardGoalFailure = { ok: false path: RunTowardGoalPathStep[] finalState: string stoppedReason: 'error' error: string status?: number setup?: { secretsNewUrl?: string settingsHint?: string } } export type RunTowardGoalResult = RunTowardGoalSuccess | RunTowardGoalFailure