@altsafe/aidirector 1.0.0 → 1.0.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 +187 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# @aidirector
|
|
1
|
+
# @altsafe/aidirector
|
|
2
2
|
|
|
3
3
|
Official TypeScript SDK for **AI Director** — the intelligent AI API gateway with automatic failover, response caching, and JSON extraction.
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/@altsafe/aidirector)
|
|
6
6
|
[](https://www.typescriptlang.org/)
|
|
7
7
|
[](https://opensource.org/licenses/MIT)
|
|
8
8
|
|
|
@@ -18,21 +18,21 @@ Official TypeScript SDK for **AI Director** — the intelligent AI API gateway w
|
|
|
18
18
|
## Installation
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
|
-
npm install @aidirector
|
|
21
|
+
npm install @altsafe/aidirector
|
|
22
22
|
# or
|
|
23
|
-
bun add @aidirector
|
|
23
|
+
bun add @altsafe/aidirector
|
|
24
24
|
# or
|
|
25
|
-
pnpm add @aidirector
|
|
25
|
+
pnpm add @altsafe/aidirector
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
## Quick Start
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
|
-
import { AIDirector } from '@aidirector
|
|
31
|
+
import { AIDirector } from '@altsafe/aidirector';
|
|
32
32
|
|
|
33
33
|
const client = new AIDirector({
|
|
34
34
|
secretKey: process.env.AIDIRECTOR_SECRET_KEY!,
|
|
35
|
-
baseUrl: 'https://api.
|
|
35
|
+
baseUrl: 'https://your-api.example.com',
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
const result = await client.generate({
|
|
@@ -55,7 +55,7 @@ if (result.success) {
|
|
|
55
55
|
```typescript
|
|
56
56
|
'use server';
|
|
57
57
|
|
|
58
|
-
import { AIDirector } from '@aidirector
|
|
58
|
+
import { AIDirector } from '@altsafe/aidirector';
|
|
59
59
|
|
|
60
60
|
const client = new AIDirector({
|
|
61
61
|
secretKey: process.env.AIDIRECTOR_SECRET_KEY!,
|
|
@@ -74,7 +74,7 @@ export async function generateContent(prompt: string) {
|
|
|
74
74
|
```typescript
|
|
75
75
|
// app/api/generate/route.ts
|
|
76
76
|
import { NextResponse } from 'next/server';
|
|
77
|
-
import { AIDirector } from '@aidirector
|
|
77
|
+
import { AIDirector } from '@altsafe/aidirector';
|
|
78
78
|
|
|
79
79
|
const client = new AIDirector({
|
|
80
80
|
secretKey: process.env.AIDIRECTOR_SECRET_KEY!,
|
|
@@ -110,8 +110,10 @@ import {
|
|
|
110
110
|
AIDirector,
|
|
111
111
|
TimeoutError,
|
|
112
112
|
RateLimitError,
|
|
113
|
-
|
|
114
|
-
|
|
113
|
+
ValidationError,
|
|
114
|
+
isAIDirectorError,
|
|
115
|
+
isRetryableError,
|
|
116
|
+
} from '@altsafe/aidirector';
|
|
115
117
|
|
|
116
118
|
try {
|
|
117
119
|
const result = await client.generate({ chainId, prompt });
|
|
@@ -120,8 +122,15 @@ try {
|
|
|
120
122
|
console.log('Request timed out after', error.timeoutMs, 'ms');
|
|
121
123
|
} else if (error instanceof RateLimitError) {
|
|
122
124
|
console.log('Rate limited, retry after', error.retryAfterMs, 'ms');
|
|
125
|
+
} else if (error instanceof ValidationError) {
|
|
126
|
+
console.log('Validation failed:', error.validationErrors);
|
|
123
127
|
} else if (isAIDirectorError(error)) {
|
|
124
128
|
console.log('AI Director error:', error.code, error.message);
|
|
129
|
+
|
|
130
|
+
// Check if we should retry
|
|
131
|
+
if (isRetryableError(error)) {
|
|
132
|
+
console.log('This error is retryable');
|
|
133
|
+
}
|
|
125
134
|
}
|
|
126
135
|
}
|
|
127
136
|
```
|
|
@@ -140,6 +149,8 @@ try {
|
|
|
140
149
|
|
|
141
150
|
### `client.generate(options)`
|
|
142
151
|
|
|
152
|
+
Generate content using your fallback chain.
|
|
153
|
+
|
|
143
154
|
| Option | Type | Required | Description |
|
|
144
155
|
|--------|------|----------|-------------|
|
|
145
156
|
| `chainId` | `string` | Yes | Fallback chain ID |
|
|
@@ -148,39 +159,186 @@ try {
|
|
|
148
159
|
| `timeout` | `number` | No | Override timeout for this request |
|
|
149
160
|
| `options.temperature` | `number` | No | Randomness (0-2) |
|
|
150
161
|
| `options.maxTokens` | `number` | No | Max output tokens |
|
|
162
|
+
| `options.topP` | `number` | No | Nucleus sampling (0-1) |
|
|
163
|
+
| `options.topK` | `number` | No | Top-K sampling |
|
|
164
|
+
| `options.systemPrompt` | `string` | No | System prompt to prepend |
|
|
165
|
+
|
|
166
|
+
**Returns:** `Promise<GenerateResult>`
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
interface GenerateResult {
|
|
170
|
+
success: boolean;
|
|
171
|
+
data: {
|
|
172
|
+
valid: unknown[]; // Schema-compliant objects
|
|
173
|
+
invalid: unknown[]; // Failed validation but parsed
|
|
174
|
+
rawContent?: string; // Raw AI response
|
|
175
|
+
};
|
|
176
|
+
meta: {
|
|
177
|
+
cached: boolean;
|
|
178
|
+
modelUsed: string;
|
|
179
|
+
tokensUsed: { input: number; output: number };
|
|
180
|
+
latencyMs: number;
|
|
181
|
+
attemptedModels: string[];
|
|
182
|
+
finishReason?: string;
|
|
183
|
+
};
|
|
184
|
+
error?: {
|
|
185
|
+
code: string;
|
|
186
|
+
message: string;
|
|
187
|
+
retryable?: boolean;
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `client.generateStream(options, callbacks)`
|
|
193
|
+
|
|
194
|
+
Stream responses in real-time.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
interface StreamCallbacks {
|
|
198
|
+
onChunk?: (chunk: string) => void;
|
|
199
|
+
onComplete?: (result: GenerateResult) => void;
|
|
200
|
+
onError?: (error: Error) => void;
|
|
201
|
+
onProgress?: (progress: StreamProgress) => void;
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### `client.listModels()`
|
|
151
206
|
|
|
152
|
-
|
|
207
|
+
List all available AI models.
|
|
153
208
|
|
|
154
209
|
```typescript
|
|
155
|
-
// List available models
|
|
156
210
|
const models = await client.listModels();
|
|
211
|
+
// Returns: ModelInfo[]
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### `client.listChains()`
|
|
157
215
|
|
|
158
|
-
|
|
216
|
+
Get your fallback chains (requires session auth).
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
159
219
|
const chains = await client.listChains();
|
|
220
|
+
// Returns: ChainInfo[]
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### `client.getUsage(options?)`
|
|
224
|
+
|
|
225
|
+
Get usage statistics for your account.
|
|
160
226
|
|
|
161
|
-
|
|
227
|
+
```typescript
|
|
162
228
|
const usage = await client.getUsage({
|
|
163
|
-
startDate: new Date('2024-01-01')
|
|
229
|
+
startDate: new Date('2024-01-01'),
|
|
230
|
+
endDate: new Date(),
|
|
164
231
|
});
|
|
232
|
+
// Returns: UsageStats
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### `client.health()`
|
|
236
|
+
|
|
237
|
+
Health check to verify API connection.
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
const { ok, latencyMs, version } = await client.health();
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### `client.withConfig(overrides)`
|
|
165
244
|
|
|
166
|
-
|
|
167
|
-
const { ok, latencyMs } = await client.health();
|
|
245
|
+
Create a new client with different configuration.
|
|
168
246
|
|
|
169
|
-
|
|
247
|
+
```typescript
|
|
170
248
|
const debugClient = client.withConfig({ debug: true });
|
|
249
|
+
const fastClient = client.withConfig({ timeout: 30000 });
|
|
171
250
|
```
|
|
172
251
|
|
|
173
252
|
## Error Classes
|
|
174
253
|
|
|
175
|
-
| Error | Code | Retryable | Description |
|
|
176
|
-
|
|
177
|
-
| `ConfigurationError` | `CONFIGURATION_ERROR` | No | Invalid client setup |
|
|
178
|
-
| `AuthenticationError` | `AUTH_ERROR` | No | Invalid credentials |
|
|
179
|
-
| `RateLimitError` | `RATE_LIMITED` | Yes | Too many requests |
|
|
180
|
-
| `TimeoutError` | `TIMEOUT` | Yes | Request timed out |
|
|
181
|
-
| `NetworkError` | `NETWORK_ERROR` | Yes | Connection failed |
|
|
182
|
-
| `ChainExecutionError` | `CHAIN_FAILED` | No | All models failed |
|
|
183
|
-
| `
|
|
254
|
+
| Error | Code | Retryable | Properties | Description |
|
|
255
|
+
|-------|------|-----------|------------|-------------|
|
|
256
|
+
| `ConfigurationError` | `CONFIGURATION_ERROR` | No | — | Invalid client setup |
|
|
257
|
+
| `AuthenticationError` | `AUTH_ERROR` | No | `statusCode` | Invalid credentials |
|
|
258
|
+
| `RateLimitError` | `RATE_LIMITED` | Yes | `retryAfterMs` | Too many requests |
|
|
259
|
+
| `TimeoutError` | `TIMEOUT` | Yes | `timeoutMs` | Request timed out |
|
|
260
|
+
| `NetworkError` | `NETWORK_ERROR` | Yes | `originalError` | Connection failed |
|
|
261
|
+
| `ChainExecutionError` | `CHAIN_FAILED` | No | `attemptedModels` | All models failed |
|
|
262
|
+
| `ValidationError` | `VALIDATION_ERROR` | No | `validationErrors` | Schema validation failed |
|
|
263
|
+
| `ServerError` | `SERVER_ERROR` | Yes | `statusCode` | Internal server error |
|
|
264
|
+
|
|
265
|
+
### Error Helpers
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
import { isAIDirectorError, isRetryableError } from '@altsafe/aidirector';
|
|
269
|
+
|
|
270
|
+
// Check if error is from AI Director
|
|
271
|
+
if (isAIDirectorError(error)) {
|
|
272
|
+
console.log(error.code, error.message);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Check if error should be retried
|
|
276
|
+
if (isRetryableError(error)) {
|
|
277
|
+
// Implement retry logic
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Advanced: HMAC Utilities
|
|
282
|
+
|
|
283
|
+
For custom integrations, you can use the HMAC utilities directly:
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import {
|
|
287
|
+
generateSignature,
|
|
288
|
+
getKeyPrefix,
|
|
289
|
+
isValidSecretKey
|
|
290
|
+
} from '@altsafe/aidirector';
|
|
291
|
+
|
|
292
|
+
// Validate a secret key format
|
|
293
|
+
if (isValidSecretKey(key)) {
|
|
294
|
+
console.log('Valid key');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Get key prefix for headers
|
|
298
|
+
const prefix = getKeyPrefix(secretKey); // "aid_sk_xxxx"
|
|
299
|
+
|
|
300
|
+
// Generate signature for custom requests
|
|
301
|
+
const signature = await generateSignature(
|
|
302
|
+
secretKey,
|
|
303
|
+
'POST',
|
|
304
|
+
'/api/v1/generate',
|
|
305
|
+
JSON.stringify(body),
|
|
306
|
+
Date.now()
|
|
307
|
+
);
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## TypeScript Types
|
|
311
|
+
|
|
312
|
+
All types are exported for your convenience:
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import type {
|
|
316
|
+
// Configuration
|
|
317
|
+
AIDirectorConfig,
|
|
318
|
+
|
|
319
|
+
// Generation
|
|
320
|
+
GenerateOptions,
|
|
321
|
+
GenerateResult,
|
|
322
|
+
GenerateData,
|
|
323
|
+
GenerateMeta,
|
|
324
|
+
GenerateError,
|
|
325
|
+
GenerationParameters,
|
|
326
|
+
TokenUsage,
|
|
327
|
+
|
|
328
|
+
// Streaming
|
|
329
|
+
StreamCallbacks,
|
|
330
|
+
StreamProgress,
|
|
331
|
+
|
|
332
|
+
// Chains & Models
|
|
333
|
+
ChainInfo,
|
|
334
|
+
ChainStep,
|
|
335
|
+
ModelInfo,
|
|
336
|
+
|
|
337
|
+
// Usage & Health
|
|
338
|
+
UsageStats,
|
|
339
|
+
HealthResult,
|
|
340
|
+
} from '@altsafe/aidirector';
|
|
341
|
+
```
|
|
184
342
|
|
|
185
343
|
## Security
|
|
186
344
|
|
|
@@ -200,4 +358,4 @@ AIDIRECTOR_SECRET_KEY=aid_sk_your_secret_key_here
|
|
|
200
358
|
|
|
201
359
|
## License
|
|
202
360
|
|
|
203
|
-
MIT ©
|
|
361
|
+
MIT © altsafe
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altsafe/aidirector",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Official TypeScript SDK for AI Director - Intelligent AI API Gateway with automatic failover, caching, and JSON extraction",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|