/ src / lib / memory.ts
memory.ts
 1  import { api } from './app/api-client'
 2  import type { MemoryEntry } from '../types'
 3  
 4  interface MemoryQueryOptions {
 5    q?: string
 6    agentId?: string
 7    scope?: 'auto' | 'all' | 'global' | 'shared' | 'agent' | 'session' | 'project'
 8    scopeSessionId?: string
 9    projectRoot?: string
10    rerank?: 'balanced' | 'semantic' | 'lexical'
11    depth?: number
12    limit?: number
13    linkedLimit?: number
14    envelope?: boolean
15  }
16  
17  export const searchMemory = (opts: MemoryQueryOptions = {}) => {
18    const params = new URLSearchParams()
19    if (opts.q) params.set('q', opts.q)
20    if (opts.agentId) params.set('agentId', opts.agentId)
21    if (opts.scope) params.set('scope', opts.scope)
22    if (opts.scopeSessionId) params.set('scopeSessionId', opts.scopeSessionId)
23    if (opts.projectRoot) params.set('projectRoot', opts.projectRoot)
24    if (opts.rerank) params.set('rerank', opts.rerank)
25    if (typeof opts.depth === 'number') params.set('depth', String(opts.depth))
26    if (typeof opts.limit === 'number') params.set('limit', String(opts.limit))
27    if (typeof opts.linkedLimit === 'number') params.set('linkedLimit', String(opts.linkedLimit))
28    if (opts.envelope) params.set('envelope', 'true')
29    const qs = params.toString()
30    return api<MemoryEntry[]>('GET', `/memory${qs ? '?' + qs : ''}`)
31  }
32  
33  export const getMemory = (id: string, opts: Omit<MemoryQueryOptions, 'q' | 'agentId'> = {}) => {
34    const params = new URLSearchParams()
35    if (typeof opts.depth === 'number') params.set('depth', String(opts.depth))
36    if (typeof opts.limit === 'number') params.set('limit', String(opts.limit))
37    if (typeof opts.linkedLimit === 'number') params.set('linkedLimit', String(opts.linkedLimit))
38    if (opts.envelope) params.set('envelope', 'true')
39    const qs = params.toString()
40    return api<MemoryEntry | MemoryEntry[]>('GET', `/memory/${id}${qs ? '?' + qs : ''}`)
41  }
42  
43  export const createMemory = (data: Omit<MemoryEntry, 'id' | 'createdAt' | 'updatedAt'>) =>
44    api<MemoryEntry>('POST', '/memory', data)
45  
46  export const updateMemory = (id: string, data: Partial<MemoryEntry>) =>
47    api<MemoryEntry>('PUT', `/memory/${id}`, data)
48  
49  export const deleteMemory = (id: string) =>
50    api<string>('DELETE', `/memory/${id}`)
51  
52  export const getMemoryCounts = () =>
53    api<Record<string, number>>('GET', '/memory?counts=true')