@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,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The object types a client may list: their display names, their plugin, their
|
|
3
|
+
* filters and where the client puts them. `GET /v1/types` and the MCP `types`
|
|
4
|
+
* tool serve this one listing.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { typeFilters, type TypeFilter } from "../plugin/filters.ts";
|
|
8
|
+
import type { PluginRegistry } from "../plugin/registry.ts";
|
|
9
|
+
|
|
10
|
+
/** One object type as a client sees it. */
|
|
11
|
+
export interface ViewTypeInfo {
|
|
12
|
+
readonly type: string;
|
|
13
|
+
readonly title: string;
|
|
14
|
+
/** The plugin the type belongs to; a client groups types by it. */
|
|
15
|
+
readonly plugin: string;
|
|
16
|
+
/**
|
|
17
|
+
* True when the plugin's objects belong under a client's settings view
|
|
18
|
+
* instead of a tab of their own. Present only when it holds.
|
|
19
|
+
*/
|
|
20
|
+
readonly settings?: true | undefined;
|
|
21
|
+
/** The type's filters in display order; absent when it declares none. */
|
|
22
|
+
readonly filters?: readonly TypeFilter[] | undefined;
|
|
23
|
+
/** The filter a list opens on; present with `filters`. */
|
|
24
|
+
readonly defaultFilter?: string | undefined;
|
|
25
|
+
/** The object types an action that creates this type works on; absent when none does. */
|
|
26
|
+
readonly parents?: readonly string[] | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The name a type shows in a client: `task` becomes `Task`. */
|
|
30
|
+
export function displayNameOf(type: string): string {
|
|
31
|
+
const words = type
|
|
32
|
+
.split(/[-_\s]+/)
|
|
33
|
+
.filter((word) => word.length > 0)
|
|
34
|
+
.map((word) => `${word[0]?.toUpperCase() ?? ""}${word.slice(1)}`);
|
|
35
|
+
return words.length === 0 ? type : words.join(" ");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Every object type a loaded plugin shows, once, in load order: the types of
|
|
40
|
+
* the plugins that have views, with the filters and the where-to-create list a
|
|
41
|
+
* client reads.
|
|
42
|
+
*/
|
|
43
|
+
export function listTypes(registry: PluginRegistry): readonly ViewTypeInfo[] {
|
|
44
|
+
const seen = new Set<string>();
|
|
45
|
+
const types: ViewTypeInfo[] = [];
|
|
46
|
+
for (const entry of registry.objectTypes()) {
|
|
47
|
+
if (seen.has(entry.type)) continue;
|
|
48
|
+
if (registry.viewsFor(entry.type) === undefined) continue;
|
|
49
|
+
seen.add(entry.type);
|
|
50
|
+
const filters = typeFilters(entry.definition);
|
|
51
|
+
// The types an action that creates this one works on: where a client offers a new one.
|
|
52
|
+
const parents = [
|
|
53
|
+
...new Set(
|
|
54
|
+
registry
|
|
55
|
+
.actions()
|
|
56
|
+
.filter((action) => action.creates === entry.type)
|
|
57
|
+
.map((action) => action.on),
|
|
58
|
+
),
|
|
59
|
+
];
|
|
60
|
+
types.push({
|
|
61
|
+
...(parents.length === 0 ? {} : { parents }),
|
|
62
|
+
type: entry.type,
|
|
63
|
+
title: displayNameOf(entry.type),
|
|
64
|
+
plugin: entry.plugin,
|
|
65
|
+
...(registry.objectsUnderSettings(entry.plugin) ? { settings: true } : {}),
|
|
66
|
+
...(filters === undefined
|
|
67
|
+
? {}
|
|
68
|
+
: { filters: filters.filters, defaultFilter: filters.default }),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return types;
|
|
72
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files a client sends for an action's argument. An upload is stored under the
|
|
3
|
+
* operad home until an action takes it; taking moves it into the target
|
|
4
|
+
* object's own attachment directory, so a reference works once. An upload no
|
|
5
|
+
* action took within {@link UPLOAD_TTL_MS} is removed.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { randomUUID } from "node:crypto";
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { mkdir, open, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
11
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
12
|
+
import { isRecord } from "../util.ts";
|
|
13
|
+
|
|
14
|
+
/** The largest file an upload takes, in bytes (20 MB). */
|
|
15
|
+
export const MAX_UPLOAD_BYTES = 20 * 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
/** How long an upload nobody used stays, in milliseconds. */
|
|
18
|
+
export const UPLOAD_TTL_MS = 60 * 60 * 1000;
|
|
19
|
+
|
|
20
|
+
/** The file name an action's `format` names: an argument that is a file. */
|
|
21
|
+
export const FILE_FORMAT = "file";
|
|
22
|
+
|
|
23
|
+
/** The upload is larger than {@link MAX_UPLOAD_BYTES}. */
|
|
24
|
+
export class UploadTooLargeError extends Error {
|
|
25
|
+
readonly limit: number;
|
|
26
|
+
constructor(limit: number) {
|
|
27
|
+
super(`the file is larger than the ${Math.floor(limit / (1024 * 1024))} MB limit`);
|
|
28
|
+
this.name = "UploadTooLargeError";
|
|
29
|
+
this.limit = limit;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A reference that names no upload: never made, already used, or expired. */
|
|
34
|
+
export class UploadMissingError extends Error {
|
|
35
|
+
constructor(ref: string) {
|
|
36
|
+
super(`the upload "${ref}" is not there: it was already used, it expired, or it was never made`);
|
|
37
|
+
this.name = "UploadMissingError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What an upload is, as the client is told. */
|
|
42
|
+
export interface UploadInfo {
|
|
43
|
+
/** What a client passes as the action's file argument. */
|
|
44
|
+
readonly ref: string;
|
|
45
|
+
readonly name: string;
|
|
46
|
+
readonly type: string;
|
|
47
|
+
readonly size: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface UploadMeta {
|
|
51
|
+
readonly name: string;
|
|
52
|
+
readonly type: string;
|
|
53
|
+
readonly size: number;
|
|
54
|
+
readonly at: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const REF = /^up-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
58
|
+
const DATA = "data";
|
|
59
|
+
const META = "meta.json";
|
|
60
|
+
/** Beside a stored file: what its upload said it was. A dot name cannot be an upload's own name. */
|
|
61
|
+
const STORED_META = ".meta.json";
|
|
62
|
+
const PART = ".part";
|
|
63
|
+
|
|
64
|
+
/** A file name that is safe as one path segment; the extension is kept. */
|
|
65
|
+
export function safeFileName(name: string): string {
|
|
66
|
+
const base = basename(name.replace(/\\/g, "/")).replace(/[^\p{L}\p{N}._ +-]/gu, "_").trim();
|
|
67
|
+
const cut = base.slice(-100).replace(/^\.+/, "");
|
|
68
|
+
return cut.length === 0 ? "file" : cut;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The directory an object's attachments are stored in. */
|
|
72
|
+
export function attachmentDir(home: string, type: string, id: string): string {
|
|
73
|
+
return join(home, "attachments", safeFileName(type), safeFileName(id));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The type the upload of a stored file gave, when the file was stored from an upload. */
|
|
77
|
+
export function storedType(path: string): string | undefined {
|
|
78
|
+
try {
|
|
79
|
+
const raw: unknown = JSON.parse(readFileSync(join(dirname(path), STORED_META), "utf8"));
|
|
80
|
+
return isRecord(raw) && typeof raw.type === "string" && raw.type.length > 0 ? raw.type : undefined;
|
|
81
|
+
} catch {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The path of a stored file as a client asks for it, `/v1/attachments/...`; undefined outside the attachment directories. */
|
|
87
|
+
export function attachmentUrl(home: string, path: string): string | undefined {
|
|
88
|
+
const rel = relative(join(home, "attachments"), path);
|
|
89
|
+
if (rel.length === 0 || rel.startsWith("..") || resolve(path) !== path) return undefined;
|
|
90
|
+
return `/v1/attachments/${rel.split(sep).map(encodeURIComponent).join("/")}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The stored file a `/v1/attachments/<rel>` request names, with its type; undefined when there is none. */
|
|
94
|
+
export async function findAttachment(
|
|
95
|
+
home: string,
|
|
96
|
+
rel: readonly string[],
|
|
97
|
+
): Promise<{ readonly path: string; readonly type: string; readonly name: string } | undefined> {
|
|
98
|
+
const root = join(home, "attachments");
|
|
99
|
+
if (rel.length < 4 || rel.some((part) => part === "" || part === "." || part === ".." || part.includes("/") || part.includes("\\"))) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const path = join(root, ...rel);
|
|
103
|
+
if (!path.startsWith(`${root}${sep}`) || basename(path) === STORED_META) return undefined;
|
|
104
|
+
try {
|
|
105
|
+
if (!(await stat(path)).isFile()) return undefined;
|
|
106
|
+
} catch {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
return { path, type: storedType(path) ?? "application/octet-stream", name: basename(path) };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface UploadsOptions {
|
|
113
|
+
readonly home: string;
|
|
114
|
+
readonly clock?: (() => number) | undefined;
|
|
115
|
+
readonly ttlMs?: number | undefined;
|
|
116
|
+
readonly limit?: number | undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The upload store of one operad home. */
|
|
120
|
+
export class Uploads {
|
|
121
|
+
private readonly root: string;
|
|
122
|
+
private readonly clock: () => number;
|
|
123
|
+
private readonly ttlMs: number;
|
|
124
|
+
readonly limit: number;
|
|
125
|
+
|
|
126
|
+
constructor(options: UploadsOptions) {
|
|
127
|
+
this.root = join(options.home, "uploads");
|
|
128
|
+
this.clock = options.clock ?? Date.now;
|
|
129
|
+
this.ttlMs = options.ttlMs ?? UPLOAD_TTL_MS;
|
|
130
|
+
this.limit = options.limit ?? MAX_UPLOAD_BYTES;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Stores a stream as a new upload. Past the limit the stream is dropped and
|
|
135
|
+
* nothing stays; the caller sees {@link UploadTooLargeError}.
|
|
136
|
+
*/
|
|
137
|
+
async save(
|
|
138
|
+
body: ReadableStream<Uint8Array> | null,
|
|
139
|
+
name: string,
|
|
140
|
+
type: string,
|
|
141
|
+
): Promise<UploadInfo> {
|
|
142
|
+
const ref = `up-${randomUUID()}`;
|
|
143
|
+
const part = join(this.root, `${ref}${PART}`);
|
|
144
|
+
await mkdir(part, { recursive: true });
|
|
145
|
+
const shown = safeFileName(name);
|
|
146
|
+
const mediaType = type.split(";")[0]?.trim().toLowerCase() || "application/octet-stream";
|
|
147
|
+
try {
|
|
148
|
+
// The start time goes first, so a sweep can tell how old an unfinished upload is.
|
|
149
|
+
await writeFile(join(part, META), JSON.stringify({ name: shown, type: mediaType, size: 0, at: this.clock() }));
|
|
150
|
+
let size = 0;
|
|
151
|
+
const file = await open(join(part, DATA), "w");
|
|
152
|
+
try {
|
|
153
|
+
if (body !== null) {
|
|
154
|
+
const reader = body.getReader();
|
|
155
|
+
for (;;) {
|
|
156
|
+
const chunk = await reader.read();
|
|
157
|
+
if (chunk.done) break;
|
|
158
|
+
size += chunk.value.byteLength;
|
|
159
|
+
if (size > this.limit) {
|
|
160
|
+
await reader.cancel().catch(() => undefined);
|
|
161
|
+
throw new UploadTooLargeError(this.limit);
|
|
162
|
+
}
|
|
163
|
+
await file.write(chunk.value);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
} finally {
|
|
167
|
+
await file.close();
|
|
168
|
+
}
|
|
169
|
+
const meta: UploadMeta = { name: shown, type: mediaType, size, at: this.clock() };
|
|
170
|
+
await writeFile(join(part, META), JSON.stringify(meta));
|
|
171
|
+
await rename(part, join(this.root, ref));
|
|
172
|
+
return { ref, name: shown, type: mediaType, size };
|
|
173
|
+
} catch (error) {
|
|
174
|
+
await rm(part, { recursive: true, force: true });
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Moves the upload into `dir` and returns where its file is now. The move is
|
|
181
|
+
* one rename, so two actions asking for the same reference cannot both get it.
|
|
182
|
+
*/
|
|
183
|
+
async take(ref: string, dir: string): Promise<{ readonly path: string } & UploadInfo> {
|
|
184
|
+
if (!REF.test(ref)) throw new UploadMissingError(ref);
|
|
185
|
+
await mkdir(dir, { recursive: true });
|
|
186
|
+
const target = join(dir, ref);
|
|
187
|
+
try {
|
|
188
|
+
await rename(join(this.root, ref), target);
|
|
189
|
+
} catch {
|
|
190
|
+
throw new UploadMissingError(ref);
|
|
191
|
+
}
|
|
192
|
+
const raw: unknown = JSON.parse(await readFile(join(target, META), "utf8"));
|
|
193
|
+
const name = isRecord(raw) && typeof raw.name === "string" ? safeFileName(raw.name) : "file";
|
|
194
|
+
const type = isRecord(raw) && typeof raw.type === "string" ? raw.type : "application/octet-stream";
|
|
195
|
+
await rename(join(target, DATA), join(target, name));
|
|
196
|
+
const size = (await stat(join(target, name))).size;
|
|
197
|
+
// The type the client gave stays beside the file, for whoever shows or reads it later.
|
|
198
|
+
await rename(join(target, META), join(target, STORED_META));
|
|
199
|
+
await writeFile(join(target, STORED_META), JSON.stringify({ name, type, size }));
|
|
200
|
+
return { ref, name, type, size, path: join(target, name) };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Removes the uploads older than the time to keep one; returns how many went. */
|
|
204
|
+
async sweep(): Promise<number> {
|
|
205
|
+
let names: string[];
|
|
206
|
+
try {
|
|
207
|
+
names = await readdir(this.root);
|
|
208
|
+
} catch {
|
|
209
|
+
return 0;
|
|
210
|
+
}
|
|
211
|
+
let removed = 0;
|
|
212
|
+
for (const entry of names) {
|
|
213
|
+
const dir = join(this.root, entry);
|
|
214
|
+
let at: number;
|
|
215
|
+
try {
|
|
216
|
+
const raw: unknown = JSON.parse(await readFile(join(dir, META), "utf8"));
|
|
217
|
+
if (!isRecord(raw) || typeof raw.at !== "number") continue;
|
|
218
|
+
at = raw.at;
|
|
219
|
+
} catch {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (this.clock() - at < this.ttlMs) continue;
|
|
223
|
+
await rm(dir, { recursive: true, force: true });
|
|
224
|
+
removed += 1;
|
|
225
|
+
}
|
|
226
|
+
return removed;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://operad.io/schemas/triggers.schema.json",
|
|
4
|
+
"title": "operad trigger rules",
|
|
5
|
+
"description": "The rules in $OPERAD_HOME/triggers.json: which event starts which flow, and how far.",
|
|
6
|
+
"type": "array",
|
|
7
|
+
"items": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"source": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"minLength": 1,
|
|
13
|
+
"description": "The source that sends the event, such as cron or operad."
|
|
14
|
+
},
|
|
15
|
+
"type": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"minLength": 1,
|
|
18
|
+
"description": "The event type, such as tick or run.done."
|
|
19
|
+
},
|
|
20
|
+
"start": {
|
|
21
|
+
"type": "object",
|
|
22
|
+
"description": "The flow and fixed parameters to start.",
|
|
23
|
+
"properties": {
|
|
24
|
+
"flow": { "type": "string", "minLength": 1 },
|
|
25
|
+
"params": {}
|
|
26
|
+
},
|
|
27
|
+
"required": ["flow"],
|
|
28
|
+
"additionalProperties": false
|
|
29
|
+
},
|
|
30
|
+
"object": {
|
|
31
|
+
"type": "string",
|
|
32
|
+
"minLength": 1,
|
|
33
|
+
"description": "A fixed object, or $event.object."
|
|
34
|
+
},
|
|
35
|
+
"mode": {
|
|
36
|
+
"type": "string",
|
|
37
|
+
"enum": ["observe", "candidate", "auto"]
|
|
38
|
+
},
|
|
39
|
+
"schedule": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"minLength": 1,
|
|
42
|
+
"description": "A five-field cron expression in the local time zone."
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"required": ["source", "type", "start", "object", "mode"],
|
|
46
|
+
"additionalProperties": false
|
|
47
|
+
}
|
|
48
|
+
}
|
package/README.md
DELETED