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:
+26
-1
@@ -1,13 +1,38 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
import AppHeader from "@/components/AppHeader.vue";
|
||||
import ChatPanel from "@/components/ChatPanel.vue";
|
||||
import ToastNotification from "@/components/ToastNotification.vue";
|
||||
import { useTheme } from "@/composables/useTheme";
|
||||
|
||||
useTheme();
|
||||
|
||||
const route = useRoute();
|
||||
const chatPanelOpen = ref(false);
|
||||
|
||||
const contextNoteId = computed(() => {
|
||||
const id = route.params.id;
|
||||
if (!id) return null;
|
||||
const path = route.path;
|
||||
if (path.startsWith("/notes/") || path.startsWith("/tasks/")) {
|
||||
return Number(id);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
function toggleChatPanel() {
|
||||
chatPanelOpen.value = !chatPanelOpen.value;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AppHeader />
|
||||
<AppHeader @toggle-chat-panel="toggleChatPanel" />
|
||||
<router-view />
|
||||
<ChatPanel
|
||||
v-if="chatPanelOpen"
|
||||
:context-note-id="contextNoteId"
|
||||
@close="chatPanelOpen = false"
|
||||
/>
|
||||
<ToastNotification />
|
||||
</template>
|
||||
|
||||
@@ -48,3 +48,55 @@ export async function apiDelete(path: string): Promise<void> {
|
||||
throw new Error(`API error: ${res.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function apiStreamPost(
|
||||
path: string,
|
||||
body: unknown,
|
||||
onChunk: (data: Record<string, unknown>) => void
|
||||
): Promise<void> {
|
||||
const res = await fetch(path, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error(`API error: ${res.status}`);
|
||||
}
|
||||
const reader = res.body?.getReader();
|
||||
if (!reader) throw new Error("No response body");
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split("\n");
|
||||
// Keep the last (possibly incomplete) line in the buffer
|
||||
buffer = lines.pop() || "";
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.startsWith("data: ")) {
|
||||
try {
|
||||
const data = JSON.parse(trimmed.slice(6));
|
||||
onChunk(data);
|
||||
} catch {
|
||||
// Skip malformed JSON lines
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process any remaining buffer
|
||||
if (buffer.trim().startsWith("data: ")) {
|
||||
try {
|
||||
const data = JSON.parse(buffer.trim().slice(6));
|
||||
onChunk(data);
|
||||
} catch {
|
||||
// Skip malformed JSON
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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">
|
||||
💬
|
||||
</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);
|
||||
|
||||
@@ -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>
|
||||
@@ -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')">×</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>
|
||||
@@ -49,6 +49,16 @@ const router = createRouter({
|
||||
name: "task-edit",
|
||||
component: () => import("@/views/TaskEditorView.vue"),
|
||||
},
|
||||
{
|
||||
path: "/chat",
|
||||
name: "chat",
|
||||
component: () => import("@/views/ChatView.vue"),
|
||||
},
|
||||
{
|
||||
path: "/chat/:id",
|
||||
name: "chat-conversation",
|
||||
component: () => import("@/views/ChatView.vue"),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
import { ref } from "vue";
|
||||
import { defineStore } from "pinia";
|
||||
import {
|
||||
apiGet,
|
||||
apiPost,
|
||||
apiPatch,
|
||||
apiDelete,
|
||||
apiStreamPost,
|
||||
} from "@/api/client";
|
||||
import type {
|
||||
Conversation,
|
||||
ConversationDetail,
|
||||
Message,
|
||||
OllamaModel,
|
||||
} from "@/types/chat";
|
||||
|
||||
export const useChatStore = defineStore("chat", () => {
|
||||
const conversations = ref<Conversation[]>([]);
|
||||
const currentConversation = ref<ConversationDetail | null>(null);
|
||||
const total = ref(0);
|
||||
const loading = ref(false);
|
||||
const streaming = ref(false);
|
||||
const streamingContent = ref("");
|
||||
const models = ref<OllamaModel[]>([]);
|
||||
|
||||
async function fetchConversations(limit = 50, offset = 0) {
|
||||
loading.value = true;
|
||||
try {
|
||||
const data = await apiGet<{ conversations: Conversation[]; total: number }>(
|
||||
`/api/chat/conversations?limit=${limit}&offset=${offset}`
|
||||
);
|
||||
conversations.value = data.conversations;
|
||||
total.value = data.total;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function createConversation(
|
||||
title = "",
|
||||
model = ""
|
||||
): Promise<Conversation> {
|
||||
const conv = await apiPost<Conversation>("/api/chat/conversations", {
|
||||
title,
|
||||
model,
|
||||
});
|
||||
conversations.value.unshift(conv);
|
||||
return conv;
|
||||
}
|
||||
|
||||
async function fetchConversation(id: number) {
|
||||
loading.value = true;
|
||||
try {
|
||||
currentConversation.value = await apiGet<ConversationDetail>(
|
||||
`/api/chat/conversations/${id}`
|
||||
);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteConversation(id: number) {
|
||||
await apiDelete(`/api/chat/conversations/${id}`);
|
||||
conversations.value = conversations.value.filter((c) => c.id !== id);
|
||||
if (currentConversation.value?.id === id) {
|
||||
currentConversation.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
async function updateTitle(id: number, title: string) {
|
||||
const updated = await apiPatch<Conversation>(
|
||||
`/api/chat/conversations/${id}`,
|
||||
{ title }
|
||||
);
|
||||
const idx = conversations.value.findIndex((c) => c.id === id);
|
||||
if (idx !== -1) {
|
||||
conversations.value[idx] = { ...conversations.value[idx], ...updated };
|
||||
}
|
||||
if (currentConversation.value?.id === id) {
|
||||
currentConversation.value.title = updated.title;
|
||||
}
|
||||
}
|
||||
|
||||
async function sendMessage(content: string, contextNoteId?: number | null) {
|
||||
if (!currentConversation.value) return;
|
||||
|
||||
const convId = currentConversation.value.id;
|
||||
|
||||
// Add optimistic user message
|
||||
const userMsg: Message = {
|
||||
id: -Date.now(), // Temporary ID
|
||||
conversation_id: convId,
|
||||
role: "user",
|
||||
content,
|
||||
context_note_id: contextNoteId ?? null,
|
||||
created_at: new Date().toISOString(),
|
||||
};
|
||||
currentConversation.value.messages.push(userMsg);
|
||||
|
||||
streaming.value = true;
|
||||
streamingContent.value = "";
|
||||
|
||||
try {
|
||||
await apiStreamPost(
|
||||
`/api/chat/conversations/${convId}/messages`,
|
||||
{ content, context_note_id: contextNoteId },
|
||||
(data) => {
|
||||
if (data.chunk) {
|
||||
streamingContent.value += data.chunk as string;
|
||||
}
|
||||
if (data.done) {
|
||||
// Add the final assistant message
|
||||
const assistantMsg: Message = {
|
||||
id: data.message_id as number,
|
||||
conversation_id: convId,
|
||||
role: "assistant",
|
||||
content: streamingContent.value,
|
||||
context_note_id: null,
|
||||
created_at: new Date().toISOString(),
|
||||
};
|
||||
if (currentConversation.value?.id === convId) {
|
||||
currentConversation.value.messages.push(assistantMsg);
|
||||
}
|
||||
streamingContent.value = "";
|
||||
streaming.value = false;
|
||||
|
||||
// Update conversation in list
|
||||
const idx = conversations.value.findIndex((c) => c.id === convId);
|
||||
if (idx !== -1) {
|
||||
conversations.value[idx].message_count += 2;
|
||||
conversations.value[idx].updated_at = new Date().toISOString();
|
||||
}
|
||||
}
|
||||
if (data.error) {
|
||||
streaming.value = false;
|
||||
streamingContent.value = "";
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch {
|
||||
streaming.value = false;
|
||||
streamingContent.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function saveMessageAsNote(messageId: number) {
|
||||
return await apiPost<Record<string, unknown>>(
|
||||
`/api/chat/messages/${messageId}/save-as-note`,
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
async function summarizeAsNote(conversationId: number) {
|
||||
return await apiPost<Record<string, unknown>>(
|
||||
`/api/chat/conversations/${conversationId}/summarize`,
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
async function fetchModels() {
|
||||
try {
|
||||
const data = await apiGet<{ models: OllamaModel[] }>("/api/chat/models");
|
||||
models.value = data.models;
|
||||
} catch {
|
||||
models.value = [];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
conversations,
|
||||
currentConversation,
|
||||
total,
|
||||
loading,
|
||||
streaming,
|
||||
streamingContent,
|
||||
models,
|
||||
fetchConversations,
|
||||
createConversation,
|
||||
fetchConversation,
|
||||
deleteConversation,
|
||||
updateTitle,
|
||||
sendMessage,
|
||||
saveMessageAsNote,
|
||||
summarizeAsNote,
|
||||
fetchModels,
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
export interface Message {
|
||||
id: number;
|
||||
conversation_id: number;
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string;
|
||||
context_note_id: number | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Conversation {
|
||||
id: number;
|
||||
title: string;
|
||||
model: string;
|
||||
message_count: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface ConversationDetail extends Conversation {
|
||||
messages: Message[];
|
||||
}
|
||||
|
||||
export interface OllamaModel {
|
||||
name: string;
|
||||
size: number;
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref, computed, watch, nextTick } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { useChatStore } from "@/stores/chat";
|
||||
import { renderMarkdown } from "@/utils/markdown";
|
||||
import ChatMessage from "@/components/ChatMessage.vue";
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const store = useChatStore();
|
||||
|
||||
const messageInput = ref("");
|
||||
const messagesEl = ref<HTMLElement | null>(null);
|
||||
const sending = ref(false);
|
||||
const summarizing = ref(false);
|
||||
|
||||
const convId = computed(() => {
|
||||
const id = route.params.id;
|
||||
return id ? Number(id) : null;
|
||||
});
|
||||
|
||||
const streamingRendered = computed(() => {
|
||||
if (!store.streamingContent) return "";
|
||||
return renderMarkdown(store.streamingContent);
|
||||
});
|
||||
|
||||
onMounted(async () => {
|
||||
await store.fetchConversations();
|
||||
if (convId.value) {
|
||||
await store.fetchConversation(convId.value);
|
||||
}
|
||||
});
|
||||
|
||||
watch(convId, async (newId) => {
|
||||
if (newId) {
|
||||
await store.fetchConversation(newId);
|
||||
scrollToBottom();
|
||||
} else {
|
||||
store.currentConversation = null;
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => store.streamingContent,
|
||||
() => scrollToBottom()
|
||||
);
|
||||
|
||||
function scrollToBottom() {
|
||||
nextTick(() => {
|
||||
if (messagesEl.value) {
|
||||
messagesEl.value.scrollTop = messagesEl.value.scrollHeight;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function selectConversation(id: number) {
|
||||
router.push(`/chat/${id}`);
|
||||
}
|
||||
|
||||
async function newConversation() {
|
||||
const conv = await store.createConversation();
|
||||
router.push(`/chat/${conv.id}`);
|
||||
}
|
||||
|
||||
async function removeConversation(id: number) {
|
||||
await store.deleteConversation(id);
|
||||
if (convId.value === id) {
|
||||
router.push("/chat");
|
||||
}
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
const content = messageInput.value.trim();
|
||||
if (!content || store.streaming) return;
|
||||
|
||||
// Auto-create conversation if none selected
|
||||
if (!store.currentConversation) {
|
||||
const conv = await store.createConversation();
|
||||
await store.fetchConversation(conv.id);
|
||||
router.push(`/chat/${conv.id}`);
|
||||
}
|
||||
|
||||
sending.value = true;
|
||||
messageInput.value = "";
|
||||
scrollToBottom();
|
||||
|
||||
await store.sendMessage(content);
|
||||
sending.value = false;
|
||||
|
||||
// Refresh conversation list to show updated title/timestamps
|
||||
store.fetchConversations();
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSummarize() {
|
||||
if (!store.currentConversation || summarizing.value) return;
|
||||
summarizing.value = true;
|
||||
try {
|
||||
await store.summarizeAsNote(store.currentConversation.id);
|
||||
const { useToastStore } = await import("@/stores/toast");
|
||||
useToastStore().show("Conversation summarized and saved as note");
|
||||
} catch {
|
||||
const { useToastStore } = await import("@/stores/toast");
|
||||
useToastStore().show("Failed to summarize", "error");
|
||||
} finally {
|
||||
summarizing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onInputKeydown(e: KeyboardEvent) {
|
||||
if (e.key === "Enter" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
sendMessage();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="chat-page">
|
||||
<aside class="chat-sidebar">
|
||||
<button class="btn-new-conv" @click="newConversation">
|
||||
+ New Chat
|
||||
</button>
|
||||
<div class="conv-list">
|
||||
<div
|
||||
v-for="conv in store.conversations"
|
||||
:key="conv.id"
|
||||
class="conv-item"
|
||||
:class="{ active: convId === conv.id }"
|
||||
@click="selectConversation(conv.id)"
|
||||
>
|
||||
<span class="conv-title">{{ conv.title || "Untitled" }}</span>
|
||||
<button
|
||||
class="btn-delete-conv"
|
||||
@click.stop="removeConversation(conv.id)"
|
||||
title="Delete conversation"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
<p v-if="!store.conversations.length" class="empty-msg">
|
||||
No conversations yet
|
||||
</p>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<section class="chat-main">
|
||||
<template v-if="store.currentConversation">
|
||||
<div class="chat-header">
|
||||
<h2>{{ store.currentConversation.title || "New Chat" }}</h2>
|
||||
<button
|
||||
v-if="store.currentConversation.messages.length"
|
||||
class="btn-summarize"
|
||||
@click="handleSummarize"
|
||||
:disabled="summarizing || store.streaming"
|
||||
>
|
||||
{{ summarizing ? "Summarizing..." : "Summarize as Note" }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div ref="messagesEl" class="messages-container">
|
||||
<ChatMessage
|
||||
v-for="msg in store.currentConversation.messages"
|
||||
:key="msg.id"
|
||||
:message="msg"
|
||||
@save-as-note="handleSaveAsNote"
|
||||
/>
|
||||
<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"
|
||||
>
|
||||
Send a message to start the conversation.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="input-area">
|
||||
<textarea
|
||||
v-model="messageInput"
|
||||
@keydown="onInputKeydown"
|
||||
placeholder="Type a message... (Enter to send, Shift+Enter for new line)"
|
||||
:disabled="store.streaming"
|
||||
rows="2"
|
||||
></textarea>
|
||||
<button
|
||||
class="btn-send"
|
||||
@click="sendMessage"
|
||||
:disabled="!messageInput.trim() || store.streaming"
|
||||
>
|
||||
Send
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div v-else class="no-conversation">
|
||||
<p>Select a conversation or start a new chat.</p>
|
||||
<button class="btn-new-conv" @click="newConversation">
|
||||
+ New Chat
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.chat-page {
|
||||
display: flex;
|
||||
height: calc(100vh - 49px);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.chat-sidebar {
|
||||
width: 260px;
|
||||
min-width: 200px;
|
||||
border-right: 1px solid var(--color-border);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--color-bg-secondary);
|
||||
}
|
||||
|
||||
.btn-new-conv {
|
||||
margin: 0.75rem;
|
||||
padding: 0.5rem;
|
||||
background: var(--color-primary);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.btn-new-conv:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.conv-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 0 0.5rem;
|
||||
}
|
||||
|
||||
.conv-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0.5rem 0.75rem;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
.conv-item:hover {
|
||||
background: var(--color-bg-card);
|
||||
}
|
||||
.conv-item.active {
|
||||
background: var(--color-primary);
|
||||
color: #fff;
|
||||
}
|
||||
.conv-title {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.btn-delete-conv {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--color-text-muted);
|
||||
cursor: pointer;
|
||||
font-size: 1.2rem;
|
||||
padding: 0 0.25rem;
|
||||
line-height: 1;
|
||||
}
|
||||
.conv-item.active .btn-delete-conv {
|
||||
color: rgba(255, 255, 255, 0.7);
|
||||
}
|
||||
.btn-delete-conv:hover {
|
||||
color: var(--color-danger, #e74c3c);
|
||||
}
|
||||
|
||||
.chat-main {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.chat-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0.75rem 1rem;
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
.chat-header h2 {
|
||||
margin: 0;
|
||||
font-size: 1.1rem;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.btn-summarize {
|
||||
padding: 0.3rem 0.75rem;
|
||||
background: var(--color-bg-secondary);
|
||||
color: var(--color-text);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 0.85rem;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.btn-summarize:hover:not(:disabled) {
|
||||
background: var(--color-primary);
|
||||
color: #fff;
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.btn-summarize:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.messages-container {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.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; }
|
||||
}
|
||||
|
||||
.input-area {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
padding: 0.75rem 1rem;
|
||||
border-top: 1px solid var(--color-border);
|
||||
background: var(--color-bg-secondary);
|
||||
}
|
||||
.input-area textarea {
|
||||
flex: 1;
|
||||
resize: none;
|
||||
padding: 0.5rem 0.75rem;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 6px;
|
||||
font-family: inherit;
|
||||
font-size: 0.95rem;
|
||||
background: var(--color-bg);
|
||||
color: var(--color-text);
|
||||
}
|
||||
.input-area textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.btn-send {
|
||||
padding: 0.5rem 1.25rem;
|
||||
background: var(--color-primary);
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
align-self: flex-end;
|
||||
}
|
||||
.btn-send:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.no-conversation {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 1rem;
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
.empty-msg {
|
||||
color: var(--color-text-muted);
|
||||
font-size: 0.9rem;
|
||||
text-align: center;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.chat-sidebar {
|
||||
width: 200px;
|
||||
min-width: 160px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user