@openviking/openclaw-plugin 2026.5.8
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 +355 -0
- package/INSTALL-ZH.md +345 -0
- package/INSTALL.md +362 -0
- package/README.md +315 -0
- package/auto-recall.ts +257 -0
- package/client.ts +828 -0
- package/commands/setup.ts +1002 -0
- package/config.ts +476 -0
- package/context-engine.ts +1700 -0
- package/dist/auto-recall.js +167 -0
- package/dist/client.js +502 -0
- package/dist/commands/setup.js +815 -0
- package/dist/config.js +352 -0
- package/dist/context-engine.js +1324 -0
- package/dist/index.js +1194 -0
- package/dist/memory-ranking.js +221 -0
- package/dist/process-manager.js +127 -0
- package/dist/runtime-utils.js +7 -0
- package/dist/session-transcript-repair.js +427 -0
- package/dist/text-utils.js +453 -0
- package/dist/tool-call-id.js +254 -0
- package/index.ts +1597 -0
- package/install-manifest.json +47 -0
- package/memory-ranking.ts +267 -0
- package/openclaw.plugin.json +331 -0
- package/package.json +75 -0
- package/process-manager.ts +169 -0
- package/runtime-utils.ts +8 -0
- package/session-transcript-repair.ts +530 -0
- package/skills/install-openviking-memory/SKILL.md +665 -0
- package/text-utils.ts +524 -0
- package/tool-call-id.ts +331 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
export function clampScore(value) {
|
|
2
|
+
if (typeof value !== "number" || Number.isNaN(value)) {
|
|
3
|
+
return 0;
|
|
4
|
+
}
|
|
5
|
+
return Math.max(0, Math.min(1, value));
|
|
6
|
+
}
|
|
7
|
+
function normalizeDedupeText(text) {
|
|
8
|
+
return text.toLowerCase().replace(/\s+/g, " ").trim();
|
|
9
|
+
}
|
|
10
|
+
function isEventOrCaseMemory(item) {
|
|
11
|
+
const category = (item.category ?? "").toLowerCase();
|
|
12
|
+
const uri = item.uri.toLowerCase();
|
|
13
|
+
return (category === "events" ||
|
|
14
|
+
category === "cases" ||
|
|
15
|
+
uri.includes("/events/") ||
|
|
16
|
+
uri.includes("/cases/"));
|
|
17
|
+
}
|
|
18
|
+
function getMemoryDedupeKey(item) {
|
|
19
|
+
const abstract = normalizeDedupeText(item.abstract ?? item.overview ?? "");
|
|
20
|
+
const category = (item.category ?? "").toLowerCase() || "unknown";
|
|
21
|
+
if (abstract && !isEventOrCaseMemory(item)) {
|
|
22
|
+
return `abstract:${category}:${abstract}`;
|
|
23
|
+
}
|
|
24
|
+
return `uri:${item.uri}`;
|
|
25
|
+
}
|
|
26
|
+
export function postProcessMemories(items, options) {
|
|
27
|
+
const deduped = [];
|
|
28
|
+
const seen = new Set();
|
|
29
|
+
const sorted = [...items].sort((a, b) => clampScore(b.score) - clampScore(a.score));
|
|
30
|
+
for (const item of sorted) {
|
|
31
|
+
if (options.leafOnly && item.level !== 2) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (clampScore(item.score) < options.scoreThreshold) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const key = getMemoryDedupeKey(item);
|
|
38
|
+
if (seen.has(key)) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
seen.add(key);
|
|
42
|
+
deduped.push(item);
|
|
43
|
+
if (deduped.length >= options.limit) {
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return deduped;
|
|
48
|
+
}
|
|
49
|
+
export function formatMemoryLines(items) {
|
|
50
|
+
return items
|
|
51
|
+
.map((item, index) => {
|
|
52
|
+
const score = clampScore(item.score);
|
|
53
|
+
const abstract = item.abstract?.trim() || item.overview?.trim() || item.uri;
|
|
54
|
+
const category = item.category ?? "memory";
|
|
55
|
+
return `${index + 1}. [${category}] ${abstract} (${(score * 100).toFixed(0)}%)`;
|
|
56
|
+
})
|
|
57
|
+
.join("\n");
|
|
58
|
+
}
|
|
59
|
+
export function trimForLog(value, limit = 260) {
|
|
60
|
+
const normalized = value.trim();
|
|
61
|
+
if (normalized.length <= limit) {
|
|
62
|
+
return normalized;
|
|
63
|
+
}
|
|
64
|
+
return `${normalized.slice(0, limit)}...`;
|
|
65
|
+
}
|
|
66
|
+
export function toJsonLog(value, maxLen = 6000) {
|
|
67
|
+
try {
|
|
68
|
+
const json = JSON.stringify(value);
|
|
69
|
+
if (json.length <= maxLen) {
|
|
70
|
+
return json;
|
|
71
|
+
}
|
|
72
|
+
return JSON.stringify({
|
|
73
|
+
truncated: true,
|
|
74
|
+
length: json.length,
|
|
75
|
+
preview: `${json.slice(0, maxLen)}...`,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return JSON.stringify({ error: "stringify_failed" });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export function summarizeInjectionMemories(items) {
|
|
83
|
+
return items.map((item) => ({
|
|
84
|
+
uri: item.uri,
|
|
85
|
+
category: item.category ?? null,
|
|
86
|
+
abstract: trimForLog(item.abstract?.trim() || item.overview?.trim() || item.uri, 180),
|
|
87
|
+
score: clampScore(item.score),
|
|
88
|
+
is_leaf: item.level === 2,
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
export function summarizeExtractedMemories(items) {
|
|
92
|
+
return items.slice(0, 10).map((item) => {
|
|
93
|
+
const abstractRaw = typeof item.abstract === "string"
|
|
94
|
+
? item.abstract
|
|
95
|
+
: typeof item.overview === "string"
|
|
96
|
+
? item.overview
|
|
97
|
+
: typeof item.title === "string"
|
|
98
|
+
? item.title
|
|
99
|
+
: "";
|
|
100
|
+
return {
|
|
101
|
+
uri: typeof item.uri === "string" ? item.uri : null,
|
|
102
|
+
category: typeof item.category === "string" ? item.category : null,
|
|
103
|
+
abstract: trimForLog(abstractRaw, 180),
|
|
104
|
+
is_leaf: item.level === 2,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function isPreferencesMemory(item) {
|
|
109
|
+
return (item.category === "preferences" ||
|
|
110
|
+
item.uri.includes("/preferences/") ||
|
|
111
|
+
item.uri.endsWith("/preferences"));
|
|
112
|
+
}
|
|
113
|
+
function isEventMemory(item) {
|
|
114
|
+
const category = (item.category ?? "").toLowerCase();
|
|
115
|
+
return category === "events" || item.uri.includes("/events/");
|
|
116
|
+
}
|
|
117
|
+
function isLeafLikeMemory(item) {
|
|
118
|
+
return item.level === 2;
|
|
119
|
+
}
|
|
120
|
+
const PREFERENCE_QUERY_RE = /prefer|preference|favorite|favourite|like|偏好|喜欢|爱好|更倾向/i;
|
|
121
|
+
const TEMPORAL_QUERY_RE = /when|what time|date|day|month|year|yesterday|today|tomorrow|last|next|什么时候|何时|哪天|几月|几年|昨天|今天|明天|上周|下周|上个月|下个月|去年|明年/i;
|
|
122
|
+
const QUERY_TOKEN_RE = /[a-z0-9]{2,}/gi;
|
|
123
|
+
const QUERY_TOKEN_STOPWORDS = new Set([
|
|
124
|
+
"what",
|
|
125
|
+
"when",
|
|
126
|
+
"where",
|
|
127
|
+
"which",
|
|
128
|
+
"who",
|
|
129
|
+
"whom",
|
|
130
|
+
"whose",
|
|
131
|
+
"why",
|
|
132
|
+
"how",
|
|
133
|
+
"did",
|
|
134
|
+
"does",
|
|
135
|
+
"is",
|
|
136
|
+
"are",
|
|
137
|
+
"was",
|
|
138
|
+
"were",
|
|
139
|
+
"the",
|
|
140
|
+
"and",
|
|
141
|
+
"for",
|
|
142
|
+
"with",
|
|
143
|
+
"from",
|
|
144
|
+
"that",
|
|
145
|
+
"this",
|
|
146
|
+
"your",
|
|
147
|
+
"you",
|
|
148
|
+
]);
|
|
149
|
+
function buildRecallQueryProfile(query) {
|
|
150
|
+
const text = query.trim();
|
|
151
|
+
const allTokens = text.toLowerCase().match(QUERY_TOKEN_RE) ?? [];
|
|
152
|
+
const tokens = allTokens.filter((token) => !QUERY_TOKEN_STOPWORDS.has(token));
|
|
153
|
+
return {
|
|
154
|
+
tokens,
|
|
155
|
+
wantsPreference: PREFERENCE_QUERY_RE.test(text),
|
|
156
|
+
wantsTemporal: TEMPORAL_QUERY_RE.test(text),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function lexicalOverlapBoost(tokens, text) {
|
|
160
|
+
if (tokens.length === 0 || !text) {
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
const haystack = ` ${text.toLowerCase()} `;
|
|
164
|
+
let matched = 0;
|
|
165
|
+
for (const token of tokens.slice(0, 8)) {
|
|
166
|
+
if (haystack.includes(` ${token} `) || haystack.includes(token)) {
|
|
167
|
+
matched += 1;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return Math.min(0.2, (matched / Math.min(tokens.length, 4)) * 0.2);
|
|
171
|
+
}
|
|
172
|
+
function rankForInjection(item, query) {
|
|
173
|
+
// Keep ranking simple and stable: semantic score + light query-aware boosts.
|
|
174
|
+
const baseScore = clampScore(item.score);
|
|
175
|
+
const abstract = (item.abstract ?? item.overview ?? "").trim();
|
|
176
|
+
const leafBoost = isLeafLikeMemory(item) ? 0.12 : 0;
|
|
177
|
+
const eventBoost = query.wantsTemporal && isEventMemory(item) ? 0.1 : 0;
|
|
178
|
+
const preferenceBoost = query.wantsPreference && isPreferencesMemory(item) ? 0.08 : 0;
|
|
179
|
+
const overlapBoost = lexicalOverlapBoost(query.tokens, `${item.uri} ${abstract}`);
|
|
180
|
+
return baseScore + leafBoost + eventBoost + preferenceBoost + overlapBoost;
|
|
181
|
+
}
|
|
182
|
+
export function pickMemoriesForInjection(items, limit, queryText, scoreThreshold = 0) {
|
|
183
|
+
if (items.length === 0 || limit <= 0) {
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
const query = buildRecallQueryProfile(queryText);
|
|
187
|
+
const sorted = [...items].sort((a, b) => rankForInjection(b, query) - rankForInjection(a, query));
|
|
188
|
+
const deduped = [];
|
|
189
|
+
const seen = new Set();
|
|
190
|
+
for (const item of sorted) {
|
|
191
|
+
const abstractKey = (item.abstract ?? item.overview ?? "").trim().toLowerCase();
|
|
192
|
+
const key = abstractKey || item.uri;
|
|
193
|
+
if (seen.has(key)) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
seen.add(key);
|
|
197
|
+
deduped.push(item);
|
|
198
|
+
}
|
|
199
|
+
const leaves = deduped.filter((item) => isLeafLikeMemory(item));
|
|
200
|
+
if (leaves.length >= limit) {
|
|
201
|
+
return leaves.slice(0, limit);
|
|
202
|
+
}
|
|
203
|
+
const picked = [...leaves];
|
|
204
|
+
const used = new Set(leaves.map((item) => item.uri));
|
|
205
|
+
for (const item of deduped) {
|
|
206
|
+
if (picked.length >= limit) {
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
if (used.has(item.uri)) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
// Respect score threshold when supplementing leaf memories with
|
|
213
|
+
// non-leaf items. Without this check, low-scoring memories bypass
|
|
214
|
+
// the threshold configured in recallScoreThreshold (see #1106).
|
|
215
|
+
if (clampScore(item.score) < scoreThreshold) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
picked.push(item);
|
|
219
|
+
}
|
|
220
|
+
return picked;
|
|
221
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Socket } from "node:net";
|
|
2
|
+
export function waitForHealth(baseUrl, timeoutMs, intervalMs) {
|
|
3
|
+
const deadline = Date.now() + timeoutMs;
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
const tick = () => {
|
|
6
|
+
if (Date.now() > deadline) {
|
|
7
|
+
reject(new Error(`OpenViking health check timeout at ${baseUrl}`));
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
fetch(`${baseUrl}/health`)
|
|
11
|
+
.then((r) => r.json())
|
|
12
|
+
.then((body) => {
|
|
13
|
+
if (body?.status === "ok") {
|
|
14
|
+
resolve();
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
setTimeout(tick, intervalMs);
|
|
18
|
+
})
|
|
19
|
+
.catch(() => setTimeout(tick, intervalMs));
|
|
20
|
+
};
|
|
21
|
+
tick();
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export function waitForHealthOrExit(baseUrl, timeoutMs, intervalMs, child) {
|
|
25
|
+
const exited = child.killed || child.exitCode !== null || child.signalCode !== null;
|
|
26
|
+
if (exited) {
|
|
27
|
+
return Promise.reject(new Error(`OpenViking subprocess exited before health check ` +
|
|
28
|
+
`(code=${child.exitCode}, signal=${child.signalCode})`));
|
|
29
|
+
}
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
let settled = false;
|
|
32
|
+
const cleanup = () => {
|
|
33
|
+
child.off?.("error", onError);
|
|
34
|
+
child.off?.("exit", onExit);
|
|
35
|
+
};
|
|
36
|
+
const finishResolve = () => {
|
|
37
|
+
if (settled) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
settled = true;
|
|
41
|
+
cleanup();
|
|
42
|
+
resolve();
|
|
43
|
+
};
|
|
44
|
+
const finishReject = (err) => {
|
|
45
|
+
if (settled) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
settled = true;
|
|
49
|
+
cleanup();
|
|
50
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
51
|
+
};
|
|
52
|
+
const onError = (err) => {
|
|
53
|
+
finishReject(err);
|
|
54
|
+
};
|
|
55
|
+
const onExit = (code, signal) => {
|
|
56
|
+
finishReject(new Error(`OpenViking subprocess exited before health check ` +
|
|
57
|
+
`(code=${code}, signal=${signal})`));
|
|
58
|
+
};
|
|
59
|
+
child.once("error", onError);
|
|
60
|
+
child.once("exit", onExit);
|
|
61
|
+
waitForHealth(baseUrl, timeoutMs, intervalMs).then(finishResolve, finishReject);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
export function withTimeout(promise, timeoutMs, timeoutMessage) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const timer = setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs);
|
|
67
|
+
promise.then((value) => {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
resolve(value);
|
|
70
|
+
}, (err) => {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
reject(err);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
export function quickTcpProbe(host, port, timeoutMs) {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
const socket = new Socket();
|
|
79
|
+
let done = false;
|
|
80
|
+
const finish = (ok) => {
|
|
81
|
+
if (done) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
done = true;
|
|
85
|
+
socket.destroy();
|
|
86
|
+
resolve(ok);
|
|
87
|
+
};
|
|
88
|
+
socket.setTimeout(timeoutMs);
|
|
89
|
+
socket.once("connect", () => finish(true));
|
|
90
|
+
socket.once("timeout", () => finish(false));
|
|
91
|
+
socket.once("error", () => finish(false));
|
|
92
|
+
try {
|
|
93
|
+
socket.connect(port, host);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
finish(false);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
export async function quickHealthCheck(baseUrl, timeoutMs) {
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
103
|
+
try {
|
|
104
|
+
const response = await fetch(`${baseUrl}/health`, {
|
|
105
|
+
method: "GET",
|
|
106
|
+
signal: controller.signal,
|
|
107
|
+
});
|
|
108
|
+
if (!response.ok) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
const body = (await response.json().catch(() => ({})));
|
|
112
|
+
return body.status === "ok";
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export async function quickRecallPrecheck(baseUrl) {
|
|
122
|
+
const healthOk = await quickHealthCheck(baseUrl, 500);
|
|
123
|
+
if (healthOk) {
|
|
124
|
+
return { ok: true };
|
|
125
|
+
}
|
|
126
|
+
return { ok: false, reason: "health check failed" };
|
|
127
|
+
}
|