-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
228 lines (201 loc) · 7.68 KB
/
Copy pathindex.js
File metadata and controls
228 lines (201 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* hanako-lightrag/index.js
*
* 生命周期管理:onload spawn Python sidecar,onunload kill。
*
* 配置流(唯一来源):
* config.json (ctx.config) → 用户配置 + manifest 默认值
* bus.request(provider:credentials) → API 凭据
* ↓ 合并
* resolved_config.json → Python 端读取(不含 API Key)
* env: LLM/EMBED/RERANK_API_KEY → Python 端仅从环境变量拿密钥
*/
import { spawn } from "node:child_process";
import path from "node:path";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import http from "node:http";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// ═══════════════════════════════════════
// 工具函数
// ═══════════════════════════════════════
function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); }
function healthCheck(port, timeout = 2000) {
return new Promise((resolve) => {
const req = http.get(`http://127.0.0.1:${port}/health`, (res) => {
resolve(res.statusCode === 200);
});
req.on("error", () => resolve(false));
req.setTimeout(timeout, () => { req.destroy(); resolve(false); });
});
}
// ═══════════════════════════════════════
// Plugin
// ═══════════════════════════════════════
export default class Plugin {
_proc = null;
_port = 9621;
_restarts = 0;
_maxRestarts = 3;
// ── 配置读取(来源:ctx.config 已有 manifest 默认值合并,无需 || 回退) ──
async _readConfig() {
const ctx = this.ctx;
const [workingDir, summaryLanguage, chunkSize, chunkOverlapSize,
entityTypes, defaultQueryMode, storageBackend] = await Promise.all([
ctx.config.get("workingDir"),
ctx.config.get("summaryLanguage"),
ctx.config.get("chunkSize"),
ctx.config.get("chunkOverlapSize"),
ctx.config.get("entityTypes"),
ctx.config.get("defaultQueryMode"),
ctx.config.get("storageBackend"),
]);
return {
port: await ctx.config.get("lightragPort"),
llmBaseUrl: await ctx.config.get("llmBaseUrl"),
llmModel: await ctx.config.get("llmModel"),
embedBaseUrl: await ctx.config.get("embedBaseUrl"),
embedModel: await ctx.config.get("embedModel"),
embedDim: await ctx.config.get("embedDim"),
rerankEnabled: await ctx.config.get("rerankEnabled"),
rerankBaseUrl: await ctx.config.get("rerankBaseUrl"),
rerankModel: await ctx.config.get("rerankModel"),
// workingDir manifest 默认值为 "",此处解析为插件数据目录
workingDir: workingDir || ctx.dataDir,
summaryLanguage,
maxGleaning: await ctx.config.get("maxGleaning"),
chunkSize,
chunkOverlapSize,
entityTypes: entityTypes || [],
defaultQueryMode,
storageBackend,
};
}
// ── 校验 API Key 环境变量(仅警告,不阻止启动,Python 侧会自行报错) ──
_checkEnvKeys() {
const ctx = this.ctx;
if (!process.env.LLM_API_KEY) ctx.log.error("LLM_API_KEY 环境变量未设置。请在系统环境变量中配置后重启 Hanako。");
if (!process.env.EMBED_API_KEY) ctx.log.error("EMBED_API_KEY 环境变量未设置。请在系统环境变量中配置后重启 Hanako。");
}
// ── 生成 resolved_config.json(全部从 cfg 取值,不依赖 Provider 凭据) ──
async _writeResolvedConfig(cfg) {
const dataDir = this.ctx.dataDir;
const configPath = path.join(dataDir, "config.resolved.json");
const resolved = {
port: cfg.port,
working_dir: cfg.workingDir,
llm: {
base_url: cfg.llmBaseUrl,
model: cfg.llmModel,
},
embedding: {
base_url: cfg.embedBaseUrl,
model: cfg.embedModel,
dim: cfg.embedDim,
max_tokens: 8192,
},
rerank: {
enabled: cfg.rerankEnabled,
base_url: cfg.rerankBaseUrl,
model: cfg.rerankModel,
},
chunk: {
size: cfg.chunkSize,
overlap: cfg.chunkOverlapSize,
},
summary_language: cfg.summaryLanguage,
max_gleaning: cfg.maxGleaning,
entity_types: cfg.entityTypes,
default_query_mode: cfg.defaultQueryMode,
storage_backend: cfg.storageBackend,
};
if (cfg.storageBackend === "postgres") {
const [pgHost, pgPort, pgDb, pgUser, pgPassword] = await Promise.all([
this.ctx.config.get("pgHost"),
this.ctx.config.get("pgPort"),
this.ctx.config.get("pgDatabase"),
this.ctx.config.get("pgUser"),
this.ctx.config.get("pgPassword"),
]);
resolved.postgres = {
host: pgHost,
port: pgPort,
database: pgDb,
user: pgUser,
password: pgPassword || "",
};
}
fs.mkdirSync(dataDir, { recursive: true });
fs.writeFileSync(configPath, JSON.stringify(resolved, null, 2), "utf-8");
this.ctx.log.info(`Resolved config written to ${configPath}`);
return configPath;
}
// ── 构建环境变量(直接透传 process.env,Python 端读 LLM/EMBED/RERANK_API_KEY) ──
_buildEnv() {
return { ...process.env };
}
// ── 启动 Python sidecar ──
_spawnServer(env, configPath) {
const ctx = this.ctx;
this._proc?.kill();
this._proc = null;
const pyScript = path.join(__dirname, "py", "server_manager.py");
ctx.log.info(`Starting LightRAG server (config: ${configPath})...`);
const proc = spawn("python", ["-u", pyScript, "--config", configPath], {
env, cwd: __dirname, stdio: ["ignore", "pipe", "pipe"], windowsHide: true,
});
proc.stdout.on("data", (d) => {
const line = d.toString().trim();
if (line) ctx.log.info(`[lightrag] ${line}`);
});
proc.stderr.on("data", (d) => {
const line = d.toString().trim();
if (!line) return;
if (/address already in use|10048|10013/.test(line)) {
ctx.log.error(`Port ${env.LIGHTRAG_PORT || "?"} is already in use. Change lightragPort in plugin settings.`);
} else if (/ERROR|FATAL|Traceback|WARNING/.test(line)) {
ctx.log.warn(`[lightrag:err] ${line}`);
}
});
// ── watchdog: 崩溃自动重启 ──
proc.on("exit", (code) => {
ctx.log.warn(`LightRAG server exited (code ${code})`);
if (this._proc === proc && this._restarts < this._maxRestarts) {
this._restarts++;
ctx.log.info(`Restarting LightRAG (attempt ${this._restarts}/${this._maxRestarts})...`);
setTimeout(() => {
if (this._proc === proc) this._spawnServer(env, configPath);
}, 2000);
} else {
this._proc = null;
}
});
this._proc = proc;
return proc;
}
// ── 生命周期 ──
async onload() {
const cfg = await this._readConfig();
this._port = cfg.port;
this._checkEnvKeys();
const configPath = await this._writeResolvedConfig(cfg);
const env = this._buildEnv();
this._spawnServer(env, configPath);
// 非阻塞健康检查
this._restarts = 0;
healthCheck(this._port).then(ok => {
if (ok) this.ctx.log.info(`LightRAG server ready on port ${this._port}`);
});
}
async onunload() {
this._maxRestarts = 0; // 禁止自动重启
if (this._proc) {
this.ctx.log.info("Stopping LightRAG server...");
this._proc.kill("SIGTERM");
await sleep(3000);
if (this._proc && !this._proc.killed) this._proc.kill("SIGKILL");
this._proc = null;
this.ctx.log.info("LightRAG server stopped");
}
}
}