mirror of
https://github.com/elder-plinius/G0DM0D3.git
synced 2026-11-16 07:02:25 +00:00
889 lines
35 KiB
TypeScript
889 lines
35 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useRef, useEffect, KeyboardEvent } from 'react'
|
|
import { useStore } from '@/store'
|
|
import { sendMessage, sendMessageViaProxy, streamUltraplinian, streamConsortium } from '@/lib/openrouter'
|
|
import { recordChatEvent } from '@/lib/telemetry'
|
|
import { classifyPrompt } from '@/lib/classify'
|
|
import { classifyWithLLM } from '@/lib/classify-llm'
|
|
import type { ClassificationResult } from '@/lib/classify'
|
|
import { computeAutoTuneParams, getContextLabel, getStrategyLabel, PARAM_META } from '@/lib/autotune'
|
|
import type { AutoTuneResult } from '@/lib/autotune'
|
|
import { applyParseltongue, detectTriggers } from '@/lib/parseltongue'
|
|
import { Send, Loader2, StopCircle, SlidersHorizontal, AlertTriangle } from 'lucide-react'
|
|
|
|
export function ChatInput() {
|
|
const {
|
|
currentConversationId,
|
|
currentConversation,
|
|
addMessage,
|
|
updateMessageContent,
|
|
apiKey,
|
|
isStreaming,
|
|
setIsStreaming,
|
|
personas,
|
|
stmModules,
|
|
noLogMode,
|
|
autoTuneEnabled,
|
|
autoTuneStrategy,
|
|
autoTuneOverrides,
|
|
autoTuneLastResult,
|
|
setAutoTuneLastResult,
|
|
feedbackState,
|
|
memories,
|
|
memoriesEnabled,
|
|
parseltongueConfig,
|
|
customSystemPrompt,
|
|
useCustomSystemPrompt,
|
|
// Liquid Response (universal)
|
|
liquidResponseEnabled,
|
|
liquidMinDelta,
|
|
incrementPromptsTried,
|
|
// ULTRAPLINIAN
|
|
ultraplinianEnabled,
|
|
ultraplinianTier,
|
|
ultraplinianApiUrl,
|
|
ultraplinianApiKey,
|
|
ultraplinianRacing,
|
|
ultraplinianModelsResponded,
|
|
ultraplinianModelsTotal,
|
|
ultraplinianLiveModel,
|
|
ultraplinianLiveScore,
|
|
setUltraplinianLive,
|
|
setUltraplinianProgress,
|
|
setUltraplinianRacing,
|
|
resetUltraplinianRace,
|
|
// Privacy
|
|
datasetGenerationEnabled,
|
|
// CONSORTIUM
|
|
consortiumEnabled,
|
|
consortiumTier,
|
|
consortiumPhase,
|
|
consortiumModelsCollected,
|
|
consortiumModelsTotal,
|
|
setConsortiumPhase,
|
|
setConsortiumProgress,
|
|
resetConsortium,
|
|
} = useStore()
|
|
|
|
const [input, setInput] = useState('')
|
|
const [showTuneDetails, setShowTuneDetails] = useState(false)
|
|
const [parseltonguePreview, setParseltonguePreview] = useState<{
|
|
triggersFound: string[]
|
|
transformed: boolean
|
|
} | null>(null)
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null)
|
|
const abortControllerRef = useRef<AbortController | null>(null)
|
|
|
|
// Auto-resize textarea
|
|
useEffect(() => {
|
|
if (textareaRef.current) {
|
|
textareaRef.current.style.height = 'auto'
|
|
textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 200)}px`
|
|
}
|
|
}, [input])
|
|
|
|
// Live preview: compute autotune params as user types (debounced)
|
|
const [livePreview, setLivePreview] = useState<AutoTuneResult | null>(null)
|
|
useEffect(() => {
|
|
if (!autoTuneEnabled || !input.trim()) {
|
|
setLivePreview(null)
|
|
return
|
|
}
|
|
|
|
const timer = setTimeout(() => {
|
|
const persona = personas.find(p => p.id === currentConversation?.persona) || personas[0]
|
|
const history = (currentConversation?.messages || []).map(m => ({
|
|
role: m.role,
|
|
content: m.content
|
|
}))
|
|
|
|
const result = computeAutoTuneParams({
|
|
strategy: autoTuneStrategy,
|
|
message: input.trim(),
|
|
conversationHistory: history,
|
|
overrides: autoTuneOverrides,
|
|
learnedProfiles: feedbackState.learnedProfiles
|
|
})
|
|
|
|
setLivePreview(result)
|
|
}, 300)
|
|
|
|
return () => clearTimeout(timer)
|
|
}, [input, autoTuneEnabled, autoTuneStrategy, autoTuneOverrides, currentConversation, personas, feedbackState])
|
|
|
|
// Live preview: detect triggers as user types (debounced)
|
|
useEffect(() => {
|
|
if (!parseltongueConfig.enabled || !input.trim()) {
|
|
setParseltonguePreview(null)
|
|
return
|
|
}
|
|
|
|
const timer = setTimeout(() => {
|
|
const triggers = detectTriggers(input.trim(), parseltongueConfig.customTriggers)
|
|
if (triggers.length > 0) {
|
|
setParseltonguePreview({
|
|
triggersFound: triggers,
|
|
transformed: true
|
|
})
|
|
} else {
|
|
setParseltonguePreview(null)
|
|
}
|
|
}, 200)
|
|
|
|
return () => clearTimeout(timer)
|
|
}, [input, parseltongueConfig])
|
|
|
|
// Proxy mode: when no personal OpenRouter key, route through self-hosted API
|
|
const proxyMode = !apiKey && !!ultraplinianApiUrl && !!ultraplinianApiKey
|
|
|
|
const handleSubmit = async () => {
|
|
if (!input.trim() || !currentConversationId || isStreaming) return
|
|
if (!apiKey && !proxyMode) return
|
|
|
|
const originalMessage = input.trim()
|
|
setInput('')
|
|
setIsStreaming(true)
|
|
incrementPromptsTried()
|
|
|
|
// Apply parseltongue obfuscation if enabled
|
|
const parseltongueResult = applyParseltongue(originalMessage, parseltongueConfig)
|
|
const userMessage = parseltongueResult.transformedText
|
|
|
|
|
|
// Add user message (show original to user, send transformed to API)
|
|
addMessage(currentConversationId, {
|
|
role: 'user',
|
|
content: originalMessage // Show original message in UI
|
|
})
|
|
|
|
// Get persona and model
|
|
const persona = personas.find(p => p.id === currentConversation?.persona) || personas[0]
|
|
const model = currentConversation?.model || 'anthropic/claude-3-opus'
|
|
|
|
// Build memory context if enabled
|
|
const activeMemories = memoriesEnabled ? memories.filter(m => m.active) : []
|
|
let memoryContext = ''
|
|
if (activeMemories.length > 0) {
|
|
const facts = activeMemories.filter(m => m.type === 'fact')
|
|
const preferences = activeMemories.filter(m => m.type === 'preference')
|
|
const instructions = activeMemories.filter(m => m.type === 'instruction')
|
|
|
|
memoryContext = '\n\n<user_memory>\n'
|
|
if (facts.length > 0) {
|
|
memoryContext += '## About the User\n'
|
|
facts.forEach(f => { memoryContext += `- ${f.content}\n` })
|
|
}
|
|
if (preferences.length > 0) {
|
|
memoryContext += '\n## User Preferences\n'
|
|
preferences.forEach(p => { memoryContext += `- ${p.content}\n` })
|
|
}
|
|
if (instructions.length > 0) {
|
|
memoryContext += '\n## Always Follow\n'
|
|
instructions.forEach(i => { memoryContext += `- ${i.content}\n` })
|
|
}
|
|
memoryContext += '</user_memory>\n'
|
|
}
|
|
|
|
// Build system prompt with GODMODE prompt + memory
|
|
const basePrompt = useCustomSystemPrompt ? customSystemPrompt : (persona.systemPrompt || persona.coreDirective || '')
|
|
const systemPrompt = basePrompt + memoryContext
|
|
|
|
// Build messages array
|
|
const messages = [
|
|
// System prompt from persona + memory
|
|
...(systemPrompt ? [{ role: 'system' as const, content: systemPrompt }] : []),
|
|
// Conversation history
|
|
...((currentConversation?.messages || []).map(m => ({
|
|
role: m.role as 'user' | 'assistant',
|
|
content: m.content
|
|
}))),
|
|
// New user message
|
|
{ role: 'user' as const, content: userMessage }
|
|
]
|
|
|
|
// Classify prompt for research telemetry
|
|
// Regex runs instantly as fallback; LLM classifier fires in parallel
|
|
// with the main model call and overwrites with a more accurate result.
|
|
let promptClassification: ClassificationResult = classifyPrompt(userMessage)
|
|
const llmClassifyPromise = apiKey
|
|
? classifyWithLLM(userMessage, apiKey).then(result => { promptClassification = result })
|
|
: Promise.resolve()
|
|
|
|
// Compute AutoTune parameters if enabled
|
|
let tuneResult: AutoTuneResult | null = null
|
|
if (autoTuneEnabled) {
|
|
const history = (currentConversation?.messages || []).map(m => ({
|
|
role: m.role,
|
|
content: m.content
|
|
}))
|
|
|
|
tuneResult = computeAutoTuneParams({
|
|
strategy: autoTuneStrategy,
|
|
message: userMessage,
|
|
conversationHistory: history,
|
|
overrides: autoTuneOverrides,
|
|
learnedProfiles: feedbackState.learnedProfiles
|
|
})
|
|
|
|
setAutoTuneLastResult(tuneResult)
|
|
}
|
|
|
|
try {
|
|
abortControllerRef.current = new AbortController()
|
|
|
|
// ── CONSORTIUM PATH: Hive-mind synthesis ──────────────────────
|
|
if (consortiumEnabled && ultraplinianApiUrl && ultraplinianApiKey && !ultraplinianEnabled) {
|
|
const assistantMsgId = addMessage(currentConversationId, {
|
|
role: 'assistant',
|
|
content: '',
|
|
model: 'consortium',
|
|
persona: persona.id,
|
|
})
|
|
|
|
setConsortiumPhase('collecting')
|
|
resetConsortium()
|
|
|
|
await streamConsortium(
|
|
{
|
|
messages,
|
|
openrouterApiKey: apiKey,
|
|
apiBaseUrl: ultraplinianApiUrl,
|
|
godmodeApiKey: ultraplinianApiKey,
|
|
tier: consortiumTier,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
liquid: liquidResponseEnabled,
|
|
liquid_min_delta: liquidMinDelta,
|
|
signal: abortControllerRef.current.signal,
|
|
},
|
|
{
|
|
onStart: (data) => {
|
|
setConsortiumProgress(0, data.models_queried)
|
|
updateMessageContent(currentConversationId, assistantMsgId,
|
|
`*Collecting from ${data.models_queried} models...*`)
|
|
},
|
|
onModelResult: (data) => {
|
|
setConsortiumProgress(data.models_collected, data.models_total)
|
|
// Only update with progress text if liquid hasn't already shown real content
|
|
if (!liquidResponseEnabled) {
|
|
updateMessageContent(currentConversationId, assistantMsgId,
|
|
`*Collecting responses... ${data.models_collected}/${data.models_total} models*`)
|
|
}
|
|
},
|
|
onBestResponse: (data) => {
|
|
// Liquid response: show best individual model response while collecting
|
|
updateMessageContent(currentConversationId, assistantMsgId, data.content, {
|
|
model: `${data.model} (${data.score}pts — synthesizing...)`,
|
|
})
|
|
},
|
|
onSynthesisStart: (data) => {
|
|
setConsortiumPhase('synthesizing')
|
|
if (!liquidResponseEnabled) {
|
|
updateMessageContent(currentConversationId, assistantMsgId,
|
|
`*${data.responses_collected} models collected. Orchestrator synthesizing ground truth...*`)
|
|
}
|
|
},
|
|
onComplete: (data) => {
|
|
const finalContent = data.synthesis || ''
|
|
const orchModel = data.orchestrator?.model || 'consortium'
|
|
setConsortiumPhase('done')
|
|
|
|
updateMessageContent(currentConversationId, assistantMsgId, finalContent, {
|
|
model: `consortium (${orchModel})`,
|
|
...(tuneResult ? {
|
|
autoTuneParams: tuneResult.params,
|
|
autoTuneContext: tuneResult.detectedContext,
|
|
autoTuneContextScores: tuneResult.contextScores,
|
|
autoTunePatternMatches: tuneResult.patternMatches,
|
|
autoTuneDeltas: tuneResult.paramDeltas,
|
|
} : {}),
|
|
})
|
|
},
|
|
onError: (error) => {
|
|
updateMessageContent(currentConversationId, assistantMsgId,
|
|
`CONSORTIUM error: ${error}`)
|
|
setConsortiumPhase('idle')
|
|
},
|
|
},
|
|
)
|
|
|
|
setIsStreaming(false)
|
|
setConsortiumPhase('idle')
|
|
return
|
|
}
|
|
|
|
// ── ULTRAPLINIAN PATH: Multi-model race with liquid response ──
|
|
if (ultraplinianEnabled && ultraplinianApiUrl && ultraplinianApiKey) {
|
|
// Add placeholder assistant message that we'll update live
|
|
const assistantMsgId = addMessage(currentConversationId, {
|
|
role: 'assistant',
|
|
content: '',
|
|
model: 'ultraplinian',
|
|
persona: persona.id,
|
|
})
|
|
|
|
setUltraplinianRacing(true)
|
|
resetUltraplinianRace()
|
|
|
|
// Collect all race responses for browsing later
|
|
const collectedResponses: Array<{ model: string; content: string; score: number; duration_ms: number }> = []
|
|
|
|
await streamUltraplinian(
|
|
{
|
|
messages,
|
|
openrouterApiKey: apiKey,
|
|
apiBaseUrl: ultraplinianApiUrl,
|
|
godmodeApiKey: ultraplinianApiKey,
|
|
tier: ultraplinianTier,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
liquid: liquidResponseEnabled,
|
|
liquid_min_delta: liquidMinDelta,
|
|
signal: abortControllerRef.current.signal,
|
|
},
|
|
{
|
|
onRaceStart: (data) => {
|
|
setUltraplinianProgress(0, data.models_queried)
|
|
updateMessageContent(currentConversationId, assistantMsgId,
|
|
`*Racing ${data.models_queried} models...*`)
|
|
},
|
|
onModelResult: (data) => {
|
|
setUltraplinianProgress(data.models_responded, data.models_total)
|
|
},
|
|
onLeaderChange: (data) => {
|
|
// Collect each leader response for later browsing
|
|
collectedResponses.push({
|
|
model: data.model,
|
|
content: data.content,
|
|
score: data.score,
|
|
duration_ms: data.duration_ms,
|
|
})
|
|
setUltraplinianLive(data.content, data.model, data.score)
|
|
updateMessageContent(currentConversationId, assistantMsgId, data.content, {
|
|
model: data.model,
|
|
})
|
|
},
|
|
onComplete: async (data) => {
|
|
const finalContent = data.response || ''
|
|
const winnerModel = data.winner?.model || 'ultraplinian'
|
|
|
|
// Build full race responses from rankings (backend now includes content)
|
|
const rankingResponses = (data.race?.rankings ?? [])
|
|
.filter(r => r.success && r.content)
|
|
.map(r => ({
|
|
model: r.model,
|
|
content: r.content!,
|
|
score: r.score,
|
|
duration_ms: r.duration_ms,
|
|
isWinner: r.model === winnerModel,
|
|
}))
|
|
.sort((a, b) => b.score - a.score)
|
|
|
|
// Fall back to collected leader changes if rankings lack content
|
|
const raceResponses = rankingResponses.length > 0
|
|
? rankingResponses
|
|
: collectedResponses.map(r => ({
|
|
...r,
|
|
isWinner: r.model === winnerModel,
|
|
}))
|
|
|
|
updateMessageContent(currentConversationId, assistantMsgId, finalContent, {
|
|
model: winnerModel,
|
|
raceResponses: raceResponses.length > 1 ? raceResponses : undefined,
|
|
...(tuneResult ? {
|
|
autoTuneParams: tuneResult.params,
|
|
autoTuneContext: tuneResult.detectedContext,
|
|
autoTuneContextScores: tuneResult.contextScores,
|
|
autoTunePatternMatches: tuneResult.patternMatches,
|
|
autoTuneDeltas: tuneResult.paramDeltas,
|
|
} : {}),
|
|
})
|
|
resetUltraplinianRace()
|
|
|
|
// Wait for LLM classification to land (usually already resolved)
|
|
await llmClassifyPromise
|
|
|
|
// Beacon metadata to HF dataset (fire-and-forget, no content)
|
|
recordChatEvent({
|
|
mode: 'ultraplinian',
|
|
model: winnerModel,
|
|
duration_ms: data.race?.total_duration_ms || 0,
|
|
response_length: finalContent.length,
|
|
success: true,
|
|
pipeline: {
|
|
autotune: autoTuneEnabled,
|
|
parseltongue: parseltongueConfig.enabled,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
strategy: autoTuneStrategy,
|
|
godmode: true,
|
|
},
|
|
...(tuneResult ? {
|
|
autotune: {
|
|
detected_context: tuneResult.detectedContext,
|
|
confidence: tuneResult.confidence,
|
|
},
|
|
} : {}),
|
|
parseltongue: parseltongueConfig.enabled ? {
|
|
triggers_found: parseltongueResult.triggersFound.length,
|
|
technique: parseltongueConfig.technique,
|
|
intensity: parseltongueConfig.intensity,
|
|
} : undefined,
|
|
ultraplinian: {
|
|
tier: ultraplinianTier,
|
|
models_queried: data.race?.models_queried || 0,
|
|
models_succeeded: data.race?.models_succeeded || 0,
|
|
winner_model: winnerModel,
|
|
winner_score: data.winner?.score || 0,
|
|
total_duration_ms: data.race?.total_duration_ms || 0,
|
|
},
|
|
classification: promptClassification,
|
|
persona: persona.id,
|
|
prompt_length: originalMessage.length,
|
|
conversation_depth: currentConversation?.messages?.length || 0,
|
|
memory_count: activeMemories.length,
|
|
no_log: noLogMode,
|
|
parseltongue_transformed: parseltongueResult.triggersFound.length > 0,
|
|
})
|
|
},
|
|
onError: (error) => {
|
|
updateMessageContent(currentConversationId, assistantMsgId,
|
|
`**ULTRAPLINIAN Error:** ${error}`)
|
|
resetUltraplinianRace()
|
|
},
|
|
},
|
|
)
|
|
} else {
|
|
// ── STANDARD PATH: Single model ────────────────────────────
|
|
const startTime = Date.now()
|
|
const response = proxyMode
|
|
? await sendMessageViaProxy({
|
|
messages,
|
|
model,
|
|
apiBaseUrl: ultraplinianApiUrl,
|
|
godmodeApiKey: ultraplinianApiKey,
|
|
signal: abortControllerRef.current.signal,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
...(tuneResult ? {
|
|
temperature: tuneResult.params.temperature,
|
|
top_p: tuneResult.params.top_p,
|
|
top_k: tuneResult.params.top_k,
|
|
frequency_penalty: tuneResult.params.frequency_penalty,
|
|
presence_penalty: tuneResult.params.presence_penalty,
|
|
repetition_penalty: tuneResult.params.repetition_penalty,
|
|
} : {}),
|
|
})
|
|
: await sendMessage({
|
|
messages,
|
|
model,
|
|
apiKey,
|
|
noLog: noLogMode,
|
|
signal: abortControllerRef.current.signal,
|
|
...(tuneResult ? {
|
|
temperature: tuneResult.params.temperature,
|
|
top_p: tuneResult.params.top_p,
|
|
top_k: tuneResult.params.top_k,
|
|
frequency_penalty: tuneResult.params.frequency_penalty,
|
|
presence_penalty: tuneResult.params.presence_penalty,
|
|
repetition_penalty: tuneResult.params.repetition_penalty
|
|
} : {})
|
|
})
|
|
const durationMs = Date.now() - startTime
|
|
|
|
// Apply STM transformations
|
|
let transformedResponse = response
|
|
for (const stm of stmModules) {
|
|
if (stm.enabled) {
|
|
transformedResponse = stm.transformer(transformedResponse)
|
|
}
|
|
}
|
|
|
|
addMessage(currentConversationId, {
|
|
role: 'assistant',
|
|
content: transformedResponse,
|
|
model,
|
|
persona: persona.id,
|
|
...(tuneResult ? {
|
|
autoTuneParams: tuneResult.params,
|
|
autoTuneContext: tuneResult.detectedContext,
|
|
autoTuneContextScores: tuneResult.contextScores,
|
|
autoTunePatternMatches: tuneResult.patternMatches,
|
|
autoTuneDeltas: tuneResult.paramDeltas
|
|
} : {})
|
|
})
|
|
|
|
// Wait for LLM classification to land (usually already resolved)
|
|
await llmClassifyPromise
|
|
|
|
// Beacon metadata to HF dataset (fire-and-forget, no content)
|
|
recordChatEvent({
|
|
mode: 'standard',
|
|
model,
|
|
duration_ms: durationMs,
|
|
response_length: transformedResponse.length,
|
|
success: true,
|
|
pipeline: {
|
|
autotune: autoTuneEnabled,
|
|
parseltongue: parseltongueConfig.enabled,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
strategy: autoTuneStrategy,
|
|
godmode: useCustomSystemPrompt,
|
|
},
|
|
...(tuneResult ? {
|
|
autotune: {
|
|
detected_context: tuneResult.detectedContext,
|
|
confidence: tuneResult.confidence,
|
|
},
|
|
} : {}),
|
|
parseltongue: parseltongueConfig.enabled ? {
|
|
triggers_found: parseltongueResult.triggersFound.length,
|
|
technique: parseltongueConfig.technique,
|
|
intensity: parseltongueConfig.intensity,
|
|
} : undefined,
|
|
classification: promptClassification,
|
|
persona: persona.id,
|
|
prompt_length: originalMessage.length,
|
|
conversation_depth: currentConversation?.messages?.length || 0,
|
|
memory_count: activeMemories.length,
|
|
no_log: noLogMode,
|
|
parseltongue_transformed: parseltongueResult.triggersFound.length > 0,
|
|
})
|
|
}
|
|
} catch (error: any) {
|
|
resetUltraplinianRace()
|
|
if (error.name === 'AbortError') {
|
|
addMessage(currentConversationId, {
|
|
role: 'assistant',
|
|
content: '_[Response stopped by user]_',
|
|
model,
|
|
persona: persona.id
|
|
})
|
|
recordChatEvent({
|
|
mode: ultraplinianEnabled ? 'ultraplinian' : 'standard',
|
|
model,
|
|
duration_ms: 0,
|
|
response_length: 0,
|
|
success: false,
|
|
error_type: 'abort',
|
|
pipeline: {
|
|
autotune: autoTuneEnabled,
|
|
parseltongue: parseltongueConfig.enabled,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
strategy: autoTuneStrategy,
|
|
godmode: useCustomSystemPrompt,
|
|
},
|
|
classification: promptClassification,
|
|
persona: persona.id,
|
|
prompt_length: originalMessage.length,
|
|
conversation_depth: currentConversation?.messages?.length || 0,
|
|
memory_count: activeMemories.length,
|
|
no_log: noLogMode,
|
|
parseltongue_transformed: parseltongueResult.triggersFound.length > 0,
|
|
})
|
|
} else {
|
|
console.error('Error sending message:', error)
|
|
const errMsg = error.message || 'Failed to get response. Check your API key in Settings and try again.'
|
|
const errLower = errMsg.toLowerCase()
|
|
const errorType = errLower.includes('api key') || errLower.includes('expired') || errLower.includes('denied') || errLower.includes('permission')
|
|
? 'auth'
|
|
: errLower.includes('rate limit') || errLower.includes('wait')
|
|
? 'rate_limit'
|
|
: errLower.includes('timeout') || errLower.includes('timed out')
|
|
? 'timeout'
|
|
: errLower.includes('unavailable') || errLower.includes('overloaded')
|
|
? 'model_error'
|
|
: errLower.includes('credit') || errLower.includes('insufficient')
|
|
? 'billing'
|
|
: 'unknown'
|
|
addMessage(currentConversationId, {
|
|
role: 'assistant',
|
|
content: `**Error:** ${errMsg}`,
|
|
model,
|
|
persona: persona.id
|
|
})
|
|
recordChatEvent({
|
|
mode: ultraplinianEnabled ? 'ultraplinian' : 'standard',
|
|
model,
|
|
duration_ms: 0,
|
|
response_length: 0,
|
|
success: false,
|
|
error_type: errorType,
|
|
pipeline: {
|
|
autotune: autoTuneEnabled,
|
|
parseltongue: parseltongueConfig.enabled,
|
|
stm_modules: stmModules.filter(m => m.enabled).map(m => m.id),
|
|
strategy: autoTuneStrategy,
|
|
godmode: useCustomSystemPrompt,
|
|
},
|
|
classification: promptClassification,
|
|
persona: persona.id,
|
|
prompt_length: originalMessage.length,
|
|
conversation_depth: currentConversation?.messages?.length || 0,
|
|
memory_count: activeMemories.length,
|
|
no_log: noLogMode,
|
|
parseltongue_transformed: parseltongueResult.triggersFound.length > 0,
|
|
})
|
|
}
|
|
} finally {
|
|
setIsStreaming(false)
|
|
setUltraplinianRacing(false)
|
|
abortControllerRef.current = null
|
|
}
|
|
}
|
|
|
|
const handleStop = () => {
|
|
if (abortControllerRef.current) {
|
|
abortControllerRef.current.abort()
|
|
}
|
|
}
|
|
|
|
const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
e.preventDefault()
|
|
handleSubmit()
|
|
}
|
|
}
|
|
|
|
// Determine which result to show (live preview while typing, last result after send)
|
|
const displayResult = livePreview || autoTuneLastResult
|
|
|
|
// Count active memories for display
|
|
const activeMemoryCount = memoriesEnabled ? memories.filter(m => m.active).length : 0
|
|
|
|
return (
|
|
<div className="border-t border-theme-primary bg-theme-dim/50 p-4">
|
|
<div className="max-w-4xl mx-auto">
|
|
{/* AutoTune live parameter display */}
|
|
{autoTuneEnabled && displayResult && showTuneDetails && (
|
|
<div className="mb-3 p-3 bg-theme-bg border border-theme-primary rounded-lg space-y-3">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2 text-xs font-semibold theme-primary">
|
|
<SlidersHorizontal className="w-3 h-3" />
|
|
AUTOTUNE {autoTuneStrategy === 'adaptive'
|
|
? `// ${getContextLabel(displayResult.detectedContext)} (${Math.round(displayResult.confidence * 100)}%)`
|
|
: `// ${getStrategyLabel(autoTuneStrategy)}`
|
|
}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Context Competition - show all context scores */}
|
|
{displayResult.contextScores && displayResult.contextScores.length > 1 && (
|
|
<div className="flex items-center gap-1 text-[10px] font-mono">
|
|
<span className="theme-secondary mr-1">CONTEXT:</span>
|
|
{displayResult.contextScores
|
|
.filter(s => s.percentage > 0)
|
|
.slice(0, 4)
|
|
.map((s, i) => (
|
|
<span key={s.type} className="flex items-center">
|
|
{i > 0 && <span className="text-gray-600 mx-1">></span>}
|
|
<span className={i === 0 ? 'text-cyan-400 font-bold' : 'theme-secondary'}>
|
|
{getContextLabel(s.type)} {s.percentage}%
|
|
</span>
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{/* Pattern Match Reasoning - why this context was detected */}
|
|
{displayResult.patternMatches && displayResult.patternMatches.length > 0 && (
|
|
<div className="text-[10px] font-mono">
|
|
<span className="theme-secondary">MATCHED: </span>
|
|
<span className="text-purple-400">
|
|
{displayResult.patternMatches
|
|
.slice(0, 3)
|
|
.map(p => p.pattern)
|
|
.join(' | ')}
|
|
{displayResult.patternMatches.length > 3 && ` +${displayResult.patternMatches.length - 3} more`}
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Parameter Grid with Deltas */}
|
|
<div className="grid grid-cols-6 gap-2">
|
|
{(Object.entries(displayResult.params) as [keyof typeof PARAM_META, number][]).map(
|
|
([key, value]) => {
|
|
// Find if there's a delta for this param
|
|
const delta = displayResult.paramDeltas?.find(d => d.param === key)
|
|
const hasDelta = delta && Math.abs(delta.delta) > 0.001
|
|
|
|
return (
|
|
<div
|
|
key={key}
|
|
className={`text-center p-1.5 rounded border transition-all
|
|
${hasDelta
|
|
? 'bg-cyan-500/10 border-cyan-500/30'
|
|
: 'bg-theme-dim border-theme-primary/30'
|
|
}`}
|
|
title={delta?.reason || PARAM_META[key].description}
|
|
>
|
|
<div className="text-[10px] theme-secondary font-mono">
|
|
{PARAM_META[key].short}
|
|
</div>
|
|
<div className="text-sm font-bold theme-primary font-mono">
|
|
{typeof value === 'number' ? value.toFixed(2) : value}
|
|
</div>
|
|
{hasDelta && (
|
|
<div className={`text-[9px] font-mono ${delta.delta > 0 ? 'text-green-400' : 'text-red-400'}`}>
|
|
{delta.delta > 0 ? '+' : ''}{delta.delta.toFixed(2)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
)}
|
|
</div>
|
|
|
|
{/* Delta Explanations - what changed and why */}
|
|
{displayResult.paramDeltas && displayResult.paramDeltas.length > 0 && (
|
|
<div className="text-[10px] font-mono space-y-0.5 pt-1 border-t border-theme-primary/20">
|
|
<span className="theme-secondary">TUNING:</span>
|
|
{displayResult.paramDeltas.slice(0, 4).map((d, i) => (
|
|
<div key={`${d.param}-${i}`} className="flex items-center gap-1 pl-2">
|
|
<span className="text-cyan-400">{PARAM_META[d.param].short}</span>
|
|
<span className="theme-secondary">
|
|
{d.before.toFixed(2)} → {d.after.toFixed(2)}
|
|
</span>
|
|
<span className={d.delta > 0 ? 'text-green-400' : 'text-red-400'}>
|
|
({d.delta > 0 ? '+' : ''}{d.delta.toFixed(2)})
|
|
</span>
|
|
<span className="text-purple-400">{d.reason}</span>
|
|
</div>
|
|
))}
|
|
{displayResult.paramDeltas.length > 4 && (
|
|
<div className="pl-2 theme-secondary">+{displayResult.paramDeltas.length - 4} more adjustments</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{/* Dataset Mode Warning Badge */}
|
|
{datasetGenerationEnabled && (
|
|
<div className="mb-2 flex items-center gap-2 px-3 py-1.5 bg-yellow-500/10 border border-yellow-500/30 rounded-lg text-xs text-yellow-500">
|
|
<AlertTriangle className="w-3 h-3 flex-shrink-0" />
|
|
<span className="font-semibold">DATASET MODE</span>
|
|
<span className="theme-secondary">— your prompts are being collected for public research. Avoid sharing personal info.</span>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex items-end gap-3">
|
|
<div className="flex-1 relative">
|
|
<textarea
|
|
ref={textareaRef}
|
|
value={input}
|
|
onChange={(e) => setInput(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder={(apiKey || proxyMode) ? "Enter your message... (Shift+Enter for new line)" : "Set your API key in Settings first"}
|
|
disabled={(!apiKey && !proxyMode) || isStreaming}
|
|
rows={1}
|
|
className="w-full px-4 py-3 pr-12 bg-theme-bg border border-theme-primary rounded-lg
|
|
resize-none focus:outline-none focus:glow-box
|
|
placeholder:theme-secondary disabled:opacity-50
|
|
transition-all duration-200"
|
|
style={{ minHeight: '48px', maxHeight: '200px' }}
|
|
/>
|
|
|
|
{/* Character count */}
|
|
{input.length > 0 && (
|
|
<div className="absolute right-3 bottom-3 text-xs theme-secondary">
|
|
{input.length}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Submit/Stop button */}
|
|
{isStreaming ? (
|
|
<button
|
|
onClick={handleStop}
|
|
className="p-3 bg-red-500/20 border border-red-500 rounded-lg
|
|
hover:bg-red-500/30 transition-all"
|
|
aria-label="Stop generation"
|
|
>
|
|
<StopCircle className="w-5 h-5 text-red-500" />
|
|
</button>
|
|
) : (
|
|
<button
|
|
onClick={handleSubmit}
|
|
disabled={!input.trim() || (!apiKey && !proxyMode)}
|
|
className="p-3 bg-theme-accent border border-theme-primary rounded-lg
|
|
hover:glow-box transition-all
|
|
disabled:opacity-50 disabled:cursor-not-allowed"
|
|
aria-label="Send message"
|
|
>
|
|
{isStreaming ? (
|
|
<Loader2 className="w-5 h-5 animate-spin" />
|
|
) : (
|
|
<Send className="w-5 h-5" />
|
|
)}
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{/* Status indicators */}
|
|
<div className="flex items-center justify-between mt-2 text-xs theme-secondary">
|
|
<div className="flex items-center gap-4">
|
|
{autoTuneEnabled && (
|
|
<button
|
|
onClick={() => setShowTuneDetails(!showTuneDetails)}
|
|
className={`flex items-center gap-1 transition-colors hover:text-cyan-400
|
|
${showTuneDetails ? 'text-cyan-400' : ''}`}
|
|
>
|
|
<SlidersHorizontal className="w-3 h-3 text-cyan-400" />
|
|
AutoTune {autoTuneStrategy === 'adaptive' && displayResult
|
|
? `[${getContextLabel(displayResult.detectedContext)}]`
|
|
: `[${getStrategyLabel(autoTuneStrategy)}]`
|
|
}
|
|
</button>
|
|
)}
|
|
{noLogMode && (
|
|
<span className="flex items-center gap-1">
|
|
<span className="text-yellow-500 text-[10px]">◈</span>
|
|
No-Log Mode
|
|
</span>
|
|
)}
|
|
{stmModules.some(m => m.enabled) && (
|
|
<span className="flex items-center gap-1">
|
|
<span className="text-purple-500 text-[10px]">⬣</span>
|
|
{stmModules.filter(m => m.enabled).length} STM Active
|
|
</span>
|
|
)}
|
|
{activeMemoryCount > 0 && (
|
|
<span className="flex items-center gap-1">
|
|
<span className="text-cyan-400 text-[10px]">✦</span>
|
|
{activeMemoryCount} Memories
|
|
</span>
|
|
)}
|
|
{parseltongueConfig.enabled && (
|
|
<span className={`flex items-center gap-1 ${parseltonguePreview ? 'text-green-400' : ''}`}>
|
|
<span className="text-green-500 text-[10px]">☡</span>
|
|
Parseltongue
|
|
{parseltonguePreview && ` [${parseltonguePreview.triggersFound.length} triggers]`}
|
|
</span>
|
|
)}
|
|
{ultraplinianEnabled && (
|
|
<span className="flex items-center gap-1 text-orange-400">
|
|
<span className="text-[10px]">⚔</span>
|
|
ULTRAPLINIAN [{ultraplinianTier}]
|
|
</span>
|
|
)}
|
|
</div>
|
|
{isStreaming && (
|
|
<span className="flex items-center gap-1">
|
|
<Loader2 className="w-3 h-3 animate-spin" />
|
|
{consortiumPhase === 'collecting'
|
|
? `Collecting ${consortiumModelsCollected}/${consortiumModelsTotal} models...`
|
|
: consortiumPhase === 'synthesizing'
|
|
? `Synthesizing ground truth...`
|
|
: ultraplinianRacing
|
|
? `Racing ${ultraplinianModelsResponded}/${ultraplinianModelsTotal} models${ultraplinianLiveModel ? ` // Leader: ${ultraplinianLiveModel.split('/').pop()} (${ultraplinianLiveScore})` : '...'}`
|
|
: autoTuneEnabled && autoTuneLastResult
|
|
? `Tuned @ T=${autoTuneLastResult.params.temperature.toFixed(2)}...`
|
|
: 'Thinking...'
|
|
}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|