mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-30 15:15:24 +00:00
feat: wire ai, automations, fleets and ssh-terminal to snippets (B2)
This commit is contained in:
1 parent
3656caf168
commit
c77301577f
16 files changed
+473
-52
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import type { TerminalHandle } from "@/features/terminal/terminal-types";
|
||||
|
||||
export interface TerminalSessionInfo {
|
||||
id: string;
|
||||
hostId: number | null;
|
||||
hostName?: string;
|
||||
ip?: string;
|
||||
username?: string;
|
||||
port?: number;
|
||||
}
|
||||
|
||||
interface Session extends TerminalSessionInfo {
|
||||
ref: TerminalHandle;
|
||||
}
|
||||
|
||||
const sessions = new Map<string, Session>();
|
||||
let activeId: string | null = null;
|
||||
|
||||
/** Called by the terminal tab wrapper on mount/unmount and focus change. */
|
||||
export function registerSession(
|
||||
info: TerminalSessionInfo,
|
||||
ref: TerminalHandle,
|
||||
): () => void {
|
||||
sessions.set(info.id, { ...info, ref });
|
||||
return () => {
|
||||
sessions.delete(info.id);
|
||||
if (activeId === info.id) activeId = null;
|
||||
};
|
||||
}
|
||||
|
||||
export function setActiveSession(id: string | null): void {
|
||||
activeId = id;
|
||||
}
|
||||
|
||||
export function listSessions(): TerminalSessionInfo[] {
|
||||
return [...sessions.values()].map(({ ref: _ref, ...info }) => info);
|
||||
}
|
||||
|
||||
function send(session: Session, text: string, run?: boolean): void {
|
||||
if (run) session.ref.sendInput(text + "\r");
|
||||
else session.ref.paste(text);
|
||||
}
|
||||
|
||||
export function sendToActive(text: string, opts?: { run?: boolean }): boolean {
|
||||
if (!activeId) return false;
|
||||
const session = sessions.get(activeId);
|
||||
if (!session) return false;
|
||||
send(session, text, opts?.run);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function sendToSession(
|
||||
sessionId: string,
|
||||
text: string,
|
||||
opts?: { run?: boolean },
|
||||
): boolean {
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) return false;
|
||||
send(session, text, opts?.run);
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user