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