@dxos/functions 0.8.3-main.672df60 → 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,302 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// packages/core/functions/src/services/ai.ts
|
|
4
|
+
import { Context, Layer } from "effect";
|
|
5
|
+
var AiService = class _AiService extends Context.Tag("AiService")() {
|
|
6
|
+
static {
|
|
7
|
+
this.make = (client) => {
|
|
8
|
+
return {
|
|
9
|
+
get client() {
|
|
10
|
+
return client;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
static {
|
|
16
|
+
this.makeLayer = (client) => Layer.succeed(_AiService, _AiService.make(client));
|
|
17
|
+
}
|
|
18
|
+
static {
|
|
19
|
+
this.notAvailable = Layer.succeed(_AiService, {
|
|
20
|
+
get client() {
|
|
21
|
+
throw new Error("AiService not available");
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// packages/core/functions/src/services/database.ts
|
|
28
|
+
import { Context as Context2, Layer as Layer2 } from "effect";
|
|
29
|
+
var DatabaseService = class _DatabaseService extends Context2.Tag("DatabaseService")() {
|
|
30
|
+
static {
|
|
31
|
+
this.notAvailable = Layer2.succeed(_DatabaseService, {
|
|
32
|
+
get db() {
|
|
33
|
+
throw new Error("Database not available");
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
static {
|
|
38
|
+
this.make = (db) => {
|
|
39
|
+
return {
|
|
40
|
+
get db() {
|
|
41
|
+
return db;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// packages/core/functions/src/services/queues.ts
|
|
49
|
+
import { Context as Context3, Layer as Layer3 } from "effect";
|
|
50
|
+
var QueueService = class _QueueService extends Context3.Tag("QueueService")() {
|
|
51
|
+
static {
|
|
52
|
+
this.notAvailable = Layer3.succeed(_QueueService, {
|
|
53
|
+
get queues() {
|
|
54
|
+
throw new Error("Queues not available");
|
|
55
|
+
},
|
|
56
|
+
contextQueue: void 0
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
static {
|
|
60
|
+
this.make = (queues, contextQueue) => {
|
|
61
|
+
return {
|
|
62
|
+
queues,
|
|
63
|
+
contextQueue
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// packages/core/functions/src/services/credentials.ts
|
|
70
|
+
import { Context as Context4 } from "effect";
|
|
71
|
+
var CredentialsService = class extends Context4.Tag("CredentialsService")() {
|
|
72
|
+
};
|
|
73
|
+
var ConfiguredCredentialsService = class {
|
|
74
|
+
constructor(credentials = []) {
|
|
75
|
+
this.credentials = credentials;
|
|
76
|
+
}
|
|
77
|
+
addCredentials(credentials) {
|
|
78
|
+
this.credentials.push(...credentials);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
async queryCredentials(query) {
|
|
82
|
+
return this.credentials.filter((credential) => credential.service === query.service);
|
|
83
|
+
}
|
|
84
|
+
async getCredential(query) {
|
|
85
|
+
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
86
|
+
if (!credential) {
|
|
87
|
+
throw new Error(`Credential not found for service: ${query.service}`);
|
|
88
|
+
}
|
|
89
|
+
return credential;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// packages/core/functions/src/services/event-logger.ts
|
|
94
|
+
import { Effect, Context as Context5 } from "effect";
|
|
95
|
+
import { invariant } from "@dxos/invariant";
|
|
96
|
+
import { log, LogLevel } from "@dxos/log";
|
|
97
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/functions/src/services/event-logger.ts";
|
|
98
|
+
var EventLogger = class extends Context5.Tag("EventLogger")() {
|
|
99
|
+
static {
|
|
100
|
+
this.noop = {
|
|
101
|
+
log: () => {
|
|
102
|
+
},
|
|
103
|
+
nodeId: void 0
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var logCustomEvent = (data) => Effect.gen(function* () {
|
|
108
|
+
const logger = yield* EventLogger;
|
|
109
|
+
if (!logger.nodeId) {
|
|
110
|
+
throw new Error("logCustomEvent must be called within a node compute function");
|
|
111
|
+
}
|
|
112
|
+
logger.log({
|
|
113
|
+
type: "custom",
|
|
114
|
+
nodeId: logger.nodeId,
|
|
115
|
+
event: data
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
var createDefectLogger = () => Effect.catchAll((error) => Effect.gen(function* () {
|
|
119
|
+
log.error("unhandled effect error", {
|
|
120
|
+
error
|
|
121
|
+
}, {
|
|
122
|
+
F: __dxlog_file,
|
|
123
|
+
L: 65,
|
|
124
|
+
S: this,
|
|
125
|
+
C: (f, a) => f(...a)
|
|
126
|
+
});
|
|
127
|
+
throw error;
|
|
128
|
+
}));
|
|
129
|
+
var createEventLogger = (level, message = "event") => {
|
|
130
|
+
const logFunction = {
|
|
131
|
+
[LogLevel.WARN]: log.warn,
|
|
132
|
+
[LogLevel.VERBOSE]: log.verbose,
|
|
133
|
+
[LogLevel.DEBUG]: log.debug,
|
|
134
|
+
[LogLevel.INFO]: log.info,
|
|
135
|
+
[LogLevel.ERROR]: log.error
|
|
136
|
+
}[level];
|
|
137
|
+
invariant(logFunction, void 0, {
|
|
138
|
+
F: __dxlog_file,
|
|
139
|
+
L: 80,
|
|
140
|
+
S: void 0,
|
|
141
|
+
A: [
|
|
142
|
+
"logFunction",
|
|
143
|
+
""
|
|
144
|
+
]
|
|
145
|
+
});
|
|
146
|
+
return {
|
|
147
|
+
log: (event) => {
|
|
148
|
+
logFunction(message, event);
|
|
149
|
+
},
|
|
150
|
+
nodeId: void 0
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// packages/core/functions/src/services/function-call-service.ts
|
|
155
|
+
import { Context as Context6 } from "effect";
|
|
156
|
+
var FunctionCallService = class extends Context6.Tag("FunctionCallService")() {
|
|
157
|
+
static fromClient(baseUrl, spaceId) {
|
|
158
|
+
return {
|
|
159
|
+
callFunction: async (deployedFunctionId, input) => {
|
|
160
|
+
const url = getInvocationUrl(deployedFunctionId, baseUrl, {
|
|
161
|
+
spaceId
|
|
162
|
+
});
|
|
163
|
+
const result = await fetch(url, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers: {
|
|
166
|
+
"Content-Type": "application/json"
|
|
167
|
+
},
|
|
168
|
+
body: JSON.stringify(input)
|
|
169
|
+
});
|
|
170
|
+
if (result.status >= 300 || result.status < 200) {
|
|
171
|
+
throw new Error("Failed to invoke function", {
|
|
172
|
+
cause: new Error(`HTTP error: ${await result.text()}`)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return await result.json();
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
static {
|
|
180
|
+
this.mock = () => {
|
|
181
|
+
return {
|
|
182
|
+
callFunction: async (deployedFunctionId, input) => {
|
|
183
|
+
return input;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
190
|
+
const baseUrl = new URL("functions/", edgeUrl);
|
|
191
|
+
const relativeUrl = functionUrl.replace(/^\//, "");
|
|
192
|
+
const url = new URL(`./${relativeUrl}`, baseUrl.toString());
|
|
193
|
+
options.spaceId && url.searchParams.set("spaceId", options.spaceId);
|
|
194
|
+
options.subjectId && url.searchParams.set("subjectId", options.subjectId);
|
|
195
|
+
url.protocol = isSecure(url.protocol) ? "https" : "http";
|
|
196
|
+
return url.toString();
|
|
197
|
+
};
|
|
198
|
+
var isSecure = (protocol) => {
|
|
199
|
+
return protocol === "https:" || protocol === "wss:";
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// packages/core/functions/src/services/tracing.ts
|
|
203
|
+
import { Context as Context7 } from "effect";
|
|
204
|
+
var TracingService = class extends Context7.Tag("TracingService")() {
|
|
205
|
+
static {
|
|
206
|
+
this.noop = {
|
|
207
|
+
write: () => {
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
static {
|
|
212
|
+
this.console = {
|
|
213
|
+
write: (event) => {
|
|
214
|
+
console.log(event);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// packages/core/functions/src/services/service-container.ts
|
|
221
|
+
import { Layer as Layer4 } from "effect";
|
|
222
|
+
var SERVICE_MAPPING = {
|
|
223
|
+
[AiService.key]: "ai",
|
|
224
|
+
[CredentialsService.key]: "credentials",
|
|
225
|
+
[DatabaseService.key]: "database",
|
|
226
|
+
[EventLogger.key]: "eventLogger",
|
|
227
|
+
[FunctionCallService.key]: "functionCallService",
|
|
228
|
+
[QueueService.key]: "queues",
|
|
229
|
+
[TracingService.key]: "tracing"
|
|
230
|
+
};
|
|
231
|
+
var SERVICE_TAGS = [
|
|
232
|
+
AiService,
|
|
233
|
+
CredentialsService,
|
|
234
|
+
DatabaseService,
|
|
235
|
+
EventLogger,
|
|
236
|
+
FunctionCallService,
|
|
237
|
+
TracingService,
|
|
238
|
+
QueueService
|
|
239
|
+
];
|
|
240
|
+
var DEFAULT_SERVICES = {
|
|
241
|
+
tracing: TracingService.noop
|
|
242
|
+
};
|
|
243
|
+
var ServiceContainer = class _ServiceContainer {
|
|
244
|
+
constructor() {
|
|
245
|
+
this._services = {
|
|
246
|
+
...DEFAULT_SERVICES
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Set services.
|
|
251
|
+
* @param services - Services to set.
|
|
252
|
+
* @returns The container instance.
|
|
253
|
+
*/
|
|
254
|
+
setServices(services) {
|
|
255
|
+
this._services = {
|
|
256
|
+
...this._services,
|
|
257
|
+
...services
|
|
258
|
+
};
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
getService(tag) {
|
|
262
|
+
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
263
|
+
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
264
|
+
if (!service) {
|
|
265
|
+
throw new Error(`Service not available: ${tag.key}`);
|
|
266
|
+
}
|
|
267
|
+
return service;
|
|
268
|
+
}
|
|
269
|
+
clone() {
|
|
270
|
+
return new _ServiceContainer().setServices({
|
|
271
|
+
...this._services
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
// 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.
|
|
275
|
+
createLayer() {
|
|
276
|
+
const ai = this._services.ai != null ? Layer4.succeed(AiService, this._services.ai) : AiService.notAvailable;
|
|
277
|
+
const credentials = Layer4.succeed(CredentialsService, new ConfiguredCredentialsService());
|
|
278
|
+
const database = this._services.database != null ? Layer4.succeed(DatabaseService, this._services.database) : DatabaseService.notAvailable;
|
|
279
|
+
const queues = this._services.queues != null ? Layer4.succeed(QueueService, this._services.queues) : QueueService.notAvailable;
|
|
280
|
+
const tracing = Layer4.succeed(TracingService, this._services.tracing ?? TracingService.noop);
|
|
281
|
+
const eventLogger = Layer4.succeed(EventLogger, this._services.eventLogger ?? EventLogger.noop);
|
|
282
|
+
const functionCallService = Layer4.succeed(FunctionCallService, this._services.functionCallService ?? FunctionCallService.mock());
|
|
283
|
+
return Layer4.mergeAll(ai, credentials, database, queues, tracing, eventLogger, functionCallService);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
export {
|
|
288
|
+
AiService,
|
|
289
|
+
DatabaseService,
|
|
290
|
+
QueueService,
|
|
291
|
+
CredentialsService,
|
|
292
|
+
ConfiguredCredentialsService,
|
|
293
|
+
EventLogger,
|
|
294
|
+
logCustomEvent,
|
|
295
|
+
createDefectLogger,
|
|
296
|
+
createEventLogger,
|
|
297
|
+
FunctionCallService,
|
|
298
|
+
TracingService,
|
|
299
|
+
SERVICE_TAGS,
|
|
300
|
+
ServiceContainer
|
|
301
|
+
};
|
|
302
|
+
//# sourceMappingURL=chunk-LIYPMWNQ.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, functionCall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,UAAAA,IAAcE;AAClE,QAAI,CAACD,SAAS;AACZ,YAAM,IAAIE,MAAM,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,4 +1,19 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
AiService,
|
|
4
|
+
ConfiguredCredentialsService,
|
|
5
|
+
CredentialsService,
|
|
6
|
+
DatabaseService,
|
|
7
|
+
EventLogger,
|
|
8
|
+
FunctionCallService,
|
|
9
|
+
QueueService,
|
|
10
|
+
SERVICE_TAGS,
|
|
11
|
+
ServiceContainer,
|
|
12
|
+
TracingService,
|
|
13
|
+
createDefectLogger,
|
|
14
|
+
createEventLogger,
|
|
15
|
+
logCustomEvent
|
|
16
|
+
} from "./chunk-LIYPMWNQ.mjs";
|
|
2
17
|
|
|
3
18
|
// packages/core/functions/src/handler.ts
|
|
4
19
|
import { Schema } from "effect";
|
|
@@ -19,7 +34,8 @@ var defineFunction = (params) => {
|
|
|
19
34
|
|
|
20
35
|
// packages/core/functions/src/schema.ts
|
|
21
36
|
import { Schema as Schema2 } from "effect";
|
|
22
|
-
import {
|
|
37
|
+
import { Type } from "@dxos/echo";
|
|
38
|
+
import { JsonSchemaType, LabelAnnotation, Ref } from "@dxos/echo-schema";
|
|
23
39
|
import { DataType } from "@dxos/schema";
|
|
24
40
|
var ScriptType = Schema2.Struct({
|
|
25
41
|
name: Schema2.optional(Schema2.String),
|
|
@@ -28,16 +44,13 @@ var ScriptType = Schema2.Struct({
|
|
|
28
44
|
// Whether source has changed since last deploy.
|
|
29
45
|
changed: Schema2.optional(Schema2.Boolean),
|
|
30
46
|
source: Ref(DataType.Text)
|
|
31
|
-
}).pipe(
|
|
47
|
+
}).pipe(Type.Obj({
|
|
32
48
|
typename: "dxos.org/type/Script",
|
|
33
49
|
version: "0.1.0"
|
|
34
50
|
}), LabelAnnotation.set([
|
|
35
51
|
"name"
|
|
36
52
|
]));
|
|
37
|
-
var FunctionType =
|
|
38
|
-
typename: "dxos.org/type/Function",
|
|
39
|
-
version: "0.1.0"
|
|
40
|
-
})({
|
|
53
|
+
var FunctionType = Schema2.Struct({
|
|
41
54
|
// TODO(burdon): Rename to id/uri?
|
|
42
55
|
name: Schema2.NonEmptyString,
|
|
43
56
|
version: Schema2.String,
|
|
@@ -49,18 +62,23 @@ var FunctionType = class extends TypedObject({
|
|
|
49
62
|
outputSchema: Schema2.optional(JsonSchemaType),
|
|
50
63
|
// Local binding to a function name.
|
|
51
64
|
binding: Schema2.optional(Schema2.String)
|
|
52
|
-
})
|
|
53
|
-
|
|
65
|
+
}).pipe(Type.Obj({
|
|
66
|
+
typename: "dxos.org/type/Function",
|
|
67
|
+
version: "0.1.0"
|
|
68
|
+
}), LabelAnnotation.set([
|
|
69
|
+
"name"
|
|
70
|
+
]));
|
|
54
71
|
|
|
55
72
|
// packages/core/functions/src/trace.ts
|
|
56
73
|
import { Schema as Schema4 } from "effect";
|
|
74
|
+
import { Type as Type2 } from "@dxos/echo";
|
|
57
75
|
import { Queue } from "@dxos/echo-db";
|
|
58
|
-
import {
|
|
76
|
+
import { ObjectId } from "@dxos/echo-schema";
|
|
59
77
|
import { log } from "@dxos/log";
|
|
60
78
|
|
|
61
79
|
// packages/core/functions/src/types.ts
|
|
62
80
|
import { Schema as Schema3, SchemaAST } from "effect";
|
|
63
|
-
import { Expando, OptionsAnnotationId, TypedObject
|
|
81
|
+
import { Expando, OptionsAnnotationId, TypedObject, Ref as Ref2, RawObject } from "@dxos/echo-schema";
|
|
64
82
|
import { DXN } from "@dxos/keys";
|
|
65
83
|
var TriggerKind = /* @__PURE__ */ function(TriggerKind2) {
|
|
66
84
|
TriggerKind2["Timer"] = "timer";
|
|
@@ -197,7 +215,7 @@ var FunctionTriggerSchema = Schema3.Struct({
|
|
|
197
215
|
value: Schema3.Any
|
|
198
216
|
})))
|
|
199
217
|
});
|
|
200
|
-
var FunctionTrigger = class extends
|
|
218
|
+
var FunctionTrigger = class extends TypedObject({
|
|
201
219
|
typename: "dxos.org/type/FunctionTrigger",
|
|
202
220
|
version: "0.2.0"
|
|
203
221
|
})(FunctionTriggerSchema.fields) {
|
|
@@ -252,16 +270,16 @@ var InvocationTraceStartEvent = Schema4.Struct({
|
|
|
252
270
|
/**
|
|
253
271
|
* Queue for function/workflow invocation events.
|
|
254
272
|
*/
|
|
255
|
-
invocationTraceQueue:
|
|
273
|
+
invocationTraceQueue: Type2.Ref(Queue),
|
|
256
274
|
/**
|
|
257
275
|
* DXN of the invoked function/workflow.
|
|
258
276
|
*/
|
|
259
|
-
invocationTarget:
|
|
277
|
+
invocationTarget: Type2.Ref(Type2.Expando),
|
|
260
278
|
/**
|
|
261
279
|
* Present for automatic invocations.
|
|
262
280
|
*/
|
|
263
|
-
trigger: Schema4.optional(
|
|
264
|
-
}).pipe(
|
|
281
|
+
trigger: Schema4.optional(Type2.Ref(FunctionTrigger))
|
|
282
|
+
}).pipe(Type2.Obj({
|
|
265
283
|
typename: "dxos.org/type/InvocationTraceStart",
|
|
266
284
|
version: "0.1.0"
|
|
267
285
|
}));
|
|
@@ -282,7 +300,7 @@ var InvocationTraceEndEvent = Schema4.Struct({
|
|
|
282
300
|
timestampMs: Schema4.Number,
|
|
283
301
|
outcome: Schema4.Enums(InvocationOutcome),
|
|
284
302
|
exception: Schema4.optional(TraceEventException)
|
|
285
|
-
}).pipe(
|
|
303
|
+
}).pipe(Type2.Obj({
|
|
286
304
|
typename: "dxos.org/type/InvocationTraceEnd",
|
|
287
305
|
version: "0.1.0"
|
|
288
306
|
}));
|
|
@@ -303,7 +321,7 @@ var TraceEvent = Schema4.Struct({
|
|
|
303
321
|
ingestionTimestampMs: Schema4.Number,
|
|
304
322
|
logs: Schema4.Array(TraceEventLog),
|
|
305
323
|
exceptions: Schema4.Array(TraceEventException)
|
|
306
|
-
}).pipe(
|
|
324
|
+
}).pipe(Type2.Obj({
|
|
307
325
|
typename: "dxos.org/type/TraceEvent",
|
|
308
326
|
version: "0.1.0"
|
|
309
327
|
}));
|
|
@@ -391,107 +409,6 @@ var getInvocationUrl = (functionUrl, edgeUrl, options = {}) => {
|
|
|
391
409
|
return url.toString();
|
|
392
410
|
};
|
|
393
411
|
|
|
394
|
-
// packages/core/functions/src/services/ai.ts
|
|
395
|
-
import { Context } from "effect";
|
|
396
|
-
var AiService = class extends Context.Tag("AiService")() {
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
// packages/core/functions/src/services/database.ts
|
|
400
|
-
import { Context as Context2 } from "effect";
|
|
401
|
-
var DatabaseService = class extends Context2.Tag("DatabaseService")() {
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
// packages/core/functions/src/services/queues.ts
|
|
405
|
-
import { Context as Context3 } from "effect";
|
|
406
|
-
var QueuesService = class extends Context3.Tag("QueuesService")() {
|
|
407
|
-
};
|
|
408
|
-
|
|
409
|
-
// packages/core/functions/src/services/credentials.ts
|
|
410
|
-
import { Context as Context4 } from "effect";
|
|
411
|
-
var CredentialsService = class extends Context4.Tag("CredentialsService")() {
|
|
412
|
-
};
|
|
413
|
-
var ConfiguredCredentialsService = class {
|
|
414
|
-
constructor(credentials = []) {
|
|
415
|
-
this.credentials = credentials;
|
|
416
|
-
}
|
|
417
|
-
addCredentials(credentials) {
|
|
418
|
-
this.credentials.push(...credentials);
|
|
419
|
-
return this;
|
|
420
|
-
}
|
|
421
|
-
async queryCredentials(query) {
|
|
422
|
-
return this.credentials.filter((credential) => credential.service === query.service);
|
|
423
|
-
}
|
|
424
|
-
async getCredential(query) {
|
|
425
|
-
const credential = this.credentials.find((credential2) => credential2.service === query.service);
|
|
426
|
-
if (!credential) {
|
|
427
|
-
throw new Error(`Credential not found for service: ${query.service}`);
|
|
428
|
-
}
|
|
429
|
-
return credential;
|
|
430
|
-
}
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
// packages/core/functions/src/services/tracing.ts
|
|
434
|
-
import { Context as Context5 } from "effect";
|
|
435
|
-
var TracingService = class extends Context5.Tag("TracingService")() {
|
|
436
|
-
static {
|
|
437
|
-
this.noop = {
|
|
438
|
-
write: () => {
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
static {
|
|
443
|
-
this.console = {
|
|
444
|
-
write: (event) => {
|
|
445
|
-
console.log(event);
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
};
|
|
450
|
-
|
|
451
|
-
// packages/core/functions/src/services/service-container.ts
|
|
452
|
-
var SERVICE_MAPPING = {
|
|
453
|
-
[DatabaseService.key]: "database",
|
|
454
|
-
[AiService.key]: "ai",
|
|
455
|
-
[QueuesService.key]: "queues",
|
|
456
|
-
[CredentialsService.key]: "credentials",
|
|
457
|
-
[TracingService.key]: "tracing"
|
|
458
|
-
};
|
|
459
|
-
var DEFAULT_SERVICES = {
|
|
460
|
-
tracing: TracingService.noop
|
|
461
|
-
};
|
|
462
|
-
var ServiceContainer = class _ServiceContainer {
|
|
463
|
-
constructor() {
|
|
464
|
-
this._services = {
|
|
465
|
-
...DEFAULT_SERVICES
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Set services.
|
|
470
|
-
* @param services - Services to set.
|
|
471
|
-
* @returns The container instance.
|
|
472
|
-
*/
|
|
473
|
-
setServices(services) {
|
|
474
|
-
this._services = {
|
|
475
|
-
...this._services,
|
|
476
|
-
...services
|
|
477
|
-
};
|
|
478
|
-
return this;
|
|
479
|
-
}
|
|
480
|
-
getService(tag) {
|
|
481
|
-
const serviceKey = SERVICE_MAPPING[tag.key];
|
|
482
|
-
const service = serviceKey != null ? this._services[serviceKey] : void 0;
|
|
483
|
-
if (!service) {
|
|
484
|
-
throw new Error(`Service not available: ${tag.key}`);
|
|
485
|
-
}
|
|
486
|
-
return service;
|
|
487
|
-
}
|
|
488
|
-
clone() {
|
|
489
|
-
return new _ServiceContainer().setServices({
|
|
490
|
-
...this._services
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
|
|
495
412
|
// packages/core/functions/src/executor/executor.ts
|
|
496
413
|
import { Effect, Schema as Schema5 } from "effect";
|
|
497
414
|
var FunctionExecutor = class {
|
|
@@ -530,8 +447,10 @@ export {
|
|
|
530
447
|
CredentialsService,
|
|
531
448
|
DatabaseService,
|
|
532
449
|
EmailTriggerOutput,
|
|
450
|
+
EventLogger,
|
|
533
451
|
FUNCTIONS_PRESET_META_KEY,
|
|
534
452
|
FUNCTION_TYPES,
|
|
453
|
+
FunctionCallService,
|
|
535
454
|
FunctionExecutor,
|
|
536
455
|
FunctionManifestSchema,
|
|
537
456
|
FunctionTrigger,
|
|
@@ -541,8 +460,9 @@ export {
|
|
|
541
460
|
InvocationTraceEndEvent,
|
|
542
461
|
InvocationTraceEventType,
|
|
543
462
|
InvocationTraceStartEvent,
|
|
463
|
+
QueueService,
|
|
544
464
|
QueueTriggerOutput,
|
|
545
|
-
|
|
465
|
+
SERVICE_TAGS,
|
|
546
466
|
ScriptType,
|
|
547
467
|
ServiceContainer,
|
|
548
468
|
SubscriptionTriggerOutput,
|
|
@@ -554,10 +474,13 @@ export {
|
|
|
554
474
|
TriggerKind,
|
|
555
475
|
TriggerSchema,
|
|
556
476
|
WebhookTriggerOutput,
|
|
477
|
+
createDefectLogger,
|
|
478
|
+
createEventLogger,
|
|
557
479
|
createInvocationSpans,
|
|
558
480
|
defineFunction,
|
|
559
481
|
getInvocationUrl,
|
|
560
482
|
getUserFunctionUrlInMetadata,
|
|
483
|
+
logCustomEvent,
|
|
561
484
|
makeFunctionUrl,
|
|
562
485
|
setUserFunctionUrlInMetadata
|
|
563
486
|
};
|