Add LLM chat integration with streaming responses via Ollama

Phase 4: Full chat system with SSE streaming, note-aware context, and
conversation persistence.

Backend:
- Migration 0005: conversations + messages tables with FKs and indexes
- Conversation/Message SQLAlchemy models with relationships
- LLM service: ensure_model (auto-pull on startup), stream_chat (NDJSON),
  generate_completion, fetch_url_content (HTML stripping), build_context
  (keyword extraction, related note search, URL content injection)
- Chat service: conversation CRUD, save_response_as_note,
  summarize_conversation_as_note
- Chat routes blueprint: 9 endpoints including SSE streaming for messages,
  save/summarize as note, Ollama model listing
- Auto-pull llama3.1 model on app startup (non-blocking)

Frontend:
- apiStreamPost: SSE client using fetch + ReadableStream
- Chat Pinia store with streaming state management
- ChatView: dedicated /chat page with conversation sidebar + message thread
- ChatPanel: slide-out panel with contextNoteId from current route
- ChatMessage: markdown-rendered message bubble with "Save as Note" action
- Updated AppHeader with Chat nav link + panel toggle button
- Updated App.vue to mount ChatPanel with route-derived context
- Added /chat and /chat/:id routes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-10 18:45:22 -05:00
parent 807cde30be
commit d2b8ab8fe8
19 changed files with 1906 additions and 35 deletions
+21
View File
@@ -2,6 +2,10 @@
import { useTheme } from "@/composables/useTheme";
const { theme, toggleTheme } = useTheme();
const emit = defineEmits<{
toggleChatPanel: [];
}>();
</script>
<template>
@@ -11,6 +15,10 @@ const { theme, toggleTheme } = useTheme();
<div class="nav-links">
<router-link to="/notes" class="nav-link">Notes</router-link>
<router-link to="/tasks" class="nav-link">Tasks</router-link>
<router-link to="/chat" class="nav-link">Chat</router-link>
<button class="btn-chat-panel" @click="emit('toggleChatPanel')" title="Open chat panel">
&#x1F4AC;
</button>
<button class="theme-toggle" @click="toggleTheme" :title="`Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`">
{{ theme === "dark" ? "\u2600" : "\u263E" }}
</button>
@@ -51,6 +59,19 @@ const { theme, toggleTheme } = useTheme();
.nav-link:hover {
color: var(--color-primary);
}
.btn-chat-panel {
background: none;
border: 1px solid var(--color-border);
border-radius: 4px;
padding: 0.25rem 0.5rem;
cursor: pointer;
font-size: 1rem;
color: var(--color-text);
line-height: 1;
}
.btn-chat-panel:hover {
background: var(--color-bg-card);
}
.theme-toggle {
background: none;
border: 1px solid var(--color-border);
+113
View File
@@ -0,0 +1,113 @@
<script setup lang="ts">
import { computed } from "vue";
import { renderMarkdown } from "@/utils/markdown";
import type { Message } from "@/types/chat";
const props = defineProps<{
message: Message;
isStreaming?: boolean;
}>();
const emit = defineEmits<{
saveAsNote: [messageId: number];
}>();
const rendered = computed(() => renderMarkdown(props.message.content));
const roleLabel = computed(() => {
switch (props.message.role) {
case "user":
return "You";
case "assistant":
return "Assistant";
default:
return props.message.role;
}
});
</script>
<template>
<div class="chat-message" :class="`role-${message.role}`">
<div class="message-header">
<span class="role-label">{{ roleLabel }}</span>
<div class="message-actions" v-if="message.role === 'assistant' && !isStreaming">
<button class="btn-save" @click="emit('saveAsNote', message.id)" title="Save as note">
Save as Note
</button>
</div>
</div>
<div class="message-content prose" v-html="rendered"></div>
<div
v-if="message.context_note_id"
class="context-badge"
>
<router-link :to="`/notes/${message.context_note_id}`">
Note #{{ message.context_note_id }}
</router-link>
</div>
</div>
</template>
<style scoped>
.chat-message {
padding: 0.75rem 1rem;
border-radius: 8px;
margin-bottom: 0.5rem;
}
.role-user {
background: var(--color-bg-secondary);
}
.role-assistant {
background: var(--color-bg-card);
border: 1px solid var(--color-border);
}
.message-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 0.25rem;
}
.role-label {
font-size: 0.8rem;
font-weight: 600;
color: var(--color-text-muted);
text-transform: uppercase;
}
.message-content {
font-size: 0.95rem;
line-height: 1.5;
word-break: break-word;
}
.message-content :deep(p:last-child) {
margin-bottom: 0;
}
.message-actions {
display: flex;
gap: 0.5rem;
}
.btn-save {
font-size: 0.75rem;
padding: 0.15rem 0.5rem;
background: transparent;
border: 1px solid var(--color-border);
border-radius: 4px;
color: var(--color-text-secondary);
cursor: pointer;
}
.btn-save:hover {
background: var(--color-primary);
color: #fff;
border-color: var(--color-primary);
}
.context-badge {
margin-top: 0.5rem;
font-size: 0.8rem;
}
.context-badge a {
color: var(--color-primary);
text-decoration: none;
}
.context-badge a:hover {
text-decoration: underline;
}
</style>
+273
View File
@@ -0,0 +1,273 @@
<script setup lang="ts">
import { ref, computed, watch, nextTick } from "vue";
import { useChatStore } from "@/stores/chat";
import { renderMarkdown } from "@/utils/markdown";
import ChatMessage from "@/components/ChatMessage.vue";
const props = defineProps<{
contextNoteId?: number | null;
}>();
const emit = defineEmits<{
close: [];
}>();
const store = useChatStore();
const messageInput = ref("");
const messagesEl = ref<HTMLElement | null>(null);
const streamingRendered = computed(() => {
if (!store.streamingContent) return "";
return renderMarkdown(store.streamingContent);
});
watch(
() => store.streamingContent,
() => scrollToBottom()
);
function scrollToBottom() {
nextTick(() => {
if (messagesEl.value) {
messagesEl.value.scrollTop = messagesEl.value.scrollHeight;
}
});
}
async function sendMessage() {
const content = messageInput.value.trim();
if (!content || store.streaming) return;
// Auto-create conversation if none active
if (!store.currentConversation) {
const conv = await store.createConversation();
await store.fetchConversation(conv.id);
}
messageInput.value = "";
scrollToBottom();
await store.sendMessage(content, props.contextNoteId);
scrollToBottom();
}
async function handleSaveAsNote(messageId: number) {
try {
await store.saveMessageAsNote(messageId);
const { useToastStore } = await import("@/stores/toast");
useToastStore().show("Saved as note");
} catch {
const { useToastStore } = await import("@/stores/toast");
useToastStore().show("Failed to save as note", "error");
}
}
function onInputKeydown(e: KeyboardEvent) {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
}
</script>
<template>
<div class="chat-panel-overlay" @click.self="emit('close')">
<aside class="chat-panel">
<div class="panel-header">
<h3>Chat</h3>
<span v-if="contextNoteId" class="context-indicator">
Note #{{ contextNoteId }}
</span>
<button class="btn-close" @click="emit('close')">&times;</button>
</div>
<div ref="messagesEl" class="panel-messages">
<template v-if="store.currentConversation">
<ChatMessage
v-for="msg in store.currentConversation.messages"
:key="msg.id"
:message="msg"
@save-as-note="handleSaveAsNote"
/>
</template>
<div v-if="store.streaming" class="chat-message role-assistant streaming">
<div class="message-header">
<span class="role-label">Assistant</span>
</div>
<div class="message-content prose" v-html="streamingRendered"></div>
<span class="typing-indicator"></span>
</div>
<p
v-if="!store.currentConversation?.messages.length && !store.streaming"
class="empty-msg"
>
Start chatting...
</p>
</div>
<div class="panel-input">
<textarea
v-model="messageInput"
@keydown="onInputKeydown"
placeholder="Type a message..."
:disabled="store.streaming"
rows="2"
></textarea>
<button
class="btn-send"
@click="sendMessage"
:disabled="!messageInput.trim() || store.streaming"
>
Send
</button>
</div>
</aside>
</div>
</template>
<style scoped>
.chat-panel-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.3);
z-index: 100;
display: flex;
justify-content: flex-end;
}
.chat-panel {
width: 400px;
max-width: 100vw;
height: 100%;
background: var(--color-bg);
display: flex;
flex-direction: column;
box-shadow: -2px 0 8px rgba(0, 0, 0, 0.15);
}
.panel-header {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1rem;
border-bottom: 1px solid var(--color-border);
}
.panel-header h3 {
margin: 0;
font-size: 1rem;
flex: 1;
}
.context-indicator {
font-size: 0.8rem;
color: var(--color-primary);
background: var(--color-bg-secondary);
padding: 0.15rem 0.5rem;
border-radius: 4px;
}
.btn-close {
background: none;
border: none;
font-size: 1.5rem;
cursor: pointer;
color: var(--color-text-muted);
line-height: 1;
padding: 0 0.25rem;
}
.btn-close:hover {
color: var(--color-text);
}
.panel-messages {
flex: 1;
overflow-y: auto;
padding: 0.75rem;
}
.streaming {
border: 1px solid var(--color-border);
border-radius: 8px;
padding: 0.75rem 1rem;
background: var(--color-bg-card);
margin-bottom: 0.5rem;
}
.streaming .message-header {
display: flex;
align-items: center;
margin-bottom: 0.25rem;
}
.streaming .role-label {
font-size: 0.8rem;
font-weight: 600;
color: var(--color-text-muted);
text-transform: uppercase;
}
.streaming .message-content {
font-size: 0.95rem;
line-height: 1.5;
word-break: break-word;
}
.typing-indicator {
display: inline-block;
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--color-primary);
animation: blink 1s infinite;
margin-left: 4px;
vertical-align: middle;
}
@keyframes blink {
0%, 100% { opacity: 0.3; }
50% { opacity: 1; }
}
.panel-input {
display: flex;
gap: 0.5rem;
padding: 0.75rem;
border-top: 1px solid var(--color-border);
background: var(--color-bg-secondary);
}
.panel-input textarea {
flex: 1;
resize: none;
padding: 0.5rem;
border: 1px solid var(--color-border);
border-radius: 6px;
font-family: inherit;
font-size: 0.9rem;
background: var(--color-bg);
color: var(--color-text);
}
.panel-input textarea:focus {
outline: none;
border-color: var(--color-primary);
}
.btn-send {
padding: 0.5rem 1rem;
background: var(--color-primary);
color: #fff;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 0.85rem;
align-self: flex-end;
}
.btn-send:disabled {
opacity: 0.5;
cursor: default;
}
.empty-msg {
color: var(--color-text-muted);
font-size: 0.9rem;
text-align: center;
padding: 2rem 1rem;
}
@media (max-width: 480px) {
.chat-panel {
width: 100vw;
}
}
</style>