@ravi-os/sdk 0.2.0 → 0.260519.1

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
@@ -1,70 +1,360 @@
1
1
  # @ravi-os/sdk
2
2
 
3
- Type-safe TypeScript client for the Ravi gateway. The `RaviClient` surface is
4
- generated 1:1 from `getRegistry()`, so every CLI command becomes a typed method
5
- on the client.
3
+ Type-safe TypeScript SDK for controlling a Ravi runtime through the authenticated
4
+ SDK gateway.
5
+
6
+ Ravi is not just a model wrapper. It is a local-first runtime for long-lived
7
+ agents: sessions, tasks, contacts, artifacts, routes, events, permissions, and
8
+ audit all live behind one command registry. `@ravi-os/sdk` turns that registry
9
+ into a typed client you can use from apps, dashboards, browser extensions,
10
+ workers, tests, and internal tools.
11
+
12
+ ```ts
13
+ import { RaviClient, createHttpTransport } from "@ravi-os/sdk";
14
+
15
+ const ravi = new RaviClient(
16
+ createHttpTransport({
17
+ baseUrl: "http://127.0.0.1:7777",
18
+ contextKey: process.env.RAVI_CONTEXT_KEY!,
19
+ }),
20
+ );
21
+
22
+ const sessions = await ravi.sessions.list({ live: true, limit: "20" });
23
+ const reply = await ravi.sessions.send("main", "Summarize the current work.", {
24
+ wait: true,
25
+ });
26
+ ```
27
+
28
+ ## Why This Exists
29
+
30
+ Use the SDK when you need a programmatic control plane for Ravi:
31
+
32
+ - Build a dashboard that reads sessions, tasks, contacts, and runtime events.
33
+ - Let a browser extension talk to the local Ravi daemon without shelling out.
34
+ - Drive agents from tests or internal automation.
35
+ - Stream live task/session/event updates into another UI.
36
+ - Write a custom app while keeping auth, permissions, validation, and audit in
37
+ Ravi instead of duplicating them.
38
+
39
+ The package is generated from Ravi's decorated CLI registry. If a command exists
40
+ in the registry, the SDK gets the same shape, names, args, options, and return
41
+ typing.
6
42
 
7
43
  ## Install
8
44
 
9
45
  ```bash
10
46
  bun add @ravi-os/sdk
11
- # or
12
- npm install @ravi-os/sdk
13
47
  ```
14
48
 
15
- ## HTTP transport (browser- or node-compatible)
49
+ The package is ESM-only and works anywhere `fetch` is available: Bun, Node 18+,
50
+ modern browsers, Deno, and edge runtimes.
51
+
52
+ ## Start The Ravi Gateway
53
+
54
+ The SDK talks to Ravi's HTTP gateway. Enable it on the daemon host:
55
+
56
+ ```bash
57
+ RAVI_HTTP_PORT=7777
58
+ RAVI_HTTP_HOST=127.0.0.1
59
+ ravi daemon start
60
+ ```
61
+
62
+ The gateway is mounted under:
63
+
64
+ ```text
65
+ http://127.0.0.1:7777/api/v1/*
66
+ ```
67
+
68
+ By default Ravi is local-only. If you bind the gateway to a non-loopback host,
69
+ Ravi requires:
70
+
71
+ ```bash
72
+ RAVI_GATEWAY_NETWORK_AUTHORIZED=1
73
+ ```
74
+
75
+ To keep the HTTP server available for webhooks while disabling SDK routes:
76
+
77
+ ```bash
78
+ RAVI_SDK_GATEWAY_DISABLE=1
79
+ ```
80
+
81
+ ## Create A Context Key
82
+
83
+ Most SDK routes require a runtime context key (`rctx_*`) in the bearer auth
84
+ header. Bootstrap the first admin key on the daemon machine:
85
+
86
+ ```bash
87
+ ravi daemon init-admin-key
88
+ ```
89
+
90
+ For apps, issue a narrow child key instead of shipping the admin key:
91
+
92
+ ```bash
93
+ ravi context issue dashboard \
94
+ --ttl 2h \
95
+ --allow view:system:events,view:system:tasks,access:session:main \
96
+ --json
97
+ ```
98
+
99
+ `createHttpTransport` sends the key as `Authorization: Bearer <rctx_key>`.
100
+
101
+ ## Create A Client
102
+
103
+ ```ts
104
+ import { RaviClient, createHttpTransport } from "@ravi-os/sdk";
105
+
106
+ export function createRaviClient() {
107
+ return new RaviClient(
108
+ createHttpTransport({
109
+ baseUrl: process.env.RAVI_BASE_URL ?? "http://127.0.0.1:7777",
110
+ contextKey: process.env.RAVI_CONTEXT_KEY!,
111
+ timeoutMs: 10_000,
112
+ }),
113
+ );
114
+ }
115
+ ```
116
+
117
+ Deep imports are available when you want smaller bundles:
16
118
 
