@dxos/functions 0.8.4-main.ef1bc66f44 → 0.8.4-main.f466a3d56e

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 (79) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +5 -7
  3. package/dist/lib/neutral/index.mjs +295 -922
  4. package/dist/lib/neutral/index.mjs.map +4 -4
  5. package/dist/lib/neutral/meta.json +1 -1
  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.map +1 -1
  9. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
  10. package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
  11. package/dist/types/src/protocol/protocol.d.ts +19 -2
  12. package/dist/types/src/protocol/protocol.d.ts.map +1 -1
  13. package/dist/types/src/sdk.d.ts +5 -109
  14. package/dist/types/src/sdk.d.ts.map +1 -1
  15. package/dist/types/src/services/credentials.d.ts +15 -38
  16. package/dist/types/src/services/credentials.d.ts.map +1 -1
  17. package/dist/types/src/services/function-invocation-service.d.ts +8 -6
  18. package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
  19. package/dist/types/src/services/index.d.ts +1 -4
  20. package/dist/types/src/services/index.d.ts.map +1 -1
  21. package/dist/types/src/services/queues.d.ts +1 -46
  22. package/dist/types/src/services/queues.d.ts.map +1 -1
  23. package/dist/types/src/services/tracing.d.ts +1 -84
  24. package/dist/types/src/services/tracing.d.ts.map +1 -1
  25. package/dist/types/src/types/index.d.ts +0 -4
  26. package/dist/types/src/types/index.d.ts.map +1 -1
  27. package/dist/types/src/types/url.d.ts +3 -3
  28. package/dist/types/src/types/url.d.ts.map +1 -1
  29. package/dist/types/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +18 -17
  31. package/src/index.ts +0 -2
  32. package/src/protocol/functions-ai-http-client.test.ts +105 -0
  33. package/src/protocol/functions-ai-http-client.ts +75 -1
  34. package/src/protocol/protocol.test.ts +9 -10
  35. package/src/protocol/protocol.ts +252 -59
  36. package/src/sdk.ts +12 -270
  37. package/src/services/credentials.ts +76 -120
  38. package/src/services/function-invocation-service.ts +9 -10
  39. package/src/services/index.ts +1 -4
  40. package/src/services/queues.ts +1 -78
  41. package/src/services/tracing.ts +0 -159
  42. package/src/types/index.ts +0 -4
  43. package/src/types/url.ts +3 -3
  44. package/dist/types/src/errors.d.ts +0 -121
  45. package/dist/types/src/errors.d.ts.map +0 -1
  46. package/dist/types/src/example/fib.d.ts +0 -7
  47. package/dist/types/src/example/fib.d.ts.map +0 -1
  48. package/dist/types/src/example/forex-effect.d.ts +0 -3
  49. package/dist/types/src/example/forex-effect.d.ts.map +0 -1
  50. package/dist/types/src/example/index.d.ts +0 -12
  51. package/dist/types/src/example/index.d.ts.map +0 -1
  52. package/dist/types/src/example/reply.d.ts +0 -3
  53. package/dist/types/src/example/reply.d.ts.map +0 -1
  54. package/dist/types/src/example/sleep.d.ts +0 -5
  55. package/dist/types/src/example/sleep.d.ts.map +0 -1
  56. package/dist/types/src/operation-compatibility.test.d.ts +0 -2
  57. package/dist/types/src/operation-compatibility.test.d.ts.map +0 -1
  58. package/dist/types/src/services/event-logger.d.ts +0 -81
  59. package/dist/types/src/services/event-logger.d.ts.map +0 -1
  60. package/dist/types/src/types/Function.d.ts +0 -52
  61. package/dist/types/src/types/Function.d.ts.map +0 -1
  62. package/dist/types/src/types/Script.d.ts +0 -21
  63. package/dist/types/src/types/Script.d.ts.map +0 -1
  64. package/dist/types/src/types/Trigger.d.ts +0 -121
  65. package/dist/types/src/types/Trigger.d.ts.map +0 -1
  66. package/dist/types/src/types/TriggerEvent.d.ts +0 -74
  67. package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
  68. package/src/errors.ts +0 -21
  69. package/src/example/fib.ts +0 -32
  70. package/src/example/forex-effect.ts +0 -40
  71. package/src/example/index.ts +0 -13
  72. package/src/example/reply.ts +0 -21
  73. package/src/example/sleep.ts +0 -24
  74. package/src/operation-compatibility.test.ts +0 -185
  75. package/src/services/event-logger.ts +0 -127
  76. package/src/types/Function.ts +0 -82
  77. package/src/types/Script.ts +0 -34
  78. package/src/types/Trigger.ts +0 -143
  79. package/src/types/TriggerEvent.ts +0 -62
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/functions",
3
- "version": "0.8.4-main.ef1bc66f44",
3
+ "version": "0.8.4-main.f466a3d56e",
4
4
  "description": "Functions API.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -8,7 +8,7 @@
