refactor(utils): apply branded types to session-persistence and auto-anchoring
This commit is contained in:
parent
3f4f2d70af
commit
a06b9d5053
2 changed files with 11 additions and 9 deletions
|
|
@ -1,6 +1,7 @@
|
|||
// Auto-anchoring — creates session anchors on first compaction event
|
||||
// Extracted from agent-dispatcher.ts (SRP: anchor creation concern)
|
||||
|
||||
import type { ProjectId as ProjectIdType } from '../types/ids';
|
||||
import type { AgentMessage } from '../adapters/claude-messages';
|
||||
import type { SessionAnchor } from '../types/anchors';
|
||||
import { getAnchorSettings, addAnchors } from '../stores/anchors.svelte';
|
||||
|
|
@ -11,7 +12,7 @@ import { notify } from '../stores/notifications.svelte';
|
|||
|
||||
/** Auto-anchor first N turns on first compaction event for a project */
|
||||
export function triggerAutoAnchor(
|
||||
projectId: string,
|
||||
projectId: ProjectIdType,
|
||||
messages: AgentMessage[],
|
||||
sessionPrompt: string,
|
||||
): void {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
// Session persistence — maps session IDs to projects/providers and persists state to SQLite
|
||||
// Extracted from agent-dispatcher.ts (SRP: persistence concern)
|
||||
|
||||
import type { SessionId as SessionIdType, ProjectId as ProjectIdType } from '../types/ids';
|
||||
import type { ProviderId } from '../providers/types';
|
||||
import { getAgentSession } from '../stores/agents.svelte';
|
||||
import {
|
||||
|
|
@ -11,31 +12,31 @@ import {
|
|||
} from '../adapters/groups-bridge';
|
||||
|
||||
// Map sessionId -> projectId for persistence routing
|
||||
const sessionProjectMap = new Map<string, string>();
|
||||
const sessionProjectMap = new Map<SessionIdType, ProjectIdType>();
|
||||
|
||||
// Map sessionId -> provider for message adapter routing
|
||||
const sessionProviderMap = new Map<string, ProviderId>();
|
||||
const sessionProviderMap = new Map<SessionIdType, ProviderId>();
|
||||
|
||||
// Map sessionId -> start timestamp for metrics
|
||||
const sessionStartTimes = new Map<string, number>();
|
||||
const sessionStartTimes = new Map<SessionIdType, number>();
|
||||
|
||||
// In-flight persistence counter — prevents teardown from racing with async saves
|
||||
let pendingPersistCount = 0;
|
||||
|
||||
export function registerSessionProject(sessionId: string, projectId: string, provider: ProviderId = 'claude'): void {
|
||||
export function registerSessionProject(sessionId: SessionIdType, projectId: ProjectIdType, provider: ProviderId = 'claude'): void {
|
||||
sessionProjectMap.set(sessionId, projectId);
|
||||
sessionProviderMap.set(sessionId, provider);
|
||||
}
|
||||
|
||||
export function getSessionProjectId(sessionId: string): string | undefined {
|
||||
export function getSessionProjectId(sessionId: SessionIdType): ProjectIdType | undefined {
|
||||
return sessionProjectMap.get(sessionId);
|
||||
}
|
||||
|
||||
export function getSessionProvider(sessionId: string): ProviderId {
|
||||
export function getSessionProvider(sessionId: SessionIdType): ProviderId {
|
||||
return sessionProviderMap.get(sessionId) ?? 'claude';
|
||||
}
|
||||
|
||||
export function recordSessionStart(sessionId: string): void {
|
||||
export function recordSessionStart(sessionId: SessionIdType): void {
|
||||
sessionStartTimes.set(sessionId, Date.now());
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +48,7 @@ export async function waitForPendingPersistence(): Promise<void> {
|
|||
}
|
||||
|
||||
/** Persist session state + messages to SQLite for the project that owns this session */
|
||||
export async function persistSessionForProject(sessionId: string): Promise<void> {
|
||||
export async function persistSessionForProject(sessionId: SessionIdType): Promise<void> {
|
||||
const projectId = sessionProjectMap.get(sessionId);
|
||||
if (!projectId) return; // Not a project-scoped session
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue