Agnes 2.0-flash是推理模型,推理token占大量预算。 800仍不够→JSON截断(parse error: unexpected end of JSON input)。 1200确保推理+正文都够用。
216 lines
7.9 KiB
JavaScript
216 lines
7.9 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ZhiYiClient = void 0;
|
|
const axios_1 = __importDefault(require("axios"));
|
|
class ZhiYiClient {
|
|
client;
|
|
ns;
|
|
agentId;
|
|
config;
|
|
/** LRU prefetch cache: session key → results */
|
|
cache = new Map();
|
|
MAX_CACHE = 10;
|
|
constructor(config) {
|
|
this.config = config;
|
|
this.ns = config.namespace || 'openclaw-main';
|
|
this.agentId = config.agentId || 'openclaw';
|
|
this.client = axios_1.default.create({
|
|
baseURL: config.baseUrl,
|
|
timeout: config.timeout || 10000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-API-Key': config.apiKey,
|
|
},
|
|
});
|
|
}
|
|
// ─── Health ───────────────────────────────────────────────
|
|
async health() {
|
|
try {
|
|
const r = await this.client.get('/health');
|
|
return r.status === 200;
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
}
|
|
// ─── Commit ───────────────────────────────────────────────
|
|
async commit(content, category = 'episodes', metadata) {
|
|
try {
|
|
const payload = {
|
|
content,
|
|
category: category || 'episodes',
|
|
namespace: this.ns,
|
|
agent_id: this.agentId,
|
|
};
|
|
// Merge metadata into top-level (don't nest)
|
|
if (metadata && typeof metadata === 'object') {
|
|
for (const [k, v] of Object.entries(metadata)) {
|
|
if (!['content', 'category', 'namespace', 'agent_id'].includes(k)) {
|
|
payload[k] = v;
|
|
}
|
|
}
|
|
}
|
|
const r = await this.client.post('/api/v1/commit', payload);
|
|
if (r.status === 200 || r.status === 201) {
|
|
return r.data;
|
|
}
|
|
return null;
|
|
}
|
|
catch (e) {
|
|
this._log('commit', e);
|
|
return { error: e.message };
|
|
}
|
|
}
|
|
// ─── Batch commit with dedup ──────────────────────────────
|
|
async batchCommit(items, concurrency = 3) {
|
|
const errors = [];
|
|
let ok = 0;
|
|
// Simple concurrency limiter
|
|
for (let i = 0; i < items.length; i += concurrency) {
|
|
const batch = items.slice(i, i + concurrency);
|
|
const results = await Promise.all(batch.map(item => this.commit(item.content, item.category, item.metadata)));
|
|
for (const r of results) {
|
|
if (r && !r.error)
|
|
ok++;
|
|
else
|
|
errors.push(r?.error || 'unknown');
|
|
}
|
|
// Rate limit: 10 req/s, 3 at a time = ~300ms between batches
|
|
if (i + concurrency < items.length) {
|
|
await new Promise(r => setTimeout(r, 300));
|
|
}
|
|
}
|
|
return { ok, fail: errors.length, errors };
|
|
}
|
|
// ─── Recall ───────────────────────────────────────────────
|
|
async recall(query, topK = 5) {
|
|
const cacheKey = `${query}:${topK}`;
|
|
const cached = this.cache.get(cacheKey);
|
|
if (cached)
|
|
return cached;
|
|
try {
|
|
const r = await this.client.post('/api/v1/recall', {
|
|
query,
|
|
top_k: topK,
|
|
namespace: this.ns,
|
|
use_rerank: true,
|
|
});
|
|
const results = (r.data.results || []).map((x) => ({
|
|
id: x.id || '',
|
|
content: x.content || x.text || '',
|
|
score: x.score || 0,
|
|
category: x.category || '',
|
|
}));
|
|
// Cache with LRU eviction
|
|
if (this.cache.size >= this.MAX_CACHE) {
|
|
const firstKey = this.cache.keys().next().value;
|
|
if (firstKey)
|
|
this.cache.delete(firstKey);
|
|
}
|
|
this.cache.set(cacheKey, results);
|
|
return results;
|
|
}
|
|
catch (e) {
|
|
this._log('recall', e);
|
|
return [];
|
|
}
|
|
}
|
|
// ─── Prefetch (lightweight recall for context) ────────────
|
|
async prefetch(context) {
|
|
if (!context || context.length < 5)
|
|
return [];
|
|
return await this.recall(context, 3);
|
|
}
|
|
clearCache() {
|
|
this.cache.clear();
|
|
}
|
|
// ─── Feedback ─────────────────────────────────────────────
|
|
async markUseful(memoryId) {
|
|
try {
|
|
const r = await this.client.post('/api/v1/feedback/useful', {
|
|
memory_id: memoryId,
|
|
agent_id: this.agentId,
|
|
});
|
|
return { success: r.status === 200 };
|
|
}
|
|
catch (e) {
|
|
this._log('markUseful', e);
|
|
return { success: false, error: e.message };
|
|
}
|
|
}
|
|
async markNotUseful(memoryId, reason) {
|
|
try {
|
|
const r = await this.client.post('/api/v1/feedback/not-useful', {
|
|
memory_id: memoryId,
|
|
agent_id: this.agentId,
|
|
reason: reason || '',
|
|
});
|
|
return { success: r.status === 200 };
|
|
}
|
|
catch (e) {
|
|
this._log('markNotUseful', e);
|
|
return { success: false, error: e.message };
|
|
}
|
|
}
|
|
// ─── Forget (soft-delete) ─────────────────────────────────
|
|
async forget(memoryId) {
|
|
try {
|
|
const r = await this.client.post('/api/v1/admin/forget', {
|
|
memory_id: memoryId,
|
|
agent_id: this.agentId,
|
|
});
|
|
return { success: r.status === 200 };
|
|
}
|
|
catch (e) {
|
|
this._log('forget', e);
|
|
return { success: false, error: e.message };
|
|
}
|
|
}
|
|
// ─── Graph ────────────────────────────────────────────────
|
|
async searchNotes(entity, maxHops = 2, maxNotes = 5) {
|
|
try {
|
|
const r = await this.client.get('/api/v1/graph/notes', {
|
|
params: { entity, max_hops: maxHops, max_notes: maxNotes },
|
|
});
|
|
return r.data.notes || [];
|
|
}
|
|
catch (e) {
|
|
this._log('searchNotes', e);
|
|
return [];
|
|
}
|
|
}
|
|
async navigate(entity, maxHops = 2) {
|
|
try {
|
|
const r = await this.client.post('/api/v1/graph/navigate', {
|
|
entity,
|
|
max_hops: maxHops,
|
|
namespace: this.ns,
|
|
});
|
|
return r.data.paths || [];
|
|
}
|
|
catch (e) {
|
|
this._log('navigate', e);
|
|
return [];
|
|
}
|
|
}
|
|
// ─── Stats ────────────────────────────────────────────────
|
|
async stats() {
|
|
try {
|
|
const r = await this.client.get('/api/v1/stats');
|
|
return r.data;
|
|
}
|
|
catch {
|
|
return {};
|
|
}
|
|
}
|
|
// ─── Internal ─────────────────────────────────────────────
|
|
_log(op, e) {
|
|
const msg = e?.response?.data?.error || e?.message || String(e);
|
|
console.error(`[ZhiYi] ${op}: ${msg}`);
|
|
}
|
|
}
|
|
exports.ZhiYiClient = ZhiYiClient;
|