@dxos/functions 0.8.4-main.422d1c7879 → 0.8.4-main.4f23b4e393
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/dist/lib/neutral/index.mjs +256 -883
- 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 -4
- 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 +1 -2
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +0 -3
- 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 -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 +15 -20
- 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 +200 -45
- package/src/sdk.ts +5 -4
- package/src/services/credentials.ts +76 -120
- package/src/services/function-invocation-service.ts +1 -3
- package/src/services/index.ts +0 -3
- package/src/services/queues.ts +1 -87
- package/src/types/index.ts +0 -3
- package/src/types/url.ts +1 -1
- package/dist/lib/neutral/Trace.mjs +0 -42
- package/dist/lib/neutral/Trace.mjs.map +0 -7
- package/dist/lib/neutral/chunk-27Y24OTY.mjs +0 -167
- package/dist/lib/neutral/chunk-27Y24OTY.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 -174
- 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 -246
- 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 -77
- 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 -150
- 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 -225
- 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 -487
- package/src/process/ServiceResolver.ts +0 -174
- package/src/process/StorageService.ts +0 -99
- package/src/services/event-logger.ts +0 -111
- package/src/types/Script.ts +0 -38
- package/src/types/Trigger.ts +0 -207
- 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.4f23b4e393",
|
|
4
4
|
"description": "Functions API.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -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/
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/invariant": "0.8.4-main.
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/keys": "0.8.4-main.
|
|
46
|
-
"@dxos/log": "0.8.4-main.
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/types": "0.8.4-main.
|
|
50
|
-
"@dxos/node-std": "0.8.4-main.
|
|
51
|
-
"@dxos/
|
|
33
|
+
"@dxos/ai": "0.8.4-main.4f23b4e393",
|
|
34
|
+
"@dxos/echo-db": "0.8.4-main.4f23b4e393",
|
|
35
|
+
"@dxos/compute": "0.8.4-main.4f23b4e393",
|
|
36
|
+
"@dxos/effect": "0.8.4-main.4f23b4e393",
|
|
37
|
+
"@dxos/echo": "0.8.4-main.4f23b4e393",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.4f23b4e393",
|
|
39
|
+
"@dxos/errors": "0.8.4-main.4f23b4e393",
|
|
40
|
+
"@dxos/keys": "0.8.4-main.4f23b4e393",
|
|
41
|
+
"@dxos/log": "0.8.4-main.4f23b4e393",
|
|
42
|
+
"@dxos/protocols": "0.8.4-main.4f23b4e393",
|
|
43
|
+
"@dxos/schema": "0.8.4-main.4f23b4e393",
|
|
44
|
+
"@dxos/types": "0.8.4-main.4f23b4e393",
|
|
45
|
+
"@dxos/node-std": "0.8.4-main.4f23b4e393",
|
|
46
|
+
"@dxos/context": "0.8.4-main.4f23b4e393"
|
|
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 =
|
|
@@ -91,7 +122,14 @@ export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Defini
|
|
|
91
122
|
}
|
|
92
123
|
|
|
93
124
|
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
94
|
-
|
|
125
|
+
try {
|
|
126
|
+
Schema.validateSync(func.output, { onExcessProperty: 'error' })(result);
|
|
127
|
+
} catch (error: any) {
|
|
128
|
+
throw new InvalidOperationOutputError({
|
|
129
|
+
message: `Operation output did not match schema (${func.meta.key}): ${error.message}`,
|
|
130
|
+
cause: error,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
95
133
|
}
|
|
96
134
|
|
|
97
135
|
return result;
|
|
@@ -111,10 +149,12 @@ class FunctionContext extends Resource {
|
|
|
111
149
|
readonly client: EchoClient | undefined;
|
|
112
150
|
db: EchoDatabaseImpl | undefined;
|
|
113
151
|
queues: QueueFactory | undefined;
|
|
152
|
+
readonly opts: FunctionWrappingOptions;
|
|
114
153
|
|
|
115
|
-
constructor(context: FunctionProtocol.Context) {
|
|
154
|
+
constructor(context: FunctionProtocol.Context, opts: FunctionWrappingOptions) {
|
|
116
155
|
super();
|
|
117
156
|
this.context = context;
|
|
157
|
+
this.opts = opts;
|
|
118
158
|
if (context.services.dataService && context.services.queryService) {
|
|
119
159
|
this.client = new EchoClient().connectToService({
|
|
120
160
|
dataService: context.services.dataService,
|
|
@@ -154,50 +194,165 @@ class FunctionContext extends Resource {
|
|
|
154
194
|
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
155
195
|
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
156
196
|
const credentials = dbLayer
|
|
157
|
-
?
|
|
158
|
-
:
|
|
159
|
-
const functionInvocationService = MockedFunctionInvocationService;
|
|
160
|
-
const operationServiceLayer = MockedOperationServiceLayer;
|
|
197
|
+
? credentialsLayerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
198
|
+
: configuredCredentialsLayer([]);
|
|
161
199
|
|
|
162
200
|
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
|
-
)
|
|
201
|
+
? InternalAiServiceLayer(this.context.services.functionsAiService)
|
|
175
202
|
: AiService.notAvailable;
|
|
176
203
|
|
|
204
|
+
const operationServiceLayer = this.context.services.functionsService
|
|
205
|
+
? makeOperationServiceLayer(this.context.services.functionsService)
|
|
206
|
+
: unavailableOperationServiceLayer;
|
|
207
|
+
|
|
208
|
+
const operationRegistryLayer = this.context.services.functionsService
|
|
209
|
+
? makeOperationRegistryLayer(this.context.services.functionsService, this.context.spaceId as SpaceId | undefined)
|
|
210
|
+
: emptyOperationRegistryLayer;
|
|
211
|
+
|
|
212
|
+
const traceWriterLayer = this.context.services.traceService
|
|
213
|
+
? makeTraceWriterLayer(this.context.services.traceService)
|
|
214
|
+
: Trace.writerLayerNoop;
|
|
215
|
+
|
|
216
|
+
log('Creating function context layer', {
|
|
217
|
+
traceService: !!this.context.services.traceService,
|
|
218
|
+
functionsService: !!this.context.services.functionsService,
|
|
219
|
+
functionsAiService: !!this.context.services.functionsAiService,
|
|
220
|
+
spaceId: this.context.spaceId,
|
|
221
|
+
spaceRootUrl: this.context.spaceRootUrl,
|
|
222
|
+
toolkits: this.opts.toolkits?.length ?? 0,
|
|
223
|
+
types: this.opts.types?.length ?? 0,
|
|
224
|
+
});
|
|
225
|
+
|
|
177
226
|
return Layer.mergeAll(
|
|
178
227
|
dbLayer,
|
|
179
228
|
queuesLayer,
|
|
180
229
|
feedLayer,
|
|
181
230
|
credentials,
|
|
182
|
-
functionInvocationService,
|
|
183
231
|
operationServiceLayer,
|
|
232
|
+
operationRegistryLayer,
|
|
184
233
|
aiLayer,
|
|
185
|
-
|
|
186
|
-
|
|
234
|
+
OpaqueToolkit.providerLayer(OpaqueToolkit.merge(...(this.opts.toolkits ?? []))),
|
|
235
|
+
traceWriterLayer,
|
|
236
|
+
|
|
237
|
+
// `FunctionInvocationService` is deprecated; new code should yield `Operation.Service`.
|
|
238
|
+
// The cloudflare wrapper provides only the unavailable layer to satisfy the (still-present)
|
|
239
|
+
// type union — handlers that yield it will die at invocation time.
|
|
240
|
+
FunctionInvocationService.layerNotAvailable,
|
|
187
241
|
);
|
|
188
242
|
}
|
|
189
243
|
}
|
|
190
244
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
245
|
+
/**
|
|
246
|
+
* Backs `Trace.TraceService` with the EDGE-provided `TraceService` so that operation
|
|
247
|
+
* handlers can write trace events that are forwarded to the runtime's trace sink.
|
|
248
|
+
*/
|
|
249
|
+
const makeTraceWriterLayer = (traceService: TraceProtocol.TraceService): Layer.Layer<Trace.TraceService> =>
|
|
250
|
+
Layer.succeed(Trace.TraceService, {
|
|
251
|
+
write: (eventType, payload) => {
|
|
252
|
+
log('Writing trace event', {
|
|
253
|
+
eventType: eventType.key,
|
|
254
|
+
});
|
|
255
|
+
traceService.write([
|
|
256
|
+
{
|
|
257
|
+
key: eventType.key,
|
|
258
|
+
isEphemeral: eventType.isEphemeral,
|
|
259
|
+
data: payload,
|
|
260
|
+
},
|
|
261
|
+
]);
|
|
262
|
+
},
|
|
263
|
+
});
|
|
195
264
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
265
|
+
/**
|
|
266
|
+
* AI service layer that proxies HTTP requests through the EDGE-provided `FunctionsAiService`.
|
|
267
|
+
*/
|
|
268
|
+
const InternalAiServiceLayer = (functionsAiService: EdgeFunctionEnv.FunctionsAiService) =>
|
|
269
|
+
AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
270
|
+
Layer.provide(
|
|
271
|
+
AnthropicResolver.make().pipe(
|
|
272
|
+
Layer.provide(
|
|
273
|
+
AnthropicClient.layer({
|
|
274
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
275
|
+
apiUrl: 'http://internal/provider/anthropic',
|
|
276
|
+
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(functionsAiService))),
|
|
277
|
+
),
|
|
278
|
+
),
|
|
279
|
+
),
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Backs `Operation.Service` with the EDGE-provided `FunctionsService` so that operation
|
|
284
|
+
* handlers can invoke other deployed operations remotely. The `deployedId` on the operation
|
|
285
|
+
* definition is used as the routing key.
|
|
286
|
+
*/
|
|
287
|
+
const makeOperationServiceLayer = (
|
|
288
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
289
|
+
): Layer.Layer<Operation.Service> => {
|
|
290
|
+
const invokeRemote = async (
|
|
291
|
+
op: Operation.Definition.Any,
|
|
292
|
+
input: unknown,
|
|
293
|
+
options?: Operation.InvokeOptions,
|
|
294
|
+
): Promise<{ data?: unknown; error?: Error }> => {
|
|
295
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot invoke remotely.`);
|
|
296
|
+
const result = await functionsService.invoke(op.meta.deployedId, input, {
|
|
297
|
+
spaceId: options?.spaceId,
|
|
298
|
+
});
|
|
299
|
+
if (result._kind === 'success') {
|
|
300
|
+
return { data: result.data };
|
|
301
|
+
}
|
|
302
|
+
return { error: ErrorCodec.decode(result.error) };
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
return Layer.succeed(Operation.Service, {
|
|
306
|
+
invoke: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
307
|
+
Effect.tryPromise(() => invokeRemote(op, input, options)).pipe(
|
|
308
|
+
Effect.orDie,
|
|
309
|
+
Effect.flatMap((outcome) =>
|
|
310
|
+
outcome.error ? Effect.die(outcome.error) : Effect.succeed(outcome.data as never),
|
|
311
|
+
),
|
|
312
|
+
)) as Operation.OperationService['invoke'],
|
|
313
|
+
schedule: ((op: Operation.Definition.Any, input: unknown) =>
|
|
314
|
+
Effect.sync(() => {
|
|
315
|
+
invariant(op.meta.deployedId, `Operation '${op.meta.key}' has no deployedId; cannot schedule remotely.`);
|
|
316
|
+
// Fire and forget — schedule is intentionally non-awaiting.
|
|
317
|
+
void functionsService.invoke(op.meta.deployedId, input).catch(() => {
|
|
318
|
+
// Swallow errors — schedule is observability-only.
|
|
319
|
+
});
|
|
320
|
+
})) as Operation.OperationService['schedule'],
|
|
321
|
+
invokePromise: ((op: Operation.Definition.Any, input: unknown, options?: Operation.InvokeOptions) =>
|
|
322
|
+
invokeRemote(op, input, options).catch((error: unknown) => ({
|
|
323
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
324
|
+
}))) as Operation.OperationService['invokePromise'],
|
|
325
|
+
} satisfies Operation.OperationService);
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const unavailableOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
329
|
+
invoke: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
330
|
+
schedule: () => Effect.die('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
331
|
+
invokePromise: async () => ({
|
|
332
|
+
error: new Error('Operation.Service is not available: missing functionsService in EDGE context.'),
|
|
333
|
+
}),
|
|
334
|
+
} as Operation.OperationService);
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Backs `OperationRegistry.Service` with the EDGE-provided `FunctionsService.query`. Returns
|
|
338
|
+
* the first persistent operation matching the requested key, or `Option.none()` when not found.
|
|
339
|
+
*/
|
|
340
|
+
const makeOperationRegistryLayer = (
|
|
341
|
+
functionsService: EdgeFunctionEnv.FunctionsService,
|
|
342
|
+
spaceId: SpaceId | undefined,
|
|
343
|
+
): Layer.Layer<OperationRegistry.Service> =>
|
|
344
|
+
Layer.succeed(OperationRegistry.Service, {
|
|
345
|
+
resolve: (key: string) =>
|
|
346
|
+
Effect.gen(function* () {
|
|
347
|
+
const records = yield* Effect.tryPromise(() => functionsService.query({ spaceId })).pipe(Effect.orDie);
|
|
348
|
+
const match = (records as Operation.PersistentOperation[]).find((record) => record.key === key);
|
|
349
|
+
return match ? Option.some(Operation.deserialize(match)) : Option.none();
|
|
350
|
+
}),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const emptyOperationRegistryLayer = Layer.succeed(OperationRegistry.Service, {
|
|
354
|
+
resolve: () => Effect.succeed(Option.none()),
|
|
355
|
+
});
|
|
201
356
|
|
|
202
357
|
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|
|
203
358
|
if (value == null) {
|
package/src/sdk.ts
CHANGED
|
@@ -3,11 +3,10 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { type AiService } from '@dxos/ai';
|
|
6
|
+
import { type Credential, type Operation, type Trace } from '@dxos/compute';
|
|
6
7
|
import { type Database, type Feed } from '@dxos/echo';
|
|
7
|
-
import { type Trace } from '@dxos/functions';
|
|
8
|
-
import type { Operation } from '@dxos/operation';
|
|
9
8
|
|
|
10
|
-
import { type
|
|
9
|
+
import { type FunctionInvocationService, type QueueService } from './services';
|
|
11
10
|
|
|
12
11
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
13
12
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
@@ -20,11 +19,13 @@ import { type CredentialsService, type FunctionInvocationService, type QueueServ
|
|
|
20
19
|
*/
|
|
21
20
|
export type FunctionServices =
|
|
22
21
|
| AiService.AiService
|
|
23
|
-
| CredentialsService
|
|
22
|
+
| Credential.CredentialsService
|
|
24
23
|
| Database.Service
|
|
25
24
|
// TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
|
|
26
25
|
| QueueService
|
|
27
26
|
| Feed.FeedService
|
|
28
27
|
| Trace.TraceService
|
|
28
|
+
// TODO(dmaretskyi): `FunctionInvocationService` is being phased out in favour of `Operation.Service`;
|
|
29
|
+
// it's kept in the union until `functions-runtime/local-function-execution.ts` migrates.
|
|
29
30
|
| FunctionInvocationService
|
|
30
31
|
| Operation.Service;
|