prompts.chat 0.0.1 → 0.0.3
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 +856 -0
- package/dist/builder/index.d.mts +1067 -0
- package/dist/builder/index.d.ts +1067 -0
- package/dist/builder/index.js +2471 -0
- package/dist/builder/index.js.map +1 -0
- package/dist/builder/index.mjs +2432 -0
- package/dist/builder/index.mjs.map +1 -0
- package/dist/index-BEIO8LCd.d.mts +61 -0
- package/dist/index-BEIO8LCd.d.ts +61 -0
- package/dist/index-CSHEKYfQ.d.mts +64 -0
- package/dist/index-CSHEKYfQ.d.ts +64 -0
- package/dist/index-D41E6D9X.d.mts +77 -0
- package/dist/index-D41E6D9X.d.ts +77 -0
- package/dist/index-DOz8zcA0.d.mts +68 -0
- package/dist/index-DOz8zcA0.d.ts +68 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3335 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3276 -0
- package/dist/index.mjs.map +1 -0
- package/dist/parser/index.d.mts +1 -0
- package/dist/parser/index.d.ts +1 -0
- package/dist/parser/index.js +288 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/index.mjs +259 -0
- package/dist/parser/index.mjs.map +1 -0
- package/dist/quality/index.d.mts +1 -0
- package/dist/quality/index.d.ts +1 -0
- package/dist/quality/index.js +212 -0
- package/dist/quality/index.js.map +1 -0
- package/dist/quality/index.mjs +184 -0
- package/dist/quality/index.mjs.map +1 -0
- package/dist/similarity/index.d.mts +1 -0
- package/dist/similarity/index.d.ts +1 -0
- package/dist/similarity/index.js +123 -0
- package/dist/similarity/index.js.map +1 -0
- package/dist/similarity/index.mjs +91 -0
- package/dist/similarity/index.mjs.map +1 -0
- package/dist/variables/index.d.mts +1 -0
- package/dist/variables/index.d.ts +1 -0
- package/dist/variables/index.js +306 -0
- package/dist/variables/index.js.map +1 -0
- package/dist/variables/index.mjs +274 -0
- package/dist/variables/index.mjs.map +1 -0
- package/package.json +77 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,3335 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
AudioPromptBuilder: () => AudioPromptBuilder,
|
|
24
|
+
ChatPromptBuilder: () => ChatPromptBuilder,
|
|
25
|
+
ImagePromptBuilder: () => ImagePromptBuilder,
|
|
26
|
+
PromptBuilder: () => PromptBuilder,
|
|
27
|
+
VideoPromptBuilder: () => VideoPromptBuilder,
|
|
28
|
+
audio: () => audio,
|
|
29
|
+
builder: () => builder,
|
|
30
|
+
calculateSimilarity: () => calculateSimilarity,
|
|
31
|
+
chat: () => chat,
|
|
32
|
+
chatPresets: () => chatPresets,
|
|
33
|
+
checkQuality: () => check,
|
|
34
|
+
compile: () => compile,
|
|
35
|
+
convertAllVariables: () => convertAllVariables,
|
|
36
|
+
convertToSupportedFormat: () => convertToSupportedFormat,
|
|
37
|
+
deduplicate: () => deduplicate,
|
|
38
|
+
detectVariables: () => detectVariables,
|
|
39
|
+
detectVars: () => detect,
|
|
40
|
+
extractVariables: () => extractVariables,
|
|
41
|
+
findDuplicates: () => findDuplicates,
|
|
42
|
+
fromPrompt: () => fromPrompt,
|
|
43
|
+
getContentFingerprint: () => getContentFingerprint,
|
|
44
|
+
getSuggestions: () => getSuggestions,
|
|
45
|
+
getSystemPrompt: () => getSystemPrompt,
|
|
46
|
+
image: () => image,
|
|
47
|
+
interpolate: () => interpolate,
|
|
48
|
+
isSimilarContent: () => isSimilarContent,
|
|
49
|
+
isValidPrompt: () => isValid,
|
|
50
|
+
normalizeContent: () => normalizeContent,
|
|
51
|
+
normalizeVariables: () => normalize,
|
|
52
|
+
parsePrompt: () => parse,
|
|
53
|
+
parser: () => parser_exports,
|
|
54
|
+
quality: () => quality_exports,
|
|
55
|
+
similarity: () => similarity_exports,
|
|
56
|
+
templates: () => templates,
|
|
57
|
+
toJson: () => toJson,
|
|
58
|
+
toYaml: () => toYaml,
|
|
59
|
+
validatePrompt: () => validate,
|
|
60
|
+
variables: () => variables_exports,
|
|
61
|
+
video: () => video
|
|
62
|
+
});
|
|
63
|
+
module.exports = __toCommonJS(src_exports);
|
|
64
|
+
|
|
65
|
+
// src/variables/index.ts
|
|
66
|
+
var variables_exports = {};
|
|
67
|
+
__export(variables_exports, {
|
|
68
|
+
compile: () => compile,
|
|
69
|
+
convertAllVariables: () => convertAllVariables,
|
|
70
|
+
convertToSupportedFormat: () => convertToSupportedFormat,
|
|
71
|
+
detect: () => detect,
|
|
72
|
+
detectVariables: () => detectVariables,
|
|
73
|
+
extractVariables: () => extractVariables,
|
|
74
|
+
getPatternDescription: () => getPatternDescription,
|
|
75
|
+
normalize: () => normalize
|
|
76
|
+
});
|
|
77
|
+
var PATTERNS = [
|
|
78
|
+
// Double bracket: [[name]] or [[ name ]] or [[name: default]]
|
|
79
|
+
{
|
|
80
|
+
pattern: "double_bracket",
|
|
81
|
+
regex: /\[\[\s*([a-zA-Z_][a-zA-Z0-9_\s]*?)(?:\s*:\s*([^\]]*?))?\s*\]\]/g,
|
|
82
|
+
extractName: (m) => m[1].trim(),
|
|
83
|
+
extractDefault: (m) => m[2]?.trim()
|
|
84
|
+
},
|
|
85
|
+
// Double curly: {{name}} or {{ name }} or {{name: default}}
|
|
86
|
+
{
|
|
87
|
+
pattern: "double_curly",
|
|
88
|
+
regex: /\{\{\s*([a-zA-Z_][a-zA-Z0-9_\s]*?)(?:\s*:\s*([^}]*?))?\s*\}\}/g,
|
|
89
|
+
extractName: (m) => m[1].trim(),
|
|
90
|
+
extractDefault: (m) => m[2]?.trim()
|
|
91
|
+
},
|
|
92
|
+
// Our supported format (to exclude from warnings)
|
|
93
|
+
{
|
|
94
|
+
pattern: "dollar_curly",
|
|
95
|
+
regex: /\$\{([a-zA-Z_][a-zA-Z0-9_\s]*?)(?::([^}]*))?\}/g,
|
|
96
|
+
extractName: (m) => m[1].trim()
|
|
97
|
+
},
|
|
98
|
+
// Single bracket with uppercase or placeholder-like: [NAME] or [Your Name]
|
|
99
|
+
{
|
|
100
|
+
pattern: "single_bracket",
|
|
101
|
+
regex: /\[([A-Z][A-Z0-9_\s]*|[A-Za-z][a-zA-Z0-9_]*(?:\s+[A-Za-z][a-zA-Z0-9_]*)*)\]/g,
|
|
102
|
+
extractName: (m) => m[1].trim()
|
|
103
|
+
},
|
|
104
|
+
// Single curly with uppercase: {NAME} or {Your Name}
|
|
105
|
+
{
|
|
106
|
+
pattern: "single_curly",
|
|
107
|
+
regex: /\{([A-Z][A-Z0-9_\s]*|[A-Za-z][a-zA-Z0-9_]*(?:\s+[A-Za-z][a-zA-Z0-9_]*)*)\}/g,
|
|
108
|
+
extractName: (m) => m[1].trim()
|
|
109
|
+
},
|
|
110
|
+
// Angle brackets: <NAME> or <name>
|
|
111
|
+
{
|
|
112
|
+
pattern: "angle_bracket",
|
|
113
|
+
regex: /<([A-Z][A-Z0-9_\s]*|[a-zA-Z_][a-zA-Z0-9_\s]*)>/g,
|
|
114
|
+
extractName: (m) => m[1].trim()
|
|
115
|
+
},
|
|
116
|
+
// Percent signs: %NAME% or %name%
|
|
117
|
+
{
|
|
118
|
+
pattern: "percent",
|
|
119
|
+
regex: /%([a-zA-Z_][a-zA-Z0-9_]*)%/g,
|
|
120
|
+
extractName: (m) => m[1].trim()
|
|
121
|
+
}
|
|
122
|
+
];
|
|
123
|
+
var FALSE_POSITIVES = /* @__PURE__ */ new Set([
|
|
124
|
+
// HTML/XML common tags
|
|
125
|
+
"div",
|
|
126
|
+
"span",
|
|
127
|
+
"p",
|
|
128
|
+
"a",
|
|
129
|
+
"br",
|
|
130
|
+
"hr",
|
|
131
|
+
"img",
|
|
132
|
+
"input",
|
|
133
|
+
"button",
|
|
134
|
+
"h1",
|
|
135
|
+
"h2",
|
|
136
|
+
"h3",
|
|
137
|
+
"h4",
|
|
138
|
+
"h5",
|
|
139
|
+
"h6",
|
|
140
|
+
"ul",
|
|
141
|
+
"ol",
|
|
142
|
+
"li",
|
|
143
|
+
"table",
|
|
144
|
+
"tr",
|
|
145
|
+
"td",
|
|
146
|
+
"th",
|
|
147
|
+
"thead",
|
|
148
|
+
"tbody",
|
|
149
|
+
"form",
|
|
150
|
+
"label",
|
|
151
|
+
"select",
|
|
152
|
+
"option",
|
|
153
|
+
"textarea",
|
|
154
|
+
"script",
|
|
155
|
+
"style",
|
|
156
|
+
"link",
|
|
157
|
+
"meta",
|
|
158
|
+
"head",
|
|
159
|
+
"body",
|
|
160
|
+
"html",
|
|
161
|
+
"section",
|
|
162
|
+
"article",
|
|
163
|
+
"nav",
|
|
164
|
+
"header",
|
|
165
|
+
"footer",
|
|
166
|
+
"main",
|
|
167
|
+
"aside",
|
|
168
|
+
"figure",
|
|
169
|
+
"figcaption",
|
|
170
|
+
"strong",
|
|
171
|
+
"em",
|
|
172
|
+
"code",
|
|
173
|
+
"pre",
|
|
174
|
+
"blockquote",
|
|
175
|
+
"cite",
|
|
176
|
+
"abbr",
|
|
177
|
+
"address",
|
|
178
|
+
"b",
|
|
179
|
+
"i",
|
|
180
|
+
"u",
|
|
181
|
+
// Common programming constructs
|
|
182
|
+
"if",
|
|
183
|
+
"else",
|
|
184
|
+
"for",
|
|
185
|
+
"while",
|
|
186
|
+
"switch",
|
|
187
|
+
"case",
|
|
188
|
+
"break",
|
|
189
|
+
"return",
|
|
190
|
+
"function",
|
|
191
|
+
"class",
|
|
192
|
+
"const",
|
|
193
|
+
"let",
|
|
194
|
+
"var",
|
|
195
|
+
"import",
|
|
196
|
+
"export",
|
|
197
|
+
"default",
|
|
198
|
+
"try",
|
|
199
|
+
"catch",
|
|
200
|
+
"finally",
|
|
201
|
+
"throw",
|
|
202
|
+
"new",
|
|
203
|
+
"this",
|
|
204
|
+
"null",
|
|
205
|
+
"undefined",
|
|
206
|
+
"true",
|
|
207
|
+
"false",
|
|
208
|
+
"typeof",
|
|
209
|
+
"instanceof",
|
|
210
|
+
// JSON structure keywords (when in context)
|
|
211
|
+
"type",
|
|
212
|
+
"id",
|
|
213
|
+
"key",
|
|
214
|
+
"value",
|
|
215
|
+
"data",
|
|
216
|
+
"items",
|
|
217
|
+
"properties"
|
|
218
|
+
]);
|
|
219
|
+
function isInsideJsonString(text, index) {
|
|
220
|
+
let inString = false;
|
|
221
|
+
for (let i = 0; i < index; i++) {
|
|
222
|
+
if (text[i] === '"' && (i === 0 || text[i - 1] !== "\\")) {
|
|
223
|
+
inString = !inString;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return inString;
|
|
227
|
+
}
|
|
228
|
+
function detectVariables(text) {
|
|
229
|
+
const detected = [];
|
|
230
|
+
const seenRanges = [];
|
|
231
|
+
const supportedVars = /* @__PURE__ */ new Set();
|
|
232
|
+
const dollarCurlyPattern = /\$\{([a-zA-Z_][a-zA-Z0-9_\s]*?)(?::([^}]*))?\}/g;
|
|
233
|
+
let match;
|
|
234
|
+
while ((match = dollarCurlyPattern.exec(text)) !== null) {
|
|
235
|
+
seenRanges.push([match.index, match.index + match[0].length]);
|
|
236
|
+
supportedVars.add(match[0]);
|
|
237
|
+
}
|
|
238
|
+
for (const config of PATTERNS) {
|
|
239
|
+
if (config.pattern === "dollar_curly") continue;
|
|
240
|
+
const regex = new RegExp(config.regex.source, config.regex.flags);
|
|
241
|
+
while ((match = regex.exec(text)) !== null) {
|
|
242
|
+
const startIndex = match.index;
|
|
243
|
+
const endIndex = startIndex + match[0].length;
|
|
244
|
+
const overlaps = seenRanges.some(
|
|
245
|
+
([start, end]) => startIndex >= start && startIndex < end || endIndex > start && endIndex <= end
|
|
246
|
+
);
|
|
247
|
+
if (overlaps) continue;
|
|
248
|
+
const name = config.extractName(match);
|
|
249
|
+
if (FALSE_POSITIVES.has(name.toLowerCase())) continue;
|
|
250
|
+
if (name.length < 2) continue;
|
|
251
|
+
if (config.pattern === "angle_bracket") {
|
|
252
|
+
if (!/^[A-Z]/.test(name) && !name.includes(" ")) continue;
|
|
253
|
+
}
|
|
254
|
+
if ((config.pattern === "single_curly" || config.pattern === "single_bracket") && isInsideJsonString(text, startIndex)) {
|
|
255
|
+
if (!/^[A-Z]/.test(name) && !name.includes(" ")) continue;
|
|
256
|
+
}
|
|
257
|
+
const defaultValue = config.extractDefault?.(match);
|
|
258
|
+
detected.push({
|
|
259
|
+
original: match[0],
|
|
260
|
+
name,
|
|
261
|
+
defaultValue,
|
|
262
|
+
pattern: config.pattern,
|
|
263
|
+
startIndex,
|
|
264
|
+
endIndex
|
|
265
|
+
});
|
|
266
|
+
seenRanges.push([startIndex, endIndex]);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return detected.sort((a, b) => a.startIndex - b.startIndex).filter(
|
|
270
|
+
(v, i, arr) => i === 0 || v.original !== arr[i - 1].original || v.startIndex !== arr[i - 1].startIndex
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
function convertToSupportedFormat(variable) {
|
|
274
|
+
const normalizedName = variable.name.toLowerCase().replace(/\s+/g, "_").replace(/[^a-z0-9_]/g, "");
|
|
275
|
+
if (variable.defaultValue) {
|
|
276
|
+
return `\${${normalizedName}:${variable.defaultValue}}`;
|
|
277
|
+
}
|
|
278
|
+
return `\${${normalizedName}}`;
|
|
279
|
+
}
|
|
280
|
+
function convertAllVariables(text) {
|
|
281
|
+
const detected = detectVariables(text);
|
|
282
|
+
if (detected.length === 0) return text;
|
|
283
|
+
const sorted = [...detected].sort((a, b) => b.startIndex - a.startIndex);
|
|
284
|
+
let result = text;
|
|
285
|
+
for (const variable of sorted) {
|
|
286
|
+
const converted = convertToSupportedFormat(variable);
|
|
287
|
+
result = result.slice(0, variable.startIndex) + converted + result.slice(variable.endIndex);
|
|
288
|
+
}
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
var normalize = convertAllVariables;
|
|
292
|
+
var detect = detectVariables;
|
|
293
|
+
function getPatternDescription(pattern) {
|
|
294
|
+
switch (pattern) {
|
|
295
|
+
case "double_bracket":
|
|
296
|
+
return "[[...]]";
|
|
297
|
+
case "double_curly":
|
|
298
|
+
return "{{...}}";
|
|
299
|
+
case "single_bracket":
|
|
300
|
+
return "[...]";
|
|
301
|
+
case "single_curly":
|
|
302
|
+
return "{...}";
|
|
303
|
+
case "angle_bracket":
|
|
304
|
+
return "<...>";
|
|
305
|
+
case "percent":
|
|
306
|
+
return "%...%";
|
|
307
|
+
case "dollar_curly":
|
|
308
|
+
return "${...}";
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
function extractVariables(text) {
|
|
312
|
+
const regex = /\$\{([a-zA-Z_][a-zA-Z0-9_\s]*?)(?::([^}]*))?\}/g;
|
|
313
|
+
const variables = [];
|
|
314
|
+
let match;
|
|
315
|
+
while ((match = regex.exec(text)) !== null) {
|
|
316
|
+
variables.push({
|
|
317
|
+
name: match[1].trim(),
|
|
318
|
+
defaultValue: match[2]?.trim()
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
return variables;
|
|
322
|
+
}
|
|
323
|
+
function compile(template, values, options = {}) {
|
|
324
|
+
const { useDefaults = true } = options;
|
|
325
|
+
return template.replace(
|
|
326
|
+
/\$\{([a-zA-Z_][a-zA-Z0-9_\s]*?)(?::([^}]*))?\}/g,
|
|
327
|
+
(match, name, defaultValue) => {
|
|
328
|
+
const trimmedName = name.trim();
|
|
329
|
+
if (trimmedName in values) {
|
|
330
|
+
return values[trimmedName];
|
|
331
|
+
}
|
|
332
|
+
if (useDefaults && defaultValue !== void 0) {
|
|
333
|
+
return defaultValue.trim();
|
|
334
|
+
}
|
|
335
|
+
return match;
|
|
336
|
+
}
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// src/similarity/index.ts
|
|
341
|
+
var similarity_exports = {};
|
|
342
|
+
__export(similarity_exports, {
|
|
343
|
+
calculate: () => calculate,
|
|
344
|
+
calculateSimilarity: () => calculateSimilarity,
|
|
345
|
+
deduplicate: () => deduplicate,
|
|
346
|
+
findDuplicates: () => findDuplicates,
|
|
347
|
+
getContentFingerprint: () => getContentFingerprint,
|
|
348
|
+
isDuplicate: () => isDuplicate,
|
|
349
|
+
isSimilarContent: () => isSimilarContent,
|
|
350
|
+
normalizeContent: () => normalizeContent
|
|
351
|
+
});
|
|
352
|
+
function normalizeContent(content) {
|
|
353
|
+
return content.replace(/\$\{[^}]+\}/g, "").replace(/\[[^\]]+\]/g, "").replace(/<[^>]+>/g, "").toLowerCase().replace(/[^\w\s]/g, "").replace(/\s+/g, " ").trim();
|
|
354
|
+
}
|
|
355
|
+
function jaccardSimilarity(str1, str2) {
|
|
356
|
+
const set1 = new Set(str1.split(" ").filter(Boolean));
|
|
357
|
+
const set2 = new Set(str2.split(" ").filter(Boolean));
|
|
358
|
+
if (set1.size === 0 && set2.size === 0) return 1;
|
|
359
|
+
if (set1.size === 0 || set2.size === 0) return 0;
|
|
360
|
+
const intersection = new Set([...set1].filter((x) => set2.has(x)));
|
|
361
|
+
const union = /* @__PURE__ */ new Set([...set1, ...set2]);
|
|
362
|
+
return intersection.size / union.size;
|
|
363
|
+
}
|
|
364
|
+
function ngramSimilarity(str1, str2, n = 3) {
|
|
365
|
+
const getNgrams = (str) => {
|
|
366
|
+
const ngrams = /* @__PURE__ */ new Set();
|
|
367
|
+
const padded = " ".repeat(n - 1) + str + " ".repeat(n - 1);
|
|
368
|
+
for (let i = 0; i <= padded.length - n; i++) {
|
|
369
|
+
ngrams.add(padded.slice(i, i + n));
|
|
370
|
+
}
|
|
371
|
+
return ngrams;
|
|
372
|
+
};
|
|
373
|
+
const ngrams1 = getNgrams(str1);
|
|
374
|
+
const ngrams2 = getNgrams(str2);
|
|
375
|
+
if (ngrams1.size === 0 && ngrams2.size === 0) return 1;
|
|
376
|
+
if (ngrams1.size === 0 || ngrams2.size === 0) return 0;
|
|
377
|
+
const intersection = new Set([...ngrams1].filter((x) => ngrams2.has(x)));
|
|
378
|
+
const union = /* @__PURE__ */ new Set([...ngrams1, ...ngrams2]);
|
|
379
|
+
return intersection.size / union.size;
|
|
380
|
+
}
|
|
381
|
+
function calculateSimilarity(content1, content2) {
|
|
382
|
+
const normalized1 = normalizeContent(content1);
|
|
383
|
+
const normalized2 = normalizeContent(content2);
|
|
384
|
+
if (normalized1 === normalized2) return 1;
|
|
385
|
+
if (!normalized1 || !normalized2) return 0;
|
|
386
|
+
const jaccard = jaccardSimilarity(normalized1, normalized2);
|
|
387
|
+
const ngram = ngramSimilarity(normalized1, normalized2);
|
|
388
|
+
return jaccard * 0.6 + ngram * 0.4;
|
|
389
|
+
}
|
|
390
|
+
var calculate = calculateSimilarity;
|
|
391
|
+
function isSimilarContent(content1, content2, threshold = 0.85) {
|
|
392
|
+
return calculateSimilarity(content1, content2) >= threshold;
|
|
393
|
+
}
|
|
394
|
+
var isDuplicate = isSimilarContent;
|
|
395
|
+
function getContentFingerprint(content) {
|
|
396
|
+
const normalized = normalizeContent(content);
|
|
397
|
+
return normalized.slice(0, 500);
|
|
398
|
+
}
|
|
399
|
+
function findDuplicates(prompts, threshold = 0.85) {
|
|
400
|
+
const groups = [];
|
|
401
|
+
const used = /* @__PURE__ */ new Set();
|
|
402
|
+
for (let i = 0; i < prompts.length; i++) {
|
|
403
|
+
if (used.has(i)) continue;
|
|
404
|
+
const group = [prompts[i]];
|
|
405
|
+
used.add(i);
|
|
406
|
+
for (let j = i + 1; j < prompts.length; j++) {
|
|
407
|
+
if (used.has(j)) continue;
|
|
408
|
+
if (isSimilarContent(prompts[i].content, prompts[j].content, threshold)) {
|
|
409
|
+
group.push(prompts[j]);
|
|
410
|
+
used.add(j);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (group.length > 1) {
|
|
414
|
+
groups.push(group);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return groups;
|
|
418
|
+
}
|
|
419
|
+
function deduplicate(prompts, threshold = 0.85) {
|
|
420
|
+
const result = [];
|
|
421
|
+
for (const prompt of prompts) {
|
|
422
|
+
const isDupe = result.some(
|
|
423
|
+
(existing) => isSimilarContent(existing.content, prompt.content, threshold)
|
|
424
|
+
);
|
|
425
|
+
if (!isDupe) {
|
|
426
|
+
result.push(prompt);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/quality/index.ts
|
|
433
|
+
var quality_exports = {};
|
|
434
|
+
__export(quality_exports, {
|
|
435
|
+
check: () => check,
|
|
436
|
+
getSuggestions: () => getSuggestions,
|
|
437
|
+
isValid: () => isValid,
|
|
438
|
+
validate: () => validate
|
|
439
|
+
});
|
|
440
|
+
var MIN_CHAR_COUNT = 20;
|
|
441
|
+
var MIN_WORD_COUNT = 5;
|
|
442
|
+
var OPTIMAL_MIN_WORDS = 20;
|
|
443
|
+
var OPTIMAL_MAX_WORDS = 2e3;
|
|
444
|
+
function isGibberish(text) {
|
|
445
|
+
if (/(.)\1{4,}/.test(text)) return true;
|
|
446
|
+
const keyboardPatterns = ["qwerty", "asdfgh", "zxcvbn", "qwertz", "azerty"];
|
|
447
|
+
const lower = text.toLowerCase();
|
|
448
|
+
if (keyboardPatterns.some((p) => lower.includes(p))) return true;
|
|
449
|
+
const vowels = (text.match(/[aeiouAEIOU]/g) || []).length;
|
|
450
|
+
const consonants = (text.match(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/g) || []).length;
|
|
451
|
+
if (consonants > 0 && vowels / consonants < 0.1) return true;
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
function detectPatterns(text) {
|
|
455
|
+
const lower = text.toLowerCase();
|
|
456
|
+
return {
|
|
457
|
+
hasRole: /\b(act as|you are|imagine you|pretend to be|role:|persona:)\b/i.test(text),
|
|
458
|
+
hasTask: /\b(your task|you (will|should|must)|please|help me|i need|i want you to)\b/i.test(text),
|
|
459
|
+
hasConstraints: /\b(do not|don't|never|always|must|should not|avoid|only|limit)\b/i.test(text) || /\b(rule|constraint|requirement|guideline)\b/i.test(lower),
|
|
460
|
+
hasExamples: /\b(example|for instance|such as|e\.g\.|like this)\b/i.test(lower) || /```[\s\S]*```/.test(text)
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
function countVariables(text) {
|
|
464
|
+
const patterns = [
|
|
465
|
+
/\$\{[^}]+\}/g,
|
|
466
|
+
// ${var}
|
|
467
|
+
/\{\{[^}]+\}\}/g,
|
|
468
|
+
// {{var}}
|
|
469
|
+
/\[\[[^\]]+\]\]/g,
|
|
470
|
+
// [[var]]
|
|
471
|
+
/\[[A-Z][A-Z0-9_\s]*\]/g
|
|
472
|
+
// [VAR]
|
|
473
|
+
];
|
|
474
|
+
let count = 0;
|
|
475
|
+
for (const pattern of patterns) {
|
|
476
|
+
const matches = text.match(pattern);
|
|
477
|
+
if (matches) count += matches.length;
|
|
478
|
+
}
|
|
479
|
+
return count;
|
|
480
|
+
}
|
|
481
|
+
function calculateScore(stats, issues) {
|
|
482
|
+
let score = 1;
|
|
483
|
+
const errors = issues.filter((i) => i.type === "error").length;
|
|
484
|
+
const warnings = issues.filter((i) => i.type === "warning").length;
|
|
485
|
+
score -= errors * 0.2;
|
|
486
|
+
score -= warnings * 0.05;
|
|
487
|
+
if (stats.hasRole) score += 0.05;
|
|
488
|
+
if (stats.hasTask) score += 0.05;
|
|
489
|
+
if (stats.hasConstraints) score += 0.03;
|
|
490
|
+
if (stats.hasExamples) score += 0.05;
|
|
491
|
+
if (stats.wordCount < OPTIMAL_MIN_WORDS) {
|
|
492
|
+
score -= 0.1 * (1 - stats.wordCount / OPTIMAL_MIN_WORDS);
|
|
493
|
+
}
|
|
494
|
+
if (stats.wordCount > OPTIMAL_MAX_WORDS) {
|
|
495
|
+
score -= 0.05;
|
|
496
|
+
}
|
|
497
|
+
if (stats.variableCount > 0) {
|
|
498
|
+
score += 0.05;
|
|
499
|
+
}
|
|
500
|
+
return Math.max(0, Math.min(1, score));
|
|
501
|
+
}
|
|
502
|
+
function check(prompt) {
|
|
503
|
+
const issues = [];
|
|
504
|
+
const trimmed = prompt.trim();
|
|
505
|
+
const characterCount = trimmed.length;
|
|
506
|
+
const words = trimmed.split(/\s+/).filter((w) => w.length > 0);
|
|
507
|
+
const wordCount = words.length;
|
|
508
|
+
const sentenceCount = (trimmed.match(/[.!?]+/g) || []).length || 1;
|
|
509
|
+
const variableCount = countVariables(trimmed);
|
|
510
|
+
const patterns = detectPatterns(trimmed);
|
|
511
|
+
if (characterCount === 0) {
|
|
512
|
+
issues.push({
|
|
513
|
+
type: "error",
|
|
514
|
+
code: "EMPTY",
|
|
515
|
+
message: "Prompt is empty"
|
|
516
|
+
});
|
|
517
|
+
} else if (characterCount < MIN_CHAR_COUNT) {
|
|
518
|
+
issues.push({
|
|
519
|
+
type: "error",
|
|
520
|
+
code: "TOO_SHORT",
|
|
521
|
+
message: `Prompt is too short (${characterCount} chars, minimum ${MIN_CHAR_COUNT})`
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
if (wordCount > 0 && wordCount < MIN_WORD_COUNT) {
|
|
525
|
+
issues.push({
|
|
526
|
+
type: "warning",
|
|
527
|
+
code: "FEW_WORDS",
|
|
528
|
+
message: `Prompt has very few words (${wordCount} words, recommended ${OPTIMAL_MIN_WORDS}+)`
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
if (isGibberish(trimmed)) {
|
|
532
|
+
issues.push({
|
|
533
|
+
type: "error",
|
|
534
|
+
code: "GIBBERISH",
|
|
535
|
+
message: "Prompt appears to contain gibberish or random characters"
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
if (!patterns.hasTask && !patterns.hasRole) {
|
|
539
|
+
issues.push({
|
|
540
|
+
type: "suggestion",
|
|
541
|
+
code: "NO_CLEAR_INSTRUCTION",
|
|
542
|
+
message: "Consider adding a clear task or role definition"
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
const brackets = [
|
|
546
|
+
{ open: "{", close: "}" },
|
|
547
|
+
{ open: "[", close: "]" },
|
|
548
|
+
{ open: "(", close: ")" }
|
|
549
|
+
];
|
|
550
|
+
for (const { open, close } of brackets) {
|
|
551
|
+
const openCount = (trimmed.match(new RegExp(`\\${open}`, "g")) || []).length;
|
|
552
|
+
const closeCount = (trimmed.match(new RegExp(`\\${close}`, "g")) || []).length;
|
|
553
|
+
if (openCount !== closeCount) {
|
|
554
|
+
issues.push({
|
|
555
|
+
type: "warning",
|
|
556
|
+
code: "UNBALANCED_BRACKETS",
|
|
557
|
+
message: `Unbalanced ${open}${close} brackets (${openCount} open, ${closeCount} close)`
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const lines = trimmed.split("\n");
|
|
562
|
+
const longLines = lines.filter((l) => l.length > 500);
|
|
563
|
+
if (longLines.length > 0) {
|
|
564
|
+
issues.push({
|
|
565
|
+
type: "suggestion",
|
|
566
|
+
code: "LONG_LINES",
|
|
567
|
+
message: "Some lines are very long. Consider breaking them up for readability."
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
const stats = {
|
|
571
|
+
characterCount,
|
|
572
|
+
wordCount,
|
|
573
|
+
sentenceCount,
|
|
574
|
+
variableCount,
|
|
575
|
+
...patterns
|
|
576
|
+
};
|
|
577
|
+
const score = calculateScore(stats, issues);
|
|
578
|
+
const hasErrors = issues.some((i) => i.type === "error");
|
|
579
|
+
return {
|
|
580
|
+
valid: !hasErrors,
|
|
581
|
+
score,
|
|
582
|
+
issues,
|
|
583
|
+
stats
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
function validate(prompt) {
|
|
587
|
+
const result = check(prompt);
|
|
588
|
+
if (!result.valid) {
|
|
589
|
+
const errors = result.issues.filter((i) => i.type === "error").map((i) => i.message).join("; ");
|
|
590
|
+
throw new Error(`Invalid prompt: ${errors}`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
function isValid(prompt) {
|
|
594
|
+
return check(prompt).valid;
|
|
595
|
+
}
|
|
596
|
+
function getSuggestions(prompt) {
|
|
597
|
+
const result = check(prompt);
|
|
598
|
+
const suggestions = [];
|
|
599
|
+
suggestions.push(
|
|
600
|
+
...result.issues.filter((i) => i.type === "suggestion" || i.type === "warning").map((i) => i.message)
|
|
601
|
+
);
|
|
602
|
+
if (!result.stats.hasRole) {
|
|
603
|
+
suggestions.push('Add a role definition (e.g., "Act as a...")');
|
|
604
|
+
}
|
|
605
|
+
if (!result.stats.hasConstraints && result.stats.wordCount > 50) {
|
|
606
|
+
suggestions.push("Consider adding constraints or rules for better control");
|
|
607
|
+
}
|
|
608
|
+
if (!result.stats.hasExamples && result.stats.wordCount > 100) {
|
|
609
|
+
suggestions.push("Adding examples can improve output quality");
|
|
610
|
+
}
|
|
611
|
+
if (result.stats.variableCount === 0 && result.stats.wordCount > 30) {
|
|
612
|
+
suggestions.push("Consider adding variables (${var}) to make the prompt reusable");
|
|
613
|
+
}
|
|
614
|
+
return suggestions;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// src/parser/index.ts
|
|
618
|
+
var parser_exports = {};
|
|
619
|
+
__export(parser_exports, {
|
|
620
|
+
getSystemPrompt: () => getSystemPrompt,
|
|
621
|
+
interpolate: () => interpolate,
|
|
622
|
+
parse: () => parse,
|
|
623
|
+
toJson: () => toJson,
|
|
624
|
+
toYaml: () => toYaml
|
|
625
|
+
});
|
|
626
|
+
function parseSimpleYaml(content) {
|
|
627
|
+
const result = {};
|
|
628
|
+
const lines = content.split("\n");
|
|
629
|
+
let currentKey = null;
|
|
630
|
+
let currentValue = null;
|
|
631
|
+
let inArray = false;
|
|
632
|
+
let inMultiline = false;
|
|
633
|
+
let multilineContent = "";
|
|
634
|
+
let arrayItems = [];
|
|
635
|
+
let indent = 0;
|
|
636
|
+
for (let i = 0; i < lines.length; i++) {
|
|
637
|
+
const line = lines[i];
|
|
638
|
+
const trimmed = line.trim();
|
|
639
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
640
|
+
if (inMultiline) {
|
|
641
|
+
multilineContent += "\n";
|
|
642
|
+
}
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
if (inMultiline) {
|
|
646
|
+
const lineIndent = line.search(/\S/);
|
|
647
|
+
if (lineIndent > indent) {
|
|
648
|
+
multilineContent += (multilineContent ? "\n" : "") + line.slice(indent + 2);
|
|
649
|
+
continue;
|
|
650
|
+
} else {
|
|
651
|
+
if (inArray && currentKey) {
|
|
652
|
+
const lastItem = arrayItems[arrayItems.length - 1];
|
|
653
|
+
if (lastItem && typeof lastItem === "object") {
|
|
654
|
+
const keys = Object.keys(lastItem);
|
|
655
|
+
const lastKey = keys[keys.length - 1];
|
|
656
|
+
lastItem[lastKey] = multilineContent.trim();
|
|
657
|
+
}
|
|
658
|
+
} else if (currentKey) {
|
|
659
|
+
result[currentKey] = multilineContent.trim();
|
|
660
|
+
}
|
|
661
|
+
inMultiline = false;
|
|
662
|
+
multilineContent = "";
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (trimmed.startsWith("- ")) {
|
|
666
|
+
if (!inArray && currentKey) {
|
|
667
|
+
inArray = true;
|
|
668
|
+
arrayItems = [];
|
|
669
|
+
}
|
|
670
|
+
const itemContent = trimmed.slice(2);
|
|
671
|
+
const kvMatch = itemContent.match(/^(\w+):\s*(.*)$/);
|
|
672
|
+
if (kvMatch) {
|
|
673
|
+
const obj = {};
|
|
674
|
+
obj[kvMatch[1]] = kvMatch[2] === "|" ? "" : kvMatch[2] || "";
|
|
675
|
+
if (kvMatch[2] === "|") {
|
|
676
|
+
inMultiline = true;
|
|
677
|
+
indent = line.search(/\S/);
|
|
678
|
+
multilineContent = "";
|
|
679
|
+
}
|
|
680
|
+
arrayItems.push(obj);
|
|
681
|
+
} else {
|
|
682
|
+
arrayItems.push(itemContent);
|
|
683
|
+
}
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
if (inArray && line.startsWith(" ")) {
|
|
687
|
+
const propMatch = trimmed.match(/^(\w+):\s*(.*)$/);
|
|
688
|
+
if (propMatch && arrayItems.length > 0) {
|
|
689
|
+
const lastItem = arrayItems[arrayItems.length - 1];
|
|
690
|
+
if (typeof lastItem === "object" && lastItem !== null) {
|
|
691
|
+
lastItem[propMatch[1]] = propMatch[2] === "|" ? "" : propMatch[2] || "";
|
|
692
|
+
if (propMatch[2] === "|") {
|
|
693
|
+
inMultiline = true;
|
|
694
|
+
indent = line.search(/\S/);
|
|
695
|
+
multilineContent = "";
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
if (inArray && !line.startsWith(" ") && !line.startsWith(" ")) {
|
|
702
|
+
if (currentKey) {
|
|
703
|
+
result[currentKey] = arrayItems;
|
|
704
|
+
}
|
|
705
|
+
inArray = false;
|
|
706
|
+
arrayItems = [];
|
|
707
|
+
}
|
|
708
|
+
const match = trimmed.match(/^(\w+):\s*(.*)$/);
|
|
709
|
+
if (match) {
|
|
710
|
+
currentKey = match[1];
|
|
711
|
+
const value = match[2];
|
|
712
|
+
if (value === "" || value === "|" || value === ">") {
|
|
713
|
+
if (value === "|" || value === ">") {
|
|
714
|
+
inMultiline = true;
|
|
715
|
+
indent = line.search(/\S/);
|
|
716
|
+
multilineContent = "";
|
|
717
|
+
}
|
|
718
|
+
} else if (value.startsWith('"') && value.endsWith('"')) {
|
|
719
|
+
result[currentKey] = value.slice(1, -1);
|
|
720
|
+
} else if (value.startsWith("'") && value.endsWith("'")) {
|
|
721
|
+
result[currentKey] = value.slice(1, -1);
|
|
722
|
+
} else if (value === "true") {
|
|
723
|
+
result[currentKey] = true;
|
|
724
|
+
} else if (value === "false") {
|
|
725
|
+
result[currentKey] = false;
|
|
726
|
+
} else if (!isNaN(Number(value))) {
|
|
727
|
+
result[currentKey] = Number(value);
|
|
728
|
+
} else {
|
|
729
|
+
result[currentKey] = value;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
if (inArray && currentKey) {
|
|
734
|
+
result[currentKey] = arrayItems;
|
|
735
|
+
}
|
|
736
|
+
if (inMultiline && currentKey) {
|
|
737
|
+
result[currentKey] = multilineContent.trim();
|
|
738
|
+
}
|
|
739
|
+
return result;
|
|
740
|
+
}
|
|
741
|
+
function parseJson(content) {
|
|
742
|
+
return JSON.parse(content);
|
|
743
|
+
}
|
|
744
|
+
function parseMarkdown(content) {
|
|
745
|
+
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
|
|
746
|
+
if (frontmatterMatch) {
|
|
747
|
+
const frontmatter = parseSimpleYaml(frontmatterMatch[1]);
|
|
748
|
+
const body = frontmatterMatch[2].trim();
|
|
749
|
+
return {
|
|
750
|
+
...frontmatter,
|
|
751
|
+
messages: [{ role: "system", content: body }]
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
return {
|
|
755
|
+
messages: [{ role: "system", content: content.trim() }]
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
function normalize2(data) {
|
|
759
|
+
const messages = [];
|
|
760
|
+
if (Array.isArray(data.messages)) {
|
|
761
|
+
for (const msg of data.messages) {
|
|
762
|
+
if (typeof msg === "object" && msg !== null) {
|
|
763
|
+
const m = msg;
|
|
764
|
+
messages.push({
|
|
765
|
+
role: m.role || "user",
|
|
766
|
+
content: String(m.content || "")
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
if (messages.length === 0 && typeof data.content === "string") {
|
|
772
|
+
messages.push({ role: "system", content: data.content });
|
|
773
|
+
}
|
|
774
|
+
if (messages.length === 0 && typeof data.prompt === "string") {
|
|
775
|
+
messages.push({ role: "system", content: data.prompt });
|
|
776
|
+
}
|
|
777
|
+
return {
|
|
778
|
+
name: data.name,
|
|
779
|
+
description: data.description,
|
|
780
|
+
model: data.model,
|
|
781
|
+
modelParameters: data.modelParameters,
|
|
782
|
+
messages,
|
|
783
|
+
variables: data.variables,
|
|
784
|
+
metadata: data.metadata
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
function parse(content, format) {
|
|
788
|
+
const trimmed = content.trim();
|
|
789
|
+
if (!format) {
|
|
790
|
+
if (trimmed.startsWith("{")) {
|
|
791
|
+
format = "json";
|
|
792
|
+
} else if (trimmed.startsWith("---")) {
|
|
793
|
+
format = "markdown";
|
|
794
|
+
} else if (trimmed.includes(":") && (trimmed.includes("\n ") || trimmed.includes("\n-"))) {
|
|
795
|
+
format = "yaml";
|
|
796
|
+
} else {
|
|
797
|
+
format = "text";
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
let data;
|
|
801
|
+
switch (format) {
|
|
802
|
+
case "json":
|
|
803
|
+
data = parseJson(trimmed);
|
|
804
|
+
break;
|
|
805
|
+
case "yaml":
|
|
806
|
+
data = parseSimpleYaml(trimmed);
|
|
807
|
+
break;
|
|
808
|
+
case "markdown":
|
|
809
|
+
data = parseMarkdown(trimmed);
|
|
810
|
+
break;
|
|
811
|
+
case "text":
|
|
812
|
+
default:
|
|
813
|
+
data = { messages: [{ role: "system", content: trimmed }] };
|
|
814
|
+
break;
|
|
815
|
+
}
|
|
816
|
+
return normalize2(data);
|
|
817
|
+
}
|
|
818
|
+
function toYaml(prompt) {
|
|
819
|
+
const lines = [];
|
|
820
|
+
if (prompt.name) {
|
|
821
|
+
lines.push(`name: ${prompt.name}`);
|
|
822
|
+
}
|
|
823
|
+
if (prompt.description) {
|
|
824
|
+
lines.push(`description: ${prompt.description}`);
|
|
825
|
+
}
|
|
826
|
+
if (prompt.model) {
|
|
827
|
+
lines.push(`model: ${prompt.model}`);
|
|
828
|
+
}
|
|
829
|
+
if (prompt.modelParameters) {
|
|
830
|
+
lines.push("modelParameters:");
|
|
831
|
+
for (const [key, value] of Object.entries(prompt.modelParameters)) {
|
|
832
|
+
if (value !== void 0) {
|
|
833
|
+
lines.push(` ${key}: ${value}`);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
if (prompt.messages.length > 0) {
|
|
838
|
+
lines.push("messages:");
|
|
839
|
+
for (const msg of prompt.messages) {
|
|
840
|
+
lines.push(` - role: ${msg.role}`);
|
|
841
|
+
if (msg.content.includes("\n")) {
|
|
842
|
+
lines.push(" content: |");
|
|
843
|
+
for (const line of msg.content.split("\n")) {
|
|
844
|
+
lines.push(` ${line}`);
|
|
845
|
+
}
|
|
846
|
+
} else {
|
|
847
|
+
lines.push(` content: "${msg.content.replace(/"/g, '\\"')}"`);
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
return lines.join("\n");
|
|
852
|
+
}
|
|
853
|
+
function toJson(prompt, pretty = true) {
|
|
854
|
+
return JSON.stringify(prompt, null, pretty ? 2 : 0);
|
|
855
|
+
}
|
|
856
|
+
function getSystemPrompt(prompt) {
|
|
857
|
+
const systemMessage = prompt.messages.find((m) => m.role === "system");
|
|
858
|
+
return systemMessage?.content || "";
|
|
859
|
+
}
|
|
860
|
+
function interpolate(prompt, values) {
|
|
861
|
+
const interpolateString = (str) => {
|
|
862
|
+
return str.replace(/\{\{(\w+)\}\}/g, (match, key) => {
|
|
863
|
+
if (key in values) return values[key];
|
|
864
|
+
if (prompt.variables?.[key]?.default) return prompt.variables[key].default;
|
|
865
|
+
return match;
|
|
866
|
+
});
|
|
867
|
+
};
|
|
868
|
+
return {
|
|
869
|
+
...prompt,
|
|
870
|
+
messages: prompt.messages.map((msg) => ({
|
|
871
|
+
...msg,
|
|
872
|
+
content: interpolateString(msg.content)
|
|
873
|
+
}))
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// src/builder/media.ts
|
|
878
|
+
var ImagePromptBuilder = class {
|
|
879
|
+
constructor() {
|
|
880
|
+
this._negative = [];
|
|
881
|
+
this._custom = [];
|
|
882
|
+
}
|
|
883
|
+
// --- Subject Methods ---
|
|
884
|
+
subject(main) {
|
|
885
|
+
if (typeof main === "string") {
|
|
886
|
+
this._subject = { ...this._subject || {}, main };
|
|
887
|
+
} else {
|
|
888
|
+
this._subject = { ...this._subject || {}, ...main };
|
|
889
|
+
}
|
|
890
|
+
return this;
|
|
891
|
+
}
|
|
892
|
+
subjectDetails(details) {
|
|
893
|
+
this._subject = { ...this._subject || { main: "" }, details };
|
|
894
|
+
return this;
|
|
895
|
+
}
|
|
896
|
+
expression(expression) {
|
|
897
|
+
this._subject = { ...this._subject || { main: "" }, expression };
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
pose(pose) {
|
|
901
|
+
this._subject = { ...this._subject || { main: "" }, pose };
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
action(action) {
|
|
905
|
+
this._subject = { ...this._subject || { main: "" }, action };
|
|
906
|
+
return this;
|
|
907
|
+
}
|
|
908
|
+
clothing(clothing) {
|
|
909
|
+
this._subject = { ...this._subject || { main: "" }, clothing };
|
|
910
|
+
return this;
|
|
911
|
+
}
|
|
912
|
+
accessories(accessories) {
|
|
913
|
+
this._subject = { ...this._subject || { main: "" }, accessories };
|
|
914
|
+
return this;
|
|
915
|
+
}
|
|
916
|
+
subjectCount(count) {
|
|
917
|
+
this._subject = { ...this._subject || { main: "" }, count };
|
|
918
|
+
return this;
|
|
919
|
+
}
|
|
920
|
+
// --- Camera Methods ---
|
|
921
|
+
camera(settings) {
|
|
922
|
+
this._camera = { ...this._camera || {}, ...settings };
|
|
923
|
+
return this;
|
|
924
|
+
}
|
|
925
|
+
angle(angle) {
|
|
926
|
+
this._camera = { ...this._camera || {}, angle };
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
shot(shot) {
|
|
930
|
+
this._camera = { ...this._camera || {}, shot };
|
|
931
|
+
return this;
|
|
932
|
+
}
|
|
933
|
+
lens(lens) {
|
|
934
|
+
this._camera = { ...this._camera || {}, lens };
|
|
935
|
+
return this;
|
|
936
|
+
}
|
|
937
|
+
focus(focus) {
|
|
938
|
+
this._camera = { ...this._camera || {}, focus };
|
|
939
|
+
return this;
|
|
940
|
+
}
|
|
941
|
+
aperture(aperture) {
|
|
942
|
+
this._camera = { ...this._camera || {}, aperture };
|
|
943
|
+
return this;
|
|
944
|
+
}
|
|
945
|
+
filmStock(filmStock) {
|
|
946
|
+
this._camera = { ...this._camera || {}, filmStock };
|
|
947
|
+
return this;
|
|
948
|
+
}
|
|
949
|
+
filmFormat(format) {
|
|
950
|
+
this._camera = { ...this._camera || {}, filmFormat: format };
|
|
951
|
+
return this;
|
|
952
|
+
}
|
|
953
|
+
cameraBrand(brand) {
|
|
954
|
+
this._camera = { ...this._camera || {}, brand };
|
|
955
|
+
return this;
|
|
956
|
+
}
|
|
957
|
+
cameraModel(model) {
|
|
958
|
+
this._camera = { ...this._camera || {}, model };
|
|
959
|
+
return this;
|
|
960
|
+
}
|
|
961
|
+
sensor(sensor) {
|
|
962
|
+
this._camera = { ...this._camera || {}, sensor };
|
|
963
|
+
return this;
|
|
964
|
+
}
|
|
965
|
+
lensModel(model) {
|
|
966
|
+
this._camera = { ...this._camera || {}, lensModel: model };
|
|
967
|
+
return this;
|
|
968
|
+
}
|
|
969
|
+
lensBrand(brand) {
|
|
970
|
+
this._camera = { ...this._camera || {}, lensBrand: brand };
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
focalLength(length) {
|
|
974
|
+
this._camera = { ...this._camera || {}, focalLength: length };
|
|
975
|
+
return this;
|
|
976
|
+
}
|
|
977
|
+
bokeh(style) {
|
|
978
|
+
this._camera = { ...this._camera || {}, bokeh: style };
|
|
979
|
+
return this;
|
|
980
|
+
}
|
|
981
|
+
filter(filter) {
|
|
982
|
+
this._camera = { ...this._camera || {}, filter };
|
|
983
|
+
return this;
|
|
984
|
+
}
|
|
985
|
+
iso(iso) {
|
|
986
|
+
this._camera = { ...this._camera || {}, iso };
|
|
987
|
+
return this;
|
|
988
|
+
}
|
|
989
|
+
shutterSpeed(speed) {
|
|
990
|
+
this._camera = { ...this._camera || {}, shutterSpeed: speed };
|
|
991
|
+
return this;
|
|
992
|
+
}
|
|
993
|
+
whiteBalance(wb) {
|
|
994
|
+
this._camera = { ...this._camera || {}, whiteBalance: wb };
|
|
995
|
+
return this;
|
|
996
|
+
}
|
|
997
|
+
colorProfile(profile) {
|
|
998
|
+
this._camera = { ...this._camera || {}, colorProfile: profile };
|
|
999
|
+
return this;
|
|
1000
|
+
}
|
|
1001
|
+
// --- Lighting Methods ---
|
|
1002
|
+
lighting(settings) {
|
|
1003
|
+
this._lighting = { ...this._lighting || {}, ...settings };
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
1006
|
+
lightingType(type) {
|
|
1007
|
+
this._lighting = { ...this._lighting || {}, type };
|
|
1008
|
+
return this;
|
|
1009
|
+
}
|
|
1010
|
+
timeOfDay(time) {
|
|
1011
|
+
this._lighting = { ...this._lighting || {}, time };
|
|
1012
|
+
return this;
|
|
1013
|
+
}
|
|
1014
|
+
weather(weather) {
|
|
1015
|
+
this._lighting = { ...this._lighting || {}, weather };
|
|
1016
|
+
return this;
|
|
1017
|
+
}
|
|
1018
|
+
lightDirection(direction) {
|
|
1019
|
+
this._lighting = { ...this._lighting || {}, direction };
|
|
1020
|
+
return this;
|
|
1021
|
+
}
|
|
1022
|
+
lightIntensity(intensity) {
|
|
1023
|
+
this._lighting = { ...this._lighting || {}, intensity };
|
|
1024
|
+
return this;
|
|
1025
|
+
}
|
|
1026
|
+
// --- Composition Methods ---
|
|
1027
|
+
composition(settings) {
|
|
1028
|
+
this._composition = { ...this._composition || {}, ...settings };
|
|
1029
|
+
return this;
|
|
1030
|
+
}
|
|
1031
|
+
ruleOfThirds() {
|
|
1032
|
+
this._composition = { ...this._composition || {}, ruleOfThirds: true };
|
|
1033
|
+
return this;
|
|
1034
|
+
}
|
|
1035
|
+
goldenRatio() {
|
|
1036
|
+
this._composition = { ...this._composition || {}, goldenRatio: true };
|
|
1037
|
+
return this;
|
|
1038
|
+
}
|
|
1039
|
+
symmetry(type) {
|
|
1040
|
+
this._composition = { ...this._composition || {}, symmetry: type };
|
|
1041
|
+
return this;
|
|
1042
|
+
}
|
|
1043
|
+
foreground(fg) {
|
|
1044
|
+
this._composition = { ...this._composition || {}, foreground: fg };
|
|
1045
|
+
return this;
|
|
1046
|
+
}
|
|
1047
|
+
midground(mg) {
|
|
1048
|
+
this._composition = { ...this._composition || {}, midground: mg };
|
|
1049
|
+
return this;
|
|
1050
|
+
}
|
|
1051
|
+
background(bg) {
|
|
1052
|
+
this._composition = { ...this._composition || {}, background: bg };
|
|
1053
|
+
return this;
|
|
1054
|
+
}
|
|
1055
|
+
// --- Environment Methods ---
|
|
1056
|
+
environment(setting) {
|
|
1057
|
+
if (typeof setting === "string") {
|
|
1058
|
+
this._environment = { ...this._environment || { setting: "" }, setting };
|
|
1059
|
+
} else {
|
|
1060
|
+
this._environment = { ...this._environment || { setting: "" }, ...setting };
|
|
1061
|
+
}
|
|
1062
|
+
return this;
|
|
1063
|
+
}
|
|
1064
|
+
location(location) {
|
|
1065
|
+
this._environment = { ...this._environment || { setting: "" }, location };
|
|
1066
|
+
return this;
|
|
1067
|
+
}
|
|
1068
|
+
props(props) {
|
|
1069
|
+
this._environment = { ...this._environment || { setting: "" }, props };
|
|
1070
|
+
return this;
|
|
1071
|
+
}
|
|
1072
|
+
atmosphere(atmosphere) {
|
|
1073
|
+
this._environment = { ...this._environment || { setting: "" }, atmosphere };
|
|
1074
|
+
return this;
|
|
1075
|
+
}
|
|
1076
|
+
season(season) {
|
|
1077
|
+
this._environment = { ...this._environment || { setting: "" }, season };
|
|
1078
|
+
return this;
|
|
1079
|
+
}
|
|
1080
|
+
// --- Style Methods ---
|
|
1081
|
+
style(settings) {
|
|
1082
|
+
this._style = { ...this._style || {}, ...settings };
|
|
1083
|
+
return this;
|
|
1084
|
+
}
|
|
1085
|
+
medium(medium) {
|
|
1086
|
+
this._style = { ...this._style || {}, medium };
|
|
1087
|
+
return this;
|
|
1088
|
+
}
|
|
1089
|
+
artist(artist) {
|
|
1090
|
+
this._style = { ...this._style || {}, artist };
|
|
1091
|
+
return this;
|
|
1092
|
+
}
|
|
1093
|
+
influence(influences) {
|
|
1094
|
+
this._style = { ...this._style || {}, influence: influences };
|
|
1095
|
+
return this;
|
|
1096
|
+
}
|
|
1097
|
+
// --- Color Methods ---
|
|
1098
|
+
color(settings) {
|
|
1099
|
+
this._color = { ...this._color || {}, ...settings };
|
|
1100
|
+
return this;
|
|
1101
|
+
}
|
|
1102
|
+
palette(palette) {
|
|
1103
|
+
this._color = { ...this._color || {}, palette };
|
|
1104
|
+
return this;
|
|
1105
|
+
}
|
|
1106
|
+
primaryColors(colors) {
|
|
1107
|
+
this._color = { ...this._color || {}, primary: colors };
|
|
1108
|
+
return this;
|
|
1109
|
+
}
|
|
1110
|
+
accentColors(colors) {
|
|
1111
|
+
this._color = { ...this._color || {}, accent: colors };
|
|
1112
|
+
return this;
|
|
1113
|
+
}
|
|
1114
|
+
colorGrade(grade) {
|
|
1115
|
+
this._color = { ...this._color || {}, grade };
|
|
1116
|
+
return this;
|
|
1117
|
+
}
|
|
1118
|
+
// --- Technical Methods ---
|
|
1119
|
+
technical(settings) {
|
|
1120
|
+
this._technical = { ...this._technical || {}, ...settings };
|
|
1121
|
+
return this;
|
|
1122
|
+
}
|
|
1123
|
+
aspectRatio(ratio) {
|
|
1124
|
+
this._technical = { ...this._technical || {}, aspectRatio: ratio };
|
|
1125
|
+
return this;
|
|
1126
|
+
}
|
|
1127
|
+
resolution(resolution) {
|
|
1128
|
+
this._technical = { ...this._technical || {}, resolution };
|
|
1129
|
+
return this;
|
|
1130
|
+
}
|
|
1131
|
+
quality(quality) {
|
|
1132
|
+
this._technical = { ...this._technical || {}, quality };
|
|
1133
|
+
return this;
|
|
1134
|
+
}
|
|
1135
|
+
// --- Mood & Misc ---
|
|
1136
|
+
mood(mood) {
|
|
1137
|
+
this._mood = mood;
|
|
1138
|
+
return this;
|
|
1139
|
+
}
|
|
1140
|
+
negative(items) {
|
|
1141
|
+
this._negative = [...this._negative, ...items];
|
|
1142
|
+
return this;
|
|
1143
|
+
}
|
|
1144
|
+
custom(text) {
|
|
1145
|
+
this._custom.push(text);
|
|
1146
|
+
return this;
|
|
1147
|
+
}
|
|
1148
|
+
// --- Build Methods ---
|
|
1149
|
+
buildPromptText() {
|
|
1150
|
+
const parts = [];
|
|
1151
|
+
if (this._subject) {
|
|
1152
|
+
let subjectText = this._subject.main;
|
|
1153
|
+
if (this._subject.count && this._subject.count !== "single") {
|
|
1154
|
+
subjectText = `${this._subject.count} ${subjectText}`;
|
|
1155
|
+
}
|
|
1156
|
+
if (this._subject.expression) subjectText += `, ${this._subject.expression} expression`;
|
|
1157
|
+
if (this._subject.pose) subjectText += `, ${this._subject.pose}`;
|
|
1158
|
+
if (this._subject.action) subjectText += `, ${this._subject.action}`;
|
|
1159
|
+
if (this._subject.clothing) subjectText += `, wearing ${this._subject.clothing}`;
|
|
1160
|
+
if (this._subject.accessories?.length) subjectText += `, with ${this._subject.accessories.join(", ")}`;
|
|
1161
|
+
if (this._subject.details?.length) subjectText += `, ${this._subject.details.join(", ")}`;
|
|
1162
|
+
parts.push(subjectText);
|
|
1163
|
+
}
|
|
1164
|
+
if (this._environment) {
|
|
1165
|
+
let envText = this._environment.setting;
|
|
1166
|
+
if (this._environment.location) envText += ` in ${this._environment.location}`;
|
|
1167
|
+
if (this._environment.atmosphere) envText += `, ${this._environment.atmosphere} atmosphere`;
|
|
1168
|
+
if (this._environment.season) envText += `, ${this._environment.season}`;
|
|
1169
|
+
if (this._environment.props?.length) envText += `, with ${this._environment.props.join(", ")}`;
|
|
1170
|
+
parts.push(envText);
|
|
1171
|
+
}
|
|
1172
|
+
if (this._composition) {
|
|
1173
|
+
const compParts = [];
|
|
1174
|
+
if (this._composition.foreground) compParts.push(`foreground: ${this._composition.foreground}`);
|
|
1175
|
+
if (this._composition.midground) compParts.push(`midground: ${this._composition.midground}`);
|
|
1176
|
+
if (this._composition.background) compParts.push(`background: ${this._composition.background}`);
|
|
1177
|
+
if (this._composition.ruleOfThirds) compParts.push("rule of thirds composition");
|
|
1178
|
+
if (this._composition.goldenRatio) compParts.push("golden ratio composition");
|
|
1179
|
+
if (this._composition.symmetry && this._composition.symmetry !== "none") {
|
|
1180
|
+
compParts.push(`${this._composition.symmetry} symmetry`);
|
|
1181
|
+
}
|
|
1182
|
+
if (compParts.length) parts.push(compParts.join(", "));
|
|
1183
|
+
}
|
|
1184
|
+
if (this._camera) {
|
|
1185
|
+
const camParts = [];
|
|
1186
|
+
if (this._camera.shot) camParts.push(`${this._camera.shot} shot`);
|
|
1187
|
+
if (this._camera.angle) camParts.push(`${this._camera.angle}`);
|
|
1188
|
+
if (this._camera.lens) camParts.push(`${this._camera.lens} lens`);
|
|
1189
|
+
if (this._camera.focus) camParts.push(`${this._camera.focus} depth of field`);
|
|
1190
|
+
if (this._camera.aperture) camParts.push(`f/${this._camera.aperture}`);
|
|
1191
|
+
if (this._camera.filmStock) camParts.push(`shot on ${this._camera.filmStock}`);
|
|
1192
|
+
if (this._camera.brand) camParts.push(`${this._camera.brand}`);
|
|
1193
|
+
if (camParts.length) parts.push(camParts.join(", "));
|
|
1194
|
+
}
|
|
1195
|
+
if (this._lighting) {
|
|
1196
|
+
const lightParts = [];
|
|
1197
|
+
if (this._lighting.type) {
|
|
1198
|
+
const types = Array.isArray(this._lighting.type) ? this._lighting.type : [this._lighting.type];
|
|
1199
|
+
lightParts.push(`${types.join(" and ")} lighting`);
|
|
1200
|
+
}
|
|
1201
|
+
if (this._lighting.time) lightParts.push(this._lighting.time);
|
|
1202
|
+
if (this._lighting.weather) lightParts.push(`${this._lighting.weather} weather`);
|
|
1203
|
+
if (this._lighting.direction) lightParts.push(`light from ${this._lighting.direction}`);
|
|
1204
|
+
if (this._lighting.intensity) lightParts.push(`${this._lighting.intensity} light`);
|
|
1205
|
+
if (lightParts.length) parts.push(lightParts.join(", "));
|
|
1206
|
+
}
|
|
1207
|
+
if (this._style) {
|
|
1208
|
+
const styleParts = [];
|
|
1209
|
+
if (this._style.medium) {
|
|
1210
|
+
const mediums = Array.isArray(this._style.medium) ? this._style.medium : [this._style.medium];
|
|
1211
|
+
styleParts.push(mediums.join(", "));
|
|
1212
|
+
}
|
|
1213
|
+
if (this._style.artist) {
|
|
1214
|
+
const artists = Array.isArray(this._style.artist) ? this._style.artist : [this._style.artist];
|
|
1215
|
+
styleParts.push(`in the style of ${artists.join(" and ")}`);
|
|
1216
|
+
}
|
|
1217
|
+
if (this._style.era) styleParts.push(this._style.era);
|
|
1218
|
+
if (this._style.influence?.length) styleParts.push(`influenced by ${this._style.influence.join(", ")}`);
|
|
1219
|
+
if (this._style.quality?.length) styleParts.push(this._style.quality.join(", "));
|
|
1220
|
+
if (styleParts.length) parts.push(styleParts.join(", "));
|
|
1221
|
+
}
|
|
1222
|
+
if (this._color) {
|
|
1223
|
+
const colorParts = [];
|
|
1224
|
+
if (this._color.palette) {
|
|
1225
|
+
const palettes = Array.isArray(this._color.palette) ? this._color.palette : [this._color.palette];
|
|
1226
|
+
colorParts.push(`${palettes.join(" and ")} color palette`);
|
|
1227
|
+
}
|
|
1228
|
+
if (this._color.primary?.length) colorParts.push(`primary colors: ${this._color.primary.join(", ")}`);
|
|
1229
|
+
if (this._color.accent?.length) colorParts.push(`accent colors: ${this._color.accent.join(", ")}`);
|
|
1230
|
+
if (this._color.grade) colorParts.push(`${this._color.grade} color grade`);
|
|
1231
|
+
if (this._color.temperature) colorParts.push(`${this._color.temperature} tones`);
|
|
1232
|
+
if (colorParts.length) parts.push(colorParts.join(", "));
|
|
1233
|
+
}
|
|
1234
|
+
if (this._mood) {
|
|
1235
|
+
const moods = Array.isArray(this._mood) ? this._mood : [this._mood];
|
|
1236
|
+
parts.push(`${moods.join(", ")} mood`);
|
|
1237
|
+
}
|
|
1238
|
+
if (this._technical) {
|
|
1239
|
+
const techParts = [];
|
|
1240
|
+
if (this._technical.quality) techParts.push(`${this._technical.quality} quality`);
|
|
1241
|
+
if (this._technical.detail) techParts.push(`${this._technical.detail} detail`);
|
|
1242
|
+
if (this._technical.resolution) techParts.push(this._technical.resolution);
|
|
1243
|
+
if (techParts.length) parts.push(techParts.join(", "));
|
|
1244
|
+
}
|
|
1245
|
+
if (this._custom.length) {
|
|
1246
|
+
parts.push(this._custom.join(", "));
|
|
1247
|
+
}
|
|
1248
|
+
let prompt = parts.join(", ");
|
|
1249
|
+
if (this._negative.length) {
|
|
1250
|
+
prompt += ` --no ${this._negative.join(", ")}`;
|
|
1251
|
+
}
|
|
1252
|
+
if (this._technical?.aspectRatio) {
|
|
1253
|
+
prompt += ` --ar ${this._technical.aspectRatio}`;
|
|
1254
|
+
}
|
|
1255
|
+
return prompt;
|
|
1256
|
+
}
|
|
1257
|
+
build() {
|
|
1258
|
+
return {
|
|
1259
|
+
prompt: this.buildPromptText(),
|
|
1260
|
+
structure: {
|
|
1261
|
+
subject: this._subject,
|
|
1262
|
+
camera: this._camera,
|
|
1263
|
+
lighting: this._lighting,
|
|
1264
|
+
composition: this._composition,
|
|
1265
|
+
style: this._style,
|
|
1266
|
+
color: this._color,
|
|
1267
|
+
environment: this._environment,
|
|
1268
|
+
technical: this._technical,
|
|
1269
|
+
mood: this._mood,
|
|
1270
|
+
negative: this._negative.length ? this._negative : void 0
|
|
1271
|
+
}
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
toString() {
|
|
1275
|
+
return this.build().prompt;
|
|
1276
|
+
}
|
|
1277
|
+
toJSON() {
|
|
1278
|
+
return JSON.stringify(this.build().structure, null, 2);
|
|
1279
|
+
}
|
|
1280
|
+
toYAML() {
|
|
1281
|
+
return objectToYaml(this.build().structure);
|
|
1282
|
+
}
|
|
1283
|
+
toMarkdown() {
|
|
1284
|
+
const built = this.build();
|
|
1285
|
+
const sections = ["# Image Prompt\n"];
|
|
1286
|
+
sections.push("## Prompt\n```\n" + built.prompt + "\n```\n");
|
|
1287
|
+
if (built.structure.subject) {
|
|
1288
|
+
sections.push("## Subject\n" + objectToMarkdownList(built.structure.subject));
|
|
1289
|
+
}
|
|
1290
|
+
if (built.structure.environment) {
|
|
1291
|
+
sections.push("## Environment\n" + objectToMarkdownList(built.structure.environment));
|
|
1292
|
+
}
|
|
1293
|
+
if (built.structure.camera) {
|
|
1294
|
+
sections.push("## Camera\n" + objectToMarkdownList(built.structure.camera));
|
|
1295
|
+
}
|
|
1296
|
+
if (built.structure.lighting) {
|
|
1297
|
+
sections.push("## Lighting\n" + objectToMarkdownList(built.structure.lighting));
|
|
1298
|
+
}
|
|
1299
|
+
if (built.structure.composition) {
|
|
1300
|
+
sections.push("## Composition\n" + objectToMarkdownList(built.structure.composition));
|
|
1301
|
+
}
|
|
1302
|
+
if (built.structure.style) {
|
|
1303
|
+
sections.push("## Style\n" + objectToMarkdownList(built.structure.style));
|
|
1304
|
+
}
|
|
1305
|
+
if (built.structure.color) {
|
|
1306
|
+
sections.push("## Color\n" + objectToMarkdownList(built.structure.color));
|
|
1307
|
+
}
|
|
1308
|
+
if (built.structure.technical) {
|
|
1309
|
+
sections.push("## Technical\n" + objectToMarkdownList(built.structure.technical));
|
|
1310
|
+
}
|
|
1311
|
+
return sections.join("\n");
|
|
1312
|
+
}
|
|
1313
|
+
format(fmt) {
|
|
1314
|
+
switch (fmt) {
|
|
1315
|
+
case "json":
|
|
1316
|
+
return this.toJSON();
|
|
1317
|
+
case "yaml":
|
|
1318
|
+
return this.toYAML();
|
|
1319
|
+
case "markdown":
|
|
1320
|
+
return this.toMarkdown();
|
|
1321
|
+
default:
|
|
1322
|
+
return this.toString();
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
};
|
|
1326
|
+
function objectToYaml(obj, indent = 0) {
|
|
1327
|
+
const spaces = " ".repeat(indent);
|
|
1328
|
+
const lines = [];
|
|
1329
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1330
|
+
if (value === void 0 || value === null) continue;
|
|
1331
|
+
if (Array.isArray(value)) {
|
|
1332
|
+
if (value.length === 0) continue;
|
|
1333
|
+
lines.push(`${spaces}${key}:`);
|
|
1334
|
+
for (const item of value) {
|
|
1335
|
+
if (typeof item === "object") {
|
|
1336
|
+
lines.push(`${spaces} -`);
|
|
1337
|
+
lines.push(objectToYaml(item, indent + 2).replace(/^/gm, " "));
|
|
1338
|
+
} else {
|
|
1339
|
+
lines.push(`${spaces} - ${item}`);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
} else if (typeof value === "object") {
|
|
1343
|
+
lines.push(`${spaces}${key}:`);
|
|
1344
|
+
lines.push(objectToYaml(value, indent + 1));
|
|
1345
|
+
} else {
|
|
1346
|
+
lines.push(`${spaces}${key}: ${value}`);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
return lines.join("\n");
|
|
1350
|
+
}
|
|
1351
|
+
function objectToMarkdownList(obj, indent = 0) {
|
|
1352
|
+
const spaces = " ".repeat(indent);
|
|
1353
|
+
const lines = [];
|
|
1354
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1355
|
+
if (value === void 0 || value === null) continue;
|
|
1356
|
+
if (Array.isArray(value)) {
|
|
1357
|
+
lines.push(`${spaces}- **${key}:** ${value.join(", ")}`);
|
|
1358
|
+
} else if (typeof value === "object") {
|
|
1359
|
+
lines.push(`${spaces}- **${key}:**`);
|
|
1360
|
+
lines.push(objectToMarkdownList(value, indent + 1));
|
|
1361
|
+
} else {
|
|
1362
|
+
lines.push(`${spaces}- **${key}:** ${value}`);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
return lines.join("\n");
|
|
1366
|
+
}
|
|
1367
|
+
function image() {
|
|
1368
|
+
return new ImagePromptBuilder();
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
// src/builder/video.ts
|
|
1372
|
+
var VideoPromptBuilder = class {
|
|
1373
|
+
constructor() {
|
|
1374
|
+
this._actions = [];
|
|
1375
|
+
this._shots = [];
|
|
1376
|
+
this._transitions = [];
|
|
1377
|
+
this._custom = [];
|
|
1378
|
+
}
|
|
1379
|
+
// --- Scene Methods ---
|
|
1380
|
+
scene(description) {
|
|
1381
|
+
if (typeof description === "string") {
|
|
1382
|
+
this._scene = { ...this._scene || { description: "" }, description };
|
|
1383
|
+
} else {
|
|
1384
|
+
this._scene = { ...this._scene || { description: "" }, ...description };
|
|
1385
|
+
}
|
|
1386
|
+
return this;
|
|
1387
|
+
}
|
|
1388
|
+
setting(setting) {
|
|
1389
|
+
this._scene = { ...this._scene || { description: "" }, setting };
|
|
1390
|
+
return this;
|
|
1391
|
+
}
|
|
1392
|
+
// --- Subject Methods ---
|
|
1393
|
+
subject(main) {
|
|
1394
|
+
if (typeof main === "string") {
|
|
1395
|
+
this._subject = { ...this._subject || { main: "" }, main };
|
|
1396
|
+
} else {
|
|
1397
|
+
this._subject = { ...this._subject || { main: "" }, ...main };
|
|
1398
|
+
}
|
|
1399
|
+
return this;
|
|
1400
|
+
}
|
|
1401
|
+
appearance(appearance) {
|
|
1402
|
+
this._subject = { ...this._subject || { main: "" }, appearance };
|
|
1403
|
+
return this;
|
|
1404
|
+
}
|
|
1405
|
+
clothing(clothing) {
|
|
1406
|
+
this._subject = { ...this._subject || { main: "" }, clothing };
|
|
1407
|
+
return this;
|
|
1408
|
+
}
|
|
1409
|
+
// --- Camera Methods ---
|
|
1410
|
+
camera(settings) {
|
|
1411
|
+
this._camera = { ...this._camera || {}, ...settings };
|
|
1412
|
+
return this;
|
|
1413
|
+
}
|
|
1414
|
+
shot(shot) {
|
|
1415
|
+
this._camera = { ...this._camera || {}, shot };
|
|
1416
|
+
return this;
|
|
1417
|
+
}
|
|
1418
|
+
angle(angle) {
|
|
1419
|
+
this._camera = { ...this._camera || {}, angle };
|
|
1420
|
+
return this;
|
|
1421
|
+
}
|
|
1422
|
+
movement(movement) {
|
|
1423
|
+
this._camera = { ...this._camera || {}, movement };
|
|
1424
|
+
return this;
|
|
1425
|
+
}
|
|
1426
|
+
lens(lens) {
|
|
1427
|
+
this._camera = { ...this._camera || {}, lens };
|
|
1428
|
+
return this;
|
|
1429
|
+
}
|
|
1430
|
+
platform(platform) {
|
|
1431
|
+
this._camera = { ...this._camera || {}, platform };
|
|
1432
|
+
return this;
|
|
1433
|
+
}
|
|
1434
|
+
cameraSpeed(speed) {
|
|
1435
|
+
this._camera = { ...this._camera || {}, movementSpeed: speed };
|
|
1436
|
+
return this;
|
|
1437
|
+
}
|
|
1438
|
+
movementDirection(direction) {
|
|
1439
|
+
this._camera = { ...this._camera || {}, movementDirection: direction };
|
|
1440
|
+
return this;
|
|
1441
|
+
}
|
|
1442
|
+
rig(rig) {
|
|
1443
|
+
this._camera = { ...this._camera || {}, rig };
|
|
1444
|
+
return this;
|
|
1445
|
+
}
|
|
1446
|
+
gimbal(gimbal) {
|
|
1447
|
+
this._camera = { ...this._camera || {}, gimbal };
|
|
1448
|
+
return this;
|
|
1449
|
+
}
|
|
1450
|
+
cameraBrand(brand) {
|
|
1451
|
+
this._camera = { ...this._camera || {}, brand };
|
|
1452
|
+
return this;
|
|
1453
|
+
}
|
|
1454
|
+
cameraModel(model) {
|
|
1455
|
+
this._camera = { ...this._camera || {}, model };
|
|
1456
|
+
return this;
|
|
1457
|
+
}
|
|
1458
|
+
sensor(sensor) {
|
|
1459
|
+
this._camera = { ...this._camera || {}, sensor };
|
|
1460
|
+
return this;
|
|
1461
|
+
}
|
|
1462
|
+
lensModel(model) {
|
|
1463
|
+
this._camera = { ...this._camera || {}, lensModel: model };
|
|
1464
|
+
return this;
|
|
1465
|
+
}
|
|
1466
|
+
lensBrand(brand) {
|
|
1467
|
+
this._camera = { ...this._camera || {}, lensBrand: brand };
|
|
1468
|
+
return this;
|
|
1469
|
+
}
|
|
1470
|
+
focalLength(length) {
|
|
1471
|
+
this._camera = { ...this._camera || {}, focalLength: length };
|
|
1472
|
+
return this;
|
|
1473
|
+
}
|
|
1474
|
+
anamorphic(ratio) {
|
|
1475
|
+
this._camera = { ...this._camera || {}, anamorphic: true, anamorphicRatio: ratio };
|
|
1476
|
+
return this;
|
|
1477
|
+
}
|
|
1478
|
+
aperture(aperture) {
|
|
1479
|
+
this._camera = { ...this._camera || {}, aperture };
|
|
1480
|
+
return this;
|
|
1481
|
+
}
|
|
1482
|
+
frameRate(fps) {
|
|
1483
|
+
this._camera = { ...this._camera || {}, frameRate: fps };
|
|
1484
|
+
return this;
|
|
1485
|
+
}
|
|
1486
|
+
slowMotion(enabled = true) {
|
|
1487
|
+
this._camera = { ...this._camera || {}, slowMotion: enabled };
|
|
1488
|
+
return this;
|
|
1489
|
+
}
|
|
1490
|
+
shutterAngle(angle) {
|
|
1491
|
+
this._camera = { ...this._camera || {}, shutterAngle: angle };
|
|
1492
|
+
return this;
|
|
1493
|
+
}
|
|
1494
|
+
filter(filter) {
|
|
1495
|
+
this._camera = { ...this._camera || {}, filter };
|
|
1496
|
+
return this;
|
|
1497
|
+
}
|
|
1498
|
+
filmStock(stock) {
|
|
1499
|
+
this._camera = { ...this._camera || {}, filmStock: stock };
|
|
1500
|
+
return this;
|
|
1501
|
+
}
|
|
1502
|
+
filmGrain(grain) {
|
|
1503
|
+
this._camera = { ...this._camera || {}, filmGrain: grain };
|
|
1504
|
+
return this;
|
|
1505
|
+
}
|
|
1506
|
+
halation(enabled = true) {
|
|
1507
|
+
this._camera = { ...this._camera || {}, halation: enabled };
|
|
1508
|
+
return this;
|
|
1509
|
+
}
|
|
1510
|
+
// --- Lighting Methods ---
|
|
1511
|
+
lighting(settings) {
|
|
1512
|
+
this._lighting = { ...this._lighting || {}, ...settings };
|
|
1513
|
+
return this;
|
|
1514
|
+
}
|
|
1515
|
+
lightingType(type) {
|
|
1516
|
+
this._lighting = { ...this._lighting || {}, type };
|
|
1517
|
+
return this;
|
|
1518
|
+
}
|
|
1519
|
+
timeOfDay(time) {
|
|
1520
|
+
this._lighting = { ...this._lighting || {}, time };
|
|
1521
|
+
this._scene = { ...this._scene || { description: "" }, timeOfDay: time };
|
|
1522
|
+
return this;
|
|
1523
|
+
}
|
|
1524
|
+
weather(weather) {
|
|
1525
|
+
this._lighting = { ...this._lighting || {}, weather };
|
|
1526
|
+
this._scene = { ...this._scene || { description: "" }, weather };
|
|
1527
|
+
return this;
|
|
1528
|
+
}
|
|
1529
|
+
// --- Action & Motion Methods ---
|
|
1530
|
+
action(action, options = {}) {
|
|
1531
|
+
this._actions.push({
|
|
1532
|
+
beat: this._actions.length + 1,
|
|
1533
|
+
action,
|
|
1534
|
+
...options
|
|
1535
|
+
});
|
|
1536
|
+
return this;
|
|
1537
|
+
}
|
|
1538
|
+
actions(actions) {
|
|
1539
|
+
actions.forEach((a, i) => this._actions.push({ beat: i + 1, action: a }));
|
|
1540
|
+
return this;
|
|
1541
|
+
}
|
|
1542
|
+
motion(settings) {
|
|
1543
|
+
this._motion = { ...this._motion || {}, ...settings };
|
|
1544
|
+
return this;
|
|
1545
|
+
}
|
|
1546
|
+
motionBeats(beats) {
|
|
1547
|
+
this._motion = { ...this._motion || {}, beats };
|
|
1548
|
+
return this;
|
|
1549
|
+
}
|
|
1550
|
+
// --- Style Methods ---
|
|
1551
|
+
style(settings) {
|
|
1552
|
+
this._style = { ...this._style || {}, ...settings };
|
|
1553
|
+
return this;
|
|
1554
|
+
}
|
|
1555
|
+
format(format) {
|
|
1556
|
+
this._style = { ...this._style || {}, format };
|
|
1557
|
+
return this;
|
|
1558
|
+
}
|
|
1559
|
+
era(era) {
|
|
1560
|
+
this._style = { ...this._style || {}, era };
|
|
1561
|
+
return this;
|
|
1562
|
+
}
|
|
1563
|
+
styleFilmStock(stock) {
|
|
1564
|
+
this._style = { ...this._style || {}, filmStock: stock };
|
|
1565
|
+
return this;
|
|
1566
|
+
}
|
|
1567
|
+
look(look) {
|
|
1568
|
+
this._style = { ...this._style || {}, look };
|
|
1569
|
+
return this;
|
|
1570
|
+
}
|
|
1571
|
+
reference(references) {
|
|
1572
|
+
this._style = { ...this._style || {}, reference: references };
|
|
1573
|
+
return this;
|
|
1574
|
+
}
|
|
1575
|
+
// --- Color Methods ---
|
|
1576
|
+
color(settings) {
|
|
1577
|
+
this._color = { ...this._color || {}, ...settings };
|
|
1578
|
+
return this;
|
|
1579
|
+
}
|
|
1580
|
+
palette(palette) {
|
|
1581
|
+
this._color = { ...this._color || {}, palette };
|
|
1582
|
+
return this;
|
|
1583
|
+
}
|
|
1584
|
+
colorAnchors(anchors) {
|
|
1585
|
+
this._color = { ...this._color || {}, anchors };
|
|
1586
|
+
return this;
|
|
1587
|
+
}
|
|
1588
|
+
colorGrade(grade) {
|
|
1589
|
+
this._color = { ...this._color || {}, grade };
|
|
1590
|
+
return this;
|
|
1591
|
+
}
|
|
1592
|
+
// --- Audio Methods ---
|
|
1593
|
+
audio(settings) {
|
|
1594
|
+
this._audio = { ...this._audio || {}, ...settings };
|
|
1595
|
+
return this;
|
|
1596
|
+
}
|
|
1597
|
+
dialogue(dialogue) {
|
|
1598
|
+
this._audio = { ...this._audio || {}, dialogue };
|
|
1599
|
+
return this;
|
|
1600
|
+
}
|
|
1601
|
+
ambient(ambient) {
|
|
1602
|
+
this._audio = { ...this._audio || {}, ambient };
|
|
1603
|
+
return this;
|
|
1604
|
+
}
|
|
1605
|
+
diegetic(sounds) {
|
|
1606
|
+
this._audio = { ...this._audio || {}, diegetic: sounds };
|
|
1607
|
+
return this;
|
|
1608
|
+
}
|
|
1609
|
+
soundEffects(effects) {
|
|
1610
|
+
this._audio = { ...this._audio || {}, soundEffects: effects };
|
|
1611
|
+
return this;
|
|
1612
|
+
}
|
|
1613
|
+
music(music) {
|
|
1614
|
+
this._audio = { ...this._audio || {}, music };
|
|
1615
|
+
return this;
|
|
1616
|
+
}
|
|
1617
|
+
// --- Technical Methods ---
|
|
1618
|
+
technical(settings) {
|
|
1619
|
+
this._technical = { ...this._technical || {}, ...settings };
|
|
1620
|
+
return this;
|
|
1621
|
+
}
|
|
1622
|
+
duration(seconds) {
|
|
1623
|
+
this._technical = { ...this._technical || {}, duration: seconds };
|
|
1624
|
+
return this;
|
|
1625
|
+
}
|
|
1626
|
+
resolution(res) {
|
|
1627
|
+
this._technical = { ...this._technical || {}, resolution: res };
|
|
1628
|
+
return this;
|
|
1629
|
+
}
|
|
1630
|
+
fps(fps) {
|
|
1631
|
+
this._technical = { ...this._technical || {}, fps };
|
|
1632
|
+
return this;
|
|
1633
|
+
}
|
|
1634
|
+
aspectRatio(ratio) {
|
|
1635
|
+
this._technical = { ...this._technical || {}, aspectRatio: ratio };
|
|
1636
|
+
return this;
|
|
1637
|
+
}
|
|
1638
|
+
// --- Shot List Methods ---
|
|
1639
|
+
addShot(shot) {
|
|
1640
|
+
this._shots.push(shot);
|
|
1641
|
+
return this;
|
|
1642
|
+
}
|
|
1643
|
+
shotList(shots) {
|
|
1644
|
+
this._shots = [...this._shots, ...shots];
|
|
1645
|
+
return this;
|
|
1646
|
+
}
|
|
1647
|
+
// --- Mood & Pacing ---
|
|
1648
|
+
mood(mood) {
|
|
1649
|
+
this._mood = mood;
|
|
1650
|
+
return this;
|
|
1651
|
+
}
|
|
1652
|
+
pacing(pacing) {
|
|
1653
|
+
this._pacing = pacing;
|
|
1654
|
+
return this;
|
|
1655
|
+
}
|
|
1656
|
+
transition(transition) {
|
|
1657
|
+
this._transitions.push(transition);
|
|
1658
|
+
return this;
|
|
1659
|
+
}
|
|
1660
|
+
transitions(transitions) {
|
|
1661
|
+
this._transitions = [...this._transitions, ...transitions];
|
|
1662
|
+
return this;
|
|
1663
|
+
}
|
|
1664
|
+
custom(text) {
|
|
1665
|
+
this._custom.push(text);
|
|
1666
|
+
return this;
|
|
1667
|
+
}
|
|
1668
|
+
// --- Build Methods ---
|
|
1669
|
+
buildPromptText() {
|
|
1670
|
+
const sections = [];
|
|
1671
|
+
if (this._scene) {
|
|
1672
|
+
let sceneText = this._scene.description;
|
|
1673
|
+
if (this._scene.setting) sceneText = `${this._scene.setting}. ${sceneText}`;
|
|
1674
|
+
if (this._scene.atmosphere) sceneText += `, ${this._scene.atmosphere} atmosphere`;
|
|
1675
|
+
sections.push(sceneText);
|
|
1676
|
+
}
|
|
1677
|
+
if (this._subject) {
|
|
1678
|
+
let subjectText = this._subject.main;
|
|
1679
|
+
if (this._subject.appearance) subjectText += `, ${this._subject.appearance}`;
|
|
1680
|
+
if (this._subject.clothing) subjectText += `, wearing ${this._subject.clothing}`;
|
|
1681
|
+
sections.push(subjectText);
|
|
1682
|
+
}
|
|
1683
|
+
const cinematography = [];
|
|
1684
|
+
if (this._camera) {
|
|
1685
|
+
if (this._camera.shot) cinematography.push(`${this._camera.shot} shot`);
|
|
1686
|
+
if (this._camera.angle) cinematography.push(this._camera.angle);
|
|
1687
|
+
if (this._camera.movement) cinematography.push(`${this._camera.movement} camera`);
|
|
1688
|
+
if (this._camera.lens) cinematography.push(`${this._camera.lens} lens`);
|
|
1689
|
+
if (this._camera.platform) cinematography.push(this._camera.platform);
|
|
1690
|
+
if (this._camera.focus) cinematography.push(`${this._camera.focus} focus`);
|
|
1691
|
+
}
|
|
1692
|
+
if (cinematography.length) {
|
|
1693
|
+
sections.push(`Cinematography: ${cinematography.join(", ")}`);
|
|
1694
|
+
}
|
|
1695
|
+
if (this._lighting) {
|
|
1696
|
+
const lightParts = [];
|
|
1697
|
+
if (this._lighting.type) {
|
|
1698
|
+
const types = Array.isArray(this._lighting.type) ? this._lighting.type : [this._lighting.type];
|
|
1699
|
+
lightParts.push(`${types.join(" and ")} lighting`);
|
|
1700
|
+
}
|
|
1701
|
+
if (this._lighting.time) lightParts.push(this._lighting.time);
|
|
1702
|
+
if (this._lighting.weather) lightParts.push(`${this._lighting.weather} weather`);
|
|
1703
|
+
if (this._lighting.intensity) lightParts.push(`${this._lighting.intensity} light`);
|
|
1704
|
+
if (this._lighting.sources?.length) lightParts.push(`light sources: ${this._lighting.sources.join(", ")}`);
|
|
1705
|
+
if (lightParts.length) sections.push(`Lighting: ${lightParts.join(", ")}`);
|
|
1706
|
+
}
|
|
1707
|
+
if (this._actions.length) {
|
|
1708
|
+
const actionText = this._actions.map((a) => `- ${a.action}`).join("\n");
|
|
1709
|
+
sections.push(`Actions:
|
|
1710
|
+
${actionText}`);
|
|
1711
|
+
}
|
|
1712
|
+
if (this._motion?.beats?.length) {
|
|
1713
|
+
sections.push(`Motion beats: ${this._motion.beats.join(", ")}`);
|
|
1714
|
+
}
|
|
1715
|
+
if (this._style) {
|
|
1716
|
+
const styleParts = [];
|
|
1717
|
+
if (this._style.format) styleParts.push(this._style.format);
|
|
1718
|
+
if (this._style.era) styleParts.push(this._style.era);
|
|
1719
|
+
if (this._style.filmStock) styleParts.push(`shot on ${this._style.filmStock}`);
|
|
1720
|
+
if (this._style.look) {
|
|
1721
|
+
const looks = Array.isArray(this._style.look) ? this._style.look : [this._style.look];
|
|
1722
|
+
styleParts.push(looks.join(", "));
|
|
1723
|
+
}
|
|
1724
|
+
if (styleParts.length) sections.push(`Style: ${styleParts.join(", ")}`);
|
|
1725
|
+
}
|
|
1726
|
+
if (this._color) {
|
|
1727
|
+
const colorParts = [];
|
|
1728
|
+
if (this._color.palette) {
|
|
1729
|
+
const palettes = Array.isArray(this._color.palette) ? this._color.palette : [this._color.palette];
|
|
1730
|
+
colorParts.push(`${palettes.join(" and ")} palette`);
|
|
1731
|
+
}
|
|
1732
|
+
if (this._color.anchors?.length) colorParts.push(`color anchors: ${this._color.anchors.join(", ")}`);
|
|
1733
|
+
if (this._color.grade) colorParts.push(this._color.grade);
|
|
1734
|
+
if (colorParts.length) sections.push(`Color: ${colorParts.join(", ")}`);
|
|
1735
|
+
}
|
|
1736
|
+
if (this._audio) {
|
|
1737
|
+
const audioParts = [];
|
|
1738
|
+
if (this._audio.dialogue) audioParts.push(`Dialogue: "${this._audio.dialogue}"`);
|
|
1739
|
+
if (this._audio.ambient) audioParts.push(`Ambient: ${this._audio.ambient}`);
|
|
1740
|
+
if (this._audio.diegetic?.length) audioParts.push(`Diegetic sounds: ${this._audio.diegetic.join(", ")}`);
|
|
1741
|
+
if (this._audio.music) audioParts.push(`Music: ${this._audio.music}`);
|
|
1742
|
+
if (audioParts.length) sections.push(`Audio:
|
|
1743
|
+
${audioParts.join("\n")}`);
|
|
1744
|
+
}
|
|
1745
|
+
if (this._mood) {
|
|
1746
|
+
const moods = Array.isArray(this._mood) ? this._mood : [this._mood];
|
|
1747
|
+
sections.push(`Mood: ${moods.join(", ")}`);
|
|
1748
|
+
}
|
|
1749
|
+
if (this._pacing) {
|
|
1750
|
+
sections.push(`Pacing: ${this._pacing}`);
|
|
1751
|
+
}
|
|
1752
|
+
if (this._custom.length) {
|
|
1753
|
+
sections.push(this._custom.join("\n"));
|
|
1754
|
+
}
|
|
1755
|
+
return sections.join("\n\n");
|
|
1756
|
+
}
|
|
1757
|
+
build() {
|
|
1758
|
+
return {
|
|
1759
|
+
prompt: this.buildPromptText(),
|
|
1760
|
+
structure: {
|
|
1761
|
+
scene: this._scene,
|
|
1762
|
+
subject: this._subject,
|
|
1763
|
+
camera: this._camera,
|
|
1764
|
+
lighting: this._lighting,
|
|
1765
|
+
actions: this._actions.length ? this._actions : void 0,
|
|
1766
|
+
motion: this._motion,
|
|
1767
|
+
style: this._style,
|
|
1768
|
+
color: this._color,
|
|
1769
|
+
audio: this._audio,
|
|
1770
|
+
technical: this._technical,
|
|
1771
|
+
shots: this._shots.length ? this._shots : void 0,
|
|
1772
|
+
mood: this._mood,
|
|
1773
|
+
pacing: this._pacing,
|
|
1774
|
+
transitions: this._transitions.length ? this._transitions : void 0
|
|
1775
|
+
}
|
|
1776
|
+
};
|
|
1777
|
+
}
|
|
1778
|
+
toString() {
|
|
1779
|
+
return this.build().prompt;
|
|
1780
|
+
}
|
|
1781
|
+
toJSON() {
|
|
1782
|
+
return JSON.stringify(this.build().structure, null, 2);
|
|
1783
|
+
}
|
|
1784
|
+
toYAML() {
|
|
1785
|
+
return objectToYaml2(this.build().structure);
|
|
1786
|
+
}
|
|
1787
|
+
toMarkdown() {
|
|
1788
|
+
const built = this.build();
|
|
1789
|
+
const sections = ["# Video Prompt\n"];
|
|
1790
|
+
sections.push("## Prompt\n```\n" + built.prompt + "\n```\n");
|
|
1791
|
+
if (built.structure.scene) {
|
|
1792
|
+
sections.push("## Scene\n" + objectToMarkdownList2(built.structure.scene));
|
|
1793
|
+
}
|
|
1794
|
+
if (built.structure.subject) {
|
|
1795
|
+
sections.push("## Subject\n" + objectToMarkdownList2(built.structure.subject));
|
|
1796
|
+
}
|
|
1797
|
+
if (built.structure.camera) {
|
|
1798
|
+
sections.push("## Camera\n" + objectToMarkdownList2(built.structure.camera));
|
|
1799
|
+
}
|
|
1800
|
+
if (built.structure.lighting) {
|
|
1801
|
+
sections.push("## Lighting\n" + objectToMarkdownList2(built.structure.lighting));
|
|
1802
|
+
}
|
|
1803
|
+
if (built.structure.actions) {
|
|
1804
|
+
sections.push("## Actions\n" + built.structure.actions.map((a) => `- **Beat ${a.beat}:** ${a.action}`).join("\n"));
|
|
1805
|
+
}
|
|
1806
|
+
if (built.structure.style) {
|
|
1807
|
+
sections.push("## Style\n" + objectToMarkdownList2(built.structure.style));
|
|
1808
|
+
}
|
|
1809
|
+
if (built.structure.audio) {
|
|
1810
|
+
sections.push("## Audio\n" + objectToMarkdownList2(built.structure.audio));
|
|
1811
|
+
}
|
|
1812
|
+
if (built.structure.technical) {
|
|
1813
|
+
sections.push("## Technical\n" + objectToMarkdownList2(built.structure.technical));
|
|
1814
|
+
}
|
|
1815
|
+
return sections.join("\n");
|
|
1816
|
+
}
|
|
1817
|
+
outputFormat(fmt) {
|
|
1818
|
+
switch (fmt) {
|
|
1819
|
+
case "json":
|
|
1820
|
+
return this.toJSON();
|
|
1821
|
+
case "yaml":
|
|
1822
|
+
return this.toYAML();
|
|
1823
|
+
case "markdown":
|
|
1824
|
+
return this.toMarkdown();
|
|
1825
|
+
default:
|
|
1826
|
+
return this.toString();
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
};
|
|
1830
|
+
function objectToYaml2(obj, indent = 0) {
|
|
1831
|
+
const spaces = " ".repeat(indent);
|
|
1832
|
+
const lines = [];
|
|
1833
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1834
|
+
if (value === void 0 || value === null) continue;
|
|
1835
|
+
if (Array.isArray(value)) {
|
|
1836
|
+
if (value.length === 0) continue;
|
|
1837
|
+
lines.push(`${spaces}${key}:`);
|
|
1838
|
+
for (const item of value) {
|
|
1839
|
+
if (typeof item === "object") {
|
|
1840
|
+
lines.push(`${spaces} -`);
|
|
1841
|
+
lines.push(objectToYaml2(item, indent + 2).replace(/^/gm, " "));
|
|
1842
|
+
} else {
|
|
1843
|
+
lines.push(`${spaces} - ${item}`);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
} else if (typeof value === "object") {
|
|
1847
|
+
lines.push(`${spaces}${key}:`);
|
|
1848
|
+
lines.push(objectToYaml2(value, indent + 1));
|
|
1849
|
+
} else {
|
|
1850
|
+
lines.push(`${spaces}${key}: ${value}`);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
return lines.join("\n");
|
|
1854
|
+
}
|
|
1855
|
+
function objectToMarkdownList2(obj, indent = 0) {
|
|
1856
|
+
const spaces = " ".repeat(indent);
|
|
1857
|
+
const lines = [];
|
|
1858
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1859
|
+
if (value === void 0 || value === null) continue;
|
|
1860
|
+
if (Array.isArray(value)) {
|
|
1861
|
+
lines.push(`${spaces}- **${key}:** ${value.join(", ")}`);
|
|
1862
|
+
} else if (typeof value === "object") {
|
|
1863
|
+
lines.push(`${spaces}- **${key}:**`);
|
|
1864
|
+
lines.push(objectToMarkdownList2(value, indent + 1));
|
|
1865
|
+
} else {
|
|
1866
|
+
lines.push(`${spaces}- **${key}:** ${value}`);
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
return lines.join("\n");
|
|
1870
|
+
}
|
|
1871
|
+
function video() {
|
|
1872
|
+
return new VideoPromptBuilder();
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
// src/builder/audio.ts
|
|
1876
|
+
var AudioPromptBuilder = class {
|
|
1877
|
+
constructor() {
|
|
1878
|
+
this._tags = [];
|
|
1879
|
+
this._custom = [];
|
|
1880
|
+
}
|
|
1881
|
+
// --- Genre Methods ---
|
|
1882
|
+
genre(primary) {
|
|
1883
|
+
if (typeof primary === "string") {
|
|
1884
|
+
this._genre = { ...this._genre || { primary: "pop" }, primary };
|
|
1885
|
+
} else {
|
|
1886
|
+
this._genre = { ...this._genre || { primary: "pop" }, ...primary };
|
|
1887
|
+
}
|
|
1888
|
+
return this;
|
|
1889
|
+
}
|
|
1890
|
+
subgenre(subgenre) {
|
|
1891
|
+
this._genre = { ...this._genre || { primary: "pop" }, subgenre };
|
|
1892
|
+
return this;
|
|
1893
|
+
}
|
|
1894
|
+
fusion(genres) {
|
|
1895
|
+
this._genre = {
|
|
1896
|
+
...this._genre || { primary: "pop" },
|
|
1897
|
+
secondary: genres,
|
|
1898
|
+
fusion: genres
|
|
1899
|
+
};
|
|
1900
|
+
return this;
|
|
1901
|
+
}
|
|
1902
|
+
// --- Mood Methods ---
|
|
1903
|
+
mood(primary, ...secondary) {
|
|
1904
|
+
this._mood = {
|
|
1905
|
+
primary,
|
|
1906
|
+
secondary: secondary.length ? secondary : void 0
|
|
1907
|
+
};
|
|
1908
|
+
return this;
|
|
1909
|
+
}
|
|
1910
|
+
energy(level) {
|
|
1911
|
+
this._mood = { ...this._mood || { primary: "energetic" }, energy: level };
|
|
1912
|
+
return this;
|
|
1913
|
+
}
|
|
1914
|
+
emotion(emotion) {
|
|
1915
|
+
this._mood = { ...this._mood || { primary: "emotional" }, emotion };
|
|
1916
|
+
return this;
|
|
1917
|
+
}
|
|
1918
|
+
// --- Tempo Methods ---
|
|
1919
|
+
tempo(bpmOrSettings) {
|
|
1920
|
+
if (typeof bpmOrSettings === "number") {
|
|
1921
|
+
this._tempo = { ...this._tempo || {}, bpm: bpmOrSettings };
|
|
1922
|
+
} else {
|
|
1923
|
+
this._tempo = { ...this._tempo || {}, ...bpmOrSettings };
|
|
1924
|
+
}
|
|
1925
|
+
return this;
|
|
1926
|
+
}
|
|
1927
|
+
bpm(bpm) {
|
|
1928
|
+
this._tempo = { ...this._tempo || {}, bpm };
|
|
1929
|
+
return this;
|
|
1930
|
+
}
|
|
1931
|
+
tempoMarking(marking) {
|
|
1932
|
+
this._tempo = { ...this._tempo || {}, marking };
|
|
1933
|
+
return this;
|
|
1934
|
+
}
|
|
1935
|
+
tempoFeel(feel) {
|
|
1936
|
+
this._tempo = { ...this._tempo || {}, feel };
|
|
1937
|
+
return this;
|
|
1938
|
+
}
|
|
1939
|
+
// --- Vocal Methods ---
|
|
1940
|
+
vocals(settings) {
|
|
1941
|
+
this._vocals = { ...this._vocals || {}, ...settings };
|
|
1942
|
+
return this;
|
|
1943
|
+
}
|
|
1944
|
+
vocalStyle(style) {
|
|
1945
|
+
this._vocals = { ...this._vocals || {}, style };
|
|
1946
|
+
return this;
|
|
1947
|
+
}
|
|
1948
|
+
language(language) {
|
|
1949
|
+
this._vocals = { ...this._vocals || {}, language };
|
|
1950
|
+
return this;
|
|
1951
|
+
}
|
|
1952
|
+
lyrics(lyrics) {
|
|
1953
|
+
this._vocals = { ...this._vocals || {}, lyrics };
|
|
1954
|
+
return this;
|
|
1955
|
+
}
|
|
1956
|
+
lyricsTheme(theme) {
|
|
1957
|
+
this._vocals = { ...this._vocals || {}, theme };
|
|
1958
|
+
return this;
|
|
1959
|
+
}
|
|
1960
|
+
delivery(delivery) {
|
|
1961
|
+
this._vocals = { ...this._vocals || {}, delivery };
|
|
1962
|
+
return this;
|
|
1963
|
+
}
|
|
1964
|
+
instrumental() {
|
|
1965
|
+
this._vocals = { ...this._vocals || {}, language: "instrumental" };
|
|
1966
|
+
return this;
|
|
1967
|
+
}
|
|
1968
|
+
// --- Instrumentation Methods ---
|
|
1969
|
+
instruments(instruments) {
|
|
1970
|
+
this._instrumentation = {
|
|
1971
|
+
...this._instrumentation || {},
|
|
1972
|
+
lead: instruments
|
|
1973
|
+
};
|
|
1974
|
+
return this;
|
|
1975
|
+
}
|
|
1976
|
+
instrumentation(settings) {
|
|
1977
|
+
this._instrumentation = { ...this._instrumentation || {}, ...settings };
|
|
1978
|
+
return this;
|
|
1979
|
+
}
|
|
1980
|
+
leadInstrument(instrument) {
|
|
1981
|
+
this._instrumentation = { ...this._instrumentation || {}, lead: instrument };
|
|
1982
|
+
return this;
|
|
1983
|
+
}
|
|
1984
|
+
rhythmSection(instruments) {
|
|
1985
|
+
this._instrumentation = { ...this._instrumentation || {}, rhythm: instruments };
|
|
1986
|
+
return this;
|
|
1987
|
+
}
|
|
1988
|
+
bassInstrument(instrument) {
|
|
1989
|
+
this._instrumentation = { ...this._instrumentation || {}, bass: instrument };
|
|
1990
|
+
return this;
|
|
1991
|
+
}
|
|
1992
|
+
percussion(instruments) {
|
|
1993
|
+
this._instrumentation = { ...this._instrumentation || {}, percussion: instruments };
|
|
1994
|
+
return this;
|
|
1995
|
+
}
|
|
1996
|
+
pads(instruments) {
|
|
1997
|
+
this._instrumentation = { ...this._instrumentation || {}, pads: instruments };
|
|
1998
|
+
return this;
|
|
1999
|
+
}
|
|
2000
|
+
featuredInstrument(instrument) {
|
|
2001
|
+
this._instrumentation = { ...this._instrumentation || {}, featured: instrument };
|
|
2002
|
+
return this;
|
|
2003
|
+
}
|
|
2004
|
+
// --- Structure Methods ---
|
|
2005
|
+
structure(settings) {
|
|
2006
|
+
if ("sections" in settings || "form" in settings || "duration" in settings) {
|
|
2007
|
+
this._structure = { ...this._structure || {}, ...settings };
|
|
2008
|
+
} else {
|
|
2009
|
+
const sections = [];
|
|
2010
|
+
for (const [type, bars] of Object.entries(settings)) {
|
|
2011
|
+
if (typeof bars === "number") {
|
|
2012
|
+
sections.push({ type, bars });
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
this._structure = { ...this._structure || {}, sections };
|
|
2016
|
+
}
|
|
2017
|
+
return this;
|
|
2018
|
+
}
|
|
2019
|
+
section(type, bars, description) {
|
|
2020
|
+
const sections = this._structure?.sections || [];
|
|
2021
|
+
sections.push({ type, bars, description });
|
|
2022
|
+
this._structure = { ...this._structure || {}, sections };
|
|
2023
|
+
return this;
|
|
2024
|
+
}
|
|
2025
|
+
form(form) {
|
|
2026
|
+
this._structure = { ...this._structure || {}, form };
|
|
2027
|
+
return this;
|
|
2028
|
+
}
|
|
2029
|
+
duration(seconds) {
|
|
2030
|
+
this._structure = { ...this._structure || {}, duration: seconds };
|
|
2031
|
+
return this;
|
|
2032
|
+
}
|
|
2033
|
+
// --- Production Methods ---
|
|
2034
|
+
production(settings) {
|
|
2035
|
+
this._production = { ...this._production || {}, ...settings };
|
|
2036
|
+
return this;
|
|
2037
|
+
}
|
|
2038
|
+
productionStyle(style) {
|
|
2039
|
+
this._production = { ...this._production || {}, style };
|
|
2040
|
+
return this;
|
|
2041
|
+
}
|
|
2042
|
+
era(era) {
|
|
2043
|
+
this._production = { ...this._production || {}, era };
|
|
2044
|
+
return this;
|
|
2045
|
+
}
|
|
2046
|
+
reference(artists) {
|
|
2047
|
+
this._production = { ...this._production || {}, reference: artists };
|
|
2048
|
+
return this;
|
|
2049
|
+
}
|
|
2050
|
+
texture(texture) {
|
|
2051
|
+
this._production = { ...this._production || {}, texture };
|
|
2052
|
+
return this;
|
|
2053
|
+
}
|
|
2054
|
+
effects(effects) {
|
|
2055
|
+
this._production = { ...this._production || {}, effects };
|
|
2056
|
+
return this;
|
|
2057
|
+
}
|
|
2058
|
+
// --- Technical Methods ---
|
|
2059
|
+
technical(settings) {
|
|
2060
|
+
this._technical = { ...this._technical || {}, ...settings };
|
|
2061
|
+
return this;
|
|
2062
|
+
}
|
|
2063
|
+
key(key) {
|
|
2064
|
+
this._technical = { ...this._technical || {}, key };
|
|
2065
|
+
return this;
|
|
2066
|
+
}
|
|
2067
|
+
timeSignature(sig) {
|
|
2068
|
+
this._technical = { ...this._technical || {}, timeSignature: sig };
|
|
2069
|
+
return this;
|
|
2070
|
+
}
|
|
2071
|
+
formatType(format) {
|
|
2072
|
+
this._technical = { ...this._technical || {}, format };
|
|
2073
|
+
return this;
|
|
2074
|
+
}
|
|
2075
|
+
// --- Tags & Custom ---
|
|
2076
|
+
tag(tag) {
|
|
2077
|
+
this._tags.push(tag);
|
|
2078
|
+
return this;
|
|
2079
|
+
}
|
|
2080
|
+
tags(tags) {
|
|
2081
|
+
this._tags = [...this._tags, ...tags];
|
|
2082
|
+
return this;
|
|
2083
|
+
}
|
|
2084
|
+
custom(text) {
|
|
2085
|
+
this._custom.push(text);
|
|
2086
|
+
return this;
|
|
2087
|
+
}
|
|
2088
|
+
// --- Build Methods ---
|
|
2089
|
+
buildStylePrompt() {
|
|
2090
|
+
const parts = [];
|
|
2091
|
+
if (this._genre) {
|
|
2092
|
+
let genreText = this._genre.primary;
|
|
2093
|
+
if (this._genre.subgenre) genreText = `${this._genre.subgenre} ${genreText}`;
|
|
2094
|
+
if (this._genre.secondary?.length) {
|
|
2095
|
+
genreText += ` with ${this._genre.secondary.join(" and ")} influences`;
|
|
2096
|
+
}
|
|
2097
|
+
parts.push(genreText);
|
|
2098
|
+
}
|
|
2099
|
+
if (this._mood) {
|
|
2100
|
+
let moodText = String(this._mood.primary);
|
|
2101
|
+
if (this._mood.secondary?.length) {
|
|
2102
|
+
moodText += `, ${this._mood.secondary.join(", ")}`;
|
|
2103
|
+
}
|
|
2104
|
+
if (this._mood.energy) moodText += `, ${this._mood.energy} energy`;
|
|
2105
|
+
parts.push(moodText);
|
|
2106
|
+
}
|
|
2107
|
+
if (this._tempo) {
|
|
2108
|
+
const tempoParts = [];
|
|
2109
|
+
if (this._tempo.bpm) tempoParts.push(`${this._tempo.bpm} BPM`);
|
|
2110
|
+
if (this._tempo.marking) tempoParts.push(this._tempo.marking);
|
|
2111
|
+
if (this._tempo.feel) tempoParts.push(`${this._tempo.feel} feel`);
|
|
2112
|
+
if (tempoParts.length) parts.push(tempoParts.join(", "));
|
|
2113
|
+
}
|
|
2114
|
+
if (this._instrumentation) {
|
|
2115
|
+
const instrParts = [];
|
|
2116
|
+
if (this._instrumentation.lead) {
|
|
2117
|
+
const leads = Array.isArray(this._instrumentation.lead) ? this._instrumentation.lead : [this._instrumentation.lead];
|
|
2118
|
+
instrParts.push(leads.join(", "));
|
|
2119
|
+
}
|
|
2120
|
+
if (this._instrumentation.featured) {
|
|
2121
|
+
instrParts.push(`featuring ${this._instrumentation.featured}`);
|
|
2122
|
+
}
|
|
2123
|
+
if (instrParts.length) parts.push(instrParts.join(", "));
|
|
2124
|
+
}
|
|
2125
|
+
if (this._vocals) {
|
|
2126
|
+
const vocalParts = [];
|
|
2127
|
+
if (this._vocals.language === "instrumental") {
|
|
2128
|
+
vocalParts.push("instrumental");
|
|
2129
|
+
} else {
|
|
2130
|
+
if (this._vocals.style) {
|
|
2131
|
+
const styles = Array.isArray(this._vocals.style) ? this._vocals.style : [this._vocals.style];
|
|
2132
|
+
vocalParts.push(`${styles.join(" and ")} vocals`);
|
|
2133
|
+
}
|
|
2134
|
+
if (this._vocals.language && this._vocals.language !== "english") {
|
|
2135
|
+
vocalParts.push(`in ${this._vocals.language}`);
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
if (vocalParts.length) parts.push(vocalParts.join(" "));
|
|
2139
|
+
}
|
|
2140
|
+
if (this._production) {
|
|
2141
|
+
const prodParts = [];
|
|
2142
|
+
if (this._production.style) {
|
|
2143
|
+
const styles = Array.isArray(this._production.style) ? this._production.style : [this._production.style];
|
|
2144
|
+
prodParts.push(`${styles.join(", ")} production`);
|
|
2145
|
+
}
|
|
2146
|
+
if (this._production.era) prodParts.push(`${this._production.era} sound`);
|
|
2147
|
+
if (this._production.texture) prodParts.push(this._production.texture);
|
|
2148
|
+
if (prodParts.length) parts.push(prodParts.join(", "));
|
|
2149
|
+
}
|
|
2150
|
+
if (this._technical) {
|
|
2151
|
+
const techParts = [];
|
|
2152
|
+
if (this._technical.key) techParts.push(`in the key of ${this._technical.key}`);
|
|
2153
|
+
if (this._technical.timeSignature && this._technical.timeSignature !== "4/4") {
|
|
2154
|
+
techParts.push(`${this._technical.timeSignature} time`);
|
|
2155
|
+
}
|
|
2156
|
+
if (techParts.length) parts.push(techParts.join(", "));
|
|
2157
|
+
}
|
|
2158
|
+
if (this._tags.length) {
|
|
2159
|
+
parts.push(this._tags.join(", "));
|
|
2160
|
+
}
|
|
2161
|
+
if (this._custom.length) {
|
|
2162
|
+
parts.push(this._custom.join(", "));
|
|
2163
|
+
}
|
|
2164
|
+
return parts.join(", ");
|
|
2165
|
+
}
|
|
2166
|
+
buildLyricsPrompt() {
|
|
2167
|
+
if (!this._vocals?.lyrics && !this._vocals?.theme) return void 0;
|
|
2168
|
+
const parts = [];
|
|
2169
|
+
if (this._vocals.theme) {
|
|
2170
|
+
parts.push(`Theme: ${this._vocals.theme}`);
|
|
2171
|
+
}
|
|
2172
|
+
if (this._vocals.lyrics) {
|
|
2173
|
+
parts.push(this._vocals.lyrics);
|
|
2174
|
+
}
|
|
2175
|
+
return parts.join("\n\n");
|
|
2176
|
+
}
|
|
2177
|
+
buildFullPrompt() {
|
|
2178
|
+
const sections = [];
|
|
2179
|
+
sections.push(`Style: ${this.buildStylePrompt()}`);
|
|
2180
|
+
if (this._structure?.sections?.length) {
|
|
2181
|
+
const structureText = this._structure.sections.map((s) => `[${s.type.toUpperCase()}]${s.description ? ` ${s.description}` : ""}`).join("\n");
|
|
2182
|
+
sections.push(`Structure:
|
|
2183
|
+
${structureText}`);
|
|
2184
|
+
}
|
|
2185
|
+
const lyrics = this.buildLyricsPrompt();
|
|
2186
|
+
if (lyrics) {
|
|
2187
|
+
sections.push(`Lyrics:
|
|
2188
|
+
${lyrics}`);
|
|
2189
|
+
}
|
|
2190
|
+
return sections.join("\n\n");
|
|
2191
|
+
}
|
|
2192
|
+
build() {
|
|
2193
|
+
return {
|
|
2194
|
+
prompt: this.buildFullPrompt(),
|
|
2195
|
+
stylePrompt: this.buildStylePrompt(),
|
|
2196
|
+
lyricsPrompt: this.buildLyricsPrompt(),
|
|
2197
|
+
structure: {
|
|
2198
|
+
genre: this._genre,
|
|
2199
|
+
mood: this._mood,
|
|
2200
|
+
tempo: this._tempo,
|
|
2201
|
+
vocals: this._vocals,
|
|
2202
|
+
instrumentation: this._instrumentation,
|
|
2203
|
+
structure: this._structure,
|
|
2204
|
+
production: this._production,
|
|
2205
|
+
technical: this._technical,
|
|
2206
|
+
tags: this._tags.length ? this._tags : void 0
|
|
2207
|
+
}
|
|
2208
|
+
};
|
|
2209
|
+
}
|
|
2210
|
+
toString() {
|
|
2211
|
+
return this.build().prompt;
|
|
2212
|
+
}
|
|
2213
|
+
toStyleString() {
|
|
2214
|
+
return this.build().stylePrompt;
|
|
2215
|
+
}
|
|
2216
|
+
toJSON() {
|
|
2217
|
+
return JSON.stringify(this.build().structure, null, 2);
|
|
2218
|
+
}
|
|
2219
|
+
toYAML() {
|
|
2220
|
+
return objectToYaml3(this.build().structure);
|
|
2221
|
+
}
|
|
2222
|
+
toMarkdown() {
|
|
2223
|
+
const built = this.build();
|
|
2224
|
+
const sections = ["# Audio Prompt\n"];
|
|
2225
|
+
sections.push("## Style Prompt\n```\n" + built.stylePrompt + "\n```\n");
|
|
2226
|
+
if (built.lyricsPrompt) {
|
|
2227
|
+
sections.push("## Lyrics\n```\n" + built.lyricsPrompt + "\n```\n");
|
|
2228
|
+
}
|
|
2229
|
+
if (built.structure.genre) {
|
|
2230
|
+
sections.push("## Genre\n" + objectToMarkdownList3(built.structure.genre));
|
|
2231
|
+
}
|
|
2232
|
+
if (built.structure.mood) {
|
|
2233
|
+
sections.push("## Mood\n" + objectToMarkdownList3(built.structure.mood));
|
|
2234
|
+
}
|
|
2235
|
+
if (built.structure.tempo) {
|
|
2236
|
+
sections.push("## Tempo\n" + objectToMarkdownList3(built.structure.tempo));
|
|
2237
|
+
}
|
|
2238
|
+
if (built.structure.vocals) {
|
|
2239
|
+
sections.push("## Vocals\n" + objectToMarkdownList3(built.structure.vocals));
|
|
2240
|
+
}
|
|
2241
|
+
if (built.structure.instrumentation) {
|
|
2242
|
+
sections.push("## Instrumentation\n" + objectToMarkdownList3(built.structure.instrumentation));
|
|
2243
|
+
}
|
|
2244
|
+
if (built.structure.production) {
|
|
2245
|
+
sections.push("## Production\n" + objectToMarkdownList3(built.structure.production));
|
|
2246
|
+
}
|
|
2247
|
+
return sections.join("\n");
|
|
2248
|
+
}
|
|
2249
|
+
outputFormat(fmt) {
|
|
2250
|
+
switch (fmt) {
|
|
2251
|
+
case "json":
|
|
2252
|
+
return this.toJSON();
|
|
2253
|
+
case "yaml":
|
|
2254
|
+
return this.toYAML();
|
|
2255
|
+
case "markdown":
|
|
2256
|
+
return this.toMarkdown();
|
|
2257
|
+
default:
|
|
2258
|
+
return this.toString();
|
|
2259
|
+
}
|
|
2260
|
+
}
|
|
2261
|
+
};
|
|
2262
|
+
function objectToYaml3(obj, indent = 0) {
|
|
2263
|
+
const spaces = " ".repeat(indent);
|
|
2264
|
+
const lines = [];
|
|
2265
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
2266
|
+
if (value === void 0 || value === null) continue;
|
|
2267
|
+
if (Array.isArray(value)) {
|
|
2268
|
+
if (value.length === 0) continue;
|
|
2269
|
+
lines.push(`${spaces}${key}:`);
|
|
2270
|
+
for (const item of value) {
|
|
2271
|
+
if (typeof item === "object") {
|
|
2272
|
+
lines.push(`${spaces} -`);
|
|
2273
|
+
lines.push(objectToYaml3(item, indent + 2).replace(/^/gm, " "));
|
|
2274
|
+
} else {
|
|
2275
|
+
lines.push(`${spaces} - ${item}`);
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
} else if (typeof value === "object") {
|
|
2279
|
+
lines.push(`${spaces}${key}:`);
|
|
2280
|
+
lines.push(objectToYaml3(value, indent + 1));
|
|
2281
|
+
} else {
|
|
2282
|
+
lines.push(`${spaces}${key}: ${value}`);
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
return lines.join("\n");
|
|
2286
|
+
}
|
|
2287
|
+
function objectToMarkdownList3(obj, indent = 0) {
|
|
2288
|
+
const spaces = " ".repeat(indent);
|
|
2289
|
+
const lines = [];
|
|
2290
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
2291
|
+
if (value === void 0 || value === null) continue;
|
|
2292
|
+
if (Array.isArray(value)) {
|
|
2293
|
+
lines.push(`${spaces}- **${key}:** ${value.join(", ")}`);
|
|
2294
|
+
} else if (typeof value === "object") {
|
|
2295
|
+
lines.push(`${spaces}- **${key}:**`);
|
|
2296
|
+
lines.push(objectToMarkdownList3(value, indent + 1));
|
|
2297
|
+
} else {
|
|
2298
|
+
lines.push(`${spaces}- **${key}:** ${value}`);
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
return lines.join("\n");
|
|
2302
|
+
}
|
|
2303
|
+
function audio() {
|
|
2304
|
+
return new AudioPromptBuilder();
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
// src/builder/chat.ts
|
|
2308
|
+
var ChatPromptBuilder = class {
|
|
2309
|
+
constructor() {
|
|
2310
|
+
this._messages = [];
|
|
2311
|
+
this._examples = [];
|
|
2312
|
+
this._customSystemParts = [];
|
|
2313
|
+
}
|
|
2314
|
+
// --- Message Methods ---
|
|
2315
|
+
system(content) {
|
|
2316
|
+
this._messages = this._messages.filter((m) => m.role !== "system");
|
|
2317
|
+
this._messages.unshift({ role: "system", content });
|
|
2318
|
+
return this;
|
|
2319
|
+
}
|
|
2320
|
+
user(content, name) {
|
|
2321
|
+
this._messages.push({ role: "user", content, name });
|
|
2322
|
+
return this;
|
|
2323
|
+
}
|
|
2324
|
+
assistant(content) {
|
|
2325
|
+
this._messages.push({ role: "assistant", content });
|
|
2326
|
+
return this;
|
|
2327
|
+
}
|
|
2328
|
+
message(role, content, name) {
|
|
2329
|
+
this._messages.push({ role, content, name });
|
|
2330
|
+
return this;
|
|
2331
|
+
}
|
|
2332
|
+
messages(messages) {
|
|
2333
|
+
this._messages = [...this._messages, ...messages];
|
|
2334
|
+
return this;
|
|
2335
|
+
}
|
|
2336
|
+
conversation(turns) {
|
|
2337
|
+
for (const turn of turns) {
|
|
2338
|
+
this.user(turn.user);
|
|
2339
|
+
if (turn.assistant) {
|
|
2340
|
+
this.assistant(turn.assistant);
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
return this;
|
|
2344
|
+
}
|
|
2345
|
+
// --- Persona Methods ---
|
|
2346
|
+
persona(settings) {
|
|
2347
|
+
if (typeof settings === "string") {
|
|
2348
|
+
this._persona = { ...this._persona || {}, role: settings };
|
|
2349
|
+
} else {
|
|
2350
|
+
this._persona = { ...this._persona || {}, ...settings };
|
|
2351
|
+
}
|
|
2352
|
+
return this;
|
|
2353
|
+
}
|
|
2354
|
+
role(role) {
|
|
2355
|
+
this._persona = { ...this._persona || {}, role };
|
|
2356
|
+
return this;
|
|
2357
|
+
}
|
|
2358
|
+
tone(tone) {
|
|
2359
|
+
this._persona = { ...this._persona || {}, tone };
|
|
2360
|
+
return this;
|
|
2361
|
+
}
|
|
2362
|
+
expertise(expertise) {
|
|
2363
|
+
this._persona = { ...this._persona || {}, expertise };
|
|
2364
|
+
return this;
|
|
2365
|
+
}
|
|
2366
|
+
personality(traits) {
|
|
2367
|
+
this._persona = { ...this._persona || {}, personality: traits };
|
|
2368
|
+
return this;
|
|
2369
|
+
}
|
|
2370
|
+
background(background) {
|
|
2371
|
+
this._persona = { ...this._persona || {}, background };
|
|
2372
|
+
return this;
|
|
2373
|
+
}
|
|
2374
|
+
speakAs(name) {
|
|
2375
|
+
this._persona = { ...this._persona || {}, name };
|
|
2376
|
+
return this;
|
|
2377
|
+
}
|
|
2378
|
+
responseLanguage(language) {
|
|
2379
|
+
this._persona = { ...this._persona || {}, language };
|
|
2380
|
+
return this;
|
|
2381
|
+
}
|
|
2382
|
+
// --- Context Methods ---
|
|
2383
|
+
context(settings) {
|
|
2384
|
+
if (typeof settings === "string") {
|
|
2385
|
+
this._context = { ...this._context || {}, background: settings };
|
|
2386
|
+
} else {
|
|
2387
|
+
this._context = { ...this._context || {}, ...settings };
|
|
2388
|
+
}
|
|
2389
|
+
return this;
|
|
2390
|
+
}
|
|
2391
|
+
domain(domain) {
|
|
2392
|
+
this._context = { ...this._context || {}, domain };
|
|
2393
|
+
return this;
|
|
2394
|
+
}
|
|
2395
|
+
audience(audience) {
|
|
2396
|
+
this._context = { ...this._context || {}, audience };
|
|
2397
|
+
return this;
|
|
2398
|
+
}
|
|
2399
|
+
purpose(purpose) {
|
|
2400
|
+
this._context = { ...this._context || {}, purpose };
|
|
2401
|
+
return this;
|
|
2402
|
+
}
|
|
2403
|
+
constraints(constraints) {
|
|
2404
|
+
const existing = this._context?.constraints || [];
|
|
2405
|
+
this._context = { ...this._context || {}, constraints: [...existing, ...constraints] };
|
|
2406
|
+
return this;
|
|
2407
|
+
}
|
|
2408
|
+
constraint(constraint) {
|
|
2409
|
+
return this.constraints([constraint]);
|
|
2410
|
+
}
|
|
2411
|
+
assumptions(assumptions) {
|
|
2412
|
+
this._context = { ...this._context || {}, assumptions };
|
|
2413
|
+
return this;
|
|
2414
|
+
}
|
|
2415
|
+
knowledge(facts) {
|
|
2416
|
+
this._context = { ...this._context || {}, knowledge: facts };
|
|
2417
|
+
return this;
|
|
2418
|
+
}
|
|
2419
|
+
// --- Task Methods ---
|
|
2420
|
+
task(instruction) {
|
|
2421
|
+
if (typeof instruction === "string") {
|
|
2422
|
+
this._task = { ...this._task || { instruction: "" }, instruction };
|
|
2423
|
+
} else {
|
|
2424
|
+
this._task = { ...this._task || { instruction: "" }, ...instruction };
|
|
2425
|
+
}
|
|
2426
|
+
return this;
|
|
2427
|
+
}
|
|
2428
|
+
instruction(instruction) {
|
|
2429
|
+
this._task = { ...this._task || { instruction: "" }, instruction };
|
|
2430
|
+
return this;
|
|
2431
|
+
}
|
|
2432
|
+
steps(steps) {
|
|
2433
|
+
this._task = { ...this._task || { instruction: "" }, steps };
|
|
2434
|
+
return this;
|
|
2435
|
+
}
|
|
2436
|
+
deliverables(deliverables) {
|
|
2437
|
+
this._task = { ...this._task || { instruction: "" }, deliverables };
|
|
2438
|
+
return this;
|
|
2439
|
+
}
|
|
2440
|
+
criteria(criteria) {
|
|
2441
|
+
this._task = { ...this._task || { instruction: "" }, criteria };
|
|
2442
|
+
return this;
|
|
2443
|
+
}
|
|
2444
|
+
avoid(antiPatterns) {
|
|
2445
|
+
this._task = { ...this._task || { instruction: "" }, antiPatterns };
|
|
2446
|
+
return this;
|
|
2447
|
+
}
|
|
2448
|
+
priority(priority) {
|
|
2449
|
+
this._task = { ...this._task || { instruction: "" }, priority };
|
|
2450
|
+
return this;
|
|
2451
|
+
}
|
|
2452
|
+
// --- Example Methods ---
|
|
2453
|
+
example(input, output, explanation) {
|
|
2454
|
+
this._examples.push({ input, output, explanation });
|
|
2455
|
+
return this;
|
|
2456
|
+
}
|
|
2457
|
+
examples(examples) {
|
|
2458
|
+
this._examples = [...this._examples, ...examples];
|
|
2459
|
+
return this;
|
|
2460
|
+
}
|
|
2461
|
+
fewShot(examples) {
|
|
2462
|
+
for (const ex of examples) {
|
|
2463
|
+
this._examples.push(ex);
|
|
2464
|
+
}
|
|
2465
|
+
return this;
|
|
2466
|
+
}
|
|
2467
|
+
// --- Output Methods ---
|
|
2468
|
+
output(settings) {
|
|
2469
|
+
this._output = { ...this._output || {}, ...settings };
|
|
2470
|
+
return this;
|
|
2471
|
+
}
|
|
2472
|
+
outputFormat(format) {
|
|
2473
|
+
this._output = {
|
|
2474
|
+
...this._output || {},
|
|
2475
|
+
format: { type: format }
|
|
2476
|
+
};
|
|
2477
|
+
return this;
|
|
2478
|
+
}
|
|
2479
|
+
json(schema) {
|
|
2480
|
+
if (schema) {
|
|
2481
|
+
this._output = {
|
|
2482
|
+
...this._output || {},
|
|
2483
|
+
format: { type: "json", jsonSchema: schema }
|
|
2484
|
+
};
|
|
2485
|
+
} else {
|
|
2486
|
+
this._output = {
|
|
2487
|
+
...this._output || {},
|
|
2488
|
+
format: { type: "json" }
|
|
2489
|
+
};
|
|
2490
|
+
}
|
|
2491
|
+
return this;
|
|
2492
|
+
}
|
|
2493
|
+
jsonSchema(name, schema, description) {
|
|
2494
|
+
this._output = {
|
|
2495
|
+
...this._output || {},
|
|
2496
|
+
format: {
|
|
2497
|
+
type: "json",
|
|
2498
|
+
jsonSchema: { name, schema, description }
|
|
2499
|
+
}
|
|
2500
|
+
};
|
|
2501
|
+
return this;
|
|
2502
|
+
}
|
|
2503
|
+
markdown() {
|
|
2504
|
+
this._output = { ...this._output || {}, format: { type: "markdown" } };
|
|
2505
|
+
return this;
|
|
2506
|
+
}
|
|
2507
|
+
code(language) {
|
|
2508
|
+
this._output = { ...this._output || {}, format: { type: "code", language } };
|
|
2509
|
+
return this;
|
|
2510
|
+
}
|
|
2511
|
+
table() {
|
|
2512
|
+
this._output = { ...this._output || {}, format: { type: "table" } };
|
|
2513
|
+
return this;
|
|
2514
|
+
}
|
|
2515
|
+
length(length) {
|
|
2516
|
+
this._output = { ...this._output || {}, length };
|
|
2517
|
+
return this;
|
|
2518
|
+
}
|
|
2519
|
+
style(style) {
|
|
2520
|
+
this._output = { ...this._output || {}, style };
|
|
2521
|
+
return this;
|
|
2522
|
+
}
|
|
2523
|
+
brief() {
|
|
2524
|
+
return this.length("brief");
|
|
2525
|
+
}
|
|
2526
|
+
moderate() {
|
|
2527
|
+
return this.length("moderate");
|
|
2528
|
+
}
|
|
2529
|
+
detailed() {
|
|
2530
|
+
return this.length("detailed");
|
|
2531
|
+
}
|
|
2532
|
+
comprehensive() {
|
|
2533
|
+
return this.length("comprehensive");
|
|
2534
|
+
}
|
|
2535
|
+
exhaustive() {
|
|
2536
|
+
return this.length("exhaustive");
|
|
2537
|
+
}
|
|
2538
|
+
withExamples() {
|
|
2539
|
+
this._output = { ...this._output || {}, includeExamples: true };
|
|
2540
|
+
return this;
|
|
2541
|
+
}
|
|
2542
|
+
withExplanation() {
|
|
2543
|
+
this._output = { ...this._output || {}, includeExplanation: true };
|
|
2544
|
+
return this;
|
|
2545
|
+
}
|
|
2546
|
+
withSources() {
|
|
2547
|
+
this._output = { ...this._output || {}, includeSources: true };
|
|
2548
|
+
return this;
|
|
2549
|
+
}
|
|
2550
|
+
withConfidence() {
|
|
2551
|
+
this._output = { ...this._output || {}, includeConfidence: true };
|
|
2552
|
+
return this;
|
|
2553
|
+
}
|
|
2554
|
+
// --- Reasoning Methods ---
|
|
2555
|
+
reasoning(settings) {
|
|
2556
|
+
this._reasoning = { ...this._reasoning || {}, ...settings };
|
|
2557
|
+
return this;
|
|
2558
|
+
}
|
|
2559
|
+
reasoningStyle(style) {
|
|
2560
|
+
this._reasoning = { ...this._reasoning || {}, style };
|
|
2561
|
+
return this;
|
|
2562
|
+
}
|
|
2563
|
+
stepByStep() {
|
|
2564
|
+
this._reasoning = { ...this._reasoning || {}, style: "step-by-step", showWork: true };
|
|
2565
|
+
return this;
|
|
2566
|
+
}
|
|
2567
|
+
chainOfThought() {
|
|
2568
|
+
this._reasoning = { ...this._reasoning || {}, style: "chain-of-thought", showWork: true };
|
|
2569
|
+
return this;
|
|
2570
|
+
}
|
|
2571
|
+
treeOfThought() {
|
|
2572
|
+
this._reasoning = { ...this._reasoning || {}, style: "tree-of-thought", showWork: true };
|
|
2573
|
+
return this;
|
|
2574
|
+
}
|
|
2575
|
+
firstPrinciples() {
|
|
2576
|
+
this._reasoning = { ...this._reasoning || {}, style: "first-principles", showWork: true };
|
|
2577
|
+
return this;
|
|
2578
|
+
}
|
|
2579
|
+
devilsAdvocate() {
|
|
2580
|
+
this._reasoning = { ...this._reasoning || {}, style: "devil-advocate", considerAlternatives: true };
|
|
2581
|
+
return this;
|
|
2582
|
+
}
|
|
2583
|
+
showWork(show = true) {
|
|
2584
|
+
this._reasoning = { ...this._reasoning || {}, showWork: show };
|
|
2585
|
+
return this;
|
|
2586
|
+
}
|
|
2587
|
+
verifyAnswer(verify = true) {
|
|
2588
|
+
this._reasoning = { ...this._reasoning || {}, verifyAnswer: verify };
|
|
2589
|
+
return this;
|
|
2590
|
+
}
|
|
2591
|
+
considerAlternatives(consider = true) {
|
|
2592
|
+
this._reasoning = { ...this._reasoning || {}, considerAlternatives: consider };
|
|
2593
|
+
return this;
|
|
2594
|
+
}
|
|
2595
|
+
explainAssumptions(explain = true) {
|
|
2596
|
+
this._reasoning = { ...this._reasoning || {}, explainAssumptions: explain };
|
|
2597
|
+
return this;
|
|
2598
|
+
}
|
|
2599
|
+
// --- Memory Methods ---
|
|
2600
|
+
memory(memory) {
|
|
2601
|
+
this._memory = { ...this._memory || {}, ...memory };
|
|
2602
|
+
return this;
|
|
2603
|
+
}
|
|
2604
|
+
remember(facts) {
|
|
2605
|
+
const existing = this._memory?.facts || [];
|
|
2606
|
+
this._memory = { ...this._memory || {}, facts: [...existing, ...facts] };
|
|
2607
|
+
return this;
|
|
2608
|
+
}
|
|
2609
|
+
preferences(prefs) {
|
|
2610
|
+
this._memory = { ...this._memory || {}, preferences: prefs };
|
|
2611
|
+
return this;
|
|
2612
|
+
}
|
|
2613
|
+
history(messages) {
|
|
2614
|
+
this._memory = { ...this._memory || {}, history: messages };
|
|
2615
|
+
return this;
|
|
2616
|
+
}
|
|
2617
|
+
summarizeHistory(summary) {
|
|
2618
|
+
this._memory = { ...this._memory || {}, summary };
|
|
2619
|
+
return this;
|
|
2620
|
+
}
|
|
2621
|
+
// --- Custom System Prompt Parts ---
|
|
2622
|
+
addSystemPart(part) {
|
|
2623
|
+
this._customSystemParts.push(part);
|
|
2624
|
+
return this;
|
|
2625
|
+
}
|
|
2626
|
+
raw(content) {
|
|
2627
|
+
this._customSystemParts = [content];
|
|
2628
|
+
return this;
|
|
2629
|
+
}
|
|
2630
|
+
// --- Build Methods ---
|
|
2631
|
+
buildSystemPrompt() {
|
|
2632
|
+
const parts = [];
|
|
2633
|
+
if (this._persona) {
|
|
2634
|
+
let personaText = "";
|
|
2635
|
+
if (this._persona.name) {
|
|
2636
|
+
personaText += `You are ${this._persona.name}`;
|
|
2637
|
+
if (this._persona.role) personaText += `, ${this._persona.role}`;
|
|
2638
|
+
personaText += ".";
|
|
2639
|
+
} else if (this._persona.role) {
|
|
2640
|
+
personaText += `You are ${this._persona.role}.`;
|
|
2641
|
+
}
|
|
2642
|
+
if (this._persona.tone) {
|
|
2643
|
+
const tones = Array.isArray(this._persona.tone) ? this._persona.tone : [this._persona.tone];
|
|
2644
|
+
personaText += ` Your tone is ${tones.join(" and ")}.`;
|
|
2645
|
+
}
|
|
2646
|
+
if (this._persona.expertise) {
|
|
2647
|
+
const areas = Array.isArray(this._persona.expertise) ? this._persona.expertise : [this._persona.expertise];
|
|
2648
|
+
personaText += ` You have expertise in ${areas.join(", ")}.`;
|
|
2649
|
+
}
|
|
2650
|
+
if (this._persona.personality?.length) {
|
|
2651
|
+
personaText += ` You are ${this._persona.personality.join(", ")}.`;
|
|
2652
|
+
}
|
|
2653
|
+
if (this._persona.background) {
|
|
2654
|
+
personaText += ` ${this._persona.background}`;
|
|
2655
|
+
}
|
|
2656
|
+
if (this._persona.verbosity) {
|
|
2657
|
+
personaText += ` Keep responses ${this._persona.verbosity}.`;
|
|
2658
|
+
}
|
|
2659
|
+
if (this._persona.language) {
|
|
2660
|
+
personaText += ` Respond in ${this._persona.language}.`;
|
|
2661
|
+
}
|
|
2662
|
+
if (personaText) parts.push(personaText.trim());
|
|
2663
|
+
}
|
|
2664
|
+
if (this._context) {
|
|
2665
|
+
const contextParts = [];
|
|
2666
|
+
if (this._context.background) {
|
|
2667
|
+
contextParts.push(this._context.background);
|
|
2668
|
+
}
|
|
2669
|
+
if (this._context.domain) {
|
|
2670
|
+
contextParts.push(`Domain: ${this._context.domain}`);
|
|
2671
|
+
}
|
|
2672
|
+
if (this._context.audience) {
|
|
2673
|
+
contextParts.push(`Target audience: ${this._context.audience}`);
|
|
2674
|
+
}
|
|
2675
|
+
if (this._context.purpose) {
|
|
2676
|
+
contextParts.push(`Purpose: ${this._context.purpose}`);
|
|
2677
|
+
}
|
|
2678
|
+
if (this._context.knowledge?.length) {
|
|
2679
|
+
contextParts.push(`Known facts:
|
|
2680
|
+
${this._context.knowledge.map((k) => `- ${k}`).join("\n")}`);
|
|
2681
|
+
}
|
|
2682
|
+
if (this._context.assumptions?.length) {
|
|
2683
|
+
contextParts.push(`Assumptions:
|
|
2684
|
+
${this._context.assumptions.map((a) => `- ${a}`).join("\n")}`);
|
|
2685
|
+
}
|
|
2686
|
+
if (contextParts.length) {
|
|
2687
|
+
parts.push(`## Context
|
|
2688
|
+
${contextParts.join("\n")}`);
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
if (this._task) {
|
|
2692
|
+
const taskParts = [];
|
|
2693
|
+
if (this._task.instruction) {
|
|
2694
|
+
taskParts.push(this._task.instruction);
|
|
2695
|
+
}
|
|
2696
|
+
if (this._task.priority) {
|
|
2697
|
+
taskParts.push(`Priority: ${this._task.priority}`);
|
|
2698
|
+
}
|
|
2699
|
+
if (this._task.steps?.length) {
|
|
2700
|
+
taskParts.push(`
|
|
2701
|
+
Steps:
|
|
2702
|
+
${this._task.steps.map((s, i) => `${i + 1}. ${s}`).join("\n")}`);
|
|
2703
|
+
}
|
|
2704
|
+
if (this._task.deliverables?.length) {
|
|
2705
|
+
taskParts.push(`
|
|
2706
|
+
Deliverables:
|
|
2707
|
+
${this._task.deliverables.map((d) => `- ${d}`).join("\n")}`);
|
|
2708
|
+
}
|
|
2709
|
+
if (this._task.criteria?.length) {
|
|
2710
|
+
taskParts.push(`
|
|
2711
|
+
Success criteria:
|
|
2712
|
+
${this._task.criteria.map((c) => `- ${c}`).join("\n")}`);
|
|
2713
|
+
}
|
|
2714
|
+
if (this._task.antiPatterns?.length) {
|
|
2715
|
+
taskParts.push(`
|
|
2716
|
+
Avoid:
|
|
2717
|
+
${this._task.antiPatterns.map((a) => `- ${a}`).join("\n")}`);
|
|
2718
|
+
}
|
|
2719
|
+
if (taskParts.length) {
|
|
2720
|
+
parts.push(`## Task
|
|
2721
|
+
${taskParts.join("\n")}`);
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
if (this._context?.constraints?.length) {
|
|
2725
|
+
parts.push(`## Constraints
|
|
2726
|
+
${this._context.constraints.map((c, i) => `${i + 1}. ${c}`).join("\n")}`);
|
|
2727
|
+
}
|
|
2728
|
+
if (this._examples.length) {
|
|
2729
|
+
const examplesText = this._examples.map((ex, i) => {
|
|
2730
|
+
let text = `### Example ${i + 1}
|
|
2731
|
+
**Input:** ${ex.input}
|
|
2732
|
+
**Output:** ${ex.output}`;
|
|
2733
|
+
if (ex.explanation) text += `
|
|
2734
|
+
**Explanation:** ${ex.explanation}`;
|
|
2735
|
+
return text;
|
|
2736
|
+
}).join("\n\n");
|
|
2737
|
+
parts.push(`## Examples
|
|
2738
|
+
${examplesText}`);
|
|
2739
|
+
}
|
|
2740
|
+
if (this._output) {
|
|
2741
|
+
const outputParts = [];
|
|
2742
|
+
if (this._output.format) {
|
|
2743
|
+
switch (this._output.format.type) {
|
|
2744
|
+
case "json":
|
|
2745
|
+
if (this._output.format.jsonSchema) {
|
|
2746
|
+
outputParts.push(`Respond in valid JSON matching this schema:
|
|
2747
|
+
\`\`\`json
|
|
2748
|
+
${JSON.stringify(this._output.format.jsonSchema.schema, null, 2)}
|
|
2749
|
+
\`\`\``);
|
|
2750
|
+
} else {
|
|
2751
|
+
outputParts.push("Respond in valid JSON format.");
|
|
2752
|
+
}
|
|
2753
|
+
break;
|
|
2754
|
+
case "markdown":
|
|
2755
|
+
outputParts.push("Format your response using Markdown.");
|
|
2756
|
+
break;
|
|
2757
|
+
case "code":
|
|
2758
|
+
outputParts.push(`Respond with code${this._output.format.language ? ` in ${this._output.format.language}` : ""}.`);
|
|
2759
|
+
break;
|
|
2760
|
+
case "table":
|
|
2761
|
+
outputParts.push("Format your response as a table.");
|
|
2762
|
+
break;
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
if (this._output.length) {
|
|
2766
|
+
outputParts.push(`Keep your response ${this._output.length}.`);
|
|
2767
|
+
}
|
|
2768
|
+
if (this._output.style) {
|
|
2769
|
+
const styleMap = {
|
|
2770
|
+
"prose": "flowing prose",
|
|
2771
|
+
"bullet-points": "bullet points",
|
|
2772
|
+
"numbered-list": "a numbered list",
|
|
2773
|
+
"table": "a table",
|
|
2774
|
+
"code": "code",
|
|
2775
|
+
"mixed": "a mix of prose and lists",
|
|
2776
|
+
"qa": "Q&A format",
|
|
2777
|
+
"dialogue": "dialogue format"
|
|
2778
|
+
};
|
|
2779
|
+
outputParts.push(`Structure as ${styleMap[this._output.style]}.`);
|
|
2780
|
+
}
|
|
2781
|
+
if (this._output.language) {
|
|
2782
|
+
outputParts.push(`Respond in ${this._output.language}.`);
|
|
2783
|
+
}
|
|
2784
|
+
if (this._output.includeExamples) {
|
|
2785
|
+
outputParts.push("Include relevant examples.");
|
|
2786
|
+
}
|
|
2787
|
+
if (this._output.includeExplanation) {
|
|
2788
|
+
outputParts.push("Include clear explanations.");
|
|
2789
|
+
}
|
|
2790
|
+
if (this._output.includeSources) {
|
|
2791
|
+
outputParts.push("Cite your sources.");
|
|
2792
|
+
}
|
|
2793
|
+
if (this._output.includeConfidence) {
|
|
2794
|
+
outputParts.push("Include your confidence level in the answer.");
|
|
2795
|
+
}
|
|
2796
|
+
if (outputParts.length) {
|
|
2797
|
+
parts.push(`## Output Format
|
|
2798
|
+
${outputParts.join("\n")}`);
|
|
2799
|
+
}
|
|
2800
|
+
}
|
|
2801
|
+
if (this._reasoning) {
|
|
2802
|
+
const reasoningParts = [];
|
|
2803
|
+
if (this._reasoning.style) {
|
|
2804
|
+
const styleInstructions = {
|
|
2805
|
+
"step-by-step": "Think through this step by step.",
|
|
2806
|
+
"chain-of-thought": "Use chain-of-thought reasoning to work through the problem.",
|
|
2807
|
+
"tree-of-thought": "Consider multiple approaches and evaluate each before deciding.",
|
|
2808
|
+
"direct": "Provide a direct answer.",
|
|
2809
|
+
"analytical": "Analyze the problem systematically.",
|
|
2810
|
+
"comparative": "Compare different options or approaches.",
|
|
2811
|
+
"deductive": "Use deductive reasoning from general principles.",
|
|
2812
|
+
"inductive": "Use inductive reasoning from specific examples.",
|
|
2813
|
+
"first-principles": "Reason from first principles, breaking down to fundamental truths.",
|
|
2814
|
+
"analogical": "Use analogies to explain and reason about the problem.",
|
|
2815
|
+
"devil-advocate": "Consider and argue against your own conclusions."
|
|
2816
|
+
};
|
|
2817
|
+
reasoningParts.push(styleInstructions[this._reasoning.style]);
|
|
2818
|
+
}
|
|
2819
|
+
if (this._reasoning.showWork) {
|
|
2820
|
+
reasoningParts.push("Show your reasoning process.");
|
|
2821
|
+
}
|
|
2822
|
+
if (this._reasoning.verifyAnswer) {
|
|
2823
|
+
reasoningParts.push("Verify your answer before presenting it.");
|
|
2824
|
+
}
|
|
2825
|
+
if (this._reasoning.considerAlternatives) {
|
|
2826
|
+
reasoningParts.push("Consider alternative perspectives and solutions.");
|
|
2827
|
+
}
|
|
2828
|
+
if (this._reasoning.explainAssumptions) {
|
|
2829
|
+
reasoningParts.push("Explicitly state any assumptions you make.");
|
|
2830
|
+
}
|
|
2831
|
+
if (reasoningParts.length) {
|
|
2832
|
+
parts.push(`## Reasoning
|
|
2833
|
+
${reasoningParts.join("\n")}`);
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
if (this._memory) {
|
|
2837
|
+
const memoryParts = [];
|
|
2838
|
+
if (this._memory.summary) {
|
|
2839
|
+
memoryParts.push(`Previous conversation summary: ${this._memory.summary}`);
|
|
2840
|
+
}
|
|
2841
|
+
if (this._memory.facts?.length) {
|
|
2842
|
+
memoryParts.push(`Known facts:
|
|
2843
|
+
${this._memory.facts.map((f) => `- ${f}`).join("\n")}`);
|
|
2844
|
+
}
|
|
2845
|
+
if (this._memory.preferences?.length) {
|
|
2846
|
+
memoryParts.push(`User preferences:
|
|
2847
|
+
${this._memory.preferences.map((p) => `- ${p}`).join("\n")}`);
|
|
2848
|
+
}
|
|
2849
|
+
if (memoryParts.length) {
|
|
2850
|
+
parts.push(`## Memory
|
|
2851
|
+
${memoryParts.join("\n")}`);
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
if (this._customSystemParts.length) {
|
|
2855
|
+
parts.push(...this._customSystemParts);
|
|
2856
|
+
}
|
|
2857
|
+
return parts.join("\n\n");
|
|
2858
|
+
}
|
|
2859
|
+
build() {
|
|
2860
|
+
const systemPrompt = this.buildSystemPrompt();
|
|
2861
|
+
let messages = [...this._messages];
|
|
2862
|
+
const hasSystemMessage = messages.some((m) => m.role === "system");
|
|
2863
|
+
if (systemPrompt && !hasSystemMessage) {
|
|
2864
|
+
messages = [{ role: "system", content: systemPrompt }, ...messages];
|
|
2865
|
+
} else if (systemPrompt && hasSystemMessage) {
|
|
2866
|
+
messages = messages.map(
|
|
2867
|
+
(m) => m.role === "system" ? { ...m, content: `${systemPrompt}
|
|
2868
|
+
|
|
2869
|
+
${m.content}` } : m
|
|
2870
|
+
);
|
|
2871
|
+
}
|
|
2872
|
+
if (this._memory?.history) {
|
|
2873
|
+
const systemIdx = messages.findIndex((m) => m.role === "system");
|
|
2874
|
+
const insertIdx = systemIdx >= 0 ? systemIdx + 1 : 0;
|
|
2875
|
+
messages.splice(insertIdx, 0, ...this._memory.history);
|
|
2876
|
+
}
|
|
2877
|
+
const userMessages = messages.filter((m) => m.role === "user");
|
|
2878
|
+
const userPrompt = userMessages.length ? userMessages[userMessages.length - 1].content : void 0;
|
|
2879
|
+
return {
|
|
2880
|
+
messages,
|
|
2881
|
+
systemPrompt,
|
|
2882
|
+
userPrompt,
|
|
2883
|
+
metadata: {
|
|
2884
|
+
persona: this._persona,
|
|
2885
|
+
context: this._context,
|
|
2886
|
+
task: this._task,
|
|
2887
|
+
output: this._output,
|
|
2888
|
+
reasoning: this._reasoning,
|
|
2889
|
+
examples: this._examples.length ? this._examples : void 0
|
|
2890
|
+
}
|
|
2891
|
+
};
|
|
2892
|
+
}
|
|
2893
|
+
// --- Output Methods ---
|
|
2894
|
+
toString() {
|
|
2895
|
+
return this.buildSystemPrompt();
|
|
2896
|
+
}
|
|
2897
|
+
toSystemPrompt() {
|
|
2898
|
+
return this.buildSystemPrompt();
|
|
2899
|
+
}
|
|
2900
|
+
toMessages() {
|
|
2901
|
+
return this.build().messages;
|
|
2902
|
+
}
|
|
2903
|
+
toJSON() {
|
|
2904
|
+
return JSON.stringify(this.build(), null, 2);
|
|
2905
|
+
}
|
|
2906
|
+
toYAML() {
|
|
2907
|
+
return objectToYaml4(this.build());
|
|
2908
|
+
}
|
|
2909
|
+
toMarkdown() {
|
|
2910
|
+
const built = this.build();
|
|
2911
|
+
const sections = ["# Chat Prompt\n"];
|
|
2912
|
+
sections.push("## System Prompt\n```\n" + built.systemPrompt + "\n```\n");
|
|
2913
|
+
if (built.messages.length > 1) {
|
|
2914
|
+
sections.push("## Messages\n");
|
|
2915
|
+
for (const msg of built.messages) {
|
|
2916
|
+
if (msg.role === "system") continue;
|
|
2917
|
+
sections.push(`**${msg.role.toUpperCase()}${msg.name ? ` (${msg.name})` : ""}:**
|
|
2918
|
+
${msg.content}
|
|
2919
|
+
`);
|
|
2920
|
+
}
|
|
2921
|
+
}
|
|
2922
|
+
return sections.join("\n");
|
|
2923
|
+
}
|
|
2924
|
+
};
|
|
2925
|
+
function objectToYaml4(obj, indent = 0) {
|
|
2926
|
+
const spaces = " ".repeat(indent);
|
|
2927
|
+
const lines = [];
|
|
2928
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
2929
|
+
if (value === void 0 || value === null) continue;
|
|
2930
|
+
if (Array.isArray(value)) {
|
|
2931
|
+
if (value.length === 0) continue;
|
|
2932
|
+
lines.push(`${spaces}${key}:`);
|
|
2933
|
+
for (const item of value) {
|
|
2934
|
+
if (typeof item === "object") {
|
|
2935
|
+
lines.push(`${spaces} -`);
|
|
2936
|
+
lines.push(objectToYaml4(item, indent + 2).replace(/^/gm, " "));
|
|
2937
|
+
} else {
|
|
2938
|
+
lines.push(`${spaces} - ${item}`);
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
} else if (typeof value === "object") {
|
|
2942
|
+
lines.push(`${spaces}${key}:`);
|
|
2943
|
+
lines.push(objectToYaml4(value, indent + 1));
|
|
2944
|
+
} else if (typeof value === "string" && value.includes("\n")) {
|
|
2945
|
+
lines.push(`${spaces}${key}: |`);
|
|
2946
|
+
for (const line of value.split("\n")) {
|
|
2947
|
+
lines.push(`${spaces} ${line}`);
|
|
2948
|
+
}
|
|
2949
|
+
} else {
|
|
2950
|
+
lines.push(`${spaces}${key}: ${value}`);
|
|
2951
|
+
}
|
|
2952
|
+
}
|
|
2953
|
+
return lines.join("\n");
|
|
2954
|
+
}
|
|
2955
|
+
function chat() {
|
|
2956
|
+
return new ChatPromptBuilder();
|
|
2957
|
+
}
|
|
2958
|
+
var chatPresets = {
|
|
2959
|
+
/**
|
|
2960
|
+
* Code assistant preset
|
|
2961
|
+
*/
|
|
2962
|
+
coder: (language) => {
|
|
2963
|
+
const c = chat().role("expert software developer").expertise("coding").tone("technical");
|
|
2964
|
+
if (language) {
|
|
2965
|
+
c.context(`Programming language: ${language}`);
|
|
2966
|
+
}
|
|
2967
|
+
return c;
|
|
2968
|
+
},
|
|
2969
|
+
/**
|
|
2970
|
+
* Writing assistant preset
|
|
2971
|
+
*/
|
|
2972
|
+
writer: (style) => {
|
|
2973
|
+
const c = chat().role("skilled writer and editor").expertise("writing");
|
|
2974
|
+
if (style) {
|
|
2975
|
+
c.tone(style === "creative" ? "creative" : style === "academic" ? "academic" : "professional");
|
|
2976
|
+
}
|
|
2977
|
+
return c;
|
|
2978
|
+
},
|
|
2979
|
+
/**
|
|
2980
|
+
* Teacher/tutor preset
|
|
2981
|
+
*/
|
|
2982
|
+
tutor: (subject) => {
|
|
2983
|
+
const c = chat().role("patient and knowledgeable tutor").expertise("teaching").tone(["friendly", "empathetic"]).stepByStep().withExamples();
|
|
2984
|
+
if (subject) {
|
|
2985
|
+
c.domain(subject);
|
|
2986
|
+
}
|
|
2987
|
+
return c;
|
|
2988
|
+
},
|
|
2989
|
+
/**
|
|
2990
|
+
* Analyst preset
|
|
2991
|
+
*/
|
|
2992
|
+
analyst: () => {
|
|
2993
|
+
return chat().role("data analyst and researcher").expertise("analysis").tone("analytical").chainOfThought().detailed().withSources();
|
|
2994
|
+
},
|
|
2995
|
+
/**
|
|
2996
|
+
* Socratic dialogue preset
|
|
2997
|
+
*/
|
|
2998
|
+
socratic: () => {
|
|
2999
|
+
return chat().role("Socratic philosopher and teacher").tone("socratic").reasoning({ style: "deductive", showWork: true }).avoid(["Give direct answers", "Lecture", "Be condescending"]);
|
|
3000
|
+
},
|
|
3001
|
+
/**
|
|
3002
|
+
* Critic preset
|
|
3003
|
+
*/
|
|
3004
|
+
critic: () => {
|
|
3005
|
+
return chat().role("constructive critic").tone(["analytical", "professional"]).devilsAdvocate().detailed().avoid(["Be harsh", "Dismiss ideas without explanation"]);
|
|
3006
|
+
},
|
|
3007
|
+
/**
|
|
3008
|
+
* Brainstormer preset
|
|
3009
|
+
*/
|
|
3010
|
+
brainstormer: () => {
|
|
3011
|
+
return chat().role("creative brainstorming partner").tone(["creative", "encouraging"]).expertise("creative").considerAlternatives().avoid(["Dismiss ideas", "Be negative", "Limit creativity"]);
|
|
3012
|
+
},
|
|
3013
|
+
/**
|
|
3014
|
+
* JSON response preset
|
|
3015
|
+
*/
|
|
3016
|
+
jsonResponder: (schemaName, schema) => {
|
|
3017
|
+
return chat().role("data processing assistant").tone("concise").jsonSchema(schemaName, schema).avoid(["Include markdown", "Add explanations outside JSON", "Include code fences"]);
|
|
3018
|
+
},
|
|
3019
|
+
/**
|
|
3020
|
+
* Summarizer preset
|
|
3021
|
+
*/
|
|
3022
|
+
summarizer: (length = "brief") => {
|
|
3023
|
+
return chat().role("expert summarizer").expertise("analysis").tone("concise").length(length).task("Summarize the provided content, preserving key information");
|
|
3024
|
+
},
|
|
3025
|
+
/**
|
|
3026
|
+
* Translator preset
|
|
3027
|
+
*/
|
|
3028
|
+
translator: (targetLanguage) => {
|
|
3029
|
+
return chat().role("professional translator").expertise("languages").responseLanguage(targetLanguage).avoid(["Add commentary", "Change meaning", "Omit content"]);
|
|
3030
|
+
}
|
|
3031
|
+
};
|
|
3032
|
+
|
|
3033
|
+
// src/builder/index.ts
|
|
3034
|
+
var PromptBuilder = class {
|
|
3035
|
+
constructor() {
|
|
3036
|
+
this._constraints = [];
|
|
3037
|
+
this._examples = [];
|
|
3038
|
+
this._variables = [];
|
|
3039
|
+
this._customSections = [];
|
|
3040
|
+
}
|
|
3041
|
+
/**
|
|
3042
|
+
* Set the role/persona for the AI
|
|
3043
|
+
*/
|
|
3044
|
+
role(role) {
|
|
3045
|
+
this._role = role;
|
|
3046
|
+
return this;
|
|
3047
|
+
}
|
|
3048
|
+
/**
|
|
3049
|
+
* Alias for role()
|
|
3050
|
+
*/
|
|
3051
|
+
persona(persona) {
|
|
3052
|
+
return this.role(persona);
|
|
3053
|
+
}
|
|
3054
|
+
/**
|
|
3055
|
+
* Set the context/background information
|
|
3056
|
+
*/
|
|
3057
|
+
context(context) {
|
|
3058
|
+
this._context = context;
|
|
3059
|
+
return this;
|
|
3060
|
+
}
|
|
3061
|
+
/**
|
|
3062
|
+
* Alias for context()
|
|
3063
|
+
*/
|
|
3064
|
+
background(background) {
|
|
3065
|
+
return this.context(background);
|
|
3066
|
+
}
|
|
3067
|
+
/**
|
|
3068
|
+
* Set the main task/instruction
|
|
3069
|
+
*/
|
|
3070
|
+
task(task) {
|
|
3071
|
+
this._task = task;
|
|
3072
|
+
return this;
|
|
3073
|
+
}
|
|
3074
|
+
/**
|
|
3075
|
+
* Alias for task()
|
|
3076
|
+
*/
|
|
3077
|
+
instruction(instruction) {
|
|
3078
|
+
return this.task(instruction);
|
|
3079
|
+
}
|
|
3080
|
+
/**
|
|
3081
|
+
* Add constraints/rules the AI should follow
|
|
3082
|
+
*/
|
|
3083
|
+
constraints(constraints) {
|
|
3084
|
+
this._constraints = [...this._constraints, ...constraints];
|
|
3085
|
+
return this;
|
|
3086
|
+
}
|
|
3087
|
+
/**
|
|
3088
|
+
* Add a single constraint
|
|
3089
|
+
*/
|
|
3090
|
+
constraint(constraint) {
|
|
3091
|
+
this._constraints.push(constraint);
|
|
3092
|
+
return this;
|
|
3093
|
+
}
|
|
3094
|
+
/**
|
|
3095
|
+
* Alias for constraints()
|
|
3096
|
+
*/
|
|
3097
|
+
rules(rules) {
|
|
3098
|
+
return this.constraints(rules);
|
|
3099
|
+
}
|
|
3100
|
+
/**
|
|
3101
|
+
* Set the expected output format
|
|
3102
|
+
*/
|
|
3103
|
+
output(format) {
|
|
3104
|
+
this._outputFormat = format;
|
|
3105
|
+
return this;
|
|
3106
|
+
}
|
|
3107
|
+
/**
|
|
3108
|
+
* Alias for output()
|
|
3109
|
+
*/
|
|
3110
|
+
format(format) {
|
|
3111
|
+
return this.output(format);
|
|
3112
|
+
}
|
|
3113
|
+
/**
|
|
3114
|
+
* Add an example input/output pair
|
|
3115
|
+
*/
|
|
3116
|
+
example(input, output) {
|
|
3117
|
+
this._examples.push({ input, output });
|
|
3118
|
+
return this;
|
|
3119
|
+
}
|
|
3120
|
+
/**
|
|
3121
|
+
* Add multiple examples
|
|
3122
|
+
*/
|
|
3123
|
+
examples(examples) {
|
|
3124
|
+
this._examples = [...this._examples, ...examples];
|
|
3125
|
+
return this;
|
|
3126
|
+
}
|
|
3127
|
+
/**
|
|
3128
|
+
* Define a variable placeholder
|
|
3129
|
+
*/
|
|
3130
|
+
variable(name, options = {}) {
|
|
3131
|
+
this._variables.push({
|
|
3132
|
+
name,
|
|
3133
|
+
description: options.description,
|
|
3134
|
+
required: options.required ?? true,
|
|
3135
|
+
defaultValue: options.defaultValue
|
|
3136
|
+
});
|
|
3137
|
+
return this;
|
|
3138
|
+
}
|
|
3139
|
+
/**
|
|
3140
|
+
* Add a custom section
|
|
3141
|
+
*/
|
|
3142
|
+
section(title, content) {
|
|
3143
|
+
this._customSections.push({ title, content });
|
|
3144
|
+
return this;
|
|
3145
|
+
}
|
|
3146
|
+
/**
|
|
3147
|
+
* Set raw content (bypasses structured building)
|
|
3148
|
+
*/
|
|
3149
|
+
raw(content) {
|
|
3150
|
+
this._rawContent = content;
|
|
3151
|
+
return this;
|
|
3152
|
+
}
|
|
3153
|
+
/**
|
|
3154
|
+
* Build the final prompt
|
|
3155
|
+
*/
|
|
3156
|
+
build() {
|
|
3157
|
+
if (this._rawContent) {
|
|
3158
|
+
return {
|
|
3159
|
+
content: this._rawContent,
|
|
3160
|
+
variables: this._variables,
|
|
3161
|
+
metadata: {}
|
|
3162
|
+
};
|
|
3163
|
+
}
|
|
3164
|
+
const sections = [];
|
|
3165
|
+
if (this._role) {
|
|
3166
|
+
sections.push(`You are ${this._role}.`);
|
|
3167
|
+
}
|
|
3168
|
+
if (this._context) {
|
|
3169
|
+
sections.push(`
|
|
3170
|
+
## Context
|
|
3171
|
+
${this._context}`);
|
|
3172
|
+
}
|
|
3173
|
+
if (this._task) {
|
|
3174
|
+
sections.push(`
|
|
3175
|
+
## Task
|
|
3176
|
+
${this._task}`);
|
|
3177
|
+
}
|
|
3178
|
+
if (this._constraints.length > 0) {
|
|
3179
|
+
const constraintsList = this._constraints.map((c, i) => `${i + 1}. ${c}`).join("\n");
|
|
3180
|
+
sections.push(`
|
|
3181
|
+
## Constraints
|
|
3182
|
+
${constraintsList}`);
|
|
3183
|
+
}
|
|
3184
|
+
if (this._outputFormat) {
|
|
3185
|
+
sections.push(`
|
|
3186
|
+
## Output Format
|
|
3187
|
+
${this._outputFormat}`);
|
|
3188
|
+
}
|
|
3189
|
+
if (this._examples.length > 0) {
|
|
3190
|
+
const examplesText = this._examples.map((e, i) => `### Example ${i + 1}
|
|
3191
|
+
**Input:** ${e.input}
|
|
3192
|
+
**Output:** ${e.output}`).join("\n\n");
|
|
3193
|
+
sections.push(`
|
|
3194
|
+
## Examples
|
|
3195
|
+
${examplesText}`);
|
|
3196
|
+
}
|
|
3197
|
+
for (const section of this._customSections) {
|
|
3198
|
+
sections.push(`
|
|
3199
|
+
## ${section.title}
|
|
3200
|
+
${section.content}`);
|
|
3201
|
+
}
|
|
3202
|
+
if (this._variables.length > 0) {
|
|
3203
|
+
const varsText = this._variables.map((v) => {
|
|
3204
|
+
const placeholder = v.defaultValue ? `\${${v.name}:${v.defaultValue}}` : `\${${v.name}}`;
|
|
3205
|
+
const desc = v.description ? ` - ${v.description}` : "";
|
|
3206
|
+
const req = v.required ? " (required)" : " (optional)";
|
|
3207
|
+
return `- ${placeholder}${desc}${req}`;
|
|
3208
|
+
}).join("\n");
|
|
3209
|
+
sections.push(`
|
|
3210
|
+
## Variables
|
|
3211
|
+
${varsText}`);
|
|
3212
|
+
}
|
|
3213
|
+
return {
|
|
3214
|
+
content: sections.join("\n").trim(),
|
|
3215
|
+
variables: this._variables,
|
|
3216
|
+
metadata: {
|
|
3217
|
+
role: this._role,
|
|
3218
|
+
context: this._context,
|
|
3219
|
+
task: this._task,
|
|
3220
|
+
constraints: this._constraints.length > 0 ? this._constraints : void 0,
|
|
3221
|
+
outputFormat: this._outputFormat,
|
|
3222
|
+
examples: this._examples.length > 0 ? this._examples : void 0
|
|
3223
|
+
}
|
|
3224
|
+
};
|
|
3225
|
+
}
|
|
3226
|
+
/**
|
|
3227
|
+
* Build and return only the content string
|
|
3228
|
+
*/
|
|
3229
|
+
toString() {
|
|
3230
|
+
return this.build().content;
|
|
3231
|
+
}
|
|
3232
|
+
};
|
|
3233
|
+
function builder() {
|
|
3234
|
+
return new PromptBuilder();
|
|
3235
|
+
}
|
|
3236
|
+
function fromPrompt(content) {
|
|
3237
|
+
return new PromptBuilder().raw(content);
|
|
3238
|
+
}
|
|
3239
|
+
var templates = {
|
|
3240
|
+
/**
|
|
3241
|
+
* Create a code review prompt
|
|
3242
|
+
*/
|
|
3243
|
+
codeReview: (options = {}) => {
|
|
3244
|
+
const b = builder().role("expert code reviewer").task("Review the provided code and identify issues, improvements, and best practices.").variable("code", { required: true, description: "The code to review" });
|
|
3245
|
+
if (options.language) {
|
|
3246
|
+
b.context(`You are reviewing ${options.language} code.`);
|
|
3247
|
+
}
|
|
3248
|
+
if (options.focus && options.focus.length > 0) {
|
|
3249
|
+
b.constraints(options.focus.map((f) => `Focus on ${f}`));
|
|
3250
|
+
}
|
|
3251
|
+
return b.output("Provide a structured review with: issues found, suggestions, and overall assessment.");
|
|
3252
|
+
},
|
|
3253
|
+
/**
|
|
3254
|
+
* Create a translation prompt
|
|
3255
|
+
*/
|
|
3256
|
+
translation: (from, to) => {
|
|
3257
|
+
return builder().role(`professional translator fluent in ${from} and ${to}`).task(`Translate the following text from ${from} to ${to}.`).constraints([
|
|
3258
|
+
"Maintain the original meaning and tone",
|
|
3259
|
+
"Use natural, idiomatic expressions in the target language",
|
|
3260
|
+
"Preserve formatting and structure"
|
|
3261
|
+
]).variable("text", { required: true, description: "Text to translate" });
|
|
3262
|
+
},
|
|
3263
|
+
/**
|
|
3264
|
+
* Create a summarization prompt
|
|
3265
|
+
*/
|
|
3266
|
+
summarize: (options = {}) => {
|
|
3267
|
+
const b = builder().role("expert summarizer").task("Summarize the following content concisely while preserving key information.").variable("content", { required: true, description: "Content to summarize" });
|
|
3268
|
+
if (options.maxLength) {
|
|
3269
|
+
b.constraint(`Keep the summary under ${options.maxLength} words`);
|
|
3270
|
+
}
|
|
3271
|
+
if (options.style === "bullet") {
|
|
3272
|
+
b.output("Provide the summary as bullet points");
|
|
3273
|
+
}
|
|
3274
|
+
return b;
|
|
3275
|
+
},
|
|
3276
|
+
/**
|
|
3277
|
+
* Create a Q&A prompt
|
|
3278
|
+
*/
|
|
3279
|
+
qa: (context) => {
|
|
3280
|
+
const b = builder().role("helpful assistant").task("Answer the question based on the provided context.").variable("question", { required: true, description: "The question to answer" });
|
|
3281
|
+
if (context) {
|
|
3282
|
+
b.context(context);
|
|
3283
|
+
} else {
|
|
3284
|
+
b.variable("context", { required: false, description: "Additional context" });
|
|
3285
|
+
}
|
|
3286
|
+
return b.constraints([
|
|
3287
|
+
"Be accurate and concise",
|
|
3288
|
+
"If you don't know the answer, say so",
|
|
3289
|
+
"Cite relevant parts of the context if applicable"
|
|
3290
|
+
]);
|
|
3291
|
+
}
|
|
3292
|
+
};
|
|
3293
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
3294
|
+
0 && (module.exports = {
|
|
3295
|
+
AudioPromptBuilder,
|
|
3296
|
+
ChatPromptBuilder,
|
|
3297
|
+
ImagePromptBuilder,
|
|
3298
|
+
PromptBuilder,
|
|
3299
|
+
VideoPromptBuilder,
|
|
3300
|
+
audio,
|
|
3301
|
+
builder,
|
|
3302
|
+
calculateSimilarity,
|
|
3303
|
+
chat,
|
|
3304
|
+
chatPresets,
|
|
3305
|
+
checkQuality,
|
|
3306
|
+
compile,
|
|
3307
|
+
convertAllVariables,
|
|
3308
|
+
convertToSupportedFormat,
|
|
3309
|
+
deduplicate,
|
|
3310
|
+
detectVariables,
|
|
3311
|
+
detectVars,
|
|
3312
|
+
extractVariables,
|
|
3313
|
+
findDuplicates,
|
|
3314
|
+
fromPrompt,
|
|
3315
|
+
getContentFingerprint,
|
|
3316
|
+
getSuggestions,
|
|
3317
|
+
getSystemPrompt,
|
|
3318
|
+
image,
|
|
3319
|
+
interpolate,
|
|
3320
|
+
isSimilarContent,
|
|
3321
|
+
isValidPrompt,
|
|
3322
|
+
normalizeContent,
|
|
3323
|
+
normalizeVariables,
|
|
3324
|
+
parsePrompt,
|
|
3325
|
+
parser,
|
|
3326
|
+
quality,
|
|
3327
|
+
similarity,
|
|
3328
|
+
templates,
|
|
3329
|
+
toJson,
|
|
3330
|
+
toYaml,
|
|
3331
|
+
validatePrompt,
|
|
3332
|
+
variables,
|
|
3333
|
+
video
|
|
3334
|
+
});
|
|
3335
|
+
//# sourceMappingURL=index.js.map
|