@elsium-ai/client 0.3.0 → 0.4.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/README.md +139 -0
- package/dist/index.js +28 -16
- package/dist/sse-parser.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -148,6 +148,145 @@ interface ElsiumClient {
|
|
|
148
148
|
|
|
149
149
|
---
|
|
150
150
|
|
|
151
|
+
## Type Definitions
|
|
152
|
+
|
|
153
|
+
### `ClientConfig`
|
|
154
|
+
|
|
155
|
+
Configuration for creating a client instance.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
interface ClientConfig {
|
|
159
|
+
baseUrl: string
|
|
160
|
+
apiKey?: string
|
|
161
|
+
timeout?: number
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
| Field | Type | Default | Description |
|
|
166
|
+
|-------|------|---------|-------------|
|
|
167
|
+
| `baseUrl` | `string` | **(required)** | The base URL of the ElsiumAI server. |
|
|
168
|
+
| `apiKey` | `string` | `undefined` | API token sent as `Authorization: Bearer` header. |
|
|
169
|
+
| `timeout` | `number` | `30000` | Request timeout in milliseconds. |
|
|
170
|
+
|
|
171
|
+
### `ChatRequest`
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
interface ChatRequest {
|
|
175
|
+
message: string
|
|
176
|
+
agent?: string
|
|
177
|
+
stream?: boolean
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### `ChatResponse`
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
interface ChatResponse {
|
|
185
|
+
message: string
|
|
186
|
+
usage: { inputTokens: number; outputTokens: number; totalTokens: number; cost: number }
|
|
187
|
+
model: string
|
|
188
|
+
traceId: string
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `CompleteRequest`
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
interface CompleteRequest {
|
|
196
|
+
messages: Array<{ role: string; content: string }>
|
|
197
|
+
model?: string
|
|
198
|
+
system?: string
|
|
199
|
+
maxTokens?: number
|
|
200
|
+
temperature?: number
|
|
201
|
+
stream?: boolean
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### `CompleteResponse`
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
interface CompleteResponse {
|
|
209
|
+
message: string
|
|
210
|
+
usage: { inputTokens: number; outputTokens: number; totalTokens: number }
|
|
211
|
+
model: string
|
|
212
|
+
stopReason: string
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### `HealthResponse`
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
interface HealthResponse {
|
|
220
|
+
status: 'ok' | 'degraded'
|
|
221
|
+
version: string
|
|
222
|
+
uptime: number
|
|
223
|
+
providers: string[]
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `MetricsResponse`
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
interface MetricsResponse {
|
|
231
|
+
uptime: number
|
|
232
|
+
totalRequests: number
|
|
233
|
+
totalTokens: number
|
|
234
|
+
totalCost: number
|
|
235
|
+
byModel: Record<string, { requests: number; tokens: number; cost: number }>
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### `AgentInfo`
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
interface AgentInfo {
|
|
243
|
+
name: string
|
|
244
|
+
model?: string
|
|
245
|
+
tools: string[]
|
|
246
|
+
description?: string
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### `StreamEvent`
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
type StreamEvent =
|
|
254
|
+
| { type: 'text_delta'; text: string }
|
|
255
|
+
| { type: 'message_end'; usage: { inputTokens: number; outputTokens: number; totalTokens: number } }
|
|
256
|
+
| { type: 'error'; error: string }
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## Error Handling
|
|
262
|
+
|
|
263
|
+
The client throws errors with descriptive messages for common failure cases. Wrap calls in try/catch for robust error handling:
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
import { createClient } from '@elsium-ai/client'
|
|
267
|
+
|
|
268
|
+
const client = createClient({
|
|
269
|
+
baseUrl: 'http://localhost:3000',
|
|
270
|
+
apiKey: 'my-token',
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
try {
|
|
274
|
+
const response = await client.chat({ message: 'Hello' })
|
|
275
|
+
console.log(response.message)
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (error instanceof Error) {
|
|
278
|
+
// Common errors:
|
|
279
|
+
// - Network errors (server unreachable)
|
|
280
|
+
// - 401 Unauthorized (invalid or missing API key)
|
|
281
|
+
// - 429 Too Many Requests (rate limited)
|
|
282
|
+
// - 500 Internal Server Error
|
|
283
|
+
console.error('Request failed:', error.message)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
151
290
|
## License
|
|
152
291
|
|
|
153
292
|
[MIT](https://github.com/elsium-ai/elsium-ai/blob/main/LICENSE) - Copyright (c) 2026 Eric Utrera
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
// src/sse-parser.ts
|
|
2
|
-
|
|
3
|
-
if (
|
|
4
|
-
|
|
2
|
+
function parseSSELine(line) {
|
|
3
|
+
if (line.startsWith("event: error"))
|
|
4
|
+
return;
|
|
5
|
+
if (!line.startsWith("data: "))
|
|
6
|
+
return;
|
|
7
|
+
const data = line.slice(6).trim();
|
|
8
|
+
if (!data || data === "[DONE]")
|
|
9
|
+
return;
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(data);
|
|
12
|
+
} catch {
|
|
13
|
+
return;
|
|
5
14
|
}
|
|
6
|
-
|
|
15
|
+
}
|
|
16
|
+
async function* readSSELines(response) {
|
|
17
|
+
const reader = response.body?.getReader();
|
|
18
|
+
if (!reader)
|
|
19
|
+
return;
|
|
7
20
|
const decoder = new TextDecoder;
|
|
8
21
|
let buffer = "";
|
|
9
22
|
try {
|
|
@@ -16,24 +29,23 @@ async function* parseSSEStream(response) {
|
|
|
16
29
|
`);
|
|
17
30
|
buffer = lines.pop() ?? "";
|
|
18
31
|
for (const line of lines) {
|
|
19
|
-
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
if (!line.startsWith("data: "))
|
|
23
|
-
continue;
|
|
24
|
-
const data = line.slice(6).trim();
|
|
25
|
-
if (!data || data === "[DONE]")
|
|
26
|
-
continue;
|
|
27
|
-
try {
|
|
28
|
-
const event = JSON.parse(data);
|
|
29
|
-
yield event;
|
|
30
|
-
} catch {}
|
|
32
|
+
yield line;
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
35
|
} finally {
|
|
34
36
|
reader.releaseLock();
|
|
35
37
|
}
|
|
36
38
|
}
|
|
39
|
+
async function* parseSSEStream(response) {
|
|
40
|
+
if (!response.body) {
|
|
41
|
+
throw new Error("Response body is null");
|
|
42
|
+
}
|
|
43
|
+
for await (const line of readSSELines(response)) {
|
|
44
|
+
const event = parseSSELine(line);
|
|
45
|
+
if (event)
|
|
46
|
+
yield event;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
37
49
|
|
|
38
50
|
// src/client.ts
|
|
39
51
|
function createClient(config) {
|
package/dist/sse-parser.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse-parser.d.ts","sourceRoot":"","sources":["../src/sse-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"sse-parser.d.ts","sourceRoot":"","sources":["../src/sse-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AA2ClD,wBAAuB,cAAc,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,CAAC,WAAW,CAAC,CASpF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elsium-ai/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "HTTP client SDK for ElsiumAI server",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Eric Utrera <ebutrera9103@gmail.com>",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dev": "bun --watch src/index.ts"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@elsium-ai/core": "^0.
|
|
29
|
+
"@elsium-ai/core": "^0.4.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"typescript": "^5.7.0"
|