@dxos/functions-runtime-cloudflare 0.8.4-main.66e292d → 0.8.4-main.69d29f4
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 +769 -113
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +769 -113
- 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/index.d.ts +4 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/internal/data-service-impl.d.ts +7 -6
- package/dist/types/src/internal/data-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/query-service-impl.d.ts +1 -0
- package/dist/types/src/internal/query-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/queue-service-impl.d.ts +4 -5
- package/dist/types/src/internal/queue-service-impl.d.ts.map +1 -1
- package/dist/types/src/internal/service-container.d.ts +5 -3
- 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 +2 -0
- package/dist/types/src/logger.d.ts.map +1 -0
- package/dist/types/src/queues-api.d.ts +3 -3
- 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 -15
- package/src/functions-client.ts +8 -1
- package/src/index.ts +4 -1
- package/src/internal/data-service-impl.ts +66 -17
- package/src/internal/query-service-impl.ts +24 -8
- package/src/internal/queue-service-impl.ts +54 -8
- package/src/internal/service-container.ts +25 -6
- package/src/internal/utils.ts +5 -0
- package/src/logger.ts +42 -0
- package/src/queues-api.ts +3 -3
- package/src/space-proxy.ts +4 -3
- package/src/wrap-handler-for-cloudflare.ts +3 -3
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.69d29f4",
|
|
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,22 +27,23 @@
|
|
|
23
27
|
"src"
|
|
24
28
|
],
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"effect": "3.
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/context": "0.8.4-main.
|
|
29
|
-
"@dxos/
|
|
30
|
-
"@dxos/echo": "0.8.4-main.
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/echo-
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/log": "0.8.4-main.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/protocols": "0.8.4-main.
|
|
30
|
+
"effect": "3.19.11",
|
|
31
|
+
"@dxos/debug": "0.8.4-main.69d29f4",
|
|
32
|
+
"@dxos/context": "0.8.4-main.69d29f4",
|
|
33
|
+
"@dxos/echo": "0.8.4-main.69d29f4",
|
|
34
|
+
"@dxos/echo-db": "0.8.4-main.69d29f4",
|
|
35
|
+
"@dxos/codec-protobuf": "0.8.4-main.69d29f4",
|
|
36
|
+
"@dxos/echo-protocol": "0.8.4-main.69d29f4",
|
|
37
|
+
"@dxos/errors": "0.8.4-main.69d29f4",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.69d29f4",
|
|
39
|
+
"@dxos/log": "0.8.4-main.69d29f4",
|
|
40
|
+
"@dxos/keys": "0.8.4-main.69d29f4",
|
|
41
|
+
"@dxos/protocols": "0.8.4-main.69d29f4",
|
|
42
|
+
"@dxos/util": "0.8.4-main.69d29f4"
|
|
38
43
|
},
|
|
39
44
|
"devDependencies": {
|
|
40
|
-
"@cloudflare/workers-types": "4.
|
|
41
|
-
"@dxos/types": "0.8.4-main.
|
|
45
|
+
"@cloudflare/workers-types": "^4.20251210.0",
|
|
46
|
+
"@dxos/types": "0.8.4-main.69d29f4"
|
|
42
47
|
},
|
|
43
48
|
"publishConfig": {
|
|
44
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
|
/**
|
|
@@ -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;
|
package/src/index.ts
CHANGED
|
@@ -2,5 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export
|
|
5
|
+
export * from './functions-client';
|
|
6
|
+
export * from './internal';
|
|
6
7
|
export * from './types';
|
|
8
|
+
export * from './wrap-handler-for-cloudflare';
|
|
9
|
+
export * from './logger';
|
|
@@ -5,12 +5,15 @@
|
|
|
5
5
|
import type { RequestOptions } from '@dxos/codec-protobuf';
|
|
6
6
|
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
7
7
|
import { raise } from '@dxos/debug';
|
|
8
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
9
10
|
import { SpaceId } from '@dxos/keys';
|
|
10
11
|
import { log } from '@dxos/log';
|
|
11
12
|
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
12
13
|
import type {
|
|
13
14
|
BatchedDocumentUpdates,
|
|
15
|
+
CreateDocumentRequest,
|
|
16
|
+
CreateDocumentResponse,
|
|
14
17
|
DataService as DataServiceProto,
|
|
15
18
|
GetDocumentHeadsRequest,
|
|
16
19
|
GetDocumentHeadsResponse,
|
|
@@ -21,6 +24,8 @@ import type {
|
|
|
21
24
|
UpdateSubscriptionRequest,
|
|
22
25
|
} from '@dxos/protocols/proto/dxos/echo/service';
|
|
23
26
|
|
|
27
|
+
import { copyUint8Array } from './utils';
|
|
28
|
+
|
|
24
29
|
export class DataServiceImpl implements DataServiceProto {
|
|
25
30
|
private dataSubscriptions = new Map<string, { spaceId: SpaceId; next: (msg: BatchedDocumentUpdates) => void }>();
|
|
26
31
|
|
|
@@ -40,54 +45,98 @@ export class DataServiceImpl implements DataServiceProto {
|
|
|
40
45
|
});
|
|
41
46
|
}
|
|
42
47
|
|
|
43
|
-
async updateSubscription({ subscriptionId, addIds
|
|
44
|
-
const sub =
|
|
48
|
+
async updateSubscription({ subscriptionId, addIds }: UpdateSubscriptionRequest): Promise<void> {
|
|
49
|
+
const sub =
|
|
50
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
51
|
+
raise(
|
|
52
|
+
new RuntimeServiceError({
|
|
53
|
+
message: 'Subscription not found.',
|
|
54
|
+
context: { subscriptionId },
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
45
57
|
|
|
46
58
|
if (addIds) {
|
|
47
59
|
log.info('request documents', { count: addIds.length });
|
|
48
60
|
// TODO(dmaretskyi): Batch.
|
|
49
61
|
for (const documentId of addIds) {
|
|
50
|
-
|
|
62
|
+
using document = await this._dataService.getDocument(this._executionContext, sub.spaceId, documentId);
|
|
51
63
|
log.info('document loaded', { documentId, spaceId: sub.spaceId, found: !!document });
|
|
52
64
|
if (!document) {
|
|
53
65
|
log.warn('not found', { documentId });
|
|
54
66
|
continue;
|
|
55
67
|
}
|
|
56
|
-
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
|
+
});
|
|
57
78
|
}
|
|
58
79
|
}
|
|
59
80
|
}
|
|
60
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
|
+
|
|
61
87
|
async update({ updates, subscriptionId }: UpdateRequest): Promise<void> {
|
|
62
|
-
const sub =
|
|
88
|
+
const sub =
|
|
89
|
+
this.dataSubscriptions.get(subscriptionId) ??
|
|
90
|
+
raise(
|
|
91
|
+
new RuntimeServiceError({
|
|
92
|
+
message: 'Subscription not found.',
|
|
93
|
+
context: { subscriptionId },
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
63
96
|
// TODO(dmaretskyi): Batch.
|
|
64
|
-
|
|
65
|
-
|
|
97
|
+
try {
|
|
98
|
+
for (const update of updates ?? []) {
|
|
99
|
+
await this._dataService.changeDocument(this._executionContext, sub.spaceId, update.documentId, update.mutation);
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw RuntimeServiceError.wrap({
|
|
103
|
+
message: 'Failed to apply document updates.',
|
|
104
|
+
context: { subscriptionId },
|
|
105
|
+
ifTypeDiffers: true,
|
|
106
|
+
})(error);
|
|
66
107
|
}
|
|
67
|
-
throw new Error('Method not implemented.');
|
|
68
108
|
}
|
|
69
109
|
|
|
70
110
|
async flush(): Promise<void> {
|
|
71
111
|
// No-op.
|
|
72
112
|
}
|
|
73
113
|
|
|
74
|
-
subscribeSpaceSyncState(
|
|
75
|
-
throw new
|
|
114
|
+
subscribeSpaceSyncState(_request: GetSpaceSyncStateRequest, _options?: RequestOptions): Stream<SpaceSyncState> {
|
|
115
|
+
throw new NotImplementedError({
|
|
116
|
+
message: 'subscribeSpaceSyncState is not implemented.',
|
|
117
|
+
});
|
|
76
118
|
}
|
|
77
119
|
|
|
78
|
-
async getDocumentHeads({ documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
79
|
-
throw new
|
|
120
|
+
async getDocumentHeads({ documentIds: _documentIds }: GetDocumentHeadsRequest): Promise<GetDocumentHeadsResponse> {
|
|
121
|
+
throw new NotImplementedError({
|
|
122
|
+
message: 'getDocumentHeads is not implemented.',
|
|
123
|
+
});
|
|
80
124
|
}
|
|
81
125
|
|
|
82
|
-
async reIndexHeads({ documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
83
|
-
throw new
|
|
126
|
+
async reIndexHeads({ documentIds: _documentIds }: ReIndexHeadsRequest): Promise<void> {
|
|
127
|
+
throw new NotImplementedError({
|
|
128
|
+
message: 'reIndexHeads is not implemented.',
|
|
129
|
+
});
|
|
84
130
|
}
|
|
85
131
|
|
|
86
132
|
async updateIndexes(): Promise<void> {
|
|
87
|
-
|
|
133
|
+
log.error('updateIndexes is not available in EDGE env.');
|
|
134
|
+
// No-op.
|
|
88
135
|
}
|
|
89
136
|
|
|
90
|
-
async waitUntilHeadsReplicated({ heads }: { heads: any }): Promise<void> {
|
|
91
|
-
throw new
|
|
137
|
+
async waitUntilHeadsReplicated({ heads: _heads }: { heads: any }): Promise<void> {
|
|
138
|
+
throw new NotImplementedError({
|
|
139
|
+
message: 'waitUntilHeadsReplicated is not implemented.',
|
|
140
|
+
});
|
|
92
141
|
}
|
|
93
142
|
}
|
|
@@ -6,6 +6,7 @@ import * as Schema from 'effect/Schema';
|
|
|
6
6
|
|
|
7
7
|
import { Stream } from '@dxos/codec-protobuf/stream';
|
|
8
8
|
import { QueryAST } from '@dxos/echo-protocol';
|
|
9
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
9
10
|
import { invariant } from '@dxos/invariant';
|
|
10
11
|
import { PublicKey } from '@dxos/keys';
|
|
11
12
|
import { SpaceId } from '@dxos/keys';
|
|
@@ -19,8 +20,11 @@ import {
|
|
|
19
20
|
} from '@dxos/protocols/proto/dxos/echo/query';
|
|
20
21
|
|
|
21
22
|
import { queryToDataServiceRequest } from './adapter';
|
|
23
|
+
import { copyUint8Array } from './utils';
|
|
22
24
|
|
|
23
25
|
export class QueryServiceImpl implements QueryServiceProto {
|
|
26
|
+
private _queryCount = 0;
|
|
27
|
+
|
|
24
28
|
constructor(
|
|
25
29
|
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
26
30
|
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
@@ -36,8 +40,9 @@ export class QueryServiceImpl implements QueryServiceProto {
|
|
|
36
40
|
return Stream.fromPromise<QueryResponse>(
|
|
37
41
|
(async () => {
|
|
38
42
|
try {
|
|
43
|
+
this._queryCount++;
|
|
39
44
|
log.info('begin query', { spaceId });
|
|
40
|
-
|
|
45
|
+
using queryResponse = await this._dataService.queryDocuments(
|
|
41
46
|
this._executionContext,
|
|
42
47
|
queryToDataServiceRequest(query),
|
|
43
48
|
);
|
|
@@ -49,25 +54,36 @@ export class QueryServiceImpl implements QueryServiceProto {
|
|
|
49
54
|
spaceId,
|
|
50
55
|
spaceKey: PublicKey.ZERO,
|
|
51
56
|
documentId: object.document.documentId,
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
// Rank 1 for predicate matches where ranking is not determined.
|
|
58
|
+
rank: 1,
|
|
59
|
+
// Copy returned object to avoid hanging RPC stub.
|
|
60
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
61
|
+
documentAutomerge: copyUint8Array(object.document.data),
|
|
54
62
|
}),
|
|
55
63
|
),
|
|
56
64
|
} satisfies QueryResponse;
|
|
57
|
-
} catch (
|
|
58
|
-
log.error('query failed', { err });
|
|
59
|
-
throw
|
|
65
|
+
} catch (error) {
|
|
66
|
+
log.error('query failed', { err: error });
|
|
67
|
+
throw new RuntimeServiceError({
|
|
68
|
+
message: `Query execution failed (queryCount=${this._queryCount})`,
|
|
69
|
+
context: { spaceId, filter: request.filter, queryCount: this._queryCount },
|
|
70
|
+
cause: error,
|
|
71
|
+
});
|
|
60
72
|
}
|
|
61
73
|
})(),
|
|
62
74
|
);
|
|
63
75
|
}
|
|
64
76
|
|
|
65
77
|
async reindex() {
|
|
66
|
-
throw new
|
|
78
|
+
throw new NotImplementedError({
|
|
79
|
+
message: 'Reindex is not implemented.',
|
|
80
|
+
});
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
async setConfig() {
|
|
70
|
-
throw new
|
|
84
|
+
throw new NotImplementedError({
|
|
85
|
+
message: 'SetConfig is not implemented.',
|
|
86
|
+
});
|
|
71
87
|
}
|
|
72
88
|
}
|
|
73
89
|
|
|
@@ -2,22 +2,68 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { NotImplementedError, RuntimeServiceError } from '@dxos/errors';
|
|
6
|
+
import { invariant } from '@dxos/invariant';
|
|
6
7
|
import { type QueueService as QueueServiceProto } from '@dxos/protocols';
|
|
7
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
DeleteFromQueueRequest,
|
|
10
|
+
EdgeFunctionEnv,
|
|
11
|
+
InsertIntoQueueRequest,
|
|
12
|
+
QueryQueueRequest,
|
|
13
|
+
QueryResult,
|
|
14
|
+
} from '@dxos/protocols';
|
|
8
15
|
|
|
9
16
|
export class QueueServiceImpl implements QueueServiceProto {
|
|
10
17
|
constructor(
|
|
11
18
|
protected _ctx: EdgeFunctionEnv.ExecutionContext,
|
|
12
19
|
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
13
20
|
) {}
|
|
14
|
-
queryQueue(
|
|
15
|
-
|
|
21
|
+
async queryQueue(request: QueryQueueRequest): Promise<QueryResult> {
|
|
22
|
+
const { query } = request;
|
|
23
|
+
const { queueIds, ...filter } = query!;
|
|
24
|
+
const spaceId = query!.spaceId;
|
|
25
|
+
const queueId = queueIds?.[0];
|
|
26
|
+
invariant(request.query.queuesNamespace);
|
|
27
|
+
try {
|
|
28
|
+
using result = await this._queueService.query(
|
|
29
|
+
this._ctx,
|
|
30
|
+
`dxn:queue:${request.query.queuesNamespace}:${spaceId}:${queueId}`,
|
|
31
|
+
filter,
|
|
32
|
+
);
|
|
33
|
+
return {
|
|
34
|
+
// Copy returned object to avoid hanging RPC stub
|
|
35
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
36
|
+
objects: structuredClone(result.objects),
|
|
37
|
+
nextCursor: result.nextCursor,
|
|
38
|
+
prevCursor: result.prevCursor,
|
|
39
|
+
};
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw RuntimeServiceError.wrap({
|
|
42
|
+
message: 'Queue query failed.',
|
|
43
|
+
context: { subspaceTag: request.query.queuesNamespace, spaceId, queueId },
|
|
44
|
+
ifTypeDiffers: true,
|
|
45
|
+
})(error);
|
|
46
|
+
}
|
|
16
47
|
}
|
|
17
|
-
|
|
18
|
-
|
|
48
|
+
|
|
49
|
+
async insertIntoQueue(request: InsertIntoQueueRequest): Promise<void> {
|
|
50
|
+
const { subspaceTag, spaceId, queueId, objects } = request;
|
|
51
|
+
try {
|
|
52
|
+
await this._queueService.append(this._ctx, `dxn:queue:${subspaceTag}:${spaceId}:${queueId}`, objects ?? []);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
throw RuntimeServiceError.wrap({
|
|
55
|
+
message: 'Queue append failed.',
|
|
56
|
+
context: { subspaceTag, spaceId, queueId },
|
|
57
|
+
ifTypeDiffers: true,
|
|
58
|
+
})(error);
|
|
59
|
+
}
|
|
19
60
|
}
|
|
20
|
-
|
|
21
|
-
|
|
61
|
+
|
|
62
|
+
deleteFromQueue(request: DeleteFromQueueRequest): Promise<void> {
|
|
63
|
+
const { subspaceTag, spaceId, queueId } = request;
|
|
64
|
+
throw new NotImplementedError({
|
|
65
|
+
message: 'Deleting from queue is not supported.',
|
|
66
|
+
context: { subspaceTag, spaceId, queueId },
|
|
67
|
+
});
|
|
22
68
|
}
|
|
23
69
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
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
7
|
import type { QueryResult } from '@dxos/protocols';
|
|
8
8
|
import { type EdgeFunctionEnv } from '@dxos/protocols';
|
|
@@ -22,16 +22,26 @@ export class ServiceContainer {
|
|
|
22
22
|
private readonly _executionContext: EdgeFunctionEnv.ExecutionContext,
|
|
23
23
|
private readonly _dataService: EdgeFunctionEnv.DataService,
|
|
24
24
|
private readonly _queueService: EdgeFunctionEnv.QueueService,
|
|
25
|
+
private readonly _functionsService: EdgeFunctionEnv.FunctionsAiService,
|
|
25
26
|
) {}
|
|
26
27
|
|
|
27
28
|
async getSpaceMeta(spaceId: SpaceId): Promise<EdgeFunctionEnv.SpaceMeta | undefined> {
|
|
28
|
-
|
|
29
|
+
using result = await this._dataService.getSpaceMeta(this._executionContext, spaceId);
|
|
30
|
+
// Copy returned object to avoid hanging RPC stub
|
|
31
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
32
|
+
return result
|
|
33
|
+
? {
|
|
34
|
+
spaceKey: result.spaceKey,
|
|
35
|
+
rootDocumentId: result.rootDocumentId,
|
|
36
|
+
}
|
|
37
|
+
: undefined;
|
|
29
38
|
}
|
|
30
39
|
|
|
31
40
|
async createServices(): Promise<{
|
|
32
41
|
dataService: DataServiceProto;
|
|
33
42
|
queryService: QueryServiceProto;
|
|
34
43
|
queueService: QueueServiceProto;
|
|
44
|
+
functionsAiService: EdgeFunctionEnv.FunctionsAiService;
|
|
35
45
|
}> {
|
|
36
46
|
const dataService = new DataServiceImpl(this._executionContext, this._dataService);
|
|
37
47
|
const queryService = new QueryServiceImpl(this._executionContext, this._dataService);
|
|
@@ -41,14 +51,23 @@ export class ServiceContainer {
|
|
|
41
51
|
dataService,
|
|
42
52
|
queryService,
|
|
43
53
|
queueService,
|
|
54
|
+
functionsAiService: this._functionsService,
|
|
44
55
|
};
|
|
45
56
|
}
|
|
46
57
|
|
|
47
|
-
queryQueue(queue: DXN): Promise<QueryResult> {
|
|
48
|
-
|
|
58
|
+
async queryQueue(queue: DXN): Promise<QueryResult> {
|
|
59
|
+
const { spaceId } = queue.asQueueDXN() ?? {};
|
|
60
|
+
using result = (await this._queueService.query({}, queue.toString(), { spaceId: spaceId! })) as any;
|
|
61
|
+
// Copy returned object to avoid hanging RPC stub
|
|
62
|
+
// See https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle/
|
|
63
|
+
return {
|
|
64
|
+
objects: structuredClone(result.objects),
|
|
65
|
+
nextCursor: result.nextCursor ?? null,
|
|
66
|
+
prevCursor: result.prevCursor ?? null,
|
|
67
|
+
};
|
|
49
68
|
}
|
|
50
69
|
|
|
51
|
-
insertIntoQueue(queue: DXN, objects:
|
|
52
|
-
|
|
70
|
+
async insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void> {
|
|
71
|
+
await this._queueService.append({}, queue.toString(), objects);
|
|
53
72
|
}
|
|
54
73
|
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
/* eslint-disable no-console */
|
|
6
|
+
|
|
7
|
+
import { LogLevel, type LogProcessor, log, shouldLog } from '@dxos/log';
|
|
8
|
+
|
|
9
|
+
export const setupFunctionsLogger = () => {
|
|
10
|
+
log.runtimeConfig.processors.length = 0;
|
|
11
|
+
log.runtimeConfig.processors.push(functionLogProcessor);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const functionLogProcessor: LogProcessor = (config, entry) => {
|
|
15
|
+
if (!shouldLog(entry, config.filters)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
switch (entry.level) {
|
|
20
|
+
case LogLevel.DEBUG:
|
|
21
|
+
console.debug(entry.message, entry.context);
|
|
22
|
+
break;
|
|
23
|
+
case LogLevel.TRACE:
|
|
24
|
+
console.debug(entry.message, entry.context);
|
|
25
|
+
break;
|
|
26
|
+
case LogLevel.VERBOSE:
|
|
27
|
+
console.log(entry.message, entry.context);
|
|
28
|
+
break;
|
|
29
|
+
case LogLevel.INFO:
|
|
30
|
+
console.info(entry.message, entry.context);
|
|
31
|
+
break;
|
|
32
|
+
case LogLevel.WARN:
|
|
33
|
+
console.warn(entry.message, entry.context);
|
|
34
|
+
break;
|
|
35
|
+
case LogLevel.ERROR:
|
|
36
|
+
console.error(entry.message, entry.context);
|
|
37
|
+
break;
|
|
38
|
+
default:
|
|
39
|
+
console.log(entry.message, entry.context);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
};
|
package/src/queues-api.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
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
7
|
import type { QueryResult } from '@dxos/protocols';
|
|
8
8
|
|
|
@@ -15,7 +15,7 @@ import type { ServiceContainer } from './internal';
|
|
|
15
15
|
*/
|
|
16
16
|
export interface QueuesAPI {
|
|
17
17
|
queryQueue(queue: DXN, options?: {}): Promise<QueryResult>;
|
|
18
|
-
insertIntoQueue(queue: DXN, objects:
|
|
18
|
+
insertIntoQueue(queue: DXN, objects: AnyEntity[]): Promise<void>;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -31,7 +31,7 @@ export class QueuesAPIImpl implements QueuesAPI {
|
|
|
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,
|