2026-04-21 12:35:48 +08:00
|
|
|
import * as hermesCli from '../../services/hermes/hermes-cli'
|
2026-04-22 02:09:58 +02:00
|
|
|
import { getConversationDetail, listConversationSummaries } from '../../services/hermes/conversations'
|
2026-04-23 04:49:00 +02:00
|
|
|
import {
|
|
|
|
|
getConversationDetailFromDb,
|
|
|
|
|
listConversationSummariesFromDb,
|
|
|
|
|
} from '../../db/hermes/conversations-db'
|
2026-04-25 16:23:33 +02:00
|
|
|
import { getSessionDetailFromDb, listSessionSummaries, searchSessionSummaries } from '../../db/hermes/sessions-db'
|
2026-04-22 16:14:50 +08:00
|
|
|
import { deleteUsage, getUsage, getUsageBatch } from '../../db/hermes/usage-store'
|
|
|
|
|
import { getModelContextLength } from '../../services/hermes/model-context'
|
2026-04-24 20:41:14 +08:00
|
|
|
import type { ConversationDetail, ConversationSummary } from '../../services/hermes/conversations'
|
|
|
|
|
import { getActiveProfileName } from '../../services/hermes/hermes-profile'
|
|
|
|
|
import { getGroupChatServer } from '../../routes/hermes/group-chat'
|
2026-04-21 12:35:48 +08:00
|
|
|
import { logger } from '../../services/logger'
|
|
|
|
|
|
2026-04-22 02:09:58 +02:00
|
|
|
function parseHumanOnly(value: unknown): boolean {
|
|
|
|
|
if (typeof value !== 'string') return true
|
|
|
|
|
return value !== 'false' && value !== '0'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseLimit(value: unknown): number | undefined {
|
|
|
|
|
if (typeof value !== 'string') return undefined
|
|
|
|
|
const parsed = parseInt(value, 10)
|
|
|
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 20:41:14 +08:00
|
|
|
function getPendingDeletedSessionIds(): Set<string> {
|
|
|
|
|
return getGroupChatServer()?.getStorage().getPendingDeletedSessionIds() || new Set<string>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isPendingDeletedSession(sessionId: string): boolean {
|
|
|
|
|
return getPendingDeletedSessionIds().has(sessionId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function filterPendingDeletedSessions<T extends { id: string }>(items: T[]): T[] {
|
|
|
|
|
const pendingIds = getPendingDeletedSessionIds()
|
|
|
|
|
if (pendingIds.size === 0) return items
|
|
|
|
|
return items.filter(item => !pendingIds.has(item.id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function filterPendingDeletedConversationSummaries(items: ConversationSummary[]): ConversationSummary[] {
|
|
|
|
|
return filterPendingDeletedSessions(items)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasPendingDeletedConversation(detail: ConversationDetail): boolean {
|
|
|
|
|
const pendingIds = getPendingDeletedSessionIds()
|
|
|
|
|
if (pendingIds.size === 0) return false
|
|
|
|
|
if (pendingIds.has(detail.session_id)) return true
|
|
|
|
|
return detail.messages.some(message => pendingIds.has(message.session_id))
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 16:23:33 +02:00
|
|
|
function hasPendingDeletedSessionDetail(session: { id: string; messages?: Array<{ session_id?: string | null }> }): boolean {
|
|
|
|
|
const pendingIds = getPendingDeletedSessionIds()
|
|
|
|
|
if (pendingIds.size === 0) return false
|
|
|
|
|
if (pendingIds.has(session.id)) return true
|
|
|
|
|
return (session.messages || []).some(message => {
|
|
|
|
|
const messageSessionId = message.session_id || session.id
|
|
|
|
|
return pendingIds.has(messageSessionId)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 20:41:14 +08:00
|
|
|
function getGroupChatStorage() {
|
|
|
|
|
return getGroupChatServer()?.getStorage() || null
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 02:09:58 +02:00
|
|
|
export async function listConversations(ctx: any) {
|
|
|
|
|
const source = (ctx.query.source as string) || undefined
|
|
|
|
|
const humanOnly = parseHumanOnly(ctx.query.humanOnly)
|
|
|
|
|
const limit = parseLimit(ctx.query.limit)
|
2026-04-23 04:49:00 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const sessions = await listConversationSummariesFromDb({ source, humanOnly, limit })
|
2026-04-24 20:41:14 +08:00
|
|
|
ctx.body = { sessions: filterPendingDeletedConversationSummaries(sessions) }
|
2026-04-23 04:49:00 +02:00
|
|
|
return
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.warn(err, 'Hermes Conversation DB: summary query failed, falling back to CLI export')
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 02:09:58 +02:00
|
|
|
const sessions = await listConversationSummaries({ source, humanOnly, limit })
|
2026-04-24 20:41:14 +08:00
|
|
|
ctx.body = { sessions: filterPendingDeletedConversationSummaries(sessions) }
|
2026-04-22 02:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getConversationMessages(ctx: any) {
|
|
|
|
|
const source = (ctx.query.source as string) || undefined
|
|
|
|
|
const humanOnly = parseHumanOnly(ctx.query.humanOnly)
|
2026-04-23 04:49:00 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const detail = await getConversationDetailFromDb(ctx.params.id, { source, humanOnly })
|
2026-04-24 20:41:14 +08:00
|
|
|
if (!detail || hasPendingDeletedConversation(detail)) {
|
2026-04-23 04:49:00 +02:00
|
|
|
ctx.status = 404
|
|
|
|
|
ctx.body = { error: 'Conversation not found' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.body = detail
|
|
|
|
|
return
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.warn(err, 'Hermes Conversation DB: detail query failed, falling back to CLI export')
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-22 02:09:58 +02:00
|
|
|
const detail = await getConversationDetail(ctx.params.id, { source, humanOnly })
|
2026-04-24 20:41:14 +08:00
|
|
|
if (!detail || hasPendingDeletedConversation(detail)) {
|
2026-04-22 02:09:58 +02:00
|
|
|
ctx.status = 404
|
|
|
|
|
ctx.body = { error: 'Conversation not found' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.body = detail
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 12:35:48 +08:00
|
|
|
export async function list(ctx: any) {
|
|
|
|
|
const source = (ctx.query.source as string) || undefined
|
|
|
|
|
const limit = ctx.query.limit ? parseInt(ctx.query.limit as string, 10) : undefined
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const sessions = await listSessionSummaries(source, limit && limit > 0 ? limit : 2000)
|
2026-04-24 20:41:14 +08:00
|
|
|
ctx.body = { sessions: filterPendingDeletedSessions(sessions) }
|
2026-04-21 12:35:48 +08:00
|
|
|
return
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.warn(err, 'Hermes Session DB: summary query failed, falling back to CLI')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sessions = await hermesCli.listSessions(source, limit)
|
2026-04-24 20:41:14 +08:00
|
|
|
ctx.body = { sessions: filterPendingDeletedSessions(sessions) }
|
2026-04-21 12:35:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-22 14:00:34 +08:00
|
|
|
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)
|
2026-04-24 20:41:14 +08:00
|
|
|
ctx.body = { results: filterPendingDeletedSessions(results) }
|
2026-04-22 14:00:34 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(err, 'Hermes Session DB: search failed')
|
|
|
|
|
ctx.status = 500
|
|
|
|
|
ctx.body = { error: 'Failed to search sessions' }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 12:35:48 +08:00
|
|
|
export async function get(ctx: any) {
|
2026-04-24 20:41:14 +08:00
|
|
|
if (isPendingDeletedSession(ctx.params.id)) {
|
|
|
|
|
ctx.status = 404
|
|
|
|
|
ctx.body = { error: 'Session not found' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 16:23:33 +02:00
|
|
|
try {
|
|
|
|
|
const session = await getSessionDetailFromDb(ctx.params.id)
|
|
|
|
|
if (session) {
|
|
|
|
|
if (hasPendingDeletedSessionDetail(session)) {
|
|
|
|
|
ctx.status = 404
|
|
|
|
|
ctx.body = { error: 'Session not found' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.body = { session }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.warn(err, 'Hermes Session DB: detail query failed, falling back to CLI')
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 12:35:48 +08:00
|
|
|
const session = await hermesCli.getSession(ctx.params.id)
|
|
|
|
|
if (!session) {
|
|
|
|
|
ctx.status = 404
|
|
|
|
|
ctx.body = { error: 'Session not found' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.body = { session }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function remove(ctx: any) {
|
2026-04-24 20:41:14 +08:00
|
|
|
const sessionId = ctx.params.id
|
|
|
|
|
const storage = getGroupChatStorage()
|
|
|
|
|
const currentProfile = getActiveProfileName()
|
|
|
|
|
const mapped = storage?.getSessionProfile(sessionId) || null
|
|
|
|
|
|
|
|
|
|
logger.info('[remove] sessionId=%s, currentProfile=%s, mapped=%j', sessionId, currentProfile, mapped)
|
|
|
|
|
|
|
|
|
|
if (!mapped) {
|
|
|
|
|
logger.info('[remove] no mapping found, deleting directly')
|
|
|
|
|
const ok = await hermesCli.deleteSession(sessionId)
|
|
|
|
|
if (!ok) {
|
|
|
|
|
ctx.status = 500
|
|
|
|
|
ctx.body = { error: 'Failed to delete session' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
deleteUsage(sessionId)
|
|
|
|
|
ctx.body = { ok: true }
|
2026-04-21 12:35:48 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-04-24 20:41:14 +08:00
|
|
|
|
|
|
|
|
if (mapped.profile_name === currentProfile) {
|
|
|
|
|
logger.info('[remove] same profile, deleting directly')
|
|
|
|
|
const ok = await hermesCli.deleteSession(sessionId)
|
|
|
|
|
if (!ok) {
|
|
|
|
|
ctx.status = 500
|
|
|
|
|
ctx.body = { error: 'Failed to delete session' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
storage?.deleteSessionProfile(sessionId)
|
|
|
|
|
deleteUsage(sessionId)
|
|
|
|
|
ctx.body = { ok: true }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info('[remove] cross-profile detected, enqueued deferred delete for profile=%s', mapped.profile_name)
|
|
|
|
|
storage?.enqueuePendingSessionDelete(sessionId, mapped.profile_name)
|
|
|
|
|
deleteUsage(sessionId)
|
|
|
|
|
ctx.body = { ok: true, deferred: true }
|
2026-04-21 12:35:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-22 16:14:50 +08:00
|
|
|
export async function usageBatch(ctx: any) {
|
|
|
|
|
const ids = (ctx.query.ids as string)
|
|
|
|
|
if (!ids) {
|
|
|
|
|
ctx.body = {}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const idList = ids.split(',').filter(Boolean)
|
|
|
|
|
ctx.body = getUsageBatch(idList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function usageSingle(ctx: any) {
|
|
|
|
|
const result = getUsage(ctx.params.id)
|
|
|
|
|
if (!result) {
|
|
|
|
|
ctx.body = { input_tokens: 0, output_tokens: 0 }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.body = result
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 12:35:48 +08:00
|
|
|
export async function rename(ctx: any) {
|
|
|
|
|
const { title } = ctx.request.body as { title?: string }
|
|
|
|
|
if (!title || typeof title !== 'string') {
|
|
|
|
|
ctx.status = 400
|
|
|
|
|
ctx.body = { error: 'title is required' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const ok = await hermesCli.renameSession(ctx.params.id, title.trim())
|
|
|
|
|
if (!ok) {
|
|
|
|
|
ctx.status = 500
|
|
|
|
|
ctx.body = { error: 'Failed to rename session' }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.body = { ok: true }
|
|
|
|
|
}
|
2026-04-22 16:14:50 +08:00
|
|
|
|
|
|
|
|
export async function contextLength(ctx: any) {
|
|
|
|
|
const profile = (ctx.query.profile as string) || undefined
|
|
|
|
|
ctx.body = { context_length: getModelContextLength(profile) }
|
|
|
|
|
}
|