@dxos/functions 0.8.2-main.fbd8ed0 → 0.8.2-staging.7ac8446

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 (176) hide show
  1. package/dist/lib/browser/bundler/index.mjs +3 -0
  2. package/dist/lib/browser/bundler/index.mjs.map +1 -1
  3. package/dist/lib/browser/chunk-HI7YZO2K.mjs +482 -0
  4. package/dist/lib/browser/chunk-HI7YZO2K.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-LT4LR4VU.mjs +72 -0
  6. package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +7 -0
  7. package/dist/lib/browser/chunk-RVSG6WTL.mjs +358 -0
  8. package/dist/lib/browser/chunk-RVSG6WTL.mjs.map +7 -0
  9. package/dist/lib/browser/chunk-XRCXIG74.mjs +12 -0
  10. package/dist/lib/browser/chunk-XRCXIG74.mjs.map +7 -0
  11. package/dist/lib/browser/edge/index.mjs +7 -63
  12. package/dist/lib/browser/edge/index.mjs.map +4 -4
  13. package/dist/lib/browser/index.mjs +101 -371
  14. package/dist/lib/browser/index.mjs.map +4 -4
  15. package/dist/lib/browser/meta.json +1 -1
  16. package/dist/lib/browser/testing/index.mjs +670 -0
  17. package/dist/lib/browser/testing/index.mjs.map +7 -0
  18. package/dist/lib/browser/types/index.mjs +49 -0
  19. package/dist/lib/browser/types/index.mjs.map +7 -0
  20. package/dist/lib/node/bundler/index.cjs +1 -0
  21. package/dist/lib/node/bundler/index.cjs.map +1 -1
  22. package/dist/lib/node/chunk-DSUGRAAL.cjs +392 -0
  23. package/dist/lib/node/chunk-DSUGRAAL.cjs.map +7 -0
  24. package/dist/lib/node/chunk-JEQ2X3Z6.cjs +34 -0
  25. package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +7 -0
  26. package/dist/lib/node/chunk-NXZNXVT3.cjs +94 -0
  27. package/dist/lib/node/chunk-NXZNXVT3.cjs.map +7 -0
  28. package/dist/lib/node/chunk-RXMCVAMJ.cjs +496 -0
  29. package/dist/lib/node/chunk-RXMCVAMJ.cjs.map +7 -0
  30. package/dist/lib/node/edge/index.cjs +5 -65
  31. package/dist/lib/node/edge/index.cjs.map +4 -4
  32. package/dist/lib/node/index.cjs +93 -383
  33. package/dist/lib/node/index.cjs.map +4 -4
  34. package/dist/lib/node/meta.json +1 -1
  35. package/dist/lib/node/testing/index.cjs +687 -0
  36. package/dist/lib/node/testing/index.cjs.map +7 -0
  37. package/dist/lib/node/types/index.cjs +70 -0
  38. package/dist/lib/node/types/index.cjs.map +7 -0
  39. package/dist/lib/node-esm/bundler/index.mjs +1 -0
  40. package/dist/lib/node-esm/bundler/index.mjs.map +1 -1
  41. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +12 -0
  42. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +7 -0
  43. package/dist/lib/node-esm/chunk-HBD2FZXO.mjs +358 -0
  44. package/dist/lib/node-esm/chunk-HBD2FZXO.mjs.map +7 -0
  45. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +72 -0
  46. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +7 -0
  47. package/dist/lib/node-esm/chunk-SQSJO5HI.mjs +482 -0
  48. package/dist/lib/node-esm/chunk-SQSJO5HI.mjs.map +7 -0
  49. package/dist/lib/node-esm/edge/index.mjs +6 -64
  50. package/dist/lib/node-esm/edge/index.mjs.map +4 -4
  51. package/dist/lib/node-esm/index.mjs +99 -371
  52. package/dist/lib/node-esm/index.mjs.map +4 -4
  53. package/dist/lib/node-esm/meta.json +1 -1
  54. package/dist/lib/node-esm/testing/index.mjs +670 -0
  55. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  56. package/dist/lib/node-esm/types/index.mjs +49 -0
  57. package/dist/lib/node-esm/types/index.mjs.map +7 -0
  58. package/dist/types/src/browser/index.d.ts +2 -0
  59. package/dist/types/src/browser/index.d.ts.map +1 -0
  60. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  61. package/dist/types/src/edge/functions.d.ts +3 -3
  62. package/dist/types/src/edge/functions.d.ts.map +1 -1
  63. package/dist/types/src/edge/index.d.ts.map +1 -1
  64. package/dist/types/src/function/function-registry.d.ts +25 -0
  65. package/dist/types/src/function/function-registry.d.ts.map +1 -0
  66. package/dist/types/src/function/function-registry.test.d.ts +2 -0
  67. package/dist/types/src/function/function-registry.test.d.ts.map +1 -0
  68. package/dist/types/src/function/index.d.ts +2 -0
  69. package/dist/types/src/function/index.d.ts.map +1 -0
  70. package/dist/types/src/handler.d.ts +66 -18
  71. package/dist/types/src/handler.d.ts.map +1 -1
  72. package/dist/types/src/index.d.ts +3 -3
  73. package/dist/types/src/index.d.ts.map +1 -1
  74. package/dist/types/src/runtime/dev-server.d.ts +52 -0
  75. package/dist/types/src/runtime/dev-server.d.ts.map +1 -0
  76. package/dist/types/src/runtime/dev-server.test.d.ts +2 -0
  77. package/dist/types/src/runtime/dev-server.test.d.ts.map +1 -0
  78. package/dist/types/src/runtime/index.d.ts +3 -0
  79. package/dist/types/src/runtime/index.d.ts.map +1 -0
  80. package/dist/types/src/runtime/scheduler.d.ts +34 -0
  81. package/dist/types/src/runtime/scheduler.d.ts.map +1 -0
  82. package/dist/types/src/runtime/scheduler.test.d.ts +2 -0
  83. package/dist/types/src/runtime/scheduler.test.d.ts.map +1 -0
  84. package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
  85. package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
  86. package/dist/types/src/testing/index.d.ts +5 -0
  87. package/dist/types/src/testing/index.d.ts.map +1 -0
  88. package/dist/types/src/testing/manifest.d.ts +3 -0
  89. package/dist/types/src/testing/manifest.d.ts.map +1 -0
  90. package/dist/types/src/testing/plugin-init.d.ts +6 -0
  91. package/dist/types/src/testing/plugin-init.d.ts.map +1 -0
  92. package/dist/types/src/testing/setup.d.ts +15 -0
  93. package/dist/types/src/testing/setup.d.ts.map +1 -0
  94. package/dist/types/src/testing/test/handler.d.ts +4 -0
  95. package/dist/types/src/testing/test/handler.d.ts.map +1 -0
  96. package/dist/types/src/testing/test/index.d.ts +3 -0
  97. package/dist/types/src/testing/test/index.d.ts.map +1 -0
  98. package/dist/types/src/testing/types.d.ts +10 -0
  99. package/dist/types/src/testing/types.d.ts.map +1 -0
  100. package/dist/types/src/testing/util.d.ts +5 -0
  101. package/dist/types/src/testing/util.d.ts.map +1 -0
  102. package/dist/types/src/translations.d.ts +1 -2
  103. package/dist/types/src/translations.d.ts.map +1 -1
  104. package/dist/types/src/trigger/index.d.ts +3 -0
  105. package/dist/types/src/trigger/index.d.ts.map +1 -0
  106. package/dist/types/src/trigger/trigger-registry.d.ts +38 -0
  107. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
  108. package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
  109. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
  110. package/dist/types/src/trigger/type/index.d.ts +3 -0
  111. package/dist/types/src/trigger/type/index.d.ts.map +1 -0
  112. package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
  113. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
  114. package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
  115. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
  116. package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
  117. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
  118. package/dist/types/src/types/index.d.ts +5 -0
  119. package/dist/types/src/types/index.d.ts.map +1 -0
  120. package/dist/types/src/types/schema.d.ts +53 -0
  121. package/dist/types/src/types/schema.d.ts.map +1 -0
  122. package/dist/types/src/types/trace.d.ts +146 -0
  123. package/dist/types/src/types/trace.d.ts.map +1 -0
  124. package/dist/types/src/types/types.d.ts +265 -0
  125. package/dist/types/src/types/types.d.ts.map +1 -0
  126. package/dist/types/src/{url.d.ts → types/url.d.ts} +0 -6
  127. package/dist/types/src/types/url.d.ts.map +1 -0
  128. package/dist/types/tools/schema.d.ts +2 -0
  129. package/dist/types/tools/schema.d.ts.map +1 -0
  130. package/dist/types/tsconfig.tsbuildinfo +1 -1
  131. package/package.json +35 -20
  132. package/schema/functions.json +211 -0
  133. package/src/browser/index.ts +5 -0
  134. package/src/edge/functions.ts +4 -7
  135. package/src/edge/index.ts +0 -4
  136. package/src/function/function-registry.test.ts +118 -0
  137. package/src/function/function-registry.ts +104 -0
  138. package/src/function/index.ts +5 -0
  139. package/src/handler.ts +124 -23
  140. package/src/index.ts +5 -4
  141. package/src/runtime/dev-server.test.ts +79 -0
  142. package/src/runtime/dev-server.ts +240 -0
  143. package/src/runtime/index.ts +6 -0
  144. package/src/runtime/scheduler.test.ts +152 -0
  145. package/src/runtime/scheduler.ts +170 -0
  146. package/src/testing/functions-integration.test.ts +65 -0
  147. package/src/testing/index.ts +8 -0
  148. package/src/testing/manifest.ts +15 -0
  149. package/src/testing/plugin-init.ts +20 -0
  150. package/src/testing/setup.ts +109 -0
  151. package/src/testing/test/handler.ts +15 -0
  152. package/src/testing/test/index.ts +7 -0
  153. package/src/testing/types.ts +9 -0
  154. package/src/testing/util.ts +26 -0
  155. package/src/translations.ts +1 -1
  156. package/src/trigger/index.ts +6 -0
  157. package/src/trigger/trigger-registry.test.ts +278 -0
  158. package/src/trigger/trigger-registry.ts +218 -0
  159. package/src/trigger/type/index.ts +7 -0
  160. package/src/trigger/type/subscription-trigger.ts +84 -0
  161. package/src/trigger/type/timer-trigger.ts +48 -0
  162. package/src/trigger/type/webhook-trigger.ts +48 -0
  163. package/src/types/index.ts +8 -0
  164. package/src/types/schema.ts +46 -0
  165. package/src/{trace.ts → types/trace.ts} +31 -33
  166. package/src/types/types.ts +163 -0
  167. package/src/{url.ts → types/url.ts} +0 -5
  168. package/dist/types/src/schema.d.ts +0 -57
  169. package/dist/types/src/schema.d.ts.map +0 -1
  170. package/dist/types/src/trace.d.ts +0 -148
  171. package/dist/types/src/trace.d.ts.map +0 -1
  172. package/dist/types/src/types.d.ts +0 -407
  173. package/dist/types/src/types.d.ts.map +0 -1
  174. package/dist/types/src/url.d.ts.map +0 -1
  175. package/src/schema.ts +0 -53
  176. package/src/types.ts +0 -210
