@relevanceai/sdk 3.0.0-alpha.0 → 3.0.0-alpha.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/LICENSE +21 -0
- package/README.md +320 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Relevance AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# Relevance AI JavaScript SDK
|
|
2
|
+
|
|
3
|
+
Build full-stack AI applications using our JavaScript SDK. This multi-environment
|
|
4
|
+
SDK enables you to integrate, extend, or build end-to-end solutions on top of
|
|
5
|
+
our powerful AI Workforce platform.
|
|
6
|
+
|
|
7
|
+
> **Note:** The SDK is in active development and not all features are available
|
|
8
|
+
> yet. Please refer to our roadmap for updates.
|
|
9
|
+
|
|
10
|
+
## Quickstart
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
import { createClient, AU_REGION } from "@relevanceai/sdk";
|
|
14
|
+
|
|
15
|
+
// Create a client with your credentials
|
|
16
|
+
const client = createClient({
|
|
17
|
+
apiKey: "sk-abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKL",
|
|
18
|
+
region: AU_REGION,
|
|
19
|
+
project: "12345678-90ab-cdef-1234-567890abcdef",
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// Create a task for an agent
|
|
23
|
+
const task = client.createTask({
|
|
24
|
+
agent: "fedcba09-8765-4321-fedc-ba0987654321",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Send a message to the agent
|
|
28
|
+
task.sendMessage(
|
|
29
|
+
"What is the weather like in Sydney, Australia this weekend?"
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// Listen for responses
|
|
33
|
+
task.addEventListener("message", ({ detail }) => {
|
|
34
|
+
const { message } = detail;
|
|
35
|
+
console.log(message.text);
|
|
36
|
+
|
|
37
|
+
task.sendMessage("Thanks!");
|
|
38
|
+
|
|
39
|
+
// Important: Stop listening when done to prevent memory leaks
|
|
40
|
+
task.stopListening();
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Getting Started
|
|
45
|
+
|
|
46
|
+
The JavaScript SDK is a stateless, event-driven toolkit that provides the
|
|
47
|
+
flexibility to build any application you need. It sits on top of our API and
|
|
48
|
+
offers a streamlined developer experience, making it easier for your apps to
|
|
49
|
+
integrate with agents, tools, and workforces.
|
|
50
|
+
|
|
51
|
+
This multi-environment library allows you to build wherever modern JavaScript
|
|
52
|
+
runs:
|
|
53
|
+
|
|
54
|
+
- Node.js
|
|
55
|
+
- Deno
|
|
56
|
+
- Bun
|
|
57
|
+
- Cloudflare Workers
|
|
58
|
+
- Browser
|
|
59
|
+
|
|
60
|
+
### Installation
|
|
61
|
+
|
|
62
|
+
Install the SDK for your environment:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Node.js / Cloudflare Workers / Browser (with bundler)
|
|
66
|
+
npm install @relevanceai/sdk@latest
|
|
67
|
+
|
|
68
|
+
# Deno
|
|
69
|
+
deno add jsr:@relevanceai/sdk
|
|
70
|
+
|
|
71
|
+
# Bun
|
|
72
|
+
bun add @relevanceai/sdk@latest
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### Browser (CDN)
|
|
76
|
+
|
|
77
|
+
If you are developing a frontend application and not using a bundler like
|
|
78
|
+
[Vite](https://vite.dev) or [Webpack](https://webpack.js.org), you can use the
|
|
79
|
+
CDN version directly:
|
|
80
|
+
|
|
81
|
+
```html
|
|
82
|
+
<script type="importmap">
|
|
83
|
+
{
|
|
84
|
+
"imports": {
|
|
85
|
+
"@relevanceai/sdk": "https://esm.run/@relevanceai/sdk"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
</script>
|
|
89
|
+
<script type="module">
|
|
90
|
+
import { createClient } from "@relevanceai/sdk";
|
|
91
|
+
// ...
|
|
92
|
+
</script>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Usage
|
|
96
|
+
|
|
97
|
+
### Keys
|
|
98
|
+
|
|
99
|
+
To communicate with Relevance AI, you will need a key. Keys authenticate your
|
|
100
|
+
requests and grant access to your project.
|
|
101
|
+
|
|
102
|
+
There are two types of keys: _API_ and _Embed_.
|
|
103
|
+
|
|
104
|
+
#### API Key
|
|
105
|
+
|
|
106
|
+
API keys grant access to the entire project, including agents, tools, and
|
|
107
|
+
workforces. This key is best used for server-side applications or protected web
|
|
108
|
+
apps where third parties do not have access.
|
|
109
|
+
|
|
110
|
+
Using the default client `createClient({ apiKey, region, project })` will create
|
|
111
|
+
an API Key for convenience. Alternatively, you can create a `Key` instance and
|
|
112
|
+
pass it to the client.
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
import { createClient, Key, AU_REGION } from "@relevanceai/sdk";
|
|
116
|
+
|
|
117
|
+
const apiKey = "sk-...";
|
|
118
|
+
const region = AU_REGION;
|
|
119
|
+
const project = "1234...";
|
|
120
|
+
|
|
121
|
+
const key = new Key({ apiKey, region, project });
|
|
122
|
+
const client = createClient(key);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Embed Key
|
|
126
|
+
|
|
127
|
+
If you are developing a web app that allows third-party access, such as for your
|
|
128
|
+
customers, it's best to share resources from the Relevance AI platform. This
|
|
129
|
+
makes them available for public use and requires an embed key scoped only to
|
|
130
|
+
that resource.
|
|
131
|
+
|
|
132
|
+
To get an embed key, specify which public resource you wish to scope it to. For
|
|
133
|
+
example, to create an embed key for a publicly available agent in your project:
|
|
134
|
+
|
|
135
|
+
```js
|
|
136
|
+
import { createClient, Key, AU_REGION } from "@relevanceai/sdk";
|
|
137
|
+
|
|
138
|
+
const region = AU_REGION;
|
|
139
|
+
const project = "1234...";
|
|
140
|
+
const agent = "abcd..."; // a *public* agent
|
|
141
|
+
|
|
142
|
+
const embedKey = await Key.generateEmbedKey({ region, project, agent });
|
|
143
|
+
const client = createClient(embedKey);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Clients
|
|
147
|
+
|
|
148
|
+
A client is the main entry point for the SDK. It configures communication with
|
|
149
|
+
Relevance AI and manages authentication.
|
|
150
|
+
|
|
151
|
+
You can create multiple clients, which is useful for multi-project setups.
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
import { Client, Key, AU_REGION } from "@relevanceai/sdk";
|
|
155
|
+
|
|
156
|
+
const apiKey = "sk-...";
|
|
157
|
+
const region = AU_REGION;
|
|
158
|
+
const projectOne = "1234...";
|
|
159
|
+
const projectTwo = "abcd...";
|
|
160
|
+
|
|
161
|
+
const oneKey = new Key({ apiKey, region, project: projectOne });
|
|
162
|
+
const twoKey = new Key({ apiKey, region, project: projectTwo });
|
|
163
|
+
|
|
164
|
+
const oneClient = new Client(oneKey);
|
|
165
|
+
const twoClient = new Client(twoKey);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Default client
|
|
169
|
+
|
|
170
|
+
Typically, you will only need a single client. In this case, use the default
|
|
171
|
+
client factory as shown in the quickstart:
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
import { createClient, Client } from "@relevanceai/sdk";
|
|
175
|
+
|
|
176
|
+
const client = createClient({ apiKey, region, project });
|
|
177
|
+
|
|
178
|
+
// elsewhere in your app
|
|
179
|
+
Client.default();
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Attempting to create more than one default client will throw an error. Referencing
|
|
183
|
+
the default client before creating one will also throw an error.
|
|
184
|
+
|
|
185
|
+
### Tasks
|
|
186
|
+
|
|
187
|
+
Whenever you run anything in Relevance AI, these are known as tasks. Tasks have
|
|
188
|
+
a subject: an agent, tool, or workforce. You can send messages to these subjects,
|
|
189
|
+
receive replies, and follow updates and errors.
|
|
190
|
+
|
|
191
|
+
> **Important:** Always call `task.stopListening()` when you're done with a task
|
|
192
|
+
> to prevent memory leaks and clean up resources properly.
|
|
193
|
+
|
|
194
|
+
#### Creating Tasks
|
|
195
|
+
|
|
196
|
+
The easiest way to create a new task is to use the client's convenient method
|
|
197
|
+
`createTask` and provide the subject ID.
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
const agentId = "1234...";
|
|
201
|
+
const task = client.createTask({ agent: agentId });
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
#### Sending a Message
|
|
205
|
+
|
|
206
|
+
Once you have a task instance, you can send messages to the subject using the
|
|
207
|
+
`sendMessage()` method.
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
task.sendMessage("How many letter r's are there in the word 'strawberry'?");
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Note that this call is not `async`. It sends the message and does not `await`
|
|
214
|
+
a reply. This is intentional, as tasks are event-driven.
|
|
215
|
+
|
|
216
|
+
### Events
|
|
217
|
+
|
|
218
|
+
Tasks are event-driven and an application must listen to predefined events to
|
|
219
|
+
manage the status and messages of a task.
|
|
220
|
+
|
|
221
|
+
Use `.addEventListener()` to listen for task events.
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
task.sendMessage("What came first; the chicken or the egg?");
|
|
225
|
+
|
|
226
|
+
task.addEventListener("message", ({ detail }) => {
|
|
227
|
+
const { message } = detail;
|
|
228
|
+
console.log("> %s", message.text);
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Tasks dispatch `CustomEvent`. Event properties will be set in the `detail`
|
|
233
|
+
field of the event. You can see the types of events for more information about
|
|
234
|
+
the properties associated with different events.
|
|
235
|
+
|
|
236
|
+
Remember to call `task.stopListening()` once you no longer need to listen to
|
|
237
|
+
the task.
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
The following events are available for agent subjects.
|
|
242
|
+
|
|
243
|
+
#### `start`
|
|
244
|
+
|
|
245
|
+
When a _new_ task starts.
|
|
246
|
+
|
|
247
|
+
**Details**
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
interface StartEventDetails {
|
|
251
|
+
id: string;
|
|
252
|
+
status: TaskStatus;
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
#### `status`
|
|
257
|
+
|
|
258
|
+
Whenever the task's _status_ changes.
|
|
259
|
+
|
|
260
|
+
> **Note:** this event does **not** fire for starting status. Use the `start`
|
|
261
|
+
> event if you need the initial status.
|
|
262
|
+
|
|
263
|
+
**Details**
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
interface StatusEventDetails {
|
|
267
|
+
status: TaskStatus;
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
##### `update`
|
|
272
|
+
|
|
273
|
+
A task has updated. This update will always be a tool for now but may expand in
|
|
274
|
+
the future.
|
|
275
|
+
|
|
276
|
+
**Details**
|
|
277
|
+
|
|
278
|
+
```ts
|
|
279
|
+
interface UpdateEventDetails {
|
|
280
|
+
update: TaskMessage<"tool">;
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
#### `message`
|
|
285
|
+
|
|
286
|
+
A task has received a message.
|
|
287
|
+
|
|
288
|
+
> **Note:** you will receive messages from _both_ subjects and users.
|
|
289
|
+
|
|
290
|
+
**Details**
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
interface MessageEventDetails {
|
|
294
|
+
message: TaskMessage<"agent" | "user">;
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### `error`
|
|
299
|
+
|
|
300
|
+
Whenever the task has failed.
|
|
301
|
+
|
|
302
|
+
**Details**
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
interface ErrorEventDetails {
|
|
306
|
+
error: TaskMessage<"error">;
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**Example**
|
|
311
|
+
|
|
312
|
+
```js
|
|
313
|
+
task.addEventListener("error", ({ detail }) => {
|
|
314
|
+
const { error } = detail;
|
|
315
|
+
console.error("Task failed:", error.text);
|
|
316
|
+
|
|
317
|
+
// clean up
|
|
318
|
+
task.stopListening();
|
|
319
|
+
});
|
|
320
|
+
```
|