dsh-mnemon-provider-openviking 0.5.10 → 0.5.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +40 -40
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { t as descriptor } from "./descriptor-1h1ZrvkP.js";
|
|
2
|
-
import
|
|
2
|
+
import * as providerSdk from "dsh-mnemon-source-memory-spaces/provider-sdk";
|
|
3
|
+
import { CATEGORIES, MEMORY_SPACE_PROVIDER_API_VERSION, defineMemorySpaceProvider, defineMemorySpaceProviderDefinition, jsonNumber, jsonObject, jsonString } from "dsh-mnemon-source-memory-spaces/provider-sdk";
|
|
3
4
|
import { randomUUID } from "node:crypto";
|
|
4
5
|
//#region src/driver.ts
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return
|
|
10
|
-
}
|
|
11
|
-
function number(value) {
|
|
12
|
-
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
6
|
+
function normalizeBearerHeader(apiKey) {
|
|
7
|
+
if (typeof providerSdk.bearerAuthHeader === "function") return providerSdk.bearerAuthHeader(apiKey);
|
|
8
|
+
if (typeof apiKey !== "string") return void 0;
|
|
9
|
+
const stripped = apiKey.trim().replace(/^Bearer(?:\s+|$)/iu, "").trim();
|
|
10
|
+
return stripped === "" ? void 0 : `Bearer ${stripped}`;
|
|
13
11
|
}
|
|
14
12
|
/** The categories Mnemon writes; anything else is stored, and read back, as general. */
|
|
15
13
|
const CATEGORY_NAMES = new Set(CATEGORIES);
|
|
16
14
|
function categoryOf(value) {
|
|
17
|
-
const category =
|
|
15
|
+
const category = jsonString(value)?.trim().toLowerCase();
|
|
18
16
|
return category !== void 0 && CATEGORY_NAMES.has(category) ? category : "general";
|
|
19
17
|
}
|
|
20
18
|
const MEMORY_FOLDERS = {
|
|
@@ -45,7 +43,7 @@ function discoveryUser(connection) {
|
|
|
45
43
|
const user = String(connection.discoveryUser ?? "").trim();
|
|
46
44
|
if (user === "") return void 0;
|
|
47
45
|
if (!safeUser(user) || !safeUser(String(connection.account ?? "").trim())) throw new Error("OpenViking user-key discovery requires an explicit account and a safe discovery user");
|
|
48
|
-
if (
|
|
46
|
+
if (normalizeBearerHeader(connection.apiKey) === void 0) throw new Error("OpenViking user-key discovery requires a user API key");
|
|
49
47
|
return user;
|
|
50
48
|
}
|
|
51
49
|
function isMemoryFile(uri, root) {
|
|
@@ -53,9 +51,9 @@ function isMemoryFile(uri, root) {
|
|
|
53
51
|
return uri.slice(root.length + 1).split("/").every((part) => part !== "" && !part.startsWith(".") && part.trim() === part);
|
|
54
52
|
}
|
|
55
53
|
function queueFailed(value) {
|
|
56
|
-
return Object.values(
|
|
57
|
-
const queue =
|
|
58
|
-
return (
|
|
54
|
+
return Object.values(jsonObject(value) ?? {}).some((entry) => {
|
|
55
|
+
const queue = jsonObject(entry);
|
|
56
|
+
return (jsonNumber(queue?.error_count) ?? 0) > 0 || Array.isArray(queue?.errors) && queue.errors.length > 0;
|
|
59
57
|
});
|
|
60
58
|
}
|
|
61
59
|
/** What a folder holds, for files written before the category was in the name or by OpenViking itself. */
|
|
@@ -69,7 +67,7 @@ const FOLDER_CATEGORIES = {
|
|
|
69
67
|
function categoryFromUri(uri, reported) {
|
|
70
68
|
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];
|
|
71
69
|
if (named !== void 0 && CATEGORY_NAMES.has(named)) return named;
|
|
72
|
-
const stated =
|
|
70
|
+
const stated = jsonString(reported);
|
|
73
71
|
if (stated !== void 0 && CATEGORY_NAMES.has(stated)) return stated;
|
|
74
72
|
const marker = "/memories/";
|
|
75
73
|
const folder = (uri.includes(marker) ? uri.slice(uri.indexOf(marker) + 10) : "").split("/")[0] ?? "";
|
|
@@ -111,7 +109,7 @@ var OpenVikingProvider = class {
|
|
|
111
109
|
if (account === "") {
|
|
112
110
|
const accounts = await this.requestConnection(connection, "/api/v1/admin/accounts", {}, { signal });
|
|
113
111
|
const ids = (Array.isArray(accounts) ? accounts : []).flatMap((value) => {
|
|
114
|
-
const id =
|
|
112
|
+
const id = jsonString(jsonObject(value)?.account_id) ?? jsonString(jsonObject(value)?.id);
|
|
115
113
|
return id === void 0 ? [] : [id];
|
|
116
114
|
});
|
|
117
115
|
if (ids.length > 1) throw new Error("OpenViking exposes multiple accounts; configure the account to select one discovery scope");
|
|
@@ -122,13 +120,13 @@ var OpenVikingProvider = class {
|
|
|
122
120
|
account
|
|
123
121
|
}, `/api/v1/admin/accounts/${encodeURIComponent(account)}/users?limit=100`, {}, { signal });
|
|
124
122
|
return (Array.isArray(users) ? users : []).flatMap((value) => {
|
|
125
|
-
const item =
|
|
126
|
-
const user =
|
|
123
|
+
const item = jsonObject(value);
|
|
124
|
+
const user = jsonString(item?.user_id) ?? jsonString(item?.id) ?? jsonString(item?.name);
|
|
127
125
|
if (user === void 0 || !safeUser(user)) return [];
|
|
128
126
|
return [{
|
|
129
127
|
externalId: `${account}:${user}`,
|
|
130
|
-
name:
|
|
131
|
-
description:
|
|
128
|
+
name: jsonString(item?.display_name) ?? jsonString(item?.name) ?? user,
|
|
129
|
+
description: jsonString(item?.description) ?? jsonString(item?.role) ?? `OpenViking memory namespace for ${user}`,
|
|
132
130
|
connection: {
|
|
133
131
|
targetUri: `viking://user/${user}/memories`,
|
|
134
132
|
user,
|
|
@@ -161,9 +159,9 @@ var OpenVikingProvider = class {
|
|
|
161
159
|
const keyword = request.mode === "keyword";
|
|
162
160
|
signal?.throwIfAborted();
|
|
163
161
|
if (keyword && request.query.trim() === "") return { results: [] };
|
|
164
|
-
const limit = keyword ? Math.min(50, Math.max(1, Math.floor(
|
|
162
|
+
const limit = keyword ? Math.min(50, Math.max(1, Math.floor(jsonNumber(request.limit) ?? 10))) : request.limit;
|
|
165
163
|
const memoryRoot = await this.memoryRoot(body, signal);
|
|
166
|
-
const
|
|
164
|
+
const result = await this.request(body, keyword ? "/api/v1/search/grep" : "/api/v1/search/find", {
|
|
167
165
|
method: "POST",
|
|
168
166
|
body: JSON.stringify(keyword ? {
|
|
169
167
|
uri: memoryRoot,
|
|
@@ -176,22 +174,23 @@ var OpenVikingProvider = class {
|
|
|
176
174
|
context_type: ["memory"],
|
|
177
175
|
limit: request.limit
|
|
178
176
|
})
|
|
179
|
-
}, { signal })
|
|
177
|
+
}, { signal });
|
|
178
|
+
const root = jsonObject(result);
|
|
180
179
|
if (keyword && !Array.isArray(root?.matches)) throw new Error("OpenViking keyword search did not return matches");
|
|
181
180
|
const values = keyword ? root?.matches : root?.memories;
|
|
182
181
|
const entries = Array.isArray(values) ? values : [];
|
|
183
182
|
const seen = /* @__PURE__ */ new Set();
|
|
184
183
|
const matches = entries.flatMap((value) => {
|
|
185
|
-
const item =
|
|
186
|
-
const uri =
|
|
184
|
+
const item = jsonObject(value);
|
|
185
|
+
const uri = jsonString(item?.uri);
|
|
187
186
|
if (uri === void 0 || !isMemoryFile(uri, memoryRoot)) return [];
|
|
188
187
|
if (keyword && seen.has(uri)) return [];
|
|
189
188
|
seen.add(uri);
|
|
190
|
-
const score = keyword ? void 0 :
|
|
189
|
+
const score = keyword ? void 0 : jsonNumber(item?.score);
|
|
191
190
|
return [{
|
|
192
191
|
id: uri,
|
|
193
192
|
externalUri: uri,
|
|
194
|
-
content:
|
|
193
|
+
content: jsonString(item?.overview) ?? jsonString(item?.abstract) ?? uri,
|
|
195
194
|
category: categoryFromUri(uri, item?.category),
|
|
196
195
|
source: "external",
|
|
197
196
|
...score === void 0 ? {} : { score }
|
|
@@ -224,8 +223,8 @@ var OpenVikingProvider = class {
|
|
|
224
223
|
const entries = Array.isArray(result) ? result : [];
|
|
225
224
|
const limit = Math.min(Math.max(request.limit ?? 200, 1), 1e3);
|
|
226
225
|
const files = entries.flatMap((value) => {
|
|
227
|
-
const item =
|
|
228
|
-
const uri =
|
|
226
|
+
const item = jsonObject(value);
|
|
227
|
+
const uri = jsonString(item?.uri);
|
|
229
228
|
return item === void 0 || uri === void 0 || item.isDir === true || !isMemoryFile(uri, memoryRoot) ? [] : [{
|
|
230
229
|
item,
|
|
231
230
|
uri
|
|
@@ -236,7 +235,7 @@ var OpenVikingProvider = class {
|
|
|
236
235
|
item
|
|
237
236
|
})), signal);
|
|
238
237
|
return read.map(({ item, id, content }) => {
|
|
239
|
-
const createdAt =
|
|
238
|
+
const createdAt = jsonString(item.modTime);
|
|
240
239
|
return {
|
|
241
240
|
id,
|
|
242
241
|
externalUri: id,
|
|
@@ -272,12 +271,12 @@ var OpenVikingProvider = class {
|
|
|
272
271
|
const category = categoryOf(request.category);
|
|
273
272
|
const uri = memoryUri(root, category, request.content);
|
|
274
273
|
const tags = ["source=mnemon", `category=${category}`];
|
|
275
|
-
const importance =
|
|
274
|
+
const importance = jsonNumber(request.importance);
|
|
276
275
|
if (importance !== void 0) tags.push(`importance=${importance}`);
|
|
277
276
|
const writtenBytes = Buffer.byteLength(request.content, "utf8");
|
|
278
277
|
let written;
|
|
279
278
|
try {
|
|
280
|
-
written =
|
|
279
|
+
written = jsonObject(await this.request(body, "/api/v1/content/write", {
|
|
281
280
|
method: "POST",
|
|
282
281
|
body: JSON.stringify({
|
|
283
282
|
uri,
|
|
@@ -291,7 +290,7 @@ var OpenVikingProvider = class {
|
|
|
291
290
|
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 });
|
|
292
291
|
}
|
|
293
292
|
try {
|
|
294
|
-
if (written?.uri !== uri || written.content_updated !== true || (
|
|
293
|
+
if (written?.uri !== uri || written.content_updated !== true || (jsonNumber(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");
|
|
295
294
|
if (await this.readContent(body, uri, signal) !== request.content) throw new Error("stored content does not match the requested text");
|
|
296
295
|
signal?.throwIfAborted();
|
|
297
296
|
} catch (error) {
|
|
@@ -317,7 +316,7 @@ var OpenVikingProvider = class {
|
|
|
317
316
|
*/
|
|
318
317
|
async tag(body, uri, tags, signal) {
|
|
319
318
|
try {
|
|
320
|
-
return
|
|
319
|
+
return jsonObject(await this.request(body, "/api/v1/content/set_tags", {
|
|
321
320
|
method: "POST",
|
|
322
321
|
body: JSON.stringify({
|
|
323
322
|
uri,
|
|
@@ -340,14 +339,14 @@ var OpenVikingProvider = class {
|
|
|
340
339
|
uri,
|
|
341
340
|
recursive: "false"
|
|
342
341
|
});
|
|
343
|
-
const result =
|
|
342
|
+
const result = jsonObject(await this.request(body, `/api/v1/fs?${query}`, { method: "DELETE" }, { signal })) ?? {};
|
|
344
343
|
if (result.uri !== uri) throw new Error("OpenViking did not confirm deletion of the requested memory URI");
|
|
345
344
|
return {
|
|
346
345
|
action: "deleted",
|
|
347
346
|
provider: this.id,
|
|
348
347
|
id: uri,
|
|
349
348
|
uri,
|
|
350
|
-
...
|
|
349
|
+
...jsonNumber(result.estimated_deleted_count) === void 0 ? {} : { estimatedDeletedCount: jsonNumber(result.estimated_deleted_count) }
|
|
351
350
|
};
|
|
352
351
|
}
|
|
353
352
|
connection(body) {
|
|
@@ -371,7 +370,7 @@ var OpenVikingProvider = class {
|
|
|
371
370
|
output: "original"
|
|
372
371
|
});
|
|
373
372
|
const result = await this.requestConnection(connection, `/api/v1/fs/ls?${query}`, {}, { signal });
|
|
374
|
-
if (!Array.isArray(result) || result.some((item) => !
|
|
373
|
+
if (!Array.isArray(result) || result.some((item) => !jsonString(jsonObject(item)?.uri)?.startsWith(`${root}/`))) throw new Error("OpenViking could not validate the selected memory namespace");
|
|
375
374
|
return root;
|
|
376
375
|
}
|
|
377
376
|
async memoryRoot(body, signal) {
|
|
@@ -386,7 +385,7 @@ var OpenVikingProvider = class {
|
|
|
386
385
|
return `viking://user/${selectedUser}/memories`;
|
|
387
386
|
}
|
|
388
387
|
if (match[1] !== void 0) return root;
|
|
389
|
-
const user = connection.user?.trim() ||
|
|
388
|
+
const user = connection.user?.trim() || jsonString(jsonObject(await this.request(body, "/api/v1/system/status", {}, { signal }))?.user) || "";
|
|
390
389
|
if (!safeUser(user)) throw new Error("OpenViking could not resolve the memory owner; configure an explicit user memory URI");
|
|
391
390
|
return `viking://user/${user}/memories`;
|
|
392
391
|
}
|
|
@@ -407,11 +406,12 @@ var OpenVikingProvider = class {
|
|
|
407
406
|
options.signal?.addEventListener("abort", relay, { once: true });
|
|
408
407
|
const timer = setTimeout(() => controller.abort(/* @__PURE__ */ new Error("OpenViking request timed out")), options.timeoutMs ?? this.requestTimeoutMs);
|
|
409
408
|
try {
|
|
409
|
+
const auth = normalizeBearerHeader(connection.apiKey);
|
|
410
410
|
const response = await this.requestFetch(`${connection.endpoint}${path}`, {
|
|
411
411
|
...init,
|
|
412
412
|
headers: {
|
|
413
413
|
"Content-Type": "application/json",
|
|
414
|
-
...
|
|
414
|
+
...auth === void 0 ? {} : { Authorization: auth },
|
|
415
415
|
...keyBound || connection.account === void 0 || connection.account === "" ? {} : { "X-OpenViking-Account": String(connection.account) },
|
|
416
416
|
...keyBound || connection.user === void 0 || connection.user === "" ? {} : { "X-OpenViking-User": String(connection.user) },
|
|
417
417
|
...connection.actorPeerId === void 0 || connection.actorPeerId === "" ? {} : { "X-OpenViking-Actor-Peer": String(connection.actorPeerId) },
|
|
@@ -442,7 +442,7 @@ const definition = defineMemorySpaceProviderDefinition({
|
|
|
442
442
|
kind: "provider",
|
|
443
443
|
typeId: descriptor.id,
|
|
444
444
|
packageName: "dsh-mnemon-provider-openviking",
|
|
445
|
-
version: "0.5.
|
|
445
|
+
version: "0.5.11",
|
|
446
446
|
label: descriptor.label,
|
|
447
447
|
icon: descriptor.icon,
|
|
448
448
|
summary: descriptor.summary,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mnemon-provider-openviking",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.11",
|
|
4
4
|
"description": "OpenViking Provider child for the Memory Spaces Source.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^22.0.0",
|
|
37
|
-
"dsh-mnemon-source-memory-spaces": "^0.5.
|
|
37
|
+
"dsh-mnemon-source-memory-spaces": "^0.5.29",
|
|
38
38
|
"tsdown": "^0.22.0",
|
|
39
39
|
"typescript": "^5.9.0",
|
|
40
40
|
"vitest": "^3.2.0"
|