@dxos/functions 0.8.4-main.fffef41 → 0.9.0

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 (83) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +5 -7
  3. package/dist/lib/neutral/index.mjs +584 -0
  4. package/dist/lib/neutral/index.mjs.map +7 -0
  5. package/dist/lib/neutral/meta.json +1 -0
  6. package/dist/types/src/index.d.ts +0 -2
  7. package/dist/types/src/index.d.ts.map +1 -1
  8. package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
  9. package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
  10. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
  11. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
  12. package/dist/types/src/protocol/protocol.d.ts +14 -2
  13. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  14. package/dist/types/src/sdk.d.ts +5 -84
  15. package/dist/types/src/sdk.d.ts.map +1 -1
  16. package/dist/types/src/services/credentials.d.ts +17 -38
  17. package/dist/types/src/services/credentials.d.ts.map +1 -1
  18. package/dist/types/src/services/function-invocation-service.d.ts +10 -3
  19. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  20. package/dist/types/src/services/index.d.ts +1 -6
  21. package/dist/types/src/services/index.d.ts.map +1 -1
  22. package/dist/types/src/services/tracing.d.ts +1 -50
  23. package/dist/types/src/services/tracing.d.ts.map +1 -1
  24. package/dist/types/src/types/index.d.ts +0 -4
  25. package/dist/types/src/types/index.d.ts.map +1 -1
  26. package/dist/types/src/types/url.d.ts +6 -5
  27. package/dist/types/src/types/url.d.ts.map +1 -1
  28. package/dist/types/tsconfig.tsbuildinfo +1 -1
  29. package/package.json +25 -18
  30. package/src/index.ts +0 -2
  31. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  32. package/src/protocol/functions-ai-http-client.ts +141 -0
  33. package/src/protocol/protocol.test.ts +10 -10
  34. package/src/protocol/protocol.ts +355 -86
  35. package/src/sdk.ts +12 -209
  36. package/src/services/credentials.ts +80 -108
  37. package/src/services/function-invocation-service.ts +20 -7
  38. package/src/services/index.ts +1 -7
  39. package/src/services/tracing.ts +0 -100
  40. package/src/types/index.ts +0 -4
  41. package/src/types/url.ts +6 -5
  42. package/dist/lib/browser/index.mjs +0 -927
  43. package/dist/lib/browser/index.mjs.map +0 -7
  44. package/dist/lib/browser/meta.json +0 -1
  45. package/dist/lib/node-esm/index.mjs +0 -928
  46. package/dist/lib/node-esm/index.mjs.map +0 -7
  47. package/dist/lib/node-esm/meta.json +0 -1
  48. package/dist/types/src/errors.d.ts +0 -129
  49. package/dist/types/src/errors.d.ts.map +0 -1
  50. package/dist/types/src/example/fib.d.ts +0 -7
  51. package/dist/types/src/example/fib.d.ts.map +0 -1
  52. package/dist/types/src/example/forex-effect.d.ts +0 -3
  53. package/dist/types/src/example/forex-effect.d.ts.map +0 -1
  54. package/dist/types/src/example/index.d.ts +0 -12
  55. package/dist/types/src/example/index.d.ts.map +0 -1
  56. package/dist/types/src/example/reply.d.ts +0 -3
  57. package/dist/types/src/example/reply.d.ts.map +0 -1
  58. package/dist/types/src/example/sleep.d.ts +0 -5
  59. package/dist/types/src/example/sleep.d.ts.map +0 -1
  60. package/dist/types/src/services/event-logger.d.ts +0 -87
  61. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  62. package/dist/types/src/services/queues.d.ts +0 -47
  63. package/dist/types/src/services/queues.d.ts.map +0 -1
  64. package/dist/types/src/types/Function.d.ts +0 -58
  65. package/dist/types/src/types/Function.d.ts.map +0 -1
  66. package/dist/types/src/types/Script.d.ts +0 -28
  67. package/dist/types/src/types/Script.d.ts.map +0 -1
  68. package/dist/types/src/types/Trigger.d.ts +0 -139
  69. package/dist/types/src/types/Trigger.d.ts.map +0 -1
  70. package/dist/types/src/types/TriggerEvent.d.ts +0 -44
  71. package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
  72. package/src/errors.ts +0 -21
  73. package/src/example/fib.ts +0 -32
  74. package/src/example/forex-effect.ts +0 -40
  75. package/src/example/index.ts +0 -13
  76. package/src/example/reply.ts +0 -21
  77. package/src/example/sleep.ts +0 -24
  78. package/src/services/event-logger.ts +0 -127
  79. package/src/services/queues.ts +0 -84
  80. package/src/types/Function.ts +0 -62
  81. package/src/types/Script.ts +0 -33
  82. package/src/types/Trigger.ts +0 -139
  83. package/src/types/TriggerEvent.ts +0 -62
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.fffef41",
3
+ "version": "0.9.0",
4
4
  "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
