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

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