feat: v0.5.16 - migrate to Responses API (#586)

* refactor: migrate from /v1/runs to /v1/responses streaming API

Replace EventSource-based polling with direct SSE streaming via the
/v1/responses endpoint across all server-side callers (chat-run-socket,
context-compressor, gateway-client, agent-clients). Messages are now
written to DB in real-time during streaming, eliminating post-run sync.
Frontend chat store adds tool_call_id tracking for deduplication.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* chore: bump version to 0.5.16 and add changelog

- Persist real API usage to usage table on response.completed
- Remove unused codex_reasoning_items field from message schema
- Fix unused variable warnings in chat-run-socket
- Bump version to 0.5.16
- Add changelog entries for 0.5.16 (8 locales)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
ekko
2026-05-10 02:49:58 +08:00
committed by GitHub
parent a36c0a3095
commit 50122c5ff8
19 changed files with 843 additions and 866 deletions
@@ -1,4 +1,3 @@
import { EventSource } from 'eventsource'
import type { StoredMessage, GatewayCaller } from './types'
import {
buildSummarizationSystemPrompt,
@@ -6,12 +5,11 @@ import {
buildIncrementalUpdatePrompt,
} from './prompt'
import { updateUsage } from '../../../db/hermes/usage-store'
import { getSessionDetailFromDbWithProfile } from '../../../db/hermes/sessions-db'
import { logger } from '../../logger'
/**
* Calls Hermes /v1/runs to produce LLM-generated summaries.
* Uses non-streaming EventSource to wait for run.completed.
* Calls Hermes /v1/responses to produce LLM-generated summaries.
* The context engine owns history assembly; Responses storage/chaining is not used.
*/
export class GatewaySummarizer implements GatewayCaller {
private timeoutMs: number
@@ -29,13 +27,11 @@ export class GatewaySummarizer implements GatewayCaller {
profile: string,
previousSummary?: string,
): Promise<{ summary: string; sessionId: string }> {
// Build conversation_history from messages
const history: Array<{ role: string; content: string }> = messages.map(m => ({
role: 'user',
content: `[${m.senderName}]: ${m.content}`,
}))
// Inject previous summary for incremental update
if (previousSummary) {
history.unshift(
{ role: 'user', content: `[Previous summary]\n${previousSummary}` },
@@ -47,10 +43,7 @@ export class GatewaySummarizer implements GatewayCaller {
? buildIncrementalUpdatePrompt()
: buildFullSummaryPrompt()
const sessionId = Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
// POST /v1/runs
const res = await fetch(`${upstream}/v1/runs`, {
const res = await fetch(`${upstream.replace(/\/$/, '')}/v1/responses`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@@ -60,98 +53,122 @@ export class GatewaySummarizer implements GatewayCaller {
input: userPrompt,
instructions: systemPrompt || buildSummarizationSystemPrompt(),
conversation_history: history,
session_id: sessionId,
stream: true,
store: false,
}),
signal: AbortSignal.timeout(this.timeoutMs),
})
if (!res.ok) {
throw new Error(`Summarization run failed: ${res.status}`)
throw new Error(`Summarization response failed: ${res.status}`)
}
if (!res.body) {
throw new Error('Summarization response stream missing')
}
const { run_id } = await res.json() as { run_id: string }
let output = ''
for await (const frame of readSseFrames(res.body)) {
let parsed: any
try {
parsed = JSON.parse(frame.data)
} catch {
continue
}
const eventType = parsed.type || frame.event || parsed.event
try {
const output = await this.pollForResult(upstream, apiKey, run_id, sessionId, roomId, profile)
return { summary: output, sessionId }
} finally {
// Note: session cleanup is handled by the caller (compressor.ts)
if (eventType === 'response.output_text.delta' && parsed.delta) {
output += parsed.delta
continue
}
if (eventType === 'response.completed') {
const response = parsed.response || parsed
const finalText = extractResponseText(response)
if (!output && finalText) output = finalText
const usage = response.usage || {}
updateUsage(roomId, {
inputTokens: usage.input_tokens ?? usage.inputTokens ?? 0,
outputTokens: usage.output_tokens ?? usage.outputTokens ?? 0,
cacheReadTokens: usage.cache_read_tokens ?? usage.cacheReadTokens ?? 0,
cacheWriteTokens: usage.cache_write_tokens ?? usage.cacheWriteTokens ?? 0,
reasoningTokens: usage.reasoning_tokens ?? usage.reasoningTokens ?? 0,
model: response.model || '',
profile,
})
logger.debug(`[GatewaySummarizer] Recorded response usage for compression room ${roomId} (profile=${profile}): input=${usage.input_tokens ?? 0}, output=${usage.output_tokens ?? 0}`)
if (!output || output.trim() === '') {
throw new Error('Empty summarization response')
}
return { summary: output.trim(), sessionId: '' }
}
if (eventType === 'response.failed') {
throw new Error(parsed.error?.message || parsed.error || 'Summarization response failed')
}
}
throw new Error('Summarization response stream ended without a terminal event')
}
private pollForResult(upstream: string, apiKey: string | null, runId: string, sessionId: string, roomId: string, profile: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
const timer = setTimeout(() => {
source.close()
reject(new Error('Summarization timed out'))
}, this.timeoutMs)
const eventsUrl = new URL(`${upstream}/v1/runs/${runId}/events`)
// Use Authorization header instead of query parameter for better compatibility
const eventSourceInit: any = apiKey ? {
fetch: (url: string, init: any = {}) => fetch(url, {
...init,
headers: {
...(init.headers || {}),
Authorization: `Bearer ${apiKey}`,
},
}),
} : {}
// @ts-ignore - eventsource library types are too strict
const source = new EventSource(eventsUrl.toString(), eventSourceInit)
source.onmessage = async (event: MessageEvent) => {
try {
const parsed = JSON.parse(event.data)
if (parsed.event === 'run.completed') {
clearTimeout(timer)
// Record usage data from Hermes state.db BEFORE closing source
// This ensures we fetch usage before sessionCleaner can delete it
try {
const detail = await getSessionDetailFromDbWithProfile(sessionId, profile)
if (detail) {
updateUsage(roomId, {
inputTokens: detail.input_tokens,
outputTokens: detail.output_tokens,
cacheReadTokens: detail.cache_read_tokens,
cacheWriteTokens: detail.cache_write_tokens,
reasoningTokens: detail.reasoning_tokens,
model: detail.model,
profile,
})
logger.debug(`[GatewaySummarizer] Recorded usage for compression room ${roomId} (session ${sessionId}, profile=${profile}): input=${detail.input_tokens}, output=${detail.output_tokens}`)
} else {
logger.warn(`[GatewaySummarizer] Failed to get session detail for ${sessionId} (profile=${profile})`)
}
} catch (err: any) {
logger.warn(err, '[GatewaySummarizer] Failed to record usage from DB')
}
source.close()
const output = parsed.output
if (!output || typeof output !== 'string' || output.trim() === '') {
reject(new Error('Empty summarization response'))
return
}
resolve(output.trim())
} else if (parsed.event === 'run.failed') {
clearTimeout(timer)
source.close()
reject(new Error(parsed.error || 'Summarization run failed'))
}
} catch { /* ignore parse errors for non-JSON events */ }
}
source.onerror = () => {
clearTimeout(timer)
source.close()
reject(new Error('Summarization SSE connection error'))
}
})
}
}
async function* readSseFrames(stream: ReadableStream<Uint8Array>): AsyncGenerator<{ event?: string; data: string }> {
const decoder = new TextDecoder()
const reader = stream.getReader()
let buffer = ''
try {
while (true) {
const { done, value } = await reader.read()
if (done) break
buffer += decoder.decode(value, { stream: true })
let boundary = buffer.indexOf('\n\n')
while (boundary >= 0) {
const raw = buffer.slice(0, boundary)
buffer = buffer.slice(boundary + 2)
const frame = parseSseFrame(raw)
if (frame?.data) yield frame
boundary = buffer.indexOf('\n\n')
}
}
buffer += decoder.decode()
const frame = parseSseFrame(buffer)
if (frame?.data) yield frame
} finally {
reader.releaseLock()
}
}
function parseSseFrame(raw: string): { event?: string; data: string } | null {
let event: string | undefined
const data: string[] = []
for (const line of raw.split(/\r?\n/)) {
if (!line || line.startsWith(':')) continue
if (line.startsWith('event:')) {
event = line.slice(6).trim()
} else if (line.startsWith('data:')) {
data.push(line.slice(5).trimStart())
}
}
if (data.length === 0) return null
return { event, data: data.join('\n') }
}
function extractResponseText(response: any): string {
const output = Array.isArray(response?.output) ? response.output : []
const parts: string[] = []
for (const item of output) {
if (item.type !== 'message') continue
const content = Array.isArray(item.content) ? item.content : []
for (const part of content) {
if (part.type === 'output_text' || part.type === 'text') {
parts.push(part.text || '')
}
}
}
if (parts.length > 0) return parts.join('')
return typeof response?.output_text === 'string' ? response.output_text : ''
}