@operad-io/engine 0.0.0-stage → 0.0.1
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.
- package/LICENSE +21 -0
- package/config.schema.json +188 -0
- package/keys.schema.json +12 -0
- package/package.json +22 -4
- package/src/config/config.ts +627 -0
- package/src/config/index.ts +1 -0
- package/src/flow/define-flow.ts +57 -0
- package/src/flow/index.ts +5 -0
- package/src/flow/load.ts +41 -0
- package/src/flow/types.ts +107 -0
- package/src/flow/validate.ts +296 -0
- package/src/http/address.ts +142 -0
- package/src/http/index.ts +25 -0
- package/src/http/link.ts +20 -0
- package/src/http/server.ts +920 -0
- package/src/http/token.ts +85 -0
- package/src/index.ts +16 -0
- package/src/kernel/decide.ts +689 -0
- package/src/kernel/index.ts +3 -0
- package/src/kernel/state.ts +275 -0
- package/src/kernel/types.ts +366 -0
- package/src/log/errors.ts +77 -0
- package/src/log/facts.ts +50 -0
- package/src/log/index.ts +6 -0
- package/src/log/json-value.ts +67 -0
- package/src/log/open-log.ts +17 -0
- package/src/log/schema.ts +5 -0
- package/src/log/secret.ts +30 -0
- package/src/log/sqlite.ts +1011 -0
- package/src/log/types.ts +230 -0
- package/src/loop/code-watch.ts +486 -0
- package/src/loop/index.ts +3 -0
- package/src/loop/older-code.ts +58 -0
- package/src/loop/serve.ts +483 -0
- package/src/markdown.ts +451 -0
- package/src/notify/index.ts +10 -0
- package/src/notify/notify.ts +256 -0
- package/src/platform/file-lock.ts +385 -0
- package/src/platform/index.ts +13 -0
- package/src/platform/load.ts +18 -0
- package/src/platform/process-group.ts +635 -0
- package/src/platform/process-watch.ts +152 -0
- package/src/platform/sqlite-open.ts +104 -0
- package/src/platform/step-reap.ts +668 -0
- package/src/platform/watch.ts +158 -0
- package/src/platform/worker-reports.ts +34 -0
- package/src/plugin/action-schema.ts +550 -0
- package/src/plugin/declared-flows.ts +74 -0
- package/src/plugin/define-plugin.ts +12 -0
- package/src/plugin/errors.ts +73 -0
- package/src/plugin/filters.ts +46 -0
- package/src/plugin/harness.ts +50 -0
- package/src/plugin/home.ts +13 -0
- package/src/plugin/index.ts +65 -0
- package/src/plugin/install.ts +268 -0
- package/src/plugin/keys.ts +63 -0
- package/src/plugin/load.ts +256 -0
- package/src/plugin/logger.ts +30 -0
- package/src/plugin/object-facts.ts +132 -0
- package/src/plugin/open-store.ts +32 -0
- package/src/plugin/paging.ts +133 -0
- package/src/plugin/registry.ts +1783 -0
- package/src/plugin/secrets.ts +141 -0
- package/src/plugin/semver.ts +110 -0
- package/src/plugin/settings.ts +64 -0
- package/src/plugin/sqlite-store.ts +756 -0
- package/src/plugin/store.ts +318 -0
- package/src/plugin/types.ts +1236 -0
- package/src/plugin/usage.ts +157 -0
- package/src/plugin/validate.ts +1028 -0
- package/src/runner/answer.ts +156 -0
- package/src/runner/ask.ts +483 -0
- package/src/runner/cancel.ts +51 -0
- package/src/runner/errors.ts +20 -0
- package/src/runner/grants.ts +92 -0
- package/src/runner/index.ts +33 -0
- package/src/runner/limits.ts +403 -0
- package/src/runner/lock.ts +140 -0
- package/src/runner/runner.ts +1175 -0
- package/src/runner/service.ts +184 -0
- package/src/runner/step-worker.ts +303 -0
- package/src/testing/clock.ts +67 -0
- package/src/testing/index.ts +90 -0
- package/src/testing/types.ts +116 -0
- package/src/testing/world.ts +369 -0
- package/src/trigger/cron.ts +193 -0
- package/src/trigger/index.ts +32 -0
- package/src/trigger/kernel.ts +1212 -0
- package/src/trigger/rules.ts +300 -0
- package/src/trigger/store.ts +233 -0
- package/src/trigger/types.ts +156 -0
- package/src/util.ts +50 -0
- package/src/views/history.ts +634 -0
- package/src/views/index.ts +7 -0
- package/src/views/pending.ts +209 -0
- package/src/views/presentation.ts +2551 -0
- package/src/views/running.ts +255 -0
- package/src/views/settings.ts +452 -0
- package/src/views/types.ts +72 -0
- package/src/views/uploads.ts +228 -0
- package/triggers.schema.json +48 -0
- package/README.md +0 -3
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/** The pending view: every run waiting for a person, with what can be done. */
|
|
2
|
+
|
|
3
|
+
import { OLDER_CODE_ITEM_ID, OLDER_CODE_PROMPT, servesOlderCode } from "../loop/older-code.ts";
|
|
4
|
+
import { objectLink } from "../http/link.ts";
|
|
5
|
+
import type { LogBackend, RunInfo } from "../log/types.ts";
|
|
6
|
+
import type { PluginRegistry } from "../plugin/registry.ts";
|
|
7
|
+
import type { Views, ViewRow } from "./presentation.ts";
|
|
8
|
+
import type { PendingActionView } from "../plugin/types.ts";
|
|
9
|
+
import { answerableAsks, scanAsks } from "../runner/ask.ts";
|
|
10
|
+
import { parseObjectKey, readState } from "../runner/service.ts";
|
|
11
|
+
import type { RefusedStart, TriggerCandidate } from "../trigger/types.ts";
|
|
12
|
+
|
|
13
|
+
/** Where the pending view gets candidates the kernel derived from the log. */
|
|
14
|
+
export interface CandidateSource {
|
|
15
|
+
candidates(): Promise<readonly TriggerCandidate[]> | readonly TriggerCandidate[];
|
|
16
|
+
/** Events whose start is refused for now, each with the reason. */
|
|
17
|
+
refusedStarts?():
|
|
18
|
+
| Promise<readonly RefusedStart[]>
|
|
19
|
+
| readonly RefusedStart[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface PendingItem {
|
|
23
|
+
/** A run id, or the pending id of a candidate event. */
|
|
24
|
+
readonly runId: string;
|
|
25
|
+
readonly flow: string;
|
|
26
|
+
readonly step: string | null;
|
|
27
|
+
readonly prompt: string;
|
|
28
|
+
readonly choices: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* The question's wait id, which an answer quotes. Absent when the item asks
|
|
31
|
+
* nothing (a candidate or a standing item).
|
|
32
|
+
*/
|
|
33
|
+
readonly waitId?: string;
|
|
34
|
+
readonly actions: readonly PendingActionView[];
|
|
35
|
+
readonly kind?: "run" | "candidate" | "refused" | "standing";
|
|
36
|
+
readonly pendingId?: string;
|
|
37
|
+
/** The object the run is about, as `<type>:<id>`; absent for a candidate. */
|
|
38
|
+
readonly object?: string;
|
|
39
|
+
/** The link that opens the object in the app; absent for a candidate. */
|
|
40
|
+
readonly link?: string;
|
|
41
|
+
/** The object's plugin answers this wait automatically; set only when asked for. */
|
|
42
|
+
readonly auto?: true;
|
|
43
|
+
/** False when a channel should not announce the item; it is still pending. */
|
|
44
|
+
readonly announce?: false;
|
|
45
|
+
/** Object keys the item stands for besides its own object. */
|
|
46
|
+
readonly covers?: readonly string[];
|
|
47
|
+
/**
|
|
48
|
+
* The object's row, the same one the list shows; an error row when it cannot
|
|
49
|
+
* be built. Absent for a candidate, and when the caller gave no views.
|
|
50
|
+
*/
|
|
51
|
+
readonly row?: ViewRow;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Runs waiting for a person, in the order they started. */
|
|
55
|
+
export async function listPending(
|
|
56
|
+
log: LogBackend,
|
|
57
|
+
registry: PluginRegistry,
|
|
58
|
+
triggers?: CandidateSource,
|
|
59
|
+
views?: Views,
|
|
60
|
+
options: { readonly withAuto?: boolean } = {},
|
|
61
|
+
): Promise<readonly PendingItem[]> {
|
|
62
|
+
const runs = (await log.runs({ active: true })).sort(
|
|
63
|
+
(left, right) => left.startedAt - right.startedAt,
|
|
64
|
+
);
|
|
65
|
+
const items: PendingItem[] = [];
|
|
66
|
+
|
|
67
|
+
for (const run of runs) {
|
|
68
|
+
const state = await readState(log, run.runId);
|
|
69
|
+
if (state.status === "running") {
|
|
70
|
+
// A step waiting on its own question is one item, like a flow wait.
|
|
71
|
+
const { asks } = await scanAsks(log, run.runId);
|
|
72
|
+
const open = answerableAsks(asks, state).at(-1);
|
|
73
|
+
if (open !== undefined) {
|
|
74
|
+
items.push({
|
|
75
|
+
runId: run.runId,
|
|
76
|
+
flow: run.flow,
|
|
77
|
+
step: state.step,
|
|
78
|
+
prompt: open.prompt,
|
|
79
|
+
choices: open.choices,
|
|
80
|
+
waitId: open.waitId,
|
|
81
|
+
actions: await actionsFor(registry, log, run),
|
|
82
|
+
kind: "run",
|
|
83
|
+
...objectFields(run.object),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (state.status !== "waiting_person" || state.wait === null) continue;
|
|
89
|
+
|
|
90
|
+
const waitObject = parseObjectKey(run.object);
|
|
91
|
+
const auto =
|
|
92
|
+
waitObject !== undefined && (await registry.answeredAutomatically(waitObject));
|
|
93
|
+
// A wait that is about to be answered automatically is no person's to answer.
|
|
94
|
+
if (auto && options.withAuto !== true) continue;
|
|
95
|
+
const actions = await actionsFor(registry, log, run);
|
|
96
|
+
items.push({
|
|
97
|
+
runId: run.runId,
|
|
98
|
+
flow: run.flow,
|
|
99
|
+
step: state.step,
|
|
100
|
+
prompt: state.wait.prompt,
|
|
101
|
+
choices: state.wait.choices,
|
|
102
|
+
waitId: state.wait.id,
|
|
103
|
+
actions,
|
|
104
|
+
kind: "run",
|
|
105
|
+
...(auto ? { auto: true as const } : {}),
|
|
106
|
+
...objectFields(run.object),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const candidates =
|
|
111
|
+
triggers === undefined ? [] : await triggers.candidates();
|
|
112
|
+
for (const candidate of candidates) {
|
|
113
|
+
items.push({
|
|
114
|
+
runId: candidate.pendingId,
|
|
115
|
+
flow: candidate.flow,
|
|
116
|
+
step: null,
|
|
117
|
+
prompt: `would start ${candidate.flow} on ${candidate.object}`,
|
|
118
|
+
choices: [],
|
|
119
|
+
actions: [
|
|
120
|
+
{ plugin: "operad", action: "start" },
|
|
121
|
+
{ plugin: "operad", action: "dismiss" },
|
|
122
|
+
],
|
|
123
|
+
kind: "candidate",
|
|
124
|
+
pendingId: candidate.pendingId,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const refused of (await triggers?.refusedStarts?.()) ?? []) {
|
|
129
|
+
items.push({
|
|
130
|
+
runId: refused.inputId,
|
|
131
|
+
flow: refused.flow,
|
|
132
|
+
step: null,
|
|
133
|
+
prompt: refused.reason,
|
|
134
|
+
choices: [],
|
|
135
|
+
actions: [],
|
|
136
|
+
kind: "refused",
|
|
137
|
+
pendingId: refused.inputId,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for (const item of await registry.standing()) {
|
|
142
|
+
items.push({
|
|
143
|
+
runId: `${item.plugin}:${item.id}`,
|
|
144
|
+
flow: item.plugin,
|
|
145
|
+
step: null,
|
|
146
|
+
prompt: item.prompt,
|
|
147
|
+
choices: [],
|
|
148
|
+
actions: item.actions,
|
|
149
|
+
kind: "standing",
|
|
150
|
+
...(item.announce === false ? { announce: false as const } : {}),
|
|
151
|
+
...(item.covers === undefined ? {} : { covers: item.covers }),
|
|
152
|
+
...(item.object === undefined
|
|
153
|
+
? {}
|
|
154
|
+
: objectFields(`${item.object.type}:${item.object.id}`)),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (servesOlderCode(registry.home)) {
|
|
159
|
+
items.push({
|
|
160
|
+
runId: `operad:${OLDER_CODE_ITEM_ID}`,
|
|
161
|
+
flow: "operad",
|
|
162
|
+
step: null,
|
|
163
|
+
prompt: OLDER_CODE_PROMPT,
|
|
164
|
+
choices: [],
|
|
165
|
+
actions: [],
|
|
166
|
+
kind: "standing",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (views === undefined) return items;
|
|
171
|
+
const refs = items.flatMap((item) => {
|
|
172
|
+
const object = item.object === undefined ? undefined : parseObjectKey(item.object);
|
|
173
|
+
return object === undefined ? [] : [{ type: object.type, id: object.id }];
|
|
174
|
+
});
|
|
175
|
+
const rows = await views.rowsOf(refs);
|
|
176
|
+
return items.map((item) => {
|
|
177
|
+
const row = item.object === undefined ? undefined : rows.get(item.object);
|
|
178
|
+
return row === undefined ? item : { ...item, row };
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function actionsFor(
|
|
183
|
+
registry: PluginRegistry,
|
|
184
|
+
_log: LogBackend,
|
|
185
|
+
run: RunInfo,
|
|
186
|
+
): Promise<readonly PendingActionView[]> {
|
|
187
|
+
const object = parseObjectKey(run.object);
|
|
188
|
+
if (object === undefined) return [];
|
|
189
|
+
const entry = await registry.store.get(object.type, object.id);
|
|
190
|
+
if (entry === undefined) return [];
|
|
191
|
+
const pending = await registry.pending([
|
|
192
|
+
{
|
|
193
|
+
runId: run.runId,
|
|
194
|
+
object: { type: object.type, id: object.id },
|
|
195
|
+
},
|
|
196
|
+
]);
|
|
197
|
+
return pending[0]?.actions ?? [];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function objectFields(
|
|
201
|
+
key: string,
|
|
202
|
+
): { readonly object: string; readonly link: string } | Record<string, never> {
|
|
203
|
+
const object = parseObjectKey(key);
|
|
204
|
+
if (object === undefined) return {};
|
|
205
|
+
return {
|
|
206
|
+
object: `${object.type}:${object.id}`,
|
|
207
|
+
link: objectLink(object.type, object.id),
|
|
208
|
+
};
|
|
209
|
+
}
|