@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
|
@@ -0,0 +1,262 @@
|
|
|
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, Ref, Type } from '@dxos/echo';
|
|
15
|
+
import { refFromEncodedReference } from '@dxos/echo/internal';
|
|
16
|
+
import { EchoClient, type EchoDatabaseImpl, type QueueFactory } from '@dxos/echo-db';
|
|
17
|
+
import { runAndForwardErrors } from '@dxos/effect';
|
|
18
|
+
import { assertState, failedInvariant, invariant } from '@dxos/invariant';
|
|
19
|
+
import { PublicKey } from '@dxos/keys';
|
|
20
|
+
import { type FunctionProtocol } from '@dxos/protocols';
|
|
21
|
+
|
|
22
|
+
import { FunctionError } from '../errors';
|
|
23
|
+
import { FunctionDefinition, type FunctionServices } from '../sdk';
|
|
24
|
+
import { CredentialsService, FunctionInvocationService, QueueService, TracingService } from '../services';
|
|
25
|
+
|
|
26
|
+
import { FunctionsAiHttpClient } from './functions-ai-http-client';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Wraps a function handler made with `defineFunction` to a protocol that the functions-runtime expects.
|
|
30
|
+
*/
|
|
31
|
+
export const wrapFunctionHandler = (func: FunctionDefinition): FunctionProtocol.Func => {
|
|
32
|
+
if (!FunctionDefinition.isFunction(func)) {
|
|
33
|
+
throw new TypeError('Invalid function definition');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
meta: {
|
|
38
|
+
key: func.key,
|
|
39
|
+
name: func.name,
|
|
40
|
+
description: func.description,
|
|
41
|
+
inputSchema: Type.toJsonSchema(func.inputSchema),
|
|
42
|
+
outputSchema: func.outputSchema === undefined ? undefined : Type.toJsonSchema(func.outputSchema),
|
|
43
|
+
services: func.services,
|
|
44
|
+
},
|
|
45
|
+
handler: async ({ data, context }) => {
|
|
46
|
+
if (
|
|
47
|
+
(func.services.includes(Database.Service.key) || func.services.includes(QueueService.key)) &&
|
|
48
|
+
(!context.services.dataService || !context.services.queryService)
|
|
49
|
+
) {
|
|
50
|
+
throw new FunctionError({
|
|
51
|
+
message: 'Services not provided: dataService, queryService',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// eslint-disable-next-line no-useless-catch
|
|
56
|
+
try {
|
|
57
|
+
if (!SchemaAST.isAnyKeyword(func.inputSchema.ast)) {
|
|
58
|
+
try {
|
|
59
|
+
Schema.validateSync(func.inputSchema)(data);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new FunctionError({ message: 'Invalid input schema', cause: error });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await using funcContext = await new FunctionContext(context).open();
|
|
66
|
+
|
|
67
|
+
if (func.types.length > 0) {
|
|
68
|
+
invariant(funcContext.db, 'Database is required for functions with types');
|
|
69
|
+
await funcContext.db.graph.schemaRegistry.register(func.types as Type.Entity.Any[]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const dataWithDecodedRefs =
|
|
73
|
+
funcContext.db && !SchemaAST.isAnyKeyword(func.inputSchema.ast)
|
|
74
|
+
? decodeRefsFromSchema(func.inputSchema.ast, data, funcContext.db)
|
|
75
|
+
: data;
|
|
76
|
+
|
|
77
|
+
let result = await func.handler({
|
|
78
|
+
// TODO(dmaretskyi): Fix the types.
|
|
79
|
+
context: context as any,
|
|
80
|
+
data: dataWithDecodedRefs,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (Effect.isEffect(result)) {
|
|
84
|
+
result = await runAndForwardErrors(
|
|
85
|
+
(result as Effect.Effect<unknown, unknown, FunctionServices>).pipe(
|
|
86
|
+
Effect.orDie,
|
|
87
|
+
Effect.provide(funcContext.createLayer()),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (func.outputSchema && !SchemaAST.isAnyKeyword(func.outputSchema.ast)) {
|
|
93
|
+
Schema.validateSync(func.outputSchema)(result);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return result;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
// TODO(dmaretskyi): We might do error wrapping here and add extra context.
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Container for services and context for a function.
|
|
107
|
+
*/
|
|
108
|
+
class FunctionContext extends Resource {
|
|
109
|
+
readonly context: FunctionProtocol.Context;
|
|
110
|
+
readonly client: EchoClient | undefined;
|
|
111
|
+
db: EchoDatabaseImpl | undefined;
|
|
112
|
+
queues: QueueFactory | undefined;
|
|
113
|
+
|
|
114
|
+
constructor(context: FunctionProtocol.Context) {
|
|
115
|
+
super();
|
|
116
|
+
this.context = context;
|
|
117
|
+
if (context.services.dataService && context.services.queryService) {
|
|
118
|
+
this.client = new EchoClient().connectToService({
|
|
119
|
+
dataService: context.services.dataService,
|
|
120
|
+
queryService: context.services.queryService,
|
|
121
|
+
queueService: context.services.queueService,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
override async _open() {
|
|
127
|
+
await this.client?.open();
|
|
128
|
+
this.db =
|
|
129
|
+
this.client && this.context.spaceId
|
|
130
|
+
? this.client.constructDatabase({
|
|
131
|
+
spaceId: this.context.spaceId ?? failedInvariant(),
|
|
132
|
+
spaceKey: PublicKey.fromHex(this.context.spaceKey ?? failedInvariant('spaceKey missing in context')),
|
|
133
|
+
reactiveSchemaQuery: false,
|
|
134
|
+
preloadSchemaOnOpen: false,
|
|
135
|
+
})
|
|
136
|
+
: undefined;
|
|
137
|
+
|
|
138
|
+
await this.db?.setSpaceRoot(this.context.spaceRootUrl ?? failedInvariant('spaceRootUrl missing in context'));
|
|
139
|
+
await this.db?.open();
|
|
140
|
+
this.queues =
|
|
141
|
+
this.client && this.context.spaceId ? this.client.constructQueueFactory(this.context.spaceId) : undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
override async _close() {
|
|
145
|
+
await this.db?.close();
|
|
146
|
+
await this.client?.close();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
createLayer(): Layer.Layer<FunctionServices> {
|
|
150
|
+
assertState(this._lifecycleState === LifecycleState.OPEN, 'FunctionContext is not open');
|
|
151
|
+
|
|
152
|
+
const dbLayer = this.db ? Database.Service.layer(this.db) : Database.Service.notAvailable;
|
|
153
|
+
const queuesLayer = this.queues ? QueueService.layer(this.queues) : QueueService.notAvailable;
|
|
154
|
+
const credentials = dbLayer
|
|
155
|
+
? CredentialsService.layerFromDatabase({ caching: true }).pipe(Layer.provide(dbLayer))
|
|
156
|
+
: CredentialsService.configuredLayer([]);
|
|
157
|
+
const functionInvocationService = MockedFunctionInvocationService;
|
|
158
|
+
const tracing = TracingService.layerNoop;
|
|
159
|
+
|
|
160
|
+
const aiLayer = this.context.services.functionsAiService
|
|
161
|
+
? AiModelResolver.AiModelResolver.buildAiService.pipe(
|
|
162
|
+
Layer.provide(
|
|
163
|
+
AnthropicResolver.make().pipe(
|
|
164
|
+
Layer.provide(
|
|
165
|
+
AnthropicClient.layer({
|
|
166
|
+
// Note: It doesn't matter what is base url here, it will be proxied to ai gateway in edge.
|
|
167
|
+
apiUrl: 'http://internal/provider/anthropic',
|
|
168
|
+
}).pipe(Layer.provide(FunctionsAiHttpClient.layer(this.context.services.functionsAiService))),
|
|
169
|
+
),
|
|
170
|
+
),
|
|
171
|
+
),
|
|
172
|
+
)
|
|
173
|
+
: AiService.notAvailable;
|
|
174
|
+
|
|
175
|
+
return Layer.mergeAll(
|
|
176
|
+
dbLayer, //
|
|
177
|
+
queuesLayer,
|
|
178
|
+
credentials,
|
|
179
|
+
functionInvocationService,
|
|
180
|
+
aiLayer,
|
|
181
|
+
tracing,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const MockedFunctionInvocationService = Layer.succeed(FunctionInvocationService, {
|
|
187
|
+
invokeFunction: () => Effect.die('Calling functions from functions is not implemented yet.'),
|
|
188
|
+
resolveFunction: () => Effect.die('Not implemented.'),
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const decodeRefsFromSchema = (ast: SchemaAST.AST, value: unknown, db: EchoDatabaseImpl): unknown => {
|
|
192
|
+
if (value == null) {
|
|
193
|
+
return value;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const encoded = SchemaAST.encodedBoundAST(ast);
|
|
197
|
+
if (Ref.isRefType(encoded)) {
|
|
198
|
+
if (Ref.isRef(value)) {
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (typeof value === 'object' && value !== null && typeof (value as any)['/'] === 'string') {
|
|
203
|
+
const resolver = db.graph.createRefResolver({ context: { space: db.spaceId } });
|
|
204
|
+
return refFromEncodedReference(value as any, resolver);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return value;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
switch (encoded._tag) {
|
|
211
|
+
case 'TypeLiteral': {
|
|
212
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
213
|
+
return value;
|
|
214
|
+
}
|
|
215
|
+
const result: Record<string, unknown> = { ...(value as any) };
|
|
216
|
+
for (const prop of SchemaAST.getPropertySignatures(encoded)) {
|
|
217
|
+
const key = prop.name.toString();
|
|
218
|
+
if (key in result) {
|
|
219
|
+
result[key] = decodeRefsFromSchema(prop.type, (result as any)[key], db);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
case 'TupleType': {
|
|
226
|
+
if (!Array.isArray(value)) {
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// For arrays, effect uses TupleType with empty elements and a single rest element.
|
|
231
|
+
if (encoded.elements.length === 0 && encoded.rest.length === 1) {
|
|
232
|
+
const elementType = encoded.rest[0].type;
|
|
233
|
+
return (value as unknown[]).map((item) => decodeRefsFromSchema(elementType, item, db));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
case 'Union': {
|
|
240
|
+
// Optional values are represented as union with undefined.
|
|
241
|
+
const nonUndefined = encoded.types.filter((t) => !SchemaAST.isUndefinedKeyword(t));
|
|
242
|
+
if (nonUndefined.length === 1) {
|
|
243
|
+
return decodeRefsFromSchema(nonUndefined[0], value, db);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// For other unions we can't safely pick a branch without validating.
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
case 'Suspend': {
|
|
251
|
+
return decodeRefsFromSchema(encoded.f(), value, db);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
case 'Refinement': {
|
|
255
|
+
return decodeRefsFromSchema(encoded.from, value, db);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
default: {
|
|
259
|
+
return value;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
};
|
|
@@ -6,28 +6,48 @@ import type * as Context from 'effect/Context';
|
|
|
6
6
|
import * as Effect from 'effect/Effect';
|
|
7
7
|
import * as Schema from 'effect/Schema';
|
|
8
8
|
|
|
9
|
+
import { type AiService } from '@dxos/ai';
|
|
9
10
|
import { Obj, Type } from '@dxos/echo';
|
|
10
|
-
import { type
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
14
|
-
import { type QueryResult } from '@dxos/protocols';
|
|
11
|
+
import { type Database } from '@dxos/echo';
|
|
12
|
+
import { assertArgument, failedInvariant } from '@dxos/invariant';
|
|
13
|
+
import { Operation } from '@dxos/operation';
|
|
15
14
|
|
|
16
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
type CredentialsService,
|
|
17
|
+
type FunctionInvocationService,
|
|
18
|
+
type QueueService,
|
|
19
|
+
type TracingService,
|
|
20
|
+
} from './services';
|
|
17
21
|
import { Function } from './types';
|
|
18
|
-
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './
|
|
22
|
+
import { getUserFunctionIdInMetadata, setUserFunctionIdInMetadata } from './types';
|
|
19
23
|
|
|
20
24
|
// TODO(burdon): Model after http request. Ref Lambda/OpenFaaS.
|
|
21
25
|
// https://docs.aws.amazon.com/lambda/latest/dg/typescript-handler.html
|
|
22
26
|
// https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/#functions
|
|
23
27
|
// https://www.npmjs.com/package/aws-lambda
|
|
24
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Services that are provided at the function call site by the caller.
|
|
31
|
+
*/
|
|
32
|
+
export type InvocationServices = TracingService;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Services that are available to invoked functions.
|
|
36
|
+
*/
|
|
37
|
+
export type FunctionServices =
|
|
38
|
+
| InvocationServices
|
|
39
|
+
| AiService.AiService
|
|
40
|
+
| CredentialsService
|
|
41
|
+
| Database.Service
|
|
42
|
+
| QueueService
|
|
43
|
+
| FunctionInvocationService;
|
|
44
|
+
|
|
25
45
|
/**
|
|
26
46
|
* Function handler.
|
|
27
47
|
*/
|
|
28
|
-
export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
48
|
+
export type FunctionHandler<TData = {}, TOutput = any, S extends FunctionServices = FunctionServices> = (params: {
|
|
29
49
|
/**
|
|
30
|
-
*
|
|
50
|
+
* Context available to the function.
|
|
31
51
|
*/
|
|
32
52
|
context: FunctionContext;
|
|
33
53
|
|
|
@@ -37,68 +57,37 @@ export type FunctionHandler<TData = {}, TOutput = any> = (params: {
|
|
|
37
57
|
* This will be the payload from the trigger or other data passed into the function in a workflow.
|
|
38
58
|
*/
|
|
39
59
|
data: TData;
|
|
40
|
-
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any,
|
|
60
|
+
}) => TOutput | Promise<TOutput> | Effect.Effect<TOutput, any, S>;
|
|
41
61
|
|
|
42
62
|
/**
|
|
43
63
|
* Function context.
|
|
44
64
|
*/
|
|
45
65
|
export interface FunctionContext {
|
|
46
|
-
|
|
47
|
-
* Space from which the function was invoked.
|
|
48
|
-
*/
|
|
49
|
-
space: SpaceAPI | undefined;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Resolves a service available to the function.
|
|
53
|
-
* @throws if the service is not available.
|
|
54
|
-
*/
|
|
55
|
-
getService: <T extends Context.Tag<any, any>>(tag: T) => Context.Tag.Service<T>;
|
|
56
|
-
|
|
57
|
-
getSpace: (spaceId: SpaceId) => Promise<SpaceAPI>;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface FunctionContextAi {
|
|
61
|
-
// TODO(dmaretskyi): Refer to cloudflare AI docs for more comprehensive typedefs.
|
|
62
|
-
run(model: string, inputs: any, options?: any): Promise<any>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
//
|
|
66
|
-
// API.
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
// TODO(dmaretskyi): Temporary API to get the queues working.
|
|
70
|
-
// TODO(dmaretskyi): To be replaced with integrating queues into echo.
|
|
71
|
-
export interface QueuesAPI {
|
|
72
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
73
|
-
insertIntoQueue(queue: DXN, objects: HasId[]): Promise<void>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Space interface available to functions.
|
|
78
|
-
*/
|
|
79
|
-
export interface SpaceAPI {
|
|
80
|
-
get id(): SpaceId;
|
|
81
|
-
get db(): EchoDatabase;
|
|
82
|
-
|
|
83
|
-
// TODO(dmaretskyi): Align with echo api: queues.get(id).append(items);
|
|
84
|
-
get queues(): QueuesAPI;
|
|
66
|
+
// TODO(dmaretskyi): Consider what we should put into context.
|
|
85
67
|
}
|
|
86
68
|
|
|
87
|
-
// TODO(wittjosiah): Queues are incompatible.
|
|
88
|
-
const __assertFunctionSpaceIsCompatibleWithTheClientSpace = () => {
|
|
89
|
-
// const _: SpaceAPI = {} as Space;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
69
|
const typeId = Symbol.for('@dxos/functions/FunctionDefinition');
|
|
93
70
|
|
|
94
|
-
export type FunctionDefinition<T = any, O = any> = {
|
|
71
|
+
export type FunctionDefinition<T = any, O = any, S extends FunctionServices = FunctionServices> = {
|
|
95
72
|
[typeId]: true;
|
|
96
73
|
key: string;
|
|
97
74
|
name: string;
|
|
98
75
|
description?: string;
|
|
99
76
|
inputSchema: Schema.Schema<T, any>;
|
|
100
77
|
outputSchema?: Schema.Schema<O, any>;
|
|
101
|
-
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* List of types the function uses.
|
|
81
|
+
* This is used to ensure that the types are available when the function is executed.
|
|
82
|
+
*/
|
|
83
|
+
types: readonly Type.Entity.Any[];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Keys of the required services.
|
|
87
|
+
*/
|
|
88
|
+
services: readonly string[];
|
|
89
|
+
|
|
90
|
+
handler: FunctionHandler<T, O, S>;
|
|
102
91
|
meta?: {
|
|
103
92
|
/**
|
|
104
93
|
* Tools that are projected from functions have this annotation.
|
|
@@ -113,19 +102,35 @@ export type FunctionDefinition<T = any, O = any> = {
|
|
|
113
102
|
};
|
|
114
103
|
};
|
|
115
104
|
|
|
105
|
+
export declare namespace FunctionDefinition {
|
|
106
|
+
export type Any = FunctionDefinition<any, any, any>;
|
|
107
|
+
export type Input<T extends Any> = T extends FunctionDefinition<infer I, infer _O, infer _S> ? I : never;
|
|
108
|
+
export type Output<T extends Any> = T extends FunctionDefinition<infer _I, infer O, infer _S> ? O : never;
|
|
109
|
+
export type Services<T extends Any> = T extends FunctionDefinition<infer _I, infer _O, infer S> ? S : never;
|
|
110
|
+
}
|
|
111
|
+
|
|
116
112
|
export type FunctionProps<T, O> = {
|
|
117
113
|
key: string;
|
|
118
114
|
name: string;
|
|
119
115
|
description?: string;
|
|
120
116
|
inputSchema: Schema.Schema<T, any>;
|
|
121
117
|
outputSchema?: Schema.Schema<O, any>;
|
|
122
|
-
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* List of types the function uses.
|
|
121
|
+
* This is used to ensure that the types are available when the function is executed.
|
|
122
|
+
*/
|
|
123
|
+
types?: readonly Type.Entity.Any[];
|
|
124
|
+
// TODO(dmaretskyi): This currently doesn't cause a compile-time error if the handler requests a service that is not specified
|
|
125
|
+
services?: readonly Context.Tag<any, any>[];
|
|
126
|
+
|
|
127
|
+
handler: FunctionHandler<T, O, FunctionServices>;
|
|
123
128
|
};
|
|
124
129
|
|
|
125
130
|
// TODO(dmaretskyi): Output type doesn't get typechecked.
|
|
126
131
|
export const defineFunction: {
|
|
127
|
-
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O>;
|
|
128
|
-
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler }) => {
|
|
132
|
+
<I, O>(params: FunctionProps<I, O>): FunctionDefinition<I, O, FunctionServices>;
|
|
133
|
+
} = ({ key, name, description, inputSchema, outputSchema = Schema.Any, handler, types, services }) => {
|
|
129
134
|
if (!Schema.isSchema(inputSchema)) {
|
|
130
135
|
throw new Error('Input schema must be a valid schema');
|
|
131
136
|
}
|
|
@@ -170,9 +175,68 @@ export const defineFunction: {
|
|
|
170
175
|
inputSchema,
|
|
171
176
|
outputSchema,
|
|
172
177
|
handler: handlerWithSpan,
|
|
178
|
+
types: types ?? [],
|
|
179
|
+
services: !services ? [] : getServiceKeys(services),
|
|
173
180
|
} satisfies FunctionDefinition.Any;
|
|
174
181
|
};
|
|
175
182
|
|
|
183
|
+
const getServiceKeys = (services: readonly Context.Tag<any, any>[]) => {
|
|
184
|
+
return services.map((tag: any) => {
|
|
185
|
+
if (typeof tag.key === 'string') {
|
|
186
|
+
return tag.key;
|
|
187
|
+
}
|
|
188
|
+
failedInvariant();
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Converts a FunctionDefinition to an OperationDefinition with handler.
|
|
194
|
+
* The function handler is adapted to the OperationHandler format.
|
|
195
|
+
*
|
|
196
|
+
* Note: FunctionDefinition stores service keys as strings, not Tag types,
|
|
197
|
+
* so we can't use Operation.withHandler's type inference here.
|
|
198
|
+
*/
|
|
199
|
+
export const toOperation = <T, O, S extends FunctionServices = FunctionServices>(
|
|
200
|
+
functionDef: FunctionDefinition<T, O, S>,
|
|
201
|
+
): Operation.Definition<T, O> & { handler: Operation.Handler<T, O, any, S> } => {
|
|
202
|
+
const op = Operation.make({
|
|
203
|
+
schema: {
|
|
204
|
+
input: functionDef.inputSchema,
|
|
205
|
+
output: functionDef.outputSchema ?? Schema.Any,
|
|
206
|
+
},
|
|
207
|
+
meta: {
|
|
208
|
+
key: functionDef.key,
|
|
209
|
+
name: functionDef.name,
|
|
210
|
+
description: functionDef.description,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
// Adapt FunctionHandler signature to OperationHandler format.
|
|
215
|
+
// FunctionHandler expects { context, data }, OperationHandler expects just input.
|
|
216
|
+
const operationHandler: Operation.Handler<T, O, any, S> = (input: T) => {
|
|
217
|
+
const result = functionDef.handler({
|
|
218
|
+
context: {} as FunctionContext,
|
|
219
|
+
data: input,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// Convert Promise or plain value to Effect.
|
|
223
|
+
if (Effect.isEffect(result)) {
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
if (result instanceof Promise) {
|
|
227
|
+
return Effect.tryPromise(() => result);
|
|
228
|
+
}
|
|
229
|
+
return Effect.succeed(result as O);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// Manually attach handler since FunctionDefinition stores service keys as strings,
|
|
233
|
+
// not Tag types, so withHandler's type inference doesn't apply.
|
|
234
|
+
return {
|
|
235
|
+
...op,
|
|
236
|
+
handler: operationHandler,
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
|
|
176
240
|
export const FunctionDefinition = {
|
|
177
241
|
make: defineFunction,
|
|
178
242
|
isFunction: (value: unknown): value is FunctionDefinition.Any => {
|
|
@@ -186,14 +250,10 @@ export const FunctionDefinition = {
|
|
|
186
250
|
assertArgument(Obj.instanceOf(Function.Function, functionObj), 'functionObj');
|
|
187
251
|
return deserializeFunction(functionObj);
|
|
188
252
|
},
|
|
253
|
+
toOperation,
|
|
189
254
|
};
|
|
190
|
-
export declare namespace FunctionDefinition {
|
|
191
|
-
export type Any = FunctionDefinition<any, any>;
|
|
192
|
-
export type Input<T extends FunctionDefinition> = T extends FunctionDefinition<infer I, any> ? I : never;
|
|
193
|
-
export type Output<T extends FunctionDefinition> = T extends FunctionDefinition<any, infer O> ? O : never;
|
|
194
|
-
}
|
|
195
255
|
|
|
196
|
-
export const serializeFunction = (functionDef: FunctionDefinition
|
|
256
|
+
export const serializeFunction = (functionDef: FunctionDefinition.Any): Function.Function => {
|
|
197
257
|
const fn = Function.make({
|
|
198
258
|
key: functionDef.key,
|
|
199
259
|
name: functionDef.name,
|
|
@@ -201,9 +261,10 @@ export const serializeFunction = (functionDef: FunctionDefinition<any, any>): Fu
|
|
|
201
261
|
description: functionDef.description,
|
|
202
262
|
inputSchema: Type.toJsonSchema(functionDef.inputSchema),
|
|
203
263
|
outputSchema: !functionDef.outputSchema ? undefined : Type.toJsonSchema(functionDef.outputSchema),
|
|
264
|
+
services: functionDef.services,
|
|
204
265
|
});
|
|
205
266
|
if (functionDef.meta?.deployedFunctionId) {
|
|
206
|
-
setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta
|
|
267
|
+
Obj.change(fn, (fn) => setUserFunctionIdInMetadata(Obj.getMeta(fn), functionDef.meta!.deployedFunctionId!));
|
|
207
268
|
}
|
|
208
269
|
return fn;
|
|
209
270
|
};
|
|
@@ -219,6 +280,8 @@ export const deserializeFunction = (functionObj: Function.Function): FunctionDef
|
|
|
219
280
|
outputSchema: !functionObj.outputSchema ? undefined : Type.toEffectSchema(functionObj.outputSchema),
|
|
220
281
|
// TODO(dmaretskyi): This should throw error.
|
|
221
282
|
handler: () => {},
|
|
283
|
+
services: functionObj.services ?? [],
|
|
284
|
+
types: [],
|
|
222
285
|
meta: {
|
|
223
286
|
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj)),
|
|
224
287
|
},
|
|
@@ -11,9 +11,8 @@ import * as Layer from 'effect/Layer';
|
|
|
11
11
|
import * as Redacted from 'effect/Redacted';
|
|
12
12
|
|
|
13
13
|
import { Query } from '@dxos/echo';
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
import { DatabaseService } from './database';
|
|
14
|
+
import { Database } from '@dxos/echo';
|
|
15
|
+
import { AccessToken } from '@dxos/types';
|
|
17
16
|
|
|
18
17
|
export type CredentialQuery = {
|
|
19
18
|
service?: string;
|
|
@@ -61,7 +60,12 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
61
60
|
static configuredLayer = (credentials: ServiceCredential[]) =>
|
|
62
61
|
Layer.succeed(CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
63
62
|
|
|
64
|
-
static layerConfig = (
|
|
63
|
+
static layerConfig = (
|
|
64
|
+
credentials: {
|
|
65
|
+
service: string;
|
|
66
|
+
apiKey: Config.Config<Redacted.Redacted<string>>;
|
|
67
|
+
}[],
|
|
68
|
+
) =>
|
|
65
69
|
Layer.effect(
|
|
66
70
|
CredentialsService,
|
|
67
71
|
Effect.gen(function* () {
|
|
@@ -78,20 +82,34 @@ export class CredentialsService extends Context.Tag('@dxos/functions/Credentials
|
|
|
78
82
|
}),
|
|
79
83
|
);
|
|
80
84
|
|
|
81
|
-
static layerFromDatabase = () =>
|
|
85
|
+
static layerFromDatabase = ({ caching = false }: { caching?: boolean } = {}) =>
|
|
82
86
|
Layer.effect(
|
|
83
87
|
CredentialsService,
|
|
84
88
|
Effect.gen(function* () {
|
|
85
|
-
const dbService = yield*
|
|
89
|
+
const dbService = yield* Database.Service;
|
|
90
|
+
const cache = new Map<string, ServiceCredential[]>();
|
|
91
|
+
|
|
86
92
|
const queryCredentials = async (query: CredentialQuery): Promise<ServiceCredential[]> => {
|
|
87
|
-
const
|
|
88
|
-
|
|
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
|
|
89
100
|
.filter((accessToken) => accessToken.source === query.service)
|
|
90
101
|
.map((accessToken) => ({
|
|
91
102
|
service: accessToken.source,
|
|
92
103
|
apiKey: accessToken.token,
|
|
93
104
|
}));
|
|
105
|
+
|
|
106
|
+
if (caching) {
|
|
107
|
+
cache.set(cacheKey, credentials);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return credentials;
|
|
94
111
|
};
|
|
112
|
+
|
|
95
113
|
return {
|
|
96
114
|
getCredential: async (query) => {
|
|
97
115
|
const credentials = await queryCredentials(query);
|
|
@@ -132,13 +150,10 @@ export class ConfiguredCredentialsService implements Context.Tag.Service<Credent
|
|
|
132
150
|
}
|
|
133
151
|
|
|
134
152
|
/**
|
|
135
|
-
* Maps the request to include the
|
|
153
|
+
* Maps the request to include the given token in the Authorization header.
|
|
136
154
|
*/
|
|
137
|
-
export const withAuthorization = (
|
|
138
|
-
HttpClient.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return HttpClientRequest.setHeader(request, 'Authorization', authorization);
|
|
143
|
-
}),
|
|
144
|
-
);
|
|
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
|
+
});
|
|
@@ -17,12 +17,18 @@ export const ComputeEventPayload = Schema.Union(
|
|
|
17
17
|
Schema.Struct({
|
|
18
18
|
type: Schema.Literal('begin-compute'),
|
|
19
19
|
nodeId: Schema.String,
|
|
20
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Names of the inputs begin computed.
|
|
22
|
+
*/
|
|
23
|
+
inputs: Schema.Array(Schema.String),
|
|
21
24
|
}),
|
|
22
25
|
Schema.Struct({
|
|
23
26
|
type: Schema.Literal('end-compute'),
|
|
24
27
|
nodeId: Schema.String,
|
|
25
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Names of the outputs computed.
|
|
30
|
+
*/
|
|
31
|
+
outputs: Schema.Array(Schema.String),
|
|
26
32
|
}),
|
|
27
33
|
Schema.Struct({
|
|
28
34
|
type: Schema.Literal('compute-input'),
|
|
@@ -46,7 +52,7 @@ export type ComputeEventPayload = Schema.Schema.Type<typeof ComputeEventPayload>
|
|
|
46
52
|
|
|
47
53
|
export const ComputeEvent = Schema.Struct({
|
|
48
54
|
payload: ComputeEventPayload,
|
|
49
|
-
}).pipe(Type.
|
|
55
|
+
}).pipe(Type.object({ typename: 'dxos.org/type/ComputeEvent', version: '0.1.0' }));
|
|
50
56
|
|
|
51
57
|
/**
|
|
52
58
|
* Logs event for the compute workflows.
|
|
@@ -69,7 +75,7 @@ export class ComputeEventLogger extends Context.Tag('@dxos/functions/ComputeEven
|
|
|
69
75
|
const tracing = yield* TracingService;
|
|
70
76
|
return {
|
|
71
77
|
log: (event: ComputeEventPayload) => {
|
|
72
|
-
tracing.write(Obj.make(ComputeEvent, { payload: event }));
|
|
78
|
+
tracing.write(Obj.make(ComputeEvent, { payload: event }), tracing.getTraceContext());
|
|
73
79
|
},
|
|
74
80
|
nodeId: undefined,
|
|
75
81
|
};
|