feat: add bridge session commands (#743)

This commit is contained in:
ekko
2026-05-15 12:04:03 +08:00
committed by GitHub
parent 13fad02db8
commit 48dcaee6c2
22 changed files with 1180 additions and 88 deletions
+85 -45
View File
@@ -7,6 +7,11 @@ const getConversationDetailMock = vi.fn()
const getSessionDetailFromDbMock = vi.fn()
const getUsageStatsFromDbMock = vi.fn()
const getSessionMock = vi.fn()
const localListSessionsMock = vi.fn()
const localGetSessionDetailMock = vi.fn()
const localSearchSessionsMock = vi.fn()
const localDeleteSessionMock = vi.fn()
const localRenameSessionMock = vi.fn()
const getGroupChatServerMock = vi.fn()
const getLocalUsageStatsMock = vi.fn()
const getActiveProfileNameMock = vi.fn()
@@ -47,6 +52,11 @@ vi.mock('../../packages/server/src/db/hermes/sessions-db', () => ({
// Mock useLocalSessionStore to return false so we test the CLI path
vi.mock('../../packages/server/src/db/hermes/session-store', () => ({
useLocalSessionStore: () => false,
listSessions: localListSessionsMock,
searchSessions: localSearchSessionsMock,
getSessionDetail: localGetSessionDetailMock,
deleteSession: localDeleteSessionMock,
renameSession: localRenameSessionMock,
}))
vi.mock('../../packages/server/src/db/hermes/usage-store', () => ({
@@ -97,6 +107,11 @@ describe('session conversations controller', () => {
getSessionDetailFromDbMock.mockReset()
getUsageStatsFromDbMock.mockReset()
getSessionMock.mockReset()
localListSessionsMock.mockReset()
localGetSessionDetailMock.mockReset()
localSearchSessionsMock.mockReset()
localDeleteSessionMock.mockReset()
localRenameSessionMock.mockReset()
getGroupChatServerMock.mockReset()
getGroupChatServerMock.mockReturnValue(null)
getLocalUsageStatsMock.mockReset()
@@ -106,57 +121,84 @@ describe('session conversations controller', () => {
getCompressionSnapshotMock.mockReset()
})
it('prefers the DB-backed conversations summary path', async () => {
listConversationSummariesFromDbMock.mockResolvedValue([{ id: 'db-conversation' }])
it('lists conversations from the local session store', async () => {
localListSessionsMock.mockReturnValue([{
id: 'local-conversation',
source: 'cli',
model: 'gpt-5',
title: 'Local',
started_at: 1,
ended_at: null,
last_active: Math.floor(Date.now() / 1000),
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: null,
estimated_cost_usd: 0,
actual_cost_usd: null,
cost_status: '',
preview: 'preview',
workspace: null,
}])
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { query: { humanOnly: 'true', limit: '5' }, body: null }
await mod.listConversations(ctx)
expect(listConversationSummariesFromDbMock).toHaveBeenCalledWith({ source: undefined, humanOnly: true, limit: 5 })
expect(localListSessionsMock).toHaveBeenCalledWith('default', undefined, 5)
expect(listConversationSummariesMock).not.toHaveBeenCalled()
expect(ctx.body).toEqual({ sessions: [{ id: 'db-conversation' }] })
expect(ctx.body.sessions[0]).toMatchObject({ id: 'local-conversation', source: 'cli', title: 'Local' })
})
it('falls back to the CLI-export conversations summary path when the DB query fails', async () => {
listConversationSummariesFromDbMock.mockRejectedValue(new Error('db unavailable'))
listConversationSummariesMock.mockResolvedValue([{ id: 'fallback-conversation' }])
it('propagates local session store errors for conversation summaries', async () => {
localListSessionsMock.mockImplementation(() => {
throw new Error('db unavailable')
})
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { query: { humanOnly: 'false' }, body: null }
await mod.listConversations(ctx)
expect(loggerWarnMock).toHaveBeenCalled()
expect(listConversationSummariesMock).toHaveBeenCalledWith({ source: undefined, humanOnly: false, limit: undefined })
expect(ctx.body).toEqual({ sessions: [{ id: 'fallback-conversation' }] })
await expect(mod.listConversations(ctx)).rejects.toThrow('db unavailable')
})
it('prefers the DB-backed conversation detail path', async () => {
getConversationDetailFromDbMock.mockResolvedValue({ session_id: 'root', messages: [], visible_count: 0, thread_session_count: 1 })
it('gets conversation messages from the local session store', async () => {
localGetSessionDetailMock.mockReturnValue({
id: 'root',
messages: [
{ id: 1, session_id: 'root', role: 'user', content: 'hello', timestamp: 1 },
{ id: 2, session_id: 'root', role: 'command', content: '/usage', timestamp: 2 },
],
})
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { params: { id: 'root' }, query: { humanOnly: 'true' }, body: null }
await mod.getConversationMessages(ctx)
expect(getConversationDetailFromDbMock).toHaveBeenCalledWith('root', { source: undefined, humanOnly: true })
expect(localGetSessionDetailMock).toHaveBeenCalledWith('root')
expect(getConversationDetailMock).not.toHaveBeenCalled()
expect(ctx.body).toEqual({ session_id: 'root', messages: [], visible_count: 0, thread_session_count: 1 })
expect(ctx.body).toEqual({
session_id: 'root',
messages: [{ id: 1, session_id: 'root', role: 'user', content: 'hello', timestamp: 1 }],
visible_count: 1,
thread_session_count: 1,
})
})
it('falls back to the CLI-export conversation detail path when the DB query throws', async () => {
getConversationDetailFromDbMock.mockRejectedValue(new Error('db unavailable'))
getConversationDetailMock.mockResolvedValue({ session_id: 'root', messages: [{ id: 1 }], visible_count: 1, thread_session_count: 1 })
it('returns 404 when local conversation detail is missing', async () => {
localGetSessionDetailMock.mockReturnValue(null)
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const ctx: any = { params: { id: 'root' }, query: { humanOnly: 'false' }, body: null }
await mod.getConversationMessages(ctx)
expect(loggerWarnMock).toHaveBeenCalled()
expect(getConversationDetailMock).toHaveBeenCalledWith('root', { source: undefined, humanOnly: false })
expect(ctx.body).toEqual({ session_id: 'root', messages: [{ id: 1 }], visible_count: 1, thread_session_count: 1 })
expect(ctx.status).toBe(404)
expect(ctx.body).toEqual({ error: 'Conversation not found' })
})
it('merges native state.db usage analytics with local Web UI usage for the requested period', async () => {
it('returns native state.db usage analytics for the requested period', async () => {
const today = new Date().toISOString().slice(0, 10)
getLocalUsageStatsMock.mockReturnValue({
input_tokens: 10,
@@ -193,34 +235,33 @@ describe('session conversations controller', () => {
const ctx: any = { query: { days: '2' }, body: null }
await mod.usageStats(ctx)
expect(getLocalUsageStatsMock).toHaveBeenCalledWith('default', 2)
expect(getLocalUsageStatsMock).not.toHaveBeenCalled()
expect(getUsageStatsFromDbMock).toHaveBeenCalledWith(2)
expect(ctx.body).toMatchObject({
total_input_tokens: 30,
total_output_tokens: 15,
total_cache_read_tokens: 6,
total_cache_write_tokens: 3,
total_reasoning_tokens: 9,
total_sessions: 3,
total_input_tokens: 20,
total_output_tokens: 10,
total_cache_read_tokens: 4,
total_cache_write_tokens: 2,
total_reasoning_tokens: 6,
total_sessions: 2,
total_cost: 0.02,
total_api_calls: 7,
period_days: 2,
})
expect(ctx.body.model_usage).toEqual([
{ model: 'hermes-model', input_tokens: 20, output_tokens: 10, cache_read_tokens: 4, cache_write_tokens: 2, reasoning_tokens: 6, sessions: 2 },
{ model: 'local-model', input_tokens: 10, output_tokens: 5, cache_read_tokens: 2, cache_write_tokens: 1, reasoning_tokens: 3, sessions: 1 },
])
expect(ctx.body.daily_usage.find((row: any) => row.date === today)).toMatchObject({
input_tokens: 30,
output_tokens: 15,
cache_read_tokens: 6,
cache_write_tokens: 3,
sessions: 3,
input_tokens: 20,
output_tokens: 10,
cache_read_tokens: 4,
cache_write_tokens: 2,
sessions: 2,
cost: 0.02,
})
})
it('keeps blank model usage under an unknown bucket', async () => {
it('keeps blank model usage as returned by state.db analytics', async () => {
getLocalUsageStatsMock.mockReturnValue({
input_tokens: 3,
output_tokens: 1,
@@ -253,14 +294,14 @@ describe('session conversations controller', () => {
await mod.usageStats(ctx)
expect(ctx.body.model_usage).toEqual([
{ model: 'unknown', input_tokens: 5, output_tokens: 2, cache_read_tokens: 3, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 2 },
{ model: ' ', input_tokens: 2, output_tokens: 1, cache_read_tokens: 1, cache_write_tokens: 1, reasoning_tokens: 0, sessions: 1 },
])
})
describe('exportSession', () => {
it('returns session as JSON download with correct headers (full mode)', async () => {
const sessionData = { id: 'abc-123', title: 'Test Session', messages: [{ id: 1, role: 'user', content: 'hello' }] }
getSessionDetailFromDbMock.mockResolvedValue(sessionData)
localGetSessionDetailMock.mockReturnValue(sessionData)
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const setMock = vi.fn()
@@ -268,7 +309,7 @@ describe('session conversations controller', () => {
await mod.exportSession(ctx)
expect(getSessionDetailFromDbMock).toHaveBeenCalledWith('abc-123')
expect(localGetSessionDetailMock).toHaveBeenCalledWith('abc-123')
expect(setMock).toHaveBeenCalledWith('Content-Disposition', expect.stringContaining('abc-123'))
expect(setMock).toHaveBeenCalledWith('Content-Type', 'application/json')
expect(ctx.status).toBeUndefined()
@@ -284,7 +325,7 @@ describe('session conversations controller', () => {
{ id: 2, role: 'assistant', content: 'hi', timestamp: 1700000001 },
],
}
getSessionDetailFromDbMock.mockResolvedValue(sessionData)
localGetSessionDetailMock.mockReturnValue(sessionData)
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const setMock = vi.fn()
@@ -301,7 +342,7 @@ describe('session conversations controller', () => {
})
it('returns 404 when session not found', async () => {
getSessionDetailFromDbMock.mockResolvedValue(null)
localGetSessionDetailMock.mockReturnValue(null)
getSessionMock.mockResolvedValue(null)
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
@@ -315,8 +356,7 @@ describe('session conversations controller', () => {
it('falls back to CLI when DB query fails', async () => {
const sessionData = { id: 'cli-123', title: 'CLI Session', messages: [] }
getSessionDetailFromDbMock.mockRejectedValue(new Error('db unavailable'))
getSessionMock.mockResolvedValue(sessionData)
localGetSessionDetailMock.mockReturnValue(sessionData)
const mod = await import('../../packages/server/src/controllers/hermes/sessions')
const setMock = vi.fn()
@@ -324,7 +364,7 @@ describe('session conversations controller', () => {
await mod.exportSession(ctx)
expect(getSessionMock).toHaveBeenCalledWith('cli-123')
expect(localGetSessionDetailMock).toHaveBeenCalledWith('cli-123')
expect(JSON.parse(ctx.body)).toMatchObject({ id: 'cli-123' })
})
})
+30 -19
View File
@@ -11,7 +11,7 @@ type UpdateControllerMocks = {
async function loadUpdateController(overrides: Partial<UpdateControllerMocks> = {}) {
const execFileSync = overrides.execFileSync ?? vi.fn().mockReturnValue('updated')
const unref = overrides.unref ?? vi.fn()
const spawn = overrides.spawn ?? vi.fn(() => ({ unref }))
const spawn = overrides.spawn ?? vi.fn(() => ({ unref, on: vi.fn() }))
const existsSync = overrides.existsSync ?? vi.fn(() => true)
vi.resetModules()
@@ -80,7 +80,11 @@ describe('update controller', () => {
const globalPrefix = getNodePrefix()
const cliScript = getGlobalCliScript(globalPrefix)
const execFileSync = vi.fn((_command: string, args: string[]) => {
if (args[1] === 'prefix') return globalPrefix
if (args[1] === 'root') {
return process.platform === 'win32'
? join(globalPrefix, 'node_modules')
: join(globalPrefix, 'lib', 'node_modules')
}
return 'updated'
})
const { handleUpdate, mocks } = await loadUpdateController({ execFileSync })
@@ -107,7 +111,7 @@ describe('update controller', () => {
expect(mocks.execFileSync).toHaveBeenCalledWith(
process.execPath,
[npmCli, 'prefix', '-g'],
[npmCli, 'root', '-g'],
expect.objectContaining({
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
@@ -125,7 +129,6 @@ describe('update controller', () => {
}),
)
expect(mocks.unref).toHaveBeenCalledOnce()
expect(exitSpy).toHaveBeenCalledWith(0)
})
it('falls back to the default port when PORT is not set', async () => {
@@ -143,6 +146,29 @@ describe('update controller', () => {
)
})
it('does not log a restart error when the restart helper exits successfully', async () => {
const handlers = new Map<string, (...args: any[]) => void>()
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined)
const unref = vi.fn()
const restart = {
unref,
on: vi.fn((event: string, handler: (...args: any[]) => void) => {
handlers.set(event, handler)
return restart
}),
}
const spawn = vi.fn(() => restart)
const { handleUpdate } = await loadUpdateController({ spawn, unref })
const ctx = createMockCtx()
await handleUpdate(ctx)
vi.runAllTimers()
handlers.get('exit')?.(0, null)
expect(errorSpy).not.toHaveBeenCalled()
errorSpy.mockRestore()
})
it('returns a 500 with stderr when installation fails', async () => {
const execFileSync = vi.fn(() => {
const error = new Error('install failed') as Error & { stderr?: string }
@@ -160,19 +186,4 @@ describe('update controller', () => {
expect(exitSpy).not.toHaveBeenCalled()
})
it('fails closed instead of falling back to PATH npm when the current Node install has no npm CLI', async () => {
const { handleUpdate, mocks } = await loadUpdateController({ existsSync: vi.fn(() => false) })
const ctx = createMockCtx()
await handleUpdate(ctx)
expect(ctx.status).toBe(500)
expect(ctx.body).toEqual({
success: false,
message: expect.stringContaining(`Unable to locate npm CLI for ${process.execPath}`),
})
expect(mocks.execFileSync).not.toHaveBeenCalled()
expect(mocks.spawn).not.toHaveBeenCalled()
expect(exitSpy).not.toHaveBeenCalled()
})
})
+11 -11
View File
@@ -113,7 +113,7 @@ describe('native-style Hermes usage analytics DB aggregation', () => {
profileDir = null
})
it('sums direct state.db rows in the period while excluding local api_server copies', async () => {
it('sums direct state.db rows in the period', async () => {
const now = 1_700_000_000
profileDir = createStateDb(true)
profileMock.getActiveProfileDir.mockReturnValue(profileDir)
@@ -190,17 +190,17 @@ describe('native-style Hermes usage analytics DB aggregation', () => {
const result = await mod.getUsageStatsFromDb(30, now)
expect(result).toMatchObject({
input_tokens: 138,
output_tokens: 75,
input_tokens: 638,
output_tokens: 575,
cache_read_tokens: 16,
cache_write_tokens: 3,
reasoning_tokens: 7,
sessions: 4,
total_api_calls: 3,
sessions: 5,
total_api_calls: 8,
})
expect(result.cost).toBeCloseTo(0.043)
expect(result.cost).toBeCloseTo(5.043)
expect(result.by_model).toEqual([
{ model: 'gpt-5', input_tokens: 107, output_tokens: 53, cache_read_tokens: 11, cache_write_tokens: 2, reasoning_tokens: 5, sessions: 2 },
{ model: 'gpt-5', input_tokens: 607, output_tokens: 553, cache_read_tokens: 11, cache_write_tokens: 2, reasoning_tokens: 5, sessions: 3 },
{ model: 'tool-model', input_tokens: 30, output_tokens: 20, cache_read_tokens: 5, cache_write_tokens: 1, reasoning_tokens: 2, sessions: 1 },
])
expect(result.by_day).toHaveLength(2)
@@ -216,14 +216,14 @@ describe('native-style Hermes usage analytics DB aggregation', () => {
})
expect(result.by_day[1]).toMatchObject({
date: day(now),
input_tokens: 131,
output_tokens: 72,
input_tokens: 631,
output_tokens: 572,
cache_read_tokens: 15,
cache_write_tokens: 3,
sessions: 3,
sessions: 4,
errors: 0,
})
expect(result.by_day[1].cost).toBeCloseTo(0.038)
expect(result.by_day[1].cost).toBeCloseTo(5.038)
})
it('keeps analytics working against older state.db schemas without api_call_count', async () => {