@dxos/functions 0.8.3 → 0.8.4-main.1da679c

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 (161) hide show
  1. package/dist/lib/browser/bundler/index.mjs +73 -44
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-D2XO7XXY.mjs +611 -0
  4. package/dist/lib/browser/chunk-D2XO7XXY.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  6. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  7. package/dist/lib/browser/edge/index.mjs +24 -10
  8. package/dist/lib/browser/edge/index.mjs.map +3 -3
  9. package/dist/lib/browser/index.mjs +981 -137
  10. package/dist/lib/browser/index.mjs.map +4 -4
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/testing/index.mjs +110 -9
  13. package/dist/lib/browser/testing/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/bundler/index.mjs +72 -44
  15. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  16. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  17. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs +613 -0
  19. package/dist/lib/node-esm/chunk-Z4CJ62WS.mjs.map +7 -0
  20. package/dist/lib/node-esm/edge/index.mjs +23 -10
  21. package/dist/lib/node-esm/edge/index.mjs.map +3 -3
  22. package/dist/lib/node-esm/index.mjs +981 -137
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +110 -9
  26. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  27. package/dist/types/src/bundler/bundler.d.ts +12 -14
  28. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  29. package/dist/types/src/edge/functions.d.ts +4 -3
  30. package/dist/types/src/edge/functions.d.ts.map +1 -1
  31. package/dist/types/src/errors.d.ts +137 -0
  32. package/dist/types/src/errors.d.ts.map +1 -0
  33. package/dist/types/src/examples/fib.d.ts +7 -0
  34. package/dist/types/src/examples/fib.d.ts.map +1 -0
  35. package/dist/types/src/examples/index.d.ts +4 -0
  36. package/dist/types/src/examples/index.d.ts.map +1 -0
  37. package/dist/types/src/examples/reply.d.ts +3 -0
  38. package/dist/types/src/examples/reply.d.ts.map +1 -0
  39. package/dist/types/src/examples/sleep.d.ts +5 -0
  40. package/dist/types/src/examples/sleep.d.ts.map +1 -0
  41. package/dist/types/src/executor/executor.d.ts +4 -1
  42. package/dist/types/src/executor/executor.d.ts.map +1 -1
  43. package/dist/types/src/handler.d.ts +46 -16
  44. package/dist/types/src/handler.d.ts.map +1 -1
  45. package/dist/types/src/index.d.ts +3 -0
  46. package/dist/types/src/index.d.ts.map +1 -1
  47. package/dist/types/src/schema.d.ts +8 -3
  48. package/dist/types/src/schema.d.ts.map +1 -1
  49. package/dist/types/src/services/credentials.d.ts +18 -4
  50. package/dist/types/src/services/credentials.d.ts.map +1 -1
  51. package/dist/types/src/services/database.d.ts +55 -3
  52. package/dist/types/src/services/database.d.ts.map +1 -1
  53. package/dist/types/src/services/event-logger.d.ts +65 -30
  54. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  55. package/dist/types/src/services/index.d.ts +2 -2
  56. package/dist/types/src/services/index.d.ts.map +1 -1
  57. package/dist/types/src/services/local-function-execution.d.ts +25 -0
  58. package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
  59. package/dist/types/src/services/queues.d.ts +33 -6
  60. package/dist/types/src/services/queues.d.ts.map +1 -1
  61. package/dist/types/src/services/remote-function-execution-service.d.ts +15 -0
  62. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
  63. package/dist/types/src/services/service-container.d.ts +29 -17
  64. package/dist/types/src/services/service-container.d.ts.map +1 -1
  65. package/dist/types/src/services/service-registry.d.ts +29 -0
  66. package/dist/types/src/services/service-registry.d.ts.map +1 -0
  67. package/dist/types/src/services/service-registry.test.d.ts +2 -0
  68. package/dist/types/src/services/service-registry.test.d.ts.map +1 -0
  69. package/dist/types/src/services/tracing.d.ts +46 -4
  70. package/dist/types/src/services/tracing.d.ts.map +1 -1
  71. package/dist/types/src/testing/index.d.ts +1 -0
  72. package/dist/types/src/testing/index.d.ts.map +1 -1
  73. package/dist/types/src/testing/layer.d.ts +15 -0
  74. package/dist/types/src/testing/layer.d.ts.map +1 -0
  75. package/dist/types/src/testing/logger.d.ts +3 -3
  76. package/dist/types/src/testing/logger.d.ts.map +1 -1
  77. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  78. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  79. package/dist/types/src/testing/services.d.ts +55 -9
  80. package/dist/types/src/testing/services.d.ts.map +1 -1
  81. package/dist/types/src/trace.d.ts +20 -22
  82. package/dist/types/src/trace.d.ts.map +1 -1
  83. package/dist/types/src/translations.d.ts +9 -9
  84. package/dist/types/src/translations.d.ts.map +1 -1
  85. package/dist/types/src/triggers/index.d.ts +4 -0
  86. package/dist/types/src/triggers/index.d.ts.map +1 -0
  87. package/dist/types/src/triggers/input-builder.d.ts +3 -0
  88. package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
  89. package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
  90. package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
  91. package/dist/types/src/triggers/trigger-dispatcher.d.ts +75 -0
  92. package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
  93. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
  94. package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
  95. package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
  96. package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
  97. package/dist/types/src/types.d.ts +55 -245
  98. package/dist/types/src/types.d.ts.map +1 -1
  99. package/dist/types/src/url.d.ts +10 -6
  100. package/dist/types/src/url.d.ts.map +1 -1
  101. package/dist/types/tsconfig.tsbuildinfo +1 -1
  102. package/package.json +35 -25
  103. package/src/bundler/bundler.test.ts +9 -10
  104. package/src/bundler/bundler.ts +56 -35
  105. package/src/edge/functions.ts +9 -6
  106. package/src/errors.ts +21 -0
  107. package/src/examples/fib.ts +30 -0
  108. package/src/examples/index.ts +7 -0
  109. package/src/examples/reply.ts +18 -0
  110. package/src/examples/sleep.ts +22 -0
  111. package/src/executor/executor.ts +22 -15
  112. package/src/handler.ts +117 -27
  113. package/src/index.ts +3 -2
  114. package/src/schema.ts +11 -0
  115. package/src/services/credentials.ts +87 -5
  116. package/src/services/database.ts +146 -3
  117. package/src/services/event-logger.ts +68 -37
  118. package/src/services/index.ts +2 -2
  119. package/src/services/local-function-execution.ts +127 -0
  120. package/src/services/queues.ts +56 -11
  121. package/src/services/remote-function-execution-service.ts +46 -0
  122. package/src/services/service-container.ts +47 -42
  123. package/src/services/service-registry.test.ts +42 -0
  124. package/src/services/service-registry.ts +59 -0
  125. package/src/services/tracing.ts +118 -5
  126. package/src/testing/index.ts +1 -0
  127. package/src/testing/layer.ts +111 -0
  128. package/src/testing/logger.ts +4 -4
  129. package/src/testing/persist-database.test.ts +87 -0
  130. package/src/testing/services.ts +97 -14
  131. package/src/trace.ts +17 -19
  132. package/src/translations.ts +4 -4
  133. package/src/triggers/index.ts +7 -0
  134. package/src/triggers/input-builder.ts +35 -0
  135. package/src/triggers/invocation-tracer.ts +99 -0
  136. package/src/triggers/trigger-dispatcher.test.ts +652 -0
  137. package/src/triggers/trigger-dispatcher.ts +516 -0
  138. package/src/triggers/trigger-state-store.ts +60 -0
  139. package/src/types.ts +39 -36
  140. package/src/url.ts +13 -10
  141. package/dist/lib/browser/chunk-WEFZUEL2.mjs +0 -300
  142. package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +0 -7
  143. package/dist/lib/node/bundler/index.cjs +0 -260
  144. package/dist/lib/node/bundler/index.cjs.map +0 -7
  145. package/dist/lib/node/chunk-IJAE7FZK.cjs +0 -320
  146. package/dist/lib/node/chunk-IJAE7FZK.cjs.map +0 -7
  147. package/dist/lib/node/edge/index.cjs +0 -94
  148. package/dist/lib/node/edge/index.cjs.map +0 -7
  149. package/dist/lib/node/index.cjs +0 -522
  150. package/dist/lib/node/index.cjs.map +0 -7
  151. package/dist/lib/node/meta.json +0 -1
  152. package/dist/lib/node/testing/index.cjs +0 -43
  153. package/dist/lib/node/testing/index.cjs.map +0 -7
  154. package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +0 -302
  155. package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +0 -7
  156. package/dist/types/src/services/ai.d.ts +0 -12
  157. package/dist/types/src/services/ai.d.ts.map +0 -1
  158. package/dist/types/src/services/function-call-service.d.ts +0 -16
  159. package/dist/types/src/services/function-call-service.d.ts.map +0 -1
  160. package/src/services/ai.ts +0 -32
  161. package/src/services/function-call-service.ts +0 -64
