@serenity-star/sdk 1.0.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/readme.md ADDED
@@ -0,0 +1,473 @@
1
+ ![Serenity JS/TS SDK](../../.github/resources/sdk-banner.png)
2
+
3
+ # Serenity Star JS/TS SDK
4
+
5
+ The Serenity Star JS/TS SDK provides a comprehensive interface for interacting with Serenity's different types of agents, such as activities, assistants, proxies, and more.
6
+
7
+ ## Table of Contents
8
+ - [Serenity Star JS/TS SDK](#serenity-star-jsts-sdk)
9
+ - [Table of Contents](#table-of-contents)
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [Assistants / Copilots](#assistants--copilots)
13
+ - [Start a new conversation with an Agent](#start-a-new-conversation-with-an-agent)
14
+ - [Sending messages within a conversation](#sending-messages-within-a-conversation)
15
+ - [Stream message with SSE](#stream-message-with-sse)
16
+ - [Real time conversation](#real-time-conversation)
17
+ - [Activities](#activities)
18
+ - [Execute an activity](#execute-an-activity)
19
+ - [Stream responses with SSE](#stream-responses-with-sse)
20
+ - [Proxies](#proxies)
21
+ - [Execute a proxy](#execute-a-proxy)
22
+ - [Stream responses with SSE](#stream-responses-with-sse-1)
23
+ - [Proxy Execution Options](#proxy-execution-options)
24
+ - [Chat Completions](#chat-completions)
25
+ - [Execute a chat completion](#execute-a-chat-completion)
26
+ - [Stream responses with SSE](#stream-responses-with-sse-2)
27
+ - [Plans](#plans)
28
+ - [Execute a plan](#execute-a-plan)
29
+ - [Stream responses with SSE](#stream-responses-with-sse-3)
30
+
31
+ # Installation
32
+
33
+ ```bash
34
+ npm install @serenity-star/sdk
35
+ ```
36
+
37
+ # Usage
38
+
39
+ ```tsx
40
+ import SerenityClient from '@serenity-star/sdk';
41
+
42
+ const client = new SerenityClient({
43
+ apiKey: '<SERENITY_API_KEY>',
44
+ apiVersion: 2 // Optional. 2 by default
45
+ });
46
+
47
+ // Execute an activity agent
48
+ const response = await client.agents.activities.execute("marketing-campaign")
49
+ console.log(response.content)
50
+ ```
51
+
52
+ # Assistants / Copilots
53
+
54
+ ## Start a new conversation with an Agent
55
+
56
+ ```tsx
57
+ import SerenityClient from '@serenity-star/sdk';
58
+
59
+ const client = new SerenityClient({
60
+ apiKey: '<SERENITY_API_KEY>',
61
+ });
62
+
63
+ // Create conversation with an assistant
64
+ const conversation = await client.agents.assistants.createConversation("chef-assistant");
65
+ ```
66
+
67
+ ## Sending messages within a conversation
68
+
69
+ ```tsx
70
+ import SerenityClient from '@serenity-star/sdk';
71
+
72
+ const client = new SerenityClient({
73
+ apiKey: '<SERENITY_API_KEY>',
74
+ });
75
+
76
+ // Create conversation with an assistant
77
+ const conversation = await client.agents.assistants.createConversation("chef-assistant")
78
+
79
+ const response = await conversation.sendMessage("I would like to get a recipe for parmesan chicken")
80
+
81
+ // Access Response data
82
+ console.log(
83
+ response.content, // "Sure! Here is a recipe for parmesan chicken..."
84
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
85
+ response.executor_task_logs, // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
86
+ )
87
+ ```
88
+
89
+ ### Stream message with SSE
90
+
91
+ ```tsx
92
+ import SerenityClient from '@serenity-star/sdk';
93
+
94
+ const client = new SerenityClient({
95
+ apiKey: '<SERENITY_API_KEY>',
96
+ });
97
+
98
+ // Create conversation
99
+ const conversation = await client.agents.assistants.createConversation("chef-assistant")
100
+
101
+ conversation
102
+ .on("content", (chunk) => {
103
+ console.log(chunk) // Response chunk
104
+ })
105
+ .on("error", (error) => {
106
+ // Handle stream errors here
107
+ })
108
+
109
+ // Streaming response with Server Sent Events (SSE)
110
+ const response = await conversation.streamMessage("I would like to get a recipe for parmesan chicken")
111
+
112
+ // Access Response Data
113
+ console.log(
114
+ response.content, // "Sure! Here is a recipe for parmesan chicken..."
115
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
116
+ response.executor_task_logs, // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
117
+ )
118
+ ```
119
+
120
+ ## Real time conversation
121
+
122
+ ```tsx
123
+ import SerenityClient from '@serenity-star/sdk';
124
+
125
+ const client = new SerenityClient({
126
+ apiKey: '<SERENITY_API_KEY>',
127
+ });
128
+
129
+ // Create conversation
130
+ const session = await client.agents.assistants.createRealtimeSession("chef-assistant")
131
+ .on("session.created", () => {
132
+ // Update UI to provide feedback if you need
133
+ })
134
+ .on("speech.started", () => {
135
+ // Update UI to let user know that is being recorded
136
+ })
137
+ .on("speech.stopped", () => {
138
+ // update UI to let user know a response is being processed
139
+ })
140
+ .on("response.done", () => {
141
+ // Update UI if you want to show the assistant is talking
142
+ })
143
+ .on("error", (message?: string) => {
144
+ // Show error message in the UI?
145
+ })
146
+ .on("session.stopped", (reason: string, details?: any) => {
147
+ // Update UI to let the user start a new session, or show the transcript of the entire session
148
+ })
149
+
150
+ await session.start()
151
+
152
+ // You can mute / unmute your mic during conversation
153
+ session.muteMicrophone()
154
+ session.unmuteMicrophone()
155
+
156
+ // Stop the session
157
+ session.stop();
158
+ ```
159
+
160
+ ---
161
+
162
+ # Activities
163
+
164
+ ## Execute an activity
165
+
166
+ ```tsx
167
+ import SerenityClient from '@serenity-star/sdk';
168
+
169
+ const client = new SerenityClient({
170
+ apiKey: '<SERENITY_API_KEY>',
171
+ });
172
+
173
+ // Execute activity (basic example)
174
+ const response = await client.agents.activities.execute("translator-activity");
175
+
176
+ // Execute activity (advanced example)
177
+ const response = await client.agents.activities.execute("translator-activity", {
178
+ inputParameters: {
179
+ targetLanguage: "russian",
180
+ textToTranslate: "hello world"
181
+ }
182
+ });
183
+
184
+ console.log(
185
+ response.content, // Привет, мир!
186
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
187
+ response.executor_task_logs, // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
188
+ )
189
+ ```
190
+
191
+ ## Stream responses with SSE
192
+
193
+ ```tsx
194
+ import SerenityClient from '@serenity-star/sdk';
195
+
196
+ const client = new SerenityClient({
197
+ apiKey: '<SERENITY_API_KEY>',
198
+ });
199
+
200
+ // Execute activity and stream response with Server Sent Events (SSE)
201
+ const activity = client.agents.activities.create("translator-activity", {
202
+ inputParameters: {
203
+ targetLanguage: "russian",
204
+ textToTranslate: "hello world"
205
+ }
206
+ })
207
+ .on("content", (data) => {
208
+ console.log(data.text); // Response chunk
209
+ })
210
+ .on("error", (error) => {
211
+ // Handle stream errors here
212
+ });
213
+
214
+ const response = await activity.stream()
215
+
216
+ // Access final response data
217
+ console.log(
218
+ response.content, // Привет, мир!
219
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
220
+ response.executor_task_logs, // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
221
+ );
222
+ ```
223
+
224
+ ---
225
+
226
+
227
+ # Proxies
228
+
229
+ ## Execute a proxy
230
+
231
+ ```tsx
232
+ import SerenityClient from '@serenity-star/sdk';
233
+
234
+ const client = new SerenityClient({
235
+ apiKey: '<SERENITY_API_KEY>',
236
+ });
237
+
238
+ // Execute proxy (basic example)
239
+ const response = await client.agents.proxies.execute("proxy-agent", {
240
+ model: "gpt-4o-mini-2024-07-18",
241
+ messages: [
242
+ { role: "user", content: "What is artificial intelligence?" },
243
+ ],
244
+ });
245
+
246
+ console.log(
247
+ response.content,
248
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
249
+ response.executor_task_logs // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
250
+ );
251
+
252
+ ```
253
+
254
+ ## Stream responses with SSE
255
+
256
+ ```tsx
257
+ import SerenityClient from '@serenity-star/sdk';
258
+
259
+ const client = new SerenityClient({
260
+ apiKey: '<SERENITY_API_KEY>',
261
+ });
262
+
263
+ // Execute proxy and stream response with Server Sent Events (SSE)
264
+ const proxy = client.agents.proxies.create("proxy-agent", {
265
+ model: "gpt-4o-mini-2024-07-18",
266
+ messages: [
267
+ { role: "user", content: "What is artificial intelligence?" },
268
+ ],
269
+ temperature: 1,
270
+ max_tokens: 250,
271
+ })
272
+ .on("content", (chunk) => {
273
+ console.log(chunk); // Response chunk
274
+ })
275
+ .on("error", (error) => {
276
+ console.error("Error:", error);
277
+ });
278
+
279
+ const response = await proxy.stream();
280
+
281
+ // Access final response data
282
+ console.log(
283
+ response.content,
284
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
285
+ response.executor_task_logs // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
286
+ );
287
+
288
+ ```
289
+
290
+ ## Proxy Execution Options
291
+
292
+ The following options can be passed as the second parameter in `execute` or `create`:
293
+
294
+ ```json
295
+ {
296
+ // Specify the model to use
297
+ "model": "gpt-4-turbo",
298
+
299
+ // Define conversation messages
300
+ "messages": [
301
+ {
302
+ "role": "system",
303
+ "content": "You are a knowledgeable AI assistant."
304
+ },
305
+ {
306
+ "role": "user",
307
+ "content": "Can you explain the theory of relativity in simple terms?"
308
+ }
309
+ ],
310
+
311
+ // Model parameters
312
+ "temperature": 0.7, // Controls randomness (0-1)
313
+ "max_tokens": 500, // Maximum length of response
314
+ "top_p": 0.9, // Nucleus sampling parameter
315
+ "top_k": 50, // Top-k sampling parameter
316
+ "frequency_penalty": 0.5, // Reduces repetition (-2 to 2)
317
+ "presence_penalty": 0.2, // Encourages new topics (-2 to 2)
318
+
319
+ // Additional options
320
+ "vendor": "openai", // AI provider
321
+ "userIdentifier": "user_123", // Unique user ID
322
+ "groupIdentifier": "org_456", // Organization ID
323
+ "useVision": false // Enable/disable vision features
324
+ }
325
+ ```
326
+
327
+ ---
328
+
329
+
330
+ # Chat Completions
331
+
332
+ ## Execute a chat completion
333
+
334
+ ```tsx
335
+ import SerenityClient from '@serenity-star/sdk';
336
+
337
+ const client = new SerenityClient({
338
+ apiKey: '<SERENITY_API_KEY>',
339
+ });
340
+
341
+ // Execute chat completion (basic example)
342
+ const response = await client.agents.chatCompletions.execute("AgentCreator", {
343
+ message: "Hello!!!"
344
+ });
345
+
346
+ console.log(
347
+ response.content, // AI-generated response
348
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
349
+ );
350
+
351
+ // Execute chat completion (advanced example)
352
+ const response = await client.agents.chatCompletions.execute("Health-Coach", {
353
+ userIdentifier: "user-123",
354
+ agentVersion: 2,
355
+ channel: "web",
356
+ volatileKnowledgeIds: ["knowledge-1", "knowledge-2"],
357
+ message: "Hi! How can I eat healthier?",
358
+ messages: [
359
+ { role: "assistant", content: "Hi there! How can I assist you?" }
360
+ ]
361
+ });
362
+
363
+ console.log(
364
+ response.content, // AI-generated response
365
+ response.completion_usage // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
366
+ );
367
+
368
+ ```
369
+
370
+ ## Stream responses with SSE
371
+
372
+ ```tsx
373
+ import SerenityClient from '@serenity-star/sdk';
374
+
375
+ const client = new SerenityClient({
376
+ apiKey: '<SERENITY_API_KEY>',
377
+ });
378
+
379
+ // Execute chat completion and stream response with Server Sent Events (SSE)
380
+ const chatCompletion = client.agents.chatCompletions
381
+ .create("Health-Coach", {
382
+ message: "Hi! How can I eat healthier?",
383
+ messages: [
384
+ { role: "assistant", content: "Hi there! How can I assist you?" }
385
+ ]
386
+ })
387
+ .on("start", () => {
388
+ console.log("Chat stream started");
389
+ })
390
+ .on("content", (chunk) => {
391
+ console.log("Response chunk:", chunk);
392
+ })
393
+ .on("error", (error) => {
394
+ console.error("Error:", error);
395
+ });
396
+
397
+ const response = await chatCompletion.stream();
398
+
399
+ // Access final response data
400
+ console.log(
401
+ response.content, // AI-generated response
402
+ response.completion_usage // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
403
+ );
404
+ ```
405
+
406
+ ---
407
+
408
+ # Plans
409
+
410
+ ## Execute a plan
411
+
412
+ ```tsx
413
+ import SerenityClient from '@serenity-star/sdk';
414
+
415
+ const client = new SerenityClient({
416
+ apiKey: '<SERENITY_API_KEY>',
417
+ });
418
+
419
+ // Execute plan (basic example)
420
+ const response = await client.agents.plans.execute("event-planner");
421
+
422
+ // Execute plan (advanced example)
423
+ const response = await client.agents.plans.execute("event-planner", {
424
+ userIdentifier: "user-123",
425
+ agentVersion: 2,
426
+ channel: "web",
427
+ volatileKnowledgeIds: ["knowledge-1", "knowledge-2"]
428
+ });
429
+
430
+ console.log(
431
+ response.content,
432
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
433
+ response.executor_task_logs // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
434
+ );
435
+
436
+ ```
437
+
438
+ ## Stream responses with SSE
439
+
440
+ ```tsx
441
+ import SerenityClient from '@serenity-star/sdk';
442
+
443
+ const client = new SerenityClient({
444
+ apiKey: '<SERENITY_API_KEY>',
445
+ });
446
+
447
+ // Execute plan and stream response with Server Sent Events (SSE)
448
+ const plan = client.agents.plans.create("event-planner", {
449
+ userIdentifier: "user-123",
450
+ agentVersion: 2,
451
+ channel: "web",
452
+ volatileKnowledgeIds: ["knowledge-1", "knowledge-2"]
453
+ })
454
+ .on("start", () => {
455
+ console.log("Plan execution started");
456
+ })
457
+ .on("content", (chunk) => {
458
+ console.log("Response chunk:", chunk);
459
+ })
460
+ .on("error", (error) => {
461
+ console.error("Error:", error);
462
+ });
463
+
464
+ const response = await plan.stream();
465
+
466
+ // Access final response data
467
+ console.log(
468
+ response.content,
469
+ response.completion_usage, // { completion_tokens: 200, prompt_tokens: 30, total_tokens: 230 }
470
+ response.executor_task_logs // [ { description: 'Task 1', duration: 100 }, { description: 'Task 2', duration: 500 }]
471
+ );
472
+
473
+ ```