@dxos/functions 0.8.4-main.5acf9ea → 0.8.4-main.5ea62a8

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 (100) hide show
  1. package/dist/lib/browser/bundler/index.mjs +54 -38
  2. package/dist/lib/browser/bundler/index.mjs.map +3 -3
  3. package/dist/lib/browser/chunk-7NQ77AIQ.mjs +618 -0
  4. package/dist/lib/browser/chunk-7NQ77AIQ.mjs.map +7 -0
  5. package/dist/lib/browser/edge/index.mjs +20 -8
  6. package/dist/lib/browser/edge/index.mjs.map +3 -3
  7. package/dist/lib/browser/index.mjs +141 -77
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/testing/index.mjs +68 -5
  11. package/dist/lib/browser/testing/index.mjs.map +3 -3
  12. package/dist/lib/node-esm/bundler/index.mjs +54 -38
  13. package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
  14. package/dist/lib/node-esm/chunk-KCGC6QQT.mjs +620 -0
  15. package/dist/lib/node-esm/chunk-KCGC6QQT.mjs.map +7 -0
  16. package/dist/lib/node-esm/edge/index.mjs +20 -8
  17. package/dist/lib/node-esm/edge/index.mjs.map +3 -3
  18. package/dist/lib/node-esm/index.mjs +141 -77
  19. package/dist/lib/node-esm/index.mjs.map +4 -4
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/testing/index.mjs +68 -5
  22. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  23. package/dist/types/src/bundler/bundler.d.ts +11 -12
  24. package/dist/types/src/bundler/bundler.d.ts.map +1 -1
  25. package/dist/types/src/edge/functions.d.ts +3 -2
  26. package/dist/types/src/edge/functions.d.ts.map +1 -1
  27. package/dist/types/src/errors.d.ts +10 -8
  28. package/dist/types/src/errors.d.ts.map +1 -1
  29. package/dist/types/src/examples/fib.d.ts +7 -0
  30. package/dist/types/src/examples/fib.d.ts.map +1 -0
  31. package/dist/types/src/examples/reply.d.ts +3 -0
  32. package/dist/types/src/examples/reply.d.ts.map +1 -0
  33. package/dist/types/src/examples/sleep.d.ts +5 -0
  34. package/dist/types/src/examples/sleep.d.ts.map +1 -0
  35. package/dist/types/src/executor/executor.d.ts +4 -1
  36. package/dist/types/src/executor/executor.d.ts.map +1 -1
  37. package/dist/types/src/handler.d.ts +10 -7
  38. package/dist/types/src/handler.d.ts.map +1 -1
  39. package/dist/types/src/schema.d.ts +7 -2
  40. package/dist/types/src/schema.d.ts.map +1 -1
  41. package/dist/types/src/services/credentials.d.ts +15 -3
  42. package/dist/types/src/services/credentials.d.ts.map +1 -1
  43. package/dist/types/src/services/database.d.ts +74 -6
  44. package/dist/types/src/services/database.d.ts.map +1 -1
  45. package/dist/types/src/services/event-logger.d.ts +1 -1
  46. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  47. package/dist/types/src/services/local-function-execution.d.ts +2 -1
  48. package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
  49. package/dist/types/src/services/queues.d.ts +18 -5
  50. package/dist/types/src/services/queues.d.ts.map +1 -1
  51. package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
  52. package/dist/types/src/services/service-container.d.ts +1 -1
  53. package/dist/types/src/services/service-container.d.ts.map +1 -1
  54. package/dist/types/src/services/service-registry.d.ts.map +1 -1
  55. package/dist/types/src/services/tracing.d.ts +33 -3
  56. package/dist/types/src/services/tracing.d.ts.map +1 -1
  57. package/dist/types/src/testing/layer.d.ts +6 -2
  58. package/dist/types/src/testing/layer.d.ts.map +1 -1
  59. package/dist/types/src/testing/logger.d.ts.map +1 -1
  60. package/dist/types/src/testing/persist-database.test.d.ts +2 -0
  61. package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
  62. package/dist/types/src/testing/services.d.ts +1 -1
  63. package/dist/types/src/testing/services.d.ts.map +1 -1
  64. package/dist/types/src/trace.d.ts +34 -8
  65. package/dist/types/src/trace.d.ts.map +1 -1
  66. package/dist/types/src/types.d.ts +141 -224
  67. package/dist/types/src/types.d.ts.map +1 -1
  68. package/dist/types/src/url.d.ts +10 -6
  69. package/dist/types/src/url.d.ts.map +1 -1
  70. package/dist/types/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +40 -39
  72. package/src/bundler/bundler.test.ts +8 -9
  73. package/src/bundler/bundler.ts +32 -33
  74. package/src/edge/functions.ts +8 -5
  75. package/src/examples/fib.ts +30 -0
  76. package/src/examples/reply.ts +18 -0
  77. package/src/examples/sleep.ts +22 -0
  78. package/src/executor/executor.ts +9 -9
  79. package/src/handler.ts +12 -10
  80. package/src/schema.ts +11 -0
  81. package/src/services/credentials.ts +78 -5
  82. package/src/services/database.ts +114 -18
  83. package/src/services/event-logger.ts +2 -2
  84. package/src/services/local-function-execution.ts +20 -13
  85. package/src/services/queues.ts +29 -10
  86. package/src/services/remote-function-execution-service.ts +2 -22
  87. package/src/services/service-container.ts +4 -3
  88. package/src/services/service-registry.ts +1 -1
  89. package/src/services/tracing.ts +95 -5
  90. package/src/testing/layer.ts +69 -3
  91. package/src/testing/logger.ts +1 -1
  92. package/src/testing/persist-database.test.ts +87 -0
  93. package/src/testing/services.ts +2 -1
  94. package/src/trace.ts +5 -7
  95. package/src/types.ts +17 -25
  96. package/src/url.ts +13 -10
  97. package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
  98. package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
  99. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
  100. package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
