@openviking/opencode-plugin 0.1.6 → 0.2.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-ZH.md +50 -49
- package/INSTALL.md +50 -49
- package/README.md +57 -126
- package/index.mjs +20 -12
- package/lib/config.mjs +284 -0
- package/lib/mcp-config.mjs +21 -0
- package/lib/memory-recall.mjs +21 -172
- package/lib/memory-session.mjs +301 -478
- package/lib/repo-context.mjs +5 -5
- package/lib/session-inject.mjs +71 -0
- package/lib/shared/capture-utils.mjs +500 -0
- package/lib/shared/credentials.mjs +207 -0
- package/lib/shared/debug-log.mjs +58 -0
- package/lib/shared/mcp-proxy-core.mjs +451 -0
- package/lib/shared/pending-queue.mjs +439 -0
- package/lib/shared/profile-inject.mjs +294 -0
- package/lib/shared/recall-core.mjs +302 -0
- package/lib/shared/session-model.mjs +57 -0
- package/lib/shared/setup-wizard.mjs +111 -0
- package/lib/shared/uri-guard.mjs +56 -0
- package/lib/shared/workspace-peer.mjs +16 -0
- package/lib/utils.mjs +34 -117
- package/lib/viking-uri-guard.mjs +9 -24
- package/package.json +8 -7
- package/scripts/setup.mjs +8 -0
- package/servers/mcp-proxy.mjs +69 -0
- package/servers/mcp-proxy.test.mjs +100 -0
- package/lib/code-tools.mjs +0 -134
- package/lib/memadd-local.mjs +0 -118
- package/lib/memory-tools.mjs +0 -410
package/lib/memory-recall.mjs
CHANGED
|
@@ -1,59 +1,34 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const RECALL_STOPWORDS = new Set([
|
|
5
|
-
"what", "when", "where", "which", "who", "whom", "whose", "why", "how",
|
|
6
|
-
"did", "does", "is", "are", "was", "were", "the", "and", "for", "with",
|
|
7
|
-
"from", "that", "this", "your", "you",
|
|
8
|
-
])
|
|
9
|
-
const RECALL_TOKEN_RE = /[a-z0-9]{2,}/gi
|
|
10
|
-
const PREFERENCE_QUERY_RE = /prefer|preference|favorite|favourite|like|偏好|喜欢|爱好|更倾向/i
|
|
11
|
-
const TEMPORAL_QUERY_RE = /when|what time|date|day|month|year|yesterday|today|tomorrow|last|next|什么时候|何时|哪天|几月|几年|昨天|今天|明天|上周|下周|上个月|下个月|去年|明年/i
|
|
1
|
+
import { buildRecallBlock } from "./shared/recall-core.mjs"
|
|
2
|
+
import { isBypassed } from "./shared/session-model.mjs"
|
|
3
|
+
import { effectivePeerId, fetchJSON, log } from "./utils.mjs"
|
|
12
4
|
|
|
13
5
|
export function createMemoryRecall({ config }) {
|
|
14
6
|
async function injectRelevantMemories(input, output) {
|
|
15
7
|
if (!config.autoRecall?.enabled) return
|
|
16
8
|
const query = extractCurrentUserText(output.parts ?? [])
|
|
17
9
|
if (!query) return
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
10
|
+
if (query.length < config.minQueryLength) return
|
|
11
|
+
if (isBypassed(config, {
|
|
12
|
+
sessionId: input.sessionID ?? output.message?.sessionID,
|
|
13
|
+
cwd: input.directory ?? input.cwd,
|
|
14
|
+
})) return
|
|
15
|
+
|
|
16
|
+
const health = await fetchJSON(config, "/health", {}, { timeoutMs: 5000 })
|
|
17
|
+
if (!health.ok) return
|
|
18
|
+
|
|
19
|
+
const block = await buildRecallBlock(
|
|
20
|
+
(path, init = {}, options = {}) => fetchJSON(config, path, init, { ...options, timeoutMs: 5000 }),
|
|
21
|
+
config,
|
|
25
22
|
query,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const processed = postProcessMemories(
|
|
31
|
-
ranked,
|
|
32
|
-
config.autoRecall.maxContentChars,
|
|
33
|
-
config.autoRecall.preferAbstract,
|
|
23
|
+
{
|
|
24
|
+
actorPeerId: effectivePeerId(config),
|
|
25
|
+
log: (stage, data) => log("DEBUG", "recall", stage, data),
|
|
26
|
+
},
|
|
34
27
|
)
|
|
35
|
-
const block = formatMemoryBlock(processed, config.autoRecall.tokenBudget)
|
|
36
28
|
if (!block) return
|
|
37
29
|
|
|
38
30
|
if (prependSyntheticRecallPart(input, output, block)) {
|
|
39
|
-
log("INFO", "recall",
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async function performRecallSearch(query) {
|
|
44
|
-
try {
|
|
45
|
-
const body = { query: query.slice(0, 4000), limit: 20 }
|
|
46
|
-
const response = await makeRequest(config, {
|
|
47
|
-
method: "POST",
|
|
48
|
-
endpoint: "/api/v1/search/find",
|
|
49
|
-
body,
|
|
50
|
-
timeoutMs: AUTO_RECALL_TIMEOUT_MS,
|
|
51
|
-
actorPeerId: effectivePeerId(config),
|
|
52
|
-
})
|
|
53
|
-
const result = unwrapResponse(response)
|
|
54
|
-
return result?.memories ?? result?.results ?? []
|
|
55
|
-
} catch {
|
|
56
|
-
return []
|
|
31
|
+
log("INFO", "recall", "Injected OpenViking context")
|
|
57
32
|
}
|
|
58
33
|
}
|
|
59
34
|
|
|
@@ -64,139 +39,13 @@ function extractCurrentUserText(parts) {
|
|
|
64
39
|
const texts = []
|
|
65
40
|
for (const part of parts) {
|
|
66
41
|
if (part.type !== "text" || typeof part.text !== "string") continue
|
|
67
|
-
if (part.text.includes("<
|
|
42
|
+
if (part.text.includes("<openviking-context")) return null
|
|
68
43
|
if (!part.synthetic && !part.ignored) texts.push(part.text)
|
|
69
44
|
}
|
|
70
45
|
const joined = texts.join(" ").trim()
|
|
71
46
|
return joined || null
|
|
72
47
|
}
|
|
73
48
|
|
|
74
|
-
function buildRecallQueryProfile(query) {
|
|
75
|
-
const text = query.trim()
|
|
76
|
-
const allTokens = text.toLowerCase().match(RECALL_TOKEN_RE) ?? []
|
|
77
|
-
return {
|
|
78
|
-
tokens: allTokens.filter((token) => !RECALL_STOPWORDS.has(token)),
|
|
79
|
-
wantsPreference: PREFERENCE_QUERY_RE.test(text),
|
|
80
|
-
wantsTemporal: TEMPORAL_QUERY_RE.test(text),
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function recallClampScore(value) {
|
|
85
|
-
if (typeof value !== "number" || Number.isNaN(value)) return 0
|
|
86
|
-
return Math.max(0, Math.min(1, value))
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function lexicalOverlapBoost(tokens, text) {
|
|
90
|
-
if (tokens.length === 0 || !text) return 0
|
|
91
|
-
const haystack = ` ${text.toLowerCase()} `
|
|
92
|
-
let matched = 0
|
|
93
|
-
for (const token of tokens.slice(0, 8)) {
|
|
94
|
-
if (haystack.includes(` ${token} `) || haystack.includes(token)) matched += 1
|
|
95
|
-
}
|
|
96
|
-
return Math.min(0.2, (matched / Math.min(tokens.length, 4)) * 0.2)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function isEventMemory(item) {
|
|
100
|
-
const category = (item.category ?? "").toLowerCase()
|
|
101
|
-
return category === "events" || item.uri?.includes("/events/")
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function isPreferencesMemory(item) {
|
|
105
|
-
return item.category === "preferences" || item.uri?.includes("/preferences/") || item.uri?.endsWith("/preferences")
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function isLeafLikeMemory(item) {
|
|
109
|
-
return item.level === 2 || item.is_leaf === true
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function rankForInjection(item, query) {
|
|
113
|
-
const baseScore = recallClampScore(item.score)
|
|
114
|
-
const abstract = (item.abstract ?? item.overview ?? "").trim()
|
|
115
|
-
const leafBoost = isLeafLikeMemory(item) ? 0.12 : 0
|
|
116
|
-
const eventBoost = query.wantsTemporal && isEventMemory(item) ? 0.1 : 0
|
|
117
|
-
const preferenceBoost = query.wantsPreference && isPreferencesMemory(item) ? 0.08 : 0
|
|
118
|
-
const overlapBoost = lexicalOverlapBoost(query.tokens, `${item.uri} ${abstract}`)
|
|
119
|
-
return baseScore + leafBoost + eventBoost + preferenceBoost + overlapBoost
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function normalizeDedupeText(text) {
|
|
123
|
-
return text.toLowerCase().replace(/\s+/g, " ").trim()
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function isEventOrCaseMemory(item) {
|
|
127
|
-
const category = (item.category ?? "").toLowerCase()
|
|
128
|
-
const uri = (item.uri ?? "").toLowerCase()
|
|
129
|
-
return category === "events" || category === "cases" || uri.includes("/events/") || uri.includes("/cases/")
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function getMemoryDedupeKey(item) {
|
|
133
|
-
const abstract = normalizeDedupeText(item.abstract ?? item.overview ?? "")
|
|
134
|
-
const category = (item.category ?? "").toLowerCase() || "unknown"
|
|
135
|
-
if (abstract && !isEventOrCaseMemory(item)) return `abstract:${category}:${abstract}`
|
|
136
|
-
return `uri:${item.uri}`
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function pickMemoriesForInjection(items, limit, queryText, scoreThreshold = 0) {
|
|
140
|
-
const query = buildRecallQueryProfile(queryText)
|
|
141
|
-
const sorted = [...items].sort((a, b) => rankForInjection(b, query) - rankForInjection(a, query))
|
|
142
|
-
const deduped = []
|
|
143
|
-
const seen = new Set()
|
|
144
|
-
|
|
145
|
-
for (const item of sorted) {
|
|
146
|
-
const key = getMemoryDedupeKey(item)
|
|
147
|
-
if (seen.has(key)) continue
|
|
148
|
-
seen.add(key)
|
|
149
|
-
deduped.push(item)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const leaves = deduped.filter((item) => isLeafLikeMemory(item))
|
|
153
|
-
if (leaves.length >= limit) return leaves.slice(0, limit)
|
|
154
|
-
|
|
155
|
-
const picked = [...leaves]
|
|
156
|
-
const used = new Set(leaves.map((item) => item.uri))
|
|
157
|
-
for (const item of deduped) {
|
|
158
|
-
if (picked.length >= limit) break
|
|
159
|
-
if (used.has(item.uri)) continue
|
|
160
|
-
if (recallClampScore(item.score) < scoreThreshold) continue
|
|
161
|
-
picked.push(item)
|
|
162
|
-
}
|
|
163
|
-
return picked
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function postProcessMemories(items, maxContentChars, preferAbstract) {
|
|
167
|
-
return items.map((item) => {
|
|
168
|
-
const abstract = (item.abstract ?? "").trim()
|
|
169
|
-
const content = (item.content ?? "").trim()
|
|
170
|
-
let displayContent = ""
|
|
171
|
-
if (preferAbstract && abstract) displayContent = abstract
|
|
172
|
-
else if (content) displayContent = content
|
|
173
|
-
else if (abstract) displayContent = abstract
|
|
174
|
-
if (displayContent.length > maxContentChars) displayContent = `${displayContent.slice(0, maxContentChars)}...`
|
|
175
|
-
return { ...item, content: displayContent, abstract: abstract || undefined }
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function formatMemoryBlock(items, tokenBudget) {
|
|
180
|
-
if (items.length === 0) return ""
|
|
181
|
-
const maxBlockChars = tokenBudget * 4
|
|
182
|
-
let usedChars = 0
|
|
183
|
-
const lines = ["<relevant-memories>"]
|
|
184
|
-
|
|
185
|
-
for (const item of items) {
|
|
186
|
-
const title = item.title ? `${item.title}\n` : ""
|
|
187
|
-
const content = item.content ?? ""
|
|
188
|
-
const entry = `<memory uri="${item.uri}">\n${title}${content}\n</memory>`
|
|
189
|
-
if (usedChars + entry.length + 1 > maxBlockChars) break
|
|
190
|
-
lines.push(entry)
|
|
191
|
-
usedChars += entry.length + 1
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (usedChars === 0) return ""
|
|
195
|
-
lines.push("</relevant-memories>")
|
|
196
|
-
lines.push('Use `memread` with a memory URI and level="overview" or level="read" for more details.')
|
|
197
|
-
return lines.join("\n")
|
|
198
|
-
}
|
|
199
|
-
|
|
200
49
|
function prependSyntheticRecallPart(input, output, injection) {
|
|
201
50
|
const sessionID = input.sessionID ?? output.message?.sessionID
|
|
202
51
|
const messageID = input.messageID ?? output.message?.id
|