@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
|
@@ -42,13 +42,11 @@ __export(exports_resource, {
|
|
|
42
42
|
PromptResource: () => PromptResource
|
|
43
43
|
});
|
|
44
44
|
module.exports = __toCommonJS(exports_resource);
|
|
45
|
-
var import_zod = require("zod");
|
|
46
45
|
var import_errors = require("../errors");
|
|
47
|
-
var
|
|
48
|
-
var import_cache = require("./cache");
|
|
46
|
+
var import_zod = require("../generated/zod.gen");
|
|
49
47
|
var import_shape = require("./shape");
|
|
50
48
|
var import_types = require("./types");
|
|
51
|
-
var zResolvedPromptEnvelope =
|
|
49
|
+
var zResolvedPromptEnvelope = import_zod.zResolvedPrompt.partial({ releaseId: true });
|
|
52
50
|
function unwrap(envelope) {
|
|
53
51
|
if (envelope.data === null || envelope.data === undefined) {
|
|
54
52
|
throw new Error("API returned success but data is null");
|
|
@@ -128,26 +126,33 @@ function buildLabelEventsQuery(input) {
|
|
|
128
126
|
|
|
129
127
|
class PromptResource {
|
|
130
128
|
transport;
|
|
131
|
-
cache;
|
|
132
129
|
projectId;
|
|
133
130
|
env;
|
|
134
131
|
format;
|
|
135
132
|
onUnknownRole;
|
|
136
|
-
constructor(transport,
|
|
133
|
+
constructor(transport, config) {
|
|
137
134
|
this.transport = transport;
|
|
138
|
-
this.cache = new import_cache.PromptCache(cacheConfig);
|
|
139
135
|
this.projectId = config?.projectId;
|
|
140
136
|
this.env = config?.env;
|
|
141
137
|
this.format = config?.format;
|
|
142
138
|
this.onUnknownRole = config?.onUnknownRole;
|
|
143
139
|
}
|
|
140
|
+
resolveProjectId(perCall) {
|
|
141
|
+
const projectId = perCall ?? this.projectId;
|
|
142
|
+
if (projectId === undefined || projectId.trim() === "") {
|
|
143
|
+
throw new Error("[to11ai/sdk] this call needs a `projectId` — pass it on the call or bind one via createClient({ projectId, ... }).");
|
|
144
|
+
}
|
|
145
|
+
return projectId.trim();
|
|
146
|
+
}
|
|
144
147
|
async create(input) {
|
|
145
|
-
const { projectId, ...body } = input;
|
|
148
|
+
const { projectId: perCallProjectId, ...body } = input;
|
|
149
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
146
150
|
const envelope = await this.transport.request("POST", getProjectPromptsPath(projectId), { body });
|
|
147
151
|
return unwrap(envelope);
|
|
148
152
|
}
|
|
149
|
-
async list(input) {
|
|
150
|
-
const { projectId, ...queryRest } = input;
|
|
153
|
+
async list(input = {}) {
|
|
154
|
+
const { projectId: perCallProjectId, ...queryRest } = input;
|
|
155
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
151
156
|
const envelope = await this.transport.request("GET", getProjectPromptsPath(projectId), {
|
|
152
157
|
query: {
|
|
153
158
|
...queryRest
|
|
@@ -164,42 +169,50 @@ class PromptResource {
|
|
|
164
169
|
return { items, pagination };
|
|
165
170
|
}
|
|
166
171
|
async get(input) {
|
|
167
|
-
const { projectId, promptId } = input;
|
|
172
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
173
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
168
174
|
const envelope = await this.transport.request("GET", getProjectPromptPath(projectId, promptId));
|
|
169
175
|
return unwrap(envelope);
|
|
170
176
|
}
|
|
171
177
|
async update(input) {
|
|
172
|
-
const { projectId, promptId, ...body } = input;
|
|
178
|
+
const { projectId: perCallProjectId, promptId, ...body } = input;
|
|
179
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
173
180
|
const envelope = await this.transport.request("PATCH", getProjectPromptPath(projectId, promptId), { body });
|
|
174
181
|
return unwrap(envelope);
|
|
175
182
|
}
|
|
176
183
|
async archive(input) {
|
|
177
|
-
const { projectId, promptId } = input;
|
|
184
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
185
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
178
186
|
const envelope = await this.transport.request("DELETE", getProjectPromptPath(projectId, promptId));
|
|
179
187
|
return unwrap(envelope);
|
|
180
188
|
}
|
|
181
189
|
async createVersion(input) {
|
|
182
|
-
const { projectId, promptId, ...body } = input;
|
|
190
|
+
const { projectId: perCallProjectId, promptId, ...body } = input;
|
|
191
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
183
192
|
const envelope = await this.transport.request("POST", `${getProjectPromptPath(projectId, promptId)}/versions`, { body });
|
|
184
193
|
return unwrap(envelope);
|
|
185
194
|
}
|
|
186
195
|
async listVersions(input) {
|
|
187
|
-
const { projectId, promptId } = input;
|
|
196
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
197
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
188
198
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/versions`);
|
|
189
199
|
return unwrap(envelope);
|
|
190
200
|
}
|
|
191
201
|
async getVersion(input) {
|
|
192
|
-
const { projectId, promptId, versionNumber } = input;
|
|
202
|
+
const { projectId: perCallProjectId, promptId, versionNumber } = input;
|
|
203
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
193
204
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/versions/${encodeURIComponent(String(versionNumber))}`);
|
|
194
205
|
return unwrap(envelope);
|
|
195
206
|
}
|
|
196
207
|
async moveLabel(input) {
|
|
197
|
-
const { projectId, promptId, label, ...body } = input;
|
|
208
|
+
const { projectId: perCallProjectId, promptId, label, ...body } = input;
|
|
209
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
198
210
|
const envelope = await this.transport.request("PUT", `${getProjectPromptPath(projectId, promptId)}/labels/${encodeURIComponent(label)}`, { body });
|
|
199
211
|
return unwrap(envelope);
|
|
200
212
|
}
|
|
201
213
|
async listLabels(input) {
|
|
202
|
-
const { projectId, promptId } = input;
|
|
214
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
215
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
203
216
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/labels`);
|
|
204
217
|
return unwrap(envelope);
|
|
205
218
|
}
|
|
@@ -213,21 +226,15 @@ class PromptResource {
|
|
|
213
226
|
if (this.projectId === undefined || this.projectId.trim() === "") {
|
|
214
227
|
throw new Error("[to11ai/sdk] `prompts.render()` requires a `projectId` on the client — pass it to createClient({ projectId, ... }).");
|
|
215
228
|
}
|
|
216
|
-
const
|
|
217
|
-
if (
|
|
218
|
-
throw import_errors.
|
|
229
|
+
const label = options.label ?? this.env;
|
|
230
|
+
if (label === undefined) {
|
|
231
|
+
throw new import_errors.MissingLabelError;
|
|
219
232
|
}
|
|
220
|
-
if (!import_types.ENV_SLUG_REGEX.test(
|
|
221
|
-
throw new Error(`[to11ai/sdk] \`
|
|
233
|
+
if (!import_types.ENV_SLUG_REGEX.test(label)) {
|
|
234
|
+
throw new Error(`[to11ai/sdk] \`label\` must match ${import_types.ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(label)}`);
|
|
222
235
|
}
|
|
223
|
-
const { subject, variables,
|
|
224
|
-
const
|
|
225
|
-
if (useCache) {
|
|
226
|
-
const cached = this.cache.get(cacheKey);
|
|
227
|
-
if (cached)
|
|
228
|
-
return structuredClone(cached);
|
|
229
|
-
}
|
|
230
|
-
const body = { slug, labels: [env] };
|
|
236
|
+
const { subject, variables, fallback } = options;
|
|
237
|
+
const body = { slug, labels: [label] };
|
|
231
238
|
if (subject !== undefined)
|
|
232
239
|
body.subject = subject;
|
|
233
240
|
if (variables !== undefined)
|
|
@@ -238,38 +245,37 @@ class PromptResource {
|
|
|
238
245
|
decode: (data) => zResolvedPromptEnvelope.parse(data)
|
|
239
246
|
});
|
|
240
247
|
const raw = unwrap(envelope);
|
|
241
|
-
|
|
242
|
-
const isWeighted = raw.strategy === "weighted" || result.variantName !== undefined;
|
|
243
|
-
const cacheable = !(isWeighted && subject === undefined);
|
|
244
|
-
if (useCache && cacheable) {
|
|
245
|
-
this.cache.set(cacheKey, structuredClone(result));
|
|
246
|
-
}
|
|
247
|
-
return result;
|
|
248
|
+
return normalizeResolvedEnvelope(raw, slug);
|
|
248
249
|
} catch (err) {
|
|
249
|
-
|
|
250
|
+
const transient = err instanceof import_errors.To11aiNetworkError || err instanceof import_errors.To11aiApiError && err.statusCode >= 500;
|
|
251
|
+
if (transient && fallback !== undefined) {
|
|
250
252
|
return liftFallbackToRendered(fallback, slug);
|
|
251
253
|
}
|
|
252
254
|
throw err;
|
|
253
255
|
}
|
|
254
256
|
}
|
|
255
|
-
async listProjectLabels(input) {
|
|
256
|
-
const { projectId, includeArchived } = input;
|
|
257
|
+
async listProjectLabels(input = {}) {
|
|
258
|
+
const { projectId: perCallProjectId, includeArchived } = input;
|
|
259
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
257
260
|
const envelope = await this.transport.request("GET", getProjectPromptLabelsPath(projectId), {
|
|
258
261
|
query: includeArchived ? { includeArchived: "true" } : undefined
|
|
259
262
|
});
|
|
260
263
|
return unwrap(envelope);
|
|
261
264
|
}
|
|
262
265
|
async createProjectLabel(input) {
|
|
263
|
-
const { projectId, ...body } = input;
|
|
266
|
+
const { projectId: perCallProjectId, ...body } = input;
|
|
267
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
264
268
|
const envelope = await this.transport.request("POST", getProjectPromptLabelsPath(projectId), { body });
|
|
265
269
|
return unwrap(envelope);
|
|
266
270
|
}
|
|
267
271
|
async getProjectLabel(input) {
|
|
268
|
-
const
|
|
272
|
+
const projectId = this.resolveProjectId(input.projectId);
|
|
273
|
+
const envelope = await this.transport.request("GET", getProjectPromptLabelPath(projectId, input.label));
|
|
269
274
|
return unwrap(envelope);
|
|
270
275
|
}
|
|
271
276
|
async updateProjectLabel(input) {
|
|
272
|
-
const { projectId, label, ...rest } = input;
|
|
277
|
+
const { projectId: perCallProjectId, label, ...rest } = input;
|
|
278
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
273
279
|
const body = {};
|
|
274
280
|
if (rest.isDefault !== undefined)
|
|
275
281
|
body.isDefault = rest.isDefault;
|
|
@@ -281,15 +287,16 @@ class PromptResource {
|
|
|
281
287
|
return unwrap(envelope);
|
|
282
288
|
}
|
|
283
289
|
async deleteProjectLabel(input) {
|
|
284
|
-
const
|
|
290
|
+
const projectId = this.resolveProjectId(input.projectId);
|
|
291
|
+
const envelope = await this.transport.request("DELETE", getProjectPromptLabelPath(projectId, input.label));
|
|
285
292
|
return unwrap(envelope);
|
|
286
293
|
}
|
|
287
294
|
async listProjectLabelUsages(input) {
|
|
288
|
-
const envelope = await this.transport.request("GET", `${getProjectPromptLabelPath(input.projectId, input.label)}/usages`);
|
|
295
|
+
const envelope = await this.transport.request("GET", `${getProjectPromptLabelPath(this.resolveProjectId(input.projectId), input.label)}/usages`);
|
|
289
296
|
return unwrap(envelope);
|
|
290
297
|
}
|
|
291
298
|
async listLabelEvents(input) {
|
|
292
|
-
const projectSeg = encodeURIComponent(input.projectId);
|
|
299
|
+
const projectSeg = encodeURIComponent(this.resolveProjectId(input.projectId));
|
|
293
300
|
let path;
|
|
294
301
|
switch (input.scope) {
|
|
295
302
|
case "label":
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type { Transport } from "../transport";
|
|
2
|
-
import type { CacheConfig } from "./cache";
|
|
3
2
|
import type { AnthropicRenderedPrompt, OpenAiRenderedPrompt, RenderResult } from "./shape";
|
|
4
3
|
import type { ArchivePromptInput, ArchivePromptResult, CreateProjectPromptLabelInput, CreateProjectPromptLabelResult, CreatePromptInput, CreatePromptResult, CreatePromptVersionInput, CreatePromptVersionResult, DeleteProjectPromptLabelInput, DeleteProjectPromptLabelResult, GetProjectPromptLabelInput, GetProjectPromptLabelResult, GetPromptInput, GetPromptResult, GetPromptVersionInput, GetPromptVersionResult, ListLabelEventsInput, ListLabelEventsResult, ListProjectPromptLabelsInput, ListProjectPromptLabelsResult, ListProjectPromptLabelUsagesInput, ListProjectPromptLabelUsagesResult, ListPromptLabelsInput, ListPromptLabelsResult, ListPromptsInput, ListPromptsResult, ListPromptVersionsInput, ListPromptVersionsResult, MovePromptLabelInput, MovePromptLabelResult, PaginatedResult, RenderFormat, RenderOptions, UnknownRoleMode, UpdateProjectPromptLabelInput, UpdateProjectPromptLabelResult, UpdatePromptInput, UpdatePromptResult } from "./types";
|
|
5
|
-
export type {
|
|
6
|
-
export type { ArchivePromptInput, ArchivePromptResult, BlockRenderRecord, ChatMessage, CreateProjectPromptLabelInput, CreateProjectPromptLabelResult, CreatePromptInput, CreatePromptResult, CreatePromptVersionInput, CreatePromptVersionResult, DeleteProjectPromptLabelInput, DeleteProjectPromptLabelResult, GetProjectPromptLabelInput, GetProjectPromptLabelResult, GetPromptInput, GetPromptResult, GetPromptVersionInput, GetPromptVersionResult, ListLabelEventsInput, ListLabelEventsResult, ListProjectPromptLabelsInput, ListProjectPromptLabelsResult, ListProjectPromptLabelUsagesInput, ListProjectPromptLabelUsagesResult, ListPromptLabelsInput, ListPromptLabelsResult, ListPromptsInput, ListPromptsResult, ListPromptVersionsInput, ListPromptVersionsResult, MovePromptLabelInput, MovePromptLabelResult, PaginatedResult, PaginationInfo, PaletteToken, ProjectPromptLabel, ProjectPromptLabelUsage, Prompt, PromptFormat, PromptLabelEvent, PromptLabelPointer, PromptStatus, PromptVersion, RenderedChatMessage, RenderedPrompt, RenderFormat, RenderOptions, ResolvedPrompt, ResolvedPromptEnvelope, ResolvedToolCall, ResolvedToolDefinition, RowError, TextChatMessage, TextChatRole, UnknownRoleMode, UpdateProjectPromptLabelInput, UpdateProjectPromptLabelResult, UpdatePromptInput, UpdatePromptResult, } from "./types";
|
|
4
|
+
export type { ArchivePromptInput, ArchivePromptResult, BlockRenderRecord, ChatMessage, ChatRole, CreateProjectPromptLabelInput, CreateProjectPromptLabelResult, CreatePromptInput, CreatePromptResult, CreatePromptVersionInput, CreatePromptVersionResult, DeleteProjectPromptLabelInput, DeleteProjectPromptLabelResult, GetProjectPromptLabelInput, GetProjectPromptLabelResult, GetPromptInput, GetPromptResult, GetPromptVersionInput, GetPromptVersionResult, ListLabelEventsInput, ListLabelEventsResult, ListProjectPromptLabelsInput, ListProjectPromptLabelsResult, ListProjectPromptLabelUsagesInput, ListProjectPromptLabelUsagesResult, ListPromptLabelsInput, ListPromptLabelsResult, ListPromptsInput, ListPromptsResult, ListPromptVersionsInput, ListPromptVersionsResult, MovePromptLabelInput, MovePromptLabelResult, PaginatedResult, PaginationInfo, PaletteToken, ProjectPromptLabel, ProjectPromptLabelUsage, Prompt, PromptFormat, PromptLabelEvent, PromptLabelPointer, PromptStatus, PromptVersion, RenderedChatMessage, RenderedPrompt, RenderFormat, RenderOptions, ResolvedPrompt, ResolvedPromptEnvelope, ResolvedToolCall, ResolvedToolDefinition, RowError, UnknownRoleMode, UpdateProjectPromptLabelInput, UpdateProjectPromptLabelResult, UpdatePromptInput, UpdatePromptResult, } from "./types";
|
|
7
5
|
/**
|
|
8
6
|
* Init-time config for the env-aware `render()` surface (TO11-980 / S9).
|
|
9
7
|
*
|
|
@@ -26,16 +24,22 @@ export interface PromptResourceConfig {
|
|
|
26
24
|
}
|
|
27
25
|
export declare class PromptResource<F extends RenderFormat | undefined = undefined> {
|
|
28
26
|
private readonly transport;
|
|
29
|
-
private readonly cache;
|
|
30
27
|
private readonly projectId;
|
|
31
28
|
private readonly env;
|
|
32
29
|
private readonly format;
|
|
33
30
|
private readonly onUnknownRole;
|
|
34
|
-
constructor(transport: Transport,
|
|
31
|
+
constructor(transport: Transport, config?: Omit<PromptResourceConfig, "format"> & {
|
|
35
32
|
format?: F;
|
|
36
33
|
});
|
|
34
|
+
/**
|
|
35
|
+
* Resolve the project for a control-plane call: the per-call `projectId`
|
|
36
|
+
* wins, else the client-bound one from `createClient({ projectId })`. Throws
|
|
37
|
+
* if neither is set. Mirrors how `render()` resolves its project, so every
|
|
38
|
+
* method honors the bound `projectId` and callers need not repeat it.
|
|
39
|
+
*/
|
|
40
|
+
private resolveProjectId;
|
|
37
41
|
create(input: CreatePromptInput): Promise<CreatePromptResult>;
|
|
38
|
-
list(input
|
|
42
|
+
list(input?: ListPromptsInput): Promise<PaginatedResult<ListPromptsResult>>;
|
|
39
43
|
get(input: GetPromptInput): Promise<GetPromptResult>;
|
|
40
44
|
update(input: UpdatePromptInput): Promise<UpdatePromptResult>;
|
|
41
45
|
archive(input: ArchivePromptInput): Promise<ArchivePromptResult>;
|
|
@@ -69,17 +73,14 @@ export declare class PromptResource<F extends RenderFormat | undefined = undefin
|
|
|
69
73
|
render(slug: string, options?: RenderOptions): Promise<RenderResult<F>>;
|
|
70
74
|
/**
|
|
71
75
|
* Env-aware resolution → neutral `RenderedPrompt`. Carries the resolve /
|
|
72
|
-
*
|
|
76
|
+
* offline-fallback logic; `render()` is the public shaping surface.
|
|
73
77
|
*
|
|
74
|
-
* - **
|
|
78
|
+
* - **label wire mapping:** the resolve route is label-based
|
|
75
79
|
* (`/v1/projects/:projectId/prompts:resolve`, body `labels[]`); the
|
|
76
|
-
* resolved
|
|
77
|
-
* - **No silent fallback (R4):** if no
|
|
78
|
-
*
|
|
79
|
-
* before any transport call.
|
|
80
|
-
* - **Cache (D8):** caches `single`-strategy releases and weighted releases
|
|
81
|
-
* WITH a `subject`; BYPASSES the cache for weighted releases with NO
|
|
82
|
-
* `subject` (a uniform-random assignment must not be pinned for the TTL).
|
|
80
|
+
* resolved label is sent as the single resolution label.
|
|
81
|
+
* - **No silent fallback (R4):** if no label is resolvable (none per call
|
|
82
|
+
* AND no client `env` fallback) this throws `MissingLabelError`
|
|
83
|
+
* CLIENT-side, eagerly, before any transport call.
|
|
83
84
|
*/
|
|
84
85
|
private resolveToRendered;
|
|
85
86
|
/**
|
|
@@ -89,7 +90,7 @@ export declare class PromptResource<F extends RenderFormat | undefined = undefin
|
|
|
89
90
|
* include rows with a non-null `archivedAt` (useful for admin / history
|
|
90
91
|
* affordances). Gated on `prompt:read` server-side.
|
|
91
92
|
*/
|
|
92
|
-
listProjectLabels(input
|
|
93
|
+
listProjectLabels(input?: ListProjectPromptLabelsInput): Promise<ListProjectPromptLabelsResult>;
|
|
93
94
|
/**
|
|
94
95
|
* Create a new project label.
|
|
95
96
|
*
|
package/dist/prompts/resource.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// src/prompts/resource.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
MissingLabelError,
|
|
4
|
+
To11aiApiError,
|
|
5
|
+
To11aiNetworkError
|
|
6
|
+
} from "../errors";
|
|
4
7
|
import { zResolvedPrompt } from "../generated/zod.gen";
|
|
5
|
-
import { buildRenderCacheKey, PromptCache } from "./cache";
|
|
6
8
|
import { shapePrompt } from "./shape";
|
|
7
9
|
import { ENV_SLUG_REGEX } from "./types";
|
|
8
|
-
var zResolvedPromptEnvelope = zResolvedPrompt.partial({ releaseId: true })
|
|
10
|
+
var zResolvedPromptEnvelope = zResolvedPrompt.partial({ releaseId: true });
|
|
9
11
|
function unwrap(envelope) {
|
|
10
12
|
if (envelope.data === null || envelope.data === undefined) {
|
|
11
13
|
throw new Error("API returned success but data is null");
|
|
@@ -85,26 +87,33 @@ function buildLabelEventsQuery(input) {
|
|
|
85
87
|
|
|
86
88
|
class PromptResource {
|
|
87
89
|
transport;
|
|
88
|
-
cache;
|
|
89
90
|
projectId;
|
|
90
91
|
env;
|
|
91
92
|
format;
|
|
92
93
|
onUnknownRole;
|
|
93
|
-
constructor(transport,
|
|
94
|
+
constructor(transport, config) {
|
|
94
95
|
this.transport = transport;
|
|
95
|
-
this.cache = new PromptCache(cacheConfig);
|
|
96
96
|
this.projectId = config?.projectId;
|
|
97
97
|
this.env = config?.env;
|
|
98
98
|
this.format = config?.format;
|
|
99
99
|
this.onUnknownRole = config?.onUnknownRole;
|
|
100
100
|
}
|
|
101
|
+
resolveProjectId(perCall) {
|
|
102
|
+
const projectId = perCall ?? this.projectId;
|
|
103
|
+
if (projectId === undefined || projectId.trim() === "") {
|
|
104
|
+
throw new Error("[to11ai/sdk] this call needs a `projectId` — pass it on the call or bind one via createClient({ projectId, ... }).");
|
|
105
|
+
}
|
|
106
|
+
return projectId.trim();
|
|
107
|
+
}
|
|
101
108
|
async create(input) {
|
|
102
|
-
const { projectId, ...body } = input;
|
|
109
|
+
const { projectId: perCallProjectId, ...body } = input;
|
|
110
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
103
111
|
const envelope = await this.transport.request("POST", getProjectPromptsPath(projectId), { body });
|
|
104
112
|
return unwrap(envelope);
|
|
105
113
|
}
|
|
106
|
-
async list(input) {
|
|
107
|
-
const { projectId, ...queryRest } = input;
|
|
114
|
+
async list(input = {}) {
|
|
115
|
+
const { projectId: perCallProjectId, ...queryRest } = input;
|
|
116
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
108
117
|
const envelope = await this.transport.request("GET", getProjectPromptsPath(projectId), {
|
|
109
118
|
query: {
|
|
110
119
|
...queryRest
|
|
@@ -121,42 +130,50 @@ class PromptResource {
|
|
|
121
130
|
return { items, pagination };
|
|
122
131
|
}
|
|
123
132
|
async get(input) {
|
|
124
|
-
const { projectId, promptId } = input;
|
|
133
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
134
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
125
135
|
const envelope = await this.transport.request("GET", getProjectPromptPath(projectId, promptId));
|
|
126
136
|
return unwrap(envelope);
|
|
127
137
|
}
|
|
128
138
|
async update(input) {
|
|
129
|
-
const { projectId, promptId, ...body } = input;
|
|
139
|
+
const { projectId: perCallProjectId, promptId, ...body } = input;
|
|
140
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
130
141
|
const envelope = await this.transport.request("PATCH", getProjectPromptPath(projectId, promptId), { body });
|
|
131
142
|
return unwrap(envelope);
|
|
132
143
|
}
|
|
133
144
|
async archive(input) {
|
|
134
|
-
const { projectId, promptId } = input;
|
|
145
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
146
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
135
147
|
const envelope = await this.transport.request("DELETE", getProjectPromptPath(projectId, promptId));
|
|
136
148
|
return unwrap(envelope);
|
|
137
149
|
}
|
|
138
150
|
async createVersion(input) {
|
|
139
|
-
const { projectId, promptId, ...body } = input;
|
|
151
|
+
const { projectId: perCallProjectId, promptId, ...body } = input;
|
|
152
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
140
153
|
const envelope = await this.transport.request("POST", `${getProjectPromptPath(projectId, promptId)}/versions`, { body });
|
|
141
154
|
return unwrap(envelope);
|
|
142
155
|
}
|
|
143
156
|
async listVersions(input) {
|
|
144
|
-
const { projectId, promptId } = input;
|
|
157
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
158
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
145
159
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/versions`);
|
|
146
160
|
return unwrap(envelope);
|
|
147
161
|
}
|
|
148
162
|
async getVersion(input) {
|
|
149
|
-
const { projectId, promptId, versionNumber } = input;
|
|
163
|
+
const { projectId: perCallProjectId, promptId, versionNumber } = input;
|
|
164
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
150
165
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/versions/${encodeURIComponent(String(versionNumber))}`);
|
|
151
166
|
return unwrap(envelope);
|
|
152
167
|
}
|
|
153
168
|
async moveLabel(input) {
|
|
154
|
-
const { projectId, promptId, label, ...body } = input;
|
|
169
|
+
const { projectId: perCallProjectId, promptId, label, ...body } = input;
|
|
170
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
155
171
|
const envelope = await this.transport.request("PUT", `${getProjectPromptPath(projectId, promptId)}/labels/${encodeURIComponent(label)}`, { body });
|
|
156
172
|
return unwrap(envelope);
|
|
157
173
|
}
|
|
158
174
|
async listLabels(input) {
|
|
159
|
-
const { projectId, promptId } = input;
|
|
175
|
+
const { projectId: perCallProjectId, promptId } = input;
|
|
176
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
160
177
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/labels`);
|
|
161
178
|
return unwrap(envelope);
|
|
162
179
|
}
|
|
@@ -170,21 +187,15 @@ class PromptResource {
|
|
|
170
187
|
if (this.projectId === undefined || this.projectId.trim() === "") {
|
|
171
188
|
throw new Error("[to11ai/sdk] `prompts.render()` requires a `projectId` on the client — pass it to createClient({ projectId, ... }).");
|
|
172
189
|
}
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
175
|
-
throw
|
|
190
|
+
const label = options.label ?? this.env;
|
|
191
|
+
if (label === undefined) {
|
|
192
|
+
throw new MissingLabelError;
|
|
176
193
|
}
|
|
177
|
-
if (!ENV_SLUG_REGEX.test(
|
|
178
|
-
throw new Error(`[to11ai/sdk] \`
|
|
194
|
+
if (!ENV_SLUG_REGEX.test(label)) {
|
|
195
|
+
throw new Error(`[to11ai/sdk] \`label\` must match ${ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(label)}`);
|
|
179
196
|
}
|
|
180
|
-
const { subject, variables,
|
|
181
|
-
const
|
|
182
|
-
if (useCache) {
|
|
183
|
-
const cached = this.cache.get(cacheKey);
|
|
184
|
-
if (cached)
|
|
185
|
-
return structuredClone(cached);
|
|
186
|
-
}
|
|
187
|
-
const body = { slug, labels: [env] };
|
|
197
|
+
const { subject, variables, fallback } = options;
|
|
198
|
+
const body = { slug, labels: [label] };
|
|
188
199
|
if (subject !== undefined)
|
|
189
200
|
body.subject = subject;
|
|
190
201
|
if (variables !== undefined)
|
|
@@ -195,38 +206,37 @@ class PromptResource {
|
|
|
195
206
|
decode: (data) => zResolvedPromptEnvelope.parse(data)
|
|
196
207
|
});
|
|
197
208
|
const raw = unwrap(envelope);
|
|
198
|
-
|
|
199
|
-
const isWeighted = raw.strategy === "weighted" || result.variantName !== undefined;
|
|
200
|
-
const cacheable = !(isWeighted && subject === undefined);
|
|
201
|
-
if (useCache && cacheable) {
|
|
202
|
-
this.cache.set(cacheKey, structuredClone(result));
|
|
203
|
-
}
|
|
204
|
-
return result;
|
|
209
|
+
return normalizeResolvedEnvelope(raw, slug);
|
|
205
210
|
} catch (err) {
|
|
206
|
-
|
|
211
|
+
const transient = err instanceof To11aiNetworkError || err instanceof To11aiApiError && err.statusCode >= 500;
|
|
212
|
+
if (transient && fallback !== undefined) {
|
|
207
213
|
return liftFallbackToRendered(fallback, slug);
|
|
208
214
|
}
|
|
209
215
|
throw err;
|
|
210
216
|
}
|
|
211
217
|
}
|
|
212
|
-
async listProjectLabels(input) {
|
|
213
|
-
const { projectId, includeArchived } = input;
|
|
218
|
+
async listProjectLabels(input = {}) {
|
|
219
|
+
const { projectId: perCallProjectId, includeArchived } = input;
|
|
220
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
214
221
|
const envelope = await this.transport.request("GET", getProjectPromptLabelsPath(projectId), {
|
|
215
222
|
query: includeArchived ? { includeArchived: "true" } : undefined
|
|
216
223
|
});
|
|
217
224
|
return unwrap(envelope);
|
|
218
225
|
}
|
|
219
226
|
async createProjectLabel(input) {
|
|
220
|
-
const { projectId, ...body } = input;
|
|
227
|
+
const { projectId: perCallProjectId, ...body } = input;
|
|
228
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
221
229
|
const envelope = await this.transport.request("POST", getProjectPromptLabelsPath(projectId), { body });
|
|
222
230
|
return unwrap(envelope);
|
|
223
231
|
}
|
|
224
232
|
async getProjectLabel(input) {
|
|
225
|
-
const
|
|
233
|
+
const projectId = this.resolveProjectId(input.projectId);
|
|
234
|
+
const envelope = await this.transport.request("GET", getProjectPromptLabelPath(projectId, input.label));
|
|
226
235
|
return unwrap(envelope);
|
|
227
236
|
}
|
|
228
237
|
async updateProjectLabel(input) {
|
|
229
|
-
const { projectId, label, ...rest } = input;
|
|
238
|
+
const { projectId: perCallProjectId, label, ...rest } = input;
|
|
239
|
+
const projectId = this.resolveProjectId(perCallProjectId);
|
|
230
240
|
const body = {};
|
|
231
241
|
if (rest.isDefault !== undefined)
|
|
232
242
|
body.isDefault = rest.isDefault;
|
|
@@ -238,15 +248,16 @@ class PromptResource {
|
|
|
238
248
|
return unwrap(envelope);
|
|
239
249
|
}
|
|
240
250
|
async deleteProjectLabel(input) {
|
|
241
|
-
const
|
|
251
|
+
const projectId = this.resolveProjectId(input.projectId);
|
|
252
|
+
const envelope = await this.transport.request("DELETE", getProjectPromptLabelPath(projectId, input.label));
|
|
242
253
|
return unwrap(envelope);
|
|
243
254
|
}
|
|
244
255
|
async listProjectLabelUsages(input) {
|
|
245
|
-
const envelope = await this.transport.request("GET", `${getProjectPromptLabelPath(input.projectId, input.label)}/usages`);
|
|
256
|
+
const envelope = await this.transport.request("GET", `${getProjectPromptLabelPath(this.resolveProjectId(input.projectId), input.label)}/usages`);
|
|
246
257
|
return unwrap(envelope);
|
|
247
258
|
}
|
|
248
259
|
async listLabelEvents(input) {
|
|
249
|
-
const projectSeg = encodeURIComponent(input.projectId);
|
|
260
|
+
const projectSeg = encodeURIComponent(this.resolveProjectId(input.projectId));
|
|
250
261
|
let path;
|
|
251
262
|
switch (input.scope) {
|
|
252
263
|
case "label":
|
package/dist/prompts/shape.d.ts
CHANGED
|
@@ -52,5 +52,5 @@ export interface AnthropicRenderedPrompt {
|
|
|
52
52
|
export type RenderResult<F extends RenderFormat | undefined> = F extends "openai" ? OpenAiRenderedPrompt : F extends "anthropic" ? AnthropicRenderedPrompt : RenderedPrompt;
|
|
53
53
|
export declare function shapeOpenAI(prompt: RenderedPrompt, options?: ConvertMessagesOptions): OpenAiRenderedPrompt;
|
|
54
54
|
export declare function shapeAnthropic(prompt: RenderedPrompt, options?: ConvertMessagesOptions): AnthropicRenderedPrompt;
|
|
55
|
-
/** Shape a
|
|
55
|
+
/** Shape a rendered prompt for `format`, or return it neutral when unset. */
|
|
56
56
|
export declare function shapePrompt(prompt: RenderedPrompt, format: RenderFormat | undefined, options?: ConvertMessagesOptions): RenderedPrompt | OpenAiRenderedPrompt | AnthropicRenderedPrompt;
|