@cadraos/sdk 0.1.2
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 +401 -0
- package/dist/chat/index.cjs +2330 -0
- package/dist/chat/index.cjs.map +1 -0
- package/dist/chat/index.d.cts +974 -0
- package/dist/chat/index.d.ts +974 -0
- package/dist/chat/index.js +2277 -0
- package/dist/chat/index.js.map +1 -0
- package/dist/index.cjs +1913 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1904 -0
- package/dist/index.d.ts +1904 -0
- package/dist/index.js +1901 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -0
package/README.md
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
# @cadraos/sdk
|
|
2
|
+
|
|
3
|
+
Official SDK for the AI SaaS platform - programmatic access to agents, skills, tools, and artifacts.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cadraos/sdk
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @cadraos/sdk
|
|
11
|
+
# or
|
|
12
|
+
yarn add @cadraos/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { AISaaSClient } from '@cadraos/sdk';
|
|
19
|
+
|
|
20
|
+
// Initialize the client
|
|
21
|
+
const client = new AISaaSClient({
|
|
22
|
+
apiKey: process.env.AISAAS_API_KEY!,
|
|
23
|
+
orgId: Number(process.env.AISAAS_ORG_ID),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// List available agents
|
|
27
|
+
const { data: agents } = await client.agents.list();
|
|
28
|
+
console.log('Available agents:', agents.map(a => a.name));
|
|
29
|
+
|
|
30
|
+
// Execute an agent
|
|
31
|
+
const execution = await client.agents.executeSync(agents[0].uuid, {
|
|
32
|
+
task: 'Summarize the latest news about AI',
|
|
33
|
+
});
|
|
34
|
+
console.log('Result:', execution.output);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Authentication
|
|
38
|
+
|
|
39
|
+
The SDK supports three authentication methods:
|
|
40
|
+
|
|
41
|
+
### API Key (recommended for server-side)
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const client = new AISaaSClient({
|
|
45
|
+
apiKey: 'sk_...',
|
|
46
|
+
orgId: 123,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### OAuth Token
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const client = new AISaaSClient({
|
|
54
|
+
token: 'eyJ...',
|
|
55
|
+
orgId: 123,
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Token Provider (for automatic refresh)
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const client = new AISaaSClient({
|
|
63
|
+
tokenProvider: async () => myAuth.getAccessToken(),
|
|
64
|
+
orgId: 123,
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## API Reference
|
|
69
|
+
|
|
70
|
+
### Agents
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
// List agents
|
|
74
|
+
const { data: agents, pagination } = await client.agents.list({
|
|
75
|
+
limit: 10,
|
|
76
|
+
status: 'active',
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// Get agent details
|
|
80
|
+
const agent = await client.agents.get('agent-uuid');
|
|
81
|
+
|
|
82
|
+
// Execute (async - returns immediately)
|
|
83
|
+
const { executionId, status } = await client.agents.execute('agent-uuid', {
|
|
84
|
+
task: 'Your task here',
|
|
85
|
+
context: { key: 'value' },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Execute (sync - waits for completion)
|
|
89
|
+
const execution = await client.agents.executeSync('agent-uuid', {
|
|
90
|
+
task: 'Your task here',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Stream execution (real-time events)
|
|
94
|
+
const stream = await client.agents.stream('agent-uuid', {
|
|
95
|
+
task: 'Research a topic',
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
stream.on('log', (log) => console.log(log.title));
|
|
99
|
+
stream.on('toolCall', ({ toolName }) => console.log(`Calling: ${toolName}`));
|
|
100
|
+
stream.on('complete', ({ execution }) => console.log(execution.output));
|
|
101
|
+
|
|
102
|
+
const result = await stream.wait();
|
|
103
|
+
|
|
104
|
+
// Get execution result
|
|
105
|
+
const execution = await client.agents.getExecution('execution-uuid');
|
|
106
|
+
|
|
107
|
+
// Update agent
|
|
108
|
+
const updated = await client.agents.update('agent-uuid', {
|
|
109
|
+
name: 'New Name',
|
|
110
|
+
description: 'New description',
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Cancel execution
|
|
114
|
+
await client.agents.cancelExecution('execution-uuid');
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Skills
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// List skills
|
|
121
|
+
const { data: skills } = await client.skills.list({
|
|
122
|
+
category: 'writing',
|
|
123
|
+
tags: ['seo', 'blog'],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// Get skill with prompt template
|
|
127
|
+
const skill = await client.skills.get('skill-uuid');
|
|
128
|
+
console.log(skill.promptTemplate);
|
|
129
|
+
|
|
130
|
+
// Get skill tools
|
|
131
|
+
const tools = await client.skills.getTools('skill-uuid');
|
|
132
|
+
|
|
133
|
+
// Execute skill
|
|
134
|
+
const result = await client.skills.execute('skill-uuid', {
|
|
135
|
+
variables: {
|
|
136
|
+
topic: 'AI in healthcare',
|
|
137
|
+
tone: 'professional',
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Update skill
|
|
142
|
+
await client.skills.update('skill-uuid', {
|
|
143
|
+
name: 'Updated Name',
|
|
144
|
+
tags: ['new-tag'],
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Update prompt template
|
|
148
|
+
await client.skills.updatePrompt('skill-uuid', 'New prompt template...');
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Tools
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// List tools
|
|
155
|
+
const { data: tools } = await client.tools.list({
|
|
156
|
+
category: 'data',
|
|
157
|
+
isEnabled: true,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// Get tool with schema
|
|
161
|
+
const tool = await client.tools.get('tool-uuid');
|
|
162
|
+
console.log(tool.inputSchema);
|
|
163
|
+
|
|
164
|
+
// Execute tool
|
|
165
|
+
const result = await client.tools.execute('tool-uuid', {
|
|
166
|
+
query: 'weather in London',
|
|
167
|
+
limit: 5,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Validate input
|
|
171
|
+
const validation = await client.tools.validateInput('tool-uuid', {
|
|
172
|
+
query: 'test',
|
|
173
|
+
});
|
|
174
|
+
if (!validation.valid) {
|
|
175
|
+
console.log(validation.errors);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Update tool
|
|
179
|
+
await client.tools.update('tool-uuid', {
|
|
180
|
+
name: 'Updated Name',
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// Update config
|
|
184
|
+
await client.tools.updateConfig('tool-uuid', {
|
|
185
|
+
isEnabled: false,
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Artifacts
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// List artifacts for an execution
|
|
193
|
+
const { data: artifacts } = await client.artifacts.list('execution-uuid', {
|
|
194
|
+
type: 'file',
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Get artifact metadata
|
|
198
|
+
const artifact = await client.artifacts.get('artifact-uuid');
|
|
199
|
+
|
|
200
|
+
// Download artifact
|
|
201
|
+
const content = await client.artifacts.download('artifact-uuid');
|
|
202
|
+
// Node.js: Buffer | Browser: Blob
|
|
203
|
+
|
|
204
|
+
// Download as stream
|
|
205
|
+
const stream = await client.artifacts.downloadStream('artifact-uuid');
|
|
206
|
+
|
|
207
|
+
// Get signed URL
|
|
208
|
+
const { url, expiresAt } = await client.artifacts.getSignedUrl('artifact-uuid', {
|
|
209
|
+
expiresIn: 3600, // 1 hour
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Upload artifact
|
|
213
|
+
const artifact = await client.artifacts.upload('execution-uuid', {
|
|
214
|
+
name: 'report.pdf',
|
|
215
|
+
content: fileBuffer,
|
|
216
|
+
mimeType: 'application/pdf',
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Delete artifact
|
|
220
|
+
await client.artifacts.delete('artifact-uuid');
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Sessions
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
// Create session
|
|
227
|
+
const session = await client.sessions.create({
|
|
228
|
+
agentUuid: 'agent-uuid',
|
|
229
|
+
systemMessage: 'You are a helpful assistant.',
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// List sessions
|
|
233
|
+
const { data: sessions } = await client.sessions.list({
|
|
234
|
+
agentUuid: 'agent-uuid',
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Get session
|
|
238
|
+
const session = await client.sessions.get('session-id');
|
|
239
|
+
|
|
240
|
+
// Send message (async)
|
|
241
|
+
const { executionId } = await client.sessions.sendMessage('session-id', {
|
|
242
|
+
message: 'What is the capital of France?',
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Send message (sync)
|
|
246
|
+
const execution = await client.sessions.sendMessageSync('session-id', {
|
|
247
|
+
message: 'Tell me more about that',
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Stream message
|
|
251
|
+
const stream = await client.sessions.stream('session-id', {
|
|
252
|
+
message: 'Explain in detail',
|
|
253
|
+
});
|
|
254
|
+
const result = await stream.wait();
|
|
255
|
+
|
|
256
|
+
// Get history
|
|
257
|
+
const history = await client.sessions.getHistory('session-id');
|
|
258
|
+
history.messages.forEach(msg => {
|
|
259
|
+
console.log(`${msg.role}: ${msg.content}`);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// Delete session
|
|
263
|
+
await client.sessions.delete('session-id');
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Streaming
|
|
267
|
+
|
|
268
|
+
The SDK supports real-time streaming for agent executions:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
const stream = await client.agents.stream('agent-uuid', {
|
|
272
|
+
task: 'Research climate change and provide a summary',
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// Register event handlers
|
|
276
|
+
stream.on('log', (log) => {
|
|
277
|
+
console.log(`[${log.type}] ${log.title}`);
|
|
278
|
+
if (log.content) console.log(log.content);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
stream.on('toolCall', ({ toolName, inputs }) => {
|
|
282
|
+
console.log(`Calling tool: ${toolName}`, inputs);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
stream.on('toolResult', ({ toolName, result, durationMs }) => {
|
|
286
|
+
console.log(`Tool ${toolName} completed in ${durationMs}ms`);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
stream.on('complete', ({ execution }) => {
|
|
290
|
+
console.log('Final output:', execution.output);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
stream.on('error', ({ error, errorCode }) => {
|
|
294
|
+
console.error(`Error [${errorCode}]: ${error}`);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
stream.on('connectionChange', ({ connected, usingFallback }) => {
|
|
298
|
+
if (usingFallback) {
|
|
299
|
+
console.log('Switched to polling fallback');
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// Wait for completion
|
|
304
|
+
const execution = await stream.wait();
|
|
305
|
+
|
|
306
|
+
// Or cancel
|
|
307
|
+
stream.cancel();
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## Error Handling
|
|
311
|
+
|
|
312
|
+
The SDK provides typed error classes for different error scenarios:
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import {
|
|
316
|
+
AISaaSError,
|
|
317
|
+
AuthenticationError,
|
|
318
|
+
AuthorizationError,
|
|
319
|
+
NotFoundError,
|
|
320
|
+
ValidationError,
|
|
321
|
+
RateLimitError,
|
|
322
|
+
ServerError,
|
|
323
|
+
TimeoutError,
|
|
324
|
+
NetworkError,
|
|
325
|
+
} from '@cadraos/sdk';
|
|
326
|
+
|
|
327
|
+
try {
|
|
328
|
+
await client.agents.get('invalid-uuid');
|
|
329
|
+
} catch (error) {
|
|
330
|
+
if (error instanceof NotFoundError) {
|
|
331
|
+
console.log('Agent not found');
|
|
332
|
+
} else if (error instanceof AuthenticationError) {
|
|
333
|
+
console.log('Invalid API key');
|
|
334
|
+
} else if (error instanceof RateLimitError) {
|
|
335
|
+
console.log(`Rate limited. Retry after ${error.retryAfter} seconds`);
|
|
336
|
+
} else if (error instanceof ValidationError) {
|
|
337
|
+
error.errors.forEach(e => {
|
|
338
|
+
console.log(`${e.field}: ${e.message}`);
|
|
339
|
+
});
|
|
340
|
+
} else if (error instanceof AISaaSError) {
|
|
341
|
+
console.log(`Error [${error.code}]: ${error.message}`);
|
|
342
|
+
console.log('Request ID:', error.requestId);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## Configuration Options
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
const client = new AISaaSClient({
|
|
351
|
+
// Required
|
|
352
|
+
apiKey: 'sk_...', // or token/tokenProvider
|
|
353
|
+
orgId: 123,
|
|
354
|
+
|
|
355
|
+
// Optional
|
|
356
|
+
baseUrl: 'https://api.aisaas.com/v1', // Custom API URL
|
|
357
|
+
timeout: 30000, // Request timeout (ms)
|
|
358
|
+
maxRetries: 3, // Retry attempts
|
|
359
|
+
headers: { // Custom headers
|
|
360
|
+
'X-Custom-Header': 'value',
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## TypeScript
|
|
366
|
+
|
|
367
|
+
The SDK is written in TypeScript and provides full type definitions:
|
|
368
|
+
|
|
369
|
+
```typescript
|
|
370
|
+
import type {
|
|
371
|
+
Agent,
|
|
372
|
+
AgentExecution,
|
|
373
|
+
Skill,
|
|
374
|
+
Tool,
|
|
375
|
+
Artifact,
|
|
376
|
+
Session,
|
|
377
|
+
StreamController,
|
|
378
|
+
} from '@cadraos/sdk';
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
## Browser Support
|
|
382
|
+
|
|
383
|
+
The SDK works in modern browsers for non-streaming operations. Streaming (SSE) is only supported in Node.js in v1.
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
// Works in browser
|
|
387
|
+
const agents = await client.agents.list();
|
|
388
|
+
const execution = await client.agents.executeSync(uuid, { task: '...' });
|
|
389
|
+
|
|
390
|
+
// Node.js only (v1)
|
|
391
|
+
const stream = await client.agents.stream(uuid, { task: '...' });
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## Requirements
|
|
395
|
+
|
|
396
|
+
- Node.js >= 18.0.0
|
|
397
|
+
- Modern browsers (Chrome, Firefox, Safari, Edge)
|
|
398
|
+
|
|
399
|
+
## License
|
|
400
|
+
|
|
401
|
+
MIT
|