@dxos/functions 0.8.4-main.1da679c → 0.8.4-main.1f223c7

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.
@@ -181,7 +181,6 @@ var deserializeFunction = (functionObj) => {
181
181
  description: functionObj.description,
182
182
  inputSchema: !functionObj.inputSchema ? Schema2.Unknown : Type2.toEffectSchema(functionObj.inputSchema),
183
183
  outputSchema: !functionObj.outputSchema ? void 0 : Type2.toEffectSchema(functionObj.outputSchema),
184
- // TODO(dmaretskyi): This should throw error.
185
184
  handler: () => {
186
185
  }
187
186
  };
@@ -196,7 +195,7 @@ import { log } from "@dxos/log";
196
195
 
197
196
  // src/types.ts
198
197
  import { Schema as Schema3, SchemaAST } from "effect";
199
- import { Obj as Obj2, QueryAST, Type as Type3 } from "@dxos/echo";
198
+ import { QueryAST, Type as Type3 } from "@dxos/echo";
200
199
  import { Expando, OptionsAnnotationId, RawObject, Ref as Ref2 } from "@dxos/echo-schema";
201
200
  import { DXN } from "@dxos/keys";
202
201
  var TriggerKinds = [
@@ -281,21 +280,10 @@ var QueueTriggerOutput = Schema3.mutable(Schema3.Struct({
281
280
  item: Schema3.Any,
282
281
  cursor: Schema3.String
283
282
  }));
284
- var SubscriptionTriggerOutput = Schema3.Struct({
285
- /**
286
- * Type of the mutation.
287
- */
288
- // TODO(dmaretskyi): Specify enum.
283
+ var SubscriptionTriggerOutput = Schema3.mutable(Schema3.Struct({
289
284
  type: Schema3.String,
290
- /**
291
- * Reference to the object that was changed or created.
292
- */
293
- subject: Type3.Ref(Obj2.Any),
294
- /**
295
- * @deprecated
296
- */
297
- changedObjectId: Schema3.optional(Schema3.String)
298
- }).pipe(Schema3.mutable);
285
+ changedObjectId: Schema3.String
286
+ }));
299
287
  var TimerTriggerOutput = Schema3.mutable(Schema3.Struct({
300
288
  tick: Schema3.Number
301
289
  }));
@@ -350,17 +338,17 @@ var FUNCTION_TYPES = [
350
338
 
351
339
  // src/trace.ts
352
340
  var __dxlog_file = "/__w/dxos/dxos/packages/core/functions/src/trace.ts";
353
- var InvocationOutcome = /* @__PURE__ */ (function(InvocationOutcome2) {
341
+ var InvocationOutcome = /* @__PURE__ */ function(InvocationOutcome2) {
354
342
  InvocationOutcome2["SUCCESS"] = "success";
355
343
  InvocationOutcome2["FAILURE"] = "failure";
356
344
  InvocationOutcome2["PENDING"] = "pending";
357
345
  return InvocationOutcome2;
358
- })({});
359
- var InvocationTraceEventType = /* @__PURE__ */ (function(InvocationTraceEventType2) {
346
+ }({});
347
+ var InvocationTraceEventType = /* @__PURE__ */ function(InvocationTraceEventType2) {
360
348
  InvocationTraceEventType2["START"] = "start";
361
349
  InvocationTraceEventType2["END"] = "end";
362
350
  return InvocationTraceEventType2;
363
- })({});
351
+ }({});
364
352
  var TraceEventException = Schema4.Struct({
365
353
  timestamp: Schema4.Number,
366
354
  message: Schema4.String,
@@ -495,13 +483,12 @@ var createInvocationSpans = (items) => {
495
483
 
496
484
  // src/triggers/invocation-tracer.ts
497
485
  import { Context as Context2, Effect as Effect3, Layer as Layer2 } from "effect";
498
- import { Obj as Obj3, Ref as Ref3 } from "@dxos/echo";
486
+ import { Obj as Obj2, Ref as Ref3 } from "@dxos/echo";
499
487
  import { DXN as DXN2, ObjectId as ObjectId2 } from "@dxos/keys";
500
488
 
501
489
  // src/services/local-function-execution.ts
502
490
  import { Context, Effect as Effect2, Layer, Schema as Schema5 } from "effect";
503
491
  import { todo } from "@dxos/debug";
504
- import { log as log2 } from "@dxos/log";
505
492
  function _define_property(obj, key, value) {
506
493
  if (key in obj) {
507
494
  Object.defineProperty(obj, key, {
@@ -515,7 +502,6 @@ function _define_property(obj, key, value) {
515
502
  }
516
503
  return obj;
517
504
  }
518
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/services/local-function-execution.ts";
519
505
  var _Context_Tag;
520
506
  var LocalFunctionExecutionService = class extends (_Context_Tag = Context.Tag("@dxos/functions/LocalFunctionExecutionService")()) {
521
507
  };
@@ -533,18 +519,8 @@ _define_property(LocalFunctionExecutionService, "layerLive", Layer.effect(LocalF
533
519
  })));
534
520
  _define_property(LocalFunctionExecutionService, "invokeFunction", Effect2.serviceFunctionEffect(LocalFunctionExecutionService, (_) => _.invokeFunction));
535
521
  var invokeFunction = (functionDef, input) => Effect2.gen(function* () {
536
- try {
537
- const assertInput = functionDef.inputSchema.pipe(Schema5.asserts);
538
- assertInput(input);
539
- } catch (e) {
540
- throw new FunctionError({
541
- message: "Invalid function input",
542
- context: {
543
- name: functionDef.name
544
- },
545
- cause: e
546
- });
547
- }
522
+ const assertInput = functionDef.inputSchema.pipe(Schema5.asserts);
523
+ assertInput(input);
548
524
  const context = {
549
525
  space: void 0,
550
526
  getService: () => todo(),
@@ -552,15 +528,6 @@ var invokeFunction = (functionDef, input) => Effect2.gen(function* () {
552
528
  throw new Error("Not available. Use the database service instead.");
553
529
  }
554
530
  };
555
- log2.info("Invoking function", {
556
- name: functionDef.name,
557
- input
558
- }, {
559
- F: __dxlog_file2,
560
- L: 72,
561
- S: this,
562
- C: (f, a) => f(...a)
563
- });
564
531
  const data = yield* Effect2.gen(function* () {
565
532
  const result = functionDef.handler({
566
533
  context,
@@ -579,28 +546,8 @@ var invokeFunction = (functionDef, input) => Effect2.gen(function* () {
579
546
  },
580
547
  cause: defect
581
548
  }))));
582
- log2.info("Function completed", {
583
- name: functionDef.name,
584
- input,
585
- data
586
- }, {
587
- F: __dxlog_file2,
588
- L: 96,
589
- S: this,
590
- C: (f, a) => f(...a)
591
- });
592
- try {
593
- const assertOutput = functionDef.outputSchema?.pipe(Schema5.asserts);
594
- assertOutput(data);
595
- } catch (e) {
596
- throw new FunctionError({
597
- message: "Invalid function output",
598
- context: {
599
- name: functionDef.name
600
- },
601
- cause: e
602
- });
603
- }
549
+ const assertOutput = functionDef.outputSchema?.pipe(Schema5.asserts);
550
+ assertOutput(data);
604
551
  return data;
605
552
  }).pipe(Effect2.withSpan("invokeFunction", {
606
553
  attributes: {
@@ -645,7 +592,7 @@ _define_property2(InvocationTracer, "layerLive", (opts) => Layer2.effect(Invocat
645
592
  subspaceTag: "trace"
646
593
  });
647
594
  const now = Date.now();
648
- const traceEvent = Obj3.make(InvocationTraceStartEvent, {
595
+ const traceEvent = Obj2.make(InvocationTraceStartEvent, {
649
596
  type: InvocationTraceEventType.START,
650
597
  invocationId,
651
598
  timestamp: now,
@@ -665,7 +612,7 @@ _define_property2(InvocationTracer, "layerLive", (opts) => Layer2.effect(Invocat
665
612
  }),
666
613
  traceInvocationEnd: Effect3.fn("traceInvocationEnd")(function* ({ trace, exception }) {
667
614
  const now = Date.now();
668
- const traceEvent = Obj3.make(InvocationTraceEndEvent, {
615
+ const traceEvent = Obj2.make(InvocationTraceEndEvent, {
669
616
  type: InvocationTraceEventType.END,
670
617
  invocationId: trace.invocationId,
671
618
  timestamp: now,
@@ -694,10 +641,10 @@ _define_property2(InvocationTracer, "layerTest", Layer2.unwrapEffect(Effect3.gen
694
641
 
695
642
  // src/triggers/trigger-dispatcher.ts
696
643
  import { Cause, Context as Context4, Cron, Duration, Effect as Effect5, Either, Exit, Fiber, Layer as Layer4, Option as Option2, Record, Schedule } from "effect";
697
- import { DXN as DXN3, Filter, Obj as Obj4, Query } from "@dxos/echo";
644
+ import { DXN as DXN3, Filter, Obj as Obj3, Query } from "@dxos/echo";
698
645
  import { causeToError } from "@dxos/effect";
699
646
  import { invariant } from "@dxos/invariant";
700
- import { log as log3 } from "@dxos/log";
647
+ import { log as log2 } from "@dxos/log";
701
648
  import { KEY_QUEUE_POSITION } from "@dxos/protocols";
702
649
 
703
650
  // src/triggers/input-builder.ts
@@ -791,7 +738,7 @@ function _define_property4(obj, key, value) {
791
738
  }
792
739
  return obj;
793
740
  }
794
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/functions/src/triggers/trigger-dispatcher.ts";
741
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/functions/src/triggers/trigger-dispatcher.ts";
795
742
  var _Context_Tag4;
796
743
  var TriggerDispatcher = class extends (_Context_Tag4 = Context4.Tag("@dxos/functions/TriggerDispatcher")()) {
797
744
  };
@@ -817,10 +764,10 @@ var TriggerDispatcherImpl = class {
817
764
  if (this.timeControl === "natural") {
818
765
  this._timerFiber = yield* this._startNaturalTimeProcessing().pipe(Effect5.tapErrorCause((cause) => {
819
766
  const error = causeToError(cause);
820
- log3.error("trigger dispatcher error", {
767
+ log2.error("trigger dispatcher error", {
821
768
  error
822
769
  }, {
823
- F: __dxlog_file3,
770
+ F: __dxlog_file2,
824
771
  L: 170,
825
772
  S: this,
826
773
  C: (f, a) => f(...a)
@@ -831,10 +778,10 @@ var TriggerDispatcherImpl = class {
831
778
  } else {
832
779
  return yield* Effect5.dieMessage("TriggerDispatcher started in manual time control mode");
833
780
  }
834
- log3.info("TriggerDispatcher started", {
781
+ log2.info("TriggerDispatcher started", {
835
782
  timeControl: this.timeControl
836
783
  }, {
837
- F: __dxlog_file3,
784
+ F: __dxlog_file2,
838
785
  L: 180,
839
786
  S: this,
840
787
  C: (f, a) => f(...a)
@@ -850,8 +797,8 @@ var TriggerDispatcherImpl = class {
850
797
  this._timerFiber = void 0;
851
798
  }
852
799
  this._scheduledTriggers.clear();
853
- log3.info("TriggerDispatcher stopped", void 0, {
854
- F: __dxlog_file3,
800
+ log2.info("TriggerDispatcher stopped", void 0, {
801
+ F: __dxlog_file2,
855
802
  L: 200,
856
803
  S: this,
857
804
  C: (f, a) => f(...a)
@@ -859,12 +806,12 @@ var TriggerDispatcherImpl = class {
859
806
  }));
860
807
  _define_property4(this, "invokeTrigger", (options2) => Effect5.gen(this, function* () {
861
808
  const { trigger, event } = options2;
862
- log3.info("running trigger", {
809
+ log2.info("running trigger", {
863
810
  triggerId: trigger.id,
864
811
  spec: trigger.spec,
865
812
  event
866
813
  }, {
867
- F: __dxlog_file3,
814
+ F: __dxlog_file2,
868
815
  L: 208,
869
816
  S: this,
870
817
  C: (f, a) => f(...a)
@@ -889,8 +836,8 @@ var TriggerDispatcherImpl = class {
889
836
  return yield* Effect5.dieMessage("Trigger has no function reference");
890
837
  }
891
838
  const serialiedFunction = yield* DatabaseService.load(trigger.function).pipe(Effect5.orDie);
892
- invariant(Obj4.instanceOf(FunctionType, serialiedFunction), void 0, {
893
- F: __dxlog_file3,
839
+ invariant(Obj3.instanceOf(FunctionType, serialiedFunction), void 0, {
840
+ F: __dxlog_file2,
894
841
  L: 235,
895
842
  S: this,
896
843
  A: [
@@ -907,19 +854,19 @@ var TriggerDispatcherImpl = class {
907
854
  result
908
855
  };
909
856
  if (Exit.isSuccess(result)) {
910
- log3.info("trigger execution success", {
857
+ log2.info("trigger execution success", {
911
858
  triggerId: trigger.id
912
859
  }, {
913
- F: __dxlog_file3,
860
+ F: __dxlog_file2,
914
861
  L: 256,
915
862
  S: this,
916
863
  C: (f, a) => f(...a)
917
864
  });
918
865
  } else {
919
- log3.error("trigger execution failure", {
866
+ log2.error("trigger execution failure", {
920
867
  error: causeToError(result.cause)
921
868
  }, {
922
- F: __dxlog_file3,
869
+ F: __dxlog_file2,
923
870
  L: 260,
924
871
  S: this,
925
872
  C: (f, a) => f(...a)
@@ -968,11 +915,11 @@ var TriggerDispatcherImpl = class {
968
915
  if (spec?.kind !== "queue") {
969
916
  continue;
970
917
  }
971
- const cursor = Obj4.getKeys(trigger, KEY_QUEUE_CURSOR).at(0)?.id;
918
+ const cursor = Obj3.getKeys(trigger, KEY_QUEUE_CURSOR).at(0)?.id;
972
919
  const queue = yield* QueueService.getQueue(DXN3.parse(spec.queue));
973
920
  const objects = yield* Effect5.promise(() => queue.queryObjects());
974
921
  for (const object of objects) {
975
- const objectPos = Obj4.getKeys(object, KEY_QUEUE_POSITION).at(0)?.id;
922
+ const objectPos = Obj3.getKeys(object, KEY_QUEUE_POSITION).at(0)?.id;
976
923
  if (!objectPos || cursor && parseInt(cursor) >= parseInt(objectPos)) {
977
924
  continue;
978
925
  }
@@ -984,8 +931,8 @@ var TriggerDispatcherImpl = class {
984
931
  cursor: objectPos
985
932
  }
986
933
  }));
987
- Obj4.deleteKeys(trigger, KEY_QUEUE_CURSOR);
988
- Obj4.getMeta(trigger).keys.push({
934
+ Obj3.deleteKeys(trigger, KEY_QUEUE_CURSOR);
935
+ Obj3.getMeta(trigger).keys.push({
989
936
  source: KEY_QUEUE_CURSOR,
990
937
  id: objectPos
991
938
  });
@@ -998,7 +945,7 @@ var TriggerDispatcherImpl = class {
998
945
  case "subscription": {
999
946
  const triggers = yield* this._fetchTriggers();
1000
947
  for (const trigger of triggers) {
1001
- const spec = Obj4.getSnapshot(trigger).spec;
948
+ const spec = Obj3.getSnapshot(trigger).spec;
1002
949
  if (spec?.kind !== "subscription") {
1003
950
  continue;
1004
951
  }
@@ -1012,7 +959,7 @@ var TriggerDispatcherImpl = class {
1012
959
  }
1013
960
  })));
1014
961
  invariant(state.state?._tag === "subscription", void 0, {
1015
- F: __dxlog_file3,
962
+ F: __dxlog_file2,
1016
963
  L: 373,
1017
964
  S: this,
1018
965
  A: [
@@ -1022,23 +969,21 @@ var TriggerDispatcherImpl = class {
1022
969
  });
1023
970
  let updated = false;
1024
971
  for (const object of objects) {
1025
- const existingVersion = Record.get(state.state.processedVersions, object.id).pipe(Option2.map(Obj4.decodeVersion));
1026
- const currentVersion = Obj4.version(object);
1027
- const run = Option2.isNone(existingVersion) || Obj4.compareVersions(currentVersion, existingVersion.value) === "different";
972
+ const existingVersion = Record.get(state.state.processedVersions, object.id).pipe(Option2.map(Obj3.decodeVersion));
973
+ const currentVersion = Obj3.version(object);
974
+ const run = Option2.isNone(existingVersion) || Obj3.compareVersions(currentVersion, existingVersion.value) === "different";
1028
975
  if (!run) {
1029
976
  continue;
1030
977
  }
1031
- const { db } = yield* DatabaseService;
1032
978
  invocations.push(yield* this.invokeTrigger({
1033
979
  trigger,
1034
980
  event: {
1035
981
  // TODO(dmaretskyi): Change type not supported.
1036
982
  type: "unknown",
1037
- subject: db.ref(Obj4.getDXN(object)),
1038
983
  changedObjectId: object.id
1039
984
  }
1040
985
  }));
1041
- state.state.processedVersions[object.id] = Obj4.encodeVersion(currentVersion);
986
+ state.state.processedVersions[object.id] = Obj3.encodeVersion(currentVersion);
1042
987
  updated = true;
1043
988
  }
1044
989
  if (updated) {
@@ -1060,12 +1005,12 @@ var TriggerDispatcherImpl = class {
1060
1005
  }
1061
1006
  const millis = Duration.toMillis(duration);
1062
1007
  this._internalTime = new Date(this._internalTime.getTime() + millis);
1063
- log3("Advanced internal time", {
1008
+ log2("Advanced internal time", {
1064
1009
  newTime: this._internalTime,
1065
1010
  advancedBy: Duration.format(duration)
1066
1011
  }, {
1067
- F: __dxlog_file3,
1068
- L: 430,
1012
+ F: __dxlog_file2,
1013
+ L: 426,
1069
1014
  S: this,
1070
1015
  C: (f, a) => f(...a)
1071
1016
  });
@@ -1094,14 +1039,14 @@ var TriggerDispatcherImpl = class {
1094
1039
  const existing = this._scheduledTriggers.get(trigger.id);
1095
1040
  const now = this.getCurrentTime();
1096
1041
  const nextExecution = existing?.nextExecution ?? Cron.next(cron, now);
1097
- log3("Updated scheduled trigger", {
1042
+ log2("Updated scheduled trigger", {
1098
1043
  triggerId: trigger.id,
1099
1044
  cron: timerSpec.cron,
1100
1045
  nextExecution,
1101
1046
  now
1102
1047
  }, {
1103
- F: __dxlog_file3,
1104
- L: 470,
1048
+ F: __dxlog_file2,
1049
+ L: 466,
1105
1050
  S: this,
1106
1051
  C: (f, a) => f(...a)
1107
1052
  });
@@ -1111,24 +1056,24 @@ var TriggerDispatcherImpl = class {
1111
1056
  nextExecution
1112
1057
  });
1113
1058
  } else {
1114
- log3.error("Invalid cron expression", {
1059
+ log2.error("Invalid cron expression", {
1115
1060
  triggerId: trigger.id,
1116
1061
  cron: timerSpec.cron,
1117
1062
  error: cronEither.left.message
1118
1063
  }, {
1119
- F: __dxlog_file3,
1120
- L: 482,
1064
+ F: __dxlog_file2,
1065
+ L: 478,
1121
1066
  S: this,
1122
1067
  C: (f, a) => f(...a)
1123
1068
  });
1124
1069
  }
1125
1070
  }
1126
1071
  }
1127
- log3("Updated scheduled triggers", {
1072
+ log2("Updated scheduled triggers", {
1128
1073
  count: this._scheduledTriggers.size
1129
1074
  }, {
1130
- F: __dxlog_file3,
1131
- L: 491,
1075
+ F: __dxlog_file2,
1076
+ L: 487,
1132
1077
  S: this,
1133
1078
  C: (f, a) => f(...a)
1134
1079
  });