# Agent Primitives

Agent Primitives lets an agent publish a bundle of infrastructure from one JSON manifest. A bundle can be an app, static site, scheduler, API, or harness.

## When to use this

If the user asks “agentprimitive, set up my tasks app”, publish the tasks preset. For other tasks, select a bundle type and compose the required primitives. Make a real authenticated HTTP request. Return the resulting preview or action URL after verifying the response.

## Environment and auth

- This site's API is **https://2zx6cxzb5g.execute-api.us-east-1.amazonaws.com** (production).
- Start with `GET https://2zx6cxzb5g.execute-api.us-east-1.amazonaws.com/v1/catalog`. It returns the environment, manifest version, supported primitives, bundle types and fully expanded tasks preset.
- Read the API origin and bearer from the agent's approved runtime configuration. Use `AGENT_PRIMITIVES_URL` and `AGENT_PRIMITIVES_TOKEN`. Never insert a writer token into HTML, Markdown, a user-visible reply, a browser link, or command logs.
- Send `Authorization: Bearer <environment token>` on every control API request. If the token is absent, request a credential through the user's approved secret service. Avoid switching environments unless requested.
- Published bundles are private. A browser preview URL grants access to one bundle for up to one hour. A tasks preview also permits editing that bundle’s task records. Share it only with the intended user. Refresh it with authenticated `GET /v1/bundles/{slug}`.

## Publish the tasks app

Send this exact request shape. Choose a stable lowercase slug for the intended app. To retry, use the same slug and body.

```sh
curl -fsS https://2zx6cxzb5g.execute-api.us-east-1.amazonaws.com/v1/bundles \
  -H "Authorization: Bearer $AGENT_PRIMITIVES_TOKEN" \
  -H 'Content-Type: application/json' \
  --data-binary '{"version":1,"slug":"my-tasks","preset":"tasks","title":"My tasks"}'
```

The same request can be sent with `agentprimitive setup tasks --app my-tasks --title 'My tasks'` after installing the CLI from the project repository. From a checkout, use `python3 -m scratchpad setup tasks --app my-tasks`.

A successful receipt contains `environment`, `revision`, `manifest_url`, `app_url`, `preview_url`, and `rows_url`. Confirm that `environment` matches your selection. Open the `preview_url` and expect HTTP 200. Return the preview URL to the intended user. The stable `app_url` requires a current grant.

## Compose a bundle

| Kind | Required manifest fields | Example |
| --- | --- | --- |
| `app` | `fe`, `db`, `function`. The built-in tasks template also needs the title/done schema and create/update/delete functions. | [Tasks app](/examples/app.json) |
| `static-site` | `fe` with `template:"static"` and inline HTML up to 32 KiB. | [Static site](/examples/static-site.json) |
| `scheduler` | `cron`, `function`. The interval is defined in UTC minutes. | [Scheduler](/examples/scheduler.json) |
| `api` | `api`, `function`. Bind each named route to a named function. | [API](/examples/api.json) |
| `harness` | Markdown `context`, 1–16 declarative `checks`, and the corresponding read-only functions. | [Harness](/examples/harness.json) |

Send `{"version":1,"slug":"example","kind":"…","title":"…","primitives":{…}}` to `POST /v1/bundles`. Include only known keys. Manifests are immutable. Reposting the same effective manifest returns its original revision. A different manifest at the same slug returns 409. Choose a new slug for a different bundle.

## Primitive contracts

- **fe** uses `{"template":"tasks"}` or `{"template":"static","html":"…" }`. The tasks frontend edits the same DB records as the API. Inline static HTML runs in a read-only browser sandbox.
- **function** maps a name to `echo`, `count`, `list`, `create`, `update`, or `delete`. Call `POST /v1/bundles/{slug}/functions/{name}`. Send a JSON object. The platform supplies these operations. There is no uploaded-code execution API.
- **db** uses `{"fields":{"title":"string","done":"boolean"}}`. Types are `string`, `boolean`, `integer`, and `number`. Support is capped at 128 records and 512 KiB per bundle. The base path is `/v1/bundles/{slug}/db/rows`. GET lists rows. PUT creates with `{"id":"task-1","data":{"title":"Write docs","done":false}}`. Identical repeats of the same ID and body are safe. GET `/{id}` returns the record's version. PATCH `/{id}` accepts `{"version":"…","data":{"done":true}}`. DELETE `/{id}` accepts `{"version":"…"}`.
- **cron** uses `{"everyMinutes":5,"function":"ping","input":{}}`. The interval is 1–1440 minutes aligned to UTC epoch minutes. Use an `echo` or `count` function. Read the latest result at the receipt's `runs_url`. `latest:null` means it has not fired. Repeated or late copies of a tick do not replace a newer result.
- **api** maps a route name to `{"method":"POST","function":"echo"}`. Invoke `/a/{slug}/{route}` with the environment bearer. Methods match operations. POST for echo/create, GET for count/list, PATCH for update, DELETE for delete. Mutation functions take the same row data and version fields as direct CRUD.
- **files** maps filenames to inline text. A bundle can contain up to 12 Markdown, JSON, CSS, or text files at 4 KiB each. Replace the path of a valid preview with `/apps/{slug}/files/{filename}` and keep its `access` query string. For raw HTML, Markdown, JSON, or raster images up to 1 MiB, use the existing file API `PUT /v1/apps/{app}/files/{filename}`.
- **auth** uses `{"visibility":"private"}` by default. Keep the bearer on the agent side. Never place it in a preview link.

## Verify and recover

- `201` means a new bundle or row was created. `200` with the same revision means the bundle already existed with that manifest.
- `401 unauthenticated` means recover the correct environment token before retrying.
- `409 bundle_exists` means read the existing manifest and choose a new slug unless it matches the intended bundle.
- `412 row_version_conflict` means re-read the row, merge the intended change, and send the new current version.
- `422 invalid_manifest` or `invalid_row` means validate the manifest or data against the declared shape. Do not retry unchanged.
- For `429`, back off. After a connection failure or `503`, fetch the current resource first. Identical creates can be replayed. A row update with an uncertain outcome requires reading back the value before reconciliation.
- For a harness, read `context_url`, then POST `run_url`. HTTP 200 requires `passed:true`. A 422 result includes the checks and actual values.

## Machine-readable resources

- [OpenAPI](/openapi.json)
- [llms.txt](/llms.txt)
- [Full Markdown](/llms-full.txt)
- [Agent documentation](/docs/)
