@copilotkit/core 1.66.3 → 1.67.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.umd.js CHANGED
@@ -1009,7 +1009,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1009
1009
 
1010
1010
  //#endregion
1011
1011
  //#region src/core/agent-registry.ts
1012
- /**
1012
+ /** Maximum wait for optional Inspector metadata before degrading to absence. */
1013
+ const INSPECTOR_METADATA_REQUEST_TIMEOUT_MS = 5e3;
1014
+ /** Build case-insensitive JSON headers without mutating the Core snapshot. */
1015
+ function withJsonContentType(headers) {
1016
+ const requestHeaders = new Headers(headers);
1017
+ if (!requestHeaders.has("content-type")) requestHeaders.set("content-type", "application/json");
1018
+ return requestHeaders;
1019
+ }
1020
+ /**
1013
1021
  * Manages agent registration, lifecycle, and runtime connectivity for CopilotKitCore.
1014
1022
  * Handles both local development agents and remote runtime agents.
1015
1023
  */
@@ -1030,6 +1038,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1030
1038
  _defineProperty(this, "_intelligence", void 0);
1031
1039
  _defineProperty(this, "_threadEndpoints", void 0);
1032
1040
  _defineProperty(this, "_suggestions", void 0);
1041
+ _defineProperty(this, "_inspectorMetadata", void 0);
1042
+ _defineProperty(this, "_inspectorMetadataSupported", false);
1043
+ _defineProperty(this, "inspectorMetadataRefreshReady", false);
1044
+ _defineProperty(this, "inspectorMetadataConnectionGeneration", 0);
1045
+ _defineProperty(this, "inspectorMetadataGeneration", 0);
1046
+ _defineProperty(this, "inspectorMetadataHeadersGeneration", 0);
1047
+ _defineProperty(this, "inspectorMetadataCredentialsGeneration", 0);
1048
+ _defineProperty(this, "inspectorMetadataAbortController", void 0);
1049
+ _defineProperty(this, "inspectorMetadataNotificationQueue", Promise.resolve());
1033
1050
  _defineProperty(this, "_a2uiEnabled", false);
1034
1051
  _defineProperty(this, "_a2uiAgents", void 0);
1035
1052
  _defineProperty(this, "_openGenerativeUIEnabled", false);
@@ -1070,6 +1087,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1070
1087
  get suggestions() {
1071
1088
  return this._suggestions;
1072
1089
  }
1090
+ get inspectorMetadata() {
1091
+ return this._inspectorMetadata;
1092
+ }
1073
1093
  get a2uiEnabled() {
1074
1094
  return this._a2uiEnabled;
1075
1095
  }
@@ -1104,6 +1124,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1104
1124
  setRuntimeUrl(runtimeUrl, options) {
1105
1125
  const normalizedRuntimeUrl = runtimeUrl ? runtimeUrl.replace(/\/$/, "") : void 0;
1106
1126
  if (this._runtimeUrl === normalizedRuntimeUrl) return;
1127
+ this.invalidateInspectorMetadataConnection();
1107
1128
  this._runtimeUrl = normalizedRuntimeUrl;
1108
1129
  if (options === null || options === void 0 ? void 0 : options.deferConnection) return;
1109
1130
  this.updateRuntimeConnection();
@@ -1127,6 +1148,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1127
1148
  }
1128
1149
  setRuntimeTransport(runtimeTransport) {
1129
1150
  if (this._requestedTransport === runtimeTransport) return;
1151
+ this.invalidateInspectorMetadataConnection();
1130
1152
  this._requestedTransport = runtimeTransport;
1131
1153
  this._runtimeTransport = runtimeTransport;
1132
1154
  this.updateRuntimeConnection();
@@ -1263,6 +1285,143 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1263
1285
  this.applyCredentialsToAgent(agent);
1264
1286
  });
1265
1287
  }
