@dxos/functions 0.8.2-main.f11618f → 0.8.2-staging.42af850

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 (204) hide show
  1. package/dist/lib/browser/bundler/index.mjs +0 -3
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/edge/index.mjs +63 -7
  4. package/dist/lib/browser/edge/index.mjs.map +4 -4
  5. package/dist/lib/browser/index.mjs +512 -101
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/node/bundler/index.cjs +0 -1
  9. package/dist/lib/node/bundler/index.cjs.map +3 -3
  10. package/dist/lib/node/edge/index.cjs +65 -5
  11. package/dist/lib/node/edge/index.cjs.map +4 -4
  12. package/dist/lib/node/index.cjs +518 -93
  13. package/dist/lib/node/index.cjs.map +4 -4
  14. package/dist/lib/node/meta.json +1 -1
  15. package/dist/lib/node-esm/bundler/index.mjs +0 -1
  16. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  17. package/dist/lib/node-esm/edge/index.mjs +64 -6
  18. package/dist/lib/node-esm/edge/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/index.mjs +512 -99
  20. package/dist/lib/node-esm/index.mjs.map +4 -4
  21. package/dist/lib/node-esm/meta.json +1 -1
  22. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  23. package/dist/types/src/edge/functions.d.ts +3 -3
  24. package/dist/types/src/edge/functions.d.ts.map +1 -1
  25. package/dist/types/src/edge/index.d.ts.map +1 -1
  26. package/dist/types/src/executor/executor.d.ts +8 -0
  27. package/dist/types/src/executor/executor.d.ts.map +1 -0
  28. package/dist/types/src/executor/index.d.ts +2 -0
  29. package/dist/types/src/executor/index.d.ts.map +1 -0
  30. package/dist/types/src/handler.d.ts +21 -66
  31. package/dist/types/src/handler.d.ts.map +1 -1
  32. package/dist/types/src/index.d.ts +5 -3
  33. package/dist/types/src/index.d.ts.map +1 -1
  34. package/dist/types/src/schema.d.ts +57 -0
  35. package/dist/types/src/schema.d.ts.map +1 -0
  36. package/dist/types/src/services/ai.d.ts +9 -0
  37. package/dist/types/src/services/ai.d.ts.map +1 -0
  38. package/dist/types/src/services/credentials.d.ts +30 -0
  39. package/dist/types/src/services/credentials.d.ts.map +1 -0
  40. package/dist/types/src/services/database.d.ts +9 -0
  41. package/dist/types/src/services/database.d.ts.map +1 -0
  42. package/dist/types/src/services/index.d.ts +7 -0
  43. package/dist/types/src/services/index.d.ts.map +1 -0
  44. package/dist/types/src/services/queues.d.ts +10 -0
  45. package/dist/types/src/services/queues.d.ts.map +1 -0
  46. package/dist/types/src/services/service-container.d.ts +25 -0
  47. package/dist/types/src/services/service-container.d.ts.map +1 -0
  48. package/dist/types/src/services/tracing.d.ts +15 -0
  49. package/dist/types/src/services/tracing.d.ts.map +1 -0
  50. package/dist/types/src/trace.d.ts +149 -0
  51. package/dist/types/src/trace.d.ts.map +1 -0
  52. package/dist/types/src/translations.d.ts +2 -1
  53. package/dist/types/src/translations.d.ts.map +1 -1
  54. package/dist/types/src/types.d.ts +407 -0
  55. package/dist/types/src/types.d.ts.map +1 -0
  56. package/dist/types/src/{types/url.d.ts → url.d.ts} +1 -1
  57. package/dist/types/src/url.d.ts.map +1 -0
  58. package/dist/types/tsconfig.tsbuildinfo +1 -1
  59. package/package.json +21 -37
  60. package/src/bundler/bundler.ts +7 -1
  61. package/src/edge/functions.ts +7 -4
  62. package/src/edge/index.ts +4 -0
  63. package/src/executor/executor.ts +47 -0
  64. package/src/executor/index.ts +5 -0
  65. package/src/handler.ts +27 -124
  66. package/src/index.ts +8 -5
  67. package/src/schema.ts +52 -0
  68. package/src/services/ai.ts +15 -0
  69. package/src/services/credentials.ts +55 -0
  70. package/src/services/database.ts +14 -0
  71. package/src/services/index.ts +10 -0
  72. package/src/services/queues.ts +16 -0
  73. package/src/services/service-container.ts +58 -0
  74. package/src/services/tracing.ts +27 -0
  75. package/src/{types/trace.ts → trace.ts} +37 -35
  76. package/src/translations.ts +1 -1
  77. package/src/types.ts +211 -0
  78. package/src/{types/url.ts → url.ts} +1 -1
  79. package/dist/lib/browser/chunk-2YE6S7XY.mjs +0 -360
  80. package/dist/lib/browser/chunk-2YE6S7XY.mjs.map +0 -7
  81. package/dist/lib/browser/chunk-7CHDHCV3.mjs +0 -482
  82. package/dist/lib/browser/chunk-7CHDHCV3.mjs.map +0 -7
  83. package/dist/lib/browser/chunk-LT4LR4VU.mjs +0 -72
  84. package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +0 -7
  85. package/dist/lib/browser/chunk-XRCXIG74.mjs +0 -12
  86. package/dist/lib/browser/chunk-XRCXIG74.mjs.map +0 -7
  87. package/dist/lib/browser/testing/index.mjs +0 -670
  88. package/dist/lib/browser/testing/index.mjs.map +0 -7
  89. package/dist/lib/browser/types/index.mjs +0 -51
  90. package/dist/lib/browser/types/index.mjs.map +0 -7
  91. package/dist/lib/node/chunk-FBIUZ7SD.cjs +0 -496
  92. package/dist/lib/node/chunk-FBIUZ7SD.cjs.map +0 -7
  93. package/dist/lib/node/chunk-JEQ2X3Z6.cjs +0 -34
  94. package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +0 -7
  95. package/dist/lib/node/chunk-NXZNXVT3.cjs +0 -94
  96. package/dist/lib/node/chunk-NXZNXVT3.cjs.map +0 -7
  97. package/dist/lib/node/chunk-SV5NRE5L.cjs +0 -395
  98. package/dist/lib/node/chunk-SV5NRE5L.cjs.map +0 -7
  99. package/dist/lib/node/testing/index.cjs +0 -687
  100. package/dist/lib/node/testing/index.cjs.map +0 -7
  101. package/dist/lib/node/types/index.cjs +0 -72
  102. package/dist/lib/node/types/index.cjs.map +0 -7
  103. package/dist/lib/node-esm/chunk-3XMJFSID.mjs +0 -360
  104. package/dist/lib/node-esm/chunk-3XMJFSID.mjs.map +0 -7
  105. package/dist/lib/node-esm/chunk-C6YINTWG.mjs +0 -482
  106. package/dist/lib/node-esm/chunk-C6YINTWG.mjs.map +0 -7
  107. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +0 -12
  108. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +0 -7
  109. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +0 -72
  110. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +0 -7
  111. package/dist/lib/node-esm/testing/index.mjs +0 -670
  112. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  113. package/dist/lib/node-esm/types/index.mjs +0 -51
  114. package/dist/lib/node-esm/types/index.mjs.map +0 -7
  115. package/dist/types/src/browser/index.d.ts +0 -2
  116. package/dist/types/src/browser/index.d.ts.map +0 -1
  117. package/dist/types/src/function/function-registry.d.ts +0 -25
  118. package/dist/types/src/function/function-registry.d.ts.map +0 -1
  119. package/dist/types/src/function/function-registry.test.d.ts +0 -2
  120. package/dist/types/src/function/function-registry.test.d.ts.map +0 -1
  121. package/dist/types/src/function/index.d.ts +0 -2
  122. package/dist/types/src/function/index.d.ts.map +0 -1
  123. package/dist/types/src/runtime/dev-server.d.ts +0 -52
  124. package/dist/types/src/runtime/dev-server.d.ts.map +0 -1
  125. package/dist/types/src/runtime/dev-server.test.d.ts +0 -2
  126. package/dist/types/src/runtime/dev-server.test.d.ts.map +0 -1
  127. package/dist/types/src/runtime/index.d.ts +0 -3
  128. package/dist/types/src/runtime/index.d.ts.map +0 -1
  129. package/dist/types/src/runtime/scheduler.d.ts +0 -34
  130. package/dist/types/src/runtime/scheduler.d.ts.map +0 -1
  131. package/dist/types/src/runtime/scheduler.test.d.ts +0 -2
  132. package/dist/types/src/runtime/scheduler.test.d.ts.map +0 -1
  133. package/dist/types/src/testing/functions-integration.test.d.ts +0 -2
  134. package/dist/types/src/testing/functions-integration.test.d.ts.map +0 -1
  135. package/dist/types/src/testing/index.d.ts +0 -5
  136. package/dist/types/src/testing/index.d.ts.map +0 -1
  137. package/dist/types/src/testing/manifest.d.ts +0 -3
  138. package/dist/types/src/testing/manifest.d.ts.map +0 -1
  139. package/dist/types/src/testing/plugin-init.d.ts +0 -6
  140. package/dist/types/src/testing/plugin-init.d.ts.map +0 -1
  141. package/dist/types/src/testing/setup.d.ts +0 -15
  142. package/dist/types/src/testing/setup.d.ts.map +0 -1
  143. package/dist/types/src/testing/test/handler.d.ts +0 -4
  144. package/dist/types/src/testing/test/handler.d.ts.map +0 -1
  145. package/dist/types/src/testing/test/index.d.ts +0 -3
  146. package/dist/types/src/testing/test/index.d.ts.map +0 -1
  147. package/dist/types/src/testing/types.d.ts +0 -10
  148. package/dist/types/src/testing/types.d.ts.map +0 -1
  149. package/dist/types/src/testing/util.d.ts +0 -5
  150. package/dist/types/src/testing/util.d.ts.map +0 -1
  151. package/dist/types/src/trigger/index.d.ts +0 -3
  152. package/dist/types/src/trigger/index.d.ts.map +0 -1
  153. package/dist/types/src/trigger/trigger-registry.d.ts +0 -38
  154. package/dist/types/src/trigger/trigger-registry.d.ts.map +0 -1
  155. package/dist/types/src/trigger/trigger-registry.test.d.ts +0 -2
  156. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +0 -1
  157. package/dist/types/src/trigger/type/index.d.ts +0 -3
  158. package/dist/types/src/trigger/type/index.d.ts.map +0 -1
  159. package/dist/types/src/trigger/type/subscription-trigger.d.ts +0 -4
  160. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +0 -1
  161. package/dist/types/src/trigger/type/timer-trigger.d.ts +0 -4
  162. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +0 -1
  163. package/dist/types/src/trigger/type/webhook-trigger.d.ts +0 -4
  164. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +0 -1
  165. package/dist/types/src/types/index.d.ts +0 -5
  166. package/dist/types/src/types/index.d.ts.map +0 -1
  167. package/dist/types/src/types/schema.d.ts +0 -53
  168. package/dist/types/src/types/schema.d.ts.map +0 -1
  169. package/dist/types/src/types/trace.d.ts +0 -146
  170. package/dist/types/src/types/trace.d.ts.map +0 -1
  171. package/dist/types/src/types/types.d.ts +0 -265
  172. package/dist/types/src/types/types.d.ts.map +0 -1
  173. package/dist/types/src/types/url.d.ts.map +0 -1
  174. package/dist/types/tools/schema.d.ts +0 -2
  175. package/dist/types/tools/schema.d.ts.map +0 -1
  176. package/schema/functions.json +0 -211
  177. package/src/browser/index.ts +0 -5
  178. package/src/function/function-registry.test.ts +0 -118
  179. package/src/function/function-registry.ts +0 -104
  180. package/src/function/index.ts +0 -5
  181. package/src/runtime/dev-server.test.ts +0 -79
  182. package/src/runtime/dev-server.ts +0 -240
  183. package/src/runtime/index.ts +0 -6
  184. package/src/runtime/scheduler.test.ts +0 -152
  185. package/src/runtime/scheduler.ts +0 -170
  186. package/src/testing/functions-integration.test.ts +0 -65
  187. package/src/testing/index.ts +0 -8
  188. package/src/testing/manifest.ts +0 -15
  189. package/src/testing/plugin-init.ts +0 -20
  190. package/src/testing/setup.ts +0 -109
  191. package/src/testing/test/handler.ts +0 -15
  192. package/src/testing/test/index.ts +0 -7
  193. package/src/testing/types.ts +0 -9
  194. package/src/testing/util.ts +0 -26
  195. package/src/trigger/index.ts +0 -6
  196. package/src/trigger/trigger-registry.test.ts +0 -278
  197. package/src/trigger/trigger-registry.ts +0 -218
  198. package/src/trigger/type/index.ts +0 -7
  199. package/src/trigger/type/subscription-trigger.ts +0 -84
  200. package/src/trigger/type/timer-trigger.ts +0 -48
  201. package/src/trigger/type/webhook-trigger.ts +0 -48
  202. package/src/types/index.ts +0 -8
  203. package/src/types/schema.ts +0 -46
  204. package/src/types/types.ts +0 -163
