@copilotkitnext/core 0.0.19-threads-and-attachements.1 → 0.0.20
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.d.mts +3 -59
- package/dist/index.d.ts +3 -59
- package/dist/index.js +6 -172
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -172
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -411,7 +411,7 @@ var SuggestionEngine = class {
|
|
|
411
411
|
const clonedAgent = suggestionsProviderAgent.clone();
|
|
412
412
|
agent = clonedAgent;
|
|
413
413
|
agent.agentId = suggestionId;
|
|
414
|
-
agent.threadId =
|
|
414
|
+
agent.threadId = suggestionId;
|
|
415
415
|
agent.messages = JSON.parse(JSON.stringify(suggestionsConsumerAgent.messages));
|
|
416
416
|
agent.state = JSON.parse(JSON.stringify(suggestionsConsumerAgent.state));
|
|
417
417
|
this._suggestions[consumerAgentId] = {
|
|
@@ -719,47 +719,10 @@ var RunHandler = class {
|
|
|
719
719
|
/**
|
|
720
720
|
* Connect an agent (establish initial connection)
|
|
721
721
|
*/
|
|
722
|
-
async connectAgent({ agent
|
|
722
|
+
async connectAgent({ agent }) {
|
|
723
723
|
try {
|
|
724
724
|
if (agent instanceof HttpAgent3) {
|
|
725
|
-
agent.headers = this.core.
|
|
726
|
-
}
|
|
727
|
-
if (threadId) {
|
|
728
|
-
agent.threadId = threadId;
|
|
729
|
-
}
|
|
730
|
-
const expectedThreadIdKey = "__copilotkitExpectedThreadId";
|
|
731
|
-
const guardKey = "__copilotkitThreadGuard";
|
|
732
|
-
agent[expectedThreadIdKey] = agent.threadId ?? threadId ?? null;
|
|
733
|
-
if (!agent[guardKey]) {
|
|
734
|
-
const guardSubscription = agent.subscribe({
|
|
735
|
-
onEvent: ({ event, input }) => {
|
|
736
|
-
const expectedThreadId = agent[expectedThreadIdKey];
|
|
737
|
-
const eventThreadId = input?.threadId;
|
|
738
|
-
if (!expectedThreadId) {
|
|
739
|
-
console.debug(
|
|
740
|
-
"CopilotKitCore: dropping event - no expected thread (disconnected state)",
|
|
741
|
-
"eventThread:",
|
|
742
|
-
eventThreadId,
|
|
743
|
-
"event:",
|
|
744
|
-
event.type
|
|
745
|
-
);
|
|
746
|
-
return { stopPropagation: true };
|
|
747
|
-
}
|
|
748
|
-
if (eventThreadId && eventThreadId !== expectedThreadId) {
|
|
749
|
-
console.debug(
|
|
750
|
-
"CopilotKitCore: dropping event from stale thread",
|
|
751
|
-
eventThreadId,
|
|
752
|
-
"expected",
|
|
753
|
-
expectedThreadId,
|
|
754
|
-
"event",
|
|
755
|
-
event.type
|
|
756
|
-
);
|
|
757
|
-
return { stopPropagation: true };
|
|
758
|
-
}
|
|
759
|
-
return;
|
|
760
|
-
}
|
|
761
|
-
});
|
|
762
|
-
agent[guardKey] = guardSubscription;
|
|
725
|
+
agent.headers = { ...this.core.headers };
|
|
763
726
|
}
|
|
764
727
|
const runAgentResult = await agent.connectAgent(
|
|
765
728
|
{
|
|
@@ -768,7 +731,6 @@ var RunHandler = class {
|
|
|
768
731
|
},
|
|
769
732
|
this.createAgentErrorSubscriber(agent)
|
|
770
733
|
);
|
|
771
|
-
agent.__copilotkitExpectedThreadId = agent.threadId ?? threadId ?? null;
|
|
772
734
|
return this.processAgentResult({ runAgentResult, agent });
|
|
773
735
|
} catch (error) {
|
|
774
736
|
const connectError = error instanceof Error ? error : new Error(String(error));
|
|
@@ -784,17 +746,6 @@ var RunHandler = class {
|
|
|
784
746
|
throw error;
|
|
785
747
|
}
|
|
786
748
|
}
|
|
787
|
-
async disconnectAgent({ agent }) {
|
|
788
|
-
const disconnectableAgent = agent;
|
|
789
|
-
agent.__copilotkitExpectedThreadId = null;
|
|
790
|
-
if (typeof disconnectableAgent.disconnectAgent === "function") {
|
|
791
|
-
try {
|
|
792
|
-
await disconnectableAgent.disconnectAgent();
|
|
793
|
-
} catch (error) {
|
|
794
|
-
console.debug("Error during agent disconnection (non-critical):", error);
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
749
|
/**
|
|
799
750
|
* Run an agent
|
|
800
751
|
*/
|
|
@@ -803,7 +754,7 @@ var RunHandler = class {
|
|
|
803
754
|
void this.core.suggestionEngine.clearSuggestions(agent.agentId);
|
|
804
755
|
}
|
|
805
756
|
if (agent instanceof HttpAgent3) {
|
|
806
|
-
agent.headers = this.core.
|
|
757
|
+
agent.headers = { ...this.core.headers };
|
|
807
758
|
}
|
|
808
759
|
if (withMessages) {
|
|
809
760
|
agent.addMessages(withMessages);
|
|
@@ -812,7 +763,8 @@ var RunHandler = class {
|
|
|
812
763
|
const runAgentResult = await agent.runAgent(
|
|
813
764
|
{
|
|
814
765
|
forwardedProps: this.core.properties,
|
|
815
|
-
tools: this.buildFrontendTools(agent.agentId)
|
|
766
|
+
tools: this.buildFrontendTools(agent.agentId),
|
|
767
|
+
context: Object.values(this.core.context)
|
|
816
768
|
},
|
|
817
769
|
this.createAgentErrorSubscriber(agent)
|
|
818
770
|
);
|
|
@@ -1358,7 +1310,6 @@ var CopilotKitCoreRuntimeConnectionStatus = /* @__PURE__ */ ((CopilotKitCoreRunt
|
|
|
1358
1310
|
var CopilotKitCore = class {
|
|
1359
1311
|
_headers;
|
|
1360
1312
|
_properties;
|
|
1361
|
-
_resourceId;
|
|
1362
1313
|
subscribers = /* @__PURE__ */ new Set();
|
|
1363
1314
|
// Delegate classes
|
|
1364
1315
|
agentRegistry;
|
|
@@ -1370,14 +1321,12 @@ var CopilotKitCore = class {
|
|
|
1370
1321
|
runtimeUrl,
|
|
1371
1322
|
headers = {},
|
|
1372
1323
|
properties = {},
|
|
1373
|
-
resourceId,
|
|
1374
1324
|
agents__unsafe_dev_only = {},
|
|
1375
1325
|
tools = [],
|
|
1376
1326
|
suggestionsConfig = []
|
|
1377
1327
|
}) {
|
|
1378
1328
|
this._headers = headers;
|
|
1379
1329
|
this._properties = properties;
|
|
1380
|
-
this._resourceId = resourceId;
|
|
1381
1330
|
this.agentRegistry = new AgentRegistry(this);
|
|
1382
1331
|
this.contextStore = new ContextStore(this);
|
|
1383
1332
|
this.suggestionEngine = new SuggestionEngine(this);
|
|
@@ -1457,9 +1406,6 @@ var CopilotKitCore = class {
|
|
|
1457
1406
|
get properties() {
|
|
1458
1407
|
return this._properties;
|
|
1459
1408
|
}
|
|
1460
|
-
get resourceId() {
|
|
1461
|
-
return this._resourceId;
|
|
1462
|
-
}
|
|
1463
1409
|
get runtimeConnectionStatus() {
|
|
1464
1410
|
return this.agentRegistry.runtimeConnectionStatus;
|
|
1465
1411
|
}
|
|
@@ -1487,16 +1433,6 @@ var CopilotKitCore = class {
|
|
|
1487
1433
|
"Subscriber onPropertiesChanged error:"
|
|
1488
1434
|
);
|
|
1489
1435
|
}
|
|
1490
|
-
setResourceId(resourceId) {
|
|
1491
|
-
this._resourceId = resourceId;
|
|
1492
|
-
void this.notifySubscribers(
|
|
1493
|
-
(subscriber) => subscriber.onResourceIdChanged?.({
|
|
1494
|
-
copilotkit: this,
|
|
1495
|
-
resourceId: this.resourceId
|
|
1496
|
-
}),
|
|
1497
|
-
"Subscriber onResourceIdChanged error:"
|
|
1498
|
-
);
|
|
1499
|
-
}
|
|
1500
1436
|
/**
|
|
1501
1437
|
* Agent management (delegated to AgentRegistry)
|
|
1502
1438
|
*/
|
|
@@ -1575,9 +1511,6 @@ var CopilotKitCore = class {
|
|
|
1575
1511
|
stopAgent(params) {
|
|
1576
1512
|
params.agent.abortRun();
|
|
1577
1513
|
}
|
|
1578
|
-
async disconnectAgent(params) {
|
|
1579
|
-
await this.runHandler.disconnectAgent(params);
|
|
1580
|
-
}
|
|
1581
1514
|
async runAgent(params) {
|
|
1582
1515
|
return this.runHandler.runAgent(params);
|
|
1583
1516
|
}
|
|
@@ -1593,105 +1526,6 @@ var CopilotKitCore = class {
|
|
|
1593
1526
|
getRunIdsForThread(agentId, threadId) {
|
|
1594
1527
|
return this.stateManager.getRunIdsForThread(agentId, threadId);
|
|
1595
1528
|
}
|
|
1596
|
-
/**
|
|
1597
|
-
* Helper to format resourceId for HTTP header transport.
|
|
1598
|
-
* Encodes each value with encodeURIComponent and joins with comma.
|
|
1599
|
-
*/
|
|
1600
|
-
formatResourceIdHeader() {
|
|
1601
|
-
if (!this._resourceId) {
|
|
1602
|
-
return void 0;
|
|
1603
|
-
}
|
|
1604
|
-
const ids = Array.isArray(this._resourceId) ? this._resourceId : [this._resourceId];
|
|
1605
|
-
return ids.map((id) => encodeURIComponent(id)).join(",");
|
|
1606
|
-
}
|
|
1607
|
-
/**
|
|
1608
|
-
* Get headers with resourceId header included (if resourceId is set).
|
|
1609
|
-
* Used internally by RunHandler for HttpAgent requests.
|
|
1610
|
-
*/
|
|
1611
|
-
getHeadersWithResourceId() {
|
|
1612
|
-
const resourceIdHeader = this.formatResourceIdHeader();
|
|
1613
|
-
return {
|
|
1614
|
-
...this.headers,
|
|
1615
|
-
...resourceIdHeader && { "X-CopilotKit-Resource-ID": resourceIdHeader }
|
|
1616
|
-
};
|
|
1617
|
-
}
|
|
1618
|
-
/**
|
|
1619
|
-
* Thread management
|
|
1620
|
-
*/
|
|
1621
|
-
async listThreads(params) {
|
|
1622
|
-
const runtimeUrl = this.runtimeUrl;
|
|
1623
|
-
if (!runtimeUrl) {
|
|
1624
|
-
throw new Error("Runtime URL is required to list threads");
|
|
1625
|
-
}
|
|
1626
|
-
const baseUrl = runtimeUrl.endsWith("/") ? runtimeUrl.slice(0, -1) : runtimeUrl;
|
|
1627
|
-
const urlString = `${baseUrl}/threads`;
|
|
1628
|
-
const queryParams = new URLSearchParams();
|
|
1629
|
-
if (params?.limit !== void 0) {
|
|
1630
|
-
queryParams.set("limit", params.limit.toString());
|
|
1631
|
-
}
|
|
1632
|
-
if (params?.offset !== void 0) {
|
|
1633
|
-
queryParams.set("offset", params.offset.toString());
|
|
1634
|
-
}
|
|
1635
|
-
const queryString = queryParams.toString();
|
|
1636
|
-
const fullUrl = queryString ? `${urlString}?${queryString}` : urlString;
|
|
1637
|
-
const resourceIdHeader = this.formatResourceIdHeader();
|
|
1638
|
-
const response = await fetch(fullUrl, {
|
|
1639
|
-
method: "GET",
|
|
1640
|
-
headers: {
|
|
1641
|
-
"Content-Type": "application/json",
|
|
1642
|
-
...this.headers,
|
|
1643
|
-
...resourceIdHeader && { "X-CopilotKit-Resource-ID": resourceIdHeader }
|
|
1644
|
-
}
|
|
1645
|
-
});
|
|
1646
|
-
if (!response.ok) {
|
|
1647
|
-
throw new Error(`Failed to list threads: ${response.statusText}`);
|
|
1648
|
-
}
|
|
1649
|
-
return await response.json();
|
|
1650
|
-
}
|
|
1651
|
-
async getThreadMetadata(threadId) {
|
|
1652
|
-
const runtimeUrl = this.runtimeUrl;
|
|
1653
|
-
if (!runtimeUrl) {
|
|
1654
|
-
throw new Error("Runtime URL is required to get thread metadata");
|
|
1655
|
-
}
|
|
1656
|
-
const baseUrl = runtimeUrl.endsWith("/") ? runtimeUrl.slice(0, -1) : runtimeUrl;
|
|
1657
|
-
const fullUrl = `${baseUrl}/threads/${threadId}`;
|
|
1658
|
-
const resourceIdHeader = this.formatResourceIdHeader();
|
|
1659
|
-
const response = await fetch(fullUrl, {
|
|
1660
|
-
method: "GET",
|
|
1661
|
-
headers: {
|
|
1662
|
-
"Content-Type": "application/json",
|
|
1663
|
-
...this.headers,
|
|
1664
|
-
...resourceIdHeader && { "X-CopilotKit-Resource-ID": resourceIdHeader }
|
|
1665
|
-
}
|
|
1666
|
-
});
|
|
1667
|
-
if (response.status === 404) {
|
|
1668
|
-
return null;
|
|
1669
|
-
}
|
|
1670
|
-
if (!response.ok) {
|
|
1671
|
-
throw new Error(`Failed to get thread metadata: ${response.statusText}`);
|
|
1672
|
-
}
|
|
1673
|
-
return await response.json();
|
|
1674
|
-
}
|
|
1675
|
-
async deleteThread(threadId) {
|
|
1676
|
-
const runtimeUrl = this.runtimeUrl;
|
|
1677
|
-
if (!runtimeUrl) {
|
|
1678
|
-
throw new Error("Runtime URL is required to delete a thread");
|
|
1679
|
-
}
|
|
1680
|
-
const baseUrl = runtimeUrl.endsWith("/") ? runtimeUrl.slice(0, -1) : runtimeUrl;
|
|
1681
|
-
const fullUrl = `${baseUrl}/threads/${threadId}`;
|
|
1682
|
-
const resourceIdHeader = this.formatResourceIdHeader();
|
|
1683
|
-
const response = await fetch(fullUrl, {
|
|
1684
|
-
method: "DELETE",
|
|
1685
|
-
headers: {
|
|
1686
|
-
"Content-Type": "application/json",
|
|
1687
|
-
...this.headers,
|
|
1688
|
-
...resourceIdHeader && { "X-CopilotKit-Resource-ID": resourceIdHeader }
|
|
1689
|
-
}
|
|
1690
|
-
});
|
|
1691
|
-
if (!response.ok) {
|
|
1692
|
-
throw new Error(`Failed to delete thread: ${response.statusText}`);
|
|
1693
|
-
}
|
|
1694
|
-
}
|
|
1695
1529
|
/**
|
|
1696
1530
|
* Internal method used by RunHandler to build frontend tools
|
|
1697
1531
|
*/
|