feat: update sidebar agent workflow and host filter UI
This commit is contained in:
@@ -12,6 +12,7 @@ export interface TerminalManager {
|
||||
export function createTerminalManager(): TerminalManager {
|
||||
const terminalsByCwd = new Map<string, TerminalSession[]>();
|
||||
const terminalsById = new Map<string, TerminalSession>();
|
||||
const terminalExitUnsubscribeById = new Map<string, () => void>();
|
||||
|
||||
function assertAbsolutePath(cwd: string): void {
|
||||
if (!cwd.startsWith("/")) {
|
||||
@@ -19,16 +20,56 @@ export function createTerminalManager(): TerminalManager {
|
||||
}
|
||||
}
|
||||
|
||||
function removeSessionById(id: string, options: { kill: boolean }): void {
|
||||
const session = terminalsById.get(id);
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
|
||||
const unsubscribeExit = terminalExitUnsubscribeById.get(id);
|
||||
if (unsubscribeExit) {
|
||||
unsubscribeExit();
|
||||
terminalExitUnsubscribeById.delete(id);
|
||||
}
|
||||
|
||||
terminalsById.delete(id);
|
||||
|
||||
const terminals = terminalsByCwd.get(session.cwd);
|
||||
if (terminals) {
|
||||
const index = terminals.findIndex((terminal) => terminal.id === id);
|
||||
if (index !== -1) {
|
||||
terminals.splice(index, 1);
|
||||
}
|
||||
if (terminals.length === 0) {
|
||||
terminalsByCwd.delete(session.cwd);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.kill) {
|
||||
session.kill();
|
||||
}
|
||||
}
|
||||
|
||||
function registerSession(session: TerminalSession): TerminalSession {
|
||||
terminalsById.set(session.id, session);
|
||||
const unsubscribeExit = session.onExit(() => {
|
||||
removeSessionById(session.id, { kill: false });
|
||||
});
|
||||
terminalExitUnsubscribeById.set(session.id, unsubscribeExit);
|
||||
return session;
|
||||
}
|
||||
|
||||
return {
|
||||
async getTerminals(cwd: string): Promise<TerminalSession[]> {
|
||||
assertAbsolutePath(cwd);
|
||||
|
||||
let terminals = terminalsByCwd.get(cwd);
|
||||
if (!terminals || terminals.length === 0) {
|
||||
const session = await createTerminal({ cwd, name: "Terminal 1" });
|
||||
const session = registerSession(
|
||||
await createTerminal({ cwd, name: "Terminal 1" })
|
||||
);
|
||||
terminals = [session];
|
||||
terminalsByCwd.set(cwd, terminals);
|
||||
terminalsById.set(session.id, session);
|
||||
}
|
||||
return terminals;
|
||||
},
|
||||
@@ -38,14 +79,15 @@ export function createTerminalManager(): TerminalManager {
|
||||
|
||||
const terminals = terminalsByCwd.get(options.cwd) ?? [];
|
||||
const defaultName = `Terminal ${terminals.length + 1}`;
|
||||
const session = await createTerminal({
|
||||
cwd: options.cwd,
|
||||
name: options.name ?? defaultName,
|
||||
});
|
||||
const session = registerSession(
|
||||
await createTerminal({
|
||||
cwd: options.cwd,
|
||||
name: options.name ?? defaultName,
|
||||
})
|
||||
);
|
||||
|
||||
terminals.push(session);
|
||||
terminalsByCwd.set(options.cwd, terminals);
|
||||
terminalsById.set(session.id, session);
|
||||
|
||||
return session;
|
||||
},
|
||||
@@ -55,22 +97,7 @@ export function createTerminalManager(): TerminalManager {
|
||||
},
|
||||
|
||||
killTerminal(id: string): void {
|
||||
const session = terminalsById.get(id);
|
||||
if (!session) return;
|
||||
|
||||
session.kill();
|
||||
terminalsById.delete(id);
|
||||
|
||||
const terminals = terminalsByCwd.get(session.cwd);
|
||||
if (terminals) {
|
||||
const index = terminals.indexOf(session);
|
||||
if (index !== -1) {
|
||||
terminals.splice(index, 1);
|
||||
}
|
||||
if (terminals.length === 0) {
|
||||
terminalsByCwd.delete(session.cwd);
|
||||
}
|
||||
}
|
||||
removeSessionById(id, { kill: true });
|
||||
},
|
||||
|
||||
listDirectories(): string[] {
|
||||
@@ -78,11 +105,9 @@ export function createTerminalManager(): TerminalManager {
|
||||
},
|
||||
|
||||
killAll(): void {
|
||||
for (const session of terminalsById.values()) {
|
||||
session.kill();
|
||||
for (const id of Array.from(terminalsById.keys())) {
|
||||
removeSessionById(id, { kill: true });
|
||||
}
|
||||
terminalsByCwd.clear();
|
||||
terminalsById.clear();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user