mirror of
https://github.com/EKKOLearnAI/hermes-web-ui.git
synced 2026-05-30 16:00:13 +00:00
feat: add session search modal (#128)
This commit is contained in:
@@ -7,6 +7,7 @@ import { useTheme } from '@/composables/useTheme'
|
||||
import AppSidebar from '@/components/layout/AppSidebar.vue'
|
||||
import { useKeyboard } from '@/composables/useKeyboard'
|
||||
import { useAppStore } from '@/stores/hermes/app'
|
||||
import SessionSearchModal from '@/components/hermes/chat/SessionSearchModal.vue'
|
||||
|
||||
const { isDark } = useTheme()
|
||||
const appStore = useAppStore()
|
||||
@@ -58,6 +59,7 @@ useKeyboard()
|
||||
<router-view />
|
||||
</main>
|
||||
</div>
|
||||
<SessionSearchModal />
|
||||
</NNotificationProvider>
|
||||
</NDialogProvider>
|
||||
</NMessageProvider>
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface SessionSummary {
|
||||
source: string
|
||||
model: string
|
||||
title: string | null
|
||||
preview?: string
|
||||
started_at: number
|
||||
ended_at: number | null
|
||||
last_active?: number
|
||||
@@ -25,6 +26,12 @@ export interface SessionDetail extends SessionSummary {
|
||||
messages: HermesMessage[]
|
||||
}
|
||||
|
||||
export interface SessionSearchResult extends SessionSummary {
|
||||
matched_message_id: number | null
|
||||
snippet: string
|
||||
rank: number
|
||||
}
|
||||
|
||||
export interface HermesMessage {
|
||||
id: number
|
||||
session_id: string
|
||||
@@ -48,6 +55,16 @@ export async function fetchSessions(source?: string, limit?: number): Promise<Se
|
||||
return res.sessions
|
||||
}
|
||||
|
||||
export async function searchSessions(q: string, source?: string, limit?: number): Promise<SessionSearchResult[]> {
|
||||
const params = new URLSearchParams()
|
||||
params.set('q', q)
|
||||
if (source) params.set('source', source)
|
||||
if (limit) params.set('limit', String(limit))
|
||||
const query = params.toString()
|
||||
const res = await request<{ results: SessionSearchResult[] }>(`/api/hermes/search/sessions?${query}`)
|
||||
return res.results
|
||||
}
|
||||
|
||||
export async function fetchSession(id: string): Promise<SessionDetail | null> {
|
||||
try {
|
||||
const res = await request<{ session: SessionDetail }>(`/api/hermes/sessions/${id}`)
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
|
||||
const TOOL_PAYLOAD_DISPLAY_LIMIT = 2000;
|
||||
|
||||
const props = defineProps<{ message: Message }>();
|
||||
const props = defineProps<{ message: Message; highlight?: boolean }>();
|
||||
const { t } = useI18n();
|
||||
|
||||
const isSystem = computed(() => props.message.role === "system");
|
||||
@@ -126,7 +126,11 @@ const renderedToolResult = computed(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="message" :class="[message.role]">
|
||||
<div
|
||||
class="message"
|
||||
:class="[message.role, { highlight }]"
|
||||
:id="`message-${message.id}`"
|
||||
>
|
||||
<template v-if="message.role === 'tool'">
|
||||
<div
|
||||
class="tool-line"
|
||||
@@ -306,6 +310,12 @@ const renderedToolResult = computed(() => {
|
||||
background-color: rgba(var(--warning-rgb), 0.06);
|
||||
}
|
||||
}
|
||||
|
||||
&.highlight {
|
||||
.message-bubble {
|
||||
box-shadow: 0 0 0 1px rgba(var(--accent-primary-rgb), 0.45);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.msg-body {
|
||||
|
||||
@@ -45,15 +45,37 @@ function scrollToBottom() {
|
||||
});
|
||||
}
|
||||
|
||||
function scrollToMessage(messageId: string) {
|
||||
nextTick(() => {
|
||||
const el = document.getElementById(`message-${messageId}`);
|
||||
if (el) {
|
||||
el.scrollIntoView({ block: 'center' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Scroll to bottom once when messages are first loaded
|
||||
watch(
|
||||
() => chatStore.activeSessionId,
|
||||
(id) => {
|
||||
if (id) scrollToBottom();
|
||||
if (!id) return;
|
||||
if (chatStore.focusMessageId) {
|
||||
scrollToMessage(chatStore.focusMessageId);
|
||||
return;
|
||||
}
|
||||
scrollToBottom();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
watch(
|
||||
() => chatStore.focusMessageId,
|
||||
(messageId) => {
|
||||
if (!messageId) return;
|
||||
scrollToMessage(messageId);
|
||||
},
|
||||
);
|
||||
|
||||
// When a run starts (user just sent a message), always scroll to bottom once
|
||||
watch(
|
||||
() => chatStore.isRunActive,
|
||||
@@ -66,12 +88,20 @@ watch(
|
||||
watch(
|
||||
() => chatStore.messages[chatStore.messages.length - 1]?.content,
|
||||
() => {
|
||||
if (chatStore.focusMessageId) {
|
||||
scrollToMessage(chatStore.focusMessageId);
|
||||
return;
|
||||
}
|
||||
if (!chatStore.isStreaming) { scrollToBottom(); return; }
|
||||
if (!isNearBottom()) return;
|
||||
scrollToBottom();
|
||||
},
|
||||
);
|
||||
watch(currentToolCalls, () => {
|
||||
if (chatStore.focusMessageId) {
|
||||
scrollToMessage(chatStore.focusMessageId);
|
||||
return;
|
||||
}
|
||||
if (!chatStore.isStreaming) { scrollToBottom(); return; }
|
||||
if (!isNearBottom()) return;
|
||||
scrollToBottom();
|
||||
@@ -84,7 +114,12 @@ watch(currentToolCalls, () => {
|
||||
<img src="/logo.png" alt="Hermes" class="empty-logo" />
|
||||
<p>{{ t("chat.emptyState") }}</p>
|
||||
</div>
|
||||
<MessageItem v-for="msg in displayMessages" :key="msg.id" :message="msg" />
|
||||
<MessageItem
|
||||
v-for="msg in displayMessages"
|
||||
:key="msg.id"
|
||||
:message="msg"
|
||||
:highlight="chatStore.focusMessageId === msg.id"
|
||||
/>
|
||||
<Transition name="fade">
|
||||
<div v-if="chatStore.isRunActive" class="streaming-indicator">
|
||||
<video
|
||||
|
||||
@@ -0,0 +1,435 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { NButton, NInput, NModal, NSpin, useMessage } from 'naive-ui'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { fetchSessions, searchSessions, type SessionSearchResult, type SessionSummary } from '@/api/hermes/sessions'
|
||||
import { useChatStore } from '@/stores/hermes/chat'
|
||||
import { useSessionSearch } from '@/composables/useSessionSearch'
|
||||
|
||||
const { t } = useI18n()
|
||||
const message = useMessage()
|
||||
const router = useRouter()
|
||||
const chatStore = useChatStore()
|
||||
const { sessionSearchOpen } = useSessionSearch()
|
||||
|
||||
const query = ref('')
|
||||
const loading = ref(false)
|
||||
const recentSessions = ref<SessionSummary[]>([])
|
||||
const searchResults = ref<SessionSearchResult[]>([])
|
||||
const activeIndex = ref(0)
|
||||
const inputRef = ref<InstanceType<typeof NInput> | null>(null)
|
||||
|
||||
let debounceTimer: ReturnType<typeof setTimeout> | null = null
|
||||
let requestSeq = 0
|
||||
|
||||
type SearchItem = SessionSearchResult | (SessionSummary & {
|
||||
snippet?: string
|
||||
matched_message_id: number | null
|
||||
rank: number
|
||||
})
|
||||
|
||||
const hasQuery = computed(() => query.value.trim().length > 0)
|
||||
|
||||
const items = computed<SearchItem[]>(() => {
|
||||
if (hasQuery.value) return searchResults.value
|
||||
return recentSessions.value.map(session => ({
|
||||
...session,
|
||||
matched_message_id: null,
|
||||
snippet: session.preview || '',
|
||||
rank: 0,
|
||||
}))
|
||||
})
|
||||
|
||||
function formatSource(source: string): string {
|
||||
const map: Record<string, string> = {
|
||||
api_server: 'API Server',
|
||||
cli: 'CLI',
|
||||
telegram: 'Telegram',
|
||||
discord: 'Discord',
|
||||
slack: 'Slack',
|
||||
matrix: 'Matrix',
|
||||
whatsapp: 'WhatsApp',
|
||||
signal: 'Signal',
|
||||
cron: 'Cron',
|
||||
weixin: 'WeChat',
|
||||
}
|
||||
return map[source] || source
|
||||
}
|
||||
|
||||
function formatTime(ts?: number): string {
|
||||
if (!ts) return ''
|
||||
const date = new Date(ts * 1000)
|
||||
return date.toLocaleString([], {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})
|
||||
}
|
||||
|
||||
function getItemTitle(item: SearchItem): string {
|
||||
const title = item.title?.trim()
|
||||
if (title) return title
|
||||
if (item.preview?.trim()) return item.preview.trim()
|
||||
return item.id
|
||||
}
|
||||
|
||||
async function loadRecentSessions() {
|
||||
const seq = ++requestSeq
|
||||
loading.value = true
|
||||
try {
|
||||
const sessions = await fetchSessions(undefined, 8)
|
||||
if (seq !== requestSeq) return
|
||||
recentSessions.value = sessions
|
||||
searchResults.value = []
|
||||
activeIndex.value = 0
|
||||
} catch (err) {
|
||||
if (seq !== requestSeq) return
|
||||
message.error(err instanceof Error ? err.message : t('chat.searchFailed'))
|
||||
} finally {
|
||||
if (seq === requestSeq) {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runSearch(text: string) {
|
||||
const seq = ++requestSeq
|
||||
loading.value = true
|
||||
try {
|
||||
const results = text.trim()
|
||||
? await searchSessions(text.trim(), undefined, 10)
|
||||
: []
|
||||
if (seq !== requestSeq) return
|
||||
searchResults.value = results
|
||||
activeIndex.value = 0
|
||||
} catch (err) {
|
||||
if (seq !== requestSeq) return
|
||||
message.error(err instanceof Error ? err.message : t('chat.searchFailed'))
|
||||
} finally {
|
||||
if (seq === requestSeq) {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureChatSessionsLoaded() {
|
||||
if (chatStore.sessions.length === 0) {
|
||||
await chatStore.loadSessions()
|
||||
}
|
||||
}
|
||||
|
||||
async function openItem(item: SearchItem) {
|
||||
const messageId = item.matched_message_id != null ? String(item.matched_message_id) : null
|
||||
sessionSearchOpen.value = false
|
||||
|
||||
await ensureChatSessionsLoaded()
|
||||
await chatStore.switchSession(item.id, messageId)
|
||||
if (router.currentRoute.value.name !== 'hermes.chat') {
|
||||
await router.push({ name: 'hermes.chat' })
|
||||
}
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
sessionSearchOpen.value = false
|
||||
}
|
||||
|
||||
function moveSelection(delta: number) {
|
||||
const list = items.value
|
||||
if (list.length === 0) return
|
||||
const next = activeIndex.value + delta
|
||||
activeIndex.value = (next + list.length) % list.length
|
||||
}
|
||||
|
||||
async function handleKeydown(e: KeyboardEvent) {
|
||||
if (!sessionSearchOpen.value) return
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault()
|
||||
moveSelection(1)
|
||||
return
|
||||
}
|
||||
if (e.key === 'ArrowUp') {
|
||||
e.preventDefault()
|
||||
moveSelection(-1)
|
||||
return
|
||||
}
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault()
|
||||
const item = items.value[activeIndex.value]
|
||||
if (item) {
|
||||
await openItem(item)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault()
|
||||
closeModal()
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => sessionSearchOpen.value,
|
||||
async (open) => {
|
||||
if (!open) {
|
||||
query.value = ''
|
||||
searchResults.value = []
|
||||
recentSessions.value = []
|
||||
activeIndex.value = 0
|
||||
return
|
||||
}
|
||||
|
||||
query.value = ''
|
||||
searchResults.value = []
|
||||
activeIndex.value = 0
|
||||
await loadRecentSessions()
|
||||
await nextTick()
|
||||
inputRef.value?.focus?.()
|
||||
},
|
||||
)
|
||||
|
||||
watch(query, (value) => {
|
||||
if (debounceTimer) {
|
||||
clearTimeout(debounceTimer)
|
||||
debounceTimer = null
|
||||
}
|
||||
debounceTimer = setTimeout(() => {
|
||||
if (!sessionSearchOpen.value) return
|
||||
void runSearch(value)
|
||||
}, 160)
|
||||
})
|
||||
|
||||
watch(items, () => {
|
||||
if (activeIndex.value >= items.value.length) {
|
||||
activeIndex.value = 0
|
||||
}
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener('keydown', handleKeydown)
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener('keydown', handleKeydown)
|
||||
if (debounceTimer) {
|
||||
clearTimeout(debounceTimer)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<NModal
|
||||
v-model:show="sessionSearchOpen"
|
||||
preset="card"
|
||||
:title="t('chat.searchTitle')"
|
||||
:style="{ width: 'min(760px, calc(100vw - 24px))' }"
|
||||
:mask-closable="true"
|
||||
:auto-focus="false"
|
||||
>
|
||||
<div class="session-search-modal">
|
||||
<div class="search-header">
|
||||
<div class="search-title">{{ t('chat.searchSubtitle') }}</div>
|
||||
<div class="search-hint">{{ t('chat.searchHint') }}</div>
|
||||
</div>
|
||||
|
||||
<NInput
|
||||
ref="inputRef"
|
||||
v-model:value="query"
|
||||
:placeholder="t('chat.searchPlaceholder')"
|
||||
clearable
|
||||
size="large"
|
||||
/>
|
||||
|
||||
<div class="search-body">
|
||||
<NSpin :show="loading">
|
||||
<div v-if="items.length === 0" class="search-empty">
|
||||
{{ hasQuery ? t('chat.searchNoResults') : t('chat.searchEmpty') }}
|
||||
</div>
|
||||
<div v-else class="result-list">
|
||||
<button
|
||||
v-for="(item, idx) in items"
|
||||
:key="item.id"
|
||||
class="result-item"
|
||||
:class="{ active: idx === activeIndex }"
|
||||
@click="openItem(item)"
|
||||
@mouseenter="activeIndex = idx"
|
||||
>
|
||||
<div class="result-main">
|
||||
<div class="result-title-row">
|
||||
<span class="result-title">{{ getItemTitle(item) }}</span>
|
||||
<span class="result-source">{{ formatSource(item.source) }}</span>
|
||||
</div>
|
||||
<div class="result-snippet">
|
||||
{{ hasQuery ? item.snippet || t('chat.searchNoSnippet') : item.preview || t('chat.searchRecent') }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="result-meta">
|
||||
<span class="result-time">{{ formatTime(item.last_active || item.started_at) }}</span>
|
||||
<span v-if="hasQuery && item.matched_message_id != null" class="result-match">
|
||||
#{{ item.matched_message_id }}
|
||||
</span>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</NSpin>
|
||||
</div>
|
||||
|
||||
<div class="search-footer">
|
||||
<span>{{ t('chat.searchEnterHint') }}</span>
|
||||
<NButton quaternary size="small" @click="closeModal">{{ t('common.cancel') }}</NButton>
|
||||
</div>
|
||||
</div>
|
||||
</NModal>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
@use '@/styles/variables' as *;
|
||||
|
||||
.session-search-modal {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.search-header {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.search-title {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: $text-primary;
|
||||
}
|
||||
|
||||
.search-hint {
|
||||
font-size: 12px;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
.search-body {
|
||||
max-height: min(60vh, 540px);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.search-empty {
|
||||
padding: 28px 0;
|
||||
text-align: center;
|
||||
color: $text-muted;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.result-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
max-height: min(60vh, 540px);
|
||||
overflow-y: auto;
|
||||
padding-right: 2px;
|
||||
}
|
||||
|
||||
.result-item {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding: 12px 14px;
|
||||
border: 1px solid $border-color;
|
||||
border-radius: $radius-md;
|
||||
background: $bg-card;
|
||||
color: $text-primary;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
transition: border-color $transition-fast, background-color $transition-fast, transform $transition-fast;
|
||||
|
||||
&:hover,
|
||||
&.active {
|
||||
border-color: $accent-muted;
|
||||
background: rgba(var(--accent-primary-rgb), 0.04);
|
||||
}
|
||||
}
|
||||
|
||||
.result-main {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.result-title-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.result-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.result-source {
|
||||
flex-shrink: 0;
|
||||
font-size: 11px;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
.result-snippet {
|
||||
margin-top: 4px;
|
||||
font-size: 12px;
|
||||
color: $text-secondary;
|
||||
line-height: 1.5;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
}
|
||||
|
||||
.result-meta {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-end;
|
||||
gap: 4px;
|
||||
font-size: 11px;
|
||||
color: $text-muted;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.result-match {
|
||||
font-family: $font-code;
|
||||
}
|
||||
|
||||
.search-footer {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
font-size: 12px;
|
||||
color: $text-muted;
|
||||
}
|
||||
|
||||
@media (max-width: $breakpoint-mobile) {
|
||||
:deep(.n-modal-body-wrapper) {
|
||||
width: calc(100vw - 24px);
|
||||
}
|
||||
|
||||
.search-header {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.result-item {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.result-meta {
|
||||
align-items: flex-start;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -8,6 +8,7 @@ import ModelSelector from "./ModelSelector.vue";
|
||||
import ProfileSelector from "./ProfileSelector.vue";
|
||||
import LanguageSwitch from "./LanguageSwitch.vue";
|
||||
import ThemeSwitch from "./ThemeSwitch.vue";
|
||||
import { useSessionSearch } from '@/composables/useSessionSearch'
|
||||
import danceVideoLight from "@/assets/dance-light.mp4";
|
||||
import danceVideoDark from "@/assets/dance-dark.mp4";
|
||||
|
||||
@@ -19,7 +20,9 @@ const message = useMessage();
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const appStore = useAppStore();
|
||||
const { openSessionSearch } = useSessionSearch();
|
||||
const selectedKey = computed(() => route.name as string);
|
||||
const logoPath = '/logo.png';
|
||||
|
||||
const collapsedGroups = reactive<Record<string, boolean>>({});
|
||||
|
||||
@@ -48,7 +51,7 @@ async function handleUpdate() {
|
||||
<template>
|
||||
<aside class="sidebar" :class="{ open: appStore.sidebarOpen }">
|
||||
<div class="sidebar-logo" @click="router.push('/hermes/chat')">
|
||||
<img src="/logo.png" alt="Hermes" class="logo-img" />
|
||||
<img :src="logoPath" alt="Hermes" class="logo-img" />
|
||||
<span class="logo-text">Hermes</span>
|
||||
<video class="logo-dance" :src="isDark ? danceVideoDark : danceVideoLight" autoplay loop muted playsinline />
|
||||
</div>
|
||||
@@ -62,6 +65,14 @@ async function handleUpdate() {
|
||||
<span>{{ t("sidebar.chat") }}</span>
|
||||
</button>
|
||||
|
||||
<button class="nav-item" @click="openSessionSearch">
|
||||
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
|
||||
<circle cx="11" cy="11" r="7" />
|
||||
<path d="m20 20-3.5-3.5" />
|
||||
</svg>
|
||||
<span>{{ t("sidebar.search") }}</span>
|
||||
</button>
|
||||
|
||||
<!-- Agent -->
|
||||
<div class="nav-group">
|
||||
<div class="nav-group-label" @click="toggleGroup('agent')">
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { onMounted, onUnmounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useChatStore } from '@/stores/hermes/chat'
|
||||
import { useSessionSearch } from './useSessionSearch'
|
||||
|
||||
export function useKeyboard() {
|
||||
const router = useRouter()
|
||||
const chatStore = useChatStore()
|
||||
const { sessionSearchOpen, openSessionSearch, closeSessionSearch } = useSessionSearch()
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
const mod = e.ctrlKey || e.metaKey
|
||||
@@ -12,14 +14,28 @@ export function useKeyboard() {
|
||||
if (mod && e.key === 'n') {
|
||||
e.preventDefault()
|
||||
chatStore.newChat()
|
||||
return
|
||||
}
|
||||
|
||||
if (mod && e.key === 'j') {
|
||||
e.preventDefault()
|
||||
router.push({ name: 'hermes.jobs' })
|
||||
return
|
||||
}
|
||||
|
||||
if (mod && e.key.toLowerCase() === 'k') {
|
||||
if (router.currentRoute.value.name === 'login') return
|
||||
e.preventDefault()
|
||||
openSessionSearch()
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === 'Escape') {
|
||||
if (sessionSearchOpen.value) {
|
||||
e.preventDefault()
|
||||
closeSessionSearch()
|
||||
return
|
||||
}
|
||||
// Close any open modals — naive-ui handles this internally
|
||||
const modal = document.querySelector('.n-modal-mask')
|
||||
if (modal) {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
const sessionSearchOpen = ref(false)
|
||||
|
||||
export function useSessionSearch() {
|
||||
function openSessionSearch() {
|
||||
sessionSearchOpen.value = true
|
||||
}
|
||||
|
||||
function closeSessionSearch() {
|
||||
sessionSearchOpen.value = false
|
||||
}
|
||||
|
||||
return {
|
||||
sessionSearchOpen,
|
||||
openSessionSearch,
|
||||
closeSessionSearch,
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ export default {
|
||||
// Sidebar
|
||||
sidebar: {
|
||||
chat: 'Chat',
|
||||
search: 'Search',
|
||||
jobs: 'Jobs',
|
||||
models: 'Models',
|
||||
profiles: 'Profiles',
|
||||
@@ -82,6 +83,16 @@ export default {
|
||||
contextUsed: 'Context used:',
|
||||
sessions: 'Sessions',
|
||||
noSessions: 'No sessions',
|
||||
searchTitle: 'Search Sessions',
|
||||
searchSubtitle: 'Search by title or message content',
|
||||
searchHint: 'Cmd/Ctrl+K',
|
||||
searchPlaceholder: 'Search sessions...',
|
||||
searchEmpty: 'Recent sessions',
|
||||
searchRecent: 'Recent session',
|
||||
searchNoResults: 'No sessions match your search',
|
||||
searchNoSnippet: 'No snippet available',
|
||||
searchEnterHint: 'Enter to open · Esc to close',
|
||||
searchFailed: 'Failed to search sessions',
|
||||
newChat: 'New Chat',
|
||||
deleteSession: 'Delete this session?',
|
||||
sessionDeleted: 'Session deleted',
|
||||
|
||||
@@ -42,6 +42,7 @@ export default {
|
||||
// 侧边栏
|
||||
sidebar: {
|
||||
chat: '对话',
|
||||
search: '搜索',
|
||||
jobs: '任务',
|
||||
models: '模型',
|
||||
profiles: '用户',
|
||||
@@ -82,6 +83,16 @@ export default {
|
||||
contextUsed: '上下文已用:',
|
||||
sessions: '会话',
|
||||
noSessions: '暂无会话',
|
||||
searchTitle: '搜索会话',
|
||||
searchSubtitle: '按标题或消息内容搜索',
|
||||
searchHint: 'Cmd/Ctrl+K',
|
||||
searchPlaceholder: '搜索会话...',
|
||||
searchEmpty: '最近会话',
|
||||
searchRecent: '最近会话',
|
||||
searchNoResults: '没有匹配的会话',
|
||||
searchNoSnippet: '没有可显示的摘要',
|
||||
searchEnterHint: 'Enter 打开 · Esc 关闭',
|
||||
searchFailed: '搜索会话失败',
|
||||
newChat: '新建对话',
|
||||
deleteSession: '确定删除此会话?',
|
||||
sessionDeleted: '会话已删除',
|
||||
|
||||
@@ -231,6 +231,7 @@ function sanitizeForCache(msgs: Message[]): Message[] {
|
||||
export const useChatStore = defineStore('chat', () => {
|
||||
const sessions = ref<Session[]>([])
|
||||
const activeSessionId = ref<string | null>(null)
|
||||
const focusMessageId = ref<string | null>(null)
|
||||
const streamStates = ref<Map<string, AbortController>>(new Map())
|
||||
const isStreaming = computed(() => activeSessionId.value != null && streamStates.value.has(activeSessionId.value))
|
||||
const isLoadingSessions = ref(false)
|
||||
@@ -474,8 +475,9 @@ export const useChatStore = defineStore('chat', () => {
|
||||
return session
|
||||
}
|
||||
|
||||
async function switchSession(sessionId: string) {
|
||||
async function switchSession(sessionId: string, focusId?: string | null) {
|
||||
activeSessionId.value = sessionId
|
||||
focusMessageId.value = focusId ?? null
|
||||
localStorage.setItem(storageKey(), sessionId)
|
||||
activeSession.value = sessions.value.find(s => s.id === sessionId) || null
|
||||
|
||||
@@ -915,6 +917,7 @@ export const useChatStore = defineStore('chat', () => {
|
||||
sessions,
|
||||
activeSessionId,
|
||||
activeSession,
|
||||
focusMessageId,
|
||||
messages,
|
||||
isStreaming,
|
||||
isRunActive,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as hermesCli from '../../services/hermes/hermes-cli'
|
||||
import { getConversationDetail, listConversationSummaries } from '../../services/hermes/conversations'
|
||||
import { listSessionSummaries } from '../../services/hermes/sessions-db'
|
||||
import { listSessionSummaries, searchSessionSummaries } from '../../services/hermes/sessions-db'
|
||||
import { logger } from '../../services/logger'
|
||||
|
||||
function parseHumanOnly(value: unknown): boolean {
|
||||
@@ -50,6 +50,23 @@ export async function list(ctx: any) {
|
||||
ctx.body = { sessions }
|
||||
}
|
||||
|
||||
export async function search(ctx: any) {
|
||||
const q = typeof ctx.query.q === 'string' ? ctx.query.q : ''
|
||||
const source = typeof ctx.query.source === 'string' && ctx.query.source.trim()
|
||||
? ctx.query.source.trim()
|
||||
: undefined
|
||||
const limit = ctx.query.limit ? parseInt(ctx.query.limit as string, 10) : undefined
|
||||
|
||||
try {
|
||||
const results = await searchSessionSummaries(q, source, limit && limit > 0 ? limit : 20)
|
||||
ctx.body = { results }
|
||||
} catch (err) {
|
||||
logger.error(err, 'Hermes Session DB: search failed')
|
||||
ctx.status = 500
|
||||
ctx.body = { error: 'Failed to search sessions' }
|
||||
}
|
||||
}
|
||||
|
||||
export async function get(ctx: any) {
|
||||
const session = await hermesCli.getSession(ctx.params.id)
|
||||
if (!session) {
|
||||
|
||||
@@ -6,6 +6,8 @@ export const sessionRoutes = new Router()
|
||||
sessionRoutes.get('/api/hermes/sessions/conversations', ctrl.listConversations)
|
||||
sessionRoutes.get('/api/hermes/sessions/conversations/:id/messages', ctrl.getConversationMessages)
|
||||
sessionRoutes.get('/api/hermes/sessions', ctrl.list)
|
||||
sessionRoutes.get('/api/hermes/search/sessions', ctrl.search)
|
||||
sessionRoutes.get('/api/hermes/sessions/search', ctrl.search)
|
||||
sessionRoutes.get('/api/hermes/sessions/:id', ctrl.get)
|
||||
sessionRoutes.delete('/api/hermes/sessions/:id', ctrl.remove)
|
||||
sessionRoutes.post('/api/hermes/sessions/:id/rename', ctrl.rename)
|
||||
|
||||
@@ -29,6 +29,12 @@ export interface HermesSessionRow {
|
||||
last_active: number
|
||||
}
|
||||
|
||||
export interface HermesSessionSearchRow extends HermesSessionRow {
|
||||
matched_message_id: number | null
|
||||
snippet: string
|
||||
rank: number
|
||||
}
|
||||
|
||||
function sessionDbPath(): string {
|
||||
return `${getActiveProfileDir()}/state.db`
|
||||
}
|
||||
@@ -81,43 +87,122 @@ function mapRow(row: Record<string, unknown>): HermesSessionRow {
|
||||
}
|
||||
}
|
||||
|
||||
const BASE_SELECT = `
|
||||
SELECT
|
||||
s.id,
|
||||
s.source,
|
||||
COALESCE(s.user_id, '') AS user_id,
|
||||
COALESCE(s.model, '') AS model,
|
||||
COALESCE(s.title, '') AS title,
|
||||
COALESCE(s.started_at, 0) AS started_at,
|
||||
s.ended_at AS ended_at,
|
||||
COALESCE(s.end_reason, '') AS end_reason,
|
||||
COALESCE(s.message_count, 0) AS message_count,
|
||||
COALESCE(s.tool_call_count, 0) AS tool_call_count,
|
||||
COALESCE(s.input_tokens, 0) AS input_tokens,
|
||||
COALESCE(s.output_tokens, 0) AS output_tokens,
|
||||
COALESCE(s.cache_read_tokens, 0) AS cache_read_tokens,
|
||||
COALESCE(s.cache_write_tokens, 0) AS cache_write_tokens,
|
||||
COALESCE(s.reasoning_tokens, 0) AS reasoning_tokens,
|
||||
COALESCE(s.billing_provider, '') AS billing_provider,
|
||||
COALESCE(s.estimated_cost_usd, 0) AS estimated_cost_usd,
|
||||
s.actual_cost_usd AS actual_cost_usd,
|
||||
COALESCE(s.cost_status, '') AS cost_status,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT SUBSTR(REPLACE(REPLACE(m.content, CHAR(10), ' '), CHAR(13), ' '), 1, 63)
|
||||
FROM messages m
|
||||
WHERE m.session_id = s.id AND m.role = 'user' AND m.content IS NOT NULL
|
||||
ORDER BY m.timestamp, m.id
|
||||
LIMIT 1
|
||||
),
|
||||
''
|
||||
) AS preview,
|
||||
COALESCE((SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id), s.started_at) AS last_active
|
||||
const SESSION_SELECT = `
|
||||
s.id,
|
||||
s.source,
|
||||
COALESCE(s.user_id, '') AS user_id,
|
||||
COALESCE(s.model, '') AS model,
|
||||
COALESCE(s.title, '') AS title,
|
||||
COALESCE(s.started_at, 0) AS started_at,
|
||||
s.ended_at AS ended_at,
|
||||
COALESCE(s.end_reason, '') AS end_reason,
|
||||
COALESCE(s.message_count, 0) AS message_count,
|
||||
COALESCE(s.tool_call_count, 0) AS tool_call_count,
|
||||
COALESCE(s.input_tokens, 0) AS input_tokens,
|
||||
COALESCE(s.output_tokens, 0) AS output_tokens,
|
||||
COALESCE(s.cache_read_tokens, 0) AS cache_read_tokens,
|
||||
COALESCE(s.cache_write_tokens, 0) AS cache_write_tokens,
|
||||
COALESCE(s.reasoning_tokens, 0) AS reasoning_tokens,
|
||||
COALESCE(s.billing_provider, '') AS billing_provider,
|
||||
COALESCE(s.estimated_cost_usd, 0) AS estimated_cost_usd,
|
||||
s.actual_cost_usd AS actual_cost_usd,
|
||||
COALESCE(s.cost_status, '') AS cost_status,
|
||||
COALESCE(
|
||||
(
|
||||
SELECT SUBSTR(REPLACE(REPLACE(m.content, CHAR(10), ' '), CHAR(13), ' '), 1, 63)
|
||||
FROM messages m
|
||||
WHERE m.session_id = s.id AND m.role = 'user' AND m.content IS NOT NULL
|
||||
ORDER BY m.timestamp, m.id
|
||||
LIMIT 1
|
||||
),
|
||||
''
|
||||
) AS preview,
|
||||
COALESCE((SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id), s.started_at) AS last_active
|
||||
`
|
||||
|
||||
const SESSION_FROM = `
|
||||
FROM sessions s
|
||||
WHERE s.parent_session_id IS NULL
|
||||
AND s.source != 'tool'
|
||||
`
|
||||
|
||||
function buildBaseSessionSql(source?: string): { sql: string, params: any[] } {
|
||||
const sql = source
|
||||
? `SELECT ${SESSION_SELECT}${SESSION_FROM}\n AND s.source = ?`
|
||||
: `SELECT ${SESSION_SELECT}${SESSION_FROM}`
|
||||
return { sql, params: source ? [source] : [] }
|
||||
}
|
||||
|
||||
function buildListSessionSql(source?: string, limit = 2000): { sql: string, params: any[] } {
|
||||
const base = buildBaseSessionSql(source)
|
||||
return {
|
||||
sql: `${base.sql}\n ORDER BY s.started_at DESC\n LIMIT ?`,
|
||||
params: [...base.params, limit],
|
||||
}
|
||||
}
|
||||
|
||||
function containsCjk(text: string): boolean {
|
||||
for (const ch of text) {
|
||||
const cp = ch.codePointAt(0) ?? 0
|
||||
if (
|
||||
(cp >= 0x4E00 && cp <= 0x9FFF) ||
|
||||
(cp >= 0x3400 && cp <= 0x4DBF) ||
|
||||
(cp >= 0x20000 && cp <= 0x2A6DF) ||
|
||||
(cp >= 0x3000 && cp <= 0x303F) ||
|
||||
(cp >= 0x3040 && cp <= 0x309F) ||
|
||||
(cp >= 0x30A0 && cp <= 0x30FF) ||
|
||||
(cp >= 0xAC00 && cp <= 0xD7AF)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function sanitizeFtsQuery(query: string): string {
|
||||
const quotedParts: string[] = []
|
||||
|
||||
const preserved = query.replace(/"[^"]*"/g, (match) => {
|
||||
quotedParts.push(match)
|
||||
return `\u0000Q${quotedParts.length - 1}\u0000`
|
||||
})
|
||||
|
||||
let sanitized = preserved.replace(/[+{}()"^]/g, ' ')
|
||||
sanitized = sanitized.replace(/\*+/g, '*')
|
||||
sanitized = sanitized.replace(/(^|\s)\*/g, '$1')
|
||||
sanitized = sanitized.trim().replace(/^(AND|OR|NOT)\b\s*/i, '')
|
||||
sanitized = sanitized.trim().replace(/\s+(AND|OR|NOT)\s*$/i, '')
|
||||
sanitized = sanitized.replace(/\b(\w+(?:[.-]\w+)+)\b/g, '"$1"')
|
||||
|
||||
for (let i = 0; i < quotedParts.length; i += 1) {
|
||||
sanitized = sanitized.replace(`\u0000Q${i}\u0000`, quotedParts[i])
|
||||
}
|
||||
|
||||
return sanitized.trim()
|
||||
}
|
||||
|
||||
function toPrefixQuery(query: string): string {
|
||||
const tokens = query.match(/"[^"]*"|\S+/g)
|
||||
if (!tokens) return ''
|
||||
return tokens
|
||||
.map((token) => {
|
||||
if (token === 'AND' || token === 'OR' || token === 'NOT') return token
|
||||
if (token.startsWith('"') && token.endsWith('"')) return token
|
||||
if (token.endsWith('*')) return token
|
||||
return `${token}*`
|
||||
})
|
||||
.join(' ')
|
||||
}
|
||||
|
||||
function mapSearchRow(row: Record<string, unknown>): HermesSessionSearchRow {
|
||||
return {
|
||||
...mapRow(row),
|
||||
matched_message_id: normalizeNullableNumber(row.matched_message_id),
|
||||
snippet: String(row.snippet || row.preview || ''),
|
||||
rank: Number.isFinite(Number(row.rank)) ? Number(row.rank) : 0,
|
||||
}
|
||||
}
|
||||
|
||||
export async function listSessionSummaries(source?: string, limit = 2000): Promise<HermesSessionRow[]> {
|
||||
if (!SQLITE_AVAILABLE) {
|
||||
throw new Error(`node:sqlite requires Node >= 22.5, current: ${process.versions.node}`)
|
||||
@@ -127,17 +212,141 @@ export async function listSessionSummaries(source?: string, limit = 2000): Promi
|
||||
const db = new DatabaseSync(sessionDbPath(), { open: true, readOnly: true })
|
||||
|
||||
try {
|
||||
const sql = source
|
||||
? `${BASE_SELECT}\n AND s.source = ?\n ORDER BY s.started_at DESC\n LIMIT ?`
|
||||
: `${BASE_SELECT}\n ORDER BY s.started_at DESC\n LIMIT ?`
|
||||
|
||||
const { sql, params } = buildListSessionSql(source, limit)
|
||||
const statement = db.prepare(sql)
|
||||
const rows = source
|
||||
? statement.all(source, limit) as Record<string, unknown>[]
|
||||
: statement.all(limit) as Record<string, unknown>[]
|
||||
const rows = statement.all(...params) as Record<string, unknown>[]
|
||||
|
||||
return rows.map(mapRow)
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
|
||||
export async function searchSessionSummaries(
|
||||
query: string,
|
||||
source?: string,
|
||||
limit = 20,
|
||||
): Promise<HermesSessionSearchRow[]> {
|
||||
if (!SQLITE_AVAILABLE) {
|
||||
throw new Error(`node:sqlite requires Node >= 22.5, current: ${process.versions.node}`)
|
||||
}
|
||||
|
||||
const trimmed = query.trim()
|
||||
if (!trimmed) {
|
||||
const recent = await listSessionSummaries(source, limit)
|
||||
return recent.map(row => ({
|
||||
...row,
|
||||
matched_message_id: null,
|
||||
snippet: row.preview,
|
||||
rank: 0,
|
||||
}))
|
||||
}
|
||||
|
||||
const { DatabaseSync } = await import('node:sqlite')
|
||||
const db = new DatabaseSync(sessionDbPath(), { open: true, readOnly: true })
|
||||
const normalized = sanitizeFtsQuery(trimmed)
|
||||
const prefixQuery = toPrefixQuery(normalized)
|
||||
|
||||
try {
|
||||
const titleBase = buildBaseSessionSql(source)
|
||||
const contentBase = buildBaseSessionSql(source)
|
||||
|
||||
const titleSql = `
|
||||
WITH base AS (
|
||||
${titleBase.sql}
|
||||
)
|
||||
SELECT
|
||||
base.*,
|
||||
NULL AS matched_message_id,
|
||||
CASE
|
||||
WHEN base.title IS NOT NULL AND base.title != '' THEN base.title
|
||||
ELSE base.preview
|
||||
END AS snippet,
|
||||
0 AS rank
|
||||
FROM base
|
||||
WHERE LOWER(COALESCE(base.title, '')) LIKE ?
|
||||
ORDER BY base.last_active DESC
|
||||
LIMIT ?
|
||||
`
|
||||
|
||||
const titleStatement = db.prepare(titleSql)
|
||||
const titleRows = titleStatement.all(...titleBase.params, `%${trimmed.toLowerCase()}%`, limit) as Record<string, unknown>[]
|
||||
|
||||
const contentSql = `
|
||||
WITH base AS (
|
||||
${contentBase.sql}
|
||||
)
|
||||
SELECT
|
||||
base.*,
|
||||
m.id AS matched_message_id,
|
||||
snippet(messages_fts, 0, '>>>', '<<<', '...', 40) AS snippet,
|
||||
bm25(messages_fts) AS rank
|
||||
FROM messages_fts
|
||||
JOIN messages m ON m.id = messages_fts.rowid
|
||||
JOIN base ON base.id = m.session_id
|
||||
WHERE messages_fts MATCH ?
|
||||
ORDER BY rank, base.last_active DESC
|
||||
LIMIT ?
|
||||
`
|
||||
|
||||
const contentRows = prefixQuery
|
||||
? (db.prepare(contentSql).all(...contentBase.params, prefixQuery, limit * 4) as Record<string, unknown>[])
|
||||
: []
|
||||
|
||||
const merged = new Map<string, HermesSessionSearchRow>()
|
||||
for (const row of titleRows) {
|
||||
const mapped = mapSearchRow(row)
|
||||
merged.set(mapped.id, mapped)
|
||||
}
|
||||
for (const row of contentRows) {
|
||||
const mapped = mapSearchRow(row)
|
||||
if (!merged.has(mapped.id)) {
|
||||
merged.set(mapped.id, mapped)
|
||||
}
|
||||
}
|
||||
|
||||
const items = [...merged.values()]
|
||||
items.sort((a, b) => {
|
||||
if (a.rank !== b.rank) return a.rank - b.rank
|
||||
return b.last_active - a.last_active
|
||||
})
|
||||
return items.slice(0, limit)
|
||||
} catch (err) {
|
||||
if (containsCjk(normalized)) {
|
||||
const likeBase = buildBaseSessionSql(source)
|
||||
const likeSql = `
|
||||
WITH base AS (
|
||||
${likeBase.sql}
|
||||
)
|
||||
SELECT
|
||||
base.*,
|
||||
m.id AS matched_message_id,
|
||||
substr(
|
||||
m.content,
|
||||
max(1, instr(m.content, ?) - 40),
|
||||
120
|
||||
) AS snippet,
|
||||
0 AS rank
|
||||
FROM base
|
||||
JOIN messages m ON m.session_id = base.id
|
||||
WHERE m.content LIKE ?
|
||||
ORDER BY base.last_active DESC, m.timestamp DESC
|
||||
`
|
||||
const likeStatement = db.prepare(likeSql)
|
||||
const likeRows = likeStatement.all(...likeBase.params, trimmed, `%${trimmed}%`) as Record<string, unknown>[]
|
||||
const merged = new Map<string, HermesSessionSearchRow>()
|
||||
for (const row of likeRows) {
|
||||
const mapped = mapSearchRow(row)
|
||||
if (!merged.has(mapped.id)) {
|
||||
merged.set(mapped.id, mapped)
|
||||
}
|
||||
}
|
||||
return [...merged.values()].slice(0, limit)
|
||||
}
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
throw new Error(`Failed to search sessions: ${message}`)
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
// @vitest-environment jsdom
|
||||
import { nextTick, defineComponent, h } from 'vue'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
|
||||
const apiMocks = vi.hoisted(() => ({
|
||||
fetchSessionsMock: vi.fn(),
|
||||
searchSessionsMock: vi.fn(),
|
||||
routerPushMock: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/hermes/sessions', () => ({
|
||||
fetchSessions: apiMocks.fetchSessionsMock,
|
||||
searchSessions: apiMocks.searchSessionsMock,
|
||||
}))
|
||||
|
||||
const chatStoreMock = vi.hoisted(() => ({
|
||||
sessions: [] as Array<Record<string, any>>,
|
||||
loadSessions: vi.fn(),
|
||||
switchSession: vi.fn(),
|
||||
newChat: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/hermes/chat', () => ({
|
||||
useChatStore: () => chatStoreMock,
|
||||
}))
|
||||
|
||||
const routerCurrentRoute = { value: { name: 'hermes.logs' } }
|
||||
|
||||
vi.mock('vue-router', () => ({
|
||||
useRouter: () => ({
|
||||
currentRoute: routerCurrentRoute,
|
||||
push: apiMocks.routerPushMock,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('vue-i18n', () => ({
|
||||
useI18n: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('naive-ui', async () => {
|
||||
const actual = await vi.importActual<any>('naive-ui')
|
||||
return {
|
||||
...actual,
|
||||
useMessage: () => ({
|
||||
error: vi.fn(),
|
||||
}),
|
||||
NModal: {
|
||||
props: ['show'],
|
||||
emits: ['update:show'],
|
||||
template: '<div v-if="show" class="n-modal-stub"><slot /></div>',
|
||||
},
|
||||
NInput: {
|
||||
props: ['value', 'size'],
|
||||
emits: ['update:value', 'keydown'],
|
||||
template: '<input class="n-input-stub" :value="value" @input="$emit(\'update:value\', $event.target.value)" @keydown="$emit(\'keydown\', $event)" />',
|
||||
},
|
||||
NSpin: {
|
||||
template: '<div class="n-spin-stub"><slot /></div>',
|
||||
},
|
||||
NButton: {
|
||||
template: '<button class="n-button-stub"><slot /></button>',
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
import SessionSearchModal from '@/components/hermes/chat/SessionSearchModal.vue'
|
||||
import { useSessionSearch } from '@/composables/useSessionSearch'
|
||||
import { useKeyboard } from '@/composables/useKeyboard'
|
||||
|
||||
function flushPromises() {
|
||||
return Promise.resolve().then(() => Promise.resolve())
|
||||
}
|
||||
|
||||
describe('session search modal', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
vi.clearAllMocks()
|
||||
chatStoreMock.sessions = []
|
||||
chatStoreMock.loadSessions.mockResolvedValue(undefined)
|
||||
chatStoreMock.switchSession.mockResolvedValue(undefined)
|
||||
apiMocks.fetchSessionsMock.mockResolvedValue([
|
||||
{
|
||||
id: 'recent-1',
|
||||
source: 'cli',
|
||||
model: 'openai/gpt-5.4',
|
||||
title: 'Recent Docker fix',
|
||||
preview: 'recent preview',
|
||||
started_at: 1710000000,
|
||||
ended_at: 1710000001,
|
||||
last_active: 1710000002,
|
||||
message_count: 2,
|
||||
tool_call_count: 0,
|
||||
input_tokens: 1,
|
||||
output_tokens: 2,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
reasoning_tokens: 0,
|
||||
billing_provider: 'openrouter',
|
||||
estimated_cost_usd: 0,
|
||||
actual_cost_usd: 0,
|
||||
cost_status: 'estimated',
|
||||
},
|
||||
])
|
||||
apiMocks.searchSessionsMock.mockResolvedValue([
|
||||
{
|
||||
id: 'match-1',
|
||||
source: 'telegram',
|
||||
model: 'openai/gpt-5.4',
|
||||
title: 'Debugging session',
|
||||
preview: 'search preview',
|
||||
started_at: 1710001000,
|
||||
ended_at: null,
|
||||
last_active: 1710001005,
|
||||
message_count: 4,
|
||||
tool_call_count: 1,
|
||||
input_tokens: 3,
|
||||
output_tokens: 4,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
reasoning_tokens: 0,
|
||||
billing_provider: 'openrouter',
|
||||
estimated_cost_usd: 0,
|
||||
actual_cost_usd: 0,
|
||||
cost_status: 'estimated',
|
||||
matched_message_id: 17,
|
||||
snippet: 'docker compose up',
|
||||
rank: 0.1,
|
||||
},
|
||||
])
|
||||
routerCurrentRoute.value = { name: 'hermes.logs' }
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('opens from Cmd/Ctrl+K and loads recent sessions', async () => {
|
||||
const { openSessionSearch, sessionSearchOpen } = useSessionSearch()
|
||||
const wrapper = mount(SessionSearchModal, {
|
||||
global: {
|
||||
stubs: {
|
||||
NModal: false,
|
||||
NInput: false,
|
||||
NSpin: false,
|
||||
NButton: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
openSessionSearch()
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
expect(sessionSearchOpen.value).toBe(true)
|
||||
expect(apiMocks.fetchSessionsMock).toHaveBeenCalledWith(undefined, 8)
|
||||
expect(wrapper.text()).toContain('Recent Docker fix')
|
||||
})
|
||||
|
||||
it('searches by content and opens the matched session', async () => {
|
||||
const { openSessionSearch } = useSessionSearch()
|
||||
const wrapper = mount(SessionSearchModal)
|
||||
|
||||
openSessionSearch()
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
const input = wrapper.find('input.n-input-stub')
|
||||
await input.setValue('docker')
|
||||
await vi.advanceTimersByTimeAsync(200)
|
||||
await flushPromises()
|
||||
await nextTick()
|
||||
|
||||
expect(apiMocks.searchSessionsMock).toHaveBeenCalledWith('docker', undefined, 10)
|
||||
expect(wrapper.text()).toContain('Debugging session')
|
||||
|
||||
await wrapper.find('button.result-item').trigger('click')
|
||||
await flushPromises()
|
||||
|
||||
expect(chatStoreMock.loadSessions).toHaveBeenCalled()
|
||||
expect(chatStoreMock.switchSession).toHaveBeenCalledWith('match-1', '17')
|
||||
expect(apiMocks.routerPushMock).toHaveBeenCalledWith({ name: 'hermes.chat' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('keyboard shortcut', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
const { closeSessionSearch } = useSessionSearch()
|
||||
closeSessionSearch()
|
||||
chatStoreMock.newChat.mockReset()
|
||||
})
|
||||
|
||||
it('opens session search on Cmd/Ctrl+K', async () => {
|
||||
const Dummy = defineComponent({
|
||||
setup() {
|
||||
useKeyboard()
|
||||
return () => h('div')
|
||||
},
|
||||
})
|
||||
|
||||
mount(Dummy)
|
||||
|
||||
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'k', metaKey: true }))
|
||||
await nextTick()
|
||||
|
||||
expect(useSessionSearch().sessionSearchOpen.value).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,93 @@
|
||||
// @vitest-environment jsdom
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
|
||||
const openSessionSearchMock = vi.hoisted(() => vi.fn())
|
||||
|
||||
vi.mock('@/composables/useSessionSearch', () => ({
|
||||
useSessionSearch: () => ({
|
||||
openSessionSearch: openSessionSearchMock,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/hermes/app', () => ({
|
||||
useAppStore: () => ({
|
||||
sidebarOpen: true,
|
||||
connected: true,
|
||||
serverVersion: 'test',
|
||||
updateAvailable: false,
|
||||
updating: false,
|
||||
toggleSidebar: vi.fn(),
|
||||
closeSidebar: vi.fn(),
|
||||
doUpdate: vi.fn(),
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('vue-router', async (importOriginal) => {
|
||||
const actual = await importOriginal<any>()
|
||||
return {
|
||||
...actual,
|
||||
useRoute: () => ({ name: 'hermes.chat' }),
|
||||
useRouter: () => ({ push: vi.fn() }),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('vue-i18n', () => ({
|
||||
useI18n: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/composables/useTheme', () => ({
|
||||
useTheme: () => ({ isDark: false }),
|
||||
}))
|
||||
|
||||
vi.mock('/logo.png', () => ({
|
||||
default: 'logo.png',
|
||||
}))
|
||||
|
||||
vi.mock('naive-ui', async () => {
|
||||
const actual = await vi.importActual<any>('naive-ui')
|
||||
return {
|
||||
...actual,
|
||||
useMessage: () => ({
|
||||
success: vi.fn(),
|
||||
error: vi.fn(),
|
||||
}),
|
||||
NButton: {
|
||||
template: '<button><slot /></button>',
|
||||
},
|
||||
NSelect: {
|
||||
template: '<div />',
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
import AppSidebar from '@/components/layout/AppSidebar.vue'
|
||||
|
||||
describe('AppSidebar search entry', () => {
|
||||
beforeEach(() => {
|
||||
openSessionSearchMock.mockClear()
|
||||
})
|
||||
|
||||
it('opens the session search modal from the sidebar button', async () => {
|
||||
const wrapper = mount(AppSidebar, {
|
||||
global: {
|
||||
stubs: {
|
||||
ProfileSelector: true,
|
||||
ModelSelector: true,
|
||||
LanguageSwitch: true,
|
||||
ThemeSwitch: true,
|
||||
NButton: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const buttons = wrapper.findAll('button')
|
||||
const searchButton = buttons.find(node => node.text().includes('sidebar.search'))
|
||||
expect(searchButton).toBeTruthy()
|
||||
|
||||
await searchButton!.trigger('click')
|
||||
expect(openSessionSearchMock).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,15 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const allMock = vi.fn()
|
||||
const prepareMock = vi.fn(() => ({ all: allMock }))
|
||||
const titleAllMock = vi.fn()
|
||||
const contentAllMock = vi.fn()
|
||||
const likeAllMock = vi.fn()
|
||||
const prepareMock = vi.fn((sql: string) => {
|
||||
if (sql.includes('messages_fts MATCH')) return ({ all: contentAllMock })
|
||||
if (sql.includes('m.content LIKE ?')) return ({ all: likeAllMock })
|
||||
if (sql.includes("LOWER(COALESCE(base.title, '')) LIKE ?")) return ({ all: titleAllMock })
|
||||
return ({ all: allMock })
|
||||
})
|
||||
const closeMock = vi.fn()
|
||||
const databaseSyncMock = vi.fn(() => ({ prepare: prepareMock, close: closeMock }))
|
||||
const getActiveProfileDirMock = vi.fn(() => '/tmp/hermes-profile')
|
||||
@@ -18,6 +26,9 @@ describe('session DB summaries', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
allMock.mockReset()
|
||||
titleAllMock.mockReset()
|
||||
contentAllMock.mockReset()
|
||||
likeAllMock.mockReset()
|
||||
prepareMock.mockClear()
|
||||
closeMock.mockClear()
|
||||
databaseSyncMock.mockClear()
|
||||
@@ -122,4 +133,144 @@ describe('session DB summaries', () => {
|
||||
expect(rows[0].source).toBe('telegram')
|
||||
expect(rows[0].title).toBe('preview text')
|
||||
})
|
||||
|
||||
it('searches session titles and content with deduped results', async () => {
|
||||
titleAllMock.mockReturnValue([
|
||||
{
|
||||
id: 'title-1',
|
||||
source: 'cli',
|
||||
user_id: '',
|
||||
model: 'openai/gpt-5.4',
|
||||
title: 'Docker debugging',
|
||||
started_at: 1710001000,
|
||||
ended_at: null,
|
||||
end_reason: '',
|
||||
message_count: 2,
|
||||
tool_call_count: 0,
|
||||
input_tokens: 1,
|
||||
output_tokens: 2,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
reasoning_tokens: 0,
|
||||
billing_provider: '',
|
||||
estimated_cost_usd: 0,
|
||||
actual_cost_usd: null,
|
||||
cost_status: '',
|
||||
preview: 'title preview',
|
||||
last_active: 1710001005,
|
||||
matched_message_id: null,
|
||||
snippet: 'Docker debugging',
|
||||
rank: 0,
|
||||
},
|
||||
])
|
||||
contentAllMock.mockReturnValue([
|
||||
{
|
||||
id: 'title-1',
|
||||
source: 'cli',
|
||||
user_id: '',
|
||||
model: 'openai/gpt-5.4',
|
||||
title: 'Docker debugging',
|
||||
started_at: 1710001000,
|
||||
ended_at: null,
|
||||
end_reason: '',
|
||||
message_count: 2,
|
||||
tool_call_count: 0,
|
||||
input_tokens: 1,
|
||||
output_tokens: 2,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
reasoning_tokens: 0,
|
||||
billing_provider: '',
|
||||
estimated_cost_usd: 0,
|
||||
actual_cost_usd: null,
|
||||
cost_status: '',
|
||||
preview: 'title preview',
|
||||
last_active: 1710001005,
|
||||
matched_message_id: 42,
|
||||
snippet: '>>>docker<<< compose up',
|
||||
rank: 0.25,
|
||||
},
|
||||
{
|
||||
id: 'content-2',
|
||||
source: 'telegram',
|
||||
user_id: '',
|
||||
model: 'openai/gpt-5.4',
|
||||
title: '',
|
||||
started_at: 1710002000,
|
||||
ended_at: null,
|
||||
end_reason: '',
|
||||
message_count: 1,
|
||||
tool_call_count: 0,
|
||||
input_tokens: 3,
|
||||
output_tokens: 4,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
reasoning_tokens: 0,
|
||||
billing_provider: '',
|
||||
estimated_cost_usd: 0,
|
||||
actual_cost_usd: null,
|
||||
cost_status: '',
|
||||
preview: 'content preview',
|
||||
last_active: 1710002001,
|
||||
matched_message_id: 7,
|
||||
snippet: '>>>docker<<< swarm',
|
||||
rank: 0.1,
|
||||
},
|
||||
])
|
||||
|
||||
const mod = await import('../../packages/server/src/services/hermes/sessions-db')
|
||||
const rows = await mod.searchSessionSummaries('docker', undefined, 10)
|
||||
|
||||
expect(prepareMock).toHaveBeenCalledWith(expect.stringContaining('messages_fts MATCH'))
|
||||
expect(rows).toHaveLength(2)
|
||||
expect(rows[0].id).toBe('title-1')
|
||||
expect(rows[0].matched_message_id).toBeNull()
|
||||
expect(rows[0].snippet).toBe('Docker debugging')
|
||||
expect(rows[1].id).toBe('content-2')
|
||||
expect(rows[1].matched_message_id).toBe(7)
|
||||
expect(rows[1].snippet).toContain('docker')
|
||||
})
|
||||
|
||||
it('falls back to LIKE search for CJK queries', async () => {
|
||||
titleAllMock.mockReturnValue([])
|
||||
contentAllMock.mockImplementation(() => {
|
||||
throw new Error('fts5 tokenizer miss')
|
||||
})
|
||||
likeAllMock.mockReturnValue([
|
||||
{
|
||||
id: 'cjk-1',
|
||||
source: 'cli',
|
||||
user_id: '',
|
||||
model: 'openai/gpt-5.4',
|
||||
title: '',
|
||||
started_at: 1710003000,
|
||||
ended_at: null,
|
||||
end_reason: '',
|
||||
message_count: 1,
|
||||
tool_call_count: 0,
|
||||
input_tokens: 3,
|
||||
output_tokens: 4,
|
||||
cache_read_tokens: 0,
|
||||
cache_write_tokens: 0,
|
||||
reasoning_tokens: 0,
|
||||
billing_provider: '',
|
||||
estimated_cost_usd: 0,
|
||||
actual_cost_usd: null,
|
||||
cost_status: '',
|
||||
preview: '中文预览',
|
||||
last_active: 1710003002,
|
||||
matched_message_id: 11,
|
||||
snippet: '这是一段记忆断裂的内容',
|
||||
rank: 0,
|
||||
},
|
||||
])
|
||||
|
||||
const mod = await import('../../packages/server/src/services/hermes/sessions-db')
|
||||
const rows = await mod.searchSessionSummaries('记忆断裂', undefined, 10)
|
||||
|
||||
expect(likeAllMock).toHaveBeenCalledWith('记忆断裂', '%记忆断裂%')
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].id).toBe('cjk-1')
|
||||
expect(rows[0].snippet).toContain('记忆断裂')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,111 +1,87 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const listSessionSummariesMock = vi.fn()
|
||||
const listSessionsMock = vi.fn()
|
||||
const listConversationSummariesMock = vi.fn()
|
||||
const getConversationDetailMock = vi.fn()
|
||||
const listConversationsMock = vi.fn(async (ctx: any) => { ctx.body = { sessions: [{ id: 'conversation-1' }] } })
|
||||
const getConversationMessagesMock = vi.fn(async (ctx: any) => { ctx.body = { session_id: ctx.params.id, messages: [] } })
|
||||
const listMock = vi.fn(async (ctx: any) => { ctx.body = { sessions: [{ id: 's1' }] } })
|
||||
const searchMock = vi.fn(async (ctx: any) => { ctx.body = { results: [{ id: 'search-1' }] } })
|
||||
const getMock = vi.fn(async (ctx: any) => { ctx.body = { session: { id: ctx.params.id } } })
|
||||
const removeMock = vi.fn(async (ctx: any) => { ctx.body = { ok: true } })
|
||||
const renameMock = vi.fn(async (ctx: any) => { ctx.body = { ok: true } })
|
||||
|
||||
vi.mock('../../packages/server/src/services/hermes/sessions-db', () => ({
|
||||
listSessionSummaries: listSessionSummariesMock,
|
||||
}))
|
||||
|
||||
vi.mock('../../packages/server/src/services/hermes/conversations', () => ({
|
||||
listConversationSummaries: listConversationSummariesMock,
|
||||
getConversationDetail: getConversationDetailMock,
|
||||
}))
|
||||
|
||||
vi.mock('../../packages/server/src/services/hermes/hermes-cli', () => ({
|
||||
listSessions: listSessionsMock,
|
||||
getSession: vi.fn(),
|
||||
deleteSession: vi.fn(),
|
||||
renameSession: vi.fn(),
|
||||
vi.mock('../../packages/server/src/controllers/hermes/sessions', () => ({
|
||||
listConversations: listConversationsMock,
|
||||
getConversationMessages: getConversationMessagesMock,
|
||||
list: listMock,
|
||||
search: searchMock,
|
||||
get: getMock,
|
||||
remove: removeMock,
|
||||
rename: renameMock,
|
||||
}))
|
||||
|
||||
describe('session routes', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules()
|
||||
listSessionSummariesMock.mockReset()
|
||||
listSessionsMock.mockReset()
|
||||
listConversationSummariesMock.mockReset()
|
||||
getConversationDetailMock.mockReset()
|
||||
listConversationsMock.mockClear()
|
||||
getConversationMessagesMock.mockClear()
|
||||
listMock.mockClear()
|
||||
searchMock.mockClear()
|
||||
getMock.mockClear()
|
||||
removeMock.mockClear()
|
||||
renameMock.mockClear()
|
||||
})
|
||||
|
||||
it('serves summaries from sqlite-backed helper when available', async () => {
|
||||
listSessionSummariesMock.mockResolvedValue([{ id: 's1' }])
|
||||
it('registers conversations, session list, and search routes', async () => {
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const layer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions')
|
||||
const paths = sessionRoutes.stack.map((entry: any) => entry.path)
|
||||
|
||||
expect(paths).toEqual(expect.arrayContaining([
|
||||
'/api/hermes/sessions/conversations',
|
||||
'/api/hermes/sessions/conversations/:id/messages',
|
||||
'/api/hermes/sessions',
|
||||
'/api/hermes/search/sessions',
|
||||
'/api/hermes/sessions/search',
|
||||
'/api/hermes/sessions/:id',
|
||||
'/api/hermes/sessions/:id/rename',
|
||||
]))
|
||||
})
|
||||
|
||||
it('delegates session search to the controller', async () => {
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const layer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/search/sessions')
|
||||
const handler = layer.stack[0]
|
||||
const ctx: any = { query: { source: 'cli', limit: '5' }, body: null }
|
||||
const ctx: any = { query: { q: 'docker', limit: '8' }, body: null, params: {} }
|
||||
|
||||
await handler(ctx)
|
||||
|
||||
expect(listSessionSummariesMock).toHaveBeenCalledWith('cli', 5)
|
||||
expect(listSessionsMock).not.toHaveBeenCalled()
|
||||
expect(ctx.body).toEqual({ sessions: [{ id: 's1' }] })
|
||||
expect(searchMock).toHaveBeenCalledWith(ctx)
|
||||
expect(ctx.body).toEqual({ results: [{ id: 'search-1' }] })
|
||||
})
|
||||
|
||||
it('falls back to CLI wrapper when sqlite summary query fails', async () => {
|
||||
listSessionSummariesMock.mockRejectedValue(new Error('sqlite unavailable'))
|
||||
listSessionsMock.mockResolvedValue([{ id: 'fallback' }])
|
||||
it('keeps the legacy search path wired to the same controller', async () => {
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const layer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions')
|
||||
const layer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions/search')
|
||||
const handler = layer.stack[0]
|
||||
const ctx: any = { query: { limit: '7' }, body: null }
|
||||
const ctx: any = { query: { q: 'docker' }, body: null, params: {} }
|
||||
|
||||
await handler(ctx)
|
||||
|
||||
expect(listSessionSummariesMock).toHaveBeenCalledWith(undefined, 7)
|
||||
expect(listSessionsMock).toHaveBeenCalledWith(undefined, 7)
|
||||
expect(ctx.body).toEqual({ sessions: [{ id: 'fallback' }] })
|
||||
expect(searchMock).toHaveBeenCalledWith(ctx)
|
||||
expect(ctx.body).toEqual({ results: [{ id: 'search-1' }] })
|
||||
})
|
||||
|
||||
it('serves live conversations with humanOnly defaulting to true', async () => {
|
||||
listConversationSummariesMock.mockResolvedValue([{ id: 'conversation-1' }])
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const layer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions/conversations')
|
||||
const handler = layer.stack[0]
|
||||
const ctx: any = { query: {}, body: null }
|
||||
|
||||
await handler(ctx)
|
||||
|
||||
expect(listConversationSummariesMock).toHaveBeenCalledWith({ humanOnly: true, source: undefined, limit: undefined })
|
||||
expect(ctx.body).toEqual({ sessions: [{ id: 'conversation-1' }] })
|
||||
})
|
||||
|
||||
it('supports disabling humanOnly and forwarding limit/source for live conversations', async () => {
|
||||
listConversationSummariesMock.mockResolvedValue([{ id: 'child-session' }])
|
||||
it('delegates conversations list and detail routes', async () => {
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const listLayer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions/conversations')
|
||||
|
||||
const listCtx: any = { query: { humanOnly: 'false', source: 'cli', limit: '25' }, body: null }
|
||||
await listLayer.stack[0](listCtx)
|
||||
|
||||
expect(listConversationSummariesMock).toHaveBeenCalledWith({ humanOnly: false, source: 'cli', limit: 25 })
|
||||
expect(listCtx.body).toEqual({ sessions: [{ id: 'child-session' }] })
|
||||
})
|
||||
|
||||
it('returns conversation detail and forwards humanOnly/source', async () => {
|
||||
getConversationDetailMock.mockResolvedValue({ session_id: 'child-session', messages: [] })
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const detailLayer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions/conversations/:id/messages')
|
||||
|
||||
const detailCtx: any = { params: { id: 'child-session' }, query: { humanOnly: 'false', source: 'discord' }, body: null, status: 200 }
|
||||
await detailLayer.stack[0](detailCtx)
|
||||
const listCtx: any = { query: {}, body: null, params: {} }
|
||||
await listLayer.stack[0](listCtx)
|
||||
expect(listConversationsMock).toHaveBeenCalledWith(listCtx)
|
||||
expect(listCtx.body).toEqual({ sessions: [{ id: 'conversation-1' }] })
|
||||
|
||||
expect(getConversationDetailMock).toHaveBeenCalledWith('child-session', { humanOnly: false, source: 'discord' })
|
||||
const detailCtx: any = { params: { id: 'child-session' }, query: {}, body: null }
|
||||
await detailLayer.stack[0](detailCtx)
|
||||
expect(getConversationMessagesMock).toHaveBeenCalledWith(detailCtx)
|
||||
expect(detailCtx.body).toEqual({ session_id: 'child-session', messages: [] })
|
||||
})
|
||||
|
||||
it('returns 404 when a conversation detail is not found', async () => {
|
||||
getConversationDetailMock.mockResolvedValue(null)
|
||||
const { sessionRoutes } = await import('../../packages/server/src/routes/hermes/sessions')
|
||||
const detailLayer = sessionRoutes.stack.find((entry: any) => entry.path === '/api/hermes/sessions/conversations/:id/messages')
|
||||
|
||||
const detailCtx: any = { params: { id: 'missing' }, query: {}, body: null, status: 200 }
|
||||
await detailLayer.stack[0](detailCtx)
|
||||
|
||||
expect(getConversationDetailMock).toHaveBeenCalledWith('missing', { humanOnly: true, source: undefined })
|
||||
expect(detailCtx.status).toBe(404)
|
||||
expect(detailCtx.body).toEqual({ error: 'Conversation not found' })
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user