v148 · AI · JavaScript
Safety Harness
14 edge-case tests for the Prompt API: API availability, model readiness, context overflow, prompt injection, empty input, very long input, structured output validation, and recovery paths. Run all or individually.
Checking Prompt API…
14Total
0Pass
0Fail
0Warn
Ready. Click "Run all" or run tests individually.
Test suite
// Production-safe Prompt API pattern
async function safePrompt(userInput, systemPrompt) {
// 1. Feature-detect
if (!window.ai?.languageModel) return { error: 'not-supported' };
// 2. Check model availability
const cap = await window.ai.languageModel.capabilities();
if (cap.available === 'no') return { error: 'model-unavailable' };
// 3. Guard empty / malicious input
const sanitised = userInput.trim().slice(0, 8000);
if (!sanitised) return { error: 'empty-input' };
// 4. Limit context, stream, catch errors
try {
const session = await window.ai.languageModel.create({ systemPrompt });
const stream = await session.promptStreaming(sanitised);
let out = '';
for await (const chunk of stream) out = chunk;
return { result: out };
} catch (e) {
return { error: 'inference-error', message: e.message };
}
}
see also
- Prompt Playground — interactive prompting
- Local-First Workflow — real app flows
- Quality Control Panel — performance benchmarks
- Structured Output Studio — JSON constraints
implementation reference
Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