@cadenza.io/service 2.9.0 → 2.10.0

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/index.mjs CHANGED
@@ -1,7 +1,5 @@
1
1
  // src/Cadenza.ts
2
- import Cadenza, {
3
- Actor
4
- } from "@cadenza.io/core";
2
+ import Cadenza from "@cadenza.io/core";
5
3
 
6
4
  // src/graph/definition/DeputyTask.ts
7
5
  import { v4 as uuid } from "uuid";
@@ -3299,6 +3297,44 @@ var SocketController = class _SocketController {
3299
3297
  this.diagnosticsMaxClientEntries = 500;
3300
3298
  this.destroyedDiagnosticsTtlMs = 15 * 6e4;
3301
3299
  this.socketServerDefaultKey = "socket-server-default";
3300
+ this.socketServerInitialSessionState = {
3301
+ serverKey: this.socketServerDefaultKey,
3302
+ useSocket: false,
3303
+ status: "inactive",
3304
+ securityProfile: "medium",
3305
+ networkType: "internal",
3306
+ connectionCount: 0,
3307
+ lastStartedAt: null,
3308
+ lastConnectedAt: null,
3309
+ lastDisconnectedAt: null,
3310
+ lastShutdownAt: null,
3311
+ updatedAt: 0
3312
+ };
3313
+ this.socketClientInitialSessionState = {
3314
+ fetchId: "",
3315
+ serviceInstanceId: "",
3316
+ communicationTypes: [],
3317
+ serviceName: "",
3318
+ serviceAddress: "",
3319
+ servicePort: 0,
3320
+ protocol: "http",
3321
+ url: "",
3322
+ socketId: null,
3323
+ connected: false,
3324
+ handshake: false,
3325
+ pendingDelegations: 0,
3326
+ pendingTimers: 0,
3327
+ reconnectAttempts: 0,
3328
+ connectErrors: 0,
3329
+ reconnectErrors: 0,
3330
+ socketErrors: 0,
3331
+ errorCount: 0,
3332
+ destroyed: false,
3333
+ lastHandshakeAt: null,
3334
+ lastHandshakeError: null,
3335
+ lastDisconnectAt: null,
3336
+ updatedAt: 0
3337
+ };
3302
3338
  this.socketServerActor = CadenzaService.createActor(
3303
3339
  {
3304
3340
  name: "SocketServerActor",
@@ -3307,9 +3343,7 @@ var SocketController = class _SocketController {
3307
3343
  keyResolver: (input) => this.resolveSocketServerKey(input),
3308
3344
  loadPolicy: "lazy",
3309
3345
  writeContract: "overwrite",
3310
- initState: this.createInitialSocketServerSessionState(
3311
- this.socketServerDefaultKey
3312
- )
3346
+ initState: this.socketServerInitialSessionState
3313
3347
  },
3314
3348
  { isMeta: true }
3315
3349
  );
@@ -3321,7 +3355,7 @@ var SocketController = class _SocketController {
3321
3355
  keyResolver: (input) => this.resolveSocketClientFetchId(input),
3322
3356
  loadPolicy: "lazy",
3323
3357
  writeContract: "overwrite",
3324
- initState: this.createInitialSocketClientSessionState()
3358
+ initState: this.socketClientInitialSessionState
3325
3359
  },
3326
3360
  { isMeta: true }
3327
3361
  );
@@ -4402,48 +4436,6 @@ var SocketController = class _SocketController {
4402
4436
  "Connects to a specified socket server and wires runtime tasks."
4403
4437
  ).doOn("meta.fetch.handshake_complete").emitsOnFail("meta.socket_client.connect_failed");
4404
4438
  }
4405
- createInitialSocketServerSessionState(serverKey) {
4406
- return {
4407
- serverKey,
4408
- useSocket: false,
4409
- status: "inactive",
4410
- securityProfile: "medium",
4411
- networkType: "internal",
4412
- connectionCount: 0,
4413
- lastStartedAt: null,
4414
- lastConnectedAt: null,
4415
- lastDisconnectedAt: null,
4416
- lastShutdownAt: null,
4417
- updatedAt: 0
4418
- };
4419
- }
4420
- createInitialSocketClientSessionState() {
4421
- return {
4422
- fetchId: "",
4423
- serviceInstanceId: "",
4424
- communicationTypes: [],
4425
- serviceName: "",
4426
- serviceAddress: "",
4427
- servicePort: 0,
4428
- protocol: "http",
4429
- url: "",
4430
- socketId: null,
4431
- connected: false,
4432
- handshake: false,
4433
- pendingDelegations: 0,
4434
- pendingTimers: 0,
4435
- reconnectAttempts: 0,
4436
- connectErrors: 0,
4437
- reconnectErrors: 0,
4438
- socketErrors: 0,
4439
- errorCount: 0,
4440
- destroyed: false,
4441
- lastHandshakeAt: null,
4442
- lastHandshakeError: null,
4443
- lastDisconnectAt: null,
4444
- updatedAt: 0
4445
- };
4446
- }
4447
4439
  resolveSocketServerKey(input) {
4448
4440
  return String(input.serverKey ?? input.__socketServerKey ?? this.socketServerDefaultKey).trim() || this.socketServerDefaultKey;
4449
4441
  }
@@ -4993,6 +4985,22 @@ var GraphMetadataController = class _GraphMetadataController {
4993
4985
  "Handles task execution relationship creation",
4994
4986
  { concurrency: 100, isSubMeta: true }
4995
4987
  ).doOn("meta.node.mapped", "meta.node.detected_previous_task_execution").emits("global.meta.graph_metadata.relationship_executed");
4988
+ CadenzaService.createMetaTask("Handle actor creation", (ctx) => {
4989
+ return {
4990
+ data: {
4991
+ ...ctx.data,
4992
+ service_name: CadenzaService.serviceRegistry.serviceName
4993
+ }
4994
+ };
4995
+ }).doOn("meta.actor.created").emits("global.meta.graph_metadata.actor_created");
4996
+ CadenzaService.createMetaTask("Handle actor task association", (ctx) => {
4997
+ return {
4998
+ data: {
4999
+ ...ctx.data,
5000
+ service_name: CadenzaService.serviceRegistry.serviceName
5001
+ }
5002
+ };
5003
+ }).doOn("meta.actor.task_associated").emits("global.meta.graph_metadata.actor_task_associated");
4996
5004
  CadenzaService.createMetaTask("Handle Intent Creation", (ctx) => {
4997
5005
  const intentName = ctx.data?.name;
4998
5006
  return {
@@ -6664,8 +6672,69 @@ function tableFieldTypeToSchemaType(type) {
6664
6672
  import { v4 as uuid3 } from "uuid";
6665
6673
 
6666
6674
  // src/graph/controllers/GraphSyncController.ts
6675
+ var ACTOR_TASK_METADATA = /* @__PURE__ */ Symbol.for("@cadenza.io/core/actor-task-meta");
6676
+ function getActorTaskRuntimeMetadata(taskFunction) {
6677
+ if (typeof taskFunction !== "function") {
6678
+ return void 0;
6679
+ }
6680
+ return taskFunction[ACTOR_TASK_METADATA];
6681
+ }
6682
+ function sanitizeActorMetadataValue(value) {
6683
+ if (value === null) {
6684
+ return null;
6685
+ }
6686
+ if (value === void 0 || typeof value === "function") {
6687
+ return void 0;
6688
+ }
6689
+ if (Array.isArray(value)) {
6690
+ const items = [];
6691
+ for (const item of value) {
6692
+ const sanitizedItem = sanitizeActorMetadataValue(item);
6693
+ if (sanitizedItem !== void 0) {
6694
+ items.push(sanitizedItem);
6695
+ }
6696
+ }
6697
+ return items;
6698
+ }
6699
+ if (typeof value === "object") {
6700
+ const output = {};
6701
+ for (const [key, nestedValue] of Object.entries(value)) {
6702
+ const sanitizedNestedValue = sanitizeActorMetadataValue(nestedValue);
6703
+ if (sanitizedNestedValue !== void 0) {
6704
+ output[key] = sanitizedNestedValue;
6705
+ }
6706
+ }
6707
+ return output;
6708
+ }
6709
+ return value;
6710
+ }
6711
+ function buildActorRegistrationData(actor) {
6712
+ const definition = sanitizeActorMetadataValue(
6713
+ typeof actor?.toDefinition === "function" ? actor.toDefinition() : {}
6714
+ );
6715
+ const stateDefinition = definition?.state && typeof definition.state === "object" ? definition.state : {};
6716
+ const actorKind = typeof definition?.kind === "string" ? definition.kind : actor?.kind;
6717
+ return {
6718
+ name: definition?.name ?? actor?.spec?.name ?? "",
6719
+ description: definition?.description ?? actor?.spec?.description ?? "",
6720
+ default_key: definition?.defaultKey ?? actor?.spec?.defaultKey ?? "default",
6721
+ load_policy: definition?.loadPolicy ?? actor?.spec?.loadPolicy ?? "eager",
6722
+ write_contract: definition?.writeContract ?? actor?.spec?.writeContract ?? "overwrite",
6723
+ runtime_read_guard: definition?.runtimeReadGuard ?? actor?.spec?.runtimeReadGuard ?? "none",
6724
+ consistency_profile: definition?.consistencyProfile ?? actor?.spec?.consistencyProfile ?? null,
6725
+ key_definition: definition?.key ?? null,
6726
+ state_definition: stateDefinition,
6727
+ retry_policy: definition?.retry ?? {},
6728
+ idempotency_policy: definition?.idempotency ?? {},
6729
+ session_policy: definition?.session ?? {},
6730
+ is_meta: actorKind === "meta",
6731
+ version: 1
6732
+ };
6733
+ }
6667
6734
  var GraphSyncController = class _GraphSyncController {
6668
6735
  constructor() {
6736
+ this.registeredActors = /* @__PURE__ */ new Set();
6737
+ this.registeredActorTaskMaps = /* @__PURE__ */ new Set();
6669
6738
  this.isCadenzaDBReady = false;
6670
6739
  }
6671
6740
  static get instance() {
@@ -6923,6 +6992,120 @@ var GraphSyncController = class _GraphSyncController {
6923
6992
  )
6924
6993
  )
6925
6994
  );
6995
+ this.splitActorsForRegistration = CadenzaService.createMetaTask(
6996
+ "Split actors for registration",
6997
+ function* (ctx) {
6998
+ CadenzaService.debounce("meta.sync_controller.synced_resource", {
6999
+ delayMs: 3e3
7000
+ });
7001
+ const actors = ctx.actors ?? [];
7002
+ for (const actor of actors) {
7003
+ const data = {
7004
+ ...buildActorRegistrationData(actor),
7005
+ service_name: CadenzaService.serviceRegistry.serviceName
7006
+ };
7007
+ if (!data.name) {
7008
+ continue;
7009
+ }
7010
+ const registrationKey = `${data.name}|${data.version}|${data.service_name}`;
7011
+ if (this.registeredActors.has(registrationKey)) {
7012
+ continue;
7013
+ }
7014
+ yield {
7015
+ data,
7016
+ __actorRegistrationKey: registrationKey
7017
+ };
7018
+ }
7019
+ }.bind(this)
7020
+ ).then(
7021
+ (this.isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(
7022
+ "actor",
7023
+ {
7024
+ onConflict: {
7025
+ target: ["name", "service_name", "version"],
7026
+ action: {
7027
+ do: "nothing"
7028
+ }
7029
+ }
7030
+ },
7031
+ { concurrency: 30 }
7032
+ ) : CadenzaService.get("dbInsertActor"))?.then(
7033
+ CadenzaService.createMetaTask("Record actor registration", (ctx) => {
7034
+ if (!ctx.__syncing) {
7035
+ return;
7036
+ }
7037
+ CadenzaService.debounce("meta.sync_controller.synced_resource", {
7038
+ delayMs: 3e3
7039
+ });
7040
+ this.registeredActors.add(ctx.__actorRegistrationKey);
7041
+ return true;
7042
+ }).then(
7043
+ CadenzaService.createUniqueMetaTask(
7044
+ "Gather actor registration",
7045
+ () => true
7046
+ ).emits("meta.sync_controller.synced_actors")
7047
+ )
7048
+ )
7049
+ );
7050
+ this.registerActorTaskMapTask = CadenzaService.createMetaTask(
7051
+ "Split actor task maps",
7052
+ function* (ctx) {
7053
+ const task = ctx.task;
7054
+ if (task.hidden || !task.register) {
7055
+ return;
7056
+ }
7057
+ const metadata = getActorTaskRuntimeMetadata(task.taskFunction);
7058
+ if (!metadata?.actorName) {
7059
+ return;
7060
+ }
7061
+ const registrationKey = `${metadata.actorName}|${task.name}|${task.version}|${CadenzaService.serviceRegistry.serviceName}`;
7062
+ if (this.registeredActorTaskMaps.has(registrationKey)) {
7063
+ return;
7064
+ }
7065
+ yield {
7066
+ data: {
7067
+ actor_name: metadata.actorName,
7068
+ actor_version: 1,
7069
+ task_name: task.name,
7070
+ task_version: task.version,
7071
+ service_name: CadenzaService.serviceRegistry.serviceName,
7072
+ mode: metadata.mode,
7073
+ description: task.description ?? metadata.actorDescription ?? "",
7074
+ is_meta: metadata.actorKind === "meta" || task.isMeta === true
7075
+ },
7076
+ __actorTaskMapRegistrationKey: registrationKey
7077
+ };
7078
+ }.bind(this)
7079
+ ).then(
7080
+ (this.isCadenzaDBReady ? CadenzaService.createCadenzaDBInsertTask(
7081
+ "actor_task_map",
7082
+ {
7083
+ onConflict: {
7084
+ target: [
7085
+ "actor_name",
7086
+ "actor_version",
7087
+ "task_name",
7088
+ "task_version",
7089
+ "service_name"
7090
+ ],
7091
+ action: {
7092
+ do: "nothing"
7093
+ }
7094
+ }
7095
+ },
7096
+ { concurrency: 30 }
7097
+ ) : CadenzaService.get("dbInsertActorTaskMap"))?.then(
7098
+ CadenzaService.createMetaTask("Record actor task map registration", (ctx) => {
7099
+ if (!ctx.__syncing) {
7100
+ return;
7101
+ }
7102
+ CadenzaService.debounce("meta.sync_controller.synced_resource", {
7103
+ delayMs: 3e3
7104
+ });
7105
+ this.registeredActorTaskMaps.add(ctx.__actorTaskMapRegistrationKey);
7106
+ })
7107
+ )
7108
+ );
6926
7109
  const registerSignalTask = CadenzaService.createMetaTask(
6927
7110
  "Record signal registration",
6928
7111
  (ctx) => {
@@ -7164,12 +7347,19 @@ var GraphSyncController = class _GraphSyncController {
7164
7347
  ).then(this.splitSignalsTask);
7165
7348
  CadenzaService.registry.getAllTasks.clone().doOn("meta.sync_controller.synced_signals").then(this.splitTasksForRegistration);
7166
7349
  CadenzaService.registry.getAllRoutines.clone().doOn("meta.sync_controller.synced_tasks").then(this.splitRoutinesTask);
7350
+ CadenzaService.createMetaTask("Get all actors", (ctx) => {
7351
+ return {
7352
+ ...ctx,
7353
+ actors: CadenzaService.getAllActors()
7354
+ };
7355
+ }).doOn("meta.sync_controller.synced_tasks").then(this.splitActorsForRegistration);
7167
7356
  CadenzaService.registry.doForEachTask.clone().doOn("meta.sync_controller.synced_tasks").then(
7168
7357
  this.registerTaskMapTask,
7169
7358
  this.registerSignalToTaskMapTask,
7170
7359
  this.registerIntentToTaskMapTask,
7171
7360
  this.registerDeputyRelationshipTask
7172
7361
  );
7362
+ CadenzaService.registry.doForEachTask.clone().doOn("meta.sync_controller.synced_tasks", "meta.sync_controller.synced_actors").then(this.registerActorTaskMapTask);
7173
7363
  CadenzaService.registry.getAllRoutines.clone().doOn("meta.sync_controller.synced_routines").then(this.splitTasksInRoutines);
7174
7364
  CadenzaService.createMetaTask("Finish sync", (ctx, emit) => {
7175
7365
  emit("global.meta.sync_controller.synced", {
@@ -7556,6 +7746,14 @@ var CadenzaService = class {
7556
7746
  static get(taskName) {
7557
7747
  return Cadenza.get(taskName);
7558
7748
  }
7749
+ static getActor(actorName) {
7750
+ const cadenzaWithActors = Cadenza;
7751
+ return cadenzaWithActors.getActor?.(actorName);
7752
+ }
7753
+ static getAllActors() {
7754
+ const cadenzaWithActors = Cadenza;
7755
+ return cadenzaWithActors.getAllActors?.() ?? [];
7756
+ }
7559
7757
  static getRoutine(routineName) {
7560
7758
  return Cadenza.getRoutine(routineName);
7561
7759
  }
@@ -8123,7 +8321,7 @@ var CadenzaService = class {
8123
8321
  }
8124
8322
  static createActor(spec, options = {}) {
8125
8323
  this.bootstrap();
8126
- return new Actor(spec, options);
8324
+ return Cadenza.createActor(spec, options);
8127
8325
  }
8128
8326
  static createActorFromDefinition(definition, options = {}) {
8129
8327
  this.bootstrap();