@evandu/super-pi 0.1.243 → 0.1.244
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/client/.super-pi-client-sources.json +9 -8
- package/dist/client/assets/{CodeMirrorEditor-B7eDr7dY.js → CodeMirrorEditor-B1T8wU12.js} +1 -1
- package/dist/client/assets/{GitDiffEditor-pYaHOM0Q.js → GitDiffEditor-LCXbSvsh.js} +1 -1
- package/dist/client/assets/{index-D0Po7xmY.js → index-DDrbcKk9.js} +18 -18
- package/dist/client/assets/index-DltiyYgr.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/application/group-chat-service.d.ts +14 -2
- package/dist/server/application/group-chat-service.js +183 -26
- package/dist/server/application/group-execution.d.ts +15 -0
- package/dist/server/application/group-execution.js +77 -0
- package/dist/server/http-server.js +19 -4
- package/dist/server/index.js +1 -1
- package/dist/server/runtime.d.ts +10 -0
- package/dist/server/runtime.js +25 -0
- package/dist/server/types.d.ts +4 -1
- package/dist/shared/group-chat.d.ts +47 -2
- package/package.json +1 -1
- package/dist/client/assets/index-CfAcuYba.css +0 -1
package/dist/client/index.html
CHANGED
|
@@ -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-
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-DDrbcKk9.js"></script>
|
|
12
12
|
<link rel="modulepreload" crossorigin href="/assets/rolldown-runtime-XKyaHtl9.js">
|
|
13
|
-
<link rel="stylesheet" crossorigin href="/assets/index-
|
|
13
|
+
<link rel="stylesheet" crossorigin href="/assets/index-DltiyYgr.css">
|
|
14
14
|
</head>
|
|
15
15
|
<body>
|
|
16
16
|
<div id="root"></div>
|
|
@@ -1,24 +1,36 @@
|
|
|
1
|
-
import type { GroupSnapshot, GroupSummary } from "../../shared/group-chat.js";
|
|
1
|
+
import type { GroupExecutionSnapshot, GroupSnapshot, GroupSummary } from "../../shared/group-chat.js";
|
|
2
|
+
export declare class GroupChatNotFoundError extends Error {
|
|
3
|
+
constructor(message?: string);
|
|
4
|
+
}
|
|
2
5
|
export declare class GroupChatService {
|
|
3
6
|
private readonly filePath;
|
|
4
7
|
private readonly store;
|
|
5
8
|
private readonly runtime;
|
|
6
9
|
private readonly memory?;
|
|
10
|
+
private readonly onActivity?;
|
|
7
11
|
private rooms;
|
|
8
12
|
private writes;
|
|
9
13
|
private readonly running;
|
|
10
14
|
private readonly memberRuns;
|
|
11
15
|
private readonly controllers;
|
|
12
16
|
private readonly live;
|
|
17
|
+
private readonly liveExecutions;
|
|
13
18
|
private readonly streamRevisions;
|
|
14
19
|
private closing;
|
|
15
20
|
private constructor();
|
|
16
|
-
static open(filePath: string, store: GroupChatService["store"], runtime: GroupChatService["runtime"], memory?: GroupChatService["memory"]): Promise<GroupChatService>;
|
|
21
|
+
static open(filePath: string, store: GroupChatService["store"], runtime: GroupChatService["runtime"], memory?: GroupChatService["memory"], onActivity?: GroupChatService["onActivity"]): Promise<GroupChatService>;
|
|
17
22
|
private workspace;
|
|
18
23
|
private find;
|
|
24
|
+
private activeRoom;
|
|
25
|
+
private hasUnsettledWork;
|
|
19
26
|
private edit;
|
|
27
|
+
private publishActivity;
|
|
20
28
|
list(workspaceId: string): GroupSummary[];
|
|
21
29
|
get(workspaceId: string, id: string): GroupSnapshot;
|
|
30
|
+
execution(workspaceId: string, id: string, postId: string): GroupExecutionSnapshot;
|
|
31
|
+
private saveExecution;
|
|
32
|
+
setArchived(workspaceId: string, id: string, archived: boolean): Promise<GroupSummary>;
|
|
33
|
+
deleteArchived(workspaceId: string, id: string): Promise<void>;
|
|
22
34
|
private moderator;
|
|
23
35
|
private resolveMembers;
|
|
24
36
|
create(workspaceId: string, input: {
|
|
@@ -5,18 +5,52 @@ import { DEFAULT_GROUP_MEMBER_IDS } from "../../shared/group-chat.js";
|
|
|
5
5
|
import { CODING_STANDARD_REQUEST } from "../active-memory.js";
|
|
6
6
|
import { applicableRules, assertModelAllowed } from "../model-filter.js";
|
|
7
7
|
import { replaceFile } from "../persistence/rename.js";
|
|
8
|
+
import { finishGroupExecution, GroupExecutionRecorder } from "./group-execution.js";
|
|
8
9
|
const MAX_AUTOMATIC_REPLIES = 10;
|
|
9
10
|
const MAX_REPLIES_PER_MEMBER = 3;
|
|
10
11
|
const CONTEXT_MESSAGES = 20;
|
|
11
12
|
const MAX_CONTENT = 32_000;
|
|
13
|
+
const SAVED_EXECUTIONS_PER_ROOM = 64;
|
|
12
14
|
const timestamp = () => new Date().toISOString();
|
|
13
15
|
function inputText(value, limit, label) {
|
|
14
16
|
if (typeof value !== "string" || value.length > limit)
|
|
15
17
|
throw new Error(`${label}格式无效或超过长度上限。`);
|
|
16
18
|
return value.trim();
|
|
17
19
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
+
function activityStatus(messages) {
|
|
21
|
+
let latestTopic;
|
|
22
|
+
let activeTopic = false;
|
|
23
|
+
let activeBackground = false;
|
|
24
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
25
|
+
const post = messages[i];
|
|
26
|
+
if (post.role === "user") {
|
|
27
|
+
latestTopic ??= post.status;
|
|
28
|
+
if (post.status === "queued")
|
|
29
|
+
return "running";
|
|
30
|
+
activeTopic ||= post.status === "running";
|
|
31
|
+
}
|
|
32
|
+
else if (post.status === "queued" || post.status === "running") {
|
|
33
|
+
if (!post.background)
|
|
34
|
+
return "running";
|
|
35
|
+
activeBackground = true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (activeBackground)
|
|
39
|
+
return "awaiting_background";
|
|
40
|
+
if (activeTopic)
|
|
41
|
+
return "running";
|
|
42
|
+
if (latestTopic === "completed")
|
|
43
|
+
return "completed";
|
|
44
|
+
if (latestTopic === "failed" || latestTopic === "interrupted")
|
|
45
|
+
return "failed";
|
|
46
|
+
return "idle";
|
|
47
|
+
}
|
|
48
|
+
function groupActivity(room) {
|
|
49
|
+
return { status: activityStatus(room.messages), revision: room.revision };
|
|
50
|
+
}
|
|
51
|
+
function summary(room) {
|
|
52
|
+
const { id, workspaceId, title, updatedAt, archivedAt } = room;
|
|
53
|
+
return { id, workspaceId, title, updatedAt, archivedAt, activity: groupActivity(room) };
|
|
20
54
|
}
|
|
21
55
|
async function readReferences(workspacePath, input) {
|
|
22
56
|
if (!Array.isArray(input) || !input.length)
|
|
@@ -88,27 +122,33 @@ function outstandingHandoffs(replies) {
|
|
|
88
122
|
return handoff && !delegated ? [{ ...handoff, replyTo: post.id, from: post.memberId }] : [];
|
|
89
123
|
});
|
|
90
124
|
}
|
|
125
|
+
export class GroupChatNotFoundError extends Error {
|
|
126
|
+
constructor(message = "此工作区没有这个群聊。") { super(message); }
|
|
127
|
+
}
|
|
91
128
|
export class GroupChatService {
|
|
92
129
|
filePath;
|
|
93
130
|
store;
|
|
94
131
|
runtime;
|
|
95
132
|
memory;
|
|
133
|
+
onActivity;
|
|
96
134
|
rooms = [];
|
|
97
135
|
writes = Promise.resolve();
|
|
98
136
|
running = new Map();
|
|
99
137
|
memberRuns = new Map();
|
|
100
138
|
controllers = new Map();
|
|
101
139
|
live = new Map();
|
|
140
|
+
liveExecutions = new Map();
|
|
102
141
|
streamRevisions = new Map();
|
|
103
142
|
closing = false;
|
|
104
|
-
constructor(filePath, store, runtime, memory) {
|
|
143
|
+
constructor(filePath, store, runtime, memory, onActivity) {
|
|
105
144
|
this.filePath = filePath;
|
|
106
145
|
this.store = store;
|
|
107
146
|
this.runtime = runtime;
|
|
108
147
|
this.memory = memory;
|
|
148
|
+
this.onActivity = onActivity;
|
|
109
149
|
}
|
|
110
|
-
static async open(filePath, store, runtime, memory) {
|
|
111
|
-
const service = new GroupChatService(filePath, store, runtime, memory);
|
|
150
|
+
static async open(filePath, store, runtime, memory, onActivity) {
|
|
151
|
+
const service = new GroupChatService(filePath, store, runtime, memory, onActivity);
|
|
112
152
|
try {
|
|
113
153
|
service.rooms = JSON.parse(await readFile(filePath, "utf8"));
|
|
114
154
|
}
|
|
@@ -123,6 +163,9 @@ export class GroupChatService {
|
|
|
123
163
|
if (post.status === "running" || (post.role === "assistant" && post.status === "queued")) {
|
|
124
164
|
post.status = "interrupted";
|
|
125
165
|
post.error = "服务重启,本次发言已中断。";
|
|
166
|
+
const trace = room.executions?.[post.id];
|
|
167
|
+
if (trace && room.executions)
|
|
168
|
+
room.executions[post.id] = finishGroupExecution(trace);
|
|
126
169
|
room.revision += 1;
|
|
127
170
|
}
|
|
128
171
|
});
|
|
@@ -141,32 +184,119 @@ export class GroupChatService {
|
|
|
141
184
|
this.workspace(workspaceId);
|
|
142
185
|
const room = rooms.find(item => item.id === id && item.workspaceId === workspaceId);
|
|
143
186
|
if (!room)
|
|
144
|
-
throw new
|
|
187
|
+
throw new GroupChatNotFoundError();
|
|
188
|
+
return room;
|
|
189
|
+
}
|
|
190
|
+
activeRoom(rooms, workspaceId, id) {
|
|
191
|
+
const room = this.find(rooms, workspaceId, id);
|
|
192
|
+
if (room.archivedAt)
|
|
193
|
+
throw new Error("群聊已归档,请先恢复。");
|
|
145
194
|
return room;
|
|
146
195
|
}
|
|
196
|
+
hasUnsettledWork(room) {
|
|
197
|
+
return room.messages.some(post => post.status === "queued" || post.status === "running" || this.running.has(post.id));
|
|
198
|
+
}
|
|
147
199
|
edit(change) {
|
|
148
200
|
const write = this.writes.then(async () => {
|
|
149
|
-
const
|
|
201
|
+
const previous = this.rooms;
|
|
202
|
+
const rooms = structuredClone(previous);
|
|
150
203
|
const result = change(rooms);
|
|
151
204
|
const temporary = this.filePath + ".tmp";
|
|
152
205
|
await writeFile(temporary, JSON.stringify(rooms), { encoding: "utf8", mode: 0o600 });
|
|
153
206
|
await replaceFile(temporary, this.filePath);
|
|
154
207
|
this.rooms = rooms;
|
|
208
|
+
this.publishActivity(previous);
|
|
155
209
|
return result;
|
|
156
210
|
});
|
|
157
211
|
this.writes = write.then(() => undefined, () => undefined);
|
|
158
212
|
return write;
|
|
159
213
|
}
|
|
214
|
+
publishActivity(previous) {
|
|
215
|
+
if (!this.onActivity)
|
|
216
|
+
return;
|
|
217
|
+
const prior = new Map(previous.map(room => [room.id, room]));
|
|
218
|
+
for (const room of this.rooms) {
|
|
219
|
+
const before = prior.get(room.id);
|
|
220
|
+
if (!before || before.revision === room.revision)
|
|
221
|
+
continue;
|
|
222
|
+
const workspace = this.store.workspace(room.workspaceId);
|
|
223
|
+
if (!workspace || workspace.closedAt)
|
|
224
|
+
continue;
|
|
225
|
+
const activity = groupActivity(room);
|
|
226
|
+
if (activity.status !== activityStatus(before.messages))
|
|
227
|
+
this.onActivity({ id: room.id, workspaceId: room.workspaceId, activity });
|
|
228
|
+
}
|
|
229
|
+
}
|
|
160
230
|
list(workspaceId) {
|
|
161
231
|
this.workspace(workspaceId);
|
|
162
232
|
return this.rooms.filter(room => room.workspaceId === workspaceId).map(summary).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
163
233
|
}
|
|
164
234
|
get(workspaceId, id) {
|
|
165
|
-
const
|
|
235
|
+
const { executions, ...record } = this.find(this.rooms, workspaceId, id);
|
|
236
|
+
const room = structuredClone(record);
|
|
166
237
|
for (const post of room.messages)
|
|
167
|
-
if (post.status === "running")
|
|
238
|
+
if (post.status === "running") {
|
|
168
239
|
post.content = this.live.get(post.id) ?? post.content;
|
|
169
|
-
|
|
240
|
+
if (post.role === "assistant")
|
|
241
|
+
post.executionActivity = this.liveExecutions.get(post.id)?.activity;
|
|
242
|
+
}
|
|
243
|
+
return { ...room, activity: groupActivity(room), revision: `${room.revision}:${this.streamRevisions.get(id) ?? 0}` };
|
|
244
|
+
}
|
|
245
|
+
execution(workspaceId, id, postId) {
|
|
246
|
+
const room = this.find(this.rooms, workspaceId, id);
|
|
247
|
+
const post = room.messages.find(item => item.id === postId && item.role === "assistant");
|
|
248
|
+
if (!post)
|
|
249
|
+
throw new GroupChatNotFoundError("群聊里找不到该次执行。");
|
|
250
|
+
const live = post.status === "running" ? this.liveExecutions.get(postId) : undefined;
|
|
251
|
+
const trace = live?.trace ?? room.executions?.[postId] ?? { steps: [] };
|
|
252
|
+
return { ...structuredClone(trace), status: post.status, revision: `${room.revision}:${live?.revision ?? 0}` };
|
|
253
|
+
}
|
|
254
|
+
saveExecution(room, postId, settled = false) {
|
|
255
|
+
const live = this.liveExecutions.get(postId);
|
|
256
|
+
if (!live?.trace.steps.length)
|
|
257
|
+
return;
|
|
258
|
+
const trace = structuredClone(live.trace);
|
|
259
|
+
const executions = room.executions ??= {};
|
|
260
|
+
executions[postId] = settled ? finishGroupExecution(trace) : trace;
|
|
261
|
+
const ids = Object.keys(executions);
|
|
262
|
+
if (ids.length > SAVED_EXECUTIONS_PER_ROOM) {
|
|
263
|
+
const expired = ids.find(id => !this.liveExecutions.has(id));
|
|
264
|
+
if (expired)
|
|
265
|
+
delete executions[expired];
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async setArchived(workspaceId, id, archived) {
|
|
269
|
+
return this.edit(rooms => {
|
|
270
|
+
const room = this.find(rooms, workspaceId, id);
|
|
271
|
+
if (Boolean(room.archivedAt) === archived)
|
|
272
|
+
return summary(room);
|
|
273
|
+
if (archived && this.hasUnsettledWork(room))
|
|
274
|
+
throw new Error("群聊还有任务未结束,请先停止任务或等它结束后再归档。");
|
|
275
|
+
room.updatedAt = timestamp();
|
|
276
|
+
if (archived)
|
|
277
|
+
room.archivedAt = room.updatedAt;
|
|
278
|
+
else
|
|
279
|
+
delete room.archivedAt;
|
|
280
|
+
room.revision += 1;
|
|
281
|
+
return summary(room);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
async deleteArchived(workspaceId, id) {
|
|
285
|
+
this.workspace(workspaceId);
|
|
286
|
+
const removed = await this.edit(rooms => {
|
|
287
|
+
const index = rooms.findIndex(room => room.workspaceId === workspaceId && room.id === id);
|
|
288
|
+
if (index < 0)
|
|
289
|
+
return false;
|
|
290
|
+
const room = rooms[index];
|
|
291
|
+
if (!room.archivedAt)
|
|
292
|
+
throw new Error("请先归档群聊,再永久删除。");
|
|
293
|
+
if (this.hasUnsettledWork(room))
|
|
294
|
+
throw new Error("群聊还有任务未结束,请等它结束后再删除。");
|
|
295
|
+
rooms.splice(index, 1);
|
|
296
|
+
return true;
|
|
297
|
+
});
|
|
298
|
+
if (removed)
|
|
299
|
+
this.streamRevisions.delete(id);
|
|
170
300
|
}
|
|
171
301
|
async moderator(workspaceId, body) {
|
|
172
302
|
const workspace = this.workspace(workspaceId);
|
|
@@ -212,11 +342,11 @@ export class GroupChatService {
|
|
|
212
342
|
return this.get(workspaceId, id);
|
|
213
343
|
}
|
|
214
344
|
async configure(workspaceId, id, input) {
|
|
215
|
-
const current = this.
|
|
345
|
+
const current = this.activeRoom(this.rooms, workspaceId, id);
|
|
216
346
|
const moderator = await this.moderator(workspaceId, input);
|
|
217
347
|
const members = input.memberIds === undefined ? undefined : await this.resolveMembers(id, input.memberIds, current.members);
|
|
218
348
|
await this.edit(rooms => {
|
|
219
|
-
const room = this.
|
|
349
|
+
const room = this.activeRoom(rooms, workspaceId, id);
|
|
220
350
|
if (members) {
|
|
221
351
|
for (const post of room.messages)
|
|
222
352
|
if (post.role === "assistant")
|
|
@@ -235,12 +365,12 @@ export class GroupChatService {
|
|
|
235
365
|
const clientId = inputText(input.clientId, 128, "发送标识");
|
|
236
366
|
if (!content || !clientId)
|
|
237
367
|
throw new Error("消息内容和发送标识不能为空。");
|
|
238
|
-
const current = this.
|
|
368
|
+
const current = this.activeRoom(this.rooms, workspaceId, id);
|
|
239
369
|
if (current.messages.some(post => post.role === "user" && post.clientId === clientId))
|
|
240
370
|
return this.get(workspaceId, id);
|
|
241
371
|
const files = await readReferences(this.workspace(workspaceId).path, input.filePaths);
|
|
242
372
|
await this.edit(rooms => {
|
|
243
|
-
const room = this.
|
|
373
|
+
const room = this.activeRoom(rooms, workspaceId, id);
|
|
244
374
|
if (room.messages.some(post => post.role === "user" && post.clientId === clientId))
|
|
245
375
|
return;
|
|
246
376
|
const parent = typeof input.replyTo === "string" ? room.messages.find(post => post.id === input.replyTo) : undefined;
|
|
@@ -264,11 +394,13 @@ export class GroupChatService {
|
|
|
264
394
|
}
|
|
265
395
|
async stop(workspaceId, id, postId) {
|
|
266
396
|
const cancelled = await this.edit(rooms => {
|
|
267
|
-
const room = this.
|
|
397
|
+
const room = this.activeRoom(rooms, workspaceId, id);
|
|
268
398
|
const posts = room.messages.filter(post => (post.id === postId || (post.role === "assistant" && post.topicId === postId)) && ["queued", "running"].includes(post.status));
|
|
269
399
|
for (const post of posts) {
|
|
270
400
|
post.content = this.live.get(post.id) ?? post.content;
|
|
271
401
|
post.status = "cancelled";
|
|
402
|
+
if (post.role === "assistant")
|
|
403
|
+
this.saveExecution(room, post.id, true);
|
|
272
404
|
}
|
|
273
405
|
room.revision += 1;
|
|
274
406
|
return posts.map(post => post.id);
|
|
@@ -281,7 +413,7 @@ export class GroupChatService {
|
|
|
281
413
|
if (this.closing)
|
|
282
414
|
return;
|
|
283
415
|
const room = this.rooms.find(item => item.id === id);
|
|
284
|
-
if (!room)
|
|
416
|
+
if (!room || room.archivedAt)
|
|
285
417
|
return;
|
|
286
418
|
for (const post of room.messages) {
|
|
287
419
|
if (post.role !== "user" || post.status !== "queued" || this.running.has(post.id))
|
|
@@ -505,6 +637,7 @@ export class GroupChatService {
|
|
|
505
637
|
const done = this.runMember(roomId, topic, reply, memberSignal, allowNext, prior).finally(() => {
|
|
506
638
|
allowNext();
|
|
507
639
|
this.live.delete(reply.id);
|
|
640
|
+
this.liveExecutions.delete(reply.id);
|
|
508
641
|
this.controllers.delete(reply.id);
|
|
509
642
|
});
|
|
510
643
|
if (reply.background || prior)
|
|
@@ -541,7 +674,26 @@ export class GroupChatService {
|
|
|
541
674
|
});
|
|
542
675
|
if (!started)
|
|
543
676
|
return;
|
|
544
|
-
|
|
677
|
+
const execution = new GroupExecutionRecorder();
|
|
678
|
+
this.liveExecutions.set(reply.id, execution);
|
|
679
|
+
let checkpointTimer;
|
|
680
|
+
const checkpoint = () => {
|
|
681
|
+
if (checkpointTimer)
|
|
682
|
+
return;
|
|
683
|
+
checkpointTimer = setTimeout(() => {
|
|
684
|
+
checkpointTimer = undefined;
|
|
685
|
+
void this.edit(rooms => {
|
|
686
|
+
const room = rooms.find(item => item.id === roomId);
|
|
687
|
+
const post = room?.messages.find(item => item.id === reply.id);
|
|
688
|
+
if (!room || post?.status !== "running")
|
|
689
|
+
return;
|
|
690
|
+
post.content = this.live.get(reply.id) ?? post.content;
|
|
691
|
+
this.saveExecution(room, reply.id);
|
|
692
|
+
}).catch(error => console.warn("Super Pi: could not checkpoint group reply:", error));
|
|
693
|
+
}, 800);
|
|
694
|
+
};
|
|
695
|
+
const bumpStream = () => this.streamRevisions.set(roomId, (this.streamRevisions.get(roomId) ?? 0) + 1);
|
|
696
|
+
let activityKey;
|
|
545
697
|
let yielded = Boolean(reply.background);
|
|
546
698
|
const goBackground = () => {
|
|
547
699
|
if (yielded || memberSignal.aborted)
|
|
@@ -571,19 +723,22 @@ export class GroupChatService {
|
|
|
571
723
|
const answer = await this.complete(started, {
|
|
572
724
|
prompt, agent, model: agent.model && { ...agent.model, label: agent.model.modelId }, signal: memberSignal, timeoutMs: 15 * 60_000,
|
|
573
725
|
onToolStart: goBackground,
|
|
726
|
+
onExecution: event => {
|
|
727
|
+
if (memberSignal.aborted || !execution.record(event))
|
|
728
|
+
return;
|
|
729
|
+
const key = JSON.stringify(execution.activity);
|
|
730
|
+
if (key !== activityKey) {
|
|
731
|
+
activityKey = key;
|
|
732
|
+
bumpStream();
|
|
733
|
+
}
|
|
734
|
+
checkpoint();
|
|
735
|
+
},
|
|
574
736
|
onText: delta => {
|
|
575
737
|
if (memberSignal.aborted)
|
|
576
738
|
return;
|
|
577
739
|
this.live.set(reply.id, ((this.live.get(reply.id) ?? "") + delta).slice(0, MAX_CONTENT));
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
return;
|
|
581
|
-
lastCheckpoint = Date.now();
|
|
582
|
-
void this.edit(rooms => {
|
|
583
|
-
const post = rooms.find(room => room.id === roomId).messages.find(item => item.id === reply.id);
|
|
584
|
-
if (post?.status === "running")
|
|
585
|
-
post.content = this.live.get(reply.id) ?? post.content;
|
|
586
|
-
}).catch(error => console.warn("Super Pi: could not checkpoint group reply:", error));
|
|
740
|
+
bumpStream();
|
|
741
|
+
checkpoint();
|
|
587
742
|
},
|
|
588
743
|
});
|
|
589
744
|
await this.settle(roomId, reply.id, memberSignal, undefined, answer);
|
|
@@ -593,6 +748,7 @@ export class GroupChatService {
|
|
|
593
748
|
}
|
|
594
749
|
finally {
|
|
595
750
|
clearTimeout(timer);
|
|
751
|
+
clearTimeout(checkpointTimer);
|
|
596
752
|
}
|
|
597
753
|
}
|
|
598
754
|
async settle(roomId, postId, signal, error, content) {
|
|
@@ -606,6 +762,7 @@ export class GroupChatService {
|
|
|
606
762
|
post.content = content.slice(0, MAX_CONTENT);
|
|
607
763
|
post.error = signal.aborted ? (this.closing ? "服务停止,本次发言已中断。" : undefined) : error?.slice(0, 1600);
|
|
608
764
|
if (post.role === "assistant") {
|
|
765
|
+
this.saveExecution(room, postId, true);
|
|
609
766
|
post.settledAt = timestamp();
|
|
610
767
|
if (post.status === "completed")
|
|
611
768
|
post.handoff = extractHandoff(post.memberId, post.content, room.members);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { GroupExecutionActivity, GroupExecutionTrace } from "../../shared/group-chat.js";
|
|
2
|
+
import type { IsolatedExecutionEvent } from "../runtime.js";
|
|
3
|
+
export declare function finishGroupExecution(trace: GroupExecutionTrace): GroupExecutionTrace;
|
|
4
|
+
export declare class GroupExecutionRecorder {
|
|
5
|
+
private sequence;
|
|
6
|
+
private thinkingChars;
|
|
7
|
+
private currentThinking?;
|
|
8
|
+
private changed;
|
|
9
|
+
readonly trace: GroupExecutionTrace;
|
|
10
|
+
get revision(): number;
|
|
11
|
+
get activity(): GroupExecutionActivity | undefined;
|
|
12
|
+
private add;
|
|
13
|
+
private endThinking;
|
|
14
|
+
record(event: IsolatedExecutionEvent): boolean;
|
|
15
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const MAX_STEPS = 48;
|
|
2
|
+
const MAX_THINKING_CHARS = 8000;
|
|
3
|
+
export function finishGroupExecution(trace) {
|
|
4
|
+
return { ...trace, steps: trace.steps.map(step => step.status === "running" ? { ...step, status: "interrupted" } : step) };
|
|
5
|
+
}
|
|
6
|
+
export class GroupExecutionRecorder {
|
|
7
|
+
sequence = 0;
|
|
8
|
+
thinkingChars = 0;
|
|
9
|
+
currentThinking;
|
|
10
|
+
changed = 0;
|
|
11
|
+
trace = { steps: [] };
|
|
12
|
+
get revision() { return this.changed; }
|
|
13
|
+
get activity() {
|
|
14
|
+
const tool = this.trace.steps.filter(step => step.kind === "tool" && step.status === "running").at(-1);
|
|
15
|
+
if (tool?.kind === "tool")
|
|
16
|
+
return { kind: "tool", name: tool.name, detail: tool.detail };
|
|
17
|
+
return this.currentThinking ? { kind: "thinking" } : undefined;
|
|
18
|
+
}
|
|
19
|
+
add(step) {
|
|
20
|
+
if (this.trace.steps.length === MAX_STEPS) {
|
|
21
|
+
const settled = this.trace.steps.findIndex(item => item.status !== "running");
|
|
22
|
+
this.trace.steps.splice(Math.max(settled, 0), 1);
|
|
23
|
+
this.trace.truncated = true;
|
|
24
|
+
}
|
|
25
|
+
this.trace.steps.push(step);
|
|
26
|
+
}
|
|
27
|
+
endThinking() {
|
|
28
|
+
if (!this.currentThinking)
|
|
29
|
+
return false;
|
|
30
|
+
this.currentThinking.status = "completed";
|
|
31
|
+
this.currentThinking = undefined;
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
record(event) {
|
|
35
|
+
if (event.kind === "thinking" && event.phase === "end") {
|
|
36
|
+
if (!this.endThinking())
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
else if (event.kind === "thinking") {
|
|
40
|
+
if (!this.currentThinking) {
|
|
41
|
+
this.currentThinking = { kind: "thinking", id: `thinking:${++this.sequence}`, startedAt: new Date().toISOString(), text: "", status: "running" };
|
|
42
|
+
this.add(this.currentThinking);
|
|
43
|
+
}
|
|
44
|
+
if (event.delta) {
|
|
45
|
+
const retained = event.delta.slice(0, MAX_THINKING_CHARS - this.thinkingChars);
|
|
46
|
+
if (!retained && this.trace.truncated)
|
|
47
|
+
return false;
|
|
48
|
+
this.currentThinking.text += retained;
|
|
49
|
+
this.thinkingChars += retained.length;
|
|
50
|
+
if (retained.length !== event.delta.length)
|
|
51
|
+
this.trace.truncated = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const { name, detail, status, durationMs } = event.call;
|
|
56
|
+
const id = `tool:${event.call.id}`;
|
|
57
|
+
let step = this.trace.steps.find((item) => item.kind === "tool" && item.id === id);
|
|
58
|
+
const at = new Date().toISOString();
|
|
59
|
+
if (event.phase === "start")
|
|
60
|
+
this.endThinking();
|
|
61
|
+
if (!step) {
|
|
62
|
+
step = { kind: "tool", id, name, detail, startedAt: at, status: "running" };
|
|
63
|
+
this.add(step);
|
|
64
|
+
}
|
|
65
|
+
if (detail)
|
|
66
|
+
step.detail = detail;
|
|
67
|
+
if (event.phase === "update")
|
|
68
|
+
step.lastUpdateAt = at;
|
|
69
|
+
if (event.phase === "end") {
|
|
70
|
+
step.status = status === "error" ? "failed" : "completed";
|
|
71
|
+
step.durationMs = durationMs;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
this.changed += 1;
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -22,6 +22,7 @@ import { MAX_KNOWLEDGE_REQUEST_BYTES } from "./knowledge.js";
|
|
|
22
22
|
import { MAX_DOCUMENT_REQUEST_BYTES } from "./application/document-service.js";
|
|
23
23
|
import { parseMessageDeliveryMode } from "./application/run-service.js";
|
|
24
24
|
import { createWorktreeService } from "./application/worktree-service.js";
|
|
25
|
+
import { GroupChatNotFoundError } from "./application/group-chat-service.js";
|
|
25
26
|
import { MAX_FEISHU_REQUEST_BYTES } from "./application/feishu-service.js";
|
|
26
27
|
import { MAX_WEB_REQUEST_BYTES } from "./web.js";
|
|
27
28
|
import { MAX_TOOL_BODY_BYTES } from "./tool-registry.js";
|
|
@@ -416,15 +417,20 @@ export function createHttpServer(options) {
|
|
|
416
417
|
const roomId = url.searchParams.get("id");
|
|
417
418
|
try {
|
|
418
419
|
if (request.method === "GET") {
|
|
420
|
+
const executionId = url.searchParams.get("execution");
|
|
421
|
+
if (executionId && !roomId) {
|
|
422
|
+
json(400, { error: "查看执行详情需要群聊 ID。" });
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
419
425
|
if (!roomId) {
|
|
420
426
|
json(200, groups.list(workspaceId));
|
|
421
427
|
return;
|
|
422
428
|
}
|
|
423
|
-
const
|
|
424
|
-
if (
|
|
429
|
+
const snapshot = executionId ? groups.execution(workspaceId, roomId, executionId) : groups.get(workspaceId, roomId);
|
|
430
|
+
if (snapshot.revision === url.searchParams.get("revision"))
|
|
425
431
|
response.writeHead(204, { "cache-control": "no-store" }).end();
|
|
426
432
|
else
|
|
427
|
-
json(200,
|
|
433
|
+
json(200, snapshot);
|
|
428
434
|
return;
|
|
429
435
|
}
|
|
430
436
|
if (request.method === "POST") {
|
|
@@ -446,6 +452,15 @@ export function createHttpServer(options) {
|
|
|
446
452
|
json(200, await groups.configure(workspaceId, roomId, body));
|
|
447
453
|
return;
|
|
448
454
|
}
|
|
455
|
+
if (body?.action === "archive" || body?.action === "restore") {
|
|
456
|
+
json(200, await groups.setArchived(workspaceId, roomId, body.action === "archive"));
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (body?.action === "delete") {
|
|
460
|
+
await groups.deleteArchived(workspaceId, roomId);
|
|
461
|
+
response.writeHead(204, { "cache-control": "no-store" }).end();
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
449
464
|
if (body?.action === "stop" && typeof body.postId === "string") {
|
|
450
465
|
json(200, await groups.stop(workspaceId, roomId, body.postId));
|
|
451
466
|
return;
|
|
@@ -456,7 +471,7 @@ export function createHttpServer(options) {
|
|
|
456
471
|
response.writeHead(405, { allow: "GET, POST" }).end();
|
|
457
472
|
}
|
|
458
473
|
catch (error) {
|
|
459
|
-
json(400, { error: describeThrown(error) || "群聊操作失败。" });
|
|
474
|
+
json(error instanceof GroupChatNotFoundError ? 404 : 400, { error: describeThrown(error) || "群聊操作失败。" });
|
|
460
475
|
}
|
|
461
476
|
return;
|
|
462
477
|
}
|
package/dist/server/index.js
CHANGED
|
@@ -345,7 +345,7 @@ const projectService = new ProjectService(store, sessionOrchestratorService, {
|
|
|
345
345
|
void sessionOrchestratorService.restoreWatchdogs().catch((error) => {
|
|
346
346
|
console.warn("Super Pi: restoring watchdogs failed:", error instanceof Error ? error.message : String(error));
|
|
347
347
|
});
|
|
348
|
-
const groupChatService = await GroupChatService.open(join(dataDir, "group-chats.json"), store, runtime, memory);
|
|
348
|
+
const groupChatService = await GroupChatService.open(join(dataDir, "group-chats.json"), store, runtime, memory, update => broadcaster.broadcast({ type: "group.activity", ...update }));
|
|
349
349
|
const serverContext = {
|
|
350
350
|
memory,
|
|
351
351
|
knowledge,
|
package/dist/server/runtime.d.ts
CHANGED
|
@@ -249,6 +249,15 @@ export type ForkOutcome = {
|
|
|
249
249
|
/** Entry lines copied, header excluded. */
|
|
250
250
|
entriesCopied: number;
|
|
251
251
|
};
|
|
252
|
+
export type IsolatedExecutionEvent = {
|
|
253
|
+
kind: "thinking";
|
|
254
|
+
phase: "start" | "delta" | "end";
|
|
255
|
+
delta?: string;
|
|
256
|
+
} | {
|
|
257
|
+
kind: "tool";
|
|
258
|
+
phase: "start" | "update" | "end";
|
|
259
|
+
call: Pick<ToolCallNotice, "id" | "name" | "detail" | "status" | "durationMs">;
|
|
260
|
+
};
|
|
252
261
|
export type IsolatedCompletionInput = {
|
|
253
262
|
prompt: string;
|
|
254
263
|
timeoutMs: number;
|
|
@@ -258,6 +267,7 @@ export type IsolatedCompletionInput = {
|
|
|
258
267
|
agent?: AgentDefinition;
|
|
259
268
|
onText?: (text: string) => void;
|
|
260
269
|
onToolStart?: () => void;
|
|
270
|
+
onExecution?: (event: IsolatedExecutionEvent) => void;
|
|
261
271
|
};
|
|
262
272
|
export interface AgentRuntime {
|
|
263
273
|
answer(input: AnswerInput): AsyncIterable<string>;
|
package/dist/server/runtime.js
CHANGED
|
@@ -2535,6 +2535,12 @@ export class PiRpcRuntime {
|
|
|
2535
2535
|
let failure;
|
|
2536
2536
|
const splitter = new InlineThinkingSplitter();
|
|
2537
2537
|
const append = (parts) => {
|
|
2538
|
+
for (const part of parts) {
|
|
2539
|
+
if (part.kind === "thinking")
|
|
2540
|
+
input.onExecution?.({ kind: "thinking", phase: "delta", delta: part.text });
|
|
2541
|
+
else if (part.kind === "thinking-end")
|
|
2542
|
+
input.onExecution?.({ kind: "thinking", phase: "end" });
|
|
2543
|
+
}
|
|
2538
2544
|
const delta = parts.filter(part => part.kind === "text").map(part => part.text).join("");
|
|
2539
2545
|
if (!delta)
|
|
2540
2546
|
return;
|
|
@@ -2554,10 +2560,29 @@ export class PiRpcRuntime {
|
|
|
2554
2560
|
continue;
|
|
2555
2561
|
if (record.type === "tool_execution_start")
|
|
2556
2562
|
input.onToolStart?.();
|
|
2563
|
+
if (input.onExecution && record.toolCallId && (record.type === "tool_execution_start" || record.type === "tool_execution_update" || record.type === "tool_execution_end")) {
|
|
2564
|
+
const phase = record.type === "tool_execution_start" ? "start" : record.type === "tool_execution_update" ? "update" : "end";
|
|
2565
|
+
const name = (record.toolName ?? "tool").slice(0, 64);
|
|
2566
|
+
// Keep file targets and simple executable names. Command arguments and tool output stay in Pi.
|
|
2567
|
+
const detail = ["read", "write", "edit", "grep", "find", "ls"].includes(name)
|
|
2568
|
+
? describeToolCall("read", record.args)
|
|
2569
|
+
: name === "bash" || name === "powershell" ? describeToolCall(name, record.args)?.match(/^[a-z][a-z\d_.-]{0,31}(?=\s|$)/i)?.[0] : undefined;
|
|
2570
|
+
input.onExecution({ kind: "tool", phase, call: {
|
|
2571
|
+
id: record.toolCallId.slice(0, 160), name, detail,
|
|
2572
|
+
status: phase === "end" ? record.isError === true ? "error" : "ok" : undefined,
|
|
2573
|
+
durationMs: typeof record.durationMs === "number" && Number.isFinite(record.durationMs) ? Math.max(0, Math.round(record.durationMs)) : undefined,
|
|
2574
|
+
} });
|
|
2575
|
+
}
|
|
2557
2576
|
if (record.type === "message_update") {
|
|
2558
2577
|
const event = record.assistantMessageEvent;
|
|
2559
2578
|
if (event?.type === "text_delta")
|
|
2560
2579
|
append(splitter.push(event.delta ?? ""));
|
|
2580
|
+
else if (event?.type === "thinking_start")
|
|
2581
|
+
input.onExecution?.({ kind: "thinking", phase: "start" });
|
|
2582
|
+
else if (event?.type === "thinking_delta")
|
|
2583
|
+
input.onExecution?.({ kind: "thinking", phase: "delta", delta: event.delta });
|
|
2584
|
+
else if (event?.type === "thinking_end")
|
|
2585
|
+
input.onExecution?.({ kind: "thinking", phase: "end" });
|
|
2561
2586
|
if (event?.type === "done" || event?.type === "error")
|
|
2562
2587
|
noteOutcome(event.message, event.type === "error" ? "error" : event.reason);
|
|
2563
2588
|
}
|
package/dist/server/types.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export type ModelFilterRule = {
|
|
|
6
6
|
};
|
|
7
7
|
import type { WorkflowRun } from "./workflow.js";
|
|
8
8
|
import type { BackgroundTaskDetails } from "../shared/background-task.js";
|
|
9
|
+
import type { GroupActivityUpdate } from "../shared/group-chat.js";
|
|
9
10
|
import type { ScheduledTask } from "./scheduled-tasks.js";
|
|
10
11
|
import type { ThinkingLevelMap } from "../shared/thinking-levels.js";
|
|
11
12
|
import type { TerminalCommand, TerminalEvent } from "../shared/terminal-protocol.js";
|
|
@@ -1992,7 +1993,9 @@ export type ServerEvent = {
|
|
|
1992
1993
|
type: "message.accepted";
|
|
1993
1994
|
conversationId: string;
|
|
1994
1995
|
clientId: string;
|
|
1995
|
-
} | {
|
|
1996
|
+
} | ({
|
|
1997
|
+
type: "group.activity";
|
|
1998
|
+
} & GroupActivityUpdate) | {
|
|
1996
1999
|
type: "run.stopping";
|
|
1997
2000
|
conversationId: string;
|
|
1998
2001
|
} | {
|