返回 CodeWhale
worker.ts
根目录 / web / worker.ts
1 import { DurableObject } from "cloudflare:workers";
2 import handler from "./.open-next/worker.js";
3 import type { AgentEnv } from "./lib/community-agent-tasks";
4 import { runFactsDrift } from "./lib/facts-drift";
5 import { runLinkCheck, runSemanticDrift } from "./lib/content-watch";
6 import { fetchWithStaticInstaller } from "./lib/static-installer";
7 import { applyDraftLock, type DraftLockRequest } from "./lib/draft-claim-lock";
8
9 /**
10 * Exclusive claim on one community-agent draft (binding DRAFT_CLAIM_LOCK, one
11 * instance per draft via idFromName). OpenNext's custom-worker entry exports
12 * extra Durable Object classes from here; see lib/draft-claim-lock.ts.
13 */
14 export class DraftClaimLock extends DurableObject {
15 async act(req: DraftLockRequest) {
16 return applyDraftLock(this.ctx.storage, Date.now(), req);
17 }
18 }
19
20 export default {
21 fetch(request, env, ctx) {
22 return fetchWithStaticInstaller(
23 request,
24 env,
25 ctx,
26 (nextRequest, nextEnv, nextCtx) => handler.fetch(nextRequest, nextEnv, nextCtx),
27 );
28 },
29 async scheduled(event: ScheduledEvent, env: Record<string, unknown>, ctx: ExecutionContext) {
30 const expr = event.cron;
31 ctx.waitUntil((async () => {
32 const agentEnv = env as unknown as AgentEnv;
33 if (expr === "0 */6 * * *") {
34 await runFactsDrift(agentEnv);
35 }
36 else if (expr === "0 0 * * *") {
37 await runLinkCheck(agentEnv);
38 await runSemanticDrift(agentEnv);
39 }
40 })());
41 },
42 } satisfies ExportedHandler;
43
43 lines TYPESCRIPT