@to11ai/sdk 1.0.0 → 2.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.cjs +85 -18
- package/dist/client.d.ts +140 -39
- package/dist/client.js +91 -19
- package/dist/config.d.ts +9 -0
- package/dist/context.d.ts +124 -0
- package/dist/errors.cjs +18 -1
- package/dist/errors.d.ts +18 -1
- package/dist/errors.js +18 -1
- package/dist/gateway.cjs +62 -22
- package/dist/gateway.d.ts +57 -9
- package/dist/gateway.js +62 -22
- package/dist/generated/index.d.ts +1 -1
- package/dist/generated/types.gen.d.ts +1313 -166
- package/dist/generated/zod.gen.d.ts +406 -232
- package/dist/prompts/cache.d.ts +3 -3
- package/dist/prompts/resource.cjs +21 -16
- package/dist/prompts/resource.d.ts +59 -28
- package/dist/prompts/resource.js +21 -16
- package/dist/prompts/shape.d.ts +54 -0
- package/dist/prompts/types.d.ts +31 -13
- package/package.json +1 -1
- package/src/client.ts +326 -80
- package/src/config.ts +13 -0
- package/src/context.ts +198 -0
- package/src/errors.ts +33 -3
- package/src/gateway.ts +135 -33
- package/src/generated/index.ts +1 -1
- package/src/generated/types.gen.ts +1324 -166
- package/src/generated/zod.gen.ts +371 -268
- package/src/prompts/cache.ts +3 -6
- package/src/prompts/resource.ts +89 -41
- package/src/prompts/shape.ts +149 -0
- package/src/prompts/types.ts +35 -14
package/dist/prompts/cache.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export interface CacheConfig {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* The cache stores either a legacy `resolve()` result or an enriched
|
|
10
|
-
* `
|
|
10
|
+
* `render()` result. Both are JSON-cloneable; the cache is shape-agnostic.
|
|
11
11
|
*/
|
|
12
12
|
export type CacheableResolved = ResolvedPrompt | FetchedPrompt;
|
|
13
13
|
/**
|
|
@@ -24,7 +24,7 @@ export type CacheableResolved = ResolvedPrompt | FetchedPrompt;
|
|
|
24
24
|
*/
|
|
25
25
|
export declare function buildCacheKey(projectId: string, slug: string, labels: string[], variables?: Record<string, unknown>): string;
|
|
26
26
|
/**
|
|
27
|
-
* Build a cache key for an env-aware `
|
|
27
|
+
* Build a cache key for an env-aware `render()` call.
|
|
28
28
|
*
|
|
29
29
|
* Folds in `env` and `subject` ON TOP of the base
|
|
30
30
|
* project/slug/labels/variables key so two calls that differ only by
|
|
@@ -38,7 +38,7 @@ export declare function buildCacheKey(projectId: string, slug: string, labels: s
|
|
|
38
38
|
* caches as the same deterministic variant on repeat (D8); the
|
|
39
39
|
* weighted-no-subject bypass is handled in the resource, not here.
|
|
40
40
|
*/
|
|
41
|
-
export declare function buildFetchCacheKey(projectId: string, slug: string, env: string, subject?: string, variables?: Record<string, unknown
|
|
41
|
+
export declare function buildFetchCacheKey(projectId: string, slug: string, env: string, subject?: string, variables?: Record<string, unknown>): string;
|
|
42
42
|
/**
|
|
43
43
|
* In-memory LRU cache for ResolvedPrompt objects.
|
|
44
44
|
*
|
|
@@ -46,16 +46,18 @@ var import_zod = require("zod");
|
|
|
46
46
|
var import_errors = require("../errors");
|
|
47
47
|
var import_zod2 = require("../generated/zod.gen");
|
|
48
48
|
var import_cache = require("./cache");
|
|
49
|
+
var import_shape = require("./shape");
|
|
49
50
|
var import_types = require("./types");
|
|
50
51
|
var zResolvedPromptLegacy = import_zod2.zResolvedPrompt.pick({
|
|
51
52
|
promptId: true,
|
|
52
53
|
versionId: true,
|
|
53
54
|
version: true,
|
|
54
|
-
|
|
55
|
+
label: true,
|
|
55
56
|
contentHash: true,
|
|
56
57
|
rendered: true,
|
|
57
58
|
tools: true,
|
|
58
|
-
toolChoice: true
|
|
59
|
+
toolChoice: true,
|
|
60
|
+
modelConfig: true
|
|
59
61
|
});
|
|
60
62
|
var zResolvedPromptEnvelope = import_zod2.zResolvedPrompt.partial({ releaseId: true }).extend({ strategy: import_zod.z.enum(["single", "weighted"]).optional() });
|
|
61
63
|
function unwrap(envelope) {
|
|
@@ -83,8 +85,9 @@ function normalizeResolvedEnvelope(raw) {
|
|
|
83
85
|
promptId: raw.promptId,
|
|
84
86
|
versionId: raw.versionId,
|
|
85
87
|
contentHash: raw.contentHash,
|
|
86
|
-
|
|
87
|
-
rendered: raw.rendered
|
|
88
|
+
label: raw.label,
|
|
89
|
+
rendered: raw.rendered,
|
|
90
|
+
modelConfig: provenance.modelConfig
|
|
88
91
|
};
|
|
89
92
|
if (raw.tools != null && raw.tools.length > 0) {
|
|
90
93
|
result.tools = raw.tools;
|
|
@@ -139,11 +142,15 @@ class PromptResource {
|
|
|
139
142
|
cache;
|
|
140
143
|
projectId;
|
|
141
144
|
env;
|
|
145
|
+
format;
|
|
146
|
+
onUnknownRole;
|
|
142
147
|
constructor(transport, cacheConfig, config) {
|
|
143
148
|
this.transport = transport;
|
|
144
149
|
this.cache = new import_cache.PromptCache(cacheConfig);
|
|
145
150
|
this.projectId = config?.projectId;
|
|
146
151
|
this.env = config?.env;
|
|
152
|
+
this.format = config?.format;
|
|
153
|
+
this.onUnknownRole = config?.onUnknownRole;
|
|
147
154
|
}
|
|
148
155
|
async create(input) {
|
|
149
156
|
const { projectId, ...body } = input;
|
|
@@ -207,9 +214,15 @@ class PromptResource {
|
|
|
207
214
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/labels`);
|
|
208
215
|
return unwrap(envelope);
|
|
209
216
|
}
|
|
210
|
-
async
|
|
217
|
+
async render(slug, options = {}) {
|
|
218
|
+
const fetched = await this.resolveToFetched(slug, options);
|
|
219
|
+
return import_shape.shapePrompt(fetched, options.format ?? this.format, {
|
|
220
|
+
onUnknownRole: options.onUnknownRole ?? this.onUnknownRole
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
async resolveToFetched(slug, options = {}) {
|
|
211
224
|
if (this.projectId === undefined || this.projectId.trim() === "") {
|
|
212
|
-
throw new Error("[to11ai/sdk] `prompts.
|
|
225
|
+
throw new Error("[to11ai/sdk] `prompts.render()` requires a `projectId` on the client — pass it to createClient({ projectId, ... }).");
|
|
213
226
|
}
|
|
214
227
|
const env = options.env ?? this.env;
|
|
215
228
|
if (env === undefined) {
|
|
@@ -218,14 +231,8 @@ class PromptResource {
|
|
|
218
231
|
if (!import_types.ENV_SLUG_REGEX.test(env)) {
|
|
219
232
|
throw new Error(`[to11ai/sdk] \`env\` must match ${import_types.ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(env)}`);
|
|
220
233
|
}
|
|
221
|
-
const {
|
|
222
|
-
|
|
223
|
-
variables,
|
|
224
|
-
cache: useCache = true,
|
|
225
|
-
fallback,
|
|
226
|
-
developerRole
|
|
227
|
-
} = options;
|
|
228
|
-
const cacheKey = import_cache.buildFetchCacheKey(this.projectId, slug, env, subject, variables, developerRole);
|
|
234
|
+
const { subject, variables, cache: useCache = true, fallback } = options;
|
|
235
|
+
const cacheKey = import_cache.buildFetchCacheKey(this.projectId, slug, env, subject, variables);
|
|
229
236
|
if (useCache) {
|
|
230
237
|
const cached = this.cache.get(cacheKey);
|
|
231
238
|
if (cached && "messages" in cached)
|
|
@@ -236,8 +243,6 @@ class PromptResource {
|
|
|
236
243
|
body.subject = subject;
|
|
237
244
|
if (variables !== undefined)
|
|
238
245
|
body.variables = variables;
|
|
239
|
-
if (developerRole !== undefined)
|
|
240
|
-
body.developerRole = developerRole;
|
|
241
246
|
try {
|
|
242
247
|
const envelope = await this.transport.request("POST", `${getProjectPromptsPath(this.projectId)}:resolve`, {
|
|
243
248
|
body,
|
|
@@ -1,27 +1,39 @@
|
|
|
1
1
|
import type { Transport } from "../transport";
|
|
2
2
|
import type { CacheConfig } from "./cache";
|
|
3
|
-
import type {
|
|
3
|
+
import type { AnthropicRenderedPrompt, OpenAiRenderedPrompt, RenderResult } from "./shape";
|
|
4
|
+
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, ResolvePromptInput, ResolvePromptResult, UnknownRoleMode, UpdateProjectPromptLabelInput, UpdateProjectPromptLabelResult, UpdatePromptInput, UpdatePromptResult } from "./types";
|
|
4
5
|
export type { CacheConfig } from "./cache";
|
|
5
6
|
export type { ArchivePromptInput, ArchivePromptResult, BlockRenderRecord, ChatMessage, CreateProjectPromptLabelInput, CreateProjectPromptLabelResult, CreatePromptInput, CreatePromptResult, CreatePromptVersionInput, CreatePromptVersionResult, DeleteProjectPromptLabelInput, DeleteProjectPromptLabelResult, FetchedPrompt, FetchPromptOptions, 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, ResolvedPrompt, ResolvedPromptEnvelope, ResolvedToolCall, ResolvedToolDefinition, ResolvePromptInput, ResolvePromptResult, RowError, TextChatMessage, TextChatRole, UpdateProjectPromptLabelInput, UpdateProjectPromptLabelResult, UpdatePromptInput, UpdatePromptResult, } from "./types";
|
|
6
7
|
/**
|
|
7
|
-
* Init-time config for the env-aware `
|
|
8
|
+
* Init-time config for the env-aware `render()` surface (TO11-980 / S9).
|
|
8
9
|
*
|
|
9
10
|
* `projectId` and `env` come from `createClient`. `projectId` is required for
|
|
10
|
-
* `
|
|
11
|
-
* serving environment inherited by every `
|
|
11
|
+
* `render()` (it builds the project-scoped resolve path); `env` is the default
|
|
12
|
+
* serving environment inherited by every `render(slug)` call.
|
|
12
13
|
*/
|
|
13
14
|
export interface PromptResourceConfig {
|
|
14
|
-
/** Project the client is bound to. Required for `
|
|
15
|
+
/** Project the client is bound to. Required for `render()`; validated at createClient. */
|
|
15
16
|
projectId?: string;
|
|
16
|
-
/** Default serving env for `
|
|
17
|
+
/** Default serving env for `render()`. Validated against ENV_SLUG_REGEX at createClient. */
|
|
17
18
|
env?: string;
|
|
19
|
+
/** Default provider shape for `render()` output. Per-call override allowed. */
|
|
20
|
+
format?: RenderFormat;
|
|
21
|
+
/**
|
|
22
|
+
* Default unknown-role policy for `render({ format })` output conversion.
|
|
23
|
+
* Per-call override allowed; defaults to `"drop"`.
|
|
24
|
+
*/
|
|
25
|
+
onUnknownRole?: UnknownRoleMode;
|
|
18
26
|
}
|
|
19
|
-
export declare class PromptResource {
|
|
27
|
+
export declare class PromptResource<F extends RenderFormat | undefined = undefined> {
|
|
20
28
|
private readonly transport;
|
|
21
29
|
private readonly cache;
|
|
22
30
|
private readonly projectId;
|
|
23
31
|
private readonly env;
|
|
24
|
-
|
|
32
|
+
private readonly format;
|
|
33
|
+
private readonly onUnknownRole;
|
|
34
|
+
constructor(transport: Transport, cacheConfig?: CacheConfig, config?: Omit<PromptResourceConfig, "format"> & {
|
|
35
|
+
format?: F;
|
|
36
|
+
});
|
|
25
37
|
create(input: CreatePromptInput): Promise<CreatePromptResult>;
|
|
26
38
|
list(input: ListPromptsInput): Promise<PaginatedResult<ListPromptsResult>>;
|
|
27
39
|
get(input: GetPromptInput): Promise<GetPromptResult>;
|
|
@@ -33,33 +45,52 @@ export declare class PromptResource {
|
|
|
33
45
|
moveLabel(input: MovePromptLabelInput): Promise<MovePromptLabelResult>;
|
|
34
46
|
listLabels(input: ListPromptLabelsInput): Promise<ListPromptLabelsResult>;
|
|
35
47
|
/**
|
|
36
|
-
* Resolve a prompt for the client's configured
|
|
37
|
-
*
|
|
38
|
-
*
|
|
48
|
+
* Resolve and (optionally) shape a prompt for the client's configured
|
|
49
|
+
* `projectId` + `env`, in one control-plane request. With a `format` (per
|
|
50
|
+
* call or inherited from the client) the result is provider-shaped
|
|
51
|
+
* `{ messages, config, metadata }`; without one it is the neutral
|
|
52
|
+
* `FetchedPrompt`. Resolves by the client's `env`; for label-addressed
|
|
53
|
+
* resolution see {@link PromptResource.resolve | `resolve()`}.
|
|
54
|
+
*
|
|
55
|
+
* @example Render an OpenAI-shaped prompt and send it through the gateway
|
|
56
|
+
* const prompt = await to11.prompts.render("weather-concierge", {
|
|
57
|
+
* variables: { city: "NYC" },
|
|
58
|
+
* });
|
|
59
|
+
* const res = await openai.chat.completions.create(
|
|
60
|
+
* { ...prompt.config, messages: prompt.messages },
|
|
61
|
+
* { headers: to11.turn().headers(prompt) },
|
|
62
|
+
* );
|
|
63
|
+
*/
|
|
64
|
+
render(slug: string, options: RenderOptions & {
|
|
65
|
+
format: "openai";
|
|
66
|
+
}): Promise<OpenAiRenderedPrompt>;
|
|
67
|
+
render(slug: string, options: RenderOptions & {
|
|
68
|
+
format: "anthropic";
|
|
69
|
+
}): Promise<AnthropicRenderedPrompt>;
|
|
70
|
+
render(slug: string, options?: RenderOptions): Promise<RenderResult<F>>;
|
|
71
|
+
/**
|
|
72
|
+
* Env-aware resolution → neutral `FetchedPrompt`. Carries the resolve /
|
|
73
|
+
* cache / offline-fallback logic; `render()` is the public shaping surface.
|
|
39
74
|
*
|
|
40
|
-
* - **env→label wire mapping
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* env slug (lowercase-kebab — a valid label name) is therefore sent as
|
|
44
|
-
* the single resolution label: body `{ slug, labels: [env], subject?,
|
|
45
|
-
* variables? }`.
|
|
75
|
+
* - **env→label wire mapping:** the resolve route is label-based
|
|
76
|
+
* (`/v1/projects/:projectId/prompts:resolve`, body `labels[]`); the
|
|
77
|
+
* resolved env slug is sent as the single resolution label.
|
|
46
78
|
* - **No silent fallback (R4):** if no env is resolvable (none at init AND
|
|
47
|
-
* none per call) this throws `EnvNotFoundError`
|
|
48
|
-
*
|
|
79
|
+
* none per call) this throws `EnvNotFoundError` CLIENT-side, eagerly,
|
|
80
|
+
* before any transport call.
|
|
49
81
|
* - **Cache (D8):** caches `single`-strategy releases and weighted releases
|
|
50
82
|
* WITH a `subject`; BYPASSES the cache for weighted releases with NO
|
|
51
83
|
* `subject` (a uniform-random assignment must not be pinned for the TTL).
|
|
52
|
-
*
|
|
53
|
-
* Prefer this over the deprecated `resolve()`.
|
|
54
84
|
*/
|
|
55
|
-
|
|
85
|
+
private resolveToFetched;
|
|
56
86
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
87
|
+
* Resolve a prompt by explicit `labels` for a given `projectId`, in one
|
|
88
|
+
* control-plane request. This is the label-addressed counterpart to
|
|
89
|
+
* {@link PromptResource.render | `render()`}, which resolves by the client's
|
|
90
|
+
* configured `env`. Reach for `resolve()` when you address prompts by label
|
|
91
|
+
* rather than by environment; it is env-agnostic, so it carries no `env` /
|
|
92
|
+
* `subject`, no enriched release/variant provenance, and no typed-env errors,
|
|
93
|
+
* and is not subject to the env-based no-silent-fallback gate.
|
|
63
94
|
*/
|
|
64
95
|
resolve(input: ResolvePromptInput): Promise<ResolvePromptResult>;
|
|
65
96
|
/**
|
package/dist/prompts/resource.js
CHANGED
|
@@ -3,16 +3,18 @@ import { z } from "zod";
|
|
|
3
3
|
import { EnvNotFoundError, To11aiNetworkError } from "../errors";
|
|
4
4
|
import { zResolvedPrompt } from "../generated/zod.gen";
|
|
5
5
|
import { buildCacheKey, buildFetchCacheKey, PromptCache } from "./cache";
|
|
6
|
+
import { shapePrompt } from "./shape";
|
|
6
7
|
import { ENV_SLUG_REGEX } from "./types";
|
|
7
8
|
var zResolvedPromptLegacy = zResolvedPrompt.pick({
|
|
8
9
|
promptId: true,
|
|
9
10
|
versionId: true,
|
|
10
11
|
version: true,
|
|
11
|
-
|
|
12
|
+
label: true,
|
|
12
13
|
contentHash: true,
|
|
13
14
|
rendered: true,
|
|
14
15
|
tools: true,
|
|
15
|
-
toolChoice: true
|
|
16
|
+
toolChoice: true,
|
|
17
|
+
modelConfig: true
|
|
16
18
|
});
|
|
17
19
|
var zResolvedPromptEnvelope = zResolvedPrompt.partial({ releaseId: true }).extend({ strategy: z.enum(["single", "weighted"]).optional() });
|
|
18
20
|
function unwrap(envelope) {
|
|
@@ -40,8 +42,9 @@ function normalizeResolvedEnvelope(raw) {
|
|
|
40
42
|
promptId: raw.promptId,
|
|
41
43
|
versionId: raw.versionId,
|
|
42
44
|
contentHash: raw.contentHash,
|
|
43
|
-
|
|
44
|
-
rendered: raw.rendered
|
|
45
|
+
label: raw.label,
|
|
46
|
+
rendered: raw.rendered,
|
|
47
|
+
modelConfig: provenance.modelConfig
|
|
45
48
|
};
|
|
46
49
|
if (raw.tools != null && raw.tools.length > 0) {
|
|
47
50
|
result.tools = raw.tools;
|
|
@@ -96,11 +99,15 @@ class PromptResource {
|
|
|
96
99
|
cache;
|
|
97
100
|
projectId;
|
|
98
101
|
env;
|
|
102
|
+
format;
|
|
103
|
+
onUnknownRole;
|
|
99
104
|
constructor(transport, cacheConfig, config) {
|
|
100
105
|
this.transport = transport;
|
|
101
106
|
this.cache = new PromptCache(cacheConfig);
|
|
102
107
|
this.projectId = config?.projectId;
|
|
103
108
|
this.env = config?.env;
|
|
109
|
+
this.format = config?.format;
|
|
110
|
+
this.onUnknownRole = config?.onUnknownRole;
|
|
104
111
|
}
|
|
105
112
|
async create(input) {
|
|
106
113
|
const { projectId, ...body } = input;
|
|
@@ -164,9 +171,15 @@ class PromptResource {
|
|
|
164
171
|
const envelope = await this.transport.request("GET", `${getProjectPromptPath(projectId, promptId)}/labels`);
|
|
165
172
|
return unwrap(envelope);
|
|
166
173
|
}
|
|
167
|
-
async
|
|
174
|
+
async render(slug, options = {}) {
|
|
175
|
+
const fetched = await this.resolveToFetched(slug, options);
|
|
176
|
+
return shapePrompt(fetched, options.format ?? this.format, {
|
|
177
|
+
onUnknownRole: options.onUnknownRole ?? this.onUnknownRole
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
async resolveToFetched(slug, options = {}) {
|
|
168
181
|
if (this.projectId === undefined || this.projectId.trim() === "") {
|
|
169
|
-
throw new Error("[to11ai/sdk] `prompts.
|
|
182
|
+
throw new Error("[to11ai/sdk] `prompts.render()` requires a `projectId` on the client — pass it to createClient({ projectId, ... }).");
|
|
170
183
|
}
|
|
171
184
|
const env = options.env ?? this.env;
|
|
172
185
|
if (env === undefined) {
|
|
@@ -175,14 +188,8 @@ class PromptResource {
|
|
|
175
188
|
if (!ENV_SLUG_REGEX.test(env)) {
|
|
176
189
|
throw new Error(`[to11ai/sdk] \`env\` must match ${ENV_SLUG_REGEX} (lowercase-kebab, 1-63 chars). Received: ${JSON.stringify(env)}`);
|
|
177
190
|
}
|
|
178
|
-
const {
|
|
179
|
-
|
|
180
|
-
variables,
|
|
181
|
-
cache: useCache = true,
|
|
182
|
-
fallback,
|
|
183
|
-
developerRole
|
|
184
|
-
} = options;
|
|
185
|
-
const cacheKey = buildFetchCacheKey(this.projectId, slug, env, subject, variables, developerRole);
|
|
191
|
+
const { subject, variables, cache: useCache = true, fallback } = options;
|
|
192
|
+
const cacheKey = buildFetchCacheKey(this.projectId, slug, env, subject, variables);
|
|
186
193
|
if (useCache) {
|
|
187
194
|
const cached = this.cache.get(cacheKey);
|
|
188
195
|
if (cached && "messages" in cached)
|
|
@@ -193,8 +200,6 @@ class PromptResource {
|
|
|
193
200
|
body.subject = subject;
|
|
194
201
|
if (variables !== undefined)
|
|
195
202
|
body.variables = variables;
|
|
196
|
-
if (developerRole !== undefined)
|
|
197
|
-
body.developerRole = developerRole;
|
|
198
203
|
try {
|
|
199
204
|
const envelope = await this.transport.request("POST", `${getProjectPromptsPath(this.projectId)}:resolve`, {
|
|
200
205
|
body,
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format-at-load shaping: turn a neutral `FetchedPrompt` into a provider-shaped
|
|
3
|
+
* `{ messages, config, metadata }` result the caller spreads into their own
|
|
4
|
+
* provider `create()` call. Header-only — this only reshapes data, it never
|
|
5
|
+
* sends a request. See docs/product-specs/2026-07-14-to11-2691-sdk-simpler-setup.md.
|
|
6
|
+
*/
|
|
7
|
+
import { type AnthropicMessage, type AnthropicTool, type AnthropicToolChoice, type ConvertMessagesOptions, type OpenAIChatMessage, type OpenAITool, type OpenAIToolChoice } from "../gateway";
|
|
8
|
+
import type { FetchedPrompt, RenderFormat } from "./types";
|
|
9
|
+
/** Model params passed through verbatim from the resolved version's modelConfig. */
|
|
10
|
+
export interface PromptConfig {
|
|
11
|
+
model?: string;
|
|
12
|
+
temperature?: number;
|
|
13
|
+
max_tokens?: number;
|
|
14
|
+
/** Any additional stored model-config keys are passed through unchanged. */
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
/** Prompt provenance for `turn.headers()` — kept apart from the request body. */
|
|
18
|
+
export interface PromptMetadata {
|
|
19
|
+
promptId: string;
|
|
20
|
+
versionId: string;
|
|
21
|
+
version: number;
|
|
22
|
+
releaseId: string | null;
|
|
23
|
+
/** The single label the prompt resolved through (env slug today). */
|
|
24
|
+
label: string;
|
|
25
|
+
variantName?: string;
|
|
26
|
+
}
|
|
27
|
+
/** OpenAI-shaped render result: spread `{ ...config, messages }` into create(). */
|
|
28
|
+
export interface OpenAiRenderedPrompt {
|
|
29
|
+
messages: OpenAIChatMessage[];
|
|
30
|
+
config: PromptConfig;
|
|
31
|
+
metadata: PromptMetadata;
|
|
32
|
+
/** Authored tools, provider-shaped. Present only when the prompt declares tools. */
|
|
33
|
+
tools?: OpenAITool[];
|
|
34
|
+
/** Authored tool-choice directive. Present only when the prompt sets one. */
|
|
35
|
+
tool_choice?: OpenAIToolChoice;
|
|
36
|
+
}
|
|
37
|
+
/** Anthropic-shaped render result: system/developer folded into `system`. */
|
|
38
|
+
export interface AnthropicRenderedPrompt {
|
|
39
|
+
/** Folded system/developer instruction text (Anthropic's top-level `system`). */
|
|
40
|
+
system?: string;
|
|
41
|
+
messages: AnthropicMessage[];
|
|
42
|
+
config: PromptConfig;
|
|
43
|
+
metadata: PromptMetadata;
|
|
44
|
+
/** Authored tools, provider-shaped. Present only when the prompt declares tools. */
|
|
45
|
+
tools?: AnthropicTool[];
|
|
46
|
+
/** Authored tool-choice directive. Present only when the prompt sets one. */
|
|
47
|
+
tool_choice?: AnthropicToolChoice;
|
|
48
|
+
}
|
|
49
|
+
/** The render return type for a given client/call format. */
|
|
50
|
+
export type RenderResult<F extends RenderFormat | undefined> = F extends "openai" ? OpenAiRenderedPrompt : F extends "anthropic" ? AnthropicRenderedPrompt : FetchedPrompt;
|
|
51
|
+
export declare function shapeOpenAI(prompt: FetchedPrompt, options?: ConvertMessagesOptions): OpenAiRenderedPrompt;
|
|
52
|
+
export declare function shapeAnthropic(prompt: FetchedPrompt, options?: ConvertMessagesOptions): AnthropicRenderedPrompt;
|
|
53
|
+
/** Shape a fetched prompt for `format`, or return it neutral when unset. */
|
|
54
|
+
export declare function shapePrompt(prompt: FetchedPrompt, format: RenderFormat | undefined, options?: ConvertMessagesOptions): FetchedPrompt | OpenAiRenderedPrompt | AnthropicRenderedPrompt;
|
package/dist/prompts/types.d.ts
CHANGED
|
@@ -18,6 +18,16 @@ export interface TextChatMessage {
|
|
|
18
18
|
export type ChatMessage = TextChatMessage;
|
|
19
19
|
export type RenderedChatMessage = GenRenderedChatPrompt["messages"][number];
|
|
20
20
|
export type RenderedPrompt = GenRenderedPrompt;
|
|
21
|
+
/** Provider shape for `prompts.render()` output. Omit for the neutral result. */
|
|
22
|
+
export type RenderFormat = "openai" | "anthropic";
|
|
23
|
+
/**
|
|
24
|
+
* Policy for a message whose role is outside the known set
|
|
25
|
+
* (`system | user | assistant | developer | tool`) when converting to a
|
|
26
|
+
* provider shape. `"drop"` (default) omits it silently; `"warn"` omits it and
|
|
27
|
+
* emits a `console.warn`; `"error"` throws `UnknownRoleError`. Applies to the
|
|
28
|
+
* standalone converters and to `render({ format })`.
|
|
29
|
+
*/
|
|
30
|
+
export type UnknownRoleMode = "drop" | "warn" | "error";
|
|
21
31
|
/**
|
|
22
32
|
* A provider-neutral tool definition (`{ name, description?, parameters }`),
|
|
23
33
|
* sourced from authored tool-definition blocks. Surfaced on the resolved
|
|
@@ -29,7 +39,7 @@ export type ResolvedToolDefinition = GenResolvedToolDefinition;
|
|
|
29
39
|
* on a rendered `assistant` turn (few-shot multi-tool history).
|
|
30
40
|
*/
|
|
31
41
|
export type ResolvedToolCall = GenResolvedToolCall;
|
|
32
|
-
export type ResolvedPrompt = Pick<GenResolvedPrompt, "promptId" | "versionId" | "version" | "
|
|
42
|
+
export type ResolvedPrompt = Pick<GenResolvedPrompt, "promptId" | "versionId" | "version" | "label" | "contentHash" | "rendered" | "tools" | "toolChoice" | "modelConfig">;
|
|
33
43
|
export interface PaginationInfo {
|
|
34
44
|
page: number;
|
|
35
45
|
limit: number;
|
|
@@ -131,8 +141,19 @@ export interface FetchedPrompt {
|
|
|
131
141
|
promptId: string;
|
|
132
142
|
versionId: string;
|
|
133
143
|
contentHash: string;
|
|
134
|
-
|
|
144
|
+
/**
|
|
145
|
+
* The single label the prompt resolved through — the env slug today, a real
|
|
146
|
+
* label once labels ship. Surfaced as `x-to11-prompt-label` by
|
|
147
|
+
* `gatewayPromptHeaders` / `turn.headers()` (TO11-2725).
|
|
148
|
+
*/
|
|
149
|
+
label: string;
|
|
135
150
|
rendered: RenderedPrompt;
|
|
151
|
+
/**
|
|
152
|
+
* The served version's model config (model, temperature, max_tokens, …),
|
|
153
|
+
* passed through verbatim. Surfaced as `config` by `render({ format })`.
|
|
154
|
+
* Optional: absent when a pre-`modelConfig` API omits it.
|
|
155
|
+
*/
|
|
156
|
+
modelConfig?: unknown;
|
|
136
157
|
/**
|
|
137
158
|
* Provider-neutral tool definitions lifted from authored tool-definition
|
|
138
159
|
* blocks. Absent (rather than `[]`) when the resolved version declares no
|
|
@@ -154,25 +175,22 @@ export type ResolvedPromptEnvelope = GenResolvedPrompt & {
|
|
|
154
175
|
/** Active release strategy. Used to decide weighted-no-subject cache bypass (D8). */
|
|
155
176
|
strategy?: "single" | "weighted";
|
|
156
177
|
};
|
|
157
|
-
/**
|
|
158
|
-
* How an authored `developer` block is rendered. The caller controls this
|
|
159
|
-
* per-call because it knows the upstream it targets. Omitted ⇒ `"user"`
|
|
160
|
-
* (back-compatible; safe for any upstream). `"developer"` preserves the role
|
|
161
|
-
* (OpenAI); `"system"` folds it into a system message.
|
|
162
|
-
*/
|
|
163
|
-
export type DeveloperRole = "user" | "developer" | "system";
|
|
164
178
|
export interface FetchPromptOptions {
|
|
165
179
|
env?: string;
|
|
166
180
|
subject?: string;
|
|
167
181
|
variables?: Record<string, unknown>;
|
|
168
182
|
cache?: boolean;
|
|
169
183
|
fallback?: ResolvedPrompt | FetchedPrompt;
|
|
184
|
+
}
|
|
185
|
+
export interface RenderOptions extends FetchPromptOptions {
|
|
186
|
+
/** Provider shape for this call's output; overrides the client default. */
|
|
187
|
+
format?: RenderFormat;
|
|
170
188
|
/**
|
|
171
|
-
* How
|
|
172
|
-
*
|
|
173
|
-
*
|
|
189
|
+
* How to handle a message with an unrecognized role while shaping for a
|
|
190
|
+
* provider (`format` set). Overrides the client default; defaults to `"drop"`.
|
|
191
|
+
* Only meaningful with a `format` — the neutral result runs no converter.
|
|
174
192
|
*/
|
|
175
|
-
|
|
193
|
+
onUnknownRole?: UnknownRoleMode;
|
|
176
194
|
}
|
|
177
195
|
export type PaletteToken = ProjectPromptLabel["displayColor"];
|
|
178
196
|
export type ProjectPromptLabel = GenProjectPromptLabel;
|