Fix bridge history, profile models, and Windows gateway handling (#845)
* feat: support profile-aware group chat bridge flows * feat: route cron jobs through hermes cli * Fix group chat routing and isolate bridge tests * Add Grok image-to-video media skill * Default Grok videos to media directory * Fix bridge profile fallback and cron repeat clearing * Refine bridge chat and gateway platform handling * Filter bridge tool-call text deltas * Preserve structured bridge chat history * Prepare beta release build artifacts * Fix Windows run profile resolution * Fix Windows path compatibility checks * Fix profile-scoped model page display * Hide Windows subprocess windows for jobs and updates * Hide Windows file backend subprocess windows * Avoid Windows gateway restart lock conflicts * Treat Windows gateway lock as running on startup * Force release Windows gateway lock on restart * Tighten Windows gateway lock cleanup * Update chat e2e source expectation * Bump package version to 0.5.30 --------- Co-authored-by: Codex <codex@openai.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { io, Socket } from 'socket.io-client'
|
||||
import { getToken } from '../../../services/auth'
|
||||
import type { GatewayManager } from '../gateway-manager'
|
||||
import { logger } from '../../../services/logger'
|
||||
import { updateUsage } from '../../../db/hermes/usage-store'
|
||||
import { AgentBridgeClient, type AgentBridgeMessage, type AgentBridgeOutput } from '../agent-bridge'
|
||||
import { convertContentBlocksForAgent, isContentBlockArray } from '../run-chat/content-blocks'
|
||||
import type { ContentBlock } from '../run-chat/types'
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────
|
||||
|
||||
@@ -22,6 +24,15 @@ interface MessageData {
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
type MentionMessage = {
|
||||
content: string
|
||||
senderName: string
|
||||
senderId: string
|
||||
timestamp: number
|
||||
input?: string | ContentBlock[]
|
||||
mentionDepth?: number
|
||||
}
|
||||
|
||||
interface MemberData {
|
||||
id: string
|
||||
name: string
|
||||
@@ -55,9 +66,10 @@ class AgentClient {
|
||||
private joinedRooms = new Set<string>()
|
||||
private handlers: AgentEventHandler
|
||||
private _reconnecting = false
|
||||
private gatewayManager: GatewayManager | null = null
|
||||
private contextEngine: any = null
|
||||
private storage: any = null
|
||||
private pendingToolCallIds = new Map<string, string[]>()
|
||||
private pendingToolBaseIds = new Map<string, string>()
|
||||
|
||||
constructor(config: AgentConfig, handlers: AgentEventHandler = {}) {
|
||||
this.agentId = Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
|
||||
@@ -75,10 +87,6 @@ class AgentClient {
|
||||
return this.socket?.id
|
||||
}
|
||||
|
||||
setGatewayManager(manager: GatewayManager): void {
|
||||
this.gatewayManager = manager
|
||||
}
|
||||
|
||||
setContextEngine(engine: any): void {
|
||||
this.contextEngine = engine
|
||||
}
|
||||
@@ -146,10 +154,10 @@ class AgentClient {
|
||||
})
|
||||
}
|
||||
|
||||
sendMessage(roomId: string, content: string): Promise<string> {
|
||||
sendMessage(roomId: string, content: string, messageId?: string, extra?: Record<string, unknown>): Promise<string> {
|
||||
this.ensureConnected()
|
||||
return new Promise((resolve, reject) => {
|
||||
this.socket!.emit('message', { roomId, content }, (res: { id?: string; error?: string }) => {
|
||||
this.socket!.emit('message', { roomId, content, id: messageId, ...extra }, (res: { id?: string; error?: string }) => {
|
||||
if (res.error) {
|
||||
reject(new Error(res.error))
|
||||
} else {
|
||||
@@ -174,6 +182,52 @@ class AgentClient {
|
||||
this.socket!.emit('context_status', { roomId, agentName: this.name, status })
|
||||
}
|
||||
|
||||
emitApprovalRequested(roomId: string, payload: Record<string, unknown>): void {
|
||||
this.ensureConnected()
|
||||
this.socket!.emit('approval.requested', { roomId, agentName: this.name, ...payload })
|
||||
}
|
||||
|
||||
emitApprovalResolved(roomId: string, payload: Record<string, unknown>): void {
|
||||
this.ensureConnected()
|
||||
this.socket!.emit('approval.resolved', { roomId, agentName: this.name, ...payload })
|
||||
}
|
||||
|
||||
async interrupt(roomId: string): Promise<void> {
|
||||
const sessionSeed = String(this.storage?.getRoom?.(roomId)?.sessionSeed || '0')
|
||||
const sessionId = groupBridgeSessionId(roomId, this.profile, this.name, sessionSeed)
|
||||
await new AgentBridgeClient().interrupt(sessionId, 'Interrupted by group chat user', this.profile)
|
||||
this.stopTyping(roomId)
|
||||
this.emitContextStatus(roomId, 'ready')
|
||||
}
|
||||
|
||||
emitMessageStreamStart(roomId: string, messageId: string): void {
|
||||
this.ensureConnected()
|
||||
this.socket!.emit('message_stream_start', {
|
||||
roomId,
|
||||
id: messageId,
|
||||
senderId: this.socket?.id || this.agentId,
|
||||
senderName: this.name,
|
||||
timestamp: Date.now(),
|
||||
})
|
||||
}
|
||||
|
||||
emitMessageStreamDelta(roomId: string, messageId: string, delta: string): void {
|
||||
if (!delta) return
|
||||
this.ensureConnected()
|
||||
this.socket!.emit('message_stream_delta', { roomId, id: messageId, delta })
|
||||
}
|
||||
|
||||
emitMessageReasoningDelta(roomId: string, messageId: string, delta: string): void {
|
||||
if (!delta) return
|
||||
this.ensureConnected()
|
||||
this.socket!.emit('message_reasoning_delta', { roomId, id: messageId, delta })
|
||||
}
|
||||
|
||||
emitMessageStreamEnd(roomId: string, messageId: string): void {
|
||||
this.ensureConnected()
|
||||
this.socket!.emit('message_stream_end', { roomId, id: messageId })
|
||||
}
|
||||
|
||||
getJoinedRooms(): string[] {
|
||||
return Array.from(this.joinedRooms)
|
||||
}
|
||||
@@ -193,23 +247,10 @@ class AgentClient {
|
||||
*/
|
||||
async replyToMention(
|
||||
roomId: string,
|
||||
msg: { content: string; senderName: string; senderId: string; timestamp: number },
|
||||
msg: MentionMessage,
|
||||
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
|
||||
}
|
||||
|
||||
try {
|
||||
// Notify room that agent is typing
|
||||
this.startTyping(roomId)
|
||||
@@ -244,8 +285,8 @@ class AgentClient {
|
||||
roomName: roomId,
|
||||
memberNames,
|
||||
members,
|
||||
upstream,
|
||||
apiKey,
|
||||
upstream: '',
|
||||
apiKey: null,
|
||||
currentMessage: msg,
|
||||
compression,
|
||||
profile: this.profile,
|
||||
@@ -261,86 +302,101 @@ class AgentClient {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
const responseRes = await fetch(`${upstream.replace(/\/$/, '')}/v1/responses`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
|
||||
// Keep the original mentions visible and add an explicit routing note.
|
||||
// When a user mentions multiple agents, stripping only this agent's
|
||||
// name can make the remaining input look like it was meant for
|
||||
// someone else.
|
||||
const routedPrefix = `群聊系统:这条消息已经提及你(${this.name}),请直接回复;即使消息同时提及其他成员,也不要因此输出空回复。`
|
||||
const ownMentionPattern = new RegExp(`@${this.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*`, 'gi')
|
||||
const rawInput = msg.input || msg.content
|
||||
const input = isContentBlockArray(rawInput)
|
||||
? rawInput.map((block) => {
|
||||
if (block.type !== 'text') return block
|
||||
const text = String(block.text || msg.content).replace(ownMentionPattern, '').trim()
|
||||
return { ...block, text: `${routedPrefix}\n\n原始消息:${text || msg.content}` }
|
||||
})
|
||||
: `${routedPrefix}\n\n原始消息:${msg.content.replace(ownMentionPattern, '').trim() || msg.content}`
|
||||
const bridgeInput: AgentBridgeMessage = isContentBlockArray(input)
|
||||
? await convertContentBlocksForAgent(input)
|
||||
: input
|
||||
const bridge = new AgentBridgeClient()
|
||||
const sessionSeed = String(this.storage?.getRoom?.(roomId)?.sessionSeed || '0')
|
||||
const sessionId = groupBridgeSessionId(roomId, this.profile, this.name, sessionSeed)
|
||||
const runMessageId = groupMessageId(roomId, this.profile, this.name)
|
||||
let partIndex = 0
|
||||
let streamMessageId = groupMessagePartId(runMessageId, partIndex)
|
||||
let currentContent = ''
|
||||
let totalContent = ''
|
||||
let reasoningContent = ''
|
||||
const flushedAssistantParts = new Set<string>()
|
||||
let lastChunk: AgentBridgeOutput | null = null
|
||||
const started = await bridge.chat(
|
||||
sessionId,
|
||||
bridgeInput,
|
||||
conversationHistory,
|
||||
instructions,
|
||||
this.profile,
|
||||
{
|
||||
source: 'api_server',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
input,
|
||||
...(conversationHistory.length > 0 ? { conversation_history: conversationHistory } : {}),
|
||||
...(instructions ? { instructions } : {}),
|
||||
stream: true,
|
||||
store: false,
|
||||
}),
|
||||
signal: AbortSignal.timeout(120000),
|
||||
})
|
||||
)
|
||||
|
||||
if (!responseRes.ok) {
|
||||
const text = await responseRes.text().catch(() => '')
|
||||
logger.error(`[AgentClients] ${this.name}: gateway response failed (${responseRes.status}): ${text}`)
|
||||
this.stopTyping(roomId)
|
||||
onStatus?.('ready')
|
||||
return
|
||||
}
|
||||
|
||||
if (!responseRes.body) {
|
||||
logger.error(`[AgentClients] ${this.name}: gateway response stream missing`)
|
||||
this.stopTyping(roomId)
|
||||
onStatus?.('ready')
|
||||
return
|
||||
}
|
||||
|
||||
let fullContent = ''
|
||||
for await (const frame of readSseFrames(responseRes.body)) {
|
||||
let parsed: any
|
||||
try {
|
||||
parsed = JSON.parse(frame.data)
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
const eventType = parsed.type || frame.event || parsed.event
|
||||
logger.debug(`[AgentClients] ${this.name}: event=${eventType}`)
|
||||
|
||||
if (eventType === 'response.output_text.delta' && parsed.delta) {
|
||||
fullContent += parsed.delta
|
||||
continue
|
||||
}
|
||||
|
||||
if (eventType === 'response.completed') {
|
||||
const response = parsed.response || parsed
|
||||
const finalText = extractResponseText(response)
|
||||
if (!fullContent && finalText) fullContent = 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: this.profile,
|
||||
})
|
||||
logger.debug(`[AgentClients] ${this.name}: response completed, content length=${fullContent.length}`)
|
||||
if (fullContent) {
|
||||
this.stopTyping(roomId)
|
||||
this.sendMessage(roomId, fullContent)
|
||||
this.emitMessageStreamStart(roomId, streamMessageId)
|
||||
for await (const chunk of bridge.streamOutput(started.run_id, { timeoutMs: 120000 })) {
|
||||
lastChunk = chunk
|
||||
reasoningContent += await this.recordBridgeEvents(roomId, chunk, () => streamMessageId, async () => {
|
||||
const toolBaseId = streamMessageId
|
||||
if (currentContent.trim()) {
|
||||
await this.sendMessage(roomId, currentContent, streamMessageId, {
|
||||
role: 'assistant',
|
||||
mentionDepth: nextMentionDepth(msg),
|
||||
reasoning: reasoningContent || null,
|
||||
reasoning_content: reasoningContent || null,
|
||||
})
|
||||
flushedAssistantParts.add(streamMessageId)
|
||||
currentContent = ''
|
||||
}
|
||||
onStatus?.('ready')
|
||||
return
|
||||
}
|
||||
|
||||
if (eventType === 'response.failed') {
|
||||
logger.error(`[AgentClients] ${this.name}: response failed`)
|
||||
this.stopTyping(roomId)
|
||||
onStatus?.('ready')
|
||||
return
|
||||
this.emitMessageStreamEnd(roomId, toolBaseId)
|
||||
partIndex += 1
|
||||
streamMessageId = groupMessagePartId(runMessageId, partIndex)
|
||||
this.emitMessageStreamStart(roomId, streamMessageId)
|
||||
return toolBaseId
|
||||
})
|
||||
if (chunk.delta) {
|
||||
currentContent += chunk.delta
|
||||
totalContent += chunk.delta
|
||||
this.emitMessageStreamDelta(roomId, streamMessageId, chunk.delta)
|
||||
}
|
||||
}
|
||||
logger.warn(`[AgentClients] ${this.name}: response stream ended without terminal event`)
|
||||
|
||||
if (lastChunk?.status === 'error') {
|
||||
logger.error(`[AgentClients] ${this.name}: bridge response failed: ${lastChunk.error || 'unknown error'}`)
|
||||
this.emitMessageStreamEnd(roomId, streamMessageId)
|
||||
this.stopTyping(roomId)
|
||||
onStatus?.('ready')
|
||||
return
|
||||
}
|
||||
|
||||
if (!totalContent) {
|
||||
currentContent = extractBridgeFinalText(lastChunk)
|
||||
totalContent = currentContent
|
||||
}
|
||||
recordBridgeUsage(roomId, this.profile, lastChunk?.result)
|
||||
logger.debug(`[AgentClients] ${this.name}: bridge response completed, content length=${totalContent.length}`)
|
||||
if (currentContent) {
|
||||
this.stopTyping(roomId)
|
||||
await this.sendMessage(roomId, currentContent, streamMessageId, {
|
||||
role: 'assistant',
|
||||
mentionDepth: nextMentionDepth(msg),
|
||||
reasoning: reasoningContent || null,
|
||||
reasoning_content: reasoningContent || null,
|
||||
})
|
||||
this.emitMessageStreamEnd(roomId, streamMessageId)
|
||||
onStatus?.('ready')
|
||||
return
|
||||
}
|
||||
logger.warn(`[AgentClients] ${this.name}: bridge response completed without content`)
|
||||
this.emitMessageStreamEnd(roomId, streamMessageId)
|
||||
this.stopTyping(roomId)
|
||||
onStatus?.('ready')
|
||||
} catch (err: any) {
|
||||
@@ -350,6 +406,132 @@ class AgentClient {
|
||||
}
|
||||
}
|
||||
|
||||
private async recordBridgeEvents(
|
||||
roomId: string,
|
||||
chunk: AgentBridgeOutput,
|
||||
getCurrentMessageId: () => string,
|
||||
beforeToolStarted: () => Promise<string>,
|
||||
): Promise<string> {
|
||||
let reasoning = ''
|
||||
for (const ev of chunk.events || []) {
|
||||
const eventType = String((ev as any)?.event || '')
|
||||
if (eventType === 'tool.started') {
|
||||
const toolBaseId = await beforeToolStarted()
|
||||
this.recordToolStarted(roomId, ev as Record<string, unknown>, toolBaseId)
|
||||
} else if (eventType === 'tool.completed') {
|
||||
this.recordToolCompleted(roomId, ev as Record<string, unknown>)
|
||||
} else if (eventType === 'approval.requested') {
|
||||
this.emitApprovalRequested(roomId, {
|
||||
event: 'approval.requested',
|
||||
approval_id: (ev as any).approval_id,
|
||||
command: (ev as any).command,
|
||||
description: (ev as any).description,
|
||||
choices: Array.isArray((ev as any).choices) ? (ev as any).choices : undefined,
|
||||
allow_permanent: (ev as any).allow_permanent,
|
||||
})
|
||||
} else if (eventType === 'approval.resolved') {
|
||||
this.emitApprovalResolved(roomId, {
|
||||
event: 'approval.resolved',
|
||||
approval_id: (ev as any).approval_id,
|
||||
choice: (ev as any).choice,
|
||||
})
|
||||
} else if (eventType === 'reasoning.delta' || eventType === 'thinking.delta') {
|
||||
const text = String((ev as any)?.text || '')
|
||||
reasoning += text
|
||||
this.emitMessageReasoningDelta(roomId, getCurrentMessageId(), text)
|
||||
}
|
||||
}
|
||||
return reasoning
|
||||
}
|
||||
|
||||
private recordToolStarted(roomId: string, ev: Record<string, unknown>, runMessageId: string): void {
|
||||
const toolName = String(ev.tool_name || ev.tool || ev.name || '')
|
||||
const toolCallId = groupToolCallId(ev.tool_call_id, toolName, this.nextToolIndex(roomId, toolName))
|
||||
this.trackPendingToolCall(roomId, toolName, toolCallId)
|
||||
this.pendingToolBaseIds.set(toolCallId, runMessageId)
|
||||
const timestamp = Date.now()
|
||||
const rawArgs = ev.args ?? ev.arguments ?? ev.input ?? {}
|
||||
const args = normalizeToolArgs(rawArgs)
|
||||
const toolCall = {
|
||||
id: toolCallId,
|
||||
type: 'function',
|
||||
function: {
|
||||
name: toolName,
|
||||
arguments: JSON.stringify(args),
|
||||
},
|
||||
}
|
||||
const msg: MessageData & Record<string, any> = {
|
||||
id: `${runMessageId}_toolcall_${safeId(toolCallId)}`,
|
||||
roomId,
|
||||
senderId: this.socket?.id || this.agentId,
|
||||
senderName: this.name,
|
||||
content: '',
|
||||
timestamp,
|
||||
role: 'assistant',
|
||||
tool_calls: [toolCall],
|
||||
finish_reason: 'tool_calls',
|
||||
}
|
||||
this.sendMessage(roomId, '', msg.id, {
|
||||
role: 'assistant',
|
||||
tool_calls: msg.tool_calls,
|
||||
finish_reason: 'tool_calls',
|
||||
timestamp,
|
||||
}).catch((err: any) => logger.warn(`[AgentClients] failed to record tool call: ${err.message}`))
|
||||
}
|
||||
|
||||
private recordToolCompleted(roomId: string, ev: Record<string, unknown>): void {
|
||||
const toolName = String(ev.tool_name || ev.tool || ev.name || '')
|
||||
const rawId = String(ev.tool_call_id || '').trim()
|
||||
const toolCallId = rawId || this.takePendingToolCall(roomId, toolName) || groupToolCallId(null, toolName, this.nextToolIndex(roomId, toolName))
|
||||
const runMessageId = this.pendingToolBaseIds.get(toolCallId) || groupMessagePartId(groupMessageId(roomId, this.profile, this.name), 0)
|
||||
this.pendingToolBaseIds.delete(toolCallId)
|
||||
const output = bridgeToolOutput(ev)
|
||||
const timestamp = Date.now()
|
||||
const msg: MessageData & Record<string, any> = {
|
||||
id: `${runMessageId}_toolresult_${safeId(toolCallId)}_${Date.now()}`,
|
||||
roomId,
|
||||
senderId: this.socket?.id || this.agentId,
|
||||
senderName: this.name,
|
||||
content: output,
|
||||
timestamp,
|
||||
role: 'tool',
|
||||
tool_call_id: toolCallId,
|
||||
tool_name: toolName || null,
|
||||
}
|
||||
this.sendMessage(roomId, output, msg.id, {
|
||||
role: 'tool',
|
||||
tool_call_id: toolCallId,
|
||||
tool_name: toolName || null,
|
||||
timestamp,
|
||||
}).catch((err: any) => logger.warn(`[AgentClients] failed to record tool result: ${err.message}`))
|
||||
}
|
||||
|
||||
private pendingToolKey(roomId: string, toolName: string): string {
|
||||
return `${roomId}::${toolName || 'tool'}`
|
||||
}
|
||||
|
||||
private trackPendingToolCall(roomId: string, toolName: string, toolCallId: string): void {
|
||||
const key = this.pendingToolKey(roomId, toolName)
|
||||
const list = this.pendingToolCallIds.get(key) || []
|
||||
list.push(toolCallId)
|
||||
this.pendingToolCallIds.set(key, list)
|
||||
}
|
||||
|
||||
private takePendingToolCall(roomId: string, toolName: string): string | undefined {
|
||||
const key = this.pendingToolKey(roomId, toolName)
|
||||
const list = this.pendingToolCallIds.get(key)
|
||||
if (!list?.length) return undefined
|
||||
const id = list.shift()
|
||||
if (list.length) this.pendingToolCallIds.set(key, list)
|
||||
else this.pendingToolCallIds.delete(key)
|
||||
return id
|
||||
}
|
||||
|
||||
private nextToolIndex(roomId: string, toolName: string): number {
|
||||
const key = this.pendingToolKey(roomId, toolName)
|
||||
return (this.pendingToolCallIds.get(key)?.length || 0) + 1
|
||||
}
|
||||
|
||||
private bindEvents(): void {
|
||||
const s = this.socket!
|
||||
|
||||
@@ -387,77 +569,79 @@ class AgentClient {
|
||||
}
|
||||
}
|
||||
|
||||
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 groupBridgeSessionId(roomId: string, profile: string, name: string, sessionSeed: string): string {
|
||||
const raw = `gc_${roomId}_${profile}_${name}_${sessionSeed || '0'}`
|
||||
return raw.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 120)
|
||||
}
|
||||
|
||||
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 groupMessageId(roomId: string, profile: string, name: string): string {
|
||||
const raw = `gcmsg_${safeId(roomId)}_${safeId(profile)}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`
|
||||
return raw.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 160)
|
||||
}
|
||||
|
||||
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 || '')
|
||||
}
|
||||
function groupMessagePartId(runMessageId: string, partIndex: number): string {
|
||||
return `${safeId(runMessageId)}_part_${partIndex}`
|
||||
}
|
||||
|
||||
function groupToolCallId(rawToolCallId: unknown, toolName: string, index: number): string {
|
||||
const raw = String(rawToolCallId || '').trim()
|
||||
if (raw) return raw
|
||||
return `cli_${safeId(toolName || 'tool')}_${Date.now()}_${index}`
|
||||
}
|
||||
|
||||
function safeId(value: string): string {
|
||||
return String(value || 'item').replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 80)
|
||||
}
|
||||
|
||||
function bridgeToolOutput(ev: Record<string, unknown>): string {
|
||||
const value = ev.result ?? ev.output ?? ev.result_preview ?? ev.preview ?? ''
|
||||
return typeof value === 'string' ? value : JSON.stringify(value ?? '')
|
||||
}
|
||||
|
||||
function normalizeToolArgs(value: unknown): Record<string, unknown> {
|
||||
if (!value) return {}
|
||||
if (typeof value === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(value)
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, unknown> : { value }
|
||||
} catch {
|
||||
return { value }
|
||||
}
|
||||
}
|
||||
if (parts.length > 0) return parts.join('')
|
||||
return typeof response?.output_text === 'string' ? response.output_text : ''
|
||||
return typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : { value }
|
||||
}
|
||||
|
||||
function extractBridgeFinalText(chunk: AgentBridgeOutput | null): string {
|
||||
const result = chunk?.result as any
|
||||
const output = result?.final_response || chunk?.output || ''
|
||||
return typeof output === 'string' ? output.trim() : ''
|
||||
}
|
||||
|
||||
function recordBridgeUsage(roomId: string, profile: string, result: unknown): void {
|
||||
const payload = result as any
|
||||
const usage = payload?.usage || payload?.response?.usage
|
||||
if (!usage) return
|
||||
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: payload?.model || payload?.response?.model || '',
|
||||
profile,
|
||||
})
|
||||
}
|
||||
|
||||
// ─── 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 } }>>()
|
||||
private _mentionQueue = new Map<string, Array<{ agent: AgentClient; msg: MentionMessage }>>()
|
||||
|
||||
/**
|
||||
* Create an agent client and connect it to the server.
|
||||
@@ -468,7 +652,6 @@ export class AgentClients {
|
||||
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)
|
||||
|
||||
@@ -557,6 +740,13 @@ export class AgentClients {
|
||||
return Promise.all(agents.map((agent) => agent.sendMessage(roomId, content)))
|
||||
}
|
||||
|
||||
async interruptAgent(roomId: string, agentName: string): Promise<void> {
|
||||
const agent = this.getAgents(roomId).find(a => a.name === agentName)
|
||||
if (!agent) throw new Error(`Agent "${agentName}" not found in room "${roomId}"`)
|
||||
this._mentionQueue.delete(`${roomId}:${agent.name}`)
|
||||
await agent.interrupt(roomId)
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect all agents in a room.
|
||||
*/
|
||||
@@ -576,7 +766,12 @@ export class AgentClients {
|
||||
|
||||
resetRoomContext(roomId: string): void {
|
||||
this._mentionQueue.delete(roomId)
|
||||
this._processingRooms.delete(roomId)
|
||||
for (const key of Array.from(this._mentionQueue.keys())) {
|
||||
if (key.startsWith(`${roomId}:`)) this._mentionQueue.delete(key)
|
||||
}
|
||||
for (const key of Array.from(this._processingRooms)) {
|
||||
if (key.startsWith(`${roomId}:`)) this._processingRooms.delete(key)
|
||||
}
|
||||
if (this._contextEngine) {
|
||||
try { this._contextEngine.invalidateRoom(roomId) } catch { /* ignore */ }
|
||||
}
|
||||
@@ -593,16 +788,6 @@ export class AgentClients {
|
||||
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.
|
||||
*/
|
||||
@@ -628,13 +813,14 @@ export class AgentClients {
|
||||
* 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()
|
||||
async processMentions(roomId: string, msg: MentionMessage): Promise<void> {
|
||||
const agents = this.getAgents(roomId)
|
||||
const senderName = msg.senderName.toLowerCase()
|
||||
|
||||
const mentioned = agents.filter(a => content.includes(`@${a.name.toLowerCase()}`))
|
||||
const mentioned = agents.filter(a => (
|
||||
a.name.toLowerCase() !== senderName &&
|
||||
isAgentMentioned(msg.content, a.name)
|
||||
))
|
||||
if (mentioned.length === 0) return
|
||||
|
||||
logger.debug(`[AgentClients] ${mentioned.map(a => a.name).join(', ')} mentioned by ${msg.senderName}`)
|
||||
@@ -652,7 +838,7 @@ export class AgentClients {
|
||||
private async _processAgentMention(
|
||||
roomId: string,
|
||||
agent: AgentClient,
|
||||
msg: { content: string; senderName: string; senderId: string; timestamp: number },
|
||||
msg: MentionMessage,
|
||||
): Promise<void> {
|
||||
const agentKey = `${roomId}:${agent.name}`
|
||||
if (this._processingRooms.has(agentKey)) {
|
||||
@@ -693,9 +879,16 @@ export class AgentClients {
|
||||
|
||||
// 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}`)
|
||||
})
|
||||
await this._processAgentMention(roomId, last.agent, last.msg)
|
||||
}
|
||||
}
|
||||
|
||||
function nextMentionDepth(msg: MentionMessage): number {
|
||||
return Math.max(0, msg.mentionDepth || 0) + 1
|
||||
}
|
||||
|
||||
function isAgentMentioned(content: string, agentName: string): boolean {
|
||||
const escaped = agentName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
const pattern = new RegExp(`@${escaped}(?=$|\\s|[.,!?;:,。!?;:])`, 'i')
|
||||
return pattern.test(content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user