Skip to content

Commit c3400d6

Browse files
committed
AirOps JS SDK.
0 parents  commit c3400d6

16 files changed

+3834
-0
lines changed

.env.prod.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_KEY=
2+
APP_CLUSTER=

.env.staging.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_KEY=
2+
APP_CLUSTER=

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.env.prod
2+
.env.staging
3+
/dist
4+
/node_modules

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16.13.2

README.md

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# @airops/sdk
2+
3+
## Getting Started
4+
5+
1. Server configuration
6+
7+
To authenticate with our API using the SDK, you'll need three pieces of information: your workspace id, user
8+
id, and API key. First, use the API key to hash your user id on your back-end server. This will result in a
9+
hashed user id that is unique to your API key and user id combination. Workspace id and API key can be found in your
10+
workspace settings. Below is an example nodejs configuration.
11+
12+
```javascript
13+
const crypto = require('crypto');
14+
15+
const userIdHash = () => {
16+
const apiKey = "YOUR_API_KEY";
17+
const userId = "YOUR_USER_ID";
18+
19+
// Convert your api key to a buffer
20+
const key = Buffer.from(apiKey, 'utf-8');
21+
22+
// Hash the message using HMAC-SHA256 and the key
23+
const hash = crypto.createHmac('sha256', key)
24+
.update(userId)
25+
.digest('hex');
26+
27+
return hash;
28+
}
29+
```
30+
2. Install the client SDK
31+
32+
```bash
33+
npm i @airops/js_sdk
34+
```
35+
36+
3. Identify the user
37+
38+
```javascript
39+
const airopsInstance = AirOps.identify({
40+
userId: 'YOUR_USER_ID',
41+
workspaceId: 'YOUR_WORKSPACE_ID',
42+
hashedUserId: 'YOUR_USER_ID_HASH'
43+
})
44+
```
45+
46+
4. Use it to execute an app
47+
48+
Once you have successfully initialized the SDK, you can begin using the methods available to interact with our API.
49+
Note that the methods will return promises.
50+
51+
```javascript
52+
// Execute an app
53+
const response = await airopsInstance.apps.execute({
54+
appId: 1,
55+
version: 1,
56+
payload: {
57+
"inputs": {
58+
"name": "XXXXYYYYZZZZ"
59+
}
60+
}
61+
});
62+
63+
// Wait for result
64+
const result = await response.result();
65+
// Do something with result.output
66+
67+
// Cancel execution
68+
await response.cancel();
69+
```
70+
71+
5. Example response
72+
73+
The response from the execute method will contain the execution id that can be used to retrieve results later along with two methods to wait for results or cancel the execution:
74+
75+
```typescript
76+
interface ExecuteResponse {
77+
executionId: number;
78+
result: () => Promise<AppExecution>;
79+
cancel: () => Promise<void>;
80+
}
81+
82+
interface AppExecution {
83+
airops_app_id: number;
84+
id: number;
85+
status: string;
86+
output: string;
87+
stream_channel_id: string;
88+
}
89+
```
90+
91+
6. Execute an app with streaming
92+
93+
In order to stream the app results you will need to enable stream and pass a callback function to the execute method.
94+
Optionally you can pass an extra callback function to get a notification when the app is finished.
95+
96+
```javascript
97+
const response = await airopsInstance.apps.execute({
98+
appId: 1,
99+
version: 1,
100+
payload: {
101+
inputs: {
102+
name: "XXXXYYYYZZZZ",
103+
},
104+
},
105+
stream: true,
106+
streamCallback: (data: { content: string }) => {
107+
// Do something with the data
108+
},
109+
streamCompletedCallback: (data: { content: string }) => {
110+
// Do something with the data
111+
},
112+
});
113+
```
114+
115+
7. Pull results async
116+
117+
You can implement your own pulling logic using the getResults method.
118+
119+
```javascript
120+
const result = await airopsInstance.apps.getResults({
121+
appId: 1,
122+
executionId: response.executionId,
123+
});
124+
```
125+
126+
8. Error handling
127+
```javascript
128+
try {
129+
await airopsInstance.apps.execute({
130+
appId: 1,
131+
version: 4,
132+
payload: {
133+
inputs: { name: "XXXXYYYYZZZZ" },
134+
},
135+
});
136+
} catch (e) {
137+
// Do something with error.message
138+
}
139+
```
140+
141+
9. Chat assistant
142+
143+
```javascript
144+
const response = await airopsInstance.apps.chatStream({
145+
appId: 2,
146+
message,
147+
streamCallback: (data: { token: string; }) => {
148+
// do something with data.token
149+
},
150+
streamCompletedCallback: (data: { result: string }) => {
151+
// do something with data.result
152+
},
153+
...(sessionId && { sessionId }), // optionally pass sessionId to continue chat.
154+
});
155+
// Wait for result
156+
const result = await response.result;
157+
// Do something with result.result
158+
159+
// Use session id to continue chat
160+
response.sessionId;
161+
```
162+
163+
10. Example response
164+
165+
The response from the chatStream method will contain the sessionId and a result method to wait for the response.
166+
In order to continue with the chat pass the sessionId along with the message.
167+
168+
```typescript
169+
export interface ChatStreamResponse {
170+
sessionId: string;
171+
result: Promise<{ result: string }>; // result is a promise that resolves when the execution is completed.
172+
}
173+
```

0 commit comments

Comments
 (0)