@kinotic-ai/core 5.0.0-beta.5 → 5.0.0-beta.7

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
@@ -66,6 +66,7 @@ var exports_src = {};
66
66
  __export(exports_src, {
67
67
  validateZone: () => validateZone,
68
68
  validateLabel: () => validateLabel,
69
+ scopeOptionalMethodNames: () => scopeOptionalMethodNames,
69
70
  receivesContext: () => receivesContext,
70
71
  isAdvertised: () => isAdvertised,
71
72
  createCRI: () => createCRI,
@@ -79,6 +80,7 @@ __export(exports_src, {
79
80
  SessionCredentialsResolver: () => SessionCredentialsResolver,
80
81
  ServiceRegistry: () => ServiceRegistry,
81
82
  ServerInfo: () => ServerInfo,
83
+ ScopeOptional: () => ScopeOptional,
82
84
  Scope: () => Scope,
83
85
  Publish: () => Publish,
84
86
  ParticipantConstants: () => ParticipantConstants,
@@ -162,6 +164,7 @@ var import_rx_stomp = require("@stomp/rx-stomp");
162
164
  var import_stompjs = require("@stomp/stompjs");
163
165
  var import_debug = __toESM(require("debug"));
164
166
  var import_rxjs = require("rxjs");
167
+ var import_operators = require("rxjs/operators");
165
168
  var import_uuid = require("uuid");
166
169
  var SESSION_CHECK_PATH = "/api/auth/me";
167
170
 
@@ -169,15 +172,20 @@ class StompConnectionManager {
169
172
  lastWebsocketError = null;
170
173
  maxConnectionAttemptsReached = false;
171
174
  replyToCriChangedHandler = null;
172
- rxStomp = null;
175
+ rxStomp = new import_rx_stomp.RxStomp;
176
+ isActive = false;
177
+ initialConnectedSubscription = null;
178
+ initialFailureSubscription = null;
179
+ webSocketErrorsSubscription = null;
173
180
  INITIAL_RECONNECT_DELAY = 2000;
174
181
  JITTER_MAX = 5000;
175
182
  MAX_RECONNECT_DELAY = 120000;
176
183
  connectionAttempts = 0;
177
- debugLogger = import_debug.default("kinoitc:stomp");
184
+ debugLogger = import_debug.default("kinotic:stomp");
178
185
  fatalErrorsSubject = new import_rxjs.Subject;
179
186
  _fatalErrors = this.fatalErrorsSubject.asObservable();
180
187
  initialConnectionSuccessful = false;
188
+ rxStompHasConnected = false;
181
189
  serverHeadersSubscription = null;
182
190
  stompErrorsSubscription = null;
183
191
  uuidv4 = import_uuid.v4();
@@ -186,10 +194,10 @@ class StompConnectionManager {
186
194
  return this._replyToCri;
187
195
  }
188
196
  get active() {
189
- return !!this.rxStomp;
197
+ return this.isActive;
190
198
  }
191
199
  get connected() {
192
- return this.rxStomp != null && this.rxStomp.connected();
200
+ return this.isActive && this.rxStomp.connected();
193
201
  }
194
202
  get fatalErrors() {
195
203
  return this._fatalErrors;
@@ -198,7 +206,7 @@ class StompConnectionManager {
198
206
  if (!options?.server?.host) {
199
207
  throw new Error("No host provided");
200
208
  }
201
- if (this.rxStomp) {
209
+ if (this.isActive) {
202
210
  throw new Error("Stomp connection already active");
203
211
  }
204
212
  const server = options.server;
@@ -220,7 +228,6 @@ class StompConnectionManager {
220
228
  this.lastWebsocketError = null;
221
229
  this.maxConnectionAttemptsReached = false;
222
230
  const url = buildBrokerUrl(server);
223
- this.rxStomp = new import_rx_stomp.RxStomp;
224
231
  let preparedSocket = null;
225
232
  const userWebSocketFactory = options.webSocketFactory;
226
233
  const usesPreparedSocket = userWebSocketFactory != null || headerAuth;
@@ -285,7 +292,7 @@ class StompConnectionManager {
285
292
  };
286
293
  }
287
294
  this.rxStomp.configure(stompConfig);
288
- this.rxStomp.webSocketErrors$.subscribe(async (value) => {
295
+ this.webSocketErrorsSubscription = this.rxStomp.webSocketErrors$.subscribe(async (value) => {
289
296
  this.lastWebsocketError = value;
290
297
  if (options?.maxConnectionAttempts && this.connectionAttempts >= options.maxConnectionAttempts) {
291
298
  this.maxConnectionAttemptsReached = true;
@@ -296,19 +303,20 @@ class StompConnectionManager {
296
303
  const stompError = new Error(frame.headers["message"], { cause: frame });
297
304
  await this.signalFatal(new Error("STOMP connection error", { cause: stompError }));
298
305
  });
299
- const connectedSubscription = this.rxStomp.connected$.subscribe(() => {
300
- connectedSubscription.unsubscribe();
301
- initialFailureSubscription.unsubscribe();
306
+ this.initialConnectedSubscription = this.rxStomp.connected$.subscribe(() => {
307
+ this.initialConnectedSubscription?.unsubscribe();
308
+ this.initialFailureSubscription?.unsubscribe();
302
309
  if (!this.initialConnectionSuccessful) {
303
310
  this.initialConnectionSuccessful = true;
304
311
  }
305
312
  });
306
- const initialFailureSubscription = this.fatalErrorsSubject.subscribe((err) => {
307
- connectedSubscription.unsubscribe();
308
- initialFailureSubscription.unsubscribe();
313
+ this.initialFailureSubscription = this.fatalErrorsSubject.subscribe((err) => {
314
+ this.initialConnectedSubscription?.unsubscribe();
315
+ this.initialFailureSubscription?.unsubscribe();
309
316
  reject(err.message);
310
317
  });
311
- this.serverHeadersSubscription = this.rxStomp.serverHeaders$.subscribe(async (value) => {
318
+ this.serverHeadersSubscription = this.rxStomp.serverHeaders$.pipe(import_operators.skip(this.rxStompHasConnected ? 1 : 0)).subscribe(async (value) => {
319
+ this.rxStompHasConnected = true;
312
320
  const connectedInfoJson = value["connected-info" /* CONNECTED_INFO_HEADER */];
313
321
  if (connectedInfoJson == null) {
314
322
  if (!this.initialConnectionSuccessful) {
@@ -325,7 +333,7 @@ class StompConnectionManager {
325
333
  }
326
334
  return;
327
335
  }
328
- const newReplyToCri = "reply://" /* REPLY_DESTINATION_PREFIX */ + connectedInfo.replyToId + ":" + this.uuidv4 + "@kinoitc.js.EventBus/replyHandler";
336
+ const newReplyToCri = "reply://" /* REPLY_DESTINATION_PREFIX */ + connectedInfo.replyToId + ":" + this.uuidv4 + "@kinotic.js.EventBus/replyHandler";
329
337
  if (!this.initialConnectionSuccessful) {
330
338
  this._replyToCri = newReplyToCri;
331
339
  resolve(connectedInfo);
@@ -334,21 +342,23 @@ class StompConnectionManager {
334
342
  this.replyToCriChangedHandler?.(newReplyToCri);
335
343
  }
336
344
  });
345
+ this.isActive = true;
337
346
  this.rxStomp.activate();
338
347
  });
339
348
  }
340
349
  async deactivate(force) {
341
- const rxStomp = this.rxStomp;
342
- if (rxStomp) {
343
- await rxStomp.deactivate({ force });
344
- if (this.rxStomp === rxStomp) {
345
- this.serverHeadersSubscription?.unsubscribe();
346
- this.serverHeadersSubscription = null;
347
- this.stompErrorsSubscription?.unsubscribe();
348
- this.stompErrorsSubscription = null;
349
- this.rxStomp = null;
350
- this._replyToCri = null;
351
- }
350
+ if (this.isActive) {
351
+ this.isActive = false;
352
+ await this.rxStomp.deactivate({ force });
353
+ this.initialConnectedSubscription?.unsubscribe();
354
+ this.initialConnectedSubscription = null;
355
+ this.webSocketErrorsSubscription?.unsubscribe();
356
+ this.webSocketErrorsSubscription = null;
357
+ this.serverHeadersSubscription?.unsubscribe();
358
+ this.serverHeadersSubscription = null;
359
+ this.stompErrorsSubscription?.unsubscribe();
360
+ this.stompErrorsSubscription = null;
361
+ this._replyToCri = null;
352
362
  }
353
363
  return;
354
364
  }
@@ -369,7 +379,7 @@ class StompConnectionManager {
369
379
  // packages/core/src/api/event/EventBus.ts
370
380
  var import_api = require("@opentelemetry/api");
371
381
  var import_rxjs2 = require("rxjs");
372
- var import_operators = require("rxjs/operators");
382
+ var import_operators2 = require("rxjs/operators");
373
383
  var import_typescript_optional = require("typescript-optional");
374
384
  var import_uuid2 = require("uuid");
375
385
  class Event {
@@ -460,7 +470,7 @@ class EventBus {
460
470
  return result;
461
471
  }
462
472
  send(event) {
463
- if (this.stompConnectionManager.rxStomp) {
473
+ if (this.stompConnectionManager.active) {
464
474
  const headers = {};
465
475
  for (const [key, value] of event.headers.entries()) {
466
476
  headers[key] = value;
@@ -486,17 +496,17 @@ class EventBus {
486
496
  return import_rxjs2.firstValueFrom(this.requestStream(event, false));
487
497
  }
488
498
  requestStream(event, sendControlEvents = true) {
489
- if (this.stompConnectionManager?.rxStomp) {
499
+ if (this.stompConnectionManager.active) {
490
500
  return new import_rxjs2.Observable((subscriber) => {
491
501
  if (this.requestRepliesObservable == null) {
492
502
  this.requestRepliesSubject = new import_rxjs2.Subject;
493
- this.requestRepliesObservable = this._observe(this.replyToCri).pipe(import_operators.tap((value) => this.cancelIfUnexpected(value)), import_operators.multicast(this.requestRepliesSubject));
503
+ this.requestRepliesObservable = this._observe(this.replyToCri).pipe(import_operators2.tap((value) => this.cancelIfUnexpected(value)), import_operators2.multicast(this.requestRepliesSubject));
494
504
  this.requestRepliesSubscription = this.requestRepliesObservable.connect();
495
505
  }
496
506
  let serverSignaledCompletion = false;
497
507
  const correlationId = import_uuid2.v4();
498
508
  this.activeCorrelationIds.add(correlationId);
499
- const defaultMessagesSubscription = this.requestRepliesObservable.pipe(import_operators.filter((value) => {
509
+ const defaultMessagesSubscription = this.requestRepliesObservable.pipe(import_operators2.filter((value) => {
500
510
  return value.headers.get("__correlation-id" /* CORRELATION_ID_HEADER */) === correlationId;
501
511
  })).subscribe({
502
512
  next(value) {
@@ -587,22 +597,18 @@ class EventBus {
587
597
  return new Error(ret);
588
598
  }
589
599
  _observe(cri) {
590
- if (this.stompConnectionManager?.rxStomp) {
591
- return this.stompConnectionManager.rxStomp.watch(cri).pipe(import_operators.map((message) => {
592
- const headers = new Map;
593
- let destination = "";
594
- for (const prop of Object.keys(message.headers)) {
595
- if (prop === "destination") {
596
- destination = message.headers[prop];
597
- } else {
598
- headers.set(prop, message.headers[prop]);
599
- }
600
+ return this.stompConnectionManager.rxStomp.watch(cri).pipe(import_operators2.map((message) => {
601
+ const headers = new Map;
602
+ let destination = "";
603
+ for (const prop of Object.keys(message.headers)) {
604
+ if (prop === "destination") {
605
+ destination = message.headers[prop];
606
+ } else {
607
+ headers.set(prop, message.headers[prop]);
600
608
  }
601
- return new Event(destination, headers, message.binaryBody);
602
- }));
603
- } else {
604
- throw this.createSendUnavailableError();
605
- }
609
+ }
610
+ return new Event(destination, headers, message.binaryBody);
611
+ }));
606
612
  }
607
613
  }
608
614
 
@@ -680,7 +686,7 @@ class SessionCredentialsResolver {
680
686
 
681
687
  // packages/core/src/internal/api/Util.ts
682
688
  var DEFAULT_PORT = 58503;
683
- var DEFAULT_HOST = "api.kinotic.com";
689
+ var DEFAULT_HOST = "api.kinotic.ai";
684
690
 
685
691
  class Util {
686
692
  static createReplyEvent(incomingHeaders, headers, body) {
@@ -985,6 +991,9 @@ class ServiceIdentifier {
985
991
  }
986
992
  return this._cri;
987
993
  }
994
+ unscopedCri() {
995
+ return createCRI("srv" /* SERVICE_DESTINATION_SCHEME */, null, this.qualifiedName(), null, this.version || null);
996
+ }
988
997
  }
989
998
 
990
999
  // packages/core/src/api/ZoneUtil.ts
@@ -1004,6 +1013,7 @@ function validateLabel(label) {
1004
1013
 
1005
1014
  // packages/core/src/api/KinoticDecorators.ts
1006
1015
  var scopeFunctions = new WeakSet;
1016
+ var scopeOptionalFunctions = new WeakSet;
1007
1017
  var versionRegistry = new WeakMap;
1008
1018
  var zonesRegistry = new WeakMap;
1009
1019
  var advertisedRegistry = new WeakMap;
@@ -1041,6 +1051,23 @@ function resolveScope(instance) {
1041
1051
  function Scope(value, _context) {
1042
1052
  scopeFunctions.add(value);
1043
1053
  }
1054
+ function ScopeOptional(value, _context) {
1055
+ scopeOptionalFunctions.add(value);
1056
+ }
1057
+ function scopeOptionalMethodNames(instance) {
1058
+ const ret = new Set;
1059
+ let proto = Object.getPrototypeOf(instance);
1060
+ while (proto && proto !== Object.prototype) {
1061
+ for (const key of Object.getOwnPropertyNames(proto)) {
1062
+ const descriptor = Object.getOwnPropertyDescriptor(proto, key);
1063
+ if (typeof descriptor?.value === "function" && scopeOptionalFunctions.has(descriptor.value)) {
1064
+ ret.add(key);
1065
+ }
1066
+ }
1067
+ proto = Object.getPrototypeOf(proto);
1068
+ }
1069
+ return ret;
1070
+ }
1044
1071
  function Version(version) {
1045
1072
  if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9]+)?$/.test(version)) {
1046
1073
  throw new Error(`Invalid semantic version: ${version}. Must follow X.Y.Z[-optional] format.`);
@@ -1103,6 +1130,93 @@ function Publish(namespace, name, advertise = false) {
1103
1130
  };
1104
1131
  }
1105
1132
 
1133
+ // packages/core/src/internal/api/ServiceInvocationSupervisor.ts
1134
+ var import_api2 = __toESM(require("@opentelemetry/api"));
1135
+ // packages/core/package.json
1136
+ var package_default = {
1137
+ name: "@kinotic-ai/core",
1138
+ version: "5.0.0-beta.7",
1139
+ type: "module",
1140
+ files: [
1141
+ "dist"
1142
+ ],
1143
+ module: "./dist/index.js",
1144
+ main: "./dist/index.cjs",
1145
+ types: "./dist/index.d.ts",
1146
+ exports: {
1147
+ ".": {
1148
+ import: {
1149
+ types: "./dist/index.d.ts",
1150
+ default: "./dist/index.js"
1151
+ },
1152
+ require: {
1153
+ types: "./dist/index.d.cts",
1154
+ default: "./dist/index.cjs"
1155
+ }
1156
+ },
1157
+ "./node": {
1158
+ import: {
1159
+ types: "./dist/node/index.d.ts",
1160
+ default: "./dist/node/index.js"
1161
+ },
1162
+ require: {
1163
+ types: "./dist/node/index.d.cts",
1164
+ default: "./dist/node/index.cjs"
1165
+ }
1166
+ },
1167
+ "./package.json": "./package.json"
1168
+ },
1169
+ homepage: "https://github.com/kinotic-ai/kinotic",
1170
+ repository: {
1171
+ type: "git",
1172
+ url: "git+https://github.com/kinotic-ai/kinotic.git",
1173
+ directory: "kinotic-js/workspace/packages/core"
1174
+ },
1175
+ license: "Elastic License 2.0",
1176
+ publishConfig: {
1177
+ access: "public"
1178
+ },
1179
+ scripts: {
1180
+ "type-check": "tsc --noEmit",
1181
+ test: "vitest run",
1182
+ coverage: "vitest run --coverage",
1183
+ "ui-test": "vitest --ui --coverage.enabled=true --mode development"
1184
+ },
1185
+ devDependencies: {
1186
+ "@opentelemetry/exporter-trace-otlp-grpc": "^0.221.0",
1187
+ "@opentelemetry/resources": "^2.10.0",
1188
+ "@opentelemetry/sdk-metrics": "^2.10.0",
1189
+ "@opentelemetry/sdk-node": "^0.221.0",
1190
+ "@opentelemetry/sdk-trace-node": "^2.10.0",
1191
+ "@types/debug": "^4.1.13",
1192
+ "@types/node": "^25.6.0",
1193
+ "@types/ws": "^8.18.0",
1194
+ "@vitest/coverage-v8": "^4.1.4",
1195
+ "@vitest/runner": "^4.1.4",
1196
+ "@vitest/ui": "^4.1.4",
1197
+ allure: "^3.14.3",
1198
+ "allure-vitest": "^3.10.2",
1199
+ dotenv: "^17.4.1",
1200
+ "properties-file": "^4.0.0",
1201
+ testcontainers: "^11.14.0",
1202
+ tslib: "^2.8.1",
1203
+ typescript: "^6.0.2",
1204
+ vitest: "^4.1.4"
1205
+ },
1206
+ dependencies: {
1207
+ "@opentelemetry/api": "^1.9.1",
1208
+ "@opentelemetry/semantic-conventions": "^1.40.0",
1209
+ "@stomp/rx-stomp": "^2.4.0",
1210
+ "@stomp/stompjs": "^7.3.0",
1211
+ debug: "^4.4.3",
1212
+ "p-tap": "^4.0.0",
1213
+ rxjs: "^7.8.2",
1214
+ "typescript-optional": "3.0.0-alpha.3",
1215
+ uuid: "^13.0.0",
1216
+ ws: "^8.21.0"
1217
+ }
1218
+ };
1219
+
1106
1220
  // packages/core/src/internal/api/ServiceInvocationSupervisor.ts
1107
1221
  class ServiceInvocationSupervisor {
1108
1222
  log;
@@ -1114,7 +1228,10 @@ class ServiceInvocationSupervisor {
1114
1228
  serviceIdentifier;
1115
1229
  serviceInstance;
1116
1230
  methodSubscription = null;
1231
+ unscopedSubscription = null;
1117
1232
  methodMap;
1233
+ scopeOptionalMethods;
1234
+ tracer;
1118
1235
  constructor(serviceIdentifier, serviceInstance, eventBusService, interceptorProvider, options = {}) {
1119
1236
  if (!serviceIdentifier)
1120
1237
  throw new Error("ServiceIdentifier must not be null");
@@ -1128,10 +1245,12 @@ class ServiceInvocationSupervisor {
1128
1245
  this.serviceInstance = serviceInstance;
1129
1246
  this._eventBus = eventBusService;
1130
1247
  this.interceptorProvider = interceptorProvider;
1131
- this.log = options.logger || createDebugLogger("kinoitc:ServiceInvocationSupervisor");
1248
+ this.log = options.logger || createDebugLogger("kinotic:ServiceInvocationSupervisor");
1132
1249
  this.argumentResolver = options.argumentResolver || new JsonArgumentResolver;
1133
1250
  this.returnValueConverter = options.returnValueConverter || new BasicReturnValueConverter;
1134
1251
  this.methodMap = this.buildMethodMap(serviceInstance);
1252
+ this.scopeOptionalMethods = scopeOptionalMethodNames(serviceInstance);
1253
+ this.tracer = import_api2.default.trace.getTracer(package_default.name, package_default.version);
1135
1254
  }
1136
1255
  isActive() {
1137
1256
  return this.active;
@@ -1151,7 +1270,16 @@ class ServiceInvocationSupervisor {
1151
1270
  }
1152
1271
  this.active = true;
1153
1272
  const criBase = this.serviceIdentifier.cri().baseResource();
1154
- this.methodSubscription = this._eventBus.observe(criBase).subscribe({
1273
+ this.methodSubscription = this.subscribeAt(criBase);
1274
+ this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
1275
+ if (this.serviceIdentifier.scope && this.scopeOptionalMethods.size > 0) {
1276
+ const unscopedBase = this.serviceIdentifier.unscopedCri().baseResource();
1277
+ this.unscopedSubscription = this.subscribeAt(unscopedBase);
1278
+ this.log.info(`ServiceInvocationSupervisor also listening for ScopeOptional methods at ${unscopedBase}`);
1279
+ }
1280
+ }
1281
+ subscribeAt(criBase) {
1282
+ return this._eventBus.observe(criBase).subscribe({
1155
1283
  next: async (event) => {
1156
1284
  await this.processEvent(event);
1157
1285
  },
@@ -1164,7 +1292,6 @@ class ServiceInvocationSupervisor {
1164
1292
  this.active = false;
1165
1293
  }
1166
1294
  });
1167
- this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
1168
1295
  }
1169
1296
  stop() {
1170
1297
  if (!this.active) {
@@ -1173,6 +1300,8 @@ class ServiceInvocationSupervisor {
1173
1300
  this.active = false;
1174
1301
  this.methodSubscription?.unsubscribe();
1175
1302
  this.methodSubscription = null;
1303
+ this.unscopedSubscription?.unsubscribe();
1304
+ this.unscopedSubscription = null;
1176
1305
  this.log.info("ServiceInvocationSupervisor stopped");
1177
1306
  }
1178
1307
  buildMethodMap(serviceInstance) {
@@ -1215,7 +1344,8 @@ class ServiceInvocationSupervisor {
1215
1344
  this.log.trace(`Processing control event for correlationId: ${correlationId}`);
1216
1345
  }
1217
1346
  async processInvocationRequest(event) {
1218
- const path = createCRI(event.cri).path();
1347
+ const incomingCri = createCRI(event.cri);
1348
+ const path = incomingCri.path();
1219
1349
  if (!path) {
1220
1350
  throw new Error("The methodId must not be blank");
1221
1351
  }
@@ -1223,38 +1353,76 @@ class ServiceInvocationSupervisor {
1223
1353
  if (!handlerMethod) {
1224
1354
  throw new Error(`No method resolved for methodId ${path}`);
1225
1355
  }
1356
+ if (this.serviceIdentifier.scope && !incomingCri.hasScope() && !this.scopeOptionalMethods.has(path)) {
1357
+ throw new Error(`Method ${path} of ${this.serviceIdentifier.qualifiedName()} requires a scoped invocation naming the service instance`);
1358
+ }
1226
1359
  const methodName = path;
1227
- const args = this.argumentResolver.resolveArguments(event);
1228
- const injectContext = receivesContext(this.serviceInstance, methodName);
1229
- let context2 = {};
1230
- const interceptor = this.interceptorProvider();
1231
- if (interceptor) {
1360
+ const span = this.startInvocationSpan(event, methodName);
1361
+ try {
1362
+ const args = this.argumentResolver.resolveArguments(event);
1363
+ const injectContext = receivesContext(this.serviceInstance, methodName);
1364
+ let serviceContext = {};
1365
+ const interceptor = this.interceptorProvider();
1366
+ if (interceptor) {
1367
+ try {
1368
+ serviceContext = await interceptor.intercept(event, serviceContext);
1369
+ } catch (e) {
1370
+ this.log.error(`Interceptor failed to create context for event: ${JSON.stringify(event)}`, e);
1371
+ this.handleException(event, new Error("Internal server error"));
1372
+ this.failSpan(span, e);
1373
+ return;
1374
+ }
1375
+ }
1376
+ if (injectContext) {
1377
+ args.push(serviceContext);
1378
+ }
1379
+ const expectedArgsCount = handlerMethod.length;
1380
+ if (args.length !== expectedArgsCount) {
1381
+ throw new Error(`Argument count mismatch for method ${path}: expected ${expectedArgsCount}, got ${args.length}`);
1382
+ }
1383
+ let result;
1232
1384
  try {
1233
- context2 = await interceptor.intercept(event, context2);
1385
+ result = import_api2.context.with(import_api2.trace.setSpan(import_api2.context.active(), span), () => handlerMethod(...args));
1386
+ if (result instanceof Promise) {
1387
+ result.then((resolved) => {
1388
+ this.processMethodInvocationResult(event, resolved);
1389
+ span.end();
1390
+ }, (error) => {
1391
+ this.handleException(event, error);
1392
+ this.failSpan(span, error);
1393
+ });
1394
+ } else {
1395
+ this.processMethodInvocationResult(event, result);
1396
+ span.end();
1397
+ }
1234
1398
  } catch (e) {
1235
- this.log.error(`Interceptor failed to create context for event: ${JSON.stringify(event)}`, e);
1236
- this.handleException(event, new Error("Internal server error"));
1237
- return;
1238
- }
1239
- }
1240
- if (injectContext) {
1241
- args.push(context2);
1242
- }
1243
- const expectedArgsCount = handlerMethod.length;
1244
- if (args.length !== expectedArgsCount) {
1245
- throw new Error(`Argument count mismatch for method ${path}: expected ${expectedArgsCount}, got ${args.length}`);
1246
- }
1247
- let result;
1248
- try {
1249
- result = handlerMethod(...args);
1250
- if (result instanceof Promise) {
1251
- result.then((resolved) => this.processMethodInvocationResult(event, resolved), (error) => this.handleException(event, error));
1252
- } else {
1253
- this.processMethodInvocationResult(event, result);
1399
+ this.handleException(event, e);
1400
+ this.failSpan(span, e);
1254
1401
  }
1255
1402
  } catch (e) {
1256
- this.handleException(event, e);
1257
- }
1403
+ this.failSpan(span, e);
1404
+ throw e;
1405
+ }
1406
+ }
1407
+ startInvocationSpan(event, methodName) {
1408
+ const carrier = {};
1409
+ for (const [key, value] of event.headers.entries()) {
1410
+ carrier[key] = value;
1411
+ }
1412
+ const serviceName = this.serviceIdentifier.qualifiedName();
1413
+ return this.tracer.startSpan(`${this.serviceIdentifier.name}/${methodName}`, {
1414
+ kind: import_api2.SpanKind.SERVER,
1415
+ attributes: {
1416
+ "rpc.system": "kinotic",
1417
+ "rpc.service": serviceName,
1418
+ "rpc.method": methodName
1419
+ }
1420
+ }, import_api2.propagation.extract(import_api2.context.active(), carrier));
1421
+ }
1422
+ failSpan(span, error) {
1423
+ span.recordException(error);
1424
+ span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
1425
+ span.end();
1258
1426
  }
1259
1427
  processMethodInvocationResult(event, result) {
1260
1428
  const outgoingEvent = this.returnValueConverter.convert(event.headers, result);
@@ -1286,95 +1454,9 @@ class ServiceInvocationSupervisor {
1286
1454
  }
1287
1455
 
1288
1456
  // packages/core/src/api/ServiceRegistry.ts
1289
- var import_api2 = __toESM(require("@opentelemetry/api"));
1457
+ var import_api3 = __toESM(require("@opentelemetry/api"));
1290
1458
  var import_semantic_conventions = require("@opentelemetry/semantic-conventions");
1291
- var import_operators2 = require("rxjs/operators");
1292
- // packages/core/package.json
1293
- var package_default = {
1294
- name: "@kinotic-ai/core",
1295
- version: "5.0.0-beta.5",
1296
- type: "module",
1297
- files: [
1298
- "dist"
1299
- ],
1300
- module: "./dist/index.js",
1301
- main: "./dist/index.cjs",
1302
- types: "./dist/index.d.ts",
1303
- exports: {
1304
- ".": {
1305
- import: {
1306
- types: "./dist/index.d.ts",
1307
- default: "./dist/index.js"
1308
- },
1309
- require: {
1310
- types: "./dist/index.d.cts",
1311
- default: "./dist/index.cjs"
1312
- }
1313
- },
1314
- "./node": {
1315
- import: {
1316
- types: "./dist/node/index.d.ts",
1317
- default: "./dist/node/index.js"
1318
- },
1319
- require: {
1320
- types: "./dist/node/index.d.cts",
1321
- default: "./dist/node/index.cjs"
1322
- }
1323
- },
1324
- "./package.json": "./package.json"
1325
- },
1326
- homepage: "https://github.com/kinotic-ai/kinotic",
1327
- repository: {
1328
- type: "git",
1329
- url: "git+https://github.com/kinotic-ai/kinotic.git",
1330
- directory: "kinotic-js/workspace/packages/core"
1331
- },
1332
- license: "Elastic License 2.0",
1333
- publishConfig: {
1334
- access: "public"
1335
- },
1336
- scripts: {
1337
- "type-check": "tsc --noEmit",
1338
- test: "vitest run",
1339
- coverage: "vitest run --coverage",
1340
- "ui-test": "vitest --ui --coverage.enabled=true --mode development"
1341
- },
1342
- devDependencies: {
1343
- "@opentelemetry/exporter-trace-otlp-grpc": "^0.221.0",
1344
- "@opentelemetry/resources": "^2.10.0",
1345
- "@opentelemetry/sdk-metrics": "^2.10.0",
1346
- "@opentelemetry/sdk-node": "^0.221.0",
1347
- "@opentelemetry/sdk-trace-node": "^2.10.0",
1348
- "@types/debug": "^4.1.13",
1349
- "@types/node": "^25.6.0",
1350
- "@types/ws": "^8.18.0",
1351
- "@vitest/coverage-v8": "^4.1.4",
1352
- "@vitest/runner": "^4.1.4",
1353
- "@vitest/ui": "^4.1.4",
1354
- allure: "^3.14.3",
1355
- "allure-vitest": "^3.10.2",
1356
- dotenv: "^17.4.1",
1357
- "properties-file": "^4.0.0",
1358
- testcontainers: "^11.14.0",
1359
- tslib: "^2.8.1",
1360
- typescript: "^6.0.2",
1361
- vitest: "^4.1.4"
1362
- },
1363
- dependencies: {
1364
- "@opentelemetry/api": "^1.9.1",
1365
- "@opentelemetry/semantic-conventions": "^1.40.0",
1366
- "@stomp/rx-stomp": "^2.4.0",
1367
- "@stomp/stompjs": "^7.3.0",
1368
- debug: "^4.4.3",
1369
- "p-tap": "^4.0.0",
1370
- rxjs: "^7.8.2",
1371
- "typescript-optional": "3.0.0-alpha.3",
1372
- uuid: "^13.0.0",
1373
- ws: "^8.21.0"
1374
- }
1375
- };
1376
-
1377
- // packages/core/src/api/ServiceRegistry.ts
1459
+ var import_operators3 = require("rxjs/operators");
1378
1460
  class JsonEventFactory {
1379
1461
  create(cri, args) {
1380
1462
  const event = new Event(cri);
@@ -1413,7 +1495,7 @@ class ServiceRegistry {
1413
1495
  _eventBus;
1414
1496
  supervisors = new Map;
1415
1497
  contextInterceptor = null;
1416
- debugLogger = import_debug3.default("kinoitc:serviceRegistry");
1498
+ debugLogger = import_debug3.default("kinotic:serviceRegistry");
1417
1499
  constructor(eventBus) {
1418
1500
  this._eventBus = eventBus;
1419
1501
  }
@@ -1463,24 +1545,24 @@ class ServiceProxy {
1463
1545
  }
1464
1546
  this.serviceIdentifier = serviceIdentifier;
1465
1547
  this.serviceRegistry = serviceRegistry;
1466
- this.tracer = import_api2.default.trace.getTracer("kinoitc.client", package_default.version);
1548
+ this.tracer = import_api3.default.trace.getTracer(package_default.name, package_default.version);
1467
1549
  }
1468
1550
  invoke(methodIdentifier, args, scope, eventFactory) {
1469
1551
  return this.tracer.startActiveSpan(`${this.serviceIdentifier}/${methodIdentifier}`, {
1470
- kind: import_api2.SpanKind.CLIENT
1552
+ kind: import_api3.SpanKind.CLIENT
1471
1553
  }, async (span) => {
1472
1554
  if (scope) {
1473
- span.setAttribute("kinoitc.scope", scope);
1555
+ span.setAttribute("kinotic.scope", scope);
1474
1556
  }
1475
- span.setAttribute("rpc.system", "kinoitc");
1557
+ span.setAttribute("rpc.system", "kinotic");
1476
1558
  span.setAttribute("rpc.service", this.serviceIdentifier);
1477
1559
  span.setAttribute("rpc.method", methodIdentifier);
1478
- return this.__invokeStream(false, methodIdentifier, args, scope, eventFactory).pipe(import_operators2.first()).toPromise().then(async (value) => {
1560
+ return this.__invokeStream(false, methodIdentifier, args, scope, eventFactory).pipe(import_operators3.first()).toPromise().then(async (value) => {
1479
1561
  span.end();
1480
1562
  return value;
1481
1563
  }, async (ex) => {
1482
1564
  span.recordException(ex);
1483
- span.setStatus({ code: import_api2.SpanStatusCode.ERROR });
1565
+ span.setStatus({ code: import_api3.SpanStatusCode.ERROR });
1484
1566
  span.end();
1485
1567
  throw ex;
1486
1568
  });
@@ -1496,13 +1578,13 @@ class ServiceProxy {
1496
1578
  eventFactoryToUse = eventFactory;
1497
1579
  }
1498
1580
  let eventBusToUse = this.serviceRegistry.eventBus;
1499
- const span = import_api2.default.trace.getActiveSpan();
1581
+ const span = import_api3.default.trace.getActiveSpan();
1500
1582
  if (span) {
1501
1583
  span.setAttribute(import_semantic_conventions.ATTR_SERVER_ADDRESS, eventBusToUse.serverInfo?.host || "unknown");
1502
1584
  span.setAttribute(import_semantic_conventions.ATTR_SERVER_PORT, eventBusToUse.serverInfo?.port || "unknown");
1503
1585
  }
1504
1586
  let event = eventFactoryToUse.create(cri, args);
1505
- return eventBusToUse.requestStream(event, sendControlEvents).pipe(import_operators2.map((value) => {
1587
+ return eventBusToUse.requestStream(event, sendControlEvents).pipe(import_operators3.map((value) => {
1506
1588
  const contentType = value.getHeader("content-type" /* CONTENT_TYPE_HEADER */);
1507
1589
  if (contentType !== undefined) {
1508
1590
  if (contentType === "application/json" /* CONTENT_JSON */) {