@dxos/functions 0.8.4-main.74a063c4e0 → 0.8.4-main.765dc60934
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.
- package/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/neutral/index.mjs +261 -930
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/types/src/index.d.ts +0 -6
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -1
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts +2 -0
- package/dist/types/src/protocol/functions-ai-http-client.test.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +14 -2
- package/dist/types/src/protocol/protocol.d.ts.map +1 -1
- package/dist/types/src/sdk.d.ts +3 -7
- package/dist/types/src/sdk.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +15 -38
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +3 -5
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +1 -4
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +1 -49
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +1 -107
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +0 -3
- package/dist/types/src/types/index.d.ts.map +1 -1
- package/dist/types/src/types/url.d.ts +1 -1
- package/dist/types/src/types/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +16 -21
- package/src/index.ts +0 -6
- package/src/protocol/functions-ai-http-client.test.ts +105 -0
- package/src/protocol/functions-ai-http-client.ts +75 -1
- package/src/protocol/protocol.test.ts +10 -10
- package/src/protocol/protocol.ts +213 -40
- package/src/sdk.ts +7 -15
- package/src/services/credentials.ts +76 -120
- package/src/services/function-invocation-service.ts +3 -9
- package/src/services/index.ts +1 -4
- package/src/services/queues.ts +1 -87
- package/src/services/tracing.ts +0 -192
- package/src/types/index.ts +0 -3
- package/src/types/url.ts +1 -1
- package/dist/lib/neutral/Trace.mjs +0 -38
- package/dist/lib/neutral/Trace.mjs.map +0 -7
- package/dist/lib/neutral/chunk-5T7GOX2V.mjs +0 -149
- package/dist/lib/neutral/chunk-5T7GOX2V.mjs.map +0 -7
- package/dist/lib/neutral/chunk-IVDUS56O.mjs +0 -49
- package/dist/lib/neutral/chunk-IVDUS56O.mjs.map +0 -7
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +0 -10
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +0 -7
- package/dist/lib/neutral/fib-N45KAC7C.mjs +0 -23
- package/dist/lib/neutral/fib-N45KAC7C.mjs.map +0 -7
- package/dist/lib/neutral/reply-EUEPKNJF.mjs +0 -19
- package/dist/lib/neutral/reply-EUEPKNJF.mjs.map +0 -7
- package/dist/lib/neutral/sleep-PUK3D4FF.mjs +0 -15
- package/dist/lib/neutral/sleep-PUK3D4FF.mjs.map +0 -7
- package/dist/types/src/Trace.d.ts +0 -155
- package/dist/types/src/Trace.d.ts.map +0 -1
- package/dist/types/src/errors.d.ts +0 -121
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/example/definitions.d.ts +0 -11
- package/dist/types/src/example/definitions.d.ts.map +0 -1
- package/dist/types/src/example/fib.d.ts +0 -8
- package/dist/types/src/example/fib.d.ts.map +0 -1
- package/dist/types/src/example/forex-effect.d.ts +0 -3
- package/dist/types/src/example/forex-effect.d.ts.map +0 -1
- package/dist/types/src/example/index.d.ts +0 -4
- package/dist/types/src/example/index.d.ts.map +0 -1
- package/dist/types/src/example/reply.d.ts +0 -4
- package/dist/types/src/example/reply.d.ts.map +0 -1
- package/dist/types/src/example/sleep.d.ts +0 -6
- package/dist/types/src/example/sleep.d.ts.map +0 -1
- package/dist/types/src/process/Process.d.ts +0 -247
- package/dist/types/src/process/Process.d.ts.map +0 -1
- package/dist/types/src/process/ServiceResolver.d.ts +0 -74
- package/dist/types/src/process/ServiceResolver.d.ts.map +0 -1
- package/dist/types/src/process/StorageService.d.ts +0 -58
- package/dist/types/src/process/StorageService.d.ts.map +0 -1
- package/dist/types/src/services/event-logger.d.ts +0 -81
- package/dist/types/src/services/event-logger.d.ts.map +0 -1
- package/dist/types/src/types/Script.d.ts +0 -22
- package/dist/types/src/types/Script.d.ts.map +0 -1
- package/dist/types/src/types/Trigger.d.ts +0 -120
- package/dist/types/src/types/Trigger.d.ts.map +0 -1
- package/dist/types/src/types/TriggerEvent.d.ts +0 -75
- package/dist/types/src/types/TriggerEvent.d.ts.map +0 -1
- package/src/Trace.ts +0 -202
- package/src/errors.ts +0 -21
- package/src/example/definitions.ts +0 -49
- package/src/example/fib.ts +0 -23
- package/src/example/forex-effect.ts +0 -40
- package/src/example/index.ts +0 -12
- package/src/example/reply.ts +0 -19
- package/src/example/sleep.ts +0 -17
- package/src/process/Process.ts +0 -486
- package/src/process/ServiceResolver.ts +0 -174
- package/src/process/StorageService.ts +0 -99
- package/src/services/event-logger.ts +0 -127
- package/src/types/Script.ts +0 -38
- package/src/types/Trigger.ts +0 -154
- 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.
|
|
3
|
+
"version": "0.8.4-main.765dc60934",
|
|
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": "
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
12
12
|
"author": "info@dxos.org",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"type": "module",
|
|
@@ -17,11 +17,6 @@
|
|
|
17
17
|
"source": "./src/index.ts",
|
|
18
18
|
"types": "./dist/types/src/index.d.ts",
|
|
19
19
|
"default": "./dist/lib/neutral/index.mjs"
|
|
20
|
-
},
|
|
21
|
-
"./Trace": {
|
|
22
|
-
"source": "./src/Trace.ts",
|
|
23
|
-
"types": "./dist/types/src/Trace.d.ts",
|
|
24
|
-
"default": "./dist/lib/neutral/Trace.mjs"
|
|
25
20
|
}
|
|
26
21
|
},
|
|
27
22
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -35,20 +30,20 @@
|
|
|
35
30
|
"@effect/ai-anthropic": "0.23.0",
|
|
36
31
|
"@effect/platform": "0.94.4",
|
|
37
32
|
"effect": "3.20.0",
|
|
38
|
-
"@dxos/ai": "0.8.4-main.
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/
|
|
41
|
-
"@dxos/echo
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/types": "0.8.4-main.
|
|
33
|
+
"@dxos/ai": "0.8.4-main.765dc60934",
|
|
34
|
+
"@dxos/compute": "0.8.4-main.765dc60934",
|
|
35
|
+
"@dxos/context": "0.8.4-main.765dc60934",
|
|
36
|
+
"@dxos/echo": "0.8.4-main.765dc60934",
|
|
37
|
+
"@dxos/effect": "0.8.4-main.765dc60934",
|
|
38
|
+
"@dxos/echo-db": "0.8.4-main.765dc60934",
|
|
39
|
+
"@dxos/invariant": "0.8.4-main.765dc60934",
|
|
40
|
+
"@dxos/keys": "0.8.4-main.765dc60934",
|
|
41
|
+
"@dxos/log": "0.8.4-main.765dc60934",
|
|
42
|
+
"@dxos/node-std": "0.8.4-main.765dc60934",
|
|
43
|
+
"@dxos/protocols": "0.8.4-main.765dc60934",
|
|
44
|
+
"@dxos/schema": "0.8.4-main.765dc60934",
|
|
45
|
+
"@dxos/errors": "0.8.4-main.765dc60934",
|
|
46
|
+
"@dxos/types": "0.8.4-main.765dc60934"
|
|
52
47
|
},
|
|
53
48
|
"publishConfig": {
|
|
54
49
|
"access": "public"
|
package/src/index.ts
CHANGED
|
@@ -2,13 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './errors';
|
|
6
|
-
export * from './example';
|
|
7
|
-
export * as Process from './process/Process';
|
|
8
|
-
export * as ServiceResolver from './process/ServiceResolver';
|
|
9
|
-
export * from './process/StorageService';
|
|
10
5
|
export * from './sdk';
|
|
11
6
|
export * from './services';
|
|
12
7
|
export * from './types';
|
|
13
8
|
export * from './protocol';
|
|
14
|
-
export * as Trace from './Trace';
|
|
@@ -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(
|
|
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,16 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
import { describe, test } from 'vitest';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
|
|
7
|
+
import { InvalidOperationInputError } from '@dxos/compute';
|
|
8
|
+
import { FibonacciHandler, ReplyHandler } from '@dxos/compute/testing';
|
|
9
|
+
|
|
10
10
|
import { wrapFunctionHandler } from './protocol';
|
|
11
11
|
|
|
12
12
|
describe('wrapFunctionHandler', () => {
|
|
13
13
|
test('wraps reply function and executes handler', async ({ expect }) => {
|
|
14
|
-
const wrapped = wrapFunctionHandler(
|
|
14
|
+
const wrapped = wrapFunctionHandler(ReplyHandler);
|
|
15
15
|
|
|
16
|
-
expect(wrapped.meta.key).toBe('example.
|
|
16
|
+
expect(wrapped.meta.key).toBe('org.example.function.reply');
|
|
17
17
|
expect(wrapped.meta.name).toBe('Reply');
|
|
18
18
|
|
|
19
19
|
const testData = { message: 'hello' };
|
|
@@ -28,9 +28,9 @@ describe('wrapFunctionHandler', () => {
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
test('wraps fibonacci function with valid input', async ({ expect }) => {
|
|
31
|
-
const wrapped = wrapFunctionHandler(
|
|
31
|
+
const wrapped = wrapFunctionHandler(FibonacciHandler);
|
|
32
32
|
|
|
33
|
-
expect(wrapped.meta.key).toBe('example.
|
|
33
|
+
expect(wrapped.meta.key).toBe('org.example.function.fib');
|
|
34
34
|
expect(wrapped.meta.name).toBe('Fibonacci');
|
|
35
35
|
|
|
36
36
|
const result = await wrapped.handler({
|
|
@@ -43,8 +43,8 @@ describe('wrapFunctionHandler', () => {
|
|
|
43
43
|
expect(result).toEqual({ result: '55' });
|
|
44
44
|
});
|
|
45
45
|
|
|
46
|
-
test('throws
|
|
47
|
-
const wrapped = wrapFunctionHandler(
|
|
46
|
+
test('throws InvalidOperationInputError on invalid input schema for fibonacci', async ({ expect }) => {
|
|
47
|
+
const wrapped = wrapFunctionHandler(FibonacciHandler);
|
|
48
48
|
|
|
49
49
|
await expect(
|
|
50
50
|
wrapped.handler({
|
|
@@ -53,6 +53,6 @@ describe('wrapFunctionHandler', () => {
|
|
|
53
53
|
services: {},
|
|
54
54
|
},
|
|
55
55
|
}),
|
|
56
|
-
).rejects.toThrow(
|
|
56
|
+
).rejects.toThrow(InvalidOperationInputError);
|
|
57
57
|
});
|
|
58
58
|
});
|
package/src/protocol/protocol.ts
CHANGED
|
@@ -5,31 +5,58 @@
|
|
|
5
5
|
import * as AnthropicClient from '@effect/ai-anthropic/AnthropicClient';
|
|
6
6
|
import * as Effect from 'effect/Effect';
|
|
7
7
|
import * as Layer from 'effect/Layer';
|
|
8
|
+
import * as Option from 'effect/Option';
|
|
8
9
|
import * as Schema from 'effect/Schema';
|
|
9
10
|
import * as SchemaAST from 'effect/SchemaAST';
|
|
10
11
|
|
|
11
|
-
import { AiModelResolver, AiService } from '@dxos/ai';
|
|
12
|
+
import { AiModelResolver, AiService, OpaqueToolkit } from '@dxos/ai';
|
|
12
13
|
import { AnthropicResolver } from '@dxos/ai/resolvers';
|
|
14
|
+
import {
|
|
15
|
+
FunctionError,
|
|
16
|
+
InvalidOperationInputError,
|
|
17
|
+
InvalidOperationOutputError,
|
|
18
|
+
Operation,
|
|
19
|
+
OperationRegistry,
|
|
20
|
+
Trace,
|
|
21
|
+
} from '@dxos/compute';
|
|
13
22
|
import { LifecycleState, Resource } from '@dxos/context';
|
|
14
23
|
import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
|
|
15
|
-
import { EchoClient, type EchoDatabaseImpl, type QueueFactory
|
|
24
|
+
import { createFeedServiceLayer, EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
|
|
16
25
|
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
17
26
|
import { runAndForwardErrors } from '@dxos/effect';
|
|
18
27
|
import { assertState, failedInvariant, invariant } from '@dxos/invariant';
|
|
19
|
-
import { PublicKey } from '@dxos/keys';
|
|
20
|
-
import {
|
|
21
|
-
import { type FunctionProtocol } from '@dxos/protocols';
|
|
28
|
+
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
29
|
+
import { log } from '@dxos/log';
|
|
30
|
+
import { EdgeFunctionEnv, ErrorCodec, type FunctionProtocol, type TraceProtocol } from '@dxos/protocols';
|
|
22
31
|
|
|
23
|
-
import { FunctionError } from '../errors';
|
|
24
32
|
import { type FunctionServices } from '../sdk';
|
|
25
|
-
import {
|
|
26
|
-
|
|
33
|
+
import {
|
|
34
|
+
configuredCredentialsLayer,
|
|
35
|
+
credentialsLayerFromDatabase,
|
|
36
|
+
FunctionInvocationService,
|
|
37
|
+
QueueService,
|
|
38
|
+
} from '../services';
|
|
27
39
|
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
28
40
|
|
|
41
|
+
export interface FunctionWrappingOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Additional types to register with the database.
|
|
44
|
+
*/
|
|
45
|
+
types?: Type.AnyEntity[];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Toolkits to make available via the `OpaqueToolkitProvider`.
|
|
49
|
+
*/
|
|
50
|
+
toolkits?: OpaqueToolkit.OpaqueToolkit[];
|
|
51
|
+
}
|
|
52
|
+
|
|
29
53
|
/**
|
|
30
54
|
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
31
55
|
*/
|
|
32
|
-
export const wrapFunctionHandler = (
|
|
56
|
+
export const wrapFunctionHandler = (
|
|
57
|
+
func: Operation.WithHandler<Operation.Definition.Any>,
|
|
58
|
+
opts: FunctionWrappingOptions = {},
|
|
59
|
+
): FunctionProtocol.Func => {
|
|
33
60
|
if (!Operation.isOperationWithHandler(func)) {
|
|
34
61
|
throw new TypeError('Expected operation with handler');
|
|
35
62
|
}
|
|
@@ -61,17 +88,21 @@ export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Defini
|
|
|
61
88
|
try {
|
|
62
89
|
if (!SchemaAST.isAnyKeyword(func.input.ast)) {
|
|
63
90
|
try {
|
|
64
|
-
Schema.validateSync(func.input)(data);
|
|
65
|
-
} catch (error) {
|
|
66
|
-
throw new
|
|
91
|
+
Schema.validateSync(func.input, { onExcessProperty: 'error' })(data);
|
|
92
|
+
} catch (error: any) {
|
|
93
|
+
throw new InvalidOperationInputError({
|
|
94
|
+
message: `Operation input did not match schema (${func.meta.key}): ${error.message}`,
|
|
95
|
+
cause: error,
|
|
96
|
+
});
|
|
67
97
|
}
|
|
68
98
|
}
|
|
69
99
|
|
|
70
|
-
await using funcContext = await new FunctionContext(context).open();
|
|
100
|
+
await using funcContext = await new FunctionContext(context, opts).open();
|
|
71
101
|
|
|
72
|
-
|
|
102
|
+
const types = [...(opts.types ?? []), ...(func.types ?? [])];
|
|
103
|
+
if (types.length > 0) {
|
|
73
104
|
invariant(funcContext.db, 'Database is required for functions with types');
|
|
74
|
-
await funcContext.db.graph.schemaRegistry.register(
|
|
105
|
+
await funcContext.db.graph.schemaRegistry.register(types as Type.AnyEntity[]);
|
|
75
106
|
}
|
|
76
107
|
|
|
77
108
|
const dataWithDecodedRefs =
|
|
@@ -90,8 +121,27 @@ export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Defini
|
|
|
90
121
|
);
|
|
91
122
|
}
|
|
92
123
|
|
|
124
|
+
// Flush in-memory ECHO writes before the function scope closes.
|
|
125
|
+
// Writes performed by `db.add` / `db.remove` are buffered in the in-memory
|
|
126
|
+
// `EchoDatabaseImpl` and only pushed across the `DataService` binding when
|
|
127
|
+
// `db.flush({ disk })` is called. `FunctionContext._close` (invoked by the
|
|
128
|
+
// `await using` above) calls `db.close()` but does NOT flush, so mutations
|
|
129
|
+
// performed by handlers that declare `Database.Service` (e.g. `object-create`,
|
|
130
|
+
// `object-update`, `relation-create`) would be silently dropped before reaching
|
|
131
|
+
// the edge `AutomergeReplicator`. Flushing here closes that hole.
|
|
132
|
+
if (serviceTags.includes(Database.Service.key) && funcContext.db) {
|
|
133
|
+
await funcContext.db.flush({ disk: true, indexes: false });
|
|
134
|
+
}
|
|
135
|
+
|
|
93
136
|
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
94
|
-
|
|
137
|
+
try {
|
|
138
|
+
Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
|
|
139
|
+
} catch (error: any) {
|
|
140
|
+
throw new InvalidOperationOutputError({
|
|
141
|
+
message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
|
|
142
|
+
cause: error,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
95
145
|
}
|
|
96
146
|
|
|
97
147
|
return result;
|
|
@@ -111,10 +161,12 @@ class FunctionContext extends Resource {
|
|
|
111
161
|
readonly client: EchoClient | undefined;
|
|
112
162
|
db: EchoDatabaseImpl | undefined;
|
|
113
163
|
queues: QueueFactory | undefined;
|
|
164
|
+
readonly opts: FunctionWrappingOptions;
|
|
114
165
|
|
|
115
|
-
constructor(context: FunctionProtocol.Context) {
|
|
166
|
+
constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
|
|
116
167
|
super();
|
|
117
168
|
this.context = context;
|
|
169
|
+
this.opts = opts;
|
|
118
170
|
if (context.services.dataService && context.services.queryService) {
|
|
119
171
|
this.client = new EchoClient().connectToService({
|
|
120
172
|
dataService: context.services.dataService,
|
|
@@ -154,43 +206,164 @@ class FunctionContext extends Resource {
|
|
|
154
206
|
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
155
207
|
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
156
208
|
const credentials = dbLayer
|
|
157
|
-
?
|
|
158
|
-
:
|
|
159
|
-
const functionInvocationService = MockedFunctionInvocationService;
|
|
160
|
-
const tracing = TracingService.layerNoop;
|
|
209
|
+
? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
210
|
+
: configuredCredentialsLayer([]);
|
|
161
211
|
|
|
162
212
|
const aiLayer = this.context.services.functionsAiService
|
|
163
|
-
?
|
|
164
|
-
Layer.provide(
|
|
165
|
-
AnthropicResolver.make().pipe(
|
|
166
|
-
Layer.provide(
|
|
167
|
-
AnthropicClient.layer({
|
|
168
|
-
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
169
|
-
apiUrl: 'http://internal/provider/anthropic',
|
|
170
|
-
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
|
|
171
|
-
),
|
|
172
|
-
),
|
|
173
|
-
),
|
|
174
|
-
)
|
|
213
|
+
? InternalAiServiceLayer(this.context.services.functionsAiService)
|
|
175
214
|
: AiService.notAvailable;
|
|
176
215
|
|
|
216
|
+
const operationServiceLayer = this.context.services.functionsService
|
|
217
|
+
? makeOperationServiceLayer(this.context.services.functionsService)
|
|
218
|
+
: unavailableOperationServiceLayer;
|
|
219
|
+
|
|
220
|
+
const operationRegistryLayer = this.context.services.functionsService
|
|
221
|
+
? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
|
|
222
|
+
: emptyOperationRegistryLayer;
|
|
223
|
+
|
|
224
|
+
const traceWriterLayer = this.context.services.traceService
|
|
225
|
+
? makeTraceWriterLayer(this.context.services.traceService)
|
|
226
|
+
: Trace.writerLayerNoop;
|
|
227
|
+
|
|
228
|
+
log('Creating function context layer', {
|
|
229
|
+
traceService: !!this.context.services.traceService,
|
|
230
|
+
functionsService: !!this.context.services.functionsService,
|
|
231
|
+
functionsAiService: !!this.context.services.functionsAiService,
|
|
232
|
+
spaceId: this.context.spaceId,
|
|
233
|
+
spaceRootUrl: this.context.spaceRootUrl,
|
|
234
|
+
toolkits: this.opts.toolkits?.length ?? 0,
|
|
235
|
+
types: this.opts.types?.length ?? 0,
|
|
236
|
+
});
|
|
237
|
+
|
|
177
238
|
return Layer.mergeAll(
|
|
178
239
|
dbLayer,
|
|
179
240
|
queuesLayer,
|
|
180
241
|
feedLayer,
|
|
181
242
|
credentials,
|
|
182
|
-
|
|
243
|
+
operationServiceLayer,
|
|
244
|
+
operationRegistryLayer,
|
|
183
245
|
aiLayer,
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
246
|
+
OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
|
|
247
|
+
traceWriterLayer,
|
|
248
|
+
|
|
249
|
+
// `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
|
|
250
|
+
// The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
|
|
251
|
+
// type union — handlers that yield it will die at invocation time.
|
|
252
|
+
FunctionInvocationService.layerNotAvailable,
|
|
187
253
|
);
|
|
188
254
|
}
|
|
189
255
|
}
|
|
190
256
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
257
|
+
/**
|
|
258
|
+
* Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
|
|
259
|
+
* handlers can write trace events that are forwarded to the runtime's trace sink.
|
|
260
|
+
*/
|
|
261
|
+
const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
|
|
262
|
+
Layer.succeed(Trace.TraceService, {
|
|
263
|
+
write: (eventType, payload) => {
|
|
264
|
+
log('Writing trace event', {
|
|
265
|
+
eventType: eventType.key,
|
|
266
|
+
});
|
|
267
|
+
traceService.write([
|
|
268
|
+
{
|
|
269
|
+
key: eventType.key,
|
|
270
|
+
isEphemeral: eventType.isEphemeral,
|
|
271
|
+
data: payload,
|
|
272
|
+
},
|
|
273
|
+
]);
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
|
|
279
|
+
*/
|
|
280
|
+
const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
|
|
281
|
+
AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
282
|
+
Layer.provide(
|
|
283
|
+
AnthropicResolver.make().pipe(
|
|
284
|
+
Layer.provide(
|
|
285
|
+
AnthropicClient.layer({
|
|
286
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
287
|
+
apiUrl: 'http://internal/provider/anthropic',
|
|
288
|
+
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
|
|
289
|
+
),
|
|
290
|
+
),
|
|
291
|
+
),
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
|
|
296
|
+
* handlers can invoke other deployed operations remotely. The `deployedId` on the operation
|
|
297
|
+
* definition is used as the routing key.
|
|
298
|
+
*/
|
|
299
|
+
const makeOperationServiceLayer = (
|
|
300
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
301
|
+
): Layer.Layer<Operation.Service> => {
|
|
302
|
+
const invokeRemote = async (
|
|
303
|
+
op: Operation.Definition.Any,
|
|
304
|
+
input: unknown,
|
|
305
|
+
options?: Operation.InvokeOptions,
|
|
306
|
+
): Promise<{ data?: unknown; error?: Error }> => {
|
|
307
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
|
|
308
|
+
const result = await functionsService.invoke(op.meta.deployedId, input, {
|
|
309
|
+
spaceId: options?.spaceId,
|
|
310
|
+
});
|
|
311
|
+
if (result._kind === 'success') {
|
|
312
|
+
return { data: result.data };
|
|
313
|
+
}
|
|
314
|
+
return { error: ErrorCodec.decode(result.error) };
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
return Layer.succeed(Operation.Service, {
|
|
318
|
+
invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
319
|
+
Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
|
|
320
|
+
Effect.orDie,
|
|
321
|
+
Effect.flatMap((outcome) =>
|
|
322
|
+
outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
|
|
323
|
+
),
|
|
324
|
+
)) as Operation.OperationService['invoke'],
|
|
325
|
+
schedule: ((op: Operation.Definition.Any, input: unknown) =>
|
|
326
|
+
Effect.sync(() => {
|
|
327
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
|
|
328
|
+
// Fire and forget — schedule is intentionally non-awaiting.
|
|
329
|
+
void functionsService.invoke(op.meta.deployedId, input).catch(() => {
|
|
330
|
+
// Swallow errors — schedule is observability-only.
|
|
331
|
+
});
|
|
332
|
+
})) as Operation.OperationService['schedule'],
|
|
333
|
+
invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
334
|
+
invokeRemote(op, input, options).catch((error: unknown) => ({
|
|
335
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
336
|
+
}))) as Operation.OperationService['invokePromise'],
|
|
337
|
+
} satisfies Operation.OperationService);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
341
|
+
invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
342
|
+
schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
343
|
+
invokePromise: async () => ({
|
|
344
|
+
error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
345
|
+
}),
|
|
346
|
+
} as Operation.OperationService);
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
|
|
350
|
+
* the first persistent operation matching the requested key, or `Option.none()` when not found.
|
|
351
|
+
*/
|
|
352
|
+
const makeOperationRegistryLayer = (
|
|
353
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
354
|
+
spaceId: SpaceId | undefined,
|
|
355
|
+
): Layer.Layer<OperationRegistry.Service> =>
|
|
356
|
+
Layer.succeed(OperationRegistry.Service, {
|
|
357
|
+
resolve: (key: string) =>
|
|
358
|
+
Effect.gen(function* () {
|
|
359
|
+
const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
|
|
360
|
+
const match = (records as Operation.PersistentOperation[]).find((record) => Operation.getKey(record) === key);
|
|
361
|
+
return match ? Option.some(Operation.deserialize(match)) : Option.none();
|
|
362
|
+
}),
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
|
|
366
|
+
resolve: () => Effect.succeed(Option.none()),
|
|
194
367
|
});
|
|
195
368
|
|
|
196
369
|
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|