17
119
  ```ts
18
120
  import { RaviClient } from "@ravi-os/sdk/client";
19
121
  import { createHttpTransport } from "@ravi-os/sdk/transport/http";
122
+ ```
20
123
 
21
- const client = new RaviClient(
22
- createHttpTransport({
23
- baseUrl: "http://127.0.0.1:7777",
24
- contextKey: process.env.RAVI_CONTEXT_KEY!,
25
- }),
26
- );
124
+ ## Examples
125
+
126
+ ### Read Runtime State
127
+
128
+ ```ts
129
+ const ravi = createRaviClient();
27
130
 
28
- const result = await client.artifacts.show("art_xyz");
131
+ const agents = await ravi.agents.list({ limit: "50" });
132
+ const sessions = await ravi.sessions.list({ live: true, limit: "25" });
133
+ const recentTasks = await ravi.tasks.list({
134
+ status: "running",
135
+ limit: "10",
136
+ });
29
137
  ```
30
138
 
31
- `createHttpTransport` only depends on the global `fetch`, so it runs in any
32
- modern browser, Node 18+, Bun, Deno, or edge runtime.
139
+ ### Send A Prompt To A Session
33
140
 
34
- ## In-process transport (server-side only)
141
+ ```ts
142
+ const result = await ravi.sessions.send("main", "What changed in the repo?", {
143
+ wait: true,
144
+ });
145
+
146
+ console.log(result);
147
+ ```
148
+
149
+ `wait: true` maps to the CLI's `--wait`. Without it, the command is
150
+ fire-and-forget.
151
+
152
+ ### Stream Session Events
35
153
 
