@operad-io/engine 0.1.1 → 0.3.0

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.
Files changed (81) hide show
  1. package/config.schema.json +2 -1
  2. package/package.json +1 -1
  3. package/src/config/config.ts +74 -45
  4. package/src/flow/types.ts +4 -0
  5. package/src/flow/validate.ts +5 -0
  6. package/src/http/index.ts +2 -0
  7. package/src/http/plugins.ts +68 -0
  8. package/src/http/server.ts +342 -48
  9. package/src/kernel/decide.ts +115 -30
  10. package/src/kernel/state.ts +9 -0
  11. package/src/kernel/types.ts +25 -40
  12. package/src/log/open-log.ts +2 -5
  13. package/src/log/sqlite.ts +127 -85
  14. package/src/log/types.ts +35 -9
  15. package/src/loop/code-watch.ts +108 -2
  16. package/src/loop/older-code.ts +3 -4
  17. package/src/loop/serve.ts +122 -95
  18. package/src/notify/notify.ts +2 -6
  19. package/src/platform/atomic-write.ts +163 -0
  20. package/src/platform/file-lock.ts +43 -21
  21. package/src/platform/git-program.ts +108 -0
  22. package/src/platform/index.ts +3 -0
  23. package/src/platform/kept-workers.ts +290 -0
  24. package/src/platform/process-group.ts +11 -3
  25. package/src/platform/sqlite-open.ts +20 -6
  26. package/src/platform/sqlite-schema.ts +30 -0
  27. package/src/platform/step-reap.ts +33 -18
  28. package/src/platform/watch.ts +1 -11
  29. package/src/plugin/action-schema.ts +59 -1
  30. package/src/plugin/choice-info.ts +133 -0
  31. package/src/plugin/context.ts +279 -0
  32. package/src/plugin/declared-flows.ts +3 -10
  33. package/src/plugin/errors.ts +9 -0
  34. package/src/plugin/freeze.ts +12 -0
  35. package/src/plugin/harness.ts +28 -0
  36. package/src/plugin/index.ts +8 -7
  37. package/src/plugin/install.ts +265 -17
  38. package/src/plugin/keys.ts +2 -12
  39. package/src/plugin/load.ts +47 -23
  40. package/src/plugin/logger.ts +14 -0
  41. package/src/plugin/open-store.ts +0 -6
  42. package/src/plugin/question-form.ts +323 -0
  43. package/src/plugin/registry.ts +519 -76
  44. package/src/plugin/secrets.ts +14 -24
  45. package/src/plugin/semver.ts +52 -0
  46. package/src/plugin/settings.ts +1 -9
  47. package/src/plugin/sqlite-store.ts +94 -64
  48. package/src/plugin/store.ts +5 -0
  49. package/src/plugin/types.ts +575 -21
  50. package/src/plugin/validate.ts +402 -26
  51. package/src/runner/answer.ts +26 -68
  52. package/src/runner/ask.ts +227 -28
  53. package/src/runner/cancel.ts +10 -7
  54. package/src/runner/grants.ts +2 -2
  55. package/src/runner/index.ts +1 -3
  56. package/src/runner/limits.ts +2 -2
  57. package/src/runner/runner.ts +15 -14
  58. package/src/runner/service.ts +52 -19
  59. package/src/runner/step-worker.ts +9 -0
  60. package/src/testing/answer.ts +69 -0
  61. package/src/testing/clock-manual.ts +152 -0
  62. package/src/testing/config.ts +16 -0
  63. package/src/testing/fs.ts +172 -0
  64. package/src/testing/home.ts +43 -0
  65. package/src/testing/index.ts +19 -0
  66. package/src/testing/types.ts +2 -0
  67. package/src/testing/world.ts +9 -2
  68. package/src/trigger/kernel.ts +13 -46
  69. package/src/trigger/rules.ts +22 -12
  70. package/src/trigger/types.ts +2 -6
  71. package/src/util.ts +99 -0
  72. package/src/views/event-line.ts +22 -0
  73. package/src/views/history.ts +36 -19
  74. package/src/views/live-logs.ts +313 -0
  75. package/src/views/pending.ts +23 -5
  76. package/src/views/presentation.ts +629 -81
  77. package/src/views/running.ts +5 -5
  78. package/src/views/settings.ts +205 -66
  79. package/src/views/types.ts +8 -2
  80. package/src/views/uploads.ts +15 -7
  81. package/triggers.schema.json +4 -0
