@to11ai/sdk 2.0.0-rc.2 → 2.0.0-rc.4
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 +99 -258
- package/dist/client.cjs +17 -41
- package/dist/client.d.ts +23 -37
- package/dist/client.js +15 -50
- package/dist/errors.cjs +19 -24
- package/dist/errors.d.ts +18 -46
- package/dist/errors.js +19 -24
- package/dist/gateway.cjs +18 -12
- package/dist/gateway.d.ts +19 -20
- package/dist/gateway.js +18 -12
- package/dist/generated/index.d.ts +1 -1
- package/dist/generated/types.gen.d.ts +103 -0
- package/dist/generated/zod.gen.d.ts +55 -12
- package/dist/prompts/resource.cjs +54 -47
- package/dist/prompts/resource.d.ts +17 -16
- package/dist/prompts/resource.js +58 -47
- package/dist/prompts/shape.d.ts +1 -1
- package/dist/prompts/types.d.ts +36 -32
- package/dist/transport.d.ts +2 -6
- package/package.json +3 -11
- package/src/client.ts +38 -118
- package/src/context.ts +3 -5
- package/src/errors.ts +34 -78
- package/src/gateway.ts +44 -39
- package/src/generated/index.ts +1 -1
- package/src/generated/types.gen.ts +108 -0
- package/src/generated/zod.gen.ts +46 -0
- package/src/prompts/resource.ts +90 -93
- package/src/prompts/shape.ts +1 -1
- package/src/prompts/types.ts +36 -33
- package/src/transport.ts +53 -40
- package/dist/prompts/cache.d.ts +0 -58
- package/dist/routingRules/resource.cjs +0 -138
- package/dist/routingRules/resource.js +0 -98
- package/src/prompts/cache.ts +0 -153
package/dist/prompts/types.d.ts
CHANGED
|
@@ -9,13 +9,12 @@ export type PromptStatus = Prompt["status"];
|
|
|
9
9
|
export type PromptVersion = GenPromptVersion;
|
|
10
10
|
export type PromptFormat = PromptVersion["format"];
|
|
11
11
|
export type PromptLabelPointer = GenPromptLabelPointer;
|
|
12
|
-
export type
|
|
13
|
-
export interface
|
|
14
|
-
role:
|
|
12
|
+
export type ChatRole = "system" | "user" | "assistant" | "developer";
|
|
13
|
+
export interface ChatMessage {
|
|
14
|
+
role: ChatRole;
|
|
15
15
|
content: string;
|
|
16
16
|
disabled?: boolean;
|
|
17
17
|
}
|
|
18
|
-
export type ChatMessage = TextChatMessage;
|
|
19
18
|
export type RenderedChatMessage = GenRenderedChatPrompt["messages"][number];
|
|
20
19
|
/** Provider shape for `prompts.render()` output. Omit for the neutral result. */
|
|
21
20
|
export type RenderFormat = "openai" | "anthropic";
|
|
@@ -50,7 +49,7 @@ export interface PaginatedResult<T> {
|
|
|
50
49
|
pagination: PaginationInfo;
|
|
51
50
|
}
|
|
52
51
|
export interface CreatePromptInput {
|
|
53
|
-
projectId
|
|
52
|
+
projectId?: string;
|
|
54
53
|
name: string;
|
|
55
54
|
slug: string;
|
|
56
55
|
description?: string;
|
|
@@ -58,7 +57,7 @@ export interface CreatePromptInput {
|
|
|
58
57
|
}
|
|
59
58
|
export type CreatePromptResult = Prompt;
|
|
60
59
|
export interface ListPromptsInput {
|
|
61
|
-
projectId
|
|
60
|
+
projectId?: string;
|
|
62
61
|
status?: PromptStatus;
|
|
63
62
|
sortBy?: "createdAt" | "updatedAt" | "name";
|
|
64
63
|
tags?: string;
|
|
@@ -67,12 +66,12 @@ export interface ListPromptsInput {
|
|
|
67
66
|
}
|
|
68
67
|
export type ListPromptsResult = Prompt;
|
|
69
68
|
export interface GetPromptInput {
|
|
70
|
-
projectId
|
|
69
|
+
projectId?: string;
|
|
71
70
|
promptId: string;
|
|
72
71
|
}
|
|
73
72
|
export type GetPromptResult = Prompt;
|
|
74
73
|
export interface UpdatePromptInput {
|
|
75
|
-
projectId
|
|
74
|
+
projectId?: string;
|
|
76
75
|
promptId: string;
|
|
77
76
|
name?: string;
|
|
78
77
|
description?: string | null;
|
|
@@ -80,12 +79,12 @@ export interface UpdatePromptInput {
|
|
|
80
79
|
}
|
|
81
80
|
export type UpdatePromptResult = Prompt;
|
|
82
81
|
export interface ArchivePromptInput {
|
|
83
|
-
projectId
|
|
82
|
+
projectId?: string;
|
|
84
83
|
promptId: string;
|
|
85
84
|
}
|
|
86
85
|
export type ArchivePromptResult = Prompt;
|
|
87
86
|
export interface CreatePromptVersionInput {
|
|
88
|
-
projectId
|
|
87
|
+
projectId?: string;
|
|
89
88
|
promptId: string;
|
|
90
89
|
format: PromptFormat;
|
|
91
90
|
templateJson: unknown;
|
|
@@ -95,18 +94,18 @@ export interface CreatePromptVersionInput {
|
|
|
95
94
|
}
|
|
96
95
|
export type CreatePromptVersionResult = PromptVersion;
|
|
97
96
|
export interface ListPromptVersionsInput {
|
|
98
|
-
projectId
|
|
97
|
+
projectId?: string;
|
|
99
98
|
promptId: string;
|
|
100
99
|
}
|
|
101
100
|
export type ListPromptVersionsResult = PromptVersion[];
|
|
102
101
|
export interface GetPromptVersionInput {
|
|
103
|
-
projectId
|
|
102
|
+
projectId?: string;
|
|
104
103
|
promptId: string;
|
|
105
104
|
versionNumber: number;
|
|
106
105
|
}
|
|
107
106
|
export type GetPromptVersionResult = PromptVersion;
|
|
108
107
|
export interface MovePromptLabelInput {
|
|
109
|
-
projectId
|
|
108
|
+
projectId?: string;
|
|
110
109
|
promptId: string;
|
|
111
110
|
label: string;
|
|
112
111
|
versionId: string;
|
|
@@ -114,10 +113,17 @@ export interface MovePromptLabelInput {
|
|
|
114
113
|
}
|
|
115
114
|
export type MovePromptLabelResult = PromptLabelPointer;
|
|
116
115
|
export interface ListPromptLabelsInput {
|
|
117
|
-
projectId
|
|
116
|
+
projectId?: string;
|
|
118
117
|
promptId: string;
|
|
119
118
|
}
|
|
120
119
|
export type ListPromptLabelsResult = PromptLabelPointer[];
|
|
120
|
+
/**
|
|
121
|
+
* Slug rule for a `render()` label — lowercase-kebab, 1-63 chars. Env slugs are
|
|
122
|
+
* a special case of a label value, so labels are held to the same strict rule
|
|
123
|
+
* (no uppercase, no underscores): a label is a routing key, and case-insensitive
|
|
124
|
+
* routing keys invite silent "production" vs "Production" mismatches. Intentionally
|
|
125
|
+
* strict — do not loosen.
|
|
126
|
+
*/
|
|
121
127
|
export declare const ENV_SLUG_REGEX: RegExp;
|
|
122
128
|
export type BlockRenderRecord = Record<string, "rendered" | "dimmed">;
|
|
123
129
|
export interface RenderedPrompt {
|
|
@@ -164,19 +170,17 @@ export interface RenderedPrompt {
|
|
|
164
170
|
*/
|
|
165
171
|
toolChoice?: NonNullable<ResolvedPrompt["toolChoice"]>;
|
|
166
172
|
}
|
|
167
|
-
export type ResolvedPromptEnvelope = GenResolvedPrompt
|
|
168
|
-
/** Active release strategy. Used to decide weighted-no-subject cache bypass (D8). */
|
|
169
|
-
strategy?: "single" | "weighted";
|
|
170
|
-
};
|
|
173
|
+
export type ResolvedPromptEnvelope = GenResolvedPrompt;
|
|
171
174
|
export interface RenderOptions {
|
|
172
|
-
/**
|
|
173
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Label to resolve this call's prompt version; overrides the client default.
|
|
177
|
+
* When omitted, falls back to the client's `env` (env is a label value).
|
|
178
|
+
*/
|
|
179
|
+
label?: string;
|
|
174
180
|
/** Stable id so a weighted release sends the same caller to the same variant. */
|
|
175
181
|
subject?: string;
|
|
176
182
|
variables?: Record<string, unknown>;
|
|
177
|
-
/**
|
|
178
|
-
cache?: boolean;
|
|
179
|
-
/** Prompt served if the API is unreachable on a network error and cache is empty. */
|
|
183
|
+
/** Prompt served if the API is unreachable on a network error. */
|
|
180
184
|
fallback?: RenderedPrompt;
|
|
181
185
|
/** Provider shape for this call's output; overrides the client default. */
|
|
182
186
|
format?: RenderFormat;
|
|
@@ -192,13 +196,13 @@ export type ProjectPromptLabel = GenProjectPromptLabel;
|
|
|
192
196
|
export type ProjectPromptLabelUsage = GenProjectPromptLabelUsage;
|
|
193
197
|
export type PromptLabelEvent = GenPromptLabelEvent;
|
|
194
198
|
export interface ListProjectPromptLabelsInput {
|
|
195
|
-
projectId
|
|
199
|
+
projectId?: string;
|
|
196
200
|
/** Set true to include archived (soft-deleted) entries. Default: false. */
|
|
197
201
|
includeArchived?: boolean;
|
|
198
202
|
}
|
|
199
203
|
export type ListProjectPromptLabelsResult = ProjectPromptLabel[];
|
|
200
204
|
export interface CreateProjectPromptLabelInput {
|
|
201
|
-
projectId
|
|
205
|
+
projectId?: string;
|
|
202
206
|
label: string;
|
|
203
207
|
isDefault?: boolean;
|
|
204
208
|
description?: string;
|
|
@@ -206,12 +210,12 @@ export interface CreateProjectPromptLabelInput {
|
|
|
206
210
|
}
|
|
207
211
|
export type CreateProjectPromptLabelResult = ProjectPromptLabel;
|
|
208
212
|
export interface GetProjectPromptLabelInput {
|
|
209
|
-
projectId
|
|
213
|
+
projectId?: string;
|
|
210
214
|
label: string;
|
|
211
215
|
}
|
|
212
216
|
export type GetProjectPromptLabelResult = ProjectPromptLabel;
|
|
213
217
|
export interface UpdateProjectPromptLabelInput {
|
|
214
|
-
projectId
|
|
218
|
+
projectId?: string;
|
|
215
219
|
label: string;
|
|
216
220
|
isDefault?: boolean;
|
|
217
221
|
description?: string | null;
|
|
@@ -219,18 +223,18 @@ export interface UpdateProjectPromptLabelInput {
|
|
|
219
223
|
}
|
|
220
224
|
export type UpdateProjectPromptLabelResult = ProjectPromptLabel;
|
|
221
225
|
export interface DeleteProjectPromptLabelInput {
|
|
222
|
-
projectId
|
|
226
|
+
projectId?: string;
|
|
223
227
|
label: string;
|
|
224
228
|
}
|
|
225
229
|
export type DeleteProjectPromptLabelResult = ProjectPromptLabel;
|
|
226
230
|
export interface ListProjectPromptLabelUsagesInput {
|
|
227
|
-
projectId
|
|
231
|
+
projectId?: string;
|
|
228
232
|
label: string;
|
|
229
233
|
}
|
|
230
234
|
export type ListProjectPromptLabelUsagesResult = ProjectPromptLabelUsage[];
|
|
231
235
|
export type ListLabelEventsInput = {
|
|
232
236
|
scope: "label";
|
|
233
|
-
projectId
|
|
237
|
+
projectId?: string;
|
|
234
238
|
promptId: string;
|
|
235
239
|
label: string;
|
|
236
240
|
page?: number;
|
|
@@ -239,7 +243,7 @@ export type ListLabelEventsInput = {
|
|
|
239
243
|
until?: string;
|
|
240
244
|
} | {
|
|
241
245
|
scope: "prompt";
|
|
242
|
-
projectId
|
|
246
|
+
projectId?: string;
|
|
243
247
|
promptId: string;
|
|
244
248
|
label?: string;
|
|
245
249
|
page?: number;
|
|
@@ -248,7 +252,7 @@ export type ListLabelEventsInput = {
|
|
|
248
252
|
until?: string;
|
|
249
253
|
} | {
|
|
250
254
|
scope: "project";
|
|
251
|
-
projectId
|
|
255
|
+
projectId?: string;
|
|
252
256
|
promptId?: string;
|
|
253
257
|
label?: string;
|
|
254
258
|
page?: number;
|
package/dist/transport.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import type { BehaviorStore } from "./behaviors/store";
|
|
2
|
-
export
|
|
2
|
+
export interface AuthConfig {
|
|
3
3
|
readonly apiKey: string;
|
|
4
|
-
|
|
5
|
-
} | {
|
|
6
|
-
readonly getAccessToken: () => Promise<string>;
|
|
7
|
-
readonly apiKey?: never;
|
|
8
|
-
};
|
|
4
|
+
}
|
|
9
5
|
export interface TransportConfig {
|
|
10
6
|
readonly baseUrl: string;
|
|
11
7
|
readonly auth: AuthConfig;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@to11ai/sdk",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.4",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -44,14 +44,6 @@
|
|
|
44
44
|
"require": "./dist/projectEnvironments/resource.cjs",
|
|
45
45
|
"default": "./dist/projectEnvironments/resource.js"
|
|
46
46
|
},
|
|
47
|
-
"./routingRules": {
|
|
48
|
-
"types": "./dist/routingRules/resource.d.ts",
|
|
49
|
-
"bun": "./src/routingRules/resource.ts",
|
|
50
|
-
"deno": "./src/routingRules/resource.ts",
|
|
51
|
-
"import": "./dist/routingRules/resource.js",
|
|
52
|
-
"require": "./dist/routingRules/resource.cjs",
|
|
53
|
-
"default": "./dist/routingRules/resource.js"
|
|
54
|
-
},
|
|
55
47
|
"./errors": {
|
|
56
48
|
"types": "./dist/errors.d.ts",
|
|
57
49
|
"bun": "./src/errors.ts",
|
|
@@ -75,8 +67,8 @@
|
|
|
75
67
|
"scripts": {
|
|
76
68
|
"build": "bun run build:clean && bun run build:esm && bun run build:cjs && bun run build:types",
|
|
77
69
|
"build:clean": "rm -rf dist",
|
|
78
|
-
"build:esm": "bun build src/client.ts src/prompts/resource.ts src/projectEnvironments/resource.ts src/
|
|
79
|
-
"build:cjs": "bun build src/client.ts src/prompts/resource.ts src/projectEnvironments/resource.ts src/
|
|
70
|
+
"build:esm": "bun build src/client.ts src/prompts/resource.ts src/projectEnvironments/resource.ts src/errors.ts src/gateway.ts --outdir dist --target node --format esm --external '*'",
|
|
71
|
+
"build:cjs": "bun build src/client.ts src/prompts/resource.ts src/projectEnvironments/resource.ts src/errors.ts src/gateway.ts --outdir dist --target node --format cjs --external '*' --entry-naming '[dir]/[name].cjs'",
|
|
80
72
|
"build:types": "tsc -p tsconfig.build.json",
|
|
81
73
|
"prepack": "bun run build",
|
|
82
74
|
"codegen": "openapi-ts",
|
package/src/client.ts
CHANGED
|
@@ -8,46 +8,30 @@ import {
|
|
|
8
8
|
} from "./context";
|
|
9
9
|
import { gatewayAuthHeaders } from "./gateway";
|
|
10
10
|
import { ProjectEnvironmentResource } from "./projectEnvironments/resource";
|
|
11
|
-
import type { CacheConfig } from "./prompts/cache";
|
|
12
11
|
import { PromptResource } from "./prompts/resource";
|
|
13
12
|
import {
|
|
14
13
|
ENV_SLUG_REGEX,
|
|
15
14
|
type RenderFormat,
|
|
16
15
|
type UnknownRoleMode,
|
|
17
16
|
} from "./prompts/types";
|
|
18
|
-
import { RoutingRulesResource } from "./routingRules/resource";
|
|
19
17
|
import { Transport } from "./transport";
|
|
20
18
|
|
|
21
|
-
// SDK-behavior delivery (TO11-2100): the resolution store + helpers.
|
|
22
|
-
export {
|
|
23
|
-
BehaviorStore,
|
|
24
|
-
type BehaviorStoreOptions,
|
|
25
|
-
envVarNameForKey,
|
|
26
|
-
type SdkBehaviorEnvelope,
|
|
27
|
-
} from "./behaviors/store";
|
|
28
19
|
export type { TurnAuthContext } from "./context";
|
|
29
20
|
export { Conversation, Session, Turn } from "./context";
|
|
30
21
|
// Re-export all error types for convenience
|
|
31
22
|
export type {
|
|
32
|
-
AmbiguousEnvLabelsReason,
|
|
33
|
-
EnvNotFoundReason,
|
|
34
23
|
NoActiveReleaseReason,
|
|
35
|
-
NoApplicableRouteReason,
|
|
36
24
|
PromptNotFoundReason,
|
|
37
25
|
PromptPolicyViolationReason,
|
|
38
26
|
} from "./errors";
|
|
39
27
|
export {
|
|
40
28
|
// TO11-980 (S9) prompt-resolution typed errors.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
EnvNotFoundError,
|
|
29
|
+
LabelNotFoundError,
|
|
30
|
+
MissingLabelError,
|
|
44
31
|
NoActiveReleaseError,
|
|
45
|
-
NoApplicableRouteError,
|
|
46
32
|
PromptLabelNotRegisteredError,
|
|
47
33
|
PromptNotFoundError,
|
|
48
34
|
PromptPolicyViolationError,
|
|
49
|
-
RuleNotFoundError,
|
|
50
|
-
RuleVersionInvalidSnapshotError,
|
|
51
35
|
To11aiApiError,
|
|
52
36
|
To11aiAuthError,
|
|
53
37
|
To11aiError,
|
|
@@ -75,7 +59,6 @@ export type {
|
|
|
75
59
|
UpdateProjectEnvironmentResult,
|
|
76
60
|
} from "./projectEnvironments/resource";
|
|
77
61
|
export { ProjectEnvironmentResource } from "./projectEnvironments/resource";
|
|
78
|
-
export type { CacheConfig } from "./prompts/cache";
|
|
79
62
|
export type { PromptResourceConfig } from "./prompts/resource";
|
|
80
63
|
export { PromptResource } from "./prompts/resource";
|
|
81
64
|
export type {
|
|
@@ -92,42 +75,9 @@ export type {
|
|
|
92
75
|
RenderedPrompt,
|
|
93
76
|
RenderFormat,
|
|
94
77
|
RenderOptions,
|
|
95
|
-
ResolvedPrompt,
|
|
96
|
-
ResolvedPromptEnvelope,
|
|
97
78
|
UnknownRoleMode,
|
|
98
79
|
} from "./prompts/types";
|
|
99
80
|
export { ENV_SLUG_REGEX } from "./prompts/types";
|
|
100
|
-
export type {
|
|
101
|
-
CreateRuleVersionInput,
|
|
102
|
-
CreateRuleVersionResult,
|
|
103
|
-
GetRuleVersionInput,
|
|
104
|
-
GetRuleVersionResult,
|
|
105
|
-
ListRuleReleasesInput,
|
|
106
|
-
ListRuleReleasesResult,
|
|
107
|
-
ListRuleVersionsInput,
|
|
108
|
-
ListRuleVersionsResult,
|
|
109
|
-
RollbackRuleReleaseInput,
|
|
110
|
-
RollbackRuleReleaseResult,
|
|
111
|
-
RuleModelBinding,
|
|
112
|
-
RuleProviderRef,
|
|
113
|
-
RuleRelease,
|
|
114
|
-
RuleReleaseActorType,
|
|
115
|
-
RuleReleaseApprovalStatus,
|
|
116
|
-
RuleReleaseChangeKind,
|
|
117
|
-
RuleReleaseStrategy,
|
|
118
|
-
RuleReleaseWeight,
|
|
119
|
-
RuleSnapshot,
|
|
120
|
-
RuleStrategy,
|
|
121
|
-
RuleVersion,
|
|
122
|
-
SetRuleReleaseInput,
|
|
123
|
-
SetRuleReleaseResult,
|
|
124
|
-
} from "./routingRules/resource";
|
|
125
|
-
export {
|
|
126
|
-
RoutingRuleReleaseResource,
|
|
127
|
-
RoutingRulesResource,
|
|
128
|
-
RoutingRuleVersionResource,
|
|
129
|
-
} from "./routingRules/resource";
|
|
130
|
-
export type { Transport } from "./transport";
|
|
131
81
|
|
|
132
82
|
// ─── Client options ────────────────────────────────────────────────────────
|
|
133
83
|
|
|
@@ -143,12 +93,11 @@ export interface ClientOptions {
|
|
|
143
93
|
*/
|
|
144
94
|
gatewayUrl?: string;
|
|
145
95
|
/**
|
|
146
|
-
* API key for server-side use. Precedence: this
|
|
147
|
-
* Rejected in browser contexts —
|
|
96
|
+
* API key for server-side use (server, CLI, backend job). Precedence: this
|
|
97
|
+
* option → `TO11_API_KEY` env. Required. Rejected in browser contexts — a
|
|
98
|
+
* to11 API key carries gateway access and must never ship in client code.
|
|
148
99
|
*/
|
|
149
100
|
apiKey?: string;
|
|
150
|
-
/** Token callback for browser / edge contexts — avoids static secrets. */
|
|
151
|
-
getAccessToken?: () => Promise<string>;
|
|
152
101
|
/**
|
|
153
102
|
* Provider API key (BYOK) carried by `openaiOptions()` / `anthropicOptions()`
|
|
154
103
|
* as the provider client's `apiKey`, so a BYOK call is a single
|
|
@@ -183,17 +132,10 @@ export interface ClientOptions {
|
|
|
183
132
|
* via `render(slug, { onUnknownRole })`.
|
|
184
133
|
*/
|
|
185
134
|
onUnknownRole?: UnknownRoleMode;
|
|
186
|
-
/** LRU cache configuration for render(). Defaults: ttlSeconds=300, maxEntries=100 */
|
|
187
|
-
cache?: CacheConfig;
|
|
188
135
|
/** Request timeout in milliseconds. Default: 30_000 */
|
|
189
136
|
timeoutMs?: number;
|
|
190
137
|
/** Max retry attempts for network errors / 5xx. Default: 3 */
|
|
191
138
|
maxRetries?: number;
|
|
192
|
-
/**
|
|
193
|
-
* Purely local overrides for server-evaluated SDK-behavior flags (TO11-2100).
|
|
194
|
-
* An init option here wins over a `TO11_SDK_BEHAVIOR_*` env var. No LaunchDarkly.
|
|
195
|
-
*/
|
|
196
|
-
behaviorOverrides?: Record<string, unknown>;
|
|
197
139
|
}
|
|
198
140
|
|
|
199
141
|
// ─── Provider-client options (openaiOptions / anthropicOptions) ─────────────
|
|
@@ -230,21 +172,23 @@ export type ProviderClientOptions<
|
|
|
230
172
|
export interface To11aiClient<F extends RenderFormat | undefined = undefined> {
|
|
231
173
|
readonly prompts: PromptResource<F>;
|
|
232
174
|
readonly projectEnvironments: ProjectEnvironmentResource;
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
* var) → server-delivered value (captured from responses the client has
|
|
238
|
-
* already made) → the `codedDefault` passed here. A value whose runtime type
|
|
239
|
-
* disagrees with `codedDefault` falls through to the next tier. The SDK
|
|
240
|
-
* carries no LaunchDarkly dependency; this only reads what the server
|
|
241
|
-
* delivered on the envelope.
|
|
242
|
-
*/
|
|
243
|
-
readonly resolveBehavior: <T>(key: string, codedDefault: T) => T;
|
|
244
|
-
/** Resolved API key (server mode); `undefined` in browser (`getAccessToken`) mode. */
|
|
245
|
-
readonly apiKey: string | undefined;
|
|
175
|
+
/** Resolved API key (option → `TO11_API_KEY`). */
|
|
176
|
+
readonly apiKey: string;
|
|
177
|
+
/** Resolved control-plane base URL (option → `TO11_API_URL` → default). */
|
|
178
|
+
readonly baseUrl: string;
|
|
246
179
|
/** Resolved data-plane gateway URL (option → `TO11_GATEWAY_URL` → default). */
|
|
247
180
|
readonly gatewayUrl: string;
|
|
181
|
+
/**
|
|
182
|
+
* Resolved project id (option → `TO11_PROJECT_ID`); `undefined` when none is
|
|
183
|
+
* configured. Read-only, for your own logging / manual headers — `prompts.*`
|
|
184
|
+
* already default to it, so you don't need to pass it back into calls.
|
|
185
|
+
*/
|
|
186
|
+
readonly projectId: string | undefined;
|
|
187
|
+
/**
|
|
188
|
+
* Resolved default serving environment (option → `TO11_ENV`); `undefined`
|
|
189
|
+
* when none is configured. `prompts.render()` already defaults to it.
|
|
190
|
+
*/
|
|
191
|
+
readonly env: string | undefined;
|
|
248
192
|
/** A new session (unit of work). Pass an id to group telemetry by your own id. */
|
|
249
193
|
session(id?: string): Session;
|
|
250
194
|
/**
|
|
@@ -293,9 +237,8 @@ export interface To11aiClient<F extends RenderFormat | undefined = undefined> {
|
|
|
293
237
|
* - URLs (`baseUrl` / `gatewayUrl`) — option → env var → built-in default.
|
|
294
238
|
* - `apiKey` / `projectId` — option → env var (no default).
|
|
295
239
|
* - `env` / `format` — option only.
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
* - `getAccessToken` → browser-safe; token fetched per request.
|
|
240
|
+
* Authenticate with `apiKey` (or `TO11_API_KEY`) — required, and server-side
|
|
241
|
+
* only (server, CLI, backend job); constructing with `apiKey` in a browser throws.
|
|
299
242
|
*
|
|
300
243
|
* @example Minimal setup (single call)
|
|
301
244
|
* import { createClient } from "@to11ai/sdk";
|
|
@@ -365,15 +308,7 @@ export function createClient<
|
|
|
365
308
|
)
|
|
366
309
|
.replace(/\/+$/, "")
|
|
367
310
|
.replace(/\/v1$/, "");
|
|
368
|
-
const
|
|
369
|
-
// Explicit token auth suppresses an ambient `TO11_API_KEY` so a shared env
|
|
370
|
-
// (SSR / CI / edge) can't defeat an explicit `getAccessToken`. Only an
|
|
371
|
-
// *explicit* `options.apiKey` alongside `getAccessToken` stays ambiguous and
|
|
372
|
-
// is rejected by the exactly-one check below.
|
|
373
|
-
const apiKey =
|
|
374
|
-
getAccessToken !== undefined
|
|
375
|
-
? options.apiKey
|
|
376
|
-
: pick(options.apiKey, "TO11_API_KEY");
|
|
311
|
+
const apiKey = pick(options.apiKey, "TO11_API_KEY");
|
|
377
312
|
const projectId = pick(options.projectId, "TO11_PROJECT_ID");
|
|
378
313
|
// Default serving env: explicit option → `TO11_ENV` env var — the same
|
|
379
314
|
// `pick(option, "TO11_*")` resolution `apiKey`/`projectId` use (empty option
|
|
@@ -389,7 +324,7 @@ export function createClient<
|
|
|
389
324
|
// undefined in the browser, so only the explicit option can reach here.
|
|
390
325
|
if (isBrowser && options.apiKey !== undefined) {
|
|
391
326
|
throw new Error(
|
|
392
|
-
"[to11ai/sdk] Do not use `apiKey` in browser
|
|
327
|
+
"[to11ai/sdk] Do not use `apiKey` in a browser — a to11 API key carries gateway access and must stay server-side (server, CLI, or backend job).",
|
|
393
328
|
);
|
|
394
329
|
}
|
|
395
330
|
|
|
@@ -416,23 +351,19 @@ export function createClient<
|
|
|
416
351
|
);
|
|
417
352
|
}
|
|
418
353
|
|
|
419
|
-
//
|
|
420
|
-
|
|
421
|
-
const hasGetAccessToken = getAccessToken !== undefined;
|
|
422
|
-
if (hasApiKey === hasGetAccessToken) {
|
|
354
|
+
// `apiKey` is the only auth source and is required.
|
|
355
|
+
if (apiKey === undefined) {
|
|
423
356
|
throw new Error(
|
|
424
|
-
"[to11ai/sdk]
|
|
357
|
+
"[to11ai/sdk] An `apiKey` (or `TO11_API_KEY`) is required.",
|
|
425
358
|
);
|
|
426
359
|
}
|
|
427
360
|
|
|
428
|
-
const auth =
|
|
429
|
-
? ({ apiKey: apiKey as string } as const)
|
|
430
|
-
: ({ getAccessToken: getAccessToken as () => Promise<string> } as const);
|
|
361
|
+
const auth = { apiKey } as const;
|
|
431
362
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
});
|
|
363
|
+
// The SDK-behavior store (TO11-2100) still captures any server-delivered
|
|
364
|
+
// flags off responses, but the public `resolveBehavior` accessor is hidden
|
|
365
|
+
// for the 2.0 surface — kept internal so it can be re-exposed step by step.
|
|
366
|
+
const behaviorStore = new BehaviorStore({ env: processEnv });
|
|
436
367
|
|
|
437
368
|
const transport = new Transport({
|
|
438
369
|
baseUrl,
|
|
@@ -442,24 +373,18 @@ export function createClient<
|
|
|
442
373
|
behaviorStore,
|
|
443
374
|
});
|
|
444
375
|
|
|
445
|
-
const prompts = new PromptResource<F>(transport,
|
|
376
|
+
const prompts = new PromptResource<F>(transport, {
|
|
446
377
|
projectId,
|
|
447
378
|
env,
|
|
448
379
|
format: options.format,
|
|
449
380
|
onUnknownRole: options.onUnknownRole,
|
|
450
381
|
});
|
|
451
382
|
const projectEnvironments = new ProjectEnvironmentResource(transport);
|
|
452
|
-
const routingRules = new RoutingRulesResource(transport);
|
|
453
383
|
|
|
454
384
|
// ── Gateway observability context (session → conversation → turn) ──
|
|
455
|
-
// These build gateway auth headers, so they require a
|
|
456
|
-
//
|
|
385
|
+
// These build gateway auth headers, so they require a projectId; fail loudly
|
|
386
|
+
// rather than emit a broken header bag.
|
|
457
387
|
const gatewayAuthContext = (): TurnAuthContext => {
|
|
458
|
-
if (apiKey === undefined) {
|
|
459
|
-
throw new Error(
|
|
460
|
-
"[to11ai/sdk] session()/turn()/openaiOptions() need an `apiKey` (or `TO11_API_KEY`) — gateway calls require a server-side key. In the browser use `getAccessToken` for control-plane calls only.",
|
|
461
|
-
);
|
|
462
|
-
}
|
|
463
388
|
if (projectId === undefined) {
|
|
464
389
|
throw new Error(
|
|
465
390
|
"[to11ai/sdk] session()/turn() need a `projectId` (or `TO11_PROJECT_ID`).",
|
|
@@ -490,11 +415,6 @@ export function createClient<
|
|
|
490
415
|
defaultBaseURL: string,
|
|
491
416
|
overrides?: T,
|
|
492
417
|
): ProviderClientOptions<T> => {
|
|
493
|
-
if (apiKey === undefined) {
|
|
494
|
-
throw new Error(
|
|
495
|
-
"[to11ai/sdk] openaiOptions()/anthropicOptions() need an `apiKey` (or `TO11_API_KEY`) — gateway calls require a server-side key. In the browser use `getAccessToken` for control-plane calls only.",
|
|
496
|
-
);
|
|
497
|
-
}
|
|
498
418
|
if (projectId === undefined) {
|
|
499
419
|
throw new Error(
|
|
500
420
|
"[to11ai/sdk] openaiOptions()/anthropicOptions() need a `projectId` (or `TO11_PROJECT_ID`) — it authenticates the gateway call via `x-to11-project-id`.",
|
|
@@ -524,11 +444,11 @@ export function createClient<
|
|
|
524
444
|
return {
|
|
525
445
|
prompts,
|
|
526
446
|
projectEnvironments,
|
|
527
|
-
routingRules,
|
|
528
|
-
resolveBehavior: (key, codedDefault) =>
|
|
529
|
-
behaviorStore.resolve(key, codedDefault),
|
|
530
447
|
apiKey,
|
|
448
|
+
baseUrl,
|
|
531
449
|
gatewayUrl,
|
|
450
|
+
projectId,
|
|
451
|
+
env,
|
|
532
452
|
session: (id) => new Session(gatewayAuthContext(), id),
|
|
533
453
|
conversation: (id) => clientDefaultSession().conversation(id),
|
|
534
454
|
turn: () => clientDefaultSession().turn(),
|
package/src/context.ts
CHANGED
|
@@ -49,10 +49,6 @@ function newId(): string {
|
|
|
49
49
|
return crypto.randomUUID();
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
function provenanceOf(prompt: PromptForHeaders): GatewayPromptProvenance {
|
|
53
|
-
return "metadata" in prompt ? prompt.metadata : prompt;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
52
|
/**
|
|
57
53
|
* One agent turn — a single trace. A turn's `traceparent` is fixed for its
|
|
58
54
|
* lifetime, so every LLM-call made under it groups into one trace.
|
|
@@ -105,7 +101,9 @@ export class Turn {
|
|
|
105
101
|
traceparent: this.traceparent,
|
|
106
102
|
};
|
|
107
103
|
if (prompt !== undefined) {
|
|
108
|
-
|
|
104
|
+
// gatewayPromptHeaders accepts a RenderedPrompt or a format-shaped
|
|
105
|
+
// result and unwraps `metadata` itself.
|
|
106
|
+
Object.assign(headers, gatewayPromptHeaders(prompt));
|
|
109
107
|
}
|
|
110
108
|
return headers;
|
|
111
109
|
}
|