@sanoma-ai/workflows 0.0.0-stage → 0.2.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.
- package/LICENSE +105 -0
- package/README.md +544 -2
- package/dist/apply-steps.d.ts +6 -0
- package/dist/apply-steps.js +93 -0
- package/dist/apply-steps.js.map +1 -0
- package/dist/apply.d.ts +197 -0
- package/dist/apply.js +470 -0
- package/dist/apply.js.map +1 -0
- package/dist/approvals.d.ts +46 -0
- package/dist/approvals.js +111 -0
- package/dist/approvals.js.map +1 -0
- package/dist/ast.d.ts +57 -0
- package/dist/ast.js +81 -0
- package/dist/ast.js.map +1 -0
- package/dist/call.d.ts +37 -0
- package/dist/call.js +417 -0
- package/dist/call.js.map +1 -0
- package/dist/client.d.ts +203 -0
- package/dist/client.js +457 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +126 -0
- package/dist/config.js +264 -0
- package/dist/config.js.map +1 -0
- package/dist/credentials.d.ts +55 -0
- package/dist/credentials.js +261 -0
- package/dist/credentials.js.map +1 -0
- package/dist/cucumber.d.ts +64 -0
- package/dist/cucumber.js +158 -0
- package/dist/cucumber.js.map +1 -0
- package/dist/datafile.d.ts +71 -0
- package/dist/datafile.js +380 -0
- package/dist/datafile.js.map +1 -0
- package/dist/db.d.ts +58 -0
- package/dist/db.js +179 -0
- package/dist/db.js.map +1 -0
- package/dist/define.d.ts +205 -0
- package/dist/define.js +103 -0
- package/dist/define.js.map +1 -0
- package/dist/describe.d.ts +141 -0
- package/dist/describe.js +135 -0
- package/dist/describe.js.map +1 -0
- package/dist/drift.d.ts +127 -0
- package/dist/drift.js +167 -0
- package/dist/drift.js.map +1 -0
- package/dist/env.d.ts +49 -0
- package/dist/env.js +83 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.d.ts +88 -0
- package/dist/errors.js +148 -0
- package/dist/errors.js.map +1 -0
- package/dist/expectations.d.ts +26 -0
- package/dist/expectations.js +162 -0
- package/dist/expectations.js.map +1 -0
- package/dist/fake.d.ts +55 -0
- package/dist/fake.js +87 -0
- package/dist/fake.js.map +1 -0
- package/dist/fill.d.ts +9 -0
- package/dist/fill.js +17 -0
- package/dist/fill.js.map +1 -0
- package/dist/host.d.ts +56 -0
- package/dist/host.js +45 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/ledger.d.ts +217 -0
- package/dist/ledger.js +251 -0
- package/dist/ledger.js.map +1 -0
- package/dist/lint.d.ts +32 -0
- package/dist/lint.js +168 -0
- package/dist/lint.js.map +1 -0
- package/dist/lockfile.d.ts +36 -0
- package/dist/lockfile.js +69 -0
- package/dist/lockfile.js.map +1 -0
- package/dist/log.d.ts +24 -0
- package/dist/log.js +83 -0
- package/dist/log.js.map +1 -0
- package/dist/op.d.ts +260 -0
- package/dist/op.js +188 -0
- package/dist/op.js.map +1 -0
- package/dist/outline.d.ts +179 -0
- package/dist/outline.js +506 -0
- package/dist/outline.js.map +1 -0
- package/dist/paths.d.ts +11 -0
- package/dist/paths.js +30 -0
- package/dist/paths.js.map +1 -0
- package/dist/plugin.d.ts +28 -0
- package/dist/plugin.js +25 -0
- package/dist/plugin.js.map +1 -0
- package/dist/policy.d.ts +114 -0
- package/dist/policy.js +60 -0
- package/dist/policy.js.map +1 -0
- package/dist/resource.d.ts +378 -0
- package/dist/resource.js +430 -0
- package/dist/resource.js.map +1 -0
- package/dist/resources.d.ts +77 -0
- package/dist/resources.js +244 -0
- package/dist/resources.js.map +1 -0
- package/dist/run.d.ts +101 -0
- package/dist/run.js +7 -0
- package/dist/run.js.map +1 -0
- package/dist/sandbox.d.ts +33 -0
- package/dist/sandbox.js +127 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/scenario.d.ts +221 -0
- package/dist/scenario.js +1104 -0
- package/dist/scenario.js.map +1 -0
- package/dist/scenarios.d.ts +55 -0
- package/dist/scenarios.js +71 -0
- package/dist/scenarios.js.map +1 -0
- package/dist/secret.d.ts +42 -0
- package/dist/secret.js +58 -0
- package/dist/secret.js.map +1 -0
- package/dist/shared.d.ts +69 -0
- package/dist/shared.js +84 -0
- package/dist/shared.js.map +1 -0
- package/dist/source.d.ts +58 -0
- package/dist/source.js +54 -0
- package/dist/source.js.map +1 -0
- package/dist/steps.d.ts +76 -0
- package/dist/steps.js +23 -0
- package/dist/steps.js.map +1 -0
- package/dist/version.d.ts +29 -0
- package/dist/version.js +76 -0
- package/dist/version.js.map +1 -0
- package/dist/worker.d.ts +37 -0
- package/dist/worker.js +228 -0
- package/dist/worker.js.map +1 -0
- package/dist/writer.d.ts +35 -0
- package/dist/writer.js +201 -0
- package/dist/writer.js.map +1 -0
- package/oxlint.json +128 -0
- package/package.json +96 -3
- package/src/apply-steps.ts +110 -0
- package/src/apply.ts +626 -0
- package/src/approvals.ts +157 -0
- package/src/ast.ts +115 -0
- package/src/call.ts +483 -0
- package/src/client.ts +604 -0
- package/src/config.ts +404 -0
- package/src/credentials.ts +296 -0
- package/src/cucumber.ts +230 -0
- package/src/datafile.ts +481 -0
- package/src/db.ts +197 -0
- package/src/define.ts +285 -0
- package/src/describe.ts +282 -0
- package/src/drift.ts +259 -0
- package/src/env.ts +122 -0
- package/src/errors.ts +202 -0
- package/src/expectations.ts +191 -0
- package/src/fake.ts +156 -0
- package/src/fill.ts +24 -0
- package/src/host.ts +96 -0
- package/src/index.ts +143 -0
- package/src/ledger.ts +419 -0
- package/src/lint.ts +208 -0
- package/src/lockfile.ts +95 -0
- package/src/log.ts +82 -0
- package/src/op.ts +402 -0
- package/src/outline.ts +620 -0
- package/src/paths.ts +32 -0
- package/src/plugin.ts +37 -0
- package/src/policy.ts +139 -0
- package/src/resource.ts +670 -0
- package/src/resources.ts +311 -0
- package/src/run.ts +106 -0
- package/src/sandbox.ts +161 -0
- package/src/scenario.ts +1365 -0
- package/src/scenarios.ts +111 -0
- package/src/secret.ts +92 -0
- package/src/shared.ts +126 -0
- package/src/source.ts +82 -0
- package/src/steps.ts +90 -0
- package/src/version.ts +86 -0
- package/src/worker.ts +264 -0
- package/src/writer.ts +225 -0
package/src/lockfile.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { existsSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import type { ResourceProblem } from "./resources.ts";
|
|
5
|
+
|
|
6
|
+
// The lockfile: which vendor object each declaration means, kept apart from the data files (which
|
|
7
|
+
// hold intent only), as `package-lock.json` is from `package.json`. Only apply writes it.
|
|
8
|
+
|
|
9
|
+
/** Where the lockfile is, relative to the config's root. */
|
|
10
|
+
export const LOCKFILE = "resources/sanoma.lock.json";
|
|
11
|
+
|
|
12
|
+
/** Which vendor object a declaration means: its `<vendor>.<type>` and the vendor's stable id for it. */
|
|
13
|
+
export interface Binding {
|
|
14
|
+
type: string;
|
|
15
|
+
id: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The lockfile's content: by declaration key, `<file>#<export>` (the file relative to the config's root, with `/`). */
|
|
19
|
+
export type Bindings = Record<string, Binding>;
|
|
20
|
+
|
|
21
|
+
const lockfile = z.record(
|
|
22
|
+
z.string().regex(/^resources\/[^#]+#[^#]+$/),
|
|
23
|
+
z.strictObject({ type: z.string().regex(/^[^.\s]+\.[^.\s]+$/), id: z.string().min(1) }),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The lockfile under `root`: no bindings when there is none, and no bindings with a problem when
|
|
28
|
+
* it is not JSON or not a map of `<file>#<export>` to `{ type, id }`.
|
|
29
|
+
*/
|
|
30
|
+
export function readLockfile(root: string): { bindings: Bindings; problems: ResourceProblem[] } {
|
|
31
|
+
const path = join(root, LOCKFILE);
|
|
32
|
+
if (!existsSync(path)) return { bindings: {}, problems: [] };
|
|
33
|
+
let json: unknown;
|
|
34
|
+
try {
|
|
35
|
+
json = JSON.parse(readFileSync(path, "utf8"));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
return { bindings: {}, problems: [{ file: LOCKFILE, message: `not JSON: ${(e as Error).message}` }] };
|
|
38
|
+
}
|
|
39
|
+
const parsed = lockfile.safeParse(json);
|
|
40
|
+
if (!parsed.success) {
|
|
41
|
+
// One line: zod's own spans lines.
|
|
42
|
+
return {
|
|
43
|
+
bindings: {},
|
|
44
|
+
problems: [{ file: LOCKFILE, message: z.prettifyError(parsed.error).replaceAll(/\s*\n\s*/g, " ") }],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return { bindings: parsed.data, problems: [] };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The lockfile's text for `bindings`: keys sorted, two-space JSON, a trailing newline, so a diff shows only what changed. */
|
|
51
|
+
export function serializeLockfile(bindings: Bindings): string {
|
|
52
|
+
const sorted = Object.entries(bindings)
|
|
53
|
+
.toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
54
|
+
.map(([key, { type, id }]) => [key, { type, id }]);
|
|
55
|
+
return `${JSON.stringify(Object.fromEntries(sorted), null, 2)}\n`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Writes the lockfile under `root` whole or not at all: to a file beside it, then renamed over it. Apply only. */
|
|
59
|
+
export function writeLockfile(root: string, bindings: Bindings): void {
|
|
60
|
+
const path = join(root, LOCKFILE);
|
|
61
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
62
|
+
try {
|
|
63
|
+
writeFileSync(temporary, serializeLockfile(bindings));
|
|
64
|
+
renameSync(temporary, path);
|
|
65
|
+
} catch (err) {
|
|
66
|
+
rmSync(temporary, { force: true });
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* One change to the lockfile: a declaration bound for the first time (`bind`, `to`), pointed at
|
|
73
|
+
* another object (`rebind`, `from` and `to`), or no longer bound (`unbind`, `from`). `type` is
|
|
74
|
+
* the binding's `<vendor>.<type>`, the new one's for a rebind.
|
|
75
|
+
*/
|
|
76
|
+
export interface Rebind {
|
|
77
|
+
key: string;
|
|
78
|
+
type: string;
|
|
79
|
+
kind: "bind" | "rebind" | "unbind";
|
|
80
|
+
from?: string;
|
|
81
|
+
to?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** What changed from `before` to `after`, by key, sorted. */
|
|
85
|
+
export function diffLockfiles(before: Bindings, after: Bindings): Rebind[] {
|
|
86
|
+
const keys = [...new Set([...Object.keys(before), ...Object.keys(after)])].toSorted();
|
|
87
|
+
return keys.flatMap((key): Rebind[] => {
|
|
88
|
+
const was = before[key];
|
|
89
|
+
const now = after[key];
|
|
90
|
+
if (!now) return was ? [{ key, type: was.type, kind: "unbind", from: was.id }] : [];
|
|
91
|
+
if (!was) return [{ key, type: now.type, kind: "bind", to: now.id }];
|
|
92
|
+
if (was.id === now.id && was.type === now.type) return [];
|
|
93
|
+
return [{ key, type: now.type, kind: "rebind", from: was.id, to: now.id }];
|
|
94
|
+
});
|
|
95
|
+
}
|
package/src/log.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { DLogger } from "@dbos-inc/dbos-sdk";
|
|
3
|
+
|
|
4
|
+
// DBOS's levels (winston's), most severe first. A message shows when its level is at or above the
|
|
5
|
+
// last `startWorker`'s `logLevel`, as DBOS's own do: `warn` by default. One threshold for the
|
|
6
|
+
// process, as DBOS's own logger is one per process.
|
|
7
|
+
const LEVELS = ["error", "warn", "info", "http", "verbose", "debug", "silly"];
|
|
8
|
+
const levelsUpTo = (level: string) => new Set(LEVELS.slice(0, LEVELS.indexOf(level) + 1));
|
|
9
|
+
let visible = levelsUpTo("warn");
|
|
10
|
+
|
|
11
|
+
/** Shows the messages at `level` and above from now on; an unknown level as `warn`. */
|
|
12
|
+
export function setLogLevel(level = "warn") {
|
|
13
|
+
visible = levelsUpTo(LEVELS.includes(level) ? level : "warn");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const shows = (level: string) => visible.has(level);
|
|
17
|
+
|
|
18
|
+
/** Tells the operator what the runtime did, such as loading credentials (never their values); shown at `info`. */
|
|
19
|
+
export const info = (message: string) => {
|
|
20
|
+
if (shows("info")) console.info(`sanoma: ${message}`);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Tells the operator something went wrong or was skipped, without failing anything; shown at `warn`. */
|
|
24
|
+
export const warn = (message: string) => {
|
|
25
|
+
if (shows("warn")) console.warn(`sanoma: ${message}`);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Set while a sandbox run's body runs. */
|
|
29
|
+
const sandboxRun = new AsyncLocalStorage<true>();
|
|
30
|
+
/** What sandbox runs failed with: each a copy made for its run (`quietIfSandbox`). */
|
|
31
|
+
const sandboxErrors = new WeakSet<object>();
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Runs a workflow's body, as a sandbox run's when `sandbox`: `dbosLogger` then shows none of DBOS's
|
|
35
|
+
* warnings from inside it (its steps' failed tries among them), nor the error it fails with. A
|
|
36
|
+
* scenario's fakes fail on purpose; the run's ledger and its status say what happened. The run
|
|
37
|
+
* fails with a copy of its error (same prototype and properties), made for it, so the same error
|
|
38
|
+
* object thrown by a live run is still shown.
|
|
39
|
+
*/
|
|
40
|
+
export function quietIfSandbox<T>(sandbox: boolean, body: () => Promise<T>): Promise<T> {
|
|
41
|
+
if (!sandbox) return body();
|
|
42
|
+
return sandboxRun.run(true, body).catch((err: unknown) => {
|
|
43
|
+
if (typeof err !== "object" || err === null) throw err;
|
|
44
|
+
const copy = Object.create(Object.getPrototypeOf(err), Object.getOwnPropertyDescriptors(err)) as object;
|
|
45
|
+
// V8's `stack` accessor reads the original's internal slot, which the copy lacks.
|
|
46
|
+
if ("stack" in err) Object.defineProperty(copy, "stack", { value: err.stack, writable: true, configurable: true });
|
|
47
|
+
sandboxErrors.add(copy);
|
|
48
|
+
throw copy;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* DBOS's logger (`DBOS.setConfig`'s `logger`): its lines at the last `startWorker`'s `logLevel`,
|
|
54
|
+
* printed as DBOS's own console logger (`DBOSConsoleLogger`) prints them, except a sandbox run's
|
|
55
|
+
* (`quietIfSandbox`). A live run's failed tries and failure show as they always did. Installed
|
|
56
|
+
* only while DBOS exports no logs over OTLP: a custom logger replaces that transport.
|
|
57
|
+
*/
|
|
58
|
+
export const dbosLogger: DLogger = {
|
|
59
|
+
info: (entry) => void (shows("info") && console.log(entry)),
|
|
60
|
+
debug: (entry) => void (shows("debug") && console.debug(entry)),
|
|
61
|
+
warn(entry) {
|
|
62
|
+
if (shows("warn") && !sandboxRun.getStore()) console.warn(entry);
|
|
63
|
+
},
|
|
64
|
+
// DBOS's GlobalLogger passes a string, with the stack in `metadata.stack` and an error in `metadata.error`.
|
|
65
|
+
error(entry, metadata) {
|
|
66
|
+
const error = metadata?.error;
|
|
67
|
+
if (!shows("error") || (typeof error === "object" && error !== null && sandboxErrors.has(error))) return;
|
|
68
|
+
if (metadata?.stack) console.error(entry, "\n", metadata.stack);
|
|
69
|
+
else console.error(entry);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** A value as JSON, or as a string when it isn't JSON, cut to `max` characters, for a message. */
|
|
74
|
+
export function shown(value: unknown, max = Number.POSITIVE_INFINITY): string {
|
|
75
|
+
let text: string;
|
|
76
|
+
try {
|
|
77
|
+
text = JSON.stringify(value) ?? String(value);
|
|
78
|
+
} catch {
|
|
79
|
+
text = String(value);
|
|
80
|
+
}
|
|
81
|
+
return text.length > max ? `${text.slice(0, max)}…` : text;
|
|
82
|
+
}
|
package/src/op.ts
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { Fake } from "./fake.ts";
|
|
3
|
+
import type { Secrets } from "./secret.ts";
|
|
4
|
+
import type { Steps } from "./steps.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What an operation does to the outside world. It sets the badge a reviewer
|
|
8
|
+
* sees and, later, the Cedar action the call is checked against.
|
|
9
|
+
*/
|
|
10
|
+
export type Effect = "read" | "write" | "publish" | "send" | "money" | "access";
|
|
11
|
+
|
|
12
|
+
export interface OpSpec<I extends z.ZodType = z.ZodType, O extends z.ZodType = z.ZodType> {
|
|
13
|
+
effect: Effect;
|
|
14
|
+
input: I;
|
|
15
|
+
output: O;
|
|
16
|
+
/** Safe to retry: the vendor dedupes repeated calls. Otherwise a failed call stops the run. */
|
|
17
|
+
idempotent?: boolean;
|
|
18
|
+
description?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The resource instance a call acts on, from its parsed input, such as a post's id. The
|
|
21
|
+
* policy sees it as `target`, so it can decide per instance. Must be deterministic.
|
|
22
|
+
*
|
|
23
|
+
* Its input is typed `any` inside `defineConnector`: TypeScript gives an inline function the
|
|
24
|
+
* type `Specs` declares, not the one its own spec's schema implies. Annotate it to have it
|
|
25
|
+
* checked: `target: ({ id }: { id: string }) => \`post/${id}\``.
|
|
26
|
+
*/
|
|
27
|
+
target?: (input: any) => string;
|
|
28
|
+
/**
|
|
29
|
+
* How a scenario says this operation was set up or called, in Gherkin steps: `given` seeds
|
|
30
|
+
* the fake through it, `expect` expects a call to it. Cucumber expressions whose parameters are
|
|
31
|
+
* fields of the input, such as `{ given: "a post titled {title} exists" }`.
|
|
32
|
+
*/
|
|
33
|
+
phrases?: { given?: string; expect?: string };
|
|
34
|
+
/**
|
|
35
|
+
* Top-level fields of its input and output that are the driver's own data, not the vendor's
|
|
36
|
+
* (a resource type's `handle`): the ledger records each as `"<name>"`, never its value.
|
|
37
|
+
*/
|
|
38
|
+
opaque?: readonly string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One vendor operation, such as `ghost.post.publish`. Declares the contract only; drivers implement it. */
|
|
42
|
+
export interface Op<V extends string = string, R extends string = string, N extends string = string, I = any, O = any> {
|
|
43
|
+
readonly kind: "op";
|
|
44
|
+
readonly id: `${V}.${R}.${N}`;
|
|
45
|
+
readonly vendor: V;
|
|
46
|
+
readonly resource: R;
|
|
47
|
+
readonly name: N;
|
|
48
|
+
readonly effect: Effect;
|
|
49
|
+
readonly idempotent: boolean;
|
|
50
|
+
readonly description?: string;
|
|
51
|
+
/** From the spec: the resource instance a call acts on. */
|
|
52
|
+
readonly target?: (input: any) => string;
|
|
53
|
+
/** From the spec: how a scenario's steps name it. */
|
|
54
|
+
readonly phrases?: { readonly given?: string; readonly expect?: string };
|
|
55
|
+
/** From the spec: fields the ledger records by name only. */
|
|
56
|
+
readonly opaque?: readonly string[];
|
|
57
|
+
readonly input: z.ZodType<any, I>;
|
|
58
|
+
readonly output: z.ZodType<O>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Where a resource type keeps its brand. A symbol, so `defineConnector` tells a resource type
|
|
63
|
+
* (`defineResource`) from a group of operations; a registered one (`Symbol.for`), so a copy of
|
|
64
|
+
* this package reads what another copy made, as `VENDOR`.
|
|
65
|
+
*/
|
|
66
|
+
export const RESOURCE: unique symbol = Symbol.for("sanoma.resource");
|
|
67
|
+
|
|
68
|
+
/** What `defineConnector` reads of a resource type; `defineResource` makes them. */
|
|
69
|
+
export interface ResourceGroup {
|
|
70
|
+
readonly [RESOURCE]: true;
|
|
71
|
+
readonly vendor: string;
|
|
72
|
+
readonly type: string;
|
|
73
|
+
readonly ops: Readonly<Record<string, OpSpec>>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A connector's groups: operations by name, or a resource type, whose operations are `read` and `import`. */
|
|
77
|
+
export type Specs = Record<string, Record<string, OpSpec> | ResourceGroup>;
|
|
78
|
+
|
|
79
|
+
/** The operations of one group of `Specs`. */
|
|
80
|
+
export type GroupOps<G> = G extends ResourceGroup ? G["ops"] : G;
|
|
81
|
+
type InputOf<T> = T extends OpSpec<infer I, any> ? I : never;
|
|
82
|
+
type OutputOf<T> = T extends OpSpec<any, infer O> ? O : never;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Where a connector keeps its vendor. A symbol, so it names no resource: `Object.values` and
|
|
86
|
+
* `keyof` over a connector see its resources only. A registered one (`Symbol.for`): a process
|
|
87
|
+
* may load two copies of this package (a connector's peer linked into a sanoma checkout, the
|
|
88
|
+
* app's from its tarball), and each must read the connectors the other made.
|
|
89
|
+
*/
|
|
90
|
+
export const VENDOR: unique symbol = Symbol.for("sanoma.vendor");
|
|
91
|
+
|
|
92
|
+
export type Connector<V extends string, S extends Specs> = {
|
|
93
|
+
readonly [R in keyof S & string]: {
|
|
94
|
+
readonly [N in keyof GroupOps<S[R]> & string]: Op<
|
|
95
|
+
V,
|
|
96
|
+
R,
|
|
97
|
+
N,
|
|
98
|
+
z.input<InputOf<GroupOps<S[R]>[N]>>,
|
|
99
|
+
z.output<OutputOf<GroupOps<S[R]>[N]>>
|
|
100
|
+
>;
|
|
101
|
+
};
|
|
102
|
+
} & { readonly [VENDOR]: ConnectorVendor<V> };
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The shape of a connector's `[VENDOR]` record. A process may load two builds of this package,
|
|
106
|
+
* and `resolveConfig` reads only a record of its own shape (`checkConnector`): change it with
|
|
107
|
+
* the record, so a build refuses a connector another build made in another shape.
|
|
108
|
+
*/
|
|
109
|
+
export const CONNECTOR_FORMAT = 1;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A connector's vendor: its id, who it is (`defineConnector`'s third argument, for a UI), its
|
|
113
|
+
* resource types, and the driver and fake its package attaches (`withSetup`).
|
|
114
|
+
*/
|
|
115
|
+
export interface ConnectorVendor<V extends string = string> {
|
|
116
|
+
/** The record's shape (`CONNECTOR_FORMAT`). */
|
|
117
|
+
readonly format: typeof CONNECTOR_FORMAT;
|
|
118
|
+
readonly id: V;
|
|
119
|
+
readonly info?: VendorInfo;
|
|
120
|
+
/** The resource types among its groups, in the order given. */
|
|
121
|
+
readonly resources: readonly ResourceGroup[];
|
|
122
|
+
/** What `withSetup` attached: how the config's resolver makes its driver and its fake. */
|
|
123
|
+
readonly setup?: ConnectorSetup;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Who a connector's vendor is, and where the connector lives, for a UI to show beside its
|
|
128
|
+
* operations. `logo` is the vendor's mark as inline SVG markup (one `<svg>…</svg>` element), and
|
|
129
|
+
* `dark` its variant for dark backgrounds. The app shows a logo only as an image (a `data:` URL
|
|
130
|
+
* in an `<img>`), where an SVG's scripts and external references never run or load.
|
|
131
|
+
*/
|
|
132
|
+
export interface VendorInfo {
|
|
133
|
+
/** The vendor's name as it writes it, such as "Resend". */
|
|
134
|
+
title?: string;
|
|
135
|
+
logo?: { svg: string; dark?: string };
|
|
136
|
+
/** The connector's npm package name, such as "@sanoma-ai/connector-resend". */
|
|
137
|
+
package?: string;
|
|
138
|
+
/** The connector package's `homepage` from its package.json: where its code and README are, as an https URL. */
|
|
139
|
+
homepage?: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** One `<svg>` element, with nothing but whitespace around it. */
|
|
143
|
+
const SVG_ELEMENT = /^\s*<svg[\s>][\s\S]*<\/svg>\s*$/;
|
|
144
|
+
|
|
145
|
+
const isResourceGroup = (group: unknown): group is ResourceGroup =>
|
|
146
|
+
(typeof group === "function" || typeof group === "object") &&
|
|
147
|
+
group !== null &&
|
|
148
|
+
(group as Partial<ResourceGroup>)[RESOURCE] === true;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Declares a vendor's operations, grouped by resource: `defineConnector("ghost", { post: { create: {...} } })`.
|
|
152
|
+
* A group may be a resource type from `defineResource`, under its own `type`, for its `read` and
|
|
153
|
+
* `import`: `defineConnector("github", { repository }, { package: "@sanoma-ai/connector-github" })`;
|
|
154
|
+
* it must be of the same vendor, and the connector needs `info.package`, whose `/resources`
|
|
155
|
+
* entry exports the types for data files.
|
|
156
|
+
* `info` says who the vendor is and where the connector lives, for a UI:
|
|
157
|
+
* `{ title: "Resend", logo: { svg }, package: "@sanoma-ai/connector-resend", homepage: "https://…" }`.
|
|
158
|
+
*/
|
|
159
|
+
export function defineConnector<const V extends string, const S extends Specs>(
|
|
160
|
+
vendor: V,
|
|
161
|
+
specs: S,
|
|
162
|
+
info?: VendorInfo,
|
|
163
|
+
): Connector<V, S> {
|
|
164
|
+
// A copy, frozen once checked: changing the caller's object later cannot slip a logo past the check.
|
|
165
|
+
const logo =
|
|
166
|
+
info?.logo && Object.freeze(Object.fromEntries(Object.entries(info.logo).filter(([, v]) => v !== undefined)));
|
|
167
|
+
for (const [variant, svg] of Object.entries(logo ?? {})) {
|
|
168
|
+
if (typeof svg !== "string" || !SVG_ELEMENT.test(svg)) {
|
|
169
|
+
throw new Error(
|
|
170
|
+
`defineConnector("${vendor}"): logo.${variant} must be inline SVG markup, one <svg>…</svg> element`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const vendorInfo = info && Object.freeze({ ...info, ...(logo && { logo: logo as VendorInfo["logo"] }) });
|
|
175
|
+
// It becomes a link's href: a whole https URL, so never a `javascript:` one. Checked on the copy kept.
|
|
176
|
+
const homepage: unknown = vendorInfo?.homepage;
|
|
177
|
+
if (homepage !== undefined && (typeof homepage !== "string" || URL.parse(homepage)?.protocol !== "https:"))
|
|
178
|
+
throw new Error(`defineConnector("${vendor}"): homepage must be an https URL`);
|
|
179
|
+
const out: Record<string, Record<string, Op>> = {};
|
|
180
|
+
const resources: ResourceGroup[] = [];
|
|
181
|
+
for (const [resource, group] of Object.entries(specs)) {
|
|
182
|
+
let ops: Readonly<Record<string, OpSpec>> = group as Record<string, OpSpec>;
|
|
183
|
+
if (isResourceGroup(group)) {
|
|
184
|
+
if (group.vendor !== vendor) {
|
|
185
|
+
throw new Error(`defineConnector("${vendor}"): ${resource} is a resource type of ${group.vendor}`);
|
|
186
|
+
}
|
|
187
|
+
if (group.type !== resource) {
|
|
188
|
+
throw new Error(`defineConnector("${vendor}"): the resource type ${group.type} is given as ${resource}`);
|
|
189
|
+
}
|
|
190
|
+
resources.push(group);
|
|
191
|
+
ops = group.ops;
|
|
192
|
+
}
|
|
193
|
+
out[resource] = {};
|
|
194
|
+
for (const [name, spec] of Object.entries(ops)) {
|
|
195
|
+
out[resource][name] = Object.freeze({
|
|
196
|
+
kind: "op",
|
|
197
|
+
id: `${vendor}.${resource}.${name}`,
|
|
198
|
+
vendor,
|
|
199
|
+
resource,
|
|
200
|
+
name,
|
|
201
|
+
effect: spec.effect,
|
|
202
|
+
idempotent: spec.idempotent ?? false,
|
|
203
|
+
description: spec.description,
|
|
204
|
+
target: spec.target,
|
|
205
|
+
phrases: spec.phrases,
|
|
206
|
+
...(spec.opaque?.length ? { opaque: spec.opaque } : {}),
|
|
207
|
+
input: spec.input,
|
|
208
|
+
output: spec.output,
|
|
209
|
+
} satisfies Op);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// A data file imports the resource types from `<package>/resources`, which the reader matches exactly.
|
|
213
|
+
if (resources.length && !vendorInfo?.package) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`defineConnector("${vendor}"): a connector with resource types needs info.package, the npm package data files import them from (\`<package>/resources\`)`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const owner: ConnectorVendor<V> = Object.freeze({
|
|
219
|
+
format: CONNECTOR_FORMAT,
|
|
220
|
+
id: vendor,
|
|
221
|
+
info: vendorInfo,
|
|
222
|
+
resources: Object.freeze(resources),
|
|
223
|
+
});
|
|
224
|
+
return { ...out, [VENDOR]: owner } as unknown as Connector<V, S>;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* What the runtime tells a driver about the call it is making. `idempotencyKey` is the same
|
|
229
|
+
* on every replay and retry of one call (`<runId>:<seq>`), so a driver can hand it to the
|
|
230
|
+
* vendor, or dedupe on it, and a crash between the vendor's reply and the checkpoint does
|
|
231
|
+
* not repeat the side effect.
|
|
232
|
+
*/
|
|
233
|
+
export interface CallContext {
|
|
234
|
+
idempotencyKey: string;
|
|
235
|
+
runId: string;
|
|
236
|
+
opId: string;
|
|
237
|
+
/** 1 on the first try; retries count up (idempotent operations only). */
|
|
238
|
+
attempt: number;
|
|
239
|
+
/** The secrets store, for the values `secret()` markers name: `resolveSecrets(input, call.secrets)`. In memory for a fake. */
|
|
240
|
+
secrets: Secrets;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export type DriverFn<I = any, O = unknown> = (input: I, call: CallContext) => Promise<O>;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* A vendor's answer that the driver understood. `retryable` tells the runtime whether to try
|
|
247
|
+
* again (an idempotent operation on a timeout or a 5xx) or to stop (a 4xx). `status` and
|
|
248
|
+
* `vendorCode` keep what the vendor said, so the ledger and the UI can show it.
|
|
249
|
+
*/
|
|
250
|
+
export class DriverError extends Error {
|
|
251
|
+
readonly code = "driver_failed" as const;
|
|
252
|
+
readonly retryable: boolean;
|
|
253
|
+
readonly status?: number;
|
|
254
|
+
readonly vendorCode?: string;
|
|
255
|
+
|
|
256
|
+
constructor(message: string, options: { retryable: boolean; status?: number; vendorCode?: string; cause?: unknown }) {
|
|
257
|
+
super(message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
258
|
+
this.name = "DriverError";
|
|
259
|
+
this.retryable = options.retryable;
|
|
260
|
+
if (options.status !== undefined) this.status = options.status;
|
|
261
|
+
if (options.vendorCode !== undefined) this.vendorCode = options.vendorCode;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Whether a vendor's HTTP status is worth retrying: a request timeout (408), a rate limit (429)
|
|
267
|
+
* or a server error (5xx). It is the runtime's default rule; a driver adds its vendor's
|
|
268
|
+
* exceptions around it, such as a quota that does not lift in the seconds a retry waits.
|
|
269
|
+
*/
|
|
270
|
+
export function retryableStatus(status: number): boolean {
|
|
271
|
+
return status === 408 || status === 429 || status >= 500;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The environment variables a driver reads, as a Zod object of string schemas keyed by variable
|
|
276
|
+
* name, such as `z.object({ ACME_API_KEY: z.string().describe("an API key, from Settings, API keys") })`:
|
|
277
|
+
* each `.describe()` is the variable's one-line manual (what to put there, where to get it),
|
|
278
|
+
* `.optional()` marks one that may be unset. `resolveConfig` checks it against `process.env` and
|
|
279
|
+
* never keeps a value. The driver still reads the raw environment when it is called, so a
|
|
280
|
+
* `.default()` or `.transform()` is not applied to what it reads (use `.optional()` and name the
|
|
281
|
+
* default in `.describe()`), and a `.refine()` or `.superRefine()` on the object is not evaluated,
|
|
282
|
+
* only its fields.
|
|
283
|
+
*/
|
|
284
|
+
export type DriverEnv = z.ZodObject<Record<string, z.ZodType<string | undefined, string | undefined>>>;
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Implements a vendor's operations, keyed `"resource.name"`. A driver reads its credentials
|
|
288
|
+
* from the environment when it is called, never from the config file; `env` declares them.
|
|
289
|
+
*/
|
|
290
|
+
export interface Driver {
|
|
291
|
+
readonly vendor: string;
|
|
292
|
+
readonly ops: Readonly<Record<string, DriverFn>>;
|
|
293
|
+
readonly env?: DriverEnv;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* What a connector package attaches to its connector (`withSetup`), for the config's resolver
|
|
298
|
+
* to call: the config then names the connector alone, and never a driver, a fake or steps. Any may
|
|
299
|
+
* be missing: a workflow using an operation of a connector with no driver is refused, and a
|
|
300
|
+
* connector with no fake has none for sandbox runs.
|
|
301
|
+
*/
|
|
302
|
+
export interface ConnectorSetup {
|
|
303
|
+
/** Makes the live driver, which reads its credentials from the environment when called: once per resolved config. */
|
|
304
|
+
driver?: () => Driver;
|
|
305
|
+
/** Makes a fake of the vendor (`defineFake`): once per sandbox run, its own. */
|
|
306
|
+
fake?: () => Fake<any, any>;
|
|
307
|
+
/** The connector's steps for Gherkin tests (`defineSteps`): a config's scenarios and the connector's own features can use them. */
|
|
308
|
+
steps?: Steps;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* The connector with `setup` laid over what it already carries, as a connector package exports
|
|
313
|
+
* it: `export const ghost = withSetup(ghostOps, { driver: ghostDriver, fake: fakeGhost, steps: ghostSteps })`. A
|
|
314
|
+
* config that needs a driver made otherwise (another bridge, other options) swaps that one and
|
|
315
|
+
* keeps the fake: `withSetup(github, { driver: () => githubDriver({ bridge }) })`. The operations
|
|
316
|
+
* are the same objects, and the type is the connector's. The setup is kept on the connector's
|
|
317
|
+
* vendor record (`[VENDOR]`), so a spread copy of the connector keeps it.
|
|
318
|
+
*/
|
|
319
|
+
export function withSetup<C extends Connector<any, any>>(connector: C, setup: Partial<ConnectorSetup>): C {
|
|
320
|
+
const owner = connector[VENDOR];
|
|
321
|
+
return {
|
|
322
|
+
...connector,
|
|
323
|
+
[VENDOR]: Object.freeze({ ...owner, setup: Object.freeze({ ...owner.setup, ...setup }) }),
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Throws unless `value` is a connector this package can read: one `defineConnector` made, in this
|
|
329
|
+
* copy of the package or another of the same `CONNECTOR_FORMAT`. One an older build made (its
|
|
330
|
+
* `VENDOR` an unregistered symbol, or its record without a format) is refused as such.
|
|
331
|
+
*/
|
|
332
|
+
export function checkConnector(value: unknown): asserts value is Connector<any, any> {
|
|
333
|
+
const record =
|
|
334
|
+
typeof value === "object" && value !== null ? (value as Partial<Connector<any, any>>)[VENDOR] : undefined;
|
|
335
|
+
if (record?.format === CONNECTOR_FORMAT) return;
|
|
336
|
+
const build = "install the sanoma packages and connectors from one build";
|
|
337
|
+
throw new Error(
|
|
338
|
+
record === undefined
|
|
339
|
+
? `\`connectors\` holds something that is not a connector: list what the connector packages export (or one from an older build: ${build})`
|
|
340
|
+
: `The "${String(record.id)}" connector was made by another build of @sanoma-ai/workflows: ${build}`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** What `withSetup` attached to a connector: nothing when it attached nothing. */
|
|
345
|
+
export const setupOf = (connector: Connector<any, any>): ConnectorSetup => connector[VENDOR].setup ?? {};
|
|
346
|
+
|
|
347
|
+
export function isOp(x: unknown): x is Op {
|
|
348
|
+
return typeof x === "object" && x !== null && (x as Op).kind === "op";
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* What a driver for a connector must implement: every operation, by resource and name. Each
|
|
353
|
+
* function gets the input as the operation's schema parses it and returns what its output
|
|
354
|
+
* schema accepts.
|
|
355
|
+
*/
|
|
356
|
+
export type DriverImpl<S extends Specs> = {
|
|
357
|
+
[R in keyof S & string]: {
|
|
358
|
+
[N in keyof GroupOps<S[R]> & string]: DriverFn<
|
|
359
|
+
z.output<InputOf<GroupOps<S[R]>[N]>>,
|
|
360
|
+
z.input<OutputOf<GroupOps<S[R]>[N]>>
|
|
361
|
+
>;
|
|
362
|
+
};
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/** The ids of a connector's operations, such as `"ghost.post.create" | "ghost.post.publish"`. */
|
|
366
|
+
export type OpIdOf<V extends string, S extends Specs> = {
|
|
367
|
+
[R in keyof S & string]: { [N in keyof GroupOps<S[R]> & string]: `${V}.${R}.${N}` }[keyof GroupOps<S[R]> & string];
|
|
368
|
+
}[keyof S & string];
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Implements a connector's operations, typed by its schemas. A driver must be complete: an
|
|
372
|
+
* operation the connector declares that `impl` leaves out, or one `impl` adds that the
|
|
373
|
+
* connector doesn't declare, is refused here rather than when a run calls it. `options.env`
|
|
374
|
+
* declares the environment variables it reads (see `DriverEnv`).
|
|
375
|
+
*/
|
|
376
|
+
export function defineDriver<V extends string, S extends Specs>(
|
|
377
|
+
connector: Connector<V, S>,
|
|
378
|
+
impl: DriverImpl<S>,
|
|
379
|
+
options: { env?: DriverEnv } = {},
|
|
380
|
+
): Driver {
|
|
381
|
+
const declared = new Map<string, Op>();
|
|
382
|
+
for (const resource of Object.values(connector as Record<string, Record<string, unknown>>)) {
|
|
383
|
+
for (const op of Object.values(resource)) if (isOp(op)) declared.set(`${op.resource}.${op.name}`, op);
|
|
384
|
+
}
|
|
385
|
+
const vendor = connector[VENDOR].id;
|
|
386
|
+
const ops: Record<string, DriverFn> = {};
|
|
387
|
+
const extra: string[] = [];
|
|
388
|
+
for (const [resource, fns] of Object.entries(impl as Record<string, Record<string, unknown>>)) {
|
|
389
|
+
for (const [name, fn] of Object.entries(fns ?? {})) {
|
|
390
|
+
const key = `${resource}.${name}`;
|
|
391
|
+
if (!declared.has(key)) extra.push(`${vendor}.${key}`);
|
|
392
|
+
else if (typeof fn === "function") ops[key] = fn as DriverFn;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
const missing = [...declared.keys()].filter((key) => !Object.hasOwn(ops, key)).map((key) => `${vendor}.${key}`);
|
|
396
|
+
const problems = [
|
|
397
|
+
...(missing.length ? [`it does not implement ${missing.join(", ")}`] : []),
|
|
398
|
+
...(extra.length ? [`it implements ${extra.join(", ")}, which the connector does not declare`] : []),
|
|
399
|
+
];
|
|
400
|
+
if (problems.length) throw new Error(`defineDriver("${vendor}"): ${problems.join("; ")}`);
|
|
401
|
+
return Object.freeze({ vendor, ops: Object.freeze(ops), ...(options.env && { env: options.env }) });
|
|
402
|
+
}
|