@dxos/functions 0.8.4-main.dedc0f3 → 0.8.4-main.ead640a
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/bundler/index.mjs +10 -6
- package/dist/lib/browser/bundler/index.mjs.map +3 -3
- package/dist/lib/browser/{chunk-ANP3DFCO.mjs → chunk-LKYT2SAL.mjs} +179 -137
- package/dist/lib/browser/chunk-LKYT2SAL.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +320 -190
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +11 -9
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/bundler/index.mjs +10 -6
- package/dist/lib/node-esm/bundler/index.mjs.map +3 -3
- package/dist/lib/node-esm/{chunk-MPKVY7ZR.mjs → chunk-NAQIKLZB.mjs} +179 -137
- package/dist/lib/node-esm/chunk-NAQIKLZB.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +320 -190
- 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 +11 -9
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/bundler/bundler.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +39 -39
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/examples/fib.d.ts.map +1 -1
- package/dist/types/src/examples/reply.d.ts.map +1 -1
- package/dist/types/src/examples/sleep.d.ts.map +1 -1
- package/dist/types/src/executor/executor.d.ts +3 -0
- package/dist/types/src/executor/executor.d.ts.map +1 -1
- package/dist/types/src/handler.d.ts +19 -4
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/schema.d.ts +5 -5
- package/dist/types/src/schema.d.ts.map +1 -1
- package/dist/types/src/services/credentials.d.ts +6 -2
- package/dist/types/src/services/credentials.d.ts.map +1 -1
- package/dist/types/src/services/database.d.ts +7 -38
- package/dist/types/src/services/database.d.ts.map +1 -1
- package/dist/types/src/services/event-logger.d.ts +4 -1
- package/dist/types/src/services/event-logger.d.ts.map +1 -1
- package/dist/types/src/services/function-invocation-service.d.ts +28 -0
- package/dist/types/src/services/function-invocation-service.d.ts.map +1 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts +2 -0
- package/dist/types/src/services/function-invocation-service.test.d.ts.map +1 -0
- package/dist/types/src/services/index.d.ts +4 -3
- package/dist/types/src/services/index.d.ts.map +1 -1
- package/dist/types/src/services/local-function-execution.d.ts +18 -9
- package/dist/types/src/services/local-function-execution.d.ts.map +1 -1
- package/dist/types/src/services/queues.d.ts +3 -1
- package/dist/types/src/services/queues.d.ts.map +1 -1
- package/dist/types/src/services/remote-function-execution-service.d.ts +11 -4
- package/dist/types/src/services/remote-function-execution-service.d.ts.map +1 -1
- package/dist/types/src/services/service-container.d.ts +2 -1
- package/dist/types/src/services/service-container.d.ts.map +1 -1
- package/dist/types/src/services/service-registry.d.ts +3 -1
- package/dist/types/src/services/service-registry.d.ts.map +1 -1
- package/dist/types/src/services/tracing.d.ts +5 -3
- package/dist/types/src/services/tracing.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +6 -3
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts +1 -1
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/services.d.ts +1 -1
- package/dist/types/src/testing/services.d.ts.map +1 -1
- package/dist/types/src/trace.d.ts +1 -1
- package/dist/types/src/trace.d.ts.map +1 -1
- package/dist/types/src/triggers/invocation-tracer.d.ts +3 -1
- package/dist/types/src/triggers/invocation-tracer.d.ts.map +1 -1
- package/dist/types/src/triggers/trigger-dispatcher.d.ts +7 -4
- package/dist/types/src/triggers/trigger-dispatcher.d.ts.map +1 -1
- package/dist/types/src/triggers/trigger-state-store.d.ts +5 -4
- package/dist/types/src/triggers/trigger-state-store.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +49 -30
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/url.d.ts +1 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -36
- package/src/bundler/bundler.ts +7 -3
- package/src/errors.ts +9 -9
- package/src/examples/fib.ts +4 -2
- package/src/examples/reply.ts +5 -2
- package/src/examples/sleep.ts +4 -2
- package/src/executor/executor.ts +5 -1
- package/src/handler.ts +33 -9
- package/src/schema.ts +5 -2
- package/src/services/credentials.ts +9 -2
- package/src/services/database.ts +19 -14
- package/src/services/event-logger.ts +4 -1
- package/src/services/function-invocation-service.test.ts +81 -0
- package/src/services/function-invocation-service.ts +84 -0
- package/src/services/index.ts +4 -3
- package/src/services/local-function-execution.ts +66 -27
- package/src/services/queues.ts +3 -1
- package/src/services/remote-function-execution-service.ts +38 -21
- package/src/services/service-container.ts +2 -1
- package/src/services/service-registry.test.ts +4 -1
- package/src/services/service-registry.ts +7 -3
- package/src/services/tracing.ts +17 -14
- package/src/testing/layer.ts +10 -7
- package/src/testing/logger.ts +2 -1
- package/src/testing/persist-database.test.ts +4 -4
- package/src/testing/services.ts +1 -1
- package/src/trace.ts +2 -2
- package/src/triggers/invocation-tracer.ts +3 -1
- package/src/triggers/trigger-dispatcher.test.ts +29 -16
- package/src/triggers/trigger-dispatcher.ts +29 -8
- package/src/triggers/trigger-state-store.ts +7 -6
- package/src/types.ts +30 -12
- package/src/url.ts +1 -1
- package/dist/lib/browser/chunk-ANP3DFCO.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-MPKVY7ZR.mjs.map +0 -7
|
@@ -9,12 +9,16 @@ import {
|
|
|
9
9
|
DatabaseService,
|
|
10
10
|
FUNCTIONS_META_KEY,
|
|
11
11
|
FUNCTIONS_PRESET_META_KEY,
|
|
12
|
+
FunctionError,
|
|
13
|
+
FunctionNotFoundError,
|
|
12
14
|
MESSAGE_PROPERTY_TOOL_CALL_ID,
|
|
13
15
|
QueueService,
|
|
14
16
|
RemoteFunctionExecutionService,
|
|
15
17
|
SERVICE_TAGS,
|
|
16
18
|
ServiceContainer,
|
|
19
|
+
ServiceNotAvailableError,
|
|
17
20
|
TracingService,
|
|
21
|
+
TriggerStateNotFoundError,
|
|
18
22
|
createDefectLogger,
|
|
19
23
|
createEventLogger,
|
|
20
24
|
getInvocationUrl,
|
|
@@ -22,37 +26,21 @@ import {
|
|
|
22
26
|
logCustomEvent,
|
|
23
27
|
setUserFunctionIdInMetadata,
|
|
24
28
|
withAuthorization
|
|
25
|
-
} from "./chunk-
|
|
29
|
+
} from "./chunk-NAQIKLZB.mjs";
|
|
26
30
|
import {
|
|
27
31
|
__export
|
|
28
32
|
} from "./chunk-HSLMI22Q.mjs";
|
|
29
33
|
|
|
30
|
-
// src/errors.ts
|
|
31
|
-
import { BaseError } from "@dxos/errors";
|
|
32
|
-
var ServiceNotAvailableError = class extends BaseError.extend("SERVICE_NOT_AVAILABLE") {
|
|
33
|
-
constructor(serviceName) {
|
|
34
|
-
super(`Service not available: ${serviceName}`);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
var FunctionNotFoundError = class extends BaseError.extend("FUNCTION_NOT_FOUND") {
|
|
38
|
-
constructor(functionKey) {
|
|
39
|
-
super(`Function not found: ${functionKey}`);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
var FunctionError = class extends BaseError.extend("FUNCTION_ERROR") {
|
|
43
|
-
};
|
|
44
|
-
var TriggerStateNotFoundError = class extends BaseError.extend("TRIGGER_STATE_NOT_FOUND") {
|
|
45
|
-
};
|
|
46
|
-
|
|
47
34
|
// src/handler.ts
|
|
48
|
-
import
|
|
35
|
+
import * as Effect from "effect/Effect";
|
|
36
|
+
import * as Schema2 from "effect/Schema";
|
|
49
37
|
import { Obj, Type as Type2 } from "@dxos/echo";
|
|
50
38
|
import { assertArgument } from "@dxos/invariant";
|
|
51
39
|
|
|
52
40
|
// src/schema.ts
|
|
53
|
-
import
|
|
41
|
+
import * as Schema from "effect/Schema";
|
|
54
42
|
import { Type } from "@dxos/echo";
|
|
55
|
-
import { JsonSchemaType, LabelAnnotation, Ref } from "@dxos/echo
|
|
43
|
+
import { JsonSchemaType, LabelAnnotation, Ref } from "@dxos/echo/internal";
|
|
56
44
|
import { DataType } from "@dxos/schema";
|
|
57
45
|
var ScriptType = Schema.Struct({
|
|
58
46
|
name: Schema.optional(Schema.String),
|
|
@@ -133,7 +121,7 @@ var defineFunction = ({ key, name, description, inputSchema, outputSchema = Sche
|
|
|
133
121
|
};
|
|
134
122
|
return {
|
|
135
123
|
[typeId]: true,
|
|
136
|
-
key
|
|
124
|
+
key,
|
|
137
125
|
name,
|
|
138
126
|
description,
|
|
139
127
|
inputSchema,
|
|
@@ -155,14 +143,20 @@ var FunctionDefinition = {
|
|
|
155
143
|
return deserializeFunction(functionObj);
|
|
156
144
|
}
|
|
157
145
|
};
|
|
158
|
-
var serializeFunction = (functionDef) =>
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
146
|
+
var serializeFunction = (functionDef) => {
|
|
147
|
+
const fn6 = Obj.make(FunctionType, {
|
|
148
|
+
key: functionDef.key,
|
|
149
|
+
name: functionDef.name,
|
|
150
|
+
version: "0.1.0",
|
|
151
|
+
description: functionDef.description,
|
|
152
|
+
inputSchema: Type2.toJsonSchema(functionDef.inputSchema),
|
|
153
|
+
outputSchema: !functionDef.outputSchema ? void 0 : Type2.toJsonSchema(functionDef.outputSchema)
|
|
154
|
+
});
|
|
155
|
+
if (functionDef.meta?.deployedFunctionId) {
|
|
156
|
+
setUserFunctionIdInMetadata(Obj.getMeta(fn6), functionDef.meta.deployedFunctionId);
|
|
157
|
+
}
|
|
158
|
+
return fn6;
|
|
159
|
+
};
|
|
166
160
|
var deserializeFunction = (functionObj) => {
|
|
167
161
|
return {
|
|
168
162
|
[typeId]: true,
|
|
@@ -172,22 +166,27 @@ var deserializeFunction = (functionObj) => {
|
|
|
172
166
|
description: functionObj.description,
|
|
173
167
|
inputSchema: !functionObj.inputSchema ? Schema2.Unknown : Type2.toEffectSchema(functionObj.inputSchema),
|
|
174
168
|
outputSchema: !functionObj.outputSchema ? void 0 : Type2.toEffectSchema(functionObj.outputSchema),
|
|
169
|
+
// TODO(dmaretskyi): This should throw error.
|
|
175
170
|
handler: () => {
|
|
171
|
+
},
|
|
172
|
+
meta: {
|
|
173
|
+
deployedFunctionId: getUserFunctionIdInMetadata(Obj.getMeta(functionObj))
|
|
176
174
|
}
|
|
177
175
|
};
|
|
178
176
|
};
|
|
179
177
|
|
|
180
178
|
// src/trace.ts
|
|
181
|
-
import
|
|
179
|
+
import * as Schema4 from "effect/Schema";
|
|
182
180
|
import { Type as Type4 } from "@dxos/echo";
|
|
181
|
+
import { ObjectId } from "@dxos/echo/internal";
|
|
183
182
|
import { Queue } from "@dxos/echo-db";
|
|
184
|
-
import { ObjectId } from "@dxos/echo-schema";
|
|
185
183
|
import { log } from "@dxos/log";
|
|
186
184
|
|
|
187
185
|
// src/types.ts
|
|
188
|
-
import
|
|
189
|
-
import
|
|
190
|
-
import {
|
|
186
|
+
import * as Schema3 from "effect/Schema";
|
|
187
|
+
import * as SchemaAST from "effect/SchemaAST";
|
|
188
|
+
import { Obj as Obj2, QueryAST, Type as Type3 } from "@dxos/echo";
|
|
189
|
+
import { Expando, OptionsAnnotationId, RawObject, Ref as Ref2 } from "@dxos/echo/internal";
|
|
191
190
|
import { DXN } from "@dxos/keys";
|
|
192
191
|
var TriggerKinds = [
|
|
193
192
|
"timer",
|
|
@@ -231,9 +230,12 @@ var WebhookTriggerSchema = Schema3.Struct({
|
|
|
231
230
|
}).pipe(Schema3.mutable);
|
|
232
231
|
var SubscriptionTriggerSchema = Schema3.Struct({
|
|
233
232
|
kind: Schema3.Literal("subscription").annotations(kindLiteralAnnotations),
|
|
234
|
-
query:
|
|
235
|
-
|
|
236
|
-
|
|
233
|
+
query: Schema3.Struct({
|
|
234
|
+
raw: Schema3.optional(Schema3.String.annotations({
|
|
235
|
+
title: "Query"
|
|
236
|
+
})),
|
|
237
|
+
ast: QueryAST.Query
|
|
238
|
+
}).pipe(Schema3.mutable),
|
|
237
239
|
options: Schema3.optional(Schema3.Struct({
|
|
238
240
|
// Watch changes to object (not just creation).
|
|
239
241
|
deep: Schema3.optional(Schema3.Boolean.annotations({
|
|
@@ -247,7 +249,7 @@ var SubscriptionTriggerSchema = Schema3.Struct({
|
|
|
247
249
|
title: "Options"
|
|
248
250
|
}))
|
|
249
251
|
}).pipe(Schema3.mutable);
|
|
250
|
-
var TriggerSchema = Schema3.Union(
|
|
252
|
+
var TriggerSchema = Schema3.Union(EmailTriggerSchema, QueueTriggerSchema, SubscriptionTriggerSchema, TimerTriggerSchema, WebhookTriggerSchema).annotations({
|
|
251
253
|
title: "Trigger"
|
|
252
254
|
});
|
|
253
255
|
var EmailTriggerOutput = Schema3.mutable(Schema3.Struct({
|
|
@@ -271,10 +273,21 @@ var QueueTriggerOutput = Schema3.mutable(Schema3.Struct({
|
|
|
271
273
|
item: Schema3.Any,
|
|
272
274
|
cursor: Schema3.String
|
|
273
275
|
}));
|
|
274
|
-
var SubscriptionTriggerOutput = Schema3.
|
|
276
|
+
var SubscriptionTriggerOutput = Schema3.Struct({
|
|
277
|
+
/**
|
|
278
|
+
* Type of the mutation.
|
|
279
|
+
*/
|
|
280
|
+
// TODO(dmaretskyi): Specify enum.
|
|
275
281
|
type: Schema3.String,
|
|
276
|
-
|
|
277
|
-
|
|
282
|
+
/**
|
|
283
|
+
* Reference to the object that was changed or created.
|
|
284
|
+
*/
|
|
285
|
+
subject: Type3.Ref(Obj2.Any),
|
|
286
|
+
/**
|
|
287
|
+
* @deprecated
|
|
288
|
+
*/
|
|
289
|
+
changedObjectId: Schema3.optional(Schema3.String)
|
|
290
|
+
}).pipe(Schema3.mutable);
|
|
278
291
|
var TimerTriggerOutput = Schema3.mutable(Schema3.Struct({
|
|
279
292
|
tick: Schema3.Number
|
|
280
293
|
}));
|
|
@@ -329,17 +342,17 @@ var FUNCTION_TYPES = [
|
|
|
329
342
|
|
|
330
343
|
// src/trace.ts
|
|
331
344
|
var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/trace.ts";
|
|
332
|
-
var InvocationOutcome = /* @__PURE__ */ function(InvocationOutcome2) {
|
|
345
|
+
var InvocationOutcome = /* @__PURE__ */ (function(InvocationOutcome2) {
|
|
333
346
|
InvocationOutcome2["SUCCESS"] = "success";
|
|
334
347
|
InvocationOutcome2["FAILURE"] = "failure";
|
|
335
348
|
InvocationOutcome2["PENDING"] = "pending";
|
|
336
349
|
return InvocationOutcome2;
|
|
337
|
-
}({});
|
|
338
|
-
var InvocationTraceEventType = /* @__PURE__ */ function(InvocationTraceEventType2) {
|
|
350
|
+
})({});
|
|
351
|
+
var InvocationTraceEventType = /* @__PURE__ */ (function(InvocationTraceEventType2) {
|
|
339
352
|
InvocationTraceEventType2["START"] = "start";
|
|
340
353
|
InvocationTraceEventType2["END"] = "end";
|
|
341
354
|
return InvocationTraceEventType2;
|
|
342
|
-
}({});
|
|
355
|
+
})({});
|
|
343
356
|
var TraceEventException = Schema4.Struct({
|
|
344
357
|
timestamp: Schema4.Number,
|
|
345
358
|
message: Schema4.String,
|
|
@@ -473,13 +486,26 @@ var createInvocationSpans = (items) => {
|
|
|
473
486
|
};
|
|
474
487
|
|
|
475
488
|
// src/triggers/invocation-tracer.ts
|
|
476
|
-
import
|
|
477
|
-
import
|
|
489
|
+
import * as Context3 from "effect/Context";
|
|
490
|
+
import * as Effect4 from "effect/Effect";
|
|
491
|
+
import * as Layer3 from "effect/Layer";
|
|
492
|
+
import { Obj as Obj3, Ref as Ref3 } from "@dxos/echo";
|
|
478
493
|
import { DXN as DXN2, ObjectId as ObjectId2 } from "@dxos/keys";
|
|
479
494
|
|
|
495
|
+
// src/services/function-invocation-service.ts
|
|
496
|
+
import * as Context2 from "effect/Context";
|
|
497
|
+
import * as Effect3 from "effect/Effect";
|
|
498
|
+
import * as Layer2 from "effect/Layer";
|
|
499
|
+
import { AiService as AiService2 } from "@dxos/ai";
|
|
500
|
+
|
|
480
501
|
// src/services/local-function-execution.ts
|
|
481
|
-
import
|
|
502
|
+
import * as Context from "effect/Context";
|
|
503
|
+
import * as Effect2 from "effect/Effect";
|
|
504
|
+
import * as Layer from "effect/Layer";
|
|
505
|
+
import * as Schema5 from "effect/Schema";
|
|
506
|
+
import { AiService } from "@dxos/ai";
|
|
482
507
|
import { todo } from "@dxos/debug";
|
|
508
|
+
import { log as log2 } from "@dxos/log";
|
|
483
509
|
function _define_property(obj, key, value) {
|
|
484
510
|
if (key in obj) {
|
|
485
511
|
Object.defineProperty(obj, key, {
|
|
@@ -493,25 +519,40 @@ function _define_property(obj, key, value) {
|
|
|
493
519
|
}
|
|
494
520
|
return obj;
|
|
495
521
|
}
|
|
522
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/services/local-function-execution.ts";
|
|
496
523
|
var _Context_Tag;
|
|
497
524
|
var LocalFunctionExecutionService = class extends (_Context_Tag = Context.Tag("@dxos/functions/LocalFunctionExecutionService")()) {
|
|
498
525
|
};
|
|
499
|
-
_define_property(LocalFunctionExecutionService, "layer", Layer.succeed(LocalFunctionExecutionService, {
|
|
500
|
-
invokeFunction: (functionDef, input) => invokeFunction(functionDef, input)
|
|
501
|
-
}));
|
|
502
526
|
_define_property(LocalFunctionExecutionService, "layerLive", Layer.effect(LocalFunctionExecutionService, Effect2.gen(function* () {
|
|
503
527
|
const resolver = yield* FunctionImplementationResolver;
|
|
528
|
+
const ai = yield* AiService.AiService;
|
|
529
|
+
const credentials = yield* CredentialsService;
|
|
530
|
+
const database = yield* DatabaseService;
|
|
531
|
+
const queues = yield* QueueService;
|
|
532
|
+
const functionCallService = yield* RemoteFunctionExecutionService;
|
|
504
533
|
return {
|
|
505
|
-
|
|
534
|
+
// TODO(dmaretskyi): Better error types.
|
|
535
|
+
invokeFunction: (functionDef, input) => Effect2.gen(function* () {
|
|
506
536
|
const resolved = yield* resolver.resolveFunctionImplementation(functionDef).pipe(Effect2.orDie);
|
|
507
|
-
|
|
508
|
-
|
|
537
|
+
const output = yield* invokeFunction(resolved, input);
|
|
538
|
+
return output;
|
|
539
|
+
}).pipe(Effect2.provideService(AiService.AiService, ai), Effect2.provideService(CredentialsService, credentials), Effect2.provideService(DatabaseService, database), Effect2.provideService(QueueService, queues), Effect2.provideService(RemoteFunctionExecutionService, functionCallService))
|
|
509
540
|
};
|
|
510
541
|
})));
|
|
511
542
|
_define_property(LocalFunctionExecutionService, "invokeFunction", Effect2.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction));
|
|
512
543
|
var invokeFunction = (functionDef, input) => Effect2.gen(function* () {
|
|
513
|
-
|
|
514
|
-
|
|
544
|
+
try {
|
|
545
|
+
const assertInput = functionDef.inputSchema.pipe(Schema5.asserts);
|
|
546
|
+
assertInput(input);
|
|
547
|
+
} catch (e) {
|
|
548
|
+
throw new FunctionError({
|
|
549
|
+
message: "Invalid function input",
|
|
550
|
+
context: {
|
|
551
|
+
name: functionDef.name
|
|
552
|
+
},
|
|
553
|
+
cause: e
|
|
554
|
+
});
|
|
555
|
+
}
|
|
515
556
|
const context = {
|
|
516
557
|
space: void 0,
|
|
517
558
|
getService: () => todo(),
|
|
@@ -519,6 +560,15 @@ var invokeFunction = (functionDef, input) => Effect2.gen(function* () {
|
|
|
519
560
|
throw new Error("Not available. Use the database service instead.");
|
|
520
561
|
}
|
|
521
562
|
};
|
|
563
|
+
log2.info("invoking function", {
|
|
564
|
+
name: functionDef.name,
|
|
565
|
+
input
|
|
566
|
+
}, {
|
|
567
|
+
F: __dxlog_file2,
|
|
568
|
+
L: 98,
|
|
569
|
+
S: this,
|
|
570
|
+
C: (f, a) => f(...a)
|
|
571
|
+
});
|
|
522
572
|
const data = yield* Effect2.gen(function* () {
|
|
523
573
|
const result = functionDef.handler({
|
|
524
574
|
context,
|
|
@@ -531,14 +581,34 @@ var invokeFunction = (functionDef, input) => Effect2.gen(function* () {
|
|
|
531
581
|
} else {
|
|
532
582
|
return result;
|
|
533
583
|
}
|
|
534
|
-
}).pipe(Effect2.orDie, Effect2.catchAllDefect((defect) => Effect2.die(new FunctionError(
|
|
584
|
+
}).pipe(Effect2.orDie, Effect2.catchAllDefect((defect) => Effect2.die(new FunctionError({
|
|
535
585
|
context: {
|
|
536
586
|
name: functionDef.name
|
|
537
587
|
},
|
|
538
588
|
cause: defect
|
|
539
589
|
}))));
|
|
540
|
-
|
|
541
|
-
|
|
590
|
+
log2.info("completed", {
|
|
591
|
+
function: functionDef.name,
|
|
592
|
+
input,
|
|
593
|
+
data
|
|
594
|
+
}, {
|
|
595
|
+
F: __dxlog_file2,
|
|
596
|
+
L: 122,
|
|
597
|
+
S: this,
|
|
598
|
+
C: (f, a) => f(...a)
|
|
599
|
+
});
|
|
600
|
+
try {
|
|
601
|
+
const assertOutput = functionDef.outputSchema?.pipe(Schema5.asserts);
|
|
602
|
+
assertOutput(data);
|
|
603
|
+
} catch (e) {
|
|
604
|
+
throw new FunctionError({
|
|
605
|
+
message: "Invalid function output",
|
|
606
|
+
context: {
|
|
607
|
+
name: functionDef.name
|
|
608
|
+
},
|
|
609
|
+
cause: e
|
|
610
|
+
});
|
|
611
|
+
}
|
|
542
612
|
return data;
|
|
543
613
|
}).pipe(Effect2.withSpan("invokeFunction", {
|
|
544
614
|
attributes: {
|
|
@@ -550,7 +620,7 @@ var FunctionImplementationResolver = class extends (_Context_Tag1 = Context.Tag(
|
|
|
550
620
|
};
|
|
551
621
|
_define_property(FunctionImplementationResolver, "layerTest", ({ functions }) => Layer.succeed(FunctionImplementationResolver, {
|
|
552
622
|
resolveFunctionImplementation: (functionDef) => {
|
|
553
|
-
const resolved = functions.find((f) => f.
|
|
623
|
+
const resolved = functions.find((f) => f.key === functionDef.key);
|
|
554
624
|
if (!resolved) {
|
|
555
625
|
return Effect2.fail(new FunctionNotFoundError(functionDef.name));
|
|
556
626
|
}
|
|
@@ -558,7 +628,7 @@ _define_property(FunctionImplementationResolver, "layerTest", ({ functions }) =>
|
|
|
558
628
|
}
|
|
559
629
|
}));
|
|
560
630
|
|
|
561
|
-
// src/
|
|
631
|
+
// src/services/function-invocation-service.ts
|
|
562
632
|
function _define_property2(obj, key, value) {
|
|
563
633
|
if (key in obj) {
|
|
564
634
|
Object.defineProperty(obj, key, {
|
|
@@ -573,17 +643,54 @@ function _define_property2(obj, key, value) {
|
|
|
573
643
|
return obj;
|
|
574
644
|
}
|
|
575
645
|
var _Context_Tag2;
|
|
576
|
-
var
|
|
646
|
+
var FunctionInvocationService = class extends (_Context_Tag2 = Context2.Tag("@dxos/functions/FunctionInvocationService")()) {
|
|
577
647
|
};
|
|
578
|
-
_define_property2(
|
|
648
|
+
_define_property2(FunctionInvocationService, "invokeFunction", Effect3.serviceFunctionEffect(FunctionInvocationService, (_) => _.invokeFunction));
|
|
649
|
+
_define_property2(FunctionInvocationService, "layer", Layer2.effect(FunctionInvocationService, Effect3.gen(function* () {
|
|
650
|
+
const localExecutioner = yield* LocalFunctionExecutionService;
|
|
651
|
+
const remoteExecutioner = yield* RemoteFunctionExecutionService;
|
|
579
652
|
return {
|
|
580
|
-
|
|
653
|
+
invokeFunction: (functionDef, input) => Effect3.gen(function* () {
|
|
654
|
+
if (functionDef.meta?.deployedFunctionId) {
|
|
655
|
+
return yield* remoteExecutioner.callFunction(functionDef.meta.deployedFunctionId, input);
|
|
656
|
+
}
|
|
657
|
+
return yield* localExecutioner.invokeFunction(functionDef, input);
|
|
658
|
+
})
|
|
659
|
+
};
|
|
660
|
+
})));
|
|
661
|
+
_define_property2(FunctionInvocationService, "layerTest", ({ functions = [] } = {}) => FunctionInvocationService.layer.pipe(Layer2.provide(LocalFunctionExecutionService.layerLive), Layer2.provide(FunctionImplementationResolver.layerTest({
|
|
662
|
+
functions
|
|
663
|
+
})), Layer2.provide(RemoteFunctionExecutionService.layerMock)));
|
|
664
|
+
_define_property2(FunctionInvocationService, "layerTestMocked", ({ functions }) => FunctionInvocationService.layerTest({
|
|
665
|
+
functions
|
|
666
|
+
}).pipe(Layer2.provide(AiService2.notAvailable), Layer2.provide(CredentialsService.configuredLayer([])), Layer2.provide(DatabaseService.notAvailable), Layer2.provide(QueueService.notAvailable)));
|
|
667
|
+
|
|
668
|
+
// src/triggers/invocation-tracer.ts
|
|
669
|
+
function _define_property3(obj, key, value) {
|
|
670
|
+
if (key in obj) {
|
|
671
|
+
Object.defineProperty(obj, key, {
|
|
672
|
+
value,
|
|
673
|
+
enumerable: true,
|
|
674
|
+
configurable: true,
|
|
675
|
+
writable: true
|
|
676
|
+
});
|
|
677
|
+
} else {
|
|
678
|
+
obj[key] = value;
|
|
679
|
+
}
|
|
680
|
+
return obj;
|
|
681
|
+
}
|
|
682
|
+
var _Context_Tag3;
|
|
683
|
+
var InvocationTracer = class extends (_Context_Tag3 = Context3.Tag("@dxos/functions/InvocationTracer")()) {
|
|
684
|
+
};
|
|
685
|
+
_define_property3(InvocationTracer, "layerLive", (opts) => Layer3.effect(InvocationTracer, Effect4.gen(function* () {
|
|
686
|
+
return {
|
|
687
|
+
traceInvocationStart: Effect4.fn("traceInvocationStart")(function* ({ payload, target }) {
|
|
581
688
|
const invocationId = ObjectId2.random();
|
|
582
689
|
const invocationTraceQueue = yield* QueueService.createQueue({
|
|
583
690
|
subspaceTag: "trace"
|
|
584
691
|
});
|
|
585
692
|
const now = Date.now();
|
|
586
|
-
const traceEvent =
|
|
693
|
+
const traceEvent = Obj3.make(InvocationTraceStartEvent, {
|
|
587
694
|
type: InvocationTraceEventType.START,
|
|
588
695
|
invocationId,
|
|
589
696
|
timestamp: now,
|
|
@@ -601,9 +708,9 @@ _define_property2(InvocationTracer, "layerLive", (opts) => Layer2.effect(Invocat
|
|
|
601
708
|
invocationTraceQueue
|
|
602
709
|
};
|
|
603
710
|
}),
|
|
604
|
-
traceInvocationEnd:
|
|
711
|
+
traceInvocationEnd: Effect4.fn("traceInvocationEnd")(function* ({ trace, exception }) {
|
|
605
712
|
const now = Date.now();
|
|
606
|
-
const traceEvent =
|
|
713
|
+
const traceEvent = Obj3.make(InvocationTraceEndEvent, {
|
|
607
714
|
type: InvocationTraceEventType.END,
|
|
608
715
|
invocationId: trace.invocationId,
|
|
609
716
|
timestamp: now,
|
|
@@ -621,7 +728,7 @@ _define_property2(InvocationTracer, "layerLive", (opts) => Layer2.effect(Invocat
|
|
|
621
728
|
})
|
|
622
729
|
};
|
|
623
730
|
})));
|
|
624
|
-
|
|
731
|
+
_define_property3(InvocationTracer, "layerTest", Layer3.unwrapEffect(Effect4.gen(function* () {
|
|
625
732
|
const queue = yield* QueueService.createQueue({
|
|
626
733
|
subspaceTag: "trace"
|
|
627
734
|
});
|
|
@@ -631,11 +738,22 @@ _define_property2(InvocationTracer, "layerTest", Layer2.unwrapEffect(Effect3.gen
|
|
|
631
738
|
})));
|
|
632
739
|
|
|
633
740
|
// src/triggers/trigger-dispatcher.ts
|
|
634
|
-
import
|
|
635
|
-
import
|
|
741
|
+
import * as Cause from "effect/Cause";
|
|
742
|
+
import * as Context5 from "effect/Context";
|
|
743
|
+
import * as Cron from "effect/Cron";
|
|
744
|
+
import * as Duration from "effect/Duration";
|
|
745
|
+
import * as Effect6 from "effect/Effect";
|
|
746
|
+
import * as Either from "effect/Either";
|
|
747
|
+
import * as Exit from "effect/Exit";
|
|
748
|
+
import * as Fiber from "effect/Fiber";
|
|
749
|
+
import * as Layer5 from "effect/Layer";
|
|
750
|
+
import * as Option2 from "effect/Option";
|
|
751
|
+
import * as Record3 from "effect/Record";
|
|
752
|
+
import * as Schedule from "effect/Schedule";
|
|
753
|
+
import { DXN as DXN3, Filter, Obj as Obj4, Query } from "@dxos/echo";
|
|
636
754
|
import { causeToError } from "@dxos/effect";
|
|
637
755
|
import { invariant } from "@dxos/invariant";
|
|
638
|
-
import { log as
|
|
756
|
+
import { log as log3 } from "@dxos/log";
|
|
639
757
|
import { KEY_QUEUE_POSITION } from "@dxos/protocols";
|
|
640
758
|
|
|
641
759
|
// src/triggers/input-builder.ts
|
|
@@ -662,13 +780,14 @@ var createInvocationPayload = (trigger, event) => {
|
|
|
662
780
|
};
|
|
663
781
|
|
|
664
782
|
// src/triggers/trigger-state-store.ts
|
|
665
|
-
import
|
|
666
|
-
import
|
|
667
|
-
import
|
|
668
|
-
import
|
|
669
|
-
import
|
|
783
|
+
import * as KeyValueStore from "@effect/platform/KeyValueStore";
|
|
784
|
+
import * as Context4 from "effect/Context";
|
|
785
|
+
import * as Effect5 from "effect/Effect";
|
|
786
|
+
import * as Layer4 from "effect/Layer";
|
|
787
|
+
import * as Option from "effect/Option";
|
|
788
|
+
import * as Schema6 from "effect/Schema";
|
|
670
789
|
import { ObjectId as ObjectId3 } from "@dxos/keys";
|
|
671
|
-
function
|
|
790
|
+
function _define_property4(obj, key, value) {
|
|
672
791
|
if (key in obj) {
|
|
673
792
|
Object.defineProperty(obj, key, {
|
|
674
793
|
value,
|
|
@@ -691,32 +810,32 @@ var TriggerState = Schema6.Struct({
|
|
|
691
810
|
})
|
|
692
811
|
})))
|
|
693
812
|
});
|
|
694
|
-
var
|
|
695
|
-
var TriggerStateStore = class extends (
|
|
813
|
+
var _Context_Tag4;
|
|
814
|
+
var TriggerStateStore = class extends (_Context_Tag4 = Context4.Tag("@dxos/functions/TriggerStateStore")()) {
|
|
696
815
|
};
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
816
|
+
_define_property4(TriggerStateStore, "getState", Effect5.serviceFunctionEffect(TriggerStateStore, (_) => _.getState));
|
|
817
|
+
_define_property4(TriggerStateStore, "saveState", Effect5.serviceFunctionEffect(TriggerStateStore, (_) => _.saveState));
|
|
818
|
+
_define_property4(TriggerStateStore, "layerKv", Layer4.effect(TriggerStateStore, Effect5.gen(function* () {
|
|
700
819
|
const kv = yield* KeyValueStore.KeyValueStore;
|
|
701
820
|
const schemaStore = kv.forSchema(Schema6.parseJson(TriggerState));
|
|
702
821
|
const store = {
|
|
703
|
-
getState:
|
|
704
|
-
const valueOption = yield* schemaStore.get(triggerId).pipe(
|
|
822
|
+
getState: Effect5.fn("TriggerStateStore.getState")(function* (triggerId) {
|
|
823
|
+
const valueOption = yield* schemaStore.get(triggerId).pipe(Effect5.orDie);
|
|
705
824
|
if (Option.isNone(valueOption)) {
|
|
706
|
-
return yield*
|
|
825
|
+
return yield* Effect5.fail(new TriggerStateNotFoundError());
|
|
707
826
|
}
|
|
708
827
|
return valueOption.value;
|
|
709
828
|
}),
|
|
710
|
-
saveState:
|
|
711
|
-
yield* schemaStore.set(state.triggerId, state).pipe(
|
|
829
|
+
saveState: Effect5.fn("TriggerStateStore.saveState")(function* (state) {
|
|
830
|
+
yield* schemaStore.set(state.triggerId, state).pipe(Effect5.orDie);
|
|
712
831
|
})
|
|
713
832
|
};
|
|
714
833
|
return store;
|
|
715
834
|
})));
|
|
716
|
-
|
|
835
|
+
_define_property4(TriggerStateStore, "layerMemory", TriggerStateStore.layerKv.pipe(Layer4.provide(KeyValueStore.layerMemory)));
|
|
717
836
|
|
|
718
837
|
// src/triggers/trigger-dispatcher.ts
|
|
719
|
-
function
|
|
838
|
+
function _define_property5(obj, key, value) {
|
|
720
839
|
if (key in obj) {
|
|
721
840
|
Object.defineProperty(obj, key, {
|
|
722
841
|
value,
|
|
@@ -729,11 +848,11 @@ function _define_property4(obj, key, value) {
|
|
|
729
848
|
}
|
|
730
849
|
return obj;
|
|
731
850
|
}
|
|
732
|
-
var
|
|
733
|
-
var
|
|
734
|
-
var TriggerDispatcher = class extends (
|
|
851
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions/src/triggers/trigger-dispatcher.ts";
|
|
852
|
+
var _Context_Tag5;
|
|
853
|
+
var TriggerDispatcher = class extends (_Context_Tag5 = Context5.Tag("@dxos/functions/TriggerDispatcher")()) {
|
|
735
854
|
};
|
|
736
|
-
|
|
855
|
+
_define_property5(TriggerDispatcher, "layer", (options) => Layer5.effect(TriggerDispatcher, Effect6.gen(function* () {
|
|
737
856
|
return new TriggerDispatcherImpl(options);
|
|
738
857
|
})));
|
|
739
858
|
var TriggerDispatcherImpl = class {
|
|
@@ -741,44 +860,44 @@ var TriggerDispatcherImpl = class {
|
|
|
741
860
|
return this._running;
|
|
742
861
|
}
|
|
743
862
|
constructor(options) {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
863
|
+
_define_property5(this, "livePollInterval", void 0);
|
|
864
|
+
_define_property5(this, "timeControl", void 0);
|
|
865
|
+
_define_property5(this, "_running", false);
|
|
866
|
+
_define_property5(this, "_internalTime", void 0);
|
|
867
|
+
_define_property5(this, "_timerFiber", void 0);
|
|
868
|
+
_define_property5(this, "_scheduledTriggers", /* @__PURE__ */ new Map());
|
|
869
|
+
_define_property5(this, "start", () => Effect6.gen(this, function* () {
|
|
751
870
|
if (this._running) {
|
|
752
871
|
return;
|
|
753
872
|
}
|
|
754
873
|
this._running = true;
|
|
755
874
|
if (this.timeControl === "natural") {
|
|
756
|
-
this._timerFiber = yield* this._startNaturalTimeProcessing().pipe(
|
|
875
|
+
this._timerFiber = yield* this._startNaturalTimeProcessing().pipe(Effect6.tapErrorCause((cause) => {
|
|
757
876
|
const error = causeToError(cause);
|
|
758
|
-
|
|
877
|
+
log3.error("trigger dispatcher error", {
|
|
759
878
|
error
|
|
760
879
|
}, {
|
|
761
|
-
F:
|
|
762
|
-
L:
|
|
880
|
+
F: __dxlog_file3,
|
|
881
|
+
L: 187,
|
|
763
882
|
S: this,
|
|
764
883
|
C: (f, a) => f(...a)
|
|
765
884
|
});
|
|
766
885
|
this._running = false;
|
|
767
|
-
return
|
|
768
|
-
}),
|
|
886
|
+
return Effect6.void;
|
|
887
|
+
}), Effect6.forkDaemon);
|
|
769
888
|
} else {
|
|
770
|
-
return yield*
|
|
889
|
+
return yield* Effect6.dieMessage("TriggerDispatcher started in manual time control mode");
|
|
771
890
|
}
|
|
772
|
-
|
|
891
|
+
log3.info("TriggerDispatcher started", {
|
|
773
892
|
timeControl: this.timeControl
|
|
774
893
|
}, {
|
|
775
|
-
F:
|
|
776
|
-
L:
|
|
894
|
+
F: __dxlog_file3,
|
|
895
|
+
L: 197,
|
|
777
896
|
S: this,
|
|
778
897
|
C: (f, a) => f(...a)
|
|
779
898
|
});
|
|
780
899
|
}));
|
|
781
|
-
|
|
900
|
+
_define_property5(this, "stop", () => Effect6.gen(this, function* () {
|
|
782
901
|
if (!this._running) {
|
|
783
902
|
return;
|
|
784
903
|
}
|
|
@@ -788,22 +907,22 @@ var TriggerDispatcherImpl = class {
|
|
|
788
907
|
this._timerFiber = void 0;
|
|
789
908
|
}
|
|
790
909
|
this._scheduledTriggers.clear();
|
|
791
|
-
|
|
792
|
-
F:
|
|
793
|
-
L:
|
|
910
|
+
log3.info("TriggerDispatcher stopped", void 0, {
|
|
911
|
+
F: __dxlog_file3,
|
|
912
|
+
L: 217,
|
|
794
913
|
S: this,
|
|
795
914
|
C: (f, a) => f(...a)
|
|
796
915
|
});
|
|
797
916
|
}));
|
|
798
|
-
|
|
917
|
+
_define_property5(this, "invokeTrigger", (options2) => Effect6.gen(this, function* () {
|
|
799
918
|
const { trigger, event } = options2;
|
|
800
|
-
|
|
919
|
+
log3.info("running trigger", {
|
|
801
920
|
triggerId: trigger.id,
|
|
802
921
|
spec: trigger.spec,
|
|
803
922
|
event
|
|
804
923
|
}, {
|
|
805
|
-
F:
|
|
806
|
-
L:
|
|
924
|
+
F: __dxlog_file3,
|
|
925
|
+
L: 225,
|
|
807
926
|
S: this,
|
|
808
927
|
C: (f, a) => f(...a)
|
|
809
928
|
});
|
|
@@ -819,17 +938,17 @@ var TriggerDispatcherImpl = class {
|
|
|
819
938
|
data: event
|
|
820
939
|
}
|
|
821
940
|
});
|
|
822
|
-
const result = yield*
|
|
941
|
+
const result = yield* Effect6.gen(this, function* () {
|
|
823
942
|
if (!trigger.enabled) {
|
|
824
|
-
return yield*
|
|
943
|
+
return yield* Effect6.dieMessage("Attempting to invoke disabled trigger");
|
|
825
944
|
}
|
|
826
945
|
if (!trigger.function) {
|
|
827
|
-
return yield*
|
|
946
|
+
return yield* Effect6.dieMessage("Trigger has no function reference");
|
|
828
947
|
}
|
|
829
|
-
const serialiedFunction = yield* DatabaseService.load(trigger.function).pipe(
|
|
830
|
-
invariant(
|
|
831
|
-
F:
|
|
832
|
-
L:
|
|
948
|
+
const serialiedFunction = yield* DatabaseService.load(trigger.function).pipe(Effect6.orDie);
|
|
949
|
+
invariant(Obj4.instanceOf(FunctionType, serialiedFunction), void 0, {
|
|
950
|
+
F: __dxlog_file3,
|
|
951
|
+
L: 252,
|
|
833
952
|
S: this,
|
|
834
953
|
A: [
|
|
835
954
|
"Obj.instanceOf(FunctionType, serialiedFunction)",
|
|
@@ -838,27 +957,27 @@ var TriggerDispatcherImpl = class {
|
|
|
838
957
|
});
|
|
839
958
|
const functionDef = deserializeFunction(serialiedFunction);
|
|
840
959
|
const inputData = this._prepareInputData(trigger, event);
|
|
841
|
-
return yield*
|
|
842
|
-
}).pipe(
|
|
960
|
+
return yield* FunctionInvocationService.invokeFunction(functionDef, inputData).pipe(Effect6.provide(ComputeEventLogger.layerFromTracing.pipe(Layer5.provideMerge(TracingService.layerQueue(trace.invocationTraceQueue)))));
|
|
961
|
+
}).pipe(Effect6.exit);
|
|
843
962
|
const triggerExecutionResult = {
|
|
844
963
|
triggerId: trigger.id,
|
|
845
964
|
result
|
|
846
965
|
};
|
|
847
966
|
if (Exit.isSuccess(result)) {
|
|
848
|
-
|
|
967
|
+
log3.info("trigger execution success", {
|
|
849
968
|
triggerId: trigger.id
|
|
850
969
|
}, {
|
|
851
|
-
F:
|
|
852
|
-
L:
|
|
970
|
+
F: __dxlog_file3,
|
|
971
|
+
L: 273,
|
|
853
972
|
S: this,
|
|
854
973
|
C: (f, a) => f(...a)
|
|
855
974
|
});
|
|
856
975
|
} else {
|
|
857
|
-
|
|
976
|
+
log3.error("trigger execution failure", {
|
|
858
977
|
error: causeToError(result.cause)
|
|
859
978
|
}, {
|
|
860
|
-
F:
|
|
861
|
-
L:
|
|
979
|
+
F: __dxlog_file3,
|
|
980
|
+
L: 277,
|
|
862
981
|
S: this,
|
|
863
982
|
C: (f, a) => f(...a)
|
|
864
983
|
});
|
|
@@ -870,11 +989,11 @@ var TriggerDispatcherImpl = class {
|
|
|
870
989
|
});
|
|
871
990
|
return triggerExecutionResult;
|
|
872
991
|
}));
|
|
873
|
-
|
|
992
|
+
_define_property5(this, "invokeScheduledTriggers", ({ kinds = [
|
|
874
993
|
"timer",
|
|
875
994
|
"queue",
|
|
876
995
|
"subscription"
|
|
877
|
-
] } = {}) =>
|
|
996
|
+
] } = {}) => Effect6.gen(this, function* () {
|
|
878
997
|
const invocations = [];
|
|
879
998
|
for (const kind of kinds) {
|
|
880
999
|
switch (kind) {
|
|
@@ -889,7 +1008,7 @@ var TriggerDispatcherImpl = class {
|
|
|
889
1008
|
scheduledTrigger.nextExecution = Cron.next(scheduledTrigger.cron, now);
|
|
890
1009
|
}
|
|
891
1010
|
}
|
|
892
|
-
invocations.push(...yield*
|
|
1011
|
+
invocations.push(...yield* Effect6.forEach(triggersToInvoke, (trigger) => this.invokeTrigger({
|
|
893
1012
|
trigger,
|
|
894
1013
|
event: {
|
|
895
1014
|
tick: now.getTime()
|
|
@@ -906,11 +1025,11 @@ var TriggerDispatcherImpl = class {
|
|
|
906
1025
|
if (spec?.kind !== "queue") {
|
|
907
1026
|
continue;
|
|
908
1027
|
}
|
|
909
|
-
const cursor =
|
|
1028
|
+
const cursor = Obj4.getKeys(trigger, KEY_QUEUE_CURSOR).at(0)?.id;
|
|
910
1029
|
const queue = yield* QueueService.getQueue(DXN3.parse(spec.queue));
|
|
911
|
-
const objects = yield*
|
|
1030
|
+
const objects = yield* Effect6.promise(() => queue.queryObjects());
|
|
912
1031
|
for (const object of objects) {
|
|
913
|
-
const objectPos =
|
|
1032
|
+
const objectPos = Obj4.getKeys(object, KEY_QUEUE_POSITION).at(0)?.id;
|
|
914
1033
|
if (!objectPos || cursor && parseInt(cursor) >= parseInt(objectPos)) {
|
|
915
1034
|
continue;
|
|
916
1035
|
}
|
|
@@ -922,8 +1041,8 @@ var TriggerDispatcherImpl = class {
|
|
|
922
1041
|
cursor: objectPos
|
|
923
1042
|
}
|
|
924
1043
|
}));
|
|
925
|
-
|
|
926
|
-
|
|
1044
|
+
Obj4.deleteKeys(trigger, KEY_QUEUE_CURSOR);
|
|
1045
|
+
Obj4.getMeta(trigger).keys.push({
|
|
927
1046
|
source: KEY_QUEUE_CURSOR,
|
|
928
1047
|
id: objectPos
|
|
929
1048
|
});
|
|
@@ -936,12 +1055,12 @@ var TriggerDispatcherImpl = class {
|
|
|
936
1055
|
case "subscription": {
|
|
937
1056
|
const triggers = yield* this._fetchTriggers();
|
|
938
1057
|
for (const trigger of triggers) {
|
|
939
|
-
const spec =
|
|
1058
|
+
const spec = Obj4.getSnapshot(trigger).spec;
|
|
940
1059
|
if (spec?.kind !== "subscription") {
|
|
941
1060
|
continue;
|
|
942
1061
|
}
|
|
943
|
-
const { objects } = yield* DatabaseService.runQuery(Query.fromAst(spec.query));
|
|
944
|
-
const state = yield* TriggerStateStore.getState(trigger.id).pipe(
|
|
1062
|
+
const { objects } = yield* DatabaseService.runQuery(Query.fromAst(spec.query.ast));
|
|
1063
|
+
const state = yield* TriggerStateStore.getState(trigger.id).pipe(Effect6.catchTag("TRIGGER_STATE_NOT_FOUND", () => Effect6.succeed({
|
|
945
1064
|
version: "1",
|
|
946
1065
|
triggerId: trigger.id,
|
|
947
1066
|
state: {
|
|
@@ -950,8 +1069,8 @@ var TriggerDispatcherImpl = class {
|
|
|
950
1069
|
}
|
|
951
1070
|
})));
|
|
952
1071
|
invariant(state.state?._tag === "subscription", void 0, {
|
|
953
|
-
F:
|
|
954
|
-
L:
|
|
1072
|
+
F: __dxlog_file3,
|
|
1073
|
+
L: 390,
|
|
955
1074
|
S: this,
|
|
956
1075
|
A: [
|
|
957
1076
|
"state.state?._tag === 'subscription'",
|
|
@@ -960,21 +1079,23 @@ var TriggerDispatcherImpl = class {
|
|
|
960
1079
|
});
|
|
961
1080
|
let updated = false;
|
|
962
1081
|
for (const object of objects) {
|
|
963
|
-
const existingVersion =
|
|
964
|
-
const currentVersion =
|
|
965
|
-
const run = Option2.isNone(existingVersion) ||
|
|
1082
|
+
const existingVersion = Record3.get(state.state.processedVersions, object.id).pipe(Option2.map(Obj4.decodeVersion));
|
|
1083
|
+
const currentVersion = Obj4.version(object);
|
|
1084
|
+
const run = Option2.isNone(existingVersion) || Obj4.compareVersions(currentVersion, existingVersion.value) === "different";
|
|
966
1085
|
if (!run) {
|
|
967
1086
|
continue;
|
|
968
1087
|
}
|
|
1088
|
+
const { db } = yield* DatabaseService;
|
|
969
1089
|
invocations.push(yield* this.invokeTrigger({
|
|
970
1090
|
trigger,
|
|
971
1091
|
event: {
|
|
972
1092
|
// TODO(dmaretskyi): Change type not supported.
|
|
973
1093
|
type: "unknown",
|
|
1094
|
+
subject: db.ref(Obj4.getDXN(object)),
|
|
974
1095
|
changedObjectId: object.id
|
|
975
1096
|
}
|
|
976
1097
|
}));
|
|
977
|
-
state.state.processedVersions[object.id] =
|
|
1098
|
+
state.state.processedVersions[object.id] = Obj4.encodeVersion(currentVersion);
|
|
978
1099
|
updated = true;
|
|
979
1100
|
}
|
|
980
1101
|
if (updated) {
|
|
@@ -984,36 +1105,36 @@ var TriggerDispatcherImpl = class {
|
|
|
984
1105
|
break;
|
|
985
1106
|
}
|
|
986
1107
|
default: {
|
|
987
|
-
return yield*
|
|
1108
|
+
return yield* Effect6.dieMessage(`Unknown trigger kind: ${kind}`);
|
|
988
1109
|
}
|
|
989
1110
|
}
|
|
990
1111
|
}
|
|
991
1112
|
return invocations;
|
|
992
1113
|
}));
|
|
993
|
-
|
|
1114
|
+
_define_property5(this, "advanceTime", (duration) => Effect6.gen(this, function* () {
|
|
994
1115
|
if (this.timeControl !== "manual") {
|
|
995
|
-
return yield*
|
|
1116
|
+
return yield* Effect6.dieMessage("advanceTime can only be used in manual time control mode");
|
|
996
1117
|
}
|
|
997
1118
|
const millis = Duration.toMillis(duration);
|
|
998
1119
|
this._internalTime = new Date(this._internalTime.getTime() + millis);
|
|
999
|
-
|
|
1120
|
+
log3("Advanced internal time", {
|
|
1000
1121
|
newTime: this._internalTime,
|
|
1001
1122
|
advancedBy: Duration.format(duration)
|
|
1002
1123
|
}, {
|
|
1003
|
-
F:
|
|
1004
|
-
L:
|
|
1124
|
+
F: __dxlog_file3,
|
|
1125
|
+
L: 447,
|
|
1005
1126
|
S: this,
|
|
1006
1127
|
C: (f, a) => f(...a)
|
|
1007
1128
|
});
|
|
1008
|
-
}).pipe(
|
|
1009
|
-
|
|
1129
|
+
}).pipe(Effect6.orDie));
|
|
1130
|
+
_define_property5(this, "getCurrentTime", () => {
|
|
1010
1131
|
if (this.timeControl === "natural") {
|
|
1011
1132
|
return /* @__PURE__ */ new Date();
|
|
1012
1133
|
} else {
|
|
1013
1134
|
return new Date(this._internalTime);
|
|
1014
1135
|
}
|
|
1015
1136
|
});
|
|
1016
|
-
|
|
1137
|
+
_define_property5(this, "refreshTriggers", () => Effect6.gen(this, function* () {
|
|
1017
1138
|
const triggers = yield* this._fetchTriggers();
|
|
1018
1139
|
const currentTriggerIds = new Set(triggers.map((t) => t.id));
|
|
1019
1140
|
for (const triggerId of this._scheduledTriggers.keys()) {
|
|
@@ -1030,14 +1151,14 @@ var TriggerDispatcherImpl = class {
|
|
|
1030
1151
|
const existing = this._scheduledTriggers.get(trigger.id);
|
|
1031
1152
|
const now = this.getCurrentTime();
|
|
1032
1153
|
const nextExecution = existing?.nextExecution ?? Cron.next(cron, now);
|
|
1033
|
-
|
|
1154
|
+
log3("Updated scheduled trigger", {
|
|
1034
1155
|
triggerId: trigger.id,
|
|
1035
1156
|
cron: timerSpec.cron,
|
|
1036
1157
|
nextExecution,
|
|
1037
1158
|
now
|
|
1038
1159
|
}, {
|
|
1039
|
-
F:
|
|
1040
|
-
L:
|
|
1160
|
+
F: __dxlog_file3,
|
|
1161
|
+
L: 487,
|
|
1041
1162
|
S: this,
|
|
1042
1163
|
C: (f, a) => f(...a)
|
|
1043
1164
|
});
|
|
@@ -1047,36 +1168,36 @@ var TriggerDispatcherImpl = class {
|
|
|
1047
1168
|
nextExecution
|
|
1048
1169
|
});
|
|
1049
1170
|
} else {
|
|
1050
|
-
|
|
1171
|
+
log3.error("Invalid cron expression", {
|
|
1051
1172
|
triggerId: trigger.id,
|
|
1052
1173
|
cron: timerSpec.cron,
|
|
1053
1174
|
error: cronEither.left.message
|
|
1054
1175
|
}, {
|
|
1055
|
-
F:
|
|
1056
|
-
L:
|
|
1176
|
+
F: __dxlog_file3,
|
|
1177
|
+
L: 499,
|
|
1057
1178
|
S: this,
|
|
1058
1179
|
C: (f, a) => f(...a)
|
|
1059
1180
|
});
|
|
1060
1181
|
}
|
|
1061
1182
|
}
|
|
1062
1183
|
}
|
|
1063
|
-
|
|
1184
|
+
log3("Updated scheduled triggers", {
|
|
1064
1185
|
count: this._scheduledTriggers.size
|
|
1065
1186
|
}, {
|
|
1066
|
-
F:
|
|
1067
|
-
L:
|
|
1187
|
+
F: __dxlog_file3,
|
|
1188
|
+
L: 508,
|
|
1068
1189
|
S: this,
|
|
1069
1190
|
C: (f, a) => f(...a)
|
|
1070
1191
|
});
|
|
1071
|
-
}).pipe(
|
|
1072
|
-
|
|
1192
|
+
}).pipe(Effect6.withSpan("TriggerDispatcher.refreshTriggers")));
|
|
1193
|
+
_define_property5(this, "_fetchTriggers", () => Effect6.gen(this, function* () {
|
|
1073
1194
|
const { objects } = yield* DatabaseService.runQuery(Filter.type(FunctionTrigger));
|
|
1074
1195
|
return objects;
|
|
1075
|
-
}).pipe(
|
|
1076
|
-
|
|
1196
|
+
}).pipe(Effect6.withSpan("TriggerDispatcher.fetchTriggers")));
|
|
1197
|
+
_define_property5(this, "_startNaturalTimeProcessing", () => Effect6.gen(this, function* () {
|
|
1077
1198
|
yield* this.invokeScheduledTriggers();
|
|
1078
|
-
}).pipe(
|
|
1079
|
-
|
|
1199
|
+
}).pipe(Effect6.repeat(Schedule.fixed(this.livePollInterval)), Effect6.asVoid));
|
|
1200
|
+
_define_property5(this, "_prepareInputData", (trigger, event) => {
|
|
1080
1201
|
return createInvocationPayload(trigger, event);
|
|
1081
1202
|
});
|
|
1082
1203
|
this.timeControl = options.timeControl;
|
|
@@ -1087,9 +1208,10 @@ var TriggerDispatcherImpl = class {
|
|
|
1087
1208
|
var KEY_QUEUE_CURSOR = "dxos.org/key/local-trigger-dispatcher/queue-cursor";
|
|
1088
1209
|
|
|
1089
1210
|
// src/executor/executor.ts
|
|
1090
|
-
import
|
|
1211
|
+
import * as Effect7 from "effect/Effect";
|
|
1212
|
+
import * as Schema7 from "effect/Schema";
|
|
1091
1213
|
import { runAndForwardErrors } from "@dxos/effect";
|
|
1092
|
-
function
|
|
1214
|
+
function _define_property6(obj, key, value) {
|
|
1093
1215
|
if (key in obj) {
|
|
1094
1216
|
Object.defineProperty(obj, key, {
|
|
1095
1217
|
value,
|
|
@@ -1122,8 +1244,8 @@ var FunctionExecutor = class {
|
|
|
1122
1244
|
data: input
|
|
1123
1245
|
});
|
|
1124
1246
|
let data;
|
|
1125
|
-
if (
|
|
1126
|
-
data = await result.pipe(
|
|
1247
|
+
if (Effect7.isEffect(result)) {
|
|
1248
|
+
data = await result.pipe(Effect7.provide(this._services.createLayer()), runAndForwardErrors);
|
|
1127
1249
|
} else {
|
|
1128
1250
|
data = await result;
|
|
1129
1251
|
}
|
|
@@ -1132,7 +1254,7 @@ var FunctionExecutor = class {
|
|
|
1132
1254
|
return data;
|
|
1133
1255
|
}
|
|
1134
1256
|
constructor(_services) {
|
|
1135
|
-
|
|
1257
|
+
_define_property6(this, "_services", void 0);
|
|
1136
1258
|
this._services = _services;
|
|
1137
1259
|
}
|
|
1138
1260
|
};
|
|
@@ -1146,9 +1268,11 @@ __export(examples_exports, {
|
|
|
1146
1268
|
});
|
|
1147
1269
|
|
|
1148
1270
|
// src/examples/fib.ts
|
|
1149
|
-
import
|
|
1271
|
+
import * as Effect8 from "effect/Effect";
|
|
1272
|
+
import * as Schema8 from "effect/Schema";
|
|
1150
1273
|
var fib_default = defineFunction({
|
|
1151
|
-
|
|
1274
|
+
key: "example.org/function/fib",
|
|
1275
|
+
name: "Fibonacci",
|
|
1152
1276
|
description: "Function that calculates a Fibonacci number",
|
|
1153
1277
|
inputSchema: Schema8.Struct({
|
|
1154
1278
|
iterations: Schema8.optional(Schema8.Number).annotations({
|
|
@@ -1159,7 +1283,7 @@ var fib_default = defineFunction({
|
|
|
1159
1283
|
outputSchema: Schema8.Struct({
|
|
1160
1284
|
result: Schema8.String
|
|
1161
1285
|
}),
|
|
1162
|
-
handler:
|
|
1286
|
+
handler: Effect8.fn(function* ({ data: { iterations = 1e5 } }) {
|
|
1163
1287
|
let a = 0n;
|
|
1164
1288
|
let b = 1n;
|
|
1165
1289
|
for (let i = 0; i < iterations; i++) {
|
|
@@ -1173,13 +1297,16 @@ var fib_default = defineFunction({
|
|
|
1173
1297
|
});
|
|
1174
1298
|
|
|
1175
1299
|
// src/examples/reply.ts
|
|
1176
|
-
import
|
|
1300
|
+
import * as Console from "effect/Console";
|
|
1301
|
+
import * as Effect9 from "effect/Effect";
|
|
1302
|
+
import * as Schema9 from "effect/Schema";
|
|
1177
1303
|
var reply_default = defineFunction({
|
|
1178
|
-
|
|
1304
|
+
key: "example.org/function/reply",
|
|
1305
|
+
name: "Reply",
|
|
1179
1306
|
description: "Function that echoes the input",
|
|
1180
1307
|
inputSchema: Schema9.Any,
|
|
1181
1308
|
outputSchema: Schema9.Any,
|
|
1182
|
-
handler:
|
|
1309
|
+
handler: Effect9.fn(function* ({ data }) {
|
|
1183
1310
|
yield* Console.log("reply", {
|
|
1184
1311
|
data
|
|
1185
1312
|
});
|
|
@@ -1188,9 +1315,11 @@ var reply_default = defineFunction({
|
|
|
1188
1315
|
});
|
|
1189
1316
|
|
|
1190
1317
|
// src/examples/sleep.ts
|
|
1191
|
-
import
|
|
1318
|
+
import * as Effect10 from "effect/Effect";
|
|
1319
|
+
import * as Schema10 from "effect/Schema";
|
|
1192
1320
|
var sleep_default = defineFunction({
|
|
1193
|
-
|
|
1321
|
+
key: "example.org/function/sleep",
|
|
1322
|
+
name: "Sleep",
|
|
1194
1323
|
description: "Function that sleeps for a given amount of time",
|
|
1195
1324
|
inputSchema: Schema10.Struct({
|
|
1196
1325
|
duration: Schema10.optional(Schema10.Number).annotations({
|
|
@@ -1199,8 +1328,8 @@ var sleep_default = defineFunction({
|
|
|
1199
1328
|
})
|
|
1200
1329
|
}),
|
|
1201
1330
|
outputSchema: Schema10.Void,
|
|
1202
|
-
handler:
|
|
1203
|
-
yield*
|
|
1331
|
+
handler: Effect10.fn(function* ({ data: { duration = 1e5 } }) {
|
|
1332
|
+
yield* Effect10.sleep(duration);
|
|
1204
1333
|
})
|
|
1205
1334
|
});
|
|
1206
1335
|
export {
|
|
@@ -1219,6 +1348,7 @@ export {
|
|
|
1219
1348
|
FunctionError,
|
|
1220
1349
|
FunctionExecutor,
|
|
1221
1350
|
FunctionImplementationResolver,
|
|
1351
|
+
FunctionInvocationService,
|
|
1222
1352
|
FunctionManifestSchema,
|
|
1223
1353
|
FunctionNotFoundError,
|
|
1224
1354
|
FunctionTrigger,
|