@dxos/functions 0.8.4-main.b97322e → 0.8.4-main.c4373fc
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 +61 -39
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/chunk-LKYT2SAL.mjs +665 -0
- package/dist/lib/browser/chunk-LKYT2SAL.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 +1035 -143
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +81 -41
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +60 -39
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- 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/chunk-NAQIKLZB.mjs +667 -0
- package/dist/lib/node-esm/chunk-NAQIKLZB.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 +1035 -143
- 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 +81 -41
- 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 +56 -9
- 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 +11 -6
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +20 -3
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +44 -6
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +68 -30
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +28 -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 +5 -3
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +34 -0
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -0
- package/dist/types/src/services/queues.d.ts +23 -6
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +22 -0
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -0
- package/dist/types/src/services/service-container.d.ts +7 -6
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +4 -2
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +39 -5
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +11 -3
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts +4 -4
- 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 +7 -18
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +21 -23
- 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 +37 -0
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -0
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +78 -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 +28 -0
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +71 -252
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +11 -7
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +38 -42
- package/src/bundler/bundler.test.ts +8 -9
- package/src/bundler/bundler.ts +38 -35
- package/src/edge/functions.ts +8 -5
- package/src/errors.ts +13 -5
- package/src/examples/fib.ts +32 -0
- package/src/examples/index.ts +7 -0
- package/src/examples/reply.ts +21 -0
- package/src/examples/sleep.ts +24 -0
- package/src/executor/executor.ts +14 -10
- package/src/handler.ts +125 -20
- package/src/index.ts +3 -3
- package/src/schema.ts +16 -2
- package/src/services/credentials.ts +86 -3
- package/src/services/database.ts +123 -18
- package/src/services/event-logger.ts +71 -37
- package/src/services/function-invocation-service.test.ts +81 -0
- package/src/services/function-invocation-service.ts +84 -0
- package/src/services/index.ts +5 -3
- package/src/services/local-function-execution.ts +153 -0
- package/src/services/queues.ts +39 -10
- package/src/services/remote-function-execution-service.ts +63 -0
- package/src/services/service-container.ts +13 -11
- package/src/services/service-registry.test.ts +4 -1
- package/src/services/service-registry.ts +11 -4
- package/src/services/tracing.ts +112 -11
- package/src/testing/layer.ts +87 -4
- package/src/testing/logger.ts +5 -4
- package/src/testing/persist-database.test.ts +87 -0
- package/src/testing/services.ts +11 -64
- package/src/trace.ts +19 -21
- package/src/triggers/index.ts +7 -0
- package/src/triggers/input-builder.ts +35 -0
- package/src/triggers/invocation-tracer.ts +101 -0
- package/src/triggers/trigger-dispatcher.test.ts +665 -0
- package/src/triggers/trigger-dispatcher.ts +533 -0
- package/src/triggers/trigger-state-store.ts +61 -0
- package/src/types.ts +44 -37
- package/src/url.ts +14 -11
- package/dist/lib/browser/chunk-3NGCSUEW.mjs +0 -328
- package/dist/lib/browser/chunk-3NGCSUEW.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs +0 -330
- package/dist/lib/node-esm/chunk-FJ2MU7TL.mjs.map +0 -7
- package/dist/types/src/services/function-call-service.d.ts +0 -16
- package/dist/types/src/services/function-call-service.d.ts.map +0 -1
- package/src/services/function-call-service.ts +0 -64
package/src/types.ts
CHANGED
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
7
|
|
|
7
|
-
import {
|
|
8
|
+
import { Obj, QueryAST, Type } from '@dxos/echo';
|
|
9
|
+
import { Expando, OptionsAnnotationId, RawObject, Ref } from '@dxos/echo/internal';
|
|
8
10
|
import { DXN } from '@dxos/keys';
|
|
9
11
|
|
|
10
12
|
import { FunctionType } from './schema';
|
|
@@ -14,13 +16,8 @@ import { FunctionType } from './schema';
|
|
|
14
16
|
* Every spec has a type field of type TriggerKind that we can use to understand which type we're working with.
|
|
15
17
|
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
16
18
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
Webhook = 'webhook',
|
|
20
|
-
Subscription = 'subscription',
|
|
21
|
-
Email = 'email',
|
|
22
|
-
Queue = 'queue',
|
|
23
|
-
}
|
|
19
|
+
export const TriggerKinds = ['timer', 'webhook', 'subscription', 'email', 'queue'] as const;
|
|
20
|
+
export type TriggerKind = (typeof TriggerKinds)[number];
|
|
24
21
|
|
|
25
22
|
const kindLiteralAnnotations = { title: 'Kind' };
|
|
26
23
|
|
|
@@ -28,7 +25,7 @@ const kindLiteralAnnotations = { title: 'Kind' };
|
|
|
28
25
|
* Cron timer.
|
|
29
26
|
*/
|
|
30
27
|
const TimerTriggerSchema = Schema.Struct({
|
|
31
|
-
kind: Schema.Literal(
|
|
28
|
+
kind: Schema.Literal('timer').annotations(kindLiteralAnnotations),
|
|
32
29
|
cron: Schema.String.annotations({
|
|
33
30
|
title: 'Cron',
|
|
34
31
|
[SchemaAST.ExamplesAnnotationId]: ['0 0 * * *'],
|
|
@@ -37,12 +34,14 @@ const TimerTriggerSchema = Schema.Struct({
|
|
|
37
34
|
export type TimerTrigger = Schema.Schema.Type<typeof TimerTriggerSchema>;
|
|
38
35
|
|
|
39
36
|
const EmailTriggerSchema = Schema.Struct({
|
|
40
|
-
kind: Schema.Literal(
|
|
37
|
+
kind: Schema.Literal('email').annotations(kindLiteralAnnotations),
|
|
41
38
|
}).pipe(Schema.mutable);
|
|
42
39
|
export type EmailTrigger = Schema.Schema.Type<typeof EmailTriggerSchema>;
|
|
43
40
|
|
|
44
41
|
const QueueTriggerSchema = Schema.Struct({
|
|
45
|
-
kind: Schema.Literal(
|
|
42
|
+
kind: Schema.Literal('queue').annotations(kindLiteralAnnotations),
|
|
43
|
+
|
|
44
|
+
// TODO(dmaretskyi): Change to a reference.
|
|
46
45
|
queue: DXN.Schema,
|
|
47
46
|
}).pipe(Schema.mutable);
|
|
48
47
|
export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
@@ -51,7 +50,7 @@ export type QueueTrigger = Schema.Schema.Type<typeof QueueTriggerSchema>;
|
|
|
51
50
|
* Webhook.
|
|
52
51
|
*/
|
|
53
52
|
const WebhookTriggerSchema = Schema.Struct({
|
|
54
|
-
kind: Schema.Literal(
|
|
53
|
+
kind: Schema.Literal('webhook').annotations(kindLiteralAnnotations),
|
|
55
54
|
method: Schema.optional(
|
|
56
55
|
Schema.String.annotations({
|
|
57
56
|
title: 'Method',
|
|
@@ -66,19 +65,15 @@ const WebhookTriggerSchema = Schema.Struct({
|
|
|
66
65
|
}).pipe(Schema.mutable);
|
|
67
66
|
export type WebhookTrigger = Schema.Schema.Type<typeof WebhookTriggerSchema>;
|
|
68
67
|
|
|
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
68
|
/**
|
|
76
69
|
* Subscription.
|
|
77
70
|
*/
|
|
78
71
|
const SubscriptionTriggerSchema = Schema.Struct({
|
|
79
|
-
kind: Schema.Literal(
|
|
80
|
-
|
|
81
|
-
|
|
72
|
+
kind: Schema.Literal('subscription').annotations(kindLiteralAnnotations),
|
|
73
|
+
query: Schema.Struct({
|
|
74
|
+
string: Schema.optional(Schema.String.annotations({ title: 'Query' })),
|
|
75
|
+
ast: QueryAST.Query,
|
|
76
|
+
}).pipe(Schema.mutable),
|
|
82
77
|
options: Schema.optional(
|
|
83
78
|
Schema.Struct({
|
|
84
79
|
// Watch changes to object (not just creation).
|
|
@@ -142,9 +137,23 @@ export const QueueTriggerOutput = Schema.mutable(
|
|
|
142
137
|
);
|
|
143
138
|
export type QueueTriggerOutput = Schema.Schema.Type<typeof QueueTriggerOutput>;
|
|
144
139
|
|
|
145
|
-
export const SubscriptionTriggerOutput = Schema.
|
|
146
|
-
|
|
147
|
-
|
|
140
|
+
export const SubscriptionTriggerOutput = Schema.Struct({
|
|
141
|
+
/**
|
|
142
|
+
* Type of the mutation.
|
|
143
|
+
*/
|
|
144
|
+
// TODO(dmaretskyi): Specify enum.
|
|
145
|
+
type: Schema.String,
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Reference to the object that was changed or created.
|
|
149
|
+
*/
|
|
150
|
+
subject: Type.Ref(Obj.Any),
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @deprecated
|
|
154
|
+
*/
|
|
155
|
+
changedObjectId: Schema.optional(Schema.String),
|
|
156
|
+
}).pipe(Schema.mutable);
|
|
148
157
|
export type SubscriptionTriggerOutput = Schema.Schema.Type<typeof SubscriptionTriggerOutput>;
|
|
149
158
|
|
|
150
159
|
export const TimerTriggerOutput = Schema.mutable(Schema.Struct({ tick: Schema.Number }));
|
|
@@ -155,7 +164,7 @@ export type TimerTriggerOutput = Schema.Schema.Type<typeof TimerTriggerOutput>;
|
|
|
155
164
|
* Function is invoked with the `payload` passed as input data.
|
|
156
165
|
* The event that triggers the function is available in the function context.
|
|
157
166
|
*/
|
|
158
|
-
|
|
167
|
+
const FunctionTrigger_ = Schema.Struct({
|
|
159
168
|
/**
|
|
160
169
|
* Function or workflow to invoke.
|
|
161
170
|
*/
|
|
@@ -185,17 +194,15 @@ export const FunctionTriggerSchema = Schema.Struct({
|
|
|
185
194
|
* }
|
|
186
195
|
*/
|
|
187
196
|
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) {}
|
|
197
|
+
}).pipe(
|
|
198
|
+
Type.Obj({
|
|
199
|
+
typename: 'dxos.org/type/FunctionTrigger',
|
|
200
|
+
version: '0.2.0',
|
|
201
|
+
}),
|
|
202
|
+
);
|
|
203
|
+
export interface FunctionTrigger extends Schema.Schema.Type<typeof FunctionTrigger_> {}
|
|
204
|
+
export interface FunctionTriggerEncoded extends Schema.Schema.Encoded<typeof FunctionTrigger_> {}
|
|
205
|
+
export const FunctionTrigger: Schema.Schema<FunctionTrigger, FunctionTriggerEncoded> = FunctionTrigger_;
|
|
199
206
|
|
|
200
207
|
// TODO(wittjosiah): Remove?
|
|
201
208
|
|
package/src/url.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type ObjectMeta } from '@dxos/echo
|
|
5
|
+
import { type ObjectMeta } from '@dxos/echo/internal';
|
|
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,328 +0,0 @@
|
|
|
1
|
-
// src/services/database.ts
|
|
2
|
-
import { Context, Effect, Layer } from "effect";
|
|
3
|
-
var DatabaseService = class _DatabaseService extends Context.Tag("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.resolve = Effect.fn(function* (dxn) {
|
|
22
|
-
const { db } = yield* _DatabaseService;
|
|
23
|
-
return yield* Effect.tryPromise({
|
|
24
|
-
try: () => db.graph.createRefResolver({
|
|
25
|
-
context: {
|
|
26
|
-
space: db.spaceId
|
|
27
|
-
}
|
|
28
|
-
}).resolve(dxn),
|
|
29
|
-
catch: (error) => error
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
static {
|
|
34
|
-
this.loadRef = Effect.fn(function* (ref) {
|
|
35
|
-
return yield* Effect.promise(() => ref.load());
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
static {
|
|
39
|
-
/**
|
|
40
|
-
* Creates a `QueryResult` object that can be subscribed to.
|
|
41
|
-
*/
|
|
42
|
-
this.query = (queryOrFilter) => _DatabaseService.pipe(Effect.map(({ db }) => db.query(queryOrFilter)), Effect.withSpan("DatabaseService.query"));
|
|
43
|
-
}
|
|
44
|
-
static {
|
|
45
|
-
/**
|
|
46
|
-
* Executes the query once and returns the results.
|
|
47
|
-
*/
|
|
48
|
-
this.runQuery = (queryOrFilter) => _DatabaseService.query(queryOrFilter).pipe(Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())));
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// src/services/queues.ts
|
|
53
|
-
import { Context as Context2, Layer as Layer2 } from "effect";
|
|
54
|
-
var QueueService = class _QueueService extends Context2.Tag("QueueService")() {
|
|
55
|
-
static {
|
|
56
|
-
this.notAvailable = Layer2.succeed(_QueueService, {
|
|
57
|
-
queues: {
|
|
58
|
-
get(dxn) {
|
|
59
|
-
throw new Error("Queues not available");
|
|
60
|
-
},
|
|
61
|
-
create() {
|
|
62
|
-
throw new Error("Queues not available");
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
contextQueue: void 0
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
static {
|
|
69
|
-
this.make = (queues, contextQueue) => {
|
|
70
|
-
return {
|
|
71
|
-
queues,
|
|
72
|
-
contextQueue
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
var ContextQueueService = class extends Context2.Tag("ContextQueueService")() {
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
// src/services/credentials.ts
|
|
81
|
-
import { Context as Context3, Effect as Effect2 } from "effect";
|
|
82
|
-
var CredentialsService = class _CredentialsService extends Context3.Tag("CredentialsService")() {
|
|
83
|
-
static {
|
|
84
|
-
this.getCredential = (query) => Effect2.gen(function* () {
|
|
85
|
-
const credentials = yield* _CredentialsService;
|
|
86
|
-
return yield* Effect2.promise(() => credentials.getCredential(query));
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
var ConfiguredCredentialsService = class {
|
|
91
|
-
constructor(credentials = []) {
|
|
92
|
-
this.credentials = credentials;
|
|
93
|
-
}
|
|
94
|
-
addCredentials(credentials) {
|
|
95
|
-
this.credentials.push(...credentials);
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
async queryCredentials(query) {
|
|
99
|
-
return this.credentials.filter((credential) => credential.service === query.service);
|
|
100
|
-
}
|
|
101
|
-
async getCredential(query) {
|
|
102
|
-
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
103
|
-
if (!credential) {
|
|
104
|
-
throw new Error(`Credential not found for service: ${query.service}`);
|
|
105
|
-
}
|
|
106
|
-
return credential;
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
// src/services/event-logger.ts
|
|
111
|
-
import { Effect as Effect3, Context as Context4 } from "effect";
|
|
112
|
-
import { invariant } from "@dxos/invariant";
|
|
113
|
-
import { log, LogLevel } from "@dxos/log";
|
|
114
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/services/event-logger.ts";
|
|
115
|
-
var EventLogger = class extends Context4.Tag("EventLogger")() {
|
|
116
|
-
static {
|
|
117
|
-
this.noop = {
|
|
118
|
-
log: () => {
|
|
119
|
-
},
|
|
120
|
-
nodeId: void 0
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
var logCustomEvent = (data) => Effect3.gen(function* () {
|
|
125
|
-
const logger = yield* EventLogger;
|
|
126
|
-
if (!logger.nodeId) {
|
|
127
|
-
throw new Error("logCustomEvent must be called within a node compute function");
|
|
128
|
-
}
|
|
129
|
-
logger.log({
|
|
130
|
-
type: "custom",
|
|
131
|
-
nodeId: logger.nodeId,
|
|
132
|
-
event: data
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
var createDefectLogger = () => Effect3.catchAll((error) => Effect3.gen(function* () {
|
|
136
|
-
log.error("unhandled effect error", {
|
|
137
|
-
error
|
|
138
|
-
}, {
|
|
139
|
-
F: __dxlog_file,
|
|
140
|
-
L: 65,
|
|
141
|
-
S: this,
|
|
142
|
-
C: (f, a) => f(...a)
|
|
143
|
-
});
|
|
144
|
-
throw error;
|
|
145
|
-
}));
|
|
146
|
-
var createEventLogger = (level, message = "event") => {
|
|
147
|
-
const logFunction = {
|
|
148
|
-
[LogLevel.WARN]: log.warn,
|
|
149
|
-
[LogLevel.VERBOSE]: log.verbose,
|
|
150
|
-
[LogLevel.DEBUG]: log.debug,
|
|
151
|
-
[LogLevel.INFO]: log.info,
|
|
152
|
-
[LogLevel.ERROR]: log.error
|
|
153
|
-
}[level];
|
|
154
|
-
invariant(logFunction, void 0, {
|
|
155
|
-
F: __dxlog_file,
|
|
156
|
-
L: 80,
|
|
157
|
-
S: void 0,
|
|
158
|
-
A: [
|
|
159
|
-
"logFunction",
|
|
160
|
-
""
|
|
161
|
-
]
|
|
162
|
-
});
|
|
163
|
-
return {
|
|
164
|
-
log: (event) => {
|
|
165
|
-
logFunction(message, event);
|
|
166
|
-
},
|
|
167
|
-
nodeId: void 0
|
|
168
|
-
};
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
// src/services/function-call-service.ts
|
|
172
|
-
import { Context as Context5 } from "effect";
|
|
173
|
-
var FunctionCallService = class extends Context5.Tag("FunctionCallService")() {
|
|
174
|
-
static fromClient(baseUrl, spaceId) {
|
|
175
|
-
return {
|
|
176
|
-
callFunction: async (deployedFunctionId, input) => {
|
|
177
|
-
const url = getInvocationUrl(deployedFunctionId, baseUrl, {
|
|
178
|
-
spaceId
|
|
179
|
-
});
|
|
180
|
-
const result = await fetch(url, {
|
|
181
|
-
method: "POST",
|
|
182
|
-
headers: {
|
|
183
|
-
"Content-Type": "application/json"
|
|
184
|
-
},
|
|
185
|
-
body: JSON.stringify(input)
|
|
186
|
-
});
|
|
187
|
-
if (result.status >= 300 || result.status < 200) {
|
|
188
|
-
throw new Error("Failed to invoke function", {
|
|
189
|
-
cause: new Error(`HTTP error: ${await result.text()}`)
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
return await result.json();
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
static {
|
|
197
|
-
this.mock = () => {
|
|
198
|
-
return {
|
|
199
|
-
callFunction: async (deployedFunctionId, input) => {
|
|
200
|
-
return input;
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
207
|
-
const baseUrl = new URL("functions/", edgeUrl);
|
|
208
|
-
const relativeUrl = functionUrl.replace(/^\//, "");
|
|
209
|
-
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
210
|
-
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
211
|
-
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
212
|
-
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
213
|
-
return url.toString();
|
|
214
|
-
};
|
|
215
|
-
var isSecure = (protocol) => {
|
|
216
|
-
return protocol === "https:" || protocol === "wss:";
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
// src/services/tracing.ts
|
|
220
|
-
import { Context as Context6, Effect as Effect4 } from "effect";
|
|
221
|
-
import { AgentStatus } from "@dxos/ai";
|
|
222
|
-
import { Obj } from "@dxos/echo";
|
|
223
|
-
var TracingService = class _TracingService extends Context6.Tag("TracingService")() {
|
|
224
|
-
static {
|
|
225
|
-
this.noop = {
|
|
226
|
-
write: () => {
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
static {
|
|
231
|
-
this.console = {
|
|
232
|
-
write: (event) => {
|
|
233
|
-
console.log(event);
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
static {
|
|
238
|
-
/**
|
|
239
|
-
* Emit the current human-readable execution status.
|
|
240
|
-
*/
|
|
241
|
-
this.emitStatus = Effect4.fnUntraced(function* (data) {
|
|
242
|
-
const tracing = yield* _TracingService;
|
|
243
|
-
tracing.write(Obj.make(AgentStatus, data));
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
// src/services/service-container.ts
|
|
249
|
-
import { Layer as Layer3 } from "effect";
|
|
250
|
-
import { AiService } from "@dxos/ai";
|
|
251
|
-
import { entries } from "@dxos/util";
|
|
252
|
-
var SERVICES = {
|
|
253
|
-
ai: AiService,
|
|
254
|
-
credentials: CredentialsService,
|
|
255
|
-
database: DatabaseService,
|
|
256
|
-
eventLogger: EventLogger,
|
|
257
|
-
functionCallService: FunctionCallService,
|
|
258
|
-
queues: QueueService,
|
|
259
|
-
tracing: TracingService
|
|
260
|
-
};
|
|
261
|
-
var SERVICE_MAPPING = Object.fromEntries(entries(SERVICES).map(([name, tag]) => [
|
|
262
|
-
tag.key,
|
|
263
|
-
name
|
|
264
|
-
]));
|
|
265
|
-
var SERVICE_TAGS = Object.values(SERVICES);
|
|
266
|
-
var DEFAULT_SERVICES = {
|
|
267
|
-
tracing: TracingService.noop
|
|
268
|
-
};
|
|
269
|
-
var ServiceContainer = class _ServiceContainer {
|
|
270
|
-
constructor() {
|
|
271
|
-
this._services = {
|
|
272
|
-
...DEFAULT_SERVICES
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Set services.
|
|
277
|
-
* @param services - Services to set.
|
|
278
|
-
* @returns The container instance.
|
|
279
|
-
*/
|
|
280
|
-
setServices(services) {
|
|
281
|
-
this._services = {
|
|
282
|
-
...this._services,
|
|
283
|
-
...services
|
|
284
|
-
};
|
|
285
|
-
return this;
|
|
286
|
-
}
|
|
287
|
-
getService(tag) {
|
|
288
|
-
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
289
|
-
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
290
|
-
if (!service) {
|
|
291
|
-
throw new Error(`Service not available: ${tag.key}`);
|
|
292
|
-
}
|
|
293
|
-
return service;
|
|
294
|
-
}
|
|
295
|
-
clone() {
|
|
296
|
-
return new _ServiceContainer().setServices({
|
|
297
|
-
...this._services
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
// 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.
|
|
301
|
-
createLayer() {
|
|
302
|
-
const ai = this._services.ai != null ? Layer3.succeed(AiService, this._services.ai) : AiService.notAvailable;
|
|
303
|
-
const credentials = Layer3.succeed(CredentialsService, this._services.credentials ?? new ConfiguredCredentialsService());
|
|
304
|
-
const database = this._services.database != null ? Layer3.succeed(DatabaseService, this._services.database) : DatabaseService.notAvailable;
|
|
305
|
-
const queues = this._services.queues != null ? Layer3.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
306
|
-
const tracing = Layer3.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
307
|
-
const eventLogger = Layer3.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
|
|
308
|
-
const functionCallService = Layer3.succeed(FunctionCallService, this._services.functionCallService ?? FunctionCallService.mock());
|
|
309
|
-
return Layer3.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
export {
|
|
314
|
-
DatabaseService,
|
|
315
|
-
QueueService,
|
|
316
|
-
ContextQueueService,
|
|
317
|
-
CredentialsService,
|
|
318
|
-
ConfiguredCredentialsService,
|
|
319
|
-
EventLogger,
|
|
320
|
-
logCustomEvent,
|
|
321
|
-
createDefectLogger,
|
|
322
|
-
createEventLogger,
|
|
323
|
-
FunctionCallService,
|
|
324
|
-
TracingService,
|
|
325
|
-
SERVICE_TAGS,
|
|
326
|
-
ServiceContainer
|
|
327
|
-
};
|
|
328
|
-
//# sourceMappingURL=chunk-3NGCSUEW.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../../src/services/database.ts", "../../../src/services/queues.ts", "../../../src/services/credentials.ts", "../../../src/services/event-logger.ts", "../../../src/services/function-call-service.ts", "../../../src/services/tracing.ts", "../../../src/services/service-container.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context, Effect, Layer } from 'effect';\n\nimport type { Filter, Live, Obj, Query, Ref, Relation } from '@dxos/echo';\nimport type { EchoDatabase, OneShotQueryResult, QueryResult } from '@dxos/echo-db';\nimport type { DXN } from '@dxos/keys';\n\nexport class DatabaseService extends Context.Tag('DatabaseService')<\n DatabaseService,\n {\n readonly db: EchoDatabase;\n }\n>() {\n static notAvailable = Layer.succeed(DatabaseService, {\n get db(): EchoDatabase {\n throw new Error('Database not available');\n },\n });\n\n static make = (db: EchoDatabase): Context.Tag.Service<DatabaseService> => {\n return {\n get db() {\n return db;\n },\n };\n };\n\n static resolve: (dxn: DXN) => Effect.Effect<Obj.Any | Relation.Any, Error, DatabaseService> = Effect.fn(\n function* (dxn) {\n const { db } = yield* DatabaseService;\n return yield* Effect.tryPromise({\n try: () =>\n db.graph.createRefResolver({ context: { space: db.spaceId } }).resolve(dxn) as Promise<\n Obj.Any | Relation.Any\n >,\n catch: (error) => error as Error,\n });\n },\n );\n\n static loadRef: <T>(ref: Ref.Ref<T>) => Effect.Effect<T, never, never> = Effect.fn(function* (ref) {\n return yield* Effect.promise(() => ref.load());\n });\n\n /**\n * Creates a `QueryResult` object that can be subscribed to.\n */\n static query: {\n <Q extends Query.Any>(query: Q): Effect.Effect<QueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;\n <F extends Filter.Any>(filter: F): Effect.Effect<QueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;\n } = (queryOrFilter: Query.Any | Filter.Any) =>\n DatabaseService.pipe(\n Effect.map(({ db }) => db.query(queryOrFilter as any)),\n Effect.withSpan('DatabaseService.query'),\n );\n\n /**\n * Executes the query once and returns the results.\n */\n static runQuery: {\n <Q extends Query.Any>(query: Q): Effect.Effect<OneShotQueryResult<Live<Query.Type<Q>>>, never, DatabaseService>;\n <F extends Filter.Any>(filter: F): Effect.Effect<OneShotQueryResult<Live<Filter.Type<F>>>, never, DatabaseService>;\n } = (queryOrFilter: Query.Any | Filter.Any) =>\n DatabaseService.query(queryOrFilter as any).pipe(\n Effect.flatMap((queryResult) => Effect.promise(() => queryResult.run())),\n );\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context, Layer } from 'effect';\n\nimport type { Queue, QueueAPI, QueueFactory } from '@dxos/echo-db';\n\n/**\n * Gives access to all queues.\n */\nexport class QueueService extends Context.Tag('QueueService')<\n QueueService,\n {\n /**\n * API to access the queues.\n */\n readonly queues: QueueAPI;\n\n /**\n * The queue that is used to store the context of the current research.\n * @deprecated Use `ContextQueueService` instead.\n */\n readonly contextQueue: Queue | undefined;\n }\n>() {\n static notAvailable = Layer.succeed(QueueService, {\n queues: {\n get(dxn) {\n throw new Error('Queues not available');\n },\n create() {\n throw new Error('Queues not available');\n },\n },\n contextQueue: undefined,\n });\n\n static make = (queues: QueueFactory, contextQueue?: Queue): Context.Tag.Service<QueueService> => {\n return {\n queues,\n contextQueue,\n };\n };\n}\n\n/**\n * Gives access to a specific queue passed as a context.\n */\nexport class ContextQueueService extends Context.Tag('ContextQueueService')<\n ContextQueueService,\n {\n readonly contextQueue: Queue;\n }\n>() {}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context, Effect } from 'effect';\n\ntype CredentialQuery = {\n service?: string;\n};\n\n// TODO(dmaretskyi): Unify with other apis.\n// packages/sdk/schema/src/common/access-token.ts\nexport type ServiceCredential = {\n service: string;\n\n // TODO(dmaretskyi): Build out.\n apiKey?: string;\n};\n\nexport class CredentialsService extends Context.Tag('CredentialsService')<\n CredentialsService,\n {\n /**\n * Query all.\n */\n queryCredentials: (query: CredentialQuery) => Promise<ServiceCredential[]>;\n\n /**\n * Get a single credential.\n * @throws {Error} If no credential is found.\n */\n getCredential: (query: CredentialQuery) => Promise<ServiceCredential>;\n }\n>() {\n static getCredential = (query: CredentialQuery): Effect.Effect<ServiceCredential, never, CredentialsService> =>\n Effect.gen(function* () {\n const credentials = yield* CredentialsService;\n return yield* Effect.promise(() => credentials.getCredential(query));\n });\n}\n\nexport class ConfiguredCredentialsService implements Context.Tag.Service<CredentialsService> {\n constructor(private readonly credentials: ServiceCredential[] = []) {}\n\n addCredentials(credentials: ServiceCredential[]): ConfiguredCredentialsService {\n this.credentials.push(...credentials);\n return this;\n }\n\n async queryCredentials(query: CredentialQuery): Promise<ServiceCredential[]> {\n return this.credentials.filter((credential) => credential.service === query.service);\n }\n\n async getCredential(query: CredentialQuery): Promise<ServiceCredential> {\n const credential = this.credentials.find((credential) => credential.service === query.service);\n if (!credential) {\n throw new Error(`Credential not found for service: ${query.service}`);\n }\n return credential;\n }\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Effect, Context } from 'effect';\n\nimport { invariant } from '@dxos/invariant';\nimport { log, LogLevel } from '@dxos/log';\n\nexport type ComputeEvent =\n | {\n type: 'begin-compute';\n nodeId: string;\n inputs: Record<string, any>;\n }\n | {\n type: 'end-compute';\n nodeId: string;\n outputs: Record<string, any>;\n }\n | {\n type: 'compute-input';\n nodeId: string;\n property: string;\n value: any;\n }\n | {\n type: 'compute-output';\n nodeId: string;\n property: string;\n value: any;\n }\n | {\n type: 'custom';\n nodeId: string;\n event: any;\n };\n\nexport class EventLogger extends Context.Tag('EventLogger')<\n EventLogger,\n { readonly log: (event: ComputeEvent) => void; readonly nodeId: string | undefined }\n>() {\n static noop: Context.Tag.Service<EventLogger> = {\n log: () => {},\n nodeId: undefined,\n };\n}\n\nexport const logCustomEvent = (data: any) =>\n Effect.gen(function* () {\n const logger = yield* EventLogger;\n if (!logger.nodeId) {\n throw new Error('logCustomEvent must be called within a node compute function');\n }\n logger.log({\n type: 'custom',\n nodeId: logger.nodeId,\n event: data,\n });\n });\n\nexport const createDefectLogger = <A, E, R>(): ((self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>) =>\n Effect.catchAll((error) =>\n Effect.gen(function* () {\n log.error('unhandled effect error', { error });\n throw error;\n }),\n );\n\nexport const createEventLogger = (level: LogLevel, message: string = 'event'): Context.Tag.Service<EventLogger> => {\n const logFunction = (\n {\n [LogLevel.WARN]: log.warn,\n [LogLevel.VERBOSE]: log.verbose,\n [LogLevel.DEBUG]: log.debug,\n [LogLevel.INFO]: log.info,\n [LogLevel.ERROR]: log.error,\n } as any\n )[level];\n invariant(logFunction);\n return {\n log: (event: ComputeEvent) => {\n logFunction(message, event);\n },\n nodeId: undefined,\n };\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context } from 'effect';\n\nimport type { SpaceId } from '@dxos/keys';\n\n/**\n * Allows calling into other functions.\n */\nexport class FunctionCallService extends Context.Tag('FunctionCallService')<\n FunctionCallService,\n {\n callFunction(deployedFunctionId: string, input: any, spaceId?: SpaceId): Promise<any>;\n }\n>() {\n static fromClient(baseUrl: string, spaceId: SpaceId): Context.Tag.Service<FunctionCallService> {\n return {\n callFunction: async (deployedFunctionId: string, input: any) => {\n const url = getInvocationUrl(deployedFunctionId, baseUrl, { spaceId });\n const result = await fetch(url, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify(input),\n });\n if (result.status >= 300 || result.status < 200) {\n throw new Error('Failed to invoke function', { cause: new Error(`HTTP error: ${await result.text()}`) });\n }\n return await result.json();\n },\n };\n }\n\n static mock = () => {\n return {\n callFunction: async (deployedFunctionId: string, input: any) => {\n return input;\n },\n };\n };\n}\n\n// TODO(dmaretskyi): Reconcile with `getInvocationUrl` in `@dxos/functions/edge`.\nconst getInvocationUrl = (functionUrl: string, edgeUrl: string, options: InvocationOptions = {}) => {\n const baseUrl = new URL('functions/', edgeUrl);\n\n // Leading slashes cause the URL to be treated as an absolute path.\n const relativeUrl = functionUrl.replace(/^\\//, '');\n const url = new URL(`./${relativeUrl}`, baseUrl.toString());\n options.spaceId && url.searchParams.set('spaceId', options.spaceId);\n options.subjectId && url.searchParams.set('subjectId', options.subjectId);\n url.protocol = isSecure(url.protocol) ? 'https' : 'http';\n return url.toString();\n};\n\nconst isSecure = (protocol: string) => {\n return protocol === 'https:' || protocol === 'wss:';\n};\n\ntype InvocationOptions = {\n spaceId?: SpaceId;\n subjectId?: string;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context, Effect } from 'effect';\n\nimport { AgentStatus } from '@dxos/ai';\nimport { Obj } from '@dxos/echo';\nimport type { AnyEchoObject } from '@dxos/echo-schema';\n\n/**\n * Provides a way for compute primitives (functions, workflows, tools)\n * to emit an execution trace as a series of structured ECHO objects.\n */\nexport class TracingService extends Context.Tag('TracingService')<\n TracingService,\n {\n /**\n * Write an event to the tracing queue.\n * @param event - The event to write. Must be an a typed object.\n */\n write(event: AnyEchoObject): void;\n }\n>() {\n static noop: Context.Tag.Service<TracingService> = { write: () => {} };\n\n static console: Context.Tag.Service<TracingService> = {\n write: (event) => {\n // eslint-disable-next-line no-console\n console.log(event);\n },\n };\n\n /**\n * Emit the current human-readable execution status.\n */\n static emitStatus: (data: Obj.MakeProps<typeof AgentStatus>) => Effect.Effect<unknown, never, void> =\n Effect.fnUntraced(function* (data) {\n const tracing = yield* TracingService;\n tracing.write(Obj.make(AgentStatus, data));\n });\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { type Context, Layer } from 'effect';\n\nimport { AiService } from '@dxos/ai';\nimport { entries } from '@dxos/util';\n\nimport { ConfiguredCredentialsService, CredentialsService } from './credentials';\nimport { DatabaseService } from './database';\nimport { EventLogger } from './event-logger';\nimport { FunctionCallService } from './function-call-service';\nimport { QueueService } from './queues';\nimport { TracingService } from './tracing';\n\n// TODO(dmaretskyi): Refactor this module to only rely on tags and not the human-assigned names.\n\n/**\n * List of all services.\n */\nconst SERVICES = {\n ai: AiService,\n credentials: CredentialsService,\n database: DatabaseService,\n eventLogger: EventLogger,\n functionCallService: FunctionCallService,\n queues: QueueService,\n tracing: TracingService,\n} as const satisfies Record<string, Context.TagClass<any, string, any>>;\n\n/**\n * Mapping of service names to their tags.\n */\nexport type ServiceTagRecord = {\n [K in keyof typeof SERVICES]: (typeof SERVICES)[K] extends { new (_: never): infer T } ? T : never;\n};\n\n/**\n * Mapping of service names to their runtime types.\n */\nexport type ServiceRecord = {\n [K in keyof ServiceTagRecord]: Context.Tag.Service<ServiceTagRecord[K]>;\n};\n\n/**\n * Union of all services tags.\n */\nexport type Services = ServiceTagRecord[keyof ServiceTagRecord];\n\nconst SERVICE_MAPPING: Record<string, keyof ServiceRecord> = Object.fromEntries(\n entries(SERVICES).map(([name, tag]) => [tag.key, name]),\n);\n\nexport const SERVICE_TAGS: Context.Tag<any, any>[] = Object.values(SERVICES);\n\nconst DEFAULT_SERVICES: Partial<ServiceRecord> = {\n tracing: TracingService.noop,\n};\n\n/**\n * @deprecated\n */\nexport class ServiceContainer {\n private _services: Partial<ServiceRecord> = { ...DEFAULT_SERVICES };\n\n /**\n * Set services.\n * @param services - Services to set.\n * @returns The container instance.\n */\n setServices(services: Partial<ServiceRecord>): this {\n this._services = { ...this._services, ...services };\n return this;\n }\n\n getService<Id, T>(tag: Context.Tag<Id, T>): T {\n const serviceKey = SERVICE_MAPPING[tag.key];\n const service = serviceKey != null ? this._services[serviceKey] : undefined;\n if (!service) {\n throw new Error(`Service not available: ${tag.key}`);\n }\n\n return service as T;\n }\n\n clone(): ServiceContainer {\n return new ServiceContainer().setServices({ ...this._services });\n }\n\n // 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.\n createLayer(): Layer.Layer<Services> {\n const ai = this._services.ai != null ? Layer.succeed(AiService, this._services.ai) : AiService.notAvailable;\n const credentials = Layer.succeed(\n CredentialsService,\n this._services.credentials ?? new ConfiguredCredentialsService(),\n );\n const database =\n this._services.database != null\n ? Layer.succeed(DatabaseService, this._services.database)\n : DatabaseService.notAvailable;\n const queues =\n this._services.queues != null ? Layer.succeed(QueueService, this._services.queues) : QueueService.notAvailable;\n const tracing = Layer.succeed(TracingService, this._services.tracing ?? TracingService.noop);\n const eventLogger = Layer.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);\n const functionCallService = Layer.succeed(\n FunctionCallService,\n this._services.functionCallService ?? FunctionCallService.mock(),\n );\n\n return Layer.mergeAll(ai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AQrC,WAAW,KAAK0B,UAAU3B,EAAE,IAAIC,UAAUsC;AAC/F,UAAMrC,cAAcmC,OAAMC,QACxBnC,oBACA,KAAKwB,UAAUzB,eAAe,IAAIsC,6BAAAA,CAAAA;AAEpC,UAAMpC,WACJ,KAAKuB,UAAUvB,YAAY,OACvBiC,OAAMC,QAAQjC,iBAAiB,KAAKsB,UAAUvB,QAAQ,IACtDC,gBAAgBkC;AACtB,UAAM7B,SACJ,KAAKiB,UAAUjB,UAAU,OAAO2B,OAAMC,QAAQ3B,cAAc,KAAKgB,UAAUjB,MAAM,IAAIC,aAAa4B;AACpG,UAAM3B,UAAUyB,OAAMC,QAAQzB,gBAAgB,KAAKc,UAAUf,WAAWC,eAAeY,IAAI;AAC3F,UAAMnB,cAAc+B,OAAMC,QAAQ/B,aAAa,KAAKoB,UAAUrB,eAAeC,YAAYkB,IAAI;AAC7F,UAAMjB,sBAAsB6B,OAAMC,QAChC7B,qBACA,KAAKkB,UAAUnB,uBAAuBC,oBAAoBgC,KAAI,CAAA;AAGhE,WAAOJ,OAAMK,SAAS1C,IAAIE,aAAaE,UAAUM,QAAQE,SAASN,aAAaE,mBAAAA;EACjF;AACF;",
|
|
6
|
-
"names": ["Context", "Effect", "Layer", "DatabaseService", "Context", "Tag", "notAvailable", "Layer", "succeed", "db", "Error", "make", "resolve", "Effect", "fn", "dxn", "tryPromise", "try", "graph", "createRefResolver", "context", "space", "spaceId", "catch", "error", "loadRef", "ref", "promise", "load", "query", "queryOrFilter", "pipe", "map", "withSpan", "runQuery", "flatMap", "queryResult", "run", "Context", "Layer", "QueueService", "Context", "Tag", "notAvailable", "Layer", "succeed", "queues", "get", "dxn", "Error", "create", "contextQueue", "undefined", "make", "ContextQueueService", "Context", "Effect", "CredentialsService", "Context", "Tag", "getCredential", "query", "Effect", "gen", "credentials", "promise", "ConfiguredCredentialsService", "addCredentials", "push", "queryCredentials", "filter", "credential", "service", "find", "Error", "Effect", "Context", "invariant", "log", "LogLevel", "EventLogger", "Tag", "noop", "nodeId", "undefined", "logCustomEvent", "data", "gen", "logger", "Error", "type", "event", "createDefectLogger", "catchAll", "error", "createEventLogger", "level", "message", "logFunction", "WARN", "warn", "VERBOSE", "verbose", "DEBUG", "debug", "INFO", "info", "ERROR", "Context", "FunctionCallService", "Context", "Tag", "fromClient", "baseUrl", "spaceId", "callFunction", "deployedFunctionId", "input", "url", "getInvocationUrl", "result", "fetch", "method", "headers", "body", "JSON", "stringify", "status", "Error", "cause", "text", "json", "mock", "functionUrl", "edgeUrl", "options", "URL", "relativeUrl", "replace", "toString", "searchParams", "set", "subjectId", "protocol", "isSecure", "Context", "Effect", "AgentStatus", "Obj", "TracingService", "Context", "Tag", "noop", "write", "console", "event", "log", "emitStatus", "Effect", "fnUntraced", "data", "tracing", "Obj", "make", "AgentStatus", "Layer", "AiService", "entries", "SERVICES", "ai", "AiService", "credentials", "CredentialsService", "database", "DatabaseService", "eventLogger", "EventLogger", "functionCallService", "FunctionCallService", "queues", "QueueService", "tracing", "TracingService", "SERVICE_MAPPING", "Object", "fromEntries", "entries", "map", "name", "tag", "key", "SERVICE_TAGS", "values", "DEFAULT_SERVICES", "noop", "ServiceContainer", "_services", "setServices", "services", "getService", "serviceKey", "service", "undefined", "Error", "clone", "createLayer", "Layer", "succeed", "notAvailable", "ConfiguredCredentialsService", "mock", "mergeAll"]
|
|
7
|
-
}
|