Open History to see the last commit
1import serverEntry from '@tanstack/react-start/server-entry'
2
3import { getDb } from '#/db/index.ts'
4import type { DbEnv } from '#/db/index.ts'
5import { isAdminRequest } from '#/server/admin.ts'
6import { getAuth } from '#/server/auth.ts'
7import type { KvEnv } from '#/server/kv.ts'
8import type { ProviderEnvironment } from '#/server/providers/types.ts'
9import { withDiscoveryLinks } from '#/server/discovery-links.ts'
10import {
11 markdownForPath,
12 markdownResponse,
13 wantsMarkdown,
14} from '#/server/markdown-pages.ts'
15import { narrativeRoot, wantsJsonRoot } from '#/server/narrative.ts'
16import { enforceRateLimit } from '#/server/rate-limit.ts'
17import { pollAllProviders } from '#/server/ingest/poll-models.ts'
18import {
19 FAL_PRICING_EXTRACT_CRONS,
20 extractFalPricing,
21} from '#/server/ingest/extract-fal-pricing.ts'
22import {
23 SPEC_SYNC_SHARD_CRONS,
24 specSyncShard,
25 syncAllProviders,
26} from '#/server/ingest/sync.ts'
27import { recordCompleteness } from '#/server/status.ts'
28import { runWebhookTick } from '#/server/webhooks.ts'
29import type { SyncDeps } from '#/server/ingest/sync.ts'
30
31// Not exported: workerd treats named exports of the entry module as
32// entrypoints and requires them to be functions/ExportedHandlers.
33const MODELS_POLL_CRON = '*/15 * * * *'
34
35export interface WorkerEnv extends DbEnv, KvEnv, ProviderEnvironment {
36 ADMIN_KEY?: string
37}
38
39function syncDeps(env: WorkerEnv): SyncDeps {
40 return { db: getDb(env), kv: env.SCHEMA_CACHE, secrets: env }
41}
42
43/**
44 * Public reads are keyless JSON meant for any client — including browser
45 * apps (the hosted examples fetch /v1/schemas live). CORS-open GETs on the
46 * read surface; admin and auth routes are excluded.
47 */
48function isCorsReadPath(pathname: string): boolean {
49 return (
50 (pathname.startsWith('/v1/') && !pathname.startsWith('/v1/admin/')) ||
51 pathname === '/openapi.json' ||
52 pathname === '/llms.txt'
53 )
54}
55
56function withCors(response: Response): Response {
57 // New Response: framework responses can carry immutable headers.
58 const wrapped = new Response(response.body, response)
59 wrapped.headers.set('Access-Control-Allow-Origin', '*')
60 return wrapped
61}
62
63export default {
64 async fetch(
65 request: Request,
66 env: WorkerEnv,
67 ctx: ExecutionContext,
68 ): Promise<Response> {
69 // Rate limit the public API (admin-keyed requests are exempt; auth
70 // endpoints have agent-auth's own protections).
71 const url = new URL(request.url)
72 const { pathname } = url
73 const corsRead =
74 isCorsReadPath(pathname) &&
75 (request.method === 'GET' || request.method === 'HEAD')
76 if (request.method === 'OPTIONS' && isCorsReadPath(pathname)) {
77 return new Response(null, {
78 status: 204,
79 headers: {
80 'Access-Control-Allow-Origin': '*',
81 'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS',
82 'Access-Control-Allow-Headers': 'Content-Type, If-None-Match',
83 'Access-Control-Max-Age': '86400',
84 },
85 })
86 }
87 if (
88 pathname.startsWith('/v1/') &&
89 !isAdminRequest(request, env.ADMIN_KEY)
90 ) {
91 const limited = await enforceRateLimit(
92 getAuth(),
93 getDb(env),
94 env.SCHEMA_CACHE,
95 request,
96 )
97 if (limited) return corsRead ? withCors(limited) : limited
98 }
99 // Narrative API root (task 11.2): JSON-accepting GET / gets the MCP
100 // initialize instructions document instead of the SSR landing page.
101 if (pathname === '/' && wantsJsonRoot(request)) {
102 const body = Response.json(narrativeRoot(url.origin))
103 body.headers.set('Vary', 'Accept')
104 return body
105 }
106 // Markdown content negotiation (task 10.5): Accept: text/markdown on an
107 // HTML page returns markdown instead of SSR.
108 if (wantsMarkdown(request)) {
109 const md = markdownForPath(pathname, url.origin)
110 if (md !== null) return markdownResponse(md)
111 }
112 // TanStack Start's handler takes (request, opts?) — bindings flow in via
113 // the cloudflare:workers env module, not handler arguments. HTML pages
114 // get RFC 8288 discovery Link headers (task 10.2).
115 void ctx
116 const response = withDiscoveryLinks(await serverEntry.fetch(request))
117 return corsRead ? withCors(response) : response
118 },
119
120 scheduled(
121 controller: ScheduledController,
122 env: WorkerEnv,
123 ctx: ExecutionContext,
124 ): void {
125 // The models poll runs the full registry in one invocation; the spec
126 // sync is sharded across several cron firings so each shard gets its
127 // own subrequest/CPU/memory budgets (see SPEC_SYNC_SHARD_CRONS). FAL
128 // rate-card extract runs hourly 06:00-11:00 UTC, each firing with its
129 // own budget (FAL_PRICING_EXTRACT_CRONS). Work inside each invocation stays
130 // sequential with per-provider isolation.
131 if (controller.cron === MODELS_POLL_CRON) {
132 ctx.waitUntil(
133 pollAllProviders(syncDeps(env))
134 .then(async (outcomes) => {
135 console.log(JSON.stringify({ job: 'models-poll', outcomes }))
136 // Score completeness once per poll; requests only read the
137 // record. It logs and keeps the old record rather than throw.
138 await recordCompleteness(getDb(env))
139 // The 15-min cron also drains the webhook queue (task 6.2).
140 return runWebhookTick(getDb(env))
141 })
142 .then(({ enqueued, outcomes }) => {
143 console.log(JSON.stringify({ job: 'webhooks', enqueued, outcomes }))
144 }),
145 )
146 return
147 }
148 const shard = (SPEC_SYNC_SHARD_CRONS as ReadonlyArray<string>).indexOf(
149 controller.cron,
150 )
151 if (shard >= 0) {
152 ctx.waitUntil(
153 syncAllProviders(syncDeps(env), specSyncShard(shard)).then(
154 (outcomes) => {
155 // Summarize warnings to a count: hundreds of dangling-ref lines
156 // push the blob past what Workers Logs stores, losing the log.
157 const summary = outcomes.map(({ warnings, ...rest }) => ({
158 ...rest,
159 warningCount: warnings.length,
160 }))
161 console.log(
162 JSON.stringify({ job: 'spec-sync', shard, outcomes: summary }),
163 )
164 },
165 ),
166 )
167 return
168 }
169 if (
170 (FAL_PRICING_EXTRACT_CRONS as ReadonlyArray<string>).includes(
171 controller.cron,
172 )
173 ) {
174 ctx.waitUntil(
175 extractFalPricing(syncDeps(env)).then((outcome) => {
176 console.log(JSON.stringify({ job: 'fal-pricing-extract', outcome }))
177 }),
178 )
179 return
180 }
181 console.log(JSON.stringify({ cron: controller.cron, job: 'unknown-cron' }))
182 },
183}