@bolloon/bolloon-agent 0.1.32 → 0.1.33
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/dist/agents/pi-sdk.js +10 -1
- package/dist/llm/audio-config-store.js +199 -0
- package/dist/llm/config-store.js +20 -10
- package/dist/llm/pi-ai.js +2 -2
- package/dist/llm/video-config-store.js +31 -1
- package/dist/pi-ecosystem/index.js +1 -1
- package/dist/web/api-config.html +13 -1
- package/dist/web/client.js +375 -8
- package/dist/web/server.js +269 -5
- package/package.json +1 -1
- package/src/agents/pi-sdk.ts +9 -1
- package/src/llm/audio-config-store.ts +6 -1
- package/src/llm/config-store.ts +21 -11
- package/src/llm/pi-ai.ts +2 -2
- package/src/llm/video-config-store.ts +7 -1
- package/src/web/api-config.html +13 -1
- package/src/web/client.js +375 -8
- package/src/web/server.ts +228 -5
package/src/web/server.ts
CHANGED
|
@@ -115,6 +115,13 @@ interface SessionMessage {
|
|
|
115
115
|
type: 'user' | 'ai';
|
|
116
116
|
content: string;
|
|
117
117
|
timestamp: string;
|
|
118
|
+
/** v3: 'local' = channel 内部 owner 发的, 'remote' = 远端访客通过 P2P 发的, 'ai-mention' = 同节点其他 channel 的 AI @-mention, 'ai-mention-remote' = 远端节点的 AI @-mention */
|
|
119
|
+
source?: 'local' | 'remote' | 'ai-mention' | 'ai-mention-remote';
|
|
120
|
+
/** v3: 当 source='remote' 或 'ai-mention-remote' 时记录对方 publicKey */
|
|
121
|
+
fromPublicKey?: string;
|
|
122
|
+
/** v3: 当 source 是 ai-mention* 时, 是哪个 channel 触发的 */
|
|
123
|
+
originChannelId?: string;
|
|
124
|
+
originChannelName?: string;
|
|
118
125
|
}
|
|
119
126
|
|
|
120
127
|
interface Session {
|
|
@@ -466,6 +473,118 @@ function isSharedWith(ch: Channel, peerPublicKey: string): boolean {
|
|
|
466
473
|
return shared.includes(peerPublicKey);
|
|
467
474
|
}
|
|
468
475
|
|
|
476
|
+
/**
|
|
477
|
+
* v3 新增: 解析 LLM 回复里的 @-mentions, 把消息发到目标 channel.
|
|
478
|
+
*
|
|
479
|
+
* 语法: "@渠道名 消息内容" — 渠道名匹配 local channels by name, 或 remote channels by name.
|
|
480
|
+
* - 本地 channel: 直接 push 到 session
|
|
481
|
+
* - 远端 channel: 通过 P2P RPC 转发到对端
|
|
482
|
+
*
|
|
483
|
+
* 返回: 解析到的 mention 列表, 供 SSE 广播
|
|
484
|
+
*/
|
|
485
|
+
async function routeMentionsInReply(
|
|
486
|
+
originChannelId: string,
|
|
487
|
+
replyText: string,
|
|
488
|
+
localChannels: any[],
|
|
489
|
+
remoteChannels: any[]
|
|
490
|
+
): Promise<Array<{ targetName: string; targetId: string; source: 'local' | 'remote'; text: string; status: 'sent' | 'failed' }>> {
|
|
491
|
+
const results: any[] = [];
|
|
492
|
+
// 解析: 匹配 @渠道名 后面跟一段文字 (到下一个 @ 或 行尾)
|
|
493
|
+
// 渠道名: 中文/英文/数字/下划线/连字符, 1-30 字符
|
|
494
|
+
const regex = /@([一-龥A-Za-z0-9_\-]{1,30})\s+([^\n@]+?)(?=(?:\s*@[一-龥A-Za-z0-9_\-]{1,30}\s)|$)/g;
|
|
495
|
+
const matches = [...replyText.matchAll(regex)];
|
|
496
|
+
|
|
497
|
+
if (matches.length === 0) return results;
|
|
498
|
+
|
|
499
|
+
// 找当前 channel 的 name (用于日志)
|
|
500
|
+
let originChannelName = originChannelId;
|
|
501
|
+
try {
|
|
502
|
+
const chs = await loadChannels();
|
|
503
|
+
const oc = chs.find(c => c.id === originChannelId);
|
|
504
|
+
if (oc) originChannelName = oc.name;
|
|
505
|
+
} catch {}
|
|
506
|
+
|
|
507
|
+
console.log(`[v3-cross] (${originChannelName}) 解析到 ${matches.length} 个 @-mention`);
|
|
508
|
+
|
|
509
|
+
for (const m of matches) {
|
|
510
|
+
const targetName = m[1].trim();
|
|
511
|
+
const text = m[2].trim();
|
|
512
|
+
if (!text) continue;
|
|
513
|
+
|
|
514
|
+
// 优先本地 (本地 channel 不能有 ownerPublicKey)
|
|
515
|
+
const localTarget = localChannels.find(c => c.name === targetName);
|
|
516
|
+
const remoteTarget = !localTarget ? remoteChannels.find(c => c.name === targetName) : null;
|
|
517
|
+
|
|
518
|
+
if (localTarget) {
|
|
519
|
+
// 本地: 直接 push 到 session
|
|
520
|
+
try {
|
|
521
|
+
const existing = await loadSession(localTarget.id, 'default');
|
|
522
|
+
const session: Session = existing || {
|
|
523
|
+
channelId: localTarget.id, sessionId: 'default', messages: [], lastUpdated: new Date().toISOString()
|
|
524
|
+
};
|
|
525
|
+
session.messages.push({
|
|
526
|
+
id: `msg-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
|
527
|
+
type: 'ai' as const,
|
|
528
|
+
content: text,
|
|
529
|
+
timestamp: new Date().toISOString(),
|
|
530
|
+
source: 'ai-mention' as any, // v3: 标记是其他 channel 的 AI @-mention 进来的
|
|
531
|
+
originChannelId, // 谁 @ 过来的
|
|
532
|
+
originChannelName // 渠道名 (方便显示)
|
|
533
|
+
});
|
|
534
|
+
session.lastUpdated = new Date().toISOString();
|
|
535
|
+
await saveSession(session);
|
|
536
|
+
console.log(`[v3-cross] (${originChannelName}) @${targetName} → 本地 channel ${localTarget.id}, 存了 ${text.length} chars`);
|
|
537
|
+
// 推 SSE 让本地 UI 知道有 AI 跨渠道消息
|
|
538
|
+
broadcast({
|
|
539
|
+
type: 'cross-mention-received',
|
|
540
|
+
originChannelId, originChannelName,
|
|
541
|
+
targetChannelId: localTarget.id, targetChannelName: localTarget.name,
|
|
542
|
+
text, source: 'ai-mention'
|
|
543
|
+
}, 'broadcast');
|
|
544
|
+
results.push({ targetName, targetId: localTarget.id, source: 'local', text, status: 'sent' });
|
|
545
|
+
} catch (err) {
|
|
546
|
+
console.error(`[v3-cross] @${targetName} 本地存失败:`, (err as Error).message);
|
|
547
|
+
results.push({ targetName, targetId: localTarget.id, source: 'local', text, status: 'failed' });
|
|
548
|
+
}
|
|
549
|
+
} else if (remoteTarget) {
|
|
550
|
+
// 远端: 通过 P2P RPC 转发
|
|
551
|
+
const ownerPk = remoteTarget._ownerPublicKey;
|
|
552
|
+
if (!v3P2PRef) {
|
|
553
|
+
console.warn(`[v3-cross] P2PDirect 未启动, 跳过远端 @${targetName}`);
|
|
554
|
+
results.push({ targetName, targetId: remoteTarget.id, source: 'remote', text, status: 'failed' });
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
try {
|
|
558
|
+
const rpc = JSON.stringify({
|
|
559
|
+
v: 3, op: 'agent.cross.post',
|
|
560
|
+
payload: {
|
|
561
|
+
targetChannelId: remoteTarget.id,
|
|
562
|
+
targetChannelName: remoteTarget.name,
|
|
563
|
+
originChannelId,
|
|
564
|
+
originChannelName,
|
|
565
|
+
text,
|
|
566
|
+
fromPublicKey: v3P2PRef.getPublicKey()
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
const ok = v3P2PRef.sendTo(ownerPk, rpc);
|
|
570
|
+
if (ok) {
|
|
571
|
+
console.log(`[v3-cross] (${originChannelName}) @${targetName} → 远端 peer ${ownerPk.substring(0,12)}... (channelId=${remoteTarget.id})`);
|
|
572
|
+
results.push({ targetName, targetId: remoteTarget.id, source: 'remote', text, status: 'sent' });
|
|
573
|
+
} else {
|
|
574
|
+
results.push({ targetName, targetId: remoteTarget.id, source: 'remote', text, status: 'failed' });
|
|
575
|
+
}
|
|
576
|
+
} catch (err) {
|
|
577
|
+
console.error(`[v3-cross] @${targetName} 远端 RPC 失败:`, (err as Error).message);
|
|
578
|
+
results.push({ targetName, targetId: remoteTarget.id, source: 'remote', text, status: 'failed' });
|
|
579
|
+
}
|
|
580
|
+
} else {
|
|
581
|
+
console.warn(`[v3-cross] @${targetName} 找不到匹配 channel (本地 ${localChannels.length} 个, 远端 ${remoteChannels.length} 个)`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
return results;
|
|
586
|
+
}
|
|
587
|
+
|
|
469
588
|
/**
|
|
470
589
|
* v3: 处理 Hyperswarm 通道收到的 v3 RPC 消息
|
|
471
590
|
* 设计: 用 HyperswarmCommunicator (DHT topic 自动发现) 取代 iroh 直接 connect
|
|
@@ -576,11 +695,13 @@ async function handleV3P2PMessage(parsed: any, conn: P2PConnection, comm: Hypers
|
|
|
576
695
|
id: `msg-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
|
577
696
|
type: 'user',
|
|
578
697
|
content: text,
|
|
579
|
-
timestamp: new Date().toISOString()
|
|
698
|
+
timestamp: new Date().toISOString(),
|
|
699
|
+
source: 'remote', // v3: 标记远端访客
|
|
700
|
+
fromPublicKey: senderKey // v3: 记录对方 publicKey
|
|
580
701
|
});
|
|
581
702
|
session.lastUpdated = new Date().toISOString();
|
|
582
703
|
await saveSession(session);
|
|
583
|
-
console.log(`[v3] (${channelId}) 存 user 消息 (${text.length} chars) 到 A 的 session`);
|
|
704
|
+
console.log(`[v3] (${channelId}) 存 user 消息 (${text.length} chars) 到 A 的 session (来自 ${senderKey.substring(0,12)}...)`);
|
|
584
705
|
} catch (saveErr) {
|
|
585
706
|
console.warn(`[v3] 存 user 消息失败 (不影响 chat):`, (saveErr as Error).message);
|
|
586
707
|
}
|
|
@@ -606,7 +727,29 @@ async function handleV3P2PMessage(parsed: any, conn: P2PConnection, comm: Hypers
|
|
|
606
727
|
// 2. 跑 LLM (复用 Phase 1 的 buildJudgmentHint — 注入 channel 的 judgment)
|
|
607
728
|
const { getMinimax } = await import('../constraints/index.js');
|
|
608
729
|
const llm = getMinimax();
|
|
609
|
-
|
|
730
|
+
// v3 新增: 在 prompt 头部标记"这是远端访客", 让 AI 知道对方不是自己 owner
|
|
731
|
+
const visitorHint = `[系统上下文] 消息来源: 远端访客 (P2P 连接, publicKey=${senderKey.substring(0, 12)}...). 对方不是你 owner, 是通过 P2P 网络访问你这个 channel 的合作者. 称呼对方时可用 "远端访客" / "朋友" / "合作者", 不要叫 "主人".\n\n`;
|
|
732
|
+
// v3 新增: 也注入 channel 目录给 LLM (B 的 channel 也可以 @-mention 其他)
|
|
733
|
+
let dirHint = '';
|
|
734
|
+
const localChannels = (await loadChannels()).filter(c => c.id !== channelId);
|
|
735
|
+
const remoteChannels: any[] = [];
|
|
736
|
+
for (const [peerPk, list] of remoteChannelCache.entries()) {
|
|
737
|
+
if (peerPk === senderKey) continue; // 跳过发起方
|
|
738
|
+
for (const ch of list) {
|
|
739
|
+
remoteChannels.push({ ...ch, _ownerPublicKey: peerPk });
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
if (localChannels.length > 0 || remoteChannels.length > 0) {
|
|
743
|
+
dirHint += '[系统上下文] 可用渠道 (你可以用 @渠道名 消息内容 给它们发消息):\n';
|
|
744
|
+
for (const c of localChannels) {
|
|
745
|
+
dirHint += ` - [本地] @${c.name} (id=${c.id})\n`;
|
|
746
|
+
}
|
|
747
|
+
for (const c of remoteChannels) {
|
|
748
|
+
dirHint += ` - [远端, owner=${(c._ownerPublicKey || '').substring(0,8)}…] @${c.name} (id=${c.id})\n`;
|
|
749
|
+
}
|
|
750
|
+
dirHint += '语法: 在回复中写 "@渠道名 我要说的话" 即可. 消息会持久化到目标 channel 的 session.\n\n';
|
|
751
|
+
}
|
|
752
|
+
const fullPrompt = `${visitorHint}${dirHint}${judgmentHint}${text}`;
|
|
610
753
|
let fullResponse = '';
|
|
611
754
|
// v3 新增: 流式 token 节流推给 B — 让 B 看到过程
|
|
612
755
|
let lastFlushAt = 0;
|
|
@@ -728,6 +871,53 @@ async function handleV3P2PMessage(parsed: any, conn: P2PConnection, comm: Hypers
|
|
|
728
871
|
return;
|
|
729
872
|
}
|
|
730
873
|
|
|
874
|
+
// v3 新增: 收到远端发来的 @-mention 跨渠道消息, 存到本地 target channel
|
|
875
|
+
if (op === 'agent.cross.post') {
|
|
876
|
+
const { targetChannelId, targetChannelName, originChannelId, originChannelName, text, fromPublicKey } = parsed.payload || {};
|
|
877
|
+
if (!targetChannelId || !text) {
|
|
878
|
+
console.warn(`[v3-cross] agent.cross.post 缺少 targetChannelId/text`);
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
try {
|
|
882
|
+
// 找 channel — 必须存在于本节点
|
|
883
|
+
const channels = await loadChannels();
|
|
884
|
+
const ch = channels.find(c => c.id === targetChannelId);
|
|
885
|
+
if (!ch) {
|
|
886
|
+
console.warn(`[v3-cross] agent.cross.post: 本节点无 channel ${targetChannelId}, 忽略`);
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
// 存到 session — 这是一条来自其他节点的 LLM @-mention
|
|
890
|
+
const existing = await loadSession(targetChannelId, 'default');
|
|
891
|
+
const session: Session = existing || {
|
|
892
|
+
channelId: targetChannelId, sessionId: 'default', messages: [], lastUpdated: new Date().toISOString()
|
|
893
|
+
};
|
|
894
|
+
session.messages.push({
|
|
895
|
+
id: `msg-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
|
|
896
|
+
type: 'ai' as const,
|
|
897
|
+
content: text,
|
|
898
|
+
timestamp: new Date().toISOString(),
|
|
899
|
+
source: 'ai-mention-remote' as any, // v3: 来自其他节点的 AI @-mention
|
|
900
|
+
originChannelId, // 哪个 channel 触发的
|
|
901
|
+
originChannelName,
|
|
902
|
+
fromPublicKey // 哪个节点来的
|
|
903
|
+
});
|
|
904
|
+
session.lastUpdated = new Date().toISOString();
|
|
905
|
+
await saveSession(session);
|
|
906
|
+
console.log(`[v3-cross] 收到远端 @-mention: ${originChannelName} → 本地 ${targetChannelName} (${text.length} chars)`);
|
|
907
|
+
// 推 SSE 让本地 UI 知道有跨渠道消息到达
|
|
908
|
+
broadcast({
|
|
909
|
+
type: 'cross-mention-received',
|
|
910
|
+
originChannelId, originChannelName,
|
|
911
|
+
targetChannelId, targetChannelName: ch.name,
|
|
912
|
+
text, source: 'ai-mention-remote',
|
|
913
|
+
fromPublicKey
|
|
914
|
+
}, 'broadcast');
|
|
915
|
+
} catch (err) {
|
|
916
|
+
console.error(`[v3-cross] 处理 agent.cross.post 失败:`, (err as Error).message);
|
|
917
|
+
}
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
|
|
731
921
|
console.log(`[v3] 收到未知 op: ${op}`);
|
|
732
922
|
}
|
|
733
923
|
|
|
@@ -1257,6 +1447,8 @@ export async function createWebServer(port: number = 3000, options: CreateWebSer
|
|
|
1257
1447
|
// 将真实 DID 作为上下文前缀,让 AI 使用真实的 DID 而不是自己编造的
|
|
1258
1448
|
let contextHint = '';
|
|
1259
1449
|
if (realChannelDid) contextHint += `[系统上下文] 当前频道名称: ${realChannelName}, 你的真实 DID: ${realChannelDid}\n`;
|
|
1450
|
+
// v3 新增: 标识发送方 — 让 AI 分清内部 owner vs 远端访客
|
|
1451
|
+
contextHint += `[系统上下文] 消息来源: 本地 (channel 内部 owner / 此机器上的用户). 称呼对方时用 "你" 或 "主人" 即可.\n`;
|
|
1260
1452
|
if (boundWalletAddress) {
|
|
1261
1453
|
contextHint += `[系统上下文] 已绑定的加密钱包地址: ${boundWalletAddress}。当用户授权或启用自动工具调用时, 可使用该地址发起链上操作。\n`;
|
|
1262
1454
|
}
|
|
@@ -1271,16 +1463,41 @@ export async function createWebServer(port: number = 3000, options: CreateWebSer
|
|
|
1271
1463
|
const judgmentHint = await buildJudgmentHint(channelForJudgment, channelId);
|
|
1272
1464
|
if (judgmentHint) contextHint += judgmentHint;
|
|
1273
1465
|
|
|
1466
|
+
// v3 新增: 注入"可用渠道"目录, 让 LLM 知道可以 @-mention 哪些 channel
|
|
1467
|
+
// - 本地 channels (除了自己)
|
|
1468
|
+
// - 远端 channels (remoteChannelCache 缓存的)
|
|
1469
|
+
const localChannels = (await loadChannels()).filter(c => c.id !== channelId);
|
|
1470
|
+
const remoteChannels: any[] = [];
|
|
1471
|
+
for (const [peerPk, list] of remoteChannelCache.entries()) {
|
|
1472
|
+
for (const ch of list) {
|
|
1473
|
+
remoteChannels.push({ ...ch, _ownerPublicKey: peerPk });
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
if (localChannels.length > 0 || remoteChannels.length > 0) {
|
|
1477
|
+
contextHint += '[系统上下文] 可用渠道 (你可以用 @渠道名 消息内容 给它们发消息):\n';
|
|
1478
|
+
for (const c of localChannels) {
|
|
1479
|
+
contextHint += ` - [本地] @${c.name} (id=${c.id})\n`;
|
|
1480
|
+
}
|
|
1481
|
+
for (const c of remoteChannels) {
|
|
1482
|
+
contextHint += ` - [远端, owner=${(c._ownerPublicKey || '').substring(0,8)}…] @${c.name} (id=${c.id})\n`;
|
|
1483
|
+
}
|
|
1484
|
+
contextHint += '语法: 当你想给其他渠道发消息, 在回复中写 "@渠道名 我要说的话" 即可. 消息会持久化到目标 channel 的 session, 你之后能看到"自己"在那里说的话.\n\n';
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1274
1487
|
if (contextHint) contextHint += '\n';
|
|
1275
1488
|
fullResponse = await agent.promptStream(contextHint + text, streamCallback);
|
|
1276
1489
|
|
|
1490
|
+
// v3 新增: 解析 LLM 回复里的 @-mentions, 转发到目标 channel
|
|
1491
|
+
await routeMentionsInReply(channelId, fullResponse, localChannels, remoteChannels);
|
|
1492
|
+
|
|
1277
1493
|
broadcast({ type: 'ai', content: fullResponse }, channelId);
|
|
1278
1494
|
|
|
1279
1495
|
const existingSession = await loadSession(channelId, currentSessionId);
|
|
1280
1496
|
const session: Session = existingSession || { channelId, sessionId: currentSessionId, messages: [], lastUpdated: new Date().toISOString() };
|
|
1281
1497
|
session.sessionId = currentSessionId;
|
|
1282
|
-
|
|
1283
|
-
session.messages.push({ id: crypto.randomUUID(), type: '
|
|
1498
|
+
// v3: 加 source 标记 (local = 内部 owner, remote = 远端访客)
|
|
1499
|
+
session.messages.push({ id: crypto.randomUUID(), type: 'user' as const, content: text, timestamp: new Date().toISOString(), source: 'local' as any });
|
|
1500
|
+
session.messages.push({ id: crypto.randomUUID(), type: 'ai' as const, content: fullResponse, timestamp: new Date().toISOString(), source: 'local' as any });
|
|
1284
1501
|
session.lastUpdated = new Date().toISOString();
|
|
1285
1502
|
await saveSession(session);
|
|
1286
1503
|
|
|
@@ -2212,6 +2429,12 @@ app.get('/channels', async (_req, res) => {
|
|
|
2212
2429
|
return res.status(400).json({ error: 'provider and config required' });
|
|
2213
2430
|
}
|
|
2214
2431
|
|
|
2432
|
+
// 如果前端发的是掩码(***xxx),从当前配置里取真实 key
|
|
2433
|
+
const currentConfig = await llmConfigStore.getProvider(provider as ModelProvider);
|
|
2434
|
+
if (currentConfig && config.apiKey && config.apiKey.startsWith('***')) {
|
|
2435
|
+
config.apiKey = currentConfig.apiKey;
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2215
2438
|
await llmConfigStore.updateProvider(provider, config);
|
|
2216
2439
|
|
|
2217
2440
|
// 如果是活跃供应商,重新初始化 Pi SDK
|