@@ -18,48 +18,60 @@ 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
- FUNCTIONS_PRESET_META_KEY: () => import_chunk_SV5NRE5L.FUNCTIONS_PRESET_META_KEY,
22
- FUNCTION_TYPES: () => import_chunk_SV5NRE5L.FUNCTION_TYPES,
23
- FunctionDef: () => import_chunk_SV5NRE5L.FunctionDef,
24
- FunctionManifestSchema: () => import_chunk_SV5NRE5L.FunctionManifestSchema,
25
- FunctionRegistry: () => import_chunk_FBIUZ7SD.FunctionRegistry,
26
- FunctionTrigger: () => import_chunk_SV5NRE5L.FunctionTrigger,
27
- FunctionTriggerSchema: () => import_chunk_SV5NRE5L.FunctionTriggerSchema,
28
- FunctionType: () => import_chunk_SV5NRE5L.FunctionType,
29
- InvocationOutcome: () => import_chunk_SV5NRE5L.InvocationOutcome,
30
- InvocationTraceEndEvent: () => import_chunk_SV5NRE5L.InvocationTraceEndEvent,
31
- InvocationTraceEventType: () => import_chunk_SV5NRE5L.InvocationTraceEventType,
32
- InvocationTraceStartEvent: () => import_chunk_SV5NRE5L.InvocationTraceStartEvent,
33
- ScriptType: () => import_chunk_SV5NRE5L.ScriptType,
34
- TraceEvent: () => import_chunk_SV5NRE5L.TraceEvent,
35
- TraceEventException: () => import_chunk_SV5NRE5L.TraceEventException,
36
- TraceEventLog: () => import_chunk_SV5NRE5L.TraceEventLog,
37
- TriggerKind: () => import_chunk_SV5NRE5L.TriggerKind,
38
- TriggerRegistry: () => import_chunk_FBIUZ7SD.TriggerRegistry,
39
- TriggerSchema: () => import_chunk_SV5NRE5L.TriggerSchema,
40
- createInvocationSpans: () => import_chunk_SV5NRE5L.createInvocationSpans,
41
- createSubscriptionTrigger: () => import_chunk_FBIUZ7SD.createSubscriptionTrigger,
42
- createTimerTrigger: () => import_chunk_FBIUZ7SD.createTimerTrigger,
21
+ AiService: () => AiService,
22
+ ConfiguredCredentialsService: () => ConfiguredCredentialsService,
23
+ CredentialsService: () => CredentialsService,
24
+ DatabaseService: () => DatabaseService,
25
+ EmailTriggerOutput: () => EmailTriggerOutput,
26
+ FUNCTIONS_PRESET_META_KEY: () => FUNCTIONS_PRESET_META_KEY,
27
+ FUNCTION_TYPES: () => FUNCTION_TYPES,
28
+ FunctionExecutor: () => FunctionExecutor,
29
+ FunctionManifestSchema: () => FunctionManifestSchema,
30
+ FunctionTrigger: () => FunctionTrigger,
31
+ FunctionTriggerSchema: () => FunctionTriggerSchema,
32
+ FunctionType: () => FunctionType,
33
+ InvocationOutcome: () => InvocationOutcome,
34
+ InvocationTraceEndEvent: () => InvocationTraceEndEvent,
35
+ InvocationTraceEventType: () => InvocationTraceEventType,
36
+ InvocationTraceStartEvent: () => InvocationTraceStartEvent,
37
+ QueueTriggerOutput: () => QueueTriggerOutput,
38
+ QueuesService: () => QueuesService,
39
+ ScriptType: () => ScriptType,
40
+ ServiceContainer: () => ServiceContainer,
41
+ SubscriptionTriggerOutput: () => SubscriptionTriggerOutput,
42
+ TimerTriggerOutput: () => TimerTriggerOutput,
43
+ TraceEvent: () => TraceEvent,
44
+ TraceEventException: () => TraceEventException,
45
+ TraceEventLog: () => TraceEventLog,
46
+ TracingService: () => TracingService,
47
+ TriggerKind: () => TriggerKind,
48
+ TriggerSchema: () => TriggerSchema,
49
+ WebhookTriggerOutput: () => WebhookTriggerOutput,
50
+ createInvocationSpans: () => createInvocationSpans,
43
51
  defineFunction: () => defineFunction,