8
8
  "type": "git",
9
9
  "url": "https://github.com/dxos/dxos"
10
10
  },
11
- "license": "MIT",
11
+ "license": "FSL-1.1-Apache-2.0",
12
12
  "author": "info@dxos.org",
13
13
  "sideEffects": false,
14
14
  "type": "module",
@@ -26,23 +26,24 @@
26
26
  "src"
27
27
  ],
28
28
  "dependencies": {
29
+ "@effect-atom/atom": "^0.5.1",
29
30
  "@effect/ai-anthropic": "0.23.0",
30
31
  "@effect/platform": "0.94.4",
31
- "effect": "3.19.16",
32
- "@dxos/context": "0.8.4-main.ef1bc66f44",
33
- "@dxos/ai": "0.8.4-main.ef1bc66f44",
34
- "@dxos/echo-db": "0.8.4-main.ef1bc66f44",
35
- "@dxos/effect": "0.8.4-main.ef1bc66f44",
36
- "@dxos/echo": "0.8.4-main.ef1bc66f44",
37
- "@dxos/invariant": "0.8.4-main.ef1bc66f44",
38
- "@dxos/errors": "0.8.4-main.ef1bc66f44",
39
- "@dxos/keys": "0.8.4-main.ef1bc66f44",
40
- "@dxos/node-std": "0.8.4-main.ef1bc66f44",
41
- "@dxos/operation": "0.8.4-main.ef1bc66f44",
42
- "@dxos/log": "0.8.4-main.ef1bc66f44",
43
- "@dxos/schema": "0.8.4-main.ef1bc66f44",
44
- "@dxos/protocols": "0.8.4-main.ef1bc66f44",
45
- "@dxos/types": "0.8.4-main.ef1bc66f44"
32
+ "effect": "3.20.0",
33
+ "@dxos/compute": "0.8.4-main.f466a3d56e",
34
+ "@dxos/context": "0.8.4-main.f466a3d56e",
35
+ "@dxos/echo-db": "0.8.4-main.f466a3d56e",
36
+ "@dxos/ai": "0.8.4-main.f466a3d56e",
37
+ "@dxos/echo": "0.8.4-main.f466a3d56e",
38
+ "@dxos/effect": "0.8.4-main.f466a3d56e",
39
+ "@dxos/errors": "0.8.4-main.f466a3d56e",
40
+ "@dxos/keys": "0.8.4-main.f466a3d56e",
41
+ "@dxos/invariant": "0.8.4-main.f466a3d56e",
42
+ "@dxos/node-std": "0.8.4-main.f466a3d56e",
43
+ "@dxos/protocols": "0.8.4-main.f466a3d56e",
44
+ "@dxos/log": "0.8.4-main.f466a3d56e",
45
+ "@dxos/schema": "0.8.4-main.f466a3d56e",
46
+ "@dxos/types": "0.8.4-main.f466a3d56e"
46
47
  },
