@openviking/openclaw-plugin 2026.9.1 → 2026.9.4-1
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-AGENT.md +25 -4
- package/INSTALL-ZH.md +22 -4
- package/INSTALL.md +22 -4
- package/README.md +19 -15
- package/auto-recall.ts +126 -135
- package/client.ts +95 -0
- package/commands/setup.ts +22 -8
- package/config.ts +31 -16
- package/context-engine.ts +2 -2
- package/dist/auto-recall.js +113 -114
- package/dist/client.js +54 -0
- package/dist/commands/setup.js +15 -6
- package/dist/config.js +18 -11
- package/dist/plugin/openviking-memory-tools.js +1 -1
- package/dist/plugin/openviking-session-routing-runtime.js +2 -2
- package/dist/routing/identity-routing.js +5 -5
- package/dist/services/context-lifecycle-service.js +4 -4
- package/dist/shared/recall-compress-core.mjs +188 -0
- package/dist/shared/recall-core.mjs +645 -0
- package/install-manifest.json +1 -0
- package/openclaw.plugin.json +9 -8
- package/package.json +2 -1
- package/plugin/openviking-client-runtime.ts +1 -1
- package/plugin/openviking-memory-tools.ts +1 -1
- package/plugin/openviking-session-routing-runtime.ts +2 -2
- package/routing/identity-routing.ts +8 -8
- package/services/context-lifecycle-service.ts +4 -4
- package/services/setup/setup-flow.ts +8 -6
- package/shared/recall-compress-core.mjs +195 -0
- package/shared/recall-core.d.mts +39 -0
- package/shared/recall-core.mjs +678 -0
- package/skills/install-openviking-memory/SKILL.md +33 -10
- package/skills/openviking-context-database/SKILL.md +2 -2
|
@@ -0,0 +1,678 @@
|
|
|
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
|
+
|
|
8
|
+
const PREFERENCE_QUERY_RE = /prefer|preference|favorite|favourite|like|偏好|喜欢|爱好|更倾向/i;
|
|
9
|
+
const TEMPORAL_QUERY_RE = /when|what time|date|day|month|year|yesterday|today|tomorrow|last|next|什么时候|何时|哪天|几月|几年|昨天|今天|明天/i;
|
|
10
|
+
const QUERY_TOKEN_RE = /[a-z0-9一-龥]{2,}/gi;
|
|
11
|
+
const STOPWORDS = new Set([
|
|
12
|
+
"what", "when", "where", "which", "who", "whom", "whose", "why", "how", "did", "does",
|
|
13
|
+
"is", "are", "was", "were", "the", "and", "for", "with", "from", "that", "this", "your", "you",
|
|
14
|
+
]);
|
|
15
|
+
const USER_RESERVED_DIRS = new Set(["memories", "skills"]);
|
|
16
|
+
const SOURCES = [
|
|
17
|
+
{ type: "memory", uri: "viking://~/memories", bucket: "memories" },
|
|
18
|
+
{ type: "skill", uri: "viking://~/skills", bucket: "skills" },
|
|
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
|
+
};
|
|
31
|
+
|
|
32
|
+
let userSpaceCache = "";
|
|
33
|
+
|
|
34
|
+
export function estimateTokens(text) {
|
|
35
|
+
return text ? Math.ceil(String(text).length / 4) : 0;
|
|
36
|
+
}
|
|
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
|
+
|
|
74
|
+
export function buildRecallEndpointBody(cfg = {}) {
|
|
75
|
+
const limit = Math.max(Number(cfg.recallLimit || DEFAULT_CONTEXT_LIMIT), 1);
|
|
76
|
+
const body = {
|
|
77
|
+
query: "",
|
|
78
|
+
quotas: legacyMemoryQuotas(limit),
|
|
79
|
+
max_chars: Math.max(Number(cfg.recallMaxContentChars || 0) * limit, 1000),
|
|
80
|
+
min_score: Number.isFinite(Number(cfg.scoreThreshold)) ? Number(cfg.scoreThreshold) : 0.35,
|
|
81
|
+
render: true,
|
|
82
|
+
};
|
|
83
|
+
if (cfg.recallPeerScope === "actor") body.peer_scope = "actor";
|
|
84
|
+
return body;
|
|
85
|
+
}
|
|
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
|
+
const configured = Number(cfg.recallContextTimeoutMs);
|
|
168
|
+
if (Number.isFinite(configured) && configured > 0) return Math.max(1000, Math.floor(configured));
|
|
169
|
+
if (!wantsRewrite && !wantsExpansion) return undefined;
|
|
170
|
+
const floor = wantsRewrite ? SERVER_REWRITE_REQUEST_TIMEOUT_MS : EXPANSION_REQUEST_TIMEOUT_MS;
|
|
171
|
+
return Math.max(Number(cfg.timeoutMs) || 0, floor);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Strip the context-face fields a pre-context server rejects, converting the
|
|
176
|
+
* token budget back to v1's character budget.
|
|
177
|
+
*/
|
|
178
|
+
export function downgradeToRecallBody(contextBody = {}, cfg = {}) {
|
|
179
|
+
const body = buildRecallEndpointBody(cfg);
|
|
180
|
+
body.query = contextBody.query || "";
|
|
181
|
+
body.max_chars = Math.max(1000, Math.floor(Number(contextBody.max_tokens || 1600) * 4));
|
|
182
|
+
if (contextBody.peer_scope) body.peer_scope = contextBody.peer_scope;
|
|
183
|
+
return body;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function clampScore(v) {
|
|
187
|
+
if (typeof v !== "number" || Number.isNaN(v)) return 0;
|
|
188
|
+
return Math.max(0, Math.min(1, v));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function buildQueryProfile(query) {
|
|
192
|
+
const text = query.trim();
|
|
193
|
+
const allTokens = text.toLowerCase().match(QUERY_TOKEN_RE) || [];
|
|
194
|
+
return {
|
|
195
|
+
tokens: allTokens.filter((t) => !STOPWORDS.has(t)),
|
|
196
|
+
wantsPreference: PREFERENCE_QUERY_RE.test(text),
|
|
197
|
+
wantsTemporal: TEMPORAL_QUERY_RE.test(text),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function lexicalOverlapBoost(tokens, text) {
|
|
202
|
+
if (tokens.length === 0 || !text) return 0;
|
|
203
|
+
const haystack = ` ${text.toLowerCase()} `;
|
|
204
|
+
let matched = 0;
|
|
205
|
+
for (const token of tokens.slice(0, 8)) {
|
|
206
|
+
if (haystack.includes(token)) matched += 1;
|
|
207
|
+
}
|
|
208
|
+
return Math.min(0.2, (matched / Math.min(tokens.length, 4)) * 0.2);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function rankItem(item, profile) {
|
|
212
|
+
const base = clampScore(item.score);
|
|
213
|
+
const abstract = (item.abstract || item.overview || "").trim();
|
|
214
|
+
const cat = (item.category || "").toLowerCase();
|
|
215
|
+
const uri = (item.uri || "").toLowerCase();
|
|
216
|
+
const leafBoost = (item.level === 2 || uri.endsWith(".md")) ? 0.12 : 0;
|
|
217
|
+
const eventBoost = profile.wantsTemporal && (cat === "events" || uri.includes("/events/")) ? 0.1 : 0;
|
|
218
|
+
const prefBoost = profile.wantsPreference && (cat === "preferences" || uri.includes("/preferences/")) ? 0.08 : 0;
|
|
219
|
+
const overlapBoost = lexicalOverlapBoost(profile.tokens, `${item.uri} ${abstract}`);
|
|
220
|
+
return base + leafBoost + eventBoost + prefBoost + overlapBoost;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function isEventOrCaseItem(item) {
|
|
224
|
+
const cat = (item.category || "").toLowerCase();
|
|
225
|
+
const uri = (item.uri || "").toLowerCase();
|
|
226
|
+
return cat === "events" || cat === "cases" || uri.includes("/events/") || uri.includes("/cases/");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function dedupeItems(items) {
|
|
230
|
+
const seen = new Set();
|
|
231
|
+
const out = [];
|
|
232
|
+
for (const item of items) {
|
|
233
|
+
const key = isEventOrCaseItem(item)
|
|
234
|
+
? `uri:${item.uri}`
|
|
235
|
+
: ((item.abstract || item.overview || "").trim().toLowerCase() || `uri:${item.uri}`);
|
|
236
|
+
if (seen.has(key)) continue;
|
|
237
|
+
seen.add(key);
|
|
238
|
+
out.push(item);
|
|
239
|
+
}
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async function resolveUserSpace(fetchJSON, actorPeerId = "") {
|
|
244
|
+
if (userSpaceCache) return userSpaceCache;
|
|
245
|
+
|
|
246
|
+
let fallbackSpace = "default";
|
|
247
|
+
const status = await fetchJSON("/api/v1/system/status");
|
|
248
|
+
if (status.ok && typeof status.result?.user === "string" && status.result.user.trim()) {
|
|
249
|
+
fallbackSpace = status.result.user.trim();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const lsRes = await fetchJSON(
|
|
253
|
+
`/api/v1/fs/ls?uri=${encodeURIComponent("viking://user")}&output=original`,
|
|
254
|
+
{},
|
|
255
|
+
{ actorPeerId },
|
|
256
|
+
);
|
|
257
|
+
if (lsRes.ok && Array.isArray(lsRes.result)) {
|
|
258
|
+
const spaces = lsRes.result
|
|
259
|
+
.filter((e) => e?.isDir)
|
|
260
|
+
.map((e) => (typeof e.name === "string" ? e.name.trim() : ""))
|
|
261
|
+
.filter((n) => n && !n.startsWith(".") && !USER_RESERVED_DIRS.has(n));
|
|
262
|
+
if (spaces.length > 0) {
|
|
263
|
+
if (spaces.includes(fallbackSpace)) { userSpaceCache = fallbackSpace; return fallbackSpace; }
|
|
264
|
+
if (spaces.includes("default")) { userSpaceCache = "default"; return "default"; }
|
|
265
|
+
if (spaces.length === 1) { userSpaceCache = spaces[0]; return spaces[0]; }
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
userSpaceCache = fallbackSpace;
|
|
269
|
+
return fallbackSpace;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function resolveTargetUri(fetchJSON, targetUri, actorPeerId = "") {
|
|
273
|
+
const trimmed = targetUri.trim().replace(/\/+$/, "");
|
|
274
|
+
// viking://~ is the home alias: the server expands it to the caller's own user
|
|
275
|
+
// space, so it needs no client-side rewrite.
|
|
276
|
+
if (trimmed === "viking://~" || trimmed.startsWith("viking://~/")) return trimmed;
|
|
277
|
+
// Legacy compat: uid-less viking://user/<reserved> URIs may still sit in plugin
|
|
278
|
+
// configs. Newer servers reject them, so rewrite to an explicit-uid URI here.
|
|
279
|
+
const m = trimmed.match(/^viking:\/\/user(?:\/(.*))?$/);
|
|
280
|
+
if (!m) return trimmed;
|
|
281
|
+
const rawRest = (m[1] ?? "").trim();
|
|
282
|
+
if (!rawRest) return trimmed;
|
|
283
|
+
const parts = rawRest.split("/").filter(Boolean);
|
|
284
|
+
if (parts.length === 0) return trimmed;
|
|
285
|
+
if (!USER_RESERVED_DIRS.has(parts[0])) return trimmed;
|
|
286
|
+
const space = await resolveUserSpace(fetchJSON, actorPeerId);
|
|
287
|
+
return `viking://user/${space}/${parts.join("/")}`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function searchOneSource(fetchJSON, query, source, limit, actorPeerId = "") {
|
|
291
|
+
const resolvedUri = await resolveTargetUri(fetchJSON, source.uri, actorPeerId);
|
|
292
|
+
const body = { query, target_uri: resolvedUri, limit, score_threshold: 0 };
|
|
293
|
+
const res = await fetchJSON("/api/v1/search/find", {
|
|
294
|
+
method: "POST",
|
|
295
|
+
body: JSON.stringify(body),
|
|
296
|
+
}, { actorPeerId });
|
|
297
|
+
if (!res.ok) return [];
|
|
298
|
+
const items = res.result?.[source.bucket] || [];
|
|
299
|
+
return items.map((item) => ({ ...item, _sourceType: source.type }));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async function searchAllSources(fetchJSON, query, perSourceLimit, actorPeerId = "", log = () => {}) {
|
|
303
|
+
const results = await Promise.all(
|
|
304
|
+
SOURCES.map((src) => searchOneSource(fetchJSON, query, src, perSourceLimit, actorPeerId)),
|
|
305
|
+
);
|
|
306
|
+
const all = results.flat();
|
|
307
|
+
log("recall_search_summary", {
|
|
308
|
+
counts: SOURCES.map((src, i) => ({ type: src.type, uri: src.uri, count: results[i].length })),
|
|
309
|
+
total: all.length,
|
|
310
|
+
});
|
|
311
|
+
return all;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async function resolveItemContent(fetchJSON, item, cfg, actorPeerId = "") {
|
|
315
|
+
let content;
|
|
316
|
+
|
|
317
|
+
if (cfg.recallPreferAbstract && (item.abstract || item.overview || "").trim()) {
|
|
318
|
+
content = (item.abstract || item.overview).trim();
|
|
319
|
+
} else if (item.level === 2) {
|
|
320
|
+
try {
|
|
321
|
+
const res = await fetchJSON(
|
|
322
|
+
`/api/v1/content/read?uri=${encodeURIComponent(item.uri)}`,
|
|
323
|
+
{},
|
|
324
|
+
{ actorPeerId },
|
|
325
|
+
);
|
|
326
|
+
const body = res.ok && typeof res.result === "string" ? res.result.trim() : "";
|
|
327
|
+
content = body || (item.abstract || item.overview || "").trim() || item.uri;
|
|
328
|
+
} catch {
|
|
329
|
+
content = (item.abstract || item.overview || "").trim() || item.uri;
|
|
330
|
+
}
|
|
331
|
+
} else {
|
|
332
|
+
content = (item.abstract || item.overview || "").trim() || item.uri;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const maxChars = Math.max(50, Number(cfg.recallMaxContentChars || 500));
|
|
336
|
+
if (content.length > maxChars) content = `${content.slice(0, maxChars)}...`;
|
|
337
|
+
return content;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async function buildFallbackInjectionBlock(fetchJSON, items, cfg, actorPeerId = "", log = () => {}) {
|
|
341
|
+
if (items.length === 0) return null;
|
|
342
|
+
|
|
343
|
+
let budgetRemaining = Math.max(200, Number(cfg.recallTokenBudget || 2000));
|
|
344
|
+
const lines = [
|
|
345
|
+
"<openviking-context>",
|
|
346
|
+
"Relevant context from OpenViking. Use the read MCP tool to expand URIs.",
|
|
347
|
+
];
|
|
348
|
+
let contentCount = 0;
|
|
349
|
+
let hintCount = 0;
|
|
350
|
+
|
|
351
|
+
for (const item of items) {
|
|
352
|
+
const score = (clampScore(item.score) * 100).toFixed(0);
|
|
353
|
+
const uriLine = `- [${item._sourceType} ${score}%] ${item.uri}`;
|
|
354
|
+
|
|
355
|
+
if (budgetRemaining > 0) {
|
|
356
|
+
const content = await resolveItemContent(fetchJSON, item, cfg, actorPeerId);
|
|
357
|
+
const contentLine = `- [${item._sourceType} ${score}%] ${content}`;
|
|
358
|
+
const lineTokens = estimateTokens(contentLine);
|
|
359
|
+
|
|
360
|
+
if (lineTokens > budgetRemaining && contentCount > 0) {
|
|
361
|
+
lines.push(uriLine);
|
|
362
|
+
hintCount++;
|
|
363
|
+
} else {
|
|
364
|
+
lines.push(contentLine);
|
|
365
|
+
budgetRemaining -= lineTokens;
|
|
366
|
+
contentCount++;
|
|
367
|
+
}
|
|
368
|
+
} else {
|
|
369
|
+
lines.push(uriLine);
|
|
370
|
+
hintCount++;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
lines.push("</openviking-context>");
|
|
375
|
+
|
|
376
|
+
const budgetUsed = Math.max(200, Number(cfg.recallTokenBudget || 2000)) - budgetRemaining;
|
|
377
|
+
log("recall_injection_built", {
|
|
378
|
+
contentItems: contentCount,
|
|
379
|
+
hintItems: hintCount,
|
|
380
|
+
budgetUsed,
|
|
381
|
+
budgetTotal: Math.max(200, Number(cfg.recallTokenBudget || 2000)),
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
return lines.join("\n");
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const LEGACY_CACHE_TTL_MS = 6 * 60 * 60 * 1000;
|
|
388
|
+
|
|
389
|
+
function stateFile(name) {
|
|
390
|
+
const override = String(process.env.OPENVIKING_STATE_DIR || "").trim();
|
|
391
|
+
return override ? join(override, name) : join(homedir(), ".openviking", "state", name);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async function readJsonFile(path) {
|
|
395
|
+
try { return JSON.parse(await readFile(path, "utf8")); } catch { return null; }
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
async function writeJsonFile(path, value) {
|
|
399
|
+
try {
|
|
400
|
+
await mkdir(dirname(path), { recursive: true });
|
|
401
|
+
const tmp = `${path}.tmp`;
|
|
402
|
+
await writeFile(tmp, JSON.stringify(value));
|
|
403
|
+
await rename(tmp, path);
|
|
404
|
+
} catch { /* best effort */ }
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Hooks are one-shot processes, so "this server has no context face" has to be
|
|
409
|
+
* remembered on disk or every turn pays for a rejected request.
|
|
410
|
+
*/
|
|
411
|
+
export async function isContextFaceLegacy(path = stateFile("context-face.json"), now = Date.now()) {
|
|
412
|
+
const cached = await readJsonFile(path);
|
|
413
|
+
return Boolean(cached?.legacyUntil && Number(cached.legacyUntil) > now);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export async function markContextFaceLegacy(path = stateFile("context-face.json"), now = Date.now()) {
|
|
417
|
+
await writeJsonFile(path, { legacyUntil: now + LEGACY_CACHE_TTL_MS });
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* A `peer_scope` the server rejects is remembered the same way, but unlike the
|
|
422
|
+
* context face this one is not a silent capability probe: dropping the field
|
|
423
|
+
* widens recall from the caller's own peer to the whole user root, so doctor
|
|
424
|
+
* reads this file back and warns.
|
|
425
|
+
*/
|
|
426
|
+
export function peerScopeMemoPath() {
|
|
427
|
+
return stateFile("peer-scope.json");
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export async function readPeerScopeDowngrade(path = peerScopeMemoPath(), now = Date.now()) {
|
|
431
|
+
const cached = await readJsonFile(path);
|
|
432
|
+
if (!cached?.legacyUntil || Number(cached.legacyUntil) <= now) return null;
|
|
433
|
+
return cached;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export async function markPeerScopeDowngrade(scope, status, path = peerScopeMemoPath(), now = Date.now()) {
|
|
437
|
+
await writeJsonFile(path, {
|
|
438
|
+
legacyUntil: now + LEGACY_CACHE_TTL_MS,
|
|
439
|
+
scope: String(scope || ""),
|
|
440
|
+
status: Number(status) || 0,
|
|
441
|
+
at: now,
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function looksLikeUnknownField(res) {
|
|
446
|
+
const text = JSON.stringify(res?.error ?? res?.result ?? res?.detail ?? "").toLowerCase();
|
|
447
|
+
return text.includes("extra") || text.includes("mode") || text.includes("unexpected");
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function wrapContext(body) {
|
|
451
|
+
return [
|
|
452
|
+
"<openviking-context>",
|
|
453
|
+
"Relevant memory from OpenViking. Use the search/read MCP tools to expand URIs.",
|
|
454
|
+
body,
|
|
455
|
+
"</openviking-context>",
|
|
456
|
+
].join("\n");
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Server-assembled context: the context face when the deployment has it, else
|
|
461
|
+
* the deprecated /recall preset. Returns the injection block, "" when there was
|
|
462
|
+
* nothing relevant, or null when no server-side path was usable at all.
|
|
463
|
+
*/
|
|
464
|
+
export async function buildServerAssembledBlock(fetchJSON, cfg, query, options = {}) {
|
|
465
|
+
const actorPeerId = options.actorPeerId ?? cfg.peerId ?? "";
|
|
466
|
+
const log = options.log || (() => {});
|
|
467
|
+
|
|
468
|
+
const block = await recallViaContextFace(fetchJSON, cfg, query, { ...options, actorPeerId }, log);
|
|
469
|
+
if (block !== null) return block;
|
|
470
|
+
return recallViaEndpoint(fetchJSON, cfg, query, actorPeerId, log);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Raw server-assembled context, or null when the deployment has no context face.
|
|
475
|
+
* Returns `{ rendered, entries, digest, stats }` — callers that need the entries
|
|
476
|
+
* (their own compression, their own envelope) use this instead of the block
|
|
477
|
+
* builders below.
|
|
478
|
+
*/
|
|
479
|
+
export async function fetchAssembledContext(fetchJSON, cfg, query, options = {}) {
|
|
480
|
+
const actorPeerId = options.actorPeerId || "";
|
|
481
|
+
const log = options.log || (() => {});
|
|
482
|
+
if (await isContextFaceLegacy(options.legacyCachePath)) return null;
|
|
483
|
+
|
|
484
|
+
const body = buildContextSearchBody(cfg, options);
|
|
485
|
+
body.query = query;
|
|
486
|
+
const res = await fetchJSON("/api/v1/search/search", {
|
|
487
|
+
method: "POST",
|
|
488
|
+
body: JSON.stringify(body),
|
|
489
|
+
}, { actorPeerId, timeoutMs: contextRequestTimeoutMs(cfg, body) });
|
|
490
|
+
|
|
491
|
+
if (!res.ok) {
|
|
492
|
+
const status = res.status || 0;
|
|
493
|
+
if ((status === 400 || status === 422) && looksLikeUnknownField(res)) {
|
|
494
|
+
await markContextFaceLegacy(options.legacyCachePath);
|
|
495
|
+
log("recall_context_face_unsupported", { status });
|
|
496
|
+
} else {
|
|
497
|
+
log("recall_context_face_error", { status });
|
|
498
|
+
}
|
|
499
|
+
return null;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const result = res.result || {};
|
|
503
|
+
const stats = result.stats || {};
|
|
504
|
+
log("recall_context_assembled", {
|
|
505
|
+
entries: Array.isArray(result.entries) ? result.entries.length : 0,
|
|
506
|
+
usedTokens: stats.used_tokens || 0,
|
|
507
|
+
tiers: stats.tier_counts || {},
|
|
508
|
+
rewrite: stats.rewrite || "off",
|
|
509
|
+
});
|
|
510
|
+
return {
|
|
511
|
+
rendered: String(result.rendered || "").trim(),
|
|
512
|
+
entries: Array.isArray(result.entries) ? result.entries : [],
|
|
513
|
+
digest: String(result.digest || "").trim(),
|
|
514
|
+
stats,
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Entry field compatibility: the context face returns `category`/`text`, while
|
|
520
|
+
* the deprecated /recall v1 shape used `type` plus `content`/`summary`.
|
|
521
|
+
*/
|
|
522
|
+
export function normalizeContextEntry(entry = {}) {
|
|
523
|
+
return {
|
|
524
|
+
uri: String(entry.uri || "").trim(),
|
|
525
|
+
category: String(entry.category || entry.type || "memory").trim() || "memory",
|
|
526
|
+
detail: String(entry.detail || entry.mode || "").trim(),
|
|
527
|
+
score: Number(entry.score) || 0,
|
|
528
|
+
text: String(
|
|
529
|
+
entry.text || entry.content || entry.summary || entry.abstract || entry.uri || "",
|
|
530
|
+
).trim(),
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
async function recallViaContextFace(fetchJSON, cfg, query, options, log) {
|
|
535
|
+
const assembled = await fetchAssembledContext(fetchJSON, cfg, query, { ...options, log });
|
|
536
|
+
if (assembled === null) return null;
|
|
537
|
+
|
|
538
|
+
const { rendered, entries } = assembled;
|
|
539
|
+
let digest = assembled.digest;
|
|
540
|
+
const mode = String(cfg.recallRewrite || "off").toLowerCase();
|
|
541
|
+
if (String(assembled.stats?.rewrite || "").toLowerCase() === "no_relevant") {
|
|
542
|
+
log("recall_server_compression", { status: "empty" });
|
|
543
|
+
return "";
|
|
544
|
+
}
|
|
545
|
+
const wantsLocal = mode === "client" || (mode === "auto" && !digest);
|
|
546
|
+
if (wantsLocal && rendered && typeof options.runCompressor === "function") {
|
|
547
|
+
try {
|
|
548
|
+
const compression = await compressRecallContext({
|
|
549
|
+
query,
|
|
550
|
+
rendered,
|
|
551
|
+
entries,
|
|
552
|
+
cfg,
|
|
553
|
+
runCompressor: options.runCompressor,
|
|
554
|
+
cachePath: options.digestCachePath || stateFile("recall-digest.json"),
|
|
555
|
+
now: Date.now(),
|
|
556
|
+
});
|
|
557
|
+
log("recall_local_compression", { status: compression.status });
|
|
558
|
+
if (compression.status === "ok") digest = compression.context;
|
|
559
|
+
if (compression.status === "empty") return "";
|
|
560
|
+
} catch (err) {
|
|
561
|
+
log("recall_local_compression_failed", { error: String(err?.message || err) });
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const injected = digest || rendered;
|
|
566
|
+
if (!injected) return "";
|
|
567
|
+
return wrapContext(injected);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async function recallViaEndpoint(fetchJSON, cfg, query, actorPeerId = "", log = () => {}) {
|
|
571
|
+
const body = buildRecallEndpointBody(cfg);
|
|
572
|
+
body.query = query;
|
|
573
|
+
const res = await postRecall(fetchJSON, body, { actorPeerId, log });
|
|
574
|
+
if (!res.ok) {
|
|
575
|
+
log("recall_endpoint_fallback", { status: res.status || 0 });
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
const rendered = String(res.result?.rendered || "").trim();
|
|
579
|
+
if (!rendered) return "";
|
|
580
|
+
return wrapContext(rendered);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
export async function postRecall(fetchJSON, body, opts = {}) {
|
|
584
|
+
const actorPeerId = opts.actorPeerId || "";
|
|
585
|
+
const log = opts.log || (() => {});
|
|
586
|
+
const memoPath = opts.peerScopeMemoPath;
|
|
587
|
+
const request = { ...body };
|
|
588
|
+
|
|
589
|
+
// A remembered downgrade is still a downgrade: recall runs wider than the
|
|
590
|
+
// caller asked for, so the memo doubles as the doctor's evidence.
|
|
591
|
+
if (request.peer_scope && await readPeerScopeDowngrade(memoPath)) {
|
|
592
|
+
delete request.peer_scope;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const res = await fetchJSON("/api/v1/search/recall", {
|
|
596
|
+
method: "POST",
|
|
597
|
+
body: JSON.stringify(request),
|
|
598
|
+
}, { actorPeerId });
|
|
599
|
+
if (!request.peer_scope || (res.status !== 400 && res.status !== 422)) {
|
|
600
|
+
return res;
|
|
601
|
+
}
|
|
602
|
+
// Only an unknown-field rejection means "this server predates peer_scope".
|
|
603
|
+
// Retrying every other 400/422 without it silently widens the search from
|
|
604
|
+
// the caller's own peer to the whole user root.
|
|
605
|
+
if (!looksLikeUnknownField(res)) {
|
|
606
|
+
log("recall_peer_scope_error", { status: res.status || 0 });
|
|
607
|
+
return res;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
const downgraded = { ...request };
|
|
611
|
+
delete downgraded.peer_scope;
|
|
612
|
+
await markPeerScopeDowngrade(String(request.peer_scope), res.status || 0, memoPath);
|
|
613
|
+
log("recall_peer_scope_downgrade", { status: res.status || 0 });
|
|
614
|
+
return fetchJSON("/api/v1/search/recall", {
|
|
615
|
+
method: "POST",
|
|
616
|
+
body: JSON.stringify(downgraded),
|
|
617
|
+
}, { actorPeerId });
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Recall, plus whatever is still filed under the peer this workspace used
|
|
622
|
+
* before the identity rule changed.
|
|
623
|
+
*
|
|
624
|
+
* Under the default `peer_scope: "all"` this costs nothing: the server's own
|
|
625
|
+
* sweep of `{user_root}/peers` already reaches the old peer's memories. Under
|
|
626
|
+
* `"actor"` that sweep is off by definition, so the old peer is asked for
|
|
627
|
+
* separately — as itself, which is both cheaper and wider than a bare
|
|
628
|
+
* cross-peer read (that would need the user id, and reaches memories only).
|
|
629
|
+
*/
|
|
630
|
+
export async function buildRecallBlock(fetchJSON, cfg, query, options = {}) {
|
|
631
|
+
const primary = await recallForPeer(fetchJSON, cfg, query, options);
|
|
632
|
+
|
|
633
|
+
const legacyPeerId = String(options.legacyPeerId || "").trim();
|
|
634
|
+
const actorPeerId = options.actorPeerId ?? cfg.peerId ?? "";
|
|
635
|
+
if (cfg.recallPeerScope !== "actor" || !legacyPeerId || legacyPeerId === actorPeerId) return primary;
|
|
636
|
+
|
|
637
|
+
const log = options.log || (() => {});
|
|
638
|
+
const legacy = await recallForPeer(fetchJSON, cfg, query, { ...options, actorPeerId: legacyPeerId });
|
|
639
|
+
if (!legacy) return primary;
|
|
640
|
+
log("recall_legacy_peer_hit", { legacyPeerId });
|
|
641
|
+
return primary ? `${primary}\n${legacy}` : legacy;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
async function recallForPeer(fetchJSON, cfg, query, options = {}) {
|
|
645
|
+
const actorPeerId = options.actorPeerId ?? cfg.peerId ?? "";
|
|
646
|
+
const log = options.log || (() => {});
|
|
647
|
+
const trimmed = String(query || "").trim();
|
|
648
|
+
if (!trimmed) return null;
|
|
649
|
+
|
|
650
|
+
// Assembly happens server-side when the deployment offers the context face;
|
|
651
|
+
// older servers fall through to /recall, then to raw find.
|
|
652
|
+
const serverBlock = await buildServerAssembledBlock(fetchJSON, cfg, trimmed, {
|
|
653
|
+
...options,
|
|
654
|
+
actorPeerId,
|
|
655
|
+
log,
|
|
656
|
+
});
|
|
657
|
+
if (serverBlock !== null) return serverBlock || null;
|
|
658
|
+
|
|
659
|
+
const recallLimit = Math.max(1, Number(cfg.recallLimit || DEFAULT_CONTEXT_LIMIT));
|
|
660
|
+
const perSourceLimit = Math.max(recallLimit * 2, 8);
|
|
661
|
+
const raw = await searchAllSources(fetchJSON, trimmed, perSourceLimit, actorPeerId, log);
|
|
662
|
+
if (raw.length === 0) return null;
|
|
663
|
+
|
|
664
|
+
const profile = buildQueryProfile(trimmed);
|
|
665
|
+
const scoreThreshold = Number.isFinite(Number(cfg.scoreThreshold)) ? Number(cfg.scoreThreshold) : 0.35;
|
|
666
|
+
const filtered = raw.filter((it) => clampScore(it.score) >= scoreThreshold);
|
|
667
|
+
filtered.sort((a, b) => rankItem(b, profile) - rankItem(a, profile));
|
|
668
|
+
const picked = dedupeItems(filtered).slice(0, recallLimit);
|
|
669
|
+
log("recall_picked", {
|
|
670
|
+
rawCount: raw.length,
|
|
671
|
+
filteredCount: filtered.length,
|
|
672
|
+
pickedCount: picked.length,
|
|
673
|
+
items: picked.map((it) => ({ type: it._sourceType, uri: it.uri, score: clampScore(it.score) })),
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
if (picked.length === 0) return null;
|
|
677
|
+
return buildFallbackInjectionBlock(fetchJSON, picked, cfg, actorPeerId, log);
|
|
678
|
+
}
|