agent-loft 0.2.121 → 0.2.123
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/package.json +1 -1
- package/src/channels/feishu/bot.ts +57 -23
- package/src/channels/feishu/index.ts +3 -1
- package/src/channels/web/access.ts +8 -5
- package/src/channels/web/http.ts +1 -1
- package/src/core/db.ts +3 -0
- package/src/core/hub.ts +16 -5
- package/src/core/store.ts +13 -1
- package/src/core/task-runner.ts +1 -0
- package/src/core/tasks.ts +2 -0
package/package.json
CHANGED
|
@@ -9,12 +9,12 @@ import path from "node:path";
|
|
|
9
9
|
import * as Lark from "@larksuiteoapi/node-sdk";
|
|
10
10
|
import { config, type FeishuBotConfig } from "../../core/config.js";
|
|
11
11
|
import { botAgentIds, botExpertIds } from "../../core/feishu-experts.js";
|
|
12
|
-
import { hub, type CommandReply, type EventEnvelope } from "../../core/hub.js";
|
|
12
|
+
import { GLOBAL_ALWAYS_DENIED, hub, type CommandReply, type EventEnvelope } from "../../core/hub.js";
|
|
13
13
|
import { interactions, type Interaction } from "../../core/interactions.js";
|
|
14
14
|
import { parseCommand } from "../../core/commands.js";
|
|
15
15
|
import { Dedup } from "../../core/dedup.js";
|
|
16
16
|
import { isPaired, requestPairing } from "../../core/pairing.js";
|
|
17
|
-
import { getConversation, listConversations, sessionOwner, updateConversation } from "../../core/store.js";
|
|
17
|
+
import { assistantSessionOwner, getConversation, listConversations, sessionOwner, updateConversation, type Conversation } from "../../core/store.js";
|
|
18
18
|
import { adminVisible, getUser, hasMembers, ownedBy, type User } from "../../core/users.js";
|
|
19
19
|
import { linkedUser, redeemLinkCode } from "../../core/feishu-links.js";
|
|
20
20
|
import { adoptFile, KIND_LABEL } from "../../core/uploads.js";
|
|
@@ -151,6 +151,7 @@ export class FeishuBot {
|
|
|
151
151
|
const [key] = args;
|
|
152
152
|
const selected = this.expertsFor(key);
|
|
153
153
|
const conv = getConversation(key);
|
|
154
|
+
if (conv?.assistant && !this.controls(key, args[2].chatId)) return;
|
|
154
155
|
if (selected !== undefined && (!conv || !selected.includes(conv.expert ?? conv.agent))) {
|
|
155
156
|
hub.notice(key, "这个聊天的专家已不在机器人的可用列表中,请重新发消息以选择默认专家。");
|
|
156
157
|
return;
|
|
@@ -171,21 +172,26 @@ export class FeishuBot {
|
|
|
171
172
|
}
|
|
172
173
|
|
|
173
174
|
/** feishuKey 这个聊天能不能碰主人是 owner 的东西(Web 会话、agent 会话) */
|
|
174
|
-
private canSee(feishuKey: string,
|
|
175
|
+
private canSee(feishuKey: string, conv: Conversation | undefined): boolean {
|
|
176
|
+
if (!conv) return false;
|
|
175
177
|
const viewer = this.viewerOf(feishuKey);
|
|
176
|
-
|
|
178
|
+
if (conv.assistant) {
|
|
179
|
+
const openId = getConversation(feishuKey)?.boundOpenId;
|
|
180
|
+
return !!viewer && viewer.id === conv.owner && linkedUser(this.cfg.id, openId)?.id === viewer.id;
|
|
181
|
+
}
|
|
182
|
+
return viewer ? ownedBy(viewer, conv.owner) : adminVisible(conv.owner);
|
|
177
183
|
}
|
|
178
184
|
|
|
179
|
-
/** 命令里要用的可见范围:/sessions 列哪些、/resume 能恢复哪些、/approve 能处理哪些 */
|
|
180
185
|
private commandScope(feishuKey: string) {
|
|
181
186
|
const viewer = this.viewerOf(feishuKey);
|
|
187
|
+
const members = hasMembers();
|
|
182
188
|
return {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
convVisible: (k: string) => this.canSee(feishuKey, getConversation(k)
|
|
189
|
+
sessionVisible: (id: string) => {
|
|
190
|
+
const owner = assistantSessionOwner(id);
|
|
191
|
+
if (owner) return viewer?.id === owner && linkedUser(this.cfg.id, getConversation(feishuKey)?.boundOpenId)?.id === owner;
|
|
192
|
+
return viewer ? ownedBy(viewer, sessionOwner(id)) : !members || adminVisible(sessionOwner(id));
|
|
193
|
+
},
|
|
194
|
+
convVisible: (k: string) => this.canSee(feishuKey, getConversation(k)),
|
|
189
195
|
};
|
|
190
196
|
}
|
|
191
197
|
|
|
@@ -197,13 +203,20 @@ export class FeishuBot {
|
|
|
197
203
|
if (!target) return undefined;
|
|
198
204
|
const conv = getConversation(target);
|
|
199
205
|
// 这个聊天不再能碰它了(主人被降成了普通成员、私聊换了主人):当它没了(下条消息时自动退回来)
|
|
200
|
-
return conv && !conv.hidden && this.canSee(feishuKey, conv
|
|
206
|
+
return conv && !conv.hidden && this.canSee(feishuKey, conv) ? target : undefined;
|
|
201
207
|
}
|
|
202
208
|
|
|
203
209
|
/** 挂在 Web 会话 key 上的本机器人聊天(只算还能碰它的) */
|
|
204
210
|
private watchers(key: string): string[] {
|
|
205
|
-
const
|
|
206
|
-
|
|
211
|
+
const conv = getConversation(key);
|
|
212
|
+
if (!conv || conv.hidden) return [];
|
|
213
|
+
return watchersOf(key, this.prefix).filter((k) => this.canSee(k, conv));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** 绑定私聊已经通过续聊收到这次回复时,免去第二条任务推送。 */
|
|
217
|
+
deliversAssistantReply(key: string, openId: string): boolean {
|
|
218
|
+
return !!getConversation(key)?.assistant && this.watchers(key).some((k) =>
|
|
219
|
+
getConversation(k)?.boundOpenId?.trim().toLowerCase() === openId.trim().toLowerCase());
|
|
207
220
|
}
|
|
208
221
|
|
|
209
222
|
/** 这条会话的事件该画到哪些飞书聊天里:自己的那个聊天,加上挂着它的聊天 */
|
|
@@ -270,7 +283,11 @@ export class FeishuBot {
|
|
|
270
283
|
async attachTo(feishuKey: string, webKey: string, operatorOpenId?: string): Promise<string> {
|
|
271
284
|
const conv = getConversation(webKey);
|
|
272
285
|
// 只能接上这个聊天的主人自己的 Web 会话(没主人的聊天:管理员能看的)
|
|
273
|
-
if (!conv || conv.channel !== "web" || conv.hidden || !this.canSee(feishuKey, conv
|
|
286
|
+
if (!conv || conv.channel !== "web" || conv.hidden || !this.canSee(feishuKey, conv)) throw new Error("这条 Web 会话已不存在");
|
|
287
|
+
if (conv.assistant) {
|
|
288
|
+
const issue = hub.assistantUnavailable(conv);
|
|
289
|
+
if (issue) throw new Error(issue);
|
|
290
|
+
}
|
|
274
291
|
const chatId = this.chatIdOf(feishuKey);
|
|
275
292
|
// 本聊天自己的会话也得有:之后 /detach 回来、侧栏里显示机器人都要它
|
|
276
293
|
const allowed = this.allowedAgents();
|
|
@@ -289,6 +306,7 @@ export class FeishuBot {
|
|
|
289
306
|
console.log(`${this.tag} ${feishuKey} 接上 Web 会话 ${webKey}`);
|
|
290
307
|
// 最近几句:读历史可能要一会儿,读不到也不影响接上
|
|
291
308
|
const history = await hub.history(webKey).catch(() => []);
|
|
309
|
+
if (conv.assistant && this.attachedWeb(feishuKey) !== webKey) throw new Error("会话绑定已变化");
|
|
292
310
|
const recent = history
|
|
293
311
|
.filter((m) => m.role !== "question" && m.text.trim())
|
|
294
312
|
.slice(-4)
|
|
@@ -309,7 +327,8 @@ export class FeishuBot {
|
|
|
309
327
|
const prev = detach(feishuKey);
|
|
310
328
|
if (!prev) return "这个聊天没有接着 Web 会话,本来就在用自己的会话。";
|
|
311
329
|
console.log(`${this.tag} ${feishuKey} 不再接着 Web 会话 ${prev}`);
|
|
312
|
-
const
|
|
330
|
+
const conv = getConversation(prev);
|
|
331
|
+
const title = this.canSee(feishuKey, conv) ? conv?.title : undefined;
|
|
313
332
|
return `已回到这个聊天自己的会话${title ? `,不再接着「${title}」` : ""}。之后发 \`/web\` 可以再接上。`;
|
|
314
333
|
}
|
|
315
334
|
|
|
@@ -317,7 +336,7 @@ export class FeishuBot {
|
|
|
317
336
|
private async sendWebList(feishuKey: string, chatId: string): Promise<void> {
|
|
318
337
|
const current = this.attachedWeb(feishuKey);
|
|
319
338
|
const convs = hub
|
|
320
|
-
.list((c) => c.channel === "web" && !c.hidden && this.canSee(feishuKey, c
|
|
339
|
+
.list((c) => c.channel === "web" && !c.hidden && this.canSee(feishuKey, c))
|
|
321
340
|
.slice(0, 10)
|
|
322
341
|
.map((c) => this.webEntry(c.key)!)
|
|
323
342
|
.filter(Boolean);
|
|
@@ -339,7 +358,7 @@ export class FeishuBot {
|
|
|
339
358
|
detach(feishuKey);
|
|
340
359
|
void this.client.sendCard(
|
|
341
360
|
this.chatIdOf(feishuKey),
|
|
342
|
-
replyCard("之前接着的 Web 会话已经被删除,已回到这个聊天自己的会话。"),
|
|
361
|
+
replyCard(getConversation(target)?.assistant ? "之前接着的私人会话已不可用,已回到这个聊天自己的会话。" : "之前接着的 Web 会话已经被删除,已回到这个聊天自己的会话。"),
|
|
343
362
|
);
|
|
344
363
|
return true;
|
|
345
364
|
}
|
|
@@ -492,11 +511,12 @@ export class FeishuBot {
|
|
|
492
511
|
const id = pairKey(key, chatId);
|
|
493
512
|
let stream = this.streams.get(id);
|
|
494
513
|
if (stream) return stream;
|
|
514
|
+
const allowed = () => !getConversation(key)?.assistant || this.controls(key, chatId);
|
|
495
515
|
stream = new TurnStream({
|
|
496
|
-
sendCard: (card) => this.client.sendCard(chatId, card),
|
|
497
|
-
updateCard: (messageId, card) => this.client.updateCard(messageId, card),
|
|
516
|
+
sendCard: async (card) => allowed() ? this.client.sendCard(chatId, card) : undefined,
|
|
517
|
+
updateCard: async (messageId, card) => allowed() && this.client.updateCard(messageId, card),
|
|
498
518
|
renderProgress: (steps, done, footer) => progressCard({ steps, done, ...(footer ? { footer } : {}) }),
|
|
499
|
-
sendReply: (text) => this.client.sendCard(chatId, replyCard(text)),
|
|
519
|
+
sendReply: async (text) => allowed() ? this.client.sendCard(chatId, replyCard(text)) : undefined,
|
|
500
520
|
onError: (err) => console.error(`${this.tag} 卡片更新失败:`, err),
|
|
501
521
|
});
|
|
502
522
|
this.streams.set(id, stream);
|
|
@@ -571,6 +591,7 @@ export class FeishuBot {
|
|
|
571
591
|
}
|
|
572
592
|
|
|
573
593
|
private async showInteractionIn(i: Interaction, chatId: string): Promise<void> {
|
|
594
|
+
if (getConversation(i.convKey)?.assistant && !this.controls(i.convKey, chatId)) return;
|
|
574
595
|
const messageId = await this.client.sendCard(chatId, this.interactionCard(i, chatId));
|
|
575
596
|
if (!messageId) return;
|
|
576
597
|
const list = this.cards.get(i.id) ?? [];
|
|
@@ -585,7 +606,7 @@ export class FeishuBot {
|
|
|
585
606
|
const list = this.cards.get(i.id);
|
|
586
607
|
if (!list) return;
|
|
587
608
|
if (i.settled) this.cards.delete(i.id);
|
|
588
|
-
await Promise.all(list.map((c) => this.client.updateCard(c.messageId, this.interactionCard(i, c.chatId))));
|
|
609
|
+
await Promise.all(list.filter((c) => !getConversation(i.convKey)?.assistant || this.controls(i.convKey, c.chatId)).map((c) => this.client.updateCard(c.messageId, this.interactionCard(i, c.chatId))));
|
|
589
610
|
}
|
|
590
611
|
|
|
591
612
|
|
|
@@ -671,6 +692,7 @@ export class FeishuBot {
|
|
|
671
692
|
// 群聊不动:群里不止一个人
|
|
672
693
|
const p2p = message.chat_type === "p2p";
|
|
673
694
|
const ownerChanged = p2p && hub.reassignOwner(ownKey, linkedUser(this.cfg.id, operator)?.id);
|
|
695
|
+
if (p2p && conv.owner && conv.boundOpenId !== operator) updateConversation(ownKey, { boundOpenId: operator });
|
|
674
696
|
if (ownerChanged) {
|
|
675
697
|
const owner = this.viewerOf(ownKey);
|
|
676
698
|
console.log(`${this.tag} 私聊 ${ownKey} 的主人变为 ${owner?.name ?? "(无,归管理员)"}`);
|
|
@@ -798,6 +820,7 @@ export class FeishuBot {
|
|
|
798
820
|
if (!sender.p2p) return "请在和我的私聊里发 `/bind 绑定码`(群里别人也看得到绑定码)。绑定码已经露出来了的话,到 Web 上重新生成一个。";
|
|
799
821
|
const user = redeemLinkCode(code, this.cfg.id, sender.openId);
|
|
800
822
|
hub.reassignOwner(ownKey, user.id);
|
|
823
|
+
updateConversation(ownKey, { boundOpenId: sender.openId });
|
|
801
824
|
const selected = this.expertsFor(ownKey);
|
|
802
825
|
if (selected !== undefined) {
|
|
803
826
|
const choices = hub.expertChoices(user.id, this.allowedAgents(), selected);
|
|
@@ -815,6 +838,7 @@ export class FeishuBot {
|
|
|
815
838
|
}
|
|
816
839
|
|
|
817
840
|
private async renderReply(key: string, chatId: string, reply: CommandReply): Promise<void> {
|
|
841
|
+
if (getConversation(key)?.assistant && !this.controls(key, chatId)) return;
|
|
818
842
|
const ctx = this.cardCtx(key, chatId);
|
|
819
843
|
switch (reply.type) {
|
|
820
844
|
case "text":
|
|
@@ -920,6 +944,16 @@ export class FeishuBot {
|
|
|
920
944
|
...(i?.settled ? { card: { type: "raw", data: settledCard(i) } } : {}),
|
|
921
945
|
});
|
|
922
946
|
|
|
947
|
+
const denied = { toast: { type: "error" as const, content: "此聊天已不能操作该私人会话" } };
|
|
948
|
+
const request = "r" in envelope ? interactions.get(envelope.r) : undefined;
|
|
949
|
+
const targetKey = "k" in envelope ? envelope.k : request?.convKey;
|
|
950
|
+
const target = targetKey ? getConversation(targetKey) : undefined;
|
|
951
|
+
if (target?.assistant && (!this.controls(target.key, chatId) || linkedUser(this.id, operatorOpenId)?.id !== target.owner)) return denied;
|
|
952
|
+
|
|
953
|
+
if (target?.assistant && envelope.a === "approval" && envelope.d === "allow-always"
|
|
954
|
+
&& request?.spec.kind === "approval" && request.spec.alwaysGlobal && getUser(target.owner)?.role !== "admin") {
|
|
955
|
+
return { toast: { type: "error", content: GLOBAL_ALWAYS_DENIED } };
|
|
956
|
+
}
|
|
923
957
|
switch (envelope.a) {
|
|
924
958
|
case "approval": {
|
|
925
959
|
const settled = interactions.decide(envelope.r, envelope.d, by);
|
|
@@ -1033,7 +1067,7 @@ export class FeishuBot {
|
|
|
1033
1067
|
}
|
|
1034
1068
|
const chat = chatId!;
|
|
1035
1069
|
void task()
|
|
1036
|
-
.then((text) => this.
|
|
1070
|
+
.then((text) => this.renderReply(key, chat, { type: "text", text }))
|
|
1037
1071
|
.catch((err) => this.client.sendText(chat, `出错了:${err instanceof Error ? err.message : String(err)}`));
|
|
1038
1072
|
return { toast: { type: "info", content: toast } };
|
|
1039
1073
|
}
|
|
@@ -55,7 +55,9 @@ function onTaskResult(e: TaskTurnEnd): void {
|
|
|
55
55
|
console.warn(`[飞书] 定时任务「${task.title}」要推送到飞书,但${task.feishuBot ? "指定的机器人不在线或已解绑" : "主人没有绑定在线的飞书机器人"}`);
|
|
56
56
|
return;
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
const bot = bots.get(link.botId)!;
|
|
59
|
+
if (bot.deliversAssistantReply(e.key, link.openId)) return;
|
|
60
|
+
void bot.client.sendCard(link.openId, replyCard(`⏰ **${e.title}**\n\n${e.text}`), "open_id");
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
export function startFeishu(): void {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Web 上「这个用户能碰哪些东西」。规则本身在 core/users.ts 的 ownedBy。
|
|
2
|
-
import { getConversation, sessionOwner, type Conversation } from "../../core/store.js";
|
|
2
|
+
import { assistantSessionOwner, getConversation, sessionOwner, type Conversation } from "../../core/store.js";
|
|
3
3
|
import { legacyOwnerId, listUsers, ownedBy, type User } from "../../core/users.js";
|
|
4
4
|
import type { OwnerFilter } from "./push.js";
|
|
5
5
|
|
|
6
6
|
export function canSeeConv(user: User, conv: Conversation | undefined): conv is Conversation {
|
|
7
|
-
return !!conv && ownedBy(user, conv.owner);
|
|
7
|
+
return !!conv && (conv.assistant ? conv.owner === user.id : ownedBy(user, conv.owner));
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export function canSeeKey(user: User, key: string): boolean {
|
|
@@ -13,11 +13,14 @@ export function canSeeKey(user: User, key: string): boolean {
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* 这个用户能看到、能恢复哪些 agent 会话。
|
|
16
|
-
*
|
|
16
|
+
* 单用户可恢复旧的无主会话,bot 的历史始终属于原来的账号。
|
|
17
17
|
*/
|
|
18
18
|
export function sessionVisibleTo(user: User): ((sessionId: string) => boolean) | undefined {
|
|
19
|
-
|
|
20
|
-
return (
|
|
19
|
+
const alone = listUsers().every((u) => u.id === user.id);
|
|
20
|
+
return (id) => {
|
|
21
|
+
const owner = assistantSessionOwner(id);
|
|
22
|
+
return owner ? owner === user.id : alone || ownedBy(user, sessionOwner(id));
|
|
23
|
+
};
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
/** 推送订阅是不是这个用户的。升级前的订阅没记主人,算迁移出来的那个管理员的 */
|
package/src/channels/web/http.ts
CHANGED
|
@@ -85,7 +85,7 @@ export async function readBody(req: IncomingMessage, limit: number): Promise<Buf
|
|
|
85
85
|
let size = 0;
|
|
86
86
|
for await (const chunk of req) {
|
|
87
87
|
size += (chunk as Buffer).length;
|
|
88
|
-
if (size > limit) throw new HttpError(413, `内容太大(上限 ${Math.round(limit / 1024 / 1024)}
|
|
88
|
+
if (size > limit) throw new HttpError(413, `内容太大(上限 ${limit < 1024 * 1024 ? Math.ceil(limit / 1024) + " KB" : Math.round(limit / 1024 / 1024) + " MB"})`);
|
|
89
89
|
chunks.push(chunk as Buffer);
|
|
90
90
|
}
|
|
91
91
|
return Buffer.concat(chunks);
|
package/src/core/db.ts
CHANGED
|
@@ -46,6 +46,9 @@ CREATE TABLE IF NOT EXISTS conversations (
|
|
|
46
46
|
data TEXT NOT NULL
|
|
47
47
|
);
|
|
48
48
|
|
|
49
|
+
-- 保留 0.2.122 创建的私人会话的历史归属。
|
|
50
|
+
CREATE TABLE IF NOT EXISTS assistant_sessions (session_id TEXT PRIMARY KEY, owner TEXT NOT NULL);
|
|
51
|
+
|
|
49
52
|
CREATE TABLE IF NOT EXISTS session_owners (
|
|
50
53
|
session_id TEXT PRIMARY KEY,
|
|
51
54
|
user_id TEXT NOT NULL
|
package/src/core/hub.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { recordUsage, usageOf, type UsageTotals } from "./usage.js";
|
|
|
20
20
|
import { interactions, SETTLEMENT_LABEL } from "./interactions.js";
|
|
21
21
|
import {
|
|
22
22
|
agentBinding,
|
|
23
|
+
assistantSessionOwner,
|
|
23
24
|
ensureConversation,
|
|
24
25
|
expertIdOf,
|
|
25
26
|
getConversation,
|
|
@@ -512,6 +513,13 @@ class Hub extends EventEmitter<{
|
|
|
512
513
|
|
|
513
514
|
// ---------- 专家 ----------
|
|
514
515
|
|
|
516
|
+
assistantUnavailable(conv: Conversation): string | undefined {
|
|
517
|
+
if (!canUseExpert(conv.owner, expertIdOf(conv))) return "专家已删除或使用权限已撤销";
|
|
518
|
+
if (conv.expert && getExpert(conv.expert)?.engine !== conv.agent) return "专家已更换引擎";
|
|
519
|
+
if (!this.enabledAgents().some((driver) => driver.id === conv.agent)) return "引擎已停用";
|
|
520
|
+
return undefined;
|
|
521
|
+
}
|
|
522
|
+
|
|
515
523
|
/** 对话的专家,给顶栏和列表看 */
|
|
516
524
|
expertBadge(conv: Pick<Conversation, "agent" | "expert" | "owner" | "channel">): ExpertBadge {
|
|
517
525
|
if (!conv.expert) return { id: conv.agent, name: this.label(conv.agent), builtin: true, ...(!canUseExpert(memoryOwnerOf(conv), conv.agent) ? { missing: true } : {}) };
|
|
@@ -964,7 +972,7 @@ class Hub extends EventEmitter<{
|
|
|
964
972
|
// 分叉中还报的是原会话 id:分叉尚未生效,什么都不改
|
|
965
973
|
if (forkFrom && sessionId === forkFrom) return;
|
|
966
974
|
updateAgentBinding(key, agent, { sessionId, forkFrom: undefined });
|
|
967
|
-
recordSessionOwner(sessionId, current?.owner);
|
|
975
|
+
recordSessionOwner(sessionId, current?.owner, !!current?.assistant);
|
|
968
976
|
this.emitEvent(key, { type: "meta" });
|
|
969
977
|
},
|
|
970
978
|
},
|
|
@@ -1499,7 +1507,7 @@ class Hub extends EventEmitter<{
|
|
|
1499
1507
|
tags.push(tag);
|
|
1500
1508
|
this.turnTags.set(key, tags);
|
|
1501
1509
|
const fifo = this.turnFifo.get(key) ?? [];
|
|
1502
|
-
fifo.push({
|
|
1510
|
+
fifo.push({ ...msg, input });
|
|
1503
1511
|
this.turnFifo.set(key, fifo);
|
|
1504
1512
|
try {
|
|
1505
1513
|
session.send(input);
|
|
@@ -1594,6 +1602,8 @@ class Hub extends EventEmitter<{
|
|
|
1594
1602
|
|
|
1595
1603
|
private async resumeNow(key: string, sessionId: string, originCwd: string | undefined, op: SwitchOp): Promise<string> {
|
|
1596
1604
|
const conv = getConversation(key)!;
|
|
1605
|
+
const privateOwner = assistantSessionOwner(sessionId);
|
|
1606
|
+
if (privateOwner && (!conv.assistant || conv.owner !== privateOwner)) return "请在本人的私人对话里恢复这段历史。";
|
|
1597
1607
|
const driver = this.requireDriver(conv.agent);
|
|
1598
1608
|
if (!(await withTimeout(driver.sessionExists(sessionId, conv.cwd), "查询会话"))) {
|
|
1599
1609
|
return `找不到会话 ${sessionId}。\n发 /sessions 看看有哪些可恢复的。`;
|
|
@@ -2052,7 +2062,7 @@ class Hub extends EventEmitter<{
|
|
|
2052
2062
|
this.markSwitched(key, op.seq);
|
|
2053
2063
|
updateAgentBinding(key, conv.agent, { sessionId, forkFrom: undefined });
|
|
2054
2064
|
if (sessionId) {
|
|
2055
|
-
recordSessionOwner(sessionId, conv.owner);
|
|
2065
|
+
recordSessionOwner(sessionId, conv.owner, !!conv.assistant);
|
|
2056
2066
|
for (const m of kept) if (m.footer) recordFooter(sessionId, m.text, m.footer, m.changes);
|
|
2057
2067
|
}
|
|
2058
2068
|
const owner = memoryOwnerOf(conv);
|
|
@@ -2270,8 +2280,8 @@ class Hub extends EventEmitter<{
|
|
|
2270
2280
|
copyFooters(source, sessionId);
|
|
2271
2281
|
owner ??= conv.owner;
|
|
2272
2282
|
const newKey = `web:${crypto.randomBytes(6).toString("hex")}`;
|
|
2273
|
-
ensureConversation({ key: newKey, channel: "web", channelId: "web", agent: conv.agent, ...(owner ? { owner } : {}) });
|
|
2274
|
-
recordSessionOwner(sessionId, owner);
|
|
2283
|
+
ensureConversation({ key: newKey, channel: "web", channelId: "web", agent: conv.agent, ...(conv.assistant ? { assistant: "related" } : {}), ...(owner ? { owner } : {}) });
|
|
2284
|
+
recordSessionOwner(sessionId, owner, !!conv.assistant);
|
|
2275
2285
|
updateConversation(newKey, {
|
|
2276
2286
|
title: `${conv.title ?? "会话"} · 分叉`.slice(0, 80),
|
|
2277
2287
|
cwd: conv.cwd,
|
|
@@ -2348,6 +2358,7 @@ class Hub extends EventEmitter<{
|
|
|
2348
2358
|
channel: "web",
|
|
2349
2359
|
channelId: "web",
|
|
2350
2360
|
agent: target.agent,
|
|
2361
|
+
...(conv.assistant ? { assistant: "related" } : {}),
|
|
2351
2362
|
...(target.expert ? { expert: target.expert } : {}),
|
|
2352
2363
|
...(owner ? { owner } : {}),
|
|
2353
2364
|
});
|
package/src/core/store.ts
CHANGED
|
@@ -53,6 +53,8 @@ export type Conversation = {
|
|
|
53
53
|
cwd: string;
|
|
54
54
|
/** cwd 是否用户显式指定。没指定过的跟随 workspaceRoot 变化 */
|
|
55
55
|
cwdExplicit?: boolean;
|
|
56
|
+
/** 0.2.122 的个人 bot 及派生会话沿用仅主人可见的权限。 */
|
|
57
|
+
assistant?: "active" | "removed" | "related";
|
|
56
58
|
perAgent: Record<string, AgentBinding>;
|
|
57
59
|
/** 最后一次有人说话的时间,用于闲置过期 */
|
|
58
60
|
activeAt: number;
|
|
@@ -80,6 +82,8 @@ export type Conversation = {
|
|
|
80
82
|
* 账号时记下(见 core/feishu-links.ts)。飞书群聊、没绑定的私聊和升级前就有的 Web 会话没有,归管理员。
|
|
81
83
|
*/
|
|
82
84
|
owner?: string;
|
|
85
|
+
/** 已绑定飞书私聊的发送人,用于再次核对 bot 消息的目的地。 */
|
|
86
|
+
boundOpenId?: string;
|
|
83
87
|
/** 定时任务的专用对话:是哪个任务的(见 core/tasks.ts)。任务删了就清掉 */
|
|
84
88
|
taskId?: string;
|
|
85
89
|
};
|
|
@@ -202,6 +206,7 @@ export function ensureConversation(params: {
|
|
|
202
206
|
title?: string;
|
|
203
207
|
owner?: string;
|
|
204
208
|
cwd?: string;
|
|
209
|
+
assistant?: Conversation["assistant"];
|
|
205
210
|
}): Conversation {
|
|
206
211
|
const existing = getConversation(params.key);
|
|
207
212
|
if (existing) return existing;
|
|
@@ -214,6 +219,7 @@ export function ensureConversation(params: {
|
|
|
214
219
|
...(params.expert ? { expert: params.expert } : {}),
|
|
215
220
|
cwd: params.cwd ?? config().workspaceRoot,
|
|
216
221
|
...(params.cwd && params.cwd !== config().workspaceRoot ? { cwdExplicit: true } : {}),
|
|
222
|
+
...(params.assistant ? { assistant: params.assistant } : {}),
|
|
217
223
|
perAgent: {},
|
|
218
224
|
activeAt: now,
|
|
219
225
|
createdAt: now,
|
|
@@ -281,11 +287,17 @@ export function putConversation(conv: Conversation): void {
|
|
|
281
287
|
// 记下每个 agent 会话 id 是哪个用户的对话里开出来的。没记过的(飞书、终端里开的、升级前的)归管理员。
|
|
282
288
|
|
|
283
289
|
/** 第一次见到这个会话 id 时记下主人;已经有主了不改 */
|
|
284
|
-
export function recordSessionOwner(sessionId: string | undefined, userId: string | undefined): void {
|
|
290
|
+
export function recordSessionOwner(sessionId: string | undefined, userId: string | undefined, privateAssistant = false): void {
|
|
285
291
|
if (!sessionId || !userId) return;
|
|
292
|
+
if (privateAssistant) sql("INSERT OR IGNORE INTO assistant_sessions (session_id, owner) VALUES (?, ?)").run(sessionId, userId);
|
|
286
293
|
sql("INSERT OR IGNORE INTO session_owners (session_id, user_id) VALUES (?, ?)").run(sessionId, userId);
|
|
287
294
|
}
|
|
288
295
|
|
|
296
|
+
export function assistantSessionOwner(sessionId: string): string | undefined {
|
|
297
|
+
const row = sql("SELECT owner FROM assistant_sessions WHERE session_id = ?").get(sessionId) as { owner: string } | undefined;
|
|
298
|
+
return row?.owner;
|
|
299
|
+
}
|
|
300
|
+
|
|
289
301
|
export function sessionOwner(sessionId: string): string | undefined {
|
|
290
302
|
const row = sql("SELECT user_id FROM session_owners WHERE session_id = ?").get(sessionId) as { user_id: string } | undefined;
|
|
291
303
|
return row?.user_id;
|
package/src/core/task-runner.ts
CHANGED
|
@@ -85,6 +85,7 @@ function resolveConv(task: Task): Resolved {
|
|
|
85
85
|
...(task.expert ? { expert: task.expert } : {}),
|
|
86
86
|
...(cwd ? { cwd } : {}),
|
|
87
87
|
owner: task.owner,
|
|
88
|
+
...(task.assistantKey ? { assistant: "related" } : {}),
|
|
88
89
|
title: task.title.slice(0, 80),
|
|
89
90
|
});
|
|
90
91
|
updateConversation(key, { titleAuto: false, taskId: task.id });
|
package/src/core/tasks.ts
CHANGED