@dxos/functions-runtime-cloudflare 0.8.4-main.3c1ae3b → 0.8.4-main.3fbcb4aa9b
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 +529 -314
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +529 -314
- 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 +10 -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 +6 -0
- 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 +29 -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 +12 -8
- package/src/queues-api.ts +26 -7
- package/src/space-proxy.ts +5 -4
- package/src/wrap-handler-for-cloudflare.ts +4 -4
- 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.3fbcb4aa9b",
|
|
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/codec-protobuf": "0.8.4-main.
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/echo": "0.8.4-main.
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/echo-
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/log": "0.8.4-main.
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
30
|
+
"effect": "3.20.0",
|
|
31
|
+
"@dxos/codec-protobuf": "0.8.4-main.3fbcb4aa9b",
|
|
32
|
+
"@dxos/echo": "0.8.4-main.3fbcb4aa9b",
|
|
33
|
+
"@dxos/context": "0.8.4-main.3fbcb4aa9b",
|
|
34
|
+
"@dxos/echo-db": "0.8.4-main.3fbcb4aa9b",
|
|
35
|
+
"@dxos/debug": "0.8.4-main.3fbcb4aa9b",
|
|
36
|
+
"@dxos/echo-protocol": "0.8.4-main.3fbcb4aa9b",
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.3fbcb4aa9b",
|
|
38
|
+
"@dxos/log": "0.8.4-main.3fbcb4aa9b",
|
|
39
|
+
"@dxos/util": "0.8.4-main.3fbcb4aa9b",
|
|
40
|
+
"@dxos/errors": "0.8.4-main.3fbcb4aa9b",
|
|
41
|
+
"@dxos/keys": "0.8.4-main.3fbcb4aa9b",
|
|
42
|
+
"@dxos/protocols": "0.8.4-main.3fbcb4aa9b"
|
|
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.3fbcb4aa9b"
|
|
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,32 @@ 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
|
+
invariant(SpaceId.isValid(spaceId));
|
|
84
|
+
using response = await this._dataService.createDocument(this._executionContext, spaceId, initialValue);
|
|
85
|
+
return { documentId: response.documentId };
|
|
86
|
+
}
|
|
87
|
+
|
|
69
88
|
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
70
89
|
const sub =
|
|
71
90
|
this.dataSubscriptions.get(subscriptionId) ??
|
|
@@ -93,31 +112,30 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
93
112
|
// No-op.
|
|
94
113
|
}
|
|
95
114
|
|
|
96
|
-
subscribeSpaceSyncState(
|
|
115
|
+
subscribeSpaceSyncState(_request: GetSpaceSyncStateRequest, _options?: RequestOptions): Stream<SpaceSyncState> {
|
|
97
116
|
throw new NotImplementedError({
|
|
98
117
|
message: 'subscribeSpaceSyncState is not implemented.',
|
|
99
118
|
});
|
|
100
119
|
}
|
|
101
120
|
|
|
102
|
-
async getDocumentHeads({ documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
121
|
+
async getDocumentHeads({ documentIds: _documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
103
122
|
throw new NotImplementedError({
|
|
104
123
|
message: 'getDocumentHeads is not implemented.',
|
|
105
124
|
});
|
|
106
125
|
}
|
|
107
126
|
|
|
108
|
-
async reIndexHeads({ documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
127
|
+
async reIndexHeads({ documentIds: _documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
109
128
|
throw new NotImplementedError({
|
|
110
129
|
message: 'reIndexHeads is not implemented.',
|
|
111
130
|
});
|
|
112
131
|
}
|
|
113
132
|
|
|
114
133
|
async updateIndexes(): Promise<void> {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
});
|
|
134
|
+
log.error('updateIndexes is not available in EDGE env.');
|
|
135
|
+
// No-op.
|
|
118
136
|
}
|
|
119
137
|
|
|
120
|
-
async waitUntilHeadsReplicated({ heads }: { heads: any }): Promise<void> {
|
|
138
|
+
async waitUntilHeadsReplicated({ heads: _heads }: { heads: any }): Promise<void> {
|
|
121
139
|
throw new NotImplementedError({
|
|
122
140
|
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
123
141
|
});
|
|
@@ -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.map((obj) => JSON.stringify(obj)),
|
|
87
|
+
});
|
|
53
88
|
}
|
|
54
89
|
}
|
package/src/logger.ts
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
|
|
5
7
|
import { LogLevel, type LogProcessor, log, shouldLog } from '@dxos/log';
|
|
6
8
|
|
|
7
9
|
export const setupFunctionsLogger = () => {
|
|
@@ -14,27 +16,29 @@ const functionLogProcessor: LogProcessor = (config, entry) => {
|
|
|
14
16
|
return;
|
|
15
17
|
}
|
|
16
18
|
|
|
19
|
+
const context = entry.computedContext;
|
|
20
|
+
const error = entry.computedError;
|
|
21
|
+
const extras = [Object.keys(context).length > 0 ? context : undefined, error].filter((value) => value !== undefined);
|
|
22
|
+
|
|
17
23
|
switch (entry.level) {
|
|
18
24
|
case LogLevel.DEBUG:
|
|
19
|
-
console.debug(entry.message, entry.context);
|
|
20
|
-
break;
|
|
21
25
|
case LogLevel.TRACE:
|
|
22
|
-
console.debug(entry.message,
|
|
26
|
+
console.debug(entry.message, ...extras);
|
|
23
27
|
break;
|
|
24
28
|
case LogLevel.VERBOSE:
|
|
25
|
-
console.log(entry.message,
|
|
29
|
+
console.log(entry.message, ...extras);
|
|
26
30
|
break;
|
|
27
31
|
case LogLevel.INFO:
|
|
28
|
-
console.info(entry.message,
|
|
32
|
+
console.info(entry.message, ...extras);
|
|
29
33
|
break;
|
|
30
34
|
case LogLevel.WARN:
|
|
31
|
-
console.warn(entry.message,
|
|
35
|
+
console.warn(entry.message, ...extras);
|
|
32
36
|
break;
|
|
33
37
|
case LogLevel.ERROR:
|
|
34
|
-
console.error(entry.message,
|
|
38
|
+
console.error(entry.message, ...extras);
|
|
35
39
|
break;
|
|
36
40
|
default:
|
|
37
|
-
console.log(entry.message,
|
|
41
|
+
console.log(entry.message, ...extras);
|
|
38
42
|
break;
|
|
39
43
|
}
|
|
40
44
|
};
|
package/src/queues-api.ts
CHANGED
|
@@ -2,20 +2,26 @@
|
|
|
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
|
|
7
|
+
import { log } from '@dxos/log';
|
|
8
8
|
|
|
9
9
|
import type { ServiceContainer } from './internal';
|
|
10
10
|
|
|
11
|
+
export interface QueuesQueryResult {
|
|
12
|
+
objects: AnyEntity[];
|
|
13
|
+
nextCursor: string | null;
|
|
14
|
+
prevCursor: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
// TODO(dmaretskyi): Temporary API to get the queues working.
|
|
12
18
|
// TODO(dmaretskyi): To be replaced with integrating queues into echo.
|
|
13
19
|
/**
|
|
14
20
|
* @deprecated
|
|
15
21
|
*/
|
|
16
22
|
export interface QueuesAPI {
|
|
17
|
-
queryQueue(queue: DXN, options?: {}): Promise<
|
|
18
|
-
insertIntoQueue(queue: DXN, objects:
|
|
23
|
+
queryQueue(queue: DXN, options?: {}): Promise<QueuesQueryResult>;
|
|
24
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
/**
|
|
@@ -27,11 +33,24 @@ export class QueuesAPIImpl implements QueuesAPI {
|
|
|
27
33
|
private readonly _spaceId: SpaceId,
|
|
28
34
|
) {}
|
|
29
35
|
|
|
30
|
-
queryQueue(queue: DXN, options?: {}): Promise<
|
|
31
|
-
|
|
36
|
+
async queryQueue(queue: DXN, options?: {}): Promise<QueuesQueryResult> {
|
|
37
|
+
const result = await this._serviceContainer.queryQueue(queue);
|
|
38
|
+
const objects = (result.objects ?? []).flatMap((encoded): AnyEntity[] => {
|
|
39
|
+
try {
|
|
40
|
+
return [JSON.parse(encoded) as AnyEntity];
|
|
41
|
+
} catch (err) {
|
|
42
|
+
log.verbose('queue object JSON parse failed; object ignored', { encoded, error: err });
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
objects,
|
|
48
|
+
nextCursor: result.nextCursor ?? null,
|
|
49
|
+
prevCursor: result.prevCursor ?? null,
|
|
50
|
+
};
|
|
32
51
|
}
|
|
33
52
|
|
|
34
|
-
insertIntoQueue(queue: DXN, objects:
|
|
53
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
35
54
|
// TODO(dmaretskyi): Ugly.
|
|
36
55
|
return this._serviceContainer.insertIntoQueue(queue, JSON.parse(JSON.stringify(objects)));
|
|
37
56
|
}
|
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
|
}
|
|
@@ -60,6 +61,6 @@ export class SpaceProxy extends Resource {
|
|
|
60
61
|
owningObject: this,
|
|
61
62
|
});
|
|
62
63
|
|
|
63
|
-
await this._db.coreDatabase.open({ rootUrl: meta.rootDocumentId });
|
|
64
|
+
await this._db.coreDatabase.open(this._ctx, { rootUrl: meta.rootDocumentId });
|
|
64
65
|
}
|
|
65
66
|
}
|
|
@@ -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,
|
|
@@ -98,14 +97,14 @@ const handleFunctionMetaCall = (functionDefinition: FunctionProtocol.Func, reque
|
|
|
98
97
|
});
|
|
99
98
|
};
|
|
100
99
|
|
|
101
|
-
const createFunctionContext = async ({
|
|
100
|
+
export const createFunctionContext = async ({
|
|
102
101
|
serviceContainer,
|
|
103
102
|
contextSpaceId,
|
|
104
103
|
}: {
|
|
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,
|