@dxos/functions-runtime-cloudflare 0.8.4-main.3c1ae3b → 0.8.4-main.40e3dcdf1b
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 +523 -233
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +523 -233
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/functions-client.d.ts +1 -0
- package/dist/types/src/functions-client.d.ts.map +1 -1
- package/dist/types/src/internal/data-service-impl.d.ts +8 -7
- package/dist/types/src/internal/data-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/query-service-impl.d.ts +3 -9
- package/dist/types/src/internal/query-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/queue-service-impl.d.ts +8 -9
- package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/service-container.d.ts +8 -8
- package/dist/types/src/internal/service-container.d.ts.map +1 -1
- package/dist/types/src/internal/utils.d.ts +2 -0
- package/dist/types/src/internal/utils.d.ts.map +1 -0
- package/dist/types/src/logger.d.ts.map +1 -1
- package/dist/types/src/queues-api.d.ts +6 -6
- package/dist/types/src/queues-api.d.ts.map +1 -1
- package/dist/types/src/space-proxy.d.ts +3 -2
- package/dist/types/src/space-proxy.d.ts.map +1 -1
- package/dist/types/src/wrap-handler-for-cloudflare.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -16
- package/src/functions-client.ts +9 -2
- package/src/internal/data-service-impl.ts +28 -11
- package/src/internal/query-service-impl.ts +8 -58
- package/src/internal/queue-service-impl.ts +37 -22
- package/src/internal/service-container.ts +46 -11
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +2 -0
- package/src/queues-api.ts +6 -6
- package/src/space-proxy.ts +4 -3
- package/src/wrap-handler-for-cloudflare.ts +3 -3
- package/dist/types/src/internal/adapter.d.ts +0 -12
- package/dist/types/src/internal/adapter.d.ts.map +0 -1
- package/src/internal/adapter.ts +0 -48
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/functions-runtime-cloudflare",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.40e3dcdf1b",
|
|
4
4
|
"description": "Functions runtime for Cloudflare.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"sideEffects": false,
|
|
@@ -23,23 +27,23 @@
|
|
|
23
27
|
"src"
|
|
24
28
|
],
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"effect": "3.
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/debug": "0.8.4-main.
|
|
30
|
-
"@dxos/echo": "0.8.4-main.
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/echo-db": "0.8.4-main.
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/log": "0.8.4-main.
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/keys": "0.8.4-main.
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/util": "0.8.4-main.
|
|
30
|
+
"effect": "3.19.16",
|
|
31
|
+
"@dxos/context": "0.8.4-main.40e3dcdf1b",
|
|
32
|
+
"@dxos/codec-protobuf": "0.8.4-main.40e3dcdf1b",
|
|
33
|
+
"@dxos/debug": "0.8.4-main.40e3dcdf1b",
|
|
34
|
+
"@dxos/echo": "0.8.4-main.40e3dcdf1b",
|
|
35
|
+
"@dxos/echo-protocol": "0.8.4-main.40e3dcdf1b",
|
|
36
|
+
"@dxos/echo-db": "0.8.4-main.40e3dcdf1b",
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.40e3dcdf1b",
|
|
38
|
+
"@dxos/log": "0.8.4-main.40e3dcdf1b",
|
|
39
|
+
"@dxos/errors": "0.8.4-main.40e3dcdf1b",
|
|
40
|
+
"@dxos/keys": "0.8.4-main.40e3dcdf1b",
|
|
41
|
+
"@dxos/protocols": "0.8.4-main.40e3dcdf1b",
|
|
42
|
+
"@dxos/util": "0.8.4-main.40e3dcdf1b"
|
|
39
43
|
},
|
|
40
44
|
"devDependencies": {
|
|
41
|
-
"@cloudflare/workers-types": "4.
|
|
42
|
-
"@dxos/types": "0.8.4-main.
|
|
45
|
+
"@cloudflare/workers-types": "^4.20260302.0",
|
|
46
|
+
"@dxos/types": "0.8.4-main.40e3dcdf1b"
|
|
43
47
|
},
|
|
44
48
|
"publishConfig": {
|
|
45
49
|
"access": "public"
|
package/src/functions-client.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { SpaceProxy } from './space-proxy';
|
|
|
14
14
|
type Services = {
|
|
15
15
|
dataService: EdgeFunctionEnv.DataService;
|
|
16
16
|
queueService: EdgeFunctionEnv.QueueService;
|
|
17
|
+
functionsAiService: EdgeFunctionEnv.FunctionsAiService;
|
|
17
18
|
};
|
|
18
19
|
|
|
19
20
|
/**
|
|
@@ -23,7 +24,7 @@ type Services = {
|
|
|
23
24
|
export class FunctionsClient extends Resource {
|
|
24
25
|
private readonly _serviceContainer;
|
|
25
26
|
private readonly _echoClient;
|
|
26
|
-
private readonly _executionContext: EdgeFunctionEnv.
|
|
27
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext = {};
|
|
27
28
|
|
|
28
29
|
private readonly _spaces = new Map<SpaceId, SpaceProxy>();
|
|
29
30
|
|
|
@@ -31,7 +32,12 @@ export class FunctionsClient extends Resource {
|
|
|
31
32
|
super();
|
|
32
33
|
invariant(typeof services.dataService !== 'undefined', 'DataService is required');
|
|
33
34
|
invariant(typeof services.queueService !== 'undefined', 'QueueService is required');
|
|
34
|
-
this._serviceContainer = new ServiceContainer(
|
|
35
|
+
this._serviceContainer = new ServiceContainer(
|
|
36
|
+
this._executionContext,
|
|
37
|
+
services.dataService,
|
|
38
|
+
services.queueService,
|
|
39
|
+
services.functionsAiService,
|
|
40
|
+
);
|
|
35
41
|
this._echoClient = new EchoClient({});
|
|
36
42
|
}
|
|
37
43
|
|
|
@@ -69,6 +75,7 @@ export const createClientFromEnv = async (env: any): Promise<FunctionsClient> =>
|
|
|
69
75
|
const client = new FunctionsClient({
|
|
70
76
|
dataService: env.DATA_SERVICE,
|
|
71
77
|
queueService: env.QUEUE_SERVICE,
|
|
78
|
+
functionsAiService: env.FUNCTIONS_AI_SERVICE,
|
|
72
79
|
});
|
|
73
80
|
await client.open();
|
|
74
81
|
return client;
|
|
@@ -12,6 +12,8 @@ import { log } from '@dxos/log';
|
|
|
12
12
|
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
13
13
|
import type {
|
|
14
14
|
BatchedDocumentUpdates,
|
|
15
|
+
CreateDocumentRequest,
|
|
16
|
+
CreateDocumentResponse,
|
|
15
17
|
DataService as DataServiceProto,
|
|
16
18
|
GetDocumentHeadsRequest,
|
|
17
19
|
GetDocumentHeadsResponse,
|
|
@@ -22,11 +24,13 @@ import type {
|
|
|
22
24
|
UpdateSubscriptionRequest,
|
|
23
25
|
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
24
26
|
|
|
27
|
+
import { copyUint8Array } from './utils';
|
|
28
|
+
|
|
25
29
|
export class DataServiceImpl implements DataServiceProto {
|
|
26
30
|
private dataSubscriptions = new Map<string, { spaceId: SpaceId; next: (msg: BatchedDocumentUpdates) => void }>();
|
|
27
31
|
|
|
28
32
|
constructor(
|
|
29
|
-
private _executionContext: EdgeFunctionEnv.
|
|
33
|
+
private _executionContext: EdgeFunctionEnv.TraceContext,
|
|
30
34
|
private _dataService: EdgeFunctionEnv.DataService,
|
|
31
35
|
) {}
|
|
32
36
|
|
|
@@ -41,7 +45,7 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
41
45
|
});
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
async updateSubscription({ subscriptionId, addIds
|
|
48
|
+
async updateSubscription({ subscriptionId, addIds }: UpdateSubscriptionRequest): Promise<void> {
|
|
45
49
|
const sub =
|
|
46
50
|
this.dataSubscriptions.get(subscriptionId) ??
|
|
47
51
|
raise(
|
|
@@ -55,17 +59,31 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
55
59
|
log.info('request documents', { count: addIds.length });
|
|
56
60
|
// TODO(dmaretskyi): Batch.
|
|
57
61
|
for (const documentId of addIds) {
|
|
58
|
-
|
|
62
|
+
using document = await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId);
|
|
59
63
|
log.info('document loaded', { documentId, spaceId: sub.spaceId, found: !!document });
|
|
60
64
|
if (!document) {
|
|
61
65
|
log.warn('not found', { documentId });
|
|
62
66
|
continue;
|
|
63
67
|
}
|
|
64
|
-
sub.next({
|
|
68
|
+
sub.next({
|
|
69
|
+
updates: [
|
|
70
|
+
{
|
|
71
|
+
documentId,
|
|
72
|
+
// Copy returned object to avoid hanging RPC stub
|
|
73
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
74
|
+
mutation: copyUint8Array(document.data),
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
});
|
|
65
78
|
}
|
|
66
79
|
}
|
|
67
80
|
}
|
|
68
81
|
|
|
82
|
+
async createDocument({ spaceId, initialValue }: CreateDocumentRequest): Promise<CreateDocumentResponse> {
|
|
83
|
+
using response = await this._dataService.createDocument(this._executionContext, { spaceId, initialValue });
|
|
84
|
+
return { documentId: response.documentId };
|
|
85
|
+
}
|
|
86
|
+
|
|
69
87
|
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
70
88
|
const sub =
|
|
71
89
|
this.dataSubscriptions.get(subscriptionId) ??
|
|
@@ -93,31 +111,30 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
93
111
|
// No-op.
|
|
94
112
|
}
|
|
95
113
|
|
|
96
|
-
subscribeSpaceSyncState(
|
|
114
|
+
subscribeSpaceSyncState(_request: GetSpaceSyncStateRequest, _options?: RequestOptions): Stream<SpaceSyncState> {
|
|
97
115
|
throw new NotImplementedError({
|
|
98
116
|
message: 'subscribeSpaceSyncState is not implemented.',
|
|
99
117
|
});
|
|
100
118
|
}
|
|
101
119
|
|
|
102
|
-
async getDocumentHeads({ documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
120
|
+
async getDocumentHeads({ documentIds: _documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
103
121
|
throw new NotImplementedError({
|
|
104
122
|
message: 'getDocumentHeads is not implemented.',
|
|
105
123
|
});
|
|
106
124
|
}
|
|
107
125
|
|
|
108
|
-
async reIndexHeads({ documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
126
|
+
async reIndexHeads({ documentIds: _documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
109
127
|
throw new NotImplementedError({
|
|
110
128
|
message: 'reIndexHeads is not implemented.',
|
|
111
129
|
});
|
|
112
130
|
}
|
|
113
131
|
|
|
114
132
|
async updateIndexes(): Promise<void> {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
});
|
|
133
|
+
log.error('updateIndexes is not available in EDGE env.');
|
|
134
|
+
// No-op.
|
|
118
135
|
}
|
|
119
136
|
|
|
120
|
-
async waitUntilHeadsReplicated({ heads }: { heads: any }): Promise<void> {
|
|
137
|
+
async waitUntilHeadsReplicated({ heads: _heads }: { heads: any }): Promise<void> {
|
|
121
138
|
throw new NotImplementedError({
|
|
122
139
|
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
123
140
|
});
|
|
@@ -2,67 +2,36 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import * as Schema from 'effect/Schema';
|
|
6
|
-
|
|
7
5
|
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
8
|
-
import { QueryAST } from '@dxos/echo-protocol';
|
|
9
6
|
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
10
|
-
import { invariant } from '@dxos/invariant';
|
|
11
|
-
import { PublicKey } from '@dxos/keys';
|
|
12
|
-
import { SpaceId } from '@dxos/keys';
|
|
13
7
|
import { log } from '@dxos/log';
|
|
14
8
|
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
15
|
-
import {
|
|
16
|
-
type QueryRequest,
|
|
17
|
-
type QueryResponse,
|
|
18
|
-
type QueryResult as QueryResultProto,
|
|
19
|
-
type QueryService as QueryServiceProto,
|
|
20
|
-
} from '@dxos/protocols/proto/dxos/echo/query';
|
|
21
|
-
|
|
22
|
-
import { queryToDataServiceRequest } from './adapter';
|
|
9
|
+
import { type QueryRequest, type QueryResponse, type QueryService } from '@dxos/protocols/proto/dxos/echo/query';
|
|
23
10
|
|
|
24
|
-
export class QueryServiceImpl implements
|
|
11
|
+
export class QueryServiceImpl implements QueryService {
|
|
25
12
|
private _queryCount = 0;
|
|
26
13
|
|
|
27
14
|
constructor(
|
|
28
|
-
private readonly _executionContext: EdgeFunctionEnv.
|
|
15
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext,
|
|
29
16
|
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
30
17
|
) {}
|
|
31
18
|
|
|
32
19
|
execQuery(request: QueryRequest): Stream<QueryResponse> {
|
|
33
20
|
log.info('execQuery', { request });
|
|
34
|
-
const query = QueryAST.Query.pipe(Schema.decodeUnknownSync)(JSON.parse(request.query));
|
|
35
|
-
const requestedSpaceIds = getTargetSpacesForQuery(query);
|
|
36
|
-
invariant(requestedSpaceIds.length === 1, 'Only one space is supported');
|
|
37
|
-
const spaceId = requestedSpaceIds[0];
|
|
38
21
|
|
|
39
22
|
return Stream.fromPromise<QueryResponse>(
|
|
40
23
|
(async () => {
|
|
41
24
|
try {
|
|
42
25
|
this._queryCount++;
|
|
43
|
-
log.info('begin query', {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
);
|
|
48
|
-
log.info('query response', { spaceId, filter: request.filter, resultCount: queryResponse.results.length });
|
|
49
|
-
return {
|
|
50
|
-
results: queryResponse.results.map(
|
|
51
|
-
(object): QueryResultProto => ({
|
|
52
|
-
id: object.objectId,
|
|
53
|
-
spaceId,
|
|
54
|
-
spaceKey: PublicKey.ZERO,
|
|
55
|
-
documentId: object.document.documentId,
|
|
56
|
-
rank: 0,
|
|
57
|
-
documentAutomerge: object.document.data,
|
|
58
|
-
}),
|
|
59
|
-
),
|
|
60
|
-
} satisfies QueryResponse;
|
|
26
|
+
log.info('begin query', { request });
|
|
27
|
+
using queryResponse = await this._dataService.execQuery(this._executionContext, request);
|
|
28
|
+
log.info('query response', { resultCount: queryResponse.results?.length });
|
|
29
|
+
return structuredClone(queryResponse);
|
|
61
30
|
} catch (error) {
|
|
62
31
|
log.error('query failed', { err: error });
|
|
63
32
|
throw new RuntimeServiceError({
|
|
64
33
|
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
65
|
-
context: {
|
|
34
|
+
context: { queryCount: this._queryCount },
|
|
66
35
|
cause: error,
|
|
67
36
|
});
|
|
68
37
|
}
|
|
@@ -82,22 +51,3 @@ export class QueryServiceImpl implements QueryServiceProto {
|
|
|
82
51
|
});
|
|
83
52
|
}
|
|
84
53
|
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Lists spaces this query will select from.
|
|
88
|
-
*/
|
|
89
|
-
export const getTargetSpacesForQuery = (query: QueryAST.Query): SpaceId[] => {
|
|
90
|
-
const spaces = new Set<SpaceId>();
|
|
91
|
-
|
|
92
|
-
const visitor = (node: QueryAST.Query) => {
|
|
93
|
-
if (node.type === 'options') {
|
|
94
|
-
if (node.options.spaceIds) {
|
|
95
|
-
for (const spaceId of node.options.spaceIds) {
|
|
96
|
-
spaces.add(SpaceId.make(spaceId));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
QueryAST.visit(query, visitor);
|
|
102
|
-
return [...spaces];
|
|
103
|
-
};
|
|
@@ -2,38 +2,43 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import type
|
|
7
|
-
import { type QueueService as QueueServiceProto } from '@dxos/protocols';
|
|
8
|
-
import type { EdgeFunctionEnv, QueryResult, QueueQuery } from '@dxos/protocols';
|
|
5
|
+
import { RuntimeServiceError } from '@dxos/errors';
|
|
6
|
+
import { type EdgeFunctionEnv, type FeedProtocol } from '@dxos/protocols';
|
|
9
7
|
|
|
10
|
-
export class QueueServiceImpl implements
|
|
8
|
+
export class QueueServiceImpl implements FeedProtocol.QueueService {
|
|
11
9
|
constructor(
|
|
12
|
-
protected _ctx: EdgeFunctionEnv.
|
|
10
|
+
protected _ctx: EdgeFunctionEnv.TraceContext,
|
|
13
11
|
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
14
12
|
) {}
|
|
15
|
-
|
|
13
|
+
|
|
14
|
+
async queryQueue(request: FeedProtocol.QueryQueueRequest): Promise<FeedProtocol.QueryResult> {
|
|
16
15
|
try {
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
using result = await this._queueService.queryQueue(this._ctx, request);
|
|
17
|
+
// Copy to avoid hanging RPC stub (Workers RPC lifecycle).
|
|
18
|
+
return {
|
|
19
|
+
objects: structuredClone(result.objects),
|
|
20
|
+
nextCursor: result.nextCursor,
|
|
21
|
+
prevCursor: result.prevCursor,
|
|
22
|
+
};
|
|
19
23
|
} catch (error) {
|
|
24
|
+
const { query } = request;
|
|
20
25
|
throw RuntimeServiceError.wrap({
|
|
21
26
|
message: 'Queue query failed.',
|
|
22
|
-
context: {
|
|
27
|
+
context: {
|
|
28
|
+
subspaceTag: query?.queuesNamespace,
|
|
29
|
+
spaceId: query?.spaceId,
|
|
30
|
+
queueId: query?.queueIds?.[0],
|
|
31
|
+
},
|
|
23
32
|
ifTypeDiffers: true,
|
|
24
33
|
})(error);
|
|
25
34
|
}
|
|
26
35
|
}
|
|
27
36
|
|
|
28
|
-
async insertIntoQueue(
|
|
37
|
+
async insertIntoQueue(request: FeedProtocol.InsertIntoQueueRequest): Promise<void> {
|
|
29
38
|
try {
|
|
30
|
-
|
|
31
|
-
this._ctx,
|
|
32
|
-
`dxn:queue:${subspaceTag}:${spaceId}:${queueId}`,
|
|
33
|
-
objects,
|
|
34
|
-
);
|
|
35
|
-
return result;
|
|
39
|
+
using _ = await this._queueService.insertIntoQueue(this._ctx, request);
|
|
36
40
|
} catch (error) {
|
|
41
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
37
42
|
throw RuntimeServiceError.wrap({
|
|
38
43
|
message: 'Queue append failed.',
|
|
39
44
|
context: { subspaceTag, spaceId, queueId },
|
|
@@ -42,10 +47,20 @@ export class QueueServiceImpl implements QueueServiceProto {
|
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
deleteFromQueue(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
async deleteFromQueue(request: FeedProtocol.DeleteFromQueueRequest): Promise<void> {
|
|
51
|
+
try {
|
|
52
|
+
using _ = await this._queueService.deleteFromQueue(this._ctx, request);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
55
|
+
throw RuntimeServiceError.wrap({
|
|
56
|
+
message: 'Queue delete failed.',
|
|
57
|
+
context: { subspaceTag, spaceId, queueId },
|
|
58
|
+
ifTypeDiffers: true,
|
|
59
|
+
})(error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async syncQueue(_: FeedProtocol.SyncQueueRequest): Promise<void> {
|
|
64
|
+
// No-op in Cloudflare runtime.
|
|
50
65
|
}
|
|
51
66
|
}
|
|
@@ -2,11 +2,9 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type
|
|
5
|
+
import { type AnyEntity } from '@dxos/echo/internal';
|
|
6
6
|
import { type DXN, type SpaceId } from '@dxos/keys';
|
|
7
|
-
import type
|
|
8
|
-
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
9
|
-
import { type QueueService as QueueServiceProto } from '@dxos/protocols';
|
|
7
|
+
import { type EdgeFunctionEnv, type FeedProtocol } from '@dxos/protocols';
|
|
10
8
|
import { type QueryService as QueryServiceProto } from '@dxos/protocols/proto/dxos/echo/query';
|
|
11
9
|
import type { DataService as DataServiceProto } from '@dxos/protocols/proto/dxos/echo/service';
|
|
12
10
|
|
|
@@ -19,19 +17,29 @@ import { QueueServiceImpl } from './queue-service-impl';
|
|
|
19
17
|
*/
|
|
20
18
|
export class ServiceContainer {
|
|
21
19
|
constructor(
|
|
22
|
-
private readonly _executionContext: EdgeFunctionEnv.
|
|
20
|
+
private readonly _executionContext: EdgeFunctionEnv.TraceContext,
|
|
23
21
|
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
24
22
|
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
23
|
+
private readonly _functionsService: EdgeFunctionEnv.FunctionsAiService,
|
|
25
24
|
) {}
|
|
26
25
|
|
|
27
26
|
async getSpaceMeta(spaceId: SpaceId): Promise<EdgeFunctionEnv.SpaceMeta | undefined> {
|
|
28
|
-
|
|
27
|
+
using result = await this._dataService.getSpaceMeta(this._executionContext, spaceId);
|
|
28
|
+
// Copy returned object to avoid hanging RPC stub
|
|
29
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
30
|
+
return result
|
|
31
|
+
? {
|
|
32
|
+
spaceKey: result.spaceKey,
|
|
33
|
+
rootDocumentId: result.rootDocumentId,
|
|
34
|
+
}
|
|
35
|
+
: undefined;
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
async createServices(): Promise<{
|
|
32
39
|
dataService: DataServiceProto;
|
|
33
40
|
queryService: QueryServiceProto;
|
|
34
|
-
queueService:
|
|
41
|
+
queueService: FeedProtocol.QueueService;
|
|
42
|
+
functionsAiService: EdgeFunctionEnv.FunctionsAiService;
|
|
35
43
|
}> {
|
|
36
44
|
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
37
45
|
const queryService = new QueryServiceImpl(this._executionContext, this._dataService);
|
|
@@ -41,14 +49,41 @@ export class ServiceContainer {
|
|
|
41
49
|
dataService,
|
|
42
50
|
queryService,
|
|
43
51
|
queueService,
|
|
52
|
+
functionsAiService: this._functionsService,
|
|
44
53
|
};
|
|
45
54
|
}
|
|
46
55
|
|
|
47
|
-
queryQueue(queue: DXN): Promise<QueryResult> {
|
|
48
|
-
|
|
56
|
+
async queryQueue(queue: DXN): Promise<FeedProtocol.QueryResult> {
|
|
57
|
+
const parts = queue.asQueueDXN();
|
|
58
|
+
if (!parts) {
|
|
59
|
+
throw new Error('Invalid queue DXN');
|
|
60
|
+
}
|
|
61
|
+
const { subspaceTag, spaceId, queueId } = parts;
|
|
62
|
+
const result = await this._queueService.queryQueue(this._executionContext, {
|
|
63
|
+
query: {
|
|
64
|
+
spaceId,
|
|
65
|
+
queuesNamespace: subspaceTag,
|
|
66
|
+
queueIds: [queueId],
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
objects: structuredClone(result.objects),
|
|
71
|
+
nextCursor: result.nextCursor ?? null,
|
|
72
|
+
prevCursor: result.prevCursor ?? null,
|
|
73
|
+
};
|
|
49
74
|
}
|
|
50
75
|
|
|
51
|
-
insertIntoQueue(queue: DXN, objects:
|
|
52
|
-
|
|
76
|
+
async insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
77
|
+
const parts = queue.asQueueDXN();
|
|
78
|
+
if (!parts) {
|
|
79
|
+
throw new Error('Invalid queue DXN');
|
|
80
|
+
}
|
|
81
|
+
const { subspaceTag, spaceId, queueId } = parts;
|
|
82
|
+
await this._queueService.insertIntoQueue(this._executionContext, {
|
|
83
|
+
subspaceTag,
|
|
84
|
+
spaceId,
|
|
85
|
+
queueId,
|
|
86
|
+
objects: objects as FeedProtocol.InsertIntoQueueRequest['objects'],
|
|
87
|
+
});
|
|
53
88
|
}
|
|
54
89
|
}
|
package/src/logger.ts
CHANGED
package/src/queues-api.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import type
|
|
5
|
+
import { type AnyEntity } from '@dxos/echo/internal';
|
|
6
6
|
import type { DXN, SpaceId } from '@dxos/keys';
|
|
7
|
-
import type
|
|
7
|
+
import { type FeedProtocol } from '@dxos/protocols';
|
|
8
8
|
|
|
9
9
|
import type { ServiceContainer } from './internal';
|
|
10
10
|
|
|
@@ -14,8 +14,8 @@ import type { ServiceContainer } from './internal';
|
|
|
14
14
|
* @deprecated
|
|
15
15
|
*/
|
|
16
16
|
export interface QueuesAPI {
|
|
17
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
18
|
-
insertIntoQueue(queue: DXN, objects:
|
|
17
|
+
queryQueue(queue: DXN, options?: {}): Promise<FeedProtocol.QueryResult>;
|
|
18
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -27,11 +27,11 @@ export class QueuesAPIImpl implements QueuesAPI {
|
|
|
27
27
|
private readonly _spaceId: SpaceId,
|
|
28
28
|
) {}
|
|
29
29
|
|
|
30
|
-
queryQueue(queue: DXN, options?: {}): Promise<QueryResult> {
|
|
30
|
+
queryQueue(queue: DXN, options?: {}): Promise<FeedProtocol.QueryResult> {
|
|
31
31
|
return this._serviceContainer.queryQueue(queue);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
insertIntoQueue(queue: DXN, objects:
|
|
34
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
35
35
|
// TODO(dmaretskyi): Ugly.
|
|
36
36
|
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
37
37
|
}
|
package/src/space-proxy.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Resource } from '@dxos/context';
|
|
6
|
-
import { type
|
|
6
|
+
import { type Database } from '@dxos/echo';
|
|
7
|
+
import { type CoreDatabase, type EchoClient, type EchoDatabaseImpl } from '@dxos/echo-db';
|
|
7
8
|
import { invariant } from '@dxos/invariant';
|
|
8
9
|
import { PublicKey, type SpaceId } from '@dxos/keys';
|
|
9
10
|
|
|
@@ -14,7 +15,7 @@ import { type QueuesAPI, QueuesAPIImpl } from './queues-api';
|
|
|
14
15
|
* @deprecated
|
|
15
16
|
*/
|
|
16
17
|
export class SpaceProxy extends Resource {
|
|
17
|
-
private _db?:
|
|
18
|
+
private _db?: EchoDatabaseImpl = undefined;
|
|
18
19
|
private _queuesApi: QueuesAPIImpl;
|
|
19
20
|
|
|
20
21
|
constructor(
|
|
@@ -30,7 +31,7 @@ export class SpaceProxy extends Resource {
|
|
|
30
31
|
return this._id;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
get db():
|
|
34
|
+
get db(): Database.Database {
|
|
34
35
|
invariant(this._db);
|
|
35
36
|
return this._db;
|
|
36
37
|
}
|
|
@@ -22,7 +22,6 @@ export const wrapHandlerForCloudflare = (func: FunctionProtocol.Func): ExportedH
|
|
|
22
22
|
// TODO(mykola): Wrap in withNewExecutionContext;
|
|
23
23
|
// Meta route is used to get the input schema of the function by the functions service.
|
|
24
24
|
if (request.headers.get(FUNCTION_ROUTE_HEADER) === FunctionRouteValue.Meta) {
|
|
25
|
-
log.info('>>> meta', { func });
|
|
26
25
|
return handleFunctionMetaCall(func, request);
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -34,7 +33,7 @@ export const wrapHandlerForCloudflare = (func: FunctionProtocol.Func): ExportedH
|
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
const serviceContainer = new ServiceContainer({}, env.DATA_SERVICE, env.QUEUE_SERVICE);
|
|
36
|
+
const serviceContainer = new ServiceContainer({}, env.DATA_SERVICE, env.QUEUE_SERVICE, env.FUNCTIONS_AI_SERVICE);
|
|
38
37
|
const context = await createFunctionContext({
|
|
39
38
|
serviceContainer,
|
|
40
39
|
contextSpaceId: spaceId as SpaceId | undefined,
|
|
@@ -105,7 +104,7 @@ const createFunctionContext = async ({
|
|
|
105
104
|
serviceContainer: ServiceContainer;
|
|
106
105
|
contextSpaceId: SpaceId | undefined;
|
|
107
106
|
}): Promise<FunctionProtocol.Context> => {
|
|
108
|
-
const { dataService, queryService, queueService } = await serviceContainer.createServices();
|
|
107
|
+
const { dataService, queryService, queueService, functionsAiService } = await serviceContainer.createServices();
|
|
109
108
|
|
|
110
109
|
let spaceKey: string | undefined;
|
|
111
110
|
let rootUrl: string | undefined;
|
|
@@ -124,6 +123,7 @@ const createFunctionContext = async ({
|
|
|
124
123
|
dataService,
|
|
125
124
|
queryService,
|
|
126
125
|
queueService,
|
|
126
|
+
functionsAiService,
|
|
127
127
|
},
|
|
128
128
|
spaceId: contextSpaceId,
|
|
129
129
|
spaceKey,
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type QueryAST } from '@dxos/echo-protocol';
|
|
2
|
-
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
3
|
-
export declare const queryToDataServiceRequest: (query: QueryAST.Query) => EdgeFunctionEnv.QueryRequest;
|
|
4
|
-
/**
|
|
5
|
-
* Extracts the filter and options from a query.
|
|
6
|
-
* Supports Select(...) and Options(Select(...)) queries.
|
|
7
|
-
*/
|
|
8
|
-
export declare const isSimpleSelectionQuery: (query: QueryAST.Query) => {
|
|
9
|
-
filter: QueryAST.Filter;
|
|
10
|
-
options?: QueryAST.QueryOptions;
|
|
11
|
-
} | null;
|
|
12
|
-
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../../../src/internal/adapter.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,eAAO,MAAM,yBAAyB,GAAI,OAAO,QAAQ,CAAC,KAAK,KAAG,eAAe,CAAC,YAajF,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,GACjC,OAAO,QAAQ,CAAC,KAAK,KACpB;IAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,YAAY,CAAA;CAAE,GAAG,IAgBjE,CAAC"}
|
package/src/internal/adapter.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { failUndefined } from '@dxos/debug';
|
|
6
|
-
import { type QueryAST } from '@dxos/echo-protocol';
|
|
7
|
-
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import { SpaceId } from '@dxos/keys';
|
|
9
|
-
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
10
|
-
|
|
11
|
-
export const queryToDataServiceRequest = (query: QueryAST.Query): EdgeFunctionEnv.QueryRequest => {
|
|
12
|
-
const { filter, options } = isSimpleSelectionQuery(query) ?? failUndefined();
|
|
13
|
-
invariant(options?.spaceIds?.length === 1, 'Only one space is supported');
|
|
14
|
-
invariant(filter.type === 'object', 'Only object filters are supported');
|
|
15
|
-
|
|
16
|
-
const spaceId = options.spaceIds[0];
|
|
17
|
-
invariant(SpaceId.isValid(spaceId));
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
spaceId,
|
|
21
|
-
type: filter.typename ?? undefined,
|
|
22
|
-
objectIds: [...(filter.id ?? [])],
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Extracts the filter and options from a query.
|
|
28
|
-
* Supports Select(...) and Options(Select(...)) queries.
|
|
29
|
-
*/
|
|
30
|
-
export const isSimpleSelectionQuery = (
|
|
31
|
-
query: QueryAST.Query,
|
|
32
|
-
): { filter: QueryAST.Filter; options?: QueryAST.QueryOptions } | null => {
|
|
33
|
-
switch (query.type) {
|
|
34
|
-
case 'options': {
|
|
35
|
-
const maybeFilter = isSimpleSelectionQuery(query.query);
|
|
36
|
-
if (!maybeFilter) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
return { filter: maybeFilter.filter, options: query.options };
|
|
40
|
-
}
|
|
41
|
-
case 'select': {
|
|
42
|
-
return { filter: query.filter, options: undefined };
|
|
43
|
-
}
|
|
44
|
-
default: {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|