@cadenza.io/service 2.17.74 → 2.17.76

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.
@@ -1161,39 +1161,12 @@ var ServiceRegistry = class _ServiceRegistry {
1161
1161
  if (trackedInstance?.isFrontend) {
1162
1162
  return true;
1163
1163
  }
1164
- const trackedTransport = this.getRouteableTransport(
1165
- trackedInstance,
1166
- this.useSocket ? "socket" : "rest"
1167
- );
1168
1164
  if (this.deputies.has(serviceName) || this.remoteIntents.has(serviceName) || this.remoteSignals.has(serviceName)) {
1169
- const communicationTypes = Array.from(
1170
- new Set(
1171
- this.deputies.get(serviceName)?.map((d) => d.communicationType) ?? []
1172
- )
1165
+ const connected = this.ensureDependeeClientForInstance(
1166
+ trackedInstance,
1167
+ emit
1173
1168
  );
1174
- if (!communicationTypes.includes("signal") && this.remoteSignals.has(serviceName)) {
1175
- communicationTypes.push("signal");
1176
- }
1177
- if (trackedTransport) {
1178
- const clientCreated = this.hasTransportClientCreated(
1179
- trackedInstance,
1180
- trackedTransport.uuid
1181
- );
1182
- if (!clientCreated) {
1183
- emit("meta.service_registry.dependee_registered", {
1184
- serviceName,
1185
- serviceInstanceId: uuid7,
1186
- serviceTransportId: trackedTransport.uuid,
1187
- serviceOrigin: trackedTransport.origin,
1188
- transportProtocols: trackedTransport.protocols,
1189
- communicationTypes
1190
- });
1191
- this.markTransportClientCreated(
1192
- trackedInstance,
1193
- trackedTransport.uuid
1194
- );
1195
- }
1196
- } else {
1169
+ if (!connected) {
1197
1170
  emit("meta.service_registry.routeable_transport_missing", {
1198
1171
  serviceName,
1199
1172
  serviceInstanceId: uuid7,
@@ -1259,25 +1232,7 @@ var ServiceRegistry = class _ServiceRegistry {
1259
1232
  if (!hasRemoteInterest) {
1260
1233
  return true;
1261
1234
  }
1262
- if (!this.hasTransportClientCreated(ownerInstance, transport.uuid)) {
1263
- const communicationTypes = Array.from(
1264
- new Set(
1265
- this.deputies.get(ownerInstance.serviceName)?.map((descriptor) => descriptor.communicationType) ?? []
1266
- )
1267
- );
1268
- if (!communicationTypes.includes("signal") && this.remoteSignals.has(ownerInstance.serviceName)) {
1269
- communicationTypes.push("signal");
1270
- }
1271
- emit("meta.service_registry.dependee_registered", {
1272
- serviceName: ownerInstance.serviceName,
1273
- serviceInstanceId: ownerInstance.uuid,
1274
- serviceTransportId: transport.uuid,
1275
- serviceOrigin: transport.origin,
1276
- transportProtocols: transport.protocols,
1277
- communicationTypes
1278
- });
1279
- this.markTransportClientCreated(ownerInstance, transport.uuid);
1280
- }
1235
+ this.ensureDependeeClientForInstance(ownerInstance, emit);
1281
1236
  return true;
1282
1237
  },
1283
1238
  "Handles service transport updates independently from instance rows."
@@ -1358,7 +1313,7 @@ var ServiceRegistry = class _ServiceRegistry {
1358
1313
  ).emits("meta.service_registry.registered_global_signals").doOn("global.meta.cadenza_db.gathered_sync_data");
1359
1314
  this.handleGlobalIntentRegistrationTask = CadenzaService.createMetaTask(
1360
1315
  "Handle global intent registration",
1361
- (ctx) => {
1316
+ (ctx, emit) => {
1362
1317
  const intentToTaskMaps = this.normalizeIntentMaps(ctx);
1363
1318
  const sorted = intentToTaskMaps.sort((a, b) => {
1364
1319
  if (a.deleted && !b.deleted) return -1;
@@ -1374,6 +1329,7 @@ var ServiceRegistry = class _ServiceRegistry {
1374
1329
  name: map.intentName
1375
1330
  });
1376
1331
  this.registerRemoteIntentDeputy(map);
1332
+ this.ensureDependeeClientsForService(map.serviceName, emit);
1377
1333
  }
1378
1334
  return true;
1379
1335
  },
@@ -2799,6 +2755,49 @@ var ServiceRegistry = class _ServiceRegistry {
2799
2755
  }
2800
2756
  return buildTransportClientKey(transport);
2801
2757
  }
2758
+ resolveCommunicationTypesForService(serviceName) {
2759
+ const communicationTypes = Array.from(
2760
+ new Set(
2761
+ this.deputies.get(serviceName)?.map((descriptor) => descriptor.communicationType) ?? []
2762
+ )
2763
+ );
2764
+ if (!communicationTypes.includes("signal") && this.remoteSignals.has(serviceName)) {
2765
+ communicationTypes.push("signal");
2766
+ }
2767
+ return communicationTypes;
2768
+ }
2769
+ ensureDependeeClientForInstance(instance, emit) {
2770
+ if (!instance || instance.uuid === this.serviceInstanceId || instance.isFrontend || !instance.isActive || instance.isNonResponsive || instance.isBlocked) {
2771
+ return false;
2772
+ }
2773
+ if (!this.deputies.has(instance.serviceName) && !this.remoteIntents.has(instance.serviceName) && !this.remoteSignals.has(instance.serviceName)) {
2774
+ return false;
2775
+ }
2776
+ const transport = this.getRouteableTransport(
2777
+ instance,
2778
+ this.useSocket ? "socket" : "rest"
2779
+ );
2780
+ if (!transport || this.hasTransportClientCreated(instance, transport.uuid)) {
2781
+ return false;
2782
+ }
2783
+ emit("meta.service_registry.dependee_registered", {
2784
+ serviceName: instance.serviceName,
2785
+ serviceInstanceId: instance.uuid,
2786
+ serviceTransportId: transport.uuid,
2787
+ serviceOrigin: transport.origin,
2788
+ transportProtocols: transport.protocols,
2789
+ communicationTypes: this.resolveCommunicationTypesForService(
2790
+ instance.serviceName
2791
+ )
2792
+ });
2793
+ this.markTransportClientCreated(instance, transport.uuid);
2794
+ return true;
2795
+ }
2796
+ ensureDependeeClientsForService(serviceName, emit) {
2797
+ for (const instance of this.instances.get(serviceName) ?? []) {
2798
+ this.ensureDependeeClientForInstance(instance, emit);
2799
+ }
2800
+ }
2802
2801
  hasTransportClientCreated(instance, transportId) {
2803
2802
  return (instance.clientCreatedTransportIds ?? []).includes(transportId);
2804
2803
  }
@@ -3582,13 +3581,14 @@ var RestController = class _RestController {
3582
3581
  this.diagnosticsErrorHistoryLimit = 100;
3583
3582
  this.diagnosticsMaxClientEntries = 500;
3584
3583
  this.destroyedDiagnosticsTtlMs = 15 * 6e4;
3585
- this.fetchDataWithTimeout = async function(url, requestInit, timeoutMs) {
3584
+ this.fetchDataWithTimeout = async (url, requestInit, timeoutMs) => {
3586
3585
  if (typeof globalThis.fetch !== "function") {
3587
3586
  throw new Error("Browser REST controller requires global fetch.");
3588
3587
  }
3589
3588
  const signal = AbortSignal.timeout(timeoutMs);
3590
3589
  const response = await globalThis.fetch(url, { ...requestInit, signal });
3591
- return await response.json();
3590
+ const parsedResponse = await this.parseFetchResponse(response);
3591
+ return parsedResponse.data;
3592
3592
  };
3593
3593
  CadenzaService.createMetaTask(
3594
3594
  "Collect fetch transport diagnostics",
@@ -3934,6 +3934,38 @@ var RestController = class _RestController {
3934
3934
  return String(error);
3935
3935
  }
3936
3936
  }
3937
+ async parseFetchResponse(response) {
3938
+ const contentType = response.headers.get("content-type") ?? "";
3939
+ const rawText = await response.text();
3940
+ const headers = Object.fromEntries(response.headers.entries());
3941
+ if (rawText.length === 0) {
3942
+ return {
3943
+ ok: response.ok,
3944
+ status: response.status,
3945
+ statusText: response.statusText,
3946
+ headers,
3947
+ data: {}
3948
+ };
3949
+ }
3950
+ if (!contentType.toLowerCase().includes("application/json")) {
3951
+ throw new Error(
3952
+ `Expected JSON response from ${response.url ?? "remote service"} but received ${contentType || "unknown content type"} (HTTP ${response.status}). Body preview: ${rawText.slice(0, 200)}`
3953
+ );
3954
+ }
3955
+ try {
3956
+ return {
3957
+ ok: response.ok,
3958
+ status: response.status,
3959
+ statusText: response.statusText,
3960
+ headers,
3961
+ data: JSON.parse(rawText)
3962
+ };
3963
+ } catch (error) {
3964
+ throw new Error(
3965
+ `Failed to parse JSON response from ${response.url ?? "remote service"} (HTTP ${response.status}). Body preview: ${rawText.slice(0, 200)}. Parse error: ${this.getErrorMessage(error)}`
3966
+ );
3967
+ }
3968
+ }
3937
3969
  recordFetchClientError(fetchId, serviceName, url, error) {
3938
3970
  const state = this.ensureFetchClientDiagnostics(fetchId, serviceName, url);
3939
3971
  const message = this.getErrorMessage(error);