okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petpublic access: fall back to a second decision model inside web-extract5a5f5bc

main
3.0 KiB79 linesraw
1// Serves Cloudflare's Clef-Flash decision model behind Jev's SystemOne API
2// (POST /systemone), so web-extract can use it by changing its base URL and key.
3// Workers AI bills Clef by input tokens; on the Workers Free plan the daily free
4// allocation is a hard cap; past it calls fail and web-extract uses its fallback
5// model (PUBLIC_JEV_FALLBACK_URL).
6
7interface Env {
8	AI: Ai
9	DECISION_KEY: string
10}
11
12const MAX_BODY = 200_000
13// Clef answers at most this many questions per call.
14const MAX_QUESTIONS = 64
15
16type ClefResult = {
17	answers?: Record<string, unknown>
18	usage?: { input_tokens?: number; output_tokens?: number }
19}
20
21async function sameSecret(a: string, b: string): Promise<boolean> {
22	const enc = new TextEncoder()
23	const [x, y] = await Promise.all([
24		crypto.subtle.digest('SHA-256', enc.encode(a)),
25		crypto.subtle.digest('SHA-256', enc.encode(b)),
26	])
27	return crypto.subtle.timingSafeEqual(x, y)
28}
29
30const error = (status: number, message: string) => Response.json({ error: message }, { status })
31
32export default {
33	async fetch(request: Request, env: Env): Promise<Response> {
34		const url = new URL(request.url)
35		if (url.pathname !== '/systemone') return error(404, 'not found')
36		if (request.method !== 'POST') return error(405, 'use POST')
37		const auth = request.headers.get('authorization') ?? ''
38		if (!env.DECISION_KEY || !auth.startsWith('Bearer ') || !(await sameSecret(auth.slice(7), env.DECISION_KEY)))
39			return error(401, 'invalid key')
40		const text = await request.text()
41		if (text.length > MAX_BODY) return error(413, 'request too large')
42		let body: { state?: unknown; questions?: unknown }
43		try {
44			body = JSON.parse(text)
45		} catch {
46			return error(400, 'body must be JSON')
47		}
48		const questions = (body.questions ?? {}) as Record<string, unknown>
49		const ids = Object.keys(questions)
50		const batches: string[][] = []
51		for (let i = 0; i < ids.length; i += MAX_QUESTIONS) batches.push(ids.slice(i, i + MAX_QUESTIONS))
52		try {
53			// Each batch carries the whole state, so a long call costs one state per batch.
54			const results = await Promise.all(
55				batches.map(
56					(batch) =>
57						env.AI.run('@cf/cloudflare/clef-flash' as keyof AiModels, {
58							model: 'clef-flash',
59							state: body.state,
60							questions: Object.fromEntries(batch.map((id) => [id, questions[id]])),
61						} as never) as Promise<ClefResult>,
62				),
63			)
64			const answers: Record<string, unknown> = {}
65			const usage = { input_tokens: 0, output_tokens: 0 }
66			for (const result of results) {
67				Object.assign(answers, result.answers)
68				usage.input_tokens += result.usage?.input_tokens ?? 0
69				usage.output_tokens += result.usage?.output_tokens ?? 0
70			}
71			return Response.json({ model: 'clef-flash', answers, usage })
72		} catch (e) {
73			const message = e instanceof Error ? e.message : String(e)
74			// Out of daily allocation or capacity: web-extract falls back to its other model.
75			const status = /3036|3040|capacity|neuron|limit/i.test(message) ? 429 : 502
76			return error(status, message.slice(0, 300))
77		}
78	},
79}