47
48
  "publishConfig": {
48
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 { runAndForwardErrors } 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 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 runAndForwardErrors(runRequest(service));
103
+ expect(result.status).toBe(500);
104
+ });
105
+ });
@@ -11,13 +11,40 @@ import * as FiberRef from 'effect/FiberRef';
11
11
  import * as Layer from 'effect/Layer';
12
12
  import * as Stream from 'effect/Stream';
13
13
 
14
+ import { FunctionsAiMemoizationMissError, FunctionsAiUpstreamError } from '@dxos/compute';
14
15
  import { log } from '@dxos/log';
15
16
  import { type EdgeFunctionEnv, ErrorCodec } from '@dxos/protocols';
17
+
16
18
  /**
17
19
  * Copy pasted from https://github.com/Effect-TS/effect/blob/main/packages/platform/src/internal/fetchHttpClient.ts
18
20
  */
19
21
  export const requestInitTagKey = '@effect/platform/FetchHttpClient/FetchOptions';
20
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
+
21
48
  export class FunctionsAiHttpClient {
22
49
  static make = (service: EdgeFunctionEnv.FunctionsAiService) =>
23
50
  HttpClient.make((request, url, signal, fiber) => {
@@ -47,7 +74,18 @@ export class FunctionsAiHttpClient {
47
74
  cause,
48
75
  });
49
76
  },
50
- }).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response)));
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
+ );
51
89
 
52
90
  switch (request.body._tag) {
53
91
  case 'Raw':
@@ -65,3 +103,39 @@ export class FunctionsAiHttpClient {
65
103
  static layer = (service: EdgeFunctionEnv.FunctionsAiService) =>
66
104
  Layer.succeed(HttpClient.HttpClient, FunctionsAiHttpClient.make(service));
67
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,16 @@
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';
10
9
 
11
10
  import { wrapFunctionHandler } from './protocol';
12
11
 
13
12
  describe('wrapFunctionHandler', () => {
14
13
  test('wraps reply function and executes handler', async ({ expect }) => {
15
- const wrapped = wrapFunctionHandler(replyFunc);
14
+ const wrapped = wrapFunctionHandler(ReplyHandler);
16
15
 
17
- expect(wrapped.meta.key).toBe('example.org/function/reply');
16
+ expect(wrapped.meta.key).toBe('org.example.function.reply');
18
17
  expect(wrapped.meta.name).toBe('Reply');
19
18
 
20
19
  const testData = { message: 'hello' };
@@ -29,9 +28,9 @@ describe('wrapFunctionHandler', () => {
29
28
  });
30
29
 
31
30
  test('wraps fibonacci function with valid input', async ({ expect }) => {
32
- const wrapped = wrapFunctionHandler(fibFunc);
31
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
33
32
 
34
- expect(wrapped.meta.key).toBe('example.org/function/fib');
33
+ expect(wrapped.meta.key).toBe('org.example.function.fib');
35
34
  expect(wrapped.meta.name).toBe('Fibonacci');
36
35
 
37
36
  const result = await wrapped.handler({
@@ -44,8 +43,8 @@ describe('wrapFunctionHandler', () => {
44
43
  expect(result).toEqual({ result: '55' });
45
44
  });
46
45
 
47
- test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
48
- const wrapped = wrapFunctionHandler(fibFunc);
46
+ test('throws InvalidOperationInputError on invalid input schema for fibonacci', async ({ expect }) => {
47
+ const wrapped = wrapFunctionHandler(FibonacciHandler);
49
48
 
50
49
  await expect(
51
50
  wrapped.handler({
@@ -54,6 +53,6 @@ describe('wrapFunctionHandler', () => {
54
53
  services: {},
55
54
  },
56
55
  }),
57
- ).rejects.toThrow(FunctionError);
56
+ ).rejects.toThrow(InvalidOperationInputError);
58
57
  });
59
58
  });