feat(tools): add switch agent background workflow

This commit is contained in:
YeonGyu-Kim
2026-03-26 12:59:36 +09:00
parent 647f691fe2
commit 1c125ec3ef
25 changed files with 607 additions and 0 deletions
@@ -1 +1,2 @@
export * from "./state"
export * from "./switch-agent-state"
@@ -1,3 +1,5 @@
import { resetPendingSessionAgentSwitchesForTesting } from "./switch-agent-state"
export const subagentSessions = new Set<string>()
export const syncSubagentSessions = new Set<string>()
@@ -17,6 +19,7 @@ export function _resetForTesting(): void {
subagentSessions.clear()
syncSubagentSessions.clear()
sessionAgentMap.clear()
resetPendingSessionAgentSwitchesForTesting()
}
const sessionAgentMap = new Map<string, string>()
@@ -0,0 +1,38 @@
import { describe, expect, test, beforeEach } from "bun:test"
import {
clearPendingSessionAgentSwitch,
consumePendingSessionAgentSwitch,
getPendingSessionAgentSwitch,
resetPendingSessionAgentSwitchesForTesting,
setPendingSessionAgentSwitch,
} from "./switch-agent-state"
describe("switch-agent-state", () => {
beforeEach(() => {
resetPendingSessionAgentSwitchesForTesting()
})
test("#given pending switch #when consuming #then consumes once and clears", () => {
// given
setPendingSessionAgentSwitch("ses-1", "explore")
// when
const first = consumePendingSessionAgentSwitch("ses-1")
const second = consumePendingSessionAgentSwitch("ses-1")
// then
expect(first?.agent).toBe("explore")
expect(second).toBeUndefined()
})
test("#given pending switch #when clearing #then state is removed", () => {
// given
setPendingSessionAgentSwitch("ses-1", "librarian")
// when
clearPendingSessionAgentSwitch("ses-1")
// then
expect(getPendingSessionAgentSwitch("ses-1")).toBeUndefined()
})
})
@@ -0,0 +1,37 @@
type PendingAgentSwitch = {
agent: string
requestedAt: Date
}
const pendingAgentSwitchBySession = new Map<string, PendingAgentSwitch>()
export function setPendingSessionAgentSwitch(sessionID: string, agent: string): PendingAgentSwitch {
const pendingSwitch: PendingAgentSwitch = {
agent,
requestedAt: new Date(),
}
pendingAgentSwitchBySession.set(sessionID, pendingSwitch)
return pendingSwitch
}
export function getPendingSessionAgentSwitch(sessionID: string): PendingAgentSwitch | undefined {
return pendingAgentSwitchBySession.get(sessionID)
}
export function consumePendingSessionAgentSwitch(sessionID: string): PendingAgentSwitch | undefined {
const pendingSwitch = pendingAgentSwitchBySession.get(sessionID)
if (!pendingSwitch) {
return undefined
}
pendingAgentSwitchBySession.delete(sessionID)
return pendingSwitch
}
export function clearPendingSessionAgentSwitch(sessionID: string): void {
pendingAgentSwitchBySession.delete(sessionID)
}
export function resetPendingSessionAgentSwitchesForTesting(): void {
pendingAgentSwitchBySession.clear()
}