@ravi-os/sdk 0.1.0 → 0.2.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,213 @@
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 client for the Ravi SDK gateway.
4
+
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
9
 
7
10
  ## Install
8
11
 
9
12
  ```bash
10
13
  bun add @ravi-os/sdk
11
- # or
12
- npm install @ravi-os/sdk
13
14
  ```
14
15
 
15
- ## HTTP transport (browser- or node-compatible)
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.
19
+
20
+ ## Enable the Gateway
21
+
22
+ The SDK talks to Ravi's HTTP server. The daemon only starts that server when an
23
+ HTTP port is configured:
24
+
25
+ ```bash
26
+ RAVI_HTTP_PORT=7777
27
+ RAVI_HTTP_HOST=127.0.0.1
28
+ ```
29
+
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.
34
+
35
+ ## Auth
36
+
37
+ Non-open routes require a runtime context key (`rctx_*`) sent as:
38
+
39
+ ```text
40
+ Authorization: Bearer <rctx_key>
41
+ ```
42
+
43
+ Bootstrap the first admin key on the daemon host:
44
+
45
+ ```bash
46
+ ravi daemon init-admin-key
47
+ ```
48
+
49
+ For apps, issue scoped child keys instead of shipping the admin key:
50
+
51
+ ```bash
52
+ ravi context issue dashboard \
53
+ --ttl 2h \
54
+ --allow view:system:events,view:system:tasks \
55
+ --json
56
+ ```
57
+
58
+ ## Basic Client
16
59
 
17
60
  ```ts
18
- import { RaviClient } from "@ravi-os/sdk/client";
19
- import { createHttpTransport } from "@ravi-os/sdk/transport/http";
61
+ import { RaviClient, createHttpTransport } from "@ravi-os/sdk";
20
62
 
21
63
  const client = new RaviClient(
22
64
  createHttpTransport({
23
65
  baseUrl: "http://127.0.0.1:7777",
24
66
  contextKey: process.env.RAVI_CONTEXT_KEY!,
67
+ timeoutMs: 10_000,
25
68
  }),
26
69
  );
27
70
 
28
- const result = await client.artifacts.show("art_xyz");
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
+ );
78
+ ```
79
+
80
+ Generated method names are camel-cased. For example:
81
+
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()`
85
+
86
+ ## Request Shape
87
+
88
+ Every command call maps to:
89
+
90
+ ```text
91
+ POST /api/v1/<group-segments>/<command>
92
+ ```
93
+
94
+ The JSON body is flat. Positional arguments and options are merged at the top
95
+ level:
96
+
97
+ ```json
98
+ {
99
+ "name": "main",
100
+ "pattern": "group:120363428558776322",
101
+ "agent": "ravi-web",
102
+ "channel": "whatsapp",
103
+ "session": "ravi-web"
104
+ }
29
105
  ```
30
106
 
31
- `createHttpTransport` only depends on the global `fetch`, so it runs in any
32
- modern browser, Node 18+, Bun, Deno, or edge runtime.
107
+ Do not wrap inputs as `{ "args": ..., "options": ... }`.
33
108
 
34
- ## In-process transport (server-side only)
109
+ ## Streaming
110
+
111
+ Use `createStreamClient` or `RaviStreamClient` for server-sent event streams:
35
112
 
36
113
  ```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";
114
+ import { createStreamClient } from "@ravi-os/sdk/streaming";
40
115
 
41
- const client = new RaviClient(
42
- createInProcessTransport({
43
- registry: getRegistry(),
44
- scopeContext: { agentId: "agent-orquestrador" },
45
- }),
46
- );
116
+ const stream = createStreamClient({
117
+ baseUrl: "http://127.0.0.1:7777",
118
+ contextKey: process.env.RAVI_CONTEXT_KEY!,
119
+ });
120
+
121
+ for await (const event of stream.events({ subject: "ravi.session.>", noClaude: true })) {
122
+ console.log(event.event, event.data);
123
+ }
47
124
  ```
48
125
 
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.
126
+ Available streams:
127
+
128
+ - `stream.events(...)` -> `GET /api/v1/_stream/events`
129
+ - `stream.tasks(...)` -> `GET /api/v1/_stream/tasks`
130
+ - `stream.session(name, ...)` -> `GET /api/v1/_stream/sessions/<name>`
131
+ - `stream.audit(...)` -> `GET /api/v1/_stream/audit`
53
132
 
54
- ## Generated artifacts
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`.
55
136
 
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).
137
+ ## Binary Responses
138
+
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.
142
+
143
+ ```ts
144
+ const response = await client.artifacts.blob("art_123");
145
+ const bytes = await response.arrayBuffer();
146
+ ```
59
147
 
60
148
  ## Errors
61
149
 
62
- Every transport throws strongly-typed errors:
150
+ Every transport throws the same typed error hierarchy:
151
+
152
+ - `RaviAuthError` - 401, missing/invalid/expired/revoked context key
153
+ - `RaviPermissionError` - 403, scope denied
154
+ - `RaviValidationError` - 4xx validation failure, exposes `issues[]`
155
+ - `RaviInternalError` - 5xx from the gateway or command handler
156
+ - `RaviTransportError` - network failure, timeout, or unexpected transport error
157
+
158
+ All inherit from `RaviError`:
159
+
160
+ ```ts
161
+ import { RaviError, RaviValidationError } from "@ravi-os/sdk/errors";
162
+
163
+ try {
164
+ await client.artifacts.show("missing");
165
+ } catch (error) {
166
+ if (error instanceof RaviValidationError) {
167
+ console.error(error.issues);
168
+ } else if (error instanceof RaviError) {
169
+ console.error(error.status, error.command, error.message);
170
+ }
171
+ }
172
+ ```
173
+
174
+ ## Codegen and Drift
175
+
176
+ Generated files are committed as the canonical package surface:
177
+
178
+ - `src/client.ts`
179
+ - `src/schemas.ts`
180
+ - `src/types.ts`
181
+ - `src/version.ts`
182
+
183
+ Regenerate and check drift from the Ravi repo root:
184
+
185
+ ```bash
186
+ bun run sdk:generate
187
+ bun run sdk:check
188
+ ```
189
+
190
+ OpenAPI and Swift SDK snapshots are generated from the same registry:
191
+
192
+ ```bash
193
+ bun src/cli/index.ts sdk openapi emit --out docs/openapi.json
194
+ bun src/cli/index.ts sdk swift generate
195
+ ```
196
+
197
+ The source of truth is the `RegistrySnapshot` from
198
+ `src/cli/registry-snapshot.ts`, not the OpenAPI file.
199
+
200
+ ## Published Exports
201
+
202
+ Published consumers can import:
63
203
 
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
204
+ - `@ravi-os/sdk`
205
+ - `@ravi-os/sdk/client`
206
+ - `@ravi-os/sdk/transport/http`
207
+ - `@ravi-os/sdk/streaming`
208
+ - `@ravi-os/sdk/errors`
209
+ - `@ravi-os/sdk/types`
210
+ - `@ravi-os/sdk/schemas`
69
211
 
70
- All inherit from `RaviError`, so `catch (e: unknown) { if (e instanceof RaviError) ... }` works.
212
+ `src/transport/in-process.ts` is monorepo-internal and is not exported by the
213
+ published package.