import { useState, useRef, useEffect, useCallback } from 'react'
const MODEL = 'claude-sonnet-4-6'
const API_URL = 'https://api.anthropic.com/v1/messages'
const LS_KEY = 'wtf_teacher_key'
// ── System prompt — rebuilt with live session context on every request ────────
function buildSystemPrompt({ keyInfo, currentChord, bpm, chordHistory }) {
const keyStr = keyInfo ? `${keyInfo.root} ${keyInfo.mode}` : 'not detected yet'
const chordStr = currentChord?.name ?? 'none detected'
const bpmStr = bpm ? `${Math.round(bpm)} BPM` : 'not detected'
const histStr = chordHistory?.length
? chordHistory.map(c => c.name).join(' → ')
: 'none yet'
return `You are an expert music teacher and session musician embedded in JamBuddy, a real-time chord and key detection app for guitarists and keyboard players at live jam sessions.
LIVE SESSION CONTEXT (updated in real time):
• Detected key: ${keyStr}
• Current chord: ${chordStr}
• BPM: ${bpmStr}
• Recent chord history: ${histStr}
YOUR ROLE:
- Explain chords, scales, and music theory in plain, friendly language
- Suggest what to practice based on the current key and chord progression
- Teach playing techniques: fretting, strumming patterns, chord voicings, fingerpicking
- Help musicians understand WHY things sound the way they do
- Suggest progressions that work with whatever the user is currently playing
- Adjust depth to the user — explain basics if they seem new, go deep if they ask for it
- Point out interesting connections: "that Dm7 works here because it's the ii chord in C major"
STYLE:
- Keep responses focused and practical — this is a live jam, not a classroom
- Use plain text, not markdown. Short paragraphs. Bullet points with "-" are fine.
- If someone asks about the current chord or key, use the live context above
- Max ~150 words unless someone asks for a deep dive`
}
// ── Quick-action chips ────────────────────────────────────────────────────────
const CHIPS = [
{ label: 'What should I practice?', msg: 'Based on what I\'m playing right now, what\'s the most useful thing I could practice?' },
{ label: 'Explain current chord', msg: 'Explain the current chord I\'m playing — what it is, why it sounds the way it does, and where it tends to appear.' },
{ label: 'Scales that work here', msg: 'What scales work over the current key and chord? Which notes sound best to improvise with?' },
{ label: 'Suggest a progression', msg: 'Suggest a chord progression that fits the current key. Give me something interesting to try.' },
{ label: 'Technique tip', msg: 'Give me one technique tip — something I can work on in the next few minutes to sound better.' },
{ label: 'Why does this sound good?', msg: 'Looking at my recent chord history, why do these chords sound good together? What\'s the music theory behind it?' },
]
// ── Simple text renderer (bold + line breaks) ─────────────────────────────────
function MessageText({ text }) {
const lines = text.split('\n')
return (
{lines.map((line, i) => {
if (!line.trim()) return
// Bold: **text**
const parts = line.split(/(\*\*[^*]+\*\*)/)
return (