forked from liliMozi/openhanako
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
305 lines (270 loc) · 10.4 KB
/
Copy pathindex.js
File metadata and controls
305 lines (270 loc) · 10.4 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import fs from "fs";
import readline from "readline";
import { HanaEngine } from "./core/engine.js";
import { ensureFirstRun } from "./core/first-run.js";
import { MoodParser } from "./core/events.js";
import { configureProcessPiSdkEnv, ensureHanaPiSdkDirs, resolveHanakoHome } from "./shared/hana-runtime-paths.js";
// ═══════════════════════════════════════
// Project Hana — CLI Agent with Memory
// ═══════════════════════════════════════
import path from "path";
const projectRoot = import.meta.dirname;
const productDir = projectRoot + "/lib";
// 用户数据目录:优先 HANA_HOME,默认 ~/.hanako
const hanakoHome = resolveHanakoHome(process.env.HANA_HOME);
process.env.HANA_HOME = hanakoHome;
ensureHanaPiSdkDirs(hanakoHome);
configureProcessPiSdkEnv(hanakoHome);
// ── 首次运行播种 ──
ensureFirstRun(hanakoHome, productDir);
// ── 初始化引擎 ──
const engine = new HanaEngine({ hanakoHome, productDir });
try {
await engine.init((msg) => console.log(msg));
} catch (err) {
console.error("启动失败:", err.message);
console.error("\n可能的原因:");
console.error(` 1. ${path.join(hanakoHome, "models.json")} 格式不对`);
console.error(" 2. API key 不对");
console.error(" 3. 网络连不上模型服务");
console.error(" 4. 缺少依赖:npm install js-yaml");
process.exit(1);
}
const { userName, agentName } = engine;
const available = engine.availableModels;
const memoryMdPath = engine.memoryMdPath;
// ── CLI 渲染器 ──
// Hana 文字色 #7D1C4A = RGB(125, 28, 74)
const hanaColor = `\x1b[38;2;125;28;74m`;
const resetColor = `\x1b[0m`;
// 思考动画
const thinkingHints = [
`${agentName} 正在思考`,
`${agentName} 正在想该怎么回答你`,
`${agentName} 正在摸鱼`,
`${agentName} 正在翻记忆`,
`${agentName} 正在组织语言`,
`${agentName} 正在认真想`,
`${agentName} 脑子转啊转`,
];
let thinkingTimer = null;
let thinkingFrame = 0;
let thinkingDots = 0;
function startThinkingAnim() {
if (thinkingTimer) return;
thinkingFrame = Math.floor(Math.random() * thinkingHints.length);
thinkingDots = 0;
const render = () => {
const hint = thinkingHints[thinkingFrame % thinkingHints.length];
const dots = ".".repeat((thinkingDots % 3) + 1);
process.stdout.write(`\r\x1b[90m✿ ${hint}${dots}\x1b[0m\x1b[K`);
thinkingDots++;
if (thinkingDots % 4 === 0) thinkingFrame++;
};
render();
thinkingTimer = setInterval(render, 500);
}
function stopThinkingAnim() {
if (!thinkingTimer) return;
clearInterval(thinkingTimer);
thinkingTimer = null;
process.stdout.write(`\r\x1b[K`);
}
// MOOD 解析器
const moodParser = new MoodParser();
// 订阅引擎事件 → CLI 渲染
engine.subscribe((event) => {
if (event.type === "message_update") {
const sub = event.assistantMessageEvent?.type;
if (sub === "text_delta") {
stopThinkingAnim();
const delta = event.assistantMessageEvent.delta;
moodParser.feed(delta, (evt) => {
if (evt.type === "text") {
process.stdout.write(`${hanaColor}${evt.data}${resetColor}`);
} else if (evt.type === "mood_start") {
process.stdout.write(`\x1b[90m<mood>\x1b[0m`);
} else if (evt.type === "mood_text") {
process.stdout.write(`\x1b[90m${evt.data}\x1b[0m`);
} else if (evt.type === "mood_end") {
process.stdout.write(`\x1b[90m</mood>\x1b[0m`);
}
});
} else if (sub === "thinking_delta") {
startThinkingAnim();
} else if (sub === "toolcall_start") {
stopThinkingAnim();
process.stdout.write(`\n\x1b[36m⚙ 调用工具...\x1b[0m`);
} else if (sub === "toolcall_end") {
const tool = event.assistantMessageEvent.toolCall;
const argKeys = tool?.input && typeof tool.input === "object" ? Object.keys(tool.input) : [];
console.log(`\x1b[36m ✓ ${tool?.name || "unknown"}(${argKeys.length ? `keys=${argKeys.join(",")}` : ""})\x1b[0m`);
} else if (sub === "error") {
console.error("\n\x1b[31m[模型返回错误]\x1b[0m", event.assistantMessageEvent.error);
}
} else if (event.type === "tool_execution_start") {
const name = event.toolCall?.name || "";
process.stdout.write(`\x1b[33m⏳ 执行 ${name}...\x1b[0m`);
} else if (event.type === "tool_execution_update") {
if (event.output) {
process.stdout.write(`\x1b[90m${event.output}\x1b[0m`);
}
} else if (event.type === "tool_execution_end") {
const name = event.toolCall?.name || "";
const ok = event.toolResults?.[0]?.isError ? "✗" : "✓";
console.log(` \x1b[33m${ok} ${name} 完成\x1b[0m`);
}
});
// ── 启动 session ──
let { session } = await engine.createSession();
console.log("✿ 记忆系统已激活\n");
// ── CLI 交互 ──
readline.emitKeypressEvents(process.stdin);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
process.stdin.on("keypress", (ch, key) => {
if (key && key.name === "escape") {
rl.write(null, { ctrl: true, name: "u" });
}
});
function promptLine(text) {
return new Promise((resolve) => rl.question(text, resolve));
}
const ask = () => {
rl.question(`\n\x1b[38;2;126;172;181m${userName} > \x1b[0m`, async (input) => {
const trimmed = input.trim();
if (trimmed === "/quit" || trimmed === "/exit") {
await engine.dispose();
console.log(`\n✿ ${agentName} 去休息了,下次见~`);
rl.close();
process.exit(0);
}
if (trimmed === "/model") {
console.log("\n可用模型:");
available.forEach((m, i) => {
const cur = engine.currentModel;
const current = (m.id === cur?.id && m.provider === cur?.provider) ? " ← 当前" : "";
console.log(` ${i + 1}. ${m.name} (${m.provider})${current}`);
});
rl.question("\n选择模型编号 > ", async (num) => {
const idx = parseInt(num) - 1;
if (idx >= 0 && idx < available.length) {
await engine.setPendingModel(available[idx].id, available[idx].provider);
console.log(`\n✿ 已切换到: ${available[idx].name}`);
} else {
console.log("\n取消切换");
}
ask();
});
return;
}
if (trimmed === "/think" || trimmed.startsWith("/think ")) {
const arg = trimmed.slice(7).trim().toLowerCase();
const current = engine.session?.thinkingLevel || "off";
const isOn = current !== "off";
if (arg === "on" || arg === "off") {
engine.setThinkingLevel(arg === "on" ? "medium" : "off");
console.log(`\n✿ 深度思考已${arg === "on" ? "开启" : "关闭"}`);
} else if (!arg) {
engine.setThinkingLevel(isOn ? "off" : "medium");
console.log(`\n✿ 深度思考已${isOn ? "关闭" : "开启"}`);
} else {
console.log("\n用法: /think — 切换开关");
console.log(" /think on — 开启深度思考");
console.log(" /think off — 关闭深度思考");
}
ask();
return;
}
if (trimmed === "/memory") {
try {
const md = fs.readFileSync(memoryMdPath, "utf-8");
console.log("\n\x1b[35m── 当前记忆(session 启动时的快照)──\x1b[0m");
console.log(md);
} catch {
console.log("\n(还没有记忆)");
}
ask();
return;
}
if (trimmed === "/session") {
try {
const sessions = await engine.listSessions();
if (sessions.length === 0) {
console.log("\n(没有历史 session)");
ask();
return;
}
console.log(`\n\x1b[35m── 历史 Session(${sessions.length} 个)──\x1b[0m`);
const display = sessions.slice(0, 15);
for (let i = 0; i < display.length; i++) {
const s = display[i];
const date = s.modified.toLocaleDateString("zh-CN", { month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit" });
const preview = (s.firstMessage || "(空)").slice(0, 50).replace(/\n/g, " ");
const msgs = s.messageCount || 0;
console.log(` \x1b[36m${i + 1}.\x1b[0m [${date}] ${preview}${preview.length >= 50 ? "…" : ""} \x1b[90m(${msgs} 条消息)\x1b[0m`);
}
console.log(` \x1b[90m0. 取消\x1b[0m`);
const choice = await promptLine("\n选择 session 编号 > ");
const idx = parseInt(choice) - 1;
if (idx >= 0 && idx < display.length) {
const picked = display[idx];
console.log(`\n✿ 正在加载 session...`);
moodParser.reset();
session = await engine.switchSession(picked.path);
const msgCount = engine.messages?.length ?? 0;
console.log(`✿ 已切换到历史 session(${msgCount} 条消息)`);
} else {
console.log("\n取消切换");
}
} catch (err) {
console.error(`\n[session 列表出错] ${err.message}`);
}
ask();
return;
}
if (trimmed === "/new") {
console.log("\n✿ 开始新的对话...");
moodParser.reset();
({ session } = await engine.createSession());
console.log("✿ 新 session 已创建");
ask();
return;
}
if (trimmed === "/help") {
console.log("\n\x1b[35m── 命令列表 ──\x1b[0m");
console.log(" /session — 查看并切换历史对话");
console.log(" /new — 开始新对话");
console.log(" /memory — 查看当前记忆");
console.log(" /model — 切换模型");
console.log(" /think — 切换深度思考开关(on/off)");
console.log(" /quit — 退出");
ask();
return;
}
if (!trimmed) {
ask();
return;
}
try {
moodParser.reset();
console.log(`\n\x1b[38;2;125;28;74m${agentName} >\x1b[0m `);
await engine.prompt(trimmed);
moodParser.flush((evt) => {
if (evt.type === "text") {
process.stdout.write(`${hanaColor}${evt.data}${resetColor}`);
} else if (evt.type === "mood_text") {
process.stdout.write(`\x1b[90m${evt.data}\x1b[0m`);
}
});
console.log("");
} catch (err) {
console.error("\n[出错了]", err.message);
}
ask();
});
};
console.log(`✿ ${agentName} 醒了!输入 /help 查看命令\n`);
ask();