feat(ai): add Tier 1 & 2 Auto-Deconstruct contract API logic

This commit is contained in:
djg 2026-09-23 17:07:42 +00:00
parent 9b8f00a985
commit b9bd6ae5e6

View file

@ -432,6 +432,46 @@
timerPersist(); timerPaint();
}
/* ---------- AI Auto-Deconstruct Contract (Tier 1 & 2) ---------- */
window.aiBreakdownTask = async function(taskText) {
// Shared Interface for Sibyl's Tier 3
const prompt = `Break down this intimidating task into 3-5 tiny, actionable micro-steps: "${taskText}". Return only the list of steps.`;
// Tier 1: Browser Native (window.ai)
if (window.ai && window.ai.languageModel) {
try {
const caps = await window.ai.languageModel.capabilities();
if (caps.available !== 'no') {
const session = await window.ai.languageModel.create();
const result = await session.prompt(prompt);
return result.split('\n').filter(s => s.trim().length > 0);
}
} catch (e) {
console.warn("window.ai failed, falling back", e);
}
}
// Tier 2: BYOK (Bring Your Own Key) Fallback
const key = store('byok_key');
if (!key) {
throw new Error("NO_AI_AVAILABLE"); // Let UI prompt for key or let Tier 3 try
}
const resp = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + key
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: prompt }]
})
});
if (!resp.ok) throw new Error("API_ERROR");
const data = await resp.json();
return data.choices[0].message.content.split('\n').filter(s => s.trim().length > 0);
};
$('#timer-reset').addEventListener('click', timerReset);
$$('.preset').forEach(function (p) {