File size: 2,419 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { z } from "zod";
const engineSessionStatusesSchema = z.record(z.string(), z.object({ type: z.string() }).passthrough());
const enginePendingRequestsSchema = z.array(z.object({ sessionID: z.string() }).passthrough());
export function sessionActivityFrom(statuses, permissions, questions, sessionId, descendantIds = [], unknownDescendants = 0) {
    const parsedStatuses = engineSessionStatusesSchema.safeParse(statuses);
    const parsedPermissions = enginePendingRequestsSchema.safeParse(permissions);
    const parsedQuestions = enginePendingRequestsSchema.safeParse(questions);
    const probesComplete = parsedStatuses.success && parsedPermissions.success && parsedQuestions.success;
    const waiting = new Set([
        ...parsedPermissions.success ? parsedPermissions.data.map((request) => request.sessionID) : [],
        ...parsedQuestions.success ? parsedQuestions.data.map((request) => request.sessionID) : [],
    ]);
    const statusFor = (id) => {
        const type = parsedStatuses.success ? parsedStatuses.data[id]?.type : undefined;
        if (type === "error")
            return "error";
        if (waiting.has(id) || type === "waiting")
            return "waiting";
        if (type === "busy" || type === "running")
            return "busy";
        if (type === "retry" || type === "compacting" || type === "thinking" || type === "responding")
            return type;
        return probesComplete && (type === undefined || type === "idle") ? "idle" : "unknown";
    };
    const descendantActivity = { busy: 0, waiting: 0, unknown: unknownDescendants };
    for (const id of new Set(descendantIds)) {
        if (id === sessionId)
            continue;
        const status = statusFor(id);
        if (status === "unknown")
            descendantActivity.unknown += 1;
        else if (status === "waiting")
            descendantActivity.waiting += 1;
        else if (status !== "idle" && status !== "error")
            descendantActivity.busy += 1;
    }
    const own = statusFor(sessionId);
    return {
        status: own !== "error" && descendantActivity.waiting > 0 ? "waiting" : own,
        working: own !== "idle" && own !== "error" && own !== "unknown"
            || descendantActivity.busy > 0 || descendantActivity.waiting > 0,
        descendantActivity,
        inventoryComplete: probesComplete && own !== "unknown" && descendantActivity.unknown === 0,
    };
}