@dxos/functions 0.8.4-main.72ec0f3 → 0.8.4-main.7996785055

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 (84) hide show
  1. package/README.md +4 -6
  2. package/dist/lib/neutral/chunk-GEM2K7BA.mjs +56 -0
  3. package/dist/lib/neutral/chunk-GEM2K7BA.mjs.map +7 -0
  4. package/dist/lib/neutral/fib-XBUKWUUS.mjs +22 -0
  5. package/dist/lib/neutral/fib-XBUKWUUS.mjs.map +7 -0
  6. package/dist/lib/neutral/index.mjs +960 -0
  7. package/dist/lib/neutral/index.mjs.map +7 -0
  8. package/dist/lib/neutral/meta.json +1 -0
  9. package/dist/lib/neutral/reply-XBQTXDLZ.mjs +18 -0
  10. package/dist/lib/neutral/reply-XBQTXDLZ.mjs.map +7 -0
  11. package/dist/lib/neutral/sleep-TLQKHAK2.mjs +14 -0
  12. package/dist/lib/neutral/sleep-TLQKHAK2.mjs.map +7 -0
  13. package/dist/types/src/errors.d.ts +24 -32
  14. package/dist/types/src/errors.d.ts.map +1 -1
  15. package/dist/types/src/example/definitions.d.ts +11 -0
  16. package/dist/types/src/example/definitions.d.ts.map +1 -0
  17. package/dist/types/src/example/fib.d.ts +3 -2
  18. package/dist/types/src/example/fib.d.ts.map +1 -1
  19. package/dist/types/src/example/index.d.ts +3 -11
  20. package/dist/types/src/example/index.d.ts.map +1 -1
  21. package/dist/types/src/example/reply.d.ts +2 -1
  22. package/dist/types/src/example/reply.d.ts.map +1 -1
  23. package/dist/types/src/example/sleep.d.ts +3 -2
  24. package/dist/types/src/example/sleep.d.ts.map +1 -1
  25. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  26. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  27. package/dist/types/src/protocol/protocol.d.ts +2 -2
  28. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  29. package/dist/types/src/sdk.d.ts +2 -79
  30. package/dist/types/src/sdk.d.ts.map +1 -1
  31. package/dist/types/src/services/credentials.d.ts +6 -4
  32. package/dist/types/src/services/credentials.d.ts.map +1 -1
  33. package/dist/types/src/services/event-logger.d.ts +25 -31
  34. package/dist/types/src/services/event-logger.d.ts.map +1 -1
  35. package/dist/types/src/services/function-invocation-service.d.ts +9 -3
  36. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  37. package/dist/types/src/services/index.d.ts +0 -1
  38. package/dist/types/src/services/index.d.ts.map +1 -1
  39. package/dist/types/src/services/queues.d.ts +4 -4
  40. package/dist/types/src/services/queues.d.ts.map +1 -1
  41. package/dist/types/src/services/tracing.d.ts +38 -3
  42. package/dist/types/src/services/tracing.d.ts.map +1 -1
  43. package/dist/types/src/types/Script.d.ts +9 -16
  44. package/dist/types/src/types/Script.d.ts.map +1 -1
  45. package/dist/types/src/types/Trigger.d.ts +57 -76
  46. package/dist/types/src/types/Trigger.d.ts.map +1 -1
  47. package/dist/types/src/types/TriggerEvent.d.ts +44 -13
  48. package/dist/types/src/types/TriggerEvent.d.ts.map +1 -1
  49. package/dist/types/src/types/index.d.ts +0 -1
  50. package/dist/types/src/types/index.d.ts.map +1 -1
  51. package/dist/types/src/types/url.d.ts +6 -5
  52. package/dist/types/src/types/url.d.ts.map +1 -1
  53. package/dist/types/tsconfig.tsbuildinfo +1 -1
  54. package/package.json +23 -17
  55. package/src/errors.ts +4 -4
  56. package/src/example/definitions.ts +48 -0
  57. package/src/example/fib.ts +14 -24
  58. package/src/example/forex-effect.ts +1 -1
  59. package/src/example/index.ts +7 -8
  60. package/src/example/reply.ts +10 -13
  61. package/src/example/sleep.ts +8 -17
  62. package/src/protocol/functions-ai-http-client.ts +67 -0
  63. package/src/protocol/protocol.ts +196 -84
  64. package/src/sdk.ts +4 -194
  65. package/src/services/credentials.ts +31 -15
  66. package/src/services/event-logger.ts +2 -2
  67. package/src/services/function-invocation-service.ts +18 -3
  68. package/src/services/index.ts +0 -2
  69. package/src/services/queues.ts +5 -7
  70. package/src/services/tracing.ts +64 -4
  71. package/src/types/Script.ts +13 -8
  72. package/src/types/Trigger.ts +31 -17
  73. package/src/types/TriggerEvent.ts +29 -29
  74. package/src/types/index.ts +0 -1
  75. package/src/types/url.ts +6 -5
  76. package/dist/lib/browser/index.mjs +0 -927
  77. package/dist/lib/browser/index.mjs.map +0 -7
  78. package/dist/lib/browser/meta.json +0 -1
  79. package/dist/lib/node-esm/index.mjs +0 -928
  80. package/dist/lib/node-esm/index.mjs.map +0 -7
  81. package/dist/lib/node-esm/meta.json +0 -1
  82. package/dist/types/src/types/Function.d.ts +0 -58
  83. package/dist/types/src/types/Function.d.ts.map +0 -1
  84. package/src/types/Function.ts +0 -62
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.72ec0f3",
3
+ "version": "0.8.4-main.7996785055",
4
4
  "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "info@dxos.org",
