mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 07:16:53 +05:00
feat(ipuaro): add session management (v0.10.0)
- Add ISessionStorage interface and RedisSessionStorage implementation - Add ContextManager for token budget and compression - Add StartSession, HandleMessage, UndoChange use cases - Update CHANGELOG and TODO documentation - 88 new tests (1174 total), 97.73% coverage
This commit is contained in:
62
packages/ipuaro/src/application/use-cases/StartSession.ts
Normal file
62
packages/ipuaro/src/application/use-cases/StartSession.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { randomUUID } from "node:crypto"
|
||||
import { Session } from "../../domain/entities/Session.js"
|
||||
import type { ISessionStorage } from "../../domain/services/ISessionStorage.js"
|
||||
|
||||
/**
|
||||
* Options for starting a session.
|
||||
*/
|
||||
export interface StartSessionOptions {
|
||||
/** Force creation of a new session even if one exists */
|
||||
forceNew?: boolean
|
||||
/** Specific session ID to load */
|
||||
sessionId?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of starting a session.
|
||||
*/
|
||||
export interface StartSessionResult {
|
||||
session: Session
|
||||
isNew: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Use case for starting a session.
|
||||
* Creates a new session or loads the latest one for a project.
|
||||
*/
|
||||
export class StartSession {
|
||||
constructor(private readonly sessionStorage: ISessionStorage) {}
|
||||
|
||||
/**
|
||||
* Execute the use case.
|
||||
*
|
||||
* @param projectName - The project name to start a session for
|
||||
* @param options - Optional configuration
|
||||
* @returns The session and whether it was newly created
|
||||
*/
|
||||
async execute(
|
||||
projectName: string,
|
||||
options: StartSessionOptions = {},
|
||||
): Promise<StartSessionResult> {
|
||||
if (options.sessionId) {
|
||||
const session = await this.sessionStorage.loadSession(options.sessionId)
|
||||
if (session) {
|
||||
await this.sessionStorage.touchSession(session.id)
|
||||
return { session, isNew: false }
|
||||
}
|
||||
}
|
||||
|
||||
if (!options.forceNew) {
|
||||
const latestSession = await this.sessionStorage.getLatestSession(projectName)
|
||||
if (latestSession) {
|
||||
await this.sessionStorage.touchSession(latestSession.id)
|
||||
return { session: latestSession, isNew: false }
|
||||
}
|
||||
}
|
||||
|
||||
const session = new Session(randomUUID(), projectName)
|
||||
await this.sessionStorage.saveSession(session)
|
||||
|
||||
return { session, isNew: true }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user