7
- "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
11
+ "license": "FSL-1.1-Apache-2.0",
8
12
  "author": "info@dxos.org",
9
13
  "sideEffects": false,
10
14
  "type": "module",
@@ -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,24 @@
23
26
  "src"
24
27
  ],
25
28
  "dependencies": {
26
- "@effect/platform": "0.92.1",
27
- "effect": "3.18.3",
28
- "@dxos/ai": "0.8.4-main.fffef41",
29
- "@dxos/errors": "0.8.4-main.fffef41",
30
- "@dxos/echo-db": "0.8.4-main.fffef41",
31
- "@dxos/invariant": "0.8.4-main.fffef41",
32
- "@dxos/keys": "0.8.4-main.fffef41",
33
- "@dxos/log": "0.8.4-main.fffef41",
34
- "@dxos/node-std": "0.8.4-main.fffef41",
35
- "@dxos/effect": "0.8.4-main.fffef41",
36
- "@dxos/schema": "0.8.4-main.fffef41",
37
- "@dxos/protocols": "0.8.4-main.fffef41",
38
- "@dxos/echo": "0.8.4-main.fffef41",
39
- "@dxos/types": "0.8.4-main.fffef41"
29
+ "@effect-atom/atom": "^0.5.3",
30
+ "@effect/ai-anthropic": "0.26.0",
31
+ "@effect/platform": "0.96.1",
32
+ "effect": "3.21.3",
33
+ "@dxos/ai": "0.9.0",
34
+ "@dxos/compute": "0.9.0",
35
+ "@dxos/echo": "0.9.0",
36
+ "@dxos/context": "0.9.0",
37
+ "@dxos/echo-client": "0.9.0",
38
+ "@dxos/invariant": "0.9.0",
39
+ "@dxos/errors": "0.9.0",
40
+ "@dxos/effect": "0.9.0",
41
+ "@dxos/keys": "0.9.0",
42
+ "@dxos/log": "0.9.0",
43
+ "@dxos/protocols": "0.9.0",
44
+ "@dxos/schema": "0.9.0",
45
+ "@dxos/node-std": "0.9.0",
46
+ "@dxos/types": "0.9.0"
40
47
  },
