# 4O2 — for all you

4O2 (4o2.org) is pronounced *for all you*. It helps small web tools use AI. The goal is to keep AI compute accessible like water, electricity, and bandwidth. Once a hosted slot is configured and authorized, a static page can call it with one line of JavaScript, without provider API keys or a backend of its own. Available shared capacity can cover inference costs for small tools.

## Start with one HTML file

Save this example as `index.html` and open it in a browser with network access to load the script. It uses a local hook to check the SDK wiring with sample data.

```html
<!doctype html>
<meta charset="utf-8">
<title>4O2 demo</title>
<button id="try">Try the local hook</button>
<pre id="result"></pre>
<script src="https://4o2.org/4o2.js"></script>
<script>
  const client = O4O2.connect({ appId: "local-demo", maxTier: "t1-lite" });
  const result = document.querySelector("#result");

  document.querySelector("#try").onclick = async () => {
    try {
      const value = await client.run("demo", { text: "hello" }, {
        preferLocal: (_slot, { text }) => ({ characterCount: text.length })
      });
      result.textContent = JSON.stringify(value);
    } catch (error) {
      result.textContent = error.message;
    }
  };
</script>
```

The classic script at `https://4o2.org/4o2.js` exposes `window.O4O2`. For ES modules, import the default `O4O2` or named functions from `https://4o2.org/4o2.mjs`. Both builds include four authorization helpers that use the browser's Web Crypto API and have no dependencies. `connect({ appId, authOrigin, maxTier })` synchronously returns an app-scoped client. `authOrigin` defaults to `https://4o2.org` and `maxTier` is optional. Top-level `O4O2.run` and `O4O2.feedback` use the latest connection settings.

## Slots

A hosted slot defines the input fields and output limits for a task. A `strict_json` slot returns structured data. A `light_vision` slot handles small image-understanding tasks within the slot's bounds. Use the registered slot identifier and supply the payload it expects.

## Authorize hosted calls

Register the HTTPS app origin, its callback URL, and a hosted slot. Use the SDK helpers to prepare the passkey consent and token exchange.

1. Call `await client.createPkceChallenge()` to get a verifier and challenge that tie the authorization request to the code exchange.
2. Call `await client.createDpopSigner()` to create the key pair used to sign hosted requests.
3. Call `client.authorizeUrl({ redirectUri, codeChallenge, state, dpopJkt: signer.jkt })` to build the consent URL. Keep the verifier, a fresh random `state`, and the signer's `keyPair` across the redirect.
4. On return, verify that the returned state matches the one you saved. Reuse the key pair with `await client.createDpopSigner(keyPair)`, then call `await client.exchangeCode({ code, codeVerifier, redirectUri, dpopJkt: signer.jkt })` for the grant.

Pass the grant's `access_token` and `signer.sign` to `run`. The signer produces a fresh DPoP proof for every request.

```js
const output = await client.run(slotId, { input: "Describe this sample." }, { grantToken: grant.access_token, dpopSigner: signer.sign });
```

The SDK defaults to `/v1/run` at `authOrigin`. Set `endpoint` only to a service you trust with the grant and payload. For local-first execution, pass a hook as `preferLocal`, or set `preferLocal: true` with `localRunner`. Returning `undefined` allows the call to proceed to the endpoint. A thrown hook error ends the call. The SDK returns parsed JSON for buffered calls and exposes failed HTTP status as `error.status`. Pass `stream: true` for a raw SSE Response with `X-Receipt-Id`. You can supply `dpopSigner: async ({ method, url, ath }) => proof` to sign each request.

## Live APIs

The S0–S5 backend APIs are live on `https://4o2.org`.

| Endpoint | What it does |
| --- | --- |
| `POST /v1/run` | Runs a hosted slot with an app grant and a fresh DPoP proof. |
| `POST /v1/dev/apps/:appId/checkup` | Checks whether a slot is ready to run, using the developer owner token. |
| `POST /v1/mcp` | Exposes developer tools over MCP with the same owner token. |
| `GET /v1/explore/apps` | Lists public apps. |
| `GET /v1/community/posts` | Lists community posts. |

## Feedback events

For a registered app, send a rating with `client.feedback({ slotId, kind: "rating_submitted", detail: { rating: 5 } })`. Use a registered slot ID when available and an integer from 1 to 5. The SDK sends `protocolVersion: "v1"`, `stageOrigin: "S3"`, and category `explicit_feedback` to `POST /v1/events/feedback`. The endpoint supports CORS `*` and returns `202` with `eventId` and `receivedAt`.

Keep observations (`behavior_signal`), user ratings (`explicit_feedback`), and diagnoses (`ai_inference`) separate. The shared protocol uses `schema_version: 1` and validates the envelope and category-specific facts. Keep prompts, responses, credentials, personal information, and free text out of all feedback. The SDK strips some sensitive field names. Send only the expected numbers, IDs, and enums.

## Capacity and pool

Read `GET /v1/pool/status` for aggregate capacity and active policy flags. The founder pool uses pay-as-you-go API capacity. Scheme 1 disables personal donation and cross-project overflow by default.

Send daily units per user to `POST /v1/estimate`, for example `{"daily_input_units":800,"daily_output_units":200,"concurrent_users":5}`. The response includes the policy version, horizon in days, available public light-tier capacity, per-user budget, feasible users and headroom. See `workers/API_CONTRACT.md` for the complete HTTP schemas.
