# Known RPC host

Wikis are served here to sites their organization registered, as the person signed in — never to anyone without a token. A site's own document: GET https://rpc.known.info/rpc with a site's token; one of its wikis, GET https://rpc.known.info/rpc/<name>. This page as JSON: https://rpc.known.info/rpc.json

## Signing in

A site signs people in with Known through OAuth 2.0's authorization code flow with PKCE (S256). No site holds a secret.

1. A site is added to a wiki, its home: whoever can read that wiki may sign in. It also registers its exact redirect URIs, and attaches any other wikis it reads, for reading or for writing: in Known on the wiki, or `known <org> sites create <name> --wiki <wiki>`.
2. Send the person to `https://known.info/authorize` with `response_type=code`, `client_id`, `redirect_uri` (exactly as registered), `state`, `code_challenge`, `code_challenge_method=S256`, and `scope`: wiki:<name>, space-separated, each the name of a wiki attached to the site; none signs the person in with no wiki. What the site may do in each is what the wiki is attached for and the person may do (`wiki:<name>:read`, the earlier form, means the same). A refreshed token grants again what was asked, from what the person may do then. Known signs them in and sends them back with `code` and `state`.
3. `POST https://rpc.known.info/oauth/token` with `grant_type=authorization_code`, `code`, `code_verifier`, `client_id`, `redirect_uri`. The answer: `access_token` (an hour), `refresh_token`, `scope` (the wikis granted).
4. Call this host with `Authorization: Bearer <access_token>`. Refresh with `grant_type=refresh_token`, `refresh_token`, `client_id`; each refresh token works once. Sign out with `POST https://rpc.known.info/oauth/revoke` (`token`, `client_id`).

In a browser, `@future-unknown/known-vision` does all four. On a site Known publishes, every page carries its settings (the `known-site` element: client id, this host, redirect URI, wikis, these endpoints), and the session is made from them:

```js
import { sessionOfPage } from '@future-unknown/known-vision'
const session = await sessionOfPage()
await session.getState().start()   // on every page load
if (session.getState().status === 'signed-out') await session.getState().signIn()   // or from a button
```

A site hosted elsewhere passes its own: `createSessionStore({ clientId, redirectUri, scope, signIn })`, where `signIn` is this document's `signIn` (`const { signIn } = await discoverHost({ baseUrl })`).

## How to call

JSON-RPC 2.0 over `POST https://rpc.known.info/rpc`, one named-params object per call. Paths are full: the wiki's name, a colon, then the page path, dot-separated (`capital:round`; `capital:` for the root).

