@to11ai/sdk 0.1.2 → 0.2.0

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.
Files changed (42) hide show
  1. package/dist/behaviors/store.d.ts +62 -0
  2. package/dist/client.cjs +37 -4
  3. package/dist/client.d.ts +41 -2
  4. package/dist/client.js +45 -4
  5. package/dist/errors.cjs +90 -1
  6. package/dist/errors.d.ts +142 -0
  7. package/dist/errors.js +90 -1
  8. package/dist/gateway.cjs +12 -0
  9. package/dist/gateway.d.ts +26 -12
  10. package/dist/gateway.js +14 -0
  11. package/dist/generated/index.d.ts +1 -0
  12. package/dist/generated/types.gen.d.ts +22974 -0
  13. package/dist/generated/zod.gen.d.ts +13531 -0
  14. package/dist/projectEnvironments/resource.cjs +7 -2
  15. package/dist/projectEnvironments/resource.js +7 -2
  16. package/dist/projectEnvironments/types.d.ts +4 -75
  17. package/dist/prompts/cache.d.ts +24 -3
  18. package/dist/prompts/resource.cjs +101 -3
  19. package/dist/prompts/resource.d.ts +47 -3
  20. package/dist/prompts/resource.js +103 -5
  21. package/dist/prompts/types.d.ts +36 -177
  22. package/dist/routingRules/resource.cjs +9 -2
  23. package/dist/routingRules/resource.js +9 -2
  24. package/dist/routingRules/types.d.ts +12 -123
  25. package/dist/transport.d.ts +23 -0
  26. package/package.json +23 -12
  27. package/src/behaviors/store.ts +175 -0
  28. package/src/client.ts +115 -3
  29. package/src/errors.ts +261 -0
  30. package/src/gateway.ts +54 -12
  31. package/src/generated/index.ts +3 -0
  32. package/src/generated/types.gen.ts +24083 -0
  33. package/src/generated/zod.gen.ts +12648 -0
  34. package/src/projectEnvironments/resource.ts +10 -0
  35. package/src/projectEnvironments/types.ts +9 -91
  36. package/src/prompts/cache.ts +39 -4
  37. package/src/prompts/resource.ts +258 -5
  38. package/src/prompts/types.ts +56 -237
  39. package/src/routingRules/resource.ts +10 -1
  40. package/src/routingRules/types.ts +21 -155
  41. package/src/traceparent.ts +69 -4
  42. package/src/transport.ts +188 -3
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Published-SDK behavior resolution (TO11-2100 / feature-flagging Slice S7).
3
+ *
4
+ * The to11 platform evaluates flags that influence *shipped-SDK behavior*
5
+ * server-side and delivers their values on the optional `sdkBehavior` field of
6
+ * the success envelope (PRD Decision 7). This SDK carries **no** LaunchDarkly
7
+ * dependency and never connects to to11's control plane — it only reads the
8
+ * value the server already evaluated.
9
+ *
10
+ * Resolution precedence, applied by {@link BehaviorStore.resolve}:
11
+ * 1. local override — init option, then env var (init option wins ties)
12
+ * 2. server-delivered value — the last value stored for that key
13
+ * 3. coded default — the value the caller passes to `resolve`
14
+ *
15
+ * A value whose runtime type disagrees with the coded default's type falls
16
+ * through to the next tier (ultimately the coded default), mirroring the
17
+ * server wrapper's kind-guard. An absent envelope field never clears
18
+ * previously-stored values.
19
+ */
20
+ /** The wire shape of the optional `sdkBehavior` envelope field. */
21
+ export interface SdkBehaviorEnvelope {
22
+ readonly v: number;
23
+ readonly flags: Record<string, unknown>;
24
+ }
25
+ /**
26
+ * Convert a namespaced flag key to its env-var name: prefix + uppercased key
27
+ * with `.` and `-` replaced by `_`. e.g. `sdk.retry-v2` →
28
+ * `TO11_SDK_BEHAVIOR_SDK_RETRY_V2`.
29
+ */
30
+ export declare const envVarNameForKey: (key: string) => string;
31
+ export interface BehaviorStoreOptions {
32
+ /** Init-time local overrides, keyed by namespaced flag key. Highest precedence. */
33
+ readonly initOverrides?: Record<string, unknown>;
34
+ /**
35
+ * The process environment to read `TO11_SDK_BEHAVIOR_*` overrides from.
36
+ * Pass `undefined` (the default) in browser contexts so the SDK never
37
+ * touches `process.env`.
38
+ */
39
+ readonly env?: Record<string, string | undefined>;
40
+ }
41
+ export declare class BehaviorStore {
42
+ /** Last-write-wins server-delivered values, keyed by namespaced flag key. */
43
+ private readonly serverValues;
44
+ /** Init-option overrides, keyed by flag key. */
45
+ private readonly initOverrides;
46
+ /** Env-var overrides, keyed by full env-var name (e.g. `TO11_SDK_BEHAVIOR_SDK_RETRY_V2`). */
47
+ private readonly envOverrides;
48
+ constructor(options?: BehaviorStoreOptions);
49
+ /**
50
+ * Update the server-delivered values from a response's `sdkBehavior` field.
51
+ * No-op when the field is absent or carries an unsupported version. Never
52
+ * clears previously-stored values.
53
+ */
54
+ updateFromEnvelope(sdkBehavior: SdkBehaviorEnvelope | undefined): void;
55
+ /**
56
+ * Resolve a behavior value under the fixed precedence: local override (init
57
+ * option, then env var) → server-delivered value → coded default. A tier
58
+ * whose value is absent or type-mismatched against `codedDefault` falls
59
+ * through to the next tier.
60
+ */
61
+ resolve<T>(key: string, codedDefault: T): T;
62
+ }
package/dist/client.cjs CHANGED
@@ -41,6 +41,7 @@ var exports_client = {};
41
41
  __export(exports_client, {
42
42
  gatewayPromptHeaders: () => import_gateway.gatewayPromptHeaders,
43
43
  gatewayAuthHeaders: () => import_gateway.gatewayAuthHeaders,
44
+ envVarNameForKey: () => import_store2.envVarNameForKey,
44
45
  createClient: () => createClient,
45
46
  To11aiValidationError: () => import_errors.To11aiValidationError,
46
47
  To11aiRateLimitError: () => import_errors.To11aiRateLimitError,
@@ -54,20 +55,31 @@ __export(exports_client, {
54
55
  RoutingRuleVersionResource: () => import_resource6.RoutingRuleVersionResource,
55
56
  RoutingRuleReleaseResource: () => import_resource6.RoutingRuleReleaseResource,
56
57
  PromptResource: () => import_resource5.PromptResource,
58
+ PromptPolicyViolationError: () => import_errors.PromptPolicyViolationError,
59
+ PromptNotFoundError: () => import_errors.PromptNotFoundError,
57
60
  PromptLabelNotRegisteredError: () => import_errors.PromptLabelNotRegisteredError,
58
61
  ProjectEnvironmentResource: () => import_resource4.ProjectEnvironmentResource,
59
62
  NoApplicableRouteError: () => import_errors.NoApplicableRouteError,
60
- EnvNotEnabledForProviderError: () => import_errors.EnvNotEnabledForProviderError
63
+ NoActiveReleaseError: () => import_errors.NoActiveReleaseError,
64
+ EnvNotFoundError: () => import_errors.EnvNotFoundError,
65
+ EnvNotEnabledForProviderError: () => import_errors.EnvNotEnabledForProviderError,
66
+ ENV_SLUG_REGEX: () => import_types2.ENV_SLUG_REGEX,
67
+ BehaviorStore: () => import_store2.BehaviorStore,
68
+ AmbiguousEnvLabelsError: () => import_errors.AmbiguousEnvLabelsError
61
69
  });
62
70
  module.exports = __toCommonJS(exports_client);
71
+ var import_store = require("./behaviors/store");
63
72
  var import_resource = require("./projectEnvironments/resource");
64
73
  var import_resource2 = require("./prompts/resource");
74
+ var import_types = require("./prompts/types");
65
75
  var import_resource3 = require("./routingRules/resource");
66
76
  var import_transport = require("./transport");
77
+ var import_store2 = require("./behaviors/store");
67
78
  var import_errors = require("./errors");
68
79
  var import_gateway = require("./gateway");
69
80
  var import_resource4 = require("./projectEnvironments/resource");
70
81
  var import_resource5 = require("./prompts/resource");
82
+ var import_types2 = require("./prompts/types");
71
83
  var import_resource6 = require("./routingRules/resource");
72
84
  function createClient(options) {
73
85
  const isDomBrowser = typeof globalThis === "object" && typeof globalThis.document !== "undefined";
@@ -80,6 +92,12 @@ function createClient(options) {
80
92
  if (options.apiKey !== undefined && options.apiKey.trim() === "") {
81
93
  throw new Error("[to11ai/sdk] `apiKey` must not be an empty string.");
82
94
  }
95
+ if (options.projectId !== undefined && options.projectId.trim() === "") {
96
+ throw new Error("[to11ai/sdk] `projectId` must not be an empty string — omit it or pass a valid project id.");
97
+ }
98
+ if (options.env !== undefined && !import_types.ENV_SLUG_REGEX.test(options.env)) {
99
+ throw new Error(`[to11ai/sdk] \`env\` must match ${import_types.ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(options.env)}`);
100
+ }
83
101
  const hasApiKey = options.apiKey !== undefined;
84
102
  const hasGetAccessToken = options.getAccessToken !== undefined;
85
103
  const getAccessToken = options.getAccessToken;
@@ -87,14 +105,29 @@ function createClient(options) {
87
105
  throw new Error("[to11ai/sdk] Provide exactly one of `apiKey` (server) or `getAccessToken` (browser).");
88
106
  }
89
107
  const auth = options.apiKey !== undefined ? { apiKey: options.apiKey } : { getAccessToken };
108
+ const isBrowser = isDomBrowser || isWorkerBrowser;
109
+ const processEnv = !isBrowser && typeof process !== "undefined" && typeof process.env === "object" ? process.env : undefined;
110
+ const behaviorStore = new import_store.BehaviorStore({
111
+ initOverrides: options.behaviorOverrides,
112
+ env: processEnv
113
+ });
90
114
  const transport = new import_transport.Transport({
91
115
  baseUrl: options.baseUrl,
92
116
  auth,
93
117
  timeoutMs: options.timeoutMs,
94
- maxRetries: options.maxRetries
118
+ maxRetries: options.maxRetries,
119
+ behaviorStore
120
+ });
121
+ const prompts = new import_resource2.PromptResource(transport, options.cache, {
122
+ projectId: options.projectId,
123
+ env: options.env
95
124
  });
96
- const prompts = new import_resource2.PromptResource(transport, options.cache);
97
125
  const projectEnvironments = new import_resource.ProjectEnvironmentResource(transport);
98
126
  const routingRules = new import_resource3.RoutingRulesResource(transport);
99
- return { prompts, projectEnvironments, routingRules };
127
+ return {
128
+ prompts,
129
+ projectEnvironments,
130
+ routingRules,
131
+ resolveBehavior: (key, codedDefault) => behaviorStore.resolve(key, codedDefault)
132
+ };
100
133
  }
package/dist/client.d.ts CHANGED
@@ -2,14 +2,18 @@ import { ProjectEnvironmentResource } from "./projectEnvironments/resource";
2
2
  import type { CacheConfig } from "./prompts/cache";
3
3
  import { PromptResource } from "./prompts/resource";
4
4
  import { RoutingRulesResource } from "./routingRules/resource";
5
- export type { NoApplicableRouteReason } from "./errors";
6
- export { EnvNotEnabledForProviderError, NoApplicableRouteError, PromptLabelNotRegisteredError, RuleNotFoundError, RuleVersionInvalidSnapshotError, To11aiApiError, To11aiAuthError, To11aiError, To11aiNetworkError, To11aiRateLimitError, To11aiValidationError, } from "./errors";
5
+ export { BehaviorStore, type BehaviorStoreOptions, envVarNameForKey, type SdkBehaviorEnvelope, } from "./behaviors/store";
6
+ export type { AmbiguousEnvLabelsReason, EnvNotFoundReason, NoActiveReleaseReason, NoApplicableRouteReason, PromptNotFoundReason, PromptPolicyViolationReason, } from "./errors";
7
+ export { AmbiguousEnvLabelsError, EnvNotEnabledForProviderError, EnvNotFoundError, NoActiveReleaseError, NoApplicableRouteError, PromptLabelNotRegisteredError, PromptNotFoundError, PromptPolicyViolationError, RuleNotFoundError, RuleVersionInvalidSnapshotError, To11aiApiError, To11aiAuthError, To11aiError, To11aiNetworkError, To11aiRateLimitError, To11aiValidationError, } from "./errors";
7
8
  export type { GatewayAuthOptions } from "./gateway";
8
9
  export { gatewayAuthHeaders, gatewayPromptHeaders } from "./gateway";
9
10
  export type { ArchiveProjectEnvironmentInput, ArchiveProjectEnvironmentResult, CreateProjectEnvironmentInput, CreateProjectEnvironmentResult, GetProjectEnvironmentInput, GetProjectEnvironmentResult, ListProjectEnvironmentsInput, ListProjectEnvironmentsResult, LivePointerCounts, ProjectEnvironment, RestoreProjectEnvironmentInput, RestoreProjectEnvironmentResult, UpdateProjectEnvironmentInput, UpdateProjectEnvironmentResult, } from "./projectEnvironments/resource";
10
11
  export { ProjectEnvironmentResource } from "./projectEnvironments/resource";
11
12
  export type { CacheConfig } from "./prompts/cache";
13
+ export type { PromptResourceConfig } from "./prompts/resource";
12
14
  export { PromptResource } from "./prompts/resource";
15
+ export type { BlockRenderRecord, FetchedPrompt, FetchPromptOptions, PromptContext, RenderedChatMessage, RenderedPrompt, ResolvedPrompt, ResolvedPromptEnvelope, } from "./prompts/types";
16
+ export { ENV_SLUG_REGEX } from "./prompts/types";
13
17
  export type { CreateRuleVersionInput, CreateRuleVersionResult, GetRuleVersionInput, GetRuleVersionResult, ListRuleReleasesInput, ListRuleReleasesResult, ListRuleVersionsInput, ListRuleVersionsResult, RollbackRuleReleaseInput, RollbackRuleReleaseResult, RuleModelBinding, RuleProviderRef, RuleRelease, RuleReleaseActorType, RuleReleaseApprovalStatus, RuleReleaseChangeKind, RuleReleaseStrategy, RuleReleaseWeight, RuleSnapshot, RuleStrategy, RuleVersion, SetRuleReleaseInput, SetRuleReleaseResult, } from "./routingRules/resource";
14
18
  export { RoutingRuleReleaseResource, RoutingRulesResource, RoutingRuleVersionResource, } from "./routingRules/resource";
15
19
  export type { Transport } from "./transport";
@@ -26,17 +30,52 @@ interface BrowserAuthOptions {
26
30
  type AuthOptions = ServerAuthOptions | BrowserAuthOptions;
27
31
  export type ClientOptions = AuthOptions & {
28
32
  baseUrl: string;
33
+ /**
34
+ * Project the client is bound to (TO11-980 / S9). Required for the
35
+ * env-aware `prompts.fetch(slug, …)` surface, which builds the
36
+ * project-scoped resolve path from it. Validated non-empty at
37
+ * construction. The deprecated `prompts.resolve()` keeps its per-call
38
+ * `projectId` and ignores this.
39
+ */
40
+ projectId?: string;
41
+ /**
42
+ * Default serving environment for `prompts.fetch(slug)` (TO11-980 / S9,
43
+ * Style-A). When set, every `fetch()` call inherits it unless overridden
44
+ * per call. Validated eagerly against the project-environments registry
45
+ * rule (lowercase-kebab, `^[a-z0-9-]{1,63}$`): empty / whitespace-only /
46
+ * uppercase / 64+-char values throw at construction with a loud
47
+ * `[to11ai/sdk]` message before any request hits the wire.
48
+ */
49
+ env?: string;
29
50
  /** LRU cache configuration for resolve(). Defaults: ttlSeconds=300, maxEntries=100 */
30
51
  cache?: CacheConfig;
31
52
  /** Request timeout in milliseconds. Default: 30_000 */
32
53
  timeoutMs?: number;
33
54
  /** Max retry attempts for network errors / 5xx. Default: 3 */
34
55
  maxRetries?: number;
56
+ /**
57
+ * Purely local overrides for server-evaluated SDK-behavior flags (TO11-2100),
58
+ * keyed by namespaced flag key (e.g. `{ "sdk.retry-v2": true }`). These take
59
+ * precedence over server-delivered values and the SDK's coded defaults. For
60
+ * the same flag key, an init option here wins over a `TO11_SDK_BEHAVIOR_*`
61
+ * environment variable. Published-SDK only — no LaunchDarkly involved.
62
+ */
63
+ behaviorOverrides?: Record<string, unknown>;
35
64
  };
36
65
  export interface To11aiClient {
37
66
  readonly prompts: PromptResource;
38
67
  readonly projectEnvironments: ProjectEnvironmentResource;
39
68
  readonly routingRules: RoutingRulesResource;
69
+ /**
70
+ * Resolve a server-evaluated SDK-behavior flag value (TO11-2100) under the
71
+ * fixed precedence: local override (init option → `TO11_SDK_BEHAVIOR_*` env
72
+ * var) → server-delivered value (captured from responses the client has
73
+ * already made) → the `codedDefault` passed here. A value whose runtime type
74
+ * disagrees with `codedDefault` falls through to the next tier. The SDK
75
+ * carries no LaunchDarkly dependency; this only reads what the server
76
+ * delivered on the envelope.
77
+ */
78
+ readonly resolveBehavior: <T>(key: string, codedDefault: T) => T;
40
79
  }
41
80
  /**
42
81
  * Create a to11ai SDK client.
package/dist/client.js CHANGED
@@ -1,12 +1,23 @@
1
1
  // src/client.ts
2
+ import { BehaviorStore } from "./behaviors/store";
2
3
  import { ProjectEnvironmentResource } from "./projectEnvironments/resource";
3
4
  import { PromptResource } from "./prompts/resource";
5
+ import { ENV_SLUG_REGEX } from "./prompts/types";
4
6
  import { RoutingRulesResource } from "./routingRules/resource";
5
7
  import { Transport } from "./transport";
6
8
  import {
9
+ BehaviorStore as BehaviorStore2,
10
+ envVarNameForKey
11
+ } from "./behaviors/store";
12
+ import {
13
+ AmbiguousEnvLabelsError,
7
14
  EnvNotEnabledForProviderError,
15
+ EnvNotFoundError,
16
+ NoActiveReleaseError,
8
17
  NoApplicableRouteError,
9
18
  PromptLabelNotRegisteredError,
19
+ PromptNotFoundError,
20
+ PromptPolicyViolationError,
10
21
  RuleNotFoundError,
11
22
  RuleVersionInvalidSnapshotError,
12
23
  To11aiApiError,
@@ -19,6 +30,7 @@ import {
19
30
  import { gatewayAuthHeaders, gatewayPromptHeaders } from "./gateway";
20
31
  import { ProjectEnvironmentResource as ProjectEnvironmentResource2 } from "./projectEnvironments/resource";
21
32
  import { PromptResource as PromptResource2 } from "./prompts/resource";
33
+ import { ENV_SLUG_REGEX as ENV_SLUG_REGEX2 } from "./prompts/types";
22
34
  import {
23
35
  RoutingRuleReleaseResource,
24
36
  RoutingRulesResource as RoutingRulesResource2,
@@ -35,6 +47,12 @@ function createClient(options) {
35
47
  if (options.apiKey !== undefined && options.apiKey.trim() === "") {
36
48
  throw new Error("[to11ai/sdk] `apiKey` must not be an empty string.");
37
49
  }
50
+ if (options.projectId !== undefined && options.projectId.trim() === "") {
51
+ throw new Error("[to11ai/sdk] `projectId` must not be an empty string — omit it or pass a valid project id.");
52
+ }
53
+ if (options.env !== undefined && !ENV_SLUG_REGEX.test(options.env)) {
54
+ throw new Error(`[to11ai/sdk] \`env\` must match ${ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(options.env)}`);
55
+ }
38
56
  const hasApiKey = options.apiKey !== undefined;
39
57
  const hasGetAccessToken = options.getAccessToken !== undefined;
40
58
  const getAccessToken = options.getAccessToken;
@@ -42,20 +60,36 @@ function createClient(options) {
42
60
  throw new Error("[to11ai/sdk] Provide exactly one of `apiKey` (server) or `getAccessToken` (browser).");
43
61
  }
44
62
  const auth = options.apiKey !== undefined ? { apiKey: options.apiKey } : { getAccessToken };
63
+ const isBrowser = isDomBrowser || isWorkerBrowser;
64
+ const processEnv = !isBrowser && typeof process !== "undefined" && typeof process.env === "object" ? process.env : undefined;
65
+ const behaviorStore = new BehaviorStore({
66
+ initOverrides: options.behaviorOverrides,
67
+ env: processEnv
68
+ });
45
69
  const transport = new Transport({
46
70
  baseUrl: options.baseUrl,
47
71
  auth,
48
72
  timeoutMs: options.timeoutMs,
49
- maxRetries: options.maxRetries
73
+ maxRetries: options.maxRetries,
74
+ behaviorStore
75
+ });
76
+ const prompts = new PromptResource(transport, options.cache, {
77
+ projectId: options.projectId,
78
+ env: options.env
50
79
  });
51
- const prompts = new PromptResource(transport, options.cache);
52
80
  const projectEnvironments = new ProjectEnvironmentResource(transport);
53
81
  const routingRules = new RoutingRulesResource(transport);
54
- return { prompts, projectEnvironments, routingRules };
82
+ return {
83
+ prompts,
84
+ projectEnvironments,
85
+ routingRules,
86
+ resolveBehavior: (key, codedDefault) => behaviorStore.resolve(key, codedDefault)
87
+ };
55
88
  }
56
89
  export {
57
90
  gatewayPromptHeaders,
58
91
  gatewayAuthHeaders,
92
+ envVarNameForKey,
59
93
  createClient,
60
94
  To11aiValidationError,
61
95
  To11aiRateLimitError,
@@ -69,8 +103,15 @@ export {
69
103
  RoutingRuleVersionResource,
70
104
  RoutingRuleReleaseResource,
71
105
  PromptResource2 as PromptResource,
106
+ PromptPolicyViolationError,
107
+ PromptNotFoundError,
72
108
  PromptLabelNotRegisteredError,
73
109
  ProjectEnvironmentResource2 as ProjectEnvironmentResource,
74
110
  NoApplicableRouteError,
75
- EnvNotEnabledForProviderError
111
+ NoActiveReleaseError,
112
+ EnvNotFoundError,
113
+ EnvNotEnabledForProviderError,
114
+ ENV_SLUG_REGEX2 as ENV_SLUG_REGEX,
115
+ BehaviorStore2 as BehaviorStore,
116
+ AmbiguousEnvLabelsError
76
117
  };
package/dist/errors.cjs CHANGED
@@ -40,6 +40,7 @@ var __export = (target, all) => {
40
40
  var exports_errors = {};
41
41
  __export(exports_errors, {
42
42
  To11aiValidationError: () => To11aiValidationError,
43
+ To11aiResponseValidationError: () => To11aiResponseValidationError,
43
44
  To11aiRateLimitError: () => To11aiRateLimitError,
44
45
  To11aiNetworkError: () => To11aiNetworkError,
45
46
  To11aiError: () => To11aiError,
@@ -47,9 +48,14 @@ __export(exports_errors, {
47
48
  To11aiApiError: () => To11aiApiError,
48
49
  RuleVersionInvalidSnapshotError: () => RuleVersionInvalidSnapshotError,
49
50
  RuleNotFoundError: () => RuleNotFoundError,
51
+ PromptPolicyViolationError: () => PromptPolicyViolationError,
52
+ PromptNotFoundError: () => PromptNotFoundError,
50
53
  PromptLabelNotRegisteredError: () => PromptLabelNotRegisteredError,
51
54
  NoApplicableRouteError: () => NoApplicableRouteError,
52
- EnvNotEnabledForProviderError: () => EnvNotEnabledForProviderError
55
+ NoActiveReleaseError: () => NoActiveReleaseError,
56
+ EnvNotFoundError: () => EnvNotFoundError,
57
+ EnvNotEnabledForProviderError: () => EnvNotEnabledForProviderError,
58
+ AmbiguousEnvLabelsError: () => AmbiguousEnvLabelsError
53
59
  });
54
60
  module.exports = __toCommonJS(exports_errors);
55
61
 
@@ -109,6 +115,21 @@ class To11aiNetworkError extends To11aiError {
109
115
  }
110
116
  }
111
117
 
118
+ class To11aiResponseValidationError extends To11aiError {
119
+ path;
120
+ issues;
121
+ constructor(path, zodError, cause) {
122
+ super(`[to11ai/sdk] Response from ${path} failed schema validation`, {
123
+ cause
124
+ });
125
+ this.name = "To11aiResponseValidationError";
126
+ this.path = path;
127
+ const candidate = zodError?.issues;
128
+ this.issues = Array.isArray(candidate) ? candidate : [];
129
+ Object.setPrototypeOf(this, new.target.prototype);
130
+ }
131
+ }
132
+
112
133
  class PromptLabelNotRegisteredError extends To11aiApiError {
113
134
  projectId;
114
135
  label;
@@ -175,6 +196,74 @@ class EnvNotEnabledForProviderError extends To11aiApiError {
175
196
  }
176
197
  }
177
198
 
199
+ class PromptNotFoundError extends To11aiApiError {
200
+ reason;
201
+ constructor(message, errorCode, reason, details, statusCode = 404) {
202
+ super(message, statusCode, errorCode, details);
203
+ this.name = "PromptNotFoundError";
204
+ this.reason = reason;
205
+ Object.setPrototypeOf(this, new.target.prototype);
206
+ }
207
+ static is(err) {
208
+ return err instanceof PromptNotFoundError;
209
+ }
210
+ }
211
+
212
+ class EnvNotFoundError extends To11aiApiError {
213
+ reason;
214
+ constructor(message, reason, details, statusCode) {
215
+ super(message, statusCode, "ENV_NOT_FOUND", details);
216
+ this.name = "EnvNotFoundError";
217
+ this.reason = reason;
218
+ Object.setPrototypeOf(this, new.target.prototype);
219
+ }
220
+ static missing(env) {
221
+ return new EnvNotFoundError("[to11ai/sdk] No environment resolvable for prompts.fetch(): none supplied at client init and none supplied per call. Pass `env` to createClient or fetch(slug, { env }).", { env, source: "missing" }, undefined, 0);
222
+ }
223
+ static is(err) {
224
+ return err instanceof EnvNotFoundError;
225
+ }
226
+ }
227
+
228
+ class NoActiveReleaseError extends To11aiApiError {
229
+ reason;
230
+ constructor(message, reason, details, statusCode = 409) {
231
+ super(message, statusCode, "NO_ACTIVE_RELEASE", details);
232
+ this.name = "NoActiveReleaseError";
233
+ this.reason = reason;
234
+ Object.setPrototypeOf(this, new.target.prototype);
235
+ }
236
+ static is(err) {
237
+ return err instanceof NoActiveReleaseError;
238
+ }
239
+ }
240
+
241
+ class PromptPolicyViolationError extends To11aiApiError {
242
+ reason;
243
+ constructor(message, reason, details, statusCode = 403) {
244
+ super(message, statusCode, reason.code, details);
245
+ this.name = "PromptPolicyViolationError";
246
+ this.reason = reason;
247
+ Object.setPrototypeOf(this, new.target.prototype);
248
+ }
249
+ static is(err) {
250
+ return err instanceof PromptPolicyViolationError;
251
+ }
252
+ }
253
+
254
+ class AmbiguousEnvLabelsError extends To11aiApiError {
255
+ reason;
256
+ constructor(message, reason, details, statusCode = 409) {
257
+ super(message, statusCode, "AMBIGUOUS_ENV_LABELS", details);
258
+ this.name = "AmbiguousEnvLabelsError";
259
+ this.reason = reason;
260
+ Object.setPrototypeOf(this, new.target.prototype);
261
+ }
262
+ static is(err) {
263
+ return err instanceof AmbiguousEnvLabelsError;
264
+ }
265
+ }
266
+
178
267
  class RuleVersionInvalidSnapshotError extends To11aiApiError {
179
268
  field;
180
269
  reason;
package/dist/errors.d.ts CHANGED
@@ -38,6 +38,19 @@ export declare class To11aiRateLimitError extends To11aiApiError {
38
38
  export declare class To11aiNetworkError extends To11aiError {
39
39
  constructor(message: string, cause?: unknown);
40
40
  }
41
+ /**
42
+ * Thrown when a 2xx response body fails runtime validation against the
43
+ * generated wire schema. Distinct from `To11aiValidationError`, which models a
44
+ * server-side 4xx validation rejection.
45
+ */
46
+ export declare class To11aiResponseValidationError extends To11aiError {
47
+ readonly path: string;
48
+ readonly issues: {
49
+ message: string;
50
+ path: (string | number)[];
51
+ }[];
52
+ constructor(path: string, zodError: unknown, cause?: unknown);
53
+ }
41
54
  /**
42
55
  * Thrown when strict-mode rejects a label move for an unregistered label.
43
56
  *
@@ -113,6 +126,135 @@ export declare class EnvNotEnabledForProviderError extends To11aiApiError {
113
126
  }, details?: unknown);
114
127
  static is(err: unknown): err is EnvNotEnabledForProviderError;
115
128
  }
129
+ /**
130
+ * Reason payload for `PromptNotFoundError`.
131
+ *
132
+ * Surfaced when the server cannot resolve the prompt for the given slug
133
+ * (`PROMPT_NOT_FOUND`) or cannot find a version matching the supplied
134
+ * labels (`PROMPT_LABELS_NOT_FOUND`). `slug` / `projectId` are echoed from
135
+ * the request context when the server provides them in `details`.
136
+ */
137
+ export interface PromptNotFoundReason {
138
+ slug?: string;
139
+ projectId?: string;
140
+ }
141
+ /**
142
+ * Thrown when a prompt cannot be resolved. Covers two server codes (single
143
+ * class so one `catch` matches both):
144
+ *
145
+ * * `PROMPT_NOT_FOUND` — no prompt exists for the requested slug.
146
+ * * `PROMPT_LABELS_NOT_FOUND` — the prompt exists but no version matches
147
+ * all the supplied labels (the `env`→label resolution found nothing).
148
+ *
149
+ * Surfaces at HTTP 404 (the canonical code lives in `details.code`).
150
+ */
151
+ export declare class PromptNotFoundError extends To11aiApiError {
152
+ readonly reason: PromptNotFoundReason;
153
+ constructor(message: string, errorCode: string, reason: PromptNotFoundReason, details?: unknown, statusCode?: number);
154
+ static is(err: unknown): err is PromptNotFoundError;
155
+ }
156
+ /**
157
+ * Reason payload for `EnvNotFoundError`.
158
+ *
159
+ * `source` discriminates the two failure modes (D2):
160
+ * - `"missing"` — no env was supplied at init OR at call time. Raised
161
+ * CLIENT-side, eagerly, before any request (R4 / AC4); `env` is absent.
162
+ * - `"unknown"` — an env was supplied but the server's registry has no
163
+ * such (active) env. Surfaced from the `ENV_NOT_FOUND` server code; `env`
164
+ * carries the rejected slug when the server echoes it.
165
+ */
166
+ export interface EnvNotFoundReason {
167
+ env?: string;
168
+ source: "missing" | "unknown";
169
+ }
170
+ /**
171
+ * Thrown when no serving environment can be resolved for a `prompts.fetch()`
172
+ * call. One class, two `reason.source` discriminants (see
173
+ * `EnvNotFoundReason`): client-eager `"missing"` (R4) and server-forwarded
174
+ * `"unknown"` (`ENV_NOT_FOUND`).
175
+ *
176
+ * The client-eager variant carries no `statusCode`/`errorCode` from the wire
177
+ * (no request was made); it reports status `0` and errorCode `ENV_NOT_FOUND`
178
+ * for a uniform catch surface.
179
+ */
180
+ export declare class EnvNotFoundError extends To11aiApiError {
181
+ readonly reason: EnvNotFoundReason;
182
+ constructor(message: string, reason: EnvNotFoundReason, details: unknown, statusCode: number);
183
+ /** Build the client-eager (missing-at-both) variant — no transport call. */
184
+ static missing(env?: string): EnvNotFoundError;
185
+ static is(err: unknown): err is EnvNotFoundError;
186
+ }
187
+ /**
188
+ * Reason payload for `NoActiveReleaseError`.
189
+ *
190
+ * Identifies the prompt (by `slug` and/or `promptId`) and the `env` for
191
+ * which no active release row exists.
192
+ */
193
+ export interface NoActiveReleaseReason {
194
+ slug?: string;
195
+ promptId?: string;
196
+ env?: string;
197
+ }
198
+ /**
199
+ * Thrown when a prompt resolves but has no active release for the requested
200
+ * env (`NO_ACTIVE_RELEASE`). The release primitive (S3) found the prompt but
201
+ * nothing is currently serving in that environment.
202
+ */
203
+ export declare class NoActiveReleaseError extends To11aiApiError {
204
+ readonly reason: NoActiveReleaseReason;
205
+ constructor(message: string, reason: NoActiveReleaseReason, details?: unknown, statusCode?: number);
206
+ static is(err: unknown): err is NoActiveReleaseError;
207
+ }
208
+ /**
209
+ * Reason payload for `PromptPolicyViolationError`.
210
+ *
211
+ * `code` is the specific server policy code (`POLICY_VIOLATION` or one of the
212
+ * `ENV_POLICY_*` family from S5 / TO11-976). `policy` / `detail` are
213
+ * server-shaped and forwarded verbatim when present.
214
+ */
215
+ export interface PromptPolicyViolationReason {
216
+ code: string;
217
+ policy?: string;
218
+ detail?: unknown;
219
+ }
220
+ /**
221
+ * Thrown when the server's S5 environment-policy machinery rejects a
222
+ * resolution. Forwarded verbatim from the server policy codes — the generic
223
+ * `POLICY_VIOLATION` and the `ENV_POLICY_*` family (required-source-env,
224
+ * minimum-dwell-time, requires-approval, separation-of-duties, …).
225
+ *
226
+ * CRITICAL (D3 branch-ordering): the server can return a policy violation at
227
+ * HTTP 403. The transport mapper MUST match this (by `details.code`) BEFORE
228
+ * the generic `401 || 403 → To11aiAuthError` guard, otherwise a 403 policy
229
+ * error would be swallowed as an auth error (AC6).
230
+ */
231
+ export declare class PromptPolicyViolationError extends To11aiApiError {
232
+ readonly reason: PromptPolicyViolationReason;
233
+ constructor(message: string, reason: PromptPolicyViolationReason, details?: unknown, statusCode?: number);
234
+ static is(err: unknown): err is PromptPolicyViolationError;
235
+ }
236
+ /**
237
+ * Reason payload for `AmbiguousEnvLabelsError`.
238
+ *
239
+ * Carried when the supplied env-label matched more than one candidate
240
+ * version and the server cannot pick deterministically
241
+ * (`AMBIGUOUS_ENV_LABELS`).
242
+ */
243
+ export interface AmbiguousEnvLabelsReason {
244
+ env?: string;
245
+ slug?: string;
246
+ }
247
+ /**
248
+ * Thrown when the env→label resolution is ambiguous on the server
249
+ * (`AMBIGUOUS_ENV_LABELS`) — the label matched multiple candidates and the
250
+ * server declines to guess. Surfaces (rather than 500ing) so callers get a
251
+ * typed, actionable error.
252
+ */
253
+ export declare class AmbiguousEnvLabelsError extends To11aiApiError {
254
+ readonly reason: AmbiguousEnvLabelsReason;
255
+ constructor(message: string, reason: AmbiguousEnvLabelsReason, details?: unknown, statusCode?: number);
256
+ static is(err: unknown): err is AmbiguousEnvLabelsError;
257
+ }
116
258
  /**
117
259
  * Thrown when version create rejects the snapshot for a structural
118
260
  * reason — invalid variant params, malformed `when` clause, illegal