mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 02:04:32 +00:00
Add agentDir parameter to SessionManager factory methods
This commit is contained in:
parent
ace8ea3d5b
commit
6d4ff74430
1 changed files with 16 additions and 17 deletions
|
|
@ -2,7 +2,7 @@ import type { AgentState, AppMessage } from "@mariozechner/pi-agent-core";
|
||||||
import { randomBytes } from "crypto";
|
import { randomBytes } from "crypto";
|
||||||
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync } from "fs";
|
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync } from "fs";
|
||||||
import { join, resolve } from "path";
|
import { join, resolve } from "path";
|
||||||
import { getAgentDir } from "../config.js";
|
import { getAgentDir as getDefaultAgentDir } from "../config.js";
|
||||||
|
|
||||||
function uuidv4(): string {
|
function uuidv4(): string {
|
||||||
const bytes = randomBytes(16);
|
const bytes = randomBytes(16);
|
||||||
|
|
@ -165,10 +165,9 @@ export function loadSessionFromEntries(entries: SessionEntry[]): LoadedSession {
|
||||||
return { messages, thinkingLevel, model };
|
return { messages, thinkingLevel, model };
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSessionDirectory(cwd: string): string {
|
function getSessionDirectory(cwd: string, agentDir: string): string {
|
||||||
const safePath = `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
const safePath = `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
||||||
const configDir = getAgentDir();
|
const sessionDir = join(agentDir, "sessions", safePath);
|
||||||
const sessionDir = join(configDir, "sessions", safePath);
|
|
||||||
if (!existsSync(sessionDir)) {
|
if (!existsSync(sessionDir)) {
|
||||||
mkdirSync(sessionDir, { recursive: true });
|
mkdirSync(sessionDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
@ -238,9 +237,9 @@ export class SessionManager {
|
||||||
private pendingEntries: SessionEntry[] = [];
|
private pendingEntries: SessionEntry[] = [];
|
||||||
private inMemoryEntries: SessionEntry[] = [];
|
private inMemoryEntries: SessionEntry[] = [];
|
||||||
|
|
||||||
private constructor(cwd: string, sessionFile: string | null, enabled: boolean) {
|
private constructor(cwd: string, agentDir: string, sessionFile: string | null, enabled: boolean) {
|
||||||
this.cwd = cwd;
|
this.cwd = cwd;
|
||||||
this.sessionDir = getSessionDirectory(cwd);
|
this.sessionDir = getSessionDirectory(cwd, agentDir);
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
|
|
||||||
if (sessionFile) {
|
if (sessionFile) {
|
||||||
|
|
@ -259,37 +258,37 @@ export class SessionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create a new session for the given directory */
|
/** Create a new session for the given directory */
|
||||||
static create(cwd: string): SessionManager {
|
static create(cwd: string, agentDir: string = getDefaultAgentDir()): SessionManager {
|
||||||
return new SessionManager(cwd, null, true);
|
return new SessionManager(cwd, agentDir, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Open a specific session file */
|
/** Open a specific session file */
|
||||||
static open(path: string): SessionManager {
|
static open(path: string, agentDir: string = getDefaultAgentDir()): SessionManager {
|
||||||
// Extract cwd from session header if possible, otherwise use process.cwd()
|
// Extract cwd from session header if possible, otherwise use process.cwd()
|
||||||
const entries = loadEntriesFromFile(path);
|
const entries = loadEntriesFromFile(path);
|
||||||
const header = entries.find((e) => e.type === "session") as SessionHeader | undefined;
|
const header = entries.find((e) => e.type === "session") as SessionHeader | undefined;
|
||||||
const cwd = header?.cwd ?? process.cwd();
|
const cwd = header?.cwd ?? process.cwd();
|
||||||
return new SessionManager(cwd, path, true);
|
return new SessionManager(cwd, agentDir, path, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Continue the most recent session for the given directory, or create new if none */
|
/** Continue the most recent session for the given directory, or create new if none */
|
||||||
static continueRecent(cwd: string): SessionManager {
|
static continueRecent(cwd: string, agentDir: string = getDefaultAgentDir()): SessionManager {
|
||||||
const sessionDir = getSessionDirectory(cwd);
|
const sessionDir = getSessionDirectory(cwd, agentDir);
|
||||||
const mostRecent = findMostRecentSession(sessionDir);
|
const mostRecent = findMostRecentSession(sessionDir);
|
||||||
if (mostRecent) {
|
if (mostRecent) {
|
||||||
return new SessionManager(cwd, mostRecent, true);
|
return new SessionManager(cwd, agentDir, mostRecent, true);
|
||||||
}
|
}
|
||||||
return new SessionManager(cwd, null, true);
|
return new SessionManager(cwd, agentDir, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create an in-memory session (no file persistence) */
|
/** Create an in-memory session (no file persistence) */
|
||||||
static inMemory(): SessionManager {
|
static inMemory(): SessionManager {
|
||||||
return new SessionManager(process.cwd(), null, false);
|
return new SessionManager(process.cwd(), getDefaultAgentDir(), null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** List all sessions for a directory */
|
/** List all sessions for a directory */
|
||||||
static list(cwd: string): SessionInfo[] {
|
static list(cwd: string, agentDir: string = getDefaultAgentDir()): SessionInfo[] {
|
||||||
const sessionDir = getSessionDirectory(cwd);
|
const sessionDir = getSessionDirectory(cwd, agentDir);
|
||||||
const sessions: SessionInfo[] = [];
|
const sessions: SessionInfo[] = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue