@openviking/opencode-plugin 2026.9.10 → 2026.9.21
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 +42 -28
- package/INSTALL.md +41 -27
- package/README.md +47 -33
- package/index.mjs +5 -9
- package/lib/config.mjs +30 -307
- package/lib/memory-recall.mjs +2 -2
- package/lib/memory-session.mjs +6 -5
- package/lib/repo-context.mjs +2 -3
- package/lib/runtime.mjs +12 -19
- package/lib/shared/capture-utils.mjs +77 -1
- package/lib/shared/config-schema.mjs +359 -0
- package/lib/shared/credentials.mjs +238 -102
- package/lib/shared/mcp-proxy-config.mjs +154 -2
- package/lib/shared/mcp-proxy-core.mjs +23 -15
- package/lib/shared/ov-http.mjs +112 -0
- package/lib/shared/plugin-config.mjs +300 -0
- package/lib/shared/recall-core.mjs +66 -13
- package/lib/shared/uri-guard.mjs +173 -1
- package/lib/shared/workspace-config.mjs +433 -0
- package/lib/shared/workspace-peer.mjs +63 -0
- package/lib/shared/workspace-registry.mjs +87 -0
- package/lib/utils.mjs +34 -177
- package/lib/viking-uri-guard.mjs +23 -8
- package/package.json +4 -3
- package/servers/mcp-proxy.mjs +7 -27
- package/servers/mcp-proxy.test.mjs +0 -102
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// GENERATED FROM examples/memory-plugin-shared/lib. DO NOT EDIT.
|
|
2
|
+
/**
|
|
3
|
+
* The one path from a hook to the OpenViking server.
|
|
4
|
+
*
|
|
5
|
+
* Every harness used to own its own AbortController, header block and envelope
|
|
6
|
+
* parser, and they drifted: one sent the api key twice, one named the operator
|
|
7
|
+
* to every proxy on the path whether or not the server was trusted with it, one
|
|
8
|
+
* read a different field for the same account. A server-side change now lands
|
|
9
|
+
* in one place instead of eight.
|
|
10
|
+
*
|
|
11
|
+
* `X-API-Key` is deliberately absent. The server takes `Authorization: Bearer`,
|
|
12
|
+
* and the identity headers go out only when the resolved config says this
|
|
13
|
+
* server is trusted with the operator's name.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// A request that aborts the instant it starts reads as a dead server. A config
|
|
17
|
+
// that asks for less than a second gets a second.
|
|
18
|
+
const MIN_TIMEOUT_MS = 1000;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The headers every harness puts on every OpenViking request.
|
|
22
|
+
*
|
|
23
|
+
* `identityHeaders` overrides `cfg.sendIdentityHeaders` for the callers that
|
|
24
|
+
* decide per request rather than per config — the doctor probes both ways.
|
|
25
|
+
*/
|
|
26
|
+
export function buildOvHeaders(cfg = {}, { actorPeerId = "", identityHeaders, extraHeaders } = {}) {
|
|
27
|
+
const identity = identityHeaders === undefined ? Boolean(cfg.sendIdentityHeaders) : Boolean(identityHeaders);
|
|
28
|
+
const headers = { "Content-Type": "application/json" };
|
|
29
|
+
if (cfg.apiKey) headers["Authorization"] = `Bearer ${cfg.apiKey}`;
|
|
30
|
+
if (identity && cfg.account) headers["X-OpenViking-Account"] = cfg.account;
|
|
31
|
+
if (identity && cfg.user) headers["X-OpenViking-User"] = cfg.user;
|
|
32
|
+
if (actorPeerId) headers["X-OpenViking-Actor-Peer"] = actorPeerId;
|
|
33
|
+
if (cfg.userAgent) headers["User-Agent"] = cfg.userAgent;
|
|
34
|
+
return { ...headers, ...extraHeaders };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function responseTraceId(body) {
|
|
38
|
+
return body?.result?.trace_id || body?.error?.trace_id || body?.trace_id || undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Build the `fetchJSON(path, init, { timeoutMs, actorPeerId })` a harness talks
|
|
43
|
+
* through. It resolves to `{ ok, status, result, error, traceId }` and never
|
|
44
|
+
* throws: a hook that crashes on a closed port would take its host's turn with
|
|
45
|
+
* it, so a network error is `{ ok: false, status: 0 }` and the retry rules in
|
|
46
|
+
* `retryable.mjs` decide what happens next.
|
|
47
|
+
*
|
|
48
|
+
* `resolveActorPeerId` is a callback because Codex only knows its peer after
|
|
49
|
+
* loading session state under the lock, well after the client is built.
|
|
50
|
+
* `requireJsonBody` is for the capture hooks, which would rather retry a body
|
|
51
|
+
* they cannot parse than record the turn as sent.
|
|
52
|
+
*/
|
|
53
|
+
export function createOvHttp(cfg = {}, {
|
|
54
|
+
defaultTimeoutMs = 0,
|
|
55
|
+
resolveActorPeerId = () => "",
|
|
56
|
+
identityHeaders,
|
|
57
|
+
extraHeaders,
|
|
58
|
+
requireJsonBody = false,
|
|
59
|
+
} = {}) {
|
|
60
|
+
// dsh and pi name this field `endpoint`; every other harness names it `baseUrl`.
|
|
61
|
+
const baseUrl = cfg.baseUrl || cfg.endpoint || "";
|
|
62
|
+
return async function fetchJSON(path, init = {}, options = {}) {
|
|
63
|
+
const timeoutMs = Math.max(MIN_TIMEOUT_MS, Number(options.timeoutMs) || Number(defaultTimeoutMs) || 0);
|
|
64
|
+
const controller = new AbortController();
|
|
65
|
+
let timedOut = false;
|
|
66
|
+
const timer = setTimeout(() => {
|
|
67
|
+
timedOut = true;
|
|
68
|
+
controller.abort();
|
|
69
|
+
}, timeoutMs);
|
|
70
|
+
try {
|
|
71
|
+
const headers = {
|
|
72
|
+
...buildOvHeaders(cfg, {
|
|
73
|
+
actorPeerId: options.actorPeerId ?? resolveActorPeerId(),
|
|
74
|
+
identityHeaders,
|
|
75
|
+
extraHeaders,
|
|
76
|
+
}),
|
|
77
|
+
...(init.headers || {}),
|
|
78
|
+
};
|
|
79
|
+
const response = await fetch(`${baseUrl}${path}`, { ...init, headers, signal: controller.signal });
|
|
80
|
+
const body = await response.json().catch(() => null);
|
|
81
|
+
if (requireJsonBody && !body) {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
status: response.status,
|
|
85
|
+
result: null,
|
|
86
|
+
error: { message: "empty or invalid JSON response" },
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const traceId = responseTraceId(body);
|
|
90
|
+
if (!response.ok || body?.status === "error") {
|
|
91
|
+
return {
|
|
92
|
+
ok: false,
|
|
93
|
+
status: response.status,
|
|
94
|
+
result: null,
|
|
95
|
+
error: body?.error || { message: `HTTP ${response.status}` },
|
|
96
|
+
traceId,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return { ok: true, status: response.status, result: body?.result ?? body ?? {}, traceId };
|
|
100
|
+
} catch (error) {
|
|
101
|
+
const message = error?.message || String(error);
|
|
102
|
+
// This module owns the controller, so it says a timeout was a timeout
|
|
103
|
+
// rather than leaving callers to guess it from whatever fetch threw.
|
|
104
|
+
const failure = timedOut
|
|
105
|
+
? { name: "AbortError", aborted: true, message }
|
|
106
|
+
: { message };
|
|
107
|
+
return { ok: false, status: 0, result: null, error: failure };
|
|
108
|
+
} finally {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// GENERATED FROM examples/memory-plugin-shared/lib. DO NOT EDIT.
|
|
2
|
+
/**
|
|
3
|
+
* Per-harness plugin settings from ovcli.conf.
|
|
4
|
+
*
|
|
5
|
+
* ovcli.conf carried connection fields only, so every harness had to keep its
|
|
6
|
+
* tuning knobs in ov.conf's harness section — a server-side file that a
|
|
7
|
+
* client-side plugin has no business editing. The `plugin` section fixes that:
|
|
8
|
+
* shared keys apply to every harness that reads them, and a per-harness object
|
|
9
|
+
* overrides them.
|
|
10
|
+
*
|
|
11
|
+
* {
|
|
12
|
+
* "url": "...", "api_key": "...",
|
|
13
|
+
* "plugin": {
|
|
14
|
+
* "recallQueryExpansion": "off",
|
|
15
|
+
* "recallCompress": "auto"
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* Resolution stays env → ovcli.conf plugin.<harness> → ovcli.conf plugin →
|
|
20
|
+
* ov.conf harness section (legacy) → defaults.
|
|
21
|
+
*
|
|
22
|
+
* Every harness resolves through `resolveSettings()` below, and assembles its
|
|
23
|
+
* whole configuration through `buildPluginConfig()`, so one `plugin` section
|
|
24
|
+
* configures all of them and `ov config switch` moves behaviour along with
|
|
25
|
+
* credentials. The knobs themselves are declared once in `config-schema.mjs`;
|
|
26
|
+
* this module decides which files supply them and what is derived from them.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { homedir } from "node:os";
|
|
30
|
+
import { join } from "node:path";
|
|
31
|
+
|
|
32
|
+
import { HARNESS_KEYS, KNOBS, harnessKey, resolveKnobs } from "./config-schema.mjs";
|
|
33
|
+
import {
|
|
34
|
+
buildUserAgent,
|
|
35
|
+
loadCredentialFiles,
|
|
36
|
+
readManifestVersion,
|
|
37
|
+
resolveConnection,
|
|
38
|
+
} from "./credentials.mjs";
|
|
39
|
+
import {
|
|
40
|
+
announcedOverrides,
|
|
41
|
+
loadWorkspaceLayers,
|
|
42
|
+
mergeConfigLayers,
|
|
43
|
+
normalizeWorkspaceConfig,
|
|
44
|
+
projectWorkspaceSettings,
|
|
45
|
+
} from "./workspace-config.mjs";
|
|
46
|
+
import { findWorkspaceRoot, resolveWorkspaceIdentity } from "./workspace-identity.mjs";
|
|
47
|
+
import { resolveEffectivePeerId, resolvePluginPeerId } from "./workspace-peer.mjs";
|
|
48
|
+
import { readEntry } from "./workspace-registry.mjs";
|
|
49
|
+
|
|
50
|
+
const OVCLI_LAYER = "ovcli.conf";
|
|
51
|
+
const OV_CONF_LAYER = "ov.conf";
|
|
52
|
+
|
|
53
|
+
export { HARNESS_KEYS, harnessKey };
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Merge the shared plugin section with the harness-specific override.
|
|
57
|
+
* Returns a flat settings object; unknown keys pass through untouched so a
|
|
58
|
+
* harness can add its own knobs without touching this module.
|
|
59
|
+
*
|
|
60
|
+
* `cliFile` is ovcli.conf already parsed. Callers that have it hand it over so
|
|
61
|
+
* the file is read once per build and, more importantly, so the `plugin`
|
|
62
|
+
* section comes out of the same file the credential chain read — an
|
|
63
|
+
* `OPENVIKING_CONFIG_FILE` that is ovcli-shaped is ovcli.conf, and only
|
|
64
|
+
* `loadCredentialFiles` knows that.
|
|
65
|
+
*/
|
|
66
|
+
export function loadPluginSettings(harness, env = process.env, options = {}) {
|
|
67
|
+
const file = options.cliFile || loadCredentialFiles(env).cliFile;
|
|
68
|
+
const plugin = file && typeof file.plugin === "object" && file.plugin ? file.plugin : {};
|
|
69
|
+
|
|
70
|
+
const shared = {};
|
|
71
|
+
for (const [key, value] of Object.entries(plugin)) {
|
|
72
|
+
if (value && typeof value === "object" && !Array.isArray(value)) continue;
|
|
73
|
+
shared[key] = value;
|
|
74
|
+
}
|
|
75
|
+
// Both spellings of the harness name find the same override, so copying
|
|
76
|
+
// `claude-code` out of the installer is not a silent no-op.
|
|
77
|
+
const key = harnessKey(harness);
|
|
78
|
+
const scoped = {};
|
|
79
|
+
for (const candidate of [key, key.replace(/_/g, "-")]) {
|
|
80
|
+
const value = candidate && plugin[candidate];
|
|
81
|
+
if (value && typeof value === "object" && !Array.isArray(value)) Object.assign(scoped, value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const settings = { ...shared, ...scoped };
|
|
85
|
+
const cwd = String(options.cwd || "").trim();
|
|
86
|
+
if (!cwd) return settings;
|
|
87
|
+
return { ...settings, ...resolveWorkspaceSettings(cwd, env, options).settings };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Every knob for one harness, resolved through the whole layer stack.
|
|
92
|
+
*
|
|
93
|
+
* `legacy` is that harness's section in ov.conf, which is where tuning used to
|
|
94
|
+
* live: it stays the lowest configured layer so an existing deployment keeps
|
|
95
|
+
* working, and everything above it comes from files a client may edit.
|
|
96
|
+
*
|
|
97
|
+
* Returns the settings, the names some layer actually supplied (what the
|
|
98
|
+
* `*Configured` flags report), and the raw ovcli.conf merge — a couple of
|
|
99
|
+
* callers still need to know a value came from ovcli.conf rather than ov.conf.
|
|
100
|
+
*/
|
|
101
|
+
export function resolveSettings(harness, options = {}) {
|
|
102
|
+
const { env = process.env, cwd = "", legacy = {}, clientVersion = "", cliFile = null } = options;
|
|
103
|
+
const key = harnessKey(harness);
|
|
104
|
+
const plugin = loadPluginSettings(key, env, { cwd, clientVersion, cliFile });
|
|
105
|
+
const { settings, configured, sources } = resolveKnobs({
|
|
106
|
+
harness: key,
|
|
107
|
+
layers: [
|
|
108
|
+
{ name: OV_CONF_LAYER, data: legacy },
|
|
109
|
+
{ name: OVCLI_LAYER, data: plugin },
|
|
110
|
+
],
|
|
111
|
+
env,
|
|
112
|
+
});
|
|
113
|
+
return { settings, configured, sources, plugin };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The workspace layers for one cwd, flattened into harness knobs.
|
|
118
|
+
*
|
|
119
|
+
* Takes the cwd explicitly because a hook's `loadConfig()` runs at module top
|
|
120
|
+
* level, before the payload on stdin has said which directory the session is
|
|
121
|
+
* actually in — the caller resolves this again once it knows.
|
|
122
|
+
*/
|
|
123
|
+
export function resolveWorkspaceSettings(cwd, env = process.env, { clientVersion = "" } = {}) {
|
|
124
|
+
const empty = { settings: {}, root: "", provenance: {}, warnings: [], announced: [] };
|
|
125
|
+
try {
|
|
126
|
+
const { root } = findWorkspaceRoot(cwd, env);
|
|
127
|
+
if (!root) return empty;
|
|
128
|
+
|
|
129
|
+
const { layers, warnings } = loadWorkspaceLayers(root, { clientVersion });
|
|
130
|
+
// The identity is what makes the registry's negative evidence work: without
|
|
131
|
+
// it a directory reused by a different repository inherits the old peer.
|
|
132
|
+
const identity = resolveWorkspaceIdentity({ cwd, env });
|
|
133
|
+
const registry = readEntry(root, { identity, env });
|
|
134
|
+
warnings.push(...registry.warnings);
|
|
135
|
+
if (registry.entry?.settings) layers.push({ layer: "registry", data: registry.entry.settings });
|
|
136
|
+
if (registry.entry?.peer) layers.push({ layer: "registry", data: { peer: registry.entry.peer } });
|
|
137
|
+
if (!layers.length) return { ...empty, root, warnings };
|
|
138
|
+
|
|
139
|
+
const { value, provenance } = mergeConfigLayers(layers, warnings);
|
|
140
|
+
normalizeWorkspaceConfig(value, warnings);
|
|
141
|
+
return {
|
|
142
|
+
settings: projectWorkspaceSettings(value),
|
|
143
|
+
root,
|
|
144
|
+
provenance,
|
|
145
|
+
warnings,
|
|
146
|
+
announced: announcedOverrides(provenance),
|
|
147
|
+
value,
|
|
148
|
+
};
|
|
149
|
+
} catch {
|
|
150
|
+
// A hook must never die over a config file. An unreadable layer is no layer.
|
|
151
|
+
return empty;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const SEND_ONLY_WHEN_CONFIGURED = KNOBS.filter((knob) => knob.sendOnlyWhenConfigured);
|
|
156
|
+
|
|
157
|
+
function str(value) {
|
|
158
|
+
return typeof value === "string" && value.trim() ? value.trim() : "";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The ov.conf block named after this harness, the lowest layer of every chain. */
|
|
162
|
+
function ovConfSection(ovFile, key) {
|
|
163
|
+
const section = key ? ovFile?.[key] : null;
|
|
164
|
+
return section && typeof section === "object" && !Array.isArray(section) ? section : {};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* One harness's whole configuration: the files, the knobs, the peer, and the
|
|
169
|
+
* handful of fields derived from them.
|
|
170
|
+
*
|
|
171
|
+
* Every loader used to write this sequence out by hand and each wrote a
|
|
172
|
+
* slightly different subset, which is how ov.conf's `<harness>.authMode` came
|
|
173
|
+
* to apply on three harnesses only, and why two of the four knobs the server
|
|
174
|
+
* defaults for itself were reported as configured on two harnesses and nowhere
|
|
175
|
+
* else. What is left in a loader after this call is what only that harness
|
|
176
|
+
* knows.
|
|
177
|
+
*
|
|
178
|
+
* The connection — server, key, identity, auth mode — is not a knob: it comes
|
|
179
|
+
* from `resolveConnection`, the same call an MCP proxy's connection comes
|
|
180
|
+
* from, and the `plugin` keys and ov.conf block that name it are read there.
|
|
181
|
+
*
|
|
182
|
+
* `legacy` replaces the ov.conf block this harness would otherwise get for its
|
|
183
|
+
* knobs (dsh's host hands it one of its own); `hostInput` is what an embedding
|
|
184
|
+
* host named for this process, which is the more specific answer and outranks
|
|
185
|
+
* every file; `logFile` is the log basename this harness has always used, and
|
|
186
|
+
* a harness that never had a default log path does not get one invented for
|
|
187
|
+
* it; `rootKeyFallback` is for the harnesses whose chain predates the ovcli.conf
|
|
188
|
+
* pinning and has always ended at `server.root_api_key`.
|
|
189
|
+
*/
|
|
190
|
+
export function buildPluginConfig(harness, {
|
|
191
|
+
cwd = "",
|
|
192
|
+
env = process.env,
|
|
193
|
+
legacy = null,
|
|
194
|
+
manifestUrl = "",
|
|
195
|
+
version = "",
|
|
196
|
+
hostInput = {},
|
|
197
|
+
logFile = "",
|
|
198
|
+
rootKeyFallback = false,
|
|
199
|
+
deriveEffectivePeer = false,
|
|
200
|
+
} = {}) {
|
|
201
|
+
const name = String(harness || "");
|
|
202
|
+
const key = harnessKey(name);
|
|
203
|
+
const workspaceCwd = str(cwd) || process.cwd();
|
|
204
|
+
const files = loadCredentialFiles(env);
|
|
205
|
+
// The version answers two questions: what the User-Agent reports, and whether
|
|
206
|
+
// a workspace file asking for a newer client gets to say so.
|
|
207
|
+
const clientVersion = str(version) || (manifestUrl ? readManifestVersion(manifestUrl) : "");
|
|
208
|
+
const { settings, configured, sources } = resolveSettings(key, {
|
|
209
|
+
env,
|
|
210
|
+
cwd: workspaceCwd,
|
|
211
|
+
legacy: legacy || ovConfSection(files.ovFile, key),
|
|
212
|
+
cliFile: files.cliFile,
|
|
213
|
+
clientVersion,
|
|
214
|
+
});
|
|
215
|
+
const connection = resolveConnection(key, { env, files, hostInput, rootKeyFallback });
|
|
216
|
+
const { baseUrl, account, user } = connection;
|
|
217
|
+
const timeoutMs = settings.timeoutMs;
|
|
218
|
+
const peerId = resolvePluginPeerId({
|
|
219
|
+
settings,
|
|
220
|
+
configured,
|
|
221
|
+
sources,
|
|
222
|
+
credentials: connection,
|
|
223
|
+
hostInput: hostInput?.peerId,
|
|
224
|
+
env,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const config = {
|
|
228
|
+
...settings,
|
|
229
|
+
harness: name,
|
|
230
|
+
clientVersion,
|
|
231
|
+
userAgent: buildUserAgent(name, clientVersion),
|
|
232
|
+
|
|
233
|
+
baseUrl,
|
|
234
|
+
mcpUrl: connection.mcpUrl,
|
|
235
|
+
apiKey: connection.apiKey,
|
|
236
|
+
account,
|
|
237
|
+
user,
|
|
238
|
+
authMode: connection.authMode,
|
|
239
|
+
sendIdentityHeaders: connection.sendIdentityHeaders,
|
|
240
|
+
|
|
241
|
+
credentialSource: connection.credentialSource,
|
|
242
|
+
apiKeySource: connection.apiKeySource,
|
|
243
|
+
credentialPath: connection.credentialPath,
|
|
244
|
+
hasApiKey: connection.hasApiKey,
|
|
245
|
+
configPath: connection.cliPath || connection.ovPath || null,
|
|
246
|
+
cliConfigPath: connection.cliPath,
|
|
247
|
+
ovConfigPath: connection.ovPath,
|
|
248
|
+
cliPath: connection.cliPath,
|
|
249
|
+
ovPath: connection.ovPath,
|
|
250
|
+
|
|
251
|
+
peerId,
|
|
252
|
+
explicitPeerId: peerId,
|
|
253
|
+
|
|
254
|
+
// A write gets a longer budget than a read, and a digest has to finish
|
|
255
|
+
// inside the recall request it belongs to, so both fall back to a knob.
|
|
256
|
+
captureTimeoutMs: settings.captureTimeoutMs || Math.max(timeoutMs * 2, 30000),
|
|
257
|
+
recallCompressTimeoutMs: settings.recallCompressTimeoutMs
|
|
258
|
+
|| Math.max(1000, settings.recallTimeoutMs - 10000),
|
|
259
|
+
debugLogPath: settings.debugLogPath
|
|
260
|
+
|| (logFile ? join(homedir(), ".openviking", "logs", logFile) : ""),
|
|
261
|
+
|
|
262
|
+
// The spellings these fields carried before every harness shared a loader.
|
|
263
|
+
endpoint: baseUrl,
|
|
264
|
+
accountId: account,
|
|
265
|
+
userId: user,
|
|
266
|
+
requestTimeoutMs: timeoutMs,
|
|
267
|
+
};
|
|
268
|
+
for (const knob of SEND_ONLY_WHEN_CONFIGURED) {
|
|
269
|
+
config[`${knob.name}Configured`] = configured.has(knob.name);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Only for the harnesses that resolve the peer once at load. The rest derive
|
|
273
|
+
// it per session, against the directory the payload names, and an MCP proxy
|
|
274
|
+
// must not derive one from wherever it happened to be launched.
|
|
275
|
+
if (deriveEffectivePeer) {
|
|
276
|
+
const effectivePeer = resolveEffectivePeerId({ cfg: config, cwd: workspaceCwd, env });
|
|
277
|
+
config.effectivePeer = effectivePeer;
|
|
278
|
+
config.legacyPeerId = effectivePeer.legacyPeerId;
|
|
279
|
+
}
|
|
280
|
+
return config;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const REWRITE_MODES = new Set(["off", "client", "server", "auto"]);
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Normalize the tri-state rewrite knob.
|
|
287
|
+
*
|
|
288
|
+
* off — never compress
|
|
289
|
+
* client — always compress locally through the host CLI
|
|
290
|
+
* server — always ask the server for a digest
|
|
291
|
+
* auto — prefer local (cost lands on the user's own subscription), fall back
|
|
292
|
+
* to the server when no healthy host CLI is available
|
|
293
|
+
*/
|
|
294
|
+
export function normalizeRewriteMode(value, fallback = "off") {
|
|
295
|
+
const raw = String(value ?? "").trim().toLowerCase();
|
|
296
|
+
if (REWRITE_MODES.has(raw)) return raw;
|
|
297
|
+
if (raw === "1" || raw === "true" || raw === "yes") return "auto";
|
|
298
|
+
if (raw === "0" || raw === "false" || raw === "no") return "off";
|
|
299
|
+
return fallback;
|
|
300
|
+
}
|
|
@@ -31,6 +31,28 @@ const CODING_QUOTA_WEIGHTS = {
|
|
|
31
31
|
|
|
32
32
|
let userSpaceCache = "";
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Is recall on for this config?
|
|
36
|
+
*
|
|
37
|
+
* The switch has four spellings in the wild: a boolean `autoRecall`, opencode's
|
|
38
|
+
* `{ enabled }` object, dsh and pi's `syncTurns`, and the global `enabled`
|
|
39
|
+
* that turns the whole plugin off. Reading it here rather than in each loader
|
|
40
|
+
* is what keeps it from drifting a fifth time — and any spelling that says off
|
|
41
|
+
* wins, so a config that disables recall under an older name still disables it.
|
|
42
|
+
*/
|
|
43
|
+
export function isRecallEnabled(cfg = {}) {
|
|
44
|
+
return isSwitchOn([cfg.enabled, cfg.autoRecall, cfg.recall, cfg.syncRecall]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Any recognised spelling that says off wins; everything else means on. */
|
|
48
|
+
function isSwitchOn(values) {
|
|
49
|
+
for (const value of values) {
|
|
50
|
+
if (value === false) return false;
|
|
51
|
+
if (value && typeof value === "object" && !Array.isArray(value) && value.enabled === false) return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
export function estimateTokens(text) {
|
|
35
57
|
return text ? Math.ceil(String(text).length / 4) : 0;
|
|
36
58
|
}
|
|
@@ -338,9 +360,8 @@ async function resolveItemContent(fetchJSON, item, cfg, actorPeerId = "") {
|
|
|
338
360
|
}
|
|
339
361
|
|
|
340
362
|
async function buildFallbackInjectionBlock(fetchJSON, items, cfg, actorPeerId = "", log = () => {}) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
let budgetRemaining = Math.max(200, Number(cfg.recallTokenBudget || 2000));
|
|
363
|
+
const budgetTotal = Math.max(200, Number(cfg.recallTokenBudget || 2000));
|
|
364
|
+
let budgetRemaining = budgetTotal;
|
|
344
365
|
const lines = [
|
|
345
366
|
"<openviking-context>",
|
|
346
367
|
"Relevant context from OpenViking. Use the read MCP tool to expand URIs.",
|
|
@@ -373,15 +394,15 @@ async function buildFallbackInjectionBlock(fetchJSON, items, cfg, actorPeerId =
|
|
|
373
394
|
|
|
374
395
|
lines.push("</openviking-context>");
|
|
375
396
|
|
|
376
|
-
const budgetUsed =
|
|
397
|
+
const budgetUsed = budgetTotal - budgetRemaining;
|
|
377
398
|
log("recall_injection_built", {
|
|
378
399
|
contentItems: contentCount,
|
|
379
400
|
hintItems: hintCount,
|
|
380
401
|
budgetUsed,
|
|
381
|
-
budgetTotal
|
|
402
|
+
budgetTotal,
|
|
382
403
|
});
|
|
383
404
|
|
|
384
|
-
return lines.join("\n");
|
|
405
|
+
return { block: lines.join("\n"), contentCount, hintCount, budgetUsed, stage: "ranked" };
|
|
385
406
|
}
|
|
386
407
|
|
|
387
408
|
const LEGACY_CACHE_TTL_MS = 6 * 60 * 60 * 1000;
|
|
@@ -626,8 +647,13 @@ export async function postRecall(fetchJSON, body, opts = {}) {
|
|
|
626
647
|
* `"actor"` that sweep is off by definition, so the old peer is asked for
|
|
627
648
|
* separately — as itself, which is both cheaper and wider than a bare
|
|
628
649
|
* cross-peer read (that would need the user id, and reaches memories only).
|
|
650
|
+
*
|
|
651
|
+
* `stage` names the path the block came from, which is what a host needs to
|
|
652
|
+
* tell "the server had nothing" (`no_results`) from "the score threshold
|
|
653
|
+
* dropped everything" (`filtered_out`) when the block is empty, and a
|
|
654
|
+
* `server_assembled` block from a `ranked` one when it is not.
|
|
629
655
|
*/
|
|
630
|
-
export async function
|
|
656
|
+
export async function buildRecallBlockDetailed(fetchJSON, cfg, query, options = {}) {
|
|
631
657
|
const primary = await recallForPeer(fetchJSON, cfg, query, options);
|
|
632
658
|
|
|
633
659
|
const legacyPeerId = String(options.legacyPeerId || "").trim();
|
|
@@ -636,16 +662,32 @@ export async function buildRecallBlock(fetchJSON, cfg, query, options = {}) {
|
|
|
636
662
|
|
|
637
663
|
const log = options.log || (() => {});
|
|
638
664
|
const legacy = await recallForPeer(fetchJSON, cfg, query, { ...options, actorPeerId: legacyPeerId });
|
|
639
|
-
if (!legacy) return primary;
|
|
665
|
+
if (!legacy.block) return primary;
|
|
640
666
|
log("recall_legacy_peer_hit", { legacyPeerId });
|
|
641
|
-
return
|
|
667
|
+
return {
|
|
668
|
+
block: primary.block ? `${primary.block}\n${legacy.block}` : legacy.block,
|
|
669
|
+
contentCount: primary.contentCount + legacy.contentCount,
|
|
670
|
+
hintCount: primary.hintCount + legacy.hintCount,
|
|
671
|
+
budgetUsed: primary.budgetUsed + legacy.budgetUsed,
|
|
672
|
+
stage: primary.block ? primary.stage : legacy.stage,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** The block alone, or null when nothing was injectable. */
|
|
677
|
+
export async function buildRecallBlock(fetchJSON, cfg, query, options = {}) {
|
|
678
|
+
const { block } = await buildRecallBlockDetailed(fetchJSON, cfg, query, options);
|
|
679
|
+
return block || null;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function emptyRecall(stage) {
|
|
683
|
+
return { block: "", contentCount: 0, hintCount: 0, budgetUsed: 0, stage };
|
|
642
684
|
}
|
|
643
685
|
|
|
644
686
|
async function recallForPeer(fetchJSON, cfg, query, options = {}) {
|
|
645
687
|
const actorPeerId = options.actorPeerId ?? cfg.peerId ?? "";
|
|
646
688
|
const log = options.log || (() => {});
|
|
647
689
|
const trimmed = String(query || "").trim();
|
|
648
|
-
if (!trimmed) return
|
|
690
|
+
if (!trimmed) return emptyRecall("no_results");
|
|
649
691
|
|
|
650
692
|
// Assembly happens server-side when the deployment offers the context face;
|
|
651
693
|
// older servers fall through to /recall, then to raw find.
|
|
@@ -654,12 +696,23 @@ async function recallForPeer(fetchJSON, cfg, query, options = {}) {
|
|
|
654
696
|
actorPeerId,
|
|
655
697
|
log,
|
|
656
698
|
});
|
|
657
|
-
if (serverBlock !== null)
|
|
699
|
+
if (serverBlock !== null) {
|
|
700
|
+
if (!serverBlock) return emptyRecall("no_results");
|
|
701
|
+
// The server rendered one budgeted unit, so nothing here degraded to a
|
|
702
|
+
// URI-only hint and the block's own estimate is what it cost.
|
|
703
|
+
return {
|
|
704
|
+
block: serverBlock,
|
|
705
|
+
contentCount: 1,
|
|
706
|
+
hintCount: 0,
|
|
707
|
+
budgetUsed: estimateTokens(serverBlock),
|
|
708
|
+
stage: "server_assembled",
|
|
709
|
+
};
|
|
710
|
+
}
|
|
658
711
|
|
|
659
712
|
const recallLimit = Math.max(1, Number(cfg.recallLimit || DEFAULT_CONTEXT_LIMIT));
|
|
660
713
|
const perSourceLimit = Math.max(recallLimit * 2, 8);
|
|
661
714
|
const raw = await searchAllSources(fetchJSON, trimmed, perSourceLimit, actorPeerId, log);
|
|
662
|
-
if (raw.length === 0) return
|
|
715
|
+
if (raw.length === 0) return emptyRecall("no_results");
|
|
663
716
|
|
|
664
717
|
const profile = buildQueryProfile(trimmed);
|
|
665
718
|
const scoreThreshold = Number.isFinite(Number(cfg.scoreThreshold)) ? Number(cfg.scoreThreshold) : 0.35;
|
|
@@ -673,6 +726,6 @@ async function recallForPeer(fetchJSON, cfg, query, options = {}) {
|
|
|
673
726
|
items: picked.map((it) => ({ type: it._sourceType, uri: it.uri, score: clampScore(it.score) })),
|
|
674
727
|
});
|
|
675
728
|
|
|
676
|
-
if (picked.length === 0) return
|
|
729
|
+
if (picked.length === 0) return emptyRecall("filtered_out");
|
|
677
730
|
return buildFallbackInjectionBlock(fetchJSON, picked, cfg, actorPeerId, log);
|
|
678
731
|
}
|