370 lines
18 KiB
TypeScript
370 lines
18 KiB
TypeScript
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import { NORMALIZED_RELEVANCE_SCORE, type MemorySpaceNativeRunner, type MemoryProviderAdapter, type JsonValue, type Insight, type MemoryBody as MemorySpace, type MemoryBodyStats as MemorySpaceStats, type MemoryGraphEdge, type MemoryGraphNode, type MemoryGraphSnapshot, type MemoryListRequest, type EdgeType, type RememberRequest, type SearchRequest, type ProviderBodyStatus as ProviderSpaceStatus, type ProviderSearchResult } from 'dsh-brain-mnemon-source-memory-spaces/provider-sdk'
|
|
|
|
export const STORE_DUMP_MAX_OUTPUT_BYTES = 128 * 1024 * 1024
|
|
|
|
function record(value: JsonValue | undefined): Record<string, JsonValue> | undefined {
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
? value as Record<string, JsonValue>
|
|
: undefined
|
|
}
|
|
|
|
function text(value: JsonValue | undefined): string | undefined {
|
|
return typeof value === 'string' ? value : undefined
|
|
}
|
|
|
|
function number(value: JsonValue | undefined): number | undefined {
|
|
return typeof value === 'number' && Number.isFinite(value) ? value : undefined
|
|
}
|
|
|
|
function stringArray(value: JsonValue | undefined): string[] | undefined {
|
|
if (!Array.isArray(value)) return undefined
|
|
return value.filter((entry): entry is string => typeof entry === 'string')
|
|
}
|
|
|
|
function normalizeInsight(value: JsonValue): Insight | undefined {
|
|
const item = record(value)
|
|
if (item === undefined) return undefined
|
|
const nested = record(item.insight)
|
|
const core = nested ?? item
|
|
const id = text(core.id)
|
|
const content = text(core.content)
|
|
if (id === undefined || content === undefined) return undefined
|
|
const insight: Insight = { id, content }
|
|
const optionalText = {
|
|
category: text(core.category),
|
|
source: text(core.source),
|
|
confidence: text(item.confidence),
|
|
intent: text(item.intent),
|
|
matchedVia: text(item.matched_via ?? item.via ?? item.via_edge_type),
|
|
createdAt: text(core.created_at),
|
|
edgeType: text(item.via_edge_type),
|
|
}
|
|
for (const [key, value] of Object.entries(optionalText)) if (value !== undefined) Object.assign(insight, { [key]: value })
|
|
const optionalNumbers = {
|
|
importance: number(core.importance),
|
|
score: number(item.score),
|
|
depth: number(item.depth),
|
|
}
|
|
for (const [key, value] of Object.entries(optionalNumbers)) if (value !== undefined) Object.assign(insight, { [key]: value })
|
|
const tags = stringArray(core.tags)
|
|
const entities = stringArray(core.entities)
|
|
if (tags !== undefined) insight.tags = tags
|
|
if (entities !== undefined) insight.entities = entities
|
|
return insight
|
|
}
|
|
|
|
const JS_STRING = '"(?:\\\\.|[^"\\\\])*"'
|
|
const VIZ_NODE_PATTERN = new RegExp(`\\{id:(${JS_STRING}),label:(${JS_STRING}),title:(${JS_STRING}),color:(${JS_STRING}),font:\\{color:"white"\\}\\}`, 'g')
|
|
const VIZ_EDGE_PATTERN = new RegExp(`\\{from:(${JS_STRING}),to:(${JS_STRING}),label:(${JS_STRING}),color:\\{color:(${JS_STRING})\\},arrows:"to"`, 'g')
|
|
const EDGE_COLORS: Record<string, EdgeType> = {
|
|
'#aaaaaa': 'temporal',
|
|
'#3498db': 'semantic',
|
|
'#e74c3c': 'causal',
|
|
'#2ecc71': 'entity',
|
|
}
|
|
|
|
function decodeJsString(value: string): string {
|
|
const decoded = JSON.parse(value) as unknown
|
|
if (typeof decoded !== 'string') throw new Error('Mnemon viz contained an invalid string')
|
|
return decoded
|
|
}
|
|
|
|
export function parseMemoryGraph(html: string, now = new Date()): MemoryGraphSnapshot {
|
|
const nodes: MemoryGraphNode[] = []
|
|
const edges: MemoryGraphEdge[] = []
|
|
for (const match of html.matchAll(VIZ_NODE_PATTERN)) {
|
|
const id = decodeJsString(match[1]!)
|
|
const label = decodeJsString(match[2]!)
|
|
const content = decodeJsString(match[3]!).replaceAll('\\n', '\n')
|
|
const color = decodeJsString(match[4]!)
|
|
const category = /\[([a-z_]+)\]/i.exec(label)?.[1] ?? 'general'
|
|
nodes.push({ id, content, category, color })
|
|
}
|
|
for (const match of html.matchAll(VIZ_EDGE_PATTERN)) {
|
|
const color = decodeJsString(match[4]!)
|
|
const type = EDGE_COLORS[color.toLowerCase()]
|
|
edges.push({
|
|
sourceId: decodeJsString(match[1]!),
|
|
targetId: decodeJsString(match[2]!),
|
|
label: decodeJsString(match[3]!),
|
|
color,
|
|
...(type === undefined ? {} : { type }),
|
|
})
|
|
}
|
|
if (!html.includes('var nodes = new vis.DataSet([')) throw new Error('Mnemon viz returned an unexpected HTML payload')
|
|
return { nodes, edges, generatedAt: now.toISOString() }
|
|
}
|
|
|
|
function commaList(values: string[] | undefined, label: string, limit: number): string | undefined {
|
|
if (values === undefined) return undefined
|
|
const normalized = values.map(value => value.trim()).filter(value => value !== '')
|
|
if (normalized.length > limit) throw new Error(`${label} accepts at most ${limit} values`)
|
|
if (normalized.some(value => value.includes(','))) throw new Error(`${label} values cannot contain commas`)
|
|
return normalized.length === 0 ? undefined : normalized.join(',')
|
|
}
|
|
|
|
export class MnemonNativeProvider implements MemoryProviderAdapter {
|
|
readonly id = 'mnemon-native'
|
|
readonly scoreSemantics = NORMALIZED_RELEVANCE_SCORE
|
|
constructor(private readonly runner: MemorySpaceNativeRunner, private readonly config: { defaultRecallLimit: number } = { defaultRecallLimit: 10 }) {}
|
|
|
|
list(body: MemorySpace, _request: MemoryListRequest, signal?: AbortSignal): Promise<Insight[]> {
|
|
return this.allNativeInsights(body, signal, true)
|
|
}
|
|
|
|
async status(body: MemorySpace, signal?: AbortSignal): Promise<ProviderSpaceStatus> {
|
|
try {
|
|
const raw = await this.runner.runJson(['status'], { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
const status = record(raw)
|
|
if (status === undefined) throw new Error('mnemon status returned an unexpected payload')
|
|
return { healthy: true, stats: this.parseStats(status) }
|
|
} catch (error) {
|
|
return { healthy: false, error: error instanceof Error ? error.message : String(error) }
|
|
}
|
|
}
|
|
|
|
private parseStats(status: Record<string, JsonValue>): MemorySpaceStats {
|
|
const byCategoryRecord = record(status.by_category) ?? {}
|
|
const byCategory: Record<string, number> = {}
|
|
for (const [category, count] of Object.entries(byCategoryRecord)) if (typeof count === 'number') byCategory[category] = count
|
|
const topEntities = Array.isArray(status.top_entities)
|
|
? status.top_entities.flatMap((entry) => {
|
|
const entity = record(entry)
|
|
const name = text(entity?.entity)
|
|
const count = number(entity?.count)
|
|
return name === undefined || count === undefined ? [] : [{ entity: name, count }]
|
|
})
|
|
: []
|
|
return {
|
|
totalInsights: number(status.total_insights) ?? 0,
|
|
deletedInsights: number(status.deleted_insights) ?? 0,
|
|
edgeCount: number(status.edge_count) ?? 0,
|
|
oplogCount: number(status.oplog_count) ?? 0,
|
|
dbSizeBytes: number(status.db_size_bytes) ?? 0,
|
|
byCategory,
|
|
topEntities,
|
|
}
|
|
}
|
|
|
|
async graph(body: MemorySpace, signal?: AbortSignal): Promise<MemoryGraphSnapshot> {
|
|
const [html, insights] = await Promise.all([
|
|
this.runner.runText(['viz', '--format', 'html', '--output', '-'], {
|
|
...(signal === undefined ? {} : { signal }), store: body.id, maxOutputBytes: STORE_DUMP_MAX_OUTPUT_BYTES,
|
|
}),
|
|
this.allNativeInsights(body, signal, true),
|
|
])
|
|
const snapshot = parseMemoryGraph(html)
|
|
const metadata = new Map(insights.map(insight => [insight.id, insight]))
|
|
return {
|
|
...snapshot,
|
|
nodes: snapshot.nodes.map(node => {
|
|
const insight = metadata.get(node.id)
|
|
return insight === undefined
|
|
? node
|
|
: { ...node, ...insight, id: node.id, content: node.content, color: node.color }
|
|
}),
|
|
}
|
|
}
|
|
|
|
private async allNativeInsights(body: MemorySpace, signal?: AbortSignal, readonly = false): Promise<Insight[]> {
|
|
const payload = await this.runner.runJson([
|
|
...(readonly ? ['--readonly'] : []),
|
|
'recall', '', '--basic', '--limit', '100000',
|
|
], { ...(signal === undefined ? {} : { signal }), store: body.id, maxOutputBytes: STORE_DUMP_MAX_OUTPUT_BYTES })
|
|
const values = Array.isArray(payload) ? payload : Array.isArray(record(payload)?.results) ? record(payload)!.results as JsonValue[] : []
|
|
return values.map(normalizeInsight).filter((entry): entry is Insight => entry !== undefined)
|
|
}
|
|
|
|
async metadataSample(body: MemorySpace, limit: number, signal?: AbortSignal): Promise<Insight[]> {
|
|
const payload = await this.runner.runJson([
|
|
'--readonly',
|
|
'recall', '', '--basic', '--limit', String(limit),
|
|
], { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
const wrapper = record(payload)
|
|
const values = Array.isArray(payload) ? payload : Array.isArray(wrapper?.results) ? wrapper.results : []
|
|
return values.map(normalizeInsight).filter((entry): entry is Insight => entry !== undefined)
|
|
}
|
|
|
|
async search(body: MemorySpace, request: SearchRequest, signal?: AbortSignal): Promise<ProviderSearchResult> {
|
|
const mode = request.mode ?? 'smart'
|
|
const semantic = mode === 'smart'
|
|
const limit = request.limit ?? this.config.defaultRecallLimit
|
|
const candidateLimit = semantic ? Math.max(limit, Math.min(50, Math.max(20, limit * 4))) : limit
|
|
const readonly = request.inspect === true ? ['--readonly'] : []
|
|
const args = mode === 'keyword'
|
|
? [...readonly, 'search', request.query, '--limit', String(candidateLimit)]
|
|
: [...readonly, 'recall', request.query, '--limit', String(candidateLimit)]
|
|
if (semantic) args.push('--verbose')
|
|
if (mode === 'basic') args.push('--basic')
|
|
if (mode !== 'keyword') {
|
|
if (request.category !== undefined) args.push('--cat', request.category)
|
|
if (request.source !== undefined) args.push('--source', request.source)
|
|
if (request.intent !== undefined) args.push('--intent', request.intent)
|
|
}
|
|
const options = { ...(signal === undefined ? {} : { signal }), store: body.id }
|
|
let payload: JsonValue
|
|
try {
|
|
payload = await this.runner.runJson(args, options)
|
|
} catch (error) {
|
|
if (!semantic || signal?.aborted || !(error instanceof Error)) throw error
|
|
const prefix = `mnemon ${args.join(' ')} exited `
|
|
const detail = error.message.startsWith(prefix) ? error.message.slice(prefix.length) : error.message
|
|
if (!/^(?:[1-9]\d*:\s*)?(?:Error:\s*)?unknown flag: --verbose[ \t]*(?:\r?\n|$)/u.test(detail)) throw error
|
|
const legacyArgs = [...args]
|
|
legacyArgs.splice(readonly.length + 4, 1)
|
|
payload = await this.runner.runJson(legacyArgs, options)
|
|
}
|
|
const wrapper = record(payload)
|
|
const values = Array.isArray(payload) ? payload : Array.isArray(wrapper?.results) ? wrapper.results : []
|
|
const candidates = values.flatMap(value => {
|
|
const insight = normalizeInsight(value)
|
|
if (insight === undefined) return []
|
|
const item = record(value)
|
|
const similarity = number(record(item?.signals)?.similarity)
|
|
return [{ insight, similarity, superseded: item?.superseded === true }]
|
|
})
|
|
if (semantic && candidates.some(candidate => candidate.similarity !== undefined && candidate.similarity > 0)) {
|
|
for (const candidate of candidates) {
|
|
const { insight, similarity } = candidate
|
|
delete insight.confidence
|
|
if (similarity === undefined) {
|
|
if (!candidate.superseded) delete insight.score
|
|
} else {
|
|
const relevance = Math.min(1, Math.max(0, similarity))
|
|
insight.score = candidate.superseded ? Math.min(relevance, insight.score ?? 0) : relevance
|
|
}
|
|
}
|
|
candidates.sort((left, right) => (right.insight.score ?? -1) - (left.insight.score ?? -1))
|
|
}
|
|
const hint = text(wrapper?.hint) ?? text(record(wrapper?.meta)?.hint)
|
|
return {
|
|
results: candidates.slice(0, limit).map(candidate => candidate.insight),
|
|
...(hint === undefined ? {} : { hint }),
|
|
}
|
|
}
|
|
|
|
async remember(body: MemorySpace, request: RememberRequest, signal?: AbortSignal): Promise<JsonValue> {
|
|
const args = ['remember', request.content, '--cat', request.category ?? 'general', '--imp', String(request.importance ?? 3), '--source', request.source ?? 'user']
|
|
const tags = commaList(request.tags, 'tags', 20)
|
|
const entities = commaList(request.entities, 'entities', 50)
|
|
if (tags !== undefined) args.push('--tags', tags)
|
|
if (entities !== undefined) args.push('--entities', entities)
|
|
return this.runner.runJson(args, { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
}
|
|
|
|
async rememberMany(body: MemorySpace, requests: readonly RememberRequest[], signal?: AbortSignal): Promise<JsonValue[]> {
|
|
if (requests.length === 0) return []
|
|
const existing = new Map((await this.allNativeInsights(body, signal, true)).map(entry => [entry.content, entry]))
|
|
const ordered = new Array<JsonValue>(requests.length)
|
|
const reused = new Set<number>()
|
|
let pending = new Map<string, { request: RememberRequest; indexes: number[] }>()
|
|
const queue = (index: number) => {
|
|
const request = requests[index]!
|
|
const group = pending.get(request.content)
|
|
if (group === undefined) pending.set(request.content, { request, indexes: [index] })
|
|
else group.indexes.push(index)
|
|
}
|
|
for (const [index, request] of requests.entries()) {
|
|
const exact = existing.get(request.content)
|
|
if (exact === undefined) queue(index)
|
|
else {
|
|
ordered[index] = { action: 'skipped', id: exact.id, content: exact.content }
|
|
reused.add(index)
|
|
}
|
|
}
|
|
while (pending.size > 0) {
|
|
const pruned = await this.importExact(body, [...pending.values()], ordered, signal)
|
|
pending = new Map()
|
|
for (const index of reused) {
|
|
const id = record(ordered[index])?.id
|
|
if (typeof id === 'string' && pruned.has(id)) {
|
|
reused.delete(index)
|
|
queue(index)
|
|
}
|
|
}
|
|
}
|
|
return ordered
|
|
}
|
|
|
|
private async importExact(body: MemorySpace, batch: ReadonlyArray<{ request: RememberRequest; indexes: number[] }>, ordered: JsonValue[], signal?: AbortSignal): Promise<Set<string>> {
|
|
const temporary = mkdtempSync(join(tmpdir(), 'dsh-brain-mnemon-runtime-archive-'))
|
|
const draftPath = join(temporary, 'memory-draft.json')
|
|
try {
|
|
writeFileSync(draftPath, JSON.stringify({
|
|
schema_version: '1',
|
|
source: 'dsh-brain-mnemon-runtime-archive',
|
|
insights: batch.map(({ request }) => ({
|
|
content: request.content,
|
|
category: request.category,
|
|
importance: request.importance ?? 3,
|
|
source: request.source,
|
|
...(request.tags === undefined ? {} : { tags: request.tags }),
|
|
...(request.entities === undefined ? {} : { entities: request.entities }),
|
|
})),
|
|
}), { encoding: 'utf8', mode: 0o600 })
|
|
const payload = await this.runner.runJson(['import', draftPath, '--no-diff'], { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
const summary = record(payload)
|
|
const rows = Array.isArray(summary?.results) ? summary.results : undefined
|
|
const errors = number(summary?.errors)
|
|
const imported = number(summary?.imported)
|
|
const updated = number(summary?.updated)
|
|
const skipped = number(summary?.skipped)
|
|
const invalid = () => new Error(`Mnemon runtime archive import returned an invalid or partial result for Memory Space ${body.id}`)
|
|
if (errors !== 0 || imported === undefined || updated === undefined || skipped === undefined || rows === undefined
|
|
|| ![imported, updated, skipped].every(value => Number.isInteger(value) && value >= 0)
|
|
|| imported !== batch.length || updated !== 0 || skipped !== 0 || rows.length !== batch.length) {
|
|
throw invalid()
|
|
}
|
|
const seen = new Set<number>()
|
|
for (const candidate of rows) {
|
|
const row = record(candidate)
|
|
const index = number(row?.index)
|
|
const action = text(row?.action)?.trim().toLocaleLowerCase()
|
|
if (index === undefined || !Number.isInteger(index) || index < 0 || index >= batch.length || seen.has(index)) {
|
|
throw invalid()
|
|
}
|
|
if (row?.content !== batch[index]!.request.content || action !== 'added') {
|
|
throw invalid()
|
|
}
|
|
seen.add(index)
|
|
for (const [offset, originalIndex] of batch[index]!.indexes.entries()) {
|
|
ordered[originalIndex] = offset === 0 ? row : { ...row, action: 'skipped' }
|
|
}
|
|
}
|
|
return new Set(stringArray(summary?.auto_pruned_ids) ?? [])
|
|
} finally {
|
|
rmSync(temporary, { recursive: true, force: true })
|
|
}
|
|
}
|
|
|
|
async get(body: MemorySpace, id: string, signal?: AbortSignal): Promise<Insight | undefined> {
|
|
try {
|
|
return normalizeInsight(await this.runner.runJson(['--readonly', 'show', id], { ...(signal === undefined ? {} : { signal }), store: body.id }))
|
|
} catch (error) {
|
|
if (error instanceof Error && /no rows in result set/u.test(error.message)) return undefined
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async related(body: MemorySpace, id: string, depth: number, edge?: EdgeType, signal?: AbortSignal): Promise<Insight[]> {
|
|
const args = ['related', id, '--depth', String(depth)]
|
|
if (edge !== undefined) args.push('--edge', edge)
|
|
const payload = await this.runner.runJson(args, { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
return Array.isArray(payload) ? payload.map(normalizeInsight).filter((entry): entry is Insight => entry !== undefined) : []
|
|
}
|
|
|
|
async link(body: MemorySpace, sourceId: string, targetId: string, type: EdgeType, weight: number, reason?: string, signal?: AbortSignal): Promise<JsonValue> {
|
|
const args = ['link', sourceId, targetId, '--type', type, '--weight', String(weight)]
|
|
if (reason !== undefined) args.push('--meta', JSON.stringify({ reason }))
|
|
return this.runner.runJson(args, { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
}
|
|
|
|
forget(body: MemorySpace, id: string, signal?: AbortSignal): Promise<JsonValue> {
|
|
return this.runner.runJson(['forget', id], { ...(signal === undefined ? {} : { signal }), store: body.id })
|
|
}
|
|
|
|
}
|