@dxos/functions 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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 (180) hide show
  1. package/dist/lib/browser/bundler/index.mjs +43 -34
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-M6EXIREF.mjs +610 -0
  6. package/dist/lib/browser/chunk-M6EXIREF.mjs.map +7 -0
  7. package/dist/lib/browser/edge/index.mjs +22 -8
  8. package/dist/lib/browser/edge/index.mjs.map +3 -3
  9. package/dist/lib/browser/index.mjs +1106 -252
  10. package/dist/lib/browser/index.mjs.map +4 -4
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/testing/index.mjs +94 -42
  13. package/dist/lib/browser/testing/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/bundler/index.mjs +42 -34
  15. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  16. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  17. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-P3IATZMZ.mjs +612 -0
  19. package/dist/lib/node-esm/chunk-P3IATZMZ.mjs.map +7 -0
  20. package/dist/lib/node-esm/edge/index.mjs +21 -8
  21. package/dist/lib/node-esm/edge/index.mjs.map +3 -3
  22. package/dist/lib/node-esm/index.mjs +1106 -252
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +94 -42
  26. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  27. package/dist/types/src/bundler/bundler.d.ts +11 -12
  28. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  29. package/dist/types/src/e2e/deploy.test.d.ts +2 -0
  30. package/dist/types/src/e2e/deploy.test.d.ts.map +1 -0
  31. package/dist/types/src/edge/functions.d.ts +4 -3
  32. package/dist/types/src/edge/functions.d.ts.map +1 -1
  33. package/dist/types/src/errors.d.ts +137 -0
  34. package/dist/types/src/errors.d.ts.map +1 -0
  35. package/dist/types/src/example/fib.d.ts +7 -0
  36. package/dist/types/src/example/fib.d.ts.map +1 -0
  37. package/dist/types/src/example/forex-effect.d.ts +3 -0
  38. package/dist/types/src/example/forex-effect.d.ts.map +1 -0
  39. package/dist/types/src/example/index.d.ts +12 -0
  40. package/dist/types/src/example/index.d.ts.map +1 -0
  41. package/dist/types/src/example/reply.d.ts +3 -0
  42. package/dist/types/src/example/reply.d.ts.map +1 -0
  43. package/dist/types/src/example/sleep.d.ts +5 -0
  44. package/dist/types/src/example/sleep.d.ts.map +1 -0
  45. package/dist/types/src/executor/executor.d.ts +7 -1
  46. package/dist/types/src/executor/executor.d.ts.map +1 -1
  47. package/dist/types/src/handler.d.ts +54 -13
  48. package/dist/types/src/handler.d.ts.map +1 -1
  49. package/dist/types/src/index.d.ts +5 -3
  50. package/dist/types/src/index.d.ts.map +1 -1
  51. package/dist/types/src/services/credentials.d.ts +21 -3
  52. package/dist/types/src/services/credentials.d.ts.map +1 -1
  53. package/dist/types/src/services/database.d.ts +58 -6
  54. package/dist/types/src/services/database.d.ts.map +1 -1
  55. package/dist/types/src/services/event-logger.d.ts +68 -30
  56. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  57. package/dist/types/src/services/function-invocation-service.d.ts +28 -0
  58. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
  59. package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
  60. package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
  61. package/dist/types/src/services/index.d.ts +5 -5
  62. package/dist/types/src/services/index.d.ts.map +1 -1
  63. package/dist/types/src/services/local-function-execution.d.ts +34 -0
  64. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  65. package/dist/types/src/services/queues.d.ts +33 -4
  66. package/dist/types/src/services/queues.d.ts.map +1 -1
  67. package/dist/types/src/services/remote-function-execution-service.d.ts +22 -0
  68. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  69. package/dist/types/src/services/service-container.d.ts +29 -18
  70. package/dist/types/src/services/service-container.d.ts.map +1 -1
  71. package/dist/types/src/services/service-registry.d.ts +31 -0
  72. package/dist/types/src/services/service-registry.d.ts.map +1 -0
  73. package/dist/types/src/services/service-registry.test.d.ts +2 -0
  74. package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
  75. package/dist/types/src/services/tracing.d.ts +48 -4
  76. package/dist/types/src/services/tracing.d.ts.map +1 -1
  77. package/dist/types/src/testing/index.d.ts +1 -0
  78. package/dist/types/src/testing/index.d.ts.map +1 -1
  79. package/dist/types/src/testing/layer.d.ts +18 -0
  80. package/dist/types/src/testing/layer.d.ts.map +1 -0
  81. package/dist/types/src/testing/logger.d.ts +4 -4
  82. package/dist/types/src/testing/logger.d.ts.map +1 -1
  83. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  84. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  85. package/dist/types/src/testing/services.d.ts +8 -20
  86. package/dist/types/src/testing/services.d.ts.map +1 -1
  87. package/dist/types/src/trace.d.ts +21 -23
  88. package/dist/types/src/trace.d.ts.map +1 -1
  89. package/dist/types/src/translations.d.ts +2 -2
  90. package/dist/types/src/translations.d.ts.map +1 -1
  91. package/dist/types/src/triggers/index.d.ts +4 -0
  92. package/dist/types/src/triggers/index.d.ts.map +1 -0
  93. package/dist/types/src/triggers/input-builder.d.ts +3 -0
  94. package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
  95. package/dist/types/src/triggers/invocation-tracer.d.ts +37 -0
  96. package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
  97. package/dist/types/src/triggers/trigger-dispatcher.d.ts +78 -0
  98. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
  99. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
  100. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
  101. package/dist/types/src/triggers/trigger-state-store.d.ts +28 -0
  102. package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
  103. package/dist/types/src/types/Function.d.ts +47 -0
  104. package/dist/types/src/types/Function.d.ts.map +1 -0
  105. package/dist/types/src/types/Script.d.ts +28 -0
  106. package/dist/types/src/types/Script.d.ts.map +1 -0
  107. package/dist/types/src/types/Trigger.d.ts +139 -0
  108. package/dist/types/src/types/Trigger.d.ts.map +1 -0
  109. package/dist/types/src/types/TriggerEvent.d.ts +44 -0
  110. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
  111. package/dist/types/src/types/index.d.ts +5 -0
  112. package/dist/types/src/types/index.d.ts.map +1 -0
  113. package/dist/types/src/url.d.ts +11 -7
  114. package/dist/types/src/url.d.ts.map +1 -1
  115. package/dist/types/tsconfig.tsbuildinfo +1 -1
  116. package/package.json +37 -42
  117. package/src/bundler/bundler.test.ts +8 -9
  118. package/src/bundler/bundler.ts +38 -35
  119. package/src/e2e/deploy.test.ts +69 -0
  120. package/src/edge/functions.ts +9 -6
  121. package/src/errors.ts +21 -0
  122. package/src/example/fib.ts +32 -0
  123. package/src/example/forex-effect.ts +40 -0
  124. package/src/example/index.ts +13 -0
  125. package/src/example/reply.ts +21 -0
  126. package/src/example/sleep.ts +24 -0
  127. package/src/executor/executor.ts +14 -10
  128. package/src/handler.ts +139 -26
  129. package/src/index.ts +5 -5
  130. package/src/services/credentials.ts +93 -4
  131. package/src/services/database.ts +145 -20
  132. package/src/services/event-logger.ts +71 -37
  133. package/src/services/function-invocation-service.test.ts +81 -0
  134. package/src/services/function-invocation-service.ts +84 -0
  135. package/src/services/index.ts +5 -5
  136. package/src/services/local-function-execution.ts +153 -0
  137. package/src/services/queues.ts +50 -8
  138. package/src/services/remote-function-execution-service.ts +63 -0
  139. package/src/services/service-container.ts +46 -58
  140. package/src/services/service-registry.test.ts +45 -0
  141. package/src/services/service-registry.ts +63 -0
  142. package/src/services/tracing.ts +122 -6
  143. package/src/testing/index.ts +1 -0
  144. package/src/testing/layer.ts +114 -0
  145. package/src/testing/logger.ts +5 -4
  146. package/src/testing/persist-database.test.ts +87 -0
  147. package/src/testing/services.ts +12 -71
  148. package/src/trace.ts +20 -22
  149. package/src/translations.ts +2 -2
  150. package/src/triggers/index.ts +7 -0
  151. package/src/triggers/input-builder.ts +35 -0
  152. package/src/triggers/invocation-tracer.ts +101 -0
  153. package/src/triggers/trigger-dispatcher.test.ts +664 -0
  154. package/src/triggers/trigger-dispatcher.ts +521 -0
  155. package/src/triggers/trigger-state-store.ts +61 -0
  156. package/src/types/Function.ts +51 -0
  157. package/src/types/Script.ts +33 -0
  158. package/src/types/Trigger.ts +139 -0
  159. package/src/types/TriggerEvent.ts +62 -0
  160. package/src/types/index.ts +8 -0
  161. package/src/url.ts +14 -11
  162. package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
  163. package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
  164. package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
  165. package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
  166. package/dist/types/src/schema.d.ts +0 -38
  167. package/dist/types/src/schema.d.ts.map +0 -1
  168. package/dist/types/src/services/ai.d.ts +0 -12
  169. package/dist/types/src/services/ai.d.ts.map +0 -1
  170. package/dist/types/src/services/function-call-service.d.ts +0 -16
  171. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  172. package/dist/types/src/services/tool-resolver.d.ts +0 -14
  173. package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
  174. package/dist/types/src/types.d.ts +0 -411
  175. package/dist/types/src/types.d.ts.map +0 -1
  176. package/src/schema.ts +0 -57
  177. package/src/services/ai.ts +0 -32
  178. package/src/services/function-call-service.ts +0 -64
  179. package/src/services/tool-resolver.ts +0 -31
  180. package/src/types.ts +0 -211
