@ravi-os/sdk 0.2.1 → 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,11 +1,44 @@
1
1
  # @ravi-os/sdk
2
2
 
3
- Type-safe TypeScript client for the Ravi SDK gateway.
3
+ Type-safe TypeScript SDK for controlling a Ravi runtime through the authenticated
4
+ SDK gateway.
4
5
 
5
- `RaviClient` is generated from Ravi's decorated CLI registry, so the SDK mirrors
6
- the same command surface exposed by the gateway. The generated client stays thin:
7
- it builds a flat request body and delegates auth, validation, scope checks, audit,
8
- and HTTP details to a transport.
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.
9
42
 
10
43
  ## Install
11
44
 
@@ -13,102 +46,110 @@ and HTTP details to a transport.
13
46
  bun add @ravi-os/sdk
14
47
  ```
15
48
 
16
- The package is ESM-only and expects a runtime with `fetch` available. That means
17
- modern browsers, Node 18+, Bun, Deno, and edge runtimes work with the HTTP
18
- transport.
49
+ The package is ESM-only and works anywhere `fetch` is available: Bun, Node 18+,
50
+ modern browsers, Deno, and edge runtimes.
19
51
 
20
- ## Enable the Gateway
52
+ ## Start The Ravi Gateway
21
53
 
22
- The SDK talks to Ravi's HTTP server. The daemon only starts that server when an
23
- HTTP port is configured:
54
+ The SDK talks to Ravi's HTTP gateway. Enable it on the daemon host:
24
55
 
25
56
  ```bash
26
57
  RAVI_HTTP_PORT=7777
27
58
  RAVI_HTTP_HOST=127.0.0.1
