1
1
<div align =" center " >
2
- <h1>Dust SDK for JavaScript</h1>
3
- <p>
4
- <b>A simple and easy to use client for the <a href="https://docs.dust.tt/reference/developer-platform-overview">Dust API</a></b>
5
- </p>
6
- <br>
2
+ <h1>Dust SDK for JavaScript</h1>
3
+ <p>
4
+ <b>A simple and easy to use client for the <a href="https://docs.dust.tt/reference/developer-platform-overview">Dust API</a></b>
5
+ </p>
6
+ <br>
7
7
</div >
8
8
9
9
![ Build status] ( https://github.com/dust-tt/dust/actions/workflows/deploy-front.yml/badge.svg )
@@ -13,15 +13,140 @@ Source code available [here](https://github.com/dust-tt/dust/tree/main/sdks/js).
13
13
14
14
## Installation
15
15
16
- ```
16
+ ``` js
17
17
npm install @dust- tt/ client
18
18
```
19
19
20
20
## Usage
21
21
22
22
> Use Dust's [ Dust Development Platform] ( https://docs.dust.tt/reference/developer-platform-overview ) to start learning about Dust API.
23
23
24
- Import and initialize a client using an ** workspace api key** or an OAuth ** access token** .
24
+ Import and initialize a client using an ** workspace api key** or an OAuth ** access token** (for now, requires talking to us).
25
+
26
+ Note: we use the Result pattern to handle errors ([ see more] ( https://velocidadescape.com/js/result-pattern-try-catch/ ) ).
27
+
28
+ ### Setup
29
+
30
+ ``` js
31
+ import { DustAPI } from " @dust-tt/client" ;
32
+
33
+ const dustAPI = new DustAPI (
34
+ {
35
+ url: " https://dust.tt" ,
36
+ },
37
+ {
38
+ workspaceId: " YOUR_WORKSPACE_ID" ,
39
+ apiKey: " YOUR_API_KEY_OR_ACCESS_TOKEN" ,
40
+ },
41
+ console
42
+ );
43
+ ```
44
+
45
+ ### Get all assistants (aka "agents")
46
+
47
+ ``` js
48
+ const r = await dustApi .getAgentConfigurations ();
49
+
50
+ if (r .isErr ()) {
51
+ throw new Error (` API Error: ${ r .error .message } ` );
52
+ } else {
53
+ const agents = r .value .filter ((agent ) => agent .status === " active" );
54
+ }
55
+ ```
56
+
57
+ ### Create a conversation
58
+
59
+ ``` js
60
+ const context = {
61
+ timezone: " UTC" ,
62
+ username: user .firstName ,
63
+ email: user .email ,
64
+ fullName: user .fullName ,
65
+ profilePictureUrl: user .image ,
66
+ origin: " api" , // Contact us to add more
67
+ };
68
+
69
+ const question = " Hello! What can you do for me ?" ;
70
+
71
+ const r = await dustApi .createConversation ({
72
+ title: null ,
73
+ visibility: " unlisted" ,
74
+ message: {
75
+ content: question,
76
+ mentions: [
77
+ {
78
+ configurationId: agent .sId ,
79
+ },
80
+ ],
81
+ context,
82
+ },
83
+ });
25
84
26
- ### Handling errors
85
+ if (r .isErr ()) {
86
+ throw new Error (r .error .message );
87
+ } else {
88
+ const { conversation , message } = r .value ;
27
89
90
+ try {
91
+ const r = await dustApi .streamAgentAnswerEvents ({
92
+ conversation,
93
+ userMessageId: message .sId ,
94
+ signal,
95
+ });
96
+ if (r .isErr ()) {
97
+ throw new Error (r .error .message );
98
+ } else {
99
+ const { eventStream } = r .value ;
100
+
101
+ let answer = " " ;
102
+ let action: AgentActionPublicType | undefined = undefined ;
103
+ let chainOfThought = " " ;
104
+
105
+ for await (const event of eventStream ) {
106
+ if (! event ) {
107
+ continue ;
108
+ }
109
+ switch (event .type ) {
110
+ case " user_message_error" : {
111
+ console .error (
112
+ ` User message error: code: ${ event .error .code } message: ${ event .error .message } `
113
+ );
114
+ return ;
115
+ }
116
+ case " agent_error" : {
117
+ console .error (
118
+ ` Agent message error: code: ${ event .error .code } message: ${ event .error .message } `
119
+ );
120
+ return ;
121
+ }
122
+ case " agent_action_success" : {
123
+ action = event .action ;
124
+ break ;
125
+ }
126
+
127
+ case " generation_tokens" : {
128
+ if (event .classification === " tokens" ) {
129
+ answer = (answer + event .text ).trim ();
130
+ } else if (event .classification === " chain_of_thought" ) {
131
+ chainOfThought += event .text ;
132
+ }
133
+ break ;
134
+ }
135
+ case " agent_message_success" : {
136
+ answer = event .message .content ?? " " ;
137
+ break ;
138
+ }
139
+ default:
140
+ // Nothing to do on unsupported events
141
+ }
142
+ }
143
+ }
144
+ } catch (error) {
145
+ if (error instanceof Error && error .message .includes (" AbortError" )) {
146
+ // Stream aborted
147
+ } else {
148
+ // Other error
149
+ }
150
+ }
151
+ }
152
+ ` ` `
0 commit comments