Agnes 2.0-flash是推理模型,推理token占大量预算。 800仍不够→JSON截断(parse error: unexpected end of JSON input)。 1200确保推理+正文都够用。
206 lines
8.4 KiB
JavaScript
206 lines
8.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ZhiYiClient = exports.pluginManifest = void 0;
|
|
/**
|
|
* memory-zhiyi — ZhiYi MemoryWeave Plugin for OpenClaw
|
|
*
|
|
* v0.2.0 improvements:
|
|
* - Fixed commit payload (agent_id + namespace as top-level fields)
|
|
* - Added feedback (useful/not-useful) for self-optimization engine
|
|
* - Added prefetch (auto recall context before turns)
|
|
* - Added forget (soft-delete via /api/v1/admin/forget)
|
|
* - Added dedup (check existing before commit)
|
|
* - Added stats endpoint
|
|
* - Better error logging + retry for 429 rate limits
|
|
*/
|
|
const client_1 = require("./client");
|
|
Object.defineProperty(exports, "ZhiYiClient", { enumerable: true, get: function () { return client_1.ZhiYiClient; } });
|
|
exports.pluginManifest = {
|
|
id: 'memory-zhiyi',
|
|
name: 'Memory (ZhiYi)',
|
|
description: 'ZhiYi MemoryWeave — semantic memory with knowledge graph, feedback loop, and prefetch',
|
|
kind: 'memory',
|
|
version: '0.2.0',
|
|
};
|
|
let _client = null;
|
|
let _config = null;
|
|
function loadConfig(api) {
|
|
const raw = api.config?.zhiyi || api.config?.memory_zhiyi || {};
|
|
return {
|
|
baseUrl: raw.base_url || process.env.ZHIYI_BASE_URL || 'http://localhost:7821',
|
|
apiKey: raw.api_key || process.env.ZHIYI_API_KEY || 'zhiyi-dev-key-2026',
|
|
namespace: raw.namespace || 'openclaw-main',
|
|
agentId: raw.agent_id || 'openclaw',
|
|
timeout: raw.timeout || 10000,
|
|
prefetchEnabled: raw.prefetch_enabled !== false,
|
|
};
|
|
}
|
|
const plugin = {
|
|
id: exports.pluginManifest.id,
|
|
name: exports.pluginManifest.name,
|
|
description: exports.pluginManifest.description,
|
|
kind: exports.pluginManifest.kind,
|
|
register(api) {
|
|
_config = loadConfig(api);
|
|
_client = new client_1.ZhiYiClient(_config);
|
|
// ── Health check via service start (async, doesn't block register) ─
|
|
api.registerService({
|
|
id: 'memory-zhiyi',
|
|
start: async () => {
|
|
const ok = await _client.health();
|
|
if (!ok) {
|
|
api.logger?.error('[memory-zhiyi] ZhiYi unreachable — check ZHIYI_BASE_URL');
|
|
}
|
|
else {
|
|
api.logger?.info(`[memory-zhiyi] v0.2.0 connected — ns=${_config.namespace}`);
|
|
}
|
|
},
|
|
stop: () => {
|
|
_client?.clearCache();
|
|
api.logger?.info('[memory-zhiyi] stopped');
|
|
},
|
|
});
|
|
api.logger?.info(`[memory-zhiyi] v0.2.0 registered — ns=${_config.namespace} agent=${_config.agentId}`);
|
|
// ── Hooks ─────────────────────────────────────────────
|
|
// memory:recall — semantic search
|
|
api.on('memory:recall', async (params) => {
|
|
if (!_client)
|
|
return [];
|
|
return await _client.recall(params.query, params.topK ?? 5);
|
|
});
|
|
// memory:commit — store new memory
|
|
api.on('memory:commit', async (params) => {
|
|
if (!_client)
|
|
return null;
|
|
const result = await _client.commit(params.content, params.category ?? 'episodes', params.metadata);
|
|
return result;
|
|
});
|
|
// memory:prefetch — lightweight recall before conversation turns
|
|
api.on('memory:prefetch', async (params) => {
|
|
if (!_client || !_config?.prefetchEnabled)
|
|
return [];
|
|
return await _client.prefetch(params.context);
|
|
});
|
|
// memory:feedback:useful — mark memory as useful
|
|
api.on('memory:feedback:useful', async (params) => {
|
|
if (!_client)
|
|
return { success: false };
|
|
return await _client.markUseful(params.memoryId);
|
|
});
|
|
// memory:feedback:not-useful — mark memory as not useful
|
|
api.on('memory:feedback:not-useful', async (params) => {
|
|
if (!_client)
|
|
return { success: false };
|
|
return await _client.markNotUseful(params.memoryId, params.reason);
|
|
});
|
|
// memory:forget — soft-delete a memory
|
|
api.on('memory:forget', async (params) => {
|
|
if (!_client)
|
|
return { success: false };
|
|
return await _client.forget(params.memoryId);
|
|
});
|
|
// memory:graphNavigate — knowledge graph traversal
|
|
api.on('memory:graphNavigate', async (params) => {
|
|
if (!_client)
|
|
return [];
|
|
return await _client.navigate(params.entity, params.maxHops ?? 2);
|
|
});
|
|
// memory:searchNotes — Obsidian notes via graph entities
|
|
api.on('memory:searchNotes', async (params) => {
|
|
if (!_client)
|
|
return [];
|
|
return await _client.searchNotes(params.entity, 2, params.maxNotes ?? 5);
|
|
});
|
|
// memory:stats — memory system stats
|
|
api.on('memory:stats', async () => {
|
|
if (!_client)
|
|
return {};
|
|
return await _client.stats();
|
|
});
|
|
// ── Tools for OpenClaw ────────────────────────────────
|
|
api.registerTool?.('memory_recall', {
|
|
description: 'Search memories via ZhiYi semantic recall',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
query: { type: 'string', description: 'Search query' },
|
|
topK: { type: 'number', default: 5, description: 'Max results' },
|
|
},
|
|
required: ['query'],
|
|
},
|
|
handler: async (args) => {
|
|
if (!_client)
|
|
return [];
|
|
return await _client.recall(args.query, args.topK ?? 5);
|
|
},
|
|
});
|
|
api.registerTool?.('memory_store', {
|
|
description: 'Store a new memory in ZhiYi',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
content: { type: 'string', description: 'Memory content' },
|
|
category: { type: 'string', default: 'episodes', description: 'Memory category' },
|
|
},
|
|
required: ['content'],
|
|
},
|
|
handler: async (args) => {
|
|
if (!_client)
|
|
return null;
|
|
return await _client.commit(args.content, args.category ?? 'episodes');
|
|
},
|
|
});
|
|
api.registerTool?.('memory_forget', {
|
|
description: 'Soft-delete a memory by ID',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
memoryId: { type: 'string', description: 'Memory ID to forget' },
|
|
},
|
|
required: ['memoryId'],
|
|
},
|
|
handler: async (args) => {
|
|
if (!_client)
|
|
return { success: false };
|
|
return await _client.forget(args.memoryId);
|
|
},
|
|
});
|
|
api.registerTool?.('memory_feedback', {
|
|
description: 'Mark a memory as useful or not-useful (activates self-optimization)',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
memoryId: { type: 'string', description: 'Memory ID' },
|
|
useful: { type: 'boolean', description: 'True = useful, False = not useful' },
|
|
reason: { type: 'string', description: 'Reason for not-useful (optional)' },
|
|
},
|
|
required: ['memoryId', 'useful'],
|
|
},
|
|
handler: async (args) => {
|
|
if (!_client)
|
|
return { success: false };
|
|
if (args.useful) {
|
|
return await _client.markUseful(args.memoryId);
|
|
}
|
|
return await _client.markNotUseful(args.memoryId, args.reason);
|
|
},
|
|
});
|
|
api.registerTool?.('memory_stats', {
|
|
description: 'Get ZhiYi memory system statistics',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {},
|
|
},
|
|
handler: async () => {
|
|
if (!_client)
|
|
return {};
|
|
return await _client.stats();
|
|
},
|
|
});
|
|
api.logger?.info('[memory-zhiyi] v0.2.0 all hooks + tools registered');
|
|
},
|
|
getClient() { return _client; },
|
|
getConfig() { return _config; },
|
|
};
|
|
exports.default = plugin;
|