mirror of
https://github.com/elder-plinius/G0DM0D3.git
synced 2026-11-16 07:02:25 +00:00
Add files via upload
This commit is contained in:
888
ChatInput.tsx
Normal file
888
ChatInput.tsx
Normal file
@@ -0,0 +1,888 @@
|
||||
'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>
|
||||
)
|
||||
}
|
||||
2536
SettingsModal.tsx
Normal file
2536
SettingsModal.tsx
Normal file
File diff suppressed because it is too large
Load Diff
30
TERMS.md
30
TERMS.md
@@ -112,13 +112,15 @@ This runs in your browser after every LLM request. It sends structural metadata
|
||||
|
||||
### Tier 3: Opt-In Dataset Collection (Only When You Explicitly Enable It)
|
||||
|
||||
This tier **only activates** when you set `contribute_to_dataset: true` in your API request. It exists to build an open-source research dataset. If you do not set this flag, none of this data is collected.
|
||||
> **WARNING: This tier collects your full conversation content (prompts and responses) and publishes it to a PUBLIC HuggingFace dataset that anyone can download, redistribute, and use for any purpose. Only enable this if you understand and accept this.**
|
||||
|
||||
This tier **only activates** when you explicitly enable "Dataset Generation" in Settings → Privacy, or set `contribute_to_dataset: true` in your API request. It exists to build an open-source research dataset for studying how different models respond to steering primitives. If you do not enable this, none of this data is collected.
|
||||
|
||||
#### What IS collected (only when opted in)
|
||||
|
||||
| Data Point | Description |
|
||||
|---|---|
|
||||
| Messages sent and received | **Actual conversation content** -- your prompts and the model's responses |
|
||||
| Messages sent and received | **Actual conversation content** -- your prompts and the model's responses. **This data is PUBLIC.** |
|
||||
| Model and endpoint | Which model and API endpoint were used |
|
||||
| Mode | Standard or ULTRAPLINIAN |
|
||||
| AutoTune details | Strategy, detected context, confidence, full parameter set, reasoning |
|
||||
@@ -127,6 +129,28 @@ This tier **only activates** when you set `contribute_to_dataset: true` in your
|
||||
| ULTRAPLINIAN details | Tier, full list of models queried, winner model, all scores/durations, total duration |
|
||||
| Feedback | User ratings (thumbs up/down) and heuristics (response length, repetition score, vocabulary diversity) |
|
||||
|
||||
#### Automatic PII Scrubbing
|
||||
|
||||
All dataset entries pass through an automatic PII scrubber before being stored. The scrubber detects and redacts:
|
||||
|
||||
- Email addresses
|
||||
- Phone numbers (US and international formats)
|
||||
- Social Security Numbers (US)
|
||||
- Credit card numbers
|
||||
- IPv4 and IPv6 addresses
|
||||
- API keys and bearer tokens (common patterns like `sk-`, `pk-`, `AKIA`)
|
||||
|
||||
**Automated scrubbing is best-effort and NOT guaranteed to catch all PII.** It will not catch names, physical addresses, or PII in non-standard formats. **You are responsible for not including sensitive personal information in your prompts when Dataset Mode is enabled.**
|
||||
|
||||
#### What you should NEVER include when Dataset Mode is on
|
||||
|
||||
- Real names (yours or anyone else's)
|
||||
- Physical addresses or location data
|
||||
- Passwords, private keys, or credentials
|
||||
- Financial account numbers
|
||||
- Medical records or health information
|
||||
- Any information that could identify a real person
|
||||
|
||||
#### What is NEVER collected (even when opted in)
|
||||
|
||||
| Excluded Data | Why |
|
||||
@@ -138,7 +162,7 @@ This tier **only activates** when you set `contribute_to_dataset: true` in your
|
||||
|
||||
**Storage:** In-memory buffer (10,000 entries max, FIFO eviction). Auto-publishes to HuggingFace when buffer reaches 80% capacity.
|
||||
|
||||
**Deletion:** You can delete your contributed entries at any time via `DELETE /v1/dataset/:id`. The entry ID is returned when you submit data.
|
||||
**Deletion:** You can delete your contributed entries at any time via `DELETE /v1/dataset/:id` while data remains in the server's memory buffer. **Once data has been published to HuggingFace, it is public and may be cached, forked, or redistributed by third parties.** Post-publication deletion requests must be directed to the HuggingFace repository maintainers, and full removal cannot be guaranteed.
|
||||
|
||||
---
|
||||
|
||||
|
||||
231
dataset.ts
Normal file
231
dataset.ts
Normal file
@@ -0,0 +1,231 @@
|
||||
/**
|
||||
* Open Dataset Collection Engine
|
||||
*
|
||||
* Opt-in data collection for building an open source research dataset.
|
||||
* Researchers who enable `contribute_to_dataset: true` in their requests
|
||||
* have their (anonymized) interaction data stored for the community.
|
||||
*
|
||||
* Stored data:
|
||||
* - Messages sent and received (no API keys, no IPs)
|
||||
* - AutoTune parameters and context detection results
|
||||
* - Model used and response metadata
|
||||
* - User feedback/ratings
|
||||
* - Parseltongue and STM pipeline metadata
|
||||
*
|
||||
* Privacy guarantees:
|
||||
* - Strictly opt-in per request
|
||||
* - No PII: API keys, IPs, and auth tokens are NEVER stored
|
||||
* - Dataset is exportable via GET /v1/dataset/export
|
||||
* - Caller can request deletion via DELETE /v1/dataset/:id
|
||||
*
|
||||
* Persistence: auto-publishes to HuggingFace when buffer fills up.
|
||||
*/
|
||||
|
||||
import { randomUUID } from 'crypto'
|
||||
import { registerDatasetStore, checkDatasetThreshold } from './hf-publisher'
|
||||
|
||||
// ── Types ────────────────────────────────────────────────────────────
|
||||
|
||||
export interface DatasetEntry {
|
||||
id: string
|
||||
timestamp: number
|
||||
|
||||
// Request metadata
|
||||
endpoint: string // which API endpoint was called
|
||||
model: string
|
||||
mode: 'standard' | 'ultraplinian'
|
||||
|
||||
// Messages (stripped of system prompts to avoid leaking custom prompts)
|
||||
messages: Array<{ role: string; content: string }>
|
||||
response: string
|
||||
|
||||
// AutoTune data
|
||||
autotune?: {
|
||||
strategy: string
|
||||
detected_context: string
|
||||
confidence: number
|
||||
params: Record<string, number>
|
||||
reasoning: string
|
||||
}
|
||||
|
||||
// Parseltongue data
|
||||
parseltongue?: {
|
||||
triggers_found: string[]
|
||||
technique_used: string
|
||||
transformations_count: number
|
||||
}
|
||||
|
||||
// STM data
|
||||
stm?: {
|
||||
modules_applied: string[]
|
||||
}
|
||||
|
||||
// ULTRAPLINIAN race data
|
||||
ultraplinian?: {
|
||||
tier: string
|
||||
models_queried: string[]
|
||||
winner_model: string
|
||||
all_scores: Array<{ model: string; score: number; duration_ms: number; success: boolean }>
|
||||
total_duration_ms: number
|
||||
}
|
||||
|
||||
// Feedback (added later via POST /v1/feedback if user rates)
|
||||
feedback?: {
|
||||
rating: 1 | -1
|
||||
heuristics?: {
|
||||
response_length: number
|
||||
repetition_score: number
|
||||
vocabulary_diversity: number
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── In-Memory Store with Auto-Publish ────────────────────────────────
|
||||
// Buffer auto-flushes to HuggingFace when it hits 80% capacity.
|
||||
// Falls back to FIFO eviction if HF publishing is not configured.
|
||||
|
||||
let dataset: DatasetEntry[] = []
|
||||
const MAX_ENTRIES = 10000 // Cap to prevent unbounded memory growth
|
||||
|
||||
// Track how many entries have been flushed so we can use index-based draining
|
||||
// instead of copying the entire array on each snapshot.
|
||||
let datasetFlushIndex = 0
|
||||
|
||||
// Register with HF publisher so it can snapshot/clear our buffer
|
||||
registerDatasetStore({
|
||||
snapshot: () => dataset.slice(datasetFlushIndex),
|
||||
clear: (count: number) => {
|
||||
datasetFlushIndex += count
|
||||
// Compact the array when more than half has been drained to free memory
|
||||
if (datasetFlushIndex > dataset.length / 2) {
|
||||
dataset = dataset.slice(datasetFlushIndex)
|
||||
datasetFlushIndex = 0
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
// ── PII Scrubber ────────────────────────────────────────────────────
|
||||
// Best-effort removal of common PII patterns before data hits the buffer.
|
||||
// Not perfect — users are warned to avoid submitting PII regardless.
|
||||
|
||||
const PII_PATTERNS: Array<{ pattern: RegExp; replacement: string }> = [
|
||||
// Email addresses
|
||||
{ pattern: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g, replacement: '[EMAIL_REDACTED]' },
|
||||
// Phone numbers (US and international formats)
|
||||
{ pattern: /(?:\+?1[-.\s]?)?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}/g, replacement: '[PHONE_REDACTED]' },
|
||||
{ pattern: /\+[0-9]{1,3}[-.\s]?[0-9]{4,14}/g, replacement: '[PHONE_REDACTED]' },
|
||||
// SSN (US)
|
||||
{ pattern: /\b[0-9]{3}[-\s]?[0-9]{2}[-\s]?[0-9]{4}\b/g, replacement: '[SSN_REDACTED]' },
|
||||
// Credit card numbers (13-19 digits with optional separators)
|
||||
{ pattern: /\b(?:[0-9]{4}[-\s]?){3,4}[0-9]{1,4}\b/g, replacement: '[CC_REDACTED]' },
|
||||
// IPv4 addresses
|
||||
{ pattern: /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/g, replacement: '[IP_REDACTED]' },
|
||||
// IPv6 addresses (simplified)
|
||||
{ pattern: /\b(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b/g, replacement: '[IP_REDACTED]' },
|
||||
// API keys (common patterns: sk-, pk-, key-, bearer tokens)
|
||||
{ pattern: /\b(?:sk|pk|api[_-]?key)[_-][a-zA-Z0-9]{20,}\b/gi, replacement: '[APIKEY_REDACTED]' },
|
||||
{ pattern: /\bBearer\s+[a-zA-Z0-9._-]{20,}\b/g, replacement: 'Bearer [TOKEN_REDACTED]' },
|
||||
// AWS access keys
|
||||
{ pattern: /\bAKIA[0-9A-Z]{16}\b/g, replacement: '[AWS_KEY_REDACTED]' },
|
||||
]
|
||||
|
||||
function scrubPII(text: string): string {
|
||||
let result = text
|
||||
for (const { pattern, replacement } of PII_PATTERNS) {
|
||||
// Reset lastIndex for global regexes
|
||||
pattern.lastIndex = 0
|
||||
result = result.replace(pattern, replacement)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function scrubEntryPII(entry: Omit<DatasetEntry, 'id' | 'timestamp'>): Omit<DatasetEntry, 'id' | 'timestamp'> {
|
||||
return {
|
||||
...entry,
|
||||
messages: entry.messages.map(m => ({
|
||||
...m,
|
||||
content: typeof m.content === 'string' ? scrubPII(m.content) : m.content,
|
||||
})),
|
||||
response: scrubPII(entry.response),
|
||||
}
|
||||
}
|
||||
|
||||
// ── Public API ───────────────────────────────────────────────────────
|
||||
|
||||
export function addEntry(entry: Omit<DatasetEntry, 'id' | 'timestamp'>): string {
|
||||
const id = randomUUID()
|
||||
const scrubbed = scrubEntryPII(entry)
|
||||
const record: DatasetEntry = {
|
||||
...scrubbed,
|
||||
id,
|
||||
timestamp: Date.now(),
|
||||
}
|
||||
|
||||
dataset.push(record)
|
||||
|
||||
// Auto-flush to HF when approaching capacity (async, non-blocking)
|
||||
checkDatasetThreshold(dataset.length, MAX_ENTRIES)
|
||||
|
||||
// Evict oldest entries if over cap (fallback if HF not configured or failed)
|
||||
if (dataset.length > MAX_ENTRIES) {
|
||||
dataset = dataset.slice(dataset.length - MAX_ENTRIES)
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
export function addFeedbackToEntry(
|
||||
entryId: string,
|
||||
feedback: DatasetEntry['feedback'],
|
||||
): boolean {
|
||||
const entry = dataset.find(e => e.id === entryId)
|
||||
if (!entry) return false
|
||||
entry.feedback = feedback
|
||||
return true
|
||||
}
|
||||
|
||||
export function deleteEntry(id: string): boolean {
|
||||
const idx = dataset.findIndex(e => e.id === id)
|
||||
if (idx === -1) return false
|
||||
dataset.splice(idx, 1)
|
||||
return true
|
||||
}
|
||||
|
||||
export function getDataset(): DatasetEntry[] {
|
||||
return [...dataset]
|
||||
}
|
||||
|
||||
export function getDatasetStats(): {
|
||||
total_entries: number
|
||||
entries_with_feedback: number
|
||||
mode_breakdown: Record<string, number>
|
||||
model_breakdown: Record<string, number>
|
||||
context_breakdown: Record<string, number>
|
||||
oldest_entry: number | null
|
||||
newest_entry: number | null
|
||||
} {
|
||||
const modeBreakdown: Record<string, number> = {}
|
||||
const modelBreakdown: Record<string, number> = {}
|
||||
const contextBreakdown: Record<string, number> = {}
|
||||
let withFeedback = 0
|
||||
|
||||
for (const entry of dataset) {
|
||||
modeBreakdown[entry.mode] = (modeBreakdown[entry.mode] || 0) + 1
|
||||
modelBreakdown[entry.model] = (modelBreakdown[entry.model] || 0) + 1
|
||||
if (entry.autotune?.detected_context) {
|
||||
const ctx = entry.autotune.detected_context
|
||||
contextBreakdown[ctx] = (contextBreakdown[ctx] || 0) + 1
|
||||
}
|
||||
if (entry.feedback) withFeedback++
|
||||
}
|
||||
|
||||
return {
|
||||
total_entries: dataset.length,
|
||||
entries_with_feedback: withFeedback,
|
||||
mode_breakdown: modeBreakdown,
|
||||
model_breakdown: modelBreakdown,
|
||||
context_breakdown: contextBreakdown,
|
||||
oldest_entry: dataset.length > 0 ? dataset[0].timestamp : null,
|
||||
newest_entry: dataset.length > 0 ? dataset[dataset.length - 1].timestamp : null,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user