@@ -0,0 +1,323 @@
1
+ /**
2
+ * A form a step asks a person: one or more questions, each with its choices,
3
+ * single or multiple, free text allowed, and the person's answer per question.
4
+ * The kernel checks the shape and bounds, then carries it: it gives no question
5
+ * a meaning of its own and speaks no vendor's field names.
6
+ */
7
+
8
+ import { isRecord } from "../util.ts";
9
+ import type { JsonValue } from "../log/types.ts";
10
+ import type { AskQuestion, AskQuestionAnswer, AskQuestionChoice } from "./types.ts";
11
+
12
+ /** The most questions one form may carry. */
13
+ export const MAX_FORM_QUESTIONS = 8;
14
+
15
+ /** The most choices one question may offer. */
16
+ export const MAX_QUESTION_CHOICES = 8;
17
+
18
+ /** The longest question text. */
19
+ export const MAX_QUESTION_TEXT = 500;
20
+
21
+ /** The longest Markdown body a question may carry. */
22
+ export const MAX_QUESTION_BODY = 256 * 1024;
23
+
24
+ /** The longest header a question may carry. */
25
+ export const MAX_QUESTION_HEADER = 40;
26
+
27
+ /** The longest address a question may carry. */
28
+ export const MAX_QUESTION_URL = 2048;
29
+
30
+ /** The longest code a question may carry for a person to copy. */
31
+ export const MAX_QUESTION_CODE = 80;
32
+
33
+ /** The longest choice label. */
34
+ export const MAX_QUESTION_CHOICE_LABEL = 80;
35
+
36
+ /** The longest choice description. */
37
+ export const MAX_QUESTION_CHOICE_DESCRIPTION = 280;
38
+
39
+ /** The longest free-text answer. */
40
+ export const MAX_QUESTION_ANSWER_TEXT = 4000;
41
+
42
+ const QUESTION_FIELDS = new Set(["id", "text", "body", "header", "choices", "multiple", "freeText", "url", "code", "secret"]);
43
+ const CHOICE_FIELDS = new Set(["label", "description"]);
44
+ const ANSWER_FIELDS = new Set(["id", "choices", "text", "skipped"]);
45
+
46
+ /**
47
+ * The labels a client that cannot draw a form can still answer with: the one
48
+ * question's choices, when the form is one single-answer question. A form of
49
+ * several questions, or one that takes more than one label, gives none.
50
+ */
51
+ export function formChoices(questions: readonly AskQuestion[]): string[] {
52
+ if (questions.length !== 1) return [];
53
+ const question = questions[0];
54
+ if (question === undefined || question.multiple === true) return [];
55
+ return (question.choices ?? []).map((choice) => choice.label);
56
+ }
57
+
58
+ function nonEmpty(value: unknown): value is string {
59
+ return typeof value === "string" && value.trim().length > 0;
60
+ }
61
+
62
+ function choiceProblem(value: unknown, at: string): string | undefined {
63
+ if (!isRecord(value)) return `${at}: must be an object with a label`;
64
+ for (const field of Object.keys(value)) {
65
+ if (!CHOICE_FIELDS.has(field)) return `${at}.${field}: is not a field of a choice`;
66
+ }
67
+ if (!nonEmpty(value.label)) return `${at}.label: must be a non-empty string`;
68
+ if (value.label.length > MAX_QUESTION_CHOICE_LABEL) {
69
+ return `${at}.label: must be at most ${MAX_QUESTION_CHOICE_LABEL} characters`;
70
+ }
71
+ if (value.description !== undefined) {
72
+ if (typeof value.description !== "string") return `${at}.description: must be a string`;
73
+ if (value.description.length > MAX_QUESTION_CHOICE_DESCRIPTION) {
74
+ return `${at}.description: must be at most ${MAX_QUESTION_CHOICE_DESCRIPTION} characters`;
75
+ }
76
+ }
77
+ return undefined;
78
+ }
79
+
80
+ /**
81
+ * What is wrong with a `questions` value, if anything. The returned text names
82
+ * the question and field; the caller adds its own prefix.
83
+ */
84
+ export function questionsProblem(value: unknown): string | undefined {
85
+ if (value === undefined) return undefined;
86
+ if (!Array.isArray(value) || value.length === 0) {
87
+ return "must be a list of at least one question";
88
+ }
89
+ if (value.length > MAX_FORM_QUESTIONS) {
90
+ return `must carry at most ${MAX_FORM_QUESTIONS} questions`;
91
+ }
92
+ const ids = new Set<string>();
93
+ for (const [index, raw] of value.entries()) {
94
+ const at = `questions[${index}]`;
95
+ if (!isRecord(raw)) return `${at}: must be an object`;
96
+ for (const field of Object.keys(raw)) {
97
+ if (!QUESTION_FIELDS.has(field)) return `${at}.${field}: is not a field of a question`;
98
+ }
99
+ if (!nonEmpty(raw.id)) return `${at}.id: must be a non-empty string`;
100
+ if (ids.has(raw.id)) return `${at}.id: "${raw.id}" names an earlier question`;
101
+ ids.add(raw.id);
102
+ if (!nonEmpty(raw.text)) return `${at}.text: must be a non-empty string`;
103
+ if (raw.text.length > MAX_QUESTION_TEXT) {
104
+ return `${at}.text: must be at most ${MAX_QUESTION_TEXT} characters`;
105
+ }
106
+ if (raw.body !== undefined) {
107
+ if (typeof raw.body !== "string") return `${at}.body: must be a string`;
108
+ if (raw.body.length > MAX_QUESTION_BODY) {
109
+ return `${at}.body: must be at most ${MAX_QUESTION_BODY} characters`;
110
+ }
111
+ }
112
+ if (raw.header !== undefined) {
113
+ if (typeof raw.header !== "string") return `${at}.header: must be a string`;
114
+ if (raw.header.length > MAX_QUESTION_HEADER) {
115
+ return `${at}.header: must be at most ${MAX_QUESTION_HEADER} characters`;
116
+ }
117
+ }
118
+ if (raw.choices !== undefined) {
119
+ if (!Array.isArray(raw.choices)) return `${at}.choices: must be a list of choices`;
120
+ if (raw.choices.length > MAX_QUESTION_CHOICES) {
121
+ return `${at}.choices: must offer at most ${MAX_QUESTION_CHOICES} choices`;
122
+ }
123
+ const labels = new Set<string>();
124
+ for (const [position, choice] of raw.choices.entries()) {
125
+ const choiceAt = `${at}.choices[${position}]`;
126
+ const problem = choiceProblem(choice, choiceAt);
127
+ if (problem !== undefined) return problem;
128
+ const label = (choice as Record<string, unknown>).label as string;
129
+ if (labels.has(label)) return `${choiceAt}.label: "${label}" names an earlier choice`;
130
+ labels.add(label);
131
+ }
132
+ }
133
+ for (const field of ["multiple", "freeText"] as const) {
134
+ if (raw[field] !== undefined && typeof raw[field] !== "boolean") {
135
+ return `${at}.${field}: must be true or false`;
136
+ }
137
+ }
138
+ if (raw.url !== undefined) {
139
+ if (typeof raw.url !== "string") return `${at}.url: must be a string`;
140
+ if (raw.url.length > MAX_QUESTION_URL) {
141
+ return `${at}.url: must be at most ${MAX_QUESTION_URL} characters`;
142
+ }
143
+ if (!URL.canParse(raw.url) || !["http:", "https:"].includes(new URL(raw.url).protocol)) {
144
+ return `${at}.url: must be an absolute http or https address`;
145
+ }
146
+ }
147
+ if (raw.code !== undefined) {
148
+ if (!nonEmpty(raw.code)) return `${at}.code: must be a non-empty string`;
149
+ if (raw.code.length > MAX_QUESTION_CODE) {
150
+ return `${at}.code: must be at most ${MAX_QUESTION_CODE} characters`;
151
+ }
152
+ }
153
+ if (raw.secret !== undefined && raw.secret !== true) {
154
+ return `${at}.secret: must be true when it is set`;
155
+ }
156
+ if (raw.secret === true && raw.freeText !== true) {
157
+ return `${at}.secret: needs freeText true, because the answer is the person's own text`;
158
+ }
159
+ }
160
+ return undefined;
161
+ }
162
+
163
+ /** Copies the fields the kernel knows from a value it has already checked. */
164
+ export function normalizeQuestions(value: unknown): readonly AskQuestion[] | undefined {
165
+ if (!Array.isArray(value)) return undefined;
166
+ const questions: AskQuestion[] = [];
167
+ for (const raw of value) {
168
+ if (!isRecord(raw) || !nonEmpty(raw.id) || typeof raw.text !== "string") continue;
169
+ const choices: AskQuestionChoice[] = [];
170
+ if (Array.isArray(raw.choices)) {
171
+ for (const choice of raw.choices) {
172
+ if (!isRecord(choice) || !nonEmpty(choice.label)) continue;
173
+ choices.push({
174
+ label: choice.label,
175
+ ...(typeof choice.description === "string" ? { description: choice.description } : {}),
176
+ });
177
+ }
178
+ }
179
+ questions.push({
180
+ id: raw.id,
181
+ text: raw.text,
182
+ ...(typeof raw.body === "string" ? { body: raw.body } : {}),
183
+ ...(typeof raw.header === "string" ? { header: raw.header } : {}),
184
+ ...(choices.length === 0 ? {} : { choices }),
185
+ ...(raw.multiple === true ? { multiple: true } : {}),
186
+ ...(raw.freeText === true ? { freeText: true } : {}),
187
+ ...(typeof raw.url === "string" ? { url: raw.url } : {}),
188
+ ...(typeof raw.code === "string" ? { code: raw.code } : {}),
189
+ ...(raw.secret === true ? { secret: true as const } : {}),
190
+ });
191
+ }
192
+ return questions.length === 0 ? undefined : questions;
193
+ }
194
+
195
+ /**
196
+ * Reads a form a fact or a step's data carries. A value that cannot be read is
197
+ * left out, so an old or corrupt log does not hide a question.
198
+ */
199
+ export function storedQuestions(value: unknown): readonly AskQuestion[] | undefined {
200
+ if (value === undefined) return undefined;
201
+ if (questionsProblem(value) !== undefined) return undefined;
202
+ return normalizeQuestions(value);
203
+ }
204
+
205
+ /** The form as a fact payload stores it; it holds only JSON values. */
206
+ export function questionsJson(questions: readonly AskQuestion[]): JsonValue {
207
+ return questions as unknown as JsonValue;
208
+ }
209
+
210
+ /**
211
+ * What is wrong with a person's `answers` for this form, if anything. Missing
212
+ * questions are the person skipping them, not an error.
213
+ */
214
+ export function answersProblem(
215
+ value: unknown,
216
+ questions: readonly AskQuestion[],
217
+ ): string | undefined {
218
+ if (value === undefined) return undefined;
219
+ if (!Array.isArray(value)) return "must be a list of answers";
220
+ if (value.length > questions.length) {
221
+ return `must carry at most one answer per question (${questions.length})`;
222
+ }
223
+ const byId = new Map(questions.map((question) => [question.id, question]));
224
+ const seen = new Set<string>();
225
+ for (const [index, raw] of value.entries()) {
226
+ const at = `answers[${index}]`;
227
+ if (!isRecord(raw)) return `${at}: must be an object`;
228
+ for (const field of Object.keys(raw)) {
229
+ if (!ANSWER_FIELDS.has(field)) return `${at}.${field}: is not a field of an answer`;
230
+ }
231
+ if (typeof raw.id !== "string" || !byId.has(raw.id)) {
232
+ return `${at}.id: must name one of the form's questions`;
233
+ }
234
+ if (seen.has(raw.id)) return `${at}.id: "${raw.id}" is answered twice`;
235
+ seen.add(raw.id);
236
+ const question = byId.get(raw.id) as AskQuestion;
237
+ const skipped = raw.skipped === true;
238
+ if (raw.skipped !== undefined && raw.skipped !== true) {
239
+ return `${at}.skipped: must be true when it is set`;
240
+ }
241
+ if (raw.choices !== undefined) {
242
+ if (!Array.isArray(raw.choices)) return `${at}.choices: must be a list of labels`;
243
+ const labels = (question.choices ?? []).map((choice) => choice.label);
244
+ const picked = new Set<string>();
245
+ for (const label of raw.choices) {
246
+ if (typeof label !== "string" || !labels.includes(label)) {
247
+ return `${at}.choices: "${String(label)}" is not one of the question's choices`;
248
+ }
249
+ if (picked.has(label)) return `${at}.choices: "${label}" is picked twice`;
250
+ picked.add(label);
251
+ }
252
+ if (raw.choices.length > 1 && question.multiple !== true) {
253
+ return `${at}.choices: this question takes one choice`;
254
+ }
255
+ if (raw.choices.length > 0 && skipped) {
256
+ return `${at}: a skipped question carries no choice`;
257
+ }
258
+ }
259
+ if (raw.text !== undefined) {
260
+ if (typeof raw.text !== "string") return `${at}.text: must be a string`;
261
+ if (raw.text.length > MAX_QUESTION_ANSWER_TEXT) {
262
+ return `${at}.text: must be at most ${MAX_QUESTION_ANSWER_TEXT} characters`;
263
+ }
264
+ if (question.freeText !== true) return `${at}.text: this question takes no free text`;
265
+ if (skipped && raw.text.trim().length > 0) {
266
+ return `${at}: a skipped question carries no text`;
267
+ }
268
+ }
269
+ }
270
+ return undefined;
271
+ }
272
+
273
+ /**
274
+ * The answers the person gave, one for each question in the form's order, with
275
+ * `skipped` where they gave none.
276
+ */
277
+ export function normalizeAnswers(
278
+ value: unknown,
279
+ questions: readonly AskQuestion[],
280
+ ): readonly AskQuestionAnswer[] | undefined {
281
+ if (value === undefined) return undefined;
282
+ const given = new Map<string, Record<string, unknown>>();
283
+ if (Array.isArray(value)) {
284
+ for (const raw of value) {
285
+ if (!isRecord(raw) || typeof raw.id !== "string") continue;
286
+ given.set(raw.id, raw);
287
+ }
288
+ }
289
+ return questions.map((question): AskQuestionAnswer => {
290
+ const raw = given.get(question.id);
291
+ if (raw === undefined) return { id: question.id, skipped: true };
292
+ const choices = Array.isArray(raw.choices)
293
+ ? raw.choices.filter((label): label is string => typeof label === "string")
294
+ : [];
295
+ const text = typeof raw.text === "string" && raw.text.trim().length > 0 ? raw.text : undefined;
296
+ if (raw.skipped === true || (choices.length === 0 && text === undefined)) {
297
+ return { id: question.id, skipped: true };
298
+ }
299
+ return {
300
+ id: question.id,
301
+ ...(choices.length === 0 ? {} : { choices }),
302
+ ...(text === undefined ? {} : { text }),
303
+ };
304
+ });
305
+ }
306
+
307
+ /**
308
+ * A person's answer to a form as a fact or a step's data carries it. Invalid
309
+ * entries are left out, so a log never holds an answer the form did not offer.
310
+ */
311
+ export function storedAnswers(
312
+ value: unknown,
313
+ questions: readonly AskQuestion[],
314
+ ): readonly AskQuestionAnswer[] | undefined {
315
+ if (value === undefined) return undefined;
316
+ if (answersProblem(value, questions) !== undefined) return undefined;
317
+ return normalizeAnswers(value, questions);
318
+ }
319
+
320
+ /** The answers as a fact payload stores them; they hold only JSON values. */
321
+ export function answersJson(answers: readonly AskQuestionAnswer[]): JsonValue {
322
+ return answers as unknown as JsonValue;
323
+ }