feat: add token usage tracking, context display, and dynamic context length (#132)

* fix: specify TS_NODE_PROJECT for dev:server script

ts-node/register resolves tsconfig from the entry file upward,
finding the root solution-style tsconfig.json (no compilerOptions).
This causes target to default to ES3, breaking MapIterator spread
syntax (TS2802). Set TS_NODE_PROJECT env var to point to the server
tsconfig which targets ES2024.

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

* feat: add token usage tracking, context display, and dynamic context length

- Intercept SSE proxy to capture run.completed events and persist token
  usage (input_tokens, output_tokens) per session to SQLite/JSON store
- Display context usage bar in ChatInput showing used/total/remaining tokens
- Resolve actual context length from Hermes models_dev_cache.json based
  on the active profile's default model (fallback 200K), with 5min in-memory cache
- Move sessions-db.ts to db/hermes/ for unified database layer
- Add usage store with SQLite + JSON fallback (auto-migration via ensureTable)
- Fix proxy SSE path regex to match rewritten upstream path
- Fix route ordering: /sessions/usage before /sessions/:id to avoid 404
- Fetch per-session usage on session enter instead of batch
- Add unit tests for usage-store, db index, and proxy SSE interception

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

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ekko
2026-04-22 16:14:50 +08:00
committed by GitHub
parent ce3bf5f3eb
commit 6f69c69802
26 changed files with 1203 additions and 144 deletions
@@ -0,0 +1,106 @@
import { resolve, join } from 'path'
import { homedir } from 'os'
import { readFileSync, existsSync, statSync } from 'fs'
const HERMES_BASE = resolve(homedir(), '.hermes')
const MODELS_DEV_CACHE = resolve(HERMES_BASE, 'models_dev_cache.json')
const DEFAULT_CONTEXT_LENGTH = 200_000
interface ModelLimit {
context?: number
output?: number
input?: number
}
interface ModelEntry {
id?: string
limit?: ModelLimit
}
interface ProviderEntry {
models?: Record<string, ModelEntry>
}
// --- In-memory cache: parsed models_dev_cache (1.7MB), invalidated by mtime ---
let _cache: Record<string, ProviderEntry> | null = null
let _cacheMtime = 0
const CACHE_TTL_MS = 5 * 60 * 1000 // 5 minutes
let _cacheLoadedAt = 0
function loadModelsDevCache(): Record<string, ProviderEntry> | null {
if (!existsSync(MODELS_DEV_CACHE)) return null
try {
const stat = statSync(MODELS_DEV_CACHE)
const now = Date.now()
// Return cached if file hasn't changed and within TTL
if (_cache && stat.mtimeMs === _cacheMtime && now - _cacheLoadedAt < CACHE_TTL_MS) {
return _cache
}
const raw = readFileSync(MODELS_DEV_CACHE, 'utf-8')
_cache = JSON.parse(raw) as Record<string, ProviderEntry>
_cacheMtime = stat.mtimeMs
_cacheLoadedAt = now
return _cache
} catch {
return _cache // return stale cache on error
}
}
// --- Profile helpers ---
function getProfileDir(profile?: string): string {
if (!profile || profile === 'default') return HERMES_BASE
const dir = join(HERMES_BASE, 'profiles', profile)
return existsSync(dir) ? dir : HERMES_BASE
}
function getDefaultModel(profileDir: string): string | null {
const configPath = join(profileDir, 'config.yaml')
if (!existsSync(configPath)) return null
try {
const content = readFileSync(configPath, 'utf-8')
const match = content.match(/^model:\s*\n\s+default:\s*(.+)$/m)
return match ? match[1].trim() : null
} catch {
return null
}
}
// --- Context lookup ---
function lookupContextFromCache(modelName: string): number | null {
const data = loadModelsDevCache()
if (!data) return null
// Exact match first
for (const prov of Object.values(data)) {
const models = prov.models || {}
const entry = models[modelName]
if (entry?.limit?.context) return entry.limit.context
}
// Case-insensitive fallback
const lower = modelName.toLowerCase()
for (const prov of Object.values(data)) {
const models = prov.models || {}
for (const [name, entry] of Object.entries(models)) {
if (name.toLowerCase() === lower && entry?.limit?.context) {
return entry.limit.context
}
}
}
return null
}
/**
* Get the context length for the current profile's default model.
* Results are cached in memory (5min TTL) and invalidated by file mtime.
*/
export function getModelContextLength(profile?: string): number {
const profileDir = getProfileDir(profile)
const model = getDefaultModel(profileDir)
if (!model) return DEFAULT_CONTEXT_LENGTH
return lookupContextFromCache(model) || DEFAULT_CONTEXT_LENGTH
}
@@ -1,352 +0,0 @@
import { getActiveProfileDir } from './hermes-profile'
const SQLITE_AVAILABLE = (() => {
const [major, minor] = process.versions.node.split('.').map(Number)
return major > 22 || (major === 22 && minor >= 5)
})()
export interface HermesSessionRow {
id: string
source: string
user_id: string | null
model: string
title: string | null
started_at: number
ended_at: number | null
end_reason: string | null
message_count: number
tool_call_count: number
input_tokens: number
output_tokens: number
cache_read_tokens: number
cache_write_tokens: number
reasoning_tokens: number
billing_provider: string | null
estimated_cost_usd: number
actual_cost_usd: number | null
cost_status: string
preview: string
last_active: number
}
export interface HermesSessionSearchRow extends HermesSessionRow {
matched_message_id: number | null
snippet: string
rank: number
}
function sessionDbPath(): string {
return `${getActiveProfileDir()}/state.db`
}
function normalizeNumber(value: unknown, fallback = 0): number {
if (value == null || value === '') return fallback
const num = Number(value)
return Number.isFinite(num) ? num : fallback
}
function normalizeNullableNumber(value: unknown): number | null {
if (value == null || value === '') return null
const num = Number(value)
return Number.isFinite(num) ? num : null
}
function normalizeNullableString(value: unknown): string | null {
if (value == null || value === '') return null
return String(value)
}
function mapRow(row: Record<string, unknown>): HermesSessionRow {
const startedAt = normalizeNumber(row.started_at)
const rawTitle = normalizeNullableString(row.title)
const preview = String(row.preview || '')
// Fallback: when no explicit title, use first user message as title (same as CLI path)
const title = rawTitle || (preview ? (preview.length > 40 ? preview.slice(0, 40) + '...' : preview) : null)
return {
id: String(row.id || ''),
source: String(row.source || ''),
user_id: normalizeNullableString(row.user_id),
model: String(row.model || ''),
title,
started_at: startedAt,
ended_at: normalizeNullableNumber(row.ended_at),
end_reason: normalizeNullableString(row.end_reason),
message_count: normalizeNumber(row.message_count),
tool_call_count: normalizeNumber(row.tool_call_count),
input_tokens: normalizeNumber(row.input_tokens),
output_tokens: normalizeNumber(row.output_tokens),
cache_read_tokens: normalizeNumber(row.cache_read_tokens),
cache_write_tokens: normalizeNumber(row.cache_write_tokens),
reasoning_tokens: normalizeNumber(row.reasoning_tokens),
billing_provider: normalizeNullableString(row.billing_provider),
estimated_cost_usd: normalizeNumber(row.estimated_cost_usd),
actual_cost_usd: normalizeNullableNumber(row.actual_cost_usd),
cost_status: String(row.cost_status || ''),
preview: String(row.preview || ''),
last_active: normalizeNumber(row.last_active, startedAt),
}
}
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}`)
}
const { DatabaseSync } = await import('node:sqlite')
const db = new DatabaseSync(sessionDbPath(), { open: true, readOnly: true })
try {
const { sql, params } = buildListSessionSql(source, limit)
const statement = db.prepare(sql)
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()
}
}