@dxos/functions 0.8.2-main.2f9c567 → 0.8.2-main.36232bc

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 (168) hide show
  1. package/dist/lib/browser/bundler/index.mjs +3 -0
  2. package/dist/lib/browser/bundler/index.mjs.map +1 -1
  3. package/dist/lib/browser/chunk-2YE6S7XY.mjs +360 -0
  4. package/dist/lib/browser/chunk-2YE6S7XY.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-7CHDHCV3.mjs +482 -0
  6. package/dist/lib/browser/chunk-7CHDHCV3.mjs.map +7 -0
  7. package/dist/lib/browser/chunk-LT4LR4VU.mjs +72 -0
  8. package/dist/lib/browser/chunk-LT4LR4VU.mjs.map +7 -0
  9. package/dist/lib/browser/chunk-XRCXIG74.mjs +12 -0
  10. package/dist/lib/browser/chunk-XRCXIG74.mjs.map +7 -0
  11. package/dist/lib/browser/edge/index.mjs +7 -63
  12. package/dist/lib/browser/edge/index.mjs.map +4 -4
  13. package/dist/lib/browser/index.mjs +99 -367
  14. package/dist/lib/browser/index.mjs.map +4 -4
  15. package/dist/lib/browser/meta.json +1 -1
  16. package/dist/lib/browser/testing/index.mjs +670 -0
  17. package/dist/lib/browser/testing/index.mjs.map +7 -0
  18. package/dist/lib/browser/types/index.mjs +51 -0
  19. package/dist/lib/browser/types/index.mjs.map +7 -0
  20. package/dist/lib/node/bundler/index.cjs +1 -0
  21. package/dist/lib/node/bundler/index.cjs.map +1 -1
  22. package/dist/lib/node/chunk-FBIUZ7SD.cjs +496 -0
  23. package/dist/lib/node/chunk-FBIUZ7SD.cjs.map +7 -0
  24. package/dist/lib/node/chunk-JEQ2X3Z6.cjs +34 -0
  25. package/dist/lib/node/chunk-JEQ2X3Z6.cjs.map +7 -0
  26. package/dist/lib/node/chunk-NXZNXVT3.cjs +94 -0
  27. package/dist/lib/node/chunk-NXZNXVT3.cjs.map +7 -0
  28. package/dist/lib/node/chunk-SV5NRE5L.cjs +395 -0
  29. package/dist/lib/node/chunk-SV5NRE5L.cjs.map +7 -0
  30. package/dist/lib/node/edge/index.cjs +5 -65
  31. package/dist/lib/node/edge/index.cjs.map +4 -4
  32. package/dist/lib/node/index.cjs +94 -382
  33. package/dist/lib/node/index.cjs.map +4 -4
  34. package/dist/lib/node/meta.json +1 -1
  35. package/dist/lib/node/testing/index.cjs +687 -0
  36. package/dist/lib/node/testing/index.cjs.map +7 -0
  37. package/dist/lib/node/types/index.cjs +72 -0
  38. package/dist/lib/node/types/index.cjs.map +7 -0
  39. package/dist/lib/node-esm/bundler/index.mjs +1 -0
  40. package/dist/lib/node-esm/bundler/index.mjs.map +1 -1
  41. package/dist/lib/node-esm/chunk-3XMJFSID.mjs +360 -0
  42. package/dist/lib/node-esm/chunk-3XMJFSID.mjs.map +7 -0
  43. package/dist/lib/node-esm/chunk-C6YINTWG.mjs +482 -0
  44. package/dist/lib/node-esm/chunk-C6YINTWG.mjs.map +7 -0
  45. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs +12 -0
  46. package/dist/lib/node-esm/chunk-DHGBFXSZ.mjs.map +7 -0
  47. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs +72 -0
  48. package/dist/lib/node-esm/chunk-O2SXVYU5.mjs.map +7 -0
  49. package/dist/lib/node-esm/edge/index.mjs +6 -64
  50. package/dist/lib/node-esm/edge/index.mjs.map +4 -4
  51. package/dist/lib/node-esm/index.mjs +97 -367
  52. package/dist/lib/node-esm/index.mjs.map +4 -4
  53. package/dist/lib/node-esm/meta.json +1 -1
  54. package/dist/lib/node-esm/testing/index.mjs +670 -0
  55. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  56. package/dist/lib/node-esm/types/index.mjs +51 -0
  57. package/dist/lib/node-esm/types/index.mjs.map +7 -0
  58. package/dist/types/src/browser/index.d.ts +2 -0
  59. package/dist/types/src/browser/index.d.ts.map +1 -0
  60. package/dist/types/src/edge/index.d.ts.map +1 -1
  61. package/dist/types/src/function/function-registry.d.ts +25 -0
  62. package/dist/types/src/function/function-registry.d.ts.map +1 -0
  63. package/dist/types/src/function/function-registry.test.d.ts +2 -0
  64. package/dist/types/src/function/function-registry.test.d.ts.map +1 -0
  65. package/dist/types/src/function/index.d.ts +2 -0
  66. package/dist/types/src/function/index.d.ts.map +1 -0
  67. package/dist/types/src/handler.d.ts +61 -12
  68. package/dist/types/src/handler.d.ts.map +1 -1
  69. package/dist/types/src/index.d.ts +3 -3
  70. package/dist/types/src/index.d.ts.map +1 -1
  71. package/dist/types/src/runtime/dev-server.d.ts +52 -0
  72. package/dist/types/src/runtime/dev-server.d.ts.map +1 -0
  73. package/dist/types/src/runtime/dev-server.test.d.ts +2 -0
  74. package/dist/types/src/runtime/dev-server.test.d.ts.map +1 -0
  75. package/dist/types/src/runtime/index.d.ts +3 -0
  76. package/dist/types/src/runtime/index.d.ts.map +1 -0
  77. package/dist/types/src/runtime/scheduler.d.ts +34 -0
  78. package/dist/types/src/runtime/scheduler.d.ts.map +1 -0
  79. package/dist/types/src/runtime/scheduler.test.d.ts +2 -0
  80. package/dist/types/src/runtime/scheduler.test.d.ts.map +1 -0
  81. package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
  82. package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
  83. package/dist/types/src/testing/index.d.ts +5 -0
  84. package/dist/types/src/testing/index.d.ts.map +1 -0
  85. package/dist/types/src/testing/manifest.d.ts +3 -0
  86. package/dist/types/src/testing/manifest.d.ts.map +1 -0
  87. package/dist/types/src/testing/plugin-init.d.ts +6 -0
  88. package/dist/types/src/testing/plugin-init.d.ts.map +1 -0
  89. package/dist/types/src/testing/setup.d.ts +15 -0
  90. package/dist/types/src/testing/setup.d.ts.map +1 -0
  91. package/dist/types/src/testing/test/handler.d.ts +4 -0
  92. package/dist/types/src/testing/test/handler.d.ts.map +1 -0
  93. package/dist/types/src/testing/test/index.d.ts +3 -0
  94. package/dist/types/src/testing/test/index.d.ts.map +1 -0
  95. package/dist/types/src/testing/types.d.ts +10 -0
  96. package/dist/types/src/testing/types.d.ts.map +1 -0
  97. package/dist/types/src/testing/util.d.ts +5 -0
  98. package/dist/types/src/testing/util.d.ts.map +1 -0
  99. package/dist/types/src/trigger/index.d.ts +3 -0
  100. package/dist/types/src/trigger/index.d.ts.map +1 -0
  101. package/dist/types/src/trigger/trigger-registry.d.ts +38 -0
  102. package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
  103. package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
  104. package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
  105. package/dist/types/src/trigger/type/index.d.ts +3 -0
  106. package/dist/types/src/trigger/type/index.d.ts.map +1 -0
  107. package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
  108. package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
  109. package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
  110. package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
  111. package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
  112. package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
  113. package/dist/types/src/types/index.d.ts +5 -0
  114. package/dist/types/src/types/index.d.ts.map +1 -0
  115. package/dist/types/src/types/schema.d.ts +53 -0
  116. package/dist/types/src/types/schema.d.ts.map +1 -0
  117. package/dist/types/src/types/trace.d.ts +146 -0
  118. package/dist/types/src/types/trace.d.ts.map +1 -0
  119. package/dist/types/src/{types.d.ts → types/types.d.ts} +49 -191
  120. package/dist/types/src/types/types.d.ts.map +1 -0
  121. package/dist/types/src/types/url.d.ts.map +1 -0
  122. package/dist/types/tools/schema.d.ts +2 -0
  123. package/dist/types/tools/schema.d.ts.map +1 -0
  124. package/package.json +36 -20
  125. package/schema/functions.json +211 -0
  126. package/src/browser/index.ts +5 -0
  127. package/src/edge/index.ts +0 -4
  128. package/src/function/function-registry.test.ts +118 -0
  129. package/src/function/function-registry.ts +104 -0
  130. package/src/function/index.ts +5 -0
  131. package/src/handler.ts +118 -14
  132. package/src/index.ts +5 -4
  133. package/src/runtime/dev-server.test.ts +79 -0
  134. package/src/runtime/dev-server.ts +240 -0
  135. package/src/runtime/index.ts +6 -0
  136. package/src/runtime/scheduler.test.ts +152 -0
  137. package/src/runtime/scheduler.ts +170 -0
  138. package/src/testing/functions-integration.test.ts +65 -0
  139. package/src/testing/index.ts +8 -0
  140. package/src/testing/manifest.ts +15 -0
  141. package/src/testing/plugin-init.ts +20 -0
  142. package/src/testing/setup.ts +109 -0
  143. package/src/testing/test/handler.ts +15 -0
  144. package/src/testing/test/index.ts +7 -0
  145. package/src/testing/types.ts +9 -0
  146. package/src/testing/util.ts +26 -0
  147. package/src/translations.ts +1 -1
  148. package/src/trigger/index.ts +6 -0
  149. package/src/trigger/trigger-registry.test.ts +278 -0
  150. package/src/trigger/trigger-registry.ts +218 -0
  151. package/src/trigger/type/index.ts +7 -0
  152. package/src/trigger/type/subscription-trigger.ts +84 -0
  153. package/src/trigger/type/timer-trigger.ts +48 -0
  154. package/src/trigger/type/webhook-trigger.ts +48 -0
  155. package/src/types/index.ts +8 -0
  156. package/src/types/schema.ts +46 -0
  157. package/src/{trace.ts → types/trace.ts} +31 -33
  158. package/src/types/types.ts +163 -0
  159. package/dist/types/src/schema.d.ts +0 -57
  160. package/dist/types/src/schema.d.ts.map +0 -1
  161. package/dist/types/src/trace.d.ts +0 -148
  162. package/dist/types/src/trace.d.ts.map +0 -1
  163. package/dist/types/src/types.d.ts.map +0 -1
  164. package/dist/types/src/url.d.ts.map +0 -1
  165. package/src/schema.ts +0 -53
  166. package/src/types.ts +0 -214
  167. /package/dist/types/src/{url.d.ts → types/url.d.ts} +0 -0
  168. /package/src/{url.ts → types/url.ts} +0 -0
@@ -1,66 +1,10 @@
1
- // packages/core/functions/src/edge/functions.ts
2
- import { createEdgeIdentity } from "@dxos/client/edge";
3
- import { EdgeHttpClient } from "@dxos/edge-client";
4
- import { invariant } from "@dxos/invariant";
5
- import { log } from "@dxos/log";
6
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/edge/functions.ts";
7
- var uploadWorkerFunction = async ({ client, spaceId, version, source, name, functionId }) => {
8
- const edgeUrl = client.config.values.runtime?.services?.edge?.url;
9
- invariant(edgeUrl, "Edge is not configured.", {
10
- F: __dxlog_file,
11
- L: 33,
12
- S: void 0,
13
- A: [
14
- "edgeUrl",
15
- "'Edge is not configured.'"
16
- ]
17
- });
18
- const edgeClient = new EdgeHttpClient(edgeUrl);
19
- const edgeIdentity = createEdgeIdentity(client);
20
- edgeClient.setIdentity(edgeIdentity);
21
- const response = await edgeClient.uploadFunction({
22
- spaceId,
23
- functionId
24
- }, {
25
- name,
26
- version,
27
- script: source
28
- });
29
- log.info("Uploaded", {
30
- identityKey: edgeIdentity.identityKey,
31
- functionId,
32
- name,
33
- source: source.length,
34
- response
35
- }, {
36
- F: __dxlog_file,
37
- L: 40,
38
- S: void 0,
39
- C: (f, a) => f(...a)
40
- });
41
- return response;
42
- };
43
- var incrementSemverPatch = (version) => {
44
- const [major, minor, patch] = version.split(".");
45
- const patchNum = Number(patch);
46
- invariant(!Number.isNaN(patchNum), `Unexpected function version format: ${version}`, {
47
- F: __dxlog_file,
48
- L: 54,
49
- S: void 0,
50
- A: [
51
- "!Number.isNaN(patchNum)",
52
- "`Unexpected function version format: ${version}`"
53
- ]
54
- });
55
- return [
56
- major,
57
- minor,
58
- String(patchNum + 1)
59
- ].join(".");
60
- };
61
- var publicKeyToDid = (key) => {
62
- return `did:key:${key.toHex()}`;
63
- };
1
+ import "@dxos/node-std/globals";
2
+ import {
3
+ incrementSemverPatch,
4
+ publicKeyToDid,
5
+ uploadWorkerFunction
6
+ } from "../chunk-LT4LR4VU.mjs";
7
+ import "../chunk-XRCXIG74.mjs";
64
8
  export {
65
9
  incrementSemverPatch,
66
10
  publicKeyToDid,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../../src/edge/functions.ts"],
4
- "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type DID } from 'iso-did/types';\n\nimport { type Client } from '@dxos/client';\nimport { createEdgeIdentity } from '@dxos/client/edge';\nimport { EdgeHttpClient } from '@dxos/edge-client';\nimport { invariant } from '@dxos/invariant';\nimport type { PublicKey, SpaceId } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type UploadFunctionResponseBody } from '@dxos/protocols';\n\nexport type UploadWorkerArgs = {\n client: Client;\n spaceId: SpaceId;\n source: string;\n version: string;\n name?: string;\n functionId?: string;\n};\n\nexport const uploadWorkerFunction = async ({\n client,\n spaceId,\n version,\n source,\n name,\n functionId,\n}: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {\n const edgeUrl = client.config.values.runtime?.services?.edge?.url;\n invariant(edgeUrl, 'Edge is not configured.');\n const edgeClient = new EdgeHttpClient(edgeUrl);\n const edgeIdentity = createEdgeIdentity(client);\n edgeClient.setIdentity(edgeIdentity);\n const response = await edgeClient.uploadFunction({ spaceId, functionId }, { name, version, script: source });\n\n // TODO(burdon): Edge service log.\n log.info('Uploaded', {\n identityKey: edgeIdentity.identityKey,\n functionId,\n name,\n source: source.length,\n response,\n });\n\n return response;\n};\n\nexport const incrementSemverPatch = (version: string): string => {\n const [major, minor, patch] = version.split('.');\n const patchNum = Number(patch);\n invariant(!Number.isNaN(patchNum), `Unexpected function version format: ${version}`);\n return [major, minor, String(patchNum + 1)].join('.');\n};\n\n// TODO(burdon): Factor out.\nexport const publicKeyToDid = (key: PublicKey): DID => {\n return `did:key:${key.toHex()}`;\n};\n"],
5
- "mappings": ";AAOA,SAASA,0BAA0B;AACnC,SAASC,sBAAsB;AAC/B,SAASC,iBAAiB;AAE1B,SAASC,WAAW;;AAYb,IAAMC,uBAAuB,OAAO,EACzCC,QACAC,SACAC,SACAC,QACAC,MACAC,WAAU,MACO;AACjB,QAAMC,UAAUN,OAAOO,OAAOC,OAAOC,SAASC,UAAUC,MAAMC;AAC9Df,YAAUS,SAAS,2BAAA;;;;;;;;;AACnB,QAAMO,aAAa,IAAIjB,eAAeU,OAAAA;AACtC,QAAMQ,eAAenB,mBAAmBK,MAAAA;AACxCa,aAAWE,YAAYD,YAAAA;AACvB,QAAME,WAAW,MAAMH,WAAWI,eAAe;IAAEhB;IAASI;EAAW,GAAG;IAAED;IAAMF;IAASgB,QAAQf;EAAO,CAAA;AAG1GL,MAAIqB,KAAK,YAAY;IACnBC,aAAaN,aAAaM;IAC1Bf;IACAD;IACAD,QAAQA,OAAOkB;IACfL;EACF,GAAA;;;;;;AAEA,SAAOA;AACT;AAEO,IAAMM,uBAAuB,CAACpB,YAAAA;AACnC,QAAM,CAACqB,OAAOC,OAAOC,KAAAA,IAASvB,QAAQwB,MAAM,GAAA;AAC5C,QAAMC,WAAWC,OAAOH,KAAAA;AACxB5B,YAAU,CAAC+B,OAAOC,MAAMF,QAAAA,GAAW,uCAAuCzB,OAAAA,IAAS;;;;;;;;;AACnF,SAAO;IAACqB;IAAOC;IAAOM,OAAOH,WAAW,CAAA;IAAII,KAAK,GAAA;AACnD;AAGO,IAAMC,iBAAiB,CAACC,QAAAA;AAC7B,SAAO,WAAWA,IAAIC,MAAK,CAAA;AAC7B;",
6
- "names": ["createEdgeIdentity", "EdgeHttpClient", "invariant", "log", "uploadWorkerFunction", "client", "spaceId", "version", "source", "name", "functionId", "edgeUrl", "config", "values", "runtime", "services", "edge", "url", "edgeClient", "edgeIdentity", "setIdentity", "response", "uploadFunction", "script", "info", "identityKey", "length", "incrementSemverPatch", "major", "minor", "patch", "split", "patchNum", "Number", "isNaN", "String", "join", "publicKeyToDid", "key", "toHex"]
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
7
  }
