← Public packages
@kody/clef
Cloudflare Clef decision models via Workers AI (Jev-shaped noul/choice/score API).
Files
/**
* Read or update Clef Cloudflare ids in this package's storage.
* When local storage is empty, reads fall back to `@kentcdodds/ai/settings`
* then `@kentcdodds/jev/settings` so one setup can serve related packages.
* Does not duplicate the secret name — reuse user secret `cloudflareApiToken`.
*
* Model convention: store the full Workers AI id (`@cf/cloudflare/clef-flash`);
* REST maps it to body `model: "clef-flash"` and URL `/ai/run/@cf/cloudflare/clef-flash`.
*
* @param input.accountId - Cloudflare account id to save.
* @param input.gatewayId - AI Gateway id to save (default `kody`).
* @param input.model - Optional Clef model id override to save.
* @returns Current settings when reading, or `{ updated, settings }` when writing.
*
* @example
* import clefSettings from 'kody:@kody/clef/settings'
* const current = await clefSettings()
* // => { accountId, gatewayId, model, bodyModel, sources }
*/
import { packageStorage } from 'kody:runtime'
import { normalizeClefModel, resolveClefConfig, settingNames } from './resolve.ts'
import { SettingsInputSchema, formatSchemaError, parse } from './schema.ts'
function trimString(value: unknown) {
return typeof value === 'string' ? value.trim() : ''
}
export type ClefSettingsInput = {
accountId?: string
gatewayId?: string
model?: string
}
/**
* Read or update Clef Cloudflare ids in this package's storage.
* When local storage is empty, reads fall back to `@kentcdodds/ai/settings`
* then `@kentcdodds/jev/settings`. Does not duplicate the secret name —
* reuse user secret `cloudflareApiToken`.
*
* @param input.accountId - Cloudflare account id to save.
* @param input.gatewayId - AI Gateway id to save (default `kody`).
* @param input.model - Optional Clef model id override to save.
* @returns Current settings when reading, or `{ updated, settings }` when writing.
*
* @example
* import clefSettings from 'kody:@kody/clef/settings'
* const current = await clefSettings()
* // => { accountId, gatewayId, model, bodyModel, sources }
*/
export default async function clefSettings(input: ClefSettingsInput = {}) {
let parsed: ClefSettingsInput
try {
parsed = parse(SettingsInputSchema, input ?? {}) as ClefSettingsInput
} catch (error) {
return {
ok: false as const,
error: formatSchemaError(error),
}
}
const updated: Array<string> = []
const storage = packageStorage()
const accountId = trimString(parsed.accountId)
const gatewayId = trimString(parsed.gatewayId)
const modelRaw = trimString(parsed.model)
if (accountId) {
await storage.set(settingNames.accountId, accountId)
updated.push('accountId')
}
if (gatewayId) {
await storage.set(settingNames.gatewayId, gatewayId)
updated.push('gatewayId')
}
if (modelRaw) {
const normalized = normalizeClefModel(modelRaw)
await storage.set(settingNames.model, normalized.fullId)
updated.push('model')
}
const resolved = await resolveClefConfig()
const settings = {
accountId: resolved.accountId,
gatewayId: resolved.gatewayId,
model: resolved.model,
bodyModel: resolved.bodyModel,
sources: resolved.sources,
}
if (updated.length === 0) return settings
return { updated, settings }
}