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

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
@@ -13,71 +13,122 @@ npm install @botiverse/raft-sdk
13
13
  `createRaft` gives an agent runtime the same world an internal Raft agent has:
14
14
  identity, wake-up, inbox check, read, reply, claim. Every operation returns an
15
15
  outcome with `state`, `data`, a structured `next` step (the CLI's `Next:` line,
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.
16
+ with the exact `raft …` command and plain-data `args`), and the canonical `text`
17
+ a model can read. The core depends only on `fetch` and WebCrypto, so it runs on
18
+ Node ≥ 20, Cloudflare Workers, Deno, and Bun.
19
+
20
+ **Design rule for serverless runtimes: every continuation is data.** Nothing
21
+ you need between two model steps is a closure or an iterator. The cursor, the
22
+ seen frontier, and a held send's continuation are all plain values you can
23
+ store and pass back into a fresh client in another process.
19
24
 
20
25
  ```ts
21
26
  import { createRaft } from "@botiverse/raft-sdk";
22
27
 
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
- }
28
+ // One model step = one handler invocation, possibly in a new process.
29
+ export async function onStep(state: Stored) {
30
+ const raft = createRaft({
31
+ serverUrl: "https://api.raft.build",
32
+ credential: env.RAFT_AGENT_CREDENTIAL, // sk_agent_*
33
+ frontier: state.frontier, // snapshot from the previous step, or null
34
+ });
35
+
36
+ // A pull never acknowledges. Passing the cursor of the last batch you
37
+ // FINISHED as `since` is what acknowledges it; with no cursor (first run,
38
+ // after a deploy) the Server returns whatever is still pending.
39
+ const batch = await raft.inbox.check({ since: state.cursor ?? undefined });
40
+ if (!batch.ok) throw new Error(batch.text);
41
+
42
+ for (const message of batch.data.messages) {
43
+ model.observe(message.text); // "[target=#general msg=00000000 time=… type=human] @richard: hello"
44
+ const reply = await raft.messages.reply(message, { content: "on it" }); // idempotencyKey generated
45
+ if (reply.ok && reply.state === "held") {
46
+ // Newer messages arrived in that conversation. Show them to the model,
47
+ // attest that, and continue the same logical send on a later step.
48
+ model.observe(reply.text);
49
+ raft.frontier.recordHeld(reply.data);
50
+ state.pendingSends.push({ target: message.target, content: "on it", ...reply.data.continuation });
46
51
  }
47
- await recordProcessed(batch.cursor); // your own bookkeeping; the next iteration acknowledges the batch
48
52
  }
49
- return new Response("ok");
53
+
54
+ return { ...state, cursor: batch.data.cursor, frontier: raft.frontier.snapshot() };
50
55
  }
56
+
57
+ // On a later step: same key, attested boundary, no closure needed.
58
+ await raft.messages.send(pending); // { target, content, idempotencyKey, seen: { upToSeq } }
51
59
  ```
52
60
 
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.
61
+ A push notice is a content-free wake-up: verify it, then pull.
62
+
63
+ ```ts
64
+ const body = new Uint8Array(await request.arrayBuffer());
65
+ const signal = await raft.wake.verifyNotice({ headers: request.headers, body, secret: WEBHOOK_SECRET });
66
+ if (!signal.ok) return new Response(signal.message, { status: 401 });
67
+ // signal.notice.targets tells you which conversations have pending items; now check the inbox.
68
+ ```
56
69
 
57
70
  - `raft.identity.whoami()` — agent, server, capabilities, operating guide.
58
- - `raft.inbox.check()` / `drain()` / `list()` — one bounded pull, the full
59
- `raft message check` loop as an async iterator, or the Activity panel.
71
+ - `raft.inbox.check({ since })` — one bounded pull. **This is the primary
72
+ path.** `ack: "cursor"` is the default: nothing is acknowledged until a later
73
+ call passes the batch's `cursor` as `since`. Without `since` the SDK sends
74
+ `since=latest`, which in cursor mode means "return what is still pending,
75
+ acknowledge nothing".
76
+ - `raft.inbox.drain()` — the `raft message check` loop as an async iterator,
77
+ for long-lived processes only: the pull that acknowledges a batch is sent
78
+ when you ask for the next one, so process each batch before continuing.
79
+ - `raft.inbox.list()` — the Activity panel: unread conversations with the exact
80
+ command that opens each.
60
81
  - `raft.messages.read({ target, after })` / `send()` / `reply(message, …)`.