@@ -1,5 +1,47 @@
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
+
1
39
  // packages/core/functions/src/handler.ts
2
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";
3
45
  var defineFunction = (params) => {
4
46
  if (!S.isSchema(params.inputSchema)) {
5
47
  throw new Error("Input schema must be a valid schema");
@@ -14,384 +56,71 @@ var defineFunction = (params) => {
14
56
  handler: params.handler
15
57
  };
16
58
  };
17
-
18
- // packages/core/functions/src/schema.ts
19
- import { Schema } from "effect";
20
- import { EchoObject, JsonSchemaType, LabelAnnotationId, Ref, TypedObject } from "@dxos/echo-schema";
21
- import { DataType } from "@dxos/schema";
22
- var ScriptType = Schema.Struct({
23
- name: Schema.optional(Schema.String),
24
- description: Schema.optional(Schema.String),
25
- // TODO(burdon): Change to hash of deployed content.
26
- // Whether source has changed since last deploy.
27
- changed: Schema.optional(Schema.Boolean),
28
- source: Ref(DataType.Text)
29
- }).annotations({
30
- [LabelAnnotationId]: "name"
31
- }).pipe(EchoObject({
32
- typename: "dxos.org/type/Script",
33
- version: "0.1.0"
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: Schema.NonEmptyString,
41
- version: Schema.String,
42
- description: Schema.optional(Schema.String),
43
- // Reference to a source script if it exists within ECHO.
44
- // TODO(burdon): Don't ref ScriptType directly (core).
45
- source: Schema.optional(Ref(ScriptType)),
46
- inputSchema: Schema.optional(JsonSchemaType),
47
- outputSchema: Schema.optional(JsonSchemaType),
48
- // Local binding to a function name.
49
- binding: Schema.optional(Schema.String)
50
- }) {
51
- };
52
-
53
- // packages/core/functions/src/trace.ts
54
- import { Schema as Schema2 } from "effect";
55
- import { EchoObject as EchoObject2, Expando as Expando2, ObjectId, Ref as Ref3 } from "@dxos/echo-schema";
56
- import { log } from "@dxos/log";
57
-
58
- // packages/core/functions/src/types.ts
59
- import { Schema as S2, SchemaAST } from "effect";
60
- import { Expando, OptionsAnnotationId, TypedObject as TypedObject2, DXN, Ref as Ref2, RawObject } from "@dxos/echo-schema";
61
- var TriggerKind;
62
- (function(TriggerKind2) {
63
- TriggerKind2["Timer"] = "timer";
64
- TriggerKind2["Webhook"] = "webhook";
65
- TriggerKind2["Subscription"] = "subscription";
66
- TriggerKind2["Email"] = "email";
67
- TriggerKind2["Queue"] = "queue";
68
- })(TriggerKind || (TriggerKind = {}));
69
- var typeLiteralAnnotations = {
70
- [SchemaAST.TitleAnnotationId]: "Type"
71
- };
72
- var TimerTriggerSchema = S2.Struct({
73
- type: S2.Literal("timer").annotations(typeLiteralAnnotations),
74
- cron: S2.String.annotations({
75
- [SchemaAST.TitleAnnotationId]: "Cron",
76
- [SchemaAST.ExamplesAnnotationId]: [
77
- "0 0 * * *"
78
- ]
79
- })
80
- }).pipe(S2.mutable);
81
- var EmailTriggerSchema = S2.Struct({
82
- type: S2.Literal("email").annotations(typeLiteralAnnotations)
83
- }).pipe(S2.mutable);
84
- var QueueTriggerSchema = S2.Struct({
85
- type: S2.Literal("queue").annotations(typeLiteralAnnotations),
86
- queue: DXN
87
- }).pipe(S2.mutable);
88
- var WebhookTriggerSchema = S2.Struct({
89
- type: S2.Literal("webhook").annotations(typeLiteralAnnotations),
90
- method: S2.optional(S2.String.annotations({
91
- [SchemaAST.TitleAnnotationId]: "Method",
92
- [OptionsAnnotationId]: [
93
- "GET",
94
- "POST"
95
- ]
96
- })),
97
- port: S2.optional(S2.Number.annotations({
98
- [SchemaAST.TitleAnnotationId]: "Port"
99
- }))
100
- }).pipe(S2.mutable);
101
- var QuerySchema = S2.Struct({
102
- type: S2.optional(S2.String.annotations({
103
- [SchemaAST.TitleAnnotationId]: "Type"
104
- })),
105
- props: S2.optional(S2.Record({
106
- key: S2.String,
107
- value: S2.Any
108
- }))
109
- }).annotations({
110
- [SchemaAST.TitleAnnotationId]: "Query"
111
- });
112
- var SubscriptionTriggerSchema = S2.Struct({
113
- type: S2.Literal("subscription").annotations(typeLiteralAnnotations),
114
- // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
115
- filter: QuerySchema,
116
- options: S2.optional(S2.Struct({
117
- // Watch changes to object (not just creation).
118
- deep: S2.optional(S2.Boolean.annotations({
119
- [SchemaAST.TitleAnnotationId]: "Nested"
120
- })),
121
- // Debounce changes (delay in ms).
122
- delay: S2.optional(S2.Number.annotations({
123
- [SchemaAST.TitleAnnotationId]: "Delay"
124
- }))
125
- }).annotations({
126
- [SchemaAST.TitleAnnotationId]: "Options"
127
- }))
128
- }).pipe(S2.mutable);
129
- var TriggerSchema = S2.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
130
- [SchemaAST.TitleAnnotationId]: "Trigger"
131
- });
132
- var EmailTriggerOutput = S2.mutable(S2.Struct({
133
- from: S2.String,
134
- to: S2.String,
135
- subject: S2.String,
136
- created: S2.String,
137
- body: S2.String
138
- }));
139
- var WebhookTriggerOutput = S2.mutable(S2.Struct({
140
- url: S2.String,
141
- method: S2.Literal("GET", "POST"),
142
- headers: S2.Record({
143
- key: S2.String,
144
- value: S2.String
145
- }),
146
- bodyText: S2.String
147
- }));
148
- var QueueTriggerOutput = S2.mutable(S2.Struct({
149
- queue: DXN,
150
- item: S2.Any,
151
- cursor: S2.String
152
- }));
153
- var SubscriptionTriggerOutput = S2.mutable(S2.Struct({
154
- type: S2.String,
155
- changedObjectId: S2.String
156
- }));
157
- var TimerTriggerOutput = S2.mutable(S2.Struct({
158
- tick: S2.Number
159
- }));
160
- var FunctionTriggerSchema = S2.Struct({
161
- /**
162
- * Function or workflow to invoke.
163
- */
164
- // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
165
- function: S2.optional(Ref2(Expando).annotations({
166
- [SchemaAST.TitleAnnotationId]: "Function"
167
- })),
168
- /**
169
- * Only used for workflows.
170
- * Specifies the input node in the circuit.
171
- * @deprecated Remove and enforce a single input node in all compute graphs.
172
- */
173
- inputNodeId: S2.optional(S2.String.annotations({
174
- [SchemaAST.TitleAnnotationId]: "Input Node ID"
175
- })),
176
- enabled: S2.optional(S2.Boolean.annotations({
177
- [SchemaAST.TitleAnnotationId]: "Enabled"
178
- })),
179
- spec: S2.optional(TriggerSchema),
180
- /**
181
- * Passed as the input data to the function.
182
- * Must match the function's input schema.
183
- *
184
- * @example
185
- * {
186
- * item: '{{$.trigger.event}}',
187
- * instructions: 'Summarize and perform entity-extraction'
188
- * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
189
- * }
190
- */
191
- input: S2.optional(S2.mutable(S2.Record({
192
- key: S2.String,
193
- value: S2.Any
194
- })))
195
- });
196
- var FunctionTrigger = class extends TypedObject2({
197
- typename: "dxos.org/type/FunctionTrigger",
198
- version: "0.1.0"
199
- })(FunctionTriggerSchema.fields) {
200
- };
201
- var FunctionManifestSchema = S2.Struct({
202
- functions: S2.optional(S2.mutable(S2.Array(RawObject(FunctionType)))),
203
- triggers: S2.optional(S2.mutable(S2.Array(RawObject(FunctionTrigger))))
204
- });
205
- var FUNCTION_TYPES = [
206
- FunctionType,
207
- FunctionTrigger
208
- ];
209
-
210
- // packages/core/functions/src/trace.ts
211
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/trace.ts";
212
- var InvocationOutcome;
213
- (function(InvocationOutcome2) {
214
- InvocationOutcome2["SUCCESS"] = "success";
215
- InvocationOutcome2["FAILURE"] = "failure";
216
- InvocationOutcome2["PENDING"] = "pending";
217
- })(InvocationOutcome || (InvocationOutcome = {}));
218
- var InvocationTraceEventType;
219
- (function(InvocationTraceEventType2) {
220
- InvocationTraceEventType2["START"] = "start";
221
- InvocationTraceEventType2["END"] = "end";
222
- })(InvocationTraceEventType || (InvocationTraceEventType = {}));
223
- var TraceEventException = Schema2.Struct({
224
- timestampMs: Schema2.Number,
225
- message: Schema2.String,
226
- name: Schema2.String,
227
- stack: Schema2.optional(Schema2.String)
228
- });
229
- var InvocationTraceStartEvent = Schema2.Struct({
230
- /**
231
- * Queue message id.
232
- */
233
- id: ObjectId,
234
- type: Schema2.Literal("start"),
235
- /**
236
- * Invocation id, the same for invocation start and end events.
237
- */
238
- invocationId: ObjectId,
239
- /**
240
- * Event generation time.
241
- */
242
- timestampMs: Schema2.Number,
243
- /**
244
- * Data passed to function / workflow as an argument.
245
- */
246
- // TODO(burdon): Input schema?
247
- input: Schema2.Object,
248
- /**
249
- * Queue DXN for function/workflow invocation events.
250
- */
251
- // TODO(burdon): Need reference type for queue. vs. string?
252
- invocationTraceQueue: Ref3(Expando2),
253
- /**
254
- * DXN of the invoked function/workflow.
255
- */
256
- invocationTarget: Ref3(Expando2),
257
- /**
258
- * Present for automatic invocations.
259
- */
260
- trigger: Schema2.optional(Ref3(FunctionTrigger))
261
- }).pipe(EchoObject2({
262
- typename: "dxos.org/type/InvocationTraceStart",
263
- version: "0.1.0"
264
- }));
265
- var InvocationTraceEndEvent = Schema2.Struct({
266
- /**
267
- * Trace event id.
268
- */
269
- id: ObjectId,
270
- type: Schema2.Literal("end"),
271
- /**
272
- * Invocation id, will be the same for invocation start and end.
273
- */
274
- invocationId: ObjectId,
275
- /**
276
- * Event generation time.
277
- */
278
- // TODO(burdon): Remove ms suffix.
279
- timestampMs: Schema2.Number,
280
- outcome: Schema2.Enums(InvocationOutcome),
281
- exception: Schema2.optional(TraceEventException)
282
- }).pipe(EchoObject2({
283
- typename: "dxos.org/type/InvocationTraceEnd",
284
- version: "0.1.0"
285
- }));
286
- var TraceEventLog = Schema2.Struct({
287
- timestampMs: Schema2.Number,
288
- level: Schema2.String,
289
- message: Schema2.String,
290
- context: Schema2.optional(Schema2.Object)
291
- });
292
- var TraceEvent = Schema2.Struct({
293
- id: ObjectId,
294
- // TODO(burdon): Need enum/numeric result (not string).
295
- outcome: Schema2.String,
296
- truncated: Schema2.Boolean,
297
- /**
298
- * Time when the event was persisted.
299
- */
300
- ingestionTimestampMs: Schema2.Number,
301
- logs: Schema2.Array(TraceEventLog),
302
- exceptions: Schema2.Array(TraceEventException)
303
- }).pipe(EchoObject2({
304
- typename: "dxos.org/type/TraceEvent",
305
- version: "0.1.0"
306
- }));
307
- var createInvocationSpans = (items) => {
308
- if (!items) {
309
- return [];
310
- }
311
- const eventsByInvocationId = /* @__PURE__ */ new Map();
312
- for (const event of items) {
313
- if (!("invocationId" in event)) {
314
- continue;
315
- }
316
- const invocationId = event.invocationId;
317
- const entry = eventsByInvocationId.get(invocationId) || {
318
- start: void 0,
319
- end: void 0
320
- };
321
- if (event.type === "start") {
322
- entry.start = event;
323
- } else if (event.type === "end") {
324
- entry.end = event;
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);
325
71
  }
326
- eventsByInvocationId.set(invocationId, entry);
327
- }
328
- const now = Date.now();
329
- const result = [];
330
- for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
331
- if (!start) {
332
- log.warn("found end event without matching start", {
333
- invocationId
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
334
77
  }, {
335
78
  F: __dxlog_file,
336
- L: 160,
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
87
+ }, {
88
+ F: __dxlog_file,
89
+ L: 195,
337
90
  S: void 0,
338
91
  C: (f, a) => f(...a)
339
92
  });
340
- continue;
341
93
  }
