Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(chat): auto handoff after a cold start, only where plans have a kind

Chrisa6589c4dfc9a
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
index 7181cb0358..040ce082d5 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -186,8 +186,10 @@ describe('Chat Server result delivery and refresh', () => {
async function serve(plan: Record<string, unknown>, settings: { autoContinue?: boolean; startedHere?: string[] } | null,
onContinue?: () => { status: number; body: unknown }, onRead?: () => void) {
if (settings) workspaceSettings.set(roomId, { projectDir: null, plans: {}, startedHere: [planId], ...settings });
+ // Only a server that tells a 分工 from a queue (groupSingleSpeaker) offers the switch.
fixture.handle.mockImplementation((route, method) => route === `/conversations/${roomId}/plans/${planId}` && method === 'POST'
- ? onContinue?.() ?? { body: {} } : route.startsWith(`/conversations/${roomId}/plans`) ? (onRead?.(), { body: [plan] }) : response(route));
+ ? onContinue?.() ?? { body: {} } : route.startsWith(`/conversations/${roomId}/plans`) ? (onRead?.(), { body: [plan] })
+ : route === '/me' ? { body: { actor: { id: selfId }, capabilities: { groupSingleSpeaker: 1 } } } : response(route));
await service.getGroup(roomId);
await vi.advanceTimersByTimeAsync(2000);
}
@@ -196,6 +198,10 @@ describe('Chat Server result delivery and refresh', () => {
await stepEnded('2');
expect(continues()).toHaveLength(1);
expect(continues()[0]![2]).toMatchObject({ expectedRevision: 3 });
+ });
+ it('goes on with a step that ended before the room was first read (a cold start)', async () => {
+ await serve(division(), { autoContinue: true });
+ expect(continues()).toHaveLength(1);
// The same finished step is not continued twice when later changes arrive.
await stepEnded('3');
expect(continues()).toHaveLength(1);
@@ -228,13 +234,17 @@ describe('Chat Server result delivery and refresh', () => {
expect(after.ok && [after.group.autoContinue, after.group.plans[0]?.autoContinue]).toEqual([true, true]);
});
it('does not go on when the switch is turned off while the 分工 is read', async () => {
- let reads = 0;
await serve(division(), { autoContinue: true }, undefined, () => {
- if (++reads > 1) workspaceSettings.set(roomId, { ...workspaceSettings.get(roomId) as object, autoContinue: false });
+ workspaceSettings.set(roomId, { ...workspaceSettings.get(roomId) as object, autoContinue: false });
});
await stepEnded('2');
expect(continues()).toHaveLength(0);
});
+ it('does not offer the switch on a server that cannot tell a 分工 from a queue', async () => {
+ workspaceSettings.set(roomId, { projectDir: null, plans: {}, autoContinue: true });
+ const result = await service.getGroup(roomId);
+ expect(result.ok && result.group).not.toHaveProperty('autoContinue');
+ });
it('does not try again after someone else went on first', async () => {
await serve(division(), { autoContinue: true }, () => ({ status: 409, body: { error: { code: 'REVISION_CONFLICT' } } }));
await stepEnded('2');
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index bc22e9c12b..381e61972b 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -861,6 +861,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
function subscribe(roomId: string, cursor = '0') {
const fresh = !rooms.has(roomId); advanceRoom(roomId, cursor);
if (fresh && connected && socket?.readyState === WebSocket.OPEN) socket.send(JSON.stringify({ type: 'subscribe', scope: `conversation:${roomId}`, after: cursor }));
+ // A step that ended while nothing followed the room sends no event from this cursor on.
+ if (fresh) autoContinuePlans(roomId);
}
function connect() {
if (!current()) return;
@@ -1329,7 +1331,9 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
...(m.ownerActorId === selfId && m.kind === 'bot' ? { homeDeviceId: actors.find(a => a.id === m.id)?.homeDeviceId ?? null } : {}),
avatarUrl: m.avatar?.startsWith('https://') ? m.avatar : null,
name: memberName(m), avatar: m.groupAvatar ? '' : p?.avatar ?? (m.avatar?.startsWith('https://') ? '' : m.avatar) ?? '', avatarColor: p?.avatarColor ?? 'violet', status: p?.status ?? 'active' };
- }), organizerBotId: ((id?: string) => id ? localBot(id)?.id ?? id : null)(s.room.organizer_id ?? s.members.find(m => m.kind === 'bot' && m.state === 'joined')?.id), projectDir: workspace?.projectDir ?? null, autoContinue: workspace?.autoContinue === true, lastMessage: last && lastView ? {
+ }), organizerBotId: ((id?: string) => id ? localBot(id)?.id ?? id : null)(s.room.organizer_id ?? s.members.find(m => m.kind === 'bot' && m.state === 'joined')?.id), projectDir: workspace?.projectDir ?? null,
+ // Only servers that tell a 分工 from a queue (kind) can hand one on by itself.
+ ...(singleSpeaker ? { autoContinue: workspace?.autoContinue === true } : {}), lastMessage: last && lastView ? {
isSelf: lastView.isSelf, authorKind: lastView.authorKind, authorName: lastView.authorName, noticeCode: lastView.noticeCode,
preview: lastIsShare ? '' : (botGroupMessageSummaryText(lastView) || mediaNames(last)).slice(0, 80), createdAt: lastView.createdAt, ...(lastIsShare ? { threadShare: true } : {}),
} : null, speakingBotIds: speakers.map(s => s.botId), planningBotId: planning.get(roomId)?.botId ?? null, openPlan: open ? { id: open.id, status: open.status, currentStep: open.currentStep, stepCount: open.steps.length, currentBotName: open.steps[open.currentStep ?? 0]?.botName ?? null, currentStepStatus: open.steps[open.currentStep ?? 0]?.status ?? null, ...(open.kind ? { kind: open.kind } : {}) } : null,
@@ -1387,9 +1391,12 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
function startedHere(plan: ServerPlan, workspace: ReturnType<ReturnType<typeof chatServerWorkspaces>['read']>) {
return plan.kind === 'work' && plan.creator_id === selfId && workspace?.startedHere?.includes(plan.id) === true;
}
+ /** The card already exists: failing to remember it only leaves it to manual 继续. */
function markStartedHere(roomId: string, planId: string) {
- const settings = workspaces().read(roomId) ?? { projectDir: null, plans: {} };
- workspaces().save(roomId, { ...settings, startedHere: [...(settings.startedHere ?? []).filter(known => known !== planId), planId].slice(-50) });
+ try {
+ const settings = workspaces().read(roomId) ?? { projectDir: null, plans: {} };
+ workspaces().save(roomId, { ...settings, startedHere: [...(settings.startedHere ?? []).filter(known => known !== planId), planId].slice(-50) });
+ } catch (error) { deps.log?.warn('Group 分工 origin could not be saved', { groupId: roomId, ...chatErrorDiagnostic(error) }); }
}
const autoContinued = new Set<string>();
function autoContinuePlans(roomId: string) {
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botGroupMentions.test.ts b/apps/desktop/src/renderer/features/bots/__tests__/botGroupMentions.test.ts
index e0337a84d4..ab29c85d79 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botGroupMentions.test.ts
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGroupMentions.test.ts
@@ -190,6 +190,9 @@ describe('splitBotGroupRelaySteps', () => {
['punctuation and connectors', '做登录页:@阿布 出方案,然后 @小满 实现;最后 @咪咪 测一遍。',
[['abu', '出方案'], ['xiaoman', '实现'], ['mimi', '测一遍']]],
['one teammate', '@Ann review the diff', [['ann', 'review the diff']]],
+ // A connector only leads into the next step: the last step keeps it, and words merely ending in one keep theirs.
+ ['a last step ending like a connector', '@阿布 出方案,then @Ann decide what to do next', [['abu', '出方案'], ['ann', 'decide what to do next']]],
+ ['a word ending like a connector', '@Ann add context\n@阿布 实现', [['ann', 'add context'], ['abu', '实现']]],
] as const)('reads %s', (_label, text, expected) => {
expect(split(text)).toEqual(expected.map(([botId, task]) => ({ botId, task })));
});
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index 6cb4c005f7..76eff9e359 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -305,7 +305,7 @@ Chat Server 声明 `capabilities.groupSingleSpeaker: 1` 时,服务器群按下
- 输入框「+」→「安排分工」重新可用,触发由宿主代码决定,**只看分工标签,不看话怎么说**;不加标签的话照 4.0 讨论。
- **写好了步骤就照写的建卡并直接开始**,不调辅助模型:每个 @ 后面写这位伙伴做什么,后一位从新的一行或标点之后开始
(「登录页:@A 出方案,然后 @B 按方案实现;@C 测一遍」)。第一个 @ 前的话是共同背景;「然后、接着、最后、再」这类
- 连接词不算进任何一步;同一位伙伴相邻的几步合成一步;最多 6 步,每步任务最多 200 字(服务端上限)。
+ 引出下一步的连接词不算进任何一步(最后一步照原话保留);同一位伙伴相邻的几步合成一步;最多 6 步,每步任务最多 200 字(服务端上限)。
@ 夹在句子里(「@A 和 @B 一起做」)、某个 @ 后面没写活、@所有人、重名分不清、点到不能派活的伙伴,或点选的伙伴在
文字里认不出(发出前改了名)时,不当作写好的步骤。
解析在 `@cindy/maker-shared/botGroupMentions` 的 `splitBotGroupRelaySteps`。
@@ -379,7 +379,8 @@ Chat Server 声明 `capabilities.groupSingleSpeaker: 1` 时,服务器群按下
电脑或手机),别人发起的分工不受影响;同一账号在另一台电脑发起的分工也不受影响(本机记下在这里建的卡,最近 50 张)。
读取安排期间关掉开关,这次交接取消。这一步没做完(等「重试」)、分工已停止或最后一步做完时照常停下;等继续时打开
开关立即交给下一位。交接以安排的版本号为准,别人先点了「继续」或改了安排,这次自动交接放弃,不会让一步做两次。
- 这台电脑离线时不会自动交接,分工停在「继续」,有权限的人仍可手动继续。
+ 这台电脑离线时不会自动交接,分工停在「继续」,有权限的人仍可手动继续;电脑重新连上、打开群之前就已做完的一步也会
+ 接着交出去。只在一次一位伙伴的服务器群(`groupSingleSpeaker`)提供这个开关,旧服务器分不清分工与讨论队列。
- 等「继续」时用户不 @ 任何伙伴直接说话 = 让刚做完的伙伴按意见返工,返工完成后再出现「继续」。
- 正在做时用户不 @ 任何伙伴直接说话,这些话在本步当前回合结束后交给同一位伙伴继续做,全部处理完才算本步做完。
- 停止:输入框的停止结束整个分工,已完成的结果保留,群里提示已停止。
diff --git a/packages/maker-shared/src/botGroupMentions.ts b/packages/maker-shared/src/botGroupMentions.ts
index b52e921bed..bcaccbd9e6 100644
--- a/packages/maker-shared/src/botGroupMentions.ts
+++ b/packages/maker-shared/src/botGroupMentions.ts
@@ -150,7 +150,7 @@ export interface BotGroupRelayStep {
}
/** Words that join one step to the next (「,然后 @B」); they belong to neither step's task. */
-const STEP_CONNECTOR = /(?:然后|接着|随后|之后|最后|再由|再|then|and then|next)\s*$/i;
+const STEP_CONNECTOR = /(?:然后|接着|随后|之后|最后|再由|再|\b(?:and then|then|next))\s*$/i;
/** A later step starts on a new line or after sentence / list punctuation. */
const STEP_BOUNDARY = /(?:^|[\n,,。.;;、→>::])\s*$/;
const STEP_EDGE = /^[\s,,。.;;、→>::\-]+|[\s,,。.;;、→>::\-]+$/g;
@@ -172,7 +172,9 @@ export function splitBotGroupRelaySteps(
for (const [index, token] of tokens.entries()) {
if (token.entry.all || token.entry.botIds.length !== 1) return null;
if (index > 0 && !STEP_BOUNDARY.test(text.slice(tokens[index - 1]!.end, token.start).replace(STEP_CONNECTOR, ''))) return null;
- const task = text.slice(token.end, tokens[index + 1]?.start ?? text.length).replace(STEP_CONNECTOR, '').replace(STEP_EDGE, '');
+ // A connector only leads into the next step; the last step keeps its words.
+ const span = text.slice(token.end, tokens[index + 1]?.start ?? text.length);
+ const task = (index + 1 < tokens.length ? span.replace(STEP_CONNECTOR, '') : span).replace(STEP_EDGE, '');
if (!task) return null;
steps.push({ botId: token.entry.botIds[0]!, task });
}