mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-16 17:01:02 +00:00
Refactor SessionManager to use static factory methods
- Add factory methods: create(cwd), open(path), continueRecent(cwd), inMemory() - Add static list(cwd) for session listing - Make constructor private, pass cwd explicitly - Update SDK to take sessionManager instead of sessionFile options - Update main.ts to create SessionManager based on CLI flags - Update SessionSelectorComponent to take sessions[] instead of SessionManager - Update tests to use factory methods
This commit is contained in:
parent
7bf4c8ff24
commit
ace8ea3d5b
9 changed files with 346 additions and 473 deletions
|
|
@ -106,12 +106,8 @@ export interface CreateAgentSessionOptions {
|
|||
/** Slash commands. Default: discovered from cwd/.pi/commands/ + agentDir/commands/ */
|
||||
slashCommands?: FileSlashCommand[];
|
||||
|
||||
/** Session file path, or false to disable persistence. Default: auto in agentDir/sessions/ */
|
||||
sessionFile?: string | false;
|
||||
/** Continue most recent session for cwd. */
|
||||
continueSession?: boolean;
|
||||
/** Restore model/thinking from session (default: true when continuing). */
|
||||
restoreFromSession?: boolean;
|
||||
/** Session manager. Default: SessionManager.create(cwd) */
|
||||
sessionManager?: SessionManager;
|
||||
|
||||
/** Settings overrides (merged with agentDir/settings.json) */
|
||||
settings?: Partial<Settings>;
|
||||
|
|
@ -411,7 +407,7 @@ function createLoadedHooksFromDefinitions(definitions: Array<{ path?: string; fa
|
|||
* tools: [readTool, bashTool],
|
||||
* hooks: [],
|
||||
* skills: [],
|
||||
* sessionFile: false,
|
||||
* sessionManager: SessionManager.inMemory(),
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
|
|
@ -420,34 +416,27 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
|||
const agentDir = options.agentDir ?? getDefaultAgentDir();
|
||||
|
||||
const settingsManager = new SettingsManager(agentDir);
|
||||
const sessionManager = options.sessionManager ?? SessionManager.create(cwd);
|
||||
|
||||
const sessionManager = new SessionManager(options.continueSession ?? false, undefined);
|
||||
if (options.sessionFile === false) {
|
||||
sessionManager.disable();
|
||||
} else if (typeof options.sessionFile === "string") {
|
||||
sessionManager.setSessionFile(options.sessionFile);
|
||||
}
|
||||
// Check if session has existing data to restore
|
||||
const existingSession = sessionManager.loadSession();
|
||||
const hasExistingSession = existingSession.messages.length > 0;
|
||||
|
||||
let model = options.model;
|
||||
let modelFallbackMessage: string | undefined;
|
||||
const shouldRestoreFromSession = options.restoreFromSession ?? (options.continueSession || options.sessionFile);
|
||||
|
||||
// If continuing/restoring, try to get model from session first
|
||||
if (!model && shouldRestoreFromSession) {
|
||||
const savedModel = sessionManager.loadModel();
|
||||
if (savedModel) {
|
||||
const restoredModel = findModel(savedModel.provider, savedModel.modelId);
|
||||
if (restoredModel) {
|
||||
const key = await getApiKeyForModel(restoredModel);
|
||||
if (key) {
|
||||
model = restoredModel;
|
||||
}
|
||||
}
|
||||
// If we couldn't restore, we'll fall back below and set fallback message
|
||||
if (!model) {
|
||||
modelFallbackMessage = `Could not restore model ${savedModel.provider}/${savedModel.modelId}`;
|
||||
// If session has data, try to restore model from it
|
||||
if (!model && hasExistingSession && existingSession.model) {
|
||||
const restoredModel = findModel(existingSession.model.provider, existingSession.model.modelId);
|
||||
if (restoredModel) {
|
||||
const key = await getApiKeyForModel(restoredModel);
|
||||
if (key) {
|
||||
model = restoredModel;
|
||||
}
|
||||
}
|
||||
if (!model) {
|
||||
modelFallbackMessage = `Could not restore model ${existingSession.model.provider}/${existingSession.model.modelId}`;
|
||||
}
|
||||
}
|
||||
|
||||
// If still no model, try settings default
|
||||
|
|
@ -482,12 +471,9 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
|||
|
||||
let thinkingLevel = options.thinkingLevel;
|
||||
|
||||
// If continuing/restoring, try to get thinking level from session
|
||||
if (thinkingLevel === undefined && shouldRestoreFromSession) {
|
||||
const savedThinking = sessionManager.loadThinkingLevel();
|
||||
if (savedThinking) {
|
||||
thinkingLevel = savedThinking as ThinkingLevel;
|
||||
}
|
||||
// If session has data, restore thinking level from it
|
||||
if (thinkingLevel === undefined && hasExistingSession) {
|
||||
thinkingLevel = existingSession.thinkingLevel as ThinkingLevel;
|
||||
}
|
||||
|
||||
// Fall back to settings default
|
||||
|
|
@ -595,11 +581,9 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
|||
}),
|
||||
});
|
||||
|
||||
if (shouldRestoreFromSession) {
|
||||
const messages = sessionManager.loadMessages();
|
||||
if (messages.length > 0) {
|
||||
agent.replaceMessages(messages);
|
||||
}
|
||||
// Restore messages if session has existing data
|
||||
if (hasExistingSession) {
|
||||
agent.replaceMessages(existingSession.messages);
|
||||
}
|
||||
|
||||
const session = new AgentSession({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue