@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
|
@@ -156,10 +156,6 @@ export class ProjectEnvironmentResource {
|
|
|
156
156
|
* `slug` must match lowercase-kebab (`a-z`, `0-9`, `-`, 1..63 chars).
|
|
157
157
|
* Reserved system slugs (`prod`, `staging`) and any already-used slug
|
|
158
158
|
* return 409 `ENVIRONMENT_SLUG_TAKEN`.
|
|
159
|
-
*
|
|
160
|
-
* Setting `isGatewayFallback: true` additionally requires the
|
|
161
|
-
* `project:manage_default_environment` capability; the create use
|
|
162
|
-
* case clears the flag on any other env in the same project.
|
|
163
159
|
*/
|
|
164
160
|
async create(
|
|
165
161
|
input: CreateProjectEnvironmentInput,
|
|
@@ -182,11 +178,7 @@ export class ProjectEnvironmentResource {
|
|
|
182
178
|
* - System envs (`isSystem: true`) reject `slug`/`isSystem`/immutable-
|
|
183
179
|
* field mutations with 409 `SYSTEM_ENV_IMMUTABLE`.
|
|
184
180
|
* - Renaming via `newSlug` triggers a 7-day alias row server-side so
|
|
185
|
-
*
|
|
186
|
-
* - Mutating `isGatewayFallback` requires the
|
|
187
|
-
* `project:manage_default_environment` capability AND the target
|
|
188
|
-
* env must have at least one live snapshot (else 409
|
|
189
|
-
* `FALLBACK_ENV_NOT_READY`).
|
|
181
|
+
* the old slug keeps resolving.
|
|
190
182
|
*
|
|
191
183
|
* The SDK marshals `newSlug` onto the wire as `slug` (the API accepts
|
|
192
184
|
* `slug` on the PATCH body for a rename — the URL slug stays the
|
|
@@ -202,8 +194,6 @@ export class ProjectEnvironmentResource {
|
|
|
202
194
|
if (rest.color !== undefined) body.color = rest.color;
|
|
203
195
|
if (rest.description !== undefined) body.description = rest.description;
|
|
204
196
|
if (rest.isDefault !== undefined) body.isDefault = rest.isDefault;
|
|
205
|
-
if (rest.isGatewayFallback !== undefined)
|
|
206
|
-
body.isGatewayFallback = rest.isGatewayFallback;
|
|
207
197
|
if (rest.sortOrder !== undefined) body.sortOrder = rest.sortOrder;
|
|
208
198
|
|
|
209
199
|
const envelope =
|
|
@@ -221,9 +211,8 @@ export class ProjectEnvironmentResource {
|
|
|
221
211
|
* Soft-archive an environment. The response carries the archived row
|
|
222
212
|
* under `environment`.
|
|
223
213
|
*
|
|
224
|
-
* System envs (`isSystem: true`)
|
|
225
|
-
*
|
|
226
|
-
* `CANNOT_ARCHIVE_GATEWAY_FALLBACK` respectively.
|
|
214
|
+
* System envs (`isSystem: true`) cannot be archived; the API returns
|
|
215
|
+
* 409 `SYSTEM_ENV_IMMUTABLE`.
|
|
227
216
|
*
|
|
228
217
|
* Uses `POST /:slug/archive` rather than `DELETE` so the server can
|
|
229
218
|
* return a structured 200 with the archived row.
|
|
@@ -47,7 +47,6 @@ export interface CreateProjectEnvironmentInput {
|
|
|
47
47
|
color?: string | null;
|
|
48
48
|
description?: string | null;
|
|
49
49
|
isDefault?: boolean;
|
|
50
|
-
isGatewayFallback?: boolean;
|
|
51
50
|
sortOrder?: number;
|
|
52
51
|
}
|
|
53
52
|
|
|
@@ -61,7 +60,6 @@ export interface UpdateProjectEnvironmentInput {
|
|
|
61
60
|
color?: string | null;
|
|
62
61
|
description?: string | null;
|
|
63
62
|
isDefault?: boolean;
|
|
64
|
-
isGatewayFallback?: boolean;
|
|
65
63
|
sortOrder?: number;
|
|
66
64
|
}
|
|
67
65
|
|
|
@@ -1,146 +1,16 @@
|
|
|
1
|
-
import { UnknownRoleError } from "./errors";
|
|
2
|
-
import {
|
|
3
|
-
ENV_SLUG_REGEX,
|
|
4
|
-
type RenderedChatMessage,
|
|
5
|
-
type ResolvedPrompt,
|
|
6
|
-
type ResolvedToolDefinition,
|
|
7
|
-
type UnknownRoleMode,
|
|
8
|
-
} from "./prompts/types";
|
|
9
|
-
|
|
10
|
-
export interface GatewayAuthOptions {
|
|
11
|
-
apiKey: string;
|
|
12
|
-
projectId: string;
|
|
13
|
-
/**
|
|
14
|
-
* Optional environment slug routed via the gateway's `x-to11-env`
|
|
15
|
-
* header. When omitted, the gateway falls back to the project's
|
|
16
|
-
* `is_gateway_fallback=true` environment; if no fallback exists, the
|
|
17
|
-
* request fails with `400 missing_env_no_project_default`.
|
|
18
|
-
*
|
|
19
|
-
* Slug validation matches the project-environments registry:
|
|
20
|
-
* lowercase-kebab (`^[a-z0-9-]{1,63}$`). The SDK rejects empty /
|
|
21
|
-
* whitespace-only values and any value that violates this slug rule
|
|
22
|
-
* eagerly so misconfiguration surfaces before the request hits the wire.
|
|
23
|
-
*/
|
|
24
|
-
env?: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Build the auth headers required for every request through the to11 gateway.
|
|
29
|
-
*
|
|
30
|
-
* Includes `x-to11-env: <slug>` when `options.env` is set — see the
|
|
31
|
-
* environments-registry spec for gateway routing semantics. Empty /
|
|
32
|
-
* whitespace-only and registry-invalid env values (anything outside
|
|
33
|
-
* `^[a-z0-9-]{1,63}$`) throw immediately so caller misconfiguration is loud.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* const headers = gatewayAuthHeaders({ apiKey, projectId });
|
|
37
|
-
* @example
|
|
38
|
-
* const headers = gatewayAuthHeaders({ apiKey, projectId, env: "staging" });
|
|
39
|
-
*/
|
|
40
|
-
export function gatewayAuthHeaders(
|
|
41
|
-
options: GatewayAuthOptions,
|
|
42
|
-
): Record<string, string> {
|
|
43
|
-
if (options.apiKey.trim().length === 0) {
|
|
44
|
-
throw new Error("[to11ai/sdk] `apiKey` must not be empty.");
|
|
45
|
-
}
|
|
46
|
-
if (options.projectId.trim().length === 0) {
|
|
47
|
-
throw new Error("[to11ai/sdk] `projectId` must not be empty.");
|
|
48
|
-
}
|
|
49
|
-
const headers: Record<string, string> = {
|
|
50
|
-
"x-to11-authorization": `Bearer ${options.apiKey}`,
|
|
51
|
-
"x-to11-project-id": options.projectId,
|
|
52
|
-
};
|
|
53
|
-
if (options.env !== undefined) {
|
|
54
|
-
if (options.env.trim().length === 0) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
"[to11ai/sdk] `env` must not be empty — omit the field to use the project's gateway-fallback environment.",
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
// Enforce the project-environments registry slug rule (lowercase-kebab,
|
|
60
|
-
// 1-63 chars) eagerly — same `ENV_SLUG_REGEX` already applied on the
|
|
61
|
-
// createClient / render() paths — so a misconfigured env (e.g. "Prod",
|
|
62
|
-
// "bad env", >63 chars) fails locally with a clear SDK error instead of
|
|
63
|
-
// being copied verbatim into x-to11-env and rejected later at the
|
|
64
|
-
// gateway/API boundary (TO11-1424).
|
|
65
|
-
if (!ENV_SLUG_REGEX.test(options.env)) {
|
|
66
|
-
throw new Error(
|
|
67
|
-
`[to11ai/sdk] \`env\` must match ${ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(options.env)}`,
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
headers["x-to11-env"] = options.env;
|
|
71
|
-
}
|
|
72
|
-
return headers;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
1
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* No provenance header is required, and the gateway rejects empty header
|
|
80
|
-
* values — so each header is emitted only when its value is present and valid,
|
|
81
|
-
* and the function never throws:
|
|
82
|
-
* - `x-to11-prompt-id` — a non-empty string.
|
|
83
|
-
* - `x-to11-prompt-version` — a finite number (a non-number/NaN is dropped
|
|
84
|
-
* rather than stringified to junk like `"[object Object]"`).
|
|
85
|
-
* - `x-to11-prompt-label` — the single label the prompt resolved through.
|
|
86
|
-
* - `x-to11-prompt-slug` — the prompt's human-readable slug.
|
|
87
|
-
*
|
|
88
|
-
* An input with no usable provenance yields an empty header bag — the same as
|
|
89
|
-
* a prompt-less `turn.headers()` call.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* const result = await client.prompts.render(slug);
|
|
93
|
-
* const headers = gatewayPromptHeaders(result);
|
|
2
|
+
* Provider-shape converters: reshape the provider-neutral resolved data
|
|
3
|
+
* (`tools`, rendered `messages`) into the shapes the OpenAI / Anthropic clients
|
|
4
|
+
* expect. Pure and side-effect free; they never send a request.
|
|
94
5
|
*/
|
|
95
|
-
export interface GatewayPromptProvenance {
|
|
96
|
-
promptId: string;
|
|
97
|
-
version: number;
|
|
98
|
-
/** The label the prompt resolved through (`latest` unless pinned). */
|
|
99
|
-
label: string;
|
|
100
|
-
/**
|
|
101
|
-
* The prompt's human-readable slug (the `render()`/`resolve()` input). Present
|
|
102
|
-
* on an enriched `render()` result; a legacy `ResolvedPrompt` omits it.
|
|
103
|
-
*/
|
|
104
|
-
slug?: string;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function gatewayPromptHeaders(
|
|
108
|
-
prompt: GatewayPromptProvenance | { metadata: GatewayPromptProvenance },
|
|
109
|
-
): Record<string, string> {
|
|
110
|
-
// A format-shaped result carries provenance under `metadata`; unwrap it so
|
|
111
|
-
// the whole shaped result works, not just the bare provenance (mirrors the
|
|
112
|
-
// Python SDK).
|
|
113
|
-
const p = prompt != null && "metadata" in prompt ? prompt.metadata : prompt;
|
|
114
|
-
// No prompt provenance header is required, and the gateway rejects empty
|
|
115
|
-
// header values — so emit each one only when its value is present and valid,
|
|
116
|
-
// and never throw. An all-empty input just yields an empty header bag (the
|
|
117
|
-
// same as a prompt-less `turn.headers()` call).
|
|
118
|
-
const headers: Record<string, string> = {};
|
|
119
|
-
if (p == null) return headers;
|
|
120
|
-
if (typeof p.promptId === "string" && p.promptId.length > 0) {
|
|
121
|
-
headers["x-to11-prompt-id"] = p.promptId;
|
|
122
|
-
}
|
|
123
|
-
// A non-finite/non-number version would stringify to junk (e.g. NaN or
|
|
124
|
-
// "[object Object]"); only emit a real finite number.
|
|
125
|
-
if (typeof p.version === "number" && Number.isFinite(p.version)) {
|
|
126
|
-
headers["x-to11-prompt-version"] = String(p.version);
|
|
127
|
-
}
|
|
128
|
-
if (typeof p.label === "string" && p.label.length > 0) {
|
|
129
|
-
headers["x-to11-prompt-label"] = p.label;
|
|
130
|
-
}
|
|
131
|
-
if (typeof p.slug === "string" && p.slug.length > 0) {
|
|
132
|
-
headers["x-to11-prompt-slug"] = p.slug;
|
|
133
|
-
}
|
|
134
|
-
return headers;
|
|
135
|
-
}
|
|
136
6
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
7
|
+
import { UnknownRoleError } from "../errors";
|
|
8
|
+
import type {
|
|
9
|
+
RenderedChatMessage,
|
|
10
|
+
ResolvedPrompt,
|
|
11
|
+
ResolvedToolDefinition,
|
|
12
|
+
UnknownRoleMode,
|
|
13
|
+
} from "./types";
|
|
144
14
|
|
|
145
15
|
/** OpenAI Chat Completions `tools[]` entry (`function` tool). */
|
|
146
16
|
export interface OpenAITool {
|
|
@@ -173,7 +43,7 @@ export interface OpenAIToolCall {
|
|
|
173
43
|
* `ChatCompletionMessageParam[]` (a `render({ format: "openai" })` result spreads
|
|
174
44
|
* straight into `openai.chat.completions.create({ messages })` with no cast).
|
|
175
45
|
* The SDK carries no dependency on the `openai` package — this only mirrors its
|
|
176
|
-
* shape. (Assignability is regression-guarded in `
|
|
46
|
+
* shape. (Assignability is regression-guarded in `convert.provider-types.test.ts`.)
|
|
177
47
|
*/
|
|
178
48
|
export type OpenAIChatMessage =
|
|
179
49
|
| { role: "system"; content: string }
|
|
@@ -447,9 +317,8 @@ export function toOpenAIMessages(
|
|
|
447
317
|
* Shape rendered turns for the Anthropic Messages `messages` field.
|
|
448
318
|
*
|
|
449
319
|
* - `system` / `developer` turns are OMITTED — Anthropic carries instruction
|
|
450
|
-
* content in the top-level `system` parameter, not the `messages` array
|
|
451
|
-
*
|
|
452
|
-
* yourself.
|
|
320
|
+
* content in the top-level `system` parameter, not the `messages` array.
|
|
321
|
+
* Pass them to `system` yourself (see {@link toAnthropicSystem}).
|
|
453
322
|
* - `user` → `{ role: "user", content }`.
|
|
454
323
|
* - `assistant` with `toolCalls` → `{ role: "assistant", content: [text?,
|
|
455
324
|
* ...tool_use] }`; without calls → `{ role: "assistant", content }`.
|
|
@@ -524,14 +393,11 @@ export function toAnthropicMessages(
|
|
|
524
393
|
* when there is no system/developer content.
|
|
525
394
|
*
|
|
526
395
|
* @example
|
|
527
|
-
* const res = await anthropic.messages.create(
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
* },
|
|
533
|
-
* { headers: to11.turn().headers(prompt) },
|
|
534
|
-
* );
|
|
396
|
+
* const res = await anthropic.messages.create({
|
|
397
|
+
* ...prompt.config,
|
|
398
|
+
* system: toAnthropicSystem(prompt.messages),
|
|
399
|
+
* messages: toAnthropicMessages(prompt.messages),
|
|
400
|
+
* });
|
|
535
401
|
*/
|
|
536
402
|
export function toAnthropicSystem(
|
|
537
403
|
messages: ReadonlyArray<RenderedChatMessage>,
|
package/src/prompts/resource.ts
CHANGED
|
@@ -180,8 +180,7 @@ function normalizeResolvedEnvelope(
|
|
|
180
180
|
contentHash: raw.contentHash,
|
|
181
181
|
label: raw.label,
|
|
182
182
|
// The requested slug — the API resolve envelope does not echo it, so it
|
|
183
|
-
// is threaded from the `render()` input (TO11-2727).
|
|
184
|
-
// `x-to11-prompt-slug` for trace readability.
|
|
183
|
+
// is threaded from the `render()` input (TO11-2727).
|
|
185
184
|
slug,
|
|
186
185
|
modelConfig: provenance.modelConfig,
|
|
187
186
|
};
|
|
@@ -205,7 +204,7 @@ function normalizeResolvedEnvelope(
|
|
|
205
204
|
* Lift a caller-supplied offline `fallback` into the returned shape (D7), so
|
|
206
205
|
* `render()` returns an identical shape on the online and offline paths. The
|
|
207
206
|
* fallback's own slug is preferred, falling back to the `render()` input when it
|
|
208
|
-
* carries none
|
|
207
|
+
* carries none.
|
|
209
208
|
*/
|
|
210
209
|
function liftFallbackToRendered(
|
|
211
210
|
fallback: RenderedPrompt,
|
|
@@ -507,8 +506,7 @@ export class PromptResource<F extends RenderFormat | undefined = undefined> {
|
|
|
507
506
|
/**
|
|
508
507
|
* Skills across every project of `orgId` the caller may read prompts in,
|
|
509
508
|
* each with the version its `latest` label serves at listing time. At most
|
|
510
|
-
* `limit` rows (default 20, max 50): a picker, not a list to page.
|
|
511
|
-
* the read left out are listed in `excludedProjects`.
|
|
509
|
+
* `limit` rows (default 20, max 50): a picker, not a list to page.
|
|
512
510
|
*/
|
|
513
511
|
async listOrgPrompts(
|
|
514
512
|
input: ListOrgPromptsInput,
|
|
@@ -556,14 +554,15 @@ export class PromptResource<F extends RenderFormat | undefined = undefined> {
|
|
|
556
554
|
* `{ messages, config, metadata }`; without one it is the neutral
|
|
557
555
|
* `RenderedPrompt`. Serves `latest` unless the call pins a `label`.
|
|
558
556
|
*
|
|
559
|
-
* @example Render an OpenAI-shaped prompt and send it
|
|
557
|
+
* @example Render an OpenAI-shaped prompt and send it with your own client
|
|
560
558
|
* const prompt = await to11.prompts.render("weather-concierge", {
|
|
559
|
+
* format: "openai",
|
|
561
560
|
* variables: { city: "NYC" },
|
|
562
561
|
* });
|
|
563
|
-
* const res = await openai.chat.completions.create(
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
* );
|
|
562
|
+
* const res = await openai.chat.completions.create({
|
|
563
|
+
* ...prompt.config,
|
|
564
|
+
* messages: prompt.messages,
|
|
565
|
+
* });
|
|
567
566
|
*/
|
|
568
567
|
async render(
|
|
569
568
|
slug: string,
|
|
@@ -603,8 +602,7 @@ export class PromptResource<F extends RenderFormat | undefined = undefined> {
|
|
|
603
602
|
);
|
|
604
603
|
}
|
|
605
604
|
|
|
606
|
-
// Per-call label, else `latest
|
|
607
|
-
// never stands in for a label.
|
|
605
|
+
// Per-call label, else `latest`.
|
|
608
606
|
const label = options.label ?? LATEST_PROMPT_LABEL;
|
|
609
607
|
|
|
610
608
|
// Validate the label so a malformed value (e.g. "Prod") never reaches
|
package/src/prompts/shape.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Format-at-load shaping: turn a neutral `RenderedPrompt` into a provider-shaped
|
|
3
3
|
* `{ messages, config, metadata }` result the caller spreads into their own
|
|
4
|
-
* provider `create()` call.
|
|
5
|
-
* sends a request. See docs/archive-2026-08/specs/2026-07-14-to11-2691-sdk-simpler-setup.md.
|
|
4
|
+
* provider `create()` call. This only reshapes data; it never sends a request.
|
|
6
5
|
*/
|
|
7
6
|
|
|
8
7
|
import {
|
|
@@ -20,7 +19,7 @@ import {
|
|
|
20
19
|
toOpenAIMessages,
|
|
21
20
|
toOpenAIToolChoice,
|
|
22
21
|
toOpenAITools,
|
|
23
|
-
} from "
|
|
22
|
+
} from "./convert";
|
|
24
23
|
import type { RenderedPrompt, RenderFormat } from "./types";
|
|
25
24
|
|
|
26
25
|
/** Model params passed through verbatim from the resolved version's modelConfig. */
|
|
@@ -34,14 +33,14 @@ export interface PromptConfig {
|
|
|
34
33
|
[key: string]: unknown;
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
/** Prompt provenance
|
|
36
|
+
/** Prompt provenance (id, version, label, slug), kept apart from the request body. */
|
|
38
37
|
export interface PromptMetadata {
|
|
39
38
|
promptId: string;
|
|
40
39
|
versionId: string;
|
|
41
40
|
version: number;
|
|
42
41
|
/** The label the prompt resolved through (`latest` unless pinned). */
|
|
43
42
|
label: string;
|
|
44
|
-
/** The prompt's human-readable slug
|
|
43
|
+
/** The prompt's human-readable slug (the `render()` input). */
|
|
45
44
|
slug: string;
|
|
46
45
|
}
|
|
47
46
|
|
package/src/prompts/types.ts
CHANGED
|
@@ -206,11 +206,8 @@ export type IterateLabelsInput = Omit<ListPromptLabelsInput, "cursor">;
|
|
|
206
206
|
export type ListPromptLabelsResult = CursorPaginatedResult<PromptLabelPointer>;
|
|
207
207
|
|
|
208
208
|
/**
|
|
209
|
-
* Slug rule
|
|
210
|
-
*
|
|
211
|
-
* and case-insensitive routing keys invite silent "production" vs "Production"
|
|
212
|
-
* mismatches, so the rule is strict (no uppercase, no underscores) — do not
|
|
213
|
-
* loosen.
|
|
209
|
+
* Slug rule for a `render()` label (the project-environments slug rule):
|
|
210
|
+
* lowercase-kebab, 1-63 chars, no uppercase, no underscores.
|
|
214
211
|
*/
|
|
215
212
|
export const ENV_SLUG_REGEX = /^[a-z0-9-]{1,63}$/;
|
|
216
213
|
|
|
@@ -223,17 +220,9 @@ export interface RenderedPrompt {
|
|
|
223
220
|
promptId: string;
|
|
224
221
|
versionId: string;
|
|
225
222
|
contentHash: string;
|
|
226
|
-
/**
|
|
227
|
-
* The label the prompt resolved through (`latest` unless the call pinned
|
|
228
|
-
* one). Surfaced as `x-to11-prompt-label` by `gatewayPromptHeaders` /
|
|
229
|
-
* `turn.headers()` (TO11-2725).
|
|
230
|
-
*/
|
|
223
|
+
/** The label the prompt resolved through (`latest` unless the call pinned one). */
|
|
231
224
|
label: string;
|
|
232
|
-
/**
|
|
233
|
-
* The prompt's human-readable slug (the `render()` input). Surfaced as
|
|
234
|
-
* `x-to11-prompt-slug` by `gatewayPromptHeaders` / `turn.headers()` so traces
|
|
235
|
-
* carry a readable name alongside the opaque `promptId` (TO11-2727).
|
|
236
|
-
*/
|
|
225
|
+
/** The prompt's human-readable slug (the `render()` input). */
|
|
237
226
|
slug: string;
|
|
238
227
|
/**
|
|
239
228
|
* The served version's model config (model, temperature, max_tokens, …),
|
|
@@ -264,8 +253,7 @@ export type ResolvedPromptEnvelope = GenResolvedPrompt;
|
|
|
264
253
|
export interface RenderOptions {
|
|
265
254
|
/**
|
|
266
255
|
* Label to resolve this call's prompt version. `latest` (the newest version)
|
|
267
|
-
* is the only label a prompt carries, and the default when omitted.
|
|
268
|
-
* client's `env` is gateway routing and never stands in for a label.
|
|
256
|
+
* is the only label a prompt carries, and the default when omitted.
|
|
269
257
|
*/
|
|
270
258
|
label?: string;
|
|
271
259
|
variables?: Record<string, unknown>;
|
package/src/transport.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { BehaviorStore } from "./behaviors/store";
|
|
2
2
|
import {
|
|
3
|
-
EnvNotEnabledForProviderError,
|
|
4
3
|
LabelNotFoundError,
|
|
5
|
-
NoApplicableRouteError,
|
|
6
4
|
PromptHasNoVersionsError,
|
|
7
5
|
PromptNotFoundError,
|
|
8
6
|
PromptPolicyViolationError,
|
|
9
|
-
RuleNotFoundError,
|
|
10
|
-
RuleVersionInvalidSnapshotError,
|
|
11
7
|
To11aiApiError,
|
|
12
8
|
To11aiAuthError,
|
|
13
9
|
To11aiNetworkError,
|
|
@@ -15,23 +11,6 @@ import {
|
|
|
15
11
|
To11aiResponseValidationError,
|
|
16
12
|
To11aiValidationError,
|
|
17
13
|
} from "./errors";
|
|
18
|
-
import { activeTraceContext } from "./traceparent";
|
|
19
|
-
|
|
20
|
-
const INVALID_SNAPSHOT_ERROR_CODES = new Set([
|
|
21
|
-
"INVALID_VARIANT_PARAMS",
|
|
22
|
-
"INVALID_VARIANT_WEIGHTS",
|
|
23
|
-
"INVALID_RULE_STRATEGY",
|
|
24
|
-
"INVALID_STEP_COUNT",
|
|
25
|
-
"RULE_ENDPOINT_MISMATCH",
|
|
26
|
-
"INVALID_WHEN_CLAUSE",
|
|
27
|
-
"INVALID_STRATEGY_VARIANTS",
|
|
28
|
-
"INVALID_RETRY_POLICY",
|
|
29
|
-
]);
|
|
30
|
-
|
|
31
|
-
const RULE_NOT_FOUND_DETAILS_ERRORS = new Set([
|
|
32
|
-
"rule_not_found",
|
|
33
|
-
"rule_not_found_in_env",
|
|
34
|
-
]);
|
|
35
14
|
|
|
36
15
|
/**
|
|
37
16
|
* TO11-980 (S9) prompt-resolution canonical error codes.
|
|
@@ -245,115 +224,9 @@ function mapHttpError(
|
|
|
245
224
|
details,
|
|
246
225
|
);
|
|
247
226
|
}
|
|
248
|
-
if (
|
|
249
|
-
(status === 400 || status === 422) &&
|
|
250
|
-
errorCode &&
|
|
251
|
-
INVALID_SNAPSHOT_ERROR_CODES.has(errorCode)
|
|
252
|
-
) {
|
|
253
|
-
const d = (details && typeof details === "object" ? details : {}) as Record<
|
|
254
|
-
string,
|
|
255
|
-
unknown
|
|
256
|
-
>;
|
|
257
|
-
return new RuleVersionInvalidSnapshotError(
|
|
258
|
-
message,
|
|
259
|
-
errorCode,
|
|
260
|
-
{
|
|
261
|
-
field: typeof d.field === "string" ? d.field : undefined,
|
|
262
|
-
reason: typeof d.reason === "string" ? d.reason : undefined,
|
|
263
|
-
variantName:
|
|
264
|
-
typeof d.variantName === "string" ? d.variantName : undefined,
|
|
265
|
-
},
|
|
266
|
-
details,
|
|
267
|
-
status,
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
227
|
if (status === 400) {
|
|
271
228
|
return new To11aiValidationError(message, errorCode, details);
|
|
272
229
|
}
|
|
273
|
-
if (status === 404) {
|
|
274
|
-
const d = (details && typeof details === "object" ? details : {}) as Record<
|
|
275
|
-
string,
|
|
276
|
-
unknown
|
|
277
|
-
>;
|
|
278
|
-
const detailsError = typeof d.error === "string" ? d.error : undefined;
|
|
279
|
-
const isRuleNotFound =
|
|
280
|
-
errorCode === "RULE_NOT_FOUND" ||
|
|
281
|
-
errorCode === "RULE_NOT_FOUND_IN_ENV" ||
|
|
282
|
-
(detailsError !== undefined &&
|
|
283
|
-
RULE_NOT_FOUND_DETAILS_ERRORS.has(detailsError));
|
|
284
|
-
if (isRuleNotFound) {
|
|
285
|
-
const resolvedCode =
|
|
286
|
-
errorCode === "RULE_NOT_FOUND" || errorCode === "RULE_NOT_FOUND_IN_ENV"
|
|
287
|
-
? errorCode
|
|
288
|
-
: detailsError === "rule_not_found_in_env"
|
|
289
|
-
? "RULE_NOT_FOUND_IN_ENV"
|
|
290
|
-
: "RULE_NOT_FOUND";
|
|
291
|
-
return new RuleNotFoundError(
|
|
292
|
-
message,
|
|
293
|
-
resolvedCode,
|
|
294
|
-
{
|
|
295
|
-
ruleId: typeof d.ruleId === "string" ? d.ruleId : undefined,
|
|
296
|
-
model: typeof d.model === "string" ? d.model : undefined,
|
|
297
|
-
env: typeof d.env === "string" ? d.env : undefined,
|
|
298
|
-
},
|
|
299
|
-
details,
|
|
300
|
-
);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
if (status === 422 && errorCode === "NO_APPLICABLE_ROUTE") {
|
|
304
|
-
const d = (details && typeof details === "object" ? details : {}) as Record<
|
|
305
|
-
string,
|
|
306
|
-
unknown
|
|
307
|
-
>;
|
|
308
|
-
const ruleId =
|
|
309
|
-
typeof d.rule === "string"
|
|
310
|
-
? d.rule
|
|
311
|
-
: typeof d.ruleId === "string"
|
|
312
|
-
? d.ruleId
|
|
313
|
-
: "";
|
|
314
|
-
const versionId =
|
|
315
|
-
typeof d.version === "string"
|
|
316
|
-
? d.version
|
|
317
|
-
: typeof d.versionId === "string"
|
|
318
|
-
? d.versionId
|
|
319
|
-
: "";
|
|
320
|
-
return new NoApplicableRouteError(
|
|
321
|
-
message,
|
|
322
|
-
{
|
|
323
|
-
ruleId,
|
|
324
|
-
versionId,
|
|
325
|
-
reason: typeof d.reason === "string" ? d.reason : "",
|
|
326
|
-
},
|
|
327
|
-
details,
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
if (status === 422 && errorCode === "ENV_NOT_ENABLED_FOR_PROVIDER") {
|
|
331
|
-
const d = (details && typeof details === "object" ? details : {}) as Record<
|
|
332
|
-
string,
|
|
333
|
-
unknown
|
|
334
|
-
>;
|
|
335
|
-
const ruleId =
|
|
336
|
-
typeof d.rule === "string"
|
|
337
|
-
? d.rule
|
|
338
|
-
: typeof d.ruleId === "string"
|
|
339
|
-
? d.ruleId
|
|
340
|
-
: "";
|
|
341
|
-
const envSlug =
|
|
342
|
-
typeof d.env === "string"
|
|
343
|
-
? d.env
|
|
344
|
-
: typeof d.envSlug === "string"
|
|
345
|
-
? d.envSlug
|
|
346
|
-
: "";
|
|
347
|
-
return new EnvNotEnabledForProviderError(
|
|
348
|
-
message,
|
|
349
|
-
{
|
|
350
|
-
ruleId,
|
|
351
|
-
envSlug,
|
|
352
|
-
providerId: typeof d.providerId === "string" ? d.providerId : "",
|
|
353
|
-
},
|
|
354
|
-
details,
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
230
|
return new To11aiApiError(message, status, errorCode, details);
|
|
358
231
|
}
|
|
359
232
|
|
|
@@ -380,12 +253,6 @@ export class Transport {
|
|
|
380
253
|
|
|
381
254
|
headers.authorization = `Bearer ${this.auth.apiKey}`;
|
|
382
255
|
|
|
383
|
-
const tp = activeTraceContext();
|
|
384
|
-
if (tp) {
|
|
385
|
-
headers.traceparent = tp.traceparent;
|
|
386
|
-
if (tp.tracestate) headers.tracestate = tp.tracestate;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
256
|
return headers;
|
|
390
257
|
}
|
|
391
258
|
|