1288
+ /** Refresh metadata after the Core header snapshot changes. */
1289
+ handleHeadersChanged() {
1290
+ this.inspectorMetadataHeadersGeneration += 1;
1291
+ this.setInspectorMetadata(void 0);
1292
+ this.refreshInspectorMetadata();
1293
+ }
1294
+ /** Refresh metadata after the Core credentials mode changes. */
1295
+ handleCredentialsChanged() {
1296
+ this.inspectorMetadataCredentialsGeneration += 1;
1297
+ this.setInspectorMetadata(void 0);
1298
+ this.refreshInspectorMetadata();
1299
+ }
1300
+ /**
1301
+ * Fetch trusted inspector metadata independently from runtime discovery.
1302
+ * Optional-route failures degrade to absent metadata and never affect the
1303
+ * runtime connection state.
1304
+ */
1305
+ async refreshInspectorMetadata() {
1306
+ var _this$inspectorMetada;
1307
+ const generation = ++this.inspectorMetadataGeneration;
1308
+ (_this$inspectorMetada = this.inspectorMetadataAbortController) === null || _this$inspectorMetada === void 0 || _this$inspectorMetada.abort();
1309
+ this.inspectorMetadataAbortController = void 0;
1310
+ if (!this._inspectorMetadataSupported || !this.inspectorMetadataRefreshReady || !this.runtimeUrl || this._runtimeConnectionStatus !== CopilotKitCoreRuntimeConnectionStatus.Connected) {
1311
+ this.setInspectorMetadata(void 0);
1312
+ return;
1313
+ }
1314
+ const runtimeUrl = this.runtimeUrl;
1315
+ const requestedTransport = this._requestedTransport;
1316
+ const resolvedTransport = this._runtimeTransport;
1317
+ const headersGeneration = this.inspectorMetadataHeadersGeneration;
1318
+ const credentialsGeneration = this.inspectorMetadataCredentialsGeneration;
1319
+ const friends = this.core;
1320
+ const headers = { ...friends.headers };
1321
+ const credentials = friends.credentials;
1322
+ const abortController = new AbortController();
1323
+ this.inspectorMetadataAbortController = abortController;
1324
+ let timedOut = false;
1325
+ let timeoutId;
1326
+ let rejectForAbort;
1327
+ const aborted = new Promise((_resolve, reject) => {
1328
+ rejectForAbort = reject;
1329
+ });
1330
+ const handleAbort = () => {
1331
+ rejectForAbort === null || rejectForAbort === void 0 || rejectForAbort(/* @__PURE__ */ new Error("Inspector metadata request aborted"));
1332
+ };
1333
+ abortController.signal.addEventListener("abort", handleAbort, { once: true });
1334
+ const timeout = new Promise((_resolve, reject) => {
1335
+ timeoutId = setTimeout(() => {
1336
+ timedOut = true;
1337
+ reject(/* @__PURE__ */ new Error("Inspector metadata request timed out"));
1338
+ abortController.abort();
1339
+ }, INSPECTOR_METADATA_REQUEST_TIMEOUT_MS);
1340
+ });
1341
+ let nextMetadata;
1342
+ try {
1343
+ const request = (async () => {
1344
+ const response = resolvedTransport === "single" ? await this.fetchInspectorMetadataSingle({
1345
+ runtimeUrl,
1346
+ headers,
1347
+ credentials,
1348
+ signal: abortController.signal
1349
+ }) : await this.fetchInspectorMetadataRest({
1350
+ runtimeUrl,
1351
+ headers,
1352
+ credentials,
1353
+ signal: abortController.signal
1354
+ });
1355
+ if (response.status === 204 || !response.ok) return;
1356
+ return (0, _copilotkit_shared.parseInspectorMetadataV1)(await response.json());
1357
+ })();
1358
+ nextMetadata = await Promise.race([
1359
+ request,
1360
+ aborted,
1361
+ timeout
1362
+ ]);
1363
+ } catch (_unused) {
1364
+ nextMetadata = void 0;
1365
+ } finally {
1366
+ if (timeoutId !== void 0) clearTimeout(timeoutId);
1367
+ abortController.signal.removeEventListener("abort", handleAbort);
1368
+ }
1369
+ if (!this.isInspectorMetadataRequestCurrent({
1370
+ generation,
1371
+ runtimeUrl,
1372
+ requestedTransport,
1373
+ resolvedTransport,
1374
+ headersGeneration,
1375
+ credentialsGeneration,
1376
+ signal: abortController.signal,
1377
+ allowAbortedSignal: timedOut
1378
+ })) return;
1379
+ this.inspectorMetadataAbortController = void 0;
1380
+ this.setInspectorMetadata(nextMetadata);
1381
+ }
1382
+ async fetchInspectorMetadataRest({ runtimeUrl, headers, credentials, signal }) {
1383
+ return fetch(`${runtimeUrl}/inspector-metadata`, {
1384
+ method: "GET",
1385
+ headers,
1386
+ ...credentials ? { credentials } : {},
1387
+ signal
1388
+ });
1389
+ }
1390
+ async fetchInspectorMetadataSingle({ runtimeUrl, headers, credentials, signal }) {
1391
+ return fetch(runtimeUrl, {
1392
+ method: "POST",
1393
+ headers: withJsonContentType(headers),
1394
+ body: JSON.stringify({ method: "inspector/metadata" }),
1395
+ ...credentials ? { credentials } : {},
1396
+ signal
1397
+ });
1398
+ }
1399
+ isInspectorMetadataRequestCurrent({ generation, runtimeUrl, requestedTransport, resolvedTransport, headersGeneration, credentialsGeneration, signal, allowAbortedSignal = false }) {
1400
+ return (allowAbortedSignal || !signal.aborted) && generation === this.inspectorMetadataGeneration && runtimeUrl === this.runtimeUrl && requestedTransport === this._requestedTransport && resolvedTransport === this._runtimeTransport && headersGeneration === this.inspectorMetadataHeadersGeneration && credentialsGeneration === this.inspectorMetadataCredentialsGeneration && this._inspectorMetadataSupported && this.inspectorMetadataRefreshReady && this._runtimeConnectionStatus === CopilotKitCoreRuntimeConnectionStatus.Connected;
1401
+ }
1402
+ invalidateInspectorMetadataConnection() {
1403
+ var _this$inspectorMetada2;
1404
+ this._inspectorMetadataSupported = false;
1405
+ this.inspectorMetadataRefreshReady = false;
1406
+ this.inspectorMetadataConnectionGeneration += 1;
1407
+ this.inspectorMetadataGeneration += 1;
1408
+ (_this$inspectorMetada2 = this.inspectorMetadataAbortController) === null || _this$inspectorMetada2 === void 0 || _this$inspectorMetada2.abort();
1409
+ this.inspectorMetadataAbortController = void 0;
1410
+ this.setInspectorMetadata(void 0);
1411
+ }
1412
+ /**
1413
+ * Publish one state transition without making refreshes wait on subscribers.
1414
+ * Each queued callback closes over its readonly snapshot so reentrant changes
1415
+ * cannot replace the value that an older publication is still delivering.
1416
+ */
1417
+ setInspectorMetadata(inspectorMetadata) {
1418
+ if (JSON.stringify(this._inspectorMetadata) === JSON.stringify(inspectorMetadata)) return;
1419
+ this._inspectorMetadata = inspectorMetadata;
1420
+ const snapshot = inspectorMetadata;
1421
+ this.inspectorMetadataNotificationQueue = this.inspectorMetadataNotificationQueue.then(() => this.notifyInspectorMetadataChanged(snapshot)).catch((error) => {
1422
+ console.error("Subscriber onInspectorMetadataChanged queue error:", error);
1423
+ });
1424
+ }
1266
1425
  /**
1267
1426
  * Update runtime connection and fetch remote agents
1268
1427
  */