- Every call carries a token from Known sign-in: `Authorization: Bearer <access_token>`. A site works in the wikis attached to it as the person signed in: it may do there what that person may, within what the wiki is attached for — read, or read and write.
- On a wiki attached for writing, a site writes page content (`wiki.write` without metadata, `wiki.edit`) and records (`wiki.set`, `wiki.del`), each stamped as the site acting for the person. Moving and removing pages, page metadata (a table's key, schema and retention) and notes are changed in Known.
- Pages and records have their own verbs: a page is read with `wiki.read`; records are read with `wiki.records`, and one by its key with `wiki.get`.
- A table declared personal in Known (`metadata.personal`; `wiki.tree` shows it) holds one set of rows per person. Through a site every call there is the signed-in person's own rows — `wiki.records`, `wiki.get`, `wiki.set` and `wiki.del` as they are, with no user id in the key and no filter — and a call that names nobody reaches nobody's rows but theirs, whatever their level. A page made for the wiki's admins names whose rows it wants: `person` (a user id) on those calls reads, writes or deletes that person's rows, and `wiki.recordOwners` lists the people who hold rows; anyone but an admin of the wiki is refused both. `site.watch` tells each person of changes to their own rows alone. Writing them still needs the wiki attached for writing.
- A site calls each wiki it reads by the wiki's name (`capital`); every path starts with that name and a colon (`capital:round`, `capital:` for the root), and every result, scope and stream event names the wiki the same way. The organization is the token's.
- `actors.resolve({ ids })` names the people and sites behind the ids records carry (`_actor.id`, `_actor.onBehalfOf`): the organization's members, and anyone who appears in a wiki the person can read; anything else answers null.
- `site.whoami` says who is signed in and, for every wiki the site reads for them, what it is for, their level and what they may do there — show each person what they can reach; the server refuses the rest. GET /rpc with the token is the site's whole document.
- One live stream per signed-in person, `site.watch`, says what changed in every wiki the site reads for them, as it lands. It needs live updates on the organization's plan; without them, poll `wiki.changes`.
- `site.ask` is Known's assistant inside the site: the signed-in person asks, and it answers from the wikis the site reads for them, able to do there exactly what the site may. Known keeps no conversation: send the turns so far as `history`. It needs full intelligence on the organization's plan (`site.whoami` says `assistant.available`), and each question counts against the organization's daily allowance. For the site's managers (`assistant.siteWork`) it also changes the site's own code and publishes the site, and lists what it changed; the site's own token never can.

With curl:

```sh
curl -s https://rpc.known.info/rpc -H 'content-type: application/json' -H 'authorization: Bearer <access_token>' \
  -d '{"jsonrpc":"2.0","id":1,"method":"wiki.tree","params":{"path":"<wiki>:","depth":1}}'
```

With methodry, in a browser, signed in through `@future-unknown/known-vision`:

```js
import { createClient } from '@future-unknown/methodry'
import { methodryAuth } from '@future-unknown/known-vision'
const rpc = createClient('https://rpc.known.info', null, methodryAuth(session))
const { response: page, refused } = await rpc.wiki.read({ path: '<wiki>:' })
```

A refusal is a JSON-RPC error whose `data.refused` is true. Its codes:

- `-32602` invalid params
- `-32003` forbidden
- `-32004` not found
- `-32009` conflict

## Methods

### wiki.read

Read one page: its title, content, metadata, and revision, as it is now or as it was at a change or time. Capability: read. Returns: page.

- `path` (string): the full path of the page: the wiki name, a colon, the page (acme:about.foo)
- `changeId` (number, optional): read the page as of this change
- `at` (string, optional): read the page as of this ISO-8601 time

### wiki.tree

The page hierarchy under a path, with each page's record count. Capability: read. Returns: tree.

- `path` (string): the full path to start from; the wiki name and a colon alone (acme:) is the whole wiki
- `depth` (number, optional): how many levels down to include
- `changeId` (number, optional): the tree as of this change
- `at` (string, optional): the tree as of this ISO-8601 time

### wiki.search

Full-text search under a path: pages, and the records of tables. A record hit names its page and key. Capability: read. Returns: hit[].

- `path` (string): the full path to search under
- `query` (string): the words to find
- `limit` (number, optional): at most this many hits

### wiki.links

What links to a page, or to one of its records: the pages whose content and the records that carry a wikilink to it. A linking record comes with its value. Capability: read. Returns: links.

- `path` (string): the full path of the page
- `key` (string, optional): a record of the page, by key; without it, the page itself
- `limit` (number, optional): at most this many links

### wiki.history

How a page or a row got to be as it is, newest first: a page's revisions, or — with key — a table row's versions, each as the row that version left (no fields on a deletion) with its stamps and _change (created, updated, deleted). Capability: read. Returns: revision[] | versions.

- `path` (string): the full path of the page
- `key` (string, optional): a row of the table, by key; without it, the page itself
- `limit` (number, optional): at most this many revisions or versions
- `cursor` (string, optional): with key, continue a previous read from where it stopped
- `person` (string, optional): on a personal table, the person whose row it is, by id; your own when left out. Another person's takes admin on the wiki

### wiki.getRevision

One revision of a page by id, and the revision before it, for a diff. Capability: read. Returns: revision.

- `wiki` (string): the wiki name
- `revisionId` (string): the revision to read

### wiki.getVersion

One version of a table row and the version before it, for a diff. Capability: read. Returns: version.

- `path` (string): the full path of the table
- `key` (string): the row's key
- `version` (number): the version to read
- `person` (string, optional): on a personal table, the person whose row it is, by id; your own when left out. Another person's takes admin on the wiki

### wiki.changes

What changed in a wiki, under a path, newest first: each change with who made it, its note, and the pages it touched. With records, the writes to table records too, as batches among the changes: one writer's run of writes to one table, with how many records it wrote. Capability: read. Returns: change[].

- `path` (string): the full path to read changes under; the wiki name and a colon alone (acme:) is the whole wiki
- `limit` (number, optional): at most this many changes
- `before` (number, optional): only changes before this change id
- `records` (boolean, optional): also list the batches of writes to table records (entries with `batch` and `records` in place of `id` and `pages`)

### wiki.getChange

One change to a wiki, with the revisions it made; the latest when no id is given. Capability: read. Returns: change.

- `wiki` (string): the wiki name
- `changeId` (number, optional): the change to read

### wiki.getRecordChanges

One batch of writes to a table's records, as wiki.changes lists it with records, and its writes newest first: each record's key, what the write did (created, updated, deleted) and the version it made. Capability: read. Returns: recordChanges.

- `wiki` (string): the wiki name
- `batch` (number): the batch to read, as wiki.changes names it
- `limit` (number, optional): at most this many writes
- `before` (number, optional): only writes before this write id (a read's `next`)

### wiki.snapshot

Every page of a wiki as it was at a change or time. Capability: read. Returns: snapshot.

- `wiki` (string): the wiki name
- `changeId` (number, optional): the wiki as of this change
- `at` (string, optional): the wiki as of this ISO-8601 time

### wiki.records

Read a page's records: a range by time, paged by cursor, or the records the page held at a moment. Tables read in key order, logs in time order. One record by key is wiki.get. A personal table (metadata.personal) holds one set of rows per person: it answers with your own rows, or, for the wiki's admins, with every person's, each stamped _owner; `rows` in the answer says which. Capability: read. Returns: records.

- `path` (string): the full path of the page
- `latest` (boolean, optional): only the newest record
- `since` (string, optional): records at or after this ISO-8601 time
- `until` (string, optional): records before this ISO-8601 time
- `limit` (number, optional): at most this many records
- `cursor` (string, optional): continue a previous read from where it stopped
- `reverse` (boolean, optional): newest first, or highest key first
- `at` (string, optional): the records as they stood at this ISO-8601 time: a table whole, with `unknown` naming keys whose state then was not kept; a log in pages, the records written by then
- `person` (string, optional): on a personal table, read this person's rows alone, by id; another person's takes admin on the wiki
- `mine` (boolean, optional): on a personal table, read your own rows where you would be given everyone's

### wiki.recordOwners

The people a personal table holds rows for, by id, each with how many rows and the latest stamp among them, and how many people in all. For the wiki's admins. Capability: read. Returns: recordOwners.

- `path` (string): the full path of the personal table
- `limit` (number, optional): at most this many people
- `after` (string, optional): continue after this person's id

### wiki.get

Read one record by key: a table's row, or a log entry by its _id. Pages are read with wiki.read. On a personal table the row read is your own. Capability: read. Returns: record.

- `path` (string): the full path of the page that holds the record
- `key` (string): the record's key on a table, or its _id on a log
- `person` (string, optional): on a personal table, the person whose row it is, by id; your own when left out. Another person's takes admin on the wiki

### wiki.write

Write a page whole: create it, or replace its content, title, or metadata, guarded by the revision the caller last read. The answer's problems name the page's links that resolve to nothing. Capability: write. Returns: change.

- `path` (string): the full path of the page
- `content` (string, optional): the whole Markdown body
- `title` (string, optional): the page title
- `metadata` (object, optional): the page metadata, replaced whole
- `ifRevision` (string, optional): refuse unless the page is still at this revision
- `message` (string, optional): a note on the change

### wiki.edit

Change part of a page: replace one exact string in its content. Refused unless the string occurs exactly once, or replaceAll is given. The answer's problems name the page's links that resolve to nothing. Capability: write. Returns: change.

- `path` (string): the full path of the page
- `oldString` (string): the exact text to replace, with enough around it to occur once
- `newString` (string): the text to put in its place
- `replaceAll` (boolean, optional): replace every occurrence
- `ifRevision` (string, optional): refuse unless the page is still at this revision
- `message` (string, optional): a note on the change

### wiki.set

Write one record: on a table (a page with a key) it adds or replaces the row with that key; on a log it adds an entry. On a personal table the row written is your own unless person names another's, which takes admin on the wiki; each person may hold a capped number of rows. Records are stamped with the actor, time, and version; a table keeps every version of each row. Pages are written with wiki.write. The answer's problems name the record's links that resolve to nothing. Capability: set. Returns: record.

- `path` (string): the full path of the page
- `value` (object): the record, a JSON object; on a table it must carry the key field, and it must match the page's schema if one is declared
- `ts` (string, optional): when it happened, on a log, ISO-8601; defaults to now
- `ifVersion` (number, optional): on a table, refuse unless the row is still at this version
- `person` (string, optional): on a personal table, the person whose row this is, by id; your own when left out. Another person's takes admin on the wiki

### wiki.del

Delete one record from a page. On a personal table the row deleted is your own. Capability: set. Returns: record.

- `path` (string): the full path of the page
- `key` (string): the record's key on a table, or its _id on a log
- `person` (string, optional): on a personal table, the person whose row it is, by id; your own when left out. Another person's takes admin on the wiki

## Streams

### site.watch

Every change to the wikis this token reaches through the site, as it lands: notices, never content. Hold one per signed-in person, shared by their tabs, and read what changed with the methods above. Transport: POST /rpc with `Accept: text/event-stream`; each event's data is a JSON-RPC response whose result is the event. Plan feature: `live_updates`.

- `ready` `{ wikis }`, once: the site's names for the wikis the stream covers
- `change` `{ wiki, path, action }`: a page (`path`, null for the wiki's root) or its records changed; `action` is the wiki method that changed it
- `heartbeat` every 25 seconds while nothing changes
- `expired` `{ reason }` (token, lifetime, lease): renew the token and reconnect, then read `wiki.changes` from the last change you saw
- `revoked` `{ reason }` (plan_feature, app_reach): stop; this person may no longer hold it

### site.ask

Known's assistant answers one question, streamed: what it is doing, the answer as it is written, then the answer. Answers are filtered: no images, no HTML, links only to http(s) and mailto, each showing where it goes. Render them as Markdown without raw HTML. Transport: POST /rpc with `Accept: text/event-stream`; each event's data is a JSON-RPC response whose result is the event. Plan feature: `intelligence_full`.

- `activity` `{ activity }`: one thing the assistant is doing, reported again whenever it changes; keep the latest per `activity.id`. `{ id, kind: "thinking" | "tool" | "conversation", state: "running" | "done" | "failed", startedAt, endedAt? }` (ms since the answer began); a tool also has `method`, `effect` ("read" | "write"), `target` (`{ org, address }`, the page, record, file or site), `outcome` (`{ changed?, change?, count?, refused? }`) and, when it failed, `reason`. Never content; word it yourself
- `draft` `{ text, turn }`: the answer so far, filtered, whole each time; replace what you show. A new `turn` starts a new draft
- `result` `{ answer, activities, writes }`, last: the answer, every activity as it ended, and what it changed (`{ method, address, change? }`, in order). A stream that ends in an error lists what was changed before it in the error's `data.writes`
- `revoked` `{ reason }` (plan_feature): the plan no longer includes the assistant; the answer ends