@@ -1,97 +1,172 @@
1
1
  import {
2
- AiService,
2
+ ComputeEvent,
3
+ ComputeEventLogger,
4
+ ComputeEventPayload,
3
5
  ConfiguredCredentialsService,
6
+ ContextQueueService,
4
7
  CredentialsService,
5
8
  DatabaseService,
6
- EventLogger,
7
- FunctionCallService,
9
+ FUNCTIONS_META_KEY,
10
+ FUNCTIONS_PRESET_META_KEY,
11
+ FunctionError,
12
+ FunctionNotFoundError,
13
+ MESSAGE_PROPERTY_TOOL_CALL_ID,
8
14
  QueueService,
15
+ RemoteFunctionExecutionService,
9
16
  SERVICE_TAGS,
10
17
  ServiceContainer,
11
- ToolResolverService,
18
+ ServiceNotAvailableError,
12
19
  TracingService,
20
+ TriggerStateNotFoundError,
13
21
  createDefectLogger,
14
22
  createEventLogger,
15
- logCustomEvent
16
- } from "./chunk-54U464M4.mjs";
23
+ getInvocationUrl,
24
+ getUserFunctionIdInMetadata,
25
+ logCustomEvent,
26
+ setUserFunctionIdInMetadata,
27
+ withAuthorization
28
+ } from "./chunk-M6EXIREF.mjs";
29
+ import {
30
+ __export
31
+ } from "./chunk-J5LGTIGS.mjs";
32
+
33
+ // src/example/fib.ts
34
+ import * as Effect2 from "effect/Effect";
35
+ import * as Schema6 from "effect/Schema";
17
36
 
18
37
  // src/handler.ts
19
- import { Schema } from "effect";
20
- var defineFunction = (params) => {
21
- if (!Schema.isSchema(params.inputSchema)) {
22
- throw new Error("Input schema must be a valid schema");
23
- }
24
- if (typeof params.handler !== "function") {
25
- throw new Error("Handler must be a function");
26
- }
27
- return {
28
- description: params.description,
29
- inputSchema: params.inputSchema,
30
- outputSchema: params.outputSchema ?? Schema.Any,
31
- handler: params.handler
32
- };
33
- };
38
+ import * as Effect from "effect/Effect";
39
+ import * as Schema5 from "effect/Schema";
40
+ import { Obj as Obj5, Type as Type5 } from "@dxos/echo";
41
+ import { assertArgument } from "@dxos/invariant";
42
+
43
+ // src/types/Function.ts
44
+ var Function_exports = {};
45
+ __export(Function_exports, {
46
+ Function: () => Function,
47
+ make: () => make2
48
+ });
49
+ import * as Schema2 from "effect/Schema";
50
+ import { Obj as Obj2, Type as Type2 } from "@dxos/echo";
51
+ import { JsonSchemaType, LabelAnnotation as LabelAnnotation2, Ref as Ref2 } from "@dxos/echo/internal";
34
52
 
35
- // src/schema.ts
36
- import { Schema as Schema2 } from "effect";
37
- import { Type } from "@dxos/echo";
38
- import { JsonSchemaType, LabelAnnotation, Ref } from "@dxos/echo-schema";
53
+ // src/types/Script.ts
54
+ var Script_exports = {};
55
+ __export(Script_exports, {
56
+ Script: () => Script,
57
+ make: () => make
58
+ });
59
+ import * as Schema from "effect/Schema";
60
+ import { Obj, Ref, Type } from "@dxos/echo";
61
+ import { FormAnnotation, LabelAnnotation } from "@dxos/echo/internal";
39
62
  import { DataType } from "@dxos/schema";