package/package.json CHANGED
@@ -1,37 +1,37 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.5acf9ea",
3
+ "version": "0.8.4-main.5ea62a8",
4
4
  "description": "Functions API and runtime.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
7
  "license": "MIT",
8
8
  "author": "info@dxos.org",
9
- "sideEffects": true,
9
+ "sideEffects": false,
10
10
  "type": "module",
11
11
  "exports": {
12
+ ".": {
13
+ "source": "./src/index.ts",
14
+ "types": "./dist/types/src/index.d.ts",
15
+ "browser": "./dist/lib/browser/index.mjs",
16
+ "node": "./dist/lib/node-esm/index.mjs"
17
+ },
12
18
  "./bundler": {
19
+ "source": "./src/bundler/index.ts",
13
20
  "types": "./dist/types/src/bundler/index.d.ts",
14
21
  "browser": "./dist/lib/browser/bundler/index.mjs",
15
- "node": "./dist/lib/node-esm/bundler/index.mjs",
16
- "source": "./src/bundler/index.ts"
22
+ "node": "./dist/lib/node-esm/bundler/index.mjs"
17
23
  },
18
24
  "./edge": {
25
+ "source": "./src/edge/index.ts",
19
26
  "types": "./dist/types/src/edge/index.d.ts",
20
27
  "browser": "./dist/lib/browser/edge/index.mjs",
21
- "node": "./dist/lib/node-esm/edge/index.mjs",
22
- "source": "./src/edge/index.ts"
23
- },
24
- ".": {
25
- "types": "./dist/types/src/index.d.ts",
26
- "browser": "./dist/lib/browser/index.mjs",
27
- "node": "./dist/lib/node-esm/index.mjs",
28
- "source": "./src/index.ts"
28
+ "node": "./dist/lib/node-esm/edge/index.mjs"
29
29
  },
30
30
  "./testing": {
31
+ "source": "./src/testing/index.ts",
31
32
  "types": "./dist/types/src/testing/index.d.ts",
32
33
  "browser": "./dist/lib/browser/testing/index.mjs",
33
- "node": "./dist/lib/node-esm/testing/index.mjs",
34
- "source": "./src/testing/index.ts"
34
+ "node": "./dist/lib/node-esm/testing/index.mjs"
35
35
  }
36
36
  },
37
37
  "types": "dist/types/src/index.d.ts",
@@ -51,44 +51,45 @@
51
51
  "src"
52
52
  ],
53
53
  "dependencies": {
54
- "@effect/platform": "0.89.0",
54
+ "@effect/platform": "0.90.2",
55
55
  "@preact/signals-core": "^1.9.0",
56
56
  "cron": "^3.1.6",
57
57
  "cron-schedule": "^5.0.4",
58
- "effect": "3.17.0",
58
+ "effect": "3.17.7",
59
59
  "esbuild-wasm": "^0.16.14",
60
60
  "express": "^4.19.2",
61
61
  "get-port-please": "^3.1.1",
62
62
  "i18next": "^24.2.1",
63
63
  "iso-did": "^1.6.0",
64
64
  "ws": "^8.14.2",
65
- "@dxos/ai": "0.8.4-main.5acf9ea",
66
- "@dxos/async": "0.8.4-main.5acf9ea",
67
- "@dxos/crypto": "0.8.4-main.5acf9ea",
68
- "@dxos/client": "0.8.4-main.5acf9ea",
69
- "@dxos/echo": "0.8.4-main.5acf9ea",
70
- "@dxos/context": "0.8.4-main.5acf9ea",
71
- "@dxos/debug": "0.8.4-main.5acf9ea",
72
- "@dxos/echo-db": "0.8.4-main.5acf9ea",
73
- "@dxos/echo-protocol": "0.8.4-main.5acf9ea",
74
- "@dxos/echo-pipeline": "0.8.4-main.5acf9ea",
75
- "@dxos/echo-schema": "0.8.4-main.5acf9ea",
76
- "@dxos/edge-client": "0.8.4-main.5acf9ea",
77
- "@dxos/effect": "0.8.4-main.5acf9ea",
78
- "@dxos/errors": "0.8.4-main.5acf9ea",
79
- "@dxos/keys": "0.8.4-main.5acf9ea",
80
- "@dxos/invariant": "0.8.4-main.5acf9ea",
81
- "@dxos/live-object": "0.8.4-main.5acf9ea",
82
- "@dxos/log": "0.8.4-main.5acf9ea",
83
- "@dxos/node-std": "0.8.4-main.5acf9ea",
84
- "@dxos/protocols": "0.8.4-main.5acf9ea",
85
- "@dxos/schema": "0.8.4-main.5acf9ea",
86
- "@dxos/util": "0.8.4-main.5acf9ea"
65
+ "@dxos/ai": "0.8.4-main.5ea62a8",
66
+ "@dxos/async": "0.8.4-main.5ea62a8",
67
+ "@dxos/context": "0.8.4-main.5ea62a8",
68
+ "@dxos/client": "0.8.4-main.5ea62a8",
69
+ "@dxos/crypto": "0.8.4-main.5ea62a8",
70
+ "@dxos/debug": "0.8.4-main.5ea62a8",
71
+ "@dxos/echo-db": "0.8.4-main.5ea62a8",
72
+ "@dxos/echo-pipeline": "0.8.4-main.5ea62a8",
73
+ "@dxos/echo-schema": "0.8.4-main.5ea62a8",
74
+ "@dxos/echo": "0.8.4-main.5ea62a8",
75
+ "@dxos/echo-protocol": "0.8.4-main.5ea62a8",
76
+ "@dxos/edge-client": "0.8.4-main.5ea62a8",
77
+ "@dxos/effect": "0.8.4-main.5ea62a8",
78
+ "@dxos/errors": "0.8.4-main.5ea62a8",
79
+ "@dxos/invariant": "0.8.4-main.5ea62a8",
80
+ "@dxos/keys": "0.8.4-main.5ea62a8",
81
+ "@dxos/kv-store": "0.8.4-main.5ea62a8",
82
+ "@dxos/live-object": "0.8.4-main.5ea62a8",
83
+ "@dxos/log": "0.8.4-main.5ea62a8",
84
+ "@dxos/protocols": "0.8.4-main.5ea62a8",
85
+ "@dxos/schema": "0.8.4-main.5ea62a8",
86
+ "@dxos/util": "0.8.4-main.5ea62a8",
87
+ "@dxos/node-std": "0.8.4-main.5ea62a8"
87
88
  },
