Files
hermes-ui/tests/server/response-stream-reasoning-storage.test.ts
T
toor 1e9ea89d86
Build / build (push) Canceled after 0s
NPM Lockfile Check / npm ci --ignore-scripts (push) Canceled after 0s
Playwright / e2e (push) Canceled after 0s
chore: bootstrap hermes-ui skeleton from 0.6.29 snapshot
Import the client, server, skills, tests, and build scripts as the starting
point for the rebuild, then trim the surfaces this project does not ship:
Electron desktop packaging, the marketing website, and the ESP32 firmware
tree. CI keeps the web build, e2e, lockfile, release, and Docker workflows.

Rename the package and CLI entry points to hermes-ui, drop the prepare hook so
npm ci no longer triggers a build, and drop the removed tsconfig and nodemon
references. Rewrite AGENTS.md, ARCHITECTURE.md, DEVELOPMENT.md, and the READMEs
so the documented package map and hard rules match this repository.

reference_code/ is ignored: it is a read-only snapshot, not an input to the build.
2026-10-16 10:34:35 +08:00

132 lines
4.7 KiB
TypeScript

import { describe, expect, it, vi, beforeEach } from 'vitest'
import { applyResponseStreamEvent, flushResponseRunToDb } from '../../packages/server/src/services/hermes/run-chat/response-stream'
import type { SessionState } from '../../packages/server/src/services/hermes/run-chat/types'
const { addMessageMock } = vi.hoisted(() => ({
addMessageMock: vi.fn(),
}))
vi.mock('../../packages/server/src/db/hermes/session-store', () => ({
addMessage: addMessageMock,
}))
describe('response stream reasoning storage', () => {
beforeEach(() => {
addMessageMock.mockReset()
})
it('buffers reasoning without creating an empty assistant message before text arrives', () => {
const state: SessionState = { messages: [], isWorking: false, events: [], queue: [] }
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.created', {
response: { id: 'resp-1', status: 'in_progress' },
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.reasoning.delta', {
delta: 'think first',
})
expect(state.messages).toEqual([])
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.output_text.delta', {
delta: 'answer',
})
expect(state.messages).toEqual([
expect.objectContaining({
role: 'assistant',
content: 'answer',
reasoning: 'think first',
reasoning_content: 'think first',
}),
])
})
it('keeps reasoning deltas in session memory across tool boundaries', () => {
const state: SessionState = { messages: [], isWorking: false, events: [], queue: [] }
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.created', {
response: { id: 'resp-1', status: 'in_progress' },
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.reasoning.delta', {
delta: 'think before. ',
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.output_text.delta', {
delta: 'Before tool.',
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.output_item.done', {
item: { type: 'function_call', call_id: 'tool-1', name: 'Bash', arguments: '{}' },
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.reasoning.delta', {
delta: 'think after. ',
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.output_item.done', {
item: { type: 'function_call_output', call_id: 'tool-1', output: 'tool output' },
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.output_text.delta', {
delta: 'After tool.',
})
expect(state.messages.map(message => message.role)).toEqual(['assistant', 'assistant', 'tool', 'assistant'])
expect(state.messages[0]).toMatchObject({
role: 'assistant',
content: 'Before tool.',
reasoning: 'think before. think after. ',
reasoning_content: 'think before. think after. ',
})
expect(state.messages[3]).toMatchObject({
role: 'assistant',
content: 'After tool.',
})
})
it('flushes reasoning fields to message storage', () => {
const state: SessionState = { messages: [], isWorking: false, events: [], queue: [] }
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.created', {
response: { id: 'resp-1', status: 'in_progress' },
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.reasoning.delta', {
delta: 'stored thinking',
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.output_text.delta', {
delta: 'answer',
})
flushResponseRunToDb(state, 'session-1')
expect(addMessageMock).toHaveBeenCalledWith(expect.objectContaining({
session_id: 'session-1',
role: 'assistant',
content: 'answer',
reasoning: 'stored thinking',
reasoning_content: 'stored thinking',
}))
})
it('deduplicates final reasoning snapshots after streamed reasoning deltas', () => {
const state: SessionState = { messages: [], isWorking: false, events: [], queue: [] }
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.created', {
response: { id: 'resp-1', status: 'in_progress' },
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.reasoning.delta', {
delta: 'Need inspect.',
})
applyResponseStreamEvent(state, 'session-1', 'run-1', 'response.completed', {
response: {
id: 'resp-1',
output: [
{ type: 'reasoning', summary: [{ text: 'Need inspect.' }] },
{ type: 'message', content: [{ type: 'output_text', text: 'answer' }] },
],
},
})
expect(state.messages[0]).toMatchObject({
content: 'answer',
reasoning: 'Need inspect.',
reasoning_content: 'Need inspect.',
})
})
})