9
13
  "sideEffects": false,
@@ -12,8 +16,7 @@
12
16
  ".": {
13
17
  "source": "./src/index.ts",
14
18
  "types": "./dist/types/src/index.d.ts",
15
- "browser": "./dist/lib/browser/index.mjs",
16
- "node": "./dist/lib/node-esm/index.mjs"
19
+ "default": "./dist/lib/neutral/index.mjs"
17
20
  }
18
21
  },
19
22
  "types": "dist/types/src/index.d.ts",
@@ -23,20 +26,23 @@
23
26
  "src"
24
27
  ],
25
28
  "dependencies": {
26
- "@effect/platform": "0.92.1",
27
- "effect": "3.18.3",
28
- "@dxos/echo": "0.8.4-main.72ec0f3",
29
- "@dxos/echo-db": "0.8.4-main.72ec0f3",
30
- "@dxos/effect": "0.8.4-main.72ec0f3",
31
- "@dxos/ai": "0.8.4-main.72ec0f3",
32
- "@dxos/errors": "0.8.4-main.72ec0f3",
33
- "@dxos/invariant": "0.8.4-main.72ec0f3",
34
- "@dxos/keys": "0.8.4-main.72ec0f3",
35
- "@dxos/node-std": "0.8.4-main.72ec0f3",
36
- "@dxos/protocols": "0.8.4-main.72ec0f3",
37
- "@dxos/schema": "0.8.4-main.72ec0f3",
38
- "@dxos/types": "0.8.4-main.72ec0f3",
39
- "@dxos/log": "0.8.4-main.72ec0f3"
29
+ "@effect/ai-anthropic": "0.23.0",
30
+ "@effect/platform": "0.94.4",
31
+ "effect": "3.20.0",
32
+ "@dxos/ai": "0.8.4-main.7996785055",
33
+ "@dxos/context": "0.8.4-main.7996785055",
34
+ "@dxos/echo": "0.8.4-main.7996785055",
35
+ "@dxos/echo-db": "0.8.4-main.7996785055",
36
+ "@dxos/invariant": "0.8.4-main.7996785055",
37
+ "@dxos/keys": "0.8.4-main.7996785055",
38
+ "@dxos/effect": "0.8.4-main.7996785055",
39
+ "@dxos/log": "0.8.4-main.7996785055",
40
+ "@dxos/node-std": "0.8.4-main.7996785055",
41
+ "@dxos/errors": "0.8.4-main.7996785055",
42
+ "@dxos/operation": "0.8.4-main.7996785055",
43
+ "@dxos/protocols": "0.8.4-main.7996785055",
44
+ "@dxos/types": "0.8.4-main.7996785055",
45
+ "@dxos/schema": "0.8.4-main.7996785055"
40
46
  },
