@kinotic-ai/core 5.0.0-beta.0 → 5.0.0-beta.2

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
@@ -69,12 +69,12 @@ __export(exports_src, {
69
69
  receivesContext: () => receivesContext,
70
70
  isAdvertised: () => isAdvertised,
71
71
  createCRI: () => createCRI,
72
- createAuthenticatedWebSocketFactory: () => createAuthenticatedWebSocketFactory,
73
72
  Zone: () => Zone,
74
73
  Version: () => Version,
75
74
  TextEventFactory: () => TextEventFactory,
76
75
  Sort: () => Sort,
77
76
  SessionKeepAliveMode: () => SessionKeepAliveMode,
77
+ SessionCredentialsResolver: () => SessionCredentialsResolver,
78
78
  ServiceRegistry: () => ServiceRegistry,
79
79
  ServerInfo: () => ServerInfo,
80
80
  Scope: () => Scope,
@@ -91,6 +91,7 @@ __export(exports_src, {
91
91
  EventConstants: () => EventConstants,
92
92
  EventBus: () => EventBus,
93
93
  Event: () => Event,
94
+ EnvCredentialsResolver: () => EnvCredentialsResolver,
94
95
  Direction: () => Direction,
95
96
  DefaultCRI: () => DefaultCRI,
96
97
  DataSourceUtils: () => DataSourceUtils,
@@ -98,15 +99,15 @@ __export(exports_src, {
98
99
  CrudServiceProxyFactory: () => CrudServiceProxyFactory,
99
100
  CrudServiceProxy: () => CrudServiceProxy,
100
101
  Context: () => Context,
101
- ConnectionInfo: () => ConnectionInfo,
102
102
  ConnectedInfo: () => ConnectedInfo,
103
- BearerTokenAuthProvider: () => BearerTokenAuthProvider,
104
- BasicAuthProvider: () => BasicAuthProvider,
103
+ ChainedCredentialsResolver: () => ChainedCredentialsResolver,
104
+ BearerCredentialsResolver: () => BearerCredentialsResolver,
105
+ BasicCredentialsResolver: () => BasicCredentialsResolver,
105
106
  AbstractIterablePage: () => AbstractIterablePage
106
107
  });
107
108
  module.exports = __toCommonJS(exports_src);
108
109
 
109
- // packages/core/src/api/ConnectionInfo.ts
110
+ // packages/core/src/api/ConnectOptions.ts
110
111
  class ServerInfo {
111
112
  host;
112
113
  port;
@@ -118,12 +119,6 @@ var SessionKeepAliveMode;
118
119
  SessionKeepAliveMode2["ACTIVITY"] = "ACTIVITY";
119
120
  SessionKeepAliveMode2["CONNECTION"] = "CONNECTION";
120
121
  })(SessionKeepAliveMode ||= {});
121
-
122
- class ConnectionInfo extends ServerInfo {
123
- webSocketFactory;
124
- maxConnectionAttempts;
125
- sessionKeepAlive = "ACTIVITY" /* ACTIVITY */;
126
- }
127
122
  // packages/core/src/api/event/IEventBus.ts
128
123
  var EventConstants;
129
124
  ((EventConstants2) => {
@@ -154,34 +149,6 @@ var EventConstants;
154
149
  EventConstants2["TRACESTATE_HEADER"] = "tracestate";
155
150
  })(EventConstants ||= {});
156
151
 
157
- // packages/core/src/internal/api/Util.ts
158
- class Util {
159
- static createReplyEvent(incomingHeaders, headers, body) {
160
- if (!incomingHeaders) {
161
- throw new Error("incomingHeaders cannot be null");
162
- }
163
- const replyCRI = incomingHeaders.get("reply-to" /* REPLY_TO_HEADER */);
164
- if (!replyCRI || replyCRI.trim() === "") {
165
- throw new Error("No reply-to header found, cannot create outgoing message");
166
- }
167
- const newHeaders = new Map;
168
- for (const [key, value] of incomingHeaders) {
169
- if (key.startsWith("__")) {
170
- newHeaders.set(key, value);
171
- }
172
- }
173
- if (headers) {
174
- for (const [key, value] of headers) {
175
- newHeaders.set(key, value);
176
- }
177
- }
178
- return new Event(replyCRI, newHeaders, body || undefined);
179
- }
180
- static buildBrokerUrl(serverInfo) {
181
- return "ws" + (serverInfo.useSSL ? "s" : "") + "://" + serverInfo.host + (serverInfo.port ? ":" + serverInfo.port : "") + "/v1";
182
- }
183
- }
184
-
185
152
  // packages/core/src/internal/api/StompConnectionManager.ts
186
153
  var import_rx_stomp = require("@stomp/rx-stomp");
187
154
  var import_stompjs = require("@stomp/stompjs");
@@ -219,42 +186,50 @@ class StompConnectionManager {
219
186
  get fatalErrors() {
220
187
  return this._fatalErrors;
221
188
  }
222
- activate(connectionInfo) {
223
- return new Promise((resolve, reject) => {
224
- if (!connectionInfo) {
225
- reject("You must supply a valid connectionInfo object");
226
- return;
227
- }
228
- if (!connectionInfo.host) {
229
- reject("No host provided");
230
- return;
189
+ async activate(options) {
190
+ if (!options?.host) {
191
+ throw new Error("No host provided");
192
+ }
193
+ if (this.rxStomp) {
194
+ throw new Error("Stomp connection already active");
195
+ }
196
+ const server = { host: options.host, port: options.port, useSSL: options.useSSL };
197
+ const credentialsResolver = options.credentials;
198
+ let headerAuth = false;
199
+ if (!options.webSocketFactory) {
200
+ if (!credentialsResolver) {
201
+ throw new Error("No credentials supplied and no default resolution applied");
231
202
  }
232
- if (this.rxStomp) {
233
- reject("Stomp connection already active");
234
- return;
203
+ const resolved = await credentialsResolver.resolve(server);
204
+ if (resolved === null) {
205
+ throw new Error("No Kinotic credentials found; consulted: " + credentialsResolver.name);
235
206
  }
207
+ headerAuth = resolved.authHeaders != null;
208
+ }
209
+ return new Promise((resolve, reject) => {
236
210
  this.connectionAttempts = 0;
237
211
  this.initialConnectionSuccessful = false;
238
212
  this.lastWebsocketError = null;
239
213
  this.maxConnectionAttemptsReached = false;
240
- const url = Util.buildBrokerUrl(connectionInfo);
214
+ const url = Util.buildBrokerUrl(server);
241
215
  this.rxStomp = new import_rx_stomp.RxStomp;
242
216
  let preparedSocket = null;
243
- const userWebSocketFactory = connectionInfo.webSocketFactory;
217
+ const userWebSocketFactory = options.webSocketFactory;
218
+ const usesPreparedSocket = userWebSocketFactory != null || headerAuth;
244
219
  const stompConfig = {
245
220
  brokerURL: url,
246
221
  connectHeaders: {
247
- ["session-keep-alive" /* SESSION_KEEP_ALIVE_HEADER */]: connectionInfo.sessionKeepAlive
222
+ ["session-keep-alive" /* SESSION_KEEP_ALIVE_HEADER */]: options.sessionKeepAlive ?? "ACTIVITY" /* ACTIVITY */
248
223
  },
249
224
  heartbeatIncoming: 120000,
250
225
  heartbeatOutgoing: 30000,
251
226
  reconnectDelay: this.INITIAL_RECONNECT_DELAY,
252
227
  maxReconnectDelay: this.MAX_RECONNECT_DELAY,
253
228
  reconnectTimeMode: import_stompjs.ReconnectionTimeMode.EXPONENTIAL,
254
- webSocketFactory: userWebSocketFactory ? () => preparedSocket : undefined,
229
+ webSocketFactory: usesPreparedSocket ? () => preparedSocket : undefined,
255
230
  beforeConnect: async () => {
256
- if (!userWebSocketFactory) {
257
- const sessionCheckUrl = "http" + (connectionInfo.useSSL ? "s" : "") + "://" + connectionInfo.host + (connectionInfo.port ? ":" + connectionInfo.port : "") + SESSION_CHECK_PATH;
231
+ if (!usesPreparedSocket) {
232
+ const sessionCheckUrl = "http" + (server.useSSL ? "s" : "") + "://" + server.host + (server.port ? ":" + server.port : "") + SESSION_CHECK_PATH;
258
233
  try {
259
234
  const res = await fetch(sessionCheckUrl, { credentials: "include" });
260
235
  if (res.status === 401) {
@@ -263,9 +238,9 @@ class StompConnectionManager {
263
238
  }
264
239
  } catch {}
265
240
  }
266
- if (connectionInfo?.maxConnectionAttempts) {
241
+ if (options?.maxConnectionAttempts) {
267
242
  this.connectionAttempts++;
268
- if (this.connectionAttempts > connectionInfo.maxConnectionAttempts) {
243
+ if (this.connectionAttempts > options.maxConnectionAttempts) {
269
244
  this.maxConnectionAttemptsReached = true;
270
245
  await this.signalFatal(new Error("Max number of reconnection attempts reached", { cause: this.lastWebsocketError ?? undefined }));
271
246
  return;
@@ -281,6 +256,18 @@ class StompConnectionManager {
281
256
  } catch (e) {
282
257
  await this.signalFatal(new Error("WebSocket factory failed", { cause: e }));
283
258
  }
259
+ } else if (headerAuth) {
260
+ try {
261
+ const resolved = await credentialsResolver.resolve(server);
262
+ if (resolved?.authHeaders == null) {
263
+ await this.signalFatal(new Error("Credentials resolver no longer supplies auth headers: " + credentialsResolver.name));
264
+ } else {
265
+ const WS = WebSocket;
266
+ preparedSocket = new WS(url, { headers: resolved.authHeaders });
267
+ }
268
+ } catch (e) {
269
+ await this.signalFatal(new Error("Credential resolution failed", { cause: e }));
270
+ }
284
271
  }
285
272
  }
286
273
  };
@@ -290,8 +277,12 @@ class StompConnectionManager {
290
277
  };
291
278
  }
292
279
  this.rxStomp.configure(stompConfig);
293
- this.rxStomp.webSocketErrors$.subscribe((value) => {
280
+ this.rxStomp.webSocketErrors$.subscribe(async (value) => {
294
281
  this.lastWebsocketError = value;
282
+ if (options?.maxConnectionAttempts && this.connectionAttempts >= options.maxConnectionAttempts) {
283
+ this.maxConnectionAttemptsReached = true;
284
+ await this.signalFatal(new Error("Max number of reconnection attempts reached", { cause: value ?? undefined }));
285
+ }
295
286
  });
296
287
  this.stompErrorsSubscription = this.rxStomp.stompErrors$.subscribe(async (frame) => {
297
288
  const stompError = new Error(frame.headers["message"], { cause: frame });
@@ -436,15 +427,15 @@ class EventBus {
436
427
  isConnected() {
437
428
  return this.stompConnectionManager.connected;
438
429
  }
439
- connect(connectionInfo) {
430
+ connect(options) {
440
431
  return this.serializeLifecycle(async () => {
441
432
  if (!this.stompConnectionManager.active) {
442
433
  this.cleanup();
443
- const connectedInfo = await this.stompConnectionManager.activate(connectionInfo);
434
+ const connectedInfo = await this.stompConnectionManager.activate(options);
444
435
  this.serverInfo = new ServerInfo;
445
- this.serverInfo.host = connectionInfo.host;
446
- this.serverInfo.port = connectionInfo.port;
447
- this.serverInfo.useSSL = connectionInfo.useSSL;
436
+ this.serverInfo.host = options.host;
437
+ this.serverInfo.port = options.port;
438
+ this.serverInfo.useSSL = options.useSSL;
448
439
  this.replyToCri = this.stompConnectionManager.replyToCri;
449
440
  return connectedInfo;
450
441
  } else {
@@ -610,6 +601,130 @@ class EventBus {
610
601
  }
611
602
  }
612
603
 
604
+ // packages/core/src/api/security/ChainedCredentialsResolver.ts
605
+ class ChainedCredentialsResolver {
606
+ name;
607
+ resolvers;
608
+ constructor(...resolvers) {
609
+ this.resolvers = resolvers;
610
+ this.name = `ChainedCredentialsResolver(${resolvers.map((r) => r.name).join(" -> ")})`;
611
+ }
612
+ async resolve(server) {
613
+ for (const resolver of this.resolvers) {
614
+ const resolved = await resolver.resolve(server);
615
+ if (resolved !== null) {
616
+ return resolved;
617
+ }
618
+ }
619
+ return null;
620
+ }
621
+ }
622
+
623
+ // packages/core/src/api/security/EnvCredentialsResolver.ts
624
+ class EnvCredentialsResolver {
625
+ name = "EnvCredentialsResolver";
626
+ async resolve(_server) {
627
+ const env = typeof process !== "undefined" ? process.env : undefined;
628
+ let ret = null;
629
+ if (env?.KINOTIC_CLIENT_ID && env?.KINOTIC_CLIENT_SECRET) {
630
+ const authHeaders = {
631
+ clientId: env.KINOTIC_CLIENT_ID,
632
+ clientSecret: env.KINOTIC_CLIENT_SECRET
633
+ };
634
+ if (env.KINOTIC_ORGANIZATION_ID)
635
+ authHeaders.organizationId = env.KINOTIC_ORGANIZATION_ID;
636
+ if (env.KINOTIC_APPLICATION_ID)
637
+ authHeaders.applicationId = env.KINOTIC_APPLICATION_ID;
638
+ ret = {
639
+ authHeaders,
640
+ organizationId: env.KINOTIC_ORGANIZATION_ID,
641
+ applicationId: env.KINOTIC_APPLICATION_ID
642
+ };
643
+ } else if (env?.KINOTIC_TOKEN) {
644
+ ret = { authHeaders: { Authorization: `Bearer ${env.KINOTIC_TOKEN}` } };
645
+ }
646
+ return ret;
647
+ }
648
+ }
649
+
650
+ // packages/core/src/api/security/SessionCredentialsResolver.ts
651
+ class SessionCredentialsResolver {
652
+ name = "SessionCredentialsResolver";
653
+ async resolve(_server) {
654
+ return typeof document !== "undefined" ? {} : null;
655
+ }
656
+ }
657
+
658
+ // packages/core/src/internal/api/Util.ts
659
+ var DEFAULT_PORT = 58503;
660
+ var DEFAULT_HOST = "api.kinotic.com";
661
+
662
+ class Util {
663
+ static createReplyEvent(incomingHeaders, headers, body) {
664
+ if (!incomingHeaders) {
665
+ throw new Error("incomingHeaders cannot be null");
666
+ }
667
+ const replyCRI = incomingHeaders.get("reply-to" /* REPLY_TO_HEADER */);
668
+ if (!replyCRI || replyCRI.trim() === "") {
669
+ throw new Error("No reply-to header found, cannot create outgoing message");
670
+ }
671
+ const newHeaders = new Map;
672
+ for (const [key, value] of incomingHeaders) {
673
+ if (key.startsWith("__")) {
674
+ newHeaders.set(key, value);
675
+ }
676
+ }
677
+ if (headers) {
678
+ for (const [key, value] of headers) {
679
+ newHeaders.set(key, value);
680
+ }
681
+ }
682
+ return new Event(replyCRI, newHeaders, body || undefined);
683
+ }
684
+ static buildBrokerUrl(serverInfo) {
685
+ return "ws" + (serverInfo.useSSL ? "s" : "") + "://" + serverInfo.host + (serverInfo.port ? ":" + serverInfo.port : "") + "/v1";
686
+ }
687
+ static resolveConnectOptions(options) {
688
+ const opts = options ?? {};
689
+ const env = typeof process !== "undefined" ? process.env : undefined;
690
+ const location = typeof window !== "undefined" ? window.location : undefined;
691
+ let host = opts.host ?? env?.KINOTIC_SERVER_HOST;
692
+ const hostFromLocation = host == null && location != null;
693
+ const hostDefaulted = host == null && location == null;
694
+ if (host == null) {
695
+ host = location?.hostname ?? DEFAULT_HOST;
696
+ }
697
+ let useSSL = opts.useSSL;
698
+ if (useSSL == null) {
699
+ if (env?.KINOTIC_SERVER_USE_SSL != null) {
700
+ useSSL = env.KINOTIC_SERVER_USE_SSL === "true";
701
+ } else if (hostFromLocation) {
702
+ useSSL = location.protocol === "https:";
703
+ } else {
704
+ useSSL = hostDefaulted;
705
+ }
706
+ }
707
+ let port = opts.port;
708
+ if (port === undefined) {
709
+ if (env?.KINOTIC_SERVER_PORT != null) {
710
+ port = Number(env.KINOTIC_SERVER_PORT);
711
+ } else if (hostFromLocation) {
712
+ port = location.port ? Number(location.port) : null;
713
+ } else {
714
+ port = useSSL ? null : DEFAULT_PORT;
715
+ }
716
+ }
717
+ return {
718
+ ...opts,
719
+ host,
720
+ port,
721
+ useSSL,
722
+ sessionKeepAlive: opts.sessionKeepAlive ?? "ACTIVITY" /* ACTIVITY */,
723
+ credentials: opts.credentials ?? new ChainedCredentialsResolver(new EnvCredentialsResolver, new SessionCredentialsResolver)
724
+ };
725
+ }
726
+ }
727
+
613
728
  // packages/core/src/api/ServiceRegistry.ts
614
729
  var import_debug3 = __toESM(require("debug"));
615
730
 
@@ -1158,7 +1273,7 @@ var import_operators2 = require("rxjs/operators");
1158
1273
  // packages/core/package.json
1159
1274
  var package_default = {
1160
1275
  name: "@kinotic-ai/core",
1161
- version: "5.0.0-beta.0",
1276
+ version: "5.0.0-beta.2",
1162
1277
  type: "module",
1163
1278
  files: [
1164
1279
  "dist"
@@ -1177,6 +1292,16 @@ var package_default = {
1177
1292
  default: "./dist/index.cjs"
1178
1293
  }
1179
1294
  },
1295
+ "./node": {
1296
+ import: {
1297
+ types: "./dist/node/index.d.ts",
1298
+ default: "./dist/node/index.js"
1299
+ },
1300
+ require: {
1301
+ types: "./dist/node/index.d.cts",
1302
+ default: "./dist/node/index.cjs"
1303
+ }
1304
+ },
1180
1305
  "./package.json": "./package.json"
1181
1306
  },
1182
1307
  homepage: "https://github.com/kinotic-ai/kinotic",
@@ -1198,7 +1323,7 @@ var package_default = {
1198
1323
  "@opentelemetry/sdk-trace-node": "^2.6.1",
1199
1324
  "@types/node": "^25.6.0",
1200
1325
  "@types/uuid": "^11.0.0",
1201
- "@types/ws": "^8.18.1",
1326
+ "@types/ws": "^8.18.0",
1202
1327
  "@vitest/coverage-v8": "^4.1.4",
1203
1328
  "@vitest/runner": "^4.1.4",
1204
1329
  "@vitest/ui": "^4.1.4",
@@ -1222,7 +1347,8 @@ var package_default = {
1222
1347
  "p-tap": "^4.0.0",
1223
1348
  rxjs: "^7.8.2",
1224
1349
  "typescript-optional": "3.0.0-alpha.3",
1225
- uuid: "^13.0.0"
1350
+ uuid: "^13.0.0",
1351
+ ws: "^8.18.0"
1226
1352
  },
1227
1353
  peerDependenciesMeta: {
1228
1354
  typescript: {
@@ -1395,8 +1521,8 @@ class KinoticSingleton {
1395
1521
  this._eventBus = eventBus;
1396
1522
  this.serviceRegistry.eventBus = eventBus;
1397
1523
  }
1398
- connect(connectionInfo) {
1399
- return this._eventBus.connect(connectionInfo);
1524
+ connect(options) {
1525
+ return this._eventBus.connect(Util.resolveConnectOptions(options));
1400
1526
  }
1401
1527
  disconnect(force) {
1402
1528
  return this._eventBus.disconnect(force);
@@ -1639,39 +1765,32 @@ class Order {
1639
1765
  class Sort {
1640
1766
  orders = [];
1641
1767
  }
1642
- // packages/core/src/api/security/AuthenticatedWebSocketFactory.ts
1643
- function createAuthenticatedWebSocketFactory(serverInfo, authProvider) {
1644
- const brokerUrl = Util.buildBrokerUrl(serverInfo);
1645
- return async () => {
1646
- const headers = await authProvider.getAuthHeaders();
1647
- const WS = WebSocket;
1648
- return new WS(brokerUrl, { headers });
1649
- };
1650
- }
1651
- // packages/core/src/api/security/BasicAuthProvider.ts
1652
- class BasicAuthProvider {
1653
- authHeader;
1654
- constructor(login, passcode) {
1655
- const bytes = new TextEncoder().encode(`${login}:${passcode}`);
1656
- let binary = "";
1657
- for (const byte of bytes) {
1658
- binary += String.fromCharCode(byte);
1659
- }
1660
- this.authHeader = `Basic ${btoa(binary)}`;
1661
- }
1662
- getAuthHeaders() {
1663
- return { Authorization: this.authHeader };
1768
+ // packages/core/src/api/security/BasicCredentialsResolver.ts
1769
+ class BasicCredentialsResolver {
1770
+ name = "BasicCredentialsResolver";
1771
+ credentials;
1772
+ constructor(clientId, clientSecret, organizationId, applicationId) {
1773
+ const authHeaders = { clientId, clientSecret };
1774
+ if (organizationId != null)
1775
+ authHeaders.organizationId = organizationId;
1776
+ if (applicationId != null)
1777
+ authHeaders.applicationId = applicationId;
1778
+ this.credentials = { authHeaders, organizationId, applicationId };
1779
+ }
1780
+ async resolve(_server) {
1781
+ return this.credentials;
1664
1782
  }
1665
1783
  }
1666
- // packages/core/src/api/security/BearerTokenAuthProvider.ts
1667
- class BearerTokenAuthProvider {
1784
+ // packages/core/src/api/security/BearerCredentialsResolver.ts
1785
+ class BearerCredentialsResolver {
1786
+ name = "BearerCredentialsResolver";
1668
1787
  token;
1669
1788
  constructor(token) {
1670
1789
  this.token = token;
1671
1790
  }
1672
- async getAuthHeaders() {
1791
+ async resolve(_server) {
1673
1792
  const token = typeof this.token === "function" ? await this.token() : this.token;
1674
- return { Authorization: `Bearer ${token}` };
1793
+ return { authHeaders: { Authorization: `Bearer ${token}` } };
1675
1794
  }
1676
1795
  }
1677
1796
  // packages/core/src/api/security/ConnectedInfo.ts
@@ -1682,9 +1801,8 @@ class ConnectedInfo {
1682
1801
  // packages/core/src/api/security/ParticipantConstants.ts
1683
1802
  class ParticipantConstants {
1684
1803
  static PARTICIPANT_TYPE_METADATA_KEY = "type";
1685
- static PARTICIPANT_TYPE_DEVICE = "device";
1686
- static PARTICIPANT_TYPE_CLI = "cli";
1687
1804
  static PARTICIPANT_TYPE_USER = "user";
1688
- static PARTICIPANT_TYPE_NODE = "node";
1805
+ static PARTICIPANT_TYPE_DELEGATE = "delegate";
1806
+ static PARTICIPANT_TYPE_MACHINE = "machine";
1689
1807
  static CLI_PARTICIPANT_ID = "-42-Kinotic-CLI-42-";
1690
1808
  }