@fivetu53/soul-chat 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +67 -13
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -462,7 +462,12 @@ async function showChat() {
|
|
|
462
462
|
try {
|
|
463
463
|
const history = await getMessages(currentConversationId);
|
|
464
464
|
history.forEach(m => {
|
|
465
|
-
messages.push({
|
|
465
|
+
messages.push({
|
|
466
|
+
role: m.role === 'user' ? 'user' : 'bot',
|
|
467
|
+
text: m.content,
|
|
468
|
+
id: m.id,
|
|
469
|
+
timestamp: m.created_at ? new Date(m.created_at) : null
|
|
470
|
+
});
|
|
466
471
|
});
|
|
467
472
|
} catch (err) {
|
|
468
473
|
// 忽略
|
|
@@ -485,6 +490,9 @@ async function showChat() {
|
|
|
485
490
|
: '';
|
|
486
491
|
if (msg.role === 'user') {
|
|
487
492
|
console.log(c('user', ` ${currentUser.username}${timeStr}: ${msg.text}`));
|
|
493
|
+
} else if (msg.role === 'system') {
|
|
494
|
+
// 系统消息(导出、搜索等)
|
|
495
|
+
console.log(c('cyan', ` ${msg.text}`));
|
|
488
496
|
} else {
|
|
489
497
|
// 渲染 markdown
|
|
490
498
|
const rendered = marked.parse(msg.text).trim();
|
|
@@ -553,15 +561,60 @@ async function showChat() {
|
|
|
553
561
|
try { await deleteLastMessages(currentConversationId); } catch {}
|
|
554
562
|
}
|
|
555
563
|
messages.splice(-2);
|
|
564
|
+
|
|
556
565
|
// 重新发送用户消息
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
566
|
+
const regenText = lastUserMsg.text;
|
|
567
|
+
messages.push({ role: 'user', text: regenText, timestamp: new Date() });
|
|
568
|
+
drawMessages();
|
|
569
|
+
|
|
570
|
+
// 显示思考中
|
|
571
|
+
process.stdout.write(c('bot', ` ${currentCharacter.name}: `) + c('hint', '思考中...'));
|
|
572
|
+
|
|
573
|
+
try {
|
|
574
|
+
const response = await sendMessage(regenText, currentCharacter.id, currentConversationId, null);
|
|
575
|
+
process.stdout.write('\r\x1b[K');
|
|
576
|
+
process.stdout.write(c('bot', ` ${currentCharacter.name}: `));
|
|
577
|
+
|
|
578
|
+
let fullResponse = '';
|
|
579
|
+
const reader = response.body.getReader();
|
|
580
|
+
const decoder = new TextDecoder();
|
|
581
|
+
let buffer = '';
|
|
582
|
+
|
|
583
|
+
while (true) {
|
|
584
|
+
const { done, value } = await reader.read();
|
|
585
|
+
if (done) break;
|
|
586
|
+
buffer += decoder.decode(value, { stream: true });
|
|
587
|
+
const lines = buffer.split('\n');
|
|
588
|
+
buffer = lines.pop() || '';
|
|
589
|
+
|
|
590
|
+
for (const line of lines) {
|
|
591
|
+
if (line.startsWith('data: ')) {
|
|
592
|
+
const data = line.slice(6);
|
|
593
|
+
if (data === '[DONE]') continue;
|
|
594
|
+
try {
|
|
595
|
+
const json = JSON.parse(data);
|
|
596
|
+
if (json.type === 'info') {
|
|
597
|
+
currentConversationId = json.conversationId;
|
|
598
|
+
} else if (json.type === 'content') {
|
|
599
|
+
fullResponse += json.content;
|
|
600
|
+
process.stdout.write(json.content);
|
|
601
|
+
}
|
|
602
|
+
} catch {}
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
console.log();
|
|
608
|
+
messages.push({ role: 'bot', text: fullResponse || '...', timestamp: new Date() });
|
|
609
|
+
drawMessages();
|
|
610
|
+
} catch (err) {
|
|
611
|
+
process.stdout.write('\r\x1b[K');
|
|
612
|
+
console.log(c('red', ` 错误: ${err.message}`));
|
|
613
|
+
await sleep(1500);
|
|
614
|
+
}
|
|
561
615
|
}
|
|
562
|
-
} else {
|
|
563
|
-
continue;
|
|
564
616
|
}
|
|
617
|
+
continue;
|
|
565
618
|
}
|
|
566
619
|
|
|
567
620
|
if (input === '/export') {
|
|
@@ -571,18 +624,17 @@ async function showChat() {
|
|
|
571
624
|
let content = `# 与 ${currentCharacter.name} 的对话\n\n`;
|
|
572
625
|
content += `导出时间: ${new Date().toLocaleString('zh-CN')}\n\n---\n\n`;
|
|
573
626
|
|
|
574
|
-
messages.forEach(msg => {
|
|
627
|
+
messages.filter(m => m.role !== 'system').forEach(msg => {
|
|
575
628
|
const role = msg.role === 'user' ? currentUser.username : currentCharacter.name;
|
|
576
629
|
content += `**${role}**:\n${msg.text}\n\n`;
|
|
577
630
|
});
|
|
578
631
|
|
|
579
632
|
try {
|
|
580
633
|
fs.writeFileSync(filepath, content);
|
|
581
|
-
|
|
634
|
+
messages.push({ role: 'system', text: `✓ 已导出到: ${filepath}` });
|
|
582
635
|
} catch (err) {
|
|
583
|
-
|
|
636
|
+
messages.push({ role: 'system', text: `✗ 导出失败: ${err.message}` });
|
|
584
637
|
}
|
|
585
|
-
await sleep(1500);
|
|
586
638
|
continue;
|
|
587
639
|
}
|
|
588
640
|
|
|
@@ -629,14 +681,16 @@ async function showChat() {
|
|
|
629
681
|
if (json.type === 'info') {
|
|
630
682
|
currentConversationId = json.conversationId;
|
|
631
683
|
} else if (json.type === 'tool_call') {
|
|
632
|
-
//
|
|
684
|
+
// 显示搜索状态并添加到消息
|
|
633
685
|
process.stdout.write('\r\x1b[K');
|
|
634
686
|
process.stdout.write(c('cyan', ` 🔍 正在搜索: ${json.query}...`));
|
|
687
|
+
messages.push({ role: 'system', text: `🔍 正在搜索: ${json.query}` });
|
|
635
688
|
} else if (json.type === 'tool_result') {
|
|
636
|
-
//
|
|
689
|
+
// 显示搜索结果数量并添加到消息
|
|
637
690
|
process.stdout.write('\r\x1b[K');
|
|
638
691
|
process.stdout.write(c('green', ` 📋 找到 ${json.count} 条结果\n`));
|
|
639
692
|
process.stdout.write(c('bot', ` ${currentCharacter.name}: `));
|
|
693
|
+
messages.push({ role: 'system', text: `📋 找到 ${json.count} 条结果` });
|
|
640
694
|
} else if (json.type === 'content') {
|
|
641
695
|
fullResponse += json.content;
|
|
642
696
|
// 打字机效果:直接输出内容
|