41
47
  "publishConfig": {
42
48
  "access": "public"
package/src/errors.ts CHANGED
@@ -4,18 +4,18 @@
4
4
 
5
5
  import { BaseError, type BaseErrorOptions } from '@dxos/errors';
6
6
 
7
- export class ServiceNotAvailableError extends BaseError.extend('SERVICE_NOT_AVAILABLE', 'Service not available') {
7
+ export class ServiceNotAvailableError extends BaseError.extend('ServiceNotAvailable', 'Service not available') {
8
8
  constructor(service: string, options?: Omit<BaseErrorOptions, 'context'>) {
9
9
  super({ context: { service }, ...options });
10
10
  }
11
11
  }
12
12
 
13
- export class FunctionNotFoundError extends BaseError.extend('FUNCTION_NOT_FOUND', 'Function not found') {
13
+ export class FunctionNotFoundError extends BaseError.extend('FunctionNotFound', 'Function not found') {
14
14
  constructor(functionKey: string, options?: Omit<BaseErrorOptions, 'context'>) {
15
15
  super({ context: { function: functionKey }, ...options });
16
16
  }
17
17
  }
18
18
 
19
- export class FunctionError extends BaseError.extend('FUNCTION_ERROR', 'Function invocation error') {}
19
+ export class FunctionError extends BaseError.extend('FunctionError', 'Function invocation error') {}
20
20
 
21
- export class TriggerStateNotFoundError extends BaseError.extend('TRIGGER_STATE_NOT_FOUND', 'Trigger state not found') {}
21
+ export class TriggerStateNotFoundError extends BaseError.extend('TriggerStateNotFound', 'Trigger state not found') {}
@@ -0,0 +1,48 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { Operation } from '@dxos/operation';
6
+ import * as Schema from 'effect/Schema';
7
+
8
+ export const Fibonacci = Operation.make({
9
+ meta: {
10
+ key: 'example.org/function/fib',
11
+ name: 'Fibonacci',
12
+ description: 'Function that calculates a Fibonacci number',
13
+ },
14
+ input: Schema.Struct({
15
+ iterations: Schema.optional(Schema.Number).annotations({
16
+ description: 'Number of iterations',
17
+ default: 100_000,
18
+ }),
19
+ }),
20
+ output: Schema.Struct({
21
+ result: Schema.String,
22
+ }),
23
+ });
24
+
25
+ export const Reply = Operation.make({
26
+ meta: {
27
+ key: 'example.org/function/reply',
28
+ name: 'Reply',
29
+ description: 'Function that echoes the input',
30
+ },
31
+ input: Schema.Any,
32
+ output: Schema.Any,
33
+ });
34
+
35
+ export const Sleep = Operation.make({
36
+ meta: {
37
+ key: 'example.org/function/sleep',
38
+ name: 'Sleep',
39
+ description: 'Function that sleeps for a given amount of time',
40
+ },
41
+ input: Schema.Struct({
42
+ duration: Schema.optional(Schema.Number).annotations({
43
+ description: 'Milliseconds to sleep',
44
+ default: 100_000,
45
+ }),
46
+ }),
47
+ output: Schema.Void,
48
+ });
@@ -3,30 +3,20 @@
3
3
  //
4
4
 
5
5
  import * as Effect from 'effect/Effect';
6
- import * as Schema from 'effect/Schema';
7
6
 
8
- import { defineFunction } from '../sdk';
7
+ import { Operation } from '@dxos/operation';
8
+ import { Fibonacci } from './definitions';
9
9
 
10
- export default defineFunction({
11
- key: 'example.org/function/fib',
12
- name: 'Fibonacci',
13
- description: 'Function that calculates a Fibonacci number',
14
- inputSchema: Schema.Struct({
15
- iterations: Schema.optional(Schema.Number).annotations({
16
- description: 'Number of iterations',
17
- default: 100_000,
10
+ export default Fibonacci.pipe(
11
+ Operation.withHandler(
12
+ Effect.fn(function* ({ iterations = 100_000 }) {
13
+ let a = 0n;
14
+ let b = 1n;
15
+ for (let i = 0; i < iterations; i++) {
16
+ a += b;
17
+ b = a - b;
18
+ }
19
+ return { result: a.toString() };
18
20
  }),
19
- }),
20
- outputSchema: Schema.Struct({
21
- result: Schema.String,
22
- }),
23
- handler: Effect.fn(function* ({ data: { iterations = 100_000 } }) {
24
- let a = 0n;
25
- let b = 1n;
26
- for (let i = 0; i < iterations; i++) {
27
- a += b;
28
- b = a - b;
29
- }
30
- return { result: a.toString() };
31
- }),
32
- });
21
+ ),
22
+ );
@@ -14,7 +14,7 @@ import {
14
14
  import { Effect, Schedule } from 'https://esm.sh/effect@3.17.0?bundle=false';
15
15
 
16
16
  export default defineFunction({
17
- key: 'dxos.org/script/forex-effect',
17
+ key: 'org.dxos.script.forex-effect',
18
18
  name: 'Forex Effect',
19
19
  description: 'Returns the exchange rate between two currencies.',
20
20
 
@@ -2,12 +2,11 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { default as fib$ } from './fib';
6
- import { default as reply$ } from './reply';
7
- import { default as sleep$ } from './sleep';
5
+ import { OperationHandlerSet } from '@dxos/operation';
6
+ export * from './definitions';
8
7
 
9
- export namespace Example {
10
- export const fib = fib$;
11
- export const reply = reply$;
12
- export const sleep = sleep$;
13
- }
8
+ export const ExampleHandlers = OperationHandlerSet.lazy(
9
+ () => import('./fib'),
10
+ () => import('./reply'),
11
+ () => import('./sleep'),
12
+ );
@@ -4,18 +4,15 @@
4
4
 
5
5
  import * as Console from 'effect/Console';
6
6
  import * as Effect from 'effect/Effect';
7
- import * as Schema from 'effect/Schema';
8
7
 
9
- import { defineFunction } from '../sdk';
8
+ import { Operation } from '@dxos/operation';
9
+ import { Reply } from './definitions';
10
10
 
11
- export default defineFunction({
12
- key: 'example.org/function/reply',
13
- name: 'Reply',
14
- description: 'Function that echoes the input',
15
- inputSchema: Schema.Any,
16
- outputSchema: Schema.Any,
17
- handler: Effect.fn(function* ({ data }) {
18
- yield* Console.log('reply', { data });
19
- return data;
20
- }),
21
- });
11
+ export default Reply.pipe(
12
+ Operation.withHandler(
13
+ Effect.fn(function* (input) {
14
+ yield* Console.log('reply', { input });
15
+ return input;
16
+ }),
17
+ ),
18
+ );
@@ -3,22 +3,13 @@
3
3
  //
4
4
 
5
5
  import * as Effect from 'effect/Effect';
6
- import * as Schema from 'effect/Schema';
6
+ import { Sleep } from './definitions';
7
+ import { Operation } from '@dxos/operation';
7
8
 
8
- import { defineFunction } from '../sdk';
9
-
10
- export default defineFunction({
11
- key: 'example.org/function/sleep',
12
- name: 'Sleep',
13
- description: 'Function that sleeps for a given amount of time',
14
- inputSchema: Schema.Struct({
15
- duration: Schema.optional(Schema.Number).annotations({
16
- description: 'Milliseconds to sleep',
17
- default: 100_000,
9
+ export default Sleep.pipe(
10
+ Operation.withHandler(
11
+ Effect.fn(function* ({ duration = 100_000 }) {
12
+ yield* Effect.sleep(duration);
18
13
  }),
19
- }),
20
- outputSchema: Schema.Void,
21
- handler: Effect.fn(function* ({ data: { duration = 100_000 } }) {
22
- yield* Effect.sleep(duration);
23
- }),
24
- });
14
+ ),
15
+ );
@@ -0,0 +1,67 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Headers from '@effect/platform/Headers';
6
+ import * as HttpClient from '@effect/platform/HttpClient';
7
+ import * as HttpClientError from '@effect/platform/HttpClientError';
8
+ import * as HttpClientResponse from '@effect/platform/HttpClientResponse';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as FiberRef from 'effect/FiberRef';
11
+ import * as Layer from 'effect/Layer';
12
+ import * as Stream from 'effect/Stream';
13
+
14
+ import { log } from '@dxos/log';
15
+ import { type EdgeFunctionEnv, ErrorCodec } from '@dxos/protocols';
16
+ /**
17
+ * Copy pasted from https://github.com/Effect-TS/effect/blob/main/packages/platform/src/internal/fetchHttpClient.ts
18
+ */
19
+ export const requestInitTagKey = '@effect/platform/FetchHttpClient/FetchOptions';
20
+
21
+ export class FunctionsAiHttpClient {
22
+ static make = (service: EdgeFunctionEnv.FunctionsAiService) =>
23
+ HttpClient.make((request, url, signal, fiber) => {
24
+ const context = fiber.getFiberRef(FiberRef.currentContext);
25
+ const options: RequestInit = context.unsafeMap.get(requestInitTagKey) ?? {};
26
+ const headers = options.headers
27
+ ? Headers.merge(Headers.fromInput(options.headers), request.headers)
28
+ : request.headers;
29
+
30
+ const send = (body: BodyInit | undefined) =>
31
+ Effect.tryPromise({
32
+ try: () =>
33
+ service.fetch(
34
+ new Request(url, {
35
+ ...options,
36
+ method: request.method,
37
+ headers,
38
+ body,
39
+ // Note: Don't pass signal - it can't be serialized through RPC
40
+ }),
41
+ ),
42
+ catch: (cause) => {
43
+ log.error('Failed to fetch', { errorSerialized: ErrorCodec.encode(cause as Error) });
44
+ return new HttpClientError.RequestError({
45
+ request,
46
+ reason: 'Transport',
47
+ cause,
48
+ });
49
+ },
50
+ }).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response)));
51
+
52
+ switch (request.body._tag) {
53
+ case 'Raw':
54
+ case 'Uint8Array':
55
+ return send(request.body.body as any);
56
+ case 'FormData':
57
+ return send(request.body.formData);
58
+ case 'Stream':
59
+ return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect.flatMap(send));
60
+ }
61
+
62
+ return send(undefined);
63
+ });
64
+
65
+ static layer = (service: EdgeFunctionEnv.FunctionsAiService) =>
66
+ Layer.succeed(HttpClient.HttpClient, FunctionsAiHttpClient.make(service));
67
+ }