36
154
  ```ts
37
- import { RaviClient } from "@ravi-os/sdk/client";
38
- import { createInProcessTransport } from "@ravi-os/sdk/transport/in-process";
39
- import { getRegistry } from "ravi.bot/cli/registry-snapshot";
155
+ import { createStreamClient } from "@ravi-os/sdk/streaming";
40
156
 
41
- const client = new RaviClient(
42
- createInProcessTransport({
43
- registry: getRegistry(),
44
- scopeContext: { agentId: "agent-orquestrador" },
45
- }),
46
- );
157
+ const stream = createStreamClient({
158
+ baseUrl: "http://127.0.0.1:7777",
159
+ contextKey: process.env.RAVI_CONTEXT_KEY!,
160
+ });
161
+
162
+ for await (const event of stream.session("main", { timeout: 60 })) {
163
+ console.log(event.event, event.data);
164
+ }
165
+ ```
166
+
167
+ ### Stream System Events
168
+
169
+ ```ts
170
+ for await (const event of stream.events({
171
+ subject: "ravi.session.>",
172
+ noClaude: true,
173
+ noHeartbeat: true,
174
+ })) {
175
+ console.log(event.data.topic, event.data.type);
176
+ }
177
+ ```
178
+
179
+ Available stream helpers:
180
+
181
+ - `stream.events(...)` -> `GET /api/v1/_stream/events`
182
+ - `stream.tasks(...)` -> `GET /api/v1/_stream/tasks`
183
+ - `stream.session(name, ...)` -> `GET /api/v1/_stream/sessions/<name>`
184
+ - `stream.audit(...)` -> `GET /api/v1/_stream/audit`
185
+
186
+ Streams always require a valid context key and the matching scope, such as
187
+ `view:system:events`, `view:system:tasks`, `access:session:<name>`, or
188
+ `view:system:audit`.
189
+
190
+ ### Work With Contacts And CRM
191
+
192
+ ```ts
193
+ const contacts = await ravi.contacts.list({ limit: "20" });
194
+ const crmCards = await ravi.crm.contacts({ limit: "20" });
195
+ const nextActions = await ravi.crm.next({ owner: "agent:main", limit: "10" });
196
+
197
+ await ravi.crm.contact.set("contact_123", "lifecycle", "active", {
198
+ source: "dashboard",
199
+ });
200
+ ```
201
+
202
+ ### Create And Version Artifacts
203
+
204
+ ```ts
205
+ const artifact = await ravi.artifacts.create({
206
+ title: "Weekly summary",
207
+ output: "# Summary\n\nDone.",
208
+ mime: "text/markdown",
209
+ session: "main",
210
+ tags: "summary,weekly",
211
+ });
212
+
213
+ await ravi.artifacts.snapshot("art_123", {
214
+ label: "v1",
215
+ message: "First published summary",
216
+ });
217
+ ```
218
+
219
+ Binary artifact commands return the raw `Response` on success:
220
+
221
+ ```ts
222
+ const response = await ravi.artifacts.blob("art_123");
223
+ const bytes = await response.arrayBuffer();
224
+ ```
225
+
226
+ ### Use A Custom Transport In Tests
227
+
228
+ The generated client only depends on the `Transport` interface. You can mock it
229
+ without starting a daemon:
230
+
231
+ ```ts
232
+ import { RaviClient, type Transport } from "@ravi-os/sdk";
233
+
234
+ const calls: unknown[] = [];
235
+
236
+ const mockTransport: Transport = {
237
+ async call(input) {
238
+ calls.push(input);
239
+ return { ok: true };
240
+ },
241
+ };
242
+
243
+ const ravi = new RaviClient(mockTransport);
244
+ await ravi.sessions.send("main", "hello");
245
+ ```
246
+
247
+ ## Method Naming
248
+
249
+ Generated method names mirror the CLI:
250
+
251
+ - `ravi daemon init-admin-key` -> `ravi.daemon.initAdminKey()`
252
+ - `ravi sessions send main "hello" --wait` -> `ravi.sessions.send("main", "hello", { wait: true })`
253
+ - `ravi crm contact set <contact> lifecycle active` -> `ravi.crm.contact.set(contact, "lifecycle", "active")`
254
+ - `ravi sdk client check` -> `ravi.sdk.client.check()`
255
+
256
+ Positional args become positional method parameters. CLI options become the final
257
+ `options` object. Most string-like CLI flags stay typed as strings because the
258
+ registry mirrors the CLI parser; boolean flags are booleans.
259
+
260
+ ## Wire Contract
261
+
262
+ Every command call becomes:
263
+
264
+ ```text
265
+ POST /api/v1/<group-segments>/<command>
266
+ ```
267
+
268
+ The request body is flat JSON. Positional args and options are merged at the top
269
+ level:
270
+
271
+ ```json
272
+ {
273
+ "name": "main",
274
+ "pattern": "group:120363428558776322",
275
+ "agent": "ravi-web",
276
+ "channel": "whatsapp",
277
+ "session": "ravi-web"
278
+ }
47
279
  ```
48
280
 
49
- The in-process transport reuses the same dispatcher pipeline that the HTTP
50
- gateway runs (validation, scope checks, audit), without HTTP overhead. It only
51
- runs in Node — the package exports map this entry as `null` for non-node
52
- conditions.
281
+ Do not wrap input as `{ "args": ..., "options": ... }`.
53
282
 
