Clean up and enhance memory plugin (#151)

* refactor storage to use worker-based vector operations

* update memory service and agent implementations

* update opencode plugin sdk

* update memory plugin documentation

* add tests for memory service and injection hook

* update lockfile for dependency changes

* Add memory plugin v0.0.13 with message deduplication and config UI

* Remove planning state feature from memory plugin
This commit is contained in:
Chris Scott authored and GitHub committed 2026-03-06 20:24:52 -05:00
1 parent 47ef830cbb
commit 6ef68774d8
53 files changed
+1683 -1885

No files matched your search

@@ -18,7 +18,7 @@ export class EmbeddingSyncService {
private batchSize: number
private maxRetries: number
private syncInProgress: boolean = false
private static readonly MAX_ITERATIONS = 100
private static readonly MAX_ITERATIONS = 1000
constructor(memoryService: MemoryService, logger?: Logger, options: EmbeddingSyncOptions = {}) {
this.memoryService = memoryService
@@ -44,12 +44,12 @@ export class EmbeddingSyncService {
let failed = 0
try {
let pending = this.memoryService.countMemoriesWithoutEmbeddings()
let pending = await this.memoryService.countMemoriesWithoutEmbeddings()
let iterations = 0
while (pending > 0 && iterations < EmbeddingSyncService.MAX_ITERATIONS) {
iterations++
const memories = this.memoryService.getMemoriesWithoutEmbeddings(undefined, this.batchSize)
const memories = await this.memoryService.getMemoriesWithoutEmbeddings(undefined, this.batchSize)
let batchSuccess = 0
let batchFailed = 0
@@ -83,7 +83,7 @@ export class EmbeddingSyncService {
break
}
pending = this.memoryService.countMemoriesWithoutEmbeddings()
pending = await this.memoryService.countMemoriesWithoutEmbeddings()
if (pending > 0) {
this.logger?.log(`[embedding-sync] Progress: ${embedded} embedded, ${failed} failed, ${pending} remaining`)
@@ -110,12 +110,12 @@ export class EmbeddingSyncService {
let failed = 0
try {
let pending = this.memoryService.countMemoriesWithoutEmbeddings(projectId)
let pending = await this.memoryService.countMemoriesWithoutEmbeddings(projectId)
let iterations = 0
while (pending > 0 && iterations < EmbeddingSyncService.MAX_ITERATIONS) {
iterations++
const memories = this.memoryService.getMemoriesWithoutEmbeddings(projectId, this.batchSize)
const memories = await this.memoryService.getMemoriesWithoutEmbeddings(projectId, this.batchSize)
let batchSuccess = 0
let batchFailed = 0
@@ -148,7 +148,7 @@ export class EmbeddingSyncService {
break
}
pending = this.memoryService.countMemoriesWithoutEmbeddings(projectId)
pending = await this.memoryService.countMemoriesWithoutEmbeddings(projectId)
}
if (iterations >= EmbeddingSyncService.MAX_ITERATIONS) {
+3 -2
View File
@@ -169,6 +169,7 @@ export class MemoryService {
}
): Promise<MemorySearchResult[]> {
const embedding = await this.embeddingService.embedText(query)
this.logger?.log(`memory-search: embedding generated (dimensions=${embedding.length}, hasSignal=${embedding.some(v => v !== 0)})`)
const results = await this.queries.search(embedding, projectId, filters)
results.forEach((r) => this.queries.trackAccess(r.memory.id))
return results
@@ -197,11 +198,11 @@ export class MemoryService {
return this.queries.countByProject(projectId)
}
getMemoriesWithoutEmbeddings(projectId?: string, limit: number = 50): Memory[] {
async getMemoriesWithoutEmbeddings(projectId?: string, limit: number = 50): Promise<Memory[]> {
return this.queries.getMemoriesWithoutEmbeddings(projectId, limit)
}
countMemoriesWithoutEmbeddings(projectId?: string): number {
async countMemoriesWithoutEmbeddings(projectId?: string): Promise<number> {
return this.queries.countMemoriesWithoutEmbeddings(projectId)
}
@@ -1,137 +0,0 @@
import type { Database } from 'bun:sqlite'
import { createSessionStateQueries } from '../storage/session-state-queries'
import type { SessionState, PlanningState, PreCompactionSnapshot } from '../types'
const DEFAULT_SESSION_TTL = 7 * 24 * 60 * 60 * 1000
const DEFAULT_SNAPSHOT_TTL = 24 * 60 * 60 * 1000
export interface PlanningStateResult {
sessionId: string
planningState: PlanningState
updatedAt: number
}
export class SessionStateService {
private queries: ReturnType<typeof createSessionStateQueries>
private cleanupInterval: ReturnType<typeof setInterval> | null = null
private logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void }
constructor(db: Database, logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void }) {
this.queries = createSessionStateQueries(db)
this.logger = logger
}
get<T>(key: string): T | null {
const row = this.queries.get(key)
if (!row) return null
try {
return JSON.parse(row.data) as T
} catch {
return null
}
}
set<T>(key: string, projectId: string, data: T, ttlMs?: number): void {
const expiresAt = ttlMs ? Date.now() + ttlMs : null
const jsonData = JSON.stringify(data)
this.queries.set(key, projectId, jsonData, expiresAt)
}
delete(key: string): void {
this.queries.delete(key)
}
deleteByPrefix(prefix: string): void {
this.queries.deleteByPrefix(prefix)
}
deleteExpired(): number {
return this.queries.deleteExpired()
}
listByProject(projectId: string): SessionState[] {
const rows = this.queries.listByProject(projectId)
return rows.map((row): SessionState => ({
key: row.key,
projectId: row.project_id,
data: JSON.parse(row.data),
expiresAt: row.expires_at,
createdAt: row.created_at,
updatedAt: row.updated_at,
}))
}
startCleanupInterval(intervalMs: number = 30 * 60 * 1000): void {
if (this.cleanupInterval) return
this.cleanupInterval = setInterval(() => {
try {
const deleted = this.deleteExpired()
if (deleted > 0 && this.logger) {
this.logger.log(`Cleaned up ${deleted} expired session state entries`)
}
} catch (error) {
if (this.logger) {
this.logger.error('Failed to clean up expired session state', error)
}
}
}, intervalMs)
}
stopCleanupInterval(): void {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval)
this.cleanupInterval = null
}
}
destroy(): void {
this.stopCleanupInterval()
}
setPlanningState(sessionId: string, projectId: string, planningState: PlanningState): void {
const key = `session:${projectId}:${sessionId}`
this.set(key, projectId, planningState, DEFAULT_SESSION_TTL)
}
getPlanningState(sessionId: string, projectId: string): PlanningState | null {
const key = `session:${projectId}:${sessionId}`
return this.get<PlanningState>(key)
}
listPlanningStates(projectId: string): PlanningStateResult[] {
const prefix = `session:${projectId}:`
const rows = this.queries.listByProjectAndPrefix(projectId, prefix)
return rows.map(row => ({
sessionId: row.key.slice(prefix.length),
planningState: JSON.parse(row.data) as PlanningState,
updatedAt: row.updated_at,
}))
}
searchPlanningStates(projectId: string, query: string): PlanningStateResult[] {
const prefix = `session:${projectId}:`
const rows = this.queries.searchByProjectAndPrefix(projectId, prefix, query)
return rows.map(row => ({
sessionId: row.key.slice(prefix.length),
planningState: JSON.parse(row.data) as PlanningState,
updatedAt: row.updated_at,
}))
}
setCompactionSnapshot(sessionId: string, projectId: string, snapshot: PreCompactionSnapshot): void {
const key = `compaction:snapshot:${projectId}:${sessionId}`
this.set(key, projectId, snapshot, DEFAULT_SNAPSHOT_TTL)
}
getCompactionSnapshot(sessionId: string, projectId: string): PreCompactionSnapshot | null {
const key = `compaction:snapshot:${projectId}:${sessionId}`
return this.get<PreCompactionSnapshot>(key)
}
}
export function createSessionStateService(
db: Database,
logger?: { log: (msg: string, ...args: unknown[]) => void; error: (msg: string, ...args: unknown[]) => void }
): SessionStateService {
return new SessionStateService(db, logger)
}