2026-03-29 04:02:13 +00:00
|
|
|
import { getAgentConfigKey } from "../../shared/agent-display-names"
|
|
|
|
|
|
2025-12-09 16:27:46 +09:00
|
|
|
export const subagentSessions = new Set<string>()
|
2026-02-19 04:41:00 +02:00
|
|
|
export const syncSubagentSessions = new Set<string>()
|
2025-12-09 16:27:46 +09:00
|
|
|
|
2026-01-17 16:57:31 +09:00
|
|
|
let _mainSessionID: string | undefined
|
2025-12-09 16:27:46 +09:00
|
|
|
|
|
|
|
|
export function setMainSession(id: string | undefined) {
|
2026-01-17 16:57:31 +09:00
|
|
|
_mainSessionID = id
|
2025-12-09 16:27:46 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getMainSessionID(): string | undefined {
|
2026-01-17 16:57:31 +09:00
|
|
|
return _mainSessionID
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 21:13:44 +09:00
|
|
|
const registeredAgentNames = new Set<string>()
|
2026-04-08 16:18:26 +09:00
|
|
|
const registeredAgentAliases = new Map<string, string>()
|
2026-03-27 21:13:44 +09:00
|
|
|
|
2026-03-31 16:58:15 -07:00
|
|
|
const ZERO_WIDTH_CHARACTERS_REGEX = /[\u200B\u200C\u200D\uFEFF]/g
|
|
|
|
|
|
|
|
|
|
function normalizeRegisteredAgentName(name: string): string {
|
|
|
|
|
return name.replace(ZERO_WIDTH_CHARACTERS_REGEX, "").toLowerCase()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 18:07:26 +09:00
|
|
|
function normalizeStoredAgentName(name: string): string {
|
|
|
|
|
return name.replace(ZERO_WIDTH_CHARACTERS_REGEX, "")
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 21:13:44 +09:00
|
|
|
export function registerAgentName(name: string): void {
|
2026-03-31 16:58:15 -07:00
|
|
|
const normalizedName = normalizeRegisteredAgentName(name)
|
2026-03-29 04:02:13 +00:00
|
|
|
registeredAgentNames.add(normalizedName)
|
2026-04-08 16:18:26 +09:00
|
|
|
if (!registeredAgentAliases.has(normalizedName)) {
|
|
|
|
|
registeredAgentAliases.set(normalizedName, name)
|
|
|
|
|
}
|
2026-03-29 04:02:13 +00:00
|
|
|
|
2026-03-31 16:58:15 -07:00
|
|
|
const configKey = normalizeRegisteredAgentName(getAgentConfigKey(name))
|
2026-03-29 04:02:13 +00:00
|
|
|
if (configKey !== normalizedName) {
|
|
|
|
|
registeredAgentNames.add(configKey)
|
2026-04-08 16:18:26 +09:00
|
|
|
if (!registeredAgentAliases.has(configKey)) {
|
|
|
|
|
registeredAgentAliases.set(configKey, name)
|
|
|
|
|
}
|
2026-03-29 04:02:13 +00:00
|
|
|
}
|
2026-03-27 21:13:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isAgentRegistered(name: string): boolean {
|
2026-03-31 16:58:15 -07:00
|
|
|
return registeredAgentNames.has(normalizeRegisteredAgentName(name))
|
2026-03-27 21:13:44 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 16:18:26 +09:00
|
|
|
export function resolveRegisteredAgentName(name: string | undefined): string | undefined {
|
|
|
|
|
if (typeof name !== "string") {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const normalizedName = normalizeRegisteredAgentName(name)
|
|
|
|
|
return registeredAgentAliases.get(normalizedName) ?? normalizeStoredAgentName(name)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 16:57:31 +09:00
|
|
|
/** @internal For testing only */
|
|
|
|
|
export function _resetForTesting(): void {
|
|
|
|
|
_mainSessionID = undefined
|
|
|
|
|
subagentSessions.clear()
|
2026-02-19 04:41:00 +02:00
|
|
|
syncSubagentSessions.clear()
|
2026-01-28 14:33:14 +09:00
|
|
|
sessionAgentMap.clear()
|
2026-03-27 21:13:44 +09:00
|
|
|
registeredAgentNames.clear()
|
2026-04-08 16:18:26 +09:00
|
|
|
registeredAgentAliases.clear()
|
2025-12-09 16:27:46 +09:00
|
|
|
}
|
2026-01-13 07:39:25 +07:00
|
|
|
|
|
|
|
|
const sessionAgentMap = new Map<string, string>()
|
|
|
|
|
|
|
|
|
|
export function setSessionAgent(sessionID: string, agent: string): void {
|
|
|
|
|
if (!sessionAgentMap.has(sessionID)) {
|
2026-04-06 18:07:26 +09:00
|
|
|
sessionAgentMap.set(sessionID, normalizeStoredAgentName(agent))
|
2026-01-13 07:39:25 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function updateSessionAgent(sessionID: string, agent: string): void {
|
2026-04-06 18:07:26 +09:00
|
|
|
sessionAgentMap.set(sessionID, normalizeStoredAgentName(agent))
|
2026-01-13 07:39:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getSessionAgent(sessionID: string): string | undefined {
|
|
|
|
|
return sessionAgentMap.get(sessionID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearSessionAgent(sessionID: string): void {
|
|
|
|
|
sessionAgentMap.delete(sessionID)
|
|
|
|
|
}
|