@dxos/functions 0.8.3-main.7f5a14c → 0.8.3-staging.0fa589b
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/chunk-WEFZUEL2.mjs +300 -0
- package/dist/lib/browser/chunk-WEFZUEL2.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +43 -119
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +28 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/chunk-IJAE7FZK.cjs +320 -0
- package/dist/lib/node/chunk-IJAE7FZK.cjs.map +7 -0
- package/dist/lib/node/index.cjs +41 -114
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +43 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs +302 -0
- package/dist/lib/node-esm/chunk-LIYPMWNQ.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +42 -119
- 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 +29 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/handler.d.ts +2 -2
- package/dist/types/src/schema.d.ts +16 -35
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/ai.d.ts +6 -3
- package/dist/types/src/services/ai.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +3 -1
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +37 -0
- package/dist/types/src/services/event-logger.d.ts.map +1 -0
- package/dist/types/src/services/function-call-service.d.ts +16 -0
- package/dist/types/src/services/function-call-service.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +2 -0
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +12 -4
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/service-container.d.ts +28 -9
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/logger.d.ts +5 -0
- package/dist/types/src/testing/logger.d.ts.map +1 -0
- package/dist/types/src/testing/services.d.ts +13 -0
- package/dist/types/src/testing/services.d.ts.map +1 -0
- package/dist/types/src/trace.d.ts +19 -44
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +4 -2
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +36 -32
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +25 -19
- package/src/handler.ts +2 -2
- package/src/schema.ts +13 -8
- package/src/services/ai.ts +21 -4
- package/src/services/database.ts +16 -2
- package/src/services/event-logger.ts +87 -0
- package/src/services/function-call-service.ts +64 -0
- package/src/services/index.ts +2 -0
- package/src/services/queues.ts +27 -6
- package/src/services/service-container.ts +66 -15
- package/src/testing/index.ts +5 -0
- package/src/testing/logger.ts +16 -0
- package/src/testing/services.ts +32 -0
- package/src/trace.ts +13 -13
- package/src/translations.ts +6 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// packages/core/functions/src/services/ai.ts
|
|
2
|
+
import { Context, Layer } from "effect";
|
|
3
|
+
var AiService = class _AiService extends Context.Tag("AiService")() {
|
|
4
|
+
static {
|
|
5
|
+
this.make = (client) => {
|
|
6
|
+
return {
|
|
7
|
+
get client() {
|
|
8
|
+
return client;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
static {
|
|
14
|
+
this.makeLayer = (client) => Layer.succeed(_AiService, _AiService.make(client));
|
|
15
|
+
}
|
|
16
|
+
static {
|
|
17
|
+
this.notAvailable = Layer.succeed(_AiService, {
|
|
18
|
+
get client() {
|
|
19
|
+
throw new Error("AiService not available");
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// packages/core/functions/src/services/database.ts
|
|
26
|
+
import { Context as Context2, Layer as Layer2 } from "effect";
|
|
27
|
+
var DatabaseService = class _DatabaseService extends Context2.Tag("DatabaseService")() {
|
|
28
|
+
static {
|
|
29
|
+
this.notAvailable = Layer2.succeed(_DatabaseService, {
|
|
30
|
+
get db() {
|
|
31
|
+
throw new Error("Database not available");
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
static {
|
|
36
|
+
this.make = (db) => {
|
|
37
|
+
return {
|
|
38
|
+
get db() {
|
|
39
|
+
return db;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// packages/core/functions/src/services/queues.ts
|
|
47
|
+
import { Context as Context3, Layer as Layer3 } from "effect";
|
|
48
|
+
var QueueService = class _QueueService extends Context3.Tag("QueueService")() {
|
|
49
|
+
static {
|
|
50
|
+
this.notAvailable = Layer3.succeed(_QueueService, {
|
|
51
|
+
get queues() {
|
|
52
|
+
throw new Error("Queues not available");
|
|
53
|
+
},
|
|
54
|
+
contextQueue: void 0
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
static {
|
|
58
|
+
this.make = (queues, contextQueue) => {
|
|
59
|
+
return {
|
|
60
|
+
queues,
|
|
61
|
+
contextQueue
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// packages/core/functions/src/services/credentials.ts
|
|
68
|
+
import { Context as Context4 } from "effect";
|
|
69
|
+
var CredentialsService = class extends Context4.Tag("CredentialsService")() {
|
|
70
|
+
};
|
|
71
|
+
var ConfiguredCredentialsService = class {
|
|
72
|
+
constructor(credentials = []) {
|
|
73
|
+
this.credentials = credentials;
|
|
74
|
+
}
|
|
75
|
+
addCredentials(credentials) {
|
|
76
|
+
this.credentials.push(...credentials);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
async queryCredentials(query) {
|
|
80
|
+
return this.credentials.filter((credential) => credential.service === query.service);
|
|
81
|
+
}
|
|
82
|
+
async getCredential(query) {
|
|
83
|
+
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
84
|
+
if (!credential) {
|
|
85
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
86
|
+
}
|
|
87
|
+
return credential;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// packages/core/functions/src/services/event-logger.ts
|
|
92
|
+
import { Effect, Context as Context5 } from "effect";
|
|
93
|
+
import { invariant } from "@dxos/invariant";
|
|
94
|
+
import { log, LogLevel } from "@dxos/log";
|
|
95
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/services/event-logger.ts";
|
|
96
|
+
var EventLogger = class extends Context5.Tag("EventLogger")() {
|
|
97
|
+
static {
|
|
98
|
+
this.noop = {
|
|
99
|
+
log: () => {
|
|
100
|
+
},
|
|
101
|
+
nodeId: void 0
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var logCustomEvent = (data) => Effect.gen(function* () {
|
|
106
|
+
const logger = yield* EventLogger;
|
|
107
|
+
if (!logger.nodeId) {
|
|
108
|
+
throw new Error("logCustomEvent must be called within a node compute function");
|
|
109
|
+
}
|
|
110
|
+
logger.log({
|
|
111
|
+
type: "custom",
|
|
112
|
+
nodeId: logger.nodeId,
|
|
113
|
+
event: data
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
var createDefectLogger = () => Effect.catchAll((error) => Effect.gen(function* () {
|
|
117
|
+
log.error("unhandled effect error", {
|
|
118
|
+
error
|
|
119
|
+
}, {
|
|
120
|
+
F: __dxlog_file,
|
|
121
|
+
L: 65,
|
|
122
|
+
S: this,
|
|
123
|
+
C: (f, a) => f(...a)
|
|
124
|
+
});
|
|
125
|
+
throw error;
|
|
126
|
+
}));
|
|
127
|
+
var createEventLogger = (level, message = "event") => {
|
|
128
|
+
const logFunction = {
|
|
129
|
+
[LogLevel.WARN]: log.warn,
|
|
130
|
+
[LogLevel.VERBOSE]: log.verbose,
|
|
131
|
+
[LogLevel.DEBUG]: log.debug,
|
|
132
|
+
[LogLevel.INFO]: log.info,
|
|
133
|
+
[LogLevel.ERROR]: log.error
|
|
134
|
+
}[level];
|
|
135
|
+
invariant(logFunction, void 0, {
|
|
136
|
+
F: __dxlog_file,
|
|
137
|
+
L: 80,
|
|
138
|
+
S: void 0,
|
|
139
|
+
A: [
|
|
140
|
+
"logFunction",
|
|
141
|
+
""
|
|
142
|
+
]
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
log: (event) => {
|
|
146
|
+
logFunction(message, event);
|
|
147
|
+
},
|
|
148
|
+
nodeId: void 0
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// packages/core/functions/src/services/function-call-service.ts
|
|
153
|
+
import { Context as Context6 } from "effect";
|
|
154
|
+
var FunctionCallService = class extends Context6.Tag("FunctionCallService")() {
|
|
155
|
+
static fromClient(baseUrl, spaceId) {
|
|
156
|
+
return {
|
|
157
|
+
callFunction: async (deployedFunctionId, input) => {
|
|
158
|
+
const url = getInvocationUrl(deployedFunctionId, baseUrl, {
|
|
159
|
+
spaceId
|
|
160
|
+
});
|
|
161
|
+
const result = await fetch(url, {
|
|
162
|
+
method: "POST",
|
|
163
|
+
headers: {
|
|
164
|
+
"Content-Type": "application/json"
|
|
165
|
+
},
|
|
166
|
+
body: JSON.stringify(input)
|
|
167
|
+
});
|
|
168
|
+
if (result.status >= 300 || result.status < 200) {
|
|
169
|
+
throw new Error("Failed to invoke function", {
|
|
170
|
+
cause: new Error(`HTTP error: ${await result.text()}`)
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return await result.json();
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
static {
|
|
178
|
+
this.mock = () => {
|
|
179
|
+
return {
|
|
180
|
+
callFunction: async (deployedFunctionId, input) => {
|
|
181
|
+
return input;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
188
|
+
const baseUrl = new URL("functions/", edgeUrl);
|
|
189
|
+
const relativeUrl = functionUrl.replace(/^\//, "");
|
|
190
|
+
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
191
|
+
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
192
|
+
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
193
|
+
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
194
|
+
return url.toString();
|
|
195
|
+
};
|
|
196
|
+
var isSecure = (protocol) => {
|
|
197
|
+
return protocol === "https:" || protocol === "wss:";
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// packages/core/functions/src/services/tracing.ts
|
|
201
|
+
import { Context as Context7 } from "effect";
|
|
202
|
+
var TracingService = class extends Context7.Tag("TracingService")() {
|
|
203
|
+
static {
|
|
204
|
+
this.noop = {
|
|
205
|
+
write: () => {
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
static {
|
|
210
|
+
this.console = {
|
|
211
|
+
write: (event) => {
|
|
212
|
+
console.log(event);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// packages/core/functions/src/services/service-container.ts
|
|
219
|
+
import { Layer as Layer4 } from "effect";
|
|
220
|
+
var SERVICE_MAPPING = {
|
|
221
|
+
[AiService.key]: "ai",
|
|
222
|
+
[CredentialsService.key]: "credentials",
|
|
223
|
+
[DatabaseService.key]: "database",
|
|
224
|
+
[EventLogger.key]: "eventLogger",
|
|
225
|
+
[FunctionCallService.key]: "functionCallService",
|
|
226
|
+
[QueueService.key]: "queues",
|
|
227
|
+
[TracingService.key]: "tracing"
|
|
228
|
+
};
|
|
229
|
+
var SERVICE_TAGS = [
|
|
230
|
+
AiService,
|
|
231
|
+
CredentialsService,
|
|
232
|
+
DatabaseService,
|
|
233
|
+
EventLogger,
|
|
234
|
+
FunctionCallService,
|
|
235
|
+
TracingService,
|
|
236
|
+
QueueService
|
|
237
|
+
];
|
|
238
|
+
var DEFAULT_SERVICES = {
|
|
239
|
+
tracing: TracingService.noop
|
|
240
|
+
};
|
|
241
|
+
var ServiceContainer = class _ServiceContainer {
|
|
242
|
+
constructor() {
|
|
243
|
+
this._services = {
|
|
244
|
+
...DEFAULT_SERVICES
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Set services.
|
|
249
|
+
* @param services - Services to set.
|
|
250
|
+
* @returns The container instance.
|
|
251
|
+
*/
|
|
252
|
+
setServices(services) {
|
|
253
|
+
this._services = {
|
|
254
|
+
...this._services,
|
|
255
|
+
...services
|
|
256
|
+
};
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
getService(tag) {
|
|
260
|
+
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
261
|
+
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
262
|
+
if (!service) {
|
|
263
|
+
throw new Error(`Service not available: ${tag.key}`);
|
|
264
|
+
}
|
|
265
|
+
return service;
|
|
266
|
+
}
|
|
267
|
+
clone() {
|
|
268
|
+
return new _ServiceContainer().setServices({
|
|
269
|
+
...this._services
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
// 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.
|
|
273
|
+
createLayer() {
|
|
274
|
+
const ai = this._services.ai != null ? Layer4.succeed(AiService, this._services.ai) : AiService.notAvailable;
|
|
275
|
+
const credentials = Layer4.succeed(CredentialsService, new ConfiguredCredentialsService());
|
|
276
|
+
const database = this._services.database != null ? Layer4.succeed(DatabaseService, this._services.database) : DatabaseService.notAvailable;
|
|
277
|
+
const queues = this._services.queues != null ? Layer4.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
278
|
+
const tracing = Layer4.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
279
|
+
const eventLogger = Layer4.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
|
|
280
|
+
const functionCallService = Layer4.succeed(FunctionCallService, this._services.functionCallService ?? FunctionCallService.mock());
|
|
281
|
+
return Layer4.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
export {
|
|
286
|
+
AiService,
|
|
287
|
+
DatabaseService,
|
|
288
|
+
QueueService,
|
|
289
|
+
CredentialsService,
|
|
290
|
+
ConfiguredCredentialsService,
|
|
291
|
+
EventLogger,
|
|
292
|
+
logCustomEvent,
|
|
293
|
+
createDefectLogger,
|
|
294
|
+
createEventLogger,
|
|
295
|
+
FunctionCallService,
|
|
296
|
+
TracingService,
|
|
297
|
+
SERVICE_TAGS,
|
|
298
|
+
ServiceContainer
|
|
299
|
+
};
|
|
300
|
+
//# sourceMappingURL=chunk-WEFZUEL2.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/services/ai.ts", "../../../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, Layer } from 'effect';\n\nimport type { AiServiceClient } from '@dxos/ai';\n\n// TODO(burdon): Move to @dxos/ai.\nexport class AiService extends Context.Tag('AiService')<\n AiService,\n {\n readonly client: AiServiceClient;\n }\n>() {\n static make = (client: AiServiceClient): Context.Tag.Service<AiService> => {\n return {\n get client() {\n return client;\n },\n };\n };\n\n static makeLayer = (client: AiServiceClient): Layer.Layer<AiService> =>\n Layer.succeed(AiService, AiService.make(client));\n\n static notAvailable = Layer.succeed(AiService, {\n get client(): AiServiceClient {\n throw new Error('AiService not available');\n },\n });\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context, Layer } from 'effect';\n\nimport type { EchoDatabase } from '@dxos/echo-db';\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", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context, Layer } from 'effect';\n\nimport type { Queue, QueueFactory } from '@dxos/echo-db';\n\nexport class QueueService extends Context.Tag('QueueService')<\n QueueService,\n {\n /**\n * API to access the queues.\n */\n readonly queues: QueueFactory;\n\n /**\n * The queue that is used to store the context of the current research.\n */\n // TODO(dmaretskyi): Is this really part of the queue service?\n readonly contextQueue: Queue | undefined;\n }\n>() {\n static notAvailable = Layer.succeed(QueueService, {\n get queues(): QueueFactory {\n throw new Error('Queues not available');\n },\n contextQueue: undefined,\n });\n\n static make = (queues: QueueFactory, contextQueue: Queue | undefined): Context.Tag.Service<QueueService> => {\n return {\n queues,\n contextQueue,\n };\n };\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { Context } 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\ntype 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\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 } from 'effect';\n\nimport type { AnyEchoObject } from '@dxos/echo-schema';\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// Copyright 2025 DXOS.org\n//\n\nimport { Layer, type Context } from 'effect';\n\nimport { AiService } from './ai';\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/**\n * List of all service tags and their names.\n */\nexport interface ServiceTagRecord {\n ai: AiService;\n credentials: CredentialsService;\n database: DatabaseService;\n eventLogger: EventLogger;\n functionCallService: FunctionCallService;\n tracing: TracingService;\n queues: QueueService;\n}\n\n/**\n * List of all services and 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.\n */\nexport type Services = ServiceTagRecord[keyof ServiceTagRecord];\n\nconst SERVICE_MAPPING: Record<string, keyof ServiceRecord> = {\n [AiService.key]: 'ai',\n [CredentialsService.key]: 'credentials',\n [DatabaseService.key]: 'database',\n [EventLogger.key]: 'eventLogger',\n [FunctionCallService.key]: 'functionCallService',\n [QueueService.key]: 'queues',\n [TracingService.key]: 'tracing',\n};\n\nexport const SERVICE_TAGS: Context.Tag<any, any>[] = [\n AiService,\n CredentialsService,\n DatabaseService,\n EventLogger,\n FunctionCallService,\n TracingService,\n QueueService,\n];\n\nconst DEFAULT_SERVICES: Partial<ServiceRecord> = {\n tracing: TracingService.noop,\n};\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<T extends Context.Tag<any, any>>(tag: T): Context.Tag.Service<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 return service as Context.Tag.Service<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(CredentialsService, new ConfiguredCredentialsService());\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, credentials, database, queues, tracing, eventLogger, functionCallService);\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,SAASC,aAAa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AM,0BAA0BJ,IAAIhB,GAAG,EAAE;IACrD;AACA,WAAOkB;EACT;EAEAG,QAA0B;AACxB,WAAO,IAAIV,kBAAAA,EAAmBE,YAAY;MAAE,GAAG,KAAKD;IAAU,CAAA;EAChE;;EAGAU,cAAqC;AACnC,UAAMC,KAAK,KAAKX,UAAUW,MAAM,OAAOC,OAAMC,QAAQ1B,WAAW,KAAKa,UAAUW,EAAE,IAAIxB,UAAU2B;AAC/F,UAAMC,cAAcH,OAAMC,QAAQxB,oBAAoB,IAAI2B,6BAAAA,CAAAA;AAC1D,UAAMC,WACJ,KAAKjB,UAAUiB,YAAY,OACvBL,OAAMC,QAAQvB,iBAAiB,KAAKU,UAAUiB,QAAQ,IACtD3B,gBAAgBwB;AACtB,UAAMI,SACJ,KAAKlB,UAAUkB,UAAU,OAAON,OAAMC,QAAQpB,cAAc,KAAKO,UAAUkB,MAAM,IAAIzB,aAAaqB;AACpG,UAAMjB,UAAUe,OAAMC,QAAQnB,gBAAgB,KAAKM,UAAUH,WAAWH,eAAeI,IAAI;AAC3F,UAAMqB,cAAcP,OAAMC,QAAQtB,aAAa,KAAKS,UAAUmB,eAAe5B,YAAYO,IAAI;AAC7F,UAAMsB,sBAAsBR,OAAMC,QAChCrB,qBACA,KAAKQ,UAAUoB,uBAAuB5B,oBAAoB6B,KAAI,CAAA;AAGhE,WAAOT,OAAMU,SAASX,IAAII,aAAaE,UAAUC,QAAQrB,SAASsB,aAAaC,mBAAAA;EACjF;AACF;",
|
|
6
|
+
"names": ["Context", "Layer", "AiService", "Context", "Tag", "make", "client", "makeLayer", "Layer", "succeed", "notAvailable", "Error", "Context", "Layer", "DatabaseService", "Context", "Tag", "notAvailable", "Layer", "succeed", "db", "Error", "make", "Context", "Layer", "QueueService", "Context", "Tag", "notAvailable", "Layer", "succeed", "queues", "Error", "contextQueue", "undefined", "make", "Context", "CredentialsService", "Context", "Tag", "ConfiguredCredentialsService", "credentials", "addCredentials", "push", "queryCredentials", "query", "filter", "credential", "service", "getCredential", "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", "TracingService", "Context", "Tag", "noop", "write", "console", "event", "log", "Layer", "SERVICE_MAPPING", "AiService", "key", "CredentialsService", "DatabaseService", "EventLogger", "FunctionCallService", "QueueService", "TracingService", "SERVICE_TAGS", "DEFAULT_SERVICES", "tracing", "noop", "ServiceContainer", "_services", "setServices", "services", "getService", "tag", "serviceKey", "service", "undefined", "Error", "clone", "createLayer", "ai", "Layer", "succeed", "notAvailable", "credentials", "ConfiguredCredentialsService", "database", "queues", "eventLogger", "functionCallService", "mock", "mergeAll"]
|
|
7
|
+
}
|
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AiService,
|
|
3
|
+
ConfiguredCredentialsService,
|
|
4
|
+
CredentialsService,
|
|
5
|
+
DatabaseService,
|
|
6
|
+
EventLogger,
|
|
7
|
+
FunctionCallService,
|
|
8
|
+
QueueService,
|
|
9
|
+
SERVICE_TAGS,
|
|
10
|
+
ServiceContainer,
|
|
11
|
+
TracingService,
|
|
12
|
+
createDefectLogger,
|
|
13
|
+
createEventLogger,
|
|
14
|
+
logCustomEvent
|
|
15
|
+
} from "./chunk-WEFZUEL2.mjs";
|
|
16
|
+
|
|
1
17
|
// packages/core/functions/src/handler.ts
|
|
2
18
|
import { Schema } from "effect";
|
|
3
19
|
var defineFunction = (params) => {
|
|
@@ -17,7 +33,8 @@ var defineFunction = (params) => {
|
|
|
17
33
|
|
|
18
34
|
// packages/core/functions/src/schema.ts
|
|
19
35
|
import { Schema as Schema2 } from "effect";
|
|
20
|
-
import {
|
|
36
|
+
import { Type } from "@dxos/echo";
|
|
37
|
+
import { JsonSchemaType, LabelAnnotation, Ref } from "@dxos/echo-schema";
|
|
21
38
|
import { DataType } from "@dxos/schema";
|
|
22
39
|
var ScriptType = Schema2.Struct({
|
|
23
40
|
name: Schema2.optional(Schema2.String),
|
|
@@ -26,16 +43,13 @@ var ScriptType = Schema2.Struct({
|
|
|
26
43
|
// Whether source has changed since last deploy.
|
|
27
44
|
changed: Schema2.optional(Schema2.Boolean),
|
|
28
45
|
source: Ref(DataType.Text)
|
|
29
|
-
}).pipe(
|
|
46
|
+
}).pipe(Type.Obj({
|
|
30
47
|
typename: "dxos.org/type/Script",
|
|
31
48
|
version: "0.1.0"
|
|
32
49
|
}), LabelAnnotation.set([
|
|
33
50
|
"name"
|
|
34
51
|
]));
|
|
35
|
-
var FunctionType =
|
|
36
|
-
typename: "dxos.org/type/Function",
|
|
37
|
-
version: "0.1.0"
|
|
38
|
-
})({
|
|
52
|
+
var FunctionType = Schema2.Struct({
|
|
39
53
|
// TODO(burdon): Rename to id/uri?
|
|
40
54
|
name: Schema2.NonEmptyString,
|
|
41
55
|
version: Schema2.String,
|
|
@@ -47,18 +61,23 @@ var FunctionType = class extends TypedObject({
|
|
|
47
61
|
outputSchema: Schema2.optional(JsonSchemaType),
|
|
48
62
|
// Local binding to a function name.
|
|
49
63
|
binding: Schema2.optional(Schema2.String)
|
|
50
|
-
})
|
|
51
|
-
|
|
64
|
+
}).pipe(Type.Obj({
|
|
65
|
+
typename: "dxos.org/type/Function",
|
|
66
|
+
version: "0.1.0"
|
|
67
|
+
}), LabelAnnotation.set([
|
|
68
|
+
"name"
|
|
69
|
+
]));
|
|
52
70
|
|
|
53
71
|
// packages/core/functions/src/trace.ts
|
|
54
72
|
import { Schema as Schema4 } from "effect";
|
|
73
|
+
import { Type as Type2 } from "@dxos/echo";
|
|
55
74
|
import { Queue } from "@dxos/echo-db";
|
|
56
|
-
import {
|
|
75
|
+
import { ObjectId } from "@dxos/echo-schema";
|
|
57
76
|
import { log } from "@dxos/log";
|
|
58
77
|
|
|
59
78
|
// packages/core/functions/src/types.ts
|
|
60
79
|
import { Schema as Schema3, SchemaAST } from "effect";
|
|
61
|
-
import { Expando, OptionsAnnotationId, TypedObject
|
|
80
|
+
import { Expando, OptionsAnnotationId, TypedObject, Ref as Ref2, RawObject } from "@dxos/echo-schema";
|
|
62
81
|
import { DXN } from "@dxos/keys";
|
|
63
82
|
var TriggerKind = /* @__PURE__ */ function(TriggerKind2) {
|
|
64
83
|
TriggerKind2["Timer"] = "timer";
|
|
@@ -195,7 +214,7 @@ var FunctionTriggerSchema = Schema3.Struct({
|
|
|
195
214
|
value: Schema3.Any
|
|
196
215
|
})))
|
|
197
216
|
});
|
|
198
|
-
var FunctionTrigger = class extends
|
|
217
|
+
var FunctionTrigger = class extends TypedObject({
|
|
199
218
|
typename: "dxos.org/type/FunctionTrigger",
|
|
200
219
|
version: "0.2.0"
|
|
201
220
|
})(FunctionTriggerSchema.fields) {
|
|
@@ -250,16 +269,16 @@ var InvocationTraceStartEvent = Schema4.Struct({
|
|
|
250
269
|
/**
|
|
251
270
|
* Queue for function/workflow invocation events.
|
|
252
271
|
*/
|
|
253
|
-
invocationTraceQueue:
|
|
272
|
+
invocationTraceQueue: Type2.Ref(Queue),
|
|
254
273
|
/**
|
|
255
274
|
* DXN of the invoked function/workflow.
|
|
256
275
|
*/
|
|
257
|
-
invocationTarget:
|
|
276
|
+
invocationTarget: Type2.Ref(Type2.Expando),
|
|
258
277
|
/**
|
|
259
278
|
* Present for automatic invocations.
|
|
260
279
|
*/
|
|
261
|
-
trigger: Schema4.optional(
|
|
262
|
-
}).pipe(
|
|
280
|
+
trigger: Schema4.optional(Type2.Ref(FunctionTrigger))
|
|
281
|
+
}).pipe(Type2.Obj({
|
|
263
282
|
typename: "dxos.org/type/InvocationTraceStart",
|
|
264
283
|
version: "0.1.0"
|
|
265
284
|
}));
|
|
@@ -280,7 +299,7 @@ var InvocationTraceEndEvent = Schema4.Struct({
|
|
|
280
299
|
timestampMs: Schema4.Number,
|
|
281
300
|
outcome: Schema4.Enums(InvocationOutcome),
|
|
282
301
|
exception: Schema4.optional(TraceEventException)
|
|
283
|
-
}).pipe(
|
|
302
|
+
}).pipe(Type2.Obj({
|
|
284
303
|
typename: "dxos.org/type/InvocationTraceEnd",
|
|
285
304
|
version: "0.1.0"
|
|
286
305
|
}));
|
|
@@ -301,7 +320,7 @@ var TraceEvent = Schema4.Struct({
|
|
|
301
320
|
ingestionTimestampMs: Schema4.Number,
|
|
302
321
|
logs: Schema4.Array(TraceEventLog),
|
|
303
322
|
exceptions: Schema4.Array(TraceEventException)
|
|
304
|
-
}).pipe(
|
|
323
|
+
}).pipe(Type2.Obj({
|
|
305
324
|
typename: "dxos.org/type/TraceEvent",
|
|
306
325
|
version: "0.1.0"
|
|
307
326
|
}));
|
|
@@ -389,107 +408,6 @@ var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
|
389
408
|
return url.toString();
|
|
390
409
|
};
|
|
391
410
|
|
|
392
|
-
// packages/core/functions/src/services/ai.ts
|
|
393
|
-
import { Context } from "effect";
|
|
394
|
-
var AiService = class extends Context.Tag("AiService")() {
|
|
395
|
-
};
|
|
396
|
-
|
|
397
|
-
// packages/core/functions/src/services/database.ts
|
|
398
|
-
import { Context as Context2 } from "effect";
|
|
399
|
-
var DatabaseService = class extends Context2.Tag("DatabaseService")() {
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
// packages/core/functions/src/services/queues.ts
|
|
403
|
-
import { Context as Context3 } from "effect";
|
|
404
|
-
var QueuesService = class extends Context3.Tag("QueuesService")() {
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
// packages/core/functions/src/services/credentials.ts
|
|
408
|
-
import { Context as Context4 } from "effect";
|
|
409
|
-
var CredentialsService = class extends Context4.Tag("CredentialsService")() {
|
|
410
|
-
};
|
|
411
|
-
var ConfiguredCredentialsService = class {
|
|
412
|
-
constructor(credentials = []) {
|
|
413
|
-
this.credentials = credentials;
|
|
414
|
-
}
|
|
415
|
-
addCredentials(credentials) {
|
|
416
|
-
this.credentials.push(...credentials);
|
|
417
|
-
return this;
|
|
418
|
-
}
|
|
419
|
-
async queryCredentials(query) {
|
|
420
|
-
return this.credentials.filter((credential) => credential.service === query.service);
|
|
421
|
-
}
|
|
422
|
-
async getCredential(query) {
|
|
423
|
-
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
424
|
-
if (!credential) {
|
|
425
|
-
throw new Error(`Credential not found for service: ${query.service}`);
|
|
426
|
-
}
|
|
427
|
-
return credential;
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
|
|
431
|
-
// packages/core/functions/src/services/tracing.ts
|
|
432
|
-
import { Context as Context5 } from "effect";
|
|
433
|
-
var TracingService = class extends Context5.Tag("TracingService")() {
|
|
434
|
-
static {
|
|
435
|
-
this.noop = {
|
|
436
|
-
write: () => {
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
static {
|
|
441
|
-
this.console = {
|
|
442
|
-
write: (event) => {
|
|
443
|
-
console.log(event);
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
|
|
449
|
-
// packages/core/functions/src/services/service-container.ts
|
|
450
|
-
var SERVICE_MAPPING = {
|
|
451
|
-
[DatabaseService.key]: "database",
|
|
452
|
-
[AiService.key]: "ai",
|
|
453
|
-
[QueuesService.key]: "queues",
|
|
454
|
-
[CredentialsService.key]: "credentials",
|
|
455
|
-
[TracingService.key]: "tracing"
|
|
456
|
-
};
|
|
457
|
-
var DEFAULT_SERVICES = {
|
|
458
|
-
tracing: TracingService.noop
|
|
459
|
-
};
|
|
460
|
-
var ServiceContainer = class _ServiceContainer {
|
|
461
|
-
constructor() {
|
|
462
|
-
this._services = {
|
|
463
|
-
...DEFAULT_SERVICES
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* Set services.
|
|
468
|
-
* @param services - Services to set.
|
|
469
|
-
* @returns The container instance.
|
|
470
|
-
*/
|
|
471
|
-
setServices(services) {
|
|
472
|
-
this._services = {
|
|
473
|
-
...this._services,
|
|
474
|
-
...services
|
|
475
|
-
};
|
|
476
|
-
return this;
|
|
477
|
-
}
|
|
478
|
-
getService(tag) {
|
|
479
|
-
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
480
|
-
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
481
|
-
if (!service) {
|
|
482
|
-
throw new Error(`Service not available: ${tag.key}`);
|
|
483
|
-
}
|
|
484
|
-
return service;
|
|
485
|
-
}
|
|
486
|
-
clone() {
|
|
487
|
-
return new _ServiceContainer().setServices({
|
|
488
|
-
...this._services
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
|
|
493
411
|
// packages/core/functions/src/executor/executor.ts
|
|
494
412
|
import { Effect, Schema as Schema5 } from "effect";
|
|
495
413
|
var FunctionExecutor = class {
|
|
@@ -528,8 +446,10 @@ export {
|
|
|
528
446
|
CredentialsService,
|
|
529
447
|
DatabaseService,
|
|
530
448
|
EmailTriggerOutput,
|
|
449
|
+
EventLogger,
|
|
531
450
|
FUNCTIONS_PRESET_META_KEY,
|
|
532
451
|
FUNCTION_TYPES,
|
|
452
|
+
FunctionCallService,
|
|
533
453
|
FunctionExecutor,
|
|
534
454
|
FunctionManifestSchema,
|
|
535
455
|
FunctionTrigger,
|
|
@@ -539,8 +459,9 @@ export {
|
|
|
539
459
|
InvocationTraceEndEvent,
|
|
540
460
|
InvocationTraceEventType,
|
|
541
461
|
InvocationTraceStartEvent,
|
|
462
|
+
QueueService,
|
|
542
463
|
QueueTriggerOutput,
|
|
543
|
-
|
|
464
|
+
SERVICE_TAGS,
|
|
544
465
|
ScriptType,
|
|
545
466
|
ServiceContainer,
|
|
546
467
|
SubscriptionTriggerOutput,
|
|
@@ -552,10 +473,13 @@ export {
|
|
|
552
473
|
TriggerKind,
|
|
553
474
|
TriggerSchema,
|
|
554
475
|
WebhookTriggerOutput,
|
|
476
|
+
createDefectLogger,
|
|
477
|
+
createEventLogger,
|
|
555
478
|
createInvocationSpans,
|
|
556
479
|
defineFunction,
|
|
557
480
|
getInvocationUrl,
|
|
558
481
|
getUserFunctionUrlInMetadata,
|
|
482
|
+
logCustomEvent,
|
|
559
483
|
makeFunctionUrl,
|
|
560
484
|
setUserFunctionUrlInMetadata
|
|
561
485
|
};
|