Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions examples/openclaw-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ function totalCommitMemories(r: CommitSessionResult): number {
return Object.values(m).reduce((sum, n) => sum + (n ?? 0), 0);
}

let _registered = false;

const contextEnginePlugin = {
id: "openviking",
name: "Context Engine (OpenViking)",
Expand All @@ -270,6 +272,17 @@ const contextEnginePlugin = {
configSchema: memoryOpenVikingConfigSchema,

register(api: OpenClawPluginApi) {
// Idempotency guard: when OpenClaw reloads the plugin with a different cacheKey,
// module-level state (localClientCache, localClientPendingPromises) persists from
// the previous load. Reset stale state so the second registration works cleanly.
// See: https://github.com/volcengine/OpenViking/issues/1210
if (_registered) {
api.logger.info("openviking: plugin re-loaded, resetting stale state");
localClientCache.clear();
localClientPendingPromises.clear();
}
_registered = true;

const rawCfg =
api.pluginConfig && typeof api.pluginConfig === "object" && !Array.isArray(api.pluginConfig)
? (api.pluginConfig as Record<string, unknown>)
Expand Down Expand Up @@ -1215,7 +1228,15 @@ const contextEnginePlugin = {
});
try {
await waitForHealthOrExit(baseUrl, timeoutMs, intervalMs, child);
const client = new OpenVikingClient(baseUrl, cfg.apiKey, cfg.agentId, cfg.timeoutMs);
const client = new OpenVikingClient(
baseUrl,
cfg.apiKey,
cfg.agentId,
cfg.timeoutMs,
tenantAccount,
tenantUser,
routingDebugLog,
);
localClientCache.set(localCacheKey, { client, process: child });
if (resolveLocalClient) {
resolveLocalClient(client);
Expand Down Expand Up @@ -1360,4 +1381,4 @@ export async function buildMemoryLinesWithBudget(
return { lines, estimatedTokens: totalTokens };
}

export default contextEnginePlugin;
export default contextEnginePlugin;
Loading