refactor: integrate SessionManager into daemon and agent

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
William Valentin
2026-02-05 00:43:09 -08:00
parent f671ea5ab5
commit fb7575f850
3 changed files with 78 additions and 86 deletions
+21 -22
View File
@@ -1,59 +1,58 @@
import type { ModelClient, Message } from '../../models/types.js';
import type { SessionStore } from '../../session/index.js';
import type { Session } from '../../session/index.js';
export interface NativeAgentConfig {
modelClient: ModelClient;
systemPrompt: string;
sessionStore?: SessionStore;
sessionId?: string;
session?: Session;
}
export class NativeAgent {
private modelClient: ModelClient;
private systemPrompt: string;
private sessionStore?: SessionStore;
private sessionId: string;
private history: Message[] = [];
private session?: Session;
private inMemoryHistory: Message[] = [];
constructor(config: NativeAgentConfig) {
this.modelClient = config.modelClient;
this.systemPrompt = config.systemPrompt;
this.sessionStore = config.sessionStore;
this.sessionId = config.sessionId ?? 'default';
this.session = config.session;
}
// Load existing history from store
if (this.sessionStore) {
this.history = this.sessionStore.getMessages(this.sessionId);
}
private get history(): Message[] {
return this.session?.getHistory() ?? [...this.inMemoryHistory];
}
async process(userMessage: string): Promise<string> {
const userMsg: Message = { role: 'user', content: userMessage };
this.history.push(userMsg);
if (this.sessionStore) {
this.sessionStore.addMessage(this.sessionId, userMsg);
if (this.session) {
this.session.addMessage(userMsg);
} else {
this.inMemoryHistory.push(userMsg);
}
const response = await this.modelClient.chat({
messages: [...this.history],
messages: this.history,
system: this.systemPrompt,
});
const assistantMsg: Message = { role: 'assistant', content: response.content };
this.history.push(assistantMsg);
if (this.sessionStore) {
this.sessionStore.addMessage(this.sessionId, assistantMsg);
if (this.session) {
this.session.addMessage(assistantMsg);
} else {
this.inMemoryHistory.push(assistantMsg);
}
return response.content;
}
reset(): void {
this.history = [];
if (this.sessionStore) {
this.sessionStore.clearSession(this.sessionId);
if (this.session) {
this.session.clear();
} else {
this.inMemoryHistory = [];
}
}