@relyio/sdk 0.16.0 → 0.18.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.
Files changed (74) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/automations/index.cjs +70 -0
  3. package/dist/automations/index.d.cts +1309 -0
  4. package/dist/automations/index.d.ts +1309 -0
  5. package/dist/automations/index.js +5 -0
  6. package/dist/{chunk-RSHMA4KT.cjs → chunk-2SPPU2WG.cjs} +68 -68
  7. package/dist/chunk-7I3SC7SV.cjs +40 -0
  8. package/dist/chunk-DI6KFDBO.js +43 -0
  9. package/dist/chunk-EDE2H37D.js +686 -0
  10. package/dist/chunk-HDHBYYEY.js +36 -0
  11. package/dist/{chunk-RLLZUFDB.cjs → chunk-IFKF72VJ.cjs} +718 -331
  12. package/dist/chunk-L6DXCKVQ.cjs +50 -0
  13. package/dist/chunk-LSI26ZN2.cjs +535 -0
  14. package/dist/{chunk-TZL374LS.cjs → chunk-M7MWNV3Q.cjs} +21 -21
  15. package/dist/{chunk-H6IBQSMM.js → chunk-NJQK5STK.js} +3 -3
  16. package/dist/chunk-R6E4HE7R.js +520 -0
  17. package/dist/chunk-SHSK7UUY.cjs +706 -0
  18. package/dist/{chunk-BBUB7C2Z.js → chunk-W2A4OTCI.js} +3 -3
  19. package/dist/chunk-W7JFHSFM.js +18718 -0
  20. package/dist/{chunk-5UMY6CS4.js → chunk-ZAKEY6DY.js} +3 -3
  21. package/dist/chunk-ZAMAR3X2.cjs +18759 -0
  22. package/dist/{chunk-NIUOPIIB.js → chunk-ZHTJ7GXH.js} +400 -19
  23. package/dist/{chunk-LSRPMNPT.cjs → chunk-ZO6QDYJG.cjs} +7 -7
  24. package/dist/client/index.cjs +8 -7
  25. package/dist/client/index.d.cts +4 -3
  26. package/dist/client/index.d.ts +4 -3
  27. package/dist/client/index.js +6 -5
  28. package/dist/{define-content-type-LkI0rS2W.d.cts → define-content-type-NEQDrvdq.d.cts} +3420 -749
  29. package/dist/{define-content-type-CAWSlgn0.d.ts → define-content-type-fb-WCD5e.d.ts} +3420 -749
  30. package/dist/{define-schema-Dxu3RoHV.d.ts → define-schema-DZhtvLiK.d.cts} +157 -10
  31. package/dist/{define-schema-wlsJOrnY.d.cts → define-schema-Dlwh6l_J.d.ts} +157 -10
  32. package/dist/dev-handshake/index.cjs +9 -9
  33. package/dist/dev-handshake/index.js +2 -2
  34. package/dist/extensions/index.cjs +80 -0
  35. package/dist/extensions/index.d.cts +792 -0
  36. package/dist/extensions/index.d.ts +792 -0
  37. package/dist/extensions/index.js +76 -0
  38. package/dist/functions/index.cjs +68 -0
  39. package/dist/functions/index.d.cts +6 -0
  40. package/dist/functions/index.d.ts +6 -0
  41. package/dist/functions/index.js +59 -0
  42. package/dist/{index-CGtYxUuh.d.ts → index-C4LqrNd1.d.ts} +1 -1
  43. package/dist/{index-CcHmRALk.d.cts → index-Dnmt_UOo.d.cts} +1 -1
  44. package/dist/{index-D80MXUkf.d.cts → index-DwHcL6x5.d.cts} +103 -442
  45. package/dist/{index-BH8JksfG.d.ts → index-M3luH06A.d.ts} +103 -442
  46. package/dist/index.cjs +204 -185
  47. package/dist/index.d.cts +93 -13
  48. package/dist/index.d.ts +93 -13
  49. package/dist/index.js +11 -16
  50. package/dist/markdown/index.cjs +382 -382
  51. package/dist/markdown/index.js +1 -1
  52. package/dist/preview/index.cjs +11 -10
  53. package/dist/preview/index.js +3 -2
  54. package/dist/register-B53i97A9.d.ts +677 -0
  55. package/dist/register-BYCs0FpF.d.cts +677 -0
  56. package/dist/rich-text/index.cjs +12 -12
  57. package/dist/rich-text/index.d.cts +2 -2
  58. package/dist/rich-text/index.d.ts +2 -2
  59. package/dist/rich-text/index.js +2 -2
  60. package/dist/schema/index.cjs +62 -34
  61. package/dist/schema/index.d.cts +127 -5
  62. package/dist/schema/index.d.ts +127 -5
  63. package/dist/schema/index.js +2 -2
  64. package/dist/types/index.d.cts +1 -1
  65. package/dist/types/index.d.ts +1 -1
  66. package/dist/utils/index.cjs +20 -20
  67. package/dist/utils/index.js +2 -2
  68. package/package.json +21 -3
  69. package/dist/chunk-7BZW6XYJ.cjs +0 -433
  70. package/dist/chunk-CQHEG7QA.js +0 -411
  71. package/dist/chunk-DIZY6CKM.js +0 -950
  72. package/dist/chunk-GHYB6IZK.cjs +0 -983
  73. package/dist/chunk-MWMSFIWN.cjs +0 -17
  74. package/dist/chunk-XQ4QHWZE.js +0 -13
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkMWMSFIWN_cjs = require('./chunk-MWMSFIWN.cjs');
3
+ var chunkL6DXCKVQ_cjs = require('./chunk-L6DXCKVQ.cjs');
4
4
 
