@openviking/opencode-plugin 0.2.2 → 0.2.4
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/INSTALL-ZH.md +3 -2
- package/INSTALL.md +4 -2
- package/README.md +6 -2
- package/index.mjs +1 -1
- package/lib/config.mjs +33 -5
- package/lib/memory-recall.mjs +14 -5
- package/lib/memory-session.mjs +45 -38
- package/lib/runtime.mjs +1 -0
- package/lib/shared/async-writer.mjs +78 -0
- package/lib/shared/batch-send.mjs +121 -0
- package/lib/shared/capture-utils.mjs +1 -2
- package/lib/shared/credentials.mjs +21 -0
- package/lib/shared/mcp-proxy-core.mjs +47 -1
- package/lib/shared/pending-queue.mjs +6 -5
- package/lib/shared/plugin-config.mjs +90 -0
- package/lib/shared/recall-compress-core.mjs +195 -0
- package/lib/shared/recall-core.mjs +323 -16
- package/lib/shared/retryable.mjs +9 -0
- package/lib/utils.mjs +1 -0
- package/lib/viking-uri-guard.mjs +1 -1
- package/package.json +1 -1
- package/servers/mcp-proxy.mjs +1 -0
- package/servers/mcp-proxy.test.mjs +2 -0
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
// GENERATED FROM examples/memory-plugin-shared/lib. DO NOT EDIT.
|
|
2
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
|
|
6
|
+
import { compressRecallContext } from "./recall-compress-core.mjs";
|
|
7
|
+
|
|
2
8
|
const PREFERENCE_QUERY_RE = /prefer|preference|favorite|favourite|like|偏好|喜欢|爱好|更倾向/i;
|
|
3
9
|
const TEMPORAL_QUERY_RE = /when|what time|date|day|month|year|yesterday|today|tomorrow|last|next|什么时候|何时|哪天|几月|几年|昨天|今天|明天/i;
|
|
4
10
|
const QUERY_TOKEN_RE = /[a-z0-9一-龥]{2,}/gi;
|
|
@@ -11,6 +17,17 @@ const SOURCES = [
|
|
|
11
17
|
{ type: "memory", uri: "viking://user/memories", bucket: "memories" },
|
|
12
18
|
{ type: "skill", uri: "viking://user/skills", bucket: "skills" },
|
|
13
19
|
];
|
|
20
|
+
const DEFAULT_CONTEXT_LIMIT = 10;
|
|
21
|
+
const DEFAULT_CONTEXT_MAX_TOKENS = 1600;
|
|
22
|
+
const DEFAULT_REWRITE_MAX_BULLETS = 6;
|
|
23
|
+
const CODING_QUOTA_WEIGHTS = {
|
|
24
|
+
events: 1,
|
|
25
|
+
entities: 2,
|
|
26
|
+
preferences: 1,
|
|
27
|
+
experiences: 1,
|
|
28
|
+
resources: 3,
|
|
29
|
+
skills: 2,
|
|
30
|
+
};
|
|
14
31
|
|
|
15
32
|
let userSpaceCache = "";
|
|
16
33
|
|
|
@@ -18,16 +35,47 @@ export function estimateTokens(text) {
|
|
|
18
35
|
return text ? Math.ceil(String(text).length / 4) : 0;
|
|
19
36
|
}
|
|
20
37
|
|
|
38
|
+
function scaleQuotas(limit, weights) {
|
|
39
|
+
const slots = Math.max(1, Math.floor(Number(limit) || DEFAULT_CONTEXT_LIMIT));
|
|
40
|
+
const order = Object.keys(weights);
|
|
41
|
+
const quotas = Object.fromEntries(order.map((key) => [key, 0]));
|
|
42
|
+
if (slots < order.length) {
|
|
43
|
+
for (const key of order) quotas[key] = 1;
|
|
44
|
+
return quotas;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (const key of order) quotas[key] = 1;
|
|
48
|
+
const totalWeight = Object.values(weights).reduce((sum, weight) => sum + weight, 0);
|
|
49
|
+
const ideals = Object.fromEntries(
|
|
50
|
+
order.map((key) => [key, slots * weights[key] / totalWeight]),
|
|
51
|
+
);
|
|
52
|
+
while (order.reduce((sum, key) => sum + quotas[key], 0) < slots) {
|
|
53
|
+
const key = order.reduce((best, candidate) => (
|
|
54
|
+
ideals[candidate] - quotas[candidate] > ideals[best] - quotas[best]
|
|
55
|
+
? candidate
|
|
56
|
+
: best
|
|
57
|
+
));
|
|
58
|
+
quotas[key] += 1;
|
|
59
|
+
}
|
|
60
|
+
return quotas;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function legacyMemoryQuotas(limit) {
|
|
64
|
+
return {
|
|
65
|
+
...scaleQuotas(limit, { events: 10, entities: 10, preferences: 3 }),
|
|
66
|
+
experiences: 0,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function codingQuotas(limit) {
|
|
71
|
+
return scaleQuotas(limit, CODING_QUOTA_WEIGHTS);
|
|
72
|
+
}
|
|
73
|
+
|
|
21
74
|
export function buildRecallEndpointBody(cfg = {}) {
|
|
22
|
-
const limit = Math.max(Number(cfg.recallLimit ||
|
|
75
|
+
const limit = Math.max(Number(cfg.recallLimit || DEFAULT_CONTEXT_LIMIT), 1);
|
|
23
76
|
const body = {
|
|
24
77
|
query: "",
|
|
25
|
-
quotas:
|
|
26
|
-
events: limit,
|
|
27
|
-
entities: limit,
|
|
28
|
-
preferences: Math.max(1, Math.min(limit, 3)),
|
|
29
|
-
experiences: 0,
|
|
30
|
-
},
|
|
78
|
+
quotas: legacyMemoryQuotas(limit),
|
|
31
79
|
max_chars: Math.max(Number(cfg.recallMaxContentChars || 0) * limit, 1000),
|
|
32
80
|
min_score: Number.isFinite(Number(cfg.scoreThreshold)) ? Number(cfg.scoreThreshold) : 0.35,
|
|
33
81
|
render: true,
|
|
@@ -36,6 +84,106 @@ export function buildRecallEndpointBody(cfg = {}) {
|
|
|
36
84
|
return body;
|
|
37
85
|
}
|
|
38
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Body for the server-side context face. The plugin declares intent (coding
|
|
89
|
+
* purpose, budget, session) and leaves the mechanics — quota ratios, tier
|
|
90
|
+
* degradation, cross-turn dedup — to the server's defaults.
|
|
91
|
+
*/
|
|
92
|
+
export function buildContextSearchBody(cfg = {}, options = {}) {
|
|
93
|
+
const rewriteMode = String(cfg.recallRewrite || "off").toLowerCase();
|
|
94
|
+
const limit = Math.max(1, Math.floor(Number(cfg.recallLimit || DEFAULT_CONTEXT_LIMIT)));
|
|
95
|
+
const maxTokens = Math.max(
|
|
96
|
+
64,
|
|
97
|
+
Math.floor(Number(cfg.recallMaxTokens || DEFAULT_CONTEXT_MAX_TOKENS)),
|
|
98
|
+
);
|
|
99
|
+
const body = {
|
|
100
|
+
query: "",
|
|
101
|
+
mode: "context",
|
|
102
|
+
purpose: "coding",
|
|
103
|
+
score_threshold: Number.isFinite(Number(cfg.scoreThreshold)) ? Number(cfg.scoreThreshold) : 0.35,
|
|
104
|
+
};
|
|
105
|
+
const limitConfigured = cfg.recallLimitConfigured === true;
|
|
106
|
+
const maxTokensConfigured = cfg.recallMaxTokensConfigured === true;
|
|
107
|
+
if (limitConfigured) body.quotas = codingQuotas(limit);
|
|
108
|
+
if (maxTokensConfigured) body.max_tokens = maxTokens;
|
|
109
|
+
if (cfg.recallPeerScope === "actor") body.peer_scope = "actor";
|
|
110
|
+
|
|
111
|
+
const sessionId = String(options.sessionId || "").trim();
|
|
112
|
+
if (sessionId) {
|
|
113
|
+
body.session_id = sessionId;
|
|
114
|
+
const queryExpansionConfigured = cfg.recallQueryExpansionConfigured === true;
|
|
115
|
+
if (queryExpansionConfigured) {
|
|
116
|
+
body.query_expansion = cfg.recallQueryExpansion === "off" ? "off" : "auto";
|
|
117
|
+
}
|
|
118
|
+
const dedupTurns = Number(cfg.recallDedupTurns);
|
|
119
|
+
const resolvedDedupTurns = Number.isFinite(dedupTurns)
|
|
120
|
+
? Math.max(0, Math.floor(dedupTurns))
|
|
121
|
+
: 5;
|
|
122
|
+
if (resolvedDedupTurns > 0) body.dedup_turns = resolvedDedupTurns;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const excludeUris = Array.isArray(options.excludeUris) ? options.excludeUris.slice(0, 200) : [];
|
|
126
|
+
if (excludeUris.length) body.exclude_uris = excludeUris;
|
|
127
|
+
|
|
128
|
+
if (rewriteMode === "server") body.rewrite = true;
|
|
129
|
+
else if (rewriteMode === "auto" && !options.localCompressorAvailable) body.rewrite = "auto";
|
|
130
|
+
const rewriteMaxBullets = Math.max(
|
|
131
|
+
1,
|
|
132
|
+
Math.floor(Number(cfg.recallCompressMaxBullets || DEFAULT_REWRITE_MAX_BULLETS)),
|
|
133
|
+
);
|
|
134
|
+
const rewriteMaxBulletsConfigured = cfg.recallCompressMaxBulletsConfigured === true;
|
|
135
|
+
if (body.rewrite !== undefined && rewriteMaxBulletsConfigured) {
|
|
136
|
+
body.rewrite_max_bullets = rewriteMaxBullets;
|
|
137
|
+
}
|
|
138
|
+
return body;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// The server pipeline is serial and each optional stage has its own fuse. A
|
|
142
|
+
// request is aborted client-side unless its deadline covers every stage it
|
|
143
|
+
// asked for, and aborting discards the whole response rather than just the
|
|
144
|
+
// stage that ran long.
|
|
145
|
+
//
|
|
146
|
+
// session_id -> query expansion (retrieval.recall_intent_timeout_s, 5s)
|
|
147
|
+
// always -> retrieval, body reads, budget planning
|
|
148
|
+
// rewrite -> digest (retrieval.recall_rewrite_timeout_s, 30s)
|
|
149
|
+
//
|
|
150
|
+
// Both budgets stay inside the 60s prompt-hook allowance, the rewrite one with
|
|
151
|
+
// a quarter to spare.
|
|
152
|
+
const EXPANSION_REQUEST_TIMEOUT_MS = 15000;
|
|
153
|
+
const SERVER_REWRITE_REQUEST_TIMEOUT_MS = 45000;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* HTTP deadline for one context request, or undefined to keep the caller's own.
|
|
157
|
+
*
|
|
158
|
+
* Derived from the request body, because the body is what states which server
|
|
159
|
+
* stages will run: reading `cfg` alone cannot tell a bare retrieval from one
|
|
160
|
+
* that also spends the expansion or rewrite fuse.
|
|
161
|
+
*/
|
|
162
|
+
export function contextRequestTimeoutMs(cfg = {}, body = {}) {
|
|
163
|
+
const wantsRewrite = body.rewrite !== undefined;
|
|
164
|
+
// `query_expansion` defaults to "auto" server-side, so only an explicit "off"
|
|
165
|
+
// takes the expansion fuse back out of the budget.
|
|
166
|
+
const wantsExpansion = Boolean(body.session_id) && body.query_expansion !== "off";
|
|
167
|
+
if (!wantsRewrite && !wantsExpansion) return undefined;
|
|
168
|
+
|
|
169
|
+
const configured = Number(cfg.recallContextTimeoutMs);
|
|
170
|
+
if (Number.isFinite(configured) && configured > 0) return Math.max(1000, Math.floor(configured));
|
|
171
|
+
const floor = wantsRewrite ? SERVER_REWRITE_REQUEST_TIMEOUT_MS : EXPANSION_REQUEST_TIMEOUT_MS;
|
|
172
|
+
return Math.max(Number(cfg.timeoutMs) || 0, floor);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Strip the context-face fields a pre-context server rejects, converting the
|
|
177
|
+
* token budget back to v1's character budget.
|
|
178
|
+
*/
|
|
179
|
+
export function downgradeToRecallBody(contextBody = {}, cfg = {}) {
|
|
180
|
+
const body = buildRecallEndpointBody(cfg);
|
|
181
|
+
body.query = contextBody.query || "";
|
|
182
|
+
body.max_chars = Math.max(1000, Math.floor(Number(contextBody.max_tokens || 1600) * 4));
|
|
183
|
+
if (contextBody.peer_scope) body.peer_scope = contextBody.peer_scope;
|
|
184
|
+
return body;
|
|
185
|
+
}
|
|
186
|
+
|
|
39
187
|
function clampScore(v) {
|
|
40
188
|
if (typeof v !== "number" || Number.isNaN(v)) return 0;
|
|
41
189
|
return Math.max(0, Math.min(1, v));
|
|
@@ -232,6 +380,164 @@ async function buildFallbackInjectionBlock(fetchJSON, items, cfg, actorPeerId =
|
|
|
232
380
|
return lines.join("\n");
|
|
233
381
|
}
|
|
234
382
|
|
|
383
|
+
const LEGACY_CACHE_TTL_MS = 6 * 60 * 60 * 1000;
|
|
384
|
+
|
|
385
|
+
function stateFile(name) {
|
|
386
|
+
const override = String(process.env.OPENVIKING_STATE_DIR || "").trim();
|
|
387
|
+
return override ? join(override, name) : join(homedir(), ".openviking", "state", name);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
async function readJsonFile(path) {
|
|
391
|
+
try { return JSON.parse(await readFile(path, "utf8")); } catch { return null; }
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async function writeJsonFile(path, value) {
|
|
395
|
+
try {
|
|
396
|
+
await mkdir(dirname(path), { recursive: true });
|
|
397
|
+
const tmp = `${path}.tmp`;
|
|
398
|
+
await writeFile(tmp, JSON.stringify(value));
|
|
399
|
+
await rename(tmp, path);
|
|
400
|
+
} catch { /* best effort */ }
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Hooks are one-shot processes, so "this server has no context face" has to be
|
|
405
|
+
* remembered on disk or every turn pays for a rejected request.
|
|
406
|
+
*/
|
|
407
|
+
export async function isContextFaceLegacy(path = stateFile("context-face.json"), now = Date.now()) {
|
|
408
|
+
const cached = await readJsonFile(path);
|
|
409
|
+
return Boolean(cached?.legacyUntil && Number(cached.legacyUntil) > now);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
export async function markContextFaceLegacy(path = stateFile("context-face.json"), now = Date.now()) {
|
|
413
|
+
await writeJsonFile(path, { legacyUntil: now + LEGACY_CACHE_TTL_MS });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function looksLikeUnknownField(res) {
|
|
417
|
+
const text = JSON.stringify(res?.error ?? res?.result ?? res?.detail ?? "").toLowerCase();
|
|
418
|
+
return text.includes("extra") || text.includes("mode") || text.includes("unexpected");
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function wrapContext(body) {
|
|
422
|
+
return [
|
|
423
|
+
"<openviking-context>",
|
|
424
|
+
"Relevant memory from OpenViking. Use the recall/read MCP tools to expand URIs.",
|
|
425
|
+
body,
|
|
426
|
+
"</openviking-context>",
|
|
427
|
+
].join("\n");
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Server-assembled context: the context face when the deployment has it, else
|
|
432
|
+
* the deprecated /recall preset. Returns the injection block, "" when there was
|
|
433
|
+
* nothing relevant, or null when no server-side path was usable at all.
|
|
434
|
+
*/
|
|
435
|
+
export async function buildServerAssembledBlock(fetchJSON, cfg, query, options = {}) {
|
|
436
|
+
const actorPeerId = options.actorPeerId ?? cfg.peerId ?? "";
|
|
437
|
+
const log = options.log || (() => {});
|
|
438
|
+
|
|
439
|
+
const block = await recallViaContextFace(fetchJSON, cfg, query, { ...options, actorPeerId }, log);
|
|
440
|
+
if (block !== null) return block;
|
|
441
|
+
return recallViaEndpoint(fetchJSON, cfg, query, actorPeerId, log);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Raw server-assembled context, or null when the deployment has no context face.
|
|
446
|
+
* Returns `{ rendered, entries, digest, stats }` — callers that need the entries
|
|
447
|
+
* (their own compression, their own envelope) use this instead of the block
|
|
448
|
+
* builders below.
|
|
449
|
+
*/
|
|
450
|
+
export async function fetchAssembledContext(fetchJSON, cfg, query, options = {}) {
|
|
451
|
+
const actorPeerId = options.actorPeerId || "";
|
|
452
|
+
const log = options.log || (() => {});
|
|
453
|
+
if (await isContextFaceLegacy(options.legacyCachePath)) return null;
|
|
454
|
+
|
|
455
|
+
const body = buildContextSearchBody(cfg, options);
|
|
456
|
+
body.query = query;
|
|
457
|
+
const res = await fetchJSON("/api/v1/search/search", {
|
|
458
|
+
method: "POST",
|
|
459
|
+
body: JSON.stringify(body),
|
|
460
|
+
}, { actorPeerId, timeoutMs: contextRequestTimeoutMs(cfg, body) });
|
|
461
|
+
|
|
462
|
+
if (!res.ok) {
|
|
463
|
+
const status = res.status || 0;
|
|
464
|
+
if ((status === 400 || status === 422) && looksLikeUnknownField(res)) {
|
|
465
|
+
await markContextFaceLegacy(options.legacyCachePath);
|
|
466
|
+
log("recall_context_face_unsupported", { status });
|
|
467
|
+
} else {
|
|
468
|
+
log("recall_context_face_error", { status });
|
|
469
|
+
}
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const result = res.result || {};
|
|
474
|
+
const stats = result.stats || {};
|
|
475
|
+
log("recall_context_assembled", {
|
|
476
|
+
entries: Array.isArray(result.entries) ? result.entries.length : 0,
|
|
477
|
+
usedTokens: stats.used_tokens || 0,
|
|
478
|
+
tiers: stats.tier_counts || {},
|
|
479
|
+
rewrite: stats.rewrite || "off",
|
|
480
|
+
});
|
|
481
|
+
return {
|
|
482
|
+
rendered: String(result.rendered || "").trim(),
|
|
483
|
+
entries: Array.isArray(result.entries) ? result.entries : [],
|
|
484
|
+
digest: String(result.digest || "").trim(),
|
|
485
|
+
stats,
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Entry field compatibility: the context face returns `category`/`text`, while
|
|
491
|
+
* the deprecated /recall v1 shape used `type` plus `content`/`summary`.
|
|
492
|
+
*/
|
|
493
|
+
export function normalizeContextEntry(entry = {}) {
|
|
494
|
+
return {
|
|
495
|
+
uri: String(entry.uri || "").trim(),
|
|
496
|
+
category: String(entry.category || entry.type || "memory").trim() || "memory",
|
|
497
|
+
detail: String(entry.detail || entry.mode || "").trim(),
|
|
498
|
+
score: Number(entry.score) || 0,
|
|
499
|
+
text: String(
|
|
500
|
+
entry.text || entry.content || entry.summary || entry.abstract || entry.uri || "",
|
|
501
|
+
).trim(),
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
async function recallViaContextFace(fetchJSON, cfg, query, options, log) {
|
|
506
|
+
const assembled = await fetchAssembledContext(fetchJSON, cfg, query, { ...options, log });
|
|
507
|
+
if (assembled === null) return null;
|
|
508
|
+
|
|
509
|
+
const { rendered, entries } = assembled;
|
|
510
|
+
let digest = assembled.digest;
|
|
511
|
+
const mode = String(cfg.recallRewrite || "off").toLowerCase();
|
|
512
|
+
if (String(assembled.stats?.rewrite || "").toLowerCase() === "no_relevant") {
|
|
513
|
+
log("recall_server_compression", { status: "empty" });
|
|
514
|
+
return "";
|
|
515
|
+
}
|
|
516
|
+
const wantsLocal = mode === "client" || (mode === "auto" && !digest);
|
|
517
|
+
if (wantsLocal && rendered && typeof options.runCompressor === "function") {
|
|
518
|
+
try {
|
|
519
|
+
const compression = await compressRecallContext({
|
|
520
|
+
query,
|
|
521
|
+
rendered,
|
|
522
|
+
entries,
|
|
523
|
+
cfg,
|
|
524
|
+
runCompressor: options.runCompressor,
|
|
525
|
+
cachePath: options.digestCachePath || stateFile("recall-digest.json"),
|
|
526
|
+
now: Date.now(),
|
|
527
|
+
});
|
|
528
|
+
log("recall_local_compression", { status: compression.status });
|
|
529
|
+
if (compression.status === "ok") digest = compression.context;
|
|
530
|
+
if (compression.status === "empty") return "";
|
|
531
|
+
} catch (err) {
|
|
532
|
+
log("recall_local_compression_failed", { error: String(err?.message || err) });
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const injected = digest || rendered;
|
|
537
|
+
if (!injected) return "";
|
|
538
|
+
return wrapContext(injected);
|
|
539
|
+
}
|
|
540
|
+
|
|
235
541
|
async function recallViaEndpoint(fetchJSON, cfg, query, actorPeerId = "", log = () => {}) {
|
|
236
542
|
const body = buildRecallEndpointBody(cfg);
|
|
237
543
|
body.query = query;
|
|
@@ -242,12 +548,7 @@ async function recallViaEndpoint(fetchJSON, cfg, query, actorPeerId = "", log =
|
|
|
242
548
|
}
|
|
243
549
|
const rendered = String(res.result?.rendered || "").trim();
|
|
244
550
|
if (!rendered) return "";
|
|
245
|
-
return
|
|
246
|
-
"<openviking-context>",
|
|
247
|
-
"Relevant memory from OpenViking. Use the recall/read MCP tools to expand URIs.",
|
|
248
|
-
rendered,
|
|
249
|
-
"</openviking-context>",
|
|
250
|
-
].join("\n");
|
|
551
|
+
return wrapContext(rendered);
|
|
251
552
|
}
|
|
252
553
|
|
|
253
554
|
export async function postRecall(fetchJSON, body, opts = {}) {
|
|
@@ -277,10 +578,16 @@ export async function buildRecallBlock(fetchJSON, cfg, query, options = {}) {
|
|
|
277
578
|
const trimmed = String(query || "").trim();
|
|
278
579
|
if (!trimmed) return null;
|
|
279
580
|
|
|
280
|
-
|
|
281
|
-
|
|
581
|
+
// Assembly happens server-side when the deployment offers the context face;
|
|
582
|
+
// older servers fall through to /recall, then to raw find.
|
|
583
|
+
const serverBlock = await buildServerAssembledBlock(fetchJSON, cfg, trimmed, {
|
|
584
|
+
...options,
|
|
585
|
+
actorPeerId,
|
|
586
|
+
log,
|
|
587
|
+
});
|
|
588
|
+
if (serverBlock !== null) return serverBlock || null;
|
|
282
589
|
|
|
283
|
-
const recallLimit = Math.max(1, Number(cfg.recallLimit ||
|
|
590
|
+
const recallLimit = Math.max(1, Number(cfg.recallLimit || DEFAULT_CONTEXT_LIMIT));
|
|
284
591
|
const perSourceLimit = Math.max(recallLimit * 2, 8);
|
|
285
592
|
const raw = await searchAllSources(fetchJSON, trimmed, perSourceLimit, actorPeerId, log);
|
|
286
593
|
if (raw.length === 0) return null;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// GENERATED FROM examples/memory-plugin-shared/lib. DO NOT EDIT.
|
|
2
|
+
export function isRetryableFailure(result) {
|
|
3
|
+
if (!result || result.ok) return false;
|
|
4
|
+
const status = Number(result.status || 0);
|
|
5
|
+
if (!status || status === 408 || status === 429 || status >= 500) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
return status === 409 && result.error?.details?.retryable === true;
|
|
9
|
+
}
|
package/lib/utils.mjs
CHANGED
|
@@ -213,6 +213,7 @@ function makeAuthHeaders(config, headers = {}, actorPeerId = "") {
|
|
|
213
213
|
if (config.user) result["X-OpenViking-User"] = config.user
|
|
214
214
|
const peerId = String(actorPeerId || "").trim()
|
|
215
215
|
if (peerId) result["X-OpenViking-Actor-Peer"] = peerId
|
|
216
|
+
if (config.userAgent) result["User-Agent"] = config.userAgent
|
|
216
217
|
return result
|
|
217
218
|
}
|
|
218
219
|
|
package/lib/viking-uri-guard.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { buildGuardMessage, findVikingUri, normalizeToolName } from "./shared/ur
|
|
|
4
4
|
const FILESYSTEM_TOOL_HINTS = {
|
|
5
5
|
read: {
|
|
6
6
|
tool: "openviking_read",
|
|
7
|
-
example: (uri) => `openviking_read(
|
|
7
|
+
example: (uri) => `openviking_read(uris=["${uri}"])`,
|
|
8
8
|
},
|
|
9
9
|
glob: {
|
|
10
10
|
tool: "openviking_glob",
|
package/package.json
CHANGED
package/servers/mcp-proxy.mjs
CHANGED
|
@@ -46,6 +46,7 @@ function readProxyConfig() {
|
|
|
46
46
|
account: cfg.account || "",
|
|
47
47
|
user: cfg.user || "",
|
|
48
48
|
peerId: effectivePeer.peerId,
|
|
49
|
+
userAgent: cfg.userAgent || "",
|
|
49
50
|
timeoutMs: Math.max(1000, Number(cfg.timeoutMs) || DEFAULT_TIMEOUT_MS),
|
|
50
51
|
debug: cfg.debug === true,
|
|
51
52
|
debugLogPath: cfg.debugLogPath,
|
|
@@ -56,6 +56,7 @@ function makeProxy(url) {
|
|
|
56
56
|
account: "acct",
|
|
57
57
|
user: "user",
|
|
58
58
|
peerId: "peer",
|
|
59
|
+
userAgent: "openviking-memory-opencode/9.9.9",
|
|
59
60
|
timeoutMs: 5000,
|
|
60
61
|
debug: false,
|
|
61
62
|
debugLogPath: "",
|
|
@@ -81,6 +82,7 @@ test("OpenCode MCP proxy forwards initialize with auth and identity headers", as
|
|
|
81
82
|
assert.equal(entry.headers["x-openviking-account"], "acct")
|
|
82
83
|
assert.equal(entry.headers["x-openviking-user"], "user")
|
|
83
84
|
assert.equal(entry.headers["x-openviking-actor-peer"], "peer")
|
|
85
|
+
assert.equal(entry.headers["user-agent"], "openviking-memory-opencode/9.9.9")
|
|
84
86
|
res.writeHead(200, {
|
|
85
87
|
"content-type": "text/event-stream",
|
|
86
88
|
"mcp-session-id": "sid-oc",
|