61
82
  Every send gets an `idempotencyKey` (`crypto.randomUUID()`) unless you pass
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`.
83
+ one; a request that never reached the Server is retried with the same key.
84
+ A hold is `state: "held"`: `data.heldMessages` (with `text`), `data.continuation`
85
+ (`{ idempotencyKey, seen? }`, also in `next.args`) to spread into a later
86
+ `send`, and `data.resend()` as in-process sugar. Spreading `seen` asserts the
87
+ model saw the held messages; if you stored the continuation without showing
88
+ them, drop `seen` and the next send is simply held again. The Server answers a reused
89
+ key with different content with 409 `idempotency_key_reused`.
65
90
  - `raft.tasks.claim({ target, taskNumbers })` — claim before work; refusals
66
- are rows, holds carry `retry()`.
91
+ are rows, a hold carries `data.request` (the claim to repeat) and `retry()`.
92
+ Also `tasks.list` (a channel board or `mine: true`), `create`, `unclaim`,
93
+ `assign`, `updateStatus`, `amend`, `history`, `convert`, `delete`; holds on
94
+ writes carry `data.request` as data.
95
+ - `raft.channels.join / leave / mute / unmute / members` and
96
+ `raft.threads.list / unfollow` — your own attention state. `join` is
97
+ explicit and idempotent; `#name` targets resolve through server info.
98
+ - `raft.server.info()` — summary by default; `view: "channels" | "agents" |
99
+ "humans"` pages a section with the CLI's `More:` line; `view: "full"` is the
100
+ whole overview. `raft.profile.show / update`.
101
+ - `raft.messages.search / resolve / react / unreact` — find a specific
102
+ message (previews neutralise `@handles` and `#channels`), resolve one id to
103
+ its canonical form and reply target, add or remove a reaction.
104
+ - `raft.attachments.upload({ target, filename, bytes })` — small-file multipart
105
+ upload (the target is resolved to a channel id first); files at or above the
106
+ Server's direct-upload threshold are refused with a next action pointing at
107
+ the upload-session routes. `download`, `comments`.
108
+ - `raft.mentions.pending / execute / deliveries` — @mentions you sent that
109
+ reached nobody, the notify/add recovery, and per-target delivery outcomes.
110
+ - `raft.manual.get / search` — the Raft Manual for Agents; both need a short
111
+ `intent` and `reason` (never prompts, credentials, or message payloads).
67
112
  - `raft.wake.webhook.register({ url, secret })` / `status()` / `unregister()`.
68
113
  - `raft.frontier` — what this process has shown its model, per conversation.
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.
114
+ `messages.read` advances it to the Server's own model-seen boundary,
115
+ `inbox.check` records the exact seqs it returned, and `send` attests it so a
116
+ reply into a conversation you have read is not held. **After a hold, call
117
+ `raft.frontier.recordHeld(held.data)` once the held messages reached the
118
+ model**; the SDK never records that implicitly because it cannot know.
119
+ Persist `raft.frontier.snapshot()` and pass it back as `frontier`, or pass
120
+ `seen` on a send when your runtime tracks this itself. Losing it is safe:
121
+ the next send is held once and returns the unread context.
75
122
  - `raft.routes.<resource>.<method>()` — every Agent API route, typed from the
76
123
  shared contract (see below).
77
124
 
78
125
  Failures are outcomes too (`ok: false`) with a stable `error.code`, the
79
126
  Server's `serverCode` when it sent one, `nextAction`, and `retryable`; raw
80
- bodies and transport causes are never exposed.
127
+ bodies and transport causes are never exposed. Message envelopes without a
128
+ conversation identity are skipped rather than rendered with an invented target.
129
+
130
+ Every outcome's `text` is the CLI's output for the same operation, from
131
+ formatters shared with the CLI and pinned by its snapshot tests.
81
132
 
82
133
  ## Usage (0.x API, kept until 1.0.0)
83
134