88
89
  "devDependencies": {
89
90
  "@types/express": "^4.17.17",
90
91
  "@types/ws": "^7.4.0",
91
- "@dxos/agent": "0.8.4-main.5acf9ea"
92
+ "@dxos/agent": "0.8.4-main.5ea62a8"
92
93
  },
93
94
  "publishConfig": {
94
95
  "access": "public"
@@ -4,7 +4,7 @@
4
4
 
5
5
  // @ts-ignore
6
6
  import wasmUrl from 'esbuild-wasm/esbuild.wasm?url';
7
- import { beforeAll, describe, expect, test } from 'vitest';
7
+ import { assert, beforeAll, describe, expect, test } from 'vitest';
8
8
 
9
9
  import { isNode } from '@dxos/util';
10
10
 
@@ -20,8 +20,8 @@ describe('Bundler', () => {
20
20
  test('Basic', async () => {
21
21
  const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
22
22
  const result = await bundler.bundle({ source: 'const x = 100' }); // TODO(burdon): Test import.
23
- expect(result.bundle).to.exist;
24
- expect(result.error).to.not.exist;
23
+ assert(!('error' in result), 'error should not exist');
24
+ expect(result.asset).toBeDefined();
25
25
  });
26
26
 
27
27
  test('Import', async () => {
@@ -33,8 +33,8 @@ describe('Bundler', () => {
33
33
  const query = Filter.typename('dxos.org/type/Example');
34
34
  `,
35
35
  });
36
- expect(result.bundle).to.exist;
37
- expect(result.error).to.not.exist;
36
+ assert(!('error' in result), 'error should not exist');
37
+ expect(result.asset).toBeDefined();
38
38
  });
39
39
 
40
40
  // TODO(dmaretskyi): Flaky on CI.
@@ -46,14 +46,13 @@ describe('Bundler', () => {
46
46
  invariant(true);
47
47
  `,
48
48
  });
49
- expect(result.bundle).to.exist;
50
- expect(result.error).to.not.exist;
49
+ assert(!('error' in result), 'error should not exist');
50
+ expect(result.asset).toBeDefined();
51
51
  });
52
52
 
53
53
  test('Error', async () => {
54
54
  const bundler = new Bundler({ platform: 'node', sandboxedModules: [], remoteModules: {} });
55
55
  const result = await bundler.bundle({ source: "import missing from './module'; missing();" });
56
- expect(result.bundle).to.not.exist;
57
- expect(result.error).to.exist;
56
+ assert('error' in result, 'error should exist');
58
57
  });
59
58
  });
@@ -3,8 +3,8 @@
3
3
  //
4
4
 
5
5
  import { FetchHttpClient, HttpClient } from '@effect/platform';
6
- import { Duration, Effect, pipe, Schedule } from 'effect';
7
- import { type BuildOptions, type Loader, build, initialize, type BuildResult, type Plugin } from 'esbuild-wasm';
6
+ import { Duration, Effect, Schedule, pipe } from 'effect';
7
+ import { type BuildOptions, type BuildResult, type Loader, type Plugin, build, initialize } from 'esbuild-wasm';
8
8
 
9
9
  import { subtleCrypto } from '@dxos/crypto';
10
10
  import { runAndForwardErrors } from '@dxos/effect';
@@ -18,26 +18,26 @@ export type Import = {
18
18
  };
19
19
 
20
20
  export type BundleOptions = {
21
- /**
22
- * Path to the source file on the local file system.
23
- * If provided, the path will be used instead of the `source` code.
24
- */
25
- path?: string;
26
-
27
21
  /**
28
22
  * Source code to bundle.
29
- * Required if `path` is not provided.
30
23
  */
31
- source?: string;
24
+ source: string;
32
25
  };
33
26
 
34
- export type BundleResult = {
35
- timestamp: number;
36
- sourceHash?: Buffer;
37
- imports?: Import[];
38
- bundle?: string;
39
- error?: any;
40
- };
27
+ export type BundleResult =
28
+ | {
29
+ timestamp: number;
30
+ sourceHash: Buffer;
31
+ error: unknown;
32
+ }
33
+ | {
34
+ timestamp: number;
35
+ sourceHash: Buffer;
36
+ imports: Import[];
37
+ entryPoint: string;
38
+ asset: Uint8Array;
39
+ bundle: string;
40
+ };
41
41
 
42
42
  export type BundlerOptions = {
43
43
  platform: BuildOptions['platform'];
@@ -58,16 +58,9 @@ export const initializeBundler = async (options: { wasmUrl: string }) => {
58
58
  export class Bundler {
59
59
  constructor(private readonly _options: BundlerOptions) {}
60
60
 
61
- async bundle({ path, source }: BundleOptions): Promise<BundleResult> {
61
+ async bundle({ source }: BundleOptions): Promise<BundleResult> {
62
62
  const { sandboxedModules: providedModules, ...options } = this._options;
63
-
64
- const createResult = async (result?: Partial<BundleResult>) => {
65
- return {
66
- timestamp: Date.now(),
67
- sourceHash: source ? Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source))) : undefined,
68
- ...result,
69
- };
70
- };
63
+ const sourceHash = Buffer.from(await subtleCrypto.digest('SHA-256', Buffer.from(source)));
71
64
 
72
65
  if (this._options.platform === 'browser') {
73
66
  invariant(initialized, 'Compiler not initialized.');
@@ -83,7 +76,10 @@ export class Bundler {
83
76
  conditions: ['workerd', 'browser'],
84
77
  metafile: true,
85
78
  write: false,
86
- entryPoints: [path ?? 'memory:main.tsx'],
79
+ entryPoints: {
80
+ // Gets mapped to `userFunc.js` by esbuild.
81
+ userFunc: 'memory:main.tsx',
82
+ },
87
83
  bundle: true,
88
84
  format: 'esm',
89
85
  plugins: [
@@ -136,12 +132,17 @@ export class Bundler {
136
132
 
137
133
  log('compile complete', result.metafile);
138
134
 
139
- return await createResult({
135
+ const entryPoint = 'userFunc.js';
136
+ return {
137
+ timestamp: Date.now(),
138
+ sourceHash,
140
139
  imports: this.analyzeImports(result),
140
+ entryPoint,
141
+ asset: result.outputFiles![0].contents,
141
142
  bundle: result.outputFiles![0].text,
142
- });
143
+ };
143
144
  } catch (err) {
144
- return await createResult({ error: err });
145
+ return { timestamp: Date.now(), sourceHash, error: err };
145
146
  }
146
147
  }
147
148
 
@@ -153,7 +154,6 @@ export class Bundler {
153
154
  const parsedImports = allMatches(IMPORT_REGEX, result.outputFiles[0].text);
154
155
  return Object.values(result.metafile!.outputs)[0].imports.map((entry): Import => {
155
156
  const namedImports: string[] = [];
156
-
157
157
  const parsedImport = parsedImports.find((capture) => capture?.[4] === entry.path);
158
158
  if (parsedImport?.[2]) {
159
159
  NAMED_IMPORTS_REGEX.lastIndex = 0;
@@ -207,10 +207,9 @@ const IMPORT_REGEX =
207
207
  const NAMED_IMPORTS_REGEX = /[ \n\t]*{((?:[ \n\t]*[^ \n\t"'{}]+[ \n\t]*,?)+)}[ \n\t]*/gm;
208
208
 
209
209
  const allMatches = (regex: RegExp, str: string) => {
210
- regex.lastIndex = 0;
211
-
212
210
  let match;
213
211
  const matches = [];
212
+ regex.lastIndex = 0;
214
213
  while ((match = regex.exec(str))) {
215
214
  matches.push(match);
216
215
  }
@@ -14,21 +14,24 @@ import { type UploadFunctionResponseBody } from '@dxos/protocols';
14
14
 
15
15
  export type UploadWorkerArgs = {
16
16
  client: Client;
17
- source: string;
18
17
  version: string;
19
18
  name?: string;
20
19
  functionId?: string;
21
20
  ownerPublicKey: PublicKey;
21
+ entryPoint: string;
22
+ assets: Record<string, Uint8Array>;
22
23
  };
23
24
 
24
25
  export const uploadWorkerFunction = async ({
25
26
  client,
26
27
  version,
27
- source,
28
28
  name,
29
29
  functionId,
30
30
  ownerPublicKey,
31
+ entryPoint,
32
+ assets,
31
33
  }: UploadWorkerArgs): Promise<UploadFunctionResponseBody> => {
34
+ log('uploading function', { functionId, name, version, ownerPublicKey });
32
35
  const edgeUrl = client.config.values.runtime?.services?.edge?.url;
33
36
  invariant(edgeUrl, 'Edge is not configured.');
34
37
  const edgeClient = new EdgeHttpClient(edgeUrl);
@@ -36,15 +39,15 @@ export const uploadWorkerFunction = async ({
36
39
  edgeClient.setIdentity(edgeIdentity);
37
40
  const response = await edgeClient.uploadFunction(
38
41
  { functionId },
39
- { name, version, script: source, ownerPublicKey: ownerPublicKey.toHex() },
42
+ { name, version, ownerPublicKey: ownerPublicKey.toHex(), entryPoint, assets },
40
43
  );
41
44
 
42
45
  // TODO(burdon): Edge service log.
43
- log.info('Uploaded', {
46
+ log('uploaded', {
44
47
  identityKey: edgeIdentity.identityKey,
45
48
  functionId,
46
49
  name,
47
- source: source.length,
50
+ version,
48
51
  response,
49
52
  });
50
53
 
@@ -0,0 +1,30 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect, Schema } from 'effect';
6
+
7
+ import { defineFunction } from '../handler';
8
+
9
+ export default defineFunction({
10
+ name: 'example.org/function/fib',
11
+ description: 'Function that calculates a Fibonacci number',
12
+ inputSchema: Schema.Struct({
13
+ iterations: Schema.optional(Schema.Number).annotations({
14
+ description: 'Number of iterations',
15
+ default: 100_000,
16
+ }),
17
+ }),
18
+ outputSchema: Schema.Struct({
19
+ result: Schema.String,
20
+ }),
21
+ handler: Effect.fn(function* ({ data: { iterations = 100_000 } }) {
22
+ let a = 0n;
23
+ let b = 1n;
24
+ for (let i = 0; i < iterations; i++) {
25
+ a += b;
26
+ b = a - b;
27
+ }
28
+ return { result: a.toString() };
29
+ }),
30
+ });
@@ -0,0 +1,18 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Console, Effect, Schema } from 'effect';
6
+
7
+ import { defineFunction } from '../handler';
8
+
9
+ export default defineFunction({
10
+ name: 'example.org/function/reply',
11
+ description: 'Function that echoes the input',
12
+ inputSchema: Schema.Any,
13
+ outputSchema: Schema.Any,
14
+ handler: Effect.fn(function* ({ data }) {
15
+ yield* Console.log('reply', { data });
16
+ return data;
17
+ }),
18
+ });
@@ -0,0 +1,22 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { Effect, Schema } from 'effect';
6
+
7
+ import { defineFunction } from '../handler';
8
+
9
+ export default defineFunction({
10
+ name: 'example.org/function/sleep',
11
+ description: 'Function that sleeps for a given amount of time',
12
+ inputSchema: Schema.Struct({
13
+ duration: Schema.optional(Schema.Number).annotations({
14
+ description: 'Milliseconds to sleep',
15
+ default: 100_000,
16
+ }),
17
+ }),
18
+ outputSchema: Schema.Void,
19
+ handler: Effect.fn(function* ({ data: { duration = 100_000 } }) {
20
+ yield* Effect.sleep(duration);
21
+ }),
22
+ });
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { Effect, Schema } from 'effect';
6
6
 
7
- import type { SpaceId } from '@dxos/client/echo';
7
+ import { type SpaceId } from '@dxos/client/echo';
8
8
  import { runAndForwardErrors } from '@dxos/effect';
9
9
 
10
10
  import type { FunctionContext, FunctionDefinition } from '../handler';
@@ -13,27 +13,27 @@ import type { ServiceContainer, Services } from '../services';
13
13
  export class FunctionExecutor {
14
14
  constructor(private readonly _services: ServiceContainer) {}
15
15
 
16
+ /**
17
+ * Invoke function.
18
+ */
16
19
  // TODO(dmaretskyi): Invocation context: queue, space, etc...
17
20
  async invoke<F extends FunctionDefinition<any, any>>(
18
- fnDef: F,
21
+ functionDef: F,
19
22
  input: F extends FunctionDefinition<infer I, infer _O> ? I : never,
20
23
  ): Promise<F extends FunctionDefinition<infer _I, infer O> ? O : never> {
21
24
  // Assert input matches schema
22
- const assertInput = fnDef.inputSchema.pipe(Schema.asserts);
25
+ const assertInput = functionDef.inputSchema.pipe(Schema.asserts);
23
26
  (assertInput as any)(input);
24
27
 
25
28
  const context: FunctionContext = {
29
+ space: undefined,
26
30
  getService: this._services.getService.bind(this._services),
27
31
  getSpace: async (_spaceId: SpaceId) => {
28
32
  throw new Error('Not available. Use the database service instead.');
29
33
  },
30
- space: undefined,
31
- get ai(): never {
32
- throw new Error('Not available. Use the ai service instead.');
33
- },
34
34
  };
35
35
 
36
- const result = fnDef.handler({ context, data: input });
36
+ const result = functionDef.handler({ context, data: input });
37
37
 
38
38
  let data: unknown;
39
39
  if (Effect.isEffect(result)) {
@@ -46,7 +46,7 @@ export class FunctionExecutor {
46
46
  }
47
47
 
48
48
  // Assert output matches schema
49
- const assertOutput = fnDef.outputSchema?.pipe(Schema.asserts);
49
+ const assertOutput = functionDef.outputSchema?.pipe(Schema.asserts);
50
50
  (assertOutput as any)(data);
51
51
 
52
52
  return data as any;
package/src/handler.ts CHANGED
@@ -2,16 +2,14 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { Schema, type Context, type Effect } from 'effect';
5
+ import { type Context, type Effect, Schema } from 'effect';
6
6
 
7
- import { type AiServiceClient } from '@dxos/ai';
8
- // import { type Space } from '@dxos/client/echo';
9
- import type { EchoDatabase } from '@dxos/echo-db';
7
+ import { type EchoDatabase } from '@dxos/echo-db';
10
8
  import { type HasId } from '@dxos/echo-schema';
11
- import { type SpaceId, type DXN } from '@dxos/keys';
9
+ import { type DXN, type SpaceId } from '@dxos/keys';
12
10
  import { type QueryResult } from '@dxos/protocols';
13
11
 
14
- import type { Services } from './services';
12
+ import { type Services } from './services';
15
13
 
16
14
  // TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
17
15
  // https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
@@ -44,8 +42,6 @@ export interface FunctionContext {
44
42
  */
45
43
  space: SpaceAPI | undefined;
46
44
 
47
- ai: AiServiceClient;
48
-
49
45
  /**
50
46
  * Resolves a service available to the function.
51
47
  * @throws if the service is not available.
@@ -87,7 +83,8 @@ const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
87
83
  // const _: SpaceAPI = {} as Space;
88
84
  };
89
85
 
90
- export type FunctionDefinition<T = {}, O = any> = {
86
+ export type FunctionDefinition<T = any, O = any> = {
87
+ // TODO(dmaretskyi): Use `key` for FQN and `name` for human-readable-name.
91
88
  name: string;
92
89
  description?: string;
93
90
  inputSchema: Schema.Schema<T, any>;
@@ -95,7 +92,6 @@ export type FunctionDefinition<T = {}, O = any> = {
95
92
  handler: FunctionHandler<T, O>;
96
93
  };
97
94
 
98
- // TODO(dmaretskyi): Bind input type to function handler.
99
95
  export const defineFunction = <T, O>({
100
96
  name,
101
97
  description,
@@ -118,3 +114,9 @@ export const defineFunction = <T, O>({
118
114
  handler,
119
115
  };
120
116
  };
117
+
118
+ export namespace FunctionDefinition {
119
+ export type Any = FunctionDefinition<any, any>;
120
+ export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
121
+ export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
122
+ }
package/src/schema.ts CHANGED
@@ -32,6 +32,17 @@ export interface ScriptType extends Schema.Schema.Type<typeof ScriptType> {}
32
32
  * Function deployment.
33
33
  */
34
34
  export const FunctionType = Schema.Struct({
35
+ /**
36
+ * Global registry ID.
37
+ * NOTE: The `key` property refers to the original registry entry.
38
+ */
39
+ // TODO(burdon): Create Format type for DXN-like ids, such as this and schema type.
40
+ // TODO(dmaretskyi): Consider making it part of ECHO meta.
41
+ // TODO(dmaretskyi): Make required.
42
+ key: Schema.optional(Schema.String).annotations({
43
+ description: 'Unique registration key for the blueprint',
44
+ }),
45
+
35
46
  // TODO(burdon): Rename to id/uri?
36
47
  name: Schema.NonEmptyString,
37
48
  version: Schema.String,
@@ -2,9 +2,15 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Context, Effect, Layer } from 'effect';
5
+ import { HttpClient, HttpClientRequest } from '@effect/platform';
6
+ import { type Config, Context, Effect, Layer, Redacted } from 'effect';
6
7
 
7
- type CredentialQuery = {
8
+ import { Query } from '@dxos/echo';
9
+ import { DataType } from '@dxos/schema';
10
+
11
+ import { DatabaseService } from './database';
12
+
13
+ export type CredentialQuery = {
8
14
  service?: string;
9
15
  };
10
16
 
@@ -32,14 +38,69 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
32
38
  getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
33
39
  }
34
40
  >() {
35
- static configuredLayer = (credentials: ServiceCredential[]) =>
36
- Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
37
-
38
41
  static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
39
42
  Effect.gen(function* () {
40
43
  const credentials = yield* CredentialsService;
41
44
  return yield* Effect.promise(() => credentials.getCredential(query));
42
45
  });
46
+
47
+ static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
48
+ Effect.gen(function* () {
49
+ const credential = yield* CredentialsService.getCredential(query);
50
+ if (!credential.apiKey) {
51
+ throw new Error(`API key not found for service: ${query.service}`);
52
+ }
53
+ return Redacted.make(credential.apiKey);
54
+ });
55
+
56
+ static configuredLayer = (credentials: ServiceCredential[]) =>
57
+ Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
58
+
59
+ static layerConfig = (credentials: { service: string; apiKey: Config.Config<Redacted.Redacted<string>> }[]) =>
60
+ Layer.effect(
61
+ CredentialsService,
62
+ Effect.gen(function* () {
63
+ const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
64
+ Effect.gen(function* () {
65
+ return {
66
+ service,
67
+ apiKey: Redacted.value(yield* apiKey),
68
+ };
69
+ }),
70
+ );
71
+
72
+ return new ConfiguredCredentialsService(serviceCredentials);
73
+ }),
74
+ );
75
+
76
+ static layerFromDatabase = () =>
77
+ Layer.effect(
78
+ CredentialsService,
79
+ Effect.gen(function* () {
80
+ const dbService = yield* DatabaseService;
81
+ const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
82
+ const { objects: accessTokens } = await dbService.db.query(Query.type(DataType.AccessToken)).run();
83
+ return accessTokens
84
+ .filter((accessToken) => accessToken.source === query.service)
85
+ .map((accessToken) => ({
86
+ service: accessToken.source,
87
+ apiKey: accessToken.token,
88
+ }));
89
+ };
90
+ return {
91
+ getCredential: async (query) => {
92
+ const credentials = await queryCredentials(query);
93
+ if (credentials.length === 0) {
94
+ throw new Error(`Credential not found for service: ${query.service}`);
95
+ }
96
+ return credentials[0];
97
+ },
98
+ queryCredentials: async (query) => {
99
+ return queryCredentials(query);
100
+ },
101
+ };
102
+ }),
103
+ );
43
104
  }
44
105
 
45
106
  export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
@@ -62,3 +123,15 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
62
123
  return credential;
63
124
  }
64
125
  }
126
+
127
+ /**
128
+ * Maps the request to include the API key from the credential.
129
+ */
130
+ export const withAuthorization = (query: CredentialQuery, kind?: 'Bearer' | 'Basic') =>
131
+ HttpClient.mapRequestEffect(
132
+ Effect.fnUntraced(function* (request) {
133
+ const key = yield* CredentialsService.getApiKey(query).pipe(Effect.map(Redacted.value));
134
+ const authorization = kind ? `${kind} ${key}` : key;
135
+ return HttpClientRequest.setHeader(request, 'Authorization', authorization);
136
+ }),
137
+ );