@kinotic-ai/core 5.0.0-beta.4 → 5.0.0-beta.6
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/README.md +2 -2
- package/dist/index.cjs +260 -175
- package/dist/index.d.cts +39 -9
- package/dist/index.d.ts +39 -9
- package/dist/index.js +254 -169
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51,6 +51,7 @@ import { RxStomp } from "@stomp/rx-stomp";
|
|
|
51
51
|
import { ReconnectionTimeMode } from "@stomp/stompjs";
|
|
52
52
|
import debug from "debug";
|
|
53
53
|
import { Subject } from "rxjs";
|
|
54
|
+
import { skip } from "rxjs/operators";
|
|
54
55
|
import { v4 as uuidv4 } from "uuid";
|
|
55
56
|
var SESSION_CHECK_PATH = "/api/auth/me";
|
|
56
57
|
|
|
@@ -58,15 +59,20 @@ class StompConnectionManager {
|
|
|
58
59
|
lastWebsocketError = null;
|
|
59
60
|
maxConnectionAttemptsReached = false;
|
|
60
61
|
replyToCriChangedHandler = null;
|
|
61
|
-
rxStomp =
|
|
62
|
+
rxStomp = new RxStomp;
|
|
63
|
+
isActive = false;
|
|
64
|
+
initialConnectedSubscription = null;
|
|
65
|
+
initialFailureSubscription = null;
|
|
66
|
+
webSocketErrorsSubscription = null;
|
|
62
67
|
INITIAL_RECONNECT_DELAY = 2000;
|
|
63
68
|
JITTER_MAX = 5000;
|
|
64
69
|
MAX_RECONNECT_DELAY = 120000;
|
|
65
70
|
connectionAttempts = 0;
|
|
66
|
-
debugLogger = debug("
|
|
71
|
+
debugLogger = debug("kinotic:stomp");
|
|
67
72
|
fatalErrorsSubject = new Subject;
|
|
68
73
|
_fatalErrors = this.fatalErrorsSubject.asObservable();
|
|
69
74
|
initialConnectionSuccessful = false;
|
|
75
|
+
rxStompHasConnected = false;
|
|
70
76
|
serverHeadersSubscription = null;
|
|
71
77
|
stompErrorsSubscription = null;
|
|
72
78
|
uuidv4 = uuidv4();
|
|
@@ -75,10 +81,10 @@ class StompConnectionManager {
|
|
|
75
81
|
return this._replyToCri;
|
|
76
82
|
}
|
|
77
83
|
get active() {
|
|
78
|
-
return
|
|
84
|
+
return this.isActive;
|
|
79
85
|
}
|
|
80
86
|
get connected() {
|
|
81
|
-
return this.
|
|
87
|
+
return this.isActive && this.rxStomp.connected();
|
|
82
88
|
}
|
|
83
89
|
get fatalErrors() {
|
|
84
90
|
return this._fatalErrors;
|
|
@@ -87,7 +93,7 @@ class StompConnectionManager {
|
|
|
87
93
|
if (!options?.server?.host) {
|
|
88
94
|
throw new Error("No host provided");
|
|
89
95
|
}
|
|
90
|
-
if (this.
|
|
96
|
+
if (this.isActive) {
|
|
91
97
|
throw new Error("Stomp connection already active");
|
|
92
98
|
}
|
|
93
99
|
const server = options.server;
|
|
@@ -109,7 +115,6 @@ class StompConnectionManager {
|
|
|
109
115
|
this.lastWebsocketError = null;
|
|
110
116
|
this.maxConnectionAttemptsReached = false;
|
|
111
117
|
const url = buildBrokerUrl(server);
|
|
112
|
-
this.rxStomp = new RxStomp;
|
|
113
118
|
let preparedSocket = null;
|
|
114
119
|
const userWebSocketFactory = options.webSocketFactory;
|
|
115
120
|
const usesPreparedSocket = userWebSocketFactory != null || headerAuth;
|
|
@@ -174,7 +179,7 @@ class StompConnectionManager {
|
|
|
174
179
|
};
|
|
175
180
|
}
|
|
176
181
|
this.rxStomp.configure(stompConfig);
|
|
177
|
-
this.rxStomp.webSocketErrors$.subscribe(async (value) => {
|
|
182
|
+
this.webSocketErrorsSubscription = this.rxStomp.webSocketErrors$.subscribe(async (value) => {
|
|
178
183
|
this.lastWebsocketError = value;
|
|
179
184
|
if (options?.maxConnectionAttempts && this.connectionAttempts >= options.maxConnectionAttempts) {
|
|
180
185
|
this.maxConnectionAttemptsReached = true;
|
|
@@ -185,19 +190,20 @@ class StompConnectionManager {
|
|
|
185
190
|
const stompError = new Error(frame.headers["message"], { cause: frame });
|
|
186
191
|
await this.signalFatal(new Error("STOMP connection error", { cause: stompError }));
|
|
187
192
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
initialFailureSubscription
|
|
193
|
+
this.initialConnectedSubscription = this.rxStomp.connected$.subscribe(() => {
|
|
194
|
+
this.initialConnectedSubscription?.unsubscribe();
|
|
195
|
+
this.initialFailureSubscription?.unsubscribe();
|
|
191
196
|
if (!this.initialConnectionSuccessful) {
|
|
192
197
|
this.initialConnectionSuccessful = true;
|
|
193
198
|
}
|
|
194
199
|
});
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
initialFailureSubscription
|
|
200
|
+
this.initialFailureSubscription = this.fatalErrorsSubject.subscribe((err) => {
|
|
201
|
+
this.initialConnectedSubscription?.unsubscribe();
|
|
202
|
+
this.initialFailureSubscription?.unsubscribe();
|
|
198
203
|
reject(err.message);
|
|
199
204
|
});
|
|
200
|
-
this.serverHeadersSubscription = this.rxStomp.serverHeaders$.subscribe(async (value) => {
|
|
205
|
+
this.serverHeadersSubscription = this.rxStomp.serverHeaders$.pipe(skip(this.rxStompHasConnected ? 1 : 0)).subscribe(async (value) => {
|
|
206
|
+
this.rxStompHasConnected = true;
|
|
201
207
|
const connectedInfoJson = value["connected-info" /* CONNECTED_INFO_HEADER */];
|
|
202
208
|
if (connectedInfoJson == null) {
|
|
203
209
|
if (!this.initialConnectionSuccessful) {
|
|
@@ -214,7 +220,7 @@ class StompConnectionManager {
|
|
|
214
220
|
}
|
|
215
221
|
return;
|
|
216
222
|
}
|
|
217
|
-
const newReplyToCri = "reply://" /* REPLY_DESTINATION_PREFIX */ + connectedInfo.replyToId + ":" + this.uuidv4 + "@
|
|
223
|
+
const newReplyToCri = "reply://" /* REPLY_DESTINATION_PREFIX */ + connectedInfo.replyToId + ":" + this.uuidv4 + "@kinotic.js.EventBus/replyHandler";
|
|
218
224
|
if (!this.initialConnectionSuccessful) {
|
|
219
225
|
this._replyToCri = newReplyToCri;
|
|
220
226
|
resolve(connectedInfo);
|
|
@@ -223,21 +229,23 @@ class StompConnectionManager {
|
|
|
223
229
|
this.replyToCriChangedHandler?.(newReplyToCri);
|
|
224
230
|
}
|
|
225
231
|
});
|
|
232
|
+
this.isActive = true;
|
|
226
233
|
this.rxStomp.activate();
|
|
227
234
|
});
|
|
228
235
|
}
|
|
229
236
|
async deactivate(force) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
await rxStomp.deactivate({ force });
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
237
|
+
if (this.isActive) {
|
|
238
|
+
this.isActive = false;
|
|
239
|
+
await this.rxStomp.deactivate({ force });
|
|
240
|
+
this.initialConnectedSubscription?.unsubscribe();
|
|
241
|
+
this.initialConnectedSubscription = null;
|
|
242
|
+
this.webSocketErrorsSubscription?.unsubscribe();
|
|
243
|
+
this.webSocketErrorsSubscription = null;
|
|
244
|
+
this.serverHeadersSubscription?.unsubscribe();
|
|
245
|
+
this.serverHeadersSubscription = null;
|
|
246
|
+
this.stompErrorsSubscription?.unsubscribe();
|
|
247
|
+
this.stompErrorsSubscription = null;
|
|
248
|
+
this._replyToCri = null;
|
|
241
249
|
}
|
|
242
250
|
return;
|
|
243
251
|
}
|
|
@@ -349,7 +357,7 @@ class EventBus {
|
|
|
349
357
|
return result;
|
|
350
358
|
}
|
|
351
359
|
send(event) {
|
|
352
|
-
if (this.stompConnectionManager.
|
|
360
|
+
if (this.stompConnectionManager.active) {
|
|
353
361
|
const headers = {};
|
|
354
362
|
for (const [key, value] of event.headers.entries()) {
|
|
355
363
|
headers[key] = value;
|
|
@@ -375,7 +383,7 @@ class EventBus {
|
|
|
375
383
|
return firstValueFrom(this.requestStream(event, false));
|
|
376
384
|
}
|
|
377
385
|
requestStream(event, sendControlEvents = true) {
|
|
378
|
-
if (this.stompConnectionManager
|
|
386
|
+
if (this.stompConnectionManager.active) {
|
|
379
387
|
return new Observable2((subscriber) => {
|
|
380
388
|
if (this.requestRepliesObservable == null) {
|
|
381
389
|
this.requestRepliesSubject = new Subject2;
|
|
@@ -476,22 +484,18 @@ class EventBus {
|
|
|
476
484
|
return new Error(ret);
|
|
477
485
|
}
|
|
478
486
|
_observe(cri) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
headers.set(prop, message.headers[prop]);
|
|
488
|
-
}
|
|
487
|
+
return this.stompConnectionManager.rxStomp.watch(cri).pipe(map((message) => {
|
|
488
|
+
const headers = new Map;
|
|
489
|
+
let destination = "";
|
|
490
|
+
for (const prop of Object.keys(message.headers)) {
|
|
491
|
+
if (prop === "destination") {
|
|
492
|
+
destination = message.headers[prop];
|
|
493
|
+
} else {
|
|
494
|
+
headers.set(prop, message.headers[prop]);
|
|
489
495
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
}
|
|
493
|
-
throw this.createSendUnavailableError();
|
|
494
|
-
}
|
|
496
|
+
}
|
|
497
|
+
return new Event(destination, headers, message.binaryBody);
|
|
498
|
+
}));
|
|
495
499
|
}
|
|
496
500
|
}
|
|
497
501
|
|
|
@@ -569,7 +573,7 @@ class SessionCredentialsResolver {
|
|
|
569
573
|
|
|
570
574
|
// packages/core/src/internal/api/Util.ts
|
|
571
575
|
var DEFAULT_PORT = 58503;
|
|
572
|
-
var DEFAULT_HOST = "api.kinotic.
|
|
576
|
+
var DEFAULT_HOST = "api.kinotic.ai";
|
|
573
577
|
|
|
574
578
|
class Util {
|
|
575
579
|
static createReplyEvent(incomingHeaders, headers, body) {
|
|
@@ -874,6 +878,9 @@ class ServiceIdentifier {
|
|
|
874
878
|
}
|
|
875
879
|
return this._cri;
|
|
876
880
|
}
|
|
881
|
+
unscopedCri() {
|
|
882
|
+
return createCRI("srv" /* SERVICE_DESTINATION_SCHEME */, null, this.qualifiedName(), null, this.version || null);
|
|
883
|
+
}
|
|
877
884
|
}
|
|
878
885
|
|
|
879
886
|
// packages/core/src/api/ZoneUtil.ts
|
|
@@ -893,6 +900,7 @@ function validateLabel(label) {
|
|
|
893
900
|
|
|
894
901
|
// packages/core/src/api/KinoticDecorators.ts
|
|
895
902
|
var scopeFunctions = new WeakSet;
|
|
903
|
+
var scopeOptionalFunctions = new WeakSet;
|
|
896
904
|
var versionRegistry = new WeakMap;
|
|
897
905
|
var zonesRegistry = new WeakMap;
|
|
898
906
|
var advertisedRegistry = new WeakMap;
|
|
@@ -930,6 +938,23 @@ function resolveScope(instance) {
|
|
|
930
938
|
function Scope(value, _context) {
|
|
931
939
|
scopeFunctions.add(value);
|
|
932
940
|
}
|
|
941
|
+
function ScopeOptional(value, _context) {
|
|
942
|
+
scopeOptionalFunctions.add(value);
|
|
943
|
+
}
|
|
944
|
+
function scopeOptionalMethodNames(instance) {
|
|
945
|
+
const ret = new Set;
|
|
946
|
+
let proto = Object.getPrototypeOf(instance);
|
|
947
|
+
while (proto && proto !== Object.prototype) {
|
|
948
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
949
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
|
|
950
|
+
if (typeof descriptor?.value === "function" && scopeOptionalFunctions.has(descriptor.value)) {
|
|
951
|
+
ret.add(key);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
proto = Object.getPrototypeOf(proto);
|
|
955
|
+
}
|
|
956
|
+
return ret;
|
|
957
|
+
}
|
|
933
958
|
function Version(version) {
|
|
934
959
|
if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9]+)?$/.test(version)) {
|
|
935
960
|
throw new Error(`Invalid semantic version: ${version}. Must follow X.Y.Z[-optional] format.`);
|
|
@@ -992,6 +1017,93 @@ function Publish(namespace, name, advertise = false) {
|
|
|
992
1017
|
};
|
|
993
1018
|
}
|
|
994
1019
|
|
|
1020
|
+
// packages/core/src/internal/api/ServiceInvocationSupervisor.ts
|
|
1021
|
+
import opentelemetry, { context as context2, propagation as propagation2, trace, SpanKind, SpanStatusCode } from "@opentelemetry/api";
|
|
1022
|
+
// packages/core/package.json
|
|
1023
|
+
var package_default = {
|
|
1024
|
+
name: "@kinotic-ai/core",
|
|
1025
|
+
version: "5.0.0-beta.6",
|
|
1026
|
+
type: "module",
|
|
1027
|
+
files: [
|
|
1028
|
+
"dist"
|
|
1029
|
+
],
|
|
1030
|
+
module: "./dist/index.js",
|
|
1031
|
+
main: "./dist/index.cjs",
|
|
1032
|
+
types: "./dist/index.d.ts",
|
|
1033
|
+
exports: {
|
|
1034
|
+
".": {
|
|
1035
|
+
import: {
|
|
1036
|
+
types: "./dist/index.d.ts",
|
|
1037
|
+
default: "./dist/index.js"
|
|
1038
|
+
},
|
|
1039
|
+
require: {
|
|
1040
|
+
types: "./dist/index.d.cts",
|
|
1041
|
+
default: "./dist/index.cjs"
|
|
1042
|
+
}
|
|
1043
|
+
},
|
|
1044
|
+
"./node": {
|
|
1045
|
+
import: {
|
|
1046
|
+
types: "./dist/node/index.d.ts",
|
|
1047
|
+
default: "./dist/node/index.js"
|
|
1048
|
+
},
|
|
1049
|
+
require: {
|
|
1050
|
+
types: "./dist/node/index.d.cts",
|
|
1051
|
+
default: "./dist/node/index.cjs"
|
|
1052
|
+
}
|
|
1053
|
+
},
|
|
1054
|
+
"./package.json": "./package.json"
|
|
1055
|
+
},
|
|
1056
|
+
homepage: "https://github.com/kinotic-ai/kinotic",
|
|
1057
|
+
repository: {
|
|
1058
|
+
type: "git",
|
|
1059
|
+
url: "git+https://github.com/kinotic-ai/kinotic.git",
|
|
1060
|
+
directory: "kinotic-js/workspace/packages/core"
|
|
1061
|
+
},
|
|
1062
|
+
license: "Elastic License 2.0",
|
|
1063
|
+
publishConfig: {
|
|
1064
|
+
access: "public"
|
|
1065
|
+
},
|
|
1066
|
+
scripts: {
|
|
1067
|
+
"type-check": "tsc --noEmit",
|
|
1068
|
+
test: "vitest run",
|
|
1069
|
+
coverage: "vitest run --coverage",
|
|
1070
|
+
"ui-test": "vitest --ui --coverage.enabled=true --mode development"
|
|
1071
|
+
},
|
|
1072
|
+
devDependencies: {
|
|
1073
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.221.0",
|
|
1074
|
+
"@opentelemetry/resources": "^2.10.0",
|
|
1075
|
+
"@opentelemetry/sdk-metrics": "^2.10.0",
|
|
1076
|
+
"@opentelemetry/sdk-node": "^0.221.0",
|
|
1077
|
+
"@opentelemetry/sdk-trace-node": "^2.10.0",
|
|
1078
|
+
"@types/debug": "^4.1.13",
|
|
1079
|
+
"@types/node": "^25.6.0",
|
|
1080
|
+
"@types/ws": "^8.18.0",
|
|
1081
|
+
"@vitest/coverage-v8": "^4.1.4",
|
|
1082
|
+
"@vitest/runner": "^4.1.4",
|
|
1083
|
+
"@vitest/ui": "^4.1.4",
|
|
1084
|
+
allure: "^3.14.3",
|
|
1085
|
+
"allure-vitest": "^3.10.2",
|
|
1086
|
+
dotenv: "^17.4.1",
|
|
1087
|
+
"properties-file": "^4.0.0",
|
|
1088
|
+
testcontainers: "^11.14.0",
|
|
1089
|
+
tslib: "^2.8.1",
|
|
1090
|
+
typescript: "^6.0.2",
|
|
1091
|
+
vitest: "^4.1.4"
|
|
1092
|
+
},
|
|
1093
|
+
dependencies: {
|
|
1094
|
+
"@opentelemetry/api": "^1.9.1",
|
|
1095
|
+
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
1096
|
+
"@stomp/rx-stomp": "^2.4.0",
|
|
1097
|
+
"@stomp/stompjs": "^7.3.0",
|
|
1098
|
+
debug: "^4.4.3",
|
|
1099
|
+
"p-tap": "^4.0.0",
|
|
1100
|
+
rxjs: "^7.8.2",
|
|
1101
|
+
"typescript-optional": "3.0.0-alpha.3",
|
|
1102
|
+
uuid: "^13.0.0",
|
|
1103
|
+
ws: "^8.21.0"
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
|
|
995
1107
|
// packages/core/src/internal/api/ServiceInvocationSupervisor.ts
|
|
996
1108
|
class ServiceInvocationSupervisor {
|
|
997
1109
|
log;
|
|
@@ -1003,7 +1115,10 @@ class ServiceInvocationSupervisor {
|
|
|
1003
1115
|
serviceIdentifier;
|
|
1004
1116
|
serviceInstance;
|
|
1005
1117
|
methodSubscription = null;
|
|
1118
|
+
unscopedSubscription = null;
|
|
1006
1119
|
methodMap;
|
|
1120
|
+
scopeOptionalMethods;
|
|
1121
|
+
tracer;
|
|
1007
1122
|
constructor(serviceIdentifier, serviceInstance, eventBusService, interceptorProvider, options = {}) {
|
|
1008
1123
|
if (!serviceIdentifier)
|
|
1009
1124
|
throw new Error("ServiceIdentifier must not be null");
|
|
@@ -1017,10 +1132,12 @@ class ServiceInvocationSupervisor {
|
|
|
1017
1132
|
this.serviceInstance = serviceInstance;
|
|
1018
1133
|
this._eventBus = eventBusService;
|
|
1019
1134
|
this.interceptorProvider = interceptorProvider;
|
|
1020
|
-
this.log = options.logger || createDebugLogger("
|
|
1135
|
+
this.log = options.logger || createDebugLogger("kinotic:ServiceInvocationSupervisor");
|
|
1021
1136
|
this.argumentResolver = options.argumentResolver || new JsonArgumentResolver;
|
|
1022
1137
|
this.returnValueConverter = options.returnValueConverter || new BasicReturnValueConverter;
|
|
1023
1138
|
this.methodMap = this.buildMethodMap(serviceInstance);
|
|
1139
|
+
this.scopeOptionalMethods = scopeOptionalMethodNames(serviceInstance);
|
|
1140
|
+
this.tracer = opentelemetry.trace.getTracer(package_default.name, package_default.version);
|
|
1024
1141
|
}
|
|
1025
1142
|
isActive() {
|
|
1026
1143
|
return this.active;
|
|
@@ -1040,7 +1157,16 @@ class ServiceInvocationSupervisor {
|
|
|
1040
1157
|
}
|
|
1041
1158
|
this.active = true;
|
|
1042
1159
|
const criBase = this.serviceIdentifier.cri().baseResource();
|
|
1043
|
-
this.methodSubscription = this.
|
|
1160
|
+
this.methodSubscription = this.subscribeAt(criBase);
|
|
1161
|
+
this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
|
|
1162
|
+
if (this.serviceIdentifier.scope && this.scopeOptionalMethods.size > 0) {
|
|
1163
|
+
const unscopedBase = this.serviceIdentifier.unscopedCri().baseResource();
|
|
1164
|
+
this.unscopedSubscription = this.subscribeAt(unscopedBase);
|
|
1165
|
+
this.log.info(`ServiceInvocationSupervisor also listening for ScopeOptional methods at ${unscopedBase}`);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
subscribeAt(criBase) {
|
|
1169
|
+
return this._eventBus.observe(criBase).subscribe({
|
|
1044
1170
|
next: async (event) => {
|
|
1045
1171
|
await this.processEvent(event);
|
|
1046
1172
|
},
|
|
@@ -1053,7 +1179,6 @@ class ServiceInvocationSupervisor {
|
|
|
1053
1179
|
this.active = false;
|
|
1054
1180
|
}
|
|
1055
1181
|
});
|
|
1056
|
-
this.log.info(`ServiceInvocationSupervisor started for ${criBase}`);
|
|
1057
1182
|
}
|
|
1058
1183
|
stop() {
|
|
1059
1184
|
if (!this.active) {
|
|
@@ -1062,6 +1187,8 @@ class ServiceInvocationSupervisor {
|
|
|
1062
1187
|
this.active = false;
|
|
1063
1188
|
this.methodSubscription?.unsubscribe();
|
|
1064
1189
|
this.methodSubscription = null;
|
|
1190
|
+
this.unscopedSubscription?.unsubscribe();
|
|
1191
|
+
this.unscopedSubscription = null;
|
|
1065
1192
|
this.log.info("ServiceInvocationSupervisor stopped");
|
|
1066
1193
|
}
|
|
1067
1194
|
buildMethodMap(serviceInstance) {
|
|
@@ -1104,7 +1231,8 @@ class ServiceInvocationSupervisor {
|
|
|
1104
1231
|
this.log.trace(`Processing control event for correlationId: ${correlationId}`);
|
|
1105
1232
|
}
|
|
1106
1233
|
async processInvocationRequest(event) {
|
|
1107
|
-
const
|
|
1234
|
+
const incomingCri = createCRI(event.cri);
|
|
1235
|
+
const path = incomingCri.path();
|
|
1108
1236
|
if (!path) {
|
|
1109
1237
|
throw new Error("The methodId must not be blank");
|
|
1110
1238
|
}
|
|
@@ -1112,38 +1240,76 @@ class ServiceInvocationSupervisor {
|
|
|
1112
1240
|
if (!handlerMethod) {
|
|
1113
1241
|
throw new Error(`No method resolved for methodId ${path}`);
|
|
1114
1242
|
}
|
|
1243
|
+
if (this.serviceIdentifier.scope && !incomingCri.hasScope() && !this.scopeOptionalMethods.has(path)) {
|
|
1244
|
+
throw new Error(`Method ${path} of ${this.serviceIdentifier.qualifiedName()} requires a scoped invocation naming the service instance`);
|
|
1245
|
+
}
|
|
1115
1246
|
const methodName = path;
|
|
1116
|
-
const
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1247
|
+
const span = this.startInvocationSpan(event, methodName);
|
|
1248
|
+
try {
|
|
1249
|
+
const args = this.argumentResolver.resolveArguments(event);
|
|
1250
|
+
const injectContext = receivesContext(this.serviceInstance, methodName);
|
|
1251
|
+
let serviceContext = {};
|
|
1252
|
+
const interceptor = this.interceptorProvider();
|
|
1253
|
+
if (interceptor) {
|
|
1254
|
+
try {
|
|
1255
|
+
serviceContext = await interceptor.intercept(event, serviceContext);
|
|
1256
|
+
} catch (e) {
|
|
1257
|
+
this.log.error(`Interceptor failed to create context for event: ${JSON.stringify(event)}`, e);
|
|
1258
|
+
this.handleException(event, new Error("Internal server error"));
|
|
1259
|
+
this.failSpan(span, e);
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
if (injectContext) {
|
|
1264
|
+
args.push(serviceContext);
|
|
1265
|
+
}
|
|
1266
|
+
const expectedArgsCount = handlerMethod.length;
|
|
1267
|
+
if (args.length !== expectedArgsCount) {
|
|
1268
|
+
throw new Error(`Argument count mismatch for method ${path}: expected ${expectedArgsCount}, got ${args.length}`);
|
|
1269
|
+
}
|
|
1270
|
+
let result;
|
|
1121
1271
|
try {
|
|
1122
|
-
|
|
1272
|
+
result = context2.with(trace.setSpan(context2.active(), span), () => handlerMethod(...args));
|
|
1273
|
+
if (result instanceof Promise) {
|
|
1274
|
+
result.then((resolved) => {
|
|
1275
|
+
this.processMethodInvocationResult(event, resolved);
|
|
1276
|
+
span.end();
|
|
1277
|
+
}, (error) => {
|
|
1278
|
+
this.handleException(event, error);
|
|
1279
|
+
this.failSpan(span, error);
|
|
1280
|
+
});
|
|
1281
|
+
} else {
|
|
1282
|
+
this.processMethodInvocationResult(event, result);
|
|
1283
|
+
span.end();
|
|
1284
|
+
}
|
|
1123
1285
|
} catch (e) {
|
|
1124
|
-
this.
|
|
1125
|
-
this.
|
|
1126
|
-
return;
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
if (injectContext) {
|
|
1130
|
-
args.push(context2);
|
|
1131
|
-
}
|
|
1132
|
-
const expectedArgsCount = handlerMethod.length;
|
|
1133
|
-
if (args.length !== expectedArgsCount) {
|
|
1134
|
-
throw new Error(`Argument count mismatch for method ${path}: expected ${expectedArgsCount}, got ${args.length}`);
|
|
1135
|
-
}
|
|
1136
|
-
let result;
|
|
1137
|
-
try {
|
|
1138
|
-
result = handlerMethod(...args);
|
|
1139
|
-
if (result instanceof Promise) {
|
|
1140
|
-
result.then((resolved) => this.processMethodInvocationResult(event, resolved), (error) => this.handleException(event, error));
|
|
1141
|
-
} else {
|
|
1142
|
-
this.processMethodInvocationResult(event, result);
|
|
1286
|
+
this.handleException(event, e);
|
|
1287
|
+
this.failSpan(span, e);
|
|
1143
1288
|
}
|
|
1144
1289
|
} catch (e) {
|
|
1145
|
-
this.
|
|
1146
|
-
|
|
1290
|
+
this.failSpan(span, e);
|
|
1291
|
+
throw e;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
startInvocationSpan(event, methodName) {
|
|
1295
|
+
const carrier = {};
|
|
1296
|
+
for (const [key, value] of event.headers.entries()) {
|
|
1297
|
+
carrier[key] = value;
|
|
1298
|
+
}
|
|
1299
|
+
const serviceName = this.serviceIdentifier.qualifiedName();
|
|
1300
|
+
return this.tracer.startSpan(`${this.serviceIdentifier.name}/${methodName}`, {
|
|
1301
|
+
kind: SpanKind.SERVER,
|
|
1302
|
+
attributes: {
|
|
1303
|
+
"rpc.system": "kinotic",
|
|
1304
|
+
"rpc.service": serviceName,
|
|
1305
|
+
"rpc.method": methodName
|
|
1306
|
+
}
|
|
1307
|
+
}, propagation2.extract(context2.active(), carrier));
|
|
1308
|
+
}
|
|
1309
|
+
failSpan(span, error) {
|
|
1310
|
+
span.recordException(error);
|
|
1311
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
1312
|
+
span.end();
|
|
1147
1313
|
}
|
|
1148
1314
|
processMethodInvocationResult(event, result) {
|
|
1149
1315
|
const outgoingEvent = this.returnValueConverter.convert(event.headers, result);
|
|
@@ -1175,98 +1341,12 @@ class ServiceInvocationSupervisor {
|
|
|
1175
1341
|
}
|
|
1176
1342
|
|
|
1177
1343
|
// packages/core/src/api/ServiceRegistry.ts
|
|
1178
|
-
import
|
|
1344
|
+
import opentelemetry2, { SpanKind as SpanKind2, SpanStatusCode as SpanStatusCode2 } from "@opentelemetry/api";
|
|
1179
1345
|
import {
|
|
1180
1346
|
ATTR_SERVER_ADDRESS,
|
|
1181
1347
|
ATTR_SERVER_PORT
|
|
1182
1348
|
} from "@opentelemetry/semantic-conventions";
|
|
1183
1349
|
import { first, map as map2 } from "rxjs/operators";
|
|
1184
|
-
// packages/core/package.json
|
|
1185
|
-
var package_default = {
|
|
1186
|
-
name: "@kinotic-ai/core",
|
|
1187
|
-
version: "5.0.0-beta.4",
|
|
1188
|
-
type: "module",
|
|
1189
|
-
files: [
|
|
1190
|
-
"dist"
|
|
1191
|
-
],
|
|
1192
|
-
module: "./dist/index.js",
|
|
1193
|
-
main: "./dist/index.cjs",
|
|
1194
|
-
types: "./dist/index.d.ts",
|
|
1195
|
-
exports: {
|
|
1196
|
-
".": {
|
|
1197
|
-
import: {
|
|
1198
|
-
types: "./dist/index.d.ts",
|
|
1199
|
-
default: "./dist/index.js"
|
|
1200
|
-
},
|
|
1201
|
-
require: {
|
|
1202
|
-
types: "./dist/index.d.cts",
|
|
1203
|
-
default: "./dist/index.cjs"
|
|
1204
|
-
}
|
|
1205
|
-
},
|
|
1206
|
-
"./node": {
|
|
1207
|
-
import: {
|
|
1208
|
-
types: "./dist/node/index.d.ts",
|
|
1209
|
-
default: "./dist/node/index.js"
|
|
1210
|
-
},
|
|
1211
|
-
require: {
|
|
1212
|
-
types: "./dist/node/index.d.cts",
|
|
1213
|
-
default: "./dist/node/index.cjs"
|
|
1214
|
-
}
|
|
1215
|
-
},
|
|
1216
|
-
"./package.json": "./package.json"
|
|
1217
|
-
},
|
|
1218
|
-
homepage: "https://github.com/kinotic-ai/kinotic",
|
|
1219
|
-
repository: {
|
|
1220
|
-
type: "git",
|
|
1221
|
-
url: "git+https://github.com/kinotic-ai/kinotic.git",
|
|
1222
|
-
directory: "kinotic-js/workspace/packages/core"
|
|
1223
|
-
},
|
|
1224
|
-
license: "Elastic License 2.0",
|
|
1225
|
-
publishConfig: {
|
|
1226
|
-
access: "public"
|
|
1227
|
-
},
|
|
1228
|
-
scripts: {
|
|
1229
|
-
"type-check": "tsc --noEmit",
|
|
1230
|
-
test: "vitest run",
|
|
1231
|
-
coverage: "vitest run --coverage",
|
|
1232
|
-
"ui-test": "vitest --ui --coverage.enabled=true --mode development"
|
|
1233
|
-
},
|
|
1234
|
-
devDependencies: {
|
|
1235
|
-
"@opentelemetry/exporter-trace-otlp-grpc": "^0.221.0",
|
|
1236
|
-
"@opentelemetry/resources": "^2.10.0",
|
|
1237
|
-
"@opentelemetry/sdk-metrics": "^2.10.0",
|
|
1238
|
-
"@opentelemetry/sdk-node": "^0.221.0",
|
|
1239
|
-
"@opentelemetry/sdk-trace-node": "^2.10.0",
|
|
1240
|
-
"@types/debug": "^4.1.13",
|
|
1241
|
-
"@types/node": "^25.6.0",
|
|
1242
|
-
"@types/ws": "^8.18.0",
|
|
1243
|
-
"@vitest/coverage-v8": "^4.1.4",
|
|
1244
|
-
"@vitest/runner": "^4.1.4",
|
|
1245
|
-
"@vitest/ui": "^4.1.4",
|
|
1246
|
-
allure: "^3.14.3",
|
|
1247
|
-
"allure-vitest": "^3.10.2",
|
|
1248
|
-
dotenv: "^17.4.1",
|
|
1249
|
-
"properties-file": "^4.0.0",
|
|
1250
|
-
testcontainers: "^11.14.0",
|
|
1251
|
-
tslib: "^2.8.1",
|
|
1252
|
-
typescript: "^6.0.2",
|
|
1253
|
-
vitest: "^4.1.4"
|
|
1254
|
-
},
|
|
1255
|
-
dependencies: {
|
|
1256
|
-
"@opentelemetry/api": "^1.9.1",
|
|
1257
|
-
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
1258
|
-
"@stomp/rx-stomp": "^2.4.0",
|
|
1259
|
-
"@stomp/stompjs": "^7.3.0",
|
|
1260
|
-
debug: "^4.4.3",
|
|
1261
|
-
"p-tap": "^4.0.0",
|
|
1262
|
-
rxjs: "^7.8.2",
|
|
1263
|
-
"typescript-optional": "3.0.0-alpha.3",
|
|
1264
|
-
uuid: "^13.0.0",
|
|
1265
|
-
ws: "^8.21.0"
|
|
1266
|
-
}
|
|
1267
|
-
};
|
|
1268
|
-
|
|
1269
|
-
// packages/core/src/api/ServiceRegistry.ts
|
|
1270
1350
|
class JsonEventFactory {
|
|
1271
1351
|
create(cri, args) {
|
|
1272
1352
|
const event = new Event(cri);
|
|
@@ -1305,7 +1385,7 @@ class ServiceRegistry {
|
|
|
1305
1385
|
_eventBus;
|
|
1306
1386
|
supervisors = new Map;
|
|
1307
1387
|
contextInterceptor = null;
|
|
1308
|
-
debugLogger = debug3("
|
|
1388
|
+
debugLogger = debug3("kinotic:serviceRegistry");
|
|
1309
1389
|
constructor(eventBus) {
|
|
1310
1390
|
this._eventBus = eventBus;
|
|
1311
1391
|
}
|
|
@@ -1355,16 +1435,16 @@ class ServiceProxy {
|
|
|
1355
1435
|
}
|
|
1356
1436
|
this.serviceIdentifier = serviceIdentifier;
|
|
1357
1437
|
this.serviceRegistry = serviceRegistry;
|
|
1358
|
-
this.tracer =
|
|
1438
|
+
this.tracer = opentelemetry2.trace.getTracer(package_default.name, package_default.version);
|
|
1359
1439
|
}
|
|
1360
1440
|
invoke(methodIdentifier, args, scope, eventFactory) {
|
|
1361
1441
|
return this.tracer.startActiveSpan(`${this.serviceIdentifier}/${methodIdentifier}`, {
|
|
1362
|
-
kind:
|
|
1442
|
+
kind: SpanKind2.CLIENT
|
|
1363
1443
|
}, async (span) => {
|
|
1364
1444
|
if (scope) {
|
|
1365
|
-
span.setAttribute("
|
|
1445
|
+
span.setAttribute("kinotic.scope", scope);
|
|
1366
1446
|
}
|
|
1367
|
-
span.setAttribute("rpc.system", "
|
|
1447
|
+
span.setAttribute("rpc.system", "kinotic");
|
|
1368
1448
|
span.setAttribute("rpc.service", this.serviceIdentifier);
|
|
1369
1449
|
span.setAttribute("rpc.method", methodIdentifier);
|
|
1370
1450
|
return this.__invokeStream(false, methodIdentifier, args, scope, eventFactory).pipe(first()).toPromise().then(async (value) => {
|
|
@@ -1372,7 +1452,7 @@ class ServiceProxy {
|
|
|
1372
1452
|
return value;
|
|
1373
1453
|
}, async (ex) => {
|
|
1374
1454
|
span.recordException(ex);
|
|
1375
|
-
span.setStatus({ code:
|
|
1455
|
+
span.setStatus({ code: SpanStatusCode2.ERROR });
|
|
1376
1456
|
span.end();
|
|
1377
1457
|
throw ex;
|
|
1378
1458
|
});
|
|
@@ -1388,7 +1468,7 @@ class ServiceProxy {
|
|
|
1388
1468
|
eventFactoryToUse = eventFactory;
|
|
1389
1469
|
}
|
|
1390
1470
|
let eventBusToUse = this.serviceRegistry.eventBus;
|
|
1391
|
-
const span =
|
|
1471
|
+
const span = opentelemetry2.trace.getActiveSpan();
|
|
1392
1472
|
if (span) {
|
|
1393
1473
|
span.setAttribute(ATTR_SERVER_ADDRESS, eventBusToUse.serverInfo?.host || "unknown");
|
|
1394
1474
|
span.setAttribute(ATTR_SERVER_PORT, eventBusToUse.serverInfo?.port || "unknown");
|
|
@@ -1552,6 +1632,9 @@ class CrudServiceProxy {
|
|
|
1552
1632
|
deleteById(id) {
|
|
1553
1633
|
return this.serviceProxy.invoke("deleteById", [id]);
|
|
1554
1634
|
}
|
|
1635
|
+
deleteByIdSync(id) {
|
|
1636
|
+
return this.serviceProxy.invoke("deleteByIdSync", [id]);
|
|
1637
|
+
}
|
|
1555
1638
|
async findAll(pageable) {
|
|
1556
1639
|
const page = await this.findAllSinglePage(pageable);
|
|
1557
1640
|
return new FindAllIterablePage(pageable, page, this);
|
|
@@ -1690,6 +1773,7 @@ class ParticipantConstants {
|
|
|
1690
1773
|
export {
|
|
1691
1774
|
validateZone,
|
|
1692
1775
|
validateLabel,
|
|
1776
|
+
scopeOptionalMethodNames,
|
|
1693
1777
|
receivesContext,
|
|
1694
1778
|
isAdvertised,
|
|
1695
1779
|
createCRI,
|
|
@@ -1703,6 +1787,7 @@ export {
|
|
|
1703
1787
|
SessionCredentialsResolver,
|
|
1704
1788
|
ServiceRegistry,
|
|
1705
1789
|
ServerInfo,
|
|
1790
|
+
ScopeOptional,
|
|
1706
1791
|
Scope,
|
|
1707
1792
|
Publish,
|
|
1708
1793
|
ParticipantConstants,
|