5
5
  /* RelyIO SDK - Built with ❤️ by the RelyIO Team */
6
6
 
@@ -8,11 +8,11 @@ var chunkMWMSFIWN_cjs = require('./chunk-MWMSFIWN.cjs');
8
8
  function isBrowser() {
9
9
  return typeof window !== "undefined" && typeof window.document !== "undefined";
10
10
  }
11
- chunkMWMSFIWN_cjs.__name(isBrowser, "isBrowser");
11
+ chunkL6DXCKVQ_cjs.__name(isBrowser, "isBrowser");
12
12
  function isNode() {
13
13
  return typeof process !== "undefined" && process.versions?.node != null;
14
14
  }
15
- chunkMWMSFIWN_cjs.__name(isNode, "isNode");
15
+ chunkL6DXCKVQ_cjs.__name(isNode, "isNode");
16
16
  function deepClone(obj) {
17
17
  if (obj === null || typeof obj !== "object") return obj;
18
18
  if (obj instanceof Date) return new Date(obj.getTime());
@@ -26,7 +26,7 @@ function deepClone(obj) {
26
26
  }
27
27
  return obj;
28
28
  }
29
- chunkMWMSFIWN_cjs.__name(deepClone, "deepClone");
29
+ chunkL6DXCKVQ_cjs.__name(deepClone, "deepClone");
30
30
  function debounce(func, wait) {
31
31
  let timeout;
32
32
  return (...args) => {
@@ -34,7 +34,7 @@ function debounce(func, wait) {
34
34
  timeout = setTimeout(() => func(...args), wait);
35
35
  };
36
36
  }
37
- chunkMWMSFIWN_cjs.__name(debounce, "debounce");
37
+ chunkL6DXCKVQ_cjs.__name(debounce, "debounce");
38
38
  function throttle(func, limit) {
39
39
  let inThrottle;
40
40
  return (...args) => {
@@ -45,7 +45,7 @@ function throttle(func, limit) {
45
45
  }
46
46
  };
47
47
  }
48
- chunkMWMSFIWN_cjs.__name(throttle, "throttle");
48
+ chunkL6DXCKVQ_cjs.__name(throttle, "throttle");
49
49
  function createDeferred() {
50
50
  let resolve;
51
51
  let reject;
@@ -55,11 +55,11 @@ function createDeferred() {
55
55
  });
56
56
  return { promise, resolve, reject };
57
57
  }
58
- chunkMWMSFIWN_cjs.__name(createDeferred, "createDeferred");
58
+ chunkL6DXCKVQ_cjs.__name(createDeferred, "createDeferred");
59
59
  function sleep(ms) {
60
60
  return new Promise((resolve) => setTimeout(resolve, ms));
61
61
  }
