@redonvn/open-claw-sdk 0.1.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/docs/DOCS.md ADDED
@@ -0,0 +1,342 @@
1
+ ---
2
+ title: "OpenClaw SDK"
3
+ summary: "Convert OpenAPI JSON documents into OpenClaw agent tools and generated plugin files"
4
+ ---
5
+
6
+ # OpenClaw SDK
7
+
8
+ `@redonvn/open-claw-sdk` is a small SDK for converting an OpenAPI `.json` document into:
9
+
10
+ - OpenClaw-compatible tool definitions
11
+ - A generated plugin module (`index.ts`)
12
+ - A plugin manifest (`openclaw.plugin.json`)
13
+ - A normalized tool catalog (`tool-catalog.json`)
14
+
15
+ The package is designed for the common RedAI workflow:
16
+
17
+ 1. Receive an OpenAPI `.json` file from a backend team.
18
+ 2. Convert each operation into an OpenClaw tool.
19
+ 3. Generate a plugin that can be loaded by OpenClaw without hand-writing each tool.
20
+
21
+ ## Problem It Solves
22
+
23
+ Writing OpenClaw tools manually is slow when an API already has an OpenAPI contract. This SDK removes the repetitive part:
24
+
25
+ - Reads OpenAPI `paths`
26
+ - Converts operations into JSON Schema tool parameters
27
+ - Generates consistent tool names
28
+ - Produces a ready-to-wire OpenClaw plugin skeleton
29
+
30
+ ## Package Scope
31
+
32
+ This package currently targets **OpenAPI JSON** input and **HTTP tool generation**.
33
+
34
+ Supported:
35
+
36
+ - OpenAPI 3.x JSON documents
37
+ - Path parameters
38
+ - Query parameters
39
+ - Request body JSON schema
40
+ - Non-auth header parameters when `includeHeaders` is enabled
41
+ - `$ref` resolution from `components.schemas`
42
+
43
+ Not handled yet:
44
+
45
+ - OpenAPI YAML input
46
+ - Cookie parameters
47
+ - Multipart/form-data request builders
48
+ - Custom auth flows beyond simple bearer token or API key injection
49
+ - Advanced response typing inside the generated runtime
50
+
51
+ ## Installation
52
+
53
+ From the monorepo root:
54
+
55
+ ```bash
56
+ npm --workspace @redonvn/open-claw-sdk install
57
+ ```
58
+
59
+ Or publish/install it as a standalone package:
60
+
61
+ ```bash
62
+ npm install @redonvn/open-claw-sdk
63
+ ```
64
+
65
+ ## CLI
66
+
67
+ The package ships a CLI named `open-claw-sdk`.
68
+
69
+ ### Generate plugin files from an OpenAPI document
70
+
71
+ ```bash
72
+ open-claw-sdk generate \
73
+ --input=./openapi.json \
74
+ --outDir=./generated/sepay-plugin \
75
+ --pluginId=sepay-api \
76
+ --pluginName="SePay API" \
77
+ --toolPrefix=sepay \
78
+ --defaultServerUrl=https://my-api.example.com \
79
+ --optional=true
80
+ ```
81
+
82
+ ### Required arguments
83
+
84
+ - `--input`: path or URL to the OpenAPI `.json` document
85
+ - `--outDir`: output directory for generated plugin files
86
+ - `--pluginId`: OpenClaw plugin id
87
+
88
+ ### Optional arguments
89
+
90
+ - `--pluginName`: display name for the plugin
91
+ - `--toolPrefix`: prefix added to every generated tool name
92
+ - `--defaultServerUrl`: overrides the first server URL in the OpenAPI document
93
+ - `--includeHeaders=true`: include non-auth header params in the tool schema
94
+ - `--optional=false`: register tools as required instead of opt-in
95
+
96
+ ## Generated Output
97
+
98
+ The CLI writes three files:
99
+
100
+ ### `openclaw.plugin.json`
101
+
102
+ Plugin manifest used by OpenClaw for discovery and config validation.
103
+
104
+ Generated config fields:
105
+
106
+ - `baseUrl`
107
+ - `bearerToken`
108
+ - `apiKey`
109
+ - `apiKeyHeader`
110
+ - `timeoutMs`
111
+ - `defaultHeaders`
112
+
113
+ ### `index.ts`
114
+
115
+ Generated OpenClaw plugin module. It imports `createOpenApiPlugin(...)` from this SDK and registers every converted tool automatically.
116
+
117
+ ### `tool-catalog.json`
118
+
119
+ Normalized metadata snapshot of all tools. This is useful for review, debugging, or downstream automation.
120
+
121
+ ## Programmatic API
122
+
123
+ ### Load an OpenAPI document
124
+
125
+ ```ts
126
+ import { loadOpenApiDocument } from "@redonvn/open-claw-sdk";
127
+
128
+ const document = await loadOpenApiDocument("./openapi.json");
129
+ ```
130
+
131
+ ### Convert OpenAPI into OpenClaw tools
132
+
133
+ ```ts
134
+ import { convertOpenApiToTools } from "@redonvn/open-claw-sdk";
135
+
136
+ const catalog = convertOpenApiToTools(document, {
137
+ pluginId: "sepay-api",
138
+ pluginName: "SePay API",
139
+ toolPrefix: "sepay",
140
+ optional: true
141
+ });
142
+ ```
143
+
144
+ ### Generate plugin files
145
+
146
+ ```ts
147
+ import { generatePluginFromOpenApi } from "@redonvn/open-claw-sdk";
148
+
149
+ const result = await generatePluginFromOpenApi({
150
+ input: "./openapi.json",
151
+ outDir: "./generated/sepay-plugin",
152
+ pluginId: "sepay-api",
153
+ pluginName: "SePay API",
154
+ toolPrefix: "sepay"
155
+ });
156
+
157
+ console.log(result.catalog.tools.length);
158
+ console.log(result.files.indexPath);
159
+ ```
160
+
161
+ ### Build an OpenClaw plugin at runtime
162
+
163
+ ```ts
164
+ import { createOpenApiPlugin } from "@redonvn/open-claw-sdk";
165
+
166
+ const catalog = {
167
+ pluginId: "demo-api",
168
+ pluginName: "Demo API",
169
+ description: "Generated plugin",
170
+ serverUrl: "https://api.example.com",
171
+ optional: true,
172
+ tools: [
173
+ {
174
+ name: "demo_get_user",
175
+ description: "Get user by id",
176
+ method: "GET",
177
+ path: "/users/{id}",
178
+ operationId: "get_user",
179
+ tags: ["users"],
180
+ parameters: {
181
+ type: "object",
182
+ properties: {
183
+ path: {
184
+ type: "object",
185
+ properties: {
186
+ id: { type: "string" }
187
+ },
188
+ required: ["id"],
189
+ additionalProperties: false
190
+ }
191
+ },
192
+ required: ["path"],
193
+ additionalProperties: false
194
+ }
195
+ }
196
+ ]
197
+ } as const;
198
+
199
+ export default createOpenApiPlugin(catalog);
200
+ ```
201
+
202
+ ## Conversion Rules
203
+
204
+ ### Tool naming
205
+
206
+ Each operation becomes one tool.
207
+
208
+ Tool name source:
209
+
210
+ 1. `operationId` if present
211
+ 2. fallback from `method + path`
212
+
213
+ Then the SDK:
214
+
215
+ - normalizes to `snake_case`
216
+ - optionally prepends `toolPrefix`
217
+
218
+ Example:
219
+
220
+ - `operationId: getCompanyDetail` -> `getcompanydetail`
221
+ - with `toolPrefix: "mst"` -> `mst_getcompanydetail`
222
+
223
+ If you want more readable names, keep `operationId` stable and explicit in the OpenAPI source.
224
+
225
+ ### Parameter shape
226
+
227
+ Generated tool parameters are grouped by HTTP location:
228
+
229
+ ```json
230
+ {
231
+ "type": "object",
232
+ "properties": {
233
+ "path": { "type": "object" },
234
+ "query": { "type": "object" },
235
+ "headers": { "type": "object" },
236
+ "body": { "type": "object" }
237
+ },
238
+ "additionalProperties": false
239
+ }
240
+ ```
241
+
242
+ Notes:
243
+
244
+ - `path` is required when the route contains required path params.
245
+ - `body` is required when `requestBody.required = true`.
246
+ - Auth headers such as `authorization` and `x-api-key` are excluded from generated tool parameters.
247
+ - Those auth values should come from plugin config instead.
248
+
249
+ ### Schema conversion
250
+
251
+ The SDK converts OpenAPI schemas into plain JSON Schema suitable for `api.registerTool(...)`.
252
+
253
+ Handled mappings:
254
+
255
+ - `type`, `enum`, `default`, `description`
256
+ - `properties`, `required`
257
+ - `items`
258
+ - `oneOf`, `anyOf`, `allOf`
259
+ - `nullable`
260
+ - `$ref` from `components.schemas`
261
+
262
+ ## Runtime Behavior
263
+
264
+ `createOpenApiPlugin(...)` generates tool executors with a simple HTTP runtime:
265
+
266
+ - Resolves `baseUrl` from `plugins.entries.<pluginId>.config.baseUrl`
267
+ - Falls back to the generated `serverUrl` from OpenAPI when available
268
+ - Injects bearer token or API key from plugin config
269
+ - Serializes query parameters with `URLSearchParams`
270
+ - Sends JSON body with `fetch`
271
+ - Returns either JSON or text response as OpenClaw text content
272
+
273
+ ## OpenClaw Config Example
274
+
275
+ ```json5
276
+ {
277
+ plugins: {
278
+ entries: {
279
+ "sepay-api": {
280
+ enabled: true,
281
+ config: {
282
+ baseUrl: "https://api.example.com",
283
+ bearerToken: "YOUR_TOKEN",
284
+ timeoutMs: 15000,
285
+ defaultHeaders: {
286
+ "x-tenant-id": "redon"
287
+ }
288
+ }
289
+ }
290
+ }
291
+ },
292
+ agents: {
293
+ list: [
294
+ {
295
+ id: "main",
296
+ tools: {
297
+ allow: ["sepay-api"]
298
+ }
299
+ }
300
+ ]
301
+ }
302
+ }
303
+ ```
304
+
305
+ If the generated tools stay optional, they must be allowlisted by tool name or by plugin id.
306
+
307
+ ## Recommended Workflow
308
+
309
+ 1. Keep the OpenAPI document clean, especially `operationId`, `summary`, and schema descriptions.
310
+ 2. Generate the plugin into a dedicated folder.
311
+ 3. Review `tool-catalog.json` before enabling the plugin.
312
+ 4. Adjust tool naming with `toolPrefix` if the API is large or likely to conflict with other plugins.
313
+ 5. Configure `baseUrl` and auth in `plugins.entries.<id>.config`.
314
+
315
+ ## File Layout
316
+
317
+ ```text
318
+ packages/open-claw-sdk/
319
+ src/
320
+ cli.ts
321
+ generator.ts
322
+ openapi.ts
323
+ runtime.ts
324
+ types.ts
325
+ utils.ts
326
+ docs/
327
+ DOCS.md
328
+ ```
329
+
330
+ ## Development Notes
331
+
332
+ - The package uses only Node 18+ built-ins.
333
+ - Generated plugins are intentionally simple and easy to audit.
334
+ - This SDK is positioned as a bridge from backend OpenAPI contracts to OpenClaw tools, not as a full OpenAPI client generator.
335
+
336
+ ## Next Improvements
337
+
338
+ - YAML input support
339
+ - Better `operationId` to tool-name normalization
340
+ - Multipart/form-data generation
341
+ - Per-operation auth strategies
342
+ - More structured error/result formatting for agent consumption