import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import {
AlertTriangle,
ChevronDown,
Download,
MessageSquarePlus,
Share2,
RefreshCw,
Search,
Send,
Settings,
Square,
Trash2,
} from 'lucide-react'
const STORAGE_PERSONA = 'askjerry_extra_persona'
const CONTEXT_WINDOW = 8192
const MAX_REPLY_TOKENS = 4096
const SUMMARIZE_THRESHOLD = 0.55
const DEFAULT_EXTRA_PERSONA = `You are Tawish Jerry Huaute, a Chumash man who spent 14+ years at Microsoft as a Senior Field Technical Account Manager starting in 1994. You grew up in Pomona, California. Your grandfather Semu Huaute (1908–2004) was a legendary Chumash medicine man, activist, and founder of the Red Wind Foundation who participated in the 1969 Alcatraz occupation. You are a member of Native Americans at Microsoft, mentoring Indigenous youth in tech careers.
Your core beliefs: "Don't let an opportunity pass you by. You need to invest in yourself." Keep learning, seize chances, and build your value through education and hands-on experience. You are proud of your Chumash heritage and see it as inseparable from your professional identity — not a contradiction.
Your tone is direct, warm, unpretentious, and encouraging. You speak from lived experience, not theory. You naturally mentor younger people, especially Native youth who may not see representation in tech. You know enterprise IT deeply — deployment, troubleshooting, account management, customer relationships, Microsoft technologies. You don't lead with credentials; you lead with real talk.
When discussing heritage, speak with quiet pride about your family and Chumash culture. When giving career advice, be practical and honest — you believe in people but expect them to put in the work.`
function initialExtraPersona() {
try {
const s = sessionStorage.getItem(STORAGE_PERSONA)
if (s !== null) return s
} catch { /* */ }
return DEFAULT_EXTRA_PERSONA
}
function estimateTokens(text) {
return Math.max(1, Math.ceil((text || '').length / 4))
}
function estimateMessagesTokens(msgs, systemPrompt, summary) {
let total = estimateTokens(systemPrompt) + 4
if (summary) total += estimateTokens(summary) + 8
for (const m of msgs) total += estimateTokens(m.content) + 4
return total
}
function parseSseBuffer(buf, onEvent) {
const normalized = buf.replace(/\r\n/g, '\n')
const idx = normalized.lastIndexOf('\n')
if (idx === -1) return normalized
const complete = normalized.slice(0, idx + 1)
const remainder = normalized.slice(idx + 1)
for (const line of complete.split('\n')) {
if (!line.startsWith('data: ')) continue
const raw = line.slice(6).trim()
if (raw === '[DONE]') { onEvent({ type: 'done' }); continue }
if (!raw) continue
try { onEvent(JSON.parse(raw)) } catch { /* */ }
}
return remainder
}
function appendTokenContent(acc, ev) {
if (ev.type !== 'token' || ev.content == null) return acc
let piece = ev.content
if (Array.isArray(piece)) {
piece = piece.map((p) => (typeof p === 'string' ? p : p?.text ?? '')).join('')
} else if (typeof piece !== 'string') {
piece = String(piece)
}
return piece ? acc + piece : acc
}
function AssistantSearchBar({ content, show }) {
const [open, setOpen] = useState(false)
const [draft, setDraft] = useState('')
const [loading, setLoading] = useState(false)
const [copied, setCopied] = useState(false)
const [placement, setPlacement] = useState('below')
const [popoverStyle, setPopoverStyle] = useState(null)
const wrapRef = useRef(null)
const measurePopoverPosition = useCallback(() => {
if (!open) {
setPopoverStyle(null)
return
}
const bubble = wrapRef.current?.closest('.aj-bubble-wrap')
if (!bubble) return
const br = bubble.getBoundingClientRect()
const vw = window.visualViewport?.width ?? window.innerWidth
const margin = 12
const gap = 8
const popoverW = Math.min(360, vw - margin * 2)
const spaceRight = vw - br.right - margin
const canPlaceRight = spaceRight >= popoverW + gap
const vh = window.visualViewport?.height ?? window.innerHeight
let style
if (canPlaceRight) {
setPlacement('right')
const w = Math.min(popoverW, vw - br.right - gap - margin)
style = { position: 'fixed', visibility: 'visible', bottom: vh - br.bottom, left: br.right + gap, width: w }
} else {
setPlacement('below')
const w = Math.min(br.width, popoverW, vw - 2 * margin)
const left = Math.max(margin, Math.min(br.left, vw - w - margin))
style = { position: 'fixed', visibility: 'visible', top: br.bottom + gap, left, width: w }
}
setPopoverStyle(style)
}, [open])
useLayoutEffect(() => {
measurePopoverPosition()
}, [open, loading, draft, measurePopoverPosition])
useEffect(() => {
if (!open) return
const onMove = () => measurePopoverPosition()
window.addEventListener('resize', onMove)
window.addEventListener('scroll', onMove, true)
return () => {
window.removeEventListener('resize', onMove)
window.removeEventListener('scroll', onMove, true)
}
}, [open, measurePopoverPosition])
useEffect(() => {
if (!open) return
const onDoc = (e) => {
if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false)
}
document.addEventListener('mousedown', onDoc)
return () => document.removeEventListener('mousedown', onDoc)
}, [open])
const openPanel = async () => {
if (open) {
setOpen(false)
return
}
setOpen(true)
setCopied(false)
setLoading(true)
setDraft('')
const text = (content || '').toString().slice(0, 4000)
try {
const res = await fetch('/api/search-references', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ statement: text }),
})
const data = await res.json().catch(() => ({}))
const q = (data.search_query || '').trim() || text.slice(0, 100)
setDraft(`Provide citations for ${q}`)
} catch {
setDraft(`Provide citations for ${text.slice(0, 100)}`)
} finally {
setLoading(false)
}
}
const copyDraft = () => {
navigator.clipboard.writeText(draft).then(() => {
setCopied(true)
setTimeout(() => setCopied(false), 2000)
}).catch(() => {})
}
const openPerplexity = () => {
window.open(`https://www.perplexity.ai/?q=${encodeURIComponent(draft)}`, '_blank', 'noopener,noreferrer')
}
const openWebSearch = () => {
window.open(`https://www.google.com/search?q=${encodeURIComponent(draft)}`, '_blank', 'noopener,noreferrer')
}
if (!show || !(content || '').trim()) return null
return (
{open && (
Web search for citations and resources
{loading ? (
Generating search query…
) : (
<>
)}
)
}
function AssistantBubbleContent({ content }) {
return (
)
}
export default function App() {
const [optionsOpen, setOptionsOpen] = useState(false)
const [shareOpen, setShareOpen] = useState(false)
const [exportOpen, setExportOpen] = useState(false)
const [extraPersona, setExtraPersona] = useState(initialExtraPersona)
const [messages, setMessages] = useState([])
const [input, setInput] = useState('')
const [streaming, setStreaming] = useState(false)
const [summary, setSummary] = useState(null)
const [summarizing, setSummarizing] = useState(false)
const [contextOverflow, setContextOverflow] = useState(null)
const [hideTypingCursor, setHideTypingCursor] = useState(false)
const [showJumpToBottom, setShowJumpToBottom] = useState(false)
const abortRef = useRef(null)
const streamingAssistantRowRef = useRef(null)
const didScrollForStreamRef = useRef(false)
const chatScrollRef = useRef(null)
const optionsRef = useRef(null)
const shareRef = useRef(null)
const exportRef = useRef(null)
const composerRef = useRef(null)
const lastUserRowRef = useRef(null)
useEffect(() => {
const el = document.documentElement
if (hideTypingCursor) el.classList.add('aj-hide-pointer')
else el.classList.remove('aj-hide-pointer')
return () => {
el.classList.remove('aj-hide-pointer')
}
}, [hideTypingCursor])
useEffect(() => {
if (!hideTypingCursor) return
const show = () => setHideTypingCursor(false)
window.addEventListener('mousemove', show, { passive: true })
return () => window.removeEventListener('mousemove', show)
}, [hideTypingCursor])
const updateJumpToBottomVisibility = useCallback(() => {
const el = chatScrollRef.current
if (!el) return
const { scrollHeight, clientHeight, scrollTop } = el
const canScroll = scrollHeight > clientHeight + 2
const atBottom = scrollTop + clientHeight >= scrollHeight - 48
setShowJumpToBottom(canScroll && !atBottom)
}, [])
useLayoutEffect(() => {
updateJumpToBottomVisibility()
}, [messages, updateJumpToBottomVisibility])
useLayoutEffect(() => {
if (!streaming) {
didScrollForStreamRef.current = false
return
}
if (didScrollForStreamRef.current) return
const doScroll = () => {
const container = chatScrollRef.current
const target = lastUserRowRef.current || streamingAssistantRowRef.current
if (!container || !target) return false
const canScroll = container.scrollHeight > container.clientHeight + 2
if (!canScroll) return false
const offset = target.offsetTop - container.offsetTop
container.scrollTop = offset
const delta = Math.abs(
target.getBoundingClientRect().top - container.getBoundingClientRect().top,
)
if (delta < 12) didScrollForStreamRef.current = true
updateJumpToBottomVisibility()
return true
}
if (doScroll()) return undefined
const id = requestAnimationFrame(() => {
if (!didScrollForStreamRef.current) doScroll()
})
return () => cancelAnimationFrame(id)
}, [streaming, messages, updateJumpToBottomVisibility])
useEffect(() => {
const el = chatScrollRef.current
if (!el) return
const onScroll = () => updateJumpToBottomVisibility()
el.addEventListener('scroll', onScroll, { passive: true })
window.addEventListener('resize', onScroll)
return () => {
el.removeEventListener('scroll', onScroll)
window.removeEventListener('resize', onScroll)
}
}, [updateJumpToBottomVisibility])
const scrollToLatestQuestion = useCallback(() => {
const container = chatScrollRef.current
if (!container) return
const userRow = lastUserRowRef.current
if (userRow) {
const offset = userRow.offsetTop - container.offsetTop
container.scrollTo({ top: offset, behavior: 'smooth' })
} else {
container.scrollTo({ top: container.scrollHeight, behavior: 'smooth' })
}
requestAnimationFrame(() => {
updateJumpToBottomVisibility()
composerRef.current?.focus()
})
}, [updateJumpToBottomVisibility])
const adjustComposerHeight = useCallback(() => {
const el = composerRef.current
if (!el) return
const maxPx = window.innerHeight * 0.5
el.style.height = 'auto'
const sh = el.scrollHeight
if (sh <= maxPx) {
el.style.height = `${sh}px`
el.style.overflowY = 'hidden'
} else {
el.style.height = `${maxPx}px`
el.style.overflowY = 'auto'
}
}, [])
useLayoutEffect(() => {
adjustComposerHeight()
}, [input, adjustComposerHeight])
useEffect(() => {
const onResize = () => adjustComposerHeight()
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [adjustComposerHeight])
useEffect(() => {
try { sessionStorage.setItem(STORAGE_PERSONA, extraPersona) } catch { /* */ }
}, [extraPersona])
useEffect(() => {
if (!optionsOpen) return
const onDown = (e) => {
if (optionsRef.current && !optionsRef.current.contains(e.target)) setOptionsOpen(false)
}
const onKey = (e) => { if (e.key === 'Escape') setOptionsOpen(false) }
document.addEventListener('mousedown', onDown)
document.addEventListener('keydown', onKey)
return () => {
document.removeEventListener('mousedown', onDown)
document.removeEventListener('keydown', onKey)
}
}, [optionsOpen])
useEffect(() => {
if (!shareOpen) return
const onDown = (e) => {
if (shareRef.current && !shareRef.current.contains(e.target)) setShareOpen(false)
}
const onKey = (e) => { if (e.key === 'Escape') setShareOpen(false) }
document.addEventListener('mousedown', onDown)
document.addEventListener('keydown', onKey)
return () => {
document.removeEventListener('mousedown', onDown)
document.removeEventListener('keydown', onKey)
}
}, [shareOpen])
useEffect(() => {
if (!exportOpen) return
const onDown = (e) => {
if (exportRef.current && !exportRef.current.contains(e.target)) setExportOpen(false)
}
const onKey = (e) => { if (e.key === 'Escape') setExportOpen(false) }
document.addEventListener('mousedown', onDown)
document.addEventListener('keydown', onKey)
return () => {
document.removeEventListener('mousedown', onDown)
document.removeEventListener('keydown', onKey)
}
}, [exportOpen])
const buildSystemPrompt = useCallback(() => {
const base =
'You are AI Jerry, a cybersecurity-focused assistant running on the BrainForge Security model. ' +
'You give clear, practical guidance; you distinguish facts from speculation; you flag risks and ' +
'compliance considerations when relevant. You are friendly and professional.'
const extra = (extraPersona || '').trim()
return extra ? `${base}\n\nAdditional instructions from the user:\n${extra}` : base
}, [extraPersona])
const triggerBackgroundSummarize = useCallback(
async (allMessages) => {
const sys = buildSystemPrompt()
const est = estimateMessagesTokens(
allMessages.map(({ role, content }) => ({ role, content })),
sys,
null,
)
const inputBudget = CONTEXT_WINDOW - MAX_REPLY_TOKENS
const ratio = est / inputBudget
if (ratio < SUMMARIZE_THRESHOLD) return
setSummarizing(true)
try {
const res = await fetch('/api/chat/summarize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: allMessages.map(({ role, content }) => ({ role, content })),
extra_persona: extraPersona,
}),
})
if (!res.ok) return
const data = await res.json()
if (data.summary) {
setSummary(data.summary)
}
} catch {
/* silent — summarization is best-effort */
} finally {
setSummarizing(false)
}
},
[buildSystemPrompt, extraPersona],
)
const buildApiPayload = useCallback(
(rawMsgs) => {
if (!summary) {
return { messages: rawMsgs, summary: null }
}
const sys = buildSystemPrompt()
const fullEst = estimateMessagesTokens(rawMsgs, sys, null)
const inputBudget = CONTEXT_WINDOW - MAX_REPLY_TOKENS
if (fullEst < inputBudget * 0.7) {
return { messages: rawMsgs, summary: null }
}
const KEEP_RECENT = 4
const recent = rawMsgs.slice(-KEEP_RECENT)
return { messages: recent, summary }
},
[summary, buildSystemPrompt],
)
const runStream = useCallback(
async (rawApiMsgs) => {
const ac = new AbortController()
abortRef.current = ac
setStreaming(true)
setContextOverflow(null)
let acc = ''
const { messages: apiMsgs, summary: useSummary } = buildApiPayload(rawApiMsgs)
try {
const res = await fetch('/api/chat/stream', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: apiMsgs,
extra_persona: extraPersona,
summary: useSummary || undefined,
}),
signal: ac.signal,
})
if (!res.ok) {
const t = await res.text()
throw new Error(t || res.statusText)
}
const reader = res.body.getReader()
const decoder = new TextDecoder()
let buffer = ''
const handleEv = (ev) => {
if (ev.type === 'context_overflow') {
setContextOverflow({
detail: ev.detail || 'The conversation is too long for the model context window.',
inputTokens: ev.input_tokens,
contextWindow: ev.context_window || CONTEXT_WINDOW,
})
return
}
if (ev.type === 'token') {
const nextAcc = appendTokenContent(acc, ev)
if (nextAcc !== acc) {
acc = nextAcc
setMessages((prev) => {
const next = [...prev]
const last = next[next.length - 1]
if (last?.role === 'assistant') {
next[next.length - 1] = { ...last, content: acc }
}
return next
})
}
}
if (ev.type === 'error') {
const detail = ev.detail || ''
if (/context.length|max_tokens.*too large|too many tokens/i.test(detail)) {
setContextOverflow({ detail })
} else {
throw new Error(detail || 'Stream error')
}
}
}
while (true) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
buffer = parseSseBuffer(buffer, handleEv)
}
if (buffer.trim()) {
parseSseBuffer(`${buffer}\n`, handleEv)
}
if (!acc.trim() && !contextOverflow) {
setMessages((prev) => {
const next = [...prev]
const last = next[next.length - 1]
if (last?.role === 'assistant' && !last.content.trim()) {
next[next.length - 1] = {
role: 'assistant',
content: '[No response] The stream finished with no text.',
}
}
return next
})
}
} catch (e) {
if (e.name === 'AbortError') {
setMessages((prev) => {
const next = [...prev]
const last = next[next.length - 1]
if (last?.role === 'assistant' && !last.content.trim()) next.pop()
return next
})
} else {
setMessages((prev) => {
const next = [...prev]
const last = next[next.length - 1]
if (last?.role === 'assistant') {
next[next.length - 1] = {
...last,
content: (last.content ? `${last.content}\n\n` : '') + `[Error] ${e.message}`,
}
} else {
next.push({ role: 'assistant', content: `[Error] ${e.message}` })
}
return next
})
}
} finally {
setStreaming(false)
abortRef.current = null
}
},
[extraPersona, buildApiPayload, contextOverflow],
)
useEffect(() => {
if (streaming || messages.length < 4) return
const last = messages[messages.length - 1]
if (last.role !== 'assistant' || !last.content.trim()) return
triggerBackgroundSummarize(messages)
}, [streaming, messages, triggerBackgroundSummarize])
const handleNewChat = () => {
abortRef.current?.abort()
setMessages([])
setInput('')
setSummary(null)
setContextOverflow(null)
}
const handleStop = () => { abortRef.current?.abort() }
const handleRefresh = () => {
if (streaming) return
let base = [...messages]
if (base.length && base[base.length - 1].role === 'assistant') base = base.slice(0, -1)
if (!base.length || base[base.length - 1].role !== 'user') return
const apiMsgs = base.map(({ role, content }) => ({ role, content }))
setMessages([...base, { role: 'assistant', content: '' }])
setContextOverflow(null)
runStream(apiMsgs)
}
const handleSend = () => {
const text = input.trim()
if (!text || streaming) return
setHideTypingCursor(false)
const userMsg = { role: 'user', content: text }
const apiMsgs = [...messages, userMsg].map(({ role, content }) => ({ role, content }))
setMessages([...messages, userMsg, { role: 'assistant', content: '' }])
setInput('')
setContextOverflow(null)
runStream(apiMsgs)
}
const handleOverflowRetry = () => {
setContextOverflow(null)
setMessages((prev) => {
const next = [...prev]
const last = next[next.length - 1]
if (last?.role === 'assistant' && !last.content.trim()) next.pop()
return next
})
}
const handleOverflowClear = () => {
setContextOverflow(null)
setMessages([])
setSummary(null)
}
const handleOverflowSummarize = async () => {
setContextOverflow(null)
setSummarizing(true)
try {
const res = await fetch('/api/chat/summarize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: messages.map(({ role, content }) => ({ role, content })),
extra_persona: extraPersona,
}),
})
if (res.ok) {
const data = await res.json()
if (data.summary) {
setSummary(data.summary)
setMessages([])
return
}
}
setMessages([])
setSummary(null)
} catch {
setMessages([])
setSummary(null)
} finally {
setSummarizing(false)
}
}
const onKeyDown = (e) => {
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend() }
}
const buildExportText = useCallback(() => {
if (!messages.length) return ''
const lines = messages.map((m) => {
const label = m.role === 'user' ? 'You' : 'AI Jerry'
return `${label}:\n${m.content}\n`
})
return `Ask Jerry — Chat Export\n${new Date().toLocaleString()}\n${'─'.repeat(40)}\n\n${lines.join('\n')}`
}, [messages])
const buildExportMarkdown = useCallback(() => {
if (!messages.length) return ''
const ts = new Date().toLocaleString()
const header = `# Ask Jerry — Chat Export\n\n_Exported: ${ts}_\n\n---\n\n`
const blocks = messages.map((m) => {
const heading = m.role === 'user' ? '### You' : '### AI Jerry'
return `${heading}\n\n${m.content}`
})
return header + blocks.join('\n\n---\n\n')
}, [messages])
const copyToClipboard = async (text) => {
try {
await navigator.clipboard.writeText(text)
} catch {
const ta = document.createElement('textarea')
ta.value = text
ta.setAttribute('readonly', '')
ta.style.position = 'fixed'
ta.style.left = '-9999px'
document.body.appendChild(ta)
ta.select()
try {
document.execCommand('copy')
} finally {
document.body.removeChild(ta)
}
}
}
const handleExportDownload = (format) => {
const text = format === 'md' ? buildExportMarkdown() : buildExportText()
if (!text) return
const mime =
format === 'md' ? 'text/markdown;charset=utf-8' : 'text/plain;charset=utf-8'
const ext = format === 'md' ? 'md' : 'txt'
const blob = new Blob([text], { type: mime })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `ask-jerry-chat-${Date.now()}.${ext}`
a.click()
URL.revokeObjectURL(url)
setExportOpen(false)
}
const handleShareCopyClipboard = async () => {
const text = buildExportText()
if (!text) return
await copyToClipboard(text)
setShareOpen(false)
}
const handleShareEmail = () => {
const text = buildExportText()
if (!text) return
const subject = encodeURIComponent('Ask Jerry chat')
const body = encodeURIComponent(text.length > 16000 ? `${text.slice(0, 16000)}\n\n… (truncated)` : text)
const a = document.createElement('a')
a.href = `mailto:?subject=${subject}&body=${body}`
a.rel = 'noopener noreferrer'
a.click()
setShareOpen(false)
}
const handleShareLinkedIn = async () => {
const text = buildExportText()
if (!text) return
await copyToClipboard(text)
window.open('https://www.linkedin.com/feed/?shareActive=true', '_blank', 'noopener,noreferrer')
setShareOpen(false)
}
const handleShareFacebook = async () => {
const text = buildExportText()
if (!text) return
await copyToClipboard(text)
const quote = encodeURIComponent(text.slice(0, 2000))
const fb = `https://www.facebook.com/sharer/sharer.php?quote=${quote}`
if (fb.length > 2048) {
window.open('https://www.facebook.com/', '_blank', 'noopener,noreferrer')
} else {
window.open(fb, '_blank', 'noopener,noreferrer')
}
setShareOpen(false)
}
const inputBudget = CONTEXT_WINDOW - MAX_REPLY_TOKENS
const currentTokens = estimateMessagesTokens(
messages.map(({ role, content }) => ({ role, content })),
buildSystemPrompt(),
summary && messages.length > 4 ? summary : null,
)
const usagePct = Math.min(100, Math.round((currentTokens / inputBudget) * 100))
return (
{messages.length > 0 && (
80 ? '' : undefined} />
Context: ~{currentTokens.toLocaleString()} / {inputBudget.toLocaleString()} tokens ({usagePct}%)
{summary && ' · chat history is being summarized'}
{summarizing && ' · summarizing…'}
)}
{messages.length === 0 && !summary && (
Message AI Jerry to begin.
)}
{messages.length === 0 && summary && (
Continuing from a summary of your previous conversation.
{summary}
)}
{(() => {
const lastUserIdx = messages.reduce((acc, msg, idx) => msg.role === 'user' ? idx : acc, -1)
return messages.map((m, i) => {
const isUser = m.role === 'user'
const rowRef =
i === lastUserIdx
? lastUserRowRef
: i === messages.length - 1 && m.role === 'assistant'
? streamingAssistantRowRef
: undefined
return (
{!isUser &&
}
{isUser ? 'You' : 'AI Jerry'}
{isUser ? (
m.content
) : (
<>
{m.content ?
: null}
{streaming && i === messages.length - 1 && !m.content && (
)}
>
)}
{!isUser && (
)}
)
})
})()}
{contextOverflow && (
Context window exceeded
The conversation history is too long for this model's {CONTEXT_WINDOW.toLocaleString()}-token context window.
)}
{showJumpToBottom && (
)}
)
}