44
- getInvocationUrl: () => import_chunk_SV5NRE5L.getInvocationUrl,
45
- getUserFunctionUrlInMetadata: () => import_chunk_SV5NRE5L.getUserFunctionUrlInMetadata,
46
- incrementSemverPatch: () => import_chunk_NXZNXVT3.incrementSemverPatch,
47
- makeFunctionUrl: () => import_chunk_SV5NRE5L.makeFunctionUrl,
48
- publicKeyToDid: () => import_chunk_NXZNXVT3.publicKeyToDid,
49
- setUserFunctionUrlInMetadata: () => import_chunk_SV5NRE5L.setUserFunctionUrlInMetadata,
50
- subscriptionHandler: () => subscriptionHandler,
51
- uploadWorkerFunction: () => import_chunk_NXZNXVT3.uploadWorkerFunction
52
+ getInvocationUrl: () => getInvocationUrl,
53
+ getUserFunctionUrlInMetadata: () => getUserFunctionUrlInMetadata,
54
+ makeFunctionUrl: () => makeFunctionUrl,
55
+ setUserFunctionUrlInMetadata: () => setUserFunctionUrlInMetadata
52
56
  });
53
57
  module.exports = __toCommonJS(node_exports);
54
- var import_chunk_NXZNXVT3 = require("./chunk-NXZNXVT3.cjs");
55
- var import_chunk_FBIUZ7SD = require("./chunk-FBIUZ7SD.cjs");
56
- var import_chunk_SV5NRE5L = require("./chunk-SV5NRE5L.cjs");
57
- var import_chunk_JEQ2X3Z6 = require("./chunk-JEQ2X3Z6.cjs");
58
58
  var import_effect = require("effect");
