@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,68 +1,10 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
-
3
- // packages/core/functions/src/edge/functions.ts
4
- import { createEdgeIdentity } from "@dxos/client/edge";
5
- import { EdgeHttpClient } from "@dxos/edge-client";
6
- import { invariant } from "@dxos/invariant";
7
- import { log } from "@dxos/log";
8
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/edge/functions.ts";
9
- var uploadWorkerFunction = async ({ client, spaceId, version, source, name, functionId }) => {
10
- const edgeUrl = client.config.values.runtime?.services?.edge?.url;
11
- invariant(edgeUrl, "Edge is not configured.", {
12
- F: __dxlog_file,
13
- L: 33,
14
- S: void 0,
15
- A: [
16
- "edgeUrl",
17
- "'Edge is not configured.'"
18
- ]
19
- });
20
- const edgeClient = new EdgeHttpClient(edgeUrl);
21
- const edgeIdentity = createEdgeIdentity(client);
22
- edgeClient.setIdentity(edgeIdentity);
23
- const response = await edgeClient.uploadFunction({
24
- spaceId,
25
- functionId
26
- }, {
27
- name,
28
- version,
29
- script: source
30
- });
31
- log.info("Uploaded", {
32
- identityKey: edgeIdentity.identityKey,
33
- functionId,
34
- name,
35
- source: source.length,
36
- response
37
- }, {
38
- F: __dxlog_file,
39
- L: 40,
40
- S: void 0,
41
- C: (f, a) => f(...a)
42
- });
43
- return response;
44
- };
45
- var incrementSemverPatch = (version) => {
46
- const [major, minor, patch] = version.split(".");
47
- const patchNum = Number(patch);
48
- invariant(!Number.isNaN(patchNum), `Unexpected function version format: ${version}`, {
49
- F: __dxlog_file,
50
- L: 54,
51
- S: void 0,
52
- A: [
53
- "!Number.isNaN(patchNum)",
54
- "`Unexpected function version format: ${version}`"
55
- ]
56
- });
57
- return [
58
- major,
59
- minor,
60
- String(patchNum + 1)
61
- ].join(".");
62
- };
63
- var publicKeyToDid = (key) => {
64
- return `did:key:${key.toHex()}`;
65
- };
2
+ import {
3
+ incrementSemverPatch,
4
+ publicKeyToDid,
5
+ uploadWorkerFunction
6
+ } from "../chunk-O2SXVYU5.mjs";
7
+ import "../chunk-DHGBFXSZ.mjs";
66
8
  export {
67
9
  incrementSemverPatch,
68
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,7 +1,47 @@
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-C6YINTWG.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-3XMJFSID.mjs";
37
+ import "./chunk-DHGBFXSZ.mjs";
2
38
 
3
39
  // packages/core/functions/src/handler.ts
4
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";
5
45
  var defineFunction = (params) => {
6
46
  if (!S.isSchema(params.inputSchema)) {
7
47
  throw new Error("Input schema must be a valid schema");
@@ -16,384 +56,71 @@ var defineFunction = (params) => {
16
56
  handler: params.handler
17
57
  };
18
58
  };
19
-
20
- // packages/core/functions/src/schema.ts
21
- import { Schema } from "effect";
22
- import { EchoObject, JsonSchemaType, LabelAnnotationId, Ref, TypedObject } from "@dxos/echo-schema";
23
- import { DataType } from "@dxos/schema";
24
- var ScriptType = Schema.Struct({
25
- name: Schema.optional(Schema.String),
26
- description: Schema.optional(Schema.String),
27
- // TODO(burdon): Change to hash of deployed content.
28
- // Whether source has changed since last deploy.
29
- changed: Schema.optional(Schema.Boolean),
30
- source: Ref(DataType.Text)
31
- }).annotations({
32
- [LabelAnnotationId]: "name"
33
- }).pipe(EchoObject({
34
- typename: "dxos.org/type/Script",
35
- version: "0.1.0"
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: Schema.NonEmptyString,
43
- version: Schema.String,
44
- description: Schema.optional(Schema.String),
45
- // Reference to a source script if it exists within ECHO.
46
- // TODO(burdon): Don't ref ScriptType directly (core).
47
- source: Schema.optional(Ref(ScriptType)),
48
- inputSchema: Schema.optional(JsonSchemaType),
49
- outputSchema: Schema.optional(JsonSchemaType),
50
- // Local binding to a function name.
51
- binding: Schema.optional(Schema.String)
52
- }) {
53
- };
54
-
55
- // packages/core/functions/src/trace.ts
56
- import { Schema as Schema2 } from "effect";
57
- import { EchoObject as EchoObject2, Expando as Expando2, ObjectId, Ref as Ref3 } from "@dxos/echo-schema";
58
- import { log } from "@dxos/log";
59
-
60
- // packages/core/functions/src/types.ts
61
- import { Schema as S2, SchemaAST } from "effect";
62
- import { Expando, OptionsAnnotationId, TypedObject as TypedObject2, DXN, Ref as Ref2, RawObject } from "@dxos/echo-schema";
63
- var TriggerKind;
64
- (function(TriggerKind2) {
65
- TriggerKind2["Timer"] = "timer";
66
- TriggerKind2["Webhook"] = "webhook";
67
- TriggerKind2["Subscription"] = "subscription";
68
- TriggerKind2["Email"] = "email";
69
- TriggerKind2["Queue"] = "queue";
70
- })(TriggerKind || (TriggerKind = {}));
71
- var typeLiteralAnnotations = {
72
- [SchemaAST.TitleAnnotationId]: "Type"
73
- };
74
- var TimerTriggerSchema = S2.Struct({
75
- type: S2.Literal("timer").annotations(typeLiteralAnnotations),
76
- cron: S2.String.annotations({
77
- [SchemaAST.TitleAnnotationId]: "Cron",
78
- [SchemaAST.ExamplesAnnotationId]: [
79
- "0 0 * * *"
80
- ]
81
- })
82
- }).pipe(S2.mutable);
83
- var EmailTriggerSchema = S2.Struct({
84
- type: S2.Literal("email").annotations(typeLiteralAnnotations)
85
- }).pipe(S2.mutable);
86
- var QueueTriggerSchema = S2.Struct({
87
- type: S2.Literal("queue").annotations(typeLiteralAnnotations),
88
- queue: DXN
89
- }).pipe(S2.mutable);
90
- var WebhookTriggerSchema = S2.Struct({
91
- type: S2.Literal("webhook").annotations(typeLiteralAnnotations),
92
- method: S2.optional(S2.String.annotations({
93
- [SchemaAST.TitleAnnotationId]: "Method",
94
- [OptionsAnnotationId]: [
95
- "GET",
96
- "POST"
97
- ]
98
- })),
99
- port: S2.optional(S2.Number.annotations({
100
- [SchemaAST.TitleAnnotationId]: "Port"
101
- }))
102
- }).pipe(S2.mutable);
103
- var QuerySchema = S2.Struct({
104
- type: S2.optional(S2.String.annotations({
105
- [SchemaAST.TitleAnnotationId]: "Type"
106
- })),
107
- props: S2.optional(S2.Record({
108
- key: S2.String,
109
- value: S2.Any
110
- }))
111
- }).annotations({
112
- [SchemaAST.TitleAnnotationId]: "Query"
113
- });
114
- var SubscriptionTriggerSchema = S2.Struct({
115
- type: S2.Literal("subscription").annotations(typeLiteralAnnotations),
116
- // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
117
- filter: QuerySchema,
118
- options: S2.optional(S2.Struct({
119
- // Watch changes to object (not just creation).
120
- deep: S2.optional(S2.Boolean.annotations({
121
- [SchemaAST.TitleAnnotationId]: "Nested"
122
- })),
123
- // Debounce changes (delay in ms).
124
- delay: S2.optional(S2.Number.annotations({
125
- [SchemaAST.TitleAnnotationId]: "Delay"
126
- }))
127
- }).annotations({
128
- [SchemaAST.TitleAnnotationId]: "Options"
129
- }))
130
- }).pipe(S2.mutable);
131
- var TriggerSchema = S2.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
132
- [SchemaAST.TitleAnnotationId]: "Trigger"
133
- });
134
- var EmailTriggerOutput = S2.mutable(S2.Struct({
135
- from: S2.String,
136
- to: S2.String,
137
- subject: S2.String,
138
- created: S2.String,
139
- body: S2.String
140
- }));
141
- var WebhookTriggerOutput = S2.mutable(S2.Struct({
142
- url: S2.String,
143
- method: S2.Literal("GET", "POST"),
144
- headers: S2.Record({
145
- key: S2.String,
146
- value: S2.String
147
- }),
148
- bodyText: S2.String
149
- }));
150
- var QueueTriggerOutput = S2.mutable(S2.Struct({
151
- queue: DXN,
152
- item: S2.Any,
153
- cursor: S2.String
154
- }));
155
- var SubscriptionTriggerOutput = S2.mutable(S2.Struct({
156
- type: S2.String,
157
- changedObjectId: S2.String
158
- }));
159
- var TimerTriggerOutput = S2.mutable(S2.Struct({
160
- tick: S2.Number
161
- }));
162
- var FunctionTriggerSchema = S2.Struct({
163
- /**
164
- * Function or workflow to invoke.
165
- */
166
- // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
167
- function: S2.optional(Ref2(Expando).annotations({
168
- [SchemaAST.TitleAnnotationId]: "Function"
169
- })),
170
- /**
171
- * Only used for workflows.
172
- * Specifies the input node in the circuit.
173
- * @deprecated Remove and enforce a single input node in all compute graphs.
174
- */
175
- inputNodeId: S2.optional(S2.String.annotations({
176
- [SchemaAST.TitleAnnotationId]: "Input Node ID"
177
- })),
178
- enabled: S2.optional(S2.Boolean.annotations({
179
- [SchemaAST.TitleAnnotationId]: "Enabled"
180
- })),
181
- spec: S2.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: S2.optional(S2.mutable(S2.Record({
194
- key: S2.String,
195
- value: S2.Any
196
- })))
197
- });
198
- var FunctionTrigger = class extends TypedObject2({
199
- typename: "dxos.org/type/FunctionTrigger",
200
- version: "0.1.0"
201
- })(FunctionTriggerSchema.fields) {
202
- };
203
- var FunctionManifestSchema = S2.Struct({
204
- functions: S2.optional(S2.mutable(S2.Array(RawObject(FunctionType)))),
205
- triggers: S2.optional(S2.mutable(S2.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;
215
- (function(InvocationOutcome2) {
216
- InvocationOutcome2["SUCCESS"] = "success";
217
- InvocationOutcome2["FAILURE"] = "failure";
218
- InvocationOutcome2["PENDING"] = "pending";
219
- })(InvocationOutcome || (InvocationOutcome = {}));
220
- var InvocationTraceEventType;
221
- (function(InvocationTraceEventType2) {
222
- InvocationTraceEventType2["START"] = "start";
223
- InvocationTraceEventType2["END"] = "end";
224
- })(InvocationTraceEventType || (InvocationTraceEventType = {}));
225
- var TraceEventException = Schema2.Struct({
226
- timestampMs: Schema2.Number,
227
- message: Schema2.String,
228
- name: Schema2.String,
229
- stack: Schema2.optional(Schema2.String)
230
- });
231
- var InvocationTraceStartEvent = Schema2.Struct({
232
- /**
233
- * Queue message id.
234
- */
235
- id: ObjectId,
236
- type: Schema2.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: Schema2.Number,
245
- /**
246
- * Data passed to function / workflow as an argument.
247
- */
248
- // TODO(burdon): Input schema?
249
- input: Schema2.Object,
250
- /**
251
- * Queue DXN for function/workflow invocation events.
252
- */
253
- // TODO(burdon): Need reference type for queue. vs. string?
254
- invocationTraceQueue: Ref3(Expando2),
255
- /**
256
- * DXN of the invoked function/workflow.
257
- */
258
- invocationTarget: Ref3(Expando2),
259
- /**
260
- * Present for automatic invocations.
261
- */
262
- trigger: Schema2.optional(Ref3(FunctionTrigger))
263
- }).pipe(EchoObject2({
264
- typename: "dxos.org/type/InvocationTraceStart",
265
- version: "0.1.0"
266
- }));
267
- var InvocationTraceEndEvent = Schema2.Struct({
268
- /**
269
- * Trace event id.
270
- */
271
- id: ObjectId,
272
- type: Schema2.Literal("end"),
273
- /**
274
- * Invocation id, will be the same for invocation start and end.
275
- */
276
- invocationId: ObjectId,
277
- /**
278
- * Event generation time.
279
- */
280
- // TODO(burdon): Remove ms suffix.
281
- timestampMs: Schema2.Number,
282
- outcome: Schema2.Enums(InvocationOutcome),
283
- exception: Schema2.optional(TraceEventException)
284
- }).pipe(EchoObject2({
285
- typename: "dxos.org/type/InvocationTraceEnd",
286
- version: "0.1.0"
287
- }));
288
- var TraceEventLog = Schema2.Struct({
289
- timestampMs: Schema2.Number,
290
- level: Schema2.String,
291
- message: Schema2.String,
292
- context: Schema2.optional(Schema2.Object)
293
- });
294
- var TraceEvent = Schema2.Struct({
295
- id: ObjectId,
296
- // TODO(burdon): Need enum/numeric result (not string).
297
- outcome: Schema2.String,
298
- truncated: Schema2.Boolean,
299
- /**
300
- * Time when the event was persisted.
301
- */
302
- ingestionTimestampMs: Schema2.Number,
303
- logs: Schema2.Array(TraceEventLog),
304
- exceptions: Schema2.Array(TraceEventException)
305
- }).pipe(EchoObject2({
306
- typename: "dxos.org/type/TraceEvent",
307
- version: "0.1.0"
308
- }));
309
- var createInvocationSpans = (items) => {
310
- if (!items) {
311
- return [];
312
- }
313
- const eventsByInvocationId = /* @__PURE__ */ new Map();
314
- for (const event of items) {
315
- if (!("invocationId" in event)) {
316
- continue;
317
- }
318
- const invocationId = event.invocationId;
319
- const entry = eventsByInvocationId.get(invocationId) || {
320
- start: void 0,
321
- end: void 0
322
- };
323
- if (event.type === "start") {
324
- entry.start = event;
325
- } else if (event.type === "end") {
326
- 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);
327
71
  }
328
- eventsByInvocationId.set(invocationId, entry);
329
- }
330
- const now = Date.now();
331
- const result = [];
332
- for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
333
- if (!start) {
334
- log.warn("found end event without matching start", {
335
- 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
336
77
  }, {
337
78
  F: __dxlog_file,
338
- 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,
339
90
  S: void 0,
340
91
  C: (f, a) => f(...a)
341
92
  });
342
- continue;
343
93
  }
344
- const isInProgress = end === void 0;
345
- result.push({
346
- id: invocationId,
347
- timestampMs: start.timestampMs,
348
- durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
349
- outcome: end?.outcome ?? "pending",
350
- exception: end?.exception,
351
- input: start.input,
352
- invocationTraceQueue: start.invocationTraceQueue,
353
- invocationTarget: start.invocationTarget,
354
- trigger: start.trigger
94
+ return handler({
95
+ event: {
96
+ data: {
97
+ ...data,
98
+ space,
99
+ objects
100
+ }
101
+ },
102
+ context,
103
+ response,
104
+ ...rest
355
105
  });
356
- }
357
- return result;
358
- };
359
-
360
- // packages/core/functions/src/url.ts
361
- var FUNCTIONS_META_KEY = "dxos.org/service/function";
362
- var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
363
- var isSecure = (protocol) => {
364
- return protocol === "https:" || protocol === "wss:";
365
- };
366
- var getUserFunctionUrlInMetadata = (meta) => {
367
- return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
106
+ };
368
107
  };
369
- var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
370
- const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
371
- if (key) {
372
- if (key.id !== functionUrl) {
373
- 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
+ ]);
374
115
  }
375
- } else {
376
- meta.keys.push({
377
- source: FUNCTIONS_META_KEY,
378
- id: functionUrl
379
- });
380
116
  }
381
117
  };
382
- var makeFunctionUrl = (spaceId, fn) => `/${spaceId}/${fn.functionId}`;
383
- var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
384
- const baseUrl = new URL("functions/", edgeUrl);
385
- const relativeUrl = functionUrl.replace(/^\//, "");
386
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
387
- options.spaceId && url.searchParams.set("spaceId", options.spaceId);
388
- options.subjectId && url.searchParams.set("subjectId", options.subjectId);
389
- url.protocol = isSecure(url.protocol) ? "https" : "http";
390
- return url.toString();
391
- };
392
118
  export {
393
- EmailTriggerOutput,
394
119
  FUNCTIONS_PRESET_META_KEY,
395
120
  FUNCTION_TYPES,
121
+ FunctionDef,
396
122
  FunctionManifestSchema,
123
+ FunctionRegistry,
397
124
  FunctionTrigger,
398
125
  FunctionTriggerSchema,
399
126
  FunctionType,
@@ -401,21 +128,24 @@ export {
401
128
  InvocationTraceEndEvent,
402
129
  InvocationTraceEventType,
403
130
  InvocationTraceStartEvent,
404
- QueueTriggerOutput,
405
131
  ScriptType,
406
- SubscriptionTriggerOutput,
407
- TimerTriggerOutput,
408
132
  TraceEvent,
409
133
  TraceEventException,
410
134
  TraceEventLog,
411
135
  TriggerKind,
136
+ TriggerRegistry,
412
137
  TriggerSchema,
413
- WebhookTriggerOutput,
414
138
  createInvocationSpans,
139
+ createSubscriptionTrigger,
140
+ createTimerTrigger,
415
141
  defineFunction,
416
142
  getInvocationUrl,
417
143
  getUserFunctionUrlInMetadata,
144
+ incrementSemverPatch,
418
145
  makeFunctionUrl,
419
- setUserFunctionUrlInMetadata
146
+ publicKeyToDid,
147
+ setUserFunctionUrlInMetadata,
148
+ subscriptionHandler,
149
+ uploadWorkerFunction
420
150
  };
421
151
  //# sourceMappingURL=index.mjs.map