Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312<script setup lang="ts">import type { ChatMessage as ChatMessageRecord, ChatToolRun } from "@typbase/typing";
import { summarizeDiagnostics } from "~/lib/ai/verify";
const props = defineProps<{ message: ChatMessageRecord; /** HTML from the last progressive/final render, when there is one. */ html?: string; tools: ChatToolRun[]; streaming: boolean;}>();
const emit = defineEmits<{ (e: "insert"): void; (e: "createPage"): void; (e: "repair"): void;}>();
const { t } = useI18n();const sourceOpen = ref(false);const toolsOpen = ref(false);const toolNames = computed(() => props.tools .map((tool) => tool.name) .join(", ") .slice(0, 80),);
const isAssistant = computed(() => props.message.role === "assistant");const hasSource = computed(() => props.message.source.trim().length > 0);const busy = computed( () => props.message.status === "streaming" || props.message.status === "verifying",);const problem = computed( () => props.message.status === "unverified" || props.message.status === "error",);
const statusLabel = computed(() => { switch (props.message.status) { case "streaming": return t("chat.statusStreaming"); case "verifying": return t("chat.statusVerifying"); case "repairing": return t("chat.statusRepairing"); case "verified": return t("chat.statusVerified"); case "unverified": return t("chat.statusUnverified"); case "aborted": return t("chat.statusAborted"); case "error": return t("chat.statusError"); default: return ""; }});
async function copySource(): Promise<void> { try { await navigator.clipboard.writeText(props.message.source); } catch { // Clipboard permissions are optional. The source toggle still shows it. }}</script>
<template> <article v-if="message.role === 'user'" class="chat-message chat-message--user" :data-status="message.status" > <p class="chat-message__text">{{ message.source }}</p> </article>
<article v-else class="chat-message chat-message--assistant" :data-status="message.status"> <UiBadge v-if="message.repairOf" class="chat-message__badge"> {{ $t("chat.repairBadge") }} </UiBadge>
<div v-if="html" class="chat-message__render" v-html="html" />
<div v-else-if="!hasSource && busy" class="chat-message__thinking" role="status"> <span class="chat-message__spinner" aria-hidden="true" /> <span>{{ statusLabel }}</span> </div>
<pre v-else-if="hasSource" class="chat-message__raw">{{ message.source }}</pre>
<UiCollapsible v-if="tools.length" v-model:open="toolsOpen" class="chat-message__tools" :chevron="false" > <template #trigger> {{ $t("chat.toolsUsed", { count: tools.length }) }} <span v-if="toolNames"> · {{ toolNames }}</span> </template> <div v-for="tool in tools" :key="tool.id" class="chat-message__tool"> <code>{{ tool.name }}</code> <pre>{{ tool.output }}</pre> </div> </UiCollapsible>
<div v-if="message.error" class="chat-message__error" role="alert"> {{ message.error }} </div>
<ul v-if="problem && message.diagnostics.length" class="chat-message__diagnostics"> <li v-for="(diagnostic, index) in message.diagnostics.slice(0, 6)" :key="index"> {{ diagnostic.message }} </li> </ul>
<div class="chat-message__footer"> <span class="chat-message__status" :data-status="message.status"> <span v-if="busy" class="chat-message__spinner" aria-hidden="true" /> <MsIcon v-else-if="message.status === 'verified'" name="check_circle" :size="14" aria-hidden="true" /> <MsIcon v-else-if="problem" name="error" :size="14" aria-hidden="true" /> <MsIcon v-else name="stop_circle" :size="14" aria-hidden="true" /> {{ statusLabel }} <template v-if="message.status === 'unverified' && message.diagnostics.length"> · {{ summarizeDiagnostics(message.diagnostics) }} </template> </span>
<span class="chat-message__spacer" />
<template v-if="isAssistant && hasSource && !streaming"> <UiButton size="small" variant="ghost" @click="sourceOpen = !sourceOpen"> {{ sourceOpen ? $t("chat.hideSource") : $t("chat.showSource") }} </UiButton> <UiButton size="small" variant="ghost" @click="copySource"> {{ $t("chat.copy") }} </UiButton> <UiButton v-if="problem" size="small" variant="ghost" @click="emit('repair')"> {{ $t("chat.repair") }} </UiButton> <UiButton size="small" variant="ghost" @click="emit('insert')"> {{ $t("chat.insert") }} </UiButton> <UiButton size="small" variant="ghost" @click="emit('createPage')"> {{ $t("chat.createPage") }} </UiButton> </template> </div>
<pre v-if="sourceOpen" class="chat-message__source">{{ message.source }}</pre> </article></template>
<style>.chat-message { display: flex; flex-direction: column; gap: var(--space-2); padding: var(--space-3) var(--space-3-5); border-radius: var(--radius-md); overflow-wrap: anywhere;}
.chat-message--user { align-self: flex-end; max-width: min(85%, 42rem); background: var(--color-accent-soft); border: 1px solid color-mix(in srgb, var(--color-accent) 25%, transparent);}
.chat-message--assistant { align-self: stretch; background: var(--color-surface-2); border: 1px solid var(--color-border);}
.chat-message__text { margin: 0; white-space: pre-wrap;}
.chat-message__render > * + * { margin-top: 0.6em;}
.chat-message__render ul,.chat-message__render ol { padding-inline-start: 1.4em;}
.chat-message__render pre { padding: var(--space-2); overflow: auto; background: var(--color-code); border-radius: var(--radius-sm);}
.chat-message__render a { color: var(--color-accent);}
.chat-message__raw,.chat-message__source { margin: 0; padding: var(--space-2); overflow: auto; font-family: var(--font-mono); font-size: var(--text-sm); white-space: pre-wrap; background: var(--color-code); border-radius: var(--radius-sm);}
.chat-message__source { max-height: 20rem;}
.chat-message__thinking { display: inline-flex; align-items: center; gap: var(--space-2); color: var(--color-text-secondary); font-size: var(--text-sm);}
.chat-message__spinner { width: 0.85rem; height: 0.85rem; flex: none; border: 2px solid color-mix(in srgb, var(--color-accent) 25%, transparent); border-top-color: var(--color-accent); border-radius: 50%; animation: chat-spin 0.7s linear infinite;}
@keyframes chat-spin { to { transform: rotate(360deg); }}
.chat-message__badge { align-self: flex-start;}
.chat-message__tools { font-size: var(--text-sm); color: var(--color-text-secondary);}
.chat-message__tool { margin-top: var(--space-1);}
.chat-message__tool pre { max-height: 12rem; margin: var(--space-1) 0 0; padding: var(--space-1-5); overflow: auto; font-family: var(--font-mono); font-size: var(--text-xs); white-space: pre-wrap; background: var(--color-code); border-radius: var(--radius-xs);}
.chat-message__error { padding: var(--space-1-5) var(--space-2); font-size: var(--text-sm); color: var(--color-danger); background: var(--color-danger-soft); border-radius: var(--radius-sm);}
.chat-message__diagnostics { margin: 0; padding-left: var(--space-4); font-size: var(--text-sm); color: var(--color-danger);}
.chat-message__footer { display: flex; align-items: center; gap: var(--space-1); flex-wrap: wrap;}
.chat-message__spacer { flex: 1;}
.chat-message__status { display: inline-flex; align-items: center; gap: var(--space-1); font-size: var(--text-xs); color: var(--color-text-secondary);}
.chat-message__status[data-status="unverified"],.chat-message__status[data-status="error"] { color: var(--color-danger);}</style>