// Serves Cloudflare's Clef-Flash decision model behind Jev's SystemOne API // (POST /systemone), so web-extract can use it by changing its base URL and key. // Workers AI bills Clef by input tokens; on the Workers Free plan the daily free // allocation is a hard cap; past it calls fail and web-extract uses its fallback // model (PUBLIC_JEV_FALLBACK_URL). interface Env { AI: Ai DECISION_KEY: string } const MAX_BODY = 200_000 // Clef answers at most this many questions per call. const MAX_QUESTIONS = 64 type ClefResult = { answers?: Record usage?: { input_tokens?: number; output_tokens?: number } } async function sameSecret(a: string, b: string): Promise { const enc = new TextEncoder() const [x, y] = await Promise.all([ crypto.subtle.digest('SHA-256', enc.encode(a)), crypto.subtle.digest('SHA-256', enc.encode(b)), ]) return crypto.subtle.timingSafeEqual(x, y) } const error = (status: number, message: string) => Response.json({ error: message }, { status }) export default { async fetch(request: Request, env: Env): Promise { const url = new URL(request.url) if (url.pathname !== '/systemone') return error(404, 'not found') if (request.method !== 'POST') return error(405, 'use POST') const auth = request.headers.get('authorization') ?? '' if (!env.DECISION_KEY || !auth.startsWith('Bearer ') || !(await sameSecret(auth.slice(7), env.DECISION_KEY))) return error(401, 'invalid key') const text = await request.text() if (text.length > MAX_BODY) return error(413, 'request too large') let body: { state?: unknown; questions?: unknown } try { body = JSON.parse(text) } catch { return error(400, 'body must be JSON') } const questions = (body.questions ?? {}) as Record const ids = Object.keys(questions) const batches: string[][] = [] for (let i = 0; i < ids.length; i += MAX_QUESTIONS) batches.push(ids.slice(i, i + MAX_QUESTIONS)) try { // Each batch carries the whole state, so a long call costs one state per batch. const results = await Promise.all( batches.map( (batch) => env.AI.run('@cf/cloudflare/clef-flash' as keyof AiModels, { model: 'clef-flash', state: body.state, questions: Object.fromEntries(batch.map((id) => [id, questions[id]])), } as never) as Promise, ), ) const answers: Record = {} const usage = { input_tokens: 0, output_tokens: 0 } for (const result of results) { Object.assign(answers, result.answers) usage.input_tokens += result.usage?.input_tokens ?? 0 usage.output_tokens += result.usage?.output_tokens ?? 0 } return Response.json({ model: 'clef-flash', answers, usage }) } catch (e) { const message = e instanceof Error ? e.message : String(e) // Out of daily allocation or capacity: web-extract falls back to its other model. const status = /3036|3040|capacity|neuron|limit/i.test(message) ? 429 : 502 return error(status, message.slice(0, 300)) } }, }