@promptlycms/prompts 0.3.0 → 0.4.0-canary.375b6a8

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 CHANGED
@@ -19,7 +19,8 @@ npm install @promptlycms/prompts
19
19
  Peer dependencies:
20
20
 
21
21
  ```bash
22
- npm install zod ai typescript
22
+ npm install zod ai
23
+ npm install --save-dev typescript
23
24
  ```
24
25
 
25
26
  You'll also need at least one AI provider SDK for model resolution:
package/dist/cli.js CHANGED
@@ -86,6 +86,28 @@ var extractTemplateVariables = (text) => {
86
86
  }
87
87
  return [...vars];
88
88
  };
89
+ var extractStaticSegmentVariables = (content) => {
90
+ const vars = /* @__PURE__ */ new Set();
91
+ const varRefRegex = /<span[^>]*\sdata-variable-ref(?:="[^"]*")?[^>]*\sdata-field-path="([^"]+)"[^>]*><\/span>/g;
92
+ const varRefAltRegex = /<span[^>]*\sdata-field-path="([^"]+)"[^>]*\sdata-variable-ref(?:="[^"]*")?[^>]*><\/span>/g;
93
+ const mustacheRegex = /\{\{(\w[\w.]*)\}\}/g;
94
+ for (const match of content.matchAll(varRefRegex)) {
95
+ if (match[1]) {
96
+ vars.add(match[1]);
97
+ }
98
+ }
99
+ for (const match of content.matchAll(varRefAltRegex)) {
100
+ if (match[1]) {
101
+ vars.add(match[1]);
102
+ }
103
+ }
104
+ for (const match of content.matchAll(mustacheRegex)) {
105
+ if (match[1]) {
106
+ vars.add(match[1]);
107
+ }
108
+ }
109
+ return [...vars];
110
+ };
89
111
  var fetchAllPrompts = async (apiKey, baseUrl) => {
90
112
  const url = new URL("/prompts", baseUrl ?? DEFAULT_BASE_URL);
91
113
  url.searchParams.set("include_versions", "true");
@@ -115,14 +137,17 @@ var fetchAllComposers = async (apiKey, baseUrl) => {
115
137
  var extractComposerVariables = (composer) => {
116
138
  const vars = /* @__PURE__ */ new Set();
117
139
  for (const segment of composer.segments) {
118
- if (segment.type !== "prompt") {
119
- continue;
120
- }
121
- if (!segment.userMessage) {
122
- continue;
123
- }
124
- for (const v of extractTemplateVariables(segment.userMessage)) {
125
- vars.add(v);
140
+ if (segment.type === "prompt") {
141
+ if (!segment.userMessage) {
142
+ continue;
143
+ }
144
+ for (const v of extractTemplateVariables(segment.userMessage)) {
145
+ vars.add(v);
146
+ }
147
+ } else if (segment.type === "static") {
148
+ for (const v of extractStaticSegmentVariables(segment.content)) {
149
+ vars.add(v);
150
+ }
126
151
  }
127
152
  }
128
153
  return [...vars];
@@ -216,7 +241,43 @@ var groupAndSortVersions = (prompt) => {
216
241
  });
217
242
  return result;
218
243
  };
219
- var generateMappedTypeBlock = (group, indent) => {
244
+ var ELEMENT_TYPE_MAP = {
245
+ string: "string",
246
+ number: "number",
247
+ boolean: "boolean"
248
+ };
249
+ var schemaFieldToTsType = (field) => {
250
+ if (!field) {
251
+ return "string";
252
+ }
253
+ switch (field.type) {
254
+ case "number":
255
+ case "boolean":
256
+ return field.type;
257
+ case "array": {
258
+ const elementTs = ELEMENT_TYPE_MAP[field.params.elementType ?? ""] ?? "string";
259
+ return `${elementTs}[]`;
260
+ }
261
+ case "enum": {
262
+ if (field.params.enumValues && field.params.enumValues.length > 0) {
263
+ return field.params.enumValues.map((v) => `'${v}'`).join(" | ");
264
+ }
265
+ return "string";
266
+ }
267
+ case "object":
268
+ return "Record<string, unknown>";
269
+ default:
270
+ return "string";
271
+ }
272
+ };
273
+ var buildSchemaMap = (schema) => {
274
+ const map = /* @__PURE__ */ new Map();
275
+ for (const field of schema) {
276
+ map.set(field.name, field);
277
+ }
278
+ return map;
279
+ };
280
+ var generateMappedTypeBlock = (group, indent, schemaMap = /* @__PURE__ */ new Map()) => {
220
281
  const lines = [];
221
282
  const { versions, variables } = group;
222
283
  if (versions.length === 1) {
@@ -226,7 +287,9 @@ var generateMappedTypeBlock = (group, indent) => {
226
287
  } else {
227
288
  lines.push(`${indent}[V in ${vKey}]: {`);
228
289
  for (const v of variables) {
229
- lines.push(`${indent} ${v}: string;`);
290
+ lines.push(
291
+ `${indent} ${v}: ${schemaFieldToTsType(schemaMap.get(v))};`
292
+ );
230
293
  }
231
294
  lines.push(`${indent}};`);
232
295
  }
@@ -247,7 +310,9 @@ var generateMappedTypeBlock = (group, indent) => {
247
310
  }
248
311
  if (variables.length > 0) {
249
312
  for (const v of variables) {
250
- lines.push(`${indent} ${v}: string;`);
313
+ lines.push(
314
+ `${indent} ${v}: ${schemaFieldToTsType(schemaMap.get(v))};`
315
+ );
251
316
  }
252
317
  lines.push(`${indent}};`);
253
318
  }
@@ -264,11 +329,12 @@ var generateTypeDeclaration = (prompts, composers = []) => {
264
329
  ];
265
330
  for (const prompt of prompts) {
266
331
  const groups = groupAndSortVersions(prompt);
332
+ const schemaMap = buildSchemaMap(prompt.config.schema);
267
333
  if (groups.length === 1) {
268
334
  const group = groups[0];
269
335
  if (group) {
270
336
  lines.push(` '${prompt.promptId}': {`);
271
- lines.push(...generateMappedTypeBlock(group, " "));
337
+ lines.push(...generateMappedTypeBlock(group, " ", schemaMap));
272
338
  lines.push(" };");
273
339
  }
274
340
  } else {
@@ -282,7 +348,7 @@ var generateTypeDeclaration = (prompts, composers = []) => {
282
348
  } else {
283
349
  lines.push(" } & {");
284
350
  }
285
- lines.push(...generateMappedTypeBlock(group, " "));
351
+ lines.push(...generateMappedTypeBlock(group, " ", schemaMap));
286
352
  }
287
353
  lines.push(" };");
288
354
  }
@@ -292,6 +358,7 @@ var generateTypeDeclaration = (prompts, composers = []) => {
292
358
  lines.push(" interface ComposerVariableMap {");
293
359
  for (const composer of composers) {
294
360
  const variables = extractComposerVariables(composer);
361
+ const schemaMap = buildSchemaMap(composer.config.schema);
295
362
  const versions = ["'latest'"];
296
363
  if (composer.publishedVersions) {
297
364
  for (const pv of composer.publishedVersions) {
@@ -307,7 +374,9 @@ var generateTypeDeclaration = (prompts, composers = []) => {
307
374
  } else {
308
375
  lines.push(` [V in ${versions[0]}]: {`);
309
376
  for (const v of variables) {
310
- lines.push(` ${v}: string;`);
377
+ lines.push(
378
+ ` ${v}: ${schemaFieldToTsType(schemaMap.get(v))};`
379
+ );
311
380
  }
312
381
  lines.push(" };");
313
382
  }
@@ -318,7 +387,9 @@ var generateTypeDeclaration = (prompts, composers = []) => {
318
387
  } else {
319
388
  lines.push(` [V in ${versionUnion}]: {`);
320
389
  for (const v of variables) {
321
- lines.push(` ${v}: string;`);
390
+ lines.push(
391
+ ` ${v}: ${schemaFieldToTsType(schemaMap.get(v))};`
392
+ );
322
393
  }
323
394
  lines.push(" };");
324
395
  }
package/dist/index.cjs CHANGED
@@ -147,7 +147,7 @@ var resolveModel = async (modelId) => {
147
147
  var interpolate = (template, variables) => {
148
148
  let result = template;
149
149
  for (const [key, value] of Object.entries(variables)) {
150
- result = result.replaceAll(`\${${key}}`, value);
150
+ result = result.replaceAll(`\${${key}}`, String(value));
151
151
  }
152
152
  return result;
153
153
  };
@@ -190,15 +190,15 @@ var interpolateStaticSegment = (content, input) => {
190
190
  let result = content;
191
191
  result = result.replace(
192
192
  VARIABLE_REF_REGEX,
193
- (_, fieldPath) => input[fieldPath] ?? ""
193
+ (_, fieldPath) => fieldPath in input ? String(input[fieldPath]) : ""
194
194
  );
195
195
  result = result.replace(
196
196
  VARIABLE_REF_ALT_REGEX,
197
- (_, fieldPath) => input[fieldPath] ?? ""
197
+ (_, fieldPath) => fieldPath in input ? String(input[fieldPath]) : ""
198
198
  );
199
199
  result = result.replace(
200
200
  MUSTACHE_REGEX,
201
- (_, fieldPath) => input[fieldPath] ?? ""
201
+ (_, fieldPath) => fieldPath in input ? String(input[fieldPath]) : ""
202
202
  );
203
203
  return result;
204
204
  };
package/dist/index.d.cts CHANGED
@@ -1,11 +1,11 @@
1
- import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-DyMq5QKO.cjs';
2
- export { C as ComposerConfig, b as ComposerFormatFn, c as ComposerGenerateFn, d as ComposerId, e as ComposerInputFor, f as ComposerPrompt, g as ComposerPromptMap, h as ComposerPromptNamesFor, i as ComposerPromptSegment, j as ComposerRequest, k as ComposerResponse, l as ComposerResult, m as ComposerSegment, n as ComposerStaticSegment, o as ComposerVariableMap, p as ComposerVersion, q as ErrorResponse, F as FormatInput, G as GetComposerOptions, r as GetOptions, s as PromptConfig, t as PromptId, u as PromptMessage, v as PromptRequest, w as PromptResponse, x as PromptResult, y as PromptVariableMap, z as PromptVersion, A as PublishedVersion, S as SchemaField, B as SchemaFieldParams, V as ValidationRule } from './types-DyMq5QKO.cjs';
1
+ import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-DIVyjOlH.cjs';
2
+ export { C as ComposerConfig, b as ComposerFormatFn, c as ComposerGenerateFn, d as ComposerId, e as ComposerInputFor, f as ComposerPrompt, g as ComposerPromptMap, h as ComposerPromptNamesFor, i as ComposerPromptSegment, j as ComposerRequest, k as ComposerResponse, l as ComposerResult, m as ComposerSegment, n as ComposerStaticSegment, o as ComposerVariableMap, p as ComposerVersion, q as ErrorResponse, F as FormatInput, G as GetComposerOptions, r as GetOptions, s as PromptConfig, t as PromptId, u as PromptMessage, v as PromptRequest, w as PromptResponse, x as PromptResult, y as PromptVariableMap, z as PromptVersion, A as PublishedVersion, S as SchemaField, B as SchemaFieldParams, V as ValidationRule } from './types-DIVyjOlH.cjs';
3
3
  import 'ai';
4
4
 
5
5
  declare const getSdkModelId: (modelId: string) => string;
6
- declare const interpolate: (template: string, variables: Record<string, string>) => string;
6
+ declare const interpolate: (template: string, variables: Record<string, unknown>) => string;
7
7
  declare const toCamelCase: (name: string) => string;
8
- declare const interpolateStaticSegment: (content: string, input: Record<string, string>) => string;
8
+ declare const interpolateStaticSegment: (content: string, input: Record<string, unknown>) => string;
9
9
  declare const createPromptlyClient: (config?: PromptlyClientConfig) => PromptlyClient;
10
10
 
11
11
  declare class PromptlyError extends Error {
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-DyMq5QKO.js';
2
- export { C as ComposerConfig, b as ComposerFormatFn, c as ComposerGenerateFn, d as ComposerId, e as ComposerInputFor, f as ComposerPrompt, g as ComposerPromptMap, h as ComposerPromptNamesFor, i as ComposerPromptSegment, j as ComposerRequest, k as ComposerResponse, l as ComposerResult, m as ComposerSegment, n as ComposerStaticSegment, o as ComposerVariableMap, p as ComposerVersion, q as ErrorResponse, F as FormatInput, G as GetComposerOptions, r as GetOptions, s as PromptConfig, t as PromptId, u as PromptMessage, v as PromptRequest, w as PromptResponse, x as PromptResult, y as PromptVariableMap, z as PromptVersion, A as PublishedVersion, S as SchemaField, B as SchemaFieldParams, V as ValidationRule } from './types-DyMq5QKO.js';
1
+ import { P as PromptlyClientConfig, a as PromptlyClient, E as ErrorCode } from './types-DIVyjOlH.js';
2
+ export { C as ComposerConfig, b as ComposerFormatFn, c as ComposerGenerateFn, d as ComposerId, e as ComposerInputFor, f as ComposerPrompt, g as ComposerPromptMap, h as ComposerPromptNamesFor, i as ComposerPromptSegment, j as ComposerRequest, k as ComposerResponse, l as ComposerResult, m as ComposerSegment, n as ComposerStaticSegment, o as ComposerVariableMap, p as ComposerVersion, q as ErrorResponse, F as FormatInput, G as GetComposerOptions, r as GetOptions, s as PromptConfig, t as PromptId, u as PromptMessage, v as PromptRequest, w as PromptResponse, x as PromptResult, y as PromptVariableMap, z as PromptVersion, A as PublishedVersion, S as SchemaField, B as SchemaFieldParams, V as ValidationRule } from './types-DIVyjOlH.js';
3
3
  import 'ai';
4
4
 
5
5
  declare const getSdkModelId: (modelId: string) => string;
6
- declare const interpolate: (template: string, variables: Record<string, string>) => string;
6
+ declare const interpolate: (template: string, variables: Record<string, unknown>) => string;
7
7
  declare const toCamelCase: (name: string) => string;
8
- declare const interpolateStaticSegment: (content: string, input: Record<string, string>) => string;
8
+ declare const interpolateStaticSegment: (content: string, input: Record<string, unknown>) => string;
9
9
  declare const createPromptlyClient: (config?: PromptlyClientConfig) => PromptlyClient;
10
10
 
11
11
  declare class PromptlyError extends Error {
package/dist/index.js CHANGED
@@ -106,7 +106,7 @@ var resolveModel = async (modelId) => {
106
106
  var interpolate = (template, variables) => {
107
107
  let result = template;
108
108
  for (const [key, value] of Object.entries(variables)) {
109
- result = result.replaceAll(`\${${key}}`, value);
109
+ result = result.replaceAll(`\${${key}}`, String(value));
110
110
  }
111
111
  return result;
112
112
  };
@@ -149,15 +149,15 @@ var interpolateStaticSegment = (content, input) => {
149
149
  let result = content;
150
150
  result = result.replace(
151
151
  VARIABLE_REF_REGEX,
152
- (_, fieldPath) => input[fieldPath] ?? ""
152
+ (_, fieldPath) => fieldPath in input ? String(input[fieldPath]) : ""
153
153
  );
154
154
  result = result.replace(
155
155
  VARIABLE_REF_ALT_REGEX,
156
- (_, fieldPath) => input[fieldPath] ?? ""
156
+ (_, fieldPath) => fieldPath in input ? String(input[fieldPath]) : ""
157
157
  );
158
158
  result = result.replace(
159
159
  MUSTACHE_REGEX,
160
- (_, fieldPath) => input[fieldPath] ?? ""
160
+ (_, fieldPath) => fieldPath in input ? String(input[fieldPath]) : ""
161
161
  );
162
162
  return result;
163
163
  };
package/dist/schema.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- import { S as SchemaField } from './types-DyMq5QKO.cjs';
2
+ import { S as SchemaField } from './types-DIVyjOlH.cjs';
3
3
  import 'ai';
4
4
 
5
5
  declare const buildFieldSchema: (field: SchemaField) => z.ZodTypeAny;
package/dist/schema.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- import { S as SchemaField } from './types-DyMq5QKO.js';
2
+ import { S as SchemaField } from './types-DIVyjOlH.js';
3
3
  import 'ai';
4
4
 
5
5
  declare const buildFieldSchema: (field: SchemaField) => z.ZodTypeAny;
@@ -73,12 +73,12 @@ interface PromptVariableMap {
73
73
  }
74
74
  type PromptId = keyof PromptVariableMap | (string & {});
75
75
  type PromptVersion<Id extends string> = Id extends keyof PromptVariableMap ? Exclude<keyof PromptVariableMap[Id], 'latest'> : string;
76
- type VariablesFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof PromptVariableMap ? Ver extends keyof PromptVariableMap[Id] ? PromptVariableMap[Id][Ver] : Record<string, string> : Record<string, string>;
77
- type PromptMessage<V extends Record<string, string> = Record<string, string>> = {
76
+ type VariablesFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof PromptVariableMap ? Ver extends keyof PromptVariableMap[Id] ? PromptVariableMap[Id][Ver] : Record<string, unknown> : Record<string, unknown>;
77
+ type PromptMessage<V extends Record<string, unknown> = Record<string, unknown>> = {
78
78
  (variables: V): string;
79
79
  toString(): string;
80
80
  };
81
- type PromptResult<V extends Record<string, string> = Record<string, string>> = Omit<PromptResponse, 'userMessage'> & {
81
+ type PromptResult<V extends Record<string, unknown> = Record<string, unknown>> = Omit<PromptResponse, 'userMessage'> & {
82
82
  userMessage: PromptMessage<V>;
83
83
  temperature: number;
84
84
  model: ai.LanguageModel;
@@ -130,7 +130,7 @@ interface ComposerPromptMap {
130
130
  }
131
131
  type ComposerId = keyof ComposerVariableMap | (string & {});
132
132
  type ComposerVersion<Id extends string> = Id extends keyof ComposerVariableMap ? Exclude<keyof ComposerVariableMap[Id], 'latest'> : string;
133
- type ComposerInputFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof ComposerVariableMap ? Ver extends keyof ComposerVariableMap[Id] ? ComposerVariableMap[Id][Ver] : Record<string, string> : Record<string, string>;
133
+ type ComposerInputFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof ComposerVariableMap ? Ver extends keyof ComposerVariableMap[Id] ? ComposerVariableMap[Id][Ver] : Record<string, unknown> : Record<string, unknown>;
134
134
  type ComposerPromptNamesFor<Id extends string> = Id extends keyof ComposerPromptMap ? ComposerPromptMap[Id] : string;
135
135
  type ComposerPrompt = {
136
136
  model: ai.LanguageModel;
@@ -165,7 +165,7 @@ type GetComposerOptions<Id extends string = string, V extends string = 'latest'>
165
165
  };
166
166
  type ComposerRequest = {
167
167
  composerId: string;
168
- input?: Record<string, string>;
168
+ input?: Record<string, unknown>;
169
169
  version?: string;
170
170
  };
171
171
  type GetComposersResults<T extends readonly ComposerRequest[]> = {
@@ -73,12 +73,12 @@ interface PromptVariableMap {
73
73
  }
74
74
  type PromptId = keyof PromptVariableMap | (string & {});
75
75
  type PromptVersion<Id extends string> = Id extends keyof PromptVariableMap ? Exclude<keyof PromptVariableMap[Id], 'latest'> : string;
76
- type VariablesFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof PromptVariableMap ? Ver extends keyof PromptVariableMap[Id] ? PromptVariableMap[Id][Ver] : Record<string, string> : Record<string, string>;
77
- type PromptMessage<V extends Record<string, string> = Record<string, string>> = {
76
+ type VariablesFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof PromptVariableMap ? Ver extends keyof PromptVariableMap[Id] ? PromptVariableMap[Id][Ver] : Record<string, unknown> : Record<string, unknown>;
77
+ type PromptMessage<V extends Record<string, unknown> = Record<string, unknown>> = {
78
78
  (variables: V): string;
79
79
  toString(): string;
80
80
  };
81
- type PromptResult<V extends Record<string, string> = Record<string, string>> = Omit<PromptResponse, 'userMessage'> & {
81
+ type PromptResult<V extends Record<string, unknown> = Record<string, unknown>> = Omit<PromptResponse, 'userMessage'> & {
82
82
  userMessage: PromptMessage<V>;
83
83
  temperature: number;
84
84
  model: ai.LanguageModel;
@@ -130,7 +130,7 @@ interface ComposerPromptMap {
130
130
  }
131
131
  type ComposerId = keyof ComposerVariableMap | (string & {});
132
132
  type ComposerVersion<Id extends string> = Id extends keyof ComposerVariableMap ? Exclude<keyof ComposerVariableMap[Id], 'latest'> : string;
133
- type ComposerInputFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof ComposerVariableMap ? Ver extends keyof ComposerVariableMap[Id] ? ComposerVariableMap[Id][Ver] : Record<string, string> : Record<string, string>;
133
+ type ComposerInputFor<Id extends string, Ver extends string = 'latest'> = Id extends keyof ComposerVariableMap ? Ver extends keyof ComposerVariableMap[Id] ? ComposerVariableMap[Id][Ver] : Record<string, unknown> : Record<string, unknown>;
134
134
  type ComposerPromptNamesFor<Id extends string> = Id extends keyof ComposerPromptMap ? ComposerPromptMap[Id] : string;
135
135
  type ComposerPrompt = {
136
136
  model: ai.LanguageModel;
@@ -165,7 +165,7 @@ type GetComposerOptions<Id extends string = string, V extends string = 'latest'>
165
165
  };
166
166
  type ComposerRequest = {
167
167
  composerId: string;
168
- input?: Record<string, string>;
168
+ input?: Record<string, unknown>;
169
169
  version?: string;
170
170
  };
171
171
  type GetComposersResults<T extends readonly ComposerRequest[]> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptlycms/prompts",
3
- "version": "0.3.0",
3
+ "version": "0.4.0-canary.375b6a8",
4
4
  "description": "TypeScript SDK for Promptly CMS — fetch prompts, build Zod schemas, generate typed code",
5
5
  "type": "module",
6
6
  "exports": {
@@ -50,7 +50,7 @@
50
50
  "zod": "^4.3.6"
51
51
  },
52
52
  "peerDependencies": {
53
- "typescript": "^6.0.2",
53
+ "typescript": "^5.0.0 || ^6.0.0",
54
54
  "zod": "^4.0.0",
55
55
  "ai": "^6.0.137"
56
56
  },