dsh-brain/lib/mnemon/native.js

84 lines
4.3 KiB
JavaScript

import { MEMORY_SPACE_PROVIDER_API_VERSION, defineMemorySpaceProvider, defineMemorySpaceProviderDefinition } from "./fork/provider-sdk.js";
import { LocalMemoryManager } from "../local-memory/index.js";
import { VERSION } from "../version.js";
export const descriptor = {
id: 'dsh-brain-native',
label: 'DSH Brain Native',
icon: { kind: 'glyph', value: '🧠' },
kind: 'local',
workspaceBinding: 'optional-override',
summary: 'Local DSH Brain memory spaces: project and global records stored under .dsh/brain.',
origin: 'native',
capabilities: { search: true, browse: true, graph: false, entities: false, related: false, remember: true, link: false, forget: true, writeMode: 'exact', deletionMode: 'soft' },
fields: [],
serviceConfigured: true,
};
function workspaceOf(body) {
return body.dbPath !== undefined && body.dbPath !== '' ? body.dbPath : undefined;
}
function insightOf(record, score) {
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 {
manager;
id = descriptor.id;
scoreSemantics = { kind: 'provider-native' };
constructor(manager) {
this.manager = manager;
}
async status(body) {
try {
const status = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.storageStatus(workspaceOf(body)));
return { healthy: true, stats: { totalInsights: Number(status.spaces?.project ? 0 : 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, request) {
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, request) {
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, request) {
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, id) {
const record = await this.manager.withWorkspace(workspaceOf(body), () => this.manager.read(id, workspaceOf(body)));
return record === undefined ? undefined : insightOf(record);
}
async forget(body, id) {
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() { 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({
id: descriptor.id,
apply(ctx, host) { host.install(ctx, definition); },
});