@@ -1285,6 +1444,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1285
1444
  }
1286
1445
  async performRuntimeConnection() {
1287
1446
  if (!this.runtimeUrl) {
1447
+ this.invalidateInspectorMetadataConnection();
1288
1448
  this._runtimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus.Disconnected;
1289
1449
  this._runtimeVersion = void 0;
1290
1450
  this._audioFileTranscriptionEnabled = false;
@@ -1301,11 +1461,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1301
1461
  await this.notifyAgentsChanged();
1302
1462
  return;
1303
1463
  }
1464
+ const inspectorMetadataConnectionGeneration = this.inspectorMetadataConnectionGeneration;
1304
1465
  this._runtimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus.Connecting;
1305
1466
  await this.notifyRuntimeStatusChanged(CopilotKitCoreRuntimeConnectionStatus.Connecting);
1306
1467
  try {
1307
1468
  var _runtimeInfoResponse$2, _runtimeInfoResponse$3, _ref, _a2uiInfo$enabled, _runtimeInfoResponse$4, _runtimeInfoResponse$5;
1308
- const runtimeInfoResponse = await this.fetchRuntimeInfo();
1469
+ if (inspectorMetadataConnectionGeneration !== this.inspectorMetadataConnectionGeneration) return;
1470
+ const { runtimeInfo: runtimeInfoResponse, resolvedTransport } = await this.fetchRuntimeInfo();
1471
+ if (inspectorMetadataConnectionGeneration !== this.inspectorMetadataConnectionGeneration) return;
1472
+ this._runtimeTransport = resolvedTransport;
1309
1473
  const { version, ...runtimeInfo } = runtimeInfoResponse;
1310
1474
  const credentials = this.core.credentials;
1311
1475
  const rawDebug = this.core.debug;
@@ -1342,6 +1506,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1342
1506
  this._intelligence = runtimeInfoResponse.intelligence;
1343
1507
  this._threadEndpoints = runtimeInfoResponse.threadEndpoints;
1344
1508
  this._suggestions = runtimeInfoResponse.suggestions;
1509
+ this._inspectorMetadataSupported = runtimeInfoResponse.inspectorMetadata === true;
1510
+ this.inspectorMetadataRefreshReady = false;
1511
+ if (!this._inspectorMetadataSupported) this.setInspectorMetadata(void 0);
1345
1512
  const a2uiInfo = runtimeInfoResponse.a2ui;
1346
1513
  this._a2uiEnabled = (_ref = (_a2uiInfo$enabled = a2uiInfo === null || a2uiInfo === void 0 ? void 0 : a2uiInfo.enabled) !== null && _a2uiInfo$enabled !== void 0 ? _a2uiInfo$enabled : runtimeInfoResponse.a2uiEnabled) !== null && _ref !== void 0 ? _ref : false;
1347
1514
  this._a2uiAgents = (a2uiInfo === null || a2uiInfo === void 0 ? void 0 : a2uiInfo.enabled) ? a2uiInfo.agents : void 0;
@@ -1350,7 +1517,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1350
1517
  this._telemetryDisabled = (_runtimeInfoResponse$5 = runtimeInfoResponse.telemetryDisabled) !== null && _runtimeInfoResponse$5 !== void 0 ? _runtimeInfoResponse$5 : false;
1351
1518
  await this.notifyRuntimeStatusChanged(CopilotKitCoreRuntimeConnectionStatus.Connected);
1352
1519
  await this.notifyAgentsChanged();
1520
+ if (inspectorMetadataConnectionGeneration !== this.inspectorMetadataConnectionGeneration || this._runtimeConnectionStatus !== CopilotKitCoreRuntimeConnectionStatus.Connected) return;
1521
+ this.inspectorMetadataRefreshReady = true;
1522
+ if (this._inspectorMetadataSupported) this.refreshInspectorMetadata();
1353
1523
  } catch (error) {
1524
+ if (inspectorMetadataConnectionGeneration !== this.inspectorMetadataConnectionGeneration) return;
1525
+ this.invalidateInspectorMetadataConnection();
1354
1526
  this._runtimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus.Error;
1355
1527
  this._runtimeVersion = void 0;
1356
1528
  this._audioFileTranscriptionEnabled = false;
@@ -1376,24 +1548,31 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1376
1548
  }
1377
1549
  }
