skytells 1.0.3 → 1.0.5
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 +159 -28
- package/dist/chat.cjs +33 -0
- package/dist/chat.d.ts +82 -0
- package/dist/chat.js +33 -0
- package/dist/client.cjs +588 -65
- package/dist/client.d.ts +268 -26
- package/dist/client.js +588 -65
- package/dist/embeddings.cjs +40 -0
- package/dist/embeddings.d.ts +38 -0
- package/dist/embeddings.js +40 -0
- package/dist/endpoints.cjs +25 -9
- package/dist/endpoints.d.ts +16 -0
- package/dist/endpoints.js +25 -9
- package/dist/http.cjs +612 -60
- package/dist/http.d.ts +131 -3
- package/dist/http.js +612 -60
- package/dist/index.cjs +51 -8
- package/dist/index.d.ts +45 -9
- package/dist/index.js +51 -8
- package/dist/orchestrator.cjs +202 -0
- package/dist/orchestrator.d.ts +142 -0
- package/dist/orchestrator.js +202 -0
- package/dist/prediction-urls.cjs +38 -0
- package/dist/prediction-urls.d.ts +18 -0
- package/dist/prediction-urls.js +38 -0
- package/dist/responses.cjs +23 -0
- package/dist/responses.d.ts +60 -0
- package/dist/responses.js +23 -0
- package/dist/safety.cjs +323 -0
- package/dist/safety.d.ts +91 -0
- package/dist/safety.js +323 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/inference.types.d.ts +583 -0
- package/dist/types/inference.types.js +65 -0
- package/dist/types/model.types.d.ts +1 -1
- package/dist/types/orchestrator.types.d.ts +61 -0
- package/dist/types/orchestrator.types.js +7 -0
- package/dist/types/predict.types.d.ts +70 -11
- package/dist/types/shared.types.d.ts +119 -6
- package/dist/types/shared.types.js +58 -2
- package/dist/webhooks.cjs +310 -0
- package/dist/webhooks.d.ts +171 -0
- package/dist/webhooks.js +310 -0
- package/package.json +26 -2
package/README.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Skytells JavaScript/TypeScript SDK
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/skytells)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://docs.skytells.ai/sdks/ts/)
|
|
7
|
+
[](https://learn.skytells.ai/sdks/ts/)
|
|
8
|
+
[](docs/Architecture.md)
|
|
9
|
+
|
|
3
10
|
The official JavaScript/TypeScript SDK for interacting with the [Skytells](https://skytells.ai) API. Edge-compatible with Cloudflare Workers, Vercel Edge Functions, Netlify Edge Functions, and more.
|
|
4
11
|
|
|
5
12
|
Generate text, photos, videos, avatars, audio, music, and more using Skytells' own models and partner models — all through a single API. [Explore models →](https://skytells.ai/explore/models)
|
|
@@ -14,6 +21,28 @@ yarn add skytells
|
|
|
14
21
|
pnpm add skytells
|
|
15
22
|
```
|
|
16
23
|
|
|
24
|
+
## Import
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// Recommended: factory (default export)
|
|
28
|
+
import Skytells from 'skytells';
|
|
29
|
+
|
|
30
|
+
// Direct class import
|
|
31
|
+
import { SkytellsClient } from 'skytells';
|
|
32
|
+
|
|
33
|
+
// Both in one import
|
|
34
|
+
import Skytells, { SkytellsClient } from 'skytells';
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Optionally, you may use the named factory:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { createClient } from 'skytells';
|
|
41
|
+
|
|
42
|
+
const client = createClient('sk-your-api-key');
|
|
43
|
+
// equivalent to: Skytells('sk-your-api-key') and new SkytellsClient('sk-your-api-key')
|
|
44
|
+
```
|
|
45
|
+
|
|
17
46
|
## Quick Start
|
|
18
47
|
|
|
19
48
|
```typescript
|
|
@@ -29,6 +58,13 @@ const prediction = await skytells.run('truefusion', {
|
|
|
29
58
|
console.log(prediction.outputs()); // "https://delivery.skytells.cloud/..."
|
|
30
59
|
```
|
|
31
60
|
|
|
61
|
+
**Orchestrator** (workflows / webhook triggers) uses a separate `wfb_…` key. Optional **`orchestratorApiKey`** on the same client keeps both products in one place — the SDK applies the right auth per API ([Orchestrator.md](docs/Orchestrator.md)):
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
const client = Skytells('sk-…', { orchestratorApiKey: 'wfb_…' });
|
|
65
|
+
await client.orchestrator.webhooks.execute(workflowId, { /* JSON body */ });
|
|
66
|
+
```
|
|
67
|
+
|
|
32
68
|
### Obtaining an API Key
|
|
33
69
|
|
|
34
70
|
1. Log in at [Skytells Portal](https://www.skytells.ai/auth/signin) or [Create an Account](https://www.skytells.ai/auth/signup)
|
|
@@ -90,11 +126,33 @@ console.log(response.id, response.status); // "pred_..." "pending"
|
|
|
90
126
|
// Poll until complete
|
|
91
127
|
const result = await skytells.wait(response);
|
|
92
128
|
console.log(result.output);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Server-side Await
|
|
132
|
+
|
|
133
|
+
For image models, pass `await: true` to have the server block and return the final output in one request — no polling required:
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// Explicit server-side wait (image models only; video models ignore this)
|
|
137
|
+
const response = await skytells.predictions.create({
|
|
138
|
+
model: 'flux-pro',
|
|
139
|
+
input: { prompt: 'A cat' },
|
|
140
|
+
await: true,
|
|
141
|
+
});
|
|
142
|
+
console.log(response.output); // already populated
|
|
143
|
+
|
|
144
|
+
// Or let the SDK detect the model type automatically:
|
|
145
|
+
const response = await skytells.predictions.create(
|
|
146
|
+
{ model: 'flux-pro', input: { prompt: 'A cat' } },
|
|
147
|
+
{ compatibilityCheck: true, autoAwait: true }, // sets await:true only for image models
|
|
148
|
+
);
|
|
149
|
+
console.log(response.output);
|
|
93
150
|
|
|
94
|
-
// Or with a timeout and progress
|
|
151
|
+
// Or with a timeout and progress (first GET is immediate; then every interval)
|
|
95
152
|
const result = await skytells.wait(response, {
|
|
96
|
-
interval: 2000, //
|
|
97
|
-
maxWait: 120000, //
|
|
153
|
+
interval: 2000, // delay between polls after the first refresh
|
|
154
|
+
maxWait: 120000, // wall-clock limit → WAIT_TIMEOUT
|
|
155
|
+
// signal: ac.signal, // optional AbortSignal → ABORTED
|
|
98
156
|
}, (p) => console.log(p.status));
|
|
99
157
|
```
|
|
100
158
|
|
|
@@ -120,18 +178,14 @@ const completed = await Promise.all(results.map(r => skytells.wait(r)));
|
|
|
120
178
|
### Prediction Lifecycle
|
|
121
179
|
|
|
122
180
|
```typescript
|
|
123
|
-
// Cancel a running prediction
|
|
181
|
+
// Cancel a running prediction (works while status is pending/starting/processing)
|
|
124
182
|
await prediction.cancel();
|
|
125
|
-
// or by ID
|
|
126
|
-
await skytells.cancelPrediction('pred_abc123');
|
|
127
183
|
|
|
128
|
-
// Delete a prediction and its assets
|
|
184
|
+
// Delete a prediction and all its output assets from storage
|
|
129
185
|
await prediction.delete();
|
|
130
|
-
// or by ID
|
|
131
|
-
await skytells.deletePrediction('pred_abc123');
|
|
132
186
|
|
|
133
|
-
//
|
|
134
|
-
const stream = await
|
|
187
|
+
// Fetch stream metadata for a prediction
|
|
188
|
+
const stream = await prediction.stream();
|
|
135
189
|
console.log(stream.urls?.stream);
|
|
136
190
|
```
|
|
137
191
|
|
|
@@ -175,18 +229,20 @@ import Skytells from 'skytells';
|
|
|
175
229
|
|
|
176
230
|
const skytells = Skytells('your-api-key', {
|
|
177
231
|
baseUrl: 'https://your-proxy.example.com/v1', // Custom API URL
|
|
178
|
-
timeout: 30000,
|
|
179
|
-
headers: { 'X-Custom-Header': 'value' },
|
|
232
|
+
timeout: 30000, // Omit for defaults: 60000 normally, 25000 when runtime: 'edge'
|
|
233
|
+
headers: { 'X-Custom-Header': 'value' }, // Extra headers on every request
|
|
180
234
|
retry: {
|
|
181
|
-
retries: 3,
|
|
182
|
-
retryDelay: 1000,
|
|
183
|
-
retryOn: [429, 500, 502, 503, 504],
|
|
235
|
+
retries: 3, // Retry failed requests (default: 0)
|
|
236
|
+
retryDelay: 1000, // Base delay; actual wait is retryDelay * (attempt + 1) per retry
|
|
237
|
+
retryOn: [429, 500, 502, 503, 504],
|
|
184
238
|
},
|
|
185
|
-
fetch: (url, opts) =>
|
|
186
|
-
|
|
239
|
+
fetch: (url, opts) => fetch(url, { ...opts, cache: 'no-store' }),
|
|
240
|
+
// runtime: 'edge', // Vercel Edge / Workers: shorter default timeout, smaller compat cache, one-time hints
|
|
187
241
|
});
|
|
188
242
|
```
|
|
189
243
|
|
|
244
|
+
Inspect resolved settings: **`skytells.config`** (`requestTimeoutMs`, `prefetchMaxSlugs`, `runtime`) and **`skytells.runtime`**.
|
|
245
|
+
|
|
190
246
|
## The Prediction Object
|
|
191
247
|
|
|
192
248
|
When you call `skytells.run()`, you get a `Prediction` object:
|
|
@@ -213,15 +269,17 @@ When you call `skytells.run()`, you get a `Prediction` object:
|
|
|
213
269
|
|
|
214
270
|
## Edge Compatibility
|
|
215
271
|
|
|
216
|
-
This SDK works in any environment with Fetch
|
|
272
|
+
This SDK works in any environment with **Fetch** and (for **webhook HMAC verification**) **`crypto.subtle`** (Web Crypto):
|
|
217
273
|
|
|
218
274
|
- **Cloudflare Workers & Pages**
|
|
219
275
|
- **Vercel Edge Functions**
|
|
220
276
|
- **Netlify Edge Functions**
|
|
221
277
|
- **Deno Deploy**
|
|
222
|
-
- **Node.js
|
|
278
|
+
- **Node.js** — use **19+** for global `crypto.subtle`, or verify webhooks in an environment that provides it
|
|
223
279
|
- **Browsers**
|
|
224
280
|
|
|
281
|
+
Pass **`{ runtime: 'edge' }`** when constructing the client on edge/serverless so the SDK uses a **25s default request timeout** (if you don’t set `timeout`) and a **smaller** inference-compat model cache; see **`EDGE_DEFAULT_REQUEST_TIMEOUT_MS`** / **`EDGE_PREFETCH_MAX_SLUGS`** in the package exports.
|
|
282
|
+
|
|
225
283
|
## Error Handling
|
|
226
284
|
|
|
227
285
|
All methods throw `SkytellsError` on failure:
|
|
@@ -254,10 +312,12 @@ try {
|
|
|
254
312
|
| `RATE_LIMIT_EXCEEDED` | Too many requests |
|
|
255
313
|
| `PREDICTION_FAILED` | Prediction completed with failure |
|
|
256
314
|
| `WAIT_TIMEOUT` | Polling exceeded `maxWait` |
|
|
315
|
+
| `ABORTED` | `wait()` / `run` polling stopped via `AbortSignal` |
|
|
316
|
+
| `SDK_ERROR` | Client guard (OpenAI-compat model + `compatibilityCheck`, missing `prediction.id`, webhook crypto unavailable, …) |
|
|
257
317
|
| `REQUEST_TIMEOUT` | HTTP request timed out |
|
|
258
318
|
| `NETWORK_ERROR` | Connection issue |
|
|
259
319
|
| `SERVER_ERROR` | Non-JSON response from server |
|
|
260
|
-
| `INVALID_JSON` |
|
|
320
|
+
| `INVALID_JSON` | Declared JSON but body failed `JSON.parse` |
|
|
261
321
|
|
|
262
322
|
## TypeScript
|
|
263
323
|
|
|
@@ -266,16 +326,57 @@ Full type definitions are included. Key types:
|
|
|
266
326
|
```typescript
|
|
267
327
|
import type {
|
|
268
328
|
PredictionRequest,
|
|
329
|
+
PredictionSdkOptions,
|
|
269
330
|
PredictionResponse,
|
|
270
331
|
PredictionStatus,
|
|
271
332
|
RunOptions,
|
|
272
333
|
WaitOptions,
|
|
273
334
|
Model,
|
|
274
335
|
ClientOptions,
|
|
336
|
+
SkytellsRuntime,
|
|
275
337
|
PaginatedResponse,
|
|
276
338
|
} from 'skytells';
|
|
277
339
|
```
|
|
278
340
|
|
|
341
|
+
Inference guard: pass **`{ compatibilityCheck: true }`** as the **second** argument to **`predict`** / **`predictions.create`**, **fourth** to **`run`** (use `undefined` for `onProgress` if unused), or **second** to **`queue`** — never inside the JSON body.
|
|
342
|
+
|
|
343
|
+
Auto server-side await for image models: also pass **`autoAwait: true`** alongside `compatibilityCheck: true` in `predictions.create()`.
|
|
344
|
+
|
|
345
|
+
## Safety
|
|
346
|
+
|
|
347
|
+
Proactive content moderation and response parsing via `client.safety`:
|
|
348
|
+
|
|
349
|
+
```typescript
|
|
350
|
+
import Skytells, { SafetyTemplates } from 'skytells';
|
|
351
|
+
|
|
352
|
+
const client = Skytells(process.env.SKYTELLS_API_KEY);
|
|
353
|
+
|
|
354
|
+
// Check user input before sending to a model
|
|
355
|
+
const check = await client.safety.checkText(userInput, {
|
|
356
|
+
template: SafetyTemplates.MODERATE,
|
|
357
|
+
});
|
|
358
|
+
if (!check.passed) {
|
|
359
|
+
throw new Error(`Blocked: ${check.failedCategories.join(', ')}`);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Evaluate generated prediction output (image URLs are auto-detected)
|
|
363
|
+
const prediction = await client.run('flux-pro', { input: { prompt: userInput } });
|
|
364
|
+
const eval = await client.safety.evaluate(prediction.output, SafetyTemplates.STRICT);
|
|
365
|
+
if (!eval.passed) {
|
|
366
|
+
await prediction.delete();
|
|
367
|
+
throw new Error(`Output blocked: ${eval.failedCategories.join(', ')}`);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Parse content_filter_results from an existing chat completion (no extra API call)
|
|
371
|
+
const completion = await client.chat.completions.create({ ... });
|
|
372
|
+
if (client.safety.wasFiltered(completion)) {
|
|
373
|
+
const categories = client.safety.getFilteredCategories(completion);
|
|
374
|
+
console.warn('Filtered:', categories);
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
See [Safety.md](docs/Safety.md) for templates, all input types, and integration patterns.
|
|
379
|
+
|
|
279
380
|
## Migration from v1.0.2
|
|
280
381
|
|
|
281
382
|
```diff
|
|
@@ -294,13 +395,26 @@ import type {
|
|
|
294
395
|
+ const pred = await skytells.predictions.get(id);
|
|
295
396
|
```
|
|
296
397
|
|
|
398
|
+
`createClient` is still exported for compatibility; the first call logs a console hint to prefer `import Skytells from 'skytells'`.
|
|
399
|
+
|
|
297
400
|
> The old method names still work but log deprecation warnings and will be removed in a future version.
|
|
298
401
|
|
|
299
402
|
## Documentation
|
|
300
403
|
|
|
301
|
-
- See [Official Docs](https://docs.skytells.ai/sdks/ts/) for
|
|
302
|
-
- [
|
|
303
|
-
- [
|
|
404
|
+
- See [Official Docs](https://docs.skytells.ai/sdks/ts/) for hosted documentation.
|
|
405
|
+
- **[Client.md](docs/Client.md)** — `SkytellsClient` in full: options, every method, sub-APIs
|
|
406
|
+
- **[SDKReference.md](docs/SDKReference.md)** — Low-level reference: every class, method, type, constant
|
|
407
|
+
- [Guide.md](docs/Guide.md) — Getting started walkthroughs
|
|
408
|
+
- [Architecture.md](docs/Architecture.md) — Request pipeline, retries, streams, model cache
|
|
409
|
+
- [Reliability.md](docs/Reliability.md) — Timeouts, retries, AbortSignal, edge/serverless patterns
|
|
410
|
+
- [Errors.md](docs/Errors.md) — `SkytellsError` catalog
|
|
411
|
+
- [Prediction.md](docs/Prediction.md) — Predictions: run, wait, queue, cancel, delete
|
|
412
|
+
- [Chat.md](docs/Chat.md) — Chat completions (streaming + tools)
|
|
413
|
+
- [Responses.md](docs/Responses.md) — Responses API (SSE events, multi-turn)
|
|
414
|
+
- [Embeddings.md](docs/Embeddings.md) — Embeddings, semantic search, RAG
|
|
415
|
+
- [Safety.md](docs/Safety.md) — Content moderation, templates, prediction evaluation
|
|
416
|
+
- [Webhooks.md](docs/Webhooks.md) — Inbound webhooks, framework integrations, HMAC
|
|
417
|
+
- [Orchestrator.md](docs/Orchestrator.md) — Orchestrator workflows (`wfb_…` key)
|
|
304
418
|
|
|
305
419
|
### Non-JSON Response Handling
|
|
306
420
|
|
|
@@ -308,7 +422,7 @@ The SDK automatically handles cases when the server doesn't respond with valid J
|
|
|
308
422
|
|
|
309
423
|
```typescript
|
|
310
424
|
try {
|
|
311
|
-
const models = await skytells.
|
|
425
|
+
const models = await skytells.models.list();
|
|
312
426
|
} catch (error) {
|
|
313
427
|
if (error instanceof SkytellsError) {
|
|
314
428
|
if (error.errorId === 'SERVER_ERROR') {
|
|
@@ -344,9 +458,26 @@ npm run lint
|
|
|
344
458
|
|
|
345
459
|
See [CHANGELOG.md](CHANGELOG.md) for the latest changes.
|
|
346
460
|
|
|
347
|
-
##
|
|
348
|
-
|
|
349
|
-
|
|
461
|
+
## Documentation in this repo
|
|
462
|
+
|
|
463
|
+
| File | Description |
|
|
464
|
+
|------|-------------|
|
|
465
|
+
| [docs/Client.md](docs/Client.md) | `SkytellsClient`: options, methods, sub-APIs, best practices |
|
|
466
|
+
| [docs/SDKReference.md](docs/SDKReference.md) | Low-level reference: every export, class, type, and constant |
|
|
467
|
+
| [docs/Guide.md](docs/Guide.md) | Getting started — first prediction, chat, embeddings |
|
|
468
|
+
| [docs/Architecture.md](docs/Architecture.md) | Request pipeline, retries, streaming, model cache |
|
|
469
|
+
| [docs/Reliability.md](docs/Reliability.md) | Timeouts, retries, AbortSignal, edge/serverless |
|
|
470
|
+
| [docs/Errors.md](docs/Errors.md) | `SkytellsError` fields and all `errorId` values |
|
|
471
|
+
| [docs/Prediction.md](docs/Prediction.md) | Predictions: run, wait, queue, dispatch, cancel, webhooks |
|
|
472
|
+
| [docs/Chat.md](docs/Chat.md) | Chat completions, streaming, tools, vision |
|
|
473
|
+
| [docs/Responses.md](docs/Responses.md) | Responses API, SSE events, multi-turn |
|
|
474
|
+
| [docs/Embeddings.md](docs/Embeddings.md) | Embeddings, semantic search, RAG |
|
|
475
|
+
| [docs/Safety.md](docs/Safety.md) | Safety checks, templates, prediction output evaluation |
|
|
476
|
+
| [docs/Webhooks.md](docs/Webhooks.md) | Inbound webhooks, HMAC verification, framework integrations |
|
|
477
|
+
| [docs/Orchestrator.md](docs/Orchestrator.md) | Orchestrator: workflows, executions, integrations |
|
|
478
|
+
| [docs/.env.example](docs/.env.example) | All environment variables for local dev and CI |
|
|
479
|
+
|
|
480
|
+
Hosted: [Skytells TS SDK docs](https://docs.skytells.ai/sdks/ts/).
|
|
350
481
|
|
|
351
482
|
## License
|
|
352
483
|
|
package/dist/chat.cjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat API — OpenAI-compatible chat completions.
|
|
3
|
+
* Same API surface as OpenAI: client.chat.completions.create()
|
|
4
|
+
*
|
|
5
|
+
* @module chat
|
|
6
|
+
*/
|
|
7
|
+
const { ENDPOINTS } = require("./endpoints.js");
|
|
8
|
+
const { Responses } = require("./responses.js");
|
|
9
|
+
/**
|
|
10
|
+
* Completions sub-resource. Exposes create() for chat completions.
|
|
11
|
+
* Mirrors OpenAI's client.chat.completions API.
|
|
12
|
+
*/
|
|
13
|
+
export class Completions {
|
|
14
|
+
constructor(http) {
|
|
15
|
+
this.http = http;
|
|
16
|
+
}
|
|
17
|
+
create(params) {
|
|
18
|
+
if (params.stream === true) {
|
|
19
|
+
return this.http.requestStream(ENDPOINTS.CHAT_COMPLETIONS, params);
|
|
20
|
+
}
|
|
21
|
+
return this.http.request('POST', ENDPOINTS.CHAT_COMPLETIONS, params);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Chat resource. Exposes completions and responses sub-APIs.
|
|
26
|
+
* Mirrors OpenAI's client.chat structure.
|
|
27
|
+
*/
|
|
28
|
+
export class Chat {
|
|
29
|
+
constructor(http) {
|
|
30
|
+
this.completions = new Completions(http);
|
|
31
|
+
this.responses = new Responses(http);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/chat.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat API — OpenAI-compatible chat completions.
|
|
3
|
+
* Same API surface as OpenAI: client.chat.completions.create()
|
|
4
|
+
*
|
|
5
|
+
* @module chat
|
|
6
|
+
*/
|
|
7
|
+
import type { HTTP } from './http.js';
|
|
8
|
+
import { Responses } from './responses.js';
|
|
9
|
+
import type { ChatCompletion, ChatCompletionChunk, ChatCompletionCreateParamsNonStreaming, ChatCompletionCreateParamsStreaming } from './types/inference.types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Completions sub-resource. Exposes create() for chat completions.
|
|
12
|
+
* Mirrors OpenAI's client.chat.completions API.
|
|
13
|
+
*/
|
|
14
|
+
export declare class Completions {
|
|
15
|
+
private readonly http;
|
|
16
|
+
constructor(http: HTTP);
|
|
17
|
+
/**
|
|
18
|
+
* Creates a chat completion. Same as OpenAI's `client.chat.completions.create()`.
|
|
19
|
+
*
|
|
20
|
+
* When `stream` is `false` or omitted: returns `Promise<ChatCompletion>`.
|
|
21
|
+
* When `stream` is `true`: returns an `AsyncIterable<ChatCompletionChunk>` directly;
|
|
22
|
+
* consume with `for await…of` — no extra `await` needed.
|
|
23
|
+
*
|
|
24
|
+
* @param params - model, messages, and optional stream, tools, max_tokens, temperature, etc.
|
|
25
|
+
* @returns `Promise<ChatCompletion>` or `AsyncIterable<ChatCompletionChunk>`.
|
|
26
|
+
*
|
|
27
|
+
* @example Non-streaming:
|
|
28
|
+
* ```ts
|
|
29
|
+
* const completion = await client.chat.completions.create({
|
|
30
|
+
* model: 'deepbrain-router',
|
|
31
|
+
* messages: [{ role: 'user', content: 'Hello' }],
|
|
32
|
+
* });
|
|
33
|
+
* console.log(completion.choices[0].message.content);
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example Streaming:
|
|
37
|
+
* ```ts
|
|
38
|
+
* for await (const chunk of client.chat.completions.create({
|
|
39
|
+
* model: 'deepbrain-router',
|
|
40
|
+
* messages: [{ role: 'user', content: 'Hello' }],
|
|
41
|
+
* stream: true,
|
|
42
|
+
* })) {
|
|
43
|
+
* process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example Tool calling:
|
|
48
|
+
* ```ts
|
|
49
|
+
* const result = await client.chat.completions.create({
|
|
50
|
+
* model: 'deepbrain-router',
|
|
51
|
+
* messages: [{ role: 'user', content: 'What is the weather in London?' }],
|
|
52
|
+
* tools: [{
|
|
53
|
+
* type: 'function',
|
|
54
|
+
* function: {
|
|
55
|
+
* name: 'get_weather',
|
|
56
|
+
* description: 'Get current weather for a location.',
|
|
57
|
+
* parameters: {
|
|
58
|
+
* type: 'object',
|
|
59
|
+
* properties: { location: { type: 'string' } },
|
|
60
|
+
* required: ['location'],
|
|
61
|
+
* },
|
|
62
|
+
* },
|
|
63
|
+
* }],
|
|
64
|
+
* tool_choice: 'auto',
|
|
65
|
+
* });
|
|
66
|
+
* const toolCall = result.choices[0].message.tool_calls?.[0];
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
create(params: ChatCompletionCreateParamsStreaming): AsyncIterable<ChatCompletionChunk>;
|
|
70
|
+
create(params: ChatCompletionCreateParamsNonStreaming): Promise<ChatCompletion>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Chat resource. Exposes completions and responses sub-APIs.
|
|
74
|
+
* Mirrors OpenAI's client.chat structure.
|
|
75
|
+
*/
|
|
76
|
+
export declare class Chat {
|
|
77
|
+
/** Chat completions. Same as OpenAI's `client.chat.completions` */
|
|
78
|
+
readonly completions: Completions;
|
|
79
|
+
/** Responses API (`POST /v1/responses`). Same surface as OpenAI-style `client.responses`. */
|
|
80
|
+
readonly responses: Responses;
|
|
81
|
+
constructor(http: HTTP);
|
|
82
|
+
}
|
package/dist/chat.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat API — OpenAI-compatible chat completions.
|
|
3
|
+
* Same API surface as OpenAI: client.chat.completions.create()
|
|
4
|
+
*
|
|
5
|
+
* @module chat
|
|
6
|
+
*/
|
|
7
|
+
import { ENDPOINTS } from './endpoints.js';
|
|
8
|
+
import { Responses } from './responses.js';
|
|
9
|
+
/**
|
|
10
|
+
* Completions sub-resource. Exposes create() for chat completions.
|
|
11
|
+
* Mirrors OpenAI's client.chat.completions API.
|
|
12
|
+
*/
|
|
13
|
+
export class Completions {
|
|
14
|
+
constructor(http) {
|
|
15
|
+
this.http = http;
|
|
16
|
+
}
|
|
17
|
+
create(params) {
|
|
18
|
+
if (params.stream === true) {
|
|
19
|
+
return this.http.requestStream(ENDPOINTS.CHAT_COMPLETIONS, params);
|
|
20
|
+
}
|
|
21
|
+
return this.http.request('POST', ENDPOINTS.CHAT_COMPLETIONS, params);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Chat resource. Exposes completions and responses sub-APIs.
|
|
26
|
+
* Mirrors OpenAI's client.chat structure.
|
|
27
|
+
*/
|
|
28
|
+
export class Chat {
|
|
29
|
+
constructor(http) {
|
|
30
|
+
this.completions = new Completions(http);
|
|
31
|
+
this.responses = new Responses(http);
|
|
32
|
+
}
|
|
33
|
+
}
|