Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import {
4 GuestMessageId,
5 GuestShareId,
6 IsoDateTime,
7 ProjectId,
8 RunId,
9 RuntimeRequestId,
10 ThreadId,
11 TrimmedNonEmptyString,
12} from "./baseSchemas.ts";
13import { ThreadPullRequestKey } from "./threadPullRequest.ts";
14
15/**
16 * Milestones are the coarse state changes worth telling someone about: a turn ended, an agent
17 * waits on a person, a guest asked something, a watched pull request woke its thread.
18 * They are derived from the authoritative stores and kept only in memory, so a consumer that
19 * needs the full state reads it from those stores.
20 */
21export const MilestoneId = TrimmedNonEmptyString.pipe(Schema.brand("MilestoneId"));
22export type MilestoneId = typeof MilestoneId.Type;
23
24/** Who caused a milestone, as far as the server knows. */
25export const MilestoneActor = Schema.Struct({
26 type: Schema.Literals(["user", "agent", "system", "guest"]),
27 id: Schema.optional(TrimmedNonEmptyString),
28 label: Schema.optional(TrimmedNonEmptyString),
29});
30export type MilestoneActor = typeof MilestoneActor.Type;
31
32const MilestoneEnvelope = Schema.Struct({
33 /** Stable for one transition, so publishing it twice is one milestone. */
34 id: MilestoneId,
35 occurredAt: IsoDateTime,
36 projectId: Schema.NullOr(ProjectId),
37 threadId: Schema.NullOr(ThreadId),
38 parentThreadId: Schema.NullOr(ThreadId),
39 actor: MilestoneActor,
40 /** A subagent's thread, or a run another run invoked. Muted unless asked for. */
41 isSubagentChild: Schema.Boolean,
42 /** The workflow run a workflow milestone is about, or whose agent step the thread is. */
43 workflowRunId: Schema.optional(TrimmedNonEmptyString),
44});
45
46/** `approval` covers command, file and permission prompts; `user_input` covers questions. */
47export const MilestoneInputKind = Schema.Literals(["user_input", "approval"]);
48export type MilestoneInputKind = typeof MilestoneInputKind.Type;
49
50/** Which workflow run a workflow milestone is about. Its envelope names no project or thread. */
51const WorkflowRunFields = {
52 workflowRunId: TrimmedNonEmptyString,
53 workflow: TrimmedNonEmptyString,
54 runTitle: Schema.String,
55 /** The run that invoked this one, when one did. */
56 parentWorkflowRunId: Schema.NullOr(TrimmedNonEmptyString),
57};
58
59export const MilestoneEvent = Schema.Union([
60 Schema.Struct({
61 ...MilestoneEnvelope.fields,
62 type: Schema.Literal("thread.run_settled"),
63 runId: RunId,
64 outcome: Schema.Literals(["completed", "failed", "interrupted", "cancelled"]),
65 }),
66 Schema.Struct({
67 ...MilestoneEnvelope.fields,
68 type: Schema.Literal("thread.needs_input"),
69 requestId: RuntimeRequestId,
70 kind: MilestoneInputKind,
71 }),
72 Schema.Struct({
73 ...MilestoneEnvelope.fields,
74 type: Schema.Literal("thread.input_resolved"),
75 requestId: RuntimeRequestId,
76 kind: MilestoneInputKind,
77 resolution: Schema.Literals(["resolved", "expired", "cancelled"]),
78 }),
79 Schema.Struct({
80 ...MilestoneEnvelope.fields,
81 type: Schema.Literal("thread.guest_request"),
82 shareId: GuestShareId,
83 guestMessageId: GuestMessageId,
84 }),
85 Schema.Struct({
86 ...MilestoneEnvelope.fields,
87 type: Schema.Literal("thread.guest_request_resolved"),
88 shareId: GuestShareId,
89 guestMessageId: GuestMessageId,
90 /** `closed` when the link was closed or replaced with the request still waiting. */
91 resolution: Schema.Literals(["sent", "dismissed", "closed"]),
92 }),
93 Schema.Struct({
94 ...MilestoneEnvelope.fields,
95 type: Schema.Literal("pr.watch_wake"),
96 pullRequests: Schema.Array(ThreadPullRequestKey),
97 outcome: Schema.Literals(["completed", "failed", "cancelled", "updated", "unknown"]),
98 summary: Schema.String,
99 /** The watch ended with this wake, as a merge, a close or a read failure ends it. */
100 watchEnded: Schema.Boolean,
101 }),
102 Schema.Struct({
103 ...MilestoneEnvelope.fields,
104 ...WorkflowRunFields,
105 type: Schema.Literal("workflow.run_started"),
106 }),
107 Schema.Struct({
108 ...MilestoneEnvelope.fields,
109 ...WorkflowRunFields,
110 type: Schema.Literal("workflow.needs_input"),
111 /** The step that asks, which answering names. */
112 step: TrimmedNonEmptyString,
113 question: Schema.String,
114 }),
115 Schema.Struct({
116 ...MilestoneEnvelope.fields,
117 ...WorkflowRunFields,
118 type: Schema.Literal("workflow.input_resolved"),
119 step: TrimmedNonEmptyString,
120 /** `cancelled` when the run ended with the question still open. */
121 resolution: Schema.Literals(["answered", "expired", "cancelled"]),
122 }),
123 Schema.Struct({
124 ...MilestoneEnvelope.fields,
125 ...WorkflowRunFields,
126 type: Schema.Literal("workflow.run_paused"),
127 reason: Schema.String,
128 }),
129 Schema.Struct({
130 ...MilestoneEnvelope.fields,
131 ...WorkflowRunFields,
132 type: Schema.Literal("workflow.run_resumed"),
133 }),
134 Schema.Struct({
135 ...MilestoneEnvelope.fields,
136 ...WorkflowRunFields,
137 type: Schema.Literal("workflow.run_ended"),
138 outcome: Schema.Literals(["completed", "stopped", "skipped", "cancelled"]),
139 detail: Schema.NullOr(Schema.String),
140 }),
141]);
142export type MilestoneEvent = typeof MilestoneEvent.Type;
143
144export const MilestoneEventType = Schema.Literals([
145 "thread.run_settled",
146 "thread.needs_input",
147 "thread.input_resolved",
148 "thread.guest_request",
149 "thread.guest_request_resolved",
150 "pr.watch_wake",
151 "workflow.run_started",
152 "workflow.needs_input",
153 "workflow.input_resolved",
154 "workflow.run_paused",
155 "workflow.run_resumed",
156 "workflow.run_ended",
157]);
158export type MilestoneEventType = typeof MilestoneEventType.Type;
159
160export const MilestoneFilter = Schema.Struct({
161 types: Schema.optional(Schema.Array(MilestoneEventType)),
162 projectIds: Schema.optional(Schema.Array(ProjectId)),
163 threadIds: Schema.optional(Schema.Array(ThreadId)),
164 /** Workflow names. Given, it matches only workflow milestones. */
165 workflows: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
166 /** Workflow runs, with the threads that are their agent steps. */
167 workflowRunIds: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
168 /** Absent reads as false: subagent threads and invoked runs churn and are rarely news. */
169 includeSubagentChildren: Schema.optional(Schema.Boolean),
170});
171export type MilestoneFilter = typeof MilestoneFilter.Type;
172
173/** Whether `event` is one `filter` asks for. Each given list must contain the event's value. */
174export const milestoneMatches = (event: MilestoneEvent, filter: MilestoneFilter = {}): boolean =>
175 (filter.includeSubagentChildren === true || !event.isSubagentChild) &&
176 (filter.types === undefined || filter.types.includes(event.type)) &&
177 (filter.projectIds === undefined ||
178 (event.projectId !== null && filter.projectIds.includes(event.projectId))) &&
179 (filter.threadIds === undefined ||
180 (event.threadId !== null && filter.threadIds.includes(event.threadId))) &&
181 (filter.workflows === undefined ||
182 ("workflow" in event && filter.workflows.includes(event.workflow))) &&
183 (filter.workflowRunIds === undefined ||
184 (event.workflowRunId !== undefined && filter.workflowRunIds.includes(event.workflowRunId)));