59
+ ravi daemon start
28
60
  ```
29
61
 
30
- The SDK gateway is mounted under `/api/v1/*` on the same listener as webhooks.
31
- Binding to a non-loopback host requires `RAVI_GATEWAY_NETWORK_AUTHORIZED=1`.
32
- Set `RAVI_SDK_GATEWAY_DISABLE=1` to disable SDK routes while leaving the HTTP
33
- server available for other webhook handlers.
62
+ The gateway is mounted under:
34
63
 
35
- ## Auth
64
+ ```text
65
+ http://127.0.0.1:7777/api/v1/*
66
+ ```
36
67
 
37
- Non-open routes require a runtime context key (`rctx_*`) sent as:
68
+ By default Ravi is local-only. If you bind the gateway to a non-loopback host,
69
+ Ravi requires:
38
70
 
39
- ```text
40
- Authorization: Bearer <rctx_key>
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
41
79
  ```
42
80
 
43
- Bootstrap the first admin key on the daemon host:
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:
44
85
 
45
86
  ```bash
46
87
  ravi daemon init-admin-key
47
88
  ```
48
89
 
49
- For apps, issue scoped child keys instead of shipping the admin key:
90
+ For apps, issue a narrow child key instead of shipping the admin key:
50
91
 
51
92
  ```bash
52
93
  ravi context issue dashboard \
53
94
  --ttl 2h \
54
- --allow view:system:events,view:system:tasks \
95
+ --allow view:system:events,view:system:tasks,access:session:main \
55
96
  --json
56
97
  ```
57
98
 
58
- ## Basic Client
99
+ `createHttpTransport` sends the key as `Authorization: Bearer <rctx_key>`.
100
+
101
+ ## Create A Client
59
102
 
60
103
  ```ts
61
104
  import { RaviClient, createHttpTransport } from "@ravi-os/sdk";
62
105
 
63
- const client = new RaviClient(
64
- createHttpTransport({
65
- baseUrl: "http://127.0.0.1:7777",
66
- contextKey: process.env.RAVI_CONTEXT_KEY!,
67
- timeoutMs: 10_000,
68
- }),
69
- );
70
-
71
- const agents = await client.agents.list();
72
- const route = await client.instances.routes.add(
73
- "main",
74
- "group:120363428558776322",
75
- "ravi-web",
76
- { channel: "whatsapp", session: "ravi-web" },
77
- );
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
+ }
78
115
  ```
79
116
 
80
- Generated method names are camel-cased. For example:
117
+ Deep imports are available when you want smaller bundles:
118
+
119
+ ```ts
120
+ import { RaviClient } from "@ravi-os/sdk/client";
121
+ import { createHttpTransport } from "@ravi-os/sdk/transport/http";
122
+ ```
81
123
 
82
- - `ravi daemon init-admin-key` becomes `client.daemon.initAdminKey()`
83
- - `ravi instances routes add` becomes `client.instances.routes.add(...)`
84
- - `ravi sdk client check` becomes `client.sdk.client.check()`
124
+ ## Examples
85
125
 
86
- ## Request Shape
126
+ ### Read Runtime State
87
127
 
88
- Every command call maps to:
128
+ ```ts
129
+ const ravi = createRaviClient();
89
130
 
90
- ```text
91
- POST /api/v1/<group-segments>/<command>
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
+ });
92
137
  ```
93
138
 
94
- The JSON body is flat. Positional arguments and options are merged at the top
95
- level:
139
+ ### Send A Prompt To A Session
96
140
 
97
- ```json
98
- {
99
- "name": "main",
100
- "pattern": "group:120363428558776322",
101
- "agent": "ravi-web",
102
- "channel": "whatsapp",
103
- "session": "ravi-web"
104
- }
105
- ```
141
+ ```ts
142
+ const result = await ravi.sessions.send("main", "What changed in the repo?", {
143
+ wait: true,
144
+ });
106
145
 
107
- Do not wrap inputs as `{ "args": ..., "options": ... }`.
146
+ console.log(result);
147
+ ```
108
148
 
109
- ## Streaming
149
+ `wait: true` maps to the CLI's `--wait`. Without it, the command is
150
+ fire-and-forget.
110
151
 
111
- Use `createStreamClient` or `RaviStreamClient` for server-sent event streams:
152
+ ### Stream Session Events
112
153
 
113
154
  ```ts
114
155
  import { createStreamClient } from "@ravi-os/sdk/streaming";
@@ -118,36 +159,136 @@ const stream = createStreamClient({
118
159
  contextKey: process.env.RAVI_CONTEXT_KEY!,
119
160
  });
120
161
 
121
- for await (const event of stream.events({ subject: "ravi.session.>", noClaude: true })) {
162
+ for await (const event of stream.session("main", { timeout: 60 })) {
122
163
  console.log(event.event, event.data);
123
164
  }
124
165
  ```
125
166
 
126
- Available streams:
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:
127
180
 
128
181
  - `stream.events(...)` -> `GET /api/v1/_stream/events`
129
182
  - `stream.tasks(...)` -> `GET /api/v1/_stream/tasks`
130
183
  - `stream.session(name, ...)` -> `GET /api/v1/_stream/sessions/<name>`
131
184
  - `stream.audit(...)` -> `GET /api/v1/_stream/audit`
132
185
 
133
- Streaming routes always require a valid context key. They also check scoped
134
- permissions such as `view:system:events`, `view:system:tasks`,
135
- `access:session:<name>`, or `view:system:audit`.
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" });
136
196
 
137
- ## Binary Responses
197
+ await ravi.crm.contact.set("contact_123", "lifecycle", "active", {
198
+ source: "dashboard",
199
+ });
200
+ ```
138
201
 
139
- Commands marked with `@Returns.binary()` return a raw `Response` for successful
140
- 2xx calls. Error responses are still parsed and mapped to `RaviError`
141
- subclasses.
202
+ ### Create And Version Artifacts
142
203
 
143
204
  ```ts
144
- const response = await client.artifacts.blob("art_123");
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");
145
223
  const bytes = await response.arrayBuffer();
146
224
  ```
147
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
+ }
279
+ ```
280
+
281
+ Do not wrap input as `{ "args": ..., "options": ... }`.
282
+
283
+ The HTTP transport adds:
284
+
285
+ - `Authorization: Bearer <rctx_key>`
286
+ - `x-ravi-sdk-version`
287
+ - `x-ravi-registry-hash`
288
+
148
289
  ## Errors
149
290
 
150
- Every transport throws the same typed error hierarchy:
291
+ All transports throw the same error hierarchy:
151
292
 
152
293
  - `RaviAuthError` - 401, missing/invalid/expired/revoked context key
153
294
  - `RaviPermissionError` - 403, scope denied
@@ -155,13 +296,11 @@ Every transport throws the same typed error hierarchy:
155
296
  - `RaviInternalError` - 5xx from the gateway or command handler
156
297
  - `RaviTransportError` - network failure, timeout, or unexpected transport error
157
298
 
158
- All inherit from `RaviError`:
159
-
160
299
  ```ts
161
300
  import { RaviError, RaviValidationError } from "@ravi-os/sdk/errors";
162
301
 
163
302
  try {
164
- await client.artifacts.show("missing");
303
+ await ravi.artifacts.show("missing");
165
304
  } catch (error) {
166
305
  if (error instanceof RaviValidationError) {
167
306
  console.error(error.issues);
@@ -171,7 +310,7 @@ try {
171
310
  }
172
311
  ```
173
312
 
174
- ## Codegen and Drift
313
+ ## Codegen And Drift
175
314
 
176
315
  Generated files are committed as the canonical package surface:
177
316
 
@@ -190,16 +329,24 @@ bun run sdk:check
190
329
  OpenAPI and Swift SDK snapshots are generated from the same registry:
191
330
 
192
331
  ```bash
193
- bun src/cli/index.ts sdk openapi emit --out docs/openapi.json
332
+ bun run docs:openapi
194
333
  bun src/cli/index.ts sdk swift generate
195
334
  ```
196
335
 
197
- The source of truth is the `RegistrySnapshot` from
198
- `src/cli/registry-snapshot.ts`, not the OpenAPI file.
336
+ The source of truth is `src/cli/registry-snapshot.ts`, not the OpenAPI file.
199
337
 
200
338
  ## Published Exports
201
339
 
202
- Published consumers can import:
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:
203
350
 
204
351
  - `@ravi-os/sdk`
205
352
  - `@ravi-os/sdk/client`
@@ -209,5 +356,5 @@ Published consumers can import:
209
356
  - `@ravi-os/sdk/types`
210
357
  - `@ravi-os/sdk/schemas`
211
358
 
212
- `src/transport/in-process.ts` is monorepo-internal and is not exported by the
213
- published package.
359
+ `packages/ravi-os-sdk/src/transport/in-process.ts` is monorepo-internal and is
360
+ not exported by the published package.