40
- var ScriptType = Schema2.Struct({
41
- name: Schema2.optional(Schema2.String),
42
- description: Schema2.optional(Schema2.String),
63
+ var Script = Schema.Struct({
64
+ name: Schema.String.pipe(Schema.optional),
65
+ description: Schema.String.pipe(Schema.optional),
43
66
  // TODO(burdon): Change to hash of deployed content.
44
67
  // Whether source has changed since last deploy.
45
- changed: Schema2.optional(Schema2.Boolean),
46
- source: Ref(DataType.Text)
68
+ changed: Schema.Boolean.pipe(FormAnnotation.set(false), Schema.optional),
69
+ source: Type.Ref(DataType.Text).pipe(FormAnnotation.set(false))
47
70
  }).pipe(Type.Obj({
48
71
  typename: "dxos.org/type/Script",
49
72
  version: "0.1.0"
50
73
  }), LabelAnnotation.set([
51
74
  "name"
52
75
  ]));
53
- var FunctionType = Schema2.Struct({
76
+ var make = ({ source = "", ...props } = {}) => Obj.make(Script, {
77
+ ...props,
78
+ source: Ref.make(DataType.makeText(source))
79
+ });
80
+
81
+ // src/types/Function.ts
82
+ var Function = Schema2.Struct({
83
+ /**
84
+ * Global registry ID.
85
+ * NOTE: The `key` property refers to the original registry entry.
86
+ */
87
+ // TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
88
+ // TODO(dmaretskyi): Consider making it part of ECHO meta.
89
+ // TODO(dmaretskyi): Make required.
90
+ key: Schema2.optional(Schema2.String).annotations({
91
+ description: "Unique registration key for the blueprint"
92
+ }),
54
93
  // TODO(burdon): Rename to id/uri?
55
94
  name: Schema2.NonEmptyString,
56
95
  version: Schema2.String,
57
96
  description: Schema2.optional(Schema2.String),
58
97
  // Reference to a source script if it exists within ECHO.
59
98
  // TODO(burdon): Don't ref ScriptType directly (core).
60
- source: Schema2.optional(Ref(ScriptType)),
99
+ source: Schema2.optional(Ref2(Script)),
61
100
  inputSchema: Schema2.optional(JsonSchemaType),
62
101
  outputSchema: Schema2.optional(JsonSchemaType),
63
102
  // Local binding to a function name.
64
103
  binding: Schema2.optional(Schema2.String)
65
- }).pipe(Type.Obj({
104
+ }).pipe(Type2.Obj({
66
105
  typename: "dxos.org/type/Function",
67
106
  version: "0.1.0"
68
- }), LabelAnnotation.set([
107
+ }), LabelAnnotation2.set([
69
108
  "name"
70
109
  ]));
110
+ var make2 = (props) => Obj2.make(Function, props);
71
111
 
72
- // src/trace.ts
73
- import { Schema as Schema4 } from "effect";
74
- import { Type as Type2 } from "@dxos/echo";
75
- import { Queue } from "@dxos/echo-db";
76
- import { ObjectId } from "@dxos/echo-schema";
77
- import { log } from "@dxos/log";
78
-
79
- // src/types.ts
80
- import { Schema as Schema3, SchemaAST } from "effect";
81
- import { Expando, OptionsAnnotationId, TypedObject, Ref as Ref2, RawObject } from "@dxos/echo-schema";
112
+ // src/types/Trigger.ts
113
+ var Trigger_exports = {};
114
+ __export(Trigger_exports, {
115
+ EmailSpec: () => EmailSpec,
116
+ Kinds: () => Kinds,
117
+ QueueSpec: () => QueueSpec,
118
+ Spec: () => Spec,
119
+ SubscriptionSpec: () => SubscriptionSpec,
120
+ TimerSpec: () => TimerSpec,
121
+ Trigger: () => Trigger,
122
+ WebhookSpec: () => WebhookSpec,
123
+ make: () => make3
124
+ });
125
+ import * as Schema3 from "effect/Schema";
126
+ import * as SchemaAST from "effect/SchemaAST";
127
+ import { Obj as Obj3, QueryAST, Type as Type3 } from "@dxos/echo";
128
+ import { Expando, OptionsAnnotationId, Ref as Ref3 } from "@dxos/echo/internal";
82
129
  import { DXN } from "@dxos/keys";
83
- var TriggerKind = /* @__PURE__ */ function(TriggerKind2) {
84
- TriggerKind2["Timer"] = "timer";
85
- TriggerKind2["Webhook"] = "webhook";
86
- TriggerKind2["Subscription"] = "subscription";
87
- TriggerKind2["Email"] = "email";
88
- TriggerKind2["Queue"] = "queue";
89
- return TriggerKind2;
90
- }({});
130
+ var Kinds = [
131
+ "email",
132
+ "queue",
133
+ "subscription",
134
+ "timer",
135
+ "webhook"
136
+ ];
91
137
  var kindLiteralAnnotations = {
92
138
  title: "Kind"
93
139
  };
94
- var TimerTriggerSchema = Schema3.Struct({
140
+ var EmailSpec = Schema3.Struct({
141
+ kind: Schema3.Literal("email").annotations(kindLiteralAnnotations)
142
+ }).pipe(Schema3.mutable);
143
+ var QueueSpec = Schema3.Struct({
144
+ kind: Schema3.Literal("queue").annotations(kindLiteralAnnotations),
145
+ // TODO(dmaretskyi): Change to a reference.
146
+ queue: DXN.Schema
147
+ }).pipe(Schema3.mutable);
148
+ var SubscriptionSpec = Schema3.Struct({
149
+ kind: Schema3.Literal("subscription").annotations(kindLiteralAnnotations),
150
+ query: Schema3.Struct({
151
+ raw: Schema3.optional(Schema3.String.annotations({
152
+ title: "Query"
153
+ })),
154
+ ast: QueryAST.Query
155
+ }).pipe(Schema3.mutable),
156
+ options: Schema3.optional(Schema3.Struct({
157
+ // Watch changes to object (not just creation).
158
+ deep: Schema3.optional(Schema3.Boolean.annotations({
159
+ title: "Nested"
160
+ })),
161
+ // Debounce changes (delay in ms).
162
+ delay: Schema3.optional(Schema3.Number.annotations({
163
+ title: "Delay"
164
+ }))
165
+ }).annotations({
166
+ title: "Options"
167
+ }))
168
+ }).pipe(Schema3.mutable);
169
+ var TimerSpec = Schema3.Struct({
95
170
  kind: Schema3.Literal("timer").annotations(kindLiteralAnnotations),
96
171
  cron: Schema3.String.annotations({
97
172
  title: "Cron",
@@ -100,14 +175,7 @@ var TimerTriggerSchema = Schema3.Struct({
100
175
  ]
101
176
  })
102
177
  }).pipe(Schema3.mutable);
103
- var EmailTriggerSchema = Schema3.Struct({
104
- kind: Schema3.Literal("email").annotations(kindLiteralAnnotations)
105
- }).pipe(Schema3.mutable);
106
- var QueueTriggerSchema = Schema3.Struct({
107
- kind: Schema3.Literal("queue").annotations(kindLiteralAnnotations),
108
- queue: DXN.Schema
109
- }).pipe(Schema3.mutable);
110
- var WebhookTriggerSchema = Schema3.Struct({
178
+ var WebhookSpec = Schema3.Struct({
111
179
  kind: Schema3.Literal("webhook").annotations(kindLiteralAnnotations),
112
180
  method: Schema3.optional(Schema3.String.annotations({
113
181
  title: "Method",
@@ -120,71 +188,15 @@ var WebhookTriggerSchema = Schema3.Struct({
120
188
  title: "Port"
121
189
  }))
122
190
  }).pipe(Schema3.mutable);
123
- var QuerySchema = Schema3.Struct({
124
- type: Schema3.optional(Schema3.String.annotations({
125
- title: "Type"
126
- })),
127
- props: Schema3.optional(Schema3.Record({
128
- key: Schema3.String,
129
- value: Schema3.Any
130
- }))
131
- }).annotations({
132
- title: "Query"
133
- });
134
- var SubscriptionTriggerSchema = Schema3.Struct({
135
- kind: Schema3.Literal("subscription").annotations(kindLiteralAnnotations),
136
- // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
137
- filter: QuerySchema,
138
- options: Schema3.optional(Schema3.Struct({
139
- // Watch changes to object (not just creation).
140
- deep: Schema3.optional(Schema3.Boolean.annotations({
141
- title: "Nested"
142
- })),
143
- // Debounce changes (delay in ms).
144
- delay: Schema3.optional(Schema3.Number.annotations({
145
- title: "Delay"
146
- }))
147
- }).annotations({
148
- title: "Options"
149
- }))
150
- }).pipe(Schema3.mutable);
151
- var TriggerSchema = Schema3.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
191
+ var Spec = Schema3.Union(EmailSpec, QueueSpec, SubscriptionSpec, TimerSpec, WebhookSpec).annotations({
152
192
  title: "Trigger"
153
193
  });
154
- var EmailTriggerOutput = Schema3.mutable(Schema3.Struct({
155
- from: Schema3.String,
156
- to: Schema3.String,
157
- subject: Schema3.String,
158
- created: Schema3.String,
159
- body: Schema3.String
160
- }));
161
- var WebhookTriggerOutput = Schema3.mutable(Schema3.Struct({
162
- url: Schema3.String,
163
- method: Schema3.Literal("GET", "POST"),
164
- headers: Schema3.Record({
165
- key: Schema3.String,
166
- value: Schema3.String
167
- }),
168
- bodyText: Schema3.String
169
- }));
170
- var QueueTriggerOutput = Schema3.mutable(Schema3.Struct({
171
- queue: DXN.Schema,
172
- item: Schema3.Any,
173
- cursor: Schema3.String
174
- }));
175
- var SubscriptionTriggerOutput = Schema3.mutable(Schema3.Struct({
176
- type: Schema3.String,
177
- changedObjectId: Schema3.String
178
- }));
179
- var TimerTriggerOutput = Schema3.mutable(Schema3.Struct({
180
- tick: Schema3.Number
181
- }));
182
- var FunctionTriggerSchema = Schema3.Struct({
194
+ var Trigger_ = Schema3.Struct({
183
195
  /**
184
196
  * Function or workflow to invoke.
185
197
  */
186
198
  // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
187
- function: Schema3.optional(Ref2(Expando).annotations({
199
+ function: Schema3.optional(Ref3(Expando).annotations({
188
200
  title: "Function"
189
201
  })),
190
202
  /**
@@ -198,7 +210,7 @@ var FunctionTriggerSchema = Schema3.Struct({
198
210
  enabled: Schema3.optional(Schema3.Boolean.annotations({
199
211
  title: "Enabled"
200
212
  })),
201
- spec: Schema3.optional(TriggerSchema),
213
+ spec: Schema3.optional(Spec),
202
214
  /**
203
215
  * Passed as the input data to the function.
204
216
  * Must match the function's input schema.
@@ -214,46 +226,446 @@ var FunctionTriggerSchema = Schema3.Struct({
214
226
  key: Schema3.String,
215
227
  value: Schema3.Any
216
228
  })))
229
+ }).pipe(Type3.Obj({
230
+ typename: "dxos.org/type/Trigger",
231
+ version: "0.1.0"
232
+ }));
233
+ var Trigger = Trigger_;
234
+ var make3 = (props) => Obj3.make(Trigger, props);
235
+
236
+ // src/types/TriggerEvent.ts
237
+ var TriggerEvent_exports = {};
238
+ __export(TriggerEvent_exports, {
239
+ EmailEvent: () => EmailEvent,
240
+ QueueEvent: () => QueueEvent,
241
+ SubscriptionEvent: () => SubscriptionEvent,
242
+ TimerEvent: () => TimerEvent,
243
+ WebhookEvent: () => WebhookEvent
217
244
  });
218
- var FunctionTrigger = class extends TypedObject({
219
- typename: "dxos.org/type/FunctionTrigger",
220
- version: "0.2.0"
221
- })(FunctionTriggerSchema.fields) {
245
+ import * as Schema4 from "effect/Schema";
246
+ import { DXN as DXN2, Obj as Obj4, Type as Type4 } from "@dxos/echo";
247
+ var EmailEvent = Schema4.mutable(Schema4.Struct({
248
+ from: Schema4.String,
249
+ to: Schema4.String,
250
+ subject: Schema4.String,
251
+ created: Schema4.String,
252
+ body: Schema4.String
253
+ }));
254
+ var QueueEvent = Schema4.mutable(Schema4.Struct({
255
+ queue: DXN2.Schema,
256
+ item: Schema4.Any,
257
+ cursor: Schema4.String
258
+ }));
259
+ var SubscriptionEvent = Schema4.Struct({
260
+ /**
261
+ * Type of the mutation.
262
+ */
263
+ // TODO(dmaretskyi): Specify enum.
264
+ type: Schema4.String,
265
+ /**
266
+ * Reference to the object that was changed or created.
267
+ */
268
+ subject: Type4.Ref(Obj4.Any),
269
+ /**
270
+ * @deprecated
271
+ */
272
+ changedObjectId: Schema4.optional(Schema4.String)
273
+ }).pipe(Schema4.mutable);
274
+ var TimerEvent = Schema4.mutable(Schema4.Struct({
275
+ tick: Schema4.Number
276
+ }));
277
+ var WebhookEvent = Schema4.mutable(Schema4.Struct({
278
+ url: Schema4.String,
279
+ method: Schema4.Literal("GET", "POST"),
280
+ headers: Schema4.Record({
281
+ key: Schema4.String,
282
+ value: Schema4.String
283
+ }),
284
+ bodyText: Schema4.String
285
+ }));
286
+
287
+ // src/handler.ts
288
+ var typeId = Symbol.for("@dxos/functions/FunctionDefinition");
289
+ var defineFunction = ({ key, name, description, inputSchema, outputSchema = Schema5.Any, handler }) => {
290
+ if (!Schema5.isSchema(inputSchema)) {
291
+ throw new Error("Input schema must be a valid schema");
292
+ }
293
+ if (typeof handler !== "function") {
294
+ throw new Error("Handler must be a function");
295
+ }
296
+ const limit = Error.stackTraceLimit;
297
+ Error.stackTraceLimit = 2;
298
+ const traceError = new Error();
299
+ Error.stackTraceLimit = limit;
300
+ let cache = false;
301
+ const captureStackTrace = () => {
302
+ if (cache !== false) {
303
+ return cache;
304
+ }
305
+ if (traceError.stack !== void 0) {
306
+ const stack = traceError.stack.split("\n");
307
+ if (stack[2] !== void 0) {
308
+ cache = stack[2].trim();
309
+ return cache;
310
+ }
311
+ }
312
+ };
313
+ const handlerWithSpan = (...args) => {
314
+ const result = handler(...args);
315
+ if (Effect.isEffect(result)) {
316
+ return Effect.withSpan(result, `${key ?? name}`, {
317
+ captureStackTrace
318
+ });
319
+ }
320
+ return result;
321
+ };
322
+ return {
323
+ [typeId]: true,
324
+ key,
325
+ name,
326
+ description,
327
+ inputSchema,
328
+ outputSchema,
329
+ handler: handlerWithSpan
330
+ };
331
+ };
332
+ var FunctionDefinition = {
333
+ make: defineFunction,
334
+ isFunction: (value) => {
335
+ return typeof value === "object" && value !== null && Symbol.for("@dxos/functions/FunctionDefinition") in value;
336
+ },
337
+ serialize: (functionDef) => {
338
+ assertArgument(FunctionDefinition.isFunction(functionDef), "functionDef");
339
+ return serializeFunction(functionDef);
340
+ },
341
+ deserialize: (functionObj) => {
342
+ assertArgument(Obj5.instanceOf(Function_exports.Function, functionObj), "functionObj");
343
+ return deserializeFunction(functionObj);
344
+ }
345
+ };
346
+ var serializeFunction = (functionDef) => {
347
+ const fn6 = Function_exports.make({
348
+ key: functionDef.key,
349
+ name: functionDef.name,
350
+ version: "0.1.0",
351
+ description: functionDef.description,
352
+ inputSchema: Type5.toJsonSchema(functionDef.inputSchema),
353
+ outputSchema: !functionDef.outputSchema ? void 0 : Type5.toJsonSchema(functionDef.outputSchema)
354
+ });
355
+ if (functionDef.meta?.deployedFunctionId) {
356
+ setUserFunctionIdInMetadata(Obj5.getMeta(fn6), functionDef.meta.deployedFunctionId);
357
+ }
358
+ return fn6;
359
+ };
360
+ var deserializeFunction = (functionObj) => {
361
+ return {
362
+ [typeId]: true,
363
+ // TODO(dmaretskyi): Fix key.
364
+ key: functionObj.key ?? functionObj.name,
365
+ name: functionObj.name,
366
+ description: functionObj.description,
367
+ inputSchema: !functionObj.inputSchema ? Schema5.Unknown : Type5.toEffectSchema(functionObj.inputSchema),
368
+ outputSchema: !functionObj.outputSchema ? void 0 : Type5.toEffectSchema(functionObj.outputSchema),
369
+ // TODO(dmaretskyi): This should throw error.
370
+ handler: () => {
371
+ },
372
+ meta: {
373
+ deployedFunctionId: getUserFunctionIdInMetadata(Obj5.getMeta(functionObj))
374
+ }
375
+ };
222
376
  };
223
- var FunctionManifestSchema = Schema3.Struct({
224
- functions: Schema3.optional(Schema3.mutable(Schema3.Array(RawObject(FunctionType)))),
225
- triggers: Schema3.optional(Schema3.mutable(Schema3.Array(RawObject(FunctionTrigger))))
377
+
378
+ // src/example/fib.ts
379
+ var fib_default = defineFunction({
380
+ key: "example.org/function/fib",
381
+ name: "Fibonacci",
382
+ description: "Function that calculates a Fibonacci number",
383
+ inputSchema: Schema6.Struct({
384
+ iterations: Schema6.optional(Schema6.Number).annotations({
385
+ description: "Number of iterations",
386
+ default: 1e5
387
+ })
388
+ }),
389
+ outputSchema: Schema6.Struct({
390
+ result: Schema6.String
391
+ }),
392
+ handler: Effect2.fn(function* ({ data: { iterations = 1e5 } }) {
393
+ let a = 0n;
394
+ let b = 1n;
395
+ for (let i = 0; i < iterations; i++) {
396
+ a += b;
397
+ b = a - b;
398
+ }
399
+ return {
400
+ result: a.toString()
401
+ };
402
+ })
403
+ });
404
+
405
+ // src/example/reply.ts
406
+ import * as Console from "effect/Console";
407
+ import * as Effect3 from "effect/Effect";
408
+ import * as Schema7 from "effect/Schema";
409
+ var reply_default = defineFunction({
410
+ key: "example.org/function/reply",
411
+ name: "Reply",
412
+ description: "Function that echoes the input",
413
+ inputSchema: Schema7.Any,
414
+ outputSchema: Schema7.Any,
415
+ handler: Effect3.fn(function* ({ data }) {
416
+ yield* Console.log("reply", {
417
+ data
418
+ });
419
+ return data;
420
+ })
421
+ });
422
+
423
+ // src/example/sleep.ts
424
+ import * as Effect4 from "effect/Effect";
425
+ import * as Schema8 from "effect/Schema";
426
+ var sleep_default = defineFunction({
427
+ key: "example.org/function/sleep",
428
+ name: "Sleep",
429
+ description: "Function that sleeps for a given amount of time",
430
+ inputSchema: Schema8.Struct({
431
+ duration: Schema8.optional(Schema8.Number).annotations({
432
+ description: "Milliseconds to sleep",
433
+ default: 1e5
434
+ })
435
+ }),
436
+ outputSchema: Schema8.Void,
437
+ handler: Effect4.fn(function* ({ data: { duration = 1e5 } }) {
438
+ yield* Effect4.sleep(duration);
439
+ })
226
440
  });
227
- var FUNCTION_TYPES = [
228
- FunctionType,
229
- FunctionTrigger
230
- ];
441
+
442
+ // src/example/index.ts
443
+ (function(Example2) {
444
+ Example2.fib = fib_default;
445
+ Example2.reply = reply_default;
446
+ Example2.sleep = sleep_default;
447
+ })(Example || (Example = {}));
448
+ var Example;
449
+
450
+ // src/executor/executor.ts
451
+ import * as Effect5 from "effect/Effect";
452
+ import * as Schema9 from "effect/Schema";
453
+ import { runAndForwardErrors } from "@dxos/effect";
454
+ var FunctionExecutor = class {
455
+ _services;
456
+ constructor(_services) {
457
+ this._services = _services;
458
+ }
459
+ /**
460
+ * Invoke function.
461
+ */
462
+ // TODO(dmaretskyi): Invocation context: queue, space, etc...
463
+ async invoke(functionDef, input) {
464
+ const assertInput = functionDef.inputSchema.pipe(Schema9.asserts);
465
+ assertInput(input);
466
+ const context = {
467
+ space: void 0,
468
+ getService: this._services.getService.bind(this._services),
469
+ getSpace: async (_spaceId) => {
470
+ throw new Error("Not available. Use the database service instead.");
471
+ }
472
+ };
473
+ const result = functionDef.handler({
474
+ context,
475
+ data: input
476
+ });
477
+ let data;
478
+ if (Effect5.isEffect(result)) {
479
+ data = await result.pipe(Effect5.provide(this._services.createLayer()), runAndForwardErrors);
480
+ } else {
481
+ data = await result;
482
+ }
483
+ const assertOutput = functionDef.outputSchema?.pipe(Schema9.asserts);
484
+ assertOutput(data);
485
+ return data;
486
+ }
487
+ };
488
+
489
+ // src/services/function-invocation-service.ts
490
+ import * as Context2 from "effect/Context";
491
+ import * as Effect7 from "effect/Effect";
492
+ import * as Layer2 from "effect/Layer";
493
+ import { AiService as AiService2 } from "@dxos/ai";
494
+
495
+ // src/services/local-function-execution.ts
496
+ import * as Context from "effect/Context";
497
+ import * as Effect6 from "effect/Effect";
498
+ import * as Layer from "effect/Layer";
499
+ import * as Schema10 from "effect/Schema";
500
+ import { AiService } from "@dxos/ai";
501
+ import { todo } from "@dxos/debug";
502
+ import { log as log2 } from "@dxos/log";
503
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/services/local-function-execution.ts";
504
+ var LocalFunctionExecutionService = class _LocalFunctionExecutionService extends Context.Tag("@dxos/functions/LocalFunctionExecutionService")() {
505
+ static layerLive = Layer.effect(_LocalFunctionExecutionService, Effect6.gen(function* () {
506
+ const resolver = yield* FunctionImplementationResolver;
507
+ const ai = yield* AiService.AiService;
508
+ const credentials = yield* CredentialsService;
509
+ const database = yield* DatabaseService;
510
+ const queues = yield* QueueService;
511
+ const functionCallService = yield* RemoteFunctionExecutionService;
512
+ return {
513
+ // TODO(dmaretskyi): Better error types.
514
+ invokeFunction: (functionDef, input) => Effect6.gen(function* () {
515
+ const resolved = yield* resolver.resolveFunctionImplementation(functionDef).pipe(Effect6.orDie);
516
+ const output = yield* invokeFunction(resolved, input);
517
+ return output;
518
+ }).pipe(Effect6.provideService(AiService.AiService, ai), Effect6.provideService(CredentialsService, credentials), Effect6.provideService(DatabaseService, database), Effect6.provideService(QueueService, queues), Effect6.provideService(RemoteFunctionExecutionService, functionCallService))
519
+ };
520
+ }));
521
+ static invokeFunction = Effect6.serviceFunctionEffect(_LocalFunctionExecutionService, (_) => _.invokeFunction);
522
+ };
523
+ var invokeFunction = (functionDef, input) => Effect6.gen(function* () {
524
+ try {
525
+ const assertInput = functionDef.inputSchema.pipe(Schema10.asserts);
526
+ assertInput(input);
527
+ } catch (e) {
528
+ throw new FunctionError({
529
+ message: "Invalid function input",
530
+ context: {
531
+ name: functionDef.name
532
+ },
533
+ cause: e
534
+ });
535
+ }
536
+ const context = {
537
+ space: void 0,
538
+ getService: () => todo(),
539
+ getSpace: async (_spaceId) => {
540
+ throw new Error("Not available. Use the database service instead.");
541
+ }
542
+ };
543
+ log2.info("invoking function", {
544
+ name: functionDef.name,
545
+ input
546
+ }, {
547
+ F: __dxlog_file,
548
+ L: 98,
549
+ S: this,
550
+ C: (f, a) => f(...a)
551
+ });
552
+ const data = yield* Effect6.gen(function* () {
553
+ const result = functionDef.handler({
554
+ context,
555
+ data: input
556
+ });
557
+ if (Effect6.isEffect(result)) {
558
+ return yield* result.pipe(Effect6.orDie);
559
+ } else if (typeof result === "object" && result !== null && "then" in result && typeof result.then === "function") {
560
+ return yield* Effect6.promise(() => result);
561
+ } else {
562
+ return result;
563
+ }
564
+ }).pipe(Effect6.orDie, Effect6.catchAllDefect((defect) => Effect6.die(new FunctionError({
565
+ context: {
566
+ name: functionDef.name
567
+ },
568
+ cause: defect
569
+ }))));
570
+ log2.info("completed", {
571
+ function: functionDef.name,
572
+ input,
573
+ data
574
+ }, {
575
+ F: __dxlog_file,
576
+ L: 122,
577
+ S: this,
578
+ C: (f, a) => f(...a)
579
+ });
580
+ try {
581
+ const assertOutput = functionDef.outputSchema?.pipe(Schema10.asserts);
582
+ assertOutput(data);
583
+ } catch (e) {
584
+ throw new FunctionError({
585
+ message: "Invalid function output",
586
+ context: {
587
+ name: functionDef.name
588
+ },
589
+ cause: e
590
+ });
591
+ }
592
+ return data;
593
+ }).pipe(Effect6.withSpan("invokeFunction", {
594
+ attributes: {
595
+ name: functionDef.name
596
+ }
597
+ }));
598
+ var FunctionImplementationResolver = class _FunctionImplementationResolver extends Context.Tag("@dxos/functions/FunctionImplementationResolver")() {
599
+ static layerTest = ({ functions }) => Layer.succeed(_FunctionImplementationResolver, {
600
+ resolveFunctionImplementation: (functionDef) => {
601
+ const resolved = functions.find((f) => f.key === functionDef.key);
602
+ if (!resolved) {
603
+ return Effect6.fail(new FunctionNotFoundError(functionDef.name));
604
+ }
605
+ return Effect6.succeed(resolved);
606
+ }
607
+ });
608
+ };
609
+
610
+ // src/services/function-invocation-service.ts
611
+ var FunctionInvocationService = class _FunctionInvocationService extends Context2.Tag("@dxos/functions/FunctionInvocationService")() {
612
+ static invokeFunction = Effect7.serviceFunctionEffect(_FunctionInvocationService, (_) => _.invokeFunction);
613
+ static layer = Layer2.effect(_FunctionInvocationService, Effect7.gen(function* () {
614
+ const localExecutioner = yield* LocalFunctionExecutionService;
615
+ const remoteExecutioner = yield* RemoteFunctionExecutionService;
616
+ return {
617
+ invokeFunction: (functionDef, input) => Effect7.gen(function* () {
618
+ if (functionDef.meta?.deployedFunctionId) {
619
+ return yield* remoteExecutioner.callFunction(functionDef.meta.deployedFunctionId, input);
620
+ }
621
+ return yield* localExecutioner.invokeFunction(functionDef, input);
622
+ })
623
+ };
624
+ }));
625
+ // TODO(dmaretskyi): Don't provide `FunctionImplementationResolver`.
626
+ static layerTest = ({ functions = [] } = {}) => _FunctionInvocationService.layer.pipe(Layer2.provide(LocalFunctionExecutionService.layerLive), Layer2.provide(FunctionImplementationResolver.layerTest({
627
+ functions
628
+ })), Layer2.provide(RemoteFunctionExecutionService.layerMock));
629
+ // TODO(dmaretskyi): This shouldn't default to all services being not available.
630
+ // TODO(dmaretskyi): Don't provide `FunctionImplementationResolver`.
631
+ /**
632
+ * @deprecated Use {@link layerTest} instead.
633
+ */
634
+ static layerTestMocked = ({ functions }) => _FunctionInvocationService.layerTest({
635
+ functions
636
+ }).pipe(Layer2.provide(AiService2.notAvailable), Layer2.provide(CredentialsService.configuredLayer([])), Layer2.provide(DatabaseService.notAvailable), Layer2.provide(QueueService.notAvailable));
637
+ };
231
638
 
232
639
  // src/trace.ts
233
- var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/trace.ts";
234
- var InvocationOutcome = /* @__PURE__ */ function(InvocationOutcome2) {
640
+ import * as Schema11 from "effect/Schema";
641
+ import { Type as Type6 } from "@dxos/echo";
642
+ import { ObjectId } from "@dxos/echo/internal";
643
+ import { Queue } from "@dxos/echo-db";
644
+ import { log as log3 } from "@dxos/log";
645
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/trace.ts";
646
+ var InvocationOutcome = /* @__PURE__ */ (function(InvocationOutcome2) {
235
647
  InvocationOutcome2["SUCCESS"] = "success";
236
648
  InvocationOutcome2["FAILURE"] = "failure";
237
649
  InvocationOutcome2["PENDING"] = "pending";
238
650
  return InvocationOutcome2;
239
- }({});
240
- var InvocationTraceEventType = /* @__PURE__ */ function(InvocationTraceEventType2) {
651
+ })({});
652
+ var InvocationTraceEventType = /* @__PURE__ */ (function(InvocationTraceEventType2) {
241
653
  InvocationTraceEventType2["START"] = "start";
242
654
  InvocationTraceEventType2["END"] = "end";
243
655
  return InvocationTraceEventType2;
244
- }({});
245
- var TraceEventException = Schema4.Struct({
246
- timestampMs: Schema4.Number,
247
- message: Schema4.String,
248
- name: Schema4.String,
249
- stack: Schema4.optional(Schema4.String)
656
+ })({});
657
+ var TraceEventException = Schema11.Struct({
658
+ timestamp: Schema11.Number,
659
+ message: Schema11.String,
660
+ name: Schema11.String,
661
+ stack: Schema11.optional(Schema11.String)
250
662
  });
251
- var InvocationTraceStartEvent = Schema4.Struct({
663
+ var InvocationTraceStartEvent = Schema11.Struct({
252
664
  /**
253
665
  * Queue message id.
254
666
  */
255
667
  id: ObjectId,
256
- type: Schema4.Literal("start"),
668
+ type: Schema11.Literal("start"),
257
669
  /**
258
670
  * Invocation id, the same for invocation start and end events.
259
671
  */
@@ -261,34 +673,34 @@ var InvocationTraceStartEvent = Schema4.Struct({
261
673
  /**
262
674
  * Event generation time.
263
675
  */
264
- timestampMs: Schema4.Number,
676
+ timestamp: Schema11.Number,
265
677
  /**
266
678
  * Data passed to function / workflow as an argument.
267
679
  */
268
680
  // TODO(burdon): Input schema?
269
- input: Schema4.Object,
681
+ input: Schema11.Object,
270
682
  /**
271
683
  * Queue for function/workflow invocation events.
272
684
  */
273
- invocationTraceQueue: Type2.Ref(Queue),
685
+ invocationTraceQueue: Schema11.optional(Type6.Ref(Queue)),
274
686
  /**
275
687
  * DXN of the invoked function/workflow.
276
688
  */
277
- invocationTarget: Type2.Ref(Type2.Expando),
689
+ invocationTarget: Schema11.optional(Type6.Ref(Type6.Expando)),
278
690
  /**
279
691
  * Present for automatic invocations.
280
692
  */
281
- trigger: Schema4.optional(Type2.Ref(FunctionTrigger))
282
- }).pipe(Type2.Obj({
693
+ trigger: Schema11.optional(Type6.Ref(Trigger_exports.Trigger))
694
+ }).pipe(Type6.Obj({
283
695
  typename: "dxos.org/type/InvocationTraceStart",
284
696
  version: "0.1.0"
285
697
  }));
286
- var InvocationTraceEndEvent = Schema4.Struct({
698
+ var InvocationTraceEndEvent = Schema11.Struct({
287
699
  /**
288
700
  * Trace event id.
289
701
  */
290
702
  id: ObjectId,
291
- type: Schema4.Literal("end"),
703
+ type: Schema11.Literal("end"),
292
704
  /**
293
705
  * Invocation id, will be the same for invocation start and end.
294
706
  */
@@ -297,31 +709,29 @@ var InvocationTraceEndEvent = Schema4.Struct({
297
709
  * Event generation time.
298
710
  */
299
711
  // TODO(burdon): Remove ms suffix.
300
- timestampMs: Schema4.Number,
301
- outcome: Schema4.Enums(InvocationOutcome),
302
- exception: Schema4.optional(TraceEventException)
303
- }).pipe(Type2.Obj({
712
+ timestamp: Schema11.Number,
713
+ outcome: Schema11.Enums(InvocationOutcome),
714
+ exception: Schema11.optional(TraceEventException)
715
+ }).pipe(Type6.Obj({
304
716
  typename: "dxos.org/type/InvocationTraceEnd",
305
717
  version: "0.1.0"
306
718
  }));
307
- var TraceEventLog = Schema4.Struct({
308
- timestampMs: Schema4.Number,
309
- level: Schema4.String,
310
- message: Schema4.String,
311
- context: Schema4.optional(Schema4.Object)
719
+ var TraceEventLog = Schema11.Struct({
720
+ timestamp: Schema11.Number,
721
+ level: Schema11.String,
722
+ message: Schema11.String,
723
+ context: Schema11.optional(Schema11.Object)
312
724
  });
313
- var TraceEvent = Schema4.Struct({
725
+ var TraceEvent = Schema11.Struct({
314
726
  id: ObjectId,
315
727
  // TODO(burdon): Need enum/numeric result (not string).
316
- outcome: Schema4.String,
317
- truncated: Schema4.Boolean,
318
- /**
319
- * Time when the event was persisted.
320
- */
321
- ingestionTimestampMs: Schema4.Number,
322
- logs: Schema4.Array(TraceEventLog),
323
- exceptions: Schema4.Array(TraceEventException)
324
- }).pipe(Type2.Obj({
728
+ outcome: Schema11.String,
729
+ truncated: Schema11.Boolean,
730
+ /** Time when the event was persisted. */
731
+ ingestionTimestamp: Schema11.Number,
732
+ logs: Schema11.Array(TraceEventLog),
733
+ exceptions: Schema11.Array(TraceEventException)
734
+ }).pipe(Type6.Obj({
325
735
  typename: "dxos.org/type/TraceEvent",
326
736
  version: "0.1.0"
327
737
  }));
@@ -350,11 +760,11 @@ var createInvocationSpans = (items) => {
350
760
  const result = [];
351
761
  for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
352
762
  if (!start) {
353
- log.warn("found end event without matching start", {
763
+ log3.warn("found end event without matching start", {
354
764
  invocationId
355
765
  }, {
356
- F: __dxlog_file,
357
- L: 160,
766
+ F: __dxlog_file2,
767
+ L: 158,
358
768
  S: void 0,
359
769
  C: (f, a) => f(...a)
360
770
  });
@@ -363,8 +773,8 @@ var createInvocationSpans = (items) => {
363
773
  const isInProgress = end === void 0;
364
774
  result.push({
365
775
  id: invocationId,
366
- timestampMs: start.timestampMs,
367
- durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
776
+ timestamp: start.timestamp,
777
+ duration: isInProgress ? now - start.timestamp : end.timestamp - start.timestamp,
368
778
  outcome: end?.outcome ?? "pending",
369
779
  exception: end?.exception,
370
780
  input: start.input,
@@ -376,117 +786,561 @@ var createInvocationSpans = (items) => {
376
786
  return result;
377
787
  };
378
788
 
379
- // src/url.ts
380
- var FUNCTIONS_META_KEY = "dxos.org/service/function";
381
- var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
382
- var isSecure = (protocol) => {
383
- return protocol === "https:" || protocol === "wss:";
384
- };
385
- var getUserFunctionUrlInMetadata = (meta) => {
386
- return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
789
+ // src/triggers/invocation-tracer.ts
790
+ import * as Context3 from "effect/Context";
791
+ import * as Effect8 from "effect/Effect";
792
+ import * as Layer3 from "effect/Layer";
793
+ import { Obj as Obj6, Ref as Ref4 } from "@dxos/echo";
794
+ import { DXN as DXN3, ObjectId as ObjectId2 } from "@dxos/keys";
795
+ var InvocationTracer = class _InvocationTracer extends Context3.Tag("@dxos/functions/InvocationTracer")() {
796
+ static layerLive = (opts) => Layer3.effect(_InvocationTracer, Effect8.gen(function* () {
797
+ return {
798
+ traceInvocationStart: Effect8.fn("traceInvocationStart")(function* ({ payload, target }) {
799
+ const invocationId = ObjectId2.random();
800
+ const invocationTraceQueue = yield* QueueService.createQueue({
801
+ subspaceTag: "trace"
802
+ });
803
+ const now = Date.now();
804
+ const traceEvent = Obj6.make(InvocationTraceStartEvent, {
805
+ type: InvocationTraceEventType.START,
806
+ invocationId,
807
+ timestamp: now,
808
+ // TODO(dmaretskyi): Not json-stringifying this makes ECHO fail when one ECHO object becomes embedded in another ECHO object.
809
+ input: JSON.parse(JSON.stringify(payload.data ?? {})),
810
+ invocationTraceQueue: Ref4.fromDXN(invocationTraceQueue.dxn),
811
+ invocationTarget: target ? Ref4.fromDXN(target) : void 0,
812
+ trigger: payload.trigger ? Ref4.fromDXN(DXN3.fromLocalObjectId(payload.trigger.id)) : void 0
813
+ });
814
+ yield* QueueService.append(opts.invocationTraceQueue, [
815
+ traceEvent
816
+ ]);
817
+ return {
818
+ invocationId,
819
+ invocationTraceQueue
820
+ };
821
+ }),
822
+ traceInvocationEnd: Effect8.fn("traceInvocationEnd")(function* ({ trace, exception }) {
823
+ const now = Date.now();
824
+ const traceEvent = Obj6.make(InvocationTraceEndEvent, {
825
+ type: InvocationTraceEventType.END,
826
+ invocationId: trace.invocationId,
827
+ timestamp: now,
828
+ outcome: exception ? InvocationOutcome.FAILURE : InvocationOutcome.SUCCESS,
829
+ exception: exception ? {
830
+ name: exception.constructor.name,
831
+ timestamp: now,
832
+ message: exception?.message ?? "Unknown error",
833
+ stack: exception?.stack
834
+ } : void 0
835
+ });
836
+ yield* QueueService.append(opts.invocationTraceQueue, [
837
+ traceEvent
838
+ ]);
839
+ })
840
+ };
841
+ }));
842
+ static layerTest = Layer3.unwrapEffect(Effect8.gen(function* () {
843
+ const queue = yield* QueueService.createQueue({
844
+ subspaceTag: "trace"
845
+ });
846
+ return _InvocationTracer.layerLive({
847
+ invocationTraceQueue: queue
848
+ });
849
+ }));
387
850
  };
388
- var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
389
- const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
390
- if (key) {
391
- if (key.id !== functionUrl) {
392
- throw new Error("Metadata mismatch");
851
+
852
+ // src/triggers/trigger-dispatcher.ts
853
+ import * as Cause from "effect/Cause";
854
+ import * as Context5 from "effect/Context";
855
+ import * as Cron from "effect/Cron";
856
+ import * as Duration from "effect/Duration";
857
+ import * as Effect10 from "effect/Effect";
858
+ import * as Either from "effect/Either";
859
+ import * as Exit from "effect/Exit";
860
+ import * as Fiber from "effect/Fiber";
861
+ import * as Layer5 from "effect/Layer";
862
+ import * as Option2 from "effect/Option";
863
+ import * as Record4 from "effect/Record";
864
+ import * as Schedule from "effect/Schedule";
865
+ import { DXN as DXN4, Filter, Obj as Obj7, Query } from "@dxos/echo";
866
+ import { causeToError } from "@dxos/effect";
867
+ import { invariant } from "@dxos/invariant";
868
+ import { log as log4 } from "@dxos/log";
869
+ import { KEY_QUEUE_POSITION } from "@dxos/protocols";
870
+
871
+ // src/triggers/input-builder.ts
872
+ var createInvocationPayload = (trigger, event) => {
873
+ if (!trigger.input) {
874
+ return event;
875
+ }
876
+ const payload = {};
877
+ for (const [key, value] of Object.entries(trigger.input)) {
878
+ if (typeof value !== "string" || !(value.startsWith("{{") && value.endsWith("}}"))) {
879
+ payload[key] = value;
880
+ continue;
393
881
  }
394
- } else {
395
- meta.keys.push({
396
- source: FUNCTIONS_META_KEY,
397
- id: functionUrl
398
- });
882
+ const propertyPath = value.slice(2, -2);
883
+ let valueSubstitution = propertyPath.startsWith("trigger.") ? trigger : propertyPath.startsWith("event.") ? event : void 0;
884
+ for (const pathSegment of propertyPath.split(".").slice(1)) {
885
+ if (valueSubstitution && typeof valueSubstitution === "object") {
886
+ valueSubstitution = valueSubstitution[pathSegment];
887
+ }
888
+ }
889
+ payload[key] = valueSubstitution;
399
890
  }
891
+ return payload;
400
892
  };
401
- var makeFunctionUrl = (fn) => `/${fn.functionId}`;
402
- var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
403
- const baseUrl = new URL("functions/", edgeUrl);
404
- const relativeUrl = functionUrl.replace(/^\//, "");
405
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
406
- options.spaceId && url.searchParams.set("spaceId", options.spaceId);
407
- options.subjectId && url.searchParams.set("subjectId", options.subjectId);
408
- url.protocol = isSecure(url.protocol) ? "https" : "http";
409
- return url.toString();
893
+
894
+ // src/triggers/trigger-state-store.ts
895
+ import * as KeyValueStore from "@effect/platform/KeyValueStore";
896
+ import * as Context4 from "effect/Context";
897
+ import * as Effect9 from "effect/Effect";
898
+ import * as Layer4 from "effect/Layer";
899
+ import * as Option from "effect/Option";
900
+ import * as Schema12 from "effect/Schema";
901
+ import { ObjectId as ObjectId3 } from "@dxos/keys";
902
+ var TriggerState = Schema12.Struct({
903
+ version: Schema12.Literal("1"),
904
+ triggerId: Schema12.String,
905
+ state: Schema12.optional(Schema12.Union(Schema12.TaggedStruct("subscription", {
906
+ processedVersions: Schema12.Record({
907
+ key: ObjectId3,
908
+ value: Schema12.String
909
+ })
910
+ })))
911
+ });
912
+ var TriggerStateStore = class _TriggerStateStore extends Context4.Tag("@dxos/functions/TriggerStateStore")() {
913
+ static getState = Effect9.serviceFunctionEffect(_TriggerStateStore, (_) => _.getState);
914
+ static saveState = Effect9.serviceFunctionEffect(_TriggerStateStore, (_) => _.saveState);
915
+ static layerKv = Layer4.effect(_TriggerStateStore, Effect9.gen(function* () {
916
+ const kv = yield* KeyValueStore.KeyValueStore;
917
+ const schemaStore = kv.forSchema(Schema12.parseJson(TriggerState));
918
+ const store = {
919
+ getState: Effect9.fn("TriggerStateStore.getState")(function* (triggerId) {
920
+ const valueOption = yield* schemaStore.get(triggerId).pipe(Effect9.orDie);
921
+ if (Option.isNone(valueOption)) {
922
+ return yield* Effect9.fail(new TriggerStateNotFoundError());
923
+ }
924
+ return valueOption.value;
925
+ }),
926
+ saveState: Effect9.fn("TriggerStateStore.saveState")(function* (state) {
927
+ yield* schemaStore.set(state.triggerId, state).pipe(Effect9.orDie);
928
+ })
929
+ };
930
+ return store;
931
+ }));
932
+ static layerMemory = _TriggerStateStore.layerKv.pipe(Layer4.provide(KeyValueStore.layerMemory));
410
933
  };
411
934
 
412
- // src/executor/executor.ts
413
- import { Effect, Schema as Schema5 } from "effect";
414
- import { runAndForwardErrors } from "@dxos/effect";
415
- var FunctionExecutor = class {
416
- constructor(_services) {
417
- this._services = _services;
935
+ // src/triggers/trigger-dispatcher.ts
936
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions/src/triggers/trigger-dispatcher.ts";
937
+ var TriggerDispatcher = class _TriggerDispatcher extends Context5.Tag("@dxos/functions/TriggerDispatcher")() {
938
+ static layer = (options) => Layer5.effect(_TriggerDispatcher, Effect10.gen(function* () {
939
+ return new TriggerDispatcherImpl(options);
940
+ }));
941
+ };
942
+ var TriggerDispatcherImpl = class {
943
+ livePollInterval;
944
+ timeControl;
945
+ _running = false;
946
+ _internalTime;
947
+ _timerFiber;
948
+ _scheduledTriggers = /* @__PURE__ */ new Map();
949
+ constructor(options) {
950
+ this.timeControl = options.timeControl;
951
+ this.livePollInterval = options.livePollInterval ?? Duration.seconds(1);
952
+ this._internalTime = options.startingTime ?? /* @__PURE__ */ new Date();
418
953
  }
419
- // TODO(dmaretskyi): Invocation context: queue, space, etc...
420
- async invoke(fnDef, input) {
421
- const assertInput = fnDef.inputSchema.pipe(Schema5.asserts);
422
- assertInput(input);
423
- const context = {
424
- getService: this._services.getService.bind(this._services),
425
- getSpace: async (_spaceId) => {
426
- throw new Error("Not available. Use the database service instead.");
427
- },
428
- space: void 0,
429
- get ai() {
430
- throw new Error("Not available. Use the ai service instead.");
954
+ get running() {
955
+ return this._running;
956
+ }
957
+ start = () => Effect10.gen(this, function* () {
958
+ if (this._running) {
959
+ return;
960
+ }
961
+ this._running = true;
962
+ if (this.timeControl === "natural") {
963
+ this._timerFiber = yield* this._startNaturalTimeProcessing().pipe(Effect10.tapErrorCause((cause) => {
964
+ const error = causeToError(cause);
965
+ log4.error("trigger dispatcher error", {
966
+ error
967
+ }, {
968
+ F: __dxlog_file3,
969
+ L: 178,
970
+ S: this,
971
+ C: (f, a) => f(...a)
972
+ });
973
+ this._running = false;
974
+ return Effect10.void;
975
+ }), Effect10.forkDaemon);
976
+ } else {
977
+ return yield* Effect10.dieMessage("TriggerDispatcher started in manual time control mode");
978
+ }
979
+ log4.info("TriggerDispatcher started", {
980
+ timeControl: this.timeControl
981
+ }, {
982
+ F: __dxlog_file3,
983
+ L: 188,
984
+ S: this,
985
+ C: (f, a) => f(...a)
986
+ });
987
+ });
988
+ stop = () => Effect10.gen(this, function* () {
989
+ if (!this._running) {
990
+ return;
991
+ }
992
+ this._running = false;
993
+ if (this._timerFiber) {
994
+ yield* Fiber.interrupt(this._timerFiber);
995
+ this._timerFiber = void 0;
996
+ }
997
+ this._scheduledTriggers.clear();
998
+ log4.info("TriggerDispatcher stopped", void 0, {
999
+ F: __dxlog_file3,
1000
+ L: 208,
1001
+ S: this,
1002
+ C: (f, a) => f(...a)
1003
+ });
1004
+ });
1005
+ invokeTrigger = (options) => Effect10.gen(this, function* () {
1006
+ const { trigger, event } = options;
1007
+ log4.info("running trigger", {
1008
+ triggerId: trigger.id,
1009
+ spec: trigger.spec,
1010
+ event
1011
+ }, {
1012
+ F: __dxlog_file3,
1013
+ L: 216,
1014
+ S: this,
1015
+ C: (f, a) => f(...a)
1016
+ });
1017
+ const tracer = yield* InvocationTracer;
1018
+ const trace = yield* tracer.traceInvocationStart({
1019
+ target: trigger.function?.dxn,
1020
+ payload: {
1021
+ trigger: {
1022
+ id: trigger.id,
1023
+ // TODO(dmaretskyi): Is `spec` always there>
1024
+ kind: trigger.spec.kind
1025
+ },
1026
+ data: event
1027
+ }
1028
+ });
1029
+ const result = yield* Effect10.gen(this, function* () {
1030
+ if (!trigger.enabled) {
1031
+ return yield* Effect10.dieMessage("Attempting to invoke disabled trigger");
1032
+ }
1033
+ if (!trigger.function) {
1034
+ return yield* Effect10.dieMessage("Trigger has no function reference");
431
1035
  }
1036
+ const serialiedFunction = yield* DatabaseService.load(trigger.function).pipe(Effect10.orDie);
1037
+ invariant(Obj7.instanceOf(Function_exports.Function, serialiedFunction), void 0, {
1038
+ F: __dxlog_file3,
1039
+ L: 243,
1040
+ S: this,
1041
+ A: [
1042
+ "Obj.instanceOf(Function.Function, serialiedFunction)",
1043
+ ""
1044
+ ]
1045
+ });
1046
+ const functionDef = deserializeFunction(serialiedFunction);
1047
+ const inputData = this._prepareInputData(trigger, event);
1048
+ return yield* FunctionInvocationService.invokeFunction(functionDef, inputData).pipe(Effect10.provide(ComputeEventLogger.layerFromTracing.pipe(Layer5.provideMerge(TracingService.layerQueue(trace.invocationTraceQueue)))));
1049
+ }).pipe(Effect10.exit);
1050
+ const triggerExecutionResult = {
1051
+ triggerId: trigger.id,
1052
+ result
432
1053
  };
433
- const result = fnDef.handler({
434
- context,
435
- data: input
1054
+ if (Exit.isSuccess(result)) {
1055
+ log4.info("trigger execution success", {
1056
+ triggerId: trigger.id
1057
+ }, {
1058
+ F: __dxlog_file3,
1059
+ L: 264,
1060
+ S: this,
1061
+ C: (f, a) => f(...a)
1062
+ });
1063
+ } else {
1064
+ log4.error("trigger execution failure", {
1065
+ error: causeToError(result.cause)
1066
+ }, {
1067
+ F: __dxlog_file3,
1068
+ L: 268,
1069
+ S: this,
1070
+ C: (f, a) => f(...a)
1071
+ });
1072
+ }
1073
+ yield* tracer.traceInvocationEnd({
1074
+ trace,
1075
+ // TODO(dmaretskyi): Might miss errors.
1076
+ exception: Exit.isFailure(result) ? Cause.prettyErrors(result.cause)[0] : void 0
436
1077
  });
437
- let data;
438
- if (Effect.isEffect(result)) {
439
- data = await result.pipe(Effect.provide(this._services.createLayer()), runAndForwardErrors);
1078
+ return triggerExecutionResult;
1079
+ });
1080
+ invokeScheduledTriggers = ({ kinds = [
1081
+ "timer",
1082
+ "queue",
1083
+ "subscription"
1084
+ ] } = {}) => Effect10.gen(this, function* () {
1085
+ const invocations = [];
1086
+ for (const kind of kinds) {
1087
+ switch (kind) {
1088
+ case "timer":
1089
+ {
1090
+ yield* this.refreshTriggers();
1091
+ const now = this.getCurrentTime();
1092
+ const triggersToInvoke = [];
1093
+ for (const [triggerId, scheduledTrigger] of this._scheduledTriggers.entries()) {
1094
+ if (scheduledTrigger.nextExecution <= now) {
1095
+ triggersToInvoke.push(scheduledTrigger.trigger);
1096
+ scheduledTrigger.nextExecution = Cron.next(scheduledTrigger.cron, now);
1097
+ }
1098
+ }
1099
+ invocations.push(...yield* Effect10.forEach(triggersToInvoke, (trigger) => this.invokeTrigger({
1100
+ trigger,
1101
+ event: {
1102
+ tick: now.getTime()
1103
+ }
1104
+ }), {
1105
+ concurrency: 1
1106
+ }));
1107
+ }
1108
+ break;
1109
+ case "queue": {
1110
+ const triggers = yield* this._fetchTriggers();
1111
+ for (const trigger of triggers) {
1112
+ const spec = trigger.spec;
1113
+ if (spec?.kind !== "queue") {
1114
+ continue;
1115
+ }
1116
+ const cursor = Obj7.getKeys(trigger, KEY_QUEUE_CURSOR).at(0)?.id;
1117
+ const queue = yield* QueueService.getQueue(DXN4.parse(spec.queue));
1118
+ const objects = yield* Effect10.promise(() => queue.queryObjects());
1119
+ for (const object of objects) {
1120
+ const objectPos = Obj7.getKeys(object, KEY_QUEUE_POSITION).at(0)?.id;
1121
+ if (!objectPos || cursor && parseInt(cursor) >= parseInt(objectPos)) {
1122
+ continue;
1123
+ }
1124
+ invocations.push(yield* this.invokeTrigger({
1125
+ trigger,
1126
+ event: {
1127
+ queue: spec.queue,
1128
+ item: object,
1129
+ cursor: objectPos
1130
+ }
1131
+ }));
1132
+ Obj7.deleteKeys(trigger, KEY_QUEUE_CURSOR);
1133
+ Obj7.getMeta(trigger).keys.push({
1134
+ source: KEY_QUEUE_CURSOR,
1135
+ id: objectPos
1136
+ });
1137
+ yield* DatabaseService.flush();
1138
+ break;
1139
+ }
1140
+ }
1141
+ break;
1142
+ }
1143
+ case "subscription": {
1144
+ const triggers = yield* this._fetchTriggers();
1145
+ for (const trigger of triggers) {
1146
+ const spec = Obj7.getSnapshot(trigger).spec;
1147
+ if (spec?.kind !== "subscription") {
1148
+ continue;
1149
+ }
1150
+ const { objects } = yield* DatabaseService.runQuery(Query.fromAst(spec.query.ast));
1151
+ const state = yield* TriggerStateStore.getState(trigger.id).pipe(Effect10.catchTag("TRIGGER_STATE_NOT_FOUND", () => Effect10.succeed({
1152
+ version: "1",
1153
+ triggerId: trigger.id,
1154
+ state: {
1155
+ _tag: "subscription",
1156
+ processedVersions: {}
1157
+ }
1158
+ })));
1159
+ invariant(state.state?._tag === "subscription", void 0, {
1160
+ F: __dxlog_file3,
1161
+ L: 381,
1162
+ S: this,
1163
+ A: [
1164
+ "state.state?._tag === 'subscription'",
1165
+ ""
1166
+ ]
1167
+ });
1168
+ let updated = false;
1169
+ for (const object of objects) {
1170
+ const existingVersion = Record4.get(state.state.processedVersions, object.id).pipe(Option2.map(Obj7.decodeVersion));
1171
+ const currentVersion = Obj7.version(object);
1172
+ const run = Option2.isNone(existingVersion) || Obj7.compareVersions(currentVersion, existingVersion.value) === "different";
1173
+ if (!run) {
1174
+ continue;
1175
+ }
1176
+ const { db } = yield* DatabaseService;
1177
+ invocations.push(yield* this.invokeTrigger({
1178
+ trigger,
1179
+ event: {
1180
+ // TODO(dmaretskyi): Change type not supported.
1181
+ type: "unknown",
1182
+ subject: db.ref(Obj7.getDXN(object)),
1183
+ changedObjectId: object.id
1184
+ }
1185
+ }));
1186
+ state.state.processedVersions[object.id] = Obj7.encodeVersion(currentVersion);
1187
+ updated = true;
1188
+ }
1189
+ if (updated) {
1190
+ yield* TriggerStateStore.saveState(state);
1191
+ }
1192
+ }
1193
+ break;
1194
+ }
1195
+ default: {
1196
+ return yield* Effect10.dieMessage(`Unknown trigger kind: ${kind}`);
1197
+ }
1198
+ }
1199
+ }
1200
+ return invocations;
1201
+ });
1202
+ advanceTime = (duration) => Effect10.gen(this, function* () {
1203
+ if (this.timeControl !== "manual") {
1204
+ return yield* Effect10.dieMessage("advanceTime can only be used in manual time control mode");
1205
+ }
1206
+ const millis = Duration.toMillis(duration);
1207
+ this._internalTime = new Date(this._internalTime.getTime() + millis);
1208
+ log4("Advanced internal time", {
1209
+ newTime: this._internalTime,
1210
+ advancedBy: Duration.format(duration)
1211
+ }, {
1212
+ F: __dxlog_file3,
1213
+ L: 438,
1214
+ S: this,
1215
+ C: (f, a) => f(...a)
1216
+ });
1217
+ }).pipe(Effect10.orDie);
1218
+ getCurrentTime = () => {
1219
+ if (this.timeControl === "natural") {
1220
+ return /* @__PURE__ */ new Date();
440
1221
  } else {
441
- data = await result;
1222
+ return new Date(this._internalTime);
442
1223
  }
443
- const assertOutput = fnDef.outputSchema?.pipe(Schema5.asserts);
444
- assertOutput(data);
445
- return data;
446
- }
1224
+ };
1225
+ refreshTriggers = () => Effect10.gen(this, function* () {
1226
+ const triggers = yield* this._fetchTriggers();
1227
+ const currentTriggerIds = new Set(triggers.map((t) => t.id));
1228
+ for (const triggerId of this._scheduledTriggers.keys()) {
1229
+ if (!currentTriggerIds.has(triggerId)) {
1230
+ this._scheduledTriggers.delete(triggerId);
1231
+ }
1232
+ }
1233
+ for (const trigger of triggers) {
1234
+ if (trigger.spec?.kind === "timer" && trigger.enabled) {
1235
+ const timerSpec = trigger.spec;
1236
+ const cronEither = Cron.parse(timerSpec.cron);
1237
+ if (Either.isRight(cronEither)) {
1238
+ const cron = cronEither.right;
1239
+ const existing = this._scheduledTriggers.get(trigger.id);
1240
+ const now = this.getCurrentTime();
1241
+ const nextExecution = existing?.nextExecution ?? Cron.next(cron, now);
1242
+ log4("Updated scheduled trigger", {
1243
+ triggerId: trigger.id,
1244
+ cron: timerSpec.cron,
1245
+ nextExecution,
1246
+ now
1247
+ }, {
1248
+ F: __dxlog_file3,
1249
+ L: 478,
1250
+ S: this,
1251
+ C: (f, a) => f(...a)
1252
+ });
1253
+ this._scheduledTriggers.set(trigger.id, {
1254
+ trigger,
1255
+ cron,
1256
+ nextExecution
1257
+ });
1258
+ } else {
1259
+ log4.error("Invalid cron expression", {
1260
+ triggerId: trigger.id,
1261
+ cron: timerSpec.cron,
1262
+ error: cronEither.left.message
1263
+ }, {
1264
+ F: __dxlog_file3,
1265
+ L: 490,
1266
+ S: this,
1267
+ C: (f, a) => f(...a)
1268
+ });
1269
+ }
1270
+ }
1271
+ }
1272
+ log4("Updated scheduled triggers", {
1273
+ count: this._scheduledTriggers.size
1274
+ }, {
1275
+ F: __dxlog_file3,
1276
+ L: 499,
1277
+ S: this,
1278
+ C: (f, a) => f(...a)
1279
+ });
1280
+ }).pipe(Effect10.withSpan("TriggerDispatcher.refreshTriggers"));
1281
+ _fetchTriggers = () => Effect10.gen(this, function* () {
1282
+ const { objects } = yield* DatabaseService.runQuery(Filter.type(Trigger_exports.Trigger));
1283
+ return objects;
1284
+ }).pipe(Effect10.withSpan("TriggerDispatcher.fetchTriggers"));
1285
+ _startNaturalTimeProcessing = () => Effect10.gen(this, function* () {
1286
+ yield* this.invokeScheduledTriggers();
1287
+ }).pipe(Effect10.repeat(Schedule.fixed(this.livePollInterval)), Effect10.asVoid);
1288
+ _prepareInputData = (trigger, event) => {
1289
+ return createInvocationPayload(trigger, event);
1290
+ };
447
1291
  };
1292
+ var KEY_QUEUE_CURSOR = "dxos.org/key/local-trigger-dispatcher/queue-cursor";
448
1293
  export {
449
- AiService,
1294
+ ComputeEvent,
1295
+ ComputeEventLogger,
1296
+ ComputeEventPayload,
450
1297
  ConfiguredCredentialsService,
1298
+ ContextQueueService,
451
1299
  CredentialsService,
452
1300
  DatabaseService,
453
- EmailTriggerOutput,
454
- EventLogger,
1301
+ Example,
1302
+ FUNCTIONS_META_KEY,
455
1303
  FUNCTIONS_PRESET_META_KEY,
456
- FUNCTION_TYPES,
457
- FunctionCallService,
1304
+ Function_exports as Function,
1305
+ FunctionDefinition,
1306
+ FunctionError,
458
1307
  FunctionExecutor,
459
- FunctionManifestSchema,
460
- FunctionTrigger,
461
- FunctionTriggerSchema,
462
- FunctionType,
1308
+ FunctionImplementationResolver,
1309
+ FunctionInvocationService,
1310
+ FunctionNotFoundError,
463
1311
  InvocationOutcome,
464
1312
  InvocationTraceEndEvent,
465
1313
  InvocationTraceEventType,
466
1314
  InvocationTraceStartEvent,
1315
+ InvocationTracer,
1316
+ LocalFunctionExecutionService,
1317
+ MESSAGE_PROPERTY_TOOL_CALL_ID,
467
1318
  QueueService,
468
- QueueTriggerOutput,
1319
+ RemoteFunctionExecutionService,
469
1320
  SERVICE_TAGS,
470
- ScriptType,
1321
+ Script_exports as Script,
471
1322
  ServiceContainer,
472
- SubscriptionTriggerOutput,
473
- TimerTriggerOutput,
474
- ToolResolverService,
1323
+ ServiceNotAvailableError,
475
1324
  TraceEvent,
476
1325
  TraceEventException,
477
1326
  TraceEventLog,
478
1327
  TracingService,
479
- TriggerKind,
480
- TriggerSchema,
481
- WebhookTriggerOutput,
1328
+ Trigger_exports as Trigger,
1329
+ TriggerDispatcher,
1330
+ TriggerEvent_exports as TriggerEvent,
1331
+ TriggerState,
1332
+ TriggerStateNotFoundError,
1333
+ TriggerStateStore,
482
1334
  createDefectLogger,
483
1335
  createEventLogger,
484
1336
  createInvocationSpans,
485
1337
  defineFunction,
1338
+ deserializeFunction,
486
1339
  getInvocationUrl,
487
- getUserFunctionUrlInMetadata,
1340
+ getUserFunctionIdInMetadata,
488
1341
  logCustomEvent,
489
- makeFunctionUrl,
490
- setUserFunctionUrlInMetadata
1342
+ serializeFunction,
1343
+ setUserFunctionIdInMetadata,
1344
+ withAuthorization
491
1345
  };
492
1346
  //# sourceMappingURL=index.mjs.map