@kinotic-ai/core 1.8.0 → 1.9.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.cjs CHANGED
@@ -129,6 +129,7 @@ var EventConstants;
129
129
  EventConstants2["CONNECTED_INFO_HEADER"] = "connected-info";
130
130
  EventConstants2["SESSION_KEEP_ALIVE_HEADER"] = "session-keep-alive";
131
131
  EventConstants2["CORRELATION_ID_HEADER"] = "__correlation-id";
132
+ EventConstants2["ORIGIN_CRI_HEADER"] = "__origin-cri";
132
133
  EventConstants2["ERROR_HEADER"] = "error";
133
134
  EventConstants2["COMPLETE_HEADER"] = "complete";
134
135
  EventConstants2["CONTROL_HEADER"] = "control";
@@ -176,7 +177,7 @@ class Util {
176
177
  }
177
178
  }
178
179
 
179
- // packages/core/src/internal/api/event/StompConnectionManager.ts
180
+ // packages/core/src/internal/api/StompConnectionManager.ts
180
181
  var import_rx_stomp = require("@stomp/rx-stomp");
181
182
  var import_stompjs = require("@stomp/stompjs");
182
183
  var import_debug = __toESM(require("debug"));
@@ -411,6 +412,9 @@ class EventBus {
411
412
  requestRepliesObservable = null;
412
413
  requestRepliesSubject = null;
413
414
  requestRepliesSubscription = null;
415
+ activeCorrelationIds = new Set;
416
+ recentlyReaped = new Set;
417
+ static REAP_DEBOUNCE_MS = 5000;
414
418
  constructor() {
415
419
  this.stompConnectionManager.fatalErrors.subscribe(() => this.cleanup());
416
420
  this.stompConnectionManager.replyToCriChangedHandler = (replyToCri) => {
@@ -485,11 +489,12 @@ class EventBus {
485
489
  return new import_rxjs2.Observable((subscriber) => {
486
490
  if (this.requestRepliesObservable == null) {
487
491
  this.requestRepliesSubject = new import_rxjs2.Subject;
488
- this.requestRepliesObservable = this._observe(this.replyToCri).pipe(import_operators.multicast(this.requestRepliesSubject));
492
+ this.requestRepliesObservable = this._observe(this.replyToCri).pipe(import_operators.tap((value) => this.cancelIfUnexpected(value)), import_operators.multicast(this.requestRepliesSubject));
489
493
  this.requestRepliesSubscription = this.requestRepliesObservable.connect();
490
494
  }
491
495
  let serverSignaledCompletion = false;
492
496
  const correlationId = import_uuid2.v4();
497
+ this.activeCorrelationIds.add(correlationId);
493
498
  const defaultMessagesSubscription = this.requestRepliesObservable.pipe(import_operators.filter((value) => {
494
499
  return value.headers.get("__correlation-id" /* CORRELATION_ID_HEADER */) === correlationId;
495
500
  })).subscribe({
@@ -520,6 +525,7 @@ class EventBus {
520
525
  event.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
521
526
  this.send(event);
522
527
  return () => {
528
+ this.activeCorrelationIds.delete(correlationId);
523
529
  if (sendControlEvents && !serverSignaledCompletion) {
524
530
  const controlEvent = new Event(event.cri);
525
531
  controlEvent.setHeader("control" /* CONTROL_HEADER */, "cancel" /* CONTROL_VALUE_CANCEL */);
@@ -542,6 +548,24 @@ class EventBus {
542
548
  this.resetRequestReplies("Connection disconnected");
543
549
  this.serverInfo = null;
544
550
  }
551
+ cancelIfUnexpected(value) {
552
+ const correlationId = value.getHeader("__correlation-id" /* CORRELATION_ID_HEADER */);
553
+ if (correlationId === undefined || this.activeCorrelationIds.has(correlationId)) {
554
+ return;
555
+ }
556
+ const originCri = value.getHeader("__origin-cri" /* ORIGIN_CRI_HEADER */);
557
+ if (originCri === undefined || this.recentlyReaped.has(correlationId)) {
558
+ return;
559
+ }
560
+ try {
561
+ const cancelEvent = new Event(originCri);
562
+ cancelEvent.setHeader("control" /* CONTROL_HEADER */, "cancel" /* CONTROL_VALUE_CANCEL */);
563
+ cancelEvent.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
564
+ this.send(cancelEvent);
565
+ this.recentlyReaped.add(correlationId);
566
+ setTimeout(() => this.recentlyReaped.delete(correlationId), EventBus.REAP_DEBOUNCE_MS);
567
+ } catch {}
568
+ }
545
569
  resetRequestReplies(reason) {
546
570
  if (this.requestRepliesSubject != null) {
547
571
  this.requestRepliesSubject.error(new Error(reason));
@@ -551,6 +575,7 @@ class EventBus {
551
575
  }
552
576
  this.requestRepliesSubject = null;
553
577
  this.requestRepliesObservable = null;
578
+ this.recentlyReaped.clear();
554
579
  }
555
580
  }
556
581
  createSendUnavailableError() {
@@ -995,8 +1020,8 @@ class ServiceInvocationSupervisor {
995
1020
  this.log.warn("Reply-to header must not be blank");
996
1021
  return false;
997
1022
  }
998
- if (!replyTo.startsWith(`${"srv" /* SERVICE_DESTINATION_SCHEME */}:`)) {
999
- this.log.warn("Reply-to header must be a valid service destination");
1023
+ if (!replyTo.startsWith(`${"reply" /* REPLY_DESTINATION_SCHEME */}:`)) {
1024
+ this.log.warn("Reply-to header must be a valid reply destination");
1000
1025
  return false;
1001
1026
  }
1002
1027
  return true;
@@ -1010,7 +1035,7 @@ var import_operators2 = require("rxjs/operators");
1010
1035
  // packages/core/package.json
1011
1036
  var package_default = {
1012
1037
  name: "@kinotic-ai/core",
1013
- version: "1.8.0",
1038
+ version: "1.9.0",
1014
1039
  type: "module",
1015
1040
  files: [
1016
1041
  "dist"
package/dist/index.d.cts CHANGED
@@ -266,6 +266,11 @@ declare enum EventConstants {
266
266
  */
267
267
  CORRELATION_ID_HEADER = "__correlation-id",
268
268
  /**
269
+ * Origin service CRI the server sends on stream replies so the client can address a cancel back to
270
+ * it. The __ prefix persists it across messages.
271
+ */
272
+ ORIGIN_CRI_HEADER = "__origin-cri",
273
+ /**
269
274
  * Denotes that something caused an error. Will contain a brief message about the error
270
275
  */
271
276
  ERROR_HEADER = "error",
@@ -1067,6 +1072,9 @@ declare class EventBus implements IEventBus {
1067
1072
  private requestRepliesObservable;
1068
1073
  private requestRepliesSubject;
1069
1074
  private requestRepliesSubscription;
1075
+ private readonly activeCorrelationIds;
1076
+ private readonly recentlyReaped;
1077
+ private static readonly REAP_DEBOUNCE_MS;
1070
1078
  constructor();
1071
1079
  get fatalErrors(): Observable3<Error>;
1072
1080
  isConnectionActive(): boolean;
@@ -1082,6 +1090,12 @@ declare class EventBus implements IEventBus {
1082
1090
  observe(cri: string): Observable3<IEvent>;
1083
1091
  private cleanup;
1084
1092
  /**
1093
+ * Cancels a server stream whose reply arrived for a correlationId we no longer track. The server can't
1094
+ * detect this itself since all requests share one reply destination, so the cancel is sent to the
1095
+ * originating service, whose CRI the server includes on each reply.
1096
+ */
1097
+ private cancelIfUnexpected;
1098
+ /**
1085
1099
  * Tears down the shared request-replies stream so the next request rebuilds it against the
1086
1100
  * current {@link replyToCri}. Any in-flight requests are failed with the given reason since
1087
1101
  * their replies can no longer be delivered.
package/dist/index.d.ts CHANGED
@@ -266,6 +266,11 @@ declare enum EventConstants {
266
266
  */
267
267
  CORRELATION_ID_HEADER = "__correlation-id",
268
268
  /**
269
+ * Origin service CRI the server sends on stream replies so the client can address a cancel back to
270
+ * it. The __ prefix persists it across messages.
271
+ */
272
+ ORIGIN_CRI_HEADER = "__origin-cri",
273
+ /**
269
274
  * Denotes that something caused an error. Will contain a brief message about the error
270
275
  */
271
276
  ERROR_HEADER = "error",
@@ -1067,6 +1072,9 @@ declare class EventBus implements IEventBus {
1067
1072
  private requestRepliesObservable;
1068
1073
  private requestRepliesSubject;
1069
1074
  private requestRepliesSubscription;
1075
+ private readonly activeCorrelationIds;
1076
+ private readonly recentlyReaped;
1077
+ private static readonly REAP_DEBOUNCE_MS;
1070
1078
  constructor();
1071
1079
  get fatalErrors(): Observable3<Error>;
1072
1080
  isConnectionActive(): boolean;
@@ -1082,6 +1090,12 @@ declare class EventBus implements IEventBus {
1082
1090
  observe(cri: string): Observable3<IEvent>;
1083
1091
  private cleanup;
1084
1092
  /**
1093
+ * Cancels a server stream whose reply arrived for a correlationId we no longer track. The server can't
1094
+ * detect this itself since all requests share one reply destination, so the cancel is sent to the
1095
+ * originating service, whose CRI the server includes on each reply.
1096
+ */
1097
+ private cancelIfUnexpected;
1098
+ /**
1085
1099
  * Tears down the shared request-replies stream so the next request rebuilds it against the
1086
1100
  * current {@link replyToCri}. Any in-flight requests are failed with the given reason since
1087
1101
  * their replies can no longer be delivered.
package/dist/index.js CHANGED
@@ -28,6 +28,7 @@ var EventConstants;
28
28
  EventConstants2["CONNECTED_INFO_HEADER"] = "connected-info";
29
29
  EventConstants2["SESSION_KEEP_ALIVE_HEADER"] = "session-keep-alive";
30
30
  EventConstants2["CORRELATION_ID_HEADER"] = "__correlation-id";
31
+ EventConstants2["ORIGIN_CRI_HEADER"] = "__origin-cri";
31
32
  EventConstants2["ERROR_HEADER"] = "error";
32
33
  EventConstants2["COMPLETE_HEADER"] = "complete";
33
34
  EventConstants2["CONTROL_HEADER"] = "control";
@@ -75,7 +76,7 @@ class Util {
75
76
  }
76
77
  }
77
78
 
78
- // packages/core/src/internal/api/event/StompConnectionManager.ts
79
+ // packages/core/src/internal/api/StompConnectionManager.ts
79
80
  import { RxStomp } from "@stomp/rx-stomp";
80
81
  import { ReconnectionTimeMode } from "@stomp/stompjs";
81
82
  import debug from "debug";
@@ -263,7 +264,7 @@ class StompConnectionManager {
263
264
  // packages/core/src/api/event/EventBus.ts
264
265
  import { context, propagation } from "@opentelemetry/api";
265
266
  import { firstValueFrom, Observable as Observable2, Subject as Subject2, throwError } from "rxjs";
266
- import { filter, map, multicast } from "rxjs/operators";
267
+ import { filter, map, multicast, tap } from "rxjs/operators";
267
268
  import { Optional } from "typescript-optional";
268
269
  import { v4 as uuidv42 } from "uuid";
269
270
  class Event {
@@ -310,6 +311,9 @@ class EventBus {
310
311
  requestRepliesObservable = null;
311
312
  requestRepliesSubject = null;
312
313
  requestRepliesSubscription = null;
314
+ activeCorrelationIds = new Set;
315
+ recentlyReaped = new Set;
316
+ static REAP_DEBOUNCE_MS = 5000;
313
317
  constructor() {
314
318
  this.stompConnectionManager.fatalErrors.subscribe(() => this.cleanup());
315
319
  this.stompConnectionManager.replyToCriChangedHandler = (replyToCri) => {
@@ -384,11 +388,12 @@ class EventBus {
384
388
  return new Observable2((subscriber) => {
385
389
  if (this.requestRepliesObservable == null) {
386
390
  this.requestRepliesSubject = new Subject2;
387
- this.requestRepliesObservable = this._observe(this.replyToCri).pipe(multicast(this.requestRepliesSubject));
391
+ this.requestRepliesObservable = this._observe(this.replyToCri).pipe(tap((value) => this.cancelIfUnexpected(value)), multicast(this.requestRepliesSubject));
388
392
  this.requestRepliesSubscription = this.requestRepliesObservable.connect();
389
393
  }
390
394
  let serverSignaledCompletion = false;
391
395
  const correlationId = uuidv42();
396
+ this.activeCorrelationIds.add(correlationId);
392
397
  const defaultMessagesSubscription = this.requestRepliesObservable.pipe(filter((value) => {
393
398
  return value.headers.get("__correlation-id" /* CORRELATION_ID_HEADER */) === correlationId;
394
399
  })).subscribe({
@@ -419,6 +424,7 @@ class EventBus {
419
424
  event.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
420
425
  this.send(event);
421
426
  return () => {
427
+ this.activeCorrelationIds.delete(correlationId);
422
428
  if (sendControlEvents && !serverSignaledCompletion) {
423
429
  const controlEvent = new Event(event.cri);
424
430
  controlEvent.setHeader("control" /* CONTROL_HEADER */, "cancel" /* CONTROL_VALUE_CANCEL */);
@@ -441,6 +447,24 @@ class EventBus {
441
447
  this.resetRequestReplies("Connection disconnected");
442
448
  this.serverInfo = null;
443
449
  }
450
+ cancelIfUnexpected(value) {
451
+ const correlationId = value.getHeader("__correlation-id" /* CORRELATION_ID_HEADER */);
452
+ if (correlationId === undefined || this.activeCorrelationIds.has(correlationId)) {
453
+ return;
454
+ }
455
+ const originCri = value.getHeader("__origin-cri" /* ORIGIN_CRI_HEADER */);
456
+ if (originCri === undefined || this.recentlyReaped.has(correlationId)) {
457
+ return;
458
+ }
459
+ try {
460
+ const cancelEvent = new Event(originCri);
461
+ cancelEvent.setHeader("control" /* CONTROL_HEADER */, "cancel" /* CONTROL_VALUE_CANCEL */);
462
+ cancelEvent.setHeader("__correlation-id" /* CORRELATION_ID_HEADER */, correlationId);
463
+ this.send(cancelEvent);
464
+ this.recentlyReaped.add(correlationId);
465
+ setTimeout(() => this.recentlyReaped.delete(correlationId), EventBus.REAP_DEBOUNCE_MS);
466
+ } catch {}
467
+ }
444
468
  resetRequestReplies(reason) {
445
469
  if (this.requestRepliesSubject != null) {
446
470
  this.requestRepliesSubject.error(new Error(reason));
@@ -450,6 +474,7 @@ class EventBus {
450
474
  }
451
475
  this.requestRepliesSubject = null;
452
476
  this.requestRepliesObservable = null;
477
+ this.recentlyReaped.clear();
453
478
  }
454
479
  }
455
480
  createSendUnavailableError() {
@@ -894,8 +919,8 @@ class ServiceInvocationSupervisor {
894
919
  this.log.warn("Reply-to header must not be blank");
895
920
  return false;
896
921
  }
897
- if (!replyTo.startsWith(`${"srv" /* SERVICE_DESTINATION_SCHEME */}:`)) {
898
- this.log.warn("Reply-to header must be a valid service destination");
922
+ if (!replyTo.startsWith(`${"reply" /* REPLY_DESTINATION_SCHEME */}:`)) {
923
+ this.log.warn("Reply-to header must be a valid reply destination");
899
924
  return false;
900
925
  }
901
926
  return true;
@@ -912,7 +937,7 @@ import { first, map as map2 } from "rxjs/operators";
912
937
  // packages/core/package.json
913
938
  var package_default = {
914
939
  name: "@kinotic-ai/core",
915
- version: "1.8.0",
940
+ version: "1.9.0",
916
941
  type: "module",
917
942
  files: [
918
943
  "dist"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kinotic-ai/core",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"