refactor: integrate SessionManager into daemon and agent
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+12
-9
@@ -4,7 +4,7 @@ import type { Config } from '../config/index.js';
|
||||
import { AnthropicClient, OpenAIClient, OllamaClient, ModelRouter } from '../models/index.js';
|
||||
import { NativeAgent } from '../backends/index.js';
|
||||
import { createTelegramBot } from '../frontends/telegram/index.js';
|
||||
import { SessionStore } from '../session/index.js';
|
||||
import { SessionStore, SessionManager } from '../session/index.js';
|
||||
import { HookEngine } from '../hooks/index.js';
|
||||
import { resolve } from 'path';
|
||||
import { homedir } from 'os';
|
||||
@@ -16,6 +16,7 @@ export interface DaemonContext {
|
||||
bot: Bot;
|
||||
agent: NativeAgent;
|
||||
sessionStore: SessionStore;
|
||||
sessionManager: SessionManager;
|
||||
hookEngine: HookEngine;
|
||||
modelRouter: ModelRouter;
|
||||
}
|
||||
@@ -27,12 +28,10 @@ Keep responses focused and avoid unnecessary verbosity. Use markdown formatting
|
||||
function createModelRouter(config: Config): ModelRouter {
|
||||
const models = config.models;
|
||||
|
||||
// Create default client (required)
|
||||
const defaultClient = new AnthropicClient({
|
||||
model: models.default.model,
|
||||
});
|
||||
|
||||
// Create optional tier clients
|
||||
let fastClient;
|
||||
let complexClient;
|
||||
let localClient;
|
||||
@@ -54,7 +53,6 @@ function createModelRouter(config: Config): ModelRouter {
|
||||
}
|
||||
}
|
||||
|
||||
// Build fallback chain
|
||||
const fallbackChain = [];
|
||||
for (const providerName of models.fallback_chain) {
|
||||
if (providerName === 'openai') {
|
||||
@@ -80,8 +78,10 @@ export async function startDaemon(config: Config): Promise<DaemonContext> {
|
||||
const dataDir = resolve(homedir(), '.local/share/flynn');
|
||||
mkdirSync(dataDir, { recursive: true });
|
||||
|
||||
// Initialize session store
|
||||
// Initialize session store and manager
|
||||
const sessionStore = new SessionStore(resolve(dataDir, 'sessions.db'));
|
||||
const sessionManager = new SessionManager(sessionStore);
|
||||
|
||||
lifecycle.onShutdown(async () => {
|
||||
sessionStore.close();
|
||||
console.log('Session store closed');
|
||||
@@ -93,12 +93,15 @@ export async function startDaemon(config: Config): Promise<DaemonContext> {
|
||||
// Initialize model router
|
||||
const modelRouter = createModelRouter(config);
|
||||
|
||||
// Initialize native agent with session persistence
|
||||
// Get Telegram session
|
||||
const telegramUserId = String(config.telegram.allowed_chat_ids[0]);
|
||||
const session = sessionManager.getSession('telegram', telegramUserId);
|
||||
|
||||
// Initialize native agent with session
|
||||
const agent = new NativeAgent({
|
||||
modelClient: modelRouter,
|
||||
systemPrompt: SYSTEM_PROMPT,
|
||||
sessionStore,
|
||||
sessionId: `telegram-${config.telegram.allowed_chat_ids[0]}`,
|
||||
session,
|
||||
});
|
||||
|
||||
// Initialize Telegram bot with hook engine
|
||||
@@ -136,7 +139,7 @@ export async function startDaemon(config: Config): Promise<DaemonContext> {
|
||||
|
||||
console.log('Flynn daemon started');
|
||||
|
||||
return { config, lifecycle, bot, agent, sessionStore, hookEngine, modelRouter };
|
||||
return { config, lifecycle, bot, agent, sessionStore, sessionManager, hookEngine, modelRouter };
|
||||
}
|
||||
|
||||
export { Lifecycle } from './lifecycle.js';
|
||||
|
||||
Reference in New Issue
Block a user