62
- chunkMWMSFIWN_cjs.__name(sleep, "sleep");
62
+ chunkL6DXCKVQ_cjs.__name(sleep, "sleep");
63
63
  async function retry(fn, options = {}) {
64
64
  const {
65
65
  maxAttempts = 3,
@@ -85,7 +85,7 @@ async function retry(fn, options = {}) {
85
85
  }
86
86
  throw lastError;
87
87
  }
88
- chunkMWMSFIWN_cjs.__name(retry, "retry");
88
+ chunkL6DXCKVQ_cjs.__name(retry, "retry");
89
89
  function toSafeString(value) {
90
90
  if (value === null) return "null";
91
91
  if (value === void 0) return "undefined";
@@ -97,11 +97,11 @@ function toSafeString(value) {
97
97
  return "[object Object]";
98
98
  }
99
99
  }
100
- chunkMWMSFIWN_cjs.__name(toSafeString, "toSafeString");
100
+ chunkL6DXCKVQ_cjs.__name(toSafeString, "toSafeString");
101
101
  function generateId() {
102
102
  return Math.random().toString(36).substring(2) + Date.now().toString(36);
103
103
  }
104
- chunkMWMSFIWN_cjs.__name(generateId, "generateId");
104
+ chunkL6DXCKVQ_cjs.__name(generateId, "generateId");
105
105
  function isPlainObject(value) {
106
106
  if (typeof value !== "object" || value === null) return false;
107
107
  if (Object.prototype.toString.call(value) !== "[object Object]") return false;
@@ -112,7 +112,7 @@ function isPlainObject(value) {
112
112
  }
113
113
  return Object.getPrototypeOf(value) === proto;
114
114
  }
115
- chunkMWMSFIWN_cjs.__name(isPlainObject, "isPlainObject");
115
+ chunkL6DXCKVQ_cjs.__name(isPlainObject, "isPlainObject");
116
116
  function mergeDeep(target, ...sources) {
117
117
  if (!sources.length) return target;
118
118
  const source = sources.shift();
@@ -132,7 +132,7 @@ function mergeDeep(target, ...sources) {
132
132
  }
133
133
  return mergeDeep(target, ...sources);
134
134
  }
135
- chunkMWMSFIWN_cjs.__name(mergeDeep, "mergeDeep");
135
+ chunkL6DXCKVQ_cjs.__name(mergeDeep, "mergeDeep");
136
136
  function pick(obj, keys) {
137
137
  const result = {};
138
138
  for (const key of keys) {
@@ -142,7 +142,7 @@ function pick(obj, keys) {
142
142
  }
143
143
  return result;
144
144
  }
145
- chunkMWMSFIWN_cjs.__name(pick, "pick");
145
+ chunkL6DXCKVQ_cjs.__name(pick, "pick");
146
146
  function omit(obj, keys) {
147
147
  const result = { ...obj };
148
148
  for (const key of keys) {
@@ -150,20 +150,20 @@ function omit(obj, keys) {
150
150
  }
151
151
  return result;
152
152
  }
153
- chunkMWMSFIWN_cjs.__name(omit, "omit");
153
+ chunkL6DXCKVQ_cjs.__name(omit, "omit");
154
154
  function camelToSnake(str) {
155
155
  return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
156
156
  }
157
- chunkMWMSFIWN_cjs.__name(camelToSnake, "camelToSnake");
157
+ chunkL6DXCKVQ_cjs.__name(camelToSnake, "camelToSnake");
158
158
  function snakeToCamel(str) {
159
159
  return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
160
160
  }
161
- chunkMWMSFIWN_cjs.__name(snakeToCamel, "snakeToCamel");
161
+ chunkL6DXCKVQ_cjs.__name(snakeToCamel, "snakeToCamel");
162
162
  function isValidEmail(email) {
163
163
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
164
164
  return emailRegex.test(email);
165
165
  }
166
- chunkMWMSFIWN_cjs.__name(isValidEmail, "isValidEmail");
166
+ chunkL6DXCKVQ_cjs.__name(isValidEmail, "isValidEmail");
167
167
  function isValidUrl(url) {
168
168
  try {
169
169
  new URL(url);
@@ -172,7 +172,7 @@ function isValidUrl(url) {
172
172
  return false;
173
173
  }
174
174
  }
175
- chunkMWMSFIWN_cjs.__name(isValidUrl, "isValidUrl");
175
+ chunkL6DXCKVQ_cjs.__name(isValidUrl, "isValidUrl");
176
176
 
177
177
  exports.camelToSnake = camelToSnake;
178
178
  exports.createDeferred = createDeferred;
@@ -192,5 +192,5 @@ exports.sleep = sleep;
192
192
  exports.snakeToCamel = snakeToCamel;
193
193
  exports.throttle = throttle;
194
194
  exports.toSafeString = toSafeString;
195
- //# sourceMappingURL=chunk-TZL374LS.cjs.map
196
- //# sourceMappingURL=chunk-TZL374LS.cjs.map
195
+ //# sourceMappingURL=chunk-M7MWNV3Q.cjs.map
196
+ //# sourceMappingURL=chunk-M7MWNV3Q.cjs.map
@@ -1,4 +1,4 @@
1
- import { __name } from './chunk-XQ4QHWZE.js';
1
+ import { __name } from './chunk-DI6KFDBO.js';
2
2
 
3
3
  /* RelyIO SDK - Built with ❤️ by the RelyIO Team */
4
4
 
@@ -173,5 +173,5 @@ function isValidUrl(url) {
173
173
  __name(isValidUrl, "isValidUrl");
174
174
 
175
175
  export { camelToSnake, createDeferred, debounce, deepClone, generateId, isBrowser, isNode, isPlainObject, isValidEmail, isValidUrl, mergeDeep, omit, pick, retry, sleep, snakeToCamel, throttle, toSafeString };
176
- //# sourceMappingURL=chunk-H6IBQSMM.js.map
177
- //# sourceMappingURL=chunk-H6IBQSMM.js.map
176
+ //# sourceMappingURL=chunk-NJQK5STK.js.map
177
+ //# sourceMappingURL=chunk-NJQK5STK.js.map
@@ -0,0 +1,520 @@
1
+ import { conditionRoot, compileKeyedMembers } from './chunk-W7JFHSFM.js';
2
+ import { __name } from './chunk-DI6KFDBO.js';
3
+
4
+ /* RelyIO SDK - Built with ❤️ by the RelyIO Team */
5
+
6
+ // src/automations/index.ts
7
+ var REF = /* @__PURE__ */ Symbol("relyio.automation.ref");
8
+ var TEMPLATE = /* @__PURE__ */ Symbol("relyio.automation.template");
9
+ var TESTS = /* @__PURE__ */ new Set([
10
+ "eq",
11
+ "ne",
12
+ "gt",
13
+ "gte",
14
+ "lt",
15
+ "lte",
16
+ "in",
17
+ "nin",
18
+ "exists",
19
+ "isNull",
20
+ "isEmpty",
21
+ "hasValue",
22
+ "contains",
23
+ "notContains",
24
+ "between"
25
+ ]);
26
+ function refAt(root, path, becomes) {
27
+ return new Proxy(/* @__PURE__ */ Object.create(null), {
28
+ get(_target, key) {
29
+ if (key === REF) return { kind: "ref", root, path };
30
+ if (typeof key !== "string" || key === "then") return void 0;
31
+ if (TESTS.has(key)) return conditionRoot(root, path)[key];
32
+ if (becomes && key === "becomes") {
33
+ return (value) => ({
34
+ root: "event",
35
+ path,
36
+ operator: "becomes",
37
+ value
38
+ });
39
+ }
40
+ return refAt(root, [...path, key], becomes);
41
+ }
42
+ });
43
+ }
44
+ __name(refAt, "refAt");
45
+ function refOf(value) {
46
+ if (typeof value !== "object" || value === null) return void 0;
47
+ const held = value[REF];
48
+ return held === void 0 ? void 0 : held;
49
+ }
50
+ __name(refOf, "refOf");
51
+ function tpl(strings, ...values) {
52
+ const parts = [];
53
+ const literal = /* @__PURE__ */ __name((text) => {
54
+ const last = parts[parts.length - 1];
55
+ if (typeof last === "string") parts[parts.length - 1] = last + text;
56
+ else if (text !== "") parts.push(text);
57
+ }, "literal");
58
+ for (const [index, text] of strings.entries()) {
59
+ literal(text);
60
+ if (index >= values.length) continue;
61
+ const value = values[index];
62
+ const ref = refOf(value);
63
+ if (ref === void 0) literal(String(value));
64
+ else parts.push(ref);
65
+ }
66
+ return { [TEMPLATE]: parts };
67
+ }
68
+ __name(tpl, "tpl");
69
+ function isPlainObject(value) {
70
+ if (typeof value !== "object" || value === null) return false;
71
+ const prototype = Object.getPrototypeOf(value);
72
+ return prototype === Object.prototype || prototype === null;
73
+ }
74
+ __name(isPlainObject, "isPlainObject");
75
+ function valueOf(value, at) {
76
+ const ref = refOf(value);
77
+ if (ref !== void 0) return ref;
78
+ if (typeof value === "object" && value !== null && TEMPLATE in value) {
79
+ return { kind: "template", parts: [...value[TEMPLATE]] };
80
+ }
81
+ if (Array.isArray(value)) {
82
+ const items = value.map((item, index) => valueOf(item, `${at}[${index}]`));
83
+ return items.every((item) => item.kind === "literal") ? { kind: "literal", value: items.map((item) => item.value) } : { kind: "list", items };
84
+ }
85
+ if (isPlainObject(value)) {
86
+ const entries2 = Object.entries(value).filter(([, held]) => held !== void 0).map(([key, held]) => [key, valueOf(held, `${at}.${key}`)]);
87
+ return entries2.every(([, held]) => held.kind === "literal") ? {
88
+ kind: "literal",
89
+ value: Object.fromEntries(
90
+ entries2.map(([key, held]) => [key, held.value])
91
+ )
92
+ } : { kind: "object", fields: Object.fromEntries(entries2) };
93
+ }
94
+ if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) {
95
+ return { kind: "literal", value };
96
+ }
97
+ throw new TypeError(
98
+ `${at} is ${typeof value}: a step is handed data \u2014 a value, a reference or tpl\`\u2026\` \u2014 never code`
99
+ );
100
+ }
101
+ __name(valueOf, "valueOf");
102
+ function inputOf(input, at) {
103
+ return Object.fromEntries(
104
+ Object.entries(input).filter(([, value]) => value !== void 0).map(([key, value]) => [key, valueOf(value, `${at}.${key}`)])
105
+ );
106
+ }
107
+ __name(inputOf, "inputOf");
108
+ function and(...conditions) {
109
+ return { and: conditions };
110
+ }
111
+ __name(and, "and");
112
+ function or(...conditions) {
113
+ return { or: conditions };
114
+ }
115
+ __name(or, "or");
116
+ function not(condition) {
117
+ return { not: condition };
118
+ }
119
+ __name(not, "not");
120
+ var FIELD_OPERATORS = ["eq", "ne", "gt", "gte", "lt", "lte", "in", "nin", "exists"];
121
+ function whereOf(where, item) {
122
+ if (typeof where === "function") {
123
+ return where(refAt("event", [], true));
124
+ }
125
+ if (!isPlainObject(where)) {
126
+ throw new TypeError(
127
+ "A where is { field: value | { becomes, eq, \u2026 } }, or a function of the event"
128
+ );
129
+ }
130
+ const tests = [];
131
+ for (const [field, held] of Object.entries(where)) {
132
+ const path = [item, field];
133
+ if (!isPlainObject(held)) {
134
+ tests.push({ root: "event", path, operator: "eq", value: held });
135
+ continue;
136
+ }
137
+ for (const [operator, value] of Object.entries(held)) {
138
+ if (operator === "becomes") {
139
+ tests.push({ root: "event", path, operator: "becomes", value });
140
+ } else if (FIELD_OPERATORS.includes(operator)) {
141
+ tests.push({
142
+ root: "event",
143
+ path,
144
+ operator,
145
+ value
146
+ });
147
+ } else {
148
+ throw new TypeError(`where.${field}: ${operator} is not becomes or a list grammar operator`);
149
+ }
150
+ }
151
+ }
152
+ const [only] = tests;
153
+ return tests.length === 1 && only !== void 0 ? only : { and: tests };
154
+ }
155
+ __name(whereOf, "whereOf");
156
+ var ITEM = /* @__PURE__ */ Symbol("relyio.automation.item");
157
+ var ITEM_REF = /* @__PURE__ */ Symbol("relyio.automation.item-ref");
158
+ function uidOf(uid, at) {
159
+ const value = valueOf(uid, at);
160
+ if (value.kind === "ref") return value;
161
+ if (value.kind === "literal" && typeof value.value === "string" && value.value !== "") {
162
+ return value;
163
+ }
164
+ throw new TypeError(
165
+ `${at} is a uid: a reference to one (event.record._metadata.id, a reference field, an earlier step's output) or a uid written out`
166
+ );
167
+ }
168
+ __name(uidOf, "uidOf");
169
+ function itemRefOf(type, uid, at) {
170
+ const held = uidOf(uid, at);
171
+ return type.kind === "record" ? { kind: "record", recordType: type.recordType, uid: held } : { kind: "entry", contentType: type.contentType, uid: held };
172
+ }
173
+ __name(itemRefOf, "itemRefOf");
174
+ var RECORD_ACTIONS = [
175
+ "created",
176
+ "updated",
177
+ "deleted",
178
+ "expired"
179
+ ];
180
+ var ENTRY_ACTIONS = [
181
+ "created",
182
+ "updated",
183
+ "published",
184
+ "unpublished",
185
+ "deleted",
186
+ "archived",
187
+ "restored"
188
+ ];
189
+ function triggersByType(make) {
190
+ return new Proxy(/* @__PURE__ */ Object.create(null), {
191
+ get(_target, key) {
192
+ if (typeof key !== "string" || key === "then") return void 0;
193
+ return make(key);
194
+ }
195
+ });
196
+ }
197
+ __name(triggersByType, "triggersByType");
198
+ function itemHandleOf(type) {
199
+ const name = type.kind === "record" ? `records.${type.recordType}` : `entries.${type.contentType}`;
200
+ return {
201
+ [ITEM]: type,
202
+ ref: /* @__PURE__ */ __name((uid) => ({
203
+ [ITEM_REF]: itemRefOf(type, uid, `${name}.ref`)
204
+ }), "ref")
205
+ };
206
+ }
207
+ __name(itemHandleOf, "itemHandleOf");
208
+ var records = triggersByType((recordType) => ({
209
+ ...itemHandleOf({ kind: "record", recordType }),
210
+ ...Object.fromEntries(
211
+ RECORD_ACTIONS.map((action) => [
212
+ action,
213
+ (options) => ({
214
+ trigger: {
215
+ kind: "record",
216
+ recordType,
217
+ action,
218
+ ...options?.where === void 0 ? {} : { where: whereOf(options.where, "record") }
219
+ }
220
+ })
221
+ ])
222
+ )
223
+ }));
224
+ var entries = triggersByType((contentType) => ({
225
+ ...itemHandleOf({ kind: "entry", contentType }),
226
+ ...Object.fromEntries(
227
+ ENTRY_ACTIONS.map((action) => [
228
+ action,
229
+ (options) => ({
230
+ trigger: {
231
+ kind: "entry",
232
+ contentType,
233
+ action,
234
+ ...options?.where === void 0 ? {} : { where: whereOf(options.where, "entry") }
235
+ }
236
+ })
237
+ ])
238
+ )
239
+ }));
240
+ var anyEntry = Object.fromEntries(
241
+ ENTRY_ACTIONS.map((action) => [
242
+ action,
243
+ (options) => {
244
+ const where = options?.where;
245
+ return {
246
+ trigger: {
247
+ kind: "anyEntry",
248
+ action,
249
+ ...where === void 0 ? {} : { where: where(refAt("event", [], false)) }
250
+ }
251
+ };
252
+ }
253
+ ])
254
+ );
255
+ var schedule = {
256
+ cron(cron, options) {
257
+ return { trigger: { kind: "schedule", cron, timezone: options.timezone } };
258
+ }
259
+ };
260
+ var RUN_FAILED_PATHS = {
261
+ automation: ["automation"],
262
+ step: ["step", "name"],
263
+ code: ["error", "code"]
264
+ };
265
+ function runFailedWhereOf(where) {
266
+ if (typeof where === "function") {
267
+ return where(refAt("event", [], false));
268
+ }
269
+ if (!isPlainObject(where)) {
270
+ throw new TypeError(
271
+ "A failed run\u2019s where is { automation, step, code }, or a function of the event"
272
+ );
273
+ }
274
+ const tests = [];
275
+ for (const [name, held] of Object.entries(where)) {
276
+ if (!Object.keys(RUN_FAILED_PATHS).includes(name)) {
277
+ throw new TypeError(`where.${name}: a failed run is filtered on automation, step and code`);
278
+ }
279
+ const path = RUN_FAILED_PATHS[name];
280
+ if (!isPlainObject(held)) {
281
+ tests.push({ root: "event", path, operator: "eq", value: held });
282
+ continue;
283
+ }
284
+ for (const [operator, value] of Object.entries(held)) {
285
+ if (!FIELD_OPERATORS.includes(operator)) {
286
+ throw new TypeError(`where.${name}: ${operator} is not a list grammar operator`);
287
+ }
288
+ tests.push({
289
+ root: "event",
290
+ path,
291
+ operator,
292
+ value
293
+ });
294
+ }
295
+ }
296
+ const [only] = tests;
297
+ return tests.length === 1 && only !== void 0 ? only : { and: tests };
298
+ }
299
+ __name(runFailedWhereOf, "runFailedWhereOf");
300
+ var automations = {
301
+ runFailed(options) {
302
+ return {
303
+ trigger: {
304
+ kind: "runFailed",
305
+ ...options?.where === void 0 ? {} : { where: runFailedWhereOf(options.where) }
306
+ }
307
+ };
308
+ }
309
+ };
310
+ function extensionTrigger(extension, trigger, options) {
311
+ const where = options?.where;
312
+ return {
313
+ trigger: {
314
+ kind: "extension",
315
+ extension,
316
+ trigger,
317
+ ...where === void 0 ? {} : { where: where(refAt("event", [], false)) }
318
+ }
319
+ };
320
+ }
321
+ __name(extensionTrigger, "extensionTrigger");
322
+ var on = { records, entries, anyEntry, schedule, automations };
323
+ var STEP = /* @__PURE__ */ Symbol("relyio.automation.step");
324
+ var ASK_DEFAULT_ROLES = ["editor", "admin"];
325
+ var ASK_DEFAULT_TIMEOUT = "7d";
326
+ var APPROVE_CHOICES = ["approve", "reject"];
327
+ function handle(draft, output) {
328
+ const step = { name: draft.name, [STEP]: draft };
329
+ if (!output) return step;
330
+ const withOutput = {
331
+ ...step,
332
+ output: refAt(draft.name, ["output"], false)
333
+ };
334
+ return withOutput;
335
+ }
336
+ __name(handle, "handle");
337
+ function draftsOf(steps, at) {
338
+ return steps.map((step, index) => {
339
+ const draft = step[STEP];
340
+ if (draft === void 0) {
341
+ throw new TypeError(`${at}[${index}] is not a step: build one with step.fn, step.if, \u2026`);
342
+ }
343
+ return draft;
344
+ });
345
+ }
346
+ __name(draftsOf, "draftsOf");
347
+ var STEP_BUILDER = {
348
+ fn(name, fn, input) {
349
+ return handle(
350
+ { kind: "function", name, function: fn, input: inputOf(input, `${name}.input`) },
351
+ true
352
+ );
353
+ },
354
+ agent(name, config) {
355
+ const { propose } = config;
356
+ return handle(
357
+ {
358
+ kind: "agent",
359
+ name,
360
+ ...config.agent === void 0 ? {} : { agent: config.agent },
361
+ instructions: valueOf(config.instructions, `${name}.instructions`),
362
+ context: inputOf(config.context ?? {}, `${name}.context`),
363
+ output: compileKeyedMembers(config.output),
364
+ ...propose === void 0 ? {} : {
365
+ propose: propose.map(
366
+ (type, index) => proposeTargetOf(type, `${name}.propose[${index}]`)
367
+ )
368
+ }
369
+ },
370
+ true
371
+ );
372
+ },
373
+ action(name, action, input) {
374
+ return handle(
375
+ {
376
+ kind: "action",
377
+ name,
378
+ extension: action.extension,
379
+ action: action.action,
380
+ input: inputOf(input, `${name}.input`)
381
+ },
382
+ true
383
+ );
384
+ },
385
+ lookup(name, type, uidOrOptions, options) {
386
+ const held = type ?? {};
387
+ let item;
388
+ let given;
389
+ if (held[ITEM_REF] !== void 0) {
390
+ item = held[ITEM_REF];
391
+ given = uidOrOptions;
392
+ } else if (held[ITEM] !== void 0) {
393
+ item = itemRefOf(held[ITEM], uidOrOptions, `${name}.uid`);
394
+ given = options;
395
+ } else {
396
+ throw new TypeError(
397
+ `${name} looks up a Record or entry type \u2014 records.<type>, entries.<type> \u2014 or one item, <type>.ref(uid)`
398
+ );
399
+ }
400
+ const { state } = given ?? {};
401
+ if (item.kind === "record") {
402
+ if (state !== void 0) {
403
+ throw new TypeError(`${name}: a Record has no draft; state is an entry Look-up's alone`);
404
+ }
405
+ return handle({ kind: "lookup", name, item }, true);
406
+ }
407
+ return handle({ kind: "lookup", name, item, state: state ?? "published" }, true);
408
+ },
409
+ if(name, condition, branches) {
410
+ return handle(
411
+ {
412
+ kind: "if",
413
+ name,
414
+ condition,
415
+ then: draftsOf(branches.then ?? [], `${name}.then`),
416
+ else: draftsOf(branches.else ?? [], `${name}.else`)
417
+ },
418
+ false
419
+ );
420
+ },
421
+ wait(name, duration) {
422
+ return handle({ kind: "wait", name, duration }, false);
423
+ },
424
+ ask(name, config) {
425
+ const approving = "approve" in config;
426
+ const approve = approving ? valueOf(config.approve, `${name}.approve`) : void 0;
427
+ if (approve !== void 0 && approve.kind !== "ref") {
428
+ throw new TypeError(`${name}.approve names an Agent step's proposal: <step>.output.proposal`);
429
+ }
430
+ return handle(
431
+ {
432
+ kind: "ask",
433
+ name,
434
+ to: [...config.to ?? ASK_DEFAULT_ROLES],
435
+ question: valueOf(config.question, `${name}.question`),
436
+ choices: approving ? [...APPROVE_CHOICES] : [...config.choices],
437
+ timeout: config.timeout ?? ASK_DEFAULT_TIMEOUT,
438
+ ...config.onTimeout === void 0 ? {} : { onTimeout: config.onTimeout },
439
+ ...config.sample === void 0 ? {} : { sample: config.sample },
440
+ ...approve === void 0 ? {} : { approve }
441
+ },
442
+ true
443
+ );
444
+ }
445
+ };
446
+ function proposeTargetOf(type, at) {
447
+ const held = type ?? {};
448
+ const named = held[ITEM];
449
+ if (named === void 0) {
450
+ throw new TypeError(`${at} is a Record or content type: records.<type>, entries.<type>`);
451
+ }
452
+ return named.kind === "record" ? { kind: "record", recordType: named.recordType } : { kind: "entry", contentType: named.contentType };
453
+ }
454
+ __name(proposeTargetOf, "proposeTargetOf");
455
+ function defineAutomation(config) {
456
+ const event = refAt("event", [], false);
457
+ const steps = config.steps({ event, step: STEP_BUILDER });
458
+ return {
459
+ _tag: "automation",
460
+ ...config.description === void 0 ? {} : { description: config.description },
461
+ on: config.on.trigger,
462
+ ...config.about === void 0 ? {} : { about: aboutOf(config.about({ event })) },
463
+ steps: draftsOf(steps, "steps")
464
+ };
465
+ }
466
+ __name(defineAutomation, "defineAutomation");
467
+ function aboutOf(handle2) {
468
+ const item = handle2?.[ITEM_REF];
469
+ if (item === void 0) {
470
+ throw new TypeError(
471
+ "about is one Record or entry: return records.<type>.ref(uid) or entries.<type>.ref(uid)"
472
+ );
473
+ }
474
+ return item;
475
+ }
476
+ __name(aboutOf, "aboutOf");
477
+ function isAutomationFile(value) {
478
+ return typeof value === "object" && value !== null && value._tag === "automation" && Array.isArray(value.steps) && typeof value.on === "object";
479
+ }
480
+ __name(isAutomationFile, "isAutomationFile");
481
+ function resolveAutomation(file, nameOf) {
482
+ const resolve = /* @__PURE__ */ __name((steps) => steps.map((step) => {
483
+ switch (step.kind) {
484
+ case "function": {
485
+ const name = nameOf(step.function);
486
+ if (name === void 0) {
487
+ throw new Error(
488
+ `Step ${step.name} takes a Function no file under relyio/functions/ exports: import the Function file's default export`
489
+ );
490
+ }
491
+ return { kind: "function", name: step.name, function: name, input: step.input };
492
+ }
493
+ case "if": {
494
+ return { ...step, then: resolve(step.then), else: resolve(step.else) };
495
+ }
496
+ case "agent":
497
+ case "action":
498
+ case "lookup":
499
+ case "wait":
500
+ case "ask": {
501
+ return step;
502
+ }
503
+ default: {
504
+ const unhandled = step;
505
+ throw new Error(`No step kind ${JSON.stringify(unhandled)}`);
506
+ }
507
+ }
508
+ }), "resolve");
509
+ return {
510
+ ...file.description === void 0 ? {} : { description: file.description },
511
+ on: file.on,
512
+ ...file.about === void 0 ? {} : { about: file.about },
513
+ steps: resolve(file.steps)
514
+ };
515
+ }
516
+ __name(resolveAutomation, "resolveAutomation");
517
+
518
+ export { and, anyEntry, automations, defineAutomation, entries, extensionTrigger, isAutomationFile, not, on, or, records, resolveAutomation, schedule, tpl };
519
+ //# sourceMappingURL=chunk-R6E4HE7R.js.map
520
+ //# sourceMappingURL=chunk-R6E4HE7R.js.map