59
- var import_client = require("@dxos/client");
59
+ var import_effect2 = require("effect");
60
+ var import_echo_schema = require("@dxos/echo-schema");
61
+ var import_schema = require("@dxos/schema");
62
+ var import_effect3 = require("effect");
63
+ var import_echo_db = require("@dxos/echo-db");
64
+ var import_echo_schema2 = require("@dxos/echo-schema");
60
65
  var import_log = require("@dxos/log");
61
- var import_util = require("@dxos/util");
62
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/handler.ts";
66
+ var import_effect4 = require("effect");
67
+ var import_echo_schema3 = require("@dxos/echo-schema");
68
+ var import_keys = require("@dxos/keys");
69
+ var import_effect5 = require("effect");
70
+ var import_effect6 = require("effect");
71
+ var import_effect7 = require("effect");
72
+ var import_effect8 = require("effect");
73
+ var import_effect9 = require("effect");
74
+ var import_effect10 = require("effect");
63
75
  var defineFunction = (params) => {
64
76
  if (!import_effect.Schema.isSchema(params.inputSchema)) {
65
77
  throw new Error("Input schema must be a valid schema");
@@ -74,72 +86,485 @@ var defineFunction = (params) => {
74
86
  handler: params.handler
75
87
  };
76
88
  };
77
- var subscriptionHandler = (handler, types) => {
78
- return async ({ event: { data }, context, response, ...rest }) => {
79
- const { client } = context;
80
- const space = data.spaceKey ? client.spaces.get(import_client.PublicKey.from(data.spaceKey)) : void 0;
81
- if (!space) {
82
- import_log.log.error("Invalid space", void 0, {
83
- F: __dxlog_file,
84
- L: 181,
85
- S: void 0,
86
- C: (f, a) => f(...a)
87
- });
88
- return response.status(500);
89
+ var ScriptType = import_effect2.Schema.Struct({
90
+ name: import_effect2.Schema.optional(import_effect2.Schema.String),
91
+ description: import_effect2.Schema.optional(import_effect2.Schema.String),
92
+ // TODO(burdon): Change to hash of deployed content.
93
+ // Whether source has changed since last deploy.
94
+ changed: import_effect2.Schema.optional(import_effect2.Schema.Boolean),
95
+ source: (0, import_echo_schema.Ref)(import_schema.DataType.Text)
96
+ }).pipe((0, import_echo_schema.EchoObject)({
97
+ typename: "dxos.org/type/Script",
98
+ version: "0.1.0"
99
+ }), import_echo_schema.LabelAnnotation.set([
100
+ "name"
101
+ ]));
102
+ var FunctionType = class extends (0, import_echo_schema.TypedObject)({
103
+ typename: "dxos.org/type/Function",
104
+ version: "0.1.0"
105
+ })({
106
+ // TODO(burdon): Rename to id/uri?
107
+ name: import_effect2.Schema.NonEmptyString,
108
+ version: import_effect2.Schema.String,
109
+ description: import_effect2.Schema.optional(import_effect2.Schema.String),
110
+ // Reference to a source script if it exists within ECHO.
111
+ // TODO(burdon): Don't ref ScriptType directly (core).
112
+ source: import_effect2.Schema.optional((0, import_echo_schema.Ref)(ScriptType)),
113
+ inputSchema: import_effect2.Schema.optional(import_echo_schema.JsonSchemaType),
114
+ outputSchema: import_effect2.Schema.optional(import_echo_schema.JsonSchemaType),
115
+ // Local binding to a function name.
116
+ binding: import_effect2.Schema.optional(import_effect2.Schema.String)
117
+ }) {
118
+ };
119
+ var TriggerKind = /* @__PURE__ */ function(TriggerKind2) {
120
+ TriggerKind2["Timer"] = "timer";
121
+ TriggerKind2["Webhook"] = "webhook";
122
+ TriggerKind2["Subscription"] = "subscription";
123
+ TriggerKind2["Email"] = "email";
124
+ TriggerKind2["Queue"] = "queue";
125
+ return TriggerKind2;
126
+ }({});
127
+ var kindLiteralAnnotations = {
128
+ title: "Kind"
129
+ };
130
+ var TimerTriggerSchema = import_effect4.Schema.Struct({
131
+ kind: import_effect4.Schema.Literal("timer").annotations(kindLiteralAnnotations),
132
+ cron: import_effect4.Schema.String.annotations({
133
+ title: "Cron",
134
+ [import_effect4.SchemaAST.ExamplesAnnotationId]: [
135
+ "0 0 * * *"
136
+ ]
137
+ })
138
+ }).pipe(import_effect4.Schema.mutable);
139
+ var EmailTriggerSchema = import_effect4.Schema.Struct({
140
+ kind: import_effect4.Schema.Literal("email").annotations(kindLiteralAnnotations)
141
+ }).pipe(import_effect4.Schema.mutable);
142
+ var QueueTriggerSchema = import_effect4.Schema.Struct({
143
+ kind: import_effect4.Schema.Literal("queue").annotations(kindLiteralAnnotations),
144
+ queue: import_keys.DXN.Schema
145
+ }).pipe(import_effect4.Schema.mutable);
146
+ var WebhookTriggerSchema = import_effect4.Schema.Struct({
147
+ kind: import_effect4.Schema.Literal("webhook").annotations(kindLiteralAnnotations),
148
+ method: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
149
+ title: "Method",
150
+ [import_echo_schema3.OptionsAnnotationId]: [
151
+ "GET",
152
+ "POST"
153
+ ]
154
+ })),
155
+ port: import_effect4.Schema.optional(import_effect4.Schema.Number.annotations({
156
+ title: "Port"
157
+ }))
158
+ }).pipe(import_effect4.Schema.mutable);
159
+ var QuerySchema = import_effect4.Schema.Struct({
160
+ type: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
161
+ title: "Type"
162
+ })),
163
+ props: import_effect4.Schema.optional(import_effect4.Schema.Record({
164
+ key: import_effect4.Schema.String,
165
+ value: import_effect4.Schema.Any
166
+ }))
167
+ }).annotations({
168
+ title: "Query"
169
+ });
170
+ var SubscriptionTriggerSchema = import_effect4.Schema.Struct({
171
+ kind: import_effect4.Schema.Literal("subscription").annotations(kindLiteralAnnotations),
172
+ // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
173
+ filter: QuerySchema,
174
+ options: import_effect4.Schema.optional(import_effect4.Schema.Struct({
175
+ // Watch changes to object (not just creation).
176
+ deep: import_effect4.Schema.optional(import_effect4.Schema.Boolean.annotations({
177
+ title: "Nested"
178
+ })),
179
+ // Debounce changes (delay in ms).
180
+ delay: import_effect4.Schema.optional(import_effect4.Schema.Number.annotations({
181
+ title: "Delay"
182
+ }))
183
+ }).annotations({
184
+ title: "Options"
185
+ }))
186
+ }).pipe(import_effect4.Schema.mutable);
187
+ var TriggerSchema = import_effect4.Schema.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
188
+ title: "Trigger"
189
+ });
190
+ var EmailTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
191
+ from: import_effect4.Schema.String,
192
+ to: import_effect4.Schema.String,
193
+ subject: import_effect4.Schema.String,
194
+ created: import_effect4.Schema.String,
195
+ body: import_effect4.Schema.String
196
+ }));
197
+ var WebhookTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
198
+ url: import_effect4.Schema.String,
199
+ method: import_effect4.Schema.Literal("GET", "POST"),
200
+ headers: import_effect4.Schema.Record({
201
+ key: import_effect4.Schema.String,
202
+ value: import_effect4.Schema.String
203
+ }),
204
+ bodyText: import_effect4.Schema.String
205
+ }));
206
+ var QueueTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
207
+ queue: import_keys.DXN.Schema,
208
+ item: import_effect4.Schema.Any,
209
+ cursor: import_effect4.Schema.String
210
+ }));
211
+ var SubscriptionTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
212
+ type: import_effect4.Schema.String,
213
+ changedObjectId: import_effect4.Schema.String
214
+ }));
215
+ var TimerTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
216
+ tick: import_effect4.Schema.Number
217
+ }));
218
+ var FunctionTriggerSchema = import_effect4.Schema.Struct({
219
+ /**
220
+ * Function or workflow to invoke.
221
+ */
222
+ // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
223
+ function: import_effect4.Schema.optional((0, import_echo_schema3.Ref)(import_echo_schema3.Expando).annotations({
224
+ title: "Function"
225
+ })),
226
+ /**
227
+ * Only used for workflowSchema.
228
+ * Specifies the input node in the circuit.
229
+ * @deprecated Remove and enforce a single input node in all compute graphSchema.
230
+ */
231
+ inputNodeId: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
232
+ title: "Input Node ID"
233
+ })),
234
+ enabled: import_effect4.Schema.optional(import_effect4.Schema.Boolean.annotations({
235
+ title: "Enabled"
236
+ })),
237
+ spec: import_effect4.Schema.optional(TriggerSchema),
238
+ /**
239
+ * Passed as the input data to the function.
240
+ * Must match the function's input schema.
241
+ *
242
+ * @example
243
+ * {
244
+ * item: '{{$.trigger.event}}',
245
+ * instructions: 'Summarize and perform entity-extraction'
246
+ * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
247
+ * }
248
+ */
249
+ input: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Record({
250
+ key: import_effect4.Schema.String,
251
+ value: import_effect4.Schema.Any
252
+ })))
253
+ });
254
+ var FunctionTrigger = class extends (0, import_echo_schema3.TypedObject)({
255
+ typename: "dxos.org/type/FunctionTrigger",
256
+ version: "0.2.0"
257
+ })(FunctionTriggerSchema.fields) {
258
+ };
259
+ var FunctionManifestSchema = import_effect4.Schema.Struct({
260
+ functions: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Array((0, import_echo_schema3.RawObject)(FunctionType)))),
261
+ triggers: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Array((0, import_echo_schema3.RawObject)(FunctionTrigger))))
262
+ });
263
+ var FUNCTION_TYPES = [
264
+ FunctionType,
265
+ FunctionTrigger
266
+ ];
267
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/trace.ts";
268
+ var InvocationOutcome = /* @__PURE__ */ function(InvocationOutcome2) {
269
+ InvocationOutcome2["SUCCESS"] = "success";
270
+ InvocationOutcome2["FAILURE"] = "failure";
271
+ InvocationOutcome2["PENDING"] = "pending";
272
+ return InvocationOutcome2;
273
+ }({});
274
+ var InvocationTraceEventType = /* @__PURE__ */ function(InvocationTraceEventType2) {
275
+ InvocationTraceEventType2["START"] = "start";
276
+ InvocationTraceEventType2["END"] = "end";
277
+ return InvocationTraceEventType2;
278
+ }({});
279
+ var TraceEventException = import_effect3.Schema.Struct({
280
+ timestampMs: import_effect3.Schema.Number,
281
+ message: import_effect3.Schema.String,
282
+ name: import_effect3.Schema.String,
283
+ stack: import_effect3.Schema.optional(import_effect3.Schema.String)
284
+ });
285
+ var InvocationTraceStartEvent = import_effect3.Schema.Struct({
286
+ /**
287
+ * Queue message id.
288
+ */
289
+ id: import_echo_schema2.ObjectId,
290
+ type: import_effect3.Schema.Literal("start"),
291
+ /**
292
+ * Invocation id, the same for invocation start and end events.
293
+ */
294
+ invocationId: import_echo_schema2.ObjectId,
295
+ /**
296
+ * Event generation time.
297
+ */
298
+ timestampMs: import_effect3.Schema.Number,
299
+ /**
300
+ * Data passed to function / workflow as an argument.
301
+ */
302
+ // TODO(burdon): Input schema?
303
+ input: import_effect3.Schema.Object,
304
+ /**
305
+ * Queue for function/workflow invocation events.
306
+ */
307
+ invocationTraceQueue: (0, import_echo_schema2.Ref)(import_echo_db.Queue),
308
+ /**
309
+ * DXN of the invoked function/workflow.
310
+ */
311
+ invocationTarget: (0, import_echo_schema2.Ref)(import_echo_schema2.Expando),
312
+ /**
313
+ * Present for automatic invocations.
314
+ */
315
+ trigger: import_effect3.Schema.optional((0, import_echo_schema2.Ref)(FunctionTrigger))
316
+ }).pipe((0, import_echo_schema2.EchoObject)({
317
+ typename: "dxos.org/type/InvocationTraceStart",
318
+ version: "0.1.0"
319
+ }));
320
+ var InvocationTraceEndEvent = import_effect3.Schema.Struct({
321
+ /**
322
+ * Trace event id.
323
+ */
324
+ id: import_echo_schema2.ObjectId,
325
+ type: import_effect3.Schema.Literal("end"),
326
+ /**
327
+ * Invocation id, will be the same for invocation start and end.
328
+ */
329
+ invocationId: import_echo_schema2.ObjectId,
330
+ /**
331
+ * Event generation time.
332
+ */
333
+ // TODO(burdon): Remove ms suffix.
334
+ timestampMs: import_effect3.Schema.Number,
335
+ outcome: import_effect3.Schema.Enums(InvocationOutcome),
336
+ exception: import_effect3.Schema.optional(TraceEventException)
337
+ }).pipe((0, import_echo_schema2.EchoObject)({
338
+ typename: "dxos.org/type/InvocationTraceEnd",
339
+ version: "0.1.0"
340
+ }));
341
+ var TraceEventLog = import_effect3.Schema.Struct({
342
+ timestampMs: import_effect3.Schema.Number,
343
+ level: import_effect3.Schema.String,
344
+ message: import_effect3.Schema.String,
345
+ context: import_effect3.Schema.optional(import_effect3.Schema.Object)
346
+ });
347
+ var TraceEvent = import_effect3.Schema.Struct({
348
+ id: import_echo_schema2.ObjectId,
349
+ // TODO(burdon): Need enum/numeric result (not string).
350
+ outcome: import_effect3.Schema.String,
351
+ truncated: import_effect3.Schema.Boolean,
352
+ /**
353
+ * Time when the event was persisted.
354
+ */
355
+ ingestionTimestampMs: import_effect3.Schema.Number,
356
+ logs: import_effect3.Schema.Array(TraceEventLog),
357
+ exceptions: import_effect3.Schema.Array(TraceEventException)
358
+ }).pipe((0, import_echo_schema2.EchoObject)({
359
+ typename: "dxos.org/type/TraceEvent",
360
+ version: "0.1.0"
361
+ }));
362
+ var createInvocationSpans = (items) => {
363
+ if (!items) {
364
+ return [];
365
+ }
366
+ const eventsByInvocationId = /* @__PURE__ */ new Map();
367
+ for (const event of items) {
368
+ if (!("invocationId" in event)) {
369
+ continue;
89
370
  }
90
- registerTypes(space, types);
91
- const objects = space ? data.objects?.map((id) => space.db.getObjectById(id)).filter(import_util.isNonNullable) : [];
92
- if (!!data.spaceKey && !space) {
93
- import_log.log.warn("invalid space", {
94
- data
95
- }, {
96
- F: __dxlog_file,
97
- L: 193,
98
- S: void 0,
99
- C: (f, a) => f(...a)
100
- });
101
- } else {
102
- import_log.log.info("handler", {
103
- space: space?.key.truncate(),
104
- objects: objects?.length
371
+ const invocationId = event.invocationId;
372
+ const entry = eventsByInvocationId.get(invocationId) || {
373
+ start: void 0,
374
+ end: void 0
375
+ };
376
+ if (event.type === "start") {
377
+ entry.start = event;
378
+ } else if (event.type === "end") {
379
+ entry.end = event;
380
+ }
381
+ eventsByInvocationId.set(invocationId, entry);
382
+ }
383
+ const now = Date.now();
384
+ const result = [];
385
+ for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
386
+ if (!start) {
387
+ import_log.log.warn("found end event without matching start", {
388
+ invocationId
105
389
  }, {
106
390
  F: __dxlog_file,
107
- L: 195,
391
+ L: 160,
108
392
  S: void 0,
109
393
  C: (f, a) => f(...a)
110
394
  });
395
+ continue;
111
396
  }
112
- return handler({
113
- event: {
114
- data: {
115
- ...data,
116
- space,
117
- objects
118
- }
397
+ const isInProgress = end === void 0;
398
+ result.push({
399
+ id: invocationId,
400
+ timestampMs: start.timestampMs,
401
+ durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
402
+ outcome: end?.outcome ?? "pending",
403
+ exception: end?.exception,
404
+ input: start.input,
405
+ invocationTraceQueue: start.invocationTraceQueue,
406
+ invocationTarget: start.invocationTarget,
407
+ trigger: start.trigger
408
+ });
409
+ }
410
+ return result;
411
+ };
412
+ var FUNCTIONS_META_KEY = "dxos.org/service/function";
413
+ var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
414
+ var isSecure = (protocol) => {
415
+ return protocol === "https:" || protocol === "wss:";
416
+ };
417
+ var getUserFunctionUrlInMetadata = (meta) => {
418
+ return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
419
+ };
420
+ var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
421
+ const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
422
+ if (key) {
423
+ if (key.id !== functionUrl) {
424
+ throw new Error("Metadata mismatch");
425
+ }
426
+ } else {
427
+ meta.keys.push({
428
+ source: FUNCTIONS_META_KEY,
429
+ id: functionUrl
430
+ });
431
+ }
432
+ };
433
+ var makeFunctionUrl = (fn) => `/${fn.functionId}`;
434
+ var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
435
+ const baseUrl = new URL("functions/", edgeUrl);
436
+ const relativeUrl = functionUrl.replace(/^\//, "");
437
+ const url = new URL(`./${relativeUrl}`, baseUrl.toString());
438
+ options.spaceId && url.searchParams.set("spaceId", options.spaceId);
439
+ options.subjectId && url.searchParams.set("subjectId", options.subjectId);
440
+ url.protocol = isSecure(url.protocol) ? "https" : "http";
441
+ return url.toString();
442
+ };
443
+ var AiService = class extends import_effect5.Context.Tag("AiService")() {
444
+ };
445
+ var DatabaseService = class extends import_effect6.Context.Tag("DatabaseService")() {
446
+ };
447
+ var QueuesService = class extends import_effect7.Context.Tag("QueuesService")() {
448
+ };
449
+ var CredentialsService = class extends import_effect8.Context.Tag("CredentialsService")() {
450
+ };
451
+ var ConfiguredCredentialsService = class {
452
+ constructor(credentials = []) {
453
+ this.credentials = credentials;
454
+ }
455
+ addCredentials(credentials) {
456
+ this.credentials.push(...credentials);
457
+ return this;
458
+ }
459
+ async queryCredentials(query) {
460
+ return this.credentials.filter((credential) => credential.service === query.service);
461
+ }
462
+ async getCredential(query) {
463
+ const credential = this.credentials.find((credential2) => credential2.service === query.service);
464
+ if (!credential) {
465
+ throw new Error(`Credential not found for service: ${query.service}`);
466
+ }
467
+ return credential;
468
+ }
469
+ };
470
+ var TracingService = class extends import_effect9.Context.Tag("TracingService")() {
471
+ static {
472
+ this.noop = {
473
+ write: () => {
474
+ }
475
+ };
476
+ }
477
+ static {
478
+ this.console = {
479
+ write: (event) => {
480
+ console.log(event);
481
+ }
482
+ };
483
+ }
484
+ };
485
+ var SERVICE_MAPPING = {
486
+ [DatabaseService.key]: "database",
487
+ [AiService.key]: "ai",
488
+ [QueuesService.key]: "queues",
489
+ [CredentialsService.key]: "credentials",
490
+ [TracingService.key]: "tracing"
491
+ };
492
+ var DEFAULT_SERVICES = {
493
+ tracing: TracingService.noop
494
+ };
495
+ var ServiceContainer = class _ServiceContainer {
496
+ constructor() {
497
+ this._services = {
498
+ ...DEFAULT_SERVICES
499
+ };
500
+ }
501
+ /**
502
+ * Set services.
503
+ * @param services - Services to set.
504
+ * @returns The container instance.
505
+ */
506
+ setServices(services) {
507
+ this._services = {
508
+ ...this._services,
509
+ ...services
510
+ };
511
+ return this;
512
+ }
513
+ getService(tag) {
514
+ const serviceKey = SERVICE_MAPPING[tag.key];
515
+ const service = serviceKey != null ? this._services[serviceKey] : void 0;
516
+ if (!service) {
517
+ throw new Error(`Service not available: ${tag.key}`);
518
+ }
519
+ return service;
520
+ }
521
+ clone() {
522
+ return new _ServiceContainer().setServices({
523
+ ...this._services
524
+ });
525
+ }
526
+ };
527
+ var FunctionExecutor = class {
528
+ constructor(_services) {
529
+ this._services = _services;
530
+ }
531
+ // TODO(dmaretskyi): Invocation context: queue, space, etc...
532
+ async invoke(fnDef, input) {
533
+ const assertInput = fnDef.inputSchema.pipe(import_effect10.Schema.asserts);
534
+ assertInput(input);
535
+ const context = {
536
+ getService: this._services.getService.bind(this._services),
537
+ getSpace: async (_spaceId) => {
538
+ throw new Error("Not available. Use the database service instead.");
119
539
  },
540
+ space: void 0,
541
+ get ai() {
542
+ throw new Error("Not available. Use the ai service instead.");
543
+ }
544
+ };
545
+ const result = await fnDef.handler({
120
546
  context,
121
- response,
122
- ...rest
547
+ data: input
123
548
  });
124
- };
125
- };
126
- var registerTypes = (space, types = []) => {
127
- const registry = space.db.graph.schemaRegistry;
128
- for (const type of types) {
129
- if (!registry.hasSchema(type)) {
130
- registry.addSchema([
131
- type
132
- ]);
549
+ const assertOutput = fnDef.outputSchema?.pipe(import_effect10.Schema.asserts);
550
+ assertOutput(result);
551
+ if (import_effect10.Effect.isEffect(result)) {
552
+ return import_effect10.Effect.runPromise(result);
133
553
  }
554
+ return result;
134
555
  }
135
556
  };
136
557
  // Annotate the CommonJS export names for ESM import in node:
137
558
  0 && (module.exports = {
559
+ AiService,
560
+ ConfiguredCredentialsService,
561
+ CredentialsService,
562
+ DatabaseService,
563
+ EmailTriggerOutput,
138
564
  FUNCTIONS_PRESET_META_KEY,
139
565
  FUNCTION_TYPES,
140
- FunctionDef,
566
+ FunctionExecutor,
141
567
  FunctionManifestSchema,
142
- FunctionRegistry,
143
568
  FunctionTrigger,
144
569
  FunctionTriggerSchema,
145
570
  FunctionType,
@@ -147,24 +572,24 @@ var registerTypes = (space, types = []) => {
147
572
  InvocationTraceEndEvent,
148
573
  InvocationTraceEventType,
149
574
  InvocationTraceStartEvent,
575
+ QueueTriggerOutput,
576
+ QueuesService,
150
577
  ScriptType,
578
+ ServiceContainer,
579
+ SubscriptionTriggerOutput,
580
+ TimerTriggerOutput,
151
581
  TraceEvent,
152
582
  TraceEventException,
153
583
  TraceEventLog,
584
+ TracingService,
154
585
  TriggerKind,
155
- TriggerRegistry,
156
586
  TriggerSchema,
587
+ WebhookTriggerOutput,
157
588
  createInvocationSpans,
158
- createSubscriptionTrigger,
159
- createTimerTrigger,
160
589
  defineFunction,
161
590
  getInvocationUrl,
162
591
  getUserFunctionUrlInMetadata,
163
- incrementSemverPatch,
164
592
  makeFunctionUrl,
165
- publicKeyToDid,
166
- setUserFunctionUrlInMetadata,
167
- subscriptionHandler,
168
- uploadWorkerFunction
593
+ setUserFunctionUrlInMetadata
169
594
  });
170
595
  //# sourceMappingURL=index.cjs.map