turnstone/web/src/components/QuickCapture.vue

266 lines
9 KiB
Vue

<template>
<div>
<!-- Input row -->
<div class="flex gap-3 mb-6">
<input
v-model="query"
type="text"
placeholder='e.g. "plex stopped playing audio around 2pm"'
class="flex-1 bg-surface-raised border border-surface-border rounded px-4 py-2.5 text-sm text-text-primary placeholder-text-dim focus:outline-none focus:border-accent transition-colors"
@keydown.enter="run()"
/>
<button
:disabled="loading || !query.trim()"
@click="run()"
class="px-6 py-2.5 rounded bg-accent text-white text-sm font-semibold hover:bg-blue-400 transition-colors disabled:opacity-50"
>
<span v-if="loading">Searching</span>
<span v-else>Go</span>
</button>
</div>
<!-- Error -->
<div v-if="error" class="mb-4 p-3 rounded bg-red-900/30 border border-red-700/40 text-sev-error text-sm">
{{ error }}
</div>
<!-- Summary header -->
<div v-if="summary" class="mb-4 rounded border border-surface-border bg-surface-raised p-4">
<div class="flex flex-wrap gap-x-6 gap-y-1 text-xs text-text-dim">
<span class="text-text-muted font-medium">{{ summary.total }} entr{{ summary.total !== 1 ? 'ies' : 'y' }}</span>
<span v-if="summary.window_start">
{{ fmtTs(summary.window_start) }} {{ fmtTs(summary.window_end) }}
</span>
<span v-if="!summary.time_detected" class="italic">(last 60 min no time detected)</span>
<span
v-for="(count, sev) in nonZeroSeverity"
:key="String(sev)"
:class="sevClass(String(sev))"
>{{ count }} {{ sev }}</span>
</div>
<div class="flex flex-wrap gap-x-4 gap-y-1 text-xs text-text-dim mt-2">
<span v-for="(count, src) in summary.by_source" :key="String(src)">
{{ String(src).split(':').pop() }} ({{ count }})
</span>
</div>
</div>
<!-- LLM reasoning card -->
<div
v-if="reasoning"
class="mb-4 rounded border border-accent/30 bg-accent/5 p-4"
>
<div class="flex items-center gap-2 mb-2 text-xs text-text-dim font-medium uppercase tracking-wide">
<span></span>
<span>Diagnosis</span>
</div>
<p class="text-sm text-text-primary leading-relaxed whitespace-pre-wrap">{{ reasoning }}</p>
</div>
<!-- Log stream -->
<div v-if="entries.length" class="rounded border border-surface-border overflow-hidden mb-4">
<LogEntryRow v-for="entry in entries" :key="entry.entry_id" :entry="entry" />
</div>
<!-- Zero state -->
<div v-else-if="ranOnce && !loading" class="text-center text-text-dim py-12">
<p class="mb-1">No log evidence found for "{{ lastQuery }}"</p>
<p class="text-sm">Check the Sources tab to confirm data is ingested, or try a broader description.</p>
</div>
<!-- Save CTAs -->
<div v-if="entries.length && !saved" class="mt-2 flex items-center gap-4">
<button
@click="saveQuick"
:disabled="saving"
class="px-4 py-2 bg-accent text-surface text-sm rounded font-medium hover:opacity-90 transition-opacity disabled:opacity-40"
>
{{ saving ? 'Saving…' : 'Save as incident' }}
</button>
<button
v-if="!showDetails"
@click="showDetails = true"
class="text-text-dim hover:text-text-primary text-sm underline underline-offset-2"
>
add details
</button>
</div>
<!-- Inline details panel -->
<div v-if="showDetails && !saved" class="mt-4 rounded border border-surface-border bg-surface-raised p-4 space-y-3">
<div class="flex flex-wrap gap-3">
<div>
<label class="block text-xs text-text-dim mb-1">Severity</label>
<select
v-model="detailSeverity"
class="bg-surface border border-surface-border rounded px-3 py-1.5 text-sm text-text-primary focus:outline-none focus:border-accent"
>
<option value="low">Low</option>
<option value="medium">Medium</option>
<option value="high">High</option>
<option value="critical">Critical</option>
</select>
</div>
<div class="flex-1 min-w-48">
<label class="block text-xs text-text-dim mb-1">Notes <span class="text-text-dim">(optional)</span></label>
<input
v-model="detailNotes"
type="text"
placeholder="Any extra context…"
class="w-full bg-surface border border-surface-border rounded px-3 py-1.5 text-sm text-text-primary placeholder-text-dim focus:outline-none focus:border-accent"
/>
</div>
</div>
<div class="flex gap-3">
<button
@click="saveWithDetails"
:disabled="saving"
class="px-4 py-2 bg-accent text-surface text-sm rounded font-medium hover:opacity-90 transition-opacity disabled:opacity-40"
>
{{ saving ? 'Saving…' : 'Save' }}
</button>
<button @click="showDetails = false" class="text-text-dim hover:text-text-primary text-sm">Cancel</button>
</div>
</div>
<!-- Saved confirmation -->
<div v-if="saved" class="mt-2 text-sm text-green-400">
Saved
<RouterLink to="/incidents" class="underline underline-offset-2 hover:text-green-300">view in Incidents</RouterLink>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, RouterLink } from 'vue-router'
import LogEntryRow from '@/components/LogEntryRow.vue'
import type { LogEntry } from '@/stores/search'
const BASE = import.meta.env.BASE_URL.replace(/\/$/, '')
const route = useRoute()
interface Summary {
total: number
window_start: string | null
window_end: string | null
time_detected: boolean
by_severity: Record<string, number>
by_source: Record<string, number>
}
const query = ref('')
const entries = ref<LogEntry[]>([])
const summary = ref<Summary | null>(null)
const reasoning = ref<string | null>(null)
const loading = ref(false)
const error = ref<string | null>(null)
const ranOnce = ref(false)
const lastQuery = ref('')
const saved = ref(false)
const saving = ref(false)
const showDetails = ref(false)
const detailSeverity = ref('medium')
const detailNotes = ref('')
let capturedSince: string | null = null
let capturedUntil: string | null = null
onMounted(() => {
const q = route.query.q
if (typeof q === 'string' && q.trim()) {
query.value = q
run()
}
})
watch(() => route.query.q, (newQ) => {
if (typeof newQ === 'string' && newQ.trim() && newQ !== lastQuery.value) {
query.value = newQ
run()
}
})
async function run() {
if (!query.value.trim()) return
loading.value = true
error.value = null
ranOnce.value = true
lastQuery.value = query.value
saved.value = false
showDetails.value = false
try {
const res = await fetch(`${BASE}/api/diagnose`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: query.value }),
})
if (!res.ok) throw new Error(`API returned ${res.status}`)
const data = await res.json()
entries.value = data.entries
summary.value = data.summary
reasoning.value = data.reasoning ?? null
capturedSince = data.summary.window_start
capturedUntil = data.summary.window_end
} catch (e) {
error.value = e instanceof Error ? e.message : String(e)
} finally {
loading.value = false
}
}
async function saveQuick() {
saving.value = true
try { await postIncident('medium', '') }
catch (e) { error.value = e instanceof Error ? e.message : 'Failed to save incident' }
finally { saving.value = false }
}
async function saveWithDetails() {
saving.value = true
try { await postIncident(detailSeverity.value, detailNotes.value) }
catch (e) { error.value = e instanceof Error ? e.message : 'Failed to save incident' }
finally { saving.value = false }
}
async function postIncident(severity: string, notes: string) {
const res = await fetch(`${BASE}/api/incidents`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
label: lastQuery.value,
started_at: capturedSince,
ended_at: capturedUntil,
severity,
notes,
}),
})
if (!res.ok) throw new Error(await res.text())
saved.value = true
showDetails.value = false
}
const nonZeroSeverity = computed(() => {
if (!summary.value) return {}
return Object.fromEntries(
Object.entries(summary.value.by_severity).filter(([, v]) => v > 0)
)
})
function sevClass(sev: string): string {
return ({
CRITICAL: 'text-sev-critical',
ERROR: 'text-sev-error',
WARN: 'text-sev-warn',
INFO: 'text-sev-info',
} as Record<string, string>)[sev] ?? 'text-text-dim'
}
function fmtTs(iso: string | null): string {
if (!iso) return '—'
try {
return new Date(iso).toLocaleString(undefined, {
month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit',
})
} catch { return iso }
}
</script>