342
- const isInProgress = end === void 0;
343
- result.push({
344
- id: invocationId,
345
- timestampMs: start.timestampMs,
346
- durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
347
- outcome: end?.outcome ?? "pending",
348
- exception: end?.exception,
349
- input: start.input,
350
- invocationTraceQueue: start.invocationTraceQueue,
351
- invocationTarget: start.invocationTarget,
352
- trigger: start.trigger
94
+ return handler({
95
+ event: {
96
+ data: {
97
+ ...data,
98
+ space,
99
+ objects
100
+ }
101
+ },
102
+ context,
103
+ response,
104
+ ...rest
353
105
  });
354
- }
355
- return result;
356
- };
357
-
358
- // packages/core/functions/src/url.ts
359
- var FUNCTIONS_META_KEY = "dxos.org/service/function";
360
- var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
361
- var isSecure = (protocol) => {
362
- return protocol === "https:" || protocol === "wss:";
363
- };
364
- var getUserFunctionUrlInMetadata = (meta) => {
365
- return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
106
+ };
366
107
  };
367
- var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
368
- const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
369
- if (key) {
370
- if (key.id !== functionUrl) {
371
- throw new Error("Metadata mismatch");
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
+ ]);
372
115
  }
373
- } else {
374
- meta.keys.push({
375
- source: FUNCTIONS_META_KEY,
376
- id: functionUrl
377
- });
378
116
  }
