Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import { IssueRef } from "./issue.ts";
4import { PullRequestRef } from "./pullRequest.ts";
5
6export const WorkflowRunStatus = Schema.Literals([
7 "running",
8 "waiting",
9 "paused",
10 "completed",
11 "stopped",
12 /** Ended by the workflow as not its to do. Lists leave these out unless asked. */
13 "skipped",
14 "cancelled",
15]);
16export type WorkflowRunStatus = typeof WorkflowRunStatus.Type;
17
18export const WorkflowRun = Schema.Struct({
19 runId: Schema.String,
20 workflow: Schema.String,
21 version: Schema.Number,
22 title: Schema.String,
23 status: WorkflowRunStatus,
24 /** Why it is paused, what it waits on, or how it ended. */
25 detail: Schema.NullOr(Schema.String),
26 result: Schema.NullOr(Schema.Json),
27 /** How many times in a row it has been paused by a failing step. */
28 pauses: Schema.Number,
29 /** Its version is no longer registered, so it cannot go on until that version is back. */
30 retired: Schema.Boolean,
31 /** When a paused run is resumed without anyone asking; null when it waits for a person. */
32 resumesAt: Schema.NullOr(Schema.String),
33 /** The run that invoked this one and waits for it; null for a run started by itself. */
34 parentRunId: Schema.NullOr(Schema.String),
35 /** The step of that run this one was invoked as. */
36 parentStep: Schema.NullOr(Schema.String),
37 startedAt: Schema.String,
38 updatedAt: Schema.String,
39 endedAt: Schema.NullOr(Schema.String),
40});
41export type WorkflowRun = typeof WorkflowRun.Type;
42
43export const WorkflowStep = Schema.Struct({
44 name: Schema.String,
45 // A sleep's `detail` is when it wakes, as an ISO time.
46 kind: Schema.Literals(["step", "wait", "agent", "ask", "sleep"]),
47 status: Schema.Literals(["running", "completed", "failed"]),
48 attempts: Schema.Number,
49 detail: Schema.NullOr(Schema.String),
50 /** The thread an agent step ran in. */
51 threadId: Schema.NullOr(Schema.String),
52 /** The linked environment that thread is in; null when it is this one. */
53 environmentId: Schema.NullOr(Schema.String),
54 /** What it produced: a step's value, an agent's data or reply, the answer to a question. */
55 result: Schema.NullOr(Schema.Json),
56 /** What an agent said in closing, when its result is data and not that reply. */
57 said: Schema.NullOr(Schema.String),
58 startedAt: Schema.String,
59 endedAt: Schema.NullOr(Schema.String),
60});
61export type WorkflowStep = typeof WorkflowStep.Type;
62
63/** A question a run asked. Answer it with a signal to its run and name. */
64export const WorkflowQuestion = Schema.Struct({
65 runId: Schema.String,
66 name: Schema.String,
67 /** What `ask` was given: a title, the choices, and data to lay out. */
68 request: Schema.Json,
69 askedAt: Schema.String,
70 /** The run's latest agent thread, for talking it through before answering. */
71 thread: Schema.NullOr(
72 Schema.Struct({ threadId: Schema.String, environmentId: Schema.NullOr(Schema.String) }),
73 ),
74});
75export type WorkflowQuestion = typeof WorkflowQuestion.Type;
76
77/** Where a workflow takes requests. `path` holds the hook's secret. */
78export const WorkflowHook = Schema.Struct({
79 workflow: Schema.String,
80 name: Schema.String,
81 path: Schema.String,
82});
83export type WorkflowHook = typeof WorkflowHook.Type;
84
85/** What a client shows of workflows: recent runs, newest first, and the questions still open. */
86/** A workflow this server has registered, at its newest version, and what starts it. */
87export const WorkflowSummary = Schema.Struct({
88 name: Schema.String,
89 version: Schema.Number,
90 /** In words: "every 1h", "daily at 09:00", "webhook issues". Empty when only started by hand. */
91 triggers: Schema.Array(Schema.String),
92});
93export type WorkflowSummary = typeof WorkflowSummary.Type;
94
95/** Something wrong with the definitions: in `file` when it is one file's, at `line` when it names one. */
96export const WorkflowDefinitionProblem = Schema.Struct({
97 message: Schema.String,
98 file: Schema.optional(Schema.String),
99 line: Schema.optional(Schema.Number),
100});
101export type WorkflowDefinitionProblem = typeof WorkflowDefinitionProblem.Type;
102
103/** Which loading of the definitions file is running, and what is wrong with the file now. */
104export const WorkflowDefinitionState = Schema.Struct({
105 /** Goes up whenever the file is loaded or found wrong, so a client knows to read it again. */
106 revision: Schema.Number,
107 /** Why the file on disk is not what runs. The last version that loaded keeps running. */
108 problem: Schema.NullOr(Schema.String),
109});
110
111export const WorkflowDefinitionFile = Schema.Struct({
112 name: Schema.String,
113 /** The file as it is on disk, which is not what runs while it has problems. */
114 source: Schema.String,
115 /** The workflows its last good version defines, each as `name@version`. */
116 workflows: Schema.Array(Schema.String),
117 problems: Schema.Array(WorkflowDefinitionProblem),
118});
119export type WorkflowDefinitionFile = typeof WorkflowDefinitionFile.Type;
120
121/** The files that define this server's workflows. */
122export const WorkflowDefinitionFiles = Schema.Struct({
123 /** The folder the files are in. */
124 directory: Schema.String,
125 /** The server was pointed at one file: no file can be added beside it or taken away. */
126 single: Schema.Boolean,
127 files: Schema.Array(WorkflowDefinitionFile),
128 /** What is wrong between files, such as one workflow defined in two of them. */
129 problems: Schema.Array(WorkflowDefinitionProblem),
130 ...WorkflowDefinitionState.fields,
131});
132export type WorkflowDefinitionFiles = typeof WorkflowDefinitionFiles.Type;
133
134export const WorkflowDefinitionGetInput = Schema.Struct({});
135/** A file by its name in the folder. Saving a name that is not there yet makes the file. */
136export const WorkflowDefinitionSaveInput = Schema.Struct({
137 name: Schema.String,
138 source: Schema.String,
139});
140export const WorkflowDefinitionDeleteInput = Schema.Struct({ name: Schema.String });
141export const WorkflowDefinitionChangeResult = Schema.Struct({
142 /** The file was written or deleted. */
143 changed: Schema.Boolean,
144 /** Empty when the change is what runs now. With `changed`, why it does not run yet. */
145 problems: Schema.Array(WorkflowDefinitionProblem),
146});
147export type WorkflowDefinitionChangeResult = typeof WorkflowDefinitionChangeResult.Type;
148
149export const WorkflowRunsSnapshot = Schema.Struct({
150 workflows: Schema.Array(WorkflowSummary),
151 runs: Schema.Array(WorkflowRun),
152 questions: Schema.Array(WorkflowQuestion),
153 definition: Schema.optional(WorkflowDefinitionState),
154});
155
156/** How much is going on, for a badge that must not cost a list of runs to keep current. */
157export const WorkflowCounts = Schema.Struct({
158 /** Open questions and paused runs: what waits on a person. */
159 needsYou: Schema.Number,
160 /** Runs that have not ended. */
161 active: Schema.Number,
162 /** Runs there have ever been, so a client knows whether workflows are in use at all. */
163 total: Schema.Number,
164});
165export type WorkflowCounts = typeof WorkflowCounts.Type;
166export type WorkflowRunsSnapshot = typeof WorkflowRunsSnapshot.Type;
167
168export const WorkflowRunsSubscribeInput = Schema.Struct({});
169
170export const WorkflowRunGetInput = Schema.Struct({ runId: Schema.String });
171
172/**
173 * Something a run touched: an issue, a pull request, or a kind added later. `kind` is open, so a
174 * client shows a kind it does not know by its title and `url`. `key` is the thing's identity in
175 * its kind; `address` is whatever that kind needs to open it.
176 */
177export const WorkflowRunReference = Schema.Struct({
178 kind: Schema.String,
179 key: Schema.String,
180 address: Schema.Json,
181 /** Empty until something that touched it knew its title. */
182 title: Schema.String,
183 url: Schema.NullOr(Schema.String),
184 /** A word for where it stands, such as `open`, `merged` or `closed`. */
185 state: Schema.NullOr(Schema.String),
186 /** The steps that touched it, in the order they first did. */
187 steps: Schema.Array(Schema.String),
188 origin: Schema.Literals(["trigger", "step"]),
189});
190export type WorkflowRunReference = typeof WorkflowRunReference.Type;
191
192/** The kinds of reference runs record today; `address` is an `IssueRef` or a `PullRequestRef`. */
193export const WorkflowReferenceKind = { issue: "issue", pullRequest: "pullRequest" } as const;
194export const WorkflowIssueAddress = IssueRef;
195export const WorkflowPullRequestAddress = PullRequestRef;
196
197/** One run with its steps, in the order they started, and what it touched. */
198export const WorkflowRunDetail = Schema.Struct({
199 run: WorkflowRun,
200 steps: Schema.Array(WorkflowStep),
201 references: Schema.Array(WorkflowRunReference),
202});
203export type WorkflowRunDetail = typeof WorkflowRunDetail.Type;
204
205/** A person's answer to a `WorkflowQuestion`: one of its choices and, if they gave one, why. */
206export const WorkflowAnswerInput = Schema.Struct({
207 runId: Schema.String,
208 name: Schema.String,
209 choice: Schema.String,
210 reason: Schema.optional(Schema.String),
211});
212
213export const WorkflowRunResumeInput = Schema.Struct({ runId: Schema.String });
214
215export const WorkflowRunCancelInput = Schema.Struct({
216 runId: Schema.String,
217 reason: Schema.optional(Schema.String),
218});
219
220export class WorkflowRequestError extends Schema.TaggedError<WorkflowRequestError>()(
221 "WorkflowRequestError",
222 { message: Schema.String },
223) {}