@@ -1,94 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var edge_exports = {};
20
- __export(edge_exports, {
21
- incrementSemverPatch: () => incrementSemverPatch,
22
- publicKeyToDid: () => publicKeyToDid,
23
- uploadWorkerFunction: () => uploadWorkerFunction
24
- });
25
- module.exports = __toCommonJS(edge_exports);
26
- var import_edge = require("@dxos/client/edge");
27
- var import_edge_client = require("@dxos/edge-client");
28
- var import_invariant = require("@dxos/invariant");
29
- var import_log = require("@dxos/log");
30
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/edge/functions.ts";
31
- var uploadWorkerFunction = async ({ client, version, source, name, functionId, ownerPublicKey }) => {
32
- const edgeUrl = client.config.values.runtime?.services?.edge?.url;
33
- (0, import_invariant.invariant)(edgeUrl, "Edge is not configured.", {
34
- F: __dxlog_file,
35
- L: 33,
36
- S: void 0,
37
- A: [
38
- "edgeUrl",
39
- "'Edge is not configured.'"
40
- ]
41
- });
42
- const edgeClient = new import_edge_client.EdgeHttpClient(edgeUrl);
43
- const edgeIdentity = (0, import_edge.createEdgeIdentity)(client);
44
- edgeClient.setIdentity(edgeIdentity);
45
- const response = await edgeClient.uploadFunction({
46
- functionId
47
- }, {
48
- name,
49
- version,
50
- script: source,
51
- ownerPublicKey: ownerPublicKey.toHex()
52
- });
53
- import_log.log.info("Uploaded", {
54
- identityKey: edgeIdentity.identityKey,
55
- functionId,
56
- name,
57
- source: source.length,
58
- response
59
- }, {
60
- F: __dxlog_file,
61
- L: 43,
62
- S: void 0,
63
- C: (f, a) => f(...a)
64
- });
65
- return response;
66
- };
67
- var incrementSemverPatch = (version) => {
68
- const [major, minor, patch] = version.split(".");
69
- const patchNum = Number(patch);
70
- (0, import_invariant.invariant)(!Number.isNaN(patchNum), `Unexpected function version format: ${version}`, {
71
- F: __dxlog_file,
72
- L: 57,
73
- S: void 0,
74
- A: [
75
- "!Number.isNaN(patchNum)",
76
- "`Unexpected function version format: ${version}`"
77
- ]
78
- });
79
- return [
80
- major,
81
- minor,
82
- String(patchNum + 1)
83
- ].join(".");
84
- };
85
- var publicKeyToDid = (key) => {
86
- return `did:key:${key.toHex()}`;
87
- };
88
- // Annotate the CommonJS export names for ESM import in node:
89
- 0 && (module.exports = {
90
- incrementSemverPatch,
91
- publicKeyToDid,
92
- uploadWorkerFunction
93
- });
94
- //# sourceMappingURL=index.cjs.map
@@ -1,7 +0,0 @@
1
- {
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 } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type UploadFunctionResponseBody } from '@dxos/protocols';\n\nexport type UploadWorkerArgs = {\n client: Client;\n source: string;\n version: string;\n name?: string;\n functionId?: string;\n ownerPublicKey: PublicKey;\n};\n\nexport const uploadWorkerFunction = async ({\n client,\n version,\n source,\n name,\n functionId,\n ownerPublicKey,\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(\n { functionId },\n { name, version, script: source, ownerPublicKey: ownerPublicKey.toHex() },\n );\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,kBAAmC;AACnC,yBAA+B;AAC/B,uBAA0B;AAE1B,iBAAoB;;AAYb,IAAMA,uBAAuB,OAAO,EACzCC,QACAC,SACAC,QACAC,MACAC,YACAC,eAAc,MACG;AACjB,QAAMC,UAAUN,OAAOO,OAAOC,OAAOC,SAASC,UAAUC,MAAMC;AAC9DC,kCAAUP,SAAS,2BAAA;;;;;;;;;AACnB,QAAMQ,aAAa,IAAIC,kCAAeT,OAAAA;AACtC,QAAMU,mBAAeC,gCAAmBjB,MAAAA;AACxCc,aAAWI,YAAYF,YAAAA;AACvB,QAAMG,WAAW,MAAML,WAAWM,eAChC;IAAEhB;EAAW,GACb;IAAED;IAAMF;IAASoB,QAAQnB;IAAQG,gBAAgBA,eAAeiB,MAAK;EAAG,CAAA;AAI1EC,iBAAIC,KAAK,YAAY;IACnBC,aAAaT,aAAaS;IAC1BrB;IACAD;IACAD,QAAQA,OAAOwB;IACfP;EACF,GAAA;;;;;;AAEA,SAAOA;AACT;AAEO,IAAMQ,uBAAuB,CAAC1B,YAAAA;AACnC,QAAM,CAAC2B,OAAOC,OAAOC,KAAAA,IAAS7B,QAAQ8B,MAAM,GAAA;AAC5C,QAAMC,WAAWC,OAAOH,KAAAA;AACxBjB,kCAAU,CAACoB,OAAOC,MAAMF,QAAAA,GAAW,uCAAuC/B,OAAAA,IAAS;;;;;;;;;AACnF,SAAO;IAAC2B;IAAOC;IAAOM,OAAOH,WAAW,CAAA;IAAII,KAAK,GAAA;AACnD;AAGO,IAAMC,iBAAiB,CAACC,QAAAA;AAC7B,SAAO,WAAWA,IAAIhB,MAAK,CAAA;AAC7B;",
6
- "names": ["uploadWorkerFunction", "client", "version", "source", "name", "functionId", "ownerPublicKey", "edgeUrl", "config", "values", "runtime", "services", "edge", "url", "invariant", "edgeClient", "EdgeHttpClient", "edgeIdentity", "createEdgeIdentity", "setIdentity", "response", "uploadFunction", "script", "toHex", "log", "info", "identityKey", "length", "incrementSemverPatch", "major", "minor", "patch", "split", "patchNum", "Number", "isNaN", "String", "join", "publicKeyToDid", "key"]
7
- }
@@ -1,522 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var node_exports = {};
20
- __export(node_exports, {
21
- AiService: () => import_chunk_IJAE7FZK.AiService,
22
- ConfiguredCredentialsService: () => import_chunk_IJAE7FZK.ConfiguredCredentialsService,
23
- CredentialsService: () => import_chunk_IJAE7FZK.CredentialsService,
24
- DatabaseService: () => import_chunk_IJAE7FZK.DatabaseService,
25
- EmailTriggerOutput: () => EmailTriggerOutput,
26
- EventLogger: () => import_chunk_IJAE7FZK.EventLogger,
27
- FUNCTIONS_PRESET_META_KEY: () => FUNCTIONS_PRESET_META_KEY,
28
- FUNCTION_TYPES: () => FUNCTION_TYPES,
29
- FunctionCallService: () => import_chunk_IJAE7FZK.FunctionCallService,
30
- FunctionExecutor: () => FunctionExecutor,
31
- FunctionManifestSchema: () => FunctionManifestSchema,
32
- FunctionTrigger: () => FunctionTrigger,
33
- FunctionTriggerSchema: () => FunctionTriggerSchema,
34
- FunctionType: () => FunctionType,
35
- InvocationOutcome: () => InvocationOutcome,
36
- InvocationTraceEndEvent: () => InvocationTraceEndEvent,
37
- InvocationTraceEventType: () => InvocationTraceEventType,
38
- InvocationTraceStartEvent: () => InvocationTraceStartEvent,
39
- QueueService: () => import_chunk_IJAE7FZK.QueueService,
40
- QueueTriggerOutput: () => QueueTriggerOutput,
41
- SERVICE_TAGS: () => import_chunk_IJAE7FZK.SERVICE_TAGS,
42
- ScriptType: () => ScriptType,
43
- ServiceContainer: () => import_chunk_IJAE7FZK.ServiceContainer,
44
- SubscriptionTriggerOutput: () => SubscriptionTriggerOutput,
45
- TimerTriggerOutput: () => TimerTriggerOutput,
46
- TraceEvent: () => TraceEvent,
47
- TraceEventException: () => TraceEventException,
48
- TraceEventLog: () => TraceEventLog,
49
- TracingService: () => import_chunk_IJAE7FZK.TracingService,
50
- TriggerKind: () => TriggerKind,
51
- TriggerSchema: () => TriggerSchema,
52
- WebhookTriggerOutput: () => WebhookTriggerOutput,
53
- createDefectLogger: () => import_chunk_IJAE7FZK.createDefectLogger,
54
- createEventLogger: () => import_chunk_IJAE7FZK.createEventLogger,
55
- createInvocationSpans: () => createInvocationSpans,
56
- defineFunction: () => defineFunction,
57
- getInvocationUrl: () => getInvocationUrl,
58
- getUserFunctionUrlInMetadata: () => getUserFunctionUrlInMetadata,
59
- logCustomEvent: () => import_chunk_IJAE7FZK.logCustomEvent,
60
- makeFunctionUrl: () => makeFunctionUrl,
61
- setUserFunctionUrlInMetadata: () => setUserFunctionUrlInMetadata
62
- });
63
- module.exports = __toCommonJS(node_exports);
64
- var import_chunk_IJAE7FZK = require("./chunk-IJAE7FZK.cjs");
65
- var import_effect = require("effect");
66
- var import_effect2 = require("effect");
67
- var import_echo = require("@dxos/echo");
68
- var import_echo_schema = require("@dxos/echo-schema");
69
- var import_schema = require("@dxos/schema");
70
- var import_effect3 = require("effect");
71
- var import_echo2 = require("@dxos/echo");
72
- var import_echo_db = require("@dxos/echo-db");
73
- var import_echo_schema2 = require("@dxos/echo-schema");
74
- var import_log = require("@dxos/log");
75
- var import_effect4 = require("effect");
76
- var import_echo_schema3 = require("@dxos/echo-schema");
77
- var import_keys = require("@dxos/keys");
78
- var import_effect5 = require("effect");
79
- var defineFunction = (params) => {
80
- if (!import_effect.Schema.isSchema(params.inputSchema)) {
81
- throw new Error("Input schema must be a valid schema");
82
- }
83
- if (typeof params.handler !== "function") {
84
- throw new Error("Handler must be a function");
85
- }
86
- return {
87
- description: params.description,
88
- inputSchema: params.inputSchema,
89
- outputSchema: params.outputSchema ?? import_effect.Schema.Any,
90
- handler: params.handler
91
- };
92
- };
93
- var ScriptType = import_effect2.Schema.Struct({
94
- name: import_effect2.Schema.optional(import_effect2.Schema.String),
95
- description: import_effect2.Schema.optional(import_effect2.Schema.String),
96
- // TODO(burdon): Change to hash of deployed content.
97
- // Whether source has changed since last deploy.
98
- changed: import_effect2.Schema.optional(import_effect2.Schema.Boolean),
99
- source: (0, import_echo_schema.Ref)(import_schema.DataType.Text)
100
- }).pipe(import_echo.Type.Obj({
101
- typename: "dxos.org/type/Script",
102
- version: "0.1.0"
103
- }), import_echo_schema.LabelAnnotation.set([
104
- "name"
105
- ]));
106
- var FunctionType = import_effect2.Schema.Struct({
107
- // TODO(burdon): Rename to id/uri?
108
- name: import_effect2.Schema.NonEmptyString,
109
- version: import_effect2.Schema.String,
110
- description: import_effect2.Schema.optional(import_effect2.Schema.String),
111
- // Reference to a source script if it exists within ECHO.
112
- // TODO(burdon): Don't ref ScriptType directly (core).
113
- source: import_effect2.Schema.optional((0, import_echo_schema.Ref)(ScriptType)),
114
- inputSchema: import_effect2.Schema.optional(import_echo_schema.JsonSchemaType),
115
- outputSchema: import_effect2.Schema.optional(import_echo_schema.JsonSchemaType),
116
- // Local binding to a function name.
117
- binding: import_effect2.Schema.optional(import_effect2.Schema.String)
118
- }).pipe(import_echo.Type.Obj({
119
- typename: "dxos.org/type/Function",
120
- version: "0.1.0"
121
- }), import_echo_schema.LabelAnnotation.set([
122
- "name"
123
- ]));
124
- var TriggerKind = /* @__PURE__ */ function(TriggerKind2) {
125
- TriggerKind2["Timer"] = "timer";
126
- TriggerKind2["Webhook"] = "webhook";
127
- TriggerKind2["Subscription"] = "subscription";
128
- TriggerKind2["Email"] = "email";
129
- TriggerKind2["Queue"] = "queue";
130
- return TriggerKind2;
131
- }({});
132
- var kindLiteralAnnotations = {
133
- title: "Kind"
134
- };
135
- var TimerTriggerSchema = import_effect4.Schema.Struct({
136
- kind: import_effect4.Schema.Literal("timer").annotations(kindLiteralAnnotations),
137
- cron: import_effect4.Schema.String.annotations({
138
- title: "Cron",
139
- [import_effect4.SchemaAST.ExamplesAnnotationId]: [
140
- "0 0 * * *"
141
- ]
142
- })
143
- }).pipe(import_effect4.Schema.mutable);
144
- var EmailTriggerSchema = import_effect4.Schema.Struct({
145
- kind: import_effect4.Schema.Literal("email").annotations(kindLiteralAnnotations)
146
- }).pipe(import_effect4.Schema.mutable);
147
- var QueueTriggerSchema = import_effect4.Schema.Struct({
148
- kind: import_effect4.Schema.Literal("queue").annotations(kindLiteralAnnotations),
149
- queue: import_keys.DXN.Schema
150
- }).pipe(import_effect4.Schema.mutable);
151
- var WebhookTriggerSchema = import_effect4.Schema.Struct({
152
- kind: import_effect4.Schema.Literal("webhook").annotations(kindLiteralAnnotations),
153
- method: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
154
- title: "Method",
155
- [import_echo_schema3.OptionsAnnotationId]: [
156
- "GET",
157
- "POST"
158
- ]
159
- })),
160
- port: import_effect4.Schema.optional(import_effect4.Schema.Number.annotations({
161
- title: "Port"
162
- }))
163
- }).pipe(import_effect4.Schema.mutable);
164
- var QuerySchema = import_effect4.Schema.Struct({
165
- type: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
166
- title: "Type"
167
- })),
168
- props: import_effect4.Schema.optional(import_effect4.Schema.Record({
169
- key: import_effect4.Schema.String,
170
- value: import_effect4.Schema.Any
171
- }))
172
- }).annotations({
173
- title: "Query"
174
- });
175
- var SubscriptionTriggerSchema = import_effect4.Schema.Struct({
176
- kind: import_effect4.Schema.Literal("subscription").annotations(kindLiteralAnnotations),
177
- // TODO(burdon): Define query DSL (from ECHO). Reconcile with Table.Query.
178
- filter: QuerySchema,
179
- options: import_effect4.Schema.optional(import_effect4.Schema.Struct({
180
- // Watch changes to object (not just creation).
181
- deep: import_effect4.Schema.optional(import_effect4.Schema.Boolean.annotations({
182
- title: "Nested"
183
- })),
184
- // Debounce changes (delay in ms).
185
- delay: import_effect4.Schema.optional(import_effect4.Schema.Number.annotations({
186
- title: "Delay"
187
- }))
188
- }).annotations({
189
- title: "Options"
190
- }))
191
- }).pipe(import_effect4.Schema.mutable);
192
- var TriggerSchema = import_effect4.Schema.Union(TimerTriggerSchema, WebhookTriggerSchema, SubscriptionTriggerSchema, EmailTriggerSchema, QueueTriggerSchema).annotations({
193
- title: "Trigger"
194
- });
195
- var EmailTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
196
- from: import_effect4.Schema.String,
197
- to: import_effect4.Schema.String,
198
- subject: import_effect4.Schema.String,
199
- created: import_effect4.Schema.String,
200
- body: import_effect4.Schema.String
201
- }));
202
- var WebhookTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
203
- url: import_effect4.Schema.String,
204
- method: import_effect4.Schema.Literal("GET", "POST"),
205
- headers: import_effect4.Schema.Record({
206
- key: import_effect4.Schema.String,
207
- value: import_effect4.Schema.String
208
- }),
209
- bodyText: import_effect4.Schema.String
210
- }));
211
- var QueueTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
212
- queue: import_keys.DXN.Schema,
213
- item: import_effect4.Schema.Any,
214
- cursor: import_effect4.Schema.String
215
- }));
216
- var SubscriptionTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
217
- type: import_effect4.Schema.String,
218
- changedObjectId: import_effect4.Schema.String
219
- }));
220
- var TimerTriggerOutput = import_effect4.Schema.mutable(import_effect4.Schema.Struct({
221
- tick: import_effect4.Schema.Number
222
- }));
223
- var FunctionTriggerSchema = import_effect4.Schema.Struct({
224
- /**
225
- * Function or workflow to invoke.
226
- */
227
- // TODO(dmaretskyi): Can be a Ref(FunctionType) or Ref(ComputeGraphType).
228
- function: import_effect4.Schema.optional((0, import_echo_schema3.Ref)(import_echo_schema3.Expando).annotations({
229
- title: "Function"
230
- })),
231
- /**
232
- * Only used for workflowSchema.
233
- * Specifies the input node in the circuit.
234
- * @deprecated Remove and enforce a single input node in all compute graphSchema.
235
- */
236
- inputNodeId: import_effect4.Schema.optional(import_effect4.Schema.String.annotations({
237
- title: "Input Node ID"
238
- })),
239
- enabled: import_effect4.Schema.optional(import_effect4.Schema.Boolean.annotations({
240
- title: "Enabled"
241
- })),
242
- spec: import_effect4.Schema.optional(TriggerSchema),
243
- /**
244
- * Passed as the input data to the function.
245
- * Must match the function's input schema.
246
- *
247
- * @example
248
- * {
249
- * item: '{{$.trigger.event}}',
250
- * instructions: 'Summarize and perform entity-extraction'
251
- * mailbox: { '/': 'dxn:echo:AAA:ZZZ' }
252
- * }
253
- */
254
- input: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Record({
255
- key: import_effect4.Schema.String,
256
- value: import_effect4.Schema.Any
257
- })))
258
- });
259
- var FunctionTrigger = class extends (0, import_echo_schema3.TypedObject)({
260
- typename: "dxos.org/type/FunctionTrigger",
261
- version: "0.2.0"
262
- })(FunctionTriggerSchema.fields) {
263
- };
264
- var FunctionManifestSchema = import_effect4.Schema.Struct({
265
- functions: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Array((0, import_echo_schema3.RawObject)(FunctionType)))),
266
- triggers: import_effect4.Schema.optional(import_effect4.Schema.mutable(import_effect4.Schema.Array((0, import_echo_schema3.RawObject)(FunctionTrigger))))
267
- });
268
- var FUNCTION_TYPES = [
269
- FunctionType,
270
- FunctionTrigger
271
- ];
272
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/trace.ts";
273
- var InvocationOutcome = /* @__PURE__ */ function(InvocationOutcome2) {
274
- InvocationOutcome2["SUCCESS"] = "success";
275
- InvocationOutcome2["FAILURE"] = "failure";
276
- InvocationOutcome2["PENDING"] = "pending";
277
- return InvocationOutcome2;
278
- }({});
279
- var InvocationTraceEventType = /* @__PURE__ */ function(InvocationTraceEventType2) {
280
- InvocationTraceEventType2["START"] = "start";
281
- InvocationTraceEventType2["END"] = "end";
282
- return InvocationTraceEventType2;
283
- }({});
284
- var TraceEventException = import_effect3.Schema.Struct({
285
- timestampMs: import_effect3.Schema.Number,
286
- message: import_effect3.Schema.String,
287
- name: import_effect3.Schema.String,
288
- stack: import_effect3.Schema.optional(import_effect3.Schema.String)
289
- });
290
- var InvocationTraceStartEvent = import_effect3.Schema.Struct({
291
- /**
292
- * Queue message id.
293
- */
294
- id: import_echo_schema2.ObjectId,
295
- type: import_effect3.Schema.Literal("start"),
296
- /**
297
- * Invocation id, the same for invocation start and end events.
298
- */
299
- invocationId: import_echo_schema2.ObjectId,
300
- /**
301
- * Event generation time.
302
- */
303
- timestampMs: import_effect3.Schema.Number,
304
- /**
305
- * Data passed to function / workflow as an argument.
306
- */
307
- // TODO(burdon): Input schema?
308
- input: import_effect3.Schema.Object,
309
- /**
310
- * Queue for function/workflow invocation events.
311
- */
312
- invocationTraceQueue: import_echo2.Type.Ref(import_echo_db.Queue),
313
- /**
314
- * DXN of the invoked function/workflow.
315
- */
316
- invocationTarget: import_echo2.Type.Ref(import_echo2.Type.Expando),
317
- /**
318
- * Present for automatic invocations.
319
- */
320
- trigger: import_effect3.Schema.optional(import_echo2.Type.Ref(FunctionTrigger))
321
- }).pipe(import_echo2.Type.Obj({
322
- typename: "dxos.org/type/InvocationTraceStart",
323
- version: "0.1.0"
324
- }));
325
- var InvocationTraceEndEvent = import_effect3.Schema.Struct({
326
- /**
327
- * Trace event id.
328
- */
329
- id: import_echo_schema2.ObjectId,
330
- type: import_effect3.Schema.Literal("end"),
331
- /**
332
- * Invocation id, will be the same for invocation start and end.
333
- */
334
- invocationId: import_echo_schema2.ObjectId,
335
- /**
336
- * Event generation time.
337
- */
338
- // TODO(burdon): Remove ms suffix.
339
- timestampMs: import_effect3.Schema.Number,
340
- outcome: import_effect3.Schema.Enums(InvocationOutcome),
341
- exception: import_effect3.Schema.optional(TraceEventException)
342
- }).pipe(import_echo2.Type.Obj({
343
- typename: "dxos.org/type/InvocationTraceEnd",
344
- version: "0.1.0"
345
- }));
346
- var TraceEventLog = import_effect3.Schema.Struct({
347
- timestampMs: import_effect3.Schema.Number,
348
- level: import_effect3.Schema.String,
349
- message: import_effect3.Schema.String,
350
- context: import_effect3.Schema.optional(import_effect3.Schema.Object)
351
- });
352
- var TraceEvent = import_effect3.Schema.Struct({
353
- id: import_echo_schema2.ObjectId,
354
- // TODO(burdon): Need enum/numeric result (not string).
355
- outcome: import_effect3.Schema.String,
356
- truncated: import_effect3.Schema.Boolean,
357
- /**
358
- * Time when the event was persisted.
359
- */
360
- ingestionTimestampMs: import_effect3.Schema.Number,
361
- logs: import_effect3.Schema.Array(TraceEventLog),
362
- exceptions: import_effect3.Schema.Array(TraceEventException)
363
- }).pipe(import_echo2.Type.Obj({
364
- typename: "dxos.org/type/TraceEvent",
365
- version: "0.1.0"
366
- }));
367
- var createInvocationSpans = (items) => {
368
- if (!items) {
369
- return [];
370
- }
371
- const eventsByInvocationId = /* @__PURE__ */ new Map();
372
- for (const event of items) {
373
- if (!("invocationId" in event)) {
374
- continue;
375
- }
376
- const invocationId = event.invocationId;
377
- const entry = eventsByInvocationId.get(invocationId) || {
378
- start: void 0,
379
- end: void 0
380
- };
381
- if (event.type === "start") {
382
- entry.start = event;
383
- } else if (event.type === "end") {
384
- entry.end = event;
385
- }
386
- eventsByInvocationId.set(invocationId, entry);
387
- }
388
- const now = Date.now();
389
- const result = [];
390
- for (const [invocationId, { start, end }] of eventsByInvocationId.entries()) {
391
- if (!start) {
392
- import_log.log.warn("found end event without matching start", {
393
- invocationId
394
- }, {
395
- F: __dxlog_file,
396
- L: 160,
397
- S: void 0,
398
- C: (f, a) => f(...a)
399
- });
400
- continue;
401
- }
402
- const isInProgress = end === void 0;
403
- result.push({
404
- id: invocationId,
405
- timestampMs: start.timestampMs,
406
- durationMs: isInProgress ? now - start.timestampMs : end.timestampMs - start.timestampMs,
407
- outcome: end?.outcome ?? "pending",
408
- exception: end?.exception,
409
- input: start.input,
410
- invocationTraceQueue: start.invocationTraceQueue,
411
- invocationTarget: start.invocationTarget,
412
- trigger: start.trigger
413
- });
414
- }
415
- return result;
416
- };
417
- var FUNCTIONS_META_KEY = "dxos.org/service/function";
418
- var FUNCTIONS_PRESET_META_KEY = "dxos.org/service/function-preset";
419
- var isSecure = (protocol) => {
420
- return protocol === "https:" || protocol === "wss:";
421
- };
422
- var getUserFunctionUrlInMetadata = (meta) => {
423
- return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
424
- };
425
- var setUserFunctionUrlInMetadata = (meta, functionUrl) => {
426
- const key = meta.keys.find((key2) => key2.source === FUNCTIONS_META_KEY);
427
- if (key) {
428
- if (key.id !== functionUrl) {
429
- throw new Error("Metadata mismatch");
430
- }
431
- } else {
432
- meta.keys.push({
433
- source: FUNCTIONS_META_KEY,
434
- id: functionUrl
435
- });
436
- }
437
- };
438
- var makeFunctionUrl = (fn) => `/${fn.functionId}`;
439
- var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
440
- const baseUrl = new URL("functions/", edgeUrl);
441
- const relativeUrl = functionUrl.replace(/^\//, "");
442
- const url = new URL(`./${relativeUrl}`, baseUrl.toString());
443
- options.spaceId && url.searchParams.set("spaceId", options.spaceId);
444
- options.subjectId && url.searchParams.set("subjectId", options.subjectId);
445
- url.protocol = isSecure(url.protocol) ? "https" : "http";
446
- return url.toString();
447
- };
448
- var FunctionExecutor = class {
449
- constructor(_services) {
450
- this._services = _services;
451
- }
452
- // TODO(dmaretskyi): Invocation context: queue, space, etc...
453
- async invoke(fnDef, input) {
454
- const assertInput = fnDef.inputSchema.pipe(import_effect5.Schema.asserts);
455
- assertInput(input);
456
- const context = {
457
- getService: this._services.getService.bind(this._services),
458
- getSpace: async (_spaceId) => {
459
- throw new Error("Not available. Use the database service instead.");
460
- },
461
- space: void 0,
462
- get ai() {
463
- throw new Error("Not available. Use the ai service instead.");
464
- }
465
- };
466
- const result = await fnDef.handler({
467
- context,
468
- data: input
469
- });
470
- const assertOutput = fnDef.outputSchema?.pipe(import_effect5.Schema.asserts);
471
- assertOutput(result);
472
- if (import_effect5.Effect.isEffect(result)) {
473
- return import_effect5.Effect.runPromise(result);
474
- }
475
- return result;
476
- }
477
- };
478
- // Annotate the CommonJS export names for ESM import in node:
479
- 0 && (module.exports = {
480
- AiService,
481
- ConfiguredCredentialsService,
482
- CredentialsService,
483
- DatabaseService,
484
- EmailTriggerOutput,
485
- EventLogger,
486
- FUNCTIONS_PRESET_META_KEY,
487
- FUNCTION_TYPES,
488
- FunctionCallService,
489
- FunctionExecutor,
490
- FunctionManifestSchema,
491
- FunctionTrigger,
492
- FunctionTriggerSchema,
493
- FunctionType,
494
- InvocationOutcome,
495
- InvocationTraceEndEvent,
496
- InvocationTraceEventType,
497
- InvocationTraceStartEvent,
498
- QueueService,
499
- QueueTriggerOutput,
500
- SERVICE_TAGS,
501
- ScriptType,
502
- ServiceContainer,
503
- SubscriptionTriggerOutput,
504
- TimerTriggerOutput,
505
- TraceEvent,
506
- TraceEventException,
507
- TraceEventLog,
508
- TracingService,
509
- TriggerKind,
510
- TriggerSchema,
511
- WebhookTriggerOutput,
512
- createDefectLogger,
513
- createEventLogger,
514
- createInvocationSpans,
515
- defineFunction,
516
- getInvocationUrl,
517
- getUserFunctionUrlInMetadata,
518
- logCustomEvent,
519
- makeFunctionUrl,
520
- setUserFunctionUrlInMetadata
521
- });
522
- //# sourceMappingURL=index.cjs.map