Files
oh-my-openagent/src/hooks/interactive-bash-session/storage.ts
YeonGyu-Kim 5cbef252a3 feat(tools): add interactive_bash tool for tmux session management
Add a new tool for managing tmux sessions with automatic tracking and cleanup:

- interactive_bash tool: Accepts tmux commands via tmux_command parameter
- Session tracking hook: Tracks omo-* prefixed tmux sessions per OpenCode session
- System reminder: Appends active session list after create/delete operations
- Auto cleanup: Kills all tracked tmux sessions on OpenCode session deletion
- Output truncation: Registered in TRUNCATABLE_TOOLS for long capture-pane outputs

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
2025-12-15 19:14:06 +09:00

60 lines
1.6 KiB
TypeScript

import {
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
unlinkSync,
} from "node:fs";
import { join } from "node:path";
import { INTERACTIVE_BASH_SESSION_STORAGE } from "./constants";
import type {
InteractiveBashSessionState,
SerializedInteractiveBashSessionState,
} from "./types";
function getStoragePath(sessionID: string): string {
return join(INTERACTIVE_BASH_SESSION_STORAGE, `${sessionID}.json`);
}
export function loadInteractiveBashSessionState(
sessionID: string,
): InteractiveBashSessionState | null {
const filePath = getStoragePath(sessionID);
if (!existsSync(filePath)) return null;
try {
const content = readFileSync(filePath, "utf-8");
const serialized = JSON.parse(content) as SerializedInteractiveBashSessionState;
return {
sessionID: serialized.sessionID,
tmuxSessions: new Set(serialized.tmuxSessions),
updatedAt: serialized.updatedAt,
};
} catch {
return null;
}
}
export function saveInteractiveBashSessionState(
state: InteractiveBashSessionState,
): void {
if (!existsSync(INTERACTIVE_BASH_SESSION_STORAGE)) {
mkdirSync(INTERACTIVE_BASH_SESSION_STORAGE, { recursive: true });
}
const filePath = getStoragePath(state.sessionID);
const serialized: SerializedInteractiveBashSessionState = {
sessionID: state.sessionID,
tmuxSessions: Array.from(state.tmuxSessions),
updatedAt: state.updatedAt,
};
writeFileSync(filePath, JSON.stringify(serialized, null, 2));
}
export function clearInteractiveBashSessionState(sessionID: string): void {
const filePath = getStoragePath(sessionID);
if (existsSync(filePath)) {
unlinkSync(filePath);
}
}