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

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