| 1 | /// <reference types="@cloudflare/workers-types" /> |
| 2 | |
| 3 | import type { AsyncWorkerEnv } from "./stack.ts"; |
| 4 | import { seedVectors, vector } from "./vectors.ts"; |
| 5 | |
| 6 | const LABEL = "async"; |
| 7 | |
| 8 | /** |
| 9 | * Async (non-Effect) Worker style: the Vectorize index is declared on the |
| 10 | * Worker `env` (`env: { INDEX: TestIndex }` in stack.ts). `WorkerAsyncBindings` |
| 11 | * resolves it via the `isIndex` predicate and registers the native |
| 12 | * `vectorize` binding, so the handler calls the runtime `Vectorize` binding |
| 13 | * (`env.INDEX.upsert(...)`, `.describe()`, `.query(...)`, `.getByIds(...)`) |
| 14 | * directly with plain `await`. |
| 15 | * |
| 16 | * `InferEnv` has no Vectorize mapping yet, so `env.INDEX` is typed as the |
| 17 | * alchemy resource at compile time; cast to the runtime `Vectorize` binding. |
| 18 | */ |
| 19 | export default { |
| 20 | async fetch(request: Request, env: AsyncWorkerEnv): Promise<Response> { |
| 21 | const url = new URL(request.url); |
| 22 | const index = env.INDEX as unknown as Vectorize; |
| 23 | |
| 24 | if (request.method === "GET" && url.pathname === "/health") { |
| 25 | return Response.json({ ok: true }); |
| 26 | } |
| 27 | |
| 28 | if (request.method === "POST" && url.pathname === "/upsert") { |
| 29 | const mutation = await index.upsert(seedVectors(LABEL)); |
| 30 | return Response.json({ mutationId: mutation.mutationId }); |
| 31 | } |
| 32 | |
| 33 | if (request.method === "GET" && url.pathname === "/describe") { |
| 34 | const info = await index.describe(); |
| 35 | return Response.json({ |
| 36 | dimensions: info.dimensions, |
| 37 | vectorCount: info.vectorCount, |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | if (request.method === "GET" && url.pathname === "/query") { |
| 42 | const matches = await index.query(vector(0.1, 0.2, 0.3), { |
| 43 | topK: 20, |
| 44 | returnMetadata: "all", |
| 45 | }); |
| 46 | const mine = matches.matches.filter((m) => m.id.startsWith(`${LABEL}-`)); |
| 47 | return Response.json({ |
| 48 | count: mine.length, |
| 49 | ids: mine.map((m) => m.id), |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | if (request.method === "GET" && url.pathname === "/query-filtered") { |
| 54 | const matches = await index.query(vector(0.1, 0.2, 0.3), { |
| 55 | topK: 3, |
| 56 | returnMetadata: "all", |
| 57 | filter: { kind: { $eq: "second" } }, |
| 58 | }); |
| 59 | const mine = matches.matches.filter((m) => m.id.startsWith(`${LABEL}-`)); |
| 60 | return Response.json({ |
| 61 | count: mine.length, |
| 62 | ids: mine.map((m) => m.id), |
| 63 | kinds: mine.map( |
| 64 | (m) => (m.metadata as { kind?: string } | undefined)?.kind, |
| 65 | ), |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | if (request.method === "GET" && url.pathname === "/get") { |
| 70 | const vectors = await index.getByIds([`${LABEL}-a`, `${LABEL}-b`]); |
| 71 | return Response.json({ ids: vectors.map((v) => v.id).sort() }); |
| 72 | } |
| 73 | |
| 74 | return new Response("Not Found", { status: 404 }); |
| 75 | }, |
| 76 | }; |