@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,318 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertWhere,
|
|
3
|
+
CursorError,
|
|
4
|
+
decodeCursor,
|
|
5
|
+
encodeCursor,
|
|
6
|
+
matchesWhere,
|
|
7
|
+
pageLimit,
|
|
8
|
+
whereFingerprint,
|
|
9
|
+
} from "./paging.ts";
|
|
10
|
+
import type {
|
|
11
|
+
StoreFactInput,
|
|
12
|
+
FactsOptions,
|
|
13
|
+
FactsPage,
|
|
14
|
+
ListOptions,
|
|
15
|
+
ListPage,
|
|
16
|
+
ObjectData,
|
|
17
|
+
ObjectRef,
|
|
18
|
+
PluginFact,
|
|
19
|
+
PluginStore,
|
|
20
|
+
StoredObject,
|
|
21
|
+
} from "./types.ts";
|
|
22
|
+
import { requireName } from "../util.ts";
|
|
23
|
+
import { diffObject, OBJECT_CHANGE_FACT, type ObjectChange } from "./object-facts.ts";
|
|
24
|
+
|
|
25
|
+
/** Options for the in-process plugin store: nothing it keeps survives the process. */
|
|
26
|
+
export interface MemoryPluginStoreOptions {
|
|
27
|
+
readonly root: string;
|
|
28
|
+
/** The clock facts use. Tests inject one. */
|
|
29
|
+
readonly clock?: (() => number) | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface FactSubscription {
|
|
33
|
+
readonly key: string;
|
|
34
|
+
readonly after: number;
|
|
35
|
+
readonly listener: (fact: PluginFact) => void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The in-process plugin store, used when no store location is given (tests and
|
|
40
|
+
* one-shot use). It is separate from the run log's storage backends. Each
|
|
41
|
+
* object change and each appended fact is kept in order; `facts()` is the
|
|
42
|
+
* history the kernel's views read.
|
|
43
|
+
*/
|
|
44
|
+
export class MemoryPluginStore implements PluginStore {
|
|
45
|
+
readonly kind = "memory" as const;
|
|
46
|
+
readonly root: string;
|
|
47
|
+
private readonly clock: () => number;
|
|
48
|
+
private readonly objects = new Map<string, Map<string, StoredObject>>();
|
|
49
|
+
private readonly indexes = new Map<string, readonly string[]>();
|
|
50
|
+
private readonly entries: PluginFact[] = [];
|
|
51
|
+
private readonly byObject = new Map<string, PluginFact[]>();
|
|
52
|
+
/** The object types that have facts, by object id. */
|
|
53
|
+
private readonly typesById = new Map<string, Set<string>>();
|
|
54
|
+
private readonly listeners = new Set<(object: ObjectRef) => void>();
|
|
55
|
+
private readonly factListeners = new Set<FactSubscription>();
|
|
56
|
+
|
|
57
|
+
constructor(options: MemoryPluginStoreOptions) {
|
|
58
|
+
this.root = options.root;
|
|
59
|
+
this.clock = options.clock ?? (() => Date.now());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
declare(type: string, index: readonly string[]): void {
|
|
63
|
+
this.indexes.set(type, [...index]);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
put(
|
|
67
|
+
type: string,
|
|
68
|
+
id: string,
|
|
69
|
+
data: ObjectData,
|
|
70
|
+
cause: string,
|
|
71
|
+
): StoredObject {
|
|
72
|
+
requireName(type, "object type");
|
|
73
|
+
requireName(id, "object id");
|
|
74
|
+
requireName(cause, "cause");
|
|
75
|
+
|
|
76
|
+
const previous = this.get(type, id);
|
|
77
|
+
let change: ObjectChange;
|
|
78
|
+
if (previous === undefined) {
|
|
79
|
+
change = { changed: { ...data }, removed: [] };
|
|
80
|
+
} else {
|
|
81
|
+
// An unchanged put still records its cause: dev's attempts table reads
|
|
82
|
+
// the cause of a step's put even when the step changed no field.
|
|
83
|
+
change = diffObject(previous.data, data) ?? { changed: {}, removed: [] };
|
|
84
|
+
}
|
|
85
|
+
const object: StoredObject = {
|
|
86
|
+
type,
|
|
87
|
+
id,
|
|
88
|
+
data: { ...data },
|
|
89
|
+
version: (previous?.version ?? 0) + 1,
|
|
90
|
+
cause,
|
|
91
|
+
at: this.clock(),
|
|
92
|
+
};
|
|
93
|
+
let byId = this.objects.get(type);
|
|
94
|
+
if (byId === undefined) {
|
|
95
|
+
byId = new Map();
|
|
96
|
+
this.objects.set(type, byId);
|
|
97
|
+
}
|
|
98
|
+
byId.set(id, object);
|
|
99
|
+
this.append({ type: OBJECT_CHANGE_FACT, object: { type, id }, data: change, cause });
|
|
100
|
+
return object;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get(type: string, id: string): StoredObject | undefined {
|
|
104
|
+
return this.objects.get(type)?.get(id);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
list(type: string, options: ListOptions = {}): ListPage {
|
|
108
|
+
this.assertWhere(type, options);
|
|
109
|
+
const fingerprint = whereFingerprint(options.where ?? {});
|
|
110
|
+
let after: string | undefined;
|
|
111
|
+
if (options.cursor !== undefined) {
|
|
112
|
+
const cursor = decodeCursor(options.cursor, `list "${type}"`);
|
|
113
|
+
if (cursor.kind !== "list") {
|
|
114
|
+
throw new CursorError(`list "${type}": invalid cursor`);
|
|
115
|
+
}
|
|
116
|
+
if (cursor.type !== type) {
|
|
117
|
+
throw new CursorError(
|
|
118
|
+
`list "${type}": the cursor belongs to object type "${String(cursor.type)}"`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (cursor.where !== fingerprint) {
|
|
122
|
+
throw new CursorError(
|
|
123
|
+
`list "${type}": the cursor was made for a different filter`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (typeof cursor.after !== "string") {
|
|
127
|
+
throw new CursorError(`list "${type}": invalid cursor`);
|
|
128
|
+
}
|
|
129
|
+
after = cursor.after;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const byId = this.objects.get(type);
|
|
133
|
+
const ids = byId === undefined ? [] : [...byId.keys()].sort();
|
|
134
|
+
const start =
|
|
135
|
+
after === undefined ? 0 : ids.findIndex((id) => id > after);
|
|
136
|
+
|
|
137
|
+
const objects: StoredObject[] = [];
|
|
138
|
+
const limit = pageLimit(options.limit);
|
|
139
|
+
for (const id of start < 0 ? [] : ids.slice(start)) {
|
|
140
|
+
const object = byId?.get(id);
|
|
141
|
+
if (object === undefined) continue;
|
|
142
|
+
if (!matchesWhere(object.data, options.where)) continue;
|
|
143
|
+
objects.push(object);
|
|
144
|
+
if (objects.length > limit) break;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const more = objects.length > limit;
|
|
148
|
+
if (more) objects.length = limit;
|
|
149
|
+
const last = objects.at(-1);
|
|
150
|
+
return {
|
|
151
|
+
objects,
|
|
152
|
+
...(more && last !== undefined
|
|
153
|
+
? {
|
|
154
|
+
next: encodeCursor({
|
|
155
|
+
kind: "list",
|
|
156
|
+
type,
|
|
157
|
+
after: last.id,
|
|
158
|
+
where: fingerprint,
|
|
159
|
+
}),
|
|
160
|
+
}
|
|
161
|
+
: {}),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
objectTypesOf(id: string): readonly string[] {
|
|
166
|
+
return [...(this.typesById.get(id) ?? [])].sort();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
facts(): readonly PluginFact[];
|
|
170
|
+
facts(object: ObjectRef, options?: FactsOptions): FactsPage;
|
|
171
|
+
facts(
|
|
172
|
+
object?: ObjectRef,
|
|
173
|
+
options: FactsOptions = {},
|
|
174
|
+
): readonly PluginFact[] | FactsPage {
|
|
175
|
+
if (object === undefined) return this.entries;
|
|
176
|
+
|
|
177
|
+
const key = this.key(object.type, object.id);
|
|
178
|
+
const after = this.factCursor(object, options.cursor);
|
|
179
|
+
const all = this.byObject.get(key) ?? [];
|
|
180
|
+
const later = all.filter((fact) => fact.sequence > after);
|
|
181
|
+
const limit = pageLimit(options.limit);
|
|
182
|
+
const page = later.slice(0, limit);
|
|
183
|
+
const more = later.length > limit;
|
|
184
|
+
const last = page.at(-1);
|
|
185
|
+
return {
|
|
186
|
+
facts: page,
|
|
187
|
+
...(more && last !== undefined
|
|
188
|
+
? {
|
|
189
|
+
next: encodeCursor({
|
|
190
|
+
kind: "facts",
|
|
191
|
+
type: object.type,
|
|
192
|
+
id: object.id,
|
|
193
|
+
after: last.sequence,
|
|
194
|
+
}),
|
|
195
|
+
}
|
|
196
|
+
: {}),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
subscribe(listener: (object: ObjectRef) => void): () => void;
|
|
201
|
+
subscribe(
|
|
202
|
+
object: ObjectRef,
|
|
203
|
+
options: FactsOptions,
|
|
204
|
+
listener: (fact: PluginFact) => void,
|
|
205
|
+
): () => void;
|
|
206
|
+
subscribe(
|
|
207
|
+
objectOrListener: ObjectRef | ((object: ObjectRef) => void),
|
|
208
|
+
options: FactsOptions = {},
|
|
209
|
+
factListener?: (fact: PluginFact) => void,
|
|
210
|
+
): () => void {
|
|
211
|
+
if (typeof objectOrListener === "function") {
|
|
212
|
+
this.listeners.add(objectOrListener);
|
|
213
|
+
return () => {
|
|
214
|
+
this.listeners.delete(objectOrListener);
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (factListener === undefined) {
|
|
218
|
+
throw new Error("subscribe: a fact listener is required");
|
|
219
|
+
}
|
|
220
|
+
return this.subscribeFacts(objectOrListener, options, factListener);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
subscribeFacts(
|
|
224
|
+
object: ObjectRef,
|
|
225
|
+
options: FactsOptions,
|
|
226
|
+
listener: (fact: PluginFact) => void,
|
|
227
|
+
): () => void {
|
|
228
|
+
const key = this.key(object.type, object.id);
|
|
229
|
+
let after = this.factCursor(object, options.cursor);
|
|
230
|
+
if (options.cursor === undefined) {
|
|
231
|
+
const all = this.byObject.get(key) ?? [];
|
|
232
|
+
after = all.at(-1)?.sequence ?? 0;
|
|
233
|
+
}
|
|
234
|
+
// Facts that arrived after the cursor but before the subscription are not
|
|
235
|
+
// lost; deliver them now, then continue live.
|
|
236
|
+
for (const fact of this.byObject.get(key) ?? []) {
|
|
237
|
+
if (fact.sequence > after) listener(fact);
|
|
238
|
+
}
|
|
239
|
+
const subscription: FactSubscription = { key, after, listener };
|
|
240
|
+
this.factListeners.add(subscription);
|
|
241
|
+
return () => {
|
|
242
|
+
this.factListeners.delete(subscription);
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
append(input: StoreFactInput): PluginFact {
|
|
247
|
+
requireName(input.type, "fact type");
|
|
248
|
+
const cause = input.cause;
|
|
249
|
+
requireName(cause, "cause");
|
|
250
|
+
const fact: PluginFact = {
|
|
251
|
+
sequence: this.entries.length + 1,
|
|
252
|
+
type: input.type,
|
|
253
|
+
object: input.object,
|
|
254
|
+
cause,
|
|
255
|
+
at: this.clock(),
|
|
256
|
+
data: { ...input.data },
|
|
257
|
+
};
|
|
258
|
+
this.entries.push(fact);
|
|
259
|
+
this.track(fact);
|
|
260
|
+
if (fact.object !== undefined) {
|
|
261
|
+
this.notify(fact.object);
|
|
262
|
+
}
|
|
263
|
+
return fact;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private assertWhere(type: string, options: ListOptions): void {
|
|
267
|
+
if (options.where === undefined) return;
|
|
268
|
+
const index = options.index ?? this.indexes.get(type);
|
|
269
|
+
if (index === undefined) {
|
|
270
|
+
// A type no plugin declared: everything it names is accepted.
|
|
271
|
+
assertWhere(Object.keys(options.where), options.where, `list "${type}"`);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
assertWhere(index, options.where, `list "${type}"`);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private factCursor(object: ObjectRef, cursor: string | undefined): number {
|
|
278
|
+
if (cursor === undefined) return 0;
|
|
279
|
+
const label = `facts "${object.type}:${object.id}"`;
|
|
280
|
+
const value = decodeCursor(cursor, label);
|
|
281
|
+
if (value.kind !== "facts") {
|
|
282
|
+
throw new CursorError(`${label}: invalid cursor`);
|
|
283
|
+
}
|
|
284
|
+
if (value.type !== object.type || value.id !== object.id) {
|
|
285
|
+
throw new CursorError(
|
|
286
|
+
`${label}: the cursor belongs to "${String(value.type)}:${String(value.id)}"`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
if (typeof value.after !== "number" || !Number.isInteger(value.after)) {
|
|
290
|
+
throw new CursorError(`${label}: invalid cursor`);
|
|
291
|
+
}
|
|
292
|
+
return value.after;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private track(fact: PluginFact): void {
|
|
296
|
+
if (fact.object === undefined) return;
|
|
297
|
+
const key = this.key(fact.object.type, fact.object.id);
|
|
298
|
+
const types = this.typesById.get(fact.object.id);
|
|
299
|
+
if (types === undefined) this.typesById.set(fact.object.id, new Set([fact.object.type]));
|
|
300
|
+
else types.add(fact.object.type);
|
|
301
|
+
const list = this.byObject.get(key);
|
|
302
|
+
if (list === undefined) this.byObject.set(key, [fact]);
|
|
303
|
+
else list.push(fact);
|
|
304
|
+
for (const subscription of this.factListeners) {
|
|
305
|
+
if (subscription.key !== key) continue;
|
|
306
|
+
if (fact.sequence <= subscription.after) continue;
|
|
307
|
+
subscription.listener(fact);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private key(type: string, id: string): string {
|
|
312
|
+
return `${type}\u0000${id}`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private notify(object: ObjectRef): void {
|
|
316
|
+
for (const listener of this.listeners) listener(object);
|
|
317
|
+
}
|
|
318
|
+
}
|