# Be readable by visiting agents

An agent that visits a page wants facts it can quote: what is sold, at what
price, whether it is in stock, and where to ask. `<AgentSurface>` puts the
workspace's stated facts in your page head as schema.org JSON-LD, with links
to the signed facts document, the campaign manifest and `llms.txt`.

```tsx
// app/products/mug/page.tsx (a Next.js server component)
import { AgentSurface } from "@sparkletree/react/server";

export default function MugPage() {
  return (
    <>
      <AgentSurface
        apiBase="https://p.qrtree.ai"
        organizationId={process.env.SPARKLETREE_ORG!}
        publishableKey={process.env.SPARKLETREE_KEY}
        campaignId={process.env.SPARKLETREE_CAMPAIGN}
        products={["stoneware-mug"]}
      />
      <main>…</main>
    </>
  );
}
```

Nothing in it is generated. It is built only from the facts the workspace
has on record.

## What the edge already serves

Campaign pages and brand pages served by the SparkleTree edge carry this for
agents on their own host, with no work on your side:

| Address | What it is |
|---|---|
| `/llms.txt` | The map of everything the host offers agents: pages, data endpoints, how to ask. |
| `/.well-known/agent-card.json` | A description of the host and its `ask` skill (also at `/.well-known/agent.json`). Plain HTTP and JSON. |
| `/sitemap.xml` | Every public campaign page, as `/c/{campaign-id}/{slug}`. |
| `/c/{campaign-id}/{slug}` | The campaign page, with schema.org JSON-LD. Ask for `Accept: text/markdown` to get a Markdown version of the same facts. |
| `/c/{campaign-id}/manifest.json` | The public campaign manifest: campaign facts, offer and product terms, calls to action, with a content hash for citation. Public campaigns only. |
| `/api/embed/v1/facts?campaign={id}` | The stated facts, signed (see [Show prices and other facts](https://docs.sparkletree.io/developers/sdk/facts.md)). |
| `/.well-known/sparkletree-keys.json` | The public keys that sign facts. |
| `/ask` | Questions about a campaign or the brand: `POST` with JSON, or `GET /ask?q=…` for clients that can only fetch. See [Let visitors ask](https://docs.sparkletree.io/developers/sdk/ask.md). |
| `/.well-known/api-catalog`, `/.well-known/openapi.json` | The host's public APIs. |

These answer any origin (`Access-Control-Allow-Origin: *`), so an agent in a
browser can read them too. When the workspace has switched its assistant
off, the agent card lists no skills and `llms.txt` leaves out how to ask.

## The same on your own site

`<AgentSurface>` is exported from `@sparkletree/react/server`. It reads the
facts on your server and renders, with nothing visible:

- one `<script type="application/ld+json">` with a schema.org graph;
- `<link>` elements to the facts document, the campaign manifest and the
  edge's `llms.txt`.

As an async server component it takes the options of `getAgentSurface`.
Where a renderer cannot render async components, read the surface first and
pass it in; it then renders synchronously:

```tsx
import { AgentSurface, getAgentSurface } from "@sparkletree/react/server";

const surface = await getAgentSurface({ apiBase, organizationId, publishableKey, campaignId });

<AgentSurface surface={surface} />
```

It renders nothing when the facts could not be read, and it never throws
while rendering. Render it in the page head.

Outside React, `getAgentSurface` from `@sparkletree/core/server` returns
the parts, and `serializeJsonLd` writes the JSON for a script element
safely:

```ts
import { getAgentSurface, serializeJsonLd } from "@sparkletree/core/server";

const surface = await getAgentSurface({
  apiBase: "https://p.qrtree.ai",
  organizationId: ORG,
  publishableKey: process.env.SPARKLETREE_KEY,
  campaignId: CAMPAIGN,
});

const head = surface.jsonLd
  ? `<script type="application/ld+json">${serializeJsonLd(surface.jsonLd)}</script>` +
    surface.links.map((l) => `<link rel="${l.rel}" href="${l.href}" type="${l.type}">`).join("")
  : "";
```

| Option | |
|---|---|
| `apiBase` | Required. The edge. |
| `organizationId` | Required. Your workspace id. |
| `publishableKey` | Your `st_pk_` key. Pass it: a server sends no `Origin`, so the edge reads facts for a server caller only with a valid key. Development builds warn without it. |
| `campaignId`, `surfaceId`, `products` | What to read, as for `fetchFacts`. |
| `signal`, `fetchImpl` | As elsewhere. |

The result is `{ facts, jsonLd, links, reason? }`. `facts` is the facts
document, or `null` with `reason` when it could not be read; `jsonLd` and
`links` are then `null` and empty. `getAgentSurface` never rejects.

`serializeJsonLd` writes `<`, `>`, `&`, U+2028 and U+2029 as `\u` escapes,
so no stated value can close the script element. The result parses to the
same value. `buildAgentJsonLd(facts)` builds the graph from a facts document
you already have.

## The JSON-LD

For a campaign with one product, the graph reads:

```json
{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Organization",
      "@id": "https://kiln.example#organization",
      "name": "Kiln & Co",
      "url": "https://kiln.example"
    },
    {
      "@type": "Product",
      "name": "Stoneware mug",
      "description": "A heavy mug, fired twice.",
      "sku": "MUG-01",
      "url": "https://kiln.example/mug",
      "image": "https://kiln.example/mug.jpg",
      "brand": { "@id": "https://kiln.example#organization" },
      "offers": {
        "@type": "Offer",
        "price": "249.00",
        "priceCurrency": "SEK",
        "availability": "https://schema.org/InStock",
        "url": "https://kiln.example/mug"
      }
    },
    {
      "@type": "AskAction",
      "name": "Ask about this campaign",
      "target": {
        "@type": "EntryPoint",
        "urlTemplate": "https://p.qrtree.ai/ask",
        "httpMethod": "POST",
        "contentType": "application/json"
      }
    }
  ]
}
```

- The `Organization` is identified by its website on record, or by
  `urn:sparkletree:organization:{id}` when there is none.
- Each product is a `Product` with the members the workspace has stated.
  Its `Offer` carries the price only when it is a plain decimal,
  `priceCurrency`, and `availability` only for `in_stock` (`InStock`) and
  `out_of_stock` (`OutOfStock`). A product with none of these has no offer.
- The `AskAction` is there only when the facts document has an `ask` entry:
  the campaign is public on the edge and the workspace's assistant is on.

## The links

| `rel` | `href` | When |
|---|---|---|
| `describedby` | The facts URL that was read, `application/json`. | Always, with facts. |
| `alternate` | `{apiBase}/c/{campaign-id}/manifest.json`, `application/json`. | When the campaign is public on the edge. |
| `alternate` | `{apiBase}/llms.txt`, `text/plain`. | Always, with facts. |

## How an agent checks the facts

The JSON-LD in your page is a copy, and a copy can be edited by anyone who
serves the page. The signed original is the facts document the
`describedby` link points to. An agent checks it like this:

1. Fetch the facts URL. The document's `signature` names a `keyId`.
2. Fetch `/.well-known/sparkletree-keys.json` from the SparkleTree host
   that served the facts, and find the key whose `kid` is that `keyId`.
3. Remove `signature`, write the rest as canonical JSON (RFC 8785), and
   verify `signature.value` with Ed25519 against the key's `x`.

`verifyFacts` from `@sparkletree/core` does steps 2 and 3:

```ts
import { fetchFacts, verifyFacts } from "@sparkletree/core";

const { facts } = await fetchFacts({ apiBase: "https://p.qrtree.ai", organizationId, publishableKey, campaignId });
const { valid, keyId, reason } = await verifyFacts(facts, { apiBase: "https://p.qrtree.ai" });
```

It fetches keys only from a SparkleTree host over https (or loopback); keys
from any other host prove nothing about who signed. To verify against keys
you hold, pass `keys` and nothing is fetched. When the edge has no signing
key, `signature` is `null` and `verifyFacts` resolves
`{ valid: false, reason: "unsigned" }`.

## How an agent asks

The `AskAction` and the facts document's `ask` entry name the edge's
`/ask`. An agent posts `{ "question": "…", "campaign": "{campaign-id}" }`
and gets `{ answer, campaign, links, homeCampaignId, session }` back; it
sends `session` with the next question to continue. The rules are on
[Let visitors ask](https://docs.sparkletree.io/developers/sdk/ask.md).

## What it does not do

- **No `llms.txt`, agent card or sitemap on your host.** The links point at
  the edge's documents, which describe the edge's own pages, not your site.
- **No signature on your page.** The JSON-LD is unsigned; the signed facts
  are at the `describedby` URL.
- **No copy, and nothing generated.** Only stated facts go in. Adapted
  copy on your page is not described.
- **No reviews, ratings, prices in other currencies or offers you did not
  state.** The graph has the members the facts have, and no others.
- **No refresh.** It is what the facts said when your server rendered the
  page. A page cached for a day says what was true when it was cached; the
  facts endpoint itself is cached for 60 seconds.
- **No answers on your own host.** Questions go to the edge's `/ask`.
