Files
Hermes-ui/packages/server/src/services/hermes/group-chat/agent-clients.ts
T
ekko b9f9e62179 feat: Media rendering enhancements and group chat optimizations (#444)
* fix: add missing i18n key and unify session data source (#408)

- Add `chat.sessionNotFound` translation key to all 8 locales
- Fix history page data source inconsistency:
  - Change `getHermesSession` to prioritize database over CLI
  - Now consistent with `listHermesSessions` behavior
  - Prevents "session in list but detail not found" issue
- Update CI workflow to trigger on base branch PRs
- Remove debug log from sessions-db

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: filter special characters and emoji in speech playback (#409)

- Update extractReadableText to filter special characters like *#
- Only keep common punctuation marks for speech synthesis
- Remove emoji, symbols, and special unicode characters
- Improve text-to-speech readability

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: add drawer panel with mobile sidebar support and customizable button (#412)

* feat: add drawer panel with mobile sidebar support

- Add DrawerPanel component with Terminal and Files tabs
- Extract TerminalPanel and FilesPanel from existing views
- Add mobile sidebar toggle functionality with overlay
- Add rainbow breathing light effect to drawer button
- Remove Tools section from AppSidebar (Terminal/Files entries)
- Add i18n support for drawer and file tree
- Optimize mobile button layout and spacing
- Fix z-index hierarchy for proper layering
- Add responsive sidebar behavior (PC: always visible, Mobile: toggle)

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

* feat: customize drawer button with arc rainbow border

- Change drawer button to semi-circle shape贴着右边
- Add arrow icon pointing left (向左箭头)
- Add rainbow border from top to bottom through semi-circle arc
- Slow down animation from 4s to 8s for smoother effect
- Move drawer button wrapper to messages area only (not贯穿header和input)
- Add semi-transparent accent color background to button

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

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix: resolve profile switching state sync issue (#414) (#415)

* fix: resolve profile switching state sync issue (#414)

Fix bug where switching to a different profile would still show the
old profile name in the UI and prevent switching back to default.

Root cause:
- Frontend relied entirely on fetchProfiles() return value to set
  activeProfileName
- Backend Hermes CLI may return stale active flag due to timing
  issues between profile use and profile list commands
- This caused frontend to display wrong profile and prevented
  switching back to default

Solution:
- Immediately set activeProfileName when switchProfile API succeeds
- Don't rely solely on listProfiles() result which may have stale data
- Use activeProfileName instead of activeProfile?.name in ProfileSelector

Changes:
- profiles store: Set activeProfileName immediately after successful switch
- ProfileSelector: Use activeProfileName computed property
- Add test to verify activeProfileName updates on switch

Fixes #414

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

* refine: improve error handling for profile switching failures

Add proper error handling for edge cases:
- If fetchProfiles() fails after successful switch, keep the updated
  activeProfileName (don't let fetchProfiles failure undo the switch)
- Add test cases to verify:
  1. API failure doesn't change state
  2. fetchProfiles failure doesn't affect successful switch

This ensures the UI remains consistent even when profile list refresh
fails after a successful profile switch.

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

* refine: add rollback mechanism for profile switching verification

Add backend verification after profile switch:
- Save old activeProfileName before setting new value
- After fetchProfiles, verify backend reports expected active profile
- If backend reports different profile, rollback frontend state and return false
- This handles edge case where API returns 200 but backend didn't actually switch

Test cases:
-  Normal switch: updates and verifies successfully
-  API failure: doesn't change state
-  fetchProfiles failure: assumes success (API returned 200)
-  Backend verification fails: rolls back to old profile

This ensures frontend state always matches backend reality, even in
edge cases where hermes profile use succeeded but gateway/cleanup
steps failed.

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

* refine: add user feedback for profile operations

Improve user experience with success/error messages:
- ProfileSelector: Add error message when switch fails
- ProfileCard: Add success message before reload on switch
- ProfileSelector: Use async/await for better error handling
- ProfileCard: Add 500ms delay before reload to show success message

Before: Silent failures, no feedback
After: Clear success/error messages for all operations

Example feedback:
- Success: "已切换到配置 qinghe"
- Failure: "切换配置失败,网关可能需要手动重启"

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

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: update frontend changelog for v0.5.7 (#419)

* docs: update frontend changelog for v0.5.7

- Update changelog.ts with v0.5.7 release date and changes
- Add i18n translation keys for all languages (en, zh, de, es, fr, ja, ko, pt)
- Include v0.5.7 changelog entries:
  - Optimize context compression and session sync
  - Add startup delays to prevent database race conditions

Changes:
- packages/client/src/data/changelog.ts: Update v0.5.7 entry
- packages/client/src/i18n/locales/*.ts: Add changelog translation section

This enables the changelog modal in the UI to display v0.5.7 release notes.

* feat: add v0.5.7 changelog translations to all supported languages

Add new_0_5_7_1, new_0_5_7_2, and new_0_5_7_3 changelog entries to all
locale files (en, zh, de, es, fr, ja, ko, pt) with proper translations
for each language.

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

* fix: remove duplicate changelog sections causing syntax errors

Remove duplicate changelog object sections that were causing TypeScript
syntax errors in all locale files (en, zh, de, es, fr, ja, ko, pt).
The actual changelog entries are already correctly placed in the main
changelog section of each file.

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

* feat: add v0.5.8 changelog and fix profile parsing issue

Add v0.5.8 changelog entries based on PRs merged since v0.5.7:
- Drawer panel with mobile sidebar support (#412)
- Profile switching state sync fix (#414)
- Speech playback special character filtering (#409)
- Missing i18n key and session data source unification (#408)
- Vite build optimization for faster Docker builds (#403)

Also fix issue #417: Profile names with long hyphenated names fail
to parse in profile list regex. Change \s{2,} to \s+ to handle
compressed column spacing when profile names are long.

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

* fix: remove enter key submit from profile creation and rename modals

Remove @keyup.enter handlers from NInput components in:
- ProfileCreateModal: prevent accidental profile creation when pressing enter
- ProfileRenameModal: prevent accidental profile rename when pressing enter

Users must now explicitly click the confirm button to submit, preventing
unintended profile operations.

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

* feat: allow free text input for profile names

Remove frontend character filtering from profile creation and rename
modals. Users can now input any characters including spaces and
uppercase letters to test backend Hermes CLI validation.

Changes:
- ProfileCreateModal: Remove toLowerCase() and character filtering
- ProfileRenameModal: Remove toLowerCase() and character filtering
- Use v-model:value binding instead of :value with @input

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

* fix: improve error handling for profile creation

Display backend error messages when profile creation fails instead of
generic "failed" message. This helps users understand why their
profile name was rejected (e.g., invalid characters).

Changes:
- API layer: Capture and return error messages from backend
- ProfileCreateModal: Display specific error message from backend

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

* feat: add profile name validation with i18n support

Add client-side validation for profile names to prevent invalid input
before sending to backend. Only lowercase letters, numbers, underscores,
and hyphens are allowed.

Changes:
- ProfileCreateModal: Add input validation with real-time feedback
- ProfileRenameModal: Add input validation with real-time feedback
- Add nameValidation i18n key for all 8 languages
- Filter invalid characters on input and show warning message

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

* refactor: revert profile parsing regex changes

Revert the regex changes in hermes-cli.ts and gateway-manager.ts
back to requiring \s{2,} (at least 2 spaces). Since frontend now
validates profile names to only allow lowercase letters, numbers,
underscores, and hyphens, the relaxed regex is no longer needed.

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

* refactor: revert profile parsing regex changes

Revert the regex changes in gateway-manager.ts and hermes-cli.ts
back to requiring \s{2,} (at least 2 spaces). Since frontend now
validates profile names to only allow lowercase letters, numbers,
underscores, and hyphens, the relaxed regex is no longer needed.

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

* refactor: remove tooltip from drawer button

Remove the NTooltip wrapper from the floating drawer button.
The "Terminal & Files" tooltip is no longer shown on hover.

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

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* Update assets images (#421)

Updated two asset images in the client package.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore: bump version to 0.5.8

Release v0.5.8

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

* fix: improve profile list parsing to handle long profile names (#425)

Fixed issue #423 where long profile names caused parsing failures.

Changes:
- gateway-manager.ts: Use `.+?` instead of `\S+` to match profile names, allowing names that overflow table column width
- hermes-cli.ts: Use `\s+` instead of `\s{2,}` for first delimiter to handle cases where long profile names reduce spacing to 1 space

The regex now correctly parses profile output even when profile names are long enough to compress table formatting, ensuring all profiles appear in the UI regardless of name length.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: add GitHub issue templates

Add structured issue templates to guide users when submitting issues:
- Bug Report template with version info, reproduction steps, and environment details
- Feature Request template with problem statement, solution, and priority
- General Issue template for questions that don't fit other categories
- Config to enable blank issues and provide contact links to documentation and discussions

Templates use YAML forms for better structure and validation of required fields.

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

* fix: unify profile management across the application (#432)

This commit addresses long-standing profile inconsistency issues by establishing
`~/.hermes/active_profile` file as the single source of truth for all profile
operations throughout the application.

## Changes

### Backend (Server)

**1. profiles.ts - Enhanced profile switching**
- Switch from CLI polling to direct file verification (Hermes CLI writes synchronously)
- Verify `active_profile` file with quick retry (max 2 attempts × 100ms = 300ms)
- Update GatewayManager only after file verification succeeds
- Add comprehensive logging for debugging

**2. profiles.ts - Authoritative API responses**
- Override CLI's active flag with `active_profile` file in `list()` endpoint
- Add warning when CLI output differs from file (detects inconsistencies)
- Ensures API responses always match actual runtime state

**3. jobs.ts - Use authoritative profile source**
- `resolveProfile()` falls back to `getActiveProfileName()` when no profile in request
- Ensures jobs operate on correct profile even if frontend doesn't specify

**4. cron-history.ts - Fix run history to respect active profile**
- Changed from fixed `~/.hermes/cron/output/` to `getActiveProfileDir()/cron/output/`
- Run history now correctly switches with profile (e.g., `~/.hermes/profiles/hermes/cron/output/`)

**5. proxy-handler.ts - Add fallback to authoritative source**
- If no profile in request headers/query, read from `getActiveProfileName()`
- Prevents proxy from using wrong default profile

### Frontend (Client)

**1. api/client.ts - Simplified profile resolution**
- Prioritize `useProfilesStore().activeProfileName` over localStorage
- localStorage fallback only for early initialization

**2. api/hermes/chat.ts - Consistent profile resolution**
- Same pattern: store first, localStorage fallback only during init

**3. stores/session-browser-prefs.ts - Clean up fallback logic**
- Prioritize store, remove redundant localStorage read

## Problem Solved

Previously, multiple components had different ways of determining the active profile:
- CLI output (◆ marker) - could be stale
- GatewayManager memory - startup cache only
- localStorage - frontend cache
- Various fallbacks scattered across codebase

This caused inconsistencies where:
- Frontend showed one profile but API used another
- Jobs ran on wrong profile
- Run history displayed wrong data
- Profile switches appeared to fail (but actually succeeded)

## Solution

All components now derive the active profile from the same authoritative source:
- `~/.hermes/active_profile` file (written synchronously by `hermes profile use`)
- `getActiveProfileName()` function (reads the file)
- Single source of truth = no inconsistencies

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs: add v0.5.9 changelog entries (#434)

- Add unified profile management across the application
- Add GitHub issue and pull request templates

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: Enhance Markdown Media Rendering (Image/Video/File Support) (#438)

* feat: enhance markdown media rendering with image, video, and file support

- Add image display with thumbnail preview (200x160px) and click-to-fullscreen
- Add video playback support for .mp4 and .webm formats with HTML5 player
- Add file card UI for downloads with icon and filename
- Convert local file paths (/tmp/*) to download URLs with auth token
- Add AI output format guidelines system prompt (llm-prompt.ts)
- Increase max download file size from 100MB to 200MB
- Add documentation for AI output format constraints

This enables AI agents to return images, videos, and files using standard
Markdown syntax, which the frontend renders as interactive media elements
instead of plain text links.

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

* fix: resolve unused parameter TypeScript errors in MarkdownRenderer

Use underscore prefix for unused match parameters in replace callbacks

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat: optimize group chat prompts and fix media handling (#439)

Group Chat Prompt Improvements:
- Add AI_OUTPUT_FORMAT_GUIDELINES to group chat system prompts
- Fix duplicate member issue in room member list (deduplicate by name)
- Handle empty agentDescription with default fallback
- Add rule for sending files to users using proper format

Chat Run Socket Integration:
- Integrate getSystemPrompt() into chat-run-socket.ts
- Append media format guidelines to all chat instructions
- Ensure consistent format enforcement across chat and group chat

Media Format Guidelines:
- Simplify "注意事项" section (remove frontend implementation details)
- Add "发送文件给用户" section with clear examples
- Update video format description to mention embedded player

URL Encoding Fix:
- Fix double URL encoding in download.ts (decode first, then encode)
- Prevent %25E8... double-encoded paths, now correctly %E8...

This ensures AI agents in both private chat and group chat follow
consistent media formatting rules when returning images, videos, and files.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-04 19:48:40 +08:00

706 lines
27 KiB
TypeScript

import { io, Socket } from 'socket.io-client'
import { EventSource } from 'eventsource'
import { getToken } from '../../../services/auth'
import type { GatewayManager } from '../gateway-manager'
import { deleteSession as hermesDeleteSession } from '../hermes-cli'
import { getActiveProfileName } from '../hermes-profile'
import { logger } from '../../../services/logger'
import { updateUsage } from '../../../db/hermes/usage-store'
import { getSessionDetailFromDbWithProfile } from '../../../db/hermes/sessions-db'
// ─── Types ────────────────────────────────────────────────────
interface AgentConfig {
profile: string
name: string
description: string
invited: number
}
interface MessageData {
id: string
roomId: string
senderId: string
senderName: string
content: string
timestamp: number
}
interface MemberData {
id: string
name: string
joinedAt: number
}
interface JoinResult {
roomId: string
roomName: string
members: MemberData[]
messages: MessageData[]
rooms: string[]
}
export interface AgentEventHandler {
onMessage?: (data: { roomId: string; msg: MessageData }) => void
onTyping?: (data: { roomId: string; userId: string; userName: string }) => void
onStopTyping?: (data: { roomId: string; userId: string; userName: string }) => void
onMemberJoined?: (data: { roomId: string; memberId: string; memberName: string; members: MemberData[] }) => void
onMemberLeft?: (data: { roomId: string; memberId: string; memberName: string; members: MemberData[] }) => void
}
// ─── Agent Client (single connection) ─────────────────────────
class AgentClient {
readonly agentId: string
readonly profile: string
readonly name: string
readonly description: string
private socket: Socket | null = null
private joinedRooms = new Set<string>()
private handlers: AgentEventHandler
private _reconnecting = false
private gatewayManager: GatewayManager | null = null
private contextEngine: any = null
private storage: any = null
constructor(config: AgentConfig, handlers: AgentEventHandler = {}) {
this.agentId = Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
this.profile = config.profile
this.name = config.name
this.description = config.description
this.handlers = handlers
}
get connected(): boolean {
return this.socket?.connected ?? false
}
get id(): string | undefined {
return this.socket?.id
}
setGatewayManager(manager: GatewayManager): void {
this.gatewayManager = manager
}
setContextEngine(engine: any): void {
this.contextEngine = engine
}
setStorage(storage: any): void {
this.storage = storage
}
async connect(port = 8648): Promise<void> {
const token = await getToken()
this.socket = io(`http://127.0.0.1:${port}/group-chat`, {
auth: {
token: token || undefined,
name: this.name,
},
transports: ['websocket'],
reconnection: true,
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
reconnectionDelayMax: 30000,
})
this.bindEvents()
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Connection timeout')), 10000)
this.socket!.on('connect', () => {
clearTimeout(timeout)
logger.debug(`[AgentClient] ${this.name} connected, socket id: ${this.socket!.id}`)
resolve()
})
this.socket!.on('connect_error', (err) => {
clearTimeout(timeout)
logger.error(err, `[AgentClient] ${this.name} connect_error`)
reject(err)
})
})
}
disconnect(): void {
if (this.socket) {
this.socket.disconnect()
this.socket = null
this.joinedRooms.clear()
}
}
async joinRoom(roomId: string): Promise<JoinResult> {
this.ensureConnected()
return new Promise((resolve, reject) => {
this.socket!.emit('join', { roomId }, (res: JoinResult | { error: string }) => {
if ('error' in res) {
reject(new Error(res.error))
} else {
this.joinedRooms.add(roomId)
resolve(res)
}
})
})
}
sendMessage(roomId: string, content: string): Promise<string> {
this.ensureConnected()
return new Promise((resolve, reject) => {
this.socket!.emit('message', { roomId, content }, (res: { id?: string; error?: string }) => {
if (res.error) {
reject(new Error(res.error))
} else {
resolve(res.id!)
}
})
})
}
startTyping(roomId: string): void {
this.ensureConnected()
this.socket!.emit('typing', { roomId })
}
stopTyping(roomId: string): void {
this.ensureConnected()
this.socket!.emit('stop_typing', { roomId })
}
emitContextStatus(roomId: string, status: 'compressing' | 'replying' | 'ready'): void {
this.ensureConnected()
this.socket!.emit('context_status', { roomId, agentName: this.name, status })
}
getJoinedRooms(): string[] {
return Array.from(this.joinedRooms)
}
private ensureConnected(): void {
if (!this.socket?.connected) {
throw new Error(`Agent "${this.name}" is not connected`)
}
}
private async deleteSession(sessionId: string): Promise<void> {
try {
const sessionProfile = this.storage?.getSessionProfile?.(sessionId)
const currentProfile = getActiveProfileName()
if (sessionProfile && sessionProfile.profile_name !== currentProfile) {
// Cross-profile: enqueue deferred delete, don't switch profile
this.storage?.enqueuePendingSessionDelete?.(sessionId, sessionProfile.profile_name)
logger.info(`[AgentClients] ${this.name}: cross-profile deferred delete session ${sessionId} (session=${sessionProfile.profile_name}, active=${currentProfile})`)
return
}
// Same profile or no mapping: delete directly
const ok = await hermesDeleteSession(sessionId)
if (ok) {
this.storage?.deleteSessionProfile?.(sessionId)
}
logger.debug(`[AgentClients] ${this.name}: delete session ${sessionId} (profile=${this.profile}) → ${ok ? 'ok' : 'failed'}`)
} catch (err: any) {
logger.warn(`[AgentClients] ${this.name}: failed to delete session ${sessionId}: ${err.message}`)
}
}
// ─── Hermes Gateway Integration ────────────────────────────
/**
* Handle an @mention from the server side.
* Called by AgentClients.processMentions() — no socket round-trip needed.
* onStatus is called to report context compression progress.
*/
async replyToMention(
roomId: string,
msg: { content: string; senderName: string; senderId: string; timestamp: number },
onStatus?: (status: 'compressing' | 'replying' | 'ready') => void,
): Promise<void> {
logger.debug(`[AgentClients] ${this.name} mentioned by ${msg.senderName}: "${msg.content.slice(0, 50)}"`)
if (!this.gatewayManager) {
logger.debug(`[AgentClients] ${this.name}: gatewayManager is null, skipping`)
return
}
const upstream = this.gatewayManager.getUpstream(this.profile)
const apiKey = this.gatewayManager.getApiKey(this.profile)
logger.debug(`[AgentClients] ${this.name}: upstream=${upstream}, profile=${this.profile}`)
if (!upstream) {
logger.error(`[AgentClients] ${this.name}: no gateway upstream for profile "${this.profile}"`)
return
}
const sessionId = Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
try {
// Notify room that agent is typing
this.startTyping(roomId)
// Build compressed context if context engine is available
let conversationHistory: Array<{ role: string; content: string }> = []
let instructions: string | undefined
if (this.contextEngine && this.storage) {
try {
logger.debug(`[AgentClients] ${this.name}: building context...`)
onStatus?.('compressing')
// Get room members with descriptions for context
const roomMembers: Array<{ userId: string; name: string; description: string }> = this.storage.getRoomMembers(roomId) || []
const memberNames = roomMembers.map((m: any) => m.name)
const members = roomMembers.map((m: any) => ({ userId: m.userId, name: m.name, description: m.description }))
// Get room compression config
const roomInfo = this.storage.getRoom(roomId)
const compression = roomInfo ? {
triggerTokens: roomInfo.triggerTokens,
maxHistoryTokens: roomInfo.maxHistoryTokens,
tailMessageCount: roomInfo.tailMessageCount,
} : undefined
const ctx = await this.contextEngine.buildContext({
roomId,
agentId: this.agentId,
agentName: this.name,
agentDescription: this.description,
agentSocketId: this.socket?.id || '',
roomName: roomId,
memberNames,
members,
upstream,
apiKey,
currentMessage: msg,
compression,
profile: this.profile,
})
conversationHistory = ctx.conversationHistory
instructions = ctx.instructions
logger.debug(`[AgentClients] ${this.name}: context built — historyLen=${conversationHistory.length}, meta=%j`, ctx.meta)
onStatus?.('replying')
} catch (err: any) {
logger.warn(`[AgentClients] ${this.name}: context engine failed: ${err.message}`)
onStatus?.('replying')
// Degrade: continue without context
}
}
// Strip @mention from input — agent already knows it was mentioned
const input = msg.content.replace(new RegExp(`@${this.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*`, 'gi'), '').trim() || msg.content
// Start a run on Hermes gateway
const runRes = await fetch(`${upstream}/v1/runs`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
body: JSON.stringify({
input,
session_id: sessionId,
...(conversationHistory.length > 0 ? { conversation_history: conversationHistory } : {}),
...(instructions ? { instructions } : {}),
}),
signal: AbortSignal.timeout(120000),
})
if (!runRes.ok) {
const text = await runRes.text().catch(() => '')
logger.error(`[AgentClients] ${this.name}: gateway run failed (${runRes.status}): ${text}`)
this.stopTyping(roomId)
return
}
const runData = await runRes.json() as any
const run_id = runData.run_id
logger.debug(`[AgentClients] ${this.name}: run started, response=%j`, runData)
if (!run_id) {
logger.error(`[AgentClients] ${this.name}: no run_id in response`)
this.stopTyping(roomId)
return
}
// Save session-to-profile mapping after gateway confirms the run
const actualSessionId = runData.session_id || sessionId
if (!this.storage) {
logger.warn(`[AgentClients] ${this.name}: storage is null, cannot save session profile for ${actualSessionId}`)
} else {
this.storage.saveSessionProfile(actualSessionId, roomId, this.agentId, this.profile)
logger.debug(`[AgentClients] ${this.name}: saved session profile ${actualSessionId} → profile=${this.profile}`)
}
// Stream events from Hermes
const eventsUrl = new URL(`${upstream}/v1/runs/${run_id}/events`)
logger.debug(`[AgentClients] ${this.name}: streaming events from ${eventsUrl}`)
// 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)
let fullContent = ''
source.onmessage = async (e: any) => {
try {
const parsed = JSON.parse(e.data)
logger.debug(`[AgentClients] ${this.name}: event=${parsed.event}`)
if (parsed.event === 'run.completed') {
// Record usage data from Hermes state.db BEFORE closing source
// This ensures we fetch usage before deleteSession can delete it
try {
const detail = await getSessionDetailFromDbWithProfile(actualSessionId, this.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: this.profile,
})
logger.debug(`[AgentClients] Recorded usage for room ${roomId} (session ${actualSessionId}, profile=${this.profile}): input=${detail.input_tokens}, output=${detail.output_tokens}`)
} else {
logger.warn(`[AgentClients] Failed to get session detail for ${actualSessionId} (profile=${this.profile})`)
}
} catch (err: any) {
logger.warn(err, '[AgentClients] Failed to record usage from DB')
}
source.close()
logger.debug(`[AgentClients] ${this.name}: run completed, content length=${fullContent.length}`)
if (fullContent) {
this.stopTyping(roomId)
this.sendMessage(roomId, fullContent)
}
this.deleteSession(actualSessionId).catch(() => { })
onStatus?.('ready')
return
}
if (parsed.event === 'run.failed') {
source.close()
logger.error(`[AgentClients] ${this.name}: run failed`)
this.stopTyping(roomId)
this.deleteSession(actualSessionId).catch(() => { })
onStatus?.('ready')
return
}
// Accumulate message deltas
if (parsed.event === 'message.delta' && parsed.delta) {
fullContent += parsed.delta
}
} catch {
// ignore parse errors
}
}
source.onerror = (err: any) => {
logger.error(err, `[AgentClients] ${this.name}: EventSource error`)
source.close()
this.stopTyping(roomId)
this.deleteSession(actualSessionId).catch(() => { })
onStatus?.('ready')
}
} catch (err: any) {
logger.error(`[AgentClients] ${this.name}: error handling message: ${err.message}`)
this.stopTyping(roomId)
this.deleteSession(sessionId).catch(() => { })
onStatus?.('ready')
}
}
private bindEvents(): void {
const s = this.socket!
s.on('typing', (data: any) => {
this.handlers.onTyping?.(data)
})
s.on('stop_typing', (data: any) => {
this.handlers.onStopTyping?.(data)
})
s.on('member_joined', (data: any) => {
this.handlers.onMemberJoined?.(data)
})
s.on('member_left', (data: any) => {
this.handlers.onMemberLeft?.(data)
})
// Auto rejoin rooms on reconnect
s.io.on('reconnect', async () => {
if (this._reconnecting) return
this._reconnecting = true
logger.info(`[AgentClients] ${this.name} reconnecting, rejoining ${this.joinedRooms.size} rooms...`)
const rooms = Array.from(this.joinedRooms)
for (const roomId of rooms) {
try {
await this.joinRoom(roomId)
} catch (err: any) {
logger.error(`[AgentClients] ${this.name} failed to rejoin room ${roomId}: ${err.message}`)
}
}
this._reconnecting = false
})
}
}
// ─── AgentClients (roomId -> agents) ──────────────────────────
export class AgentClients {
private rooms = new Map<string, Map<string, AgentClient>>()
private _gatewayManager: GatewayManager | null = null
private _contextEngine: any = null
private _storage: any = null
// Per-room processing lock + mention queue
private _processingRooms = new Set<string>()
private _mentionQueue = new Map<string, Array<{ agent: AgentClient; msg: { content: string; senderName: string; senderId: string; timestamp: number } }>>()
/**
* Create an agent client and connect it to the server.
* The agent will NOT auto-join any room — call addAgentToRoom separately.
*/
async createAgent(config: AgentConfig, handlers?: AgentEventHandler, port?: number): Promise<AgentClient> {
const client = new AgentClient(config, handlers)
await client.connect(port)
// Auto-apply stored references (fixes propagation for agents created after set*)
if (this._gatewayManager) client.setGatewayManager(this._gatewayManager)
if (this._contextEngine) client.setContextEngine(this._contextEngine)
if (this._storage) client.setStorage(this._storage)
logger.info(`[AgentClients] Connected: ${client.name} (${client.agentId})`)
return client
}
/**
* Connect an agent to a room.
*/
async addAgentToRoom(roomId: string, client: AgentClient): Promise<JoinResult> {
let room = this.rooms.get(roomId)
if (!room) {
room = new Map()
this.rooms.set(roomId, room)
}
room.set(client.agentId, client)
const result = await client.joinRoom(roomId)
logger.info(`[AgentClients] ${client.name} joined room: ${roomId}`)
return result
}
/**
* Remove an agent from a room and disconnect it.
*/
removeAgentFromRoom(roomId: string, agentId: string): void {
const room = this.rooms.get(roomId)
if (!room) return
const client = room.get(agentId)
if (client) {
client.disconnect()
room.delete(agentId)
logger.info(`[AgentClients] ${client.name} left room: ${roomId}`)
// Invalidate context engine cache for this agent
if (this._contextEngine) {
try { this._contextEngine.invalidateRoom(roomId) } catch { /* ignore */ }
}
}
if (room.size === 0) {
this.rooms.delete(roomId)
}
}
/**
* Get all agents in a room.
*/
getAgents(roomId: string): AgentClient[] {
const room = this.rooms.get(roomId)
return room ? Array.from(room.values()) : []
}
/**
* Get a specific agent in a room.
*/
getAgent(roomId: string, agentId: string): AgentClient | undefined {
return this.rooms.get(roomId)?.get(agentId)
}
/**
* Get all room IDs that have agents.
*/
getRoomIds(): string[] {
return Array.from(this.rooms.keys())
}
/**
* Send a message from a specific agent in a room.
*/
async sendMessage(roomId: string, agentId: string, content: string): Promise<string> {
const client = this.getAgent(roomId, agentId)
if (!client) {
throw new Error(`Agent "${agentId}" not found in room "${roomId}"`)
}
return client.sendMessage(roomId, content)
}
/**
* Broadcast a message from all agents in a room.
*/
async broadcastFromRoom(roomId: string, content: string): Promise<string[]> {
const agents = this.getAgents(roomId)
return Promise.all(agents.map((agent) => agent.sendMessage(roomId, content)))
}
/**
* Disconnect all agents in a room.
*/
disconnectRoom(roomId: string): void {
const room = this.rooms.get(roomId)
if (!room) return
room.forEach((client) => client.disconnect())
this.rooms.delete(roomId)
logger.info(`[AgentClients] All agents disconnected from room: ${roomId}`)
// Invalidate context engine cache for this room
if (this._contextEngine) {
try { this._contextEngine.invalidateRoom(roomId) } catch { /* ignore */ }
}
}
/**
* Disconnect all agents in all rooms.
*/
disconnectAll(): void {
this.rooms.forEach((room) => {
room.forEach((client) => client.disconnect())
})
this.rooms.clear()
logger.info('[AgentClients] All agents disconnected')
}
/**
* Set gateway manager for all existing and future agents.
*/
setGatewayManager(manager: GatewayManager): void {
this._gatewayManager = manager
this.rooms.forEach((room) => {
room.forEach((client) => client.setGatewayManager(manager))
})
}
/**
* Set context engine for all existing and future agents.
*/
setContextEngine(engine: any): void {
this._contextEngine = engine
this.rooms.forEach((room) => {
room.forEach((client) => client.setContextEngine(engine))
})
}
/**
* Set message storage for all existing and future agents.
*/
setStorage(storage: any): void {
this._storage = storage
this.rooms.forEach((room) => {
room.forEach((client) => client.setStorage(storage))
})
}
/**
* Server-side: parse @mentions and forward to matching agents directly.
* If the room is already processing (compressing/replying), queue the mention.
*/
async processMentions(roomId: string, msg: { content: string; senderName: string; senderId: string; timestamp: number }): Promise<void> {
if (!this._gatewayManager) return
const content = msg.content.toLowerCase()
const agents = this.getAgents(roomId)
const mentioned = agents.filter(a => content.includes(`@${a.name.toLowerCase()}`))
if (mentioned.length === 0) return
logger.debug(`[AgentClients] ${mentioned.map(a => a.name).join(', ')} mentioned by ${msg.senderName}`)
for (const agent of mentioned) {
this._processAgentMention(roomId, agent, msg).catch((err) => {
logger.error(`[AgentClients] error processing mention for ${agent.name}: ${err.message}`)
})
}
}
/**
* Process a single agent mention with status reporting and queue drain.
*/
private async _processAgentMention(
roomId: string,
agent: AgentClient,
msg: { content: string; senderName: string; senderId: string; timestamp: number },
): Promise<void> {
const agentKey = `${roomId}:${agent.name}`
if (this._processingRooms.has(agentKey)) {
// Queue for this specific agent
let queue = this._mentionQueue.get(agentKey)
if (!queue) {
queue = []
this._mentionQueue.set(agentKey, queue)
}
queue.push({ agent, msg })
logger.debug(`[AgentClients] agent ${agent.name} is processing, queued mention in room ${roomId}`)
return
}
this._processingRooms.add(agentKey)
const onStatus = (status: 'compressing' | 'replying' | 'ready') => {
agent.emitContextStatus(roomId, status)
logger.debug(`[AgentClients] room ${roomId} agent ${agent.name} status: ${status}`)
}
try {
await agent.replyToMention(roomId, msg, onStatus)
} finally {
this._processingRooms.delete(agentKey)
await this._drainQueue(agentKey, roomId)
}
}
/**
* Drain queued mentions for a room after processing completes.
*/
private async _drainQueue(agentKey: string, roomId: string): Promise<void> {
const queue = this._mentionQueue.get(agentKey)
if (!queue || queue.length === 0) return
this._mentionQueue.delete(agentKey)
logger.debug(`[AgentClients] draining ${queue.length} queued mention(s) for ${agentKey}`)
// Process the last queued mention only (most recent, discards stale intermediate ones)
const last = queue[queue.length - 1]
this._processingRooms.add(agentKey)
this._processAgentMention(roomId, last.agent, last.msg).catch((err) => {
logger.error(`[AgentClients] error processing queued mention: ${err.message}`)
})
}
}