Open History to see the last commit
1/**
2 * ThreadPlanProgressService - in-memory per-thread plan progress for the
3 * Working indicators (sidebar rows, in-chat working line).
4 *
5 * Plans are a progress annotation, not a surface of their own: the useful
6 * kernel of a turn.plan.updated event is "which step is the agent on right
7 * now". Ingestion records the current step here and the shell query reads it
8 * at mapping time — no persistence, no migration (same pattern as
9 * ThreadBackgroundLivenessService). Cleared when the turn settles or the
10 * session dies, so a finished plan never lingers as stale UI.
11 *
12 * @module ThreadPlanProgressService
13 */
14import * as Context from "effect/Context";
15import * as Effect from "effect/Effect";
16import * as Layer from "effect/Layer";
17
18export interface ThreadPlanProgress {
19 readonly step: string;
20 readonly completedSteps: number;
21 readonly totalSteps: number;
22}
23
24interface PlanStepInput {
25 readonly step: string;
26 readonly status: string;
27}
28
29export class ThreadPlanProgressService extends Context.Service<
30 ThreadPlanProgressService,
31 {
32 /**
33 * Feed one turn.plan.updated payload. An all-completed plan clears the
34 * entry (the turn is wrapping up; nothing is "in progress" anymore).
35 */
36 readonly recordPlanProgress: (threadId: string, plan: ReadonlyArray<PlanStepInput>) => void;
37
38 /** Turn settled or session died: the working indicator reverts to plain. */
39 readonly clearThreadPlanProgress: (threadId: string) => void;
40
41 readonly getThreadPlanProgress: (threadId: string) => ThreadPlanProgress | null;
42 }
43>()("t3/orchestration/ThreadPlanProgress/ThreadPlanProgressService") {}
44
45export function make(): ThreadPlanProgressService["Service"] {
46 const progressByThreadId = new Map<string, ThreadPlanProgress>();
47
48 return {
49 recordPlanProgress: (threadId, plan) => {
50 const totalSteps = plan.length;
51 const completedSteps = plan.filter((step) => step.status === "completed").length;
52 // Current step: the in-progress one, else the first pending one (a
53 // plan that was just written has no in-progress step yet).
54 const current =
55 plan.find((step) => step.status === "inProgress") ??
56 plan.find((step) => step.status !== "completed");
57 if (totalSteps === 0 || completedSteps === totalSteps || current === undefined) {
58 progressByThreadId.delete(threadId);
59 return;
60 }
61 progressByThreadId.set(threadId, {
62 step: current.step,
63 completedSteps,
64 totalSteps,
65 });
66 },
67
68 clearThreadPlanProgress: (threadId) => {
69 progressByThreadId.delete(threadId);
70 },
71
72 getThreadPlanProgress: (threadId) => progressByThreadId.get(threadId) ?? null,
73 };
74}
75
76export const layer = Layer.effect(ThreadPlanProgressService, Effect.sync(make));