@evandu/super-pi 0.1.307 → 0.1.309

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.
@@ -8,9 +8,9 @@
8
8
  <link rel="alternate icon" href="/favicon.ico" />
9
9
  <link rel="apple-touch-icon" href="/favicon.svg" />
10
10
  <title>Super Pi</title>
11
- <script type="module" crossorigin src="/assets/index-DLDXS61p.js"></script>
11
+ <script type="module" crossorigin src="/assets/index-CWwK9CVT.js"></script>
12
12
  <link rel="modulepreload" crossorigin href="/assets/rolldown-runtime-XKyaHtl9.js">
13
- <link rel="stylesheet" crossorigin href="/assets/index-CuTvXjCb.css">
13
+ <link rel="stylesheet" crossorigin href="/assets/index-BiE3Qbyj.css">
14
14
  </head>
15
15
  <body>
16
16
  <div id="root"></div>
@@ -40,6 +40,7 @@ export declare class GroupChatService {
40
40
  private readonly running;
41
41
  private readonly memberRuns;
42
42
  private readonly cloneDecisions;
43
+ private readonly humanQuestionWaiters;
43
44
  private readonly cloneWorktrees;
44
45
  private readonly controllers;
45
46
  private readonly live;
@@ -111,6 +112,7 @@ export declare class GroupChatService {
111
112
  filePaths?: unknown;
112
113
  images?: unknown;
113
114
  }): Promise<GroupSnapshot>;
115
+ answerHumanQuestion(workspaceId: string, id: string, replyId: string, value: unknown): Promise<GroupSnapshot>;
114
116
  stop(workspaceId: string, id: string, postId: string): Promise<GroupSnapshot>;
115
117
  private kick;
116
118
  private runTopic;
@@ -15,6 +15,13 @@ const CONTEXT_MESSAGES = 20;
15
15
  const MAX_CONTENT = 32_000;
16
16
  const SAVED_EXECUTIONS_PER_ROOM = 64;
17
17
  const timestamp = () => new Date().toISOString();
18
+ class InvalidModeratorSelectionError extends Error {
19
+ selection;
20
+ constructor(selection) {
21
+ super(`主持人选择了无效的群成员:${selection}`);
22
+ this.selection = selection;
23
+ }
24
+ }
18
25
  function inputText(value, limit, label) {
19
26
  if (typeof value !== "string" || value.length > limit)
20
27
  throw new Error(`${label}格式无效或超过长度上限。`);
@@ -95,7 +102,7 @@ async function readReferences(workspacePath, input) {
95
102
  }
96
103
  }));
97
104
  }