379
117
  };
380
- var makeFunctionUrl = (spaceId, fn) => `/${spaceId}/${fn.functionId}`;
381
- var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
382
- const baseUrl = new URL("functions/", edgeUrl);
383
- const relativeUrl = functionUrl.replace(/^\//, "");
384
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
385
- options.spaceId && url.searchParams.set("spaceId", options.spaceId);
386
- options.subjectId && url.searchParams.set("subjectId", options.subjectId);
387
- url.protocol = isSecure(url.protocol) ? "https" : "http";
388
- return url.toString();
389
- };
390
118
  export {
391
- EmailTriggerOutput,
392
119
  FUNCTIONS_PRESET_META_KEY,
393
120
  FUNCTION_TYPES,
121
+ FunctionDef,
394
122
  FunctionManifestSchema,
123
+ FunctionRegistry,
395
124
  FunctionTrigger,
396
125
  FunctionTriggerSchema,
397
126
  FunctionType,
@@ -399,21 +128,24 @@ export {
399
128
  InvocationTraceEndEvent,
400
129
  InvocationTraceEventType,
401
130
  InvocationTraceStartEvent,
402
- QueueTriggerOutput,
403
131
  ScriptType,
404
- SubscriptionTriggerOutput,
405
- TimerTriggerOutput,
406
132
  TraceEvent,
407
133
  TraceEventException,
408
134
  TraceEventLog,
409
135
  TriggerKind,
136
+ TriggerRegistry,
410
137
  TriggerSchema,
411
- WebhookTriggerOutput,
412
138
  createInvocationSpans,
139
+ createSubscriptionTrigger,
140
+ createTimerTrigger,
413
141
  defineFunction,
414
142
  getInvocationUrl,
415
143
  getUserFunctionUrlInMetadata,
144
+ incrementSemverPatch,
416
145
  makeFunctionUrl,
417
- setUserFunctionUrlInMetadata
146
+ publicKeyToDid,
147
+ setUserFunctionUrlInMetadata,
148
+ subscriptionHandler,
149
+ uploadWorkerFunction
418
150
  };
419
151
  //# sourceMappingURL=index.mjs.map