@dxos/functions 0.8.4-main.67995b8 → 0.8.4-main.a4bbb77
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/bundler/index.mjs +56 -38
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-C2Z7LCJ2.mjs +649 -0
- package/dist/lib/browser/chunk-C2Z7LCJ2.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/edge/index.mjs +22 -8
- package/dist/lib/browser/edge/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +992 -127
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +76 -6
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +55 -38
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-AH3AZM2U.mjs +651 -0
- package/dist/lib/node-esm/chunk-AH3AZM2U.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/edge/index.mjs +21 -8
- package/dist/lib/node-esm/edge/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +992 -127
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +76 -6
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/bundler/bundler.d.ts +11 -12
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/edge/functions.d.ts +3 -2
- package/dist/types/src/edge/functions.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +89 -20
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/examples/fib.d.ts +7 -0
- package/dist/types/src/examples/fib.d.ts.map +1 -0
- package/dist/types/src/examples/index.d.ts +4 -0
- package/dist/types/src/examples/index.d.ts.map +1 -0
- package/dist/types/src/examples/reply.d.ts +3 -0
- package/dist/types/src/examples/reply.d.ts.map +1 -0
- package/dist/types/src/examples/sleep.d.ts +5 -0
- package/dist/types/src/examples/sleep.d.ts.map +1 -0
- package/dist/types/src/executor/executor.d.ts +7 -1
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +52 -8
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +6 -1
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +15 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +39 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +1 -1
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +26 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +4 -3
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +23 -2
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +19 -5
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +9 -4
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
- package/dist/types/src/services/service-container.d.ts +1 -1
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +34 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +9 -2
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/persist-database.test.d.ts +2 -0
- package/dist/types/src/testing/persist-database.test.d.ts.map +1 -0
- package/dist/types/src/testing/services.d.ts +1 -1
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +20 -22
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/triggers/index.d.ts +4 -0
- package/dist/types/src/triggers/index.d.ts.map +1 -0
- package/dist/types/src/triggers/input-builder.d.ts +3 -0
- package/dist/types/src/triggers/input-builder.d.ts.map +1 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts +35 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +74 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts +2 -0
- package/dist/types/src/triggers/trigger-dispatcher.test.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts +27 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +60 -250
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +10 -6
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +43 -43
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +32 -33
- package/src/edge/functions.ts +8 -5
- package/src/errors.ts +13 -5
- package/src/examples/fib.ts +31 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +19 -0
- package/src/examples/sleep.ts +23 -0
- package/src/executor/executor.ts +12 -9
- package/src/handler.ts +120 -18
- package/src/index.ts +3 -3
- package/src/schema.ts +11 -0
- package/src/services/credentials.ts +80 -5
- package/src/services/database.ts +115 -18
- package/src/services/event-logger.ts +2 -2
- package/src/services/function-invocation-service.test.ts +79 -0
- package/src/services/function-invocation-service.ts +82 -0
- package/src/services/index.ts +4 -3
- package/src/services/local-function-execution.ts +97 -17
- package/src/services/queues.ts +34 -10
- package/src/services/remote-function-execution-service.ts +38 -43
- package/src/services/service-container.ts +4 -3
- package/src/services/service-registry.ts +1 -1
- package/src/services/tracing.ts +106 -9
- package/src/testing/layer.ts +84 -3
- package/src/testing/logger.ts +1 -1
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +3 -2
- package/src/trace.ts +17 -19
- package/src/triggers/index.ts +7 -0
- package/src/triggers/input-builder.ts +35 -0
- package/src/triggers/invocation-tracer.ts +99 -0
- package/src/triggers/trigger-dispatcher.test.ts +651 -0
- package/src/triggers/trigger-dispatcher.ts +522 -0
- package/src/triggers/trigger-state-store.ts +60 -0
- package/src/types.ts +39 -36
- package/src/url.ts +13 -10
- package/dist/lib/browser/chunk-6PTFLPCO.mjs +0 -462
- package/dist/lib/browser/chunk-6PTFLPCO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs +0 -464
- package/dist/lib/node-esm/chunk-NYJ2TSXO.mjs.map +0 -7
package/src/types.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import { Schema, SchemaAST } from 'effect';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { Obj, QueryAST, Type } from '@dxos/echo';
|
|
8
|
+
import { Expando, OptionsAnnotationId, RawObject, Ref } from '@dxos/echo-schema';
|
|
8
9
|
import { DXN } from '@dxos/keys';
|
|
9
10
|
|
|
10
11
|
import { FunctionType } from './schema';
|
|
@@ -14,13 +15,8 @@ import { FunctionType } from './schema';
|
|
|
14
15
|
* Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
|
|
15
16
|
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
16
17
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
Webhook = 'webhook',
|
|
20
|
-
Subscription = 'subscription',
|
|
21
|
-
Email = 'email',
|
|
22
|
-
Queue = 'queue',
|
|
23
|
-
}
|
|
18
|
+
export const TriggerKinds = ['timer', 'webhook', 'subscription', 'email', 'queue'] as const;
|
|
19
|
+
export type TriggerKind = (typeof TriggerKinds)[number];
|
|
24
20
|
|
|
25
21
|
const kindLiteralAnnotations = { title: 'Kind' };
|
|
26
22
|
|
|
@@ -28,7 +24,7 @@ const kindLiteralAnnotations = { title: 'Kind' };
|
|
|
28
24
|
* Cron timer.
|
|
29
25
|
*/
|
|
30
26
|
const TimerTriggerSchema = Schema.Struct({
|
|
31
|
-
kind: Schema.Literal(
|
|
27
|
+
kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
|
|
32
28
|
cron: Schema.String.annotations({
|
|
33
29
|
title: 'Cron',
|
|
34
30
|
[SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
|
|
@@ -37,12 +33,14 @@ const TimerTriggerSchema = Schema.Struct({
|
|
|
37
33
|
export type TimerTrigger = Schema.Schema.Type<typeof TimerTriggerSchema>;
|
|
38
34
|
|
|
39
35
|
const EmailTriggerSchema = Schema.Struct({
|
|
40
|
-
kind: Schema.Literal(
|
|
36
|
+
kind: Schema.Literal('email').annotations(kindLiteralAnnotations),
|
|
41
37
|
}).pipe(Schema.mutable);
|
|
42
38
|
export type EmailTrigger = Schema.Schema.Type<typeof EmailTriggerSchema>;
|
|
43
39
|
|
|
44
40
|
const QueueTriggerSchema = Schema.Struct({
|
|
45
|
-
kind: Schema.Literal(
|
|
41
|
+
kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
|
|
42
|
+
|
|
43
|
+
// TODO(dmaretskyi): Change to a reference.
|
|
46
44
|
queue: DXN.Schema,
|
|
47
45
|
}).pipe(Schema.mutable);
|
|
48
46
|
export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
@@ -51,7 +49,7 @@ export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
|
51
49
|
* Webhook.
|
|
52
50
|
*/
|
|
53
51
|
const WebhookTriggerSchema = Schema.Struct({
|
|
54
|
-
kind: Schema.Literal(
|
|
52
|
+
kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
|
|
55
53
|
method: Schema.optional(
|
|
56
54
|
Schema.String.annotations({
|
|
57
55
|
title: 'Method',
|
|
@@ -66,19 +64,12 @@ const WebhookTriggerSchema = Schema.Struct({
|
|
|
66
64
|
}).pipe(Schema.mutable);
|
|
67
65
|
export type WebhookTrigger = Schema.Schema.Type<typeof WebhookTriggerSchema>;
|
|
68
66
|
|
|
69
|
-
// TODO(burdon): Use ECHO definition (from https://github.com/dxos/dxos/pull/8233).
|
|
70
|
-
const QuerySchema = Schema.Struct({
|
|
71
|
-
type: Schema.optional(Schema.String.annotations({ title: 'Type' })),
|
|
72
|
-
props: Schema.optional(Schema.Record({ key: Schema.String, value: Schema.Any })),
|
|
73
|
-
}).annotations({ title: 'Query' });
|
|
74
|
-
|
|
75
67
|
/**
|
|
76
68
|
* Subscription.
|
|
77
69
|
*/
|
|
78
70
|
const SubscriptionTriggerSchema = Schema.Struct({
|
|
79
|
-
kind: Schema.Literal(
|
|
80
|
-
|
|
81
|
-
filter: QuerySchema,
|
|
71
|
+
kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
|
|
72
|
+
query: QueryAST.Query.annotations({ title: 'Query' }),
|
|
82
73
|
options: Schema.optional(
|
|
83
74
|
Schema.Struct({
|
|
84
75
|
// Watch changes to object (not just creation).
|
|
@@ -142,9 +133,23 @@ export const QueueTriggerOutput = Schema.mutable(
|
|
|
142
133
|
);
|
|
143
134
|
export type QueueTriggerOutput = Schema.Schema.Type<typeof QueueTriggerOutput>;
|
|
144
135
|
|
|
145
|
-
export const SubscriptionTriggerOutput = Schema.
|
|
146
|
-
|
|
147
|
-
|
|
136
|
+
export const SubscriptionTriggerOutput = Schema.Struct({
|
|
137
|
+
/**
|
|
138
|
+
* Type of the mutation.
|
|
139
|
+
*/
|
|
140
|
+
// TODO(dmaretskyi): Specify enum.
|
|
141
|
+
type: Schema.String,
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Reference to the object that was changed or created.
|
|
145
|
+
*/
|
|
146
|
+
subject: Type.Ref(Obj.Any),
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @deprecated
|
|
150
|
+
*/
|
|
151
|
+
changedObjectId: Schema.optional(Schema.String),
|
|
152
|
+
}).pipe(Schema.mutable);
|
|
148
153
|
export type SubscriptionTriggerOutput = Schema.Schema.Type<typeof SubscriptionTriggerOutput>;
|
|
149
154
|
|
|
150
155
|
export const TimerTriggerOutput = Schema.mutable(Schema.Struct({ tick: Schema.Number }));
|
|
@@ -155,7 +160,7 @@ export type TimerTriggerOutput = Schema.Schema.Type<typeof TimerTriggerOutput>;
|
|
|
155
160
|
* Function is invoked with the `payload` passed as input data.
|
|
156
161
|
* The event that triggers the function is available in the function context.
|
|
157
162
|
*/
|
|
158
|
-
|
|
163
|
+
const FunctionTrigger_ = Schema.Struct({
|
|
159
164
|
/**
|
|
160
165
|
* Function or workflow to invoke.
|
|
161
166
|
*/
|
|
@@ -185,17 +190,15 @@ export const FunctionTriggerSchema = Schema.Struct({
|
|
|
185
190
|
* }
|
|
186
191
|
*/
|
|
187
192
|
input: Schema.optional(Schema.mutable(Schema.Record({ key: Schema.String, value: Schema.Any }))),
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
export
|
|
196
|
-
|
|
197
|
-
version: '0.2.0',
|
|
198
|
-
})(FunctionTriggerSchema.fields) {}
|
|
193
|
+
}).pipe(
|
|
194
|
+
Type.Obj({
|
|
195
|
+
typename: 'dxos.org/type/FunctionTrigger',
|
|
196
|
+
version: '0.2.0',
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
export interface FunctionTrigger extends Schema.Schema.Type<typeof FunctionTrigger_> {}
|
|
200
|
+
export interface FunctionTriggerEncoded extends Schema.Schema.Encoded<typeof FunctionTrigger_> {}
|
|
201
|
+
export const FunctionTrigger: Schema.Schema<FunctionTrigger, FunctionTriggerEncoded> = FunctionTrigger_;
|
|
199
202
|
|
|
200
203
|
// TODO(wittjosiah): Remove?
|
|
201
204
|
|
package/src/url.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { type ObjectMeta } from '@dxos/echo-schema';
|
|
|
6
6
|
import { type SpaceId } from '@dxos/keys';
|
|
7
7
|
|
|
8
8
|
// TODO: use URL scheme for source?
|
|
9
|
-
const FUNCTIONS_META_KEY = 'dxos.org/service/function';
|
|
9
|
+
export const FUNCTIONS_META_KEY = 'dxos.org/service/function';
|
|
10
10
|
|
|
11
11
|
export const FUNCTIONS_PRESET_META_KEY = 'dxos.org/service/function-preset';
|
|
12
12
|
|
|
@@ -14,32 +14,35 @@ const isSecure = (protocol: string) => {
|
|
|
14
14
|
return protocol === 'https:' || protocol === 'wss:';
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* NOTE: functionId is backend ID, not ECHO object id.
|
|
19
|
+
*/
|
|
20
|
+
export const getUserFunctionIdInMetadata = (meta: ObjectMeta) => {
|
|
18
21
|
return meta.keys.find((key) => key.source === FUNCTIONS_META_KEY)?.id;
|
|
19
22
|
};
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
/**
|
|
25
|
+
* NOTE: functionId is backend ID, not ECHO object id.
|
|
26
|
+
*/
|
|
27
|
+
export const setUserFunctionIdInMetadata = (meta: ObjectMeta, functionId: string) => {
|
|
22
28
|
const key = meta.keys.find((key) => key.source === FUNCTIONS_META_KEY);
|
|
23
29
|
if (key) {
|
|
24
|
-
if (key.id !==
|
|
30
|
+
if (key.id !== functionId) {
|
|
25
31
|
throw new Error('Metadata mismatch');
|
|
26
32
|
}
|
|
27
33
|
} else {
|
|
28
|
-
meta.keys.push({ source: FUNCTIONS_META_KEY, id:
|
|
34
|
+
meta.keys.push({ source: FUNCTIONS_META_KEY, id: functionId });
|
|
29
35
|
}
|
|
30
36
|
};
|
|
31
37
|
|
|
32
38
|
/**
|
|
33
39
|
* NOTE: functionId is backend ID, not ECHO object id.
|
|
34
40
|
*/
|
|
35
|
-
export const
|
|
36
|
-
|
|
37
|
-
export const getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {
|
|
41
|
+
export const getInvocationUrl = (functionId: string, edgeUrl: string, options: InvocationOptions = {}) => {
|
|
38
42
|
const baseUrl = new URL('functions/', edgeUrl);
|
|
39
43
|
|
|
40
44
|
// Leading slashes cause the URL to be treated as an absolute path.
|
|
41
|
-
const
|
|
42
|
-
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
45
|
+
const url = new URL(`./${functionId}`, baseUrl.toString());
|
|
43
46
|
options.spaceId && url.searchParams.set('spaceId', options.spaceId);
|
|
44
47
|
options.subjectId && url.searchParams.set('subjectId', options.subjectId);
|
|
45
48
|
url.protocol = isSecure(url.protocol) ? 'https' : 'http';
|
|
@@ -1,462 +0,0 @@
|
|
|
1
|
-
// src/services/database.ts
|
|
2
|
-
import { Context, Effect, Layer } from "effect";
|
|
3
|
-
var DatabaseService = class _DatabaseService extends Context.Tag("@dxos/functions/DatabaseService")() {
|
|
4
|
-
static {
|
|
5
|
-
this.notAvailable = Layer.succeed(_DatabaseService, {
|
|
6
|
-
get db() {
|
|
7
|
-
throw new Error("Database not available");
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
static {
|
|
12
|
-
this.make = (db) => {
|
|
13
|
-
return {
|
|
14
|
-
get db() {
|
|
15
|
-
return db;
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
static {
|
|
21
|
-
this.makeLayer = (db) => {
|
|
22
|
-
return Layer.succeed(_DatabaseService, _DatabaseService.make(db));
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
static {
|
|
26
|
-
this.resolve = Effect.fn(function* (dxn) {
|
|
27
|
-
const { db } = yield* _DatabaseService;
|
|
28
|
-
return yield* Effect.tryPromise({
|
|
29
|
-
try: () => db.graph.createRefResolver({
|
|
30
|
-
context: {
|
|
31
|
-
space: db.spaceId
|
|
32
|
-
}
|
|
33
|
-
}).resolve(dxn),
|
|
34
|
-
catch: (error) => error
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
static {
|
|
39
|
-
this.loadRef = Effect.fn(function* (ref) {
|
|
40
|
-
return yield* Effect.promise(() => ref.load());
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
static {
|
|
44
|
-
/**
|
|
45
|
-
* Creates a `QueryResult` object that can be subscribed to.
|
|
46
|
-
*/
|
|
47
|
-
this.query = (queryOrFilter) => _DatabaseService.pipe(Effect.map(({ db }) => db.query(queryOrFilter)), Effect.withSpan("DatabaseService.query"));
|
|
48
|
-
}
|
|
49
|
-
static {
|
|
50
|
-
/**
|
|
51
|
-
* Executes the query once and returns the results.
|
|
52
|
-
*/
|
|
53
|
-
this.runQuery = (queryOrFilter) => _DatabaseService.query(queryOrFilter).pipe(Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())));
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
// src/services/queues.ts
|
|
58
|
-
import { Context as Context2, Layer as Layer2 } from "effect";
|
|
59
|
-
var QueueService = class _QueueService extends Context2.Tag("@dxos/functions/QueueService")() {
|
|
60
|
-
static {
|
|
61
|
-
this.notAvailable = Layer2.succeed(_QueueService, {
|
|
62
|
-
queues: {
|
|
63
|
-
get(dxn) {
|
|
64
|
-
throw new Error("Queues not available");
|
|
65
|
-
},
|
|
66
|
-
create() {
|
|
67
|
-
throw new Error("Queues not available");
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
contextQueue: void 0
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
static {
|
|
74
|
-
this.make = (queues, contextQueue) => {
|
|
75
|
-
return {
|
|
76
|
-
queues,
|
|
77
|
-
contextQueue
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
static {
|
|
82
|
-
this.makeLayer = (queues, contextQueue) => Layer2.succeed(_QueueService, _QueueService.make(queues, contextQueue));
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
var ContextQueueService = class extends Context2.Tag("@dxos/functions/ContextQueueService")() {
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
// src/services/credentials.ts
|
|
89
|
-
import { Context as Context3, Effect as Effect2, Layer as Layer3 } from "effect";
|
|
90
|
-
var CredentialsService = class _CredentialsService extends Context3.Tag("@dxos/functions/CredentialsService")() {
|
|
91
|
-
static {
|
|
92
|
-
this.configuredLayer = (credentials) => Layer3.succeed(_CredentialsService, new ConfiguredCredentialsService(credentials));
|
|
93
|
-
}
|
|
94
|
-
static {
|
|
95
|
-
this.getCredential = (query) => Effect2.gen(function* () {
|
|
96
|
-
const credentials = yield* _CredentialsService;
|
|
97
|
-
return yield* Effect2.promise(() => credentials.getCredential(query));
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
var ConfiguredCredentialsService = class {
|
|
102
|
-
constructor(credentials = []) {
|
|
103
|
-
this.credentials = credentials;
|
|
104
|
-
}
|
|
105
|
-
addCredentials(credentials) {
|
|
106
|
-
this.credentials.push(...credentials);
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
async queryCredentials(query) {
|
|
110
|
-
return this.credentials.filter((credential) => credential.service === query.service);
|
|
111
|
-
}
|
|
112
|
-
async getCredential(query) {
|
|
113
|
-
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
114
|
-
if (!credential) {
|
|
115
|
-
throw new Error(`Credential not found for service: ${query.service}`);
|
|
116
|
-
}
|
|
117
|
-
return credential;
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
// src/services/tracing.ts
|
|
122
|
-
import { Context as Context4, Effect as Effect3, Layer as Layer4 } from "effect";
|
|
123
|
-
import { AgentStatus } from "@dxos/ai";
|
|
124
|
-
import { Obj } from "@dxos/echo";
|
|
125
|
-
var TracingService = class _TracingService extends Context4.Tag("@dxos/functions/TracingService")() {
|
|
126
|
-
static {
|
|
127
|
-
this.noop = {
|
|
128
|
-
write: () => {
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
static {
|
|
133
|
-
this.layerNoop = Layer4.succeed(_TracingService, _TracingService.noop);
|
|
134
|
-
}
|
|
135
|
-
static {
|
|
136
|
-
this.console = {
|
|
137
|
-
write: (event) => {
|
|
138
|
-
console.log(event);
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
static {
|
|
143
|
-
/**
|
|
144
|
-
* Emit the current human-readable execution status.
|
|
145
|
-
*/
|
|
146
|
-
this.emitStatus = Effect3.fnUntraced(function* (data) {
|
|
147
|
-
const tracing = yield* _TracingService;
|
|
148
|
-
tracing.write(Obj.make(AgentStatus, data));
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
// src/services/event-logger.ts
|
|
154
|
-
import { Effect as Effect4, Context as Context5, Schema, Layer as Layer5 } from "effect";
|
|
155
|
-
import { Obj as Obj2, Type } from "@dxos/echo";
|
|
156
|
-
import { invariant } from "@dxos/invariant";
|
|
157
|
-
import { log, LogLevel } from "@dxos/log";
|
|
158
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/services/event-logger.ts";
|
|
159
|
-
var ComputeEventPayload = Schema.Union(Schema.Struct({
|
|
160
|
-
type: Schema.Literal("begin-compute"),
|
|
161
|
-
nodeId: Schema.String,
|
|
162
|
-
inputs: Schema.Record({
|
|
163
|
-
key: Schema.String,
|
|
164
|
-
value: Schema.Any
|
|
165
|
-
})
|
|
166
|
-
}), Schema.Struct({
|
|
167
|
-
type: Schema.Literal("end-compute"),
|
|
168
|
-
nodeId: Schema.String,
|
|
169
|
-
outputs: Schema.Record({
|
|
170
|
-
key: Schema.String,
|
|
171
|
-
value: Schema.Any
|
|
172
|
-
})
|
|
173
|
-
}), Schema.Struct({
|
|
174
|
-
type: Schema.Literal("compute-input"),
|
|
175
|
-
nodeId: Schema.String,
|
|
176
|
-
property: Schema.String,
|
|
177
|
-
value: Schema.Any
|
|
178
|
-
}), Schema.Struct({
|
|
179
|
-
type: Schema.Literal("compute-output"),
|
|
180
|
-
nodeId: Schema.String,
|
|
181
|
-
property: Schema.String,
|
|
182
|
-
value: Schema.Any
|
|
183
|
-
}), Schema.Struct({
|
|
184
|
-
type: Schema.Literal("custom"),
|
|
185
|
-
nodeId: Schema.String,
|
|
186
|
-
event: Schema.Any
|
|
187
|
-
}));
|
|
188
|
-
var ComputeEvent = Schema.Struct({
|
|
189
|
-
payload: ComputeEventPayload
|
|
190
|
-
}).pipe(Type.Obj({
|
|
191
|
-
typename: "dxos.org/type/ComputeEvent",
|
|
192
|
-
version: "0.1.0"
|
|
193
|
-
}));
|
|
194
|
-
var ComputeEventLogger = class _ComputeEventLogger extends Context5.Tag("@dxos/functions/ComputeEventLogger")() {
|
|
195
|
-
static {
|
|
196
|
-
this.noop = {
|
|
197
|
-
log: () => {
|
|
198
|
-
},
|
|
199
|
-
nodeId: void 0
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
static {
|
|
203
|
-
/**
|
|
204
|
-
* Implements ComputeEventLogger using TracingService.
|
|
205
|
-
*/
|
|
206
|
-
this.layerFromTracing = Layer5.effect(_ComputeEventLogger, Effect4.gen(function* () {
|
|
207
|
-
const tracing = yield* TracingService;
|
|
208
|
-
return {
|
|
209
|
-
log: (event) => {
|
|
210
|
-
tracing.write(Obj2.make(ComputeEvent, {
|
|
211
|
-
payload: event
|
|
212
|
-
}));
|
|
213
|
-
},
|
|
214
|
-
nodeId: void 0
|
|
215
|
-
};
|
|
216
|
-
}));
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
var logCustomEvent = (data) => Effect4.gen(function* () {
|
|
220
|
-
const logger = yield* ComputeEventLogger;
|
|
221
|
-
if (!logger.nodeId) {
|
|
222
|
-
throw new Error("logCustomEvent must be called within a node compute function");
|
|
223
|
-
}
|
|
224
|
-
logger.log({
|
|
225
|
-
type: "custom",
|
|
226
|
-
nodeId: logger.nodeId,
|
|
227
|
-
event: data
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
var createDefectLogger = () => Effect4.catchAll((error) => Effect4.gen(function* () {
|
|
231
|
-
log.error("unhandled effect error", {
|
|
232
|
-
error
|
|
233
|
-
}, {
|
|
234
|
-
F: __dxlog_file,
|
|
235
|
-
L: 93,
|
|
236
|
-
S: this,
|
|
237
|
-
C: (f, a) => f(...a)
|
|
238
|
-
});
|
|
239
|
-
throw error;
|
|
240
|
-
}));
|
|
241
|
-
var createEventLogger = (level, message = "event") => {
|
|
242
|
-
const logFunction = {
|
|
243
|
-
[LogLevel.WARN]: log.warn,
|
|
244
|
-
[LogLevel.VERBOSE]: log.verbose,
|
|
245
|
-
[LogLevel.DEBUG]: log.debug,
|
|
246
|
-
[LogLevel.INFO]: log.info,
|
|
247
|
-
[LogLevel.ERROR]: log.error
|
|
248
|
-
}[level];
|
|
249
|
-
invariant(logFunction, void 0, {
|
|
250
|
-
F: __dxlog_file,
|
|
251
|
-
L: 111,
|
|
252
|
-
S: void 0,
|
|
253
|
-
A: [
|
|
254
|
-
"logFunction",
|
|
255
|
-
""
|
|
256
|
-
]
|
|
257
|
-
});
|
|
258
|
-
return {
|
|
259
|
-
log: (event) => {
|
|
260
|
-
logFunction(message, event);
|
|
261
|
-
},
|
|
262
|
-
nodeId: void 0
|
|
263
|
-
};
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
// src/services/remote-function-execution-service.ts
|
|
267
|
-
import { Context as Context6, Layer as Layer6 } from "effect";
|
|
268
|
-
var RemoteFunctionExecutionService = class _RemoteFunctionExecutionService extends Context6.Tag("@dxos/functions/RemoteFunctionExecutionService")() {
|
|
269
|
-
static fromClient(baseUrl, spaceId) {
|
|
270
|
-
return {
|
|
271
|
-
callFunction: async (deployedFunctionId, input) => {
|
|
272
|
-
const url = getInvocationUrl(deployedFunctionId, baseUrl, {
|
|
273
|
-
spaceId
|
|
274
|
-
});
|
|
275
|
-
const result = await fetch(url, {
|
|
276
|
-
method: "POST",
|
|
277
|
-
headers: {
|
|
278
|
-
"Content-Type": "application/json"
|
|
279
|
-
},
|
|
280
|
-
body: JSON.stringify(input)
|
|
281
|
-
});
|
|
282
|
-
if (result.status >= 300 || result.status < 200) {
|
|
283
|
-
throw new Error("Failed to invoke function", {
|
|
284
|
-
cause: new Error(`HTTP error: ${await result.text()}`)
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
return await result.json();
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
static {
|
|
292
|
-
this.mock = () => {
|
|
293
|
-
return {
|
|
294
|
-
callFunction: async (deployedFunctionId, input) => {
|
|
295
|
-
return input;
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
static {
|
|
301
|
-
this.mockLayer = Layer6.succeed(_RemoteFunctionExecutionService, _RemoteFunctionExecutionService.mock());
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
305
|
-
const baseUrl = new URL("functions/", edgeUrl);
|
|
306
|
-
const relativeUrl = functionUrl.replace(/^\//, "");
|
|
307
|
-
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
308
|
-
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
309
|
-
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
310
|
-
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
311
|
-
return url.toString();
|
|
312
|
-
};
|
|
313
|
-
var isSecure = (protocol) => {
|
|
314
|
-
return protocol === "https:" || protocol === "wss:";
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
// src/services/service-container.ts
|
|
318
|
-
import { Layer as Layer7 } from "effect";
|
|
319
|
-
import { AiService } from "@dxos/ai";
|
|
320
|
-
import { entries } from "@dxos/util";
|
|
321
|
-
var SERVICES = {
|
|
322
|
-
ai: AiService,
|
|
323
|
-
credentials: CredentialsService,
|
|
324
|
-
database: DatabaseService,
|
|
325
|
-
eventLogger: ComputeEventLogger,
|
|
326
|
-
functionCallService: RemoteFunctionExecutionService,
|
|
327
|
-
queues: QueueService,
|
|
328
|
-
tracing: TracingService
|
|
329
|
-
};
|
|
330
|
-
var SERVICE_MAPPING = Object.fromEntries(entries(SERVICES).map(([name, tag]) => [
|
|
331
|
-
tag.key,
|
|
332
|
-
name
|
|
333
|
-
]));
|
|
334
|
-
var SERVICE_TAGS = Object.values(SERVICES);
|
|
335
|
-
var DEFAULT_SERVICES = {
|
|
336
|
-
tracing: TracingService.noop
|
|
337
|
-
};
|
|
338
|
-
var ServiceContainer = class _ServiceContainer {
|
|
339
|
-
constructor() {
|
|
340
|
-
this._services = {
|
|
341
|
-
...DEFAULT_SERVICES
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Set services.
|
|
346
|
-
* @param services - Services to set.
|
|
347
|
-
* @returns The container instance.
|
|
348
|
-
*/
|
|
349
|
-
setServices(services) {
|
|
350
|
-
this._services = {
|
|
351
|
-
...this._services,
|
|
352
|
-
...services
|
|
353
|
-
};
|
|
354
|
-
return this;
|
|
355
|
-
}
|
|
356
|
-
getService(tag) {
|
|
357
|
-
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
358
|
-
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
359
|
-
if (!service) {
|
|
360
|
-
throw new Error(`Service not available: ${tag.key}`);
|
|
361
|
-
}
|
|
362
|
-
return service;
|
|
363
|
-
}
|
|
364
|
-
clone() {
|
|
365
|
-
return new _ServiceContainer().setServices({
|
|
366
|
-
...this._services
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
// TODO(dmaretskyi): `getService` is designed to error at runtime if the service is not available, but layer forces us to provide all services and makes stubs for the ones that are not available.
|
|
370
|
-
createLayer() {
|
|
371
|
-
const ai = this._services.ai != null ? Layer7.succeed(AiService, this._services.ai) : AiService.notAvailable;
|
|
372
|
-
const credentials = Layer7.succeed(CredentialsService, this._services.credentials ?? new ConfiguredCredentialsService());
|
|
373
|
-
const database = this._services.database != null ? Layer7.succeed(DatabaseService, this._services.database) : DatabaseService.notAvailable;
|
|
374
|
-
const queues = this._services.queues != null ? Layer7.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
375
|
-
const tracing = Layer7.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
376
|
-
const eventLogger = Layer7.succeed(ComputeEventLogger, this._services.eventLogger ?? ComputeEventLogger.noop);
|
|
377
|
-
const functionCallService = Layer7.succeed(RemoteFunctionExecutionService, this._services.functionCallService ?? RemoteFunctionExecutionService.mock());
|
|
378
|
-
return Layer7.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
379
|
-
}
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
// src/errors.ts
|
|
383
|
-
import { BaseError } from "@dxos/errors";
|
|
384
|
-
var ServiceNotAvailableError = class extends BaseError.extend("SERVICE_NOT_AVAILABLE") {
|
|
385
|
-
constructor(serviceName) {
|
|
386
|
-
super(`Service not available: ${serviceName}`);
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
var FunctionError = class extends BaseError.extend("FUNCTION_ERROR") {
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
// src/services/local-function-execution.ts
|
|
393
|
-
import { Context as Context7, Effect as Effect5, Layer as Layer8, Schema as Schema2 } from "effect";
|
|
394
|
-
import { todo } from "@dxos/debug";
|
|
395
|
-
var LocalFunctionExecutionService = class _LocalFunctionExecutionService extends Context7.Tag("@dxos/functions/LocalFunctionExecutionService")() {
|
|
396
|
-
static {
|
|
397
|
-
this.layer = Layer8.succeed(_LocalFunctionExecutionService, {
|
|
398
|
-
invokeFunction: (fnDef, input) => invokeFunction(fnDef, input)
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
var invokeFunction = (fnDef, input) => Effect5.gen(function* () {
|
|
403
|
-
const assertInput = fnDef.inputSchema.pipe(Schema2.asserts);
|
|
404
|
-
assertInput(input);
|
|
405
|
-
const context = {
|
|
406
|
-
getService: () => todo(),
|
|
407
|
-
getSpace: async (_spaceId) => {
|
|
408
|
-
throw new Error("Not available. Use the database service instead.");
|
|
409
|
-
},
|
|
410
|
-
space: void 0,
|
|
411
|
-
get ai() {
|
|
412
|
-
throw new Error("Not available. Use the ai service instead.");
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
const data = yield* Effect5.gen(function* () {
|
|
416
|
-
const result = fnDef.handler({
|
|
417
|
-
context,
|
|
418
|
-
data: input
|
|
419
|
-
});
|
|
420
|
-
if (Effect5.isEffect(result)) {
|
|
421
|
-
return yield* result.pipe(Effect5.orDie);
|
|
422
|
-
} else if (typeof result === "object" && result !== null && "then" in result && typeof result.then === "function") {
|
|
423
|
-
return yield* Effect5.promise(() => result);
|
|
424
|
-
} else {
|
|
425
|
-
return result;
|
|
426
|
-
}
|
|
427
|
-
}).pipe(Effect5.orDie, Effect5.catchAllDefect((defect) => Effect5.die(new FunctionError("Error running function", {
|
|
428
|
-
context: {
|
|
429
|
-
name: fnDef.name
|
|
430
|
-
},
|
|
431
|
-
cause: defect
|
|
432
|
-
}))));
|
|
433
|
-
const assertOutput = fnDef.outputSchema?.pipe(Schema2.asserts);
|
|
434
|
-
assertOutput(data);
|
|
435
|
-
return data;
|
|
436
|
-
}).pipe(Effect5.withSpan("invokeFunction", {
|
|
437
|
-
attributes: {
|
|
438
|
-
name: fnDef.name
|
|
439
|
-
}
|
|
440
|
-
}));
|
|
441
|
-
|
|
442
|
-
export {
|
|
443
|
-
DatabaseService,
|
|
444
|
-
QueueService,
|
|
445
|
-
ContextQueueService,
|
|
446
|
-
CredentialsService,
|
|
447
|
-
ConfiguredCredentialsService,
|
|
448
|
-
TracingService,
|
|
449
|
-
ComputeEventPayload,
|
|
450
|
-
ComputeEvent,
|
|
451
|
-
ComputeEventLogger,
|
|
452
|
-
logCustomEvent,
|
|
453
|
-
createDefectLogger,
|
|
454
|
-
createEventLogger,
|
|
455
|
-
RemoteFunctionExecutionService,
|
|
456
|
-
SERVICE_TAGS,
|
|
457
|
-
ServiceContainer,
|
|
458
|
-
ServiceNotAvailableError,
|
|
459
|
-
FunctionError,
|
|
460
|
-
LocalFunctionExecutionService
|
|
461
|
-
};
|
|
462
|
-
//# sourceMappingURL=chunk-6PTFLPCO.mjs.map
|