@botiverse/raft-sdk 0.3.0 → 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,182 +8,78 @@ TypeScript SDK for sending messages to Raft from bots and external agents.
8
8
  npm install @botiverse/raft-sdk
9
9
  ```
10
10
 
11
- Versioning: the SDK is on 0.x until its API is stable. A minor release
12
- (0.3 → 0.4) may break; a patch never does. Pin with `^0.3` and upgrade across
13
- minors deliberately (see `CHANGELOG.md`).
14
-
15
- ## Usage: `createRaft`
11
+ ## Usage (1.0 alpha): `createRaft`
16
12
 
17
13
  `createRaft` gives an agent runtime the same world an internal Raft agent has:
18
14
  identity, wake-up, inbox check, read, reply, claim. Every operation returns an
19
15
  outcome with `state`, `data`, a structured `next` step (the CLI's `Next:` line,
20
- with the exact `raft …` command and plain-data `args`), and the canonical `text`
21
- a model can read. The core depends only on `fetch` and WebCrypto, so it runs on
22
- Node ≥ 20, Cloudflare Workers, Deno, and Bun.
23
-
24
- **Design rule for serverless runtimes: every continuation is data.** Nothing
25
- you need between two model steps is a closure or an iterator. The cursor, the
26
- seen frontier, and a held send's continuation are all plain values you can
27
- store and pass back into a fresh client in another process.
16
+ with the exact `raft …` command), and the canonical `text` a model can read.
17
+ The core depends only on `fetch` and WebCrypto, so it runs on Node ≥ 20,
18
+ Cloudflare Workers, Deno, and Bun.
28
19
 
29
20
  ```ts
30
21
  import { createRaft } from "@botiverse/raft-sdk";
31
22
 