98
- function route(text, candidates, anchors) {
105
+ function route(text, candidates, members, busy, anchors) {
99
106
  const parsed = JSON.parse(text.trim().replace(/^```(?:json)?\s*|\s*```$/gi, ""));
100
107
  if (!parsed || typeof parsed !== "object")
101
108
  throw new Error("主持人未返回有效决定。");
@@ -105,12 +112,20 @@ function route(text, candidates, anchors) {
105
112
  return { action: "finish", reason };
106
113
  if (decision.action === "blocked" && reason)
107
114
  return { action: "blocked", reason };
108
- if (decision.action === "wait")
109
- return { action: "wait", memberId: typeof decision.memberId === "string" ? decision.memberId : undefined };
110
- if (decision.action === "clone" && typeof decision.memberId === "string")
115
+ if (decision.action === "wait") {
116
+ const memberId = typeof decision.memberId === "string" ? decision.memberId : undefined;
117
+ if (memberId && (!members.some(member => member.id === memberId) || !busy.has(memberId)))
118
+ throw new InvalidModeratorSelectionError(memberId);
119
+ return { action: "wait", memberId };
120
+ }
121
+ if (decision.action === "clone") {
122
+ if (typeof decision.memberId !== "string" || !members.some(member => member.id === decision.memberId) || !busy.has(decision.memberId)) {
123
+ throw new InvalidModeratorSelectionError(typeof decision.memberId === "string" ? decision.memberId : "(缺少 memberId)");
124
+ }
111
125
  return { action: "clone", memberId: decision.memberId, assignment: inputText(decision.assignment, 1600, "分身任务"), reason: reason || "同类成员正在工作,新任务可以独立开展。" };
126
+ }
112
127
  if (!["reply", "background"].includes(String(decision.action)) || typeof decision.memberId !== "string" || !candidates.some(member => member.id === decision.memberId)) {
113
- throw new Error("主持人选择了无效的群成员。");
128
+ throw new InvalidModeratorSelectionError(typeof decision.memberId === "string" ? decision.memberId : "(缺少 memberId)");
114
129
  }
115
130
  const replyTo = typeof decision.replyTo === "string" && anchors.has(decision.replyTo) ? decision.replyTo : undefined;
116
131
  const assignment = inputText(decision.assignment, 1600, "接话任务");
@@ -166,6 +181,7 @@ export class GroupChatService {
166
181
  running = new Map();
167
182
  memberRuns = new Map();
168
183
  cloneDecisions = new Map();
184
+ humanQuestionWaiters = new Map();
169
185
  cloneWorktrees = new Map();
170
186
  controllers = new Map();
171
187
  live = new Map();
@@ -577,6 +593,31 @@ export class GroupChatService {
577
593
  this.kick(id);
578
594
  return this.get(workspaceId, id);
579
595
  }
596
+ async answerHumanQuestion(workspaceId, id, replyId, value) {
597
+ const answer = inputText(value, 8_000, "回答");
598
+ if (!answer)
599
+ throw new Error("请先输入回答。");
600
+ await this.edit(rooms => {
601
+ const room = this.activeRoom(rooms, workspaceId, id);
602
+ const reply = room.messages.find((post) => post.id === replyId && post.role === "assistant");
603
+ if (!reply?.humanQuestion)
604
+ throw new Error("找不到这条群聊问题。");
605
+ if (reply.humanQuestion.answer || reply.humanQuestion.dismissedAt)
606
+ throw new Error("这条问题已处理。");
607
+ reply.humanQuestion.answer = answer;
608
+ reply.humanQuestion.answeredAt = timestamp();
609
+ const topic = room.messages.find(post => post.role === "user" && post.id === reply.topicId);
610
+ if (topic?.role === "user" && (topic.status === "interrupted" || topic.status === "failed")) {
611
+ topic.status = "queued";
612
+ delete topic.error;
613
+ }
614
+ room.revision += 1;
615
+ room.updatedAt = timestamp();
616
+ });
617
+ this.humanQuestionWaiters.get(replyId)?.();
618
+ this.kick(id);
619
+ return this.get(workspaceId, id);
620
+ }
580
621
  async stop(workspaceId, id, postId) {
581
622
  const sourceRoom = this.find(this.rooms, workspaceId, id);
582
623
  const members = sourceRoom.messages.filter(post => post.role === "assistant" && (post.id === postId || post.topicId === postId)).map(post => post.role === "assistant" ? post.memberId : "");
@@ -587,8 +628,11 @@ export class GroupChatService {
587
628
  for (const post of posts) {
588
629
  post.content = this.live.get(post.id) ?? post.content;
589
630
  post.status = "cancelled";
590
- if (post.role === "assistant")
631
+ if (post.role === "assistant") {
591
632
  this.saveExecution(room, post.id, true);
633
+ if (post.humanQuestion && !post.humanQuestion.answer)
634
+ post.humanQuestion.dismissedAt = timestamp();
635
+ }
592
636
  }
593
637
  room.revision += 1;
594
638
  return posts.map(post => post.id);
@@ -648,7 +692,8 @@ export class GroupChatService {
648
692
  if (replies.length)
649
693
  await Promise.race(replies.map(reply => reply.yields));
650
694
  };
651
- if (topic.memberIds.length)
695
+ const hasPriorReplies = room.messages.some(post => post.role === "assistant" && post.topicId === topic.id);
696
+ if (topic.memberIds.length && !hasPriorReplies)
652
697
  await start(topic.memberIds.map(memberId => ({ memberId, replyTo: topic.replyTo ?? topic.id })));
653
698
  let count = 0;
654
699
  let incomplete;
@@ -657,6 +702,7 @@ export class GroupChatService {
657
702
  if (current.messages.find(post => post.id === topic.id)?.status !== "running")
658
703
  break;
659
704
  const replies = current.messages.filter((post) => post.role === "assistant" && post.topicId === topic.id);
705
+ const unanswered = replies.find(post => post.humanQuestion && !post.humanQuestion.answer && !post.humanQuestion.dismissedAt);
660
706
  const active = pending.filter(reply => replies.some(post => post.id === reply.id && (post.status === "running" || post.status === "queued")));
661
707
  const busy = new Set(current.messages.flatMap(post => post.role === "assistant" && (post.status === "running" || post.status === "queued") ? [post.memberId] : []));
662
708
  const available = current.members.filter(member => {
@@ -666,6 +712,17 @@ export class GroupChatService {
666
712
  const candidates = available.filter(member => !busy.has(member.id));
667
713
  const handoffs = outstandingHandoffs(replies);
668
714
  const remaining = MAX_AUTOMATIC_REPLIES - count;
715
+ const waitForAnswer = async () => {
716
+ if (!unanswered)
717
+ return;
718
+ const waiter = new Promise(resolve => this.humanQuestionWaiters.set(unanswered.id, resolve));
719
+ await this.waitForWork(waiter, controller.signal);
720
+ this.humanQuestionWaiters.delete(unanswered.id);
721
+ };
722
+ if (unanswered && !candidates.length && !busy.size && !active.length) {
723
+ await waitForAnswer();
724
+ continue;
725
+ }
669
726
  if (active.length && (!candidates.length || !remaining)) {
670
727
  await Promise.race(active.map(reply => reply.done));
671
728
  continue;
@@ -673,6 +730,10 @@ export class GroupChatService {
673
730
  const next = candidates.length || busy.size || handoffs.length || !remaining
674
731
  ? await this.choose(current, topic, candidates, replies, handoffs, busy, remaining, controller.signal)
675
732
  : { action: "finish" };
733
+ if (unanswered && (next.action === "finish" || next.action === "blocked")) {
734
+ await waitForAnswer();
735
+ continue;
736
+ }
676
737
  if (next.action === "clone") {
677
738
  const source = current.members.find(member => member.id === next.memberId && busy.has(member.id));
678
739
  if (!source) {
@@ -775,6 +836,10 @@ export class GroupChatService {
775
836
  await this.waitForWork(work, controller.signal);
776
837
  continue;
777
838
  }
839
+ if (unanswered) {
840
+ await waitForAnswer();
841
+ continue;
842
+ }
778
843
  }
779
844
  if (active.length) {
780
845
  await Promise.race(active.map(reply => reply.done));
@@ -843,7 +908,7 @@ export class GroupChatService {
843
908
  const remaining = CONTEXT_MESSAGES - current.length;
844
909
  return [...(remaining ? roomHistory.slice(-remaining) : []), ...current].map(post => ({
845
910
  id: post.id, author: post.role === "user" ? "用户" : post.memberName ?? room.members.find(member => member.id === post.memberId)?.name,
846
- content: post.content.slice(0, 4_000),
911
+ content: `${post.content.slice(0, 4_000)}${post.role === "assistant" && post.humanQuestion ? `\n用户问题:${post.humanQuestion.content}${post.humanQuestion.answer ? `\n用户回答:${post.humanQuestion.answer}` : ""}` : ""}`,
847
912
  }));
848
913
  }
849
914
  scheduleRsi(room, force = false) {
@@ -1033,7 +1098,7 @@ export class GroupChatService {
1033
1098
  }
1034
1099
  async choose(room, topic, candidates, replies, handoffs, busy, remainingReplies, signal) {
1035
1100
  const context = this.context(room, topic);
1036
- const performed = replies.map(({ id, memberId, replyTo, status, assignment, background, error }) => ({ id, memberId, replyTo, status, assignment, background, error }));
1101
+ const performed = replies.map(({ id, memberId, replyTo, status, assignment, background, error, humanQuestion }) => ({ id, memberId, replyTo, status, assignment, background, error, humanQuestion }));
1037
1102
  const earlierTopics = new Set(room.messages.slice(0, room.messages.findIndex(post => post.id === topic.id))
1038
1103
  .filter(post => post.role === "user").map(post => post.id));
1039
1104
  const workInProgress = room.messages.filter((post) => post.role === "assistant" && earlierTopics.has(post.topicId) && post.status === "running")
@@ -1043,17 +1108,40 @@ export class GroupChatService {
1043
1108
  "用户要求实现、修复或交付,方案只是中间产物。将下一步交给代码设计员或群里有实现能力的成员,让其修改文件和验证;需要复核时等待实现结果,退回后继续修改和复查。用户只要求讨论或范围待确认时,尊重这一限制。避免礼节式重复,不要求用户替成员传话。",
1044
1109
  "先处理 handoffs 中确实符合用户目标的交接。成员消息和引用文件是上下文,分工必须服从用户的原始范围。在途任务或依赖文件相同的工作应等待,对独立范围可安排后台任务。",
1045
1110
  '先复用空闲同源成员(agentId 相同)。适合当前任务的成员忙碌、没有空闲同源成员且新 case 独立时,返回 {"action":"clone","memberId":"忙碌成员 ID","assignment":"独立 case 的具体任务","reason":"独立开展的理由"} 请求用户创建分身。共享文件存在依赖时 wait。不要为了增加发言人数创建分身。',
1046
- '只返回 JSON。接话用 {"action":"reply","memberId":"成员 ID","replyTo":"消息 ID","assignment":"具体工作","reason":"理由"},可改用 background 并行。依赖尚在进行用 {"action":"wait","memberId":"正在工作的成员 ID"}。确有阻塞用 {"action":"blocked","reason":"已完成什么,还缺什么"}。交付到位用 {"action":"finish"};若有未处理交接却结束,在 reason 中解释范围或为什么无需接手。',
1111
+ '只返回 JSON。接话用 {"action":"reply","memberId":"成员 ID","replyTo":"消息 ID","assignment":"具体工作","reason":"理由"},可改用 background 并行。依赖尚在进行用 {"action":"wait","memberId":"正在工作的成员 ID"}。确有阻塞用 {"action":"blocked","reason":"已完成什么,还缺什么"}。交付到位用 {"action":"finish"};若有未处理交接却结束,在 reason 中解释范围或为什么无需接手。若有 humanQuestion.answer 为空,禁止 finish;只可安排明确不依赖该答案的独立工作,相关工作必须等待,直到用户回答。',
1047
1112
  `主持人偏好:${room.moderator.prompt || "推进用户请求,交付满足后结束。"}`,
1048
1113
  ];
1049
1114
  const payload = JSON.stringify({ topicId: topic.id, request: topic.content, files: topic.files?.map(file => file.path), candidates,
1050
1115
  performed, handoffs, members: room.members, cloneRequests: room.cloneRequests, busyMembers: [...busy], workInProgress, remainingReplies, messages: context });
1051
1116
  try {
1052
1117
  const anchors = new Set([topic.id, ...context.map(post => post.id), ...replies.filter(post => post.status === "failed").map(post => post.id)]);
1053
- const ask = async (instructions) => route(await this.complete(room, { images: topic.images?.length ? await loadConversationImages(join(dirname(this.filePath), "uploads"), room.id, topic.images, { required: true }) : undefined, prompt: [...instructions, payload].join("\n"), model: room.moderator.model, signal, timeoutMs: 35_000 }), candidates, anchors);
1054
- let decision = await ask(prompt);
1118
+ const ask = async (instructions) => route(await this.complete(room, { images: topic.images?.length ? await loadConversationImages(join(dirname(this.filePath), "uploads"), room.id, topic.images, { required: true }) : undefined, prompt: [...instructions, payload].join("\n"), model: room.moderator.model, signal, timeoutMs: 35_000 }), candidates, room.members, busy, anchors);
1119
+ const askSafely = async (instructions) => {
1120
+ try {
1121
+ return await ask(instructions);
1122
+ }
1123
+ catch (error) {
1124
+ if (!(error instanceof InvalidModeratorSelectionError) || signal.aborted)
1125
+ throw error;
1126
+ const correction = `上一次选择的成员 ID「${error.selection}」无效。有效空闲候选:${candidates.map(member => `${member.id}(${member.name})`).join("、") || "无"}。忙碌成员只能用于 wait 或 clone,且必须来自当前群聊成员。请纠正后只返回 JSON。`;
1127
+ try {
1128
+ return await ask([...instructions, correction]);
1129
+ }
1130
+ catch (retryError) {
1131
+ if (!(retryError instanceof InvalidModeratorSelectionError) || signal.aborted)
1132
+ throw retryError;
1133
+ const fallback = candidates[0];
1134
+ if (fallback)
1135
+ return { action: "reply", memberId: fallback.id, replyTo: topic.id, assignment: "按用户原始请求继续本轮工作。主持人连续两次返回了无效成员,现由服务端安全分配给此可用成员。", reason: "自动恢复无效成员选择" };
1136
+ if (busy.size)
1137
+ return { action: "wait", memberId: [...busy][0] };
1138
+ return { action: "blocked", reason: "主持人连续两次返回无效成员,且当前没有可用成员;已有进展已保留,可稍后继续。" };
1139
+ }
1140
+ }
1141
+ };
1142
+ let decision = await askSafely(prompt);
1055
1143
  if (decision.action === "finish" && !decision.reason && handoffs.length && !signal.aborted) {
1056
- decision = await ask([...prompt, "还有尚未接手的交接。检查它与用户目标的关系,安排接手或说明为什么结束。"]);
1144
+ decision = await askSafely([...prompt, "还有尚未接手的交接。检查它与用户目标的关系,安排接手或说明为什么结束。"]);
1057
1145
  }
1058
1146
  if (decision.action === "finish" && !performed.length && candidates[0])
1059
1147
  return { action: "reply", memberId: candidates[0].id, replyTo: topic.id, assignment: "按用户要求推进当前工作。", reason: "交给默认成员" };
@@ -1179,6 +1267,7 @@ export class GroupChatService {
1179
1267
  const prompt = [
1180
1268
  `你以 ${agent.name} 的身份在群聊中回答。直接回应引用的成员意见,推进本次分工。有可用工具且分工要求执行时,直接完成并报告实际文件与验证;其他成员可以继续讨论。只修改分工涉及的文件。`,
1181
1269
  "确实需要另一位成员推进时,在发言末尾另起一行写 @成员名:要接手的具体事项。主持人会按原请求判断并安排。可直接使用成员 ID。",
1270
+ "只有当继续当前话题确实依赖用户提供的信息或决定时,才在回复最后另起一行写 @用户:一个明确、可直接回答的问题。此标记会生成群聊待答问题;不要把问题只写进普通正文,也不要在答案到来前假设用户已同意。与问题无关的工作仍可继续。",
1182
1271
  JSON.stringify({ request: topic.content, files: topic.files, replyTo: reply.replyTo, quote: started.messages.find(post => post.id === reply.replyTo)?.content.slice(0, 4_000), assignment: reply.assignment, members: started.members, notes }),
1183
1272
  ].join("\n");
1184
1273
  const answer = await this.complete(started, {
@@ -1241,8 +1330,14 @@ export class GroupChatService {
1241
1330
  if (post.role === "assistant") {
1242
1331
  this.saveExecution(room, postId, true);
1243
1332
  post.settledAt = timestamp();
1244
- if (post.status === "completed")
1333
+ if (post.status === "completed") {
1334
+ const question = post.content.match(/(?:^|\n)@用户[::]\s*(.{1,1200})\s*$/u);
1335
+ if (question) {
1336
+ post.humanQuestion = { content: question[1].trim() };
1337
+ post.content = post.content.slice(0, question.index).trimEnd();
1338
+ }
1245
1339
  post.handoff = extractHandoff(post.memberId, post.content, room.members);
1340
+ }
1246
1341
  }
1247
1342
  room.revision += 1;
1248
1343
  room.updatedAt = timestamp();
@@ -581,6 +581,10 @@ export function createHttpServer(options) {
581
581
  json(202, await groups.send(workspaceId, roomId, body));
582
582
  return;
583
583
  }
584
+ if (body?.action === "human.answer" && typeof body.replyId === "string") {
585
+ json(200, await groups.answerHumanQuestion(workspaceId, roomId, body.replyId, body.answer));
586
+ return;
587
+ }
584
588
  if (body?.action === "configure") {
585
589
  json(200, await groups.configure(workspaceId, roomId, body));
586
590
  return;
@@ -169,6 +169,12 @@ export type GroupReply = GroupPost & {
169
169
  memberId: string;
170
170
  assignment: string;
171
171
  };
172
+ humanQuestion?: {
173
+ content: string;
174
+ answer?: string;
175
+ answeredAt?: string;
176
+ dismissedAt?: string;
177
+ };
172
178
  };
173
179
  export type GroupMessage = GroupUserPost | GroupReply;
174
180
  export declare function groupMessageTime(post: GroupMessage): string;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "superPi": {
4
4
  "managedDirectListener": true
5
5
  },
6
- "version": "0.1.307",
6
+ "version": "0.1.309",
7
7
  "description": "Web control plane and companion for Pi",
8
8
  "license": "MIT",
9
9
  "type": "module",
@@ -1,4 +0,0 @@
1
- {
2
- "hash": "2c125a0ee33f73abe97366fc02e5e0308148063e91d982412d337beb0b5f6b4d",
3
- "recordedAt": "2026-11-26T18:26:40.921Z"
4
- }