:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { v7 as uuidv7 } from 'uuid'
import type { ScopedCache } from '@/lib/cache/ScopedCache'
import type { PrefixedString } from '@/lib/types'
import type { State } from '@/tools/types'
export class SessionManager {
private cache: ScopedCache<State>
constructor(cache: ScopedCache<State>) {
this.cache = cache
}
_getKey(sessionId: string): PrefixedString<'session'> {
return `session:${sessionId}`
}
async getSessionUuid(sessionId: string): Promise<string> {
const key = this._getKey(sessionId)
const existingSession = await this.cache.get(key)
if (existingSession?.uuid) {
return existingSession.uuid
}
const newSessionUuid = uuidv7()
await this.cache.set(key, { uuid: newSessionUuid })
return newSessionUuid
}
async hasSession(sessionId: string): Promise<boolean> {
const key = await this._getKey(sessionId)
const session = await this.cache.get(key)
return !!session?.uuid
}
async removeSession(sessionId: string): Promise<void> {
const key = await this._getKey(sessionId)
await this.cache.delete(key)
}
async clearAllSessions(): Promise<void> {
await this.cache.clear()
}
}