@@ -18,44 +18,47 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var node_exports = {};
20
20
  __export(node_exports, {
21
- EmailTriggerOutput: () => EmailTriggerOutput,
22
- FUNCTIONS_PRESET_META_KEY: () => FUNCTIONS_PRESET_META_KEY,
23
- FUNCTION_TYPES: () => FUNCTION_TYPES,
24
- FunctionManifestSchema: () => FunctionManifestSchema,
25
- FunctionTrigger: () => FunctionTrigger,
26
- FunctionTriggerSchema: () => FunctionTriggerSchema,
27
- FunctionType: () => FunctionType,
28
- InvocationOutcome: () => InvocationOutcome,
29
- InvocationTraceEndEvent: () => InvocationTraceEndEvent,
30
- InvocationTraceEventType: () => InvocationTraceEventType,
31
- InvocationTraceStartEvent: () => InvocationTraceStartEvent,
32
- QueueTriggerOutput: () => QueueTriggerOutput,
33
- ScriptType: () => ScriptType,
34
- SubscriptionTriggerOutput: () => SubscriptionTriggerOutput,
35
- TimerTriggerOutput: () => TimerTriggerOutput,
36
- TraceEvent: () => TraceEvent,
37
- TraceEventException: () => TraceEventException,
38
- TraceEventLog: () => TraceEventLog,
39
- TriggerKind: () => TriggerKind,
40
- TriggerSchema: () => TriggerSchema,
41
- WebhookTriggerOutput: () => WebhookTriggerOutput,
42
- createInvocationSpans: () => createInvocationSpans,
21
+ FUNCTIONS_PRESET_META_KEY: () => import_chunk_DSUGRAAL.FUNCTIONS_PRESET_META_KEY,
22
+ FUNCTION_TYPES: () => import_chunk_DSUGRAAL.FUNCTION_TYPES,
23
+ FunctionDef: () => import_chunk_DSUGRAAL.FunctionDef,
24
+ FunctionManifestSchema: () => import_chunk_DSUGRAAL.FunctionManifestSchema,
25
+ FunctionRegistry: () => import_chunk_RXMCVAMJ.FunctionRegistry,
26
+ FunctionTrigger: () => import_chunk_DSUGRAAL.FunctionTrigger,
27
+ FunctionTriggerSchema: () => import_chunk_DSUGRAAL.FunctionTriggerSchema,
28
+ FunctionType: () => import_chunk_DSUGRAAL.FunctionType,
29
+ InvocationOutcome: () => import_chunk_DSUGRAAL.InvocationOutcome,
30
+ InvocationTraceEndEvent: () => import_chunk_DSUGRAAL.InvocationTraceEndEvent,
31
+ InvocationTraceEventType: () => import_chunk_DSUGRAAL.InvocationTraceEventType,
32
+ InvocationTraceStartEvent: () => import_chunk_DSUGRAAL.InvocationTraceStartEvent,
33
+ ScriptType: () => import_chunk_DSUGRAAL.ScriptType,
34
+ TraceEvent: () => import_chunk_DSUGRAAL.TraceEvent,
35
+ TraceEventException: () => import_chunk_DSUGRAAL.TraceEventException,
36
+ TraceEventLog: () => import_chunk_DSUGRAAL.TraceEventLog,
37
+ TriggerKind: () => import_chunk_DSUGRAAL.TriggerKind,
38
+ TriggerRegistry: () => import_chunk_RXMCVAMJ.TriggerRegistry,
39
+ TriggerSchema: () => import_chunk_DSUGRAAL.TriggerSchema,
40
+ createInvocationSpans: () => import_chunk_DSUGRAAL.createInvocationSpans,
41
+ createSubscriptionTrigger: () => import_chunk_RXMCVAMJ.createSubscriptionTrigger,
42
+ createTimerTrigger: () => import_chunk_RXMCVAMJ.createTimerTrigger,
43
43
  defineFunction: () => defineFunction,
44
- getInvocationUrl: () => getInvocationUrl,
45
- getUserFunctionUrlInMetadata: () => getUserFunctionUrlInMetadata,
46
- makeFunctionUrl: () => makeFunctionUrl,
47
- setUserFunctionUrlInMetadata: () => setUserFunctionUrlInMetadata
44
+ getInvocationUrl: () => import_chunk_DSUGRAAL.getInvocationUrl,
45
+ getUserFunctionUrlInMetadata: () => import_chunk_DSUGRAAL.getUserFunctionUrlInMetadata,
46
+ incrementSemverPatch: () => import_chunk_NXZNXVT3.incrementSemverPatch,
47
+ publicKeyToDid: () => import_chunk_NXZNXVT3.publicKeyToDid,
48
+ setUserFunctionUrlInMetadata: () => import_chunk_DSUGRAAL.setUserFunctionUrlInMetadata,
49
+ subscriptionHandler: () => subscriptionHandler,
50
+ uploadWorkerFunction: () => import_chunk_NXZNXVT3.uploadWorkerFunction
48
51
  });
49
52
  module.exports = __toCommonJS(node_exports);
53
+ var import_chunk_NXZNXVT3 = require("./chunk-NXZNXVT3.cjs");
54
+ var import_chunk_RXMCVAMJ = require("./chunk-RXMCVAMJ.cjs");
55
+ var import_chunk_DSUGRAAL = require("./chunk-DSUGRAAL.cjs");
56
+ var import_chunk_JEQ2X3Z6 = require("./chunk-JEQ2X3Z6.cjs");
50
57
  var import_effect = require("effect");
51
- var import_effect2 = require("effect");
52
- var import_echo_schema = require("@dxos/echo-schema");
53
- var import_schema = require("@dxos/schema");
54
- var import_effect3 = require("effect");
55
- var import_echo_schema2 = require("@dxos/echo-schema");
58
+ var import_client = require("@dxos/client");
56
59
  var import_log = require("@dxos/log");