41
48
  "publishConfig": {
42
49
  "access": "public"
package/src/index.ts CHANGED
@@ -2,8 +2,6 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './errors';
6
- export * from './example';
7
5
  export * from './sdk';
8
6
  export * from './services';
9
7
  export * from './types';
@@ -0,0 +1,105 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import * as HttpClient from '@effect/platform/HttpClient';
6
+ import * as HttpClientRequest from '@effect/platform/HttpClientRequest';
7
+ import * as Cause from 'effect/Cause';
8
+ import * as Chunk from 'effect/Chunk';
9
+ import * as Effect from 'effect/Effect';
10
+ import * as Exit from 'effect/Exit';
11
+ import { describe, test } from 'vitest';
12
+
13
+ import { FunctionsAiMemoizationMissError, FunctionsAiUpstreamError } from '@dxos/compute';
14
+ import { EffectEx } from '@dxos/effect';
15
+ import { type EdgeFunctionEnv } from '@dxos/protocols';
16
+
17
+ import { FunctionsAiHttpClient } from './functions-ai-http-client';
18
+
19
+ const makeStubService = (response: Response): EdgeFunctionEnv.FunctionsAiService => ({
20
+ fetch: async () => response as any,
21
+ });
22
+
23
+ const runRequest = (service: EdgeFunctionEnv.FunctionsAiService) =>
24
+ HttpClient.HttpClient.pipe(
25
+ Effect.flatMap((client) =>
26
+ client.execute(HttpClientRequest.post('http://internal/provider/anthropic/v1/messages')),
27
+ ),
28
+ Effect.provide(FunctionsAiHttpClient.layer(service)),
29
+ );
30
+
31
+ const extractDefect = (exit: Exit.Exit<unknown, unknown>): Error | null => {
32
+ if (Exit.isSuccess(exit)) {
33
+ return null;
34
+ }
35
+ return (Chunk.toReadonlyArray(Cause.defects(exit.cause))[0] as Error | undefined) ?? null;
36
+ };
37
+
38
+ describe('FunctionsAiHttpClient', () => {
39
+ test('emits FunctionsAiMemoizationMissError when upstream returns the memoization_miss envelope', async ({
40
+ expect,
41
+ }) => {
42
+ const service = makeStubService(
43
+ new Response(
44
+ JSON.stringify({
45
+ type: 'error',
46
+ error: {
47
+ type: 'memoization_miss',
48
+ message: 'No memoized Anthropic conversation found for POST /v1/messages (key=abc).',
49
+ cacheKey: 'abc',
50
+ },
51
+ }),
52
+ { status: 500, headers: { 'content-type': 'application/json' } },
53
+ ),
54
+ );
55
+
56
+ const exit = await Effect.runPromiseExit(runRequest(service));
57
+ const error = extractDefect(exit);
58
+ expect(error).toBeInstanceOf(FunctionsAiMemoizationMissError);
59
+ expect(error?.name).toBe('FunctionsAiMemoizationMissError');
60
+ expect((error as any)?.context?.cacheKey).toBe('abc');
61
+ expect((error as any)?.context?.status).toBe(500);
62
+ });
63
+
64
+ test('emits FunctionsAiUpstreamError for generic JSON error envelopes', async ({ expect }) => {
65
+ const service = makeStubService(
66
+ new Response(
67
+ JSON.stringify({
68
+ type: 'error',
69
+ error: { type: 'overloaded_error', message: 'Server overloaded.' },
70
+ }),
71
+ { status: 529, headers: { 'content-type': 'application/json' } },
72
+ ),
73
+ );
74
+
75
+ const exit = await Effect.runPromiseExit(runRequest(service));
76
+ const error = extractDefect(exit);
77
+ expect(error).toBeInstanceOf(FunctionsAiUpstreamError);
78
+ expect((error as any)?.context?.type).toBe('overloaded_error');
79
+ expect((error as any)?.context?.status).toBe(529);
80
+ });
81
+
82
+ test('passes non-error responses through unchanged', async ({ expect }) => {
83
+ const service = makeStubService(
84
+ new Response(JSON.stringify({ ok: true }), {
85
+ status: 200,
86
+ headers: { 'content-type': 'application/json' },
87
+ }),
88
+ );
89
+
90
+ const result = await EffectEx.runAndForwardErrors(runRequest(service));
91
+ expect(result.status).toBe(200);
92
+ });
93
+
94
+ test('passes non-JSON 5xx responses through to the @effect/ai layer unchanged', async ({ expect }) => {
95
+ const service = makeStubService(
96
+ new Response('Internal Server Error', {
97
+ status: 500,
98
+ headers: { 'content-type': 'text/plain' },
99
+ }),
100
+ );
101
+
102
+ const result = await EffectEx.runAndForwardErrors(runRequest(service));
103
+ expect(result.status).toBe(500);
104
+ });
105
+ });
@@ -0,0 +1,141 @@
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 { FunctionsAiMemoizationMissError, FunctionsAiUpstreamError } from '@dxos/compute';
15
+ import { log } from '@dxos/log';
16
+ import { type EdgeFunctionEnv, ErrorCodec } from '@dxos/protocols';
17
+
18
+ /**
19
+ * Copy pasted from https://github.com/Effect-TS/effect/blob/main/packages/platform/src/internal/fetchHttpClient.ts
20
+ */
21
+ export const requestInitTagKey = '@effect/platform/FetchHttpClient/FetchOptions';
22
+
23
+ /**
24
+ * Shape of the JSON error envelope emitted by the upstream AI gateway (and by the memoization
25
+ * layer that fronts it in test environments).
26
+ *
27
+ * @example
28
+ * ```json
29
+ * {
30
+ * "type": "error",
31
+ * "error": {
32
+ * "type": "memoization_miss",
33
+ * "message": "No memoized Anthropic conversation found for ...",
34
+ * "cacheKey": "114dae3db8fe60..."
35
+ * }
36
+ * }
37
+ * ```
38
+ */
39
+ type UpstreamErrorEnvelope = {
40
+ type?: string;
41
+ error?: {
42
+ type?: string;
43
+ message?: string;
44
+ cacheKey?: string;
45
+ };
46
+ };
47
+
48
+ export class FunctionsAiHttpClient {
49
+ static make = (service: EdgeFunctionEnv.FunctionsAiService) =>
50
+ HttpClient.make((request, url, signal, fiber) => {
51
+ const context = fiber.getFiberRef(FiberRef.currentContext);
52
+ const options: RequestInit = context.unsafeMap.get(requestInitTagKey) ?? {};
53
+ const headers = options.headers
54
+ ? Headers.merge(Headers.fromInput(options.headers), request.headers)
55
+ : request.headers;
56
+
57
+ const send = (body: BodyInit | undefined) =>
58
+ Effect.tryPromise({
59
+ try: () =>
60
+ service.fetch(
61
+ new Request(url, {
62
+ ...options,
63
+ method: request.method,
64
+ headers,
65
+ body,
66
+ // Note: Don't pass signal - it can't be serialized through RPC
67
+ }),
68
+ ),
69
+ catch: (cause) => {
70
+ log.error('Failed to fetch', { errorSerialized: ErrorCodec.encode(cause as Error) });
71
+ return new HttpClientError.RequestError({
72
+ request,
73
+ reason: 'Transport',
74
+ cause,
75
+ });
76
+ },
77
+ }).pipe(
78
+ Effect.flatMap((response) =>
79
+ // Inspect the body before handing the response to `@effect/ai` so that structured
80
+ // upstream errors surface as typed defects (`FunctionsAiUpstreamError` and friends)
81
+ // rather than as the generic `HttpResponseError` from `@effect/ai/AiError`.
82
+ Effect.flatMap(
83
+ Effect.promise(() => parseUpstreamError(response)),
84
+ (typedError) =>
85
+ typedError ? Effect.die(typedError) : Effect.succeed(HttpClientResponse.fromWeb(request, response)),
86
+ ),
87
+ ),
88
+ );
89
+
90
+ switch (request.body._tag) {
91
+ case 'Raw':
92
+ case 'Uint8Array':
93
+ return send(request.body.body as any);
94
+ case 'FormData':
95
+ return send(request.body.formData);
96
+ case 'Stream':
97
+ return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect.flatMap(send));
98
+ }
99
+
100
+ return send(undefined);
101
+ });
102
+
103
+ static layer = (service: EdgeFunctionEnv.FunctionsAiService) =>
104
+ Layer.succeed(HttpClient.HttpClient, FunctionsAiHttpClient.make(service));
105
+ }
106
+
107
+ /**
108
+ * Returns a typed error if the response is a non-2xx JSON payload matching
109
+ * {@link UpstreamErrorEnvelope}; otherwise returns `undefined` and the response is forwarded
110
+ * unchanged.
111
+ */
112
+ const parseUpstreamError = async (response: Response): Promise<Error | undefined> => {
113
+ if (response.ok) {
114
+ return undefined;
115
+ }
116
+ const contentType = response.headers.get('content-type') ?? '';
117
+ if (!contentType.toLowerCase().includes('application/json')) {
118
+ return undefined;
119
+ }
120
+ let body: UpstreamErrorEnvelope;
121
+ try {
122
+ body = (await response.clone().json()) as UpstreamErrorEnvelope;
123
+ } catch {
124
+ return undefined;
125
+ }
126
+ if (!body || body.type !== 'error' || typeof body.error !== 'object' || body.error === null) {
127
+ return undefined;
128
+ }
129
+ const inner = body.error;
130
+ const message = inner.message ?? `Upstream AI service responded with HTTP ${response.status}`;
131
+ if (inner.type === 'memoization_miss' && typeof inner.cacheKey === 'string') {
132
+ return new FunctionsAiMemoizationMissError({
133
+ message,
134
+ context: { cacheKey: inner.cacheKey, status: response.status },
135
+ });
136
+ }
137
+ return new FunctionsAiUpstreamError({
138
+ message,
139
+ context: { type: inner.type, status: response.status, ...(inner.cacheKey ? { cacheKey: inner.cacheKey } : {}) },
140
+ });
141
+ };
@@ -4,17 +4,17 @@
4
4
 
