94 lines
4.9 KiB
TypeScript
94 lines
4.9 KiB
TypeScript
/** Local Mnemon Native provider exposed through the same Mnemon host contract. */
|
|
import type {
|
|
Insight,
|
|
JsonValue,
|
|
MemoryGraphSnapshot,
|
|
MemoryListRequest,
|
|
MemoryProviderAdapter,
|
|
MemorySpace,
|
|
ProviderSearchResult,
|
|
ProviderSpaceStatus,
|
|
RememberRequest,
|
|
SearchRequest,
|
|
} from './fork/provider-sdk.ts'
|
|
import { MEMORY_SPACE_PROVIDER_API_VERSION, defineMemorySpaceProvider, defineMemorySpaceProviderDefinition } from './fork/provider-sdk.ts'
|
|
import { LocalMemoryManager } from '../local-memory/index.ts'
|
|
import { VERSION } from '../version.ts'
|
|
|
|
export const descriptor = {
|
|
id: 'dsh-brain-native',
|
|
label: 'DSH Brain Native',
|
|
icon: { kind: 'glyph' as const, value: '🧠' },
|
|
kind: 'local' as const,
|
|
workspaceBinding: 'optional-override' as const,
|
|
summary: 'Local DSH Brain memory spaces: project and global records stored under .dsh/brain.',
|
|
origin: 'native' as const,
|
|
capabilities: { search: true, browse: true, graph: false, entities: false, related: false, remember: true, link: false, forget: true, writeMode: 'exact' as const, deletionMode: 'soft' as const },
|
|
fields: [],
|
|
serviceConfigured: true,
|
|
}
|
|
|
|
function workspaceOf(body: MemorySpace): string | undefined {
|
|
return body.dbPath !== undefined && body.dbPath !== '' ? body.dbPath : undefined
|
|
}
|
|
function insightOf(record: { id: string; content: string; category?: string; tags: string[]; source: string; createdAt: string; updatedAt: string }, score?: number): Insight {
|
|
return { id: record.id, content: record.content, ...(record.category === undefined ? {} : { category: record.category }), tags: record.tags, source: record.source === 'session' ? 'user' : 'external', createdAt: record.createdAt, ...(score === undefined ? {} : { score }) }
|
|
}
|
|
|
|
export class LocalMnemonProvider implements MemoryProviderAdapter {
|
|
readonly id = descriptor.id
|
|
readonly scoreSemantics = { kind: 'provider-native' as const }
|
|
constructor(private readonly manager: LocalMemoryManager) {}
|
|
async status(body: MemorySpace): Promise<ProviderSpaceStatus> {
|
|
try { await this.manager.withWorkspace(workspaceOf(body), () => this.manager.storageStatus(workspaceOf(body))); return { healthy: true, stats: { totalInsights: 0, deletedInsights: 0, edgeCount: 0, oplogCount: 0, dbSizeBytes: 0, byCategory: {}, topEntities: [] } } }
|
|
catch (error) { return { healthy: false, error: error instanceof Error ? error.message : String(error) } }
|
|
}
|
|
async search(body: MemorySpace, request: SearchRequest): Promise<ProviderSearchResult> {
|
|
const records = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.search(request.query, request.limit ?? 10, workspaceOf(body)))
|
|
return { results: records.map((record) => insightOf(record)) }
|
|
}
|
|
async list(body: MemorySpace, request: MemoryListRequest): Promise<Insight[]> {
|
|
const page = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.list({ limit: request.limit ?? 50, offset: 0 }, workspaceOf(body)))
|
|
return page.items.map((record) => insightOf(record))
|
|
}
|
|
async remember(body: MemorySpace, request: RememberRequest): Promise<JsonValue> {
|
|
const result = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.remember({ content: request.content, category: request.category, tags: request.tags, source: 'mnemon-native', scope: 'project' }))
|
|
return { action: 'stored', id: result.record.id, revision: result.record.revision }
|
|
}
|
|
async get(body: MemorySpace, id: string): Promise<Insight | undefined> {
|
|
const record = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.read(id, workspaceOf(body)))
|
|
return record === undefined ? undefined : insightOf(record)
|
|
}
|
|
async forget(body: MemorySpace, id: string): Promise<JsonValue> {
|
|
const record = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.forget(id, undefined, workspaceOf(body)))
|
|
return { action: 'deleted', id: record.id, revision: record.revision }
|
|
}
|
|
async graph(): Promise<MemoryGraphSnapshot> { return { nodes: [], edges: [], generatedAt: new Date().toISOString() } }
|
|
}
|
|
|
|
export const definition = defineMemorySpaceProviderDefinition({
|
|
manifest: {
|
|
apiVersion: MEMORY_SPACE_PROVIDER_API_VERSION,
|
|
kind: 'provider',
|
|
typeId: descriptor.id,
|
|
packageName: 'dsh-brain',
|
|
version: VERSION,
|
|
label: descriptor.label,
|
|
icon: descriptor.icon,
|
|
summary: descriptor.summary,
|
|
origin: descriptor.origin,
|
|
locality: descriptor.kind,
|
|
workspaceBinding: descriptor.workspaceBinding,
|
|
capabilities: descriptor.capabilities,
|
|
fields: descriptor.fields,
|
|
secrets: [],
|
|
scoreSemantics: 'provider-native',
|
|
},
|
|
create: (context) => new LocalMnemonProvider(context.memorySpaces?.runner ? new LocalMemoryManager({ cwd: () => context.memorySpaces?.runner.effectiveDataDir() ?? process.cwd() }) : new LocalMemoryManager()),
|
|
})
|
|
|
|
export default defineMemorySpaceProvider<undefined>({
|
|
id: descriptor.id,
|
|
apply(ctx, host) { host.install(ctx, definition) },
|
|
})
|