@to11ai/sdk 0.0.2
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 +235 -0
- package/dist/client.cjs +85 -0
- package/dist/client.d.ts +39 -0
- package/dist/client.js +52 -0
- package/dist/errors.cjs +105 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.js +64 -0
- package/dist/gateway.cjs +67 -0
- package/dist/gateway.d.ts +22 -0
- package/dist/gateway.js +27 -0
- package/dist/prompts/cache.d.ts +38 -0
- package/dist/prompts/resource.cjs +160 -0
- package/dist/prompts/resource.d.ts +21 -0
- package/dist/prompts/resource.js +120 -0
- package/dist/prompts/types.d.ts +142 -0
- package/dist/transport.d.ts +44 -0
- package/package.json +71 -0
- package/src/client.ts +120 -0
- package/src/errors.ts +91 -0
- package/src/gateway.ts +49 -0
- package/src/prompts/cache.ts +122 -0
- package/src/prompts/resource.ts +279 -0
- package/src/prompts/types.ts +202 -0
- package/src/transport.ts +223 -0
package/README.md
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# @to11ai/sdk
|
|
2
|
+
|
|
3
|
+
JavaScript/TypeScript SDK for [to11ai](https://to11ai.com) — the AI engineering platform for prompt management, tracing, and evals.
|
|
4
|
+
|
|
5
|
+
Ships ESM, CommonJS, and raw TypeScript source in the same package. Runtimes and bundlers pick the right variant automatically.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @to11ai/sdk
|
|
11
|
+
# or
|
|
12
|
+
bun add @to11ai/sdk
|
|
13
|
+
# or
|
|
14
|
+
pnpm add @to11ai/sdk
|
|
15
|
+
# or
|
|
16
|
+
yarn add @to11ai/sdk
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Runtime support
|
|
20
|
+
|
|
21
|
+
| Runtime / loader | Resolves to | Notes |
|
|
22
|
+
| --------------------------- | -------------------- | --------------------------------------------- |
|
|
23
|
+
| Node.js (ESM: `import`) | `dist/*.js` | Bundled ESM, Node-targeted |
|
|
24
|
+
| Node.js (CJS: `require`) | `dist/*.cjs` | Bundled CommonJS |
|
|
25
|
+
| TypeScript (type resolution)| `dist/*.d.ts` | Declarations for editor + tsc |
|
|
26
|
+
| Bun | `src/*.ts` | Raw source — zero build overhead, full maps |
|
|
27
|
+
| Deno | `src/*.ts` | Raw source |
|
|
28
|
+
| Browsers (via bundler) | `dist/*.js` | Tree-shakeable ESM |
|
|
29
|
+
| Workers / edge runtimes | `dist/*.js` | ESM, Node-compatible |
|
|
30
|
+
|
|
31
|
+
Resolution is driven by the package's `exports` conditional map. The tarball ships both `dist/` (compiled) and `src/` (TypeScript) so runtimes that support raw `.ts` can skip the build step.
|
|
32
|
+
|
|
33
|
+
## Getting started
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { createClient } from "@to11ai/sdk";
|
|
37
|
+
|
|
38
|
+
const client = createClient({
|
|
39
|
+
baseUrl: "https://api.to11ai.com",
|
|
40
|
+
apiKey: process.env.TO11AI_API_KEY!,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const result = await client.prompts.resolve({
|
|
44
|
+
projectId: "proj_abc123",
|
|
45
|
+
slug: "chat-system-prompt",
|
|
46
|
+
labels: ["production"],
|
|
47
|
+
variables: { userName: "Alice" },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
console.log(result.rendered); // { format: "text", text: "Hello, Alice!" }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Client construction
|
|
54
|
+
|
|
55
|
+
### Server-side (API key)
|
|
56
|
+
|
|
57
|
+
Use an API key for server-side Node.js, Bun, Deno, or edge runtimes where a secret is safe:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { createClient } from "@to11ai/sdk";
|
|
61
|
+
|
|
62
|
+
const client = createClient({
|
|
63
|
+
baseUrl: "https://api.to11ai.com",
|
|
64
|
+
apiKey: process.env.TO11AI_API_KEY!,
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Browser-side (access token callback)
|
|
69
|
+
|
|
70
|
+
Use a `getAccessToken` callback to avoid shipping a static secret to the browser:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { createClient } from "@to11ai/sdk";
|
|
74
|
+
|
|
75
|
+
const client = createClient({
|
|
76
|
+
baseUrl: "https://api.to11ai.com",
|
|
77
|
+
getAccessToken: async () => {
|
|
78
|
+
const res = await fetch("/api/auth/token");
|
|
79
|
+
const { token } = await res.json();
|
|
80
|
+
return token;
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Passing `apiKey` in a browser context throws immediately to prevent accidental secret exposure.
|
|
86
|
+
|
|
87
|
+
## Prompt resolution
|
|
88
|
+
|
|
89
|
+
`prompts.resolve()` is the primary use case — it fetches a prompt by slug, applies labels to select a version, renders Liquid template variables, and returns the final text or chat messages.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
const result = await client.prompts.resolve({
|
|
93
|
+
projectId: "proj_abc123",
|
|
94
|
+
slug: "summarise-article",
|
|
95
|
+
labels: ["production", "en"],
|
|
96
|
+
variables: {
|
|
97
|
+
article: "OpenAI releases GPT-5...",
|
|
98
|
+
maxLength: 200,
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
if (result.rendered.format === "text") {
|
|
103
|
+
console.log(result.rendered.text);
|
|
104
|
+
} else {
|
|
105
|
+
// chat format
|
|
106
|
+
for (const msg of result.rendered.messages) {
|
|
107
|
+
console.log(`[${msg.role}] ${msg.content}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## All prompt methods
|
|
113
|
+
|
|
114
|
+
| Method | Description |
|
|
115
|
+
|---|---|
|
|
116
|
+
| `prompts.resolve(input)` | Fetch a prompt by slug + labels, render variables, return rendered output. Cached by default. |
|
|
117
|
+
| `prompts.create(input)` | Create a new prompt (name, slug, description, tags). |
|
|
118
|
+
| `prompts.list(input)` | List prompts in a project with optional pagination. |
|
|
119
|
+
| `prompts.get(input)` | Get a single prompt by ID. |
|
|
120
|
+
| `prompts.update(input)` | Update prompt metadata (name, description, tags). |
|
|
121
|
+
| `prompts.archive(input)` | Archive a prompt (soft delete). |
|
|
122
|
+
| `prompts.createVersion(input)` | Create a new version for an existing prompt. |
|
|
123
|
+
| `prompts.listVersions(input)` | List all versions of a prompt. |
|
|
124
|
+
| `prompts.getVersion(input)` | Get a specific version by version number. |
|
|
125
|
+
| `prompts.moveLabel(input)` | Point a label (e.g. `production`) at a specific version. |
|
|
126
|
+
| `prompts.listLabels(input)` | List all label pointers for a prompt. |
|
|
127
|
+
|
|
128
|
+
All methods require at minimum `projectId` and the relevant resource identifier(s).
|
|
129
|
+
|
|
130
|
+
## Error handling
|
|
131
|
+
|
|
132
|
+
The SDK exports five typed error classes. Catch `To11aiError` as a base or catch specific subclasses:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import {
|
|
136
|
+
To11aiError,
|
|
137
|
+
To11aiApiError,
|
|
138
|
+
To11aiAuthError,
|
|
139
|
+
To11aiRateLimitError,
|
|
140
|
+
To11aiValidationError,
|
|
141
|
+
To11aiNetworkError,
|
|
142
|
+
} from "@to11ai/sdk";
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const result = await client.prompts.resolve({ ... });
|
|
146
|
+
} catch (err) {
|
|
147
|
+
if (err instanceof To11aiRateLimitError) {
|
|
148
|
+
const retryAfter = err.retryAfterSeconds ?? 60;
|
|
149
|
+
console.warn(`Rate limited — retry in ${retryAfter}s`);
|
|
150
|
+
} else if (err instanceof To11aiAuthError) {
|
|
151
|
+
console.error("Authentication failed:", err.statusCode, err.message);
|
|
152
|
+
} else if (err instanceof To11aiValidationError) {
|
|
153
|
+
console.error("Bad request:", err.message, err.details);
|
|
154
|
+
} else if (err instanceof To11aiNetworkError) {
|
|
155
|
+
console.error("Network failure:", err.message, err.cause);
|
|
156
|
+
} else if (err instanceof To11aiApiError) {
|
|
157
|
+
console.error(`API error ${err.statusCode} [${err.errorCode}]:`, err.message);
|
|
158
|
+
} else {
|
|
159
|
+
throw err;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
| Class | When thrown |
|
|
165
|
+
|---|---|
|
|
166
|
+
| `To11aiError` | Base class for all SDK errors |
|
|
167
|
+
| `To11aiApiError` | Non-2xx API response with structured error envelope |
|
|
168
|
+
| `To11aiValidationError` | 400 schema validation failure |
|
|
169
|
+
| `To11aiAuthError` | 401 or 403 authentication/authorisation failure |
|
|
170
|
+
| `To11aiRateLimitError` | 429 rate limit hit; includes `retryAfterSeconds` |
|
|
171
|
+
| `To11aiNetworkError` | Network failure or request timeout |
|
|
172
|
+
|
|
173
|
+
## Caching
|
|
174
|
+
|
|
175
|
+
`prompts.resolve()` uses an in-memory LRU cache by default. Configure it at client construction time:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
const client = createClient({
|
|
179
|
+
baseUrl: "https://api.to11ai.com",
|
|
180
|
+
apiKey: process.env.TO11AI_API_KEY!,
|
|
181
|
+
cache: {
|
|
182
|
+
ttlSeconds: 600, // default: 300 (5 minutes)
|
|
183
|
+
maxEntries: 500, // default: 100
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
**Per-call bypass:** pass `cache: false` to skip the cache for a single call:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
const fresh = await client.prompts.resolve({
|
|
192
|
+
projectId: "proj_abc123",
|
|
193
|
+
slug: "chat-system-prompt",
|
|
194
|
+
labels: ["production"],
|
|
195
|
+
cache: false, // bypass cache, always fetch from API
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**Fallback on network error:** pass `fallback` to return a pre-defined value if the network request fails (e.g. during a deployment):
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
const result = await client.prompts.resolve({
|
|
203
|
+
projectId: "proj_abc123",
|
|
204
|
+
slug: "chat-system-prompt",
|
|
205
|
+
labels: ["production"],
|
|
206
|
+
fallback: {
|
|
207
|
+
promptId: "...",
|
|
208
|
+
versionId: "...",
|
|
209
|
+
version: 1,
|
|
210
|
+
rendered: { format: "text", text: "You are a helpful assistant." },
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## TypeScript types
|
|
216
|
+
|
|
217
|
+
All inputs and outputs are fully typed. Import types directly if needed:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import type {
|
|
221
|
+
ClientOptions,
|
|
222
|
+
CacheConfig,
|
|
223
|
+
To11aiClient,
|
|
224
|
+
} from "@to11ai/sdk";
|
|
225
|
+
|
|
226
|
+
import type {
|
|
227
|
+
ResolvePromptInput,
|
|
228
|
+
ResolvePromptResult,
|
|
229
|
+
ResolvedPrompt,
|
|
230
|
+
Prompt,
|
|
231
|
+
PromptVersion,
|
|
232
|
+
} from "@to11ai/sdk/prompts";
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Type declarations ship in the package as `dist/*.d.ts`. No separate `@types` package is required. Bun and Deno consumers additionally get the raw TypeScript source.
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/client.ts
|
|
40
|
+
var exports_client = {};
|
|
41
|
+
__export(exports_client, {
|
|
42
|
+
gatewayPromptHeaders: () => import_gateway.gatewayPromptHeaders,
|
|
43
|
+
gatewayAuthHeaders: () => import_gateway.gatewayAuthHeaders,
|
|
44
|
+
createClient: () => createClient,
|
|
45
|
+
To11aiValidationError: () => import_errors.To11aiValidationError,
|
|
46
|
+
To11aiRateLimitError: () => import_errors.To11aiRateLimitError,
|
|
47
|
+
To11aiNetworkError: () => import_errors.To11aiNetworkError,
|
|
48
|
+
To11aiError: () => import_errors.To11aiError,
|
|
49
|
+
To11aiAuthError: () => import_errors.To11aiAuthError,
|
|
50
|
+
To11aiApiError: () => import_errors.To11aiApiError,
|
|
51
|
+
PromptResource: () => import_resource2.PromptResource
|
|
52
|
+
});
|
|
53
|
+
module.exports = __toCommonJS(exports_client);
|
|
54
|
+
var import_resource = require("./prompts/resource");
|
|
55
|
+
var import_transport = require("./transport");
|
|
56
|
+
var import_errors = require("./errors");
|
|
57
|
+
var import_gateway = require("./gateway");
|
|
58
|
+
var import_resource2 = require("./prompts/resource");
|
|
59
|
+
function createClient(options) {
|
|
60
|
+
const isDomBrowser = typeof globalThis === "object" && typeof globalThis.document !== "undefined";
|
|
61
|
+
const workerLikeGlobal = globalThis;
|
|
62
|
+
const isBunMainThread = workerLikeGlobal.Bun?.isMainThread === true;
|
|
63
|
+
const isWorkerBrowser = typeof globalThis === "object" && (typeof workerLikeGlobal.importScripts === "function" || !isBunMainThread && typeof workerLikeGlobal.postMessage === "function" && typeof workerLikeGlobal.onmessage !== "undefined");
|
|
64
|
+
if ((isDomBrowser || isWorkerBrowser) && options.apiKey !== undefined) {
|
|
65
|
+
throw new Error("[to11ai/sdk] Do not use `apiKey` in browser contexts — use `getAccessToken` instead.");
|
|
66
|
+
}
|
|
67
|
+
if (options.apiKey !== undefined && options.apiKey.trim() === "") {
|
|
68
|
+
throw new Error("[to11ai/sdk] `apiKey` must not be an empty string.");
|
|
69
|
+
}
|
|
70
|
+
const hasApiKey = options.apiKey !== undefined;
|
|
71
|
+
const hasGetAccessToken = options.getAccessToken !== undefined;
|
|
72
|
+
const getAccessToken = options.getAccessToken;
|
|
73
|
+
if (hasApiKey === hasGetAccessToken) {
|
|
74
|
+
throw new Error("[to11ai/sdk] Provide exactly one of `apiKey` (server) or `getAccessToken` (browser).");
|
|
75
|
+
}
|
|
76
|
+
const auth = options.apiKey !== undefined ? { apiKey: options.apiKey } : { getAccessToken };
|
|
77
|
+
const transport = new import_transport.Transport({
|
|
78
|
+
baseUrl: options.baseUrl,
|
|
79
|
+
auth,
|
|
80
|
+
timeoutMs: options.timeoutMs,
|
|
81
|
+
maxRetries: options.maxRetries
|
|
82
|
+
});
|
|
83
|
+
const prompts = new import_resource.PromptResource(transport, options.cache);
|
|
84
|
+
return { prompts };
|
|
85
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CacheConfig } from "./prompts/cache";
|
|
2
|
+
import { PromptResource } from "./prompts/resource";
|
|
3
|
+
export { To11aiApiError, To11aiAuthError, To11aiError, To11aiNetworkError, To11aiRateLimitError, To11aiValidationError, } from "./errors";
|
|
4
|
+
export type { GatewayAuthOptions } from "./gateway";
|
|
5
|
+
export { gatewayAuthHeaders, gatewayPromptHeaders } from "./gateway";
|
|
6
|
+
export type { CacheConfig } from "./prompts/cache";
|
|
7
|
+
export { PromptResource } from "./prompts/resource";
|
|
8
|
+
export type { Transport } from "./transport";
|
|
9
|
+
interface ServerAuthOptions {
|
|
10
|
+
/** API key for server-side use. Must not be used in browser contexts. */
|
|
11
|
+
apiKey: string;
|
|
12
|
+
getAccessToken?: never;
|
|
13
|
+
}
|
|
14
|
+
interface BrowserAuthOptions {
|
|
15
|
+
/** Token callback for browser / edge contexts — avoids static secrets. */
|
|
16
|
+
getAccessToken: () => Promise<string>;
|
|
17
|
+
apiKey?: never;
|
|
18
|
+
}
|
|
19
|
+
type AuthOptions = ServerAuthOptions | BrowserAuthOptions;
|
|
20
|
+
export type ClientOptions = AuthOptions & {
|
|
21
|
+
baseUrl: string;
|
|
22
|
+
/** LRU cache configuration for resolve(). Defaults: ttlSeconds=300, maxEntries=100 */
|
|
23
|
+
cache?: CacheConfig;
|
|
24
|
+
/** Request timeout in milliseconds. Default: 30_000 */
|
|
25
|
+
timeoutMs?: number;
|
|
26
|
+
/** Max retry attempts for network errors / 5xx. Default: 3 */
|
|
27
|
+
maxRetries?: number;
|
|
28
|
+
};
|
|
29
|
+
export interface To11aiClient {
|
|
30
|
+
readonly prompts: PromptResource;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a to11ai SDK client.
|
|
34
|
+
*
|
|
35
|
+
* Auth mode is inferred from options:
|
|
36
|
+
* - `apiKey` → server-side; rejected in browser contexts.
|
|
37
|
+
* - `getAccessToken` → browser-safe; token fetched per request.
|
|
38
|
+
*/
|
|
39
|
+
export declare function createClient(options: ClientOptions): To11aiClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
import { PromptResource } from "./prompts/resource";
|
|
3
|
+
import { Transport } from "./transport";
|
|
4
|
+
import {
|
|
5
|
+
To11aiApiError,
|
|
6
|
+
To11aiAuthError,
|
|
7
|
+
To11aiError,
|
|
8
|
+
To11aiNetworkError,
|
|
9
|
+
To11aiRateLimitError,
|
|
10
|
+
To11aiValidationError
|
|
11
|
+
} from "./errors";
|
|
12
|
+
import { gatewayAuthHeaders, gatewayPromptHeaders } from "./gateway";
|
|
13
|
+
import { PromptResource as PromptResource2 } from "./prompts/resource";
|
|
14
|
+
function createClient(options) {
|
|
15
|
+
const isDomBrowser = typeof globalThis === "object" && typeof globalThis.document !== "undefined";
|
|
16
|
+
const workerLikeGlobal = globalThis;
|
|
17
|
+
const isBunMainThread = workerLikeGlobal.Bun?.isMainThread === true;
|
|
18
|
+
const isWorkerBrowser = typeof globalThis === "object" && (typeof workerLikeGlobal.importScripts === "function" || !isBunMainThread && typeof workerLikeGlobal.postMessage === "function" && typeof workerLikeGlobal.onmessage !== "undefined");
|
|
19
|
+
if ((isDomBrowser || isWorkerBrowser) && options.apiKey !== undefined) {
|
|
20
|
+
throw new Error("[to11ai/sdk] Do not use `apiKey` in browser contexts — use `getAccessToken` instead.");
|
|
21
|
+
}
|
|
22
|
+
if (options.apiKey !== undefined && options.apiKey.trim() === "") {
|
|
23
|
+
throw new Error("[to11ai/sdk] `apiKey` must not be an empty string.");
|
|
24
|
+
}
|
|
25
|
+
const hasApiKey = options.apiKey !== undefined;
|
|
26
|
+
const hasGetAccessToken = options.getAccessToken !== undefined;
|
|
27
|
+
const getAccessToken = options.getAccessToken;
|
|
28
|
+
if (hasApiKey === hasGetAccessToken) {
|
|
29
|
+
throw new Error("[to11ai/sdk] Provide exactly one of `apiKey` (server) or `getAccessToken` (browser).");
|
|
30
|
+
}
|
|
31
|
+
const auth = options.apiKey !== undefined ? { apiKey: options.apiKey } : { getAccessToken };
|
|
32
|
+
const transport = new Transport({
|
|
33
|
+
baseUrl: options.baseUrl,
|
|
34
|
+
auth,
|
|
35
|
+
timeoutMs: options.timeoutMs,
|
|
36
|
+
maxRetries: options.maxRetries
|
|
37
|
+
});
|
|
38
|
+
const prompts = new PromptResource(transport, options.cache);
|
|
39
|
+
return { prompts };
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
gatewayPromptHeaders,
|
|
43
|
+
gatewayAuthHeaders,
|
|
44
|
+
createClient,
|
|
45
|
+
To11aiValidationError,
|
|
46
|
+
To11aiRateLimitError,
|
|
47
|
+
To11aiNetworkError,
|
|
48
|
+
To11aiError,
|
|
49
|
+
To11aiAuthError,
|
|
50
|
+
To11aiApiError,
|
|
51
|
+
PromptResource2 as PromptResource
|
|
52
|
+
};
|
package/dist/errors.cjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/errors.ts
|
|
40
|
+
var exports_errors = {};
|
|
41
|
+
__export(exports_errors, {
|
|
42
|
+
To11aiValidationError: () => To11aiValidationError,
|
|
43
|
+
To11aiRateLimitError: () => To11aiRateLimitError,
|
|
44
|
+
To11aiNetworkError: () => To11aiNetworkError,
|
|
45
|
+
To11aiError: () => To11aiError,
|
|
46
|
+
To11aiAuthError: () => To11aiAuthError,
|
|
47
|
+
To11aiApiError: () => To11aiApiError
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(exports_errors);
|
|
50
|
+
|
|
51
|
+
class To11aiError extends Error {
|
|
52
|
+
constructor(message, options) {
|
|
53
|
+
super(message, options);
|
|
54
|
+
this.name = "To11aiError";
|
|
55
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class To11aiApiError extends To11aiError {
|
|
60
|
+
statusCode;
|
|
61
|
+
errorCode;
|
|
62
|
+
details;
|
|
63
|
+
constructor(message, statusCode, errorCode, details) {
|
|
64
|
+
super(message);
|
|
65
|
+
this.name = "To11aiApiError";
|
|
66
|
+
this.statusCode = statusCode;
|
|
67
|
+
this.errorCode = errorCode;
|
|
68
|
+
this.details = details;
|
|
69
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class To11aiValidationError extends To11aiApiError {
|
|
74
|
+
constructor(message, errorCode, details) {
|
|
75
|
+
super(message, 400, errorCode, details);
|
|
76
|
+
this.name = "To11aiValidationError";
|
|
77
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class To11aiAuthError extends To11aiApiError {
|
|
82
|
+
constructor(message, statusCode, errorCode, details) {
|
|
83
|
+
super(message, statusCode, errorCode, details);
|
|
84
|
+
this.name = "To11aiAuthError";
|
|
85
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
class To11aiRateLimitError extends To11aiApiError {
|
|
90
|
+
retryAfterSeconds;
|
|
91
|
+
constructor(message, retryAfterSeconds, errorCode, details) {
|
|
92
|
+
super(message, 429, errorCode, details);
|
|
93
|
+
this.name = "To11aiRateLimitError";
|
|
94
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
95
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class To11aiNetworkError extends To11aiError {
|
|
100
|
+
constructor(message, cause) {
|
|
101
|
+
super(message, { cause });
|
|
102
|
+
this.name = "To11aiNetworkError";
|
|
103
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
104
|
+
}
|
|
105
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for all @to11ai/sdk errors.
|
|
3
|
+
*/
|
|
4
|
+
export declare class To11aiError extends Error {
|
|
5
|
+
constructor(message: string, options?: ErrorOptions);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Thrown when the API returns a non-2xx response with an error envelope.
|
|
9
|
+
*/
|
|
10
|
+
export declare class To11aiApiError extends To11aiError {
|
|
11
|
+
readonly statusCode: number;
|
|
12
|
+
readonly errorCode: string | undefined;
|
|
13
|
+
readonly details: unknown;
|
|
14
|
+
constructor(message: string, statusCode: number, errorCode?: string, details?: unknown);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Thrown on 400 / schema validation failures.
|
|
18
|
+
*/
|
|
19
|
+
export declare class To11aiValidationError extends To11aiApiError {
|
|
20
|
+
constructor(message: string, errorCode?: string, details?: unknown);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Thrown on 401 or 403 responses.
|
|
24
|
+
*/
|
|
25
|
+
export declare class To11aiAuthError extends To11aiApiError {
|
|
26
|
+
constructor(message: string, statusCode: 401 | 403, errorCode?: string, details?: unknown);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Thrown on 429 rate limit responses.
|
|
30
|
+
*/
|
|
31
|
+
export declare class To11aiRateLimitError extends To11aiApiError {
|
|
32
|
+
readonly retryAfterSeconds: number | undefined;
|
|
33
|
+
constructor(message: string, retryAfterSeconds?: number, errorCode?: string, details?: unknown);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Thrown on network failure or request timeout.
|
|
37
|
+
*/
|
|
38
|
+
export declare class To11aiNetworkError extends To11aiError {
|
|
39
|
+
constructor(message: string, cause?: unknown);
|
|
40
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
class To11aiError extends Error {
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
|
+
this.name = "To11aiError";
|
|
6
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
class To11aiApiError extends To11aiError {
|
|
11
|
+
statusCode;
|
|
12
|
+
errorCode;
|
|
13
|
+
details;
|
|
14
|
+
constructor(message, statusCode, errorCode, details) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "To11aiApiError";
|
|
17
|
+
this.statusCode = statusCode;
|
|
18
|
+
this.errorCode = errorCode;
|
|
19
|
+
this.details = details;
|
|
20
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class To11aiValidationError extends To11aiApiError {
|
|
25
|
+
constructor(message, errorCode, details) {
|
|
26
|
+
super(message, 400, errorCode, details);
|
|
27
|
+
this.name = "To11aiValidationError";
|
|
28
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class To11aiAuthError extends To11aiApiError {
|
|
33
|
+
constructor(message, statusCode, errorCode, details) {
|
|
34
|
+
super(message, statusCode, errorCode, details);
|
|
35
|
+
this.name = "To11aiAuthError";
|
|
36
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class To11aiRateLimitError extends To11aiApiError {
|
|
41
|
+
retryAfterSeconds;
|
|
42
|
+
constructor(message, retryAfterSeconds, errorCode, details) {
|
|
43
|
+
super(message, 429, errorCode, details);
|
|
44
|
+
this.name = "To11aiRateLimitError";
|
|
45
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
46
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class To11aiNetworkError extends To11aiError {
|
|
51
|
+
constructor(message, cause) {
|
|
52
|
+
super(message, { cause });
|
|
53
|
+
this.name = "To11aiNetworkError";
|
|
54
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
To11aiValidationError,
|
|
59
|
+
To11aiRateLimitError,
|
|
60
|
+
To11aiNetworkError,
|
|
61
|
+
To11aiError,
|
|
62
|
+
To11aiAuthError,
|
|
63
|
+
To11aiApiError
|
|
64
|
+
};
|
package/dist/gateway.cjs
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/gateway.ts
|
|
40
|
+
var exports_gateway = {};
|
|
41
|
+
__export(exports_gateway, {
|
|
42
|
+
gatewayPromptHeaders: () => gatewayPromptHeaders,
|
|
43
|
+
gatewayAuthHeaders: () => gatewayAuthHeaders
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(exports_gateway);
|
|
46
|
+
function gatewayAuthHeaders(options) {
|
|
47
|
+
if (options.apiKey.trim().length === 0) {
|
|
48
|
+
throw new Error("[to11ai/sdk] `apiKey` must not be empty.");
|
|
49
|
+
}
|
|
50
|
+
if (options.projectId.trim().length === 0) {
|
|
51
|
+
throw new Error("[to11ai/sdk] `projectId` must not be empty.");
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
"x-to11-authorization": `Bearer ${options.apiKey}`,
|
|
55
|
+
"x-to11-project-id": options.projectId
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function gatewayPromptHeaders(prompt) {
|
|
59
|
+
const headers = {
|
|
60
|
+
"x-to11-prompt-id": prompt.promptId,
|
|
61
|
+
"x-to11-prompt-version": String(prompt.version)
|
|
62
|
+
};
|
|
63
|
+
if (prompt.labels.length > 0) {
|
|
64
|
+
headers["x-to11-prompt-labels"] = prompt.labels.join(",");
|
|
65
|
+
}
|
|
66
|
+
return headers;
|
|
67
|
+
}
|