57
- var import_effect4 = require("effect");
58
- var import_echo_schema3 = require("@dxos/echo-schema");
60
+ var import_util = require("@dxos/util");
61
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
59
62
  var defineFunction = (params) => {
60
63
  if (!import_effect.Schema.isSchema(params.inputSchema)) {
61
64
  throw new Error("Input schema must be a valid schema");
@@ -70,367 +73,72 @@ var defineFunction = (params) => {
70
73
  handler: params.handler
71
74
  };
72
75
  };
73
- var ScriptType = import_effect2.Schema.Struct({
74
- name: import_effect2.Schema.optional(import_effect2.Schema.String),
75
- description: import_effect2.Schema.optional(import_effect2.Schema.String),
76
- // TODO(burdon): Change to hash of deployed content.
77
- // Whether source has changed since last deploy.
78
- changed: import_effect2.Schema.optional(import_effect2.Schema.Boolean),
79
- source: (0, import_echo_schema.Ref)(import_schema.DataType.Text)
80
- }).annotations({
81
- [import_echo_schema.LabelAnnotationId]: "name"
82
- }).pipe((0, import_echo_schema.EchoObject)({
83
- typename: "dxos.org/type/Script",
84
- version: "0.1.0"
85
- }));
86
- var FunctionType = class extends (0, import_echo_schema.TypedObject)({
87
- typename: "dxos.org/type/Function",
88
- version: "0.1.0"
89
- })({
90
- // TODO(burdon): Rename to id/uri?
91
- name: import_effect2.Schema.NonEmptyString,
92
- version: import_effect2.Schema.String,
93
- description: import_effect2.Schema.optional(import_effect2.Schema.String),
94
- // Reference to a source script if it exists within ECHO.
95
- // TODO(burdon): Don't ref ScriptType directly (core).
96
- source: import_effect2.Schema.optional((0, import_echo_schema.Ref)(ScriptType)),
97
- inputSchema: import_effect2.Schema.optional(import_echo_schema.JsonSchemaType),
98
- outputSchema: import_effect2.Schema.optional(import_echo_schema.JsonSchemaType),
99
- // Local binding to a function name.
100
- binding: import_effect2.Schema.optional(import_effect2.Schema.String)
101
- }) {
102
- };
103
- var TriggerKind;
104
- (function(TriggerKind2) {
105
- TriggerKind2["Timer"] = "timer";
106
- TriggerKind2["Webhook"] = "webhook";
107
- TriggerKind2["Subscription"] = "subscription";
108
- TriggerKind2["Email"] = "email";
109
- TriggerKind2["Queue"] = "queue";
110
- })(TriggerKind || (TriggerKind = {}));
111
- var kindLiteralAnnotations = {
112
- [import_effect4.SchemaAST.TitleAnnotationId]: "Kind"
113
- };
114
- var TimerTriggerSchema = import_effect4.Schema.Struct({
115
- kind: import_effect4.Schema.Literal("timer").annotations(kindLiteralAnnotations),
116
- cron: import_effect4.Schema.String.annotations({
117
- [import_effect4.SchemaAST.TitleAnnotationId]: "Cron",
118
- [import_effect4.SchemaAST.ExamplesAnnotationId]: [
119
- "0 0 * * *"
120
- ]
121
- })
122
- }).pipe(import_effect4.Schema.mutable);
123
- var EmailTriggerSchema = import_effect4.Schema.Struct({
124
- kind: import_effect4.Schema.Literal("email").annotations(kindLiteralAnnotations)
125
- }).pipe(import_effect4.Schema.mutable);
126
- var QueueTriggerSchema = import_effect4.Schema.Struct({
127
- kind: import_effect4.Schema.Literal("queue").annotations(kindLiteralAnnotations),
128
- queue: import_echo_schema3.DXN
129
- }).pipe(import_effect4.Schema.mutable);
130
- var WebhookTriggerSchema = import_effect4.Schema.Struct({
131
- kind: import_effect4.Schema.Literal("webhook").annotations(kindLiteralAnnotations),
132
- method: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
133
- [import_effect4.SchemaAST.TitleAnnotationId]: "Method",
134
- [import_echo_schema3.OptionsAnnotationId]: [
135
- "GET",
136
- "POST"
137
- ]
138
- })),
139
- port: import_effect4.Schema.optional(import_effect4.Schema.Number.annotations({
140
- [import_effect4.SchemaAST.TitleAnnotationId]: "Port"
141
- }))
142
- }).pipe(import_effect4.Schema.mutable);
143
- var QuerySchema = import_effect4.Schema.Struct({
144
- type: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
145
- [import_effect4.SchemaAST.TitleAnnotationId]: "Type"
146
- })),
147
- props: import_effect4.Schema.optional(import_effect4.Schema.Record({
148
- key: import_effect4.Schema.String,
149
- value: import_effect4.Schema.Any
150
- }))
151
- }).annotations({
152
- [import_effect4.SchemaAST.TitleAnnotationId]: "Query"
153
- });
154
- var SubscriptionTriggerSchema = import_effect4.Schema.Struct({
155
- kind: import_effect4.Schema.Literal("subscription").annotations(kindLiteralAnnotations),
156
- // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
157
- filter: QuerySchema,
158
- options: import_effect4.Schema.optional(import_effect4.Schema.Struct({
159
- // Watch changes to object (not just creation).
160
- deep: import_effect4.Schema.optional(import_effect4.Schema.Boolean.annotations({
161
- [import_effect4.SchemaAST.TitleAnnotationId]: "Nested"
162
- })),
163
- // Debounce changes (delay in ms).
164
- delay: import_effect4.Schema.optional(import_effect4.Schema.Number.annotations({
165
- [import_effect4.SchemaAST.TitleAnnotationId]: "Delay"
166
- }))
167
- }).annotations({
168
- [import_effect4.SchemaAST.TitleAnnotationId]: "Options"
169
- }))
170
- }).pipe(import_effect4.Schema.mutable);
171
- var TriggerSchema = import_effect4.Schema.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
172
- [import_effect4.SchemaAST.TitleAnnotationId]: "Trigger"
173
- });
174
- var EmailTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
175
- from: import_effect4.Schema.String,
176
- to: import_effect4.Schema.String,
177
- subject: import_effect4.Schema.String,
178
- created: import_effect4.Schema.String,
179
- body: import_effect4.Schema.String
180
- }));
181
- var WebhookTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
182
- url: import_effect4.Schema.String,
183
- method: import_effect4.Schema.Literal("GET", "POST"),
184
- headers: import_effect4.Schema.Record({
185
- key: import_effect4.Schema.String,
186
- value: import_effect4.Schema.String
187
- }),
188
- bodyText: import_effect4.Schema.String
189
- }));
190
- var QueueTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
191
- queue: import_echo_schema3.DXN,
192
- item: import_effect4.Schema.Any,
193
- cursor: import_effect4.Schema.String
194
- }));
195
- var SubscriptionTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
196
- type: import_effect4.Schema.String,
197
- changedObjectId: import_effect4.Schema.String
198
- }));
199
- var TimerTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
200
- tick: import_effect4.Schema.Number
201
- }));
202
- var FunctionTriggerSchema = import_effect4.Schema.Struct({
203
- /**
204
- * Function or workflow to invoke.
205
- */
206
- // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
207
- function: import_effect4.Schema.optional((0, import_echo_schema3.Ref)(import_echo_schema3.Expando).annotations({
208
- [import_effect4.SchemaAST.TitleAnnotationId]: "Function"
209
- })),
210
- /**
211
- * Only used for workflowSchema.
212
- * Specifies the input node in the circuit.
213
- * @deprecated Remove and enforce a single input node in all compute graphSchema.
214
- */
215
- inputNodeId: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
216
- [import_effect4.SchemaAST.TitleAnnotationId]: "Input Node ID"
217
- })),
218
- enabled: import_effect4.Schema.optional(import_effect4.Schema.Boolean.annotations({
219
- [import_effect4.SchemaAST.TitleAnnotationId]: "Enabled"
220
- })),
221
- spec: import_effect4.Schema.optional(TriggerSchema),
222
- /**
223
- * Passed as the input data to the function.
224
- * Must match the function's input schema.
225
- *
226
- * @example
227
- * {
228
- * item: '{{$.trigger.event}}',
229
- * instructions: 'Summarize and perform entity-extraction'
230
- * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
231
- * }
232
- */
233
- input: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Record({
234
- key: import_effect4.Schema.String,
235
- value: import_effect4.Schema.Any
236
- })))
237
- });
238
- var FunctionTrigger = class extends (0, import_echo_schema3.TypedObject)({
239
- typename: "dxos.org/type/FunctionTrigger",
240
- version: "0.2.0"
241
- })(FunctionTriggerSchema.fields) {
242
- };
243
- var FunctionManifestSchema = import_effect4.Schema.Struct({
244
- functions: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Array((0, import_echo_schema3.RawObject)(FunctionType)))),
245
- triggers: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Array((0, import_echo_schema3.RawObject)(FunctionTrigger))))
246
- });
247
- var FUNCTION_TYPES = [
248
- FunctionType,
249
- FunctionTrigger
250
- ];
251
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/trace.ts";
252
- var InvocationOutcome;
253
- (function(InvocationOutcome2) {
254
- InvocationOutcome2["SUCCESS"] = "success";
255
- InvocationOutcome2["FAILURE"] = "failure";
256
- InvocationOutcome2["PENDING"] = "pending";
257
- })(InvocationOutcome || (InvocationOutcome = {}));
258
- var InvocationTraceEventType;
259
- (function(InvocationTraceEventType2) {
260
- InvocationTraceEventType2["START"] = "start";
261
- InvocationTraceEventType2["END"] = "end";
262
- })(InvocationTraceEventType || (InvocationTraceEventType = {}));
263
- var TraceEventException = import_effect3.Schema.Struct({
264
- timestampMs: import_effect3.Schema.Number,
265
- message: import_effect3.Schema.String,
266
- name: import_effect3.Schema.String,
267
- stack: import_effect3.Schema.optional(import_effect3.Schema.String)
268
- });
269
- var InvocationTraceStartEvent = import_effect3.Schema.Struct({
270
- /**
271
- * Queue message id.
272
- */
273
- id: import_echo_schema2.ObjectId,
274
- type: import_effect3.Schema.Literal("start"),
275
- /**
276
- * Invocation id, the same for invocation start and end events.
277
- */
278
- invocationId: import_echo_schema2.ObjectId,
279
- /**
280
- * Event generation time.
281
- */
282
- timestampMs: import_effect3.Schema.Number,
283
- /**
284
- * Data passed to function / workflow as an argument.
285
- */
286
- // TODO(burdon): Input schema?
287
- input: import_effect3.Schema.Object,
288
- /**
289
- * Queue DXN for function/workflow invocation events.
290
- */
291
- // TODO(burdon): Need reference type for queue. vs. string?
292
- invocationTraceQueue: (0, import_echo_schema2.Ref)(import_echo_schema2.Expando),
293
- /**
294
- * DXN of the invoked function/workflow.
295
- */
296
- invocationTarget: (0, import_echo_schema2.Ref)(import_echo_schema2.Expando),
297
- /**
298
- * Present for automatic invocations.
299
- */
300
- trigger: import_effect3.Schema.optional((0, import_echo_schema2.Ref)(FunctionTrigger))
301
- }).pipe((0, import_echo_schema2.EchoObject)({
302
- typename: "dxos.org/type/InvocationTraceStart",
303
- version: "0.1.0"
304
- }));
305
- var InvocationTraceEndEvent = import_effect3.Schema.Struct({
306
- /**
307
- * Trace event id.
308
- */
309
- id: import_echo_schema2.ObjectId,
310
- type: import_effect3.Schema.Literal("end"),
311
- /**
312
- * Invocation id, will be the same for invocation start and end.
313
- */
314
- invocationId: import_echo_schema2.ObjectId,
315
- /**
316
- * Event generation time.
317
- */
318
- // TODO(burdon): Remove ms suffix.
319
- timestampMs: import_effect3.Schema.Number,
320
- outcome: import_effect3.Schema.Enums(InvocationOutcome),
321
- exception: import_effect3.Schema.optional(TraceEventException)
322
- }).pipe((0, import_echo_schema2.EchoObject)({
323
- typename: "dxos.org/type/InvocationTraceEnd",
324
- version: "0.1.0"
325
- }));
326
- var TraceEventLog = import_effect3.Schema.Struct({
327
- timestampMs: import_effect3.Schema.Number,
328
- level: import_effect3.Schema.String,
329
- message: import_effect3.Schema.String,
330
- context: import_effect3.Schema.optional(import_effect3.Schema.Object)
331
- });
332
- var TraceEvent = import_effect3.Schema.Struct({
333
- id: import_echo_schema2.ObjectId,
334
- // TODO(burdon): Need enum/numeric result (not string).
335
- outcome: import_effect3.Schema.String,
336
- truncated: import_effect3.Schema.Boolean,
337
- /**
338
- * Time when the event was persisted.
339
- */
340
- ingestionTimestampMs: import_effect3.Schema.Number,
341
- logs: import_effect3.Schema.Array(TraceEventLog),
342
- exceptions: import_effect3.Schema.Array(TraceEventException)
343
- }).pipe((0, import_echo_schema2.EchoObject)({
344
- typename: "dxos.org/type/TraceEvent",
345
- version: "0.1.0"
346
- }));
347
- var createInvocationSpans = (items) => {
348
- if (!items) {
349
- return [];
350
- }
351
- const eventsByInvocationId = /* @__PURE__ */ new Map();
352
- for (const event of items) {
353
- if (!("invocationId" in event)) {
354
- continue;
355
- }
356
- const invocationId = event.invocationId;
357
- const entry = eventsByInvocationId.get(invocationId) || {
358
- start: void 0,
359
- end: void 0
360
- };
361
- if (event.type === "start") {
362
- entry.start = event;
363
- } else if (event.type === "end") {
364
- entry.end = event;
76
+ var subscriptionHandler = (handler, types) => {
77
+ return async ({ event: { data }, context, response, ...rest }) => {
78
+ const { client } = context;
79
+ const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
80
+ if (!space) {
81
+ import_log.log.error("Invalid space", void 0, {
82
+ F: __dxlog_file,
83
+ L: 180,
84
+ S: void 0,
85
+ C: (f, a) => f(...a)
86
+ });
87
+ return response.status(500);
365
88
  }
366
- eventsByInvocationId.set(invocationId, entry);
367
- }
368
- const now = Date.now();
369
- const result = [];
370
- for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
371
- if (!start) {
372
- import_log.log.warn("found end event without matching start", {
373
- invocationId
89
+ registerTypes(space, types);
90
+ const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util.isNonNullable) : [];
91
+ if (!!data.spaceKey && !space) {
92
+ import_log.log.warn("invalid space", {
93
+ data
94
+ }, {
95
+ F: __dxlog_file,
96
+ L: 192,
97
+ S: void 0,
98
+ C: (f, a) => f(...a)
99
+ });
100
+ } else {
101
+ import_log.log.info("handler", {
102
+ space: space?.key.truncate(),
103
+ objects: objects?.length
374
104
  }, {
375
105
  F: __dxlog_file,
376
- L: 160,
106
+ L: 194,
377
107
  S: void 0,
378
108
  C: (f, a) => f(...a)
379
109
  });
380
- continue;
381
110
  }
382
- const isInProgress = end === void 0;
383
- result.push({
384
- id: invocationId,
385
- timestampMs: start.timestampMs,
386
- durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
387
- outcome: end?.outcome ?? "pending",
388
- exception: end?.exception,
389
- input: start.input,
390
- invocationTraceQueue: start.invocationTraceQueue,
391
- invocationTarget: start.invocationTarget,
392
- trigger: start.trigger
111
+ return handler({
112
+ event: {
113
+ data: {
114
+ ...data,
115
+ space,
116
+ objects
117
+ }
118
+ },
119
+ context,
120
+ response,
121
+ ...rest
393
122
  });
394
- }
395
- return result;
396
- };
397
- var FUNCTIONS_META_KEY = "dxos.org/service/function";
398
- var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
399
- var isSecure = (protocol) => {
400
- return protocol === "https:" || protocol === "wss:";
401
- };
402
- var getUserFunctionUrlInMetadata = (meta) => {
403
- return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
123
+ };
404
124
  };
405
- var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
406
- const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
407
- if (key) {
408
- if (key.id !== functionUrl) {
409
- throw new Error("Metadata mismatch");
125
+ var registerTypes = (space, types = []) => {
126
+ const registry = space.db.graph.schemaRegistry;
127
+ for (const type of types) {
128
+ if (!registry.hasSchema(type)) {
129
+ registry.addSchema([
130
+ type
131
+ ]);
410
132
  }
411
- } else {
412
- meta.keys.push({
413
- source: FUNCTIONS_META_KEY,
414
- id: functionUrl
415
- });
416
133
  }
417
134
  };
418
- var makeFunctionUrl = (fn) => `/${fn.functionId}`;
419
- var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
420
- const baseUrl = new URL("functions/", edgeUrl);
421
- const relativeUrl = functionUrl.replace(/^\//, "");
422
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
423
- options.spaceId && url.searchParams.set("spaceId", options.spaceId);
424
- options.subjectId && url.searchParams.set("subjectId", options.subjectId);
425
- url.protocol = isSecure(url.protocol) ? "https" : "http";
426
- return url.toString();
427
- };
428
135
  // Annotate the CommonJS export names for ESM import in node:
429
136
  0 && (module.exports = {
430
- EmailTriggerOutput,
431
137
  FUNCTIONS_PRESET_META_KEY,
432
138
  FUNCTION_TYPES,
139
+ FunctionDef,
433
140
  FunctionManifestSchema,
141
+ FunctionRegistry,
434
142
  FunctionTrigger,
435
143
  FunctionTriggerSchema,
436
144
  FunctionType,
@@ -438,21 +146,23 @@ var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
438
146
  InvocationTraceEndEvent,
439
147
  InvocationTraceEventType,
440
148
  InvocationTraceStartEvent,
441
- QueueTriggerOutput,
442
149
  ScriptType,
443
- SubscriptionTriggerOutput,
444
- TimerTriggerOutput,
445
150
  TraceEvent,
446
151
  TraceEventException,
447
152
  TraceEventLog,
448
153
  TriggerKind,
154
+ TriggerRegistry,
449
155
  TriggerSchema,
450
- WebhookTriggerOutput,
451
156
  createInvocationSpans,
157
+ createSubscriptionTrigger,
158
+ createTimerTrigger,
452
159
  defineFunction,
453
160
  getInvocationUrl,
454
161
  getUserFunctionUrlInMetadata,
455
- makeFunctionUrl,
456
- setUserFunctionUrlInMetadata
162
+ incrementSemverPatch,
163
+ publicKeyToDid,
164
+ setUserFunctionUrlInMetadata,
165
+ subscriptionHandler,
166
+ uploadWorkerFunction
457
167
  });
458
168
  //# sourceMappingURL=index.cjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../src/handler.ts", "../../../src/schema.ts", "../../../src/trace.ts", "../../../src/types.ts", "../../../src/url.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { Schema } from 'effect';\nimport { type Effect } from 'effect';\n\nimport { type AIServiceClient } from '@dxos/assistant';\n// import { type Space } from '@dxos/client/echo';\nimport type { CoreDatabase, EchoDatabase } from '@dxos/echo-db';\nimport { type HasId } from '@dxos/echo-schema';\nimport { type SpaceId, type DXN } from '@dxos/keys';\nimport { type QueryResult } from '@dxos/protocols';\n\n// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.\n// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html\n// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions\n// https://www.npmjs.com/package/aws-lambda\n\n/**\n * Function handler.\n */\nexport type FunctionHandler<TData = {}, TOutput = any> = (params: {\n /**\n * Services and context available to the function.\n */\n context: FunctionContext;\n\n /**\n * Data passed as the input to the function.\n * Must match the function's input schema.\n * This will be the payload from the trigger or other data passed into the function in a workflow.\n */\n data: TData;\n}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any>;\n\n/**\n * Function context.\n */\nexport interface FunctionContext {\n getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;\n\n /**\n * Space from which the function was invoked.\n */\n space: SpaceAPI | undefined;\n\n ai: AIServiceClient;\n}\n\nexport interface FunctionContextAi {\n // TODO(dmaretskyi): Refer to cloudflare AI docs for more comprehensive typedefs.\n run(model: string, inputs: any, options?: any): Promise<any>;\n}\n\n//\n// API.\n//\n\n// TODO(dmaretskyi): Temporary API to get the queues working.\n// TODO(dmaretskyi): To be replaced with integrating queues into echo.\nexport interface QueuesAPI {\n queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;\n insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;\n}\n\n/**\n * Space interface available to functions.\n */\nexport interface SpaceAPI {\n get id(): SpaceId;\n /**\n * @deprecated\n */\n get crud(): CoreDatabase;\n get db(): EchoDatabase;\n // TODO(dmaretskyi): Align with echo api --- queues.get(id).append(items);\n get queues(): QueuesAPI;\n}\n\n// TODO(wittjosiah): Queues are incompatible.\nconst __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {\n // const _: SpaceAPI = {} as Space;\n};\n\nexport type FunctionDefinition<T = {}, O = any> = {\n description?: string;\n inputSchema: Schema.Schema<T, any>;\n outputSchema?: Schema.Schema<O, any>;\n handler: FunctionHandler<T, O>;\n};\n\n// TODO(dmaretskyi): Bind input type to function handler.\nexport const defineFunction = <T, O>(params: FunctionDefinition<T, O>): FunctionDefinition<T, O> => {\n if (!Schema.isSchema(params.inputSchema)) {\n throw new Error('Input schema must be a valid schema');\n }\n if (typeof params.handler !== 'function') {\n throw new Error('Handler must be a function');\n }\n\n return {\n description: params.description,\n inputSchema: params.inputSchema,\n outputSchema: params.outputSchema ?? Schema.Any,\n handler: params.handler,\n };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { EchoObject, JsonSchemaType, LabelAnnotationId, Ref, TypedObject } from '@dxos/echo-schema';\nimport { DataType } from '@dxos/schema';\n\n/**\n * Source script.\n */\nexport const ScriptType = Schema.Struct({\n name: Schema.optional(Schema.String),\n description: Schema.optional(Schema.String),\n // TODO(burdon): Change to hash of deployed content.\n // Whether source has changed since last deploy.\n changed: Schema.optional(Schema.Boolean),\n source: Ref(DataType.Text),\n})\n .annotations({ [LabelAnnotationId]: 'name' })\n .pipe(\n EchoObject({\n typename: 'dxos.org/type/Script',\n version: '0.1.0',\n }),\n );\n\nexport type ScriptType = Schema.Schema.Type<typeof ScriptType>;\n\n/**\n * Function deployment.\n */\nexport class FunctionType extends TypedObject({\n typename: 'dxos.org/type/Function',\n version: '0.1.0',\n})({\n // TODO(burdon): Rename to id/uri?\n name: Schema.NonEmptyString,\n version: Schema.String,\n\n description: Schema.optional(Schema.String),\n\n // Reference to a source script if it exists within ECHO.\n // TODO(burdon): Don't ref ScriptType directly (core).\n source: Schema.optional(Ref(ScriptType)),\n\n inputSchema: Schema.optional(JsonSchemaType),\n outputSchema: Schema.optional(JsonSchemaType),\n\n // Local binding to a function name.\n binding: Schema.optional(Schema.String),\n}) {}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { EchoObject, Expando, ObjectId, Ref } from '@dxos/echo-schema';\nimport { log } from '@dxos/log';\n\nimport { FunctionTrigger, type FunctionTriggerType } from './types';\n\nexport enum InvocationOutcome {\n SUCCESS = 'success',\n FAILURE = 'failure',\n PENDING = 'pending',\n}\n\n// TODO(burdon): Convert to extensible discriminated union of EDGE events.\nexport enum InvocationTraceEventType {\n START = 'start',\n END = 'end',\n}\n\nexport const TraceEventException = Schema.Struct({\n timestampMs: Schema.Number,\n message: Schema.String,\n name: Schema.String,\n stack: Schema.optional(Schema.String),\n});\nexport type TraceEventException = Schema.Schema.Type<typeof TraceEventException>;\n\nexport const InvocationTraceStartEvent = Schema.Struct({\n /**\n * Queue message id.\n */\n id: ObjectId,\n type: Schema.Literal(InvocationTraceEventType.START),\n /**\n * Invocation id, the same for invocation start and end events.\n */\n invocationId: ObjectId,\n /**\n * Event generation time.\n */\n timestampMs: Schema.Number,\n /**\n * Data passed to function / workflow as an argument.\n */\n // TODO(burdon): Input schema?\n input: Schema.Object,\n /**\n * Queue DXN for function/workflow invocation events.\n */\n // TODO(burdon): Need reference type for queue. vs. string?\n invocationTraceQueue: Ref(Expando),\n /**\n * DXN of the invoked function/workflow.\n */\n invocationTarget: Ref(Expando),\n /**\n * Present for automatic invocations.\n */\n trigger: Schema.optional(Ref(FunctionTrigger)),\n}).pipe(EchoObject({ typename: 'dxos.org/type/InvocationTraceStart', version: '0.1.0' }));\n\nexport type InvocationTraceStartEvent = Schema.Schema.Type<typeof InvocationTraceStartEvent>;\n\nexport const InvocationTraceEndEvent = Schema.Struct({\n /**\n * Trace event id.\n */\n id: ObjectId,\n type: Schema.Literal(InvocationTraceEventType.END),\n /**\n * Invocation id, will be the same for invocation start and end.\n */\n invocationId: ObjectId,\n /**\n * Event generation time.\n */\n // TODO(burdon): Remove ms suffix.\n timestampMs: Schema.Number,\n outcome: Schema.Enums(InvocationOutcome),\n exception: Schema.optional(TraceEventException),\n}).pipe(EchoObject({ typename: 'dxos.org/type/InvocationTraceEnd', version: '0.1.0' }));\n\nexport type InvocationTraceEndEvent = Schema.Schema.Type<typeof InvocationTraceEndEvent>;\n\nexport type InvocationTraceEvent = InvocationTraceStartEvent | InvocationTraceEndEvent;\n\nexport const TraceEventLog = Schema.Struct({\n timestampMs: Schema.Number,\n level: Schema.String,\n message: Schema.String,\n context: Schema.optional(Schema.Object),\n});\n\nexport const TraceEvent = Schema.Struct({\n id: ObjectId,\n // TODO(burdon): Need enum/numeric result (not string).\n outcome: Schema.String,\n truncated: Schema.Boolean,\n /**\n * Time when the event was persisted.\n */\n ingestionTimestampMs: Schema.Number,\n logs: Schema.Array(TraceEventLog),\n exceptions: Schema.Array(TraceEventException),\n}).pipe(EchoObject({ typename: 'dxos.org/type/TraceEvent', version: '0.1.0' }));\n\nexport type TraceEvent = Schema.Schema.Type<typeof TraceEvent>;\n\n/**\n * Deprecated InvocationTrace event format.\n * @deprecated\n */\n// TODO(burdon): Remove.\nexport type InvocationSpan = {\n id: string;\n timestampMs: number;\n outcome: InvocationOutcome;\n input: object;\n durationMs: number;\n invocationTraceQueue: Ref<Expando>;\n invocationTarget: Ref<Expando>;\n trigger?: Ref<FunctionTriggerType>;\n exception?: TraceEventException;\n};\n\nexport const createInvocationSpans = (items?: InvocationTraceEvent[]): InvocationSpan[] => {\n if (!items) {\n return [];\n }\n\n const eventsByInvocationId = new Map<string, { start?: InvocationTraceStartEvent; end?: InvocationTraceEndEvent }>();\n for (const event of items) {\n if (!('invocationId' in event)) {\n // Skip legacy format entries.\n continue;\n }\n\n const invocationId = event.invocationId;\n const entry = eventsByInvocationId.get(invocationId) || { start: undefined, end: undefined };\n if (event.type === InvocationTraceEventType.START) {\n entry.start = event as InvocationTraceStartEvent;\n } else if (event.type === InvocationTraceEventType.END) {\n entry.end = event as InvocationTraceEndEvent;\n }\n\n eventsByInvocationId.set(invocationId, entry);\n }\n\n const now = Date.now();\n const result: InvocationSpan[] = [];\n\n // Create spans for each invocation\n for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {\n if (!start) {\n // No start event, can't create a meaningful span\n log.warn('found end event without matching start', { invocationId });\n continue;\n }\n\n const isInProgress = end === undefined;\n\n result.push({\n id: invocationId,\n timestampMs: start.timestampMs,\n durationMs: isInProgress ? now - start.timestampMs : end!.timestampMs - start.timestampMs,\n outcome: end?.outcome ?? InvocationOutcome.PENDING,\n exception: end?.exception,\n input: start.input,\n invocationTraceQueue: start.invocationTraceQueue,\n invocationTarget: start.invocationTarget,\n trigger: start.trigger,\n });\n }\n\n return result;\n};\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { Schema, SchemaAST } from 'effect';\n\nimport { Expando, OptionsAnnotationId, TypedObject, DXN, Ref, RawObject } from '@dxos/echo-schema';\n\nimport { FunctionType } from './schema';\n\n/**\n * Type discriminator for TriggerType.\n * Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.\n * https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions\n */\nexport enum TriggerKind {\n Timer = 'timer',\n Webhook = 'webhook',\n Subscription = 'subscription',\n Email = 'email',\n Queue = 'queue',\n}\n\nconst kindLiteralAnnotations = { [SchemaAST.TitleAnnotationId]: 'Kind' };\n\n/**\n * Cron timer.\n */\nconst TimerTriggerSchema = Schema.Struct({\n kind: Schema.Literal(TriggerKind.Timer).annotations(kindLiteralAnnotations),\n cron: Schema.String.annotations({\n [SchemaAST.TitleAnnotationId]: 'Cron',\n [SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],\n }),\n}).pipe(Schema.mutable);\nexport type TimerTrigger = Schema.Schema.Type<typeof TimerTriggerSchema>;\n\nconst EmailTriggerSchema = Schema.Struct({\n kind: Schema.Literal(TriggerKind.Email).annotations(kindLiteralAnnotations),\n}).pipe(Schema.mutable);\nexport type EmailTrigger = Schema.Schema.Type<typeof EmailTriggerSchema>;\n\nconst QueueTriggerSchema = Schema.Struct({\n kind: Schema.Literal(TriggerKind.Queue).annotations(kindLiteralAnnotations),\n queue: DXN,\n}).pipe(Schema.mutable);\nexport type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;\n\n/**\n * Webhook.\n */\nconst WebhookTriggerSchema = Schema.Struct({\n kind: Schema.Literal(TriggerKind.Webhook).annotations(kindLiteralAnnotations),\n method: Schema.optional(\n Schema.String.annotations({\n [SchemaAST.TitleAnnotationId]: 'Method',\n [OptionsAnnotationId]: ['GET', 'POST'],\n }),\n ),\n port: Schema.optional(\n Schema.Number.annotations({\n [SchemaAST.TitleAnnotationId]: 'Port',\n }),\n ),\n}).pipe(Schema.mutable);\nexport type WebhookTrigger = Schema.Schema.Type<typeof WebhookTriggerSchema>;\n\n// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).\nconst QuerySchema = Schema.Struct({\n type: Schema.optional(Schema.String.annotations({ [SchemaAST.TitleAnnotationId]: 'Type' })),\n props: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),\n}).annotations({ [SchemaAST.TitleAnnotationId]: 'Query' });\n\n/**\n * Subscription.\n */\nconst SubscriptionTriggerSchema = Schema.Struct({\n kind: Schema.Literal(TriggerKind.Subscription).annotations(kindLiteralAnnotations),\n // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.\n filter: QuerySchema,\n options: Schema.optional(\n Schema.Struct({\n // Watch changes to object (not just creation).\n deep: Schema.optional(Schema.Boolean.annotations({ [SchemaAST.TitleAnnotationId]: 'Nested' })),\n // Debounce changes (delay in ms).\n delay: Schema.optional(Schema.Number.annotations({ [SchemaAST.TitleAnnotationId]: 'Delay' })),\n }).annotations({ [SchemaAST.TitleAnnotationId]: 'Options' }),\n ),\n}).pipe(Schema.mutable);\nexport type SubscriptionTrigger = Schema.Schema.Type<typeof SubscriptionTriggerSchema>;\n\n/**\n * Trigger schema (discriminated union).\n */\nexport const TriggerSchema = Schema.Union(\n TimerTriggerSchema,\n WebhookTriggerSchema,\n SubscriptionTriggerSchema,\n EmailTriggerSchema,\n QueueTriggerSchema,\n).annotations({\n [SchemaAST.TitleAnnotationId]: 'Trigger',\n});\nexport type TriggerType = Schema.Schema.Type<typeof TriggerSchema>;\n\nexport type EventType =\n | EmailTriggerOutput\n | WebhookTriggerOutput\n | QueueTriggerOutput\n | SubscriptionTriggerOutput\n | TimerTriggerOutput;\n\n// TODO(burdon): Reuse trigger schema from @dxos/functions (TriggerType).\nexport const EmailTriggerOutput = Schema.mutable(\n Schema.Struct({\n from: Schema.String,\n to: Schema.String,\n subject: Schema.String,\n created: Schema.String,\n body: Schema.String,\n }),\n);\nexport type EmailTriggerOutput = Schema.Schema.Type<typeof EmailTriggerOutput>;\n\nexport const WebhookTriggerOutput = Schema.mutable(\n Schema.Struct({\n url: Schema.String,\n method: Schema.Literal('GET', 'POST'),\n headers: Schema.Record({ key: Schema.String, value: Schema.String }),\n bodyText: Schema.String,\n }),\n);\nexport type WebhookTriggerOutput = Schema.Schema.Type<typeof WebhookTriggerOutput>;\n\nexport const QueueTriggerOutput = Schema.mutable(\n Schema.Struct({\n queue: DXN,\n item: Schema.Any,\n cursor: Schema.String,\n }),\n);\nexport type QueueTriggerOutput = Schema.Schema.Type<typeof QueueTriggerOutput>;\n\nexport const SubscriptionTriggerOutput = Schema.mutable(\n Schema.Struct({ type: Schema.String, changedObjectId: Schema.String }),\n);\nexport type SubscriptionTriggerOutput = Schema.Schema.Type<typeof SubscriptionTriggerOutput>;\n\nexport const TimerTriggerOutput = Schema.mutable(Schema.Struct({ tick: Schema.Number }));\nexport type TimerTriggerOutput = Schema.Schema.Type<typeof TimerTriggerOutput>;\n\n/**\n * Function trigger.\n * Function is invoked with the `payload` passed as input data.\n * The event that triggers the function is available in the function context.\n */\nexport const FunctionTriggerSchema = Schema.Struct({\n /**\n * Function or workflow to invoke.\n */\n // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).\n function: Schema.optional(Ref(Expando).annotations({ [SchemaAST.TitleAnnotationId]: 'Function' })),\n\n /**\n * Only used for workflowSchema.\n * Specifies the input node in the circuit.\n * @deprecated Remove and enforce a single input node in all compute graphSchema.\n */\n inputNodeId: Schema.optional(Schema.String.annotations({ [SchemaAST.TitleAnnotationId]: 'Input Node ID' })),\n\n enabled: Schema.optional(Schema.Boolean.annotations({ [SchemaAST.TitleAnnotationId]: 'Enabled' })),\n\n spec: Schema.optional(TriggerSchema),\n\n /**\n * Passed as the input data to the function.\n * Must match the function's input schema.\n *\n * @example\n * {\n * item: '{{$.trigger.event}}',\n * instructions: 'Summarize and perform entity-extraction'\n * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }\n * }\n */\n input: Schema.optional(Schema.mutable(Schema.Record({ key: Schema.String, value: Schema.Any }))),\n});\n\nexport type FunctionTriggerType = Schema.Schema.Type<typeof FunctionTriggerSchema>;\n\n/**\n * Function trigger.\n */\nexport class FunctionTrigger extends TypedObject({\n typename: 'dxos.org/type/FunctionTrigger',\n version: '0.2.0',\n})(FunctionTriggerSchema.fields) {}\n\n// TODO(wittjosiah): Remove?\n\n/**\n * Function manifest file.\n */\nexport const FunctionManifestSchema = Schema.Struct({\n functions: Schema.optional(Schema.mutable(Schema.Array(RawObject(FunctionType)))),\n triggers: Schema.optional(Schema.mutable(Schema.Array(RawObject(FunctionTrigger)))),\n});\nexport type FunctionManifest = Schema.Schema.Type<typeof FunctionManifestSchema>;\n\nexport const FUNCTION_TYPES = [FunctionType, FunctionTrigger];\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { type ObjectMeta } from '@dxos/echo-schema';\nimport { type SpaceId } from '@dxos/keys';\n\n// TODO: use URL scheme for source?\nconst FUNCTIONS_META_KEY = 'dxos.org/service/function';\n\nexport const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';\n\nconst isSecure = (protocol: string) => {\n return protocol === 'https:' || protocol === 'wss:';\n};\n\nexport const getUserFunctionUrlInMetadata = (meta: ObjectMeta) => {\n return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;\n};\n\nexport const setUserFunctionUrlInMetadata = (meta: ObjectMeta, functionUrl: string) => {\n const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);\n if (key) {\n if (key.id !== functionUrl) {\n throw new Error('Metadata mismatch');\n }\n } else {\n meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionUrl });\n }\n};\n\n/**\n * NOTE: functionId is backend ID, not ECHO object id.\n */\nexport const makeFunctionUrl = (fn: { functionId: string }) => `/${fn.functionId}`;\n\nexport const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {\n const baseUrl = new URL('functions/', edgeUrl);\n\n // Leading slashes cause the URL to be treated as an absolute path.\n const relativeUrl = functionUrl.replace(/^\\//, '');\n const url = new URL(`./${relativeUrl}`, baseUrl.toString());\n options.spaceId && url.searchParams.set('spaceId', options.spaceId);\n options.subjectId && url.searchParams.set('subjectId', options.subjectId);\n url.protocol = isSecure(url.protocol) ? 'https' : 'http';\n return url.toString();\n};\n\nexport type InvocationOptions = {\n spaceId?: SpaceId;\n subjectId?: string;\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oBAAuB;ACAvB,IAAAA,iBAAuB;AAEvB,yBAAgF;AAChF,oBAAyB;ACHzB,IAAAA,iBAAuB;AAEvB,IAAAC,sBAAmD;AACnD,iBAAoB;ACHpB,IAAAD,iBAAkC;AAElC,IAAAC,sBAA+E;AHuFxE,IAAMC,iBAAiB,CAAOC,WAAAA;AACnC,MAAI,CAACC,qBAAOC,SAASF,OAAOG,WAAW,GAAG;AACxC,UAAM,IAAIC,MAAM,qCAAA;EAClB;AACA,MAAI,OAA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AAC1B,YAAM,IAAIlJ,MAAM,mBAAA;IAClB;EACF,OAAO;AACL8I,SAAKC,KAAKR,KAAK;MAAE3H,QAAQ6H;MAAoBvC,IAAIgD;IAAY,CAAA;EAC/D;AACF;AAKO,IAAMC,kBAAkB,CAACC,OAA+B,IAAIA,GAAGC,UAAU;AAEzE,IAAMC,mBAAmB,CAACJ,aAAqBK,SAAiBjG,UAA6B,CAAC,MAAC;AACpG,QAAMkG,UAAU,IAAIC,IAAI,cAAcF,OAAAA;AAGtC,QAAMG,cAAcR,YAAYS,QAAQ,OAAO,EAAA;AAC/C,QAAMzF,MAAM,IAAIuF,IAAI,KAAKC,WAAAA,IAAeF,QAAQI,SAAQ,CAAA;AACxDtG,UAAQuG,WAAW3F,IAAI4F,aAAa/B,IAAI,WAAWzE,QAAQuG,OAAO;AAClEvG,UAAQyG,aAAa7F,IAAI4F,aAAa/B,IAAI,aAAazE,QAAQyG,SAAS;AACxE7F,MAAI0E,WAAWD,SAASzE,IAAI0E,QAAQ,IAAI,UAAU;AAClD,SAAO1E,IAAI0F,SAAQ;AACrB;",
6
- "names": ["import_effect", "import_echo_schema", "defineFunction", "params", "Schema", "isSchema", "inputSchema", "Error", "handler", "description", "outputSchema", "Any", "ScriptType", "Struct", "name", "optional", "String", "changed", "Boolean", "source", "Ref", "DataType", "Text", "annotations", "LabelAnnotationId", "pipe", "EchoObject", "typename", "version", "FunctionType", "TypedObject", "NonEmptyString", "JsonSchemaType", "binding", "TriggerKind", "kindLiteralAnnotations", "SchemaAST", "TitleAnnotationId", "TimerTriggerSchema", "kind", "Literal", "cron", "ExamplesAnnotationId", "mutable", "EmailTriggerSchema", "QueueTriggerSchema", "queue", "DXN", "WebhookTriggerSchema", "method", "OptionsAnnotationId", "port", "Number", "QuerySchema", "type", "props", "Record", "key", "value", "SubscriptionTriggerSchema", "filter", "options", "deep", "delay", "TriggerSchema", "Union", "EmailTriggerOutput", "from", "to", "subject", "created", "body", "WebhookTriggerOutput", "url", "headers", "bodyText", "QueueTriggerOutput", "item", "cursor", "SubscriptionTriggerOutput", "changedObjectId", "TimerTriggerOutput", "tick", "FunctionTriggerSchema", "function", "Expando", "inputNodeId", "enabled", "spec", "input", "FunctionTrigger", "fields", "FunctionManifestSchema", "functions", "Array", "RawObject", "triggers", "FUNCTION_TYPES", "InvocationOutcome", "InvocationTraceEventType", "TraceEventException", "timestampMs", "message", "stack", "InvocationTraceStartEvent", "id", "ObjectId", "invocationId", "Object", "invocationTraceQueue", "invocationTarget", "trigger", "InvocationTraceEndEvent", "outcome", "Enums", "exception", "TraceEventLog", "level", "context", "TraceEvent", "truncated", "ingestionTimestampMs", "logs", "exceptions", "createInvocationSpans", "items", "eventsByInvocationId", "Map", "event", "entry", "get", "start", "undefined", "end", "set", "now", "Date", "result", "entries", "log", "warn", "isInProgress", "push", "durationMs", "FUNCTIONS_META_KEY", "FUNCTIONS_PRESET_META_KEY", "isSecure", "protocol", "getUserFunctionUrlInMetadata", "meta", "keys", "find", "setUserFunctionUrlInMetadata", "functionUrl", "makeFunctionUrl", "fn", "functionId", "getInvocationUrl", "edgeUrl", "baseUrl", "URL", "relativeUrl", "replace", "toString", "spaceId", "searchParams", "subjectId"]
3
+ "sources": ["../../../src/handler.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { Schema as S } from 'effect';\nimport { type Effect } from 'effect';\n\nimport { type Client, PublicKey } from '@dxos/client';\nimport { type Space, type SpaceId } from '@dxos/client/echo';\nimport type { CoreDatabase, EchoDatabase, ReactiveEchoObject } from '@dxos/echo-db';\nimport { type HasId } from '@dxos/echo-schema';\nimport { type DXN } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type QueryResult } from '@dxos/protocols';\nimport { isNonNullable } from '@dxos/util';\n\n// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.\n// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html\n// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions\n// https://www.npmjs.com/package/aws-lambda\n\n/**\n * Function handler.\n */\nexport type FunctionHandler<TData = {}, TMeta = {}, TOutput = any> = (params: {\n context: FunctionContext;\n event: FunctionEvent<TData, TMeta>;\n /**\n * @deprecated\n */\n response: FunctionResponse;\n}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any>;\n\n/**\n * Function context.\n */\nexport interface FunctionContext {\n getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;\n\n /**\n * Space from which the function was invoked.\n */\n space: SpaceAPI | undefined;\n\n ai: FunctionContextAi;\n\n /**\n * @deprecated\n */\n // TODO(burdon): Limit access to individual space.\n client: Client;\n /**\n * @deprecated\n */\n // TODO(burdon): Replace with storage service abstraction.\n dataDir?: string;\n}\n\nexport interface FunctionContextAi {\n // TODO(dmaretskyi): Refer to cloudflare AI docs for more comprehensive typedefs.\n run(model: string, inputs: any, options?: any): Promise<any>;\n}\n\n/**\n * Event payload.\n */\n// TODO(dmaretskyi): Update type definitions to match the actual payload.\nexport type FunctionEvent<TData = {}, TMeta = {}> = {\n data: FunctionEventMeta<TMeta> & TData;\n};\n\n/**\n * Metadata from trigger.\n */\nexport type FunctionEventMeta<TMeta = {}> = {\n meta: TMeta;\n};\n\n/**\n * Function response.\n */\nexport type FunctionResponse = {\n status(code: number): FunctionResponse;\n};\n\n//\n// API.\n//\n\n// TODO(dmaretskyi): Temporary API to get the queues working.\n// TODO(dmaretskyi): To be replaced with integrating queues into echo.\nexport interface QueuesAPI {\n queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;\n insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;\n}\n\n/**\n * Space interface available to functions.\n */\nexport interface SpaceAPI {\n get id(): SpaceId;\n /**\n * @deprecated\n */\n get crud(): CoreDatabase;\n get db(): EchoDatabase;\n // TODO(dmaretskyi): Align with echo api --- queues.get(id).append(items);\n get queues(): QueuesAPI;\n}\n\n// TODO(wittjosiah): Fix this.\nconst __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {\n // const _: SpaceAPI = {} as Space;\n};\n\nexport type FunctionDefinition = {\n description?: string;\n inputSchema: S.Schema.AnyNoContext;\n outputSchema?: S.Schema.AnyNoContext;\n handler: FunctionHandler<any>;\n};\n\nexport type DefineFunctionParams<T, O = any> = {\n description?: string;\n inputSchema: S.Schema<T, any>;\n outputSchema?: S.Schema<O, any>;\n handler: FunctionHandler<T, any, O>;\n};\n\n// TODO(dmaretskyi): Bind input type to function handler.\nexport const defineFunction = <T, O>(params: DefineFunctionParams<T, O>): FunctionDefinition => {\n if (!S.isSchema(params.inputSchema)) {\n throw new Error('Input schema must be a valid schema');\n }\n if (typeof params.handler !== 'function') {\n throw new Error('Handler must be a function');\n }\n\n return {\n description: params.description,\n inputSchema: params.inputSchema,\n outputSchema: params.outputSchema ?? S.Any,\n handler: params.handler,\n };\n};\n\n//\n// Subscription utils.\n//\n\nexport type RawSubscriptionData = {\n spaceKey?: string;\n objects?: string[];\n};\n\nexport type SubscriptionData = {\n space?: Space;\n objects?: ReactiveEchoObject<any>[];\n};\n\n/**\n * Handler wrapper for subscription events; extracts space and objects.\n *\n * To test:\n * ```\n * curl -s -X POST -H \"Content-Type: application/json\" --data '{\"space\": \"0446...1cbb\"}' http://localhost:7100/dev/email-extractor\n * ```\n *\n * NOTE: Get space key from devtools or `dx space list --json`\n */\n// TODO(burdon): Evolve into plugin definition like Composer.\nexport const subscriptionHandler = <TMeta>(\n handler: FunctionHandler<SubscriptionData, TMeta>,\n types?: S.Schema.AnyNoContext[],\n): FunctionHandler<RawSubscriptionData, TMeta> => {\n return async ({ event: { data }, context, response, ...rest }) => {\n const { client } = context;\n const space = data.spaceKey ? client.spaces.get(PublicKey.from(data.spaceKey)) : undefined;\n if (!space) {\n log.error('Invalid space');\n return response.status(500);\n }\n\n registerTypes(space, types);\n const objects = space\n ? data.objects\n ?.map<ReactiveEchoObject<any> | undefined>((id) => space!.db.getObjectById(id))\n .filter(isNonNullable)\n : [];\n\n if (!!data.spaceKey && !space) {\n log.warn('invalid space', { data });\n } else {\n log.info('handler', { space: space?.key.truncate(), objects: objects?.length });\n }\n\n return handler({ event: { data: { ...data, space, objects } }, context, response, ...rest });\n };\n};\n\n// TODO(burdon): Evolve types as part of function metadata.\nconst registerTypes = (space: Space, types: S.Schema.AnyNoContext[] = []) => {\n const registry = space.db.graph.schemaRegistry;\n for (const type of types) {\n if (!registry.hasSchema(type)) {\n registry.addSchema([type]);\n }\n }\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oBAA4B;AAG5B,oBAAuC;AAKvC,iBAAoB;AAEpB,kBAA8B;;AAoHvB,IAAMA,iBAAiB,CAAOC,WAAAA;AACnC,MAAI,CAACC,cAAAA,OAAEC,SAASF,OAAOG,WAAW,GAAG;AACnC,UAAM,IAAIC,MAAM,qCAAA;EAClB;AACA,MAAI,OAAOJ,OAAOK,YAAY,YAAY;AACxC,UAAM,IAAID,MAAM,4BAAA;EAClB;AAEA,SAAO;IACLE,aAAaN,OAAOM;IACpBH,aAAaH,OAAOG;IACpBI,cAAcP,OAAOO,gBAAgBN,cAAAA,OAAEO;IACvCH,SAASL,OAAOK;EAClB;AACF;AA2BO,IAAMI,sBAAsB,CACjCJ,SACAK,UAAAA;AAEA,SAAO,OAAO,EAAEC,OAAO,EAAEC,KAAI,GAAIC,SAASC,UAAU,GAAGC,KAAAA,MAAM;AAC3D,UAAM,EAAEC,OAAM,IAAKH;AACnB,UAAMI,QAAQL,KAAKM,WAAWF,OAAOG,OAAOC,IAAIC,wBAAUC,KAAKV,KAAKM,QAAQ,CAAA,IAAKK;AACjF,QAAI,CAACN,OAAO;AACVO,qBAAIC,MAAM,iBAAA,QAAA;;;;;;AACV,aAAOX,SAASY,OAAO,GAAA;IACzB;AAEAC,kBAAcV,OAAOP,KAAAA;AACrB,UAAMkB,UAAUX,QACZL,KAAKgB,SACDC,IAAyC,CAACC,OAAOb,MAAOc,GAAGC,cAAcF,EAAAA,CAAAA,EAC1EG,OAAOC,yBAAAA,IACV,CAAA;AAEJ,QAAI,CAAC,CAACtB,KAAKM,YAAY,CAACD,OAAO;AAC7BO,qBAAIW,KAAK,iBAAiB;QAAEvB;MAAK,GAAA;;;;;;IACnC,OAAO;AACLY,qBAAIY,KAAK,WAAW;QAAEnB,OAAOA,OAAOoB,IAAIC,SAAAA;QAAYV,SAASA,SAASW;MAAO,GAAA;;;;;;IAC/E;AAEA,WAAOlC,QAAQ;MAAEM,OAAO;QAAEC,MAAM;UAAE,GAAGA;UAAMK;UAAOW;QAAQ;MAAE;MAAGf;MAASC;MAAU,GAAGC;IAAK,CAAA;EAC5F;AACF;AAGA,IAAMY,gBAAgB,CAACV,OAAcP,QAAiC,CAAA,MAAE;AACtE,QAAM8B,WAAWvB,MAAMc,GAAGU,MAAMC;AAChC,aAAWC,QAAQjC,OAAO;AACxB,QAAI,CAAC8B,SAASI,UAAUD,IAAAA,GAAO;AAC7BH,eAASK,UAAU;QAACF;OAAK;IAC3B;EACF;AACF;",
6
+ "names": ["defineFunction", "params", "S", "isSchema", "inputSchema", "Error", "handler", "description", "outputSchema", "Any", "subscriptionHandler", "types", "event", "data", "context", "response", "rest", "client", "space", "spaceKey", "spaces", "get", "PublicKey", "from", "undefined", "log", "error", "status", "registerTypes", "objects", "map", "id", "db", "getObjectById", "filter", "isNonNullable", "warn", "info", "key", "truncate", "length", "registry", "graph", "schemaRegistry", "type", "hasSchema", "addSchema"]
7
7
  }