import { randomUUID } from 'node:crypto' import * as providerSdk from 'dsh-brain-mnemon-source-memory-spaces/provider-sdk' import type { JsonValue } from 'dsh-brain-mnemon-source-memory-spaces/provider-sdk' import type { MemorySpaceAuthority } from 'dsh-brain-mnemon-source-memory-spaces/provider-sdk' import type { Insight, MemoryBody as MemorySpace, MemoryGraphSnapshot, MemoryListRequest, MemoryProviderConnection, OpenVikingBodyConnection as OpenVikingSpaceConnection, RememberRequest, SearchRequest, } from 'dsh-brain-mnemon-source-memory-spaces/provider-sdk' import { CATEGORIES, jsonNumber as number, jsonObject as object, jsonString as string, type MemoryProviderAdapter, type ProviderBodyStatus as ProviderSpaceStatus, type ProviderMemorySpace, type ProviderSearchResult } from 'dsh-brain-mnemon-source-memory-spaces/provider-sdk' interface OpenVikingEnvelope { status?: string result?: unknown error?: { code?: string; message?: string; trace_id?: string } trace_id?: string } interface OpenVikingRequestOptions { timeoutMs?: number signal?: AbortSignal | undefined } interface OpenVikingProviderOptions { fetch?: typeof fetch requestTimeoutMs?: number } type OpenVikingConnection = OpenVikingSpaceConnection & { discoveryUser?: string } function normalizeBearerHeader(apiKey: unknown): string | undefined { if (typeof providerSdk.bearerAuthHeader === 'function') return providerSdk.bearerAuthHeader(apiKey) if (typeof apiKey !== 'string') return undefined const stripped = apiKey.trim().replace(/^Bearer(?:\s+|$)/iu, '').trim() return stripped === '' ? undefined : `Bearer ${stripped}` } const CATEGORY_NAMES = new Set(CATEGORIES) function categoryOf(value: unknown): string { const category = string(value)?.trim().toLowerCase() return category !== undefined && CATEGORY_NAMES.has(category) ? category : 'general' } const MEMORY_FOLDERS: Record = { preference: 'preferences', insight: 'experiences', decision: 'experiences', context: 'events', fact: 'entities', general: 'entities', } const SLUG_BYTES = 180 function memoryUri(root: string, category: string, content: string): string { const folder = Object.hasOwn(MEMORY_FOLDERS, category) ? MEMORY_FOLDERS[category] : 'entities' const firstLine = content.split('\n').find(line => line.trim().length > 0) ?? 'memory' const characters = Array.from(firstLine.replace(/^#+\s*/u, '').replace(/[^\p{L}\p{N}]+/gu, '-').replace(/^-+|-+$/gu, '')).slice(0, 48) while (Buffer.byteLength(characters.join('').toLowerCase()) > SLUG_BYTES) characters.pop() const slug = characters.join('').toLowerCase() || 'memory' const stamp = new Date().toISOString().replace(/[-:T]/gu, '').slice(0, 14) return `${root}/${folder}/${stamp}-${slug}_${category}-${randomUUID()}.md` } function safeUser(user: string): boolean { return /^[a-zA-Z0-9_.@-]+$/u.test(user) && user !== '.' && user !== '..' && (user.match(/@/gu)?.length ?? 0) <= 1 } function discoveryUser(connection: MemoryProviderConnection | OpenVikingConnection): string | undefined { const user = String(connection.discoveryUser ?? '').trim() if (user === '') return undefined if (!safeUser(user) || !safeUser(String(connection.account ?? '').trim())) { throw new Error('OpenViking user-key discovery requires an explicit account and a safe discovery user') } if (normalizeBearerHeader(connection.apiKey) === undefined) throw new Error('OpenViking user-key discovery requires a user API key') return user } function isMemoryFile(uri: string, root: string): boolean { if (!uri.startsWith(`${root}/`) || !uri.endsWith('.md') || /[%?#\\\u0000-\u001f\u007f]/u.test(uri)) return false return uri.slice(root.length + 1).split('/').every(part => part !== '' && !part.startsWith('.') && part.trim() === part) } function queueFailed(value: unknown): boolean { return Object.values(object(value) ?? {}).some(entry => { const queue = object(entry) return (number(queue?.error_count) ?? 0) > 0 || (Array.isArray(queue?.errors) && queue.errors.length > 0) }) } const FOLDER_CATEGORIES: Record = { preferences: 'preference', experiences: 'insight', events: 'context', entities: 'fact' } function categoryFromUri(uri: string, reported?: unknown): string { const named = /_([a-z]+)-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.md$/u.exec(uri)?.[1] if (named !== undefined && CATEGORY_NAMES.has(named)) return named const stated = string(reported) if (stated !== undefined && CATEGORY_NAMES.has(stated)) return stated const marker = '/memories/' const folder = (uri.includes(marker) ? uri.slice(uri.indexOf(marker) + marker.length) : '').split('/')[0] ?? '' return Object.hasOwn(FOLDER_CATEGORIES, folder) ? FOLDER_CATEGORIES[folder]! : 'general' } function refused(error: unknown): boolean { const status = (error as { status?: unknown } | undefined)?.status return typeof status === 'number' && status >= 400 && status < 500 && status !== 408 && status !== 409 } export class OpenVikingProvider implements MemoryProviderAdapter { readonly id = 'openviking' as const readonly scoreSemantics = Object.freeze({ kind: 'provider-native' as const }) private readonly requestFetch: typeof fetch private readonly requestTimeoutMs: number constructor(private readonly memorySpaces: MemorySpaceAuthority, options: OpenVikingProviderOptions = {}) { this.requestFetch = options.fetch ?? globalThis.fetch this.requestTimeoutMs = options.requestTimeoutMs ?? 15_000 } async discover(connection: MemoryProviderConnection, signal?: AbortSignal): Promise { signal?.throwIfAborted() let account = String(connection.account ?? '').trim() const selectedUser = discoveryUser(connection) if (selectedUser !== undefined) { const targetUri = await this.validateUserNamespace(connection, selectedUser, signal) return [{ externalId: `${account}:${selectedUser}`, name: selectedUser, description: `OpenViking memory namespace for ${selectedUser}`, connection: { targetUri, user: selectedUser, actorPeerId: 'dsh' }, }] } if (account === '') { const accounts = await this.requestConnection(connection, '/api/v1/admin/accounts', {}, { signal }) const items = Array.isArray(accounts) ? accounts : [] const ids = items.flatMap(value => { const id = string(object(value)?.account_id) ?? string(object(value)?.id) return id === undefined ? [] : [id] }) if (ids.length > 1) throw new Error('OpenViking exposes multiple accounts; configure the account to select one discovery scope') account = ids[0] ?? 'default' } const users = await this.requestConnection({ ...connection, account }, `/api/v1/admin/accounts/${encodeURIComponent(account)}/users?limit=100`, {}, { signal }) const items = Array.isArray(users) ? users : [] return items.flatMap(value => { const item = object(value) const user = string(item?.user_id) ?? string(item?.id) ?? string(item?.name) if (user === undefined || !safeUser(user)) return [] return [{ externalId: `${account}:${user}`, name: string(item?.display_name) ?? string(item?.name) ?? user, description: string(item?.description) ?? string(item?.role) ?? `OpenViking memory namespace for ${user}`, connection: { targetUri: `viking://user/${user}/memories`, user, actorPeerId: 'dsh' }, }] }) } async status(body: MemorySpace, signal?: AbortSignal): Promise { try { const connection = this.connection(body) const selectedUser = discoveryUser(connection) if (selectedUser === undefined) await this.request(body, '/health', {}, { signal, timeoutMs: 5_000 }) else { await this.memoryRoot(body, signal) await this.validateUserNamespace(connection, selectedUser, signal) } return { healthy: true } } catch (error) { return { healthy: false, error: error instanceof Error ? error.message : String(error) } } } async search(body: MemorySpace, request: SearchRequest, signal?: AbortSignal): Promise { const keyword = request.mode === 'keyword' signal?.throwIfAborted() if (keyword && request.query.trim() === '') return { results: [] } const limit = keyword ? Math.min(50, Math.max(1, Math.floor(number(request.limit) ?? 10))) : request.limit const memoryRoot = await this.memoryRoot(body, signal) const result = await this.request(body, keyword ? '/api/v1/search/grep' : '/api/v1/search/find', { method: 'POST', body: JSON.stringify(keyword ? { uri: memoryRoot, pattern: request.query.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&'), case_insensitive: true, node_limit: limit, } : { query: request.query, target_uri: memoryRoot, context_type: ['memory'], limit: request.limit, }), }, { signal }) const root = object(result) if (keyword && !Array.isArray(root?.matches)) throw new Error('OpenViking keyword search did not return matches') const values = keyword ? root?.matches : root?.memories const entries = Array.isArray(values) ? values : [] const seen = new Set() const matches = entries.flatMap((value): Insight[] => { const item = object(value) const uri = string(item?.uri) if (uri === undefined || !isMemoryFile(uri, memoryRoot)) return [] if (keyword && seen.has(uri)) return [] seen.add(uri) const score = keyword ? undefined : number(item?.score) return [{ id: uri, externalUri: uri, content: string(item?.overview) ?? string(item?.abstract) ?? uri, category: categoryFromUri(uri, item?.category), source: 'external', ...(score === undefined ? {} : { score }), }] }).slice(0, limit) const { read, skipped } = await this.readAll(body, matches, signal) return { results: read, ...(skipped === 0 ? {} : { hint: `${skipped} OpenViking ${skipped === 1 ? 'result' : 'results'} could not be read and ${skipped === 1 ? 'was' : 'were'} left out; the server can list a file it no longer holds.` }), } } async graph(body: MemorySpace, signal?: AbortSignal): Promise { const items = await this.list(body, { limit: 200 }, signal) return { nodes: items.map(item => ({ ...item, color: '#5568d9' })), edges: [], generatedAt: new Date().toISOString(), } } async list(body: MemorySpace, request: MemoryListRequest, signal?: AbortSignal): Promise { const memoryRoot = await this.memoryRoot(body, signal) const query = new URLSearchParams({ uri: memoryRoot, recursive: 'true', output: 'original' }) const result = await this.request(body, `/api/v1/fs/ls?${query}`, {}, { signal }) const entries = Array.isArray(result) ? result : [] const limit = Math.min(Math.max(request.limit ?? 200, 1), 1000) const files = entries.flatMap((value): Array<{ item: Record; uri: string }> => { const item = object(value) const uri = string(item?.uri) return item === undefined || uri === undefined || item.isDir === true || !isMemoryFile(uri, memoryRoot) ? [] : [{ item, uri }] }).slice(0, limit) const { read } = await this.readAll(body, files.map(({ item, uri }) => ({ id: uri, item })), signal) return read.map(({ item, id, content }): Insight => { const createdAt = string(item.modTime) return { id, externalUri: id, content, category: categoryFromUri(id), source: 'external', ...(createdAt === undefined ? {} : { createdAt }), } }) } private async readAll(body: MemorySpace, items: readonly T[], signal?: AbortSignal): Promise<{ read: Array; skipped: number }> { const settled = await Promise.allSettled(items.map(async item => ({ ...item, content: await this.readContent(body, item.id, signal) }))) signal?.throwIfAborted() const read = settled.flatMap(outcome => outcome.status === 'fulfilled' ? [outcome.value] : []) const failed = settled.find(outcome => outcome.status === 'rejected') if (read.length === 0 && failed !== undefined) throw failed.reason return { read, skipped: settled.length - read.length } } async remember(body: MemorySpace, request: RememberRequest, signal?: AbortSignal): Promise { const root = await this.memoryRoot(body, signal) const category = categoryOf(request.category) const uri = memoryUri(root, category, request.content) const tags = ['source=mnemon', `category=${category}`] const importance = number(request.importance) if (importance !== undefined) tags.push(`importance=${importance}`) const writtenBytes = Buffer.byteLength(request.content, 'utf8') let written: Record | undefined try { written = object(await this.request(body, '/api/v1/content/write', { method: 'POST', body: JSON.stringify({ uri, content: request.content, mode: 'create', wait: true }), }, { signal })) } catch (error) { if (refused(error)) throw new Error(`OpenViking refused the write at ${uri}; nothing was stored. ${error instanceof Error ? error.message : String(error)}`, { cause: error }) throw new Error(`OpenViking write was not verified at ${uri}; remote content may remain. Inspect this URI before retrying. ${error instanceof Error ? error.message : String(error)}`, { cause: error }) } try { if (written?.uri !== uri || written.content_updated !== true || (number(written.written_bytes) ?? -1) < writtenBytes || written.mode !== 'create' || written.context_type !== 'memory' || written.vector_status !== 'complete' || !['complete', 'skipped'].includes(String(written.semantic_status)) || queueFailed(written.queue_status)) { throw new Error('content API did not confirm this exact memory and completed indexing') } if (await this.readContent(body, uri, signal) !== request.content) throw new Error('stored content does not match the requested text') signal?.throwIfAborted() } catch (error) { throw new Error(`OpenViking write was not verified at ${uri}; remote content may remain. Inspect this URI before retrying. ${error instanceof Error ? error.message : String(error)}`, { cause: error }) } return { action: 'stored', provider: 'openviking', id: uri, uri, externalUri: uri, summary: `OpenViking stored the memory at ${uri} (category ${category}).`, writtenBytes, vectorStatus: 'complete', tagsApplied: await this.tag(body, uri, tags, signal), } } private async tag(body: MemorySpace, uri: string, tags: string[], signal?: AbortSignal): Promise { try { const result = object(await this.request(body, '/api/v1/content/set_tags', { method: 'POST', body: JSON.stringify({ uri, tags, mode: 'replace' }) }, { signal, timeoutMs: Math.min(this.requestTimeoutMs, 5_000), })) return result?.tags_updated === true } catch { return false } } async forget(body: MemorySpace, id: string, signal?: AbortSignal): Promise { const root = await this.memoryRoot(body, signal) const uri = id.trim() if (!isMemoryFile(uri, root)) { throw new Error('OpenViking forget requires an exact non-generated .md memory URI inside this Memory Space') } const query = new URLSearchParams({ uri, recursive: 'false' }) const result = object(await this.request(body, `/api/v1/fs?${query}`, { method: 'DELETE' }, { signal })) ?? {} if (result.uri !== uri) throw new Error('OpenViking did not confirm deletion of the requested memory URI') return { action: 'deleted', provider: this.id, id: uri, uri, ...(number(result.estimated_deleted_count) === undefined ? {} : { estimatedDeletedCount: number(result.estimated_deleted_count)! }), } } private connection(body: MemorySpace): OpenVikingConnection { if ((body.provider.typeId ?? body.provider.id) !== this.id) throw new Error(`OpenViking cannot serve provider ${body.provider.id}`) const connection = this.memorySpaces.providerConnection(body.id, body.provider.id) return { endpoint: String(connection.endpoint ?? ''), targetUri: String(connection.targetUri ?? ''), apiKey: String(connection.apiKey ?? ''), account: String(connection.account ?? ''), user: String(connection.user ?? ''), actorPeerId: String(connection.actorPeerId ?? ''), discoveryUser: String(connection.discoveryUser ?? '') } } private async validateUserNamespace(connection: MemoryProviderConnection | OpenVikingConnection, user: string, signal?: AbortSignal): Promise { const root = `viking://user/${user}/memories` const query = new URLSearchParams({ uri: root, recursive: 'false', output: 'original' }) const result = await this.requestConnection(connection, `/api/v1/fs/ls?${query}`, {}, { signal }) if (!Array.isArray(result) || result.some(item => !string(object(item)?.uri)?.startsWith(`${root}/`))) { throw new Error('OpenViking could not validate the selected memory namespace') } return root } private async memoryRoot(body: MemorySpace, signal?: AbortSignal): Promise { signal?.throwIfAborted() const connection = this.connection(body) const root = connection.targetUri.replace(/\/+$/u, '') const match = /^viking:\/\/user(?:\/([^/]+))?\/memories$/u.exec(root) if (match === null || (match[1] !== undefined && !safeUser(match[1]))) throw new Error('OpenViking memory URI must be a safe viking://user//memories root') const selectedUser = discoveryUser(connection) if (selectedUser !== undefined) { if ((match[1] !== undefined && match[1] !== selectedUser) || (connection.user?.trim() && connection.user.trim() !== selectedUser)) { throw new Error('OpenViking memory owner must match the configured discovery user') } return `viking://user/${selectedUser}/memories` } if (match[1] !== undefined) return root const user = connection.user?.trim() || string(object(await this.request(body, '/api/v1/system/status', {}, { signal }))?.user) || '' if (!safeUser(user)) throw new Error('OpenViking could not resolve the memory owner; configure an explicit user memory URI') return `viking://user/${user}/memories` } private async readContent(body: MemorySpace, uri: string, signal?: AbortSignal): Promise { const result = await this.request(body, `/api/v1/content/read?${new URLSearchParams({ uri })}`, {}, { signal }) if (typeof result !== 'string') throw new Error('OpenViking content read did not return text') return result } private async request(body: MemorySpace, path: string, init: RequestInit = {}, options: OpenVikingRequestOptions = {}): Promise { const connection = this.connection(body) return this.requestConnection(connection, path, init, options) } private async requestConnection(connection: MemoryProviderConnection | OpenVikingConnection, path: string, init: RequestInit = {}, options: OpenVikingRequestOptions = {}): Promise { options.signal?.throwIfAborted() const keyBound = discoveryUser(connection) !== undefined const controller = new AbortController() const relay = () => controller.abort(options.signal?.reason) options.signal?.addEventListener('abort', relay, { once: true }) const timer = setTimeout(() => controller.abort(new Error('OpenViking request timed out')), options.timeoutMs ?? this.requestTimeoutMs) try { const auth = normalizeBearerHeader(connection.apiKey) const response = await this.requestFetch(`${connection.endpoint}${path}`, { ...init, headers: { 'Content-Type': 'application/json', ...(auth === undefined ? {} : { Authorization: auth }), ...(keyBound || connection.account === undefined || connection.account === '' ? {} : { 'X-OpenViking-Account': String(connection.account) }), ...(keyBound || connection.user === undefined || connection.user === '' ? {} : { 'X-OpenViking-User': String(connection.user) }), ...(connection.actorPeerId === undefined || connection.actorPeerId === '' ? {} : { 'X-OpenViking-Actor-Peer': String(connection.actorPeerId) }), ...init.headers, }, signal: controller.signal, }) const envelope = await response.json().catch(() => ({})) as OpenVikingEnvelope if (!response.ok || envelope.status === 'error') { const trace = envelope.error?.trace_id ?? envelope.trace_id throw Object.assign(new Error(`${envelope.error?.message ?? `OpenViking HTTP ${response.status}`}${trace === undefined ? '' : ` (trace ${trace})`}`), { status: response.status }) } return envelope.result ?? envelope } catch (error) { if (controller.signal.aborted && options.signal?.aborted !== true) throw new Error(`OpenViking request timed out after ${options.timeoutMs ?? this.requestTimeoutMs}ms`) throw error } finally { clearTimeout(timer) options.signal?.removeEventListener('abort', relay) } } }