@diagonal-hq/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 +28 -0
- package/README.md +57 -2
- package/dist/chunks/chunk-3EO3FSI5.js +94 -0
- package/dist/chunks/chunk-NUFRM6SI.js +0 -0
- package/dist/chunks/chunk-OF5QITP7.js +42 -0
- package/dist/chunks/chunk-RTCHH3JG.js +150 -0
- package/dist/console.d.ts +10 -0
- package/dist/data.d.ts +124 -0
- package/dist/errors.d.ts +23 -0
- package/dist/generated/capabilities.d.ts +2158 -0
- package/dist/http-transport.d.ts +15 -0
- package/dist/http-transport.js +49 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +472 -0
- package/dist/input.d.ts +168 -0
- package/dist/notifications.d.ts +47 -0
- package/dist/profiles/migration.d.ts +3 -0
- package/dist/profiles/migration.js +21 -0
- package/dist/profiles/test.d.ts +3 -0
- package/dist/profiles/test.js +21 -0
- package/dist/serial-transaction-transport.d.ts +10 -0
- package/dist/testing/index.d.ts +26 -0
- package/dist/testing/index.js +185 -0
- package/dist/transaction.d.ts +1 -0
- package/dist/transport.d.ts +5 -0
- package/dist/workflow.d.ts +105 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Copyright (c) 2026 Diagonal Software Corp. All rights reserved.
|
|
2
|
+
|
|
3
|
+
This software is the proprietary property of Diagonal Software Corp. ("Diagonal"). It is
|
|
4
|
+
licensed, not sold.
|
|
5
|
+
|
|
6
|
+
Diagonal grants you a limited, non-exclusive, non-transferable, revocable
|
|
7
|
+
license to install and use this software only to develop, check, build, and
|
|
8
|
+
deploy workspaces for the Diagonal platform, in accordance with your agreement
|
|
9
|
+
with Diagonal.
|
|
10
|
+
|
|
11
|
+
You may not:
|
|
12
|
+
|
|
13
|
+
- modify, adapt, translate, or create derivative works of this software;
|
|
14
|
+
- copy, distribute, sublicense, sell, or otherwise make this software available
|
|
15
|
+
to any third party, except as an unmodified dependency of a Diagonal
|
|
16
|
+
workspace;
|
|
17
|
+
- use this software with any service other than the Diagonal platform;
|
|
18
|
+
- remove or change this notice.
|
|
19
|
+
|
|
20
|
+
This license ends automatically when you stop using the Diagonal platform or
|
|
21
|
+
when you breach its terms.
|
|
22
|
+
|
|
23
|
+
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
25
|
+
FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL DIAGONAL BE
|
|
26
|
+
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
27
|
+
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE
|
|
28
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,58 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@diagonal-hq/workflows`
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This package is the authoring API of Diagonal managed workflows. A workflow module imports it to declare its handler and to call the host capabilities. The package is ESM JavaScript with declarations. It has no dependencies. It does not depend on `@diagonal-hq/pages`, `@diagonal-hq/cli`, or React.
|
|
4
|
+
|
|
5
|
+
## Entry points
|
|
6
|
+
|
|
7
|
+
| Entry point | Used by | Exports |
|
|
8
|
+
| ---------------------------------- | ----------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `@diagonal-hq/workflows` | Workflows | `defineWorkflow`, `respond`, `redirect`, `fail`, `validationError`, the input schema builder `v`, `transaction`, `data`, `mail`, `notification`, `sms`, and the generated capability wrappers |
|
|
10
|
+
| `@diagonal-hq/workflows/test` | E2E setup and assert scripts | The helpers that call no host capability: `fail`, `validationError`, `HostError`, `WorkflowFailure`, and `v` |
|
|
11
|
+
| `@diagonal-hq/workflows/migration` | Data migration scripts | The same helpers as `/test` |
|
|
12
|
+
| `@diagonal-hq/workflows/testing` | Local workflow tests in Node | `MockTransport`, `runWorkflow`, and `fake` |
|
|
13
|
+
| `@diagonal-hq/workflows/http` | Local runs against a real workspace | `HttpTransport` |
|
|
14
|
+
|
|
15
|
+
A profile entry point (`/test` or `/migration`) exports a capability only when its profile allows that capability. The capability sets of the profiles are not decided yet, so both entry points export only the helpers above. The compiler and the script host do not enforce the profiles yet.
|
|
16
|
+
|
|
17
|
+
Only `/testing` imports Node built-in modules (`node:async_hooks` and `node:crypto`). The other entry points need no Node API.
|
|
18
|
+
|
|
19
|
+
## Run a workflow in a test
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { MockTransport, runWorkflow } from '@diagonal-hq/workflows/testing';
|
|
23
|
+
|
|
24
|
+
import welcomeMember from './welcome-member';
|
|
25
|
+
|
|
26
|
+
const transport = new MockTransport({ 'mail.send': () => ({ success: true }) });
|
|
27
|
+
const output = await runWorkflow(welcomeMember, {}, { transport });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`runWorkflow` sends each capability call to the transport of its own run. Overlapping and nested runs do not share transports. A call with no mock handler fails with `capability_denied`.
|
|
31
|
+
|
|
32
|
+
## Source in the platform repository
|
|
33
|
+
|
|
34
|
+
`src/` is the same SDK source that the workflow script compiler embeds and that the QuickJS runtime bundles. Existing workspace modules import it as `@diagonal/workflow`. `php artisan workflows:script-sdk:build` regenerates `generated/capabilities.json` and `src/generated/capabilities.ts`. Do not hand-edit them.
|
|
35
|
+
|
|
36
|
+
`bin/diagonal-workflow.mjs` runs only from a platform checkout, so the package does not publish it. See `app/workflows/README.md`.
|
|
37
|
+
|
|
38
|
+
## Build the package
|
|
39
|
+
|
|
40
|
+
`npm run build:workflows-package` (`scripts/build-workflows-package.mjs`) writes `dist/` and packs `scripts/dist/npm/diagonal-hq-workflows-<version>.tgz`. esbuild writes one ESM file for each entry point, and the entry points share chunks, so the root entry and `/testing` use one transport slot. TypeScript writes the declarations next to them. `npm run generate-typescript` runs the script.
|
|
41
|
+
|
|
42
|
+
`node scripts/smoke-workflows-package.mjs [tarball]` installs the packed tarball in a consumer outside the repository. It checks the packed file list and the manifest, imports each entry point in Node, runs a workflow through `/testing`, and runs `tsc` against the packed declarations. The `generated-assets` job of PR CI runs it.
|
|
43
|
+
|
|
44
|
+
## Publish the package
|
|
45
|
+
|
|
46
|
+
The `Publish npm package` workflow (`.github/workflows/publish-npm-package.yml`) publishes the package to npm after each push to `main`. You can also run it from the Actions tab.
|
|
47
|
+
|
|
48
|
+
1. The build job builds and packs the package. It compares the packed files with the tarball that npm has for the version in `package.json`. The `version` field does not count.
|
|
49
|
+
2. When the files differ, a model reads the diff and the commit subjects since that publish. It classifies the change as breaking, feature, or fix. Before 1.0.0, a breaking change raises the minor part, and other changes raise the patch part. From 1.0.0, the workflow follows semantic versioning.
|
|
50
|
+
3. The build job packs the package again at the new version and runs the smoke test on the tarball.
|
|
51
|
+
4. The commit job commits the new version to `main` with an `[automated] release:` commit.
|
|
52
|
+
5. The publish job runs in the `npm-publish` environment and publishes the tested bytes with npm trusted publishing. It does not build again. It refuses a version that npm already has.
|
|
53
|
+
|
|
54
|
+
To choose a version yourself, such as 1.0.0 or a prerelease, set `version` in `package.json` to a version that npm does not have, and merge the change. The workflow publishes that version as it is. A version with a prerelease part, such as `0.2.0-next.1`, gets the `next` tag. Other versions get `latest`. The workflow does not raise a prerelease version. Set the next version by hand.
|
|
55
|
+
|
|
56
|
+
The model call uses the `OPENROUTER_API_KEY` secret. When the call fails, the release fails. The workflow does not guess a version.
|
|
57
|
+
|
|
58
|
+
npm must list `Diagonal-HQ/diagonal`, the `publish-npm-package.yml` workflow, and the `npm-publish` environment as the trusted publisher of `@diagonal-hq/workflows`.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coerceInput
|
|
3
|
+
} from "./chunk-RTCHH3JG.js";
|
|
4
|
+
import {
|
|
5
|
+
validationError
|
|
6
|
+
} from "./chunk-OF5QITP7.js";
|
|
7
|
+
|
|
8
|
+
// packages/workflows/src/transport.ts
|
|
9
|
+
var active = null;
|
|
10
|
+
function setTransport(transport) {
|
|
11
|
+
active = transport;
|
|
12
|
+
}
|
|
13
|
+
function invoke(name, input) {
|
|
14
|
+
if (active === null) {
|
|
15
|
+
return Promise.reject(
|
|
16
|
+
new Error(
|
|
17
|
+
"No Diagonal host transport is set. In tests, call setTransport(new MockTransport(...))."
|
|
18
|
+
)
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
return active.call(name, input);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// packages/workflows/src/workflow.ts
|
|
25
|
+
function parseWorkflowInput(workflow, input) {
|
|
26
|
+
return workflow.input === void 0 ? input : coerceInput(workflow.input, input);
|
|
27
|
+
}
|
|
28
|
+
async function validateWorkflowInput(validator, input, ctx) {
|
|
29
|
+
const reported = await validator(input, ctx);
|
|
30
|
+
if (reported === void 0 || reported === null) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (typeof reported !== "object" || Array.isArray(reported)) {
|
|
34
|
+
throw new TypeError(
|
|
35
|
+
"validateInput must return an object of errors keyed by field, or nothing."
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
const errors = {};
|
|
39
|
+
for (const [field, messages] of Object.entries(reported)) {
|
|
40
|
+
const list = (Array.isArray(messages) ? messages : [messages]).filter(
|
|
41
|
+
(message) => typeof message === "string" && message !== ""
|
|
42
|
+
);
|
|
43
|
+
if (list.length > 0) {
|
|
44
|
+
errors[field] = list;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (Object.keys(errors).length > 0) {
|
|
48
|
+
validationError(errors);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function defineWorkflow(inputOrHandler, maybeHandler) {
|
|
52
|
+
if (typeof inputOrHandler === "function") {
|
|
53
|
+
return { $diagonal: "workflow", run: async (input, ctx) => inputOrHandler(input, ctx) };
|
|
54
|
+
}
|
|
55
|
+
const spec = inputOrHandler.$spec;
|
|
56
|
+
const handler = maybeHandler;
|
|
57
|
+
if (handler === void 0) {
|
|
58
|
+
throw new TypeError("defineWorkflow(input, handler) needs a handler after the input schema.");
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
$diagonal: "workflow",
|
|
62
|
+
input: spec,
|
|
63
|
+
run: async (input, ctx) => handler(input, ctx)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function respond(body, options = {}) {
|
|
67
|
+
return {
|
|
68
|
+
$response_status: options.status ?? 200,
|
|
69
|
+
$response_body: body,
|
|
70
|
+
$response_content_type: options.contentType ?? "application/json"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function redirect(url, options = {}) {
|
|
74
|
+
const type = options.type ?? (/^\/(?![/\\])/.test(url) ? "internal" : "external");
|
|
75
|
+
const output = {
|
|
76
|
+
redirect_url: url,
|
|
77
|
+
redirect_type: type,
|
|
78
|
+
permanent: type !== "back" && options.permanent === true
|
|
79
|
+
};
|
|
80
|
+
if (options.success !== void 0) {
|
|
81
|
+
output.success = options.success;
|
|
82
|
+
}
|
|
83
|
+
return output;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
setTransport,
|
|
88
|
+
invoke,
|
|
89
|
+
parseWorkflowInput,
|
|
90
|
+
validateWorkflowInput,
|
|
91
|
+
defineWorkflow,
|
|
92
|
+
respond,
|
|
93
|
+
redirect
|
|
94
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// packages/workflows/src/errors.ts
|
|
2
|
+
var HostError = class extends Error {
|
|
3
|
+
/**
|
|
4
|
+
* @param failureId The host's ID for the failure of the call. When the error ends the run, the
|
|
5
|
+
* host records that failure, so a run records the failure of the call that it rethrows.
|
|
6
|
+
*/
|
|
7
|
+
constructor(message, code, status, details, failureId = null) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.details = details;
|
|
12
|
+
this.failureId = failureId;
|
|
13
|
+
this.name = "HostError";
|
|
14
|
+
}
|
|
15
|
+
code;
|
|
16
|
+
status;
|
|
17
|
+
details;
|
|
18
|
+
failureId;
|
|
19
|
+
};
|
|
20
|
+
var WorkflowFailure = class extends Error {
|
|
21
|
+
constructor(message, status, details = null) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.details = details;
|
|
25
|
+
this.name = "WorkflowFailure";
|
|
26
|
+
}
|
|
27
|
+
status;
|
|
28
|
+
details;
|
|
29
|
+
};
|
|
30
|
+
function fail(message, options = {}) {
|
|
31
|
+
throw new WorkflowFailure(message, options.status ?? 400, options.details ?? null);
|
|
32
|
+
}
|
|
33
|
+
function validationError(errors, options = {}) {
|
|
34
|
+
throw new WorkflowFailure(options.message ?? "The given data was invalid.", 422, { errors });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
HostError,
|
|
39
|
+
WorkflowFailure,
|
|
40
|
+
fail,
|
|
41
|
+
validationError
|
|
42
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// packages/workflows/src/input.ts
|
|
2
|
+
var InputField = class _InputField {
|
|
3
|
+
constructor($spec) {
|
|
4
|
+
this.$spec = $spec;
|
|
5
|
+
}
|
|
6
|
+
$spec;
|
|
7
|
+
/** The key may be absent. */
|
|
8
|
+
optional() {
|
|
9
|
+
return new _InputField({ ...this.$spec, optional: true });
|
|
10
|
+
}
|
|
11
|
+
/** The value may be `null`. */
|
|
12
|
+
nullable() {
|
|
13
|
+
return new _InputField({ ...this.$spec, nullable: true });
|
|
14
|
+
}
|
|
15
|
+
/** At least `value`: a number's value, a text's length, or a list's item count. */
|
|
16
|
+
min(value) {
|
|
17
|
+
return this.rule(`min:${value}`);
|
|
18
|
+
}
|
|
19
|
+
/** At most `value`: a number's value, a text's length, or a list's item count. */
|
|
20
|
+
max(value) {
|
|
21
|
+
return this.rule(`max:${value}`);
|
|
22
|
+
}
|
|
23
|
+
/** Between `min` and `max`, inclusive, measured as for `min` and `max`. */
|
|
24
|
+
between(min, max) {
|
|
25
|
+
return this.rule(`between:${min},${max}`);
|
|
26
|
+
}
|
|
27
|
+
/** Exactly `value`, measured as for `min` and `max`. */
|
|
28
|
+
size(value) {
|
|
29
|
+
return this.rule(`size:${value}`);
|
|
30
|
+
}
|
|
31
|
+
/** The text matches the pattern. */
|
|
32
|
+
regex(pattern) {
|
|
33
|
+
return this.rule(`regex:/${pattern.source}/${pattern.flags}`);
|
|
34
|
+
}
|
|
35
|
+
/** Any other platform validation rule, such as `starts_with:INV-` or `after:today`. */
|
|
36
|
+
rule(rule) {
|
|
37
|
+
return new _InputField({ ...this.$spec, rules: [...this.$spec.rules, rule] });
|
|
38
|
+
}
|
|
39
|
+
label(label) {
|
|
40
|
+
return new _InputField({ ...this.$spec, label });
|
|
41
|
+
}
|
|
42
|
+
description(description) {
|
|
43
|
+
return new _InputField({ ...this.$spec, description });
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var InputSchema = class extends InputField {
|
|
47
|
+
constructor(shape) {
|
|
48
|
+
super(objectSpec(shape));
|
|
49
|
+
this.shape = shape;
|
|
50
|
+
}
|
|
51
|
+
shape;
|
|
52
|
+
};
|
|
53
|
+
function objectSpec(shape) {
|
|
54
|
+
return {
|
|
55
|
+
kind: "object",
|
|
56
|
+
optional: false,
|
|
57
|
+
nullable: false,
|
|
58
|
+
rules: [],
|
|
59
|
+
fields: Object.fromEntries(Object.entries(shape).map(([key, field2]) => [key, field2.$spec]))
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
var field = (kind, extra = {}) => new InputField({ kind, optional: false, nullable: false, rules: [], ...extra });
|
|
63
|
+
var v = {
|
|
64
|
+
text: () => field("text"),
|
|
65
|
+
email: () => field("email"),
|
|
66
|
+
url: () => field("url"),
|
|
67
|
+
uuid: () => field("uuid"),
|
|
68
|
+
phone: () => field("phone"),
|
|
69
|
+
/** A calendar date, `YYYY-MM-DD`. */
|
|
70
|
+
date: () => field("date"),
|
|
71
|
+
/** A date and time, ISO 8601. */
|
|
72
|
+
datetime: () => field("datetime"),
|
|
73
|
+
/** A time of day, `HH:MM`. */
|
|
74
|
+
time: () => field("time"),
|
|
75
|
+
timezone: () => field("timezone"),
|
|
76
|
+
color: () => field("color"),
|
|
77
|
+
number: () => field("number"),
|
|
78
|
+
integer: () => field("integer"),
|
|
79
|
+
/** An amount of money: `amount` in the currency's minor units, such as cents, and the lowercase ISO 4217 `currency`. */
|
|
80
|
+
money: () => field("money"),
|
|
81
|
+
boolean: () => field("boolean"),
|
|
82
|
+
/** One of `options`. */
|
|
83
|
+
select: (options) => field("select", { options }),
|
|
84
|
+
/** A list whose items each satisfy `items`. */
|
|
85
|
+
list: (items) => field("list", { items: items.$spec }),
|
|
86
|
+
/** An object with the given fields. */
|
|
87
|
+
object: (shape) => new InputSchema(shape),
|
|
88
|
+
/**
|
|
89
|
+
* The field values of a record of `resource`, checked against that resource's field
|
|
90
|
+
* definitions: types, required fields, and each field's own rules.
|
|
91
|
+
*/
|
|
92
|
+
record: (resource) => field("record", { resource }),
|
|
93
|
+
/** The ID of an existing record of `resource`, in `scope`. */
|
|
94
|
+
recordReference: (resource, options = {}) => field("recordReference", { resource, ...options }),
|
|
95
|
+
/** The ID of an existing role. */
|
|
96
|
+
roleReference: () => field("roleReference"),
|
|
97
|
+
/**
|
|
98
|
+
* A file: the ID of an existing file, or the temporary path of an upload. A data call that
|
|
99
|
+
* writes the value to a file field of a record finalizes the upload.
|
|
100
|
+
*/
|
|
101
|
+
file: (options = {}) => field("file", options),
|
|
102
|
+
/** A rich text document with the formatting that `options` allows. */
|
|
103
|
+
content: (options = {}) => field("content", Object.keys(options).length === 0 ? {} : { content: options })
|
|
104
|
+
};
|
|
105
|
+
var NUMERIC = /^-?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?$/i;
|
|
106
|
+
function coerceNumber(value) {
|
|
107
|
+
return typeof value === "string" && NUMERIC.test(value.trim()) ? Number(value) : value;
|
|
108
|
+
}
|
|
109
|
+
function coerceInput(spec, value) {
|
|
110
|
+
if (value === null || value === void 0) {
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
switch (spec.kind) {
|
|
114
|
+
case "number":
|
|
115
|
+
case "integer":
|
|
116
|
+
return coerceNumber(value);
|
|
117
|
+
case "money":
|
|
118
|
+
return typeof value === "object" && !Array.isArray(value) && "amount" in value ? { ...value, amount: coerceNumber(value.amount) } : value;
|
|
119
|
+
case "boolean":
|
|
120
|
+
if (value === "true" || value === "1" || value === 1) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return value === "false" || value === "0" || value === 0 ? false : value;
|
|
124
|
+
case "select":
|
|
125
|
+
return typeof value === "string" && !spec.options?.includes(value) && spec.options?.includes(Number(value)) ? Number(value) : value;
|
|
126
|
+
case "list":
|
|
127
|
+
return Array.isArray(value) && spec.items ? value.map((item) => coerceInput(spec.items, item)) : value;
|
|
128
|
+
case "object": {
|
|
129
|
+
if (typeof value !== "object" || Array.isArray(value) || !spec.fields) {
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
const result = { ...value };
|
|
133
|
+
for (const [key, fieldSpec] of Object.entries(spec.fields)) {
|
|
134
|
+
if (key in result) {
|
|
135
|
+
result[key] = coerceInput(fieldSpec, result[key]);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
default:
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export {
|
|
146
|
+
InputField,
|
|
147
|
+
InputSchema,
|
|
148
|
+
v,
|
|
149
|
+
coerceInput
|
|
150
|
+
};
|
package/dist/data.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { type DataCountInput, type DataCreateInput, type DataCreateManyInput, type DataDeleteInput, type DataDeleteManyInput, type DataDeleteManyOutput, type DataFindInput, type DataFindOrCreateInput, type DataRestoreInput, type DataSearchInput, type DataSearchOutput, type DataUpdateInput, type DataUpdateOrCreateInput } from "./generated/capabilities.js";
|
|
2
|
+
import type { WorkspaceResources } from "./index.js";
|
|
3
|
+
type Entry = WorkspaceResources[keyof WorkspaceResources];
|
|
4
|
+
export type ResourceKey = [keyof WorkspaceResources] extends [never] ? string : Entry extends {
|
|
5
|
+
id: infer I;
|
|
6
|
+
} ? I : never;
|
|
7
|
+
/** The id of each entry of `E` that has every capability in `C`. */
|
|
8
|
+
type IdWithCapabilities<E, C extends string> = E extends {
|
|
9
|
+
id: infer I;
|
|
10
|
+
capabilities: infer Declared;
|
|
11
|
+
} ? [C] extends [Declared] ? I : never : never;
|
|
12
|
+
/**
|
|
13
|
+
* The ids of the resources that have every capability in `C`. Any string while the workspace
|
|
14
|
+
* declares no resources.
|
|
15
|
+
*/
|
|
16
|
+
export type ResourceKeyWith<C extends string> = [keyof WorkspaceResources] extends [never] ? string : IdWithCapabilities<Entry, C>;
|
|
17
|
+
type EntryFor<R> = Extract<Entry, {
|
|
18
|
+
id: R;
|
|
19
|
+
}>;
|
|
20
|
+
export type InputOf<R> = [EntryFor<R>] extends [never] ? Record<string, unknown> : EntryFor<R> extends {
|
|
21
|
+
input: infer I;
|
|
22
|
+
} ? I : never;
|
|
23
|
+
export type RecordOf<R> = [EntryFor<R>] extends [never] ? {
|
|
24
|
+
id: string;
|
|
25
|
+
} & Record<string, unknown> : EntryFor<R> extends {
|
|
26
|
+
record: infer T;
|
|
27
|
+
} ? T : never;
|
|
28
|
+
/** The declared keys of `T`. `keyof` on a type with an index signature returns only `string | number`. */
|
|
29
|
+
type DeclaredKey<T> = keyof {
|
|
30
|
+
[K in keyof T as string extends K ? never : number extends K ? never : K]: T[K];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* `DataSearchOutput` with `data` narrowed to the resource's record shape and `cursor` narrowed to
|
|
34
|
+
* the cursor text. `pagination` stays as generated.
|
|
35
|
+
*/
|
|
36
|
+
type SearchOutput<R> = Pick<DataSearchOutput, Exclude<DeclaredKey<DataSearchOutput>, 'data' | 'cursor'>> & {
|
|
37
|
+
data?: Array<RecordOf<R>>;
|
|
38
|
+
/** With `paging: 'cursor'`, the `after` value for the next page. Null when no records are left. */
|
|
39
|
+
cursor?: string | null;
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
};
|
|
42
|
+
type EachInput<R> = Omit<DataSearchInput, 'resource' | 'page' | 'pageSize' | 'paging' | 'after'> & {
|
|
43
|
+
resource: R;
|
|
44
|
+
/** Stop after this many records. Leave it out to visit every match. */
|
|
45
|
+
maxRecords?: number;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Visit every record that the search matches, one record at a time, with cursor paging. The
|
|
49
|
+
* query is copied when `each` is called, so a change to the input object during the walk does
|
|
50
|
+
* not change the pages.
|
|
51
|
+
*
|
|
52
|
+
* Each page continues after the last record of the page before it. A record that the loop body
|
|
53
|
+
* updates so that it no longer matches, or deletes, does not move a later page. A body that
|
|
54
|
+
* changes the value of a `sort` field can move a visited record after the cursor, and the walk
|
|
55
|
+
* then visits it again. Sort on a field that the body does not change, or leave `sort` empty.
|
|
56
|
+
*/
|
|
57
|
+
declare function each<R extends ResourceKey>(input: EachInput<R>): AsyncGenerator<RecordOf<R>, void, undefined>;
|
|
58
|
+
export declare const data: {
|
|
59
|
+
create: <R extends ResourceKey>(input: Omit<DataCreateInput, "resource" | "data"> & {
|
|
60
|
+
resource: R;
|
|
61
|
+
data: InputOf<R>;
|
|
62
|
+
}) => Promise<{
|
|
63
|
+
id: string;
|
|
64
|
+
} & Record<string, unknown>>;
|
|
65
|
+
find: <R extends ResourceKey>(input: Omit<DataFindInput, "resource"> & {
|
|
66
|
+
resource: R;
|
|
67
|
+
}) => Promise<{
|
|
68
|
+
id: string;
|
|
69
|
+
} & Record<string, unknown>>;
|
|
70
|
+
update: <R extends ResourceKey>(input: Omit<DataUpdateInput, "resource" | "data"> & {
|
|
71
|
+
resource: R;
|
|
72
|
+
data: InputOf<R>;
|
|
73
|
+
}) => Promise<{
|
|
74
|
+
id: string;
|
|
75
|
+
} & Record<string, unknown>>;
|
|
76
|
+
search: <R extends ResourceKey>(input: Omit<DataSearchInput, "resource"> & {
|
|
77
|
+
resource: R;
|
|
78
|
+
}) => Promise<SearchOutput<R>>;
|
|
79
|
+
each: typeof each;
|
|
80
|
+
delete: <R extends ResourceKey>(input: Omit<DataDeleteInput, "resource"> & {
|
|
81
|
+
resource: R;
|
|
82
|
+
}) => Promise<{
|
|
83
|
+
id: string;
|
|
84
|
+
} & Record<string, unknown>>;
|
|
85
|
+
restore: <R extends ResourceKey>(input: Omit<DataRestoreInput, "resource"> & {
|
|
86
|
+
resource: R;
|
|
87
|
+
}) => Promise<{
|
|
88
|
+
id: string;
|
|
89
|
+
} & Record<string, unknown>>;
|
|
90
|
+
count: <R extends ResourceKey>(input: Omit<DataCountInput, "resource"> & {
|
|
91
|
+
resource: R;
|
|
92
|
+
}) => Promise<number>;
|
|
93
|
+
findOrCreate: <R extends ResourceKey>(input: Omit<DataFindOrCreateInput, "resource"> & {
|
|
94
|
+
resource: R;
|
|
95
|
+
}) => Promise<{
|
|
96
|
+
id: string;
|
|
97
|
+
} & Record<string, unknown>>;
|
|
98
|
+
updateOrCreate: <R extends ResourceKey>(input: Omit<DataUpdateOrCreateInput, "resource"> & {
|
|
99
|
+
resource: R;
|
|
100
|
+
}) => Promise<{
|
|
101
|
+
id: string;
|
|
102
|
+
} & Record<string, unknown>>;
|
|
103
|
+
createMany: <R extends ResourceKey>(input: Omit<DataCreateManyInput, "resource" | "records"> & {
|
|
104
|
+
resource: R;
|
|
105
|
+
records: Array<InputOf<R>>;
|
|
106
|
+
}) => Promise<({
|
|
107
|
+
id: string;
|
|
108
|
+
} & Record<string, unknown>)[]>;
|
|
109
|
+
deleteMany: <R extends ResourceKey>(input: Omit<DataDeleteManyInput, "resource"> & {
|
|
110
|
+
resource: R;
|
|
111
|
+
}) => Promise<DataDeleteManyOutput>;
|
|
112
|
+
aggregateMetrics: (input: import("./index.js").DataAggregateMetricsInput) => Promise<import("./index.js").DataAggregateMetricsOutput>;
|
|
113
|
+
relation: {
|
|
114
|
+
attach: (input: import("./index.js").DataRelationAttachInput) => Promise<import("./index.js").DataRelationAttachOutput>;
|
|
115
|
+
attachMany: (input: import("./index.js").DataRelationAttachManyInput) => Promise<import("./index.js").DataRelationAttachManyOutput>;
|
|
116
|
+
detach: (input: import("./index.js").DataRelationDetachInput) => Promise<import("./index.js").DataRelationDetachOutput>;
|
|
117
|
+
detachMany: (input: import("./index.js").DataRelationDetachManyInput) => Promise<import("./index.js").DataRelationDetachManyOutput>;
|
|
118
|
+
sync: (input: import("./index.js").DataRelationSyncInput) => Promise<import("./index.js").DataRelationSyncOutput>;
|
|
119
|
+
};
|
|
120
|
+
semanticSearch: (input: import("./index.js").DataSemanticSearchInput) => Promise<import("./index.js").DataSemanticSearchOutput>;
|
|
121
|
+
sum: (input: import("./index.js").DataSumInput) => Promise<import("./index.js").DataSumOutput>;
|
|
122
|
+
validate: (input: import("./index.js").DataValidateInput) => Promise<import("./index.js").DataValidateOutput>;
|
|
123
|
+
};
|
|
124
|
+
export {};
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class HostError extends Error {
|
|
2
|
+
readonly code: string;
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly details: unknown;
|
|
5
|
+
readonly failureId: number | null;
|
|
6
|
+
/**
|
|
7
|
+
* @param failureId The host's ID for the failure of the call. When the error ends the run, the
|
|
8
|
+
* host records that failure, so a run records the failure of the call that it rethrows.
|
|
9
|
+
*/
|
|
10
|
+
constructor(message: string, code: string, status: number, details: unknown, failureId?: number | null);
|
|
11
|
+
}
|
|
12
|
+
export declare class WorkflowFailure extends Error {
|
|
13
|
+
readonly status: number;
|
|
14
|
+
readonly details: unknown;
|
|
15
|
+
constructor(message: string, status: number, details?: unknown);
|
|
16
|
+
}
|
|
17
|
+
export declare function fail(message: string, options?: {
|
|
18
|
+
status?: number;
|
|
19
|
+
details?: unknown;
|
|
20
|
+
}): never;
|
|
21
|
+
export declare function validationError(errors: Record<string, string[]>, options?: {
|
|
22
|
+
message?: string;
|
|
23
|
+
}): never;
|