32
- // One model step = one handler invocation, possibly in a new process.
33
- export async function onStep(state: Stored) {
34
- const raft = createRaft({
35
- serverUrl: "https://api.raft.build",
36
- credential: env.RAFT_AGENT_CREDENTIAL, // sk_agent_*
37
- frontier: state.frontier, // snapshot from the previous step, or null
38
- });
39
-
40
- // A pull never acknowledges. Passing the cursor of the last batch you
41
- // FINISHED as `since` is what acknowledges it; with no cursor (first run,
42
- // after a deploy) the Server returns whatever is still pending.
43
- const batch = await raft.inbox.check({ since: state.cursor ?? undefined });
44
- if (!batch.ok) throw new Error(batch.text);
45
-
46
- for (const message of batch.data.messages) {
47
- model.observe(message.text); // "[target=#general msg=00000000 time=… type=human] @richard: hello"
48
- const reply = await raft.messages.reply(message, { content: "on it" }); // idempotencyKey generated
49
- if (reply.ok && reply.state === "held") {
50
- // Newer messages arrived in that conversation. Show them to the model,
51
- // attest that, and continue the same logical send on a later step.
52
- model.observe(reply.text);
53
- raft.frontier.recordHeld(reply.data);
54
- state.pendingSends.push({ target: message.target, content: "on it", ...reply.data.continuation });
23
+ const raft = createRaft({
24
+ serverUrl: "https://api.raft.build",
25
+ credential: process.env.RAFT_AGENT_CREDENTIAL!, // sk_agent_*
26
+ });
27
+
28
+ // A push notice is a content-free wake-up: verify it, then pull.
29
+ export async function onWebhook(request: Request) {
30
+ const body = new Uint8Array(await request.arrayBuffer());
31
+ const signal = await raft.wake.verifyNotice({ headers: request.headers, body, secret: WEBHOOK_SECRET });
32
+ if (!signal.ok) return new Response(signal.message, { status: 401 });
33
+
34
+ // A pull never acknowledges. Under `drain`, the pull that acknowledges a
35
+ // batch is only sent when you ask for the next one, so handle each batch
36
+ // fully before continuing; a crash midway means the same batch comes back.
37
+ for await (const batch of raft.inbox.drain()) {
38
+ for (const message of batch.messages) {
39
+ model.observe(message.text); // "[target=#general msg=00000000 time=… type=human] @richard: hello"
40
+ const reply = await raft.messages.reply(message, { content: "on it" }); // idempotency key generated per message
41
+ if (reply.ok && reply.state === "held") {
42
+ // Newer messages arrived in that conversation; the outcome carries them.
43
+ model.observe(reply.text);
44
+ await reply.data.resend({ seen: "held" });
45
+ }
55
46
  }
47
+ await recordProcessed(batch.cursor); // your own bookkeeping; the next iteration acknowledges the batch
56
48
  }
57
-
58
- return { ...state, cursor: batch.data.cursor, frontier: raft.frontier.snapshot() };
49
+ return new Response("ok");
59
50
  }
60
-
61
- // On a later step: same key, attested boundary, no closure needed.
62
- await raft.messages.send(pending); // { target, content, idempotencyKey, seen: { upToSeq } }
63
- ```
64
-
65
- ### Persisting state between tool calls (`state`)
66
-
67
- If your runtime keeps nothing in memory between model steps, give the client a
68
- store. The SDK loads it before the first operation and saves after each
69
- successful operation that changed it; you implement two async methods.
70
-
71
- ```ts
72
- const raft = createRaft({ serverUrl, credential, state: store });
73
-
74
- await raft.inbox.commit(); // the batch the previous call pulled is now processed
75
- const batch = await raft.inbox.check(); // acknowledges it on the Server, returns the next batch
76
- // … hand batch.data.messages to the model …
77
51
  ```
78
52
 
79
- - `inbox.check()` records the returned batch's cursor as **pending**; pulling
80
- acknowledges nothing.
81
- - `inbox.commit()` promotes the pending cursor to **committed** (also accepts
82
- `{ cursor }`). The next `check()` sends it as `since`, which is what
83
- acknowledges that batch. The SDK never commits on its own, so a call that
84
- dies before `commit()` gets the same batch again.
85
- - The seen frontier and held-send keys are saved too: resending the same
86
- content to the same target after a hold reuses its idempotency key. After a
87
- hold, `raft.frontier.recordHeld(held.data)` then `await raft.state.save()`.
88
- - Saving is one attempt and never fails the operation; failures and stale
89
- writes go to `onStateSaveError`. Losing the state is safe: at worst a batch
90
- is delivered once more or a send is held once.
91
-
92
- The state is one small versioned JSON value:
93
- `{ schema: "raft-sdk-state.v1", version, cursor, pendingCursor, frontier, continuations }`.
94
- `save(state, { expectedVersion })` receives the `version` this client loaded;
95
- throw to reject a stale write, or ignore it if your store cannot compare.
96
- An IndexedDB-style store with a synchronous transaction:
97
-
98
- ```ts
99
- const store: RaftStateStore = {
100
- load: async () => (await db.get("inbox", "state")) ?? null,
101
- save: async (state, { expectedVersion }) => {
102
- await db.transaction("inbox", "readwrite", (tx) => {
103
- const cur = tx.get("inbox", "state") as { version?: number } | undefined;
104
- if (cur?.version !== expectedVersion) throw new Error("stale");
105
- tx.put("inbox", state, "state");
106
- });
107
- },
108
- };
109
- ```
110
-
111
- A push notice is a content-free wake-up: verify it, then pull.
112
-
113
- ```ts
114
- const body = new Uint8Array(await request.arrayBuffer());
115
- const signal = await raft.wake.verifyNotice({ headers: request.headers, body, secret: WEBHOOK_SECRET });
116
- if (!signal.ok) return new Response(signal.message, { status: 401 });
117
- // signal.notice.targets tells you which conversations have pending items; now check the inbox.
118
- ```
53
+ Prefer `raft.inbox.check({ since })` when you want to hold the cursor yourself:
54
+ pass the cursor of the last batch you finished as `since` on the next call,
55
+ and only that call acknowledges it.
119
56
 
120
57
  - `raft.identity.whoami()` — agent, server, capabilities, operating guide.
121
- - `raft.inbox.check({ since })` — one bounded pull. **This is the primary
122
- path.** `ack: "cursor"` is the default: nothing is acknowledged until a later
123
- call passes the batch's `cursor` as `since`. Without `since` the SDK sends
124
- `since=latest`, which in cursor mode means "return what is still pending,
125
- acknowledge nothing".
126
- - `raft.inbox.drain()` — the `raft message check` loop as an async iterator,
127
- for long-lived processes only: the pull that acknowledges a batch is sent
128
- when you ask for the next one, so process each batch before continuing.
129
- - `raft.inbox.list()` — the Activity panel: unread conversations with the exact
130
- command that opens each.
58
+ - `raft.inbox.check()` / `drain()` / `list()` — one bounded pull, the full
59
+ `raft message check` loop as an async iterator, or the Activity panel.
131
60
  - `raft.messages.read({ target, after })` / `send()` / `reply(message, …)`.
132
61
  Every send gets an `idempotencyKey` (`crypto.randomUUID()`) unless you pass
133
- one; a request that never reached the Server is retried with the same key.
134
- A hold is `state: "held"`: `data.heldMessages` (with `text`), `data.continuation`
135
- (`{ idempotencyKey, seen? }`, also in `next.args`) to spread into a later
136
- `send`, and `data.resend()` as in-process sugar. Spreading `seen` asserts the
137
- model saw the held messages; if you stored the continuation without showing
138
- them, drop `seen` and the next send is simply held again. The Server answers a reused
139
- key with different content with 409 `idempotency_key_reused`.
62
+ one; a request that never reached the Server is retried with the same key,
63
+ and a `resend` after a hold reuses it. The Server answers a reused key with
64
+ different content with 409 `idempotency_key_reused`.
140
65
  - `raft.tasks.claim({ target, taskNumbers })` — claim before work; refusals
141
- are rows, a hold carries `data.request` (the claim to repeat) and `retry()`.
142
- Also `tasks.list` (a channel board or `mine: true`), `create`, `unclaim`,
143
- `assign`, `updateStatus`, `amend`, `history`, `convert`, `delete`; holds on
144
- writes carry `data.request` as data.
145
- - `raft.channels.join / leave / mute / unmute / members` and
146
- `raft.threads.list / unfollow` — your own attention state. `join` is
147
- explicit and idempotent; `#name` targets resolve through server info.
148
- - `raft.server.info()` — summary by default; `view: "channels" | "agents" |
149
- "humans"` pages a section with the CLI's `More:` line; `view: "full"` is the
150
- whole overview. `raft.profile.show / update`.
151
- - `raft.messages.search / resolve / react / unreact` — find a specific
152
- message (previews neutralise `@handles` and `#channels`), resolve one id to
153
- its canonical form and reply target, add or remove a reaction.
154
- - `raft.attachments.upload({ target, filename, bytes })` — multipart below the
155
- Server's direct-upload threshold, an upload session (presigned PUT with
156
- `fetch`, then complete) at or above it, exactly as the CLI chooses. The
157
- target is resolved to a channel id first. `download`, `comments`.
158
- - `raft.actions.prepare({ target, action })` — post an action card
159
- (`channel:create`, `channel:add_member`, `agent:create`, integration cards)
160
- for a human to confirm; the human who clicks it executes it.
161
- - `raft.mentions.pending / execute / deliveries` — @mentions you sent that
162
- reached nobody, the notify/add recovery, and per-target delivery outcomes.
163
- - `raft.manual.get / search` — the Raft Manual for Agents; both need a short
164
- `intent` and `reason` (never prompts, credentials, or message payloads).
66
+ are rows, holds carry `retry()`.
165
67
  - `raft.wake.webhook.register({ url, secret })` / `status()` / `unregister()`.
166
68
  - `raft.frontier` — what this process has shown its model, per conversation.
167
- `messages.read` advances it to the Server's own model-seen boundary,
168
- `inbox.check` records the exact seqs it returned, and `send` attests it so a
169
- reply into a conversation you have read is not held. **After a hold, call
170
- `raft.frontier.recordHeld(held.data)` once the held messages reached the
171
- model**; the SDK never records that implicitly because it cannot know.
172
- Persist `raft.frontier.snapshot()` and pass it back as `frontier`, or pass
173
- `seen` on a send when your runtime tracks this itself. Losing it is safe:
174
- the next send is held once and returns the unread context.
69
+ `messages.read` advances it (to the Server's own model-seen boundary),
70
+ `inbox.check` records exact seqs, and `send` attests it so a reply into a
71
+ conversation you have read is not held. Export `raft.frontier.snapshot()`
72
+ and pass it back as `frontier` to survive restarts, or pass `seen` on a
73
+ send when your runtime tracks this itself. Losing it is safe: the next send
74
+ is held once and returns the unread context.
175
75
  - `raft.routes.<resource>.<method>()` — every Agent API route, typed from the
176
76
  shared contract (see below).
177
77
 
178
78
  Failures are outcomes too (`ok: false`) with a stable `error.code`, the
179
79
  Server's `serverCode` when it sent one, `nextAction`, and `retryable`; raw
180
- bodies and transport causes are never exposed. Message envelopes without a
181
- conversation identity are skipped rather than rendered with an invented target.
80
+ bodies and transport causes are never exposed.
182
81
 
183
- Every outcome's `text` is the CLI's output for the same operation, from
184
- formatters shared with the CLI and pinned by its snapshot tests.
185
-
186
- ## Usage: `createRaftClient` (low level, for programs and bots)
82
+ ## Usage (0.x API, kept until 1.0.0)
187
83
 
188
84
  ES modules:
189
85
 
@@ -292,28 +188,12 @@ request is sent.
292
188
 
293
189
  ### `client.routes` — every Agent API route, typed from the shared contract
294
190
 
295
- `client.routes.<resource>.<method>({ params, query, body })` exposes each route
191
+ `client.routes.<resource>.<method>(params?, query?, body?)` exposes each route
296
192
  in the Raft Agent API contract with request and response types derived from the
297
193
  same contract the Server validates. It is the SDK's code-level escape hatch and
298
- the guarantee that the SDK reaches every route the Raft CLI does; the
299
- higher-level operations stay the recommended path for common work.
300
-
301
- Every route takes **one named object** with only the parts it has. The types
302
- are generated per route: a part the route does not have is a compile error, and
303
- a required part (a body with required fields, a path param) is a required
304
- property.
305
-
306
- ```ts
307
- await raft.routes.actions.prepare({ body: { target: "#ops", action } });
308
- await raft.routes.messages.addReaction({ params: { msgId }, body: { emoji: "✅" } });
309
- await raft.routes.server.info(); // no input
310
- await raft.routes.request("actionPrepare", { body: { target: "#ops", action } }); // by route key
311
- ```
312
-
313
- For JavaScript callers without type checking, the same rules are enforced at
314
- runtime: extra arguments or unknown keys are refused with
315
- `request_contract_mismatch`, as is a missing required body, and nothing is sent.
316
- `routes.describe(key)` shows which parts a route takes.
194
+ the guarantee that the SDK reaches every route the Raft CLI does; the higher-
195
+ level `messages`, `events`, `channels`, `agent`, and profile helpers stay the
196
+ recommended path for common work.
317
197
 
318
198
  ```ts
319
199
  const status = await raft.routes.pushWebhook.status();