The best way to control Claude Code, Codex and more from any computer or phone

main
ts 28 lines 1016 Bytes
Raw
1 import type { K2AsyncWorkerEnv } from "./k2-async-stack.ts";
2
3 const encoder = new TextEncoder();
4
5 /**
6 * `POST /send?run=R&count=N` appends N records through `env.ORDERS.send`
7 * (the native `k2` binding) and answers 202, or 500 with the binding's
8 * error when the append failed.
9 */
10 export default {
11 async fetch(request: Request, env: K2AsyncWorkerEnv): Promise<Response> {
12 const url = new URL(request.url);
13 if (request.method !== "POST" || url.pathname !== "/send") {
14 return new Response("Not Found", { status: 404 });
15 }
16 const run = url.searchParams.get("run") ?? "default";
17 const count = Number(url.searchParams.get("count") ?? "1");
18 const result = await env.ORDERS.send(
19 Array.from({ length: count }, (_, index) => ({
20 content: encoder.encode(`${run}:${index}`),
21 headers: { run },
22 })),
23 );
24 return result.success
25 ? Response.json({ produced: count }, { status: 202 })
26 : Response.json({ error: result.error }, { status: 500 });
27 },
28 };