1378
1550
  async fetchRuntimeInfo() {
1379
- if (!this.runtimeUrl) throw new Error("Runtime URL is not set");
1551
+ const runtimeUrl = this.runtimeUrl;
1552
+ if (!runtimeUrl) throw new Error("Runtime URL is not set");
1380
1553
  const baseHeaders = this.core.headers;
1381
1554
  const credentials = this.core.credentials;
1382
1555
  const headers = { ...baseHeaders };
1383
- if (this._runtimeTransport === "single") return this.fetchRuntimeInfoSingle(headers, credentials);
1384
- if (this._runtimeTransport === "auto") return this.fetchRuntimeInfoAutoDetect(headers, credentials);
1385
- const response = await fetch(`${this.runtimeUrl}/info`, {
1556
+ const runtimeTransport = this._runtimeTransport;
1557
+ if (runtimeTransport === "single") return {
1558
+ runtimeInfo: await this.fetchRuntimeInfoSingle(runtimeUrl, headers, credentials),
1559
+ resolvedTransport: "single"
1560
+ };
1561
+ if (runtimeTransport === "auto") return this.fetchRuntimeInfoAutoDetect(runtimeUrl, headers, credentials);
1562
+ const response = await fetch(`${runtimeUrl}/info`, {
1386
1563
  headers,
1387
1564
  ...credentials ? { credentials } : {}
1388
1565
  });
1389
1566
  if (!response.ok) throw new Error(`Runtime info request failed with status ${response.status}`);
1390
- return await response.json();
1567
+ return {
1568
+ runtimeInfo: await response.json(),
1569
+ resolvedTransport: "rest"
1570
+ };
1391
1571
  }
1392
- async fetchRuntimeInfoSingle(headers, credentials) {
1393
- if (!headers["Content-Type"]) headers["Content-Type"] = "application/json";
1394
- const response = await fetch(this.runtimeUrl, {
1572
+ async fetchRuntimeInfoSingle(runtimeUrl, headers, credentials) {
1573
+ const response = await fetch(runtimeUrl, {
1395
1574
  method: "POST",
1396
- headers,
1575
+ headers: withJsonContentType(headers),
1397
1576
  body: JSON.stringify({ method: "info" }),
1398
1577
  ...credentials ? { credentials } : {}
1399
1578
  });
@@ -1402,22 +1581,24 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1402
1581
  }
1403
1582
  /**
1404
1583
  * Auto-detect transport by trying REST first, then falling back to single-endpoint.
1405
- * Updates `_runtimeTransport` to the detected value so subsequent requests use it directly.
1584
+ * The caller commits the detected transport only after confirming this
1585
+ * connection attempt is still current.
1406
1586
  */
1407
- async fetchRuntimeInfoAutoDetect(headers, credentials) {
1587
+ async fetchRuntimeInfoAutoDetect(runtimeUrl, headers, credentials) {
1408
1588
  try {
1409
- const response = await fetch(`${this.runtimeUrl}/info`, {
1589
+ const response = await fetch(`${runtimeUrl}/info`, {
1410
1590
  headers: { ...headers },
1411
1591
  ...credentials ? { credentials } : {}
1412
1592
  });
1413
- if (response.status >= 200 && response.status < 300) {
1414
- this._runtimeTransport = "rest";
1415
- return await response.json();
1416
- }
1417
- } catch (_unused) {}
1418
- const result = await this.fetchRuntimeInfoSingle({ ...headers }, credentials);
1419
- this._runtimeTransport = "single";
1420
- return result;
1593
+ if (response.status >= 200 && response.status < 300) return {
1594
+ runtimeInfo: await response.json(),
1595
+ resolvedTransport: "rest"
1596
+ };
1597
+ } catch (_unused2) {}
1598
+ return {
1599
+ runtimeInfo: await this.fetchRuntimeInfoSingle(runtimeUrl, { ...headers }, credentials),
1600
+ resolvedTransport: "single"
1601
+ };
1421
1602
  }
1422
1603
  /**
1423
1604
  * Assign agent IDs to a record of agents
@@ -1459,6 +1640,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1459
1640
  });
1460
1641
  }, "Subscriber onAgentsChanged error:");
1461
1642
  }
1643
+ async notifyInspectorMetadataChanged(inspectorMetadata) {
1644
+ await this.core.notifySubscribers((subscriber) => {
1645
+ var _subscriber$onInspect;
1646
+ return (_subscriber$onInspect = subscriber.onInspectorMetadataChanged) === null || _subscriber$onInspect === void 0 ? void 0 : _subscriber$onInspect.call(subscriber, {
1647
+ copilotkit: this.core,
1648
+ inspectorMetadata
1649
+ });
1650
+ }, "Subscriber onInspectorMetadataChanged error:");
1651
+ }
1462
1652
  };
1463
1653
 
1464
1654
  //#endregion
@@ -4302,6 +4492,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4302
4492
  get suggestions() {
4303
4493
  return this.agentRegistry.suggestions;
4304
4494
  }
4495
+ /** Trusted, optional metadata advertised by the connected runtime. */
4496
+ get inspectorMetadata() {
4497
+ return this.agentRegistry.inspectorMetadata;
4498
+ }
4499
+ /** Refresh trusted inspector metadata without reconnecting runtime agents. */
4500
+ async refreshInspectorMetadata() {
4501
+ await this.agentRegistry.refreshInspectorMetadata();
4502
+ }
4305
4503
  get a2uiEnabled() {
4306
4504
  return this.agentRegistry.a2uiEnabled;
4307
4505
  }
@@ -4357,6 +4555,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4357
4555
  this._headers = normalizeHeaders(headers);
4358
4556
  if (this._memoryStore) this.syncMemoryContext();
4359
4557
  this.agentRegistry.applyHeadersToAgents(this.agentRegistry.agents);
4558
+ this.agentRegistry.handleHeadersChanged();
4360
4559
  this.notifySubscribers((subscriber) => {
4361
4560
  var _subscriber$onHeaders;
4362
4561
  return (_subscriber$onHeaders = subscriber.onHeadersChanged) === null || _subscriber$onHeaders === void 0 ? void 0 : _subscriber$onHeaders.call(subscriber, {
@@ -4368,6 +4567,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4368
4567
  setCredentials(credentials) {
4369
4568
  this._credentials = credentials;
4370
4569
  this.agentRegistry.applyCredentialsToAgents(this.agentRegistry.agents);
4570
+ this.agentRegistry.handleCredentialsChanged();
4371
4571
  }
4372
4572
  setProperties(properties) {
4373
4573
  this._properties = properties;