@dxos/functions 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5
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/README.md +4 -6
- package/dist/lib/neutral/Trace.mjs +42 -0
- package/dist/lib/neutral/Trace.mjs.map +7 -0
- package/dist/lib/neutral/chunk-27Y24OTY.mjs +167 -0
- package/dist/lib/neutral/chunk-27Y24OTY.mjs.map +7 -0
- package/dist/lib/neutral/chunk-IVDUS56O.mjs +49 -0
- package/dist/lib/neutral/chunk-IVDUS56O.mjs.map +7 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/fib-N45KAC7C.mjs +23 -0
- package/dist/lib/neutral/fib-N45KAC7C.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1218 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/reply-EUEPKNJF.mjs +19 -0
- package/dist/lib/neutral/reply-EUEPKNJF.mjs.map +7 -0
- package/dist/lib/neutral/sleep-PUK3D4FF.mjs +15 -0
- package/dist/lib/neutral/sleep-PUK3D4FF.mjs.map +7 -0
- package/dist/types/src/Trace.d.ts +174 -0
- package/dist/types/src/Trace.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +121 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/example/definitions.d.ts +11 -0
- package/dist/types/src/example/definitions.d.ts.map +1 -0
- package/dist/types/src/example/fib.d.ts +8 -0
- package/dist/types/src/example/fib.d.ts.map +1 -0
- package/dist/types/src/example/forex-effect.d.ts +3 -0
- package/dist/types/src/example/forex-effect.d.ts.map +1 -0
- package/dist/types/src/example/index.d.ts +4 -0
- package/dist/types/src/example/index.d.ts.map +1 -0
- package/dist/types/src/example/reply.d.ts +4 -0
- package/dist/types/src/example/reply.d.ts.map +1 -0
- package/dist/types/src/example/sleep.d.ts +6 -0
- package/dist/types/src/example/sleep.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +9 -6
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/process/Process.d.ts +246 -0
- package/dist/types/src/process/Process.d.ts.map +1 -0
- package/dist/types/src/process/ServiceResolver.d.ts +74 -0
- package/dist/types/src/process/ServiceResolver.d.ts.map +1 -0
- package/dist/types/src/process/StorageService.d.ts +58 -0
- package/dist/types/src/process/StorageService.d.ts.map +1 -0
- package/dist/types/src/protocol/functions-ai-http-client.d.ts +12 -0
- package/dist/types/src/protocol/functions-ai-http-client.d.ts.map +1 -0
- package/dist/types/src/protocol/index.d.ts +2 -0
- package/dist/types/src/protocol/index.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.d.ts +7 -0
- package/dist/types/src/protocol/protocol.d.ts.map +1 -0
- package/dist/types/src/protocol/protocol.test.d.ts +2 -0
- package/dist/types/src/protocol/protocol.test.d.ts.map +1 -0
- package/dist/types/src/sdk.d.ts +11 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +23 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +70 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +16 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +5 -7
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +37 -5
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +4 -14
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Script.d.ts +22 -0
- package/dist/types/src/types/Script.d.ts.map +1 -0
- package/dist/types/src/types/Trigger.d.ts +150 -0
- package/dist/types/src/types/Trigger.d.ts.map +1 -0
- package/dist/types/src/types/TriggerEvent.d.ts +75 -0
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +5 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/url.d.ts +13 -0
- package/dist/types/src/types/url.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +32 -67
- package/src/Trace.ts +225 -0
- package/src/errors.ts +21 -0
- package/src/example/definitions.ts +49 -0
- package/src/example/fib.ts +23 -0
- package/src/example/forex-effect.ts +40 -0
- package/src/example/index.ts +12 -0
- package/src/example/reply.ts +19 -0
- package/src/example/sleep.ts +17 -0
- package/src/index.ts +9 -8
- package/src/process/Process.ts +487 -0
- package/src/process/ServiceResolver.ts +174 -0
- package/src/process/StorageService.ts +99 -0
- package/src/protocol/functions-ai-http-client.ts +67 -0
- package/src/{executor → protocol}/index.ts +1 -1
- package/src/protocol/protocol.test.ts +58 -0
- package/src/protocol/protocol.ts +273 -0
- package/src/sdk.ts +30 -0
- package/src/services/credentials.ts +108 -4
- package/src/services/event-logger.ts +61 -37
- package/src/services/function-invocation-service.ts +35 -0
- package/src/services/index.ts +5 -7
- package/src/services/queues.ts +58 -9
- package/src/services/tracing.ts +4 -23
- package/src/types/Script.ts +38 -0
- package/src/types/Trigger.ts +207 -0
- package/src/types/TriggerEvent.ts +62 -0
- package/src/types/index.ts +8 -0
- package/src/types/url.ts +32 -0
- package/dist/lib/browser/bundler/index.mjs +0 -247
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-54U464M4.mjs +0 -360
- package/dist/lib/browser/chunk-54U464M4.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -69
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -492
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs +0 -79
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -249
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs +0 -362
- package/dist/lib/node-esm/chunk-XDSX35BS.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -71
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -493
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -80
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -50
- package/dist/types/src/bundler/bundler.d.ts.map +0 -1
- package/dist/types/src/bundler/bundler.test.d.ts +0 -2
- package/dist/types/src/bundler/bundler.test.d.ts.map +0 -1
- package/dist/types/src/bundler/index.d.ts +0 -2
- package/dist/types/src/bundler/index.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -16
- package/dist/types/src/edge/functions.d.ts.map +0 -1
- package/dist/types/src/edge/index.d.ts +0 -2
- package/dist/types/src/edge/index.d.ts.map +0 -1
- package/dist/types/src/executor/executor.d.ts +0 -8
- package/dist/types/src/executor/executor.d.ts.map +0 -1
- package/dist/types/src/executor/index.d.ts +0 -2
- package/dist/types/src/executor/index.d.ts.map +0 -1
- package/dist/types/src/handler.d.ts +0 -61
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/schema.d.ts +0 -38
- package/dist/types/src/schema.d.ts.map +0 -1
- package/dist/types/src/services/ai.d.ts +0 -12
- package/dist/types/src/services/ai.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -15
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/dist/types/src/services/service-container.d.ts +0 -46
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/tool-resolver.d.ts +0 -14
- package/dist/types/src/services/tool-resolver.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -5
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -71
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -124
- package/dist/types/src/trace.d.ts.map +0 -1
- package/dist/types/src/translations.d.ts +0 -12
- package/dist/types/src/translations.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -411
- package/dist/types/src/types.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -17
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -59
- package/src/bundler/bundler.ts +0 -292
- package/src/bundler/index.ts +0 -5
- package/src/edge/functions.ts +0 -64
- package/src/edge/index.ts +0 -9
- package/src/executor/executor.ts +0 -54
- package/src/handler.ts +0 -113
- package/src/schema.ts +0 -57
- package/src/services/ai.ts +0 -32
- package/src/services/database.ts +0 -50
- package/src/services/function-call-service.ts +0 -64
- package/src/services/service-container.ts +0 -127
- package/src/services/tool-resolver.ts +0 -31
- package/src/testing/index.ts +0 -5
- package/src/testing/logger.ts +0 -16
- package/src/testing/services.ts +0 -174
- package/src/trace.ts +0 -180
- package/src/translations.ts +0 -20
- package/src/types.ts +0 -211
- package/src/url.ts +0 -52
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Option from 'effect/Option';
|
|
8
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
9
|
+
import * as Schema from 'effect/Schema';
|
|
10
|
+
|
|
11
|
+
export interface Service {
|
|
12
|
+
/** Read a value by key. Returns `None` if key does not exist. */
|
|
13
|
+
get<S extends Schema.Schema<any, string, any>>(
|
|
14
|
+
schema: S,
|
|
15
|
+
key: string,
|
|
16
|
+
): Effect.Effect<Option.Option<Schema.Schema.Type<S>>, never, Schema.Schema.Context<S>>;
|
|
17
|
+
|
|
18
|
+
/** Write a value for the given key. */
|
|
19
|
+
set<S extends Schema.Schema<any, string, any>>(
|
|
20
|
+
schema: S,
|
|
21
|
+
key: string,
|
|
22
|
+
value: Schema.Schema.Type<S>,
|
|
23
|
+
): Effect.Effect<void, never, Schema.Schema.Context<S>>;
|
|
24
|
+
|
|
25
|
+
/** Remove a key. */
|
|
26
|
+
delete(key: string): Effect.Effect<void>;
|
|
27
|
+
|
|
28
|
+
/** List all keys, optionally filtered by prefix. */
|
|
29
|
+
list(prefix?: string): Effect.Effect<readonly string[]>;
|
|
30
|
+
|
|
31
|
+
/** Remove all keys managed by this scoped store. */
|
|
32
|
+
clear(): Effect.Effect<void>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Scoped key-value storage service for processes.
|
|
37
|
+
* Each process receives its own namespaced instance via the process manager.
|
|
38
|
+
* Construct a live implementation with `StorageService.layer` from `@dxos/functions-runtime`.
|
|
39
|
+
*/
|
|
40
|
+
export class StorageService extends Context.Tag('@dxos/functions/StorageService')<StorageService, Service>() {}
|
|
41
|
+
|
|
42
|
+
export const get = Effect.serviceFunctionEffect(StorageService, (_) => _.get);
|
|
43
|
+
export const set = Effect.serviceFunctionEffect(StorageService, (_) => _.set);
|
|
44
|
+
export const deleteKey = Effect.serviceFunctionEffect(StorageService, (_) => _.delete);
|
|
45
|
+
export const list = Effect.serviceFunctionEffect(StorageService, (_) => _.list);
|
|
46
|
+
export const clear = Effect.serviceFunctionEffect(StorageService, (_) => _.clear);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Typed key in a storage service.
|
|
50
|
+
*/
|
|
51
|
+
export interface Key<T> extends Pipeable.Pipeable {
|
|
52
|
+
readonly key: string;
|
|
53
|
+
|
|
54
|
+
get: Effect.Effect<Option.Option<T>, never, StorageService>;
|
|
55
|
+
set(value: T): Effect.Effect<void, never, StorageService>;
|
|
56
|
+
delete(): Effect.Effect<void, never, StorageService>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Create a typed key in a storage service.
|
|
61
|
+
*/
|
|
62
|
+
export const key = <S extends Schema.Schema<any, string, any>>(schema: S, key: string): Key<Schema.Schema.Type<S>> => {
|
|
63
|
+
return {
|
|
64
|
+
key,
|
|
65
|
+
get: get(schema, key),
|
|
66
|
+
set: (value: Schema.Schema.Type<S>) => set(schema, key, value),
|
|
67
|
+
delete: () => deleteKey(key),
|
|
68
|
+
pipe(...args: any) {
|
|
69
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Typed key in a storage service with a default value.
|
|
76
|
+
*/
|
|
77
|
+
export interface KeyWithDefault<T, U> extends Pipeable.Pipeable {
|
|
78
|
+
readonly key: string;
|
|
79
|
+
get: Effect.Effect<T | U, never, StorageService>;
|
|
80
|
+
set(value: U): Effect.Effect<void, never, StorageService>;
|
|
81
|
+
delete(): Effect.Effect<void, never, StorageService>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Assign a default value to a key if it is not present.
|
|
86
|
+
*/
|
|
87
|
+
export const withDefault =
|
|
88
|
+
<T>(getDefault: () => NoInfer<T>) =>
|
|
89
|
+
(key: Key<T>): KeyWithDefault<T, T> => {
|
|
90
|
+
return {
|
|
91
|
+
key: key.key,
|
|
92
|
+
get: key.get.pipe(Effect.map(Option.getOrElse(() => getDefault()))),
|
|
93
|
+
set: (value) => key.set(value),
|
|
94
|
+
delete: () => key.delete(),
|
|
95
|
+
pipe(...args: any) {
|
|
96
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Headers from '@effect/platform/Headers';
|
|
6
|
+
import * as HttpClient from '@effect/platform/HttpClient';
|
|
7
|
+
import * as HttpClientError from '@effect/platform/HttpClientError';
|
|
8
|
+
import * as HttpClientResponse from '@effect/platform/HttpClientResponse';
|
|
9
|
+
import * as Effect from 'effect/Effect';
|
|
10
|
+
import * as FiberRef from 'effect/FiberRef';
|
|
11
|
+
import * as Layer from 'effect/Layer';
|
|
12
|
+
import * as Stream from 'effect/Stream';
|
|
13
|
+
|
|
14
|
+
import { log } from '@dxos/log';
|
|
15
|
+
import { type EdgeFunctionEnv, ErrorCodec } from '@dxos/protocols';
|
|
16
|
+
/**
|
|
17
|
+
* Copy pasted from https://github.com/Effect-TS/effect/blob/main/packages/platform/src/internal/fetchHttpClient.ts
|
|
18
|
+
*/
|
|
19
|
+
export const requestInitTagKey = '@effect/platform/FetchHttpClient/FetchOptions';
|
|
20
|
+
|
|
21
|
+
export class FunctionsAiHttpClient {
|
|
22
|
+
static make = (service: EdgeFunctionEnv.FunctionsAiService) =>
|
|
23
|
+
HttpClient.make((request, url, signal, fiber) => {
|
|
24
|
+
const context = fiber.getFiberRef(FiberRef.currentContext);
|
|
25
|
+
const options: RequestInit = context.unsafeMap.get(requestInitTagKey) ?? {};
|
|
26
|
+
const headers = options.headers
|
|
27
|
+
? Headers.merge(Headers.fromInput(options.headers), request.headers)
|
|
28
|
+
: request.headers;
|
|
29
|
+
|
|
30
|
+
const send = (body: BodyInit | undefined) =>
|
|
31
|
+
Effect.tryPromise({
|
|
32
|
+
try: () =>
|
|
33
|
+
service.fetch(
|
|
34
|
+
new Request(url, {
|
|
35
|
+
...options,
|
|
36
|
+
method: request.method,
|
|
37
|
+
headers,
|
|
38
|
+
body,
|
|
39
|
+
// Note: Don't pass signal - it can't be serialized through RPC
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
catch: (cause) => {
|
|
43
|
+
log.error('Failed to fetch', { errorSerialized: ErrorCodec.encode(cause as Error) });
|
|
44
|
+
return new HttpClientError.RequestError({
|
|
45
|
+
request,
|
|
46
|
+
reason: 'Transport',
|
|
47
|
+
cause,
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
}).pipe(Effect.map((response) => HttpClientResponse.fromWeb(request, response)));
|
|
51
|
+
|
|
52
|
+
switch (request.body._tag) {
|
|
53
|
+
case 'Raw':
|
|
54
|
+
case 'Uint8Array':
|
|
55
|
+
return send(request.body.body as any);
|
|
56
|
+
case 'FormData':
|
|
57
|
+
return send(request.body.formData);
|
|
58
|
+
case 'Stream':
|
|
59
|
+
return Stream.toReadableStreamEffect(request.body.stream).pipe(Effect.flatMap(send));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return send(undefined);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
static layer = (service: EdgeFunctionEnv.FunctionsAiService) =>
|
|
66
|
+
Layer.succeed(HttpClient.HttpClient, FunctionsAiHttpClient.make(service));
|
|
67
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { FunctionError } from '../errors';
|
|
8
|
+
import fibFunc from '../example/fib';
|
|
9
|
+
import replyFunc from '../example/reply';
|
|
10
|
+
import { wrapFunctionHandler } from './protocol';
|
|
11
|
+
|
|
12
|
+
describe('wrapFunctionHandler', () => {
|
|
13
|
+
test('wraps reply function and executes handler', async ({ expect }) => {
|
|
14
|
+
const wrapped = wrapFunctionHandler(replyFunc);
|
|
15
|
+
|
|
16
|
+
expect(wrapped.meta.key).toBe('example.org/function/reply');
|
|
17
|
+
expect(wrapped.meta.name).toBe('Reply');
|
|
18
|
+
|
|
19
|
+
const testData = { message: 'hello' };
|
|
20
|
+
const result = await wrapped.handler({
|
|
21
|
+
data: testData,
|
|
22
|
+
context: {
|
|
23
|
+
services: {},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
expect(result).toEqual(testData);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('wraps fibonacci function with valid input', async ({ expect }) => {
|
|
31
|
+
const wrapped = wrapFunctionHandler(fibFunc);
|
|
32
|
+
|
|
33
|
+
expect(wrapped.meta.key).toBe('example.org/function/fib');
|
|
34
|
+
expect(wrapped.meta.name).toBe('Fibonacci');
|
|
35
|
+
|
|
36
|
+
const result = await wrapped.handler({
|
|
37
|
+
data: { iterations: 10 },
|
|
38
|
+
context: {
|
|
39
|
+
services: {},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(result).toEqual({ result: '55' });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
|
|
47
|
+
const wrapped = wrapFunctionHandler(fibFunc);
|
|
48
|
+
|
|
49
|
+
await expect(
|
|
50
|
+
wrapped.handler({
|
|
51
|
+
data: { iterations: 'invalid' },
|
|
52
|
+
context: {
|
|
53
|
+
services: {},
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
).rejects.toThrow(FunctionError);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as AnthropicClient from '@effect/ai-anthropic/AnthropicClient';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Layer from 'effect/Layer';
|
|
8
|
+
import * as Schema from 'effect/Schema';
|
|
9
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
10
|
+
|
|
11
|
+
import { AiModelResolver, AiService } from '@dxos/ai';
|
|
12
|
+
import { AnthropicResolver } from '@dxos/ai/resolvers';
|
|
13
|
+
import { LifecycleState, Resource } from '@dxos/context';
|
|
14
|
+
import { Database, Feed, JsonSchema, Ref, type Type } from '@dxos/echo';
|
|
15
|
+
import { EchoClient, type EchoDatabaseImpl, type QueueFactory, createFeedServiceLayer } from '@dxos/echo-db';
|
|
16
|
+
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
17
|
+
import { runAndForwardErrors } from '@dxos/effect';
|
|
18
|
+
import { assertState, failedInvariant, invariant } from '@dxos/invariant';
|
|
19
|
+
import { PublicKey } from '@dxos/keys';
|
|
20
|
+
import { Operation } from '@dxos/operation';
|
|
21
|
+
import { type FunctionProtocol } from '@dxos/protocols';
|
|
22
|
+
|
|
23
|
+
import { FunctionError } from '../errors';
|
|
24
|
+
import { type FunctionServices } from '../sdk';
|
|
25
|
+
import { CredentialsService, FunctionInvocationService, QueueService } from '../services';
|
|
26
|
+
import * as Trace from '../Trace';
|
|
27
|
+
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
31
|
+
*/
|
|
32
|
+
export const wrapFunctionHandler = (func: Operation.WithHandler<Operation.Definition.Any>): FunctionProtocol.Func => {
|
|
33
|
+
if (!Operation.isOperationWithHandler(func)) {
|
|
34
|
+
throw new TypeError('Expected operation with handler');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const serviceTags = func.services.map((service) => service.key);
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
meta: {
|
|
41
|
+
key: func.meta.key,
|
|
42
|
+
name: func.meta.name,
|
|
43
|
+
description: func.meta.description,
|
|
44
|
+
inputSchema: JsonSchema.toJsonSchema(func.input),
|
|
45
|
+
outputSchema: func.output === undefined ? undefined : JsonSchema.toJsonSchema(func.output),
|
|
46
|
+
services: func.services.map((service) => service.key),
|
|
47
|
+
},
|
|
48
|
+
handler: async ({ data, context }) => {
|
|
49
|
+
if (
|
|
50
|
+
(serviceTags.includes(Database.Service.key) ||
|
|
51
|
+
serviceTags.includes(QueueService.key) ||
|
|
52
|
+
serviceTags.includes(Feed.FeedService.key)) &&
|
|
53
|
+
(!context.services.dataService || !context.services.queryService)
|
|
54
|
+
) {
|
|
55
|
+
throw new FunctionError({
|
|
56
|
+
message: 'Services not provided: dataService, queryService',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// eslint-disable-next-line no-useless-catch
|
|
61
|
+
try {
|
|
62
|
+
if (!SchemaAST.isAnyKeyword(func.input.ast)) {
|
|
63
|
+
try {
|
|
64
|
+
Schema.validateSync(func.input)(data);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
throw new FunctionError({ message: 'Invalid input schema', cause: error });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
await using funcContext = await new FunctionContext(context).open();
|
|
71
|
+
|
|
72
|
+
if (func.types.length > 0) {
|
|
73
|
+
invariant(funcContext.db, 'Database is required for functions with types');
|
|
74
|
+
await funcContext.db.graph.schemaRegistry.register(func.types as Type.AnyEntity[]);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const dataWithDecodedRefs =
|
|
78
|
+
funcContext.db && !SchemaAST.isAnyKeyword(func.input.ast)
|
|
79
|
+
? decodeRefsFromSchema(func.input.ast, data, funcContext.db)
|
|
80
|
+
: data;
|
|
81
|
+
|
|
82
|
+
let result: any = await func.handler(dataWithDecodedRefs);
|
|
83
|
+
|
|
84
|
+
if (Effect.isEffect(result)) {
|
|
85
|
+
result = await runAndForwardErrors(
|
|
86
|
+
(result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
|
|
87
|
+
Effect.orDie,
|
|
88
|
+
Effect.provide(funcContext.createLayer()),
|
|
89
|
+
),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (func.output && !SchemaAST.isAnyKeyword(func.output.ast)) {
|
|
94
|
+
Schema.validateSync(func.output)(result);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return result;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
// TODO(dmaretskyi): We might do error wrapping here and add extra context.
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Container for services and context for a function.
|
|
108
|
+
*/
|
|
109
|
+
class FunctionContext extends Resource {
|
|
110
|
+
readonly context: FunctionProtocol.Context;
|
|
111
|
+
readonly client: EchoClient | undefined;
|
|
112
|
+
db: EchoDatabaseImpl | undefined;
|
|
113
|
+
queues: QueueFactory | undefined;
|
|
114
|
+
|
|
115
|
+
constructor(context: FunctionProtocol.Context) {
|
|
116
|
+
super();
|
|
117
|
+
this.context = context;
|
|
118
|
+
if (context.services.dataService && context.services.queryService) {
|
|
119
|
+
this.client = new EchoClient().connectToService({
|
|
120
|
+
dataService: context.services.dataService,
|
|
121
|
+
queryService: context.services.queryService,
|
|
122
|
+
queueService: context.services.queueService,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
override async _open() {
|
|
128
|
+
await this.client?.open();
|
|
129
|
+
this.db =
|
|
130
|
+
this.client && this.context.spaceId
|
|
131
|
+
? this.client.constructDatabase({
|
|
132
|
+
spaceId: this.context.spaceId ?? failedInvariant(),
|
|
133
|
+
spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
|
|
134
|
+
reactiveSchemaQuery: false,
|
|
135
|
+
preloadSchemaOnOpen: false,
|
|
136
|
+
})
|
|
137
|
+
: undefined;
|
|
138
|
+
|
|
139
|
+
await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
|
|
140
|
+
await this.db?.open();
|
|
141
|
+
this.queues =
|
|
142
|
+
this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
override async _close() {
|
|
146
|
+
await this.db?.close();
|
|
147
|
+
await this.client?.close();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
createLayer(): Layer.Layer<FunctionServices> {
|
|
151
|
+
assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
|
|
152
|
+
|
|
153
|
+
const dbLayer = this.db ? Database.layer(this.db) : Database.notAvailable;
|
|
154
|
+
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
155
|
+
const feedLayer = this.queues ? createFeedServiceLayer(this.queues) : Feed.notAvailable;
|
|
156
|
+
const credentials = dbLayer
|
|
157
|
+
? CredentialsService.layerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
158
|
+
: CredentialsService.configuredLayer([]);
|
|
159
|
+
const functionInvocationService = MockedFunctionInvocationService;
|
|
160
|
+
const operationServiceLayer = MockedOperationServiceLayer;
|
|
161
|
+
|
|
162
|
+
const aiLayer = this.context.services.functionsAiService
|
|
163
|
+
? AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
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
|
+
)
|
|
175
|
+
: AiService.notAvailable;
|
|
176
|
+
|
|
177
|
+
return Layer.mergeAll(
|
|
178
|
+
dbLayer,
|
|
179
|
+
queuesLayer,
|
|
180
|
+
feedLayer,
|
|
181
|
+
credentials,
|
|
182
|
+
functionInvocationService,
|
|
183
|
+
operationServiceLayer,
|
|
184
|
+
aiLayer,
|
|
185
|
+
// TODO(dmaretskyi): Forward trace events.
|
|
186
|
+
Trace.writerLayerNoop,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
|
|
192
|
+
invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
|
|
193
|
+
resolveFunction: () => Effect.die('Not implemented.'),
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const MockedOperationServiceLayer = Layer.succeed(Operation.Service, {
|
|
197
|
+
invoke: () => Effect.die('Calling operations from functions is not implemented yet.'),
|
|
198
|
+
schedule: () => Effect.die('Not implemented.'),
|
|
199
|
+
invokePromise: async () => ({ error: new Error('Not implemented') }),
|
|
200
|
+
} as any);
|
|
201
|
+
|
|
202
|
+
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|
|
203
|
+
if (value == null) {
|
|
204
|
+
return value;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const encoded = SchemaAST.encodedBoundAST(ast);
|
|
208
|
+
if (Ref.isRefType(encoded)) {
|
|
209
|
+
if (Ref.isRef(value)) {
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (typeof value === 'object' && value !== null && typeof (value as any)['/'] === 'string') {
|
|
214
|
+
const resolver = db.graph.createRefResolver({ context: { space: db.spaceId } });
|
|
215
|
+
return refFromEncodedReference(value as any, resolver);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return value;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
switch (encoded._tag) {
|
|
222
|
+
case 'TypeLiteral': {
|
|
223
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
224
|
+
return value;
|
|
225
|
+
}
|
|
226
|
+
const result: Record<string, unknown> = { ...(value as any) };
|
|
227
|
+
for (const prop of SchemaAST.getPropertySignatures(encoded)) {
|
|
228
|
+
const key = prop.name.toString();
|
|
229
|
+
if (key in result) {
|
|
230
|
+
result[key] = decodeRefsFromSchema(prop.type, (result as any)[key], db);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
case 'TupleType': {
|
|
237
|
+
if (!Array.isArray(value)) {
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// For arrays, effect uses TupleType with empty elements and a single rest element.
|
|
242
|
+
if (encoded.elements.length === 0 && encoded.rest.length === 1) {
|
|
243
|
+
const elementType = encoded.rest[0].type;
|
|
244
|
+
return (value as unknown[]).map((item) => decodeRefsFromSchema(elementType, item, db));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
case 'Union': {
|
|
251
|
+
// Optional values are represented as union with undefined.
|
|
252
|
+
const nonUndefined = encoded.types.filter((t) => !SchemaAST.isUndefinedKeyword(t));
|
|
253
|
+
if (nonUndefined.length === 1) {
|
|
254
|
+
return decodeRefsFromSchema(nonUndefined[0], value, db);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// For other unions we can't safely pick a branch without validating.
|
|
258
|
+
return value;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
case 'Suspend': {
|
|
262
|
+
return decodeRefsFromSchema(encoded.f(), value, db);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case 'Refinement': {
|
|
266
|
+
return decodeRefsFromSchema(encoded.from, value, db);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
default: {
|
|
270
|
+
return value;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type AiService } from '@dxos/ai';
|
|
6
|
+
import { type Database, type Feed } from '@dxos/echo';
|
|
7
|
+
import { type Trace } from '@dxos/functions';
|
|
8
|
+
import type { Operation } from '@dxos/operation';
|
|
9
|
+
|
|
10
|
+
import { type CredentialsService, type FunctionInvocationService, type QueueService } from './services';
|
|
11
|
+
|
|
12
|
+
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
13
|
+
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
14
|
+
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
15
|
+
// https://www.npmjs.com/package/aws-lambda
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Services that are available to invoked functions.
|
|
19
|
+
* @deprecated
|
|
20
|
+
*/
|
|
21
|
+
export type FunctionServices =
|
|
22
|
+
| AiService.AiService
|
|
23
|
+
| CredentialsService
|
|
24
|
+
| Database.Service
|
|
25
|
+
// TODO(wittjosiah): Remove QueueService — use Feed.FeedService instead.
|
|
26
|
+
| QueueService
|
|
27
|
+
| Feed.FeedService
|
|
28
|
+
| Trace.TraceService
|
|
29
|
+
| FunctionInvocationService
|
|
30
|
+
| Operation.Service;
|
|
@@ -2,9 +2,19 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as HttpClient from '@effect/platform/HttpClient';
|
|
6
|
+
import * as HttpClientRequest from '@effect/platform/HttpClientRequest';
|
|
7
|
+
import type * as Config from 'effect/Config';
|
|
8
|
+
import * as Context from 'effect/Context';
|
|
9
|
+
import * as Effect from 'effect/Effect';
|
|
10
|
+
import * as Layer from 'effect/Layer';
|
|
11
|
+
import * as Redacted from 'effect/Redacted';
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
import { Query } from '@dxos/echo';
|
|
14
|
+
import { Database } from '@dxos/echo';
|
|
15
|
+
import { AccessToken } from '@dxos/types';
|
|
16
|
+
|
|
17
|
+
export type CredentialQuery = {
|
|
8
18
|
service?: string;
|
|
9
19
|
};
|
|
10
20
|
|
|
@@ -17,7 +27,7 @@ export type ServiceCredential = {
|
|
|
17
27
|
apiKey?: string;
|
|
18
28
|
};
|
|
19
29
|
|
|
20
|
-
export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
30
|
+
export class CredentialsService extends Context.Tag('@dxos/functions/CredentialsService')<
|
|
21
31
|
CredentialsService,
|
|
22
32
|
{
|
|
23
33
|
/**
|
|
@@ -31,7 +41,91 @@ export class CredentialsService extends Context.Tag('CredentialsService')<
|
|
|
31
41
|
*/
|
|
32
42
|
getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;
|
|
33
43
|
}
|
|
34
|
-
>() {
|
|
44
|
+
>() {
|
|
45
|
+
static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>
|
|
46
|
+
Effect.gen(function* () {
|
|
47
|
+
const credentials = yield* CredentialsService;
|
|
48
|
+
return yield* Effect.promise(() => credentials.getCredential(query));
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
static getApiKey = (query: CredentialQuery): Effect.Effect<Redacted.Redacted<string>, never, CredentialsService> =>
|
|
52
|
+
Effect.gen(function* () {
|
|
53
|
+
const credential = yield* CredentialsService.getCredential(query);
|
|
54
|
+
if (!credential.apiKey) {
|
|
55
|
+
throw new Error(`API key not found for service: ${query.service}`);
|
|
56
|
+
}
|
|
57
|
+
return Redacted.make(credential.apiKey);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
61
|
+
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
62
|
+
|
|
63
|
+
static layerConfig = (
|
|
64
|
+
credentials: {
|
|
65
|
+
service: string;
|
|
66
|
+
apiKey: Config.Config<Redacted.Redacted<string>>;
|
|
67
|
+
}[],
|
|
68
|
+
) =>
|
|
69
|
+
Layer.effect(
|
|
70
|
+
CredentialsService,
|
|
71
|
+
Effect.gen(function* () {
|
|
72
|
+
const serviceCredentials = yield* Effect.forEach(credentials, ({ service, apiKey }) =>
|
|
73
|
+
Effect.gen(function* () {
|
|
74
|
+
return {
|
|
75
|
+
service,
|
|
76
|
+
apiKey: Redacted.value(yield* apiKey),
|
|
77
|
+
};
|
|
78
|
+
}),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return new ConfiguredCredentialsService(serviceCredentials);
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
static layerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
|
|
86
|
+
Layer.effect(
|
|
87
|
+
CredentialsService,
|
|
88
|
+
Effect.gen(function* () {
|
|
89
|
+
const dbService = yield* Database.Service;
|
|
90
|
+
const cache = new Map<string, ServiceCredential[]>();
|
|
91
|
+
|
|
92
|
+
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
93
|
+
const cacheKey = JSON.stringify(query);
|
|
94
|
+
if (caching && cache.has(cacheKey)) {
|
|
95
|
+
return cache.get(cacheKey)!;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const accessTokens = await dbService.db.query(Query.type(AccessToken.AccessToken)).run();
|
|
99
|
+
const credentials = accessTokens
|
|
100
|
+
.filter((accessToken) => accessToken.source === query.service)
|
|
101
|
+
.map((accessToken) => ({
|
|
102
|
+
service: accessToken.source,
|
|
103
|
+
apiKey: accessToken.token,
|
|
104
|
+
}));
|
|
105
|
+
|
|
106
|
+
if (caching) {
|
|
107
|
+
cache.set(cacheKey, credentials);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return credentials;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
getCredential: async (query) => {
|
|
115
|
+
const credentials = await queryCredentials(query);
|
|
116
|
+
if (credentials.length === 0) {
|
|
117
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return credentials[0];
|
|
121
|
+
},
|
|
122
|
+
queryCredentials: async (query) => {
|
|
123
|
+
return queryCredentials(query);
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
35
129
|
|
|
36
130
|
export class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {
|
|
37
131
|
constructor(private readonly credentials: ServiceCredential[] = []) {}
|
|
@@ -50,6 +144,16 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
50
144
|
if (!credential) {
|
|
51
145
|
throw new Error(`Credential not found for service: ${query.service}`);
|
|
52
146
|
}
|
|
147
|
+
|
|
53
148
|
return credential;
|
|
54
149
|
}
|
|
55
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Maps the request to include the given token in the Authorization header.
|
|
154
|
+
*/
|
|
155
|
+
export const withAuthorization = (token: string, kind?: 'Bearer' | 'Basic') =>
|
|
156
|
+
HttpClient.mapRequest((request) => {
|
|
157
|
+
const authorization = kind ? `${kind} ${token}` : token;
|
|
158
|
+
return HttpClientRequest.setHeader(request, 'Authorization', authorization);
|
|
159
|
+
});
|