-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
343 lines (309 loc) · 10.5 KB
/
Copy pathapp.js
File metadata and controls
343 lines (309 loc) · 10.5 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
(function () {
const form = document.querySelector("#launch-form");
const productName = document.querySelector("#product-name");
const audience = document.querySelector("#audience");
const tone = document.querySelector("#tone");
const generationMode = document.querySelector("#generation-mode");
const useAiEngine = document.querySelector("#use-ai-engine");
const llmSettings = document.querySelector("[data-llm-settings]");
const llmEndpoint = document.querySelector("#llm-endpoint");
const llmModel = document.querySelector("#llm-model");
const llmApiKey = document.querySelector("#llm-api-key");
const saveLlmSettings = document.querySelector("#save-llm-settings");
const llmStatus = document.querySelector("[data-llm-status]");
const testLlmButton = document.querySelector("[data-test-llm]");
const worklog = document.querySelector("#worklog");
const kitTitle = document.querySelector("#kit-title");
const kitRoot = document.querySelector("#kit");
const emptyState = document.querySelector("#empty-state");
const tabContent = document.querySelector("#tab-content");
const exportButton = document.querySelector("[data-export]");
const fillSampleButtons = document.querySelectorAll("[data-fill-sample]");
const clearButton = document.querySelector("[data-clear]");
let activeTab = "productHunt";
let currentKit = null;
const submitButton = form.querySelector('button[type="submit"]');
function generator() {
if (!window.LaunchKitGenerator) {
throw new Error("LaunchKitGenerator is not loaded.");
}
return window.LaunchKitGenerator;
}
function fillSample() {
const sample = typeof generator().sampleInput === "function"
? generator().sampleInput()
: generator().sampleInput;
productName.value = "Launchpack";
audience.value = "indie makers, devtool founders, and solo builders";
tone.value = "crisp";
worklog.value = sample;
generate().catch(showGenerationError);
document.querySelector("#builder").scrollIntoView({ behavior: "smooth" });
}
function clearForm() {
form.reset();
syncAiToggle();
currentKit = null;
activeTab = "productHunt";
kitRoot.hidden = true;
emptyState.hidden = false;
exportButton.disabled = true;
kitTitle.textContent = "Your launch kit will appear here";
tabContent.innerHTML = "";
setStatus("");
}
async function generate() {
const input = worklog.value.trim();
if (!input) {
worklog.focus();
return;
}
setBusy(true);
setStatus("Generating launch kit...");
const localKit = generator().generateLocalKit(input, {
productName: productName.value.trim(),
audience: audience.value.trim(),
tone: tone.value,
});
currentKit = localKit;
if (generationMode.value === "llm") {
try {
saveSettings();
setStatus("Asking your configured LLM...");
currentKit = await window.LaunchKitAIClient.generateWithLLM({
input,
localKit,
settings: getLlmSettings(),
});
setStatus("LLM kit generated. Review receipts before launch.");
} catch (error) {
currentKit = localKit;
setStatus(`LLM unavailable, local fallback generated. ${error.message}`);
}
} else {
saveSettings();
setStatus("Local deterministic kit generated.");
}
kitTitle.textContent = `${currentKit.strategy.productName} launch kit`;
emptyState.hidden = true;
kitRoot.hidden = false;
exportButton.disabled = false;
setActiveTab(activeTab);
setBusy(false);
}
function setActiveTab(tab) {
activeTab = tab;
document.querySelectorAll(".tab").forEach((button) => {
button.classList.toggle("active", button.dataset.tab === tab);
});
renderTab();
}
function row(label, value) {
return `
<article class="copy-row">
<div>
<strong>${escapeHtml(label)}</strong>
<p>${escapeHtml(value)}</p>
</div>
<button class="mini-copy" type="button" data-copy="${encodeURIComponent(value)}">Copy</button>
</article>
`;
}
function preRow(label, value) {
return `
<article class="copy-row">
<div>
<strong>${escapeHtml(label)}</strong>
<pre>${escapeHtml(value)}</pre>
</div>
<button class="mini-copy" type="button" data-copy="${encodeURIComponent(value)}">Copy</button>
</article>
`;
}
function renderTab() {
if (!currentKit) return;
const kit = currentKit;
const renderers = {
productHunt: () => `
<div class="kit-card">
${row("Tagline", kit.productHunt.tagline)}
${row("Short description", kit.productHunt.shortDescription)}
${preRow("Maker comment", kit.productHunt.makerComment)}
${preRow("Demo script", kit.demoScript)}
</div>
`,
social: () => `
<div class="kit-card">
${preRow("X / Twitter post", kit.social.xPost)}
${preRow("LinkedIn post", kit.social.linkedInPost)}
${preRow("Launch email", kit.social.email)}
</div>
`,
faq: () => `
<ul class="faq-list">
${kit.faq.map((item) => `
<li>
<span>${escapeHtml(item.question)}</span>
${escapeHtml(item.answer)}
</li>
`).join("")}
</ul>
`,
checklist: () => `
<ul class="check-list">
${kit.checklist.map((item, index) => `
<li>
<span>Step ${index + 1}</span>
${escapeHtml(item)}
</li>
`).join("")}
</ul>
`,
receipts: () => `
<ul class="receipt-list">
${kit.receipts.map((item) => `
<li>
<span>${escapeHtml(item.claim)}</span>
${escapeHtml(item.source)}
</li>
`).join("")}
</ul>
`,
aiPrompt: () => `
<div class="kit-card">
${preRow("LLM polish prompt", kit.aiPrompt)}
</div>
`,
};
tabContent.innerHTML = renderers[activeTab]();
}
function exportMarkdown() {
if (!currentKit) return;
const markdown = generator().generateMarkdown(currentKit);
const blob = new Blob([markdown], { type: "text/markdown;charset=utf-8" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `${slugify(currentKit.strategy.productName)}-launch-kit.md`;
link.click();
URL.revokeObjectURL(url);
}
async function testLlmConnection() {
setStatus("Testing LLM endpoint...");
testLlmButton.disabled = true;
try {
saveSettings();
const models = await window.LaunchKitAIClient.testConnection(getLlmSettings());
const suffix = models.length ? ` Models: ${models.join(", ")}` : "";
setStatus(`Connection OK.${suffix}`);
} catch (error) {
setStatus(`Connection failed. ${error.message}`);
} finally {
testLlmButton.disabled = false;
}
}
function getLlmSettings() {
return {
endpoint: llmEndpoint.value.trim(),
model: llmModel.value.trim(),
apiKey: llmApiKey.value.trim(),
};
}
function saveSettings() {
if (!saveLlmSettings.checked) {
localStorage.removeItem("launchpack.llmSettings");
return;
}
localStorage.setItem("launchpack.llmSettings", JSON.stringify({
generationMode: generationMode.value,
useAiEngine: useAiEngine.checked,
endpoint: llmEndpoint.value.trim(),
model: llmModel.value.trim(),
save: true,
}));
}
function loadSettings() {
const stored = localStorage.getItem("launchpack.llmSettings");
if (!stored) {
llmEndpoint.value = "http://localhost:1234/v1";
llmModel.value = "local-model";
syncAiToggle();
return;
}
try {
const settings = JSON.parse(stored);
generationMode.value = settings.generationMode || "local";
useAiEngine.checked = settings.useAiEngine || generationMode.value === "llm";
llmEndpoint.value = settings.endpoint || "http://localhost:1234/v1";
llmModel.value = settings.model || "local-model";
saveLlmSettings.checked = settings.save !== false;
} catch (error) {
llmEndpoint.value = "http://localhost:1234/v1";
llmModel.value = "local-model";
}
syncAiToggle();
}
function syncAiToggle() {
generationMode.value = useAiEngine.checked ? "llm" : "local";
llmSettings.hidden = !useAiEngine.checked;
testLlmButton.disabled = !useAiEngine.checked;
setStatus(useAiEngine.checked
? "AI Engine enabled. Add an endpoint, then test the connection."
: "AI Engine off. Launchpack will use local deterministic generation.");
saveSettings();
}
function setBusy(isBusy) {
submitButton.disabled = isBusy;
submitButton.textContent = isBusy ? "Generating..." : "Generate launch kit";
}
function setStatus(message) {
llmStatus.textContent = message;
}
function showGenerationError(error) {
setBusy(false);
setStatus(error.message);
}
async function copyValue(value, button) {
await navigator.clipboard.writeText(value);
const original = button.textContent;
button.textContent = "Copied";
window.setTimeout(() => {
button.textContent = original;
}, 1200);
}
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function slugify(value) {
return String(value)
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-$)/g, "") || "launch-kit";
}
form.addEventListener("submit", (event) => {
event.preventDefault();
generate().catch(showGenerationError);
});
document.querySelector(".tabs").addEventListener("click", (event) => {
const button = event.target.closest("[data-tab]");
if (button) setActiveTab(button.dataset.tab);
});
tabContent.addEventListener("click", (event) => {
const button = event.target.closest("[data-copy]");
if (!button) return;
copyValue(decodeURIComponent(button.dataset.copy), button).catch(() => {
button.textContent = "Copy failed";
});
});
exportButton.addEventListener("click", exportMarkdown);
useAiEngine.addEventListener("change", syncAiToggle);
testLlmButton.addEventListener("click", testLlmConnection);
clearButton.addEventListener("click", clearForm);
fillSampleButtons.forEach((button) => button.addEventListener("click", fillSample));
loadSettings();
})();