@dxos/functions 0.8.4-main.ae835ea → 0.8.4-main.bc674ce
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/browser/index.mjs +767 -884
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +767 -884
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/errors.d.ts +44 -60
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/example/fib.d.ts +1 -1
- package/dist/types/src/example/index.d.ts +3 -3
- package/dist/types/src/example/index.d.ts.map +1 -1
- package/dist/types/src/example/reply.d.ts +1 -1
- package/dist/types/src/example/sleep.d.ts +1 -1
- package/dist/types/src/index.d.ts +2 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/operation-compatibility.test.d.ts +2 -0
- package/dist/types/src/operation-compatibility.test.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 +114 -0
- package/dist/types/src/sdk.d.ts.map +1 -0
- package/dist/types/src/services/credentials.d.ts +6 -4
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +33 -27
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +7 -19
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -1
- package/dist/types/src/services/index.d.ts +2 -4
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +4 -4
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +41 -12
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/types/Function.d.ts +40 -35
- package/dist/types/src/types/Function.d.ts.map +1 -1
- package/dist/types/src/types/Script.d.ts +9 -16
- package/dist/types/src/types/Script.d.ts.map +1 -1
- package/dist/types/src/types/Trigger.d.ts +58 -76
- package/dist/types/src/types/Trigger.d.ts.map +1 -1
- package/dist/types/src/types/TriggerEvent.d.ts +43 -13
- package/dist/types/src/types/TriggerEvent.d.ts.map +1 -1
- package/dist/types/src/types/index.d.ts +1 -0
- package/dist/types/src/types/index.d.ts.map +1 -1
- 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 +23 -58
- package/src/errors.ts +4 -4
- package/src/example/fib.ts +1 -1
- package/src/example/reply.ts +1 -1
- package/src/example/sleep.ts +1 -1
- package/src/index.ts +2 -5
- package/src/operation-compatibility.test.ts +185 -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 +59 -0
- package/src/protocol/protocol.ts +262 -0
- package/src/{handler.ts → sdk.ts} +129 -66
- package/src/services/credentials.ts +32 -17
- package/src/services/event-logger.ts +10 -4
- package/src/services/function-invocation-service.ts +23 -70
- package/src/services/index.ts +2 -4
- package/src/services/queues.ts +5 -7
- package/src/services/tracing.ts +68 -44
- package/src/types/Function.ts +39 -8
- package/src/types/Script.ts +10 -9
- package/src/types/Trigger.ts +18 -14
- package/src/types/TriggerEvent.ts +29 -29
- package/src/types/index.ts +1 -0
- package/src/types/url.ts +32 -0
- package/dist/lib/browser/bundler/index.mjs +0 -256
- package/dist/lib/browser/bundler/index.mjs.map +0 -7
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
- package/dist/lib/browser/chunk-M6EXIREF.mjs +0 -610
- package/dist/lib/browser/chunk-M6EXIREF.mjs.map +0 -7
- package/dist/lib/browser/edge/index.mjs +0 -83
- package/dist/lib/browser/edge/index.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -131
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/bundler/index.mjs +0 -257
- package/dist/lib/node-esm/bundler/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-P3IATZMZ.mjs +0 -612
- package/dist/lib/node-esm/chunk-P3IATZMZ.mjs.map +0 -7
- package/dist/lib/node-esm/edge/index.mjs +0 -84
- package/dist/lib/node-esm/edge/index.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -132
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/bundler/bundler.d.ts +0 -49
- 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/e2e/deploy.test.d.ts +0 -2
- package/dist/types/src/e2e/deploy.test.d.ts.map +0 -1
- package/dist/types/src/edge/functions.d.ts +0 -17
- 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 -14
- 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 -102
- package/dist/types/src/handler.d.ts.map +0 -1
- package/dist/types/src/services/database.d.ts +0 -67
- package/dist/types/src/services/database.d.ts.map +0 -1
- package/dist/types/src/services/function-invocation-service.test.d.ts +0 -2
- package/dist/types/src/services/function-invocation-service.test.d.ts.map +0 -1
- package/dist/types/src/services/local-function-execution.d.ts +0 -34
- package/dist/types/src/services/local-function-execution.d.ts.map +0 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +0 -22
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +0 -1
- package/dist/types/src/services/service-container.d.ts +0 -57
- package/dist/types/src/services/service-container.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.d.ts +0 -31
- package/dist/types/src/services/service-registry.d.ts.map +0 -1
- package/dist/types/src/services/service-registry.test.d.ts +0 -2
- package/dist/types/src/services/service-registry.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/layer.d.ts +0 -18
- package/dist/types/src/testing/layer.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/persist-database.test.d.ts +0 -2
- package/dist/types/src/testing/persist-database.test.d.ts.map +0 -1
- package/dist/types/src/testing/services.d.ts +0 -59
- package/dist/types/src/testing/services.d.ts.map +0 -1
- package/dist/types/src/trace.d.ts +0 -122
- 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/triggers/index.d.ts +0 -4
- package/dist/types/src/triggers/index.d.ts.map +0 -1
- package/dist/types/src/triggers/input-builder.d.ts +0 -3
- package/dist/types/src/triggers/input-builder.d.ts.map +0 -1
- package/dist/types/src/triggers/invocation-tracer.d.ts +0 -37
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +0 -78
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +0 -2
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +0 -1
- package/dist/types/src/triggers/trigger-state-store.d.ts +0 -28
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +0 -1
- package/dist/types/src/url.d.ts +0 -21
- package/dist/types/src/url.d.ts.map +0 -1
- package/src/bundler/bundler.test.ts +0 -58
- package/src/bundler/bundler.ts +0 -295
- package/src/bundler/index.ts +0 -5
- package/src/e2e/deploy.test.ts +0 -69
- package/src/edge/functions.ts +0 -67
- package/src/edge/index.ts +0 -9
- package/src/executor/executor.ts +0 -58
- package/src/services/database.ts +0 -175
- package/src/services/function-invocation-service.test.ts +0 -81
- package/src/services/local-function-execution.ts +0 -153
- package/src/services/remote-function-execution-service.ts +0 -63
- package/src/services/service-container.ts +0 -115
- package/src/services/service-registry.test.ts +0 -45
- package/src/services/service-registry.ts +0 -63
- package/src/testing/index.ts +0 -6
- package/src/testing/layer.ts +0 -114
- package/src/testing/logger.ts +0 -17
- package/src/testing/persist-database.test.ts +0 -87
- package/src/testing/services.ts +0 -115
- package/src/trace.ts +0 -178
- package/src/translations.ts +0 -20
- package/src/triggers/index.ts +0 -7
- package/src/triggers/input-builder.ts +0 -35
- package/src/triggers/invocation-tracer.ts +0 -101
- package/src/triggers/trigger-dispatcher.test.ts +0 -664
- package/src/triggers/trigger-dispatcher.ts +0 -521
- package/src/triggers/trigger-state-store.ts +0 -61
- package/src/url.ts +0 -55
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions",
|
|
3
|
-
"version": "0.8.4-main.
|
|
4
|
-
"description": "Functions API
|
|
3
|
+
"version": "0.8.4-main.bc674ce",
|
|
4
|
+
"description": "Functions API.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"sideEffects": false,
|
|
@@ -14,71 +18,32 @@
|
|
|
14
18
|
"types": "./dist/types/src/index.d.ts",
|
|
15
19
|
"browser": "./dist/lib/browser/index.mjs",
|
|
16
20
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
17
|
-
},
|
|
18
|
-
"./bundler": {
|
|
19
|
-
"source": "./src/bundler/index.ts",
|
|
20
|
-
"types": "./dist/types/src/bundler/index.d.ts",
|
|
21
|
-
"browser": "./dist/lib/browser/bundler/index.mjs",
|
|
22
|
-
"node": "./dist/lib/node-esm/bundler/index.mjs"
|
|
23
|
-
},
|
|
24
|
-
"./edge": {
|
|
25
|
-
"source": "./src/edge/index.ts",
|
|
26
|
-
"types": "./dist/types/src/edge/index.d.ts",
|
|
27
|
-
"browser": "./dist/lib/browser/edge/index.mjs",
|
|
28
|
-
"node": "./dist/lib/node-esm/edge/index.mjs"
|
|
29
|
-
},
|
|
30
|
-
"./testing": {
|
|
31
|
-
"source": "./src/testing/index.ts",
|
|
32
|
-
"types": "./dist/types/src/testing/index.d.ts",
|
|
33
|
-
"browser": "./dist/lib/browser/testing/index.mjs",
|
|
34
|
-
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
35
21
|
}
|
|
36
22
|
},
|
|
37
23
|
"types": "dist/types/src/index.d.ts",
|
|
38
|
-
"typesVersions": {
|
|
39
|
-
"*": {
|
|
40
|
-
"bundler": [
|
|
41
|
-
"dist/types/src/bundler/index.d.ts"
|
|
42
|
-
],
|
|
43
|
-
"edge": [
|
|
44
|
-
"dist/types/src/edge/index.d.ts"
|
|
45
|
-
]
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
24
|
"files": [
|
|
49
25
|
"dist",
|
|
50
26
|
"schema",
|
|
51
27
|
"src"
|
|
52
28
|
],
|
|
53
29
|
"dependencies": {
|
|
54
|
-
"@effect/
|
|
55
|
-
"effect": "
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"@dxos/
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/
|
|
66
|
-
"@dxos/
|
|
67
|
-
"@dxos/
|
|
68
|
-
"@dxos/
|
|
69
|
-
"@dxos/
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/effect": "0.8.4-main.ae835ea",
|
|
72
|
-
"@dxos/errors": "0.8.4-main.ae835ea",
|
|
73
|
-
"@dxos/invariant": "0.8.4-main.ae835ea",
|
|
74
|
-
"@dxos/kv-store": "0.8.4-main.ae835ea",
|
|
75
|
-
"@dxos/live-object": "0.8.4-main.ae835ea",
|
|
76
|
-
"@dxos/log": "0.8.4-main.ae835ea",
|
|
77
|
-
"@dxos/node-std": "0.8.4-main.ae835ea",
|
|
78
|
-
"@dxos/schema": "0.8.4-main.ae835ea",
|
|
79
|
-
"@dxos/protocols": "0.8.4-main.ae835ea",
|
|
80
|
-
"@dxos/util": "0.8.4-main.ae835ea",
|
|
81
|
-
"@dxos/keys": "0.8.4-main.ae835ea"
|
|
30
|
+
"@effect/ai-anthropic": "0.22.0",
|
|
31
|
+
"@effect/platform": "0.93.6",
|
|
32
|
+
"effect": "3.19.11",
|
|
33
|
+
"@dxos/context": "0.8.4-main.bc674ce",
|
|
34
|
+
"@dxos/ai": "0.8.4-main.bc674ce",
|
|
35
|
+
"@dxos/echo": "0.8.4-main.bc674ce",
|
|
36
|
+
"@dxos/echo-db": "0.8.4-main.bc674ce",
|
|
37
|
+
"@dxos/effect": "0.8.4-main.bc674ce",
|
|
38
|
+
"@dxos/errors": "0.8.4-main.bc674ce",
|
|
39
|
+
"@dxos/keys": "0.8.4-main.bc674ce",
|
|
40
|
+
"@dxos/invariant": "0.8.4-main.bc674ce",
|
|
41
|
+
"@dxos/log": "0.8.4-main.bc674ce",
|
|
42
|
+
"@dxos/node-std": "0.8.4-main.bc674ce",
|
|
43
|
+
"@dxos/operation": "0.8.4-main.bc674ce",
|
|
44
|
+
"@dxos/protocols": "0.8.4-main.bc674ce",
|
|
45
|
+
"@dxos/schema": "0.8.4-main.bc674ce",
|
|
46
|
+
"@dxos/types": "0.8.4-main.bc674ce"
|
|
82
47
|
},
|
|
83
48
|
"publishConfig": {
|
|
84
49
|
"access": "public"
|
package/src/errors.ts
CHANGED
|
@@ -4,18 +4,18 @@
|
|
|
4
4
|
|
|
5
5
|
import { BaseError, type BaseErrorOptions } from '@dxos/errors';
|
|
6
6
|
|
|
7
|
-
export class ServiceNotAvailableError extends BaseError.extend('
|
|
7
|
+
export class ServiceNotAvailableError extends BaseError.extend('ServiceNotAvailable', 'Service not available') {
|
|
8
8
|
constructor(service: string, options?: Omit<BaseErrorOptions, 'context'>) {
|
|
9
9
|
super({ context: { service }, ...options });
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export class FunctionNotFoundError extends BaseError.extend('
|
|
13
|
+
export class FunctionNotFoundError extends BaseError.extend('FunctionNotFound', 'Function not found') {
|
|
14
14
|
constructor(functionKey: string, options?: Omit<BaseErrorOptions, 'context'>) {
|
|
15
15
|
super({ context: { function: functionKey }, ...options });
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class FunctionError extends BaseError.extend('
|
|
19
|
+
export class FunctionError extends BaseError.extend('FunctionError', 'Function invocation error') {}
|
|
20
20
|
|
|
21
|
-
export class TriggerStateNotFoundError extends BaseError.extend('
|
|
21
|
+
export class TriggerStateNotFoundError extends BaseError.extend('TriggerStateNotFound', 'Trigger state not found') {}
|
package/src/example/fib.ts
CHANGED
package/src/example/reply.ts
CHANGED
|
@@ -6,7 +6,7 @@ import * as Console from 'effect/Console';
|
|
|
6
6
|
import * as Effect from 'effect/Effect';
|
|
7
7
|
import * as Schema from 'effect/Schema';
|
|
8
8
|
|
|
9
|
-
import { defineFunction } from '../
|
|
9
|
+
import { defineFunction } from '../sdk';
|
|
10
10
|
|
|
11
11
|
export default defineFunction({
|
|
12
12
|
key: 'example.org/function/reply',
|
package/src/example/sleep.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,10 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
export * from './errors';
|
|
6
6
|
export * from './example';
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './handler';
|
|
7
|
+
export * from './sdk';
|
|
9
8
|
export * from './services';
|
|
10
|
-
export * from './trace';
|
|
11
|
-
export * from './triggers';
|
|
12
9
|
export * from './types';
|
|
13
|
-
export * from './
|
|
10
|
+
export * from './protocol';
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Schema from 'effect/Schema';
|
|
7
|
+
import { describe, expect, test } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { FunctionDefinition, defineFunction } from './sdk';
|
|
10
|
+
|
|
11
|
+
describe('Function/Operation Compatibility', () => {
|
|
12
|
+
test('can convert FunctionDefinition to OperationDefinition', () => {
|
|
13
|
+
const func = defineFunction({
|
|
14
|
+
key: 'test.function',
|
|
15
|
+
name: 'Test Function',
|
|
16
|
+
description: 'A test function',
|
|
17
|
+
inputSchema: Schema.Struct({ value: Schema.Number }),
|
|
18
|
+
outputSchema: Schema.Struct({ result: Schema.String }),
|
|
19
|
+
handler: async ({ data }) => {
|
|
20
|
+
return { result: data.value.toString() };
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const op = FunctionDefinition.toOperation(func);
|
|
25
|
+
|
|
26
|
+
expect(op.meta.key).toBe('test.function');
|
|
27
|
+
expect(op.meta.name).toBe('Test Function');
|
|
28
|
+
expect(op.meta.description).toBe('A test function');
|
|
29
|
+
expect(Schema.isSchema(op.schema.input)).toBe(true);
|
|
30
|
+
expect(Schema.isSchema(op.schema.output)).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('converted operation has matching schemas', () => {
|
|
34
|
+
const inputSchema = Schema.Struct({
|
|
35
|
+
name: Schema.String,
|
|
36
|
+
age: Schema.Number,
|
|
37
|
+
});
|
|
38
|
+
const outputSchema = Schema.Struct({
|
|
39
|
+
greeting: Schema.String,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const func = defineFunction({
|
|
43
|
+
key: 'test.greet',
|
|
44
|
+
name: 'Greet',
|
|
45
|
+
inputSchema,
|
|
46
|
+
outputSchema,
|
|
47
|
+
handler: async ({ data }) => {
|
|
48
|
+
return { greeting: `Hello, ${data.name}!` };
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const op = FunctionDefinition.toOperation(func);
|
|
53
|
+
|
|
54
|
+
// Verify schemas match
|
|
55
|
+
const testInput = { name: 'Alice', age: 30 };
|
|
56
|
+
const validatedInput = Schema.decodeSync(op.schema.input)(testInput);
|
|
57
|
+
expect(validatedInput).toEqual(testInput);
|
|
58
|
+
|
|
59
|
+
const testOutput = { greeting: 'Hello, Alice!' };
|
|
60
|
+
const validatedOutput = Schema.decodeSync(op.schema.output)(testOutput);
|
|
61
|
+
expect(validatedOutput).toEqual(testOutput);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('converted operation preserves metadata', () => {
|
|
65
|
+
const func = defineFunction({
|
|
66
|
+
key: 'test.meta',
|
|
67
|
+
name: 'Meta Test',
|
|
68
|
+
description: 'Tests metadata preservation',
|
|
69
|
+
inputSchema: Schema.Void,
|
|
70
|
+
outputSchema: Schema.Void,
|
|
71
|
+
handler: async () => {},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const op = FunctionDefinition.toOperation(func);
|
|
75
|
+
|
|
76
|
+
expect(op.meta.key).toBe('test.meta');
|
|
77
|
+
expect(op.meta.name).toBe('Meta Test');
|
|
78
|
+
expect(op.meta.description).toBe('Tests metadata preservation');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('converted operation is pipeable', () => {
|
|
82
|
+
const func = defineFunction({
|
|
83
|
+
key: 'test.pipe',
|
|
84
|
+
name: 'Pipe Test',
|
|
85
|
+
inputSchema: Schema.Void,
|
|
86
|
+
outputSchema: Schema.Void,
|
|
87
|
+
handler: async () => {},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const op = FunctionDefinition.toOperation(func);
|
|
91
|
+
|
|
92
|
+
expect(typeof op.pipe).toBe('function');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('handles functions without output schema', () => {
|
|
96
|
+
const func = defineFunction({
|
|
97
|
+
key: 'test.no-output',
|
|
98
|
+
name: 'No Output',
|
|
99
|
+
inputSchema: Schema.Struct({ value: Schema.Number }),
|
|
100
|
+
handler: async ({ data }) => {
|
|
101
|
+
return data.value * 2;
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const op = FunctionDefinition.toOperation(func);
|
|
106
|
+
|
|
107
|
+
expect(op.meta.key).toBe('test.no-output');
|
|
108
|
+
expect(Schema.isSchema(op.schema.output)).toBe(true);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('converted operation includes handler', () => {
|
|
112
|
+
const func = defineFunction({
|
|
113
|
+
key: 'test.handler',
|
|
114
|
+
name: 'Handler Test',
|
|
115
|
+
inputSchema: Schema.Struct({ value: Schema.Number }),
|
|
116
|
+
outputSchema: Schema.Struct({ doubled: Schema.Number }),
|
|
117
|
+
handler: async ({ data }) => {
|
|
118
|
+
return { doubled: data.value * 2 };
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const op = FunctionDefinition.toOperation(func);
|
|
123
|
+
|
|
124
|
+
expect(op.handler).toBeDefined();
|
|
125
|
+
expect(typeof op.handler).toBe('function');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('converted operation has handler function', () => {
|
|
129
|
+
const func = defineFunction({
|
|
130
|
+
key: 'test.execute',
|
|
131
|
+
name: 'Execute Test',
|
|
132
|
+
inputSchema: Schema.Struct({ value: Schema.Number }),
|
|
133
|
+
outputSchema: Schema.Struct({ result: Schema.Number }),
|
|
134
|
+
handler: async ({ data }) => {
|
|
135
|
+
return { result: data.value * 3 };
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const op = FunctionDefinition.toOperation(func);
|
|
140
|
+
|
|
141
|
+
// Handler is present and is a function that returns an Effect
|
|
142
|
+
expect(op.handler).toBeDefined();
|
|
143
|
+
const effect = op.handler({ value: 5 });
|
|
144
|
+
// Verify it returns an Effect-like object
|
|
145
|
+
expect(effect).toHaveProperty('pipe');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('converted operation works with Effect-based handlers', () => {
|
|
149
|
+
const func = defineFunction({
|
|
150
|
+
key: 'test.effect',
|
|
151
|
+
name: 'Effect Test',
|
|
152
|
+
inputSchema: Schema.Struct({ value: Schema.Number }),
|
|
153
|
+
outputSchema: Schema.Struct({ result: Schema.Number }),
|
|
154
|
+
handler: Effect.fn(function* ({ data }) {
|
|
155
|
+
return { result: data.value * 4 };
|
|
156
|
+
}),
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const op = FunctionDefinition.toOperation(func);
|
|
160
|
+
|
|
161
|
+
// Handler is present and produces an Effect
|
|
162
|
+
expect(op.handler).toBeDefined();
|
|
163
|
+
const effect = op.handler({ value: 3 });
|
|
164
|
+
expect(effect).toHaveProperty('pipe');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('converted operation works with synchronous handlers', () => {
|
|
168
|
+
const func = defineFunction({
|
|
169
|
+
key: 'test.sync',
|
|
170
|
+
name: 'Sync Test',
|
|
171
|
+
inputSchema: Schema.Struct({ value: Schema.Number }),
|
|
172
|
+
outputSchema: Schema.Struct({ result: Schema.Number }),
|
|
173
|
+
handler: ({ data }) => {
|
|
174
|
+
return { result: data.value * 5 };
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const op = FunctionDefinition.toOperation(func);
|
|
179
|
+
|
|
180
|
+
// Handler is present and produces an Effect
|
|
181
|
+
expect(op.handler).toBeDefined();
|
|
182
|
+
const effect = op.handler({ value: 2 });
|
|
183
|
+
expect(effect).toHaveProperty('pipe');
|
|
184
|
+
});
|
|
185
|
+
});
|
|
@@ -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,59 @@
|
|
|
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
|
+
|
|
11
|
+
import { wrapFunctionHandler } from './protocol';
|
|
12
|
+
|
|
13
|
+
describe('wrapFunctionHandler', () => {
|
|
14
|
+
test('wraps reply function and executes handler', async ({ expect }) => {
|
|
15
|
+
const wrapped = wrapFunctionHandler(replyFunc);
|
|
16
|
+
|
|
17
|
+
expect(wrapped.meta.key).toBe('example.org/function/reply');
|
|
18
|
+
expect(wrapped.meta.name).toBe('Reply');
|
|
19
|
+
|
|
20
|
+
const testData = { message: 'hello' };
|
|
21
|
+
const result = await wrapped.handler({
|
|
22
|
+
data: testData,
|
|
23
|
+
context: {
|
|
24
|
+
services: {},
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(result).toEqual(testData);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('wraps fibonacci function with valid input', async ({ expect }) => {
|
|
32
|
+
const wrapped = wrapFunctionHandler(fibFunc);
|
|
33
|
+
|
|
34
|
+
expect(wrapped.meta.key).toBe('example.org/function/fib');
|
|
35
|
+
expect(wrapped.meta.name).toBe('Fibonacci');
|
|
36
|
+
|
|
37
|
+
const result = await wrapped.handler({
|
|
38
|
+
data: { iterations: 10 },
|
|
39
|
+
context: {
|
|
40
|
+
services: {},
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(result).toEqual({ result: '55' });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('throws FunctionError on invalid input schema for fibonacci', async ({ expect }) => {
|
|
48
|
+
const wrapped = wrapFunctionHandler(fibFunc);
|
|
49
|
+
|
|
50
|
+
await expect(
|
|
51
|
+
wrapped.handler({
|
|
52
|
+
data: { iterations: 'invalid' },
|
|
53
|
+
context: {
|
|
54
|
+
services: {},
|
|
55
|
+
},
|
|
56
|
+
}),
|
|
57
|
+
).rejects.toThrow(FunctionError);
|
|
58
|
+
});
|
|
59
|
+
});
|