Workers

The gateway as a Cloudflare Worker.

meted deploy writes src/worker.ts into your repository. About a hundred lines, and yours to edit.

import { parseConfig } from '@meted/config'
import { createBaselineEngine } from '@meted/engine-interface'
import { createGateway } from '@meted/gateway'

export interface Env {
  OPENAI_API_KEY?: string
  METED_MODE?: string
  METED_PROVIDER_BASE_URL?: string
}

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext,
  ): Promise<Response> {
    const gateway = await gatewayFor(env, ctx)
    return gateway.app.fetch(request, env, ctx)
  },
}

The same Hono app runs behind meted dev on a laptop. The only difference is that ctx.waitUntil carries the tail of a streamed response past the point where the caller has been served.

Isolate reuse

The gateway is built once per isolate and cached, and rebuilt when the credential changes, so a rotated secret is picked up without a redeploy.

Finishing after the response

waitUntil: (promise) => ctx.waitUntil(promise)

A streamed response is measured as it drains, which happens after the caller already has their bytes. waitUntil keeps the isolate alive for that; locally it is a tracked promise the process waits for on shutdown.

The Sufficiency Engine

Workers cannot import by dynamic path, so the engine is wired in statically:

import engineWasm from './engine.wasm'
import { createWasmEngine } from '@meted/engine-interface'

async function resolveEngine() {
  return createWasmEngine({ module: engineWasm })
}

The scaffold ships with the open-source baseline engine and the WASM wiring commented above it. Either engine runs inside your Worker. No prompt is sent anywhere to be scored.

Compatibility flags

compatibility_date = "2025-01-01"
compatibility_flags = ["nodejs_compat"]

nodejs_compat is required. Meted uses node:-prefixed built-ins in a few places that Workers provides under that flag.

Local development against the Worker

cd meted-cloudflare
wrangler dev

Runs the Worker in workerd. Closer to production than meted dev, and slower to iterate on.

Logs

wrangler tail

The gateway logs nothing. What you see is Wrangler's own request log plus any console calls you add.

Was this page helpful?