5
5
  import { describe, test } from 'vitest';
6
6
 
7
- import { FunctionError } from '../errors';
8
- import fibFunc from '../example/fib';
9
- import replyFunc from '../example/reply';
7
+ import { InvalidOperationInputError } from '@dxos/compute';
8
+ import { FibonacciHandler, ReplyHandler } from '@dxos/compute/testing';
9
+ import { DXN } from '@dxos/keys';
10
10
 
11
11
  import { wrapFunctionHandler } from './protocol';
12
12
 
13
13
  describe('wrapFunctionHandler', () => {
14
14
  test('wraps reply function and executes handler', async ({ expect }) => {
15
- const wrapped = wrapFunctionHandler(replyFunc);
15
+ const wrapped = wrapFunctionHandler(ReplyHandler);
16
16
 
17
- expect(wrapped.meta.key).toBe('example.org/function/reply');
17
+ expect(wrapped.meta.key).toBe(DXN.make('org.example.function.reply'));
18
18
  expect(wrapped.meta.name).toBe('Reply');
19
19
 
20
20
  const testData = { message: 'hello' };
@@ -29,9 +29,9 @@ describe('wrapFunctionHandler', () => {
29
29
  });
30
30
 
31
31
  test('wraps fibonacci function with valid input', async ({ expect }) => {
32
- const wrapped = wrapFunctionHandler(fibFunc);
32
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
33
33
 
34
- expect(wrapped.meta.key).toBe('example.org/function/fib');
34
+ expect(wrapped.meta.key).toBe(DXN.make('org.example.function.fib'));
35
35
  expect(wrapped.meta.name).toBe('Fibonacci');
36
36
 
37
37
  const result = await wrapped.handler({
@@ -44,8 +44,8 @@ describe('wrapFunctionHandler', () => {
44
44
  expect(result).toEqual({ result: '55' });
45
45
  });
46
46
 
47
- test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
48
- const wrapped = wrapFunctionHandler(fibFunc);
47
+ test('throws InvalidOperationInputError on invalid input schema for fibonacci', async ({ expect }) => {
48
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
49
49
 
50
50
  await expect(
51
51
  wrapped.handler({
@@ -54,6 +54,6 @@ describe('wrapFunctionHandler', () => {
54
54
  services: {},
55
55
  },
56
56
  }),
57
- ).rejects.toThrow(FunctionError);
57
+ ).rejects.toThrow(InvalidOperationInputError);
58
58
  });
59
59
  });