1da7df1aee
This reverts commitf1a11f2c92, reversing changes made to62c19ce0ef.
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
import { getAgentConfigKey } from "../../shared/agent-display-names"
|
|
|
|
export const subagentSessions = new Set<string>()
|
|
export const syncSubagentSessions = new Set<string>()
|
|
|
|
let _mainSessionID: string | undefined
|
|
|
|
export function setMainSession(id: string | undefined) {
|
|
_mainSessionID = id
|
|
}
|
|
|
|
export function getMainSessionID(): string | undefined {
|
|
return _mainSessionID
|
|
}
|
|
|
|
const registeredAgentNames = new Set<string>()
|
|
const registeredAgentAliases = new Map<string, string>()
|
|
|
|
const ZERO_WIDTH_CHARACTERS_REGEX = /[\u200B\u200C\u200D\uFEFF]/g
|
|
|
|
function normalizeRegisteredAgentName(name: string): string {
|
|
return name.replace(ZERO_WIDTH_CHARACTERS_REGEX, "").toLowerCase()
|
|
}
|
|
|
|
function normalizeStoredAgentName(name: string): string {
|
|
return name.replace(ZERO_WIDTH_CHARACTERS_REGEX, "")
|
|
}
|
|
|
|
export function registerAgentName(name: string): void {
|
|
const normalizedName = normalizeRegisteredAgentName(name)
|
|
registeredAgentNames.add(normalizedName)
|
|
if (!registeredAgentAliases.has(normalizedName)) {
|
|
registeredAgentAliases.set(normalizedName, name)
|
|
}
|
|
|
|
const configKey = normalizeRegisteredAgentName(getAgentConfigKey(name))
|
|
if (configKey !== normalizedName) {
|
|
registeredAgentNames.add(configKey)
|
|
if (!registeredAgentAliases.has(configKey)) {
|
|
registeredAgentAliases.set(configKey, name)
|
|
}
|
|
}
|
|
}
|
|
|
|
export function isAgentRegistered(name: string): boolean {
|
|
return registeredAgentNames.has(normalizeRegisteredAgentName(name))
|
|
}
|
|
|
|
export function resolveRegisteredAgentName(name: string | undefined): string | undefined {
|
|
if (typeof name !== "string") {
|
|
return undefined
|
|
}
|
|
|
|
const normalizedName = normalizeRegisteredAgentName(name)
|
|
const directMatch = registeredAgentAliases.get(normalizedName)
|
|
if (directMatch !== undefined) return directMatch
|
|
|
|
// Resolve legacy/capitalized agent names (e.g. "Sisyphus (Ultraworker)")
|
|
// to their config key, then look up the registered alias for that key.
|
|
const configKey = getAgentConfigKey(name)
|
|
const normalizedConfigKey = normalizeRegisteredAgentName(configKey)
|
|
if (normalizedConfigKey !== normalizedName) {
|
|
const aliasMatch = registeredAgentAliases.get(normalizedConfigKey)
|
|
if (aliasMatch !== undefined) return aliasMatch
|
|
}
|
|
|
|
return normalizeStoredAgentName(name)
|
|
}
|
|
|
|
/** @internal For testing only */
|
|
export function _resetForTesting(): void {
|
|
_mainSessionID = undefined
|
|
subagentSessions.clear()
|
|
syncSubagentSessions.clear()
|
|
sessionAgentMap.clear()
|
|
registeredAgentNames.clear()
|
|
registeredAgentAliases.clear()
|
|
}
|
|
|
|
const sessionAgentMap = new Map<string, string>()
|
|
|
|
export function setSessionAgent(sessionID: string, agent: string): void {
|
|
if (!sessionAgentMap.has(sessionID)) {
|
|
sessionAgentMap.set(sessionID, normalizeStoredAgentName(agent))
|
|
}
|
|
}
|
|
|
|
export function updateSessionAgent(sessionID: string, agent: string): void {
|
|
sessionAgentMap.set(sessionID, normalizeStoredAgentName(agent))
|
|
}
|
|
|
|
export function getSessionAgent(sessionID: string): string | undefined {
|
|
return sessionAgentMap.get(sessionID)
|
|
}
|
|
|
|
export function clearSessionAgent(sessionID: string): void {
|
|
sessionAgentMap.delete(sessionID)
|
|
}
|