54
- ## Generated artifacts
283
+ The HTTP transport adds:
55
284
 
56
- `client.ts`, `schemas.ts`, `types.ts`, and `version.ts` are produced by
57
- `ravi sdk client generate` and committed as the canonical surface. Drift is
58
- caught by `ravi sdk client check` (used in CI).
285
+ - `Authorization: Bearer <rctx_key>`
286
+ - `x-ravi-sdk-version`
287
+ - `x-ravi-registry-hash`
59
288
 
60
289
  ## Errors
61
290
 
62
- Every transport throws strongly-typed errors:
291
+ All transports throw the same error hierarchy:
292
+
293
+ - `RaviAuthError` - 401, missing/invalid/expired/revoked context key
294
+ - `RaviPermissionError` - 403, scope denied
295
+ - `RaviValidationError` - 4xx validation failure, exposes `issues[]`
296
+ - `RaviInternalError` - 5xx from the gateway or command handler
297
+ - `RaviTransportError` - network failure, timeout, or unexpected transport error
298
+
299
+ ```ts
300
+ import { RaviError, RaviValidationError } from "@ravi-os/sdk/errors";
301
+
302
+ try {
303
+ await ravi.artifacts.show("missing");
304
+ } catch (error) {
305
+ if (error instanceof RaviValidationError) {
306
+ console.error(error.issues);
307
+ } else if (error instanceof RaviError) {
308
+ console.error(error.status, error.command, error.message);
309
+ }
310
+ }
311
+ ```
312
+
313
+ ## Codegen And Drift
314
+
315
+ Generated files are committed as the canonical package surface:
316
+
317
+ - `src/client.ts`
318
+ - `src/schemas.ts`
319
+ - `src/types.ts`
320
+ - `src/version.ts`
321
+
322
+ Regenerate and check drift from the Ravi repo root:
323
+
324
+ ```bash
325
+ bun run sdk:generate
326
+ bun run sdk:check
327
+ ```
328
+
329
+ OpenAPI and Swift SDK snapshots are generated from the same registry:
330
+
331
+ ```bash
332
+ bun run docs:openapi
333
+ bun src/cli/index.ts sdk swift generate
334
+ ```
335
+
336
+ The source of truth is `src/cli/registry-snapshot.ts`, not the OpenAPI file.
337
+
338
+ ## Published Exports
339
+
340
+ ```ts
341
+ import { RaviClient } from "@ravi-os/sdk";
342
+ import { RaviClient as DeepClient } from "@ravi-os/sdk/client";
343
+ import { createHttpTransport } from "@ravi-os/sdk/transport/http";
344
+ import { createStreamClient } from "@ravi-os/sdk/streaming";
345
+ import { RaviError } from "@ravi-os/sdk/errors";
346
+ import type { SessionsListReturn } from "@ravi-os/sdk/types";
347
+ ```
348
+
349
+ Published exports:
63
350
 
64
- - `RaviAuthError` — 401, missing/invalid context key
65
- - `RaviPermissionError` — 403, scope denied
66
- - `RaviValidationError` — 4xx body validation, exposes `issues[]`
67
- - `RaviInternalError` — 5xx from the gateway
68
- - `RaviTransportError` — network / timeout / unexpected shape
351
+ - `@ravi-os/sdk`
352
+ - `@ravi-os/sdk/client`
353
+ - `@ravi-os/sdk/transport/http`
354
+ - `@ravi-os/sdk/streaming`
355
+ - `@ravi-os/sdk/errors`
356
+ - `@ravi-os/sdk/types`
357
+ - `@ravi-os/sdk/schemas`
69
358
 
70
- All inherit from `RaviError`, so `catch (e: unknown) { if (e instanceof RaviError) ... }` works.
359
+ `packages/ravi-os-sdk/src/transport/in-process.ts` is monorepo-internal and is
360
+ not exported by the published package.