@to11ai/sdk 3.1.0-rc → 4.0.0-rc.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 +30 -42
- package/dist/behaviors/store.d.ts +1 -1
- package/dist/client.cjs +17 -71
- package/dist/client.d.ts +16 -112
- package/dist/client.js +22 -72
- package/dist/config.d.ts +3 -6
- package/dist/errors.cjs +0 -72
- package/dist/errors.d.ts +0 -77
- package/dist/errors.js +0 -72
- package/dist/generated/index.d.ts +1 -1
- package/dist/generated/types.gen.d.ts +2962 -17155
- package/dist/generated/zod.gen.d.ts +2576 -11477
- package/dist/projectEnvironments/resource.cjs +0 -2
- package/dist/projectEnvironments/resource.d.ts +3 -12
- package/dist/projectEnvironments/resource.js +0 -2
- package/dist/projectEnvironments/types.d.ts +0 -2
- package/dist/{gateway.d.ts → prompts/convert.d.ts} +12 -75
- package/dist/prompts/resource.d.ts +7 -7
- package/dist/prompts/shape.d.ts +4 -5
- package/dist/prompts/types.d.ts +5 -17
- package/package.json +7 -24
- package/src/behaviors/store.ts +1 -1
- package/src/client.ts +36 -242
- package/src/config.ts +3 -9
- package/src/errors.ts +0 -132
- package/src/generated/index.ts +1 -1
- package/src/generated/types.gen.ts +2889 -17652
- package/src/generated/zod.gen.ts +1980 -9967
- package/src/projectEnvironments/resource.ts +3 -14
- package/src/projectEnvironments/types.ts +0 -2
- package/src/{gateway.ts → prompts/convert.ts} +18 -152
- package/src/prompts/resource.ts +10 -12
- package/src/prompts/shape.ts +4 -5
- package/src/prompts/types.ts +5 -17
- package/src/transport.ts +0 -133
- package/dist/context.d.ts +0 -124
- package/dist/gateway.cjs +0 -264
- package/dist/gateway.js +0 -226
- package/dist/routingRules/resource.d.ts +0 -64
- package/dist/routingRules/types.d.ts +0 -88
- package/dist/traceparent.d.ts +0 -61
- package/dist/usage/resource.cjs +0 -113
- package/dist/usage/resource.d.ts +0 -29
- package/dist/usage/resource.js +0 -78
- package/dist/usage/types.d.ts +0 -81
- package/src/context.ts +0 -196
- package/src/routingRules/resource.ts +0 -261
- package/src/routingRules/types.ts +0 -120
- package/src/traceparent.ts +0 -189
- package/src/usage/resource.ts +0 -183
- package/src/usage/types.ts +0 -121
package/src/client.ts
CHANGED
|
@@ -1,24 +1,10 @@
|
|
|
1
1
|
import { BehaviorStore } from "./behaviors/store";
|
|
2
|
-
import { DEFAULT_API_URL
|
|
3
|
-
import {
|
|
4
|
-
type Conversation,
|
|
5
|
-
Session,
|
|
6
|
-
type Turn,
|
|
7
|
-
type TurnAuthContext,
|
|
8
|
-
} from "./context";
|
|
9
|
-
import { gatewayAuthHeaders } from "./gateway";
|
|
2
|
+
import { DEFAULT_API_URL } from "./config";
|
|
10
3
|
import { ProjectEnvironmentResource } from "./projectEnvironments/resource";
|
|
11
4
|
import { PromptResource } from "./prompts/resource";
|
|
12
|
-
import {
|
|
13
|
-
ENV_SLUG_REGEX,
|
|
14
|
-
type RenderFormat,
|
|
15
|
-
type UnknownRoleMode,
|
|
16
|
-
} from "./prompts/types";
|
|
5
|
+
import type { RenderFormat, UnknownRoleMode } from "./prompts/types";
|
|
17
6
|
import { Transport } from "./transport";
|
|
18
|
-
import { UsageResource } from "./usage/resource";
|
|
19
7
|
|
|
20
|
-
export type { TurnAuthContext } from "./context";
|
|
21
|
-
export { Conversation, Session, Turn } from "./context";
|
|
22
8
|
// Re-export all error types for convenience
|
|
23
9
|
export type {
|
|
24
10
|
PromptHasNoVersionsReason,
|
|
@@ -39,8 +25,6 @@ export {
|
|
|
39
25
|
To11aiValidationError,
|
|
40
26
|
UnknownRoleError,
|
|
41
27
|
} from "./errors";
|
|
42
|
-
export type { ConvertMessagesOptions, GatewayAuthOptions } from "./gateway";
|
|
43
|
-
export { gatewayAuthHeaders, gatewayPromptHeaders } from "./gateway";
|
|
44
28
|
export type {
|
|
45
29
|
CursorPaginatedResult,
|
|
46
30
|
CursorPaginationInfo,
|
|
@@ -63,6 +47,26 @@ export type {
|
|
|
63
47
|
UpdateProjectEnvironmentResult,
|
|
64
48
|
} from "./projectEnvironments/resource";
|
|
65
49
|
export { ProjectEnvironmentResource } from "./projectEnvironments/resource";
|
|
50
|
+
export type {
|
|
51
|
+
AnthropicContentBlock,
|
|
52
|
+
AnthropicMessage,
|
|
53
|
+
AnthropicTool,
|
|
54
|
+
AnthropicToolChoice,
|
|
55
|
+
ConvertMessagesOptions,
|
|
56
|
+
OpenAIChatMessage,
|
|
57
|
+
OpenAITool,
|
|
58
|
+
OpenAIToolCall,
|
|
59
|
+
OpenAIToolChoice,
|
|
60
|
+
} from "./prompts/convert";
|
|
61
|
+
export {
|
|
62
|
+
toAnthropicMessages,
|
|
63
|
+
toAnthropicSystem,
|
|
64
|
+
toAnthropicToolChoice,
|
|
65
|
+
toAnthropicTools,
|
|
66
|
+
toOpenAIMessages,
|
|
67
|
+
toOpenAIToolChoice,
|
|
68
|
+
toOpenAITools,
|
|
69
|
+
} from "./prompts/convert";
|
|
66
70
|
export type { PromptResourceConfig } from "./prompts/resource";
|
|
67
71
|
export { PromptResource } from "./prompts/resource";
|
|
68
72
|
export type {
|
|
@@ -82,7 +86,6 @@ export type {
|
|
|
82
86
|
UnknownRoleMode,
|
|
83
87
|
} from "./prompts/types";
|
|
84
88
|
export { ENV_SLUG_REGEX } from "./prompts/types";
|
|
85
|
-
export { UsageResource } from "./usage/resource";
|
|
86
89
|
|
|
87
90
|
// ─── Client options ────────────────────────────────────────────────────────
|
|
88
91
|
|
|
@@ -92,41 +95,17 @@ export interface ClientOptions {
|
|
|
92
95
|
* `https://api.to11.ai`.
|
|
93
96
|
*/
|
|
94
97
|
baseUrl?: string;
|
|
95
|
-
/**
|
|
96
|
-
* Data-plane gateway URL (the app's provider-client `baseURL`). Precedence:
|
|
97
|
-
* this option → `TO11_GATEWAY_URL` env → `https://gw.to11.ai`.
|
|
98
|
-
*/
|
|
99
|
-
gatewayUrl?: string;
|
|
100
98
|
/**
|
|
101
99
|
* API key for server-side use (server, CLI, backend job). Precedence: this
|
|
102
100
|
* option → `TO11_API_KEY` env. Required. Rejected in browser contexts — a
|
|
103
|
-
* to11 API key
|
|
101
|
+
* to11 API key must never ship in client code.
|
|
104
102
|
*/
|
|
105
103
|
apiKey?: string;
|
|
106
|
-
/**
|
|
107
|
-
* Provider API key (BYOK) carried by `openaiOptions()` / `anthropicOptions()`
|
|
108
|
-
* as the provider client's `apiKey`, so a BYOK call is a single
|
|
109
|
-
* `new OpenAI(to11.openaiOptions())`. When omitted, those helpers fall back to
|
|
110
|
-
* the to11 `apiKey` as a placeholder (the managed / server-side-credential
|
|
111
|
-
* path). Option only — no env fallback. Whether the gateway actually uses this
|
|
112
|
-
* key depends on the provider's passthrough mode; otherwise the project's
|
|
113
|
-
* stored credential wins.
|
|
114
|
-
*/
|
|
115
|
-
providerApiKey?: string;
|
|
116
104
|
/**
|
|
117
105
|
* Project the client is bound to. Precedence: this option → `TO11_PROJECT_ID`
|
|
118
|
-
* env. Required for `prompts.render()
|
|
106
|
+
* env. Required for `prompts.render()`.
|
|
119
107
|
*/
|
|
120
108
|
projectId?: string;
|
|
121
|
-
/**
|
|
122
|
-
* Gateway environment, sent as `x-to11-env` by `turn().headers()` and the
|
|
123
|
-
* `openaiOptions()` / `anthropicOptions()` tenant headers so the gateway
|
|
124
|
-
* routes the call. Precedence: this option → `TO11_ENV` env. Validated
|
|
125
|
-
* eagerly against the project-environments slug rule (`^[a-z0-9-]{1,63}$`).
|
|
126
|
-
* Prompts are not environment-scoped: `prompts.render()` serves `latest`
|
|
127
|
-
* regardless of `env`.
|
|
128
|
-
*/
|
|
129
|
-
env?: string;
|
|
130
109
|
/**
|
|
131
110
|
* Default provider shape for `prompts.render()` output (`"openai"` |
|
|
132
111
|
* `"anthropic"`). Omit for the neutral result. Per-call override allowed.
|
|
@@ -145,121 +124,44 @@ export interface ClientOptions {
|
|
|
145
124
|
maxRetries?: number;
|
|
146
125
|
}
|
|
147
126
|
|
|
148
|
-
// ─── Provider-client options (openaiOptions / anthropicOptions) ─────────────
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Overrides accepted by `openaiOptions()` / `anthropicOptions()`. The three
|
|
152
|
-
* known fields are merged (`apiKey` / `baseURL` override; `defaultHeaders`
|
|
153
|
-
* shallow-merges over the tenant-auth headers, caller wins); any other keys —
|
|
154
|
-
* `timeout`, `maxRetries`, `fetch`, … — pass through untouched to the provider
|
|
155
|
-
* client, so `new OpenAI(to11.openaiOptions({ apiKey, timeout }))` needs no
|
|
156
|
-
* outer spread.
|
|
157
|
-
*/
|
|
158
|
-
export interface ProviderClientOverrides {
|
|
159
|
-
apiKey?: string;
|
|
160
|
-
baseURL?: string;
|
|
161
|
-
defaultHeaders?: Record<string, string>;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* The spread-ready result of `openaiOptions()` / `anthropicOptions()`: the three
|
|
166
|
-
* managed fields plus whatever passthrough keys the caller supplied (their types
|
|
167
|
-
* preserved), so it drops straight into `new OpenAI(...)` / `new Anthropic(...)`.
|
|
168
|
-
*/
|
|
169
|
-
export type ProviderClientOptions<
|
|
170
|
-
T extends ProviderClientOverrides = ProviderClientOverrides,
|
|
171
|
-
> = Omit<T, "apiKey" | "baseURL" | "defaultHeaders"> & {
|
|
172
|
-
baseURL: string;
|
|
173
|
-
apiKey: string;
|
|
174
|
-
defaultHeaders: Record<string, string>;
|
|
175
|
-
};
|
|
176
|
-
|
|
177
127
|
// ─── Client interface ──────────────────────────────────────────────────────
|
|
178
128
|
|
|
179
129
|
export interface To11aiClient<F extends RenderFormat | undefined = undefined> {
|
|
180
130
|
readonly prompts: PromptResource<F>;
|
|
181
131
|
readonly projectEnvironments: ProjectEnvironmentResource;
|
|
182
|
-
/** Org usage reads: members, sessions, costs and top traces (`orgId` per call). */
|
|
183
|
-
readonly usage: UsageResource;
|
|
184
132
|
/** Resolved API key (option → `TO11_API_KEY`). */
|
|
185
133
|
readonly apiKey: string;
|
|
186
134
|
/** Resolved control-plane base URL (option → `TO11_API_URL` → default). */
|
|
187
135
|
readonly baseUrl: string;
|
|
188
|
-
/** Resolved data-plane gateway URL (option → `TO11_GATEWAY_URL` → default). */
|
|
189
|
-
readonly gatewayUrl: string;
|
|
190
136
|
/**
|
|
191
137
|
* Resolved project id (option → `TO11_PROJECT_ID`); `undefined` when none is
|
|
192
|
-
* configured. Read-only, for your own logging
|
|
193
|
-
*
|
|
138
|
+
* configured. Read-only, for your own logging — `prompts.*` already default
|
|
139
|
+
* to it, so you don't need to pass it back into calls.
|
|
194
140
|
*/
|
|
195
141
|
readonly projectId: string | undefined;
|
|
196
|
-
/**
|
|
197
|
-
* Resolved gateway environment (option → `TO11_ENV`); `undefined` when none
|
|
198
|
-
* is configured. The gateway helpers already send it as `x-to11-env`.
|
|
199
|
-
*/
|
|
200
|
-
readonly env: string | undefined;
|
|
201
|
-
/** A new session (unit of work). Pass an id to group telemetry by your own id. */
|
|
202
|
-
session(id?: string): Session;
|
|
203
|
-
/**
|
|
204
|
-
* A conversation in the client's default session (for side / parallel agents).
|
|
205
|
-
*
|
|
206
|
-
* @example Two agents under one session
|
|
207
|
-
* const research = to11.conversation(); // a separate thread from to11.turn()'s default
|
|
208
|
-
* const res = await openai.chat.completions.create(
|
|
209
|
-
* { ...researchPrompt.config, messages },
|
|
210
|
-
* { headers: research.turn().headers(researchPrompt) },
|
|
211
|
-
* );
|
|
212
|
-
*/
|
|
213
|
-
conversation(id?: string): Conversation;
|
|
214
|
-
/** A turn in the client's default session + default conversation. */
|
|
215
|
-
turn(): Turn;
|
|
216
|
-
/**
|
|
217
|
-
* Spread-ready options for `new OpenAI(...)` targeting the gateway:
|
|
218
|
-
* `{ baseURL, apiKey, defaultHeaders }` (baseURL carries `/v1`). `defaultHeaders`
|
|
219
|
-
* holds the static tenant auth, so the client authenticates on its own; add
|
|
220
|
-
* `turn().headers(prompt)` per call for trace + prompt provenance. Pass
|
|
221
|
-
* `overrides` to change `apiKey`/`baseURL`, merge extra `defaultHeaders`, or
|
|
222
|
-
* forward any other provider-SDK option (`timeout`, …).
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* const openai = new OpenAI(to11.openaiOptions());
|
|
226
|
-
* @example BYOK + a provider-SDK option, no outer spread
|
|
227
|
-
* const openai = new OpenAI(to11.openaiOptions({ apiKey: MY_KEY, timeout: 60_000 }));
|
|
228
|
-
*/
|
|
229
|
-
openaiOptions<T extends ProviderClientOverrides = ProviderClientOverrides>(
|
|
230
|
-
overrides?: T,
|
|
231
|
-
): ProviderClientOptions<T>;
|
|
232
|
-
/**
|
|
233
|
-
* Spread-ready options for `new Anthropic(...)` targeting the gateway root
|
|
234
|
-
* (the Anthropic SDK appends `/v1/messages` itself). Same shape and merge
|
|
235
|
-
* rules as {@link To11aiClient.openaiOptions}.
|
|
236
|
-
*/
|
|
237
|
-
anthropicOptions<T extends ProviderClientOverrides = ProviderClientOverrides>(
|
|
238
|
-
overrides?: T,
|
|
239
|
-
): ProviderClientOptions<T>;
|
|
240
142
|
}
|
|
241
143
|
|
|
242
144
|
// ─── Factory ───────────────────────────────────────────────────────────────
|
|
243
145
|
|
|
244
146
|
/**
|
|
245
147
|
* Create a to11ai SDK client. Resolution precedence differs per option:
|
|
246
|
-
* -
|
|
148
|
+
* - `baseUrl` — option → `TO11_API_URL` → built-in default.
|
|
247
149
|
* - `apiKey` / `projectId` — option → env var (no default).
|
|
248
|
-
* - `
|
|
150
|
+
* - `format` / `onUnknownRole` — option only.
|
|
249
151
|
* Authenticate with `apiKey` (or `TO11_API_KEY`) — required, and server-side
|
|
250
152
|
* only (server, CLI, backend job); constructing with `apiKey` in a browser throws.
|
|
251
153
|
*
|
|
252
|
-
* @example
|
|
154
|
+
* @example Render a prompt and send it with your own provider client
|
|
253
155
|
* import { createClient } from "@to11ai/sdk";
|
|
254
156
|
* import OpenAI from "openai";
|
|
255
157
|
*
|
|
256
|
-
* const to11 = createClient({
|
|
257
|
-
* const openai = new OpenAI(
|
|
158
|
+
* const to11 = createClient({ format: "openai" });
|
|
159
|
+
* const openai = new OpenAI();
|
|
258
160
|
* const prompt = await to11.prompts.render("weather-concierge", { variables: { city: "NYC" } });
|
|
259
|
-
* const res = await openai.chat.completions.create(
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* );
|
|
161
|
+
* const res = await openai.chat.completions.create({
|
|
162
|
+
* ...prompt.config,
|
|
163
|
+
* messages: prompt.messages,
|
|
164
|
+
* });
|
|
263
165
|
*/
|
|
264
166
|
export function createClient<
|
|
265
167
|
const F extends RenderFormat | undefined = undefined,
|
|
@@ -308,32 +210,14 @@ export function createClient<
|
|
|
308
210
|
};
|
|
309
211
|
|
|
310
212
|
const baseUrl = pick(options.baseUrl, "TO11_API_URL") ?? DEFAULT_API_URL;
|
|
311
|
-
// The gateway root (host, no version path). Normalize away a trailing slash
|
|
312
|
-
// and a trailing `/v1` so both the root form (`https://gw.to11.ai`) and the
|
|
313
|
-
// older `/v1` form resolve identically — the provider helpers add the path
|
|
314
|
-
// each SDK expects.
|
|
315
|
-
const gatewayUrl = (
|
|
316
|
-
pick(options.gatewayUrl, "TO11_GATEWAY_URL") ?? DEFAULT_GATEWAY_URL
|
|
317
|
-
)
|
|
318
|
-
.replace(/\/+$/, "")
|
|
319
|
-
.replace(/\/v1$/, "");
|
|
320
213
|
const apiKey = pick(options.apiKey, "TO11_API_KEY");
|
|
321
214
|
const projectId = pick(options.projectId, "TO11_PROJECT_ID");
|
|
322
|
-
// Gateway env: explicit option → `TO11_ENV` env var — the same
|
|
323
|
-
// `pick(option, "TO11_*")` resolution `apiKey`/`projectId` use (empty option
|
|
324
|
-
// or empty env var trims to absent). An explicit empty option is rejected by
|
|
325
|
-
// the dedicated guard below (mirroring `apiKey`/`projectId`); a non-empty but
|
|
326
|
-
// malformed value fails the `ENV_SLUG_REGEX` check.
|
|
327
|
-
const env = pick(options.env, "TO11_ENV");
|
|
328
|
-
// Provider (BYOK) key — option only, no env fallback (which provider's env
|
|
329
|
-
// var is ambiguous). Empty trims to absent so it falls back to the placeholder.
|
|
330
|
-
const providerApiKey = options.providerApiKey?.trim() || undefined;
|
|
331
215
|
|
|
332
216
|
// A browser must never carry a static API key. `processEnv` is already
|
|
333
217
|
// undefined in the browser, so only the explicit option can reach here.
|
|
334
218
|
if (isBrowser && options.apiKey !== undefined) {
|
|
335
219
|
throw new Error(
|
|
336
|
-
"[to11ai/sdk] Do not use `apiKey` in a browser — a to11 API key
|
|
220
|
+
"[to11ai/sdk] Do not use `apiKey` in a browser — a to11 API key must stay server-side (server, CLI, or backend job).",
|
|
337
221
|
);
|
|
338
222
|
}
|
|
339
223
|
|
|
@@ -346,19 +230,6 @@ export function createClient<
|
|
|
346
230
|
"[to11ai/sdk] `projectId` must not be an empty string — omit it or pass a valid project id.",
|
|
347
231
|
);
|
|
348
232
|
}
|
|
349
|
-
if (options.env !== undefined && options.env.trim() === "") {
|
|
350
|
-
throw new Error(
|
|
351
|
-
"[to11ai/sdk] `env` must not be an empty string — omit it or pass a valid env slug.",
|
|
352
|
-
);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// Validate the init-time `env` against the project-environments slug rule so
|
|
356
|
-
// a malformed env fails locally rather than being rejected at the gateway.
|
|
357
|
-
if (env !== undefined && !ENV_SLUG_REGEX.test(env)) {
|
|
358
|
-
throw new Error(
|
|
359
|
-
`[to11ai/sdk] \`env\` must match ${ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(env)}`,
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
233
|
|
|
363
234
|
// `apiKey` is the only auth source and is required.
|
|
364
235
|
if (apiKey === undefined) {
|
|
@@ -388,89 +259,12 @@ export function createClient<
|
|
|
388
259
|
onUnknownRole: options.onUnknownRole,
|
|
389
260
|
});
|
|
390
261
|
const projectEnvironments = new ProjectEnvironmentResource(transport);
|
|
391
|
-
const usage = new UsageResource(transport);
|
|
392
|
-
|
|
393
|
-
// ── Gateway observability context (session → conversation → turn) ──
|
|
394
|
-
// These build gateway auth headers, so they require a projectId; fail loudly
|
|
395
|
-
// rather than emit a broken header bag.
|
|
396
|
-
const gatewayAuthContext = (): TurnAuthContext => {
|
|
397
|
-
if (projectId === undefined) {
|
|
398
|
-
throw new Error(
|
|
399
|
-
"[to11ai/sdk] session()/turn() need a `projectId` (or `TO11_PROJECT_ID`).",
|
|
400
|
-
);
|
|
401
|
-
}
|
|
402
|
-
return env !== undefined
|
|
403
|
-
? { apiKey, projectId, env }
|
|
404
|
-
: { apiKey, projectId };
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
let defaultSession: Session | undefined;
|
|
408
|
-
const clientDefaultSession = (): Session => {
|
|
409
|
-
if (defaultSession === undefined) {
|
|
410
|
-
defaultSession = new Session(gatewayAuthContext());
|
|
411
|
-
}
|
|
412
|
-
return defaultSession;
|
|
413
|
-
};
|
|
414
|
-
|
|
415
|
-
// OpenAI's SDK convention puts `/v1` in `baseURL`; the Anthropic SDK appends
|
|
416
|
-
// `/v1/messages` itself, so it takes the gateway root. Both point at the same
|
|
417
|
-
// gateway; only the base differs per provider convention. The result is a
|
|
418
|
-
// spread-ready provider-client options object: managed fields (baseURL/apiKey/
|
|
419
|
-
// defaultHeaders) are computed+merged, and any other override key passes
|
|
420
|
-
// through untouched to the provider SDK.
|
|
421
|
-
const gatewayClientOptions = <
|
|
422
|
-
T extends ProviderClientOverrides = ProviderClientOverrides,
|
|
423
|
-
>(
|
|
424
|
-
defaultBaseURL: string,
|
|
425
|
-
overrides?: T,
|
|
426
|
-
): ProviderClientOptions<T> => {
|
|
427
|
-
if (projectId === undefined) {
|
|
428
|
-
throw new Error(
|
|
429
|
-
"[to11ai/sdk] openaiOptions()/anthropicOptions() need a `projectId` (or `TO11_PROJECT_ID`) — it authenticates the gateway call via `x-to11-project-id`.",
|
|
430
|
-
);
|
|
431
|
-
}
|
|
432
|
-
// Static tenant auth for the provider client's defaultHeaders; per-call
|
|
433
|
-
// trace + prompt provenance still come from `turn().headers(prompt)`.
|
|
434
|
-
const authHeaders = gatewayAuthHeaders(
|
|
435
|
-
env !== undefined ? { apiKey, projectId, env } : { apiKey, projectId },
|
|
436
|
-
);
|
|
437
|
-
const {
|
|
438
|
-
apiKey: overrideApiKey,
|
|
439
|
-
baseURL: overrideBaseURL,
|
|
440
|
-
defaultHeaders: overrideHeaders,
|
|
441
|
-
...passthrough
|
|
442
|
-
} = overrides ?? ({} as T);
|
|
443
|
-
return {
|
|
444
|
-
...passthrough,
|
|
445
|
-
baseURL: overrideBaseURL ?? defaultBaseURL,
|
|
446
|
-
// Provider (BYOK) key when supplied, else the to11 key as a placeholder
|
|
447
|
-
// (the managed / server-side-credential path).
|
|
448
|
-
apiKey: overrideApiKey ?? providerApiKey ?? apiKey,
|
|
449
|
-
defaultHeaders: { ...authHeaders, ...overrideHeaders },
|
|
450
|
-
} as ProviderClientOptions<T>;
|
|
451
|
-
};
|
|
452
262
|
|
|
453
263
|
return {
|
|
454
264
|
prompts,
|
|
455
265
|
projectEnvironments,
|
|
456
|
-
usage,
|
|
457
266
|
apiKey,
|
|
458
267
|
baseUrl,
|
|
459
|
-
gatewayUrl,
|
|
460
268
|
projectId,
|
|
461
|
-
env,
|
|
462
|
-
session: (id) => new Session(gatewayAuthContext(), id),
|
|
463
|
-
conversation: (id) => clientDefaultSession().conversation(id),
|
|
464
|
-
turn: () => clientDefaultSession().turn(),
|
|
465
|
-
openaiOptions: <
|
|
466
|
-
T extends ProviderClientOverrides = ProviderClientOverrides,
|
|
467
|
-
>(
|
|
468
|
-
overrides?: T,
|
|
469
|
-
) => gatewayClientOptions(`${gatewayUrl}/v1`, overrides),
|
|
470
|
-
anthropicOptions: <
|
|
471
|
-
T extends ProviderClientOverrides = ProviderClientOverrides,
|
|
472
|
-
>(
|
|
473
|
-
overrides?: T,
|
|
474
|
-
) => gatewayClientOptions(gatewayUrl, overrides),
|
|
475
269
|
};
|
|
476
270
|
}
|
package/src/config.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Internal default
|
|
3
|
-
* root: `createClient` resolves the
|
|
4
|
-
*
|
|
5
|
-
* bare URL constant. Keeping these out of the public surface avoids a JS-only
|
|
6
|
-
* exported symbol that would need a Python-parity record.
|
|
2
|
+
* Internal default endpoint. Deliberately NOT re-exported from the package
|
|
3
|
+
* root: `createClient` resolves the URL (option → env → default), so app code
|
|
4
|
+
* never imports a bare URL constant.
|
|
7
5
|
*/
|
|
8
6
|
|
|
9
7
|
export const DEFAULT_API_URL = "https://api.to11.ai";
|
|
10
|
-
// The gateway *root* — no `/v1` version path. Provider helpers add the path each
|
|
11
|
-
// provider SDK expects (`openaiOptions()` appends `/v1`; the Anthropic SDK adds
|
|
12
|
-
// `/v1/messages` itself), so users configure only the host.
|
|
13
|
-
export const DEFAULT_GATEWAY_URL = "https://gw.to11.ai";
|
package/src/errors.ts
CHANGED
|
@@ -143,103 +143,6 @@ export class UnknownRoleError extends To11aiError {
|
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
/**
|
|
147
|
-
* Thrown when a rule cannot be located. Covers two variants of the same
|
|
148
|
-
* named class (single class so existing `catch (err: RuleNotFoundError)`
|
|
149
|
-
* blocks match both):
|
|
150
|
-
*
|
|
151
|
-
* * Control-plane variant (404 `RULE_NOT_FOUND`) — `ruleId` is the id
|
|
152
|
-
* the caller supplied and that did not exist in the project.
|
|
153
|
-
* * Gateway variant (404 `RULE_NOT_FOUND_IN_ENV`) — `model` is the
|
|
154
|
-
* `function::<alias>` (or raw model id) the gateway tried to resolve
|
|
155
|
-
* and `env` is the request env. `ruleId` is undefined because the
|
|
156
|
-
* alias may not resolve to any rule in the env.
|
|
157
|
-
*/
|
|
158
|
-
export class RuleNotFoundError extends To11aiApiError {
|
|
159
|
-
readonly ruleId: string | undefined;
|
|
160
|
-
readonly model: string | undefined;
|
|
161
|
-
readonly env: string | undefined;
|
|
162
|
-
|
|
163
|
-
constructor(
|
|
164
|
-
message: string,
|
|
165
|
-
errorCode: string,
|
|
166
|
-
fields: { ruleId?: string; model?: string; env?: string },
|
|
167
|
-
details?: unknown,
|
|
168
|
-
) {
|
|
169
|
-
super(message, 404, errorCode, details);
|
|
170
|
-
this.name = "RuleNotFoundError";
|
|
171
|
-
this.ruleId = fields.ruleId;
|
|
172
|
-
this.model = fields.model;
|
|
173
|
-
this.env = fields.env;
|
|
174
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
static is(err: unknown): err is RuleNotFoundError {
|
|
178
|
-
return err instanceof RuleNotFoundError;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export type NoApplicableRouteReason =
|
|
183
|
-
| "all_conditions_false"
|
|
184
|
-
| "all_variants_unreachable"
|
|
185
|
-
| "all_steps_non_routable";
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Thrown when the gateway resolved a rule + version but no variant /
|
|
189
|
-
* step / chain element was applicable to the request — every condition
|
|
190
|
-
* was false, every variant unreachable, or every fallback step
|
|
191
|
-
* non-routable. Surfaces as 422 `NO_APPLICABLE_ROUTE`.
|
|
192
|
-
*/
|
|
193
|
-
export class NoApplicableRouteError extends To11aiApiError {
|
|
194
|
-
readonly ruleId: string;
|
|
195
|
-
readonly versionId: string;
|
|
196
|
-
readonly reason: NoApplicableRouteReason | string;
|
|
197
|
-
|
|
198
|
-
constructor(
|
|
199
|
-
message: string,
|
|
200
|
-
fields: { ruleId: string; versionId: string; reason: string },
|
|
201
|
-
details?: unknown,
|
|
202
|
-
) {
|
|
203
|
-
super(message, 422, "NO_APPLICABLE_ROUTE", details);
|
|
204
|
-
this.name = "NoApplicableRouteError";
|
|
205
|
-
this.ruleId = fields.ruleId;
|
|
206
|
-
this.versionId = fields.versionId;
|
|
207
|
-
this.reason = fields.reason;
|
|
208
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
static is(err: unknown): err is NoApplicableRouteError {
|
|
212
|
-
return err instanceof NoApplicableRouteError;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Thrown when a rule references a provider that has no activation in the
|
|
218
|
-
* requested env. Surfaces as 422 `ENV_NOT_ENABLED_FOR_PROVIDER`.
|
|
219
|
-
*/
|
|
220
|
-
export class EnvNotEnabledForProviderError extends To11aiApiError {
|
|
221
|
-
readonly ruleId: string;
|
|
222
|
-
readonly envSlug: string;
|
|
223
|
-
readonly providerId: string;
|
|
224
|
-
|
|
225
|
-
constructor(
|
|
226
|
-
message: string,
|
|
227
|
-
fields: { ruleId: string; envSlug: string; providerId: string },
|
|
228
|
-
details?: unknown,
|
|
229
|
-
) {
|
|
230
|
-
super(message, 422, "ENV_NOT_ENABLED_FOR_PROVIDER", details);
|
|
231
|
-
this.name = "EnvNotEnabledForProviderError";
|
|
232
|
-
this.ruleId = fields.ruleId;
|
|
233
|
-
this.envSlug = fields.envSlug;
|
|
234
|
-
this.providerId = fields.providerId;
|
|
235
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
static is(err: unknown): err is EnvNotEnabledForProviderError {
|
|
239
|
-
return err instanceof EnvNotEnabledForProviderError;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
146
|
// ─── Prompt resolution typed errors (TO11-980 / S9) ────────────────────────
|
|
244
147
|
//
|
|
245
148
|
// These four subclasses give `prompts.render()` callers a discriminable error
|
|
@@ -395,38 +298,3 @@ export class PromptPolicyViolationError extends To11aiApiError {
|
|
|
395
298
|
return err instanceof PromptPolicyViolationError;
|
|
396
299
|
}
|
|
397
300
|
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Thrown when version create rejects the snapshot for a structural
|
|
401
|
-
* reason — invalid variant params, malformed `when` clause, illegal
|
|
402
|
-
* weight set, malformed retry policy, … Surfaces with one of the
|
|
403
|
-
* `INVALID_VARIANT_PARAMS` / `INVALID_VARIANT_WEIGHTS` /
|
|
404
|
-
* `INVALID_RULE_STRATEGY` / `INVALID_STEP_COUNT` /
|
|
405
|
-
* `RULE_ENDPOINT_MISMATCH` / `INVALID_WHEN_CLAUSE` /
|
|
406
|
-
* `INVALID_STRATEGY_VARIANTS` / `INVALID_RETRY_POLICY` error codes
|
|
407
|
-
* at HTTP status 400 or 422.
|
|
408
|
-
*/
|
|
409
|
-
export class RuleVersionInvalidSnapshotError extends To11aiApiError {
|
|
410
|
-
readonly field: string | undefined;
|
|
411
|
-
readonly reason: string | undefined;
|
|
412
|
-
readonly variantName: string | undefined;
|
|
413
|
-
|
|
414
|
-
constructor(
|
|
415
|
-
message: string,
|
|
416
|
-
errorCode: string,
|
|
417
|
-
fields: { field?: string; reason?: string; variantName?: string },
|
|
418
|
-
details?: unknown,
|
|
419
|
-
statusCode: number = 400,
|
|
420
|
-
) {
|
|
421
|
-
super(message, statusCode, errorCode, details);
|
|
422
|
-
this.name = "RuleVersionInvalidSnapshotError";
|
|
423
|
-
this.field = fields.field;
|
|
424
|
-
this.reason = fields.reason;
|
|
425
|
-
this.variantName = fields.variantName;
|
|
426
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
static is(err: unknown): err is RuleVersionInvalidSnapshotError {
|
|
430
|
-
return err instanceof RuleVersionInvalidSnapshotError;
|
|
431
|
-
}
|
|
432
|
-
}
|