File size: 4,570 Bytes
ef987bb | 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 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Prompt Response API Solution v2</title>
<style>
body { font-family: Arial, sans-serif; padding: 24px; }
textarea { width: min(520px, 100%); height: 80px; display: block; box-sizing: border-box; }
output { display: block; margin-top: 12px; min-height: 48px; white-space: pre-wrap; }
.error { color: #b91c1c; }
</style>
<script src="config.js"></script>
</head>
<body>
<main>
<label>Prompt<textarea data-testid="prompt-input"></textarea></label>
<button data-testid="send-button">Generate</button>
<h1 data-testid="generated-title"></h1>
<output data-testid="generated-content"></output>
<p data-testid="generation-status">idle</p>
</main>
<script>
let mockMode = 'normal';
let mockChunks = null;
let requestVersion = 0;
window.__setMockMode = mode => { mockMode = mode; };
window.__setMockChunks = chunks => { mockChunks = chunks === null ? null : [...chunks]; };
function makeTitle(prompt) {
const words = prompt.split(/\s+/).filter(Boolean).slice(0, 5).join(' ');
return `Plan for ${words}`;
}
function parseChunk(chunk) {
const text = String(chunk);
const match = text.match(/^\s*\d+:(.*)$/s);
if (!match) return text;
const parsed = JSON.parse(match[1]);
if (typeof parsed !== 'string') throw new Error('invalid text frame');
return parsed;
}
async function* streamChunks(prompt, config) {
if (!config || ['key', 'baseurl', 'modelname'].some(name =>
typeof config[name] !== 'string' || !config[name].trim())) {
throw new Error('missing API configuration');
}
if (mockMode === 'timeout') throw new Error('request timeout');
if (mockChunks !== null) {
yield* mockChunks;
return;
}
if (mockMode === 'malformed') {
yield '0:"Hello"';
yield '0:"world"';
return;
}
const topic = prompt.split(/[::;;.!?\n]/u, 1)[0].trim() || 'the requested work';
if (/\brisk register|mitigat/i.test(prompt)) {
yield `List hazards, affected components and a mitigation owner for ${topic}. `;
yield 'Rate each hazard by likelihood and severity, then document how to verify the mitigation.';
return;
}
if (/\b(?:restor\w*|repair\w*|renovat\w*)\b/i.test(topic)) {
yield `Document the current condition of ${topic} and identify what can be preserved. `;
yield 'Stabilize the most fragile components first. Test repairs on a small, reversible area before expanding.';
} else if (/\b(?:inspect\w*|audit\w*)\b/i.test(topic)) {
yield `Identify the critical components and safe access points for ${topic}. `;
yield 'Record the evidence for each finding, then prioritize defects by severity and urgency.';
} else {
yield `Set a concrete deliverable for ${topic} and list the inputs needed to produce it. `;
yield 'Complete a small first pass, check it against the deliverable, then handle the remaining cases.';
}
}
async function generate() {
const version = ++requestVersion;
const prompt = document.querySelector('[data-testid="prompt-input"]').value;
const status = document.querySelector('[data-testid="generation-status"]');
const title = document.querySelector('[data-testid="generated-title"]');
const content = document.querySelector('[data-testid="generated-content"]');
status.className = '';
status.textContent = 'loading stream';
title.textContent = 'Loading title...';
content.textContent = 'Loading content...';
try {
const config = {...window.aiConfig};
await new Promise(resolve => setTimeout(resolve, 20));
if (version !== requestVersion) return;
title.textContent = makeTitle(prompt);
content.textContent = '';
for await (const chunk of streamChunks(prompt, config)) {
if (version !== requestVersion) return;
content.textContent += parseChunk(chunk);
await new Promise(resolve => setTimeout(resolve, 60));
}
if (version !== requestVersion) return;
status.textContent = 'complete';
} catch (err) {
if (version !== requestVersion) return;
status.className = 'error';
status.textContent = `failed: ${err.message || err}`;
}
}
document.querySelector('[data-testid="send-button"]').addEventListener('click', generate);
</script>
</body>
</html>
|