-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
377 lines (343 loc) · 11.4 KB
/
app.js
File metadata and controls
377 lines (343 loc) · 11.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// PromptMatrix core logic
// All logic runs in-browser, no backend required.
const styles = {
academic: {
id: "academic",
label: "学术风格 · Academic",
system: `You are an expert academic writer and research assistant. You strictly follow academic conventions, provide citations when possible, and prioritise clarity, structure, and reproducibility.`,
constraints: [
"Use precise, formal language with minimal rhetorical fluff.",
"Prefer numbered lists and clear section headings.",
"State assumptions explicitly and separate opinion from evidence.",
"Where applicable, briefly discuss limitations and future work.",
],
outputFormat: [
"Use Markdown headings for major sections.",
"Suggested structure: Background → Objective → Method → Analysis → Conclusion.",
"When including code or formulas, place them in fenced code blocks.",
],
},
marketing: {
id: "marketing",
label: "营销风格 · Marketing",
system: `You are a senior brand & growth copy strategist. You craft messaging that is crisp, differentiated, and conversion‑oriented without sounding spammy.`,
constraints: [
"Focus on benefits and outcomes, not just features.",
"Avoid over‑promising or making unverifiable claims.",
"Align tone with target audience maturity and channel (e.g. Twitter vs. landing page).",
],
outputFormat: [
"Start with a punchy one‑sentence hook.",
"Follow with 3‑5 bullet points highlighting key value props.",
"Optionally include a short CTA section.",
],
},
minimal: {
id: "minimal",
label: "极简风格 · Minimal",
system: `You are a minimalist prompt designer. You optimise for composability, reusability, and clarity with as little text as possible.`,
constraints: [
"Remove redundant words and clauses.",
"Prefer parameters and placeholders over hard‑coded details.",
"Avoid style instructions unless strictly required.",
],
outputFormat: [
"Use a compact structure with clearly named sections.",
"Prefer bullet points and short lines over long paragraphs.",
],
},
roleplay: {
id: "roleplay",
label: "角色扮演风格 · Role Play",
system: `You are a simulation engine for role‑play scenarios. You consistently stay in character, maintain internal coherence, and respect safety constraints.`,
constraints: [
"Describe internal reasoning only when explicitly requested.",
"Avoid breaking the fourth wall or referencing being an AI unless asked.",
"Respect content and safety guidelines at all times.",
],
outputFormat: [
"Start by restating the character, context, and boundaries.",
"Use dialogue and short narrative descriptions where helpful.",
"Optionally define a turn‑based structure if the scenario is interactive.",
],
},
analytic: {
id: "analytic",
label: "分析型风格 · Analytic",
system: `You are a senior analyst. You decompose problems, examine trade‑offs, and surface non‑obvious risks and opportunities.`,
constraints: [
"Explicitly list assumptions and unknowns.",
"Use step‑by‑step reasoning and intermediate summaries.",
"Highlight edge cases and failure modes where relevant.",
],
outputFormat: [
"Use Markdown sections: Problem framing → Assumptions → Analysis → Options → Recommendation.",
"When helpful, use tables to compare options.",
],
},
creative: {
id: "creative",
label: "创意写作风格 · Creative",
system: `You are an imaginative storyteller and world‑builder. You prioritise voice, atmosphere, and emotional resonance while remaining coherent.`,
constraints: [
"Avoid clichés where possible; prefer fresh metaphors and images.",
"Maintain consistency of tone, tense, and point of view.",
"Respect content and safety constraints while remaining vivid.",
],
outputFormat: [
"Optionally suggest multiple directions or variations.",
"Use paragraphing and pacing to control rhythm and tension.",
],
},
};
const $ = (selector) => document.querySelector(selector);
const elements = {
styleCards: () => document.querySelectorAll("[data-style-id]"),
themeChips: () => document.querySelectorAll("[data-theme-target]"),
taskSummary: () => $("#taskSummary"),
tone: () => $("#tone"),
audience: () => $("#audience"),
system: () => $("#systemPrompt"),
user: () => $("#userPrompt"),
examples: () => $("#examples"),
constraints: () => $("#constraints"),
outputFormat: () => $("#outputFormat"),
preview: () => $("#previewContent"),
currentStyleLabel: () => $("#currentStyleLabel"),
lengthIndicator: () => $("#lengthIndicator"),
btnCopyPlain: () => $("#btnCopyPlain"),
btnExportMd: () => $("#btnExportMd"),
};
let currentStyleId = "academic";
function applyTheme(theme) {
if (theme === "light") {
// Light theme is no longer exposed; fall back to dark
theme = "dark";
}
const root = document.documentElement;
root.setAttribute("data-theme", theme);
document.body.setAttribute("data-theme", theme);
elements.themeChips().forEach((chip) => {
const target = chip.getAttribute("data-theme-target");
chip.classList.toggle("is-active", target === theme);
});
try {
localStorage.setItem("promptmatrix-theme", theme);
} catch {
// ignore
}
}
function loadInitialTheme() {
let initial = "dark";
try {
const stored = localStorage.getItem("promptmatrix-theme");
if (stored) initial = stored;
else if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
initial = "dark";
}
} catch {
// ignore
}
applyTheme(initial);
}
function computePromptSections() {
const style = styles[currentStyleId];
const summary = (elements.taskSummary().value || "").trim();
const tone = (elements.tone().value || "").trim();
const audience = (elements.audience().value || "").trim();
const systemOverride = (elements.system().value || "").trim();
const userOverride = (elements.user().value || "").trim();
const examples = (elements.examples().value || "").trim();
const constraintsOverride = (elements.constraints().value || "").trim();
const outputOverride = (elements.outputFormat().value || "").trim();
const sections = [];
// System
const systemParts = [];
systemParts.push(systemOverride || style.system);
if (tone) {
systemParts.push(`Tone: ${tone}.`);
}
if (audience) {
systemParts.push(`Primary audience: ${audience}.`);
}
sections.push({
heading: "System",
body: systemParts.join("\n\n"),
});
// User
const userLines = [];
if (summary) {
userLines.push(
`Your task: ${summary}${
summary.endsWith(".") || summary.endsWith("。") ? "" : "."
}`
);
} else if (!userOverride) {
userLines.push(
"Your task: [简要描述你希望模型完成的具体任务,例如生成某类内容、完成某项分析、改写一段文本等]。"
);
}
if (userOverride) {
userLines.push("", userOverride);
}
sections.push({
heading: "User",
body: userLines.join("\n"),
});
// Examples
if (examples) {
sections.push({
heading: "Examples",
body: examples,
});
}
// Constraints
const constraintsLines = [];
if (constraintsOverride) {
constraintsLines.push(constraintsOverride);
} else if (style.constraints?.length) {
constraintsLines.push(
...style.constraints.map((c, idx) => `${idx + 1}. ${c}`)
);
}
if (constraintsLines.length) {
sections.push({
heading: "Constraints",
body: constraintsLines.join("\n"),
});
}
// Output format
const outputLines = [];
if (outputOverride) {
outputLines.push(outputOverride);
} else if (style.outputFormat?.length) {
outputLines.push(
...style.outputFormat.map((c, idx) => `${idx + 1}. ${c}`)
);
}
if (outputLines.length) {
sections.push({
heading: "Output format",
body: outputLines.join("\n"),
});
}
return sections;
}
function buildPlainPrompt() {
const sections = computePromptSections();
const lines = [];
sections.forEach((section) => {
lines.push(`### ${section.heading}`, "", section.body.trim(), "");
});
return lines.join("\n").trim() + "\n";
}
function buildMarkdownPrompt() {
const style = styles[currentStyleId];
const plain = buildPlainPrompt();
const meta = [
`# Prompt · ${style.label}`,
"",
"> Generated with **PromptMatrix** · tweak the parameters and reuse this prompt across models.",
"",
];
return meta.join("\n") + plain;
}
function updatePreview() {
const style = styles[currentStyleId];
const plain = buildPlainPrompt();
const previewEl = elements.preview();
if (!previewEl) return;
previewEl.textContent = plain;
const labelEl = elements.currentStyleLabel();
if (labelEl) {
labelEl.textContent = `当前风格:${style.label}`;
}
const lenEl = elements.lengthIndicator();
if (lenEl) {
lenEl.textContent = `字数:${plain.length}`;
}
}
async function copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
return false;
}
}
function attachEvents() {
// Style cards
elements.styleCards().forEach((card) => {
card.addEventListener("click", () => {
const id = card.getAttribute("data-style-id");
if (!id || !styles[id]) return;
if (id === currentStyleId) return;
currentStyleId = id;
elements.styleCards().forEach((c) =>
c.classList.toggle("is-active", c === card)
);
updatePreview();
});
});
// Theme chips
elements.themeChips().forEach((chip) => {
chip.addEventListener("click", () => {
const theme = chip.getAttribute("data-theme-target");
if (!theme) return;
applyTheme(theme);
});
});
// Inputs to live‑update preview
[
elements.taskSummary(),
elements.tone(),
elements.audience(),
elements.system(),
elements.user(),
elements.examples(),
elements.constraints(),
elements.outputFormat(),
].forEach((el) => {
if (!el) return;
const eventName = el.tagName === "TEXTAREA" ? "input" : "input";
el.addEventListener(eventName, () => updatePreview());
});
// Copy plain prompt
const btnCopy = elements.btnCopyPlain();
if (btnCopy) {
btnCopy.addEventListener("click", async () => {
const text = buildPlainPrompt();
const ok = await copyToClipboard(text);
btnCopy.textContent = ok ? "已复制 ✓" : "复制失败";
setTimeout(() => {
btnCopy.textContent = "复制 Prompt";
}, 1800);
});
}
// Export markdown
const btnExport = elements.btnExportMd();
if (btnExport) {
btnExport.addEventListener("click", () => {
const content = buildMarkdownPrompt();
const blob = new Blob([content], { type: "text/markdown;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
const style = styles[currentStyleId];
a.href = url;
a.download = `prompt-${style.id}.md`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
btnExport.textContent = "已导出 ✓";
setTimeout(() => {
btnExport.textContent = "导出为 Markdown";
}, 1800);
});
}
}
document.addEventListener("DOMContentLoaded", () => {
loadInitialTheme();
attachEvents();
updatePreview();
});