-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompts.js
More file actions
164 lines (156 loc) · 4.48 KB
/
prompts.js
File metadata and controls
164 lines (156 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const {
STORYBOARD_PROMPT_PREFIX_LINES,
STORYBOARD_RULE_LINES
} = require('./data/prompt-templates');
function buildStoryboardPrompt({
sourceTitle,
sourceLabel,
sourceText,
panelCount,
objective,
stylePrompt,
outputLanguage,
objectiveDescription,
objectivePromptOverride,
customStoryPrompt
}) {
const objectiveDesc = String(objectiveDescription || '').trim();
const out = [
...STORYBOARD_PROMPT_PREFIX_LINES,
`Panel count: ${panelCount}`,
`Objective: ${objective || 'summarize'}`,
`Output language: ${outputLanguage || 'en'}`,
`Visual style: ${stylePrompt}`,
'Rules:',
...STORYBOARD_RULE_LINES
];
if (objectiveDesc) {
out.splice(3, 0, `Objective description: ${objectiveDesc}`);
}
const objectiveOverride = String(objectivePromptOverride || '').trim();
if (objectiveOverride) {
out.push(`Objective-specific instructions: ${objectiveOverride}`);
}
const customStory = String(customStoryPrompt || '').trim();
if (customStory) {
out.push(`Custom user story prompt: ${customStory}`);
}
out.push(
`Source title: ${sourceTitle}`,
`Source label: ${sourceLabel}`,
'Source text:',
sourceText
);
return out.join('\n');
}
function extractJsonCandidate(rawText) {
const raw = String(rawText || '');
const fenceMatch = raw.match(/```(?:json)?\s*([\s\S]*?)```/i);
const source = fenceMatch && fenceMatch[1] ? fenceMatch[1] : raw;
const start = source.indexOf('{');
if (start < 0) return '';
let depth = 0;
let inString = false;
let escape = false;
for (let i = start; i < source.length; i += 1) {
const ch = source[i];
if (inString) {
if (escape) escape = false;
else if (ch === '\\') escape = true;
else if (ch === '"') inString = false;
continue;
}
if (ch === '"') { inString = true; continue; }
if (ch === '{') depth += 1;
if (ch === '}') {
depth -= 1;
if (depth === 0) return source.slice(start, i + 1);
}
}
return '';
}
function stripTrailingCommas(jsonText) {
const src = String(jsonText || '');
if (!src) return src;
let out = '';
let inString = false;
let escape = false;
for (let i = 0; i < src.length; i += 1) {
const ch = src[i];
if (inString) {
out += ch;
if (escape) escape = false;
else if (ch === '\\') escape = true;
else if (ch === '"') inString = false;
continue;
}
if (ch === '"') {
inString = true;
out += ch;
continue;
}
if (ch === ',') {
let j = i + 1;
while (j < src.length && /\s/.test(src[j])) j += 1;
if (j < src.length && (src[j] === '}' || src[j] === ']')) {
continue;
}
}
out += ch;
}
return out;
}
function sanitizeJsonCandidate(candidate) {
const trimmed = String(candidate || '').replace(/^\uFEFF/, '').trim();
if (!trimmed) return '';
return stripTrailingCommas(trimmed);
}
function normalizeStoryboard(storyboard, panelCount) {
const safeCount = Math.max(1, Number(panelCount || 3));
const parsed = storyboard && typeof storyboard === 'object' ? storyboard : {};
const panelsRaw = Array.isArray(parsed.panels) ? parsed.panels : [];
const outPanels = [];
for (let i = 0; i < safeCount; i += 1) {
const p = panelsRaw[i] || {};
const caption = String(p.caption || p.title || `Panel ${i + 1}`).trim();
const imagePrompt = String(p.image_prompt || p.prompt || caption).trim();
outPanels.push({
panel_id: `panel_${i + 1}`,
caption,
image_prompt: imagePrompt
});
}
return {
title: String(parsed.title || 'Comic Summary').trim() || 'Comic Summary',
description: String(parsed.description || '').trim(),
panels: outPanels
};
}
function parseStoryboardResponse(rawText, panelCount) {
const candidate = extractJsonCandidate(rawText);
if (!candidate) throw new Error('No JSON object found in storyboard response');
const attempts = [candidate, sanitizeJsonCandidate(candidate)];
for (const attempt of attempts) {
if (!attempt) continue;
try {
const parsed = JSON.parse(attempt);
return normalizeStoryboard(parsed, panelCount);
} catch (_) {
// Try next parsing strategy.
}
}
try {
JSON.parse(candidate);
} catch (error) {
throw new Error(`Failed to parse storyboard JSON: ${error.message}`);
}
throw new Error('Failed to parse storyboard JSON');
}
module.exports = {
buildStoryboardPrompt,
extractJsonCandidate,
sanitizeJsonCandidate,
stripTrailingCommas,
parseStoryboardResponse,
normalizeStoryboard
};