@copilotkitnext/core 1.52.2-next.0 → 1.52.2-next.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
@@ -4,6 +4,7 @@ let _copilotkitnext_shared = require("@copilotkitnext/shared");
4
4
  let rxjs = require("rxjs");
5
5
  let rxjs_operators = require("rxjs/operators");
6
6
  let zod_to_json_schema = require("zod-to-json-schema");
7
+ let phoenix = require("phoenix");
7
8
 
8
9
  //#region src/agent.ts
9
10
  /**
@@ -1717,12 +1718,162 @@ function completePartialMarkdown(input) {
1717
1718
  return result;
1718
1719
  }
1719
1720
 
1721
+ //#endregion
1722
+ //#region src/intelligence-agent.ts
1723
+ var IntelligenceAgent = class IntelligenceAgent extends _ag_ui_client.AbstractAgent {
1724
+ config;
1725
+ socket = null;
1726
+ activeChannel = null;
1727
+ threadId = null;
1728
+ constructor(config) {
1729
+ super();
1730
+ this.config = config;
1731
+ }
1732
+ clone() {
1733
+ return new IntelligenceAgent(this.config);
1734
+ }
1735
+ abortRun() {
1736
+ if (!this.threadId) return;
1737
+ if (typeof fetch === "undefined") {
1738
+ this.cleanup();
1739
+ return;
1740
+ }
1741
+ const { runtimeUrl, agentId, headers, credentials } = this.config;
1742
+ const stopPath = `${runtimeUrl}/agent/${encodeURIComponent(agentId)}/stop/${encodeURIComponent(this.threadId)}`;
1743
+ const origin = typeof window !== "undefined" && window.location ? window.location.origin : "http://localhost";
1744
+ const stopUrl = new URL(stopPath, new URL(runtimeUrl, origin));
1745
+ fetch(stopUrl.toString(), {
1746
+ method: "POST",
1747
+ headers: {
1748
+ "Content-Type": "application/json",
1749
+ ...headers
1750
+ },
1751
+ ...credentials ? { credentials } : {}
1752
+ }).catch((error) => {
1753
+ console.error("IntelligenceAgent: stop request failed", error);
1754
+ });
1755
+ this.cleanup();
1756
+ }
1757
+ /**
1758
+ * Connect to a Phoenix channel scoped to the thread, trigger the run via
1759
+ * REST, and relay server-pushed AG-UI events to the Observable subscriber.
1760
+ *
1761
+ * The server pushes each AG-UI event using its EventType string as the
1762
+ * Phoenix event name (e.g. "TEXT_MESSAGE_CHUNK", "TOOL_CALL_START"), with
1763
+ * the full BaseEvent as payload. RUN_FINISHED and RUN_ERROR are terminal
1764
+ * events that complete or error the Observable.
1765
+ */
1766
+ run(input) {
1767
+ return new rxjs.Observable((observer) => {
1768
+ this.threadId = input.threadId;
1769
+ const socket = new phoenix.Socket(this.config.url, { params: this.config.socketParams ?? {} });
1770
+ this.socket = socket;
1771
+ socket.connect();
1772
+ const channel = socket.channel(`agent:${input.threadId}`, { runId: input.runId });
1773
+ this.activeChannel = channel;
1774
+ channel.on(_copilotkitnext_shared.AG_UI_CHANNEL_EVENT, (payload) => {
1775
+ observer.next(payload);
1776
+ if (payload.type === _ag_ui_client.EventType.RUN_FINISHED) {
1777
+ observer.complete();
1778
+ this.cleanup();
1779
+ } else if (payload.type === _ag_ui_client.EventType.RUN_ERROR) {
1780
+ observer.error(new Error(payload.message ?? "Run error"));
1781
+ this.cleanup();
1782
+ }
1783
+ });
1784
+ channel.join().receive("ok", () => {
1785
+ const { runtimeUrl, agentId, headers, credentials } = this.config;
1786
+ const runPath = `${runtimeUrl}/agent/${encodeURIComponent(agentId)}/run`;
1787
+ const origin = typeof window !== "undefined" && window.location ? window.location.origin : "http://localhost";
1788
+ const runUrl = new URL(runPath, new URL(runtimeUrl, origin));
1789
+ fetch(runUrl.toString(), {
1790
+ method: "POST",
1791
+ headers: {
1792
+ "Content-Type": "application/json",
1793
+ ...headers
1794
+ },
1795
+ body: JSON.stringify({
1796
+ threadId: input.threadId,
1797
+ runId: input.runId,
1798
+ messages: input.messages,
1799
+ tools: input.tools,
1800
+ context: input.context,
1801
+ state: input.state,
1802
+ forwardedProps: input.forwardedProps
1803
+ }),
1804
+ ...credentials ? { credentials } : {}
1805
+ }).catch((error) => {
1806
+ observer.error(/* @__PURE__ */ new Error(`REST run request failed: ${error.message ?? error}`));
1807
+ this.cleanup();
1808
+ });
1809
+ }).receive("error", (resp) => {
1810
+ observer.error(/* @__PURE__ */ new Error(`Failed to join channel: ${JSON.stringify(resp)}`));
1811
+ this.cleanup();
1812
+ }).receive("timeout", () => {
1813
+ observer.error(/* @__PURE__ */ new Error("Timed out joining channel"));
1814
+ this.cleanup();
1815
+ });
1816
+ return () => {
1817
+ this.cleanup();
1818
+ };
1819
+ });
1820
+ }
1821
+ /**
1822
+ * Reconnect to an existing thread by joining the Phoenix channel in
1823
+ * "connect" mode and requesting the server replay history.
1824
+ */
1825
+ connect(input) {
1826
+ return new rxjs.Observable((observer) => {
1827
+ this.threadId = input.threadId;
1828
+ const socket = new phoenix.Socket(this.config.url, { params: this.config.socketParams ?? {} });
1829
+ this.socket = socket;
1830
+ socket.connect();
1831
+ const channel = socket.channel(`agent:${input.threadId}`, { mode: "connect" });
1832
+ this.activeChannel = channel;
1833
+ channel.on(_copilotkitnext_shared.AG_UI_CHANNEL_EVENT, (payload) => {
1834
+ observer.next(payload);
1835
+ if (payload.type === _ag_ui_client.EventType.RUN_FINISHED || payload.type === _ag_ui_client.EventType.RUN_ERROR) {
1836
+ observer.complete();
1837
+ this.cleanup();
1838
+ }
1839
+ });
1840
+ channel.join().receive("ok", () => {
1841
+ channel.push(_ag_ui_client.EventType.CUSTOM, {
1842
+ type: _ag_ui_client.EventType.CUSTOM,
1843
+ name: "connect",
1844
+ value: { threadId: input.threadId }
1845
+ });
1846
+ }).receive("error", (resp) => {
1847
+ observer.error(/* @__PURE__ */ new Error(`Failed to join channel: ${JSON.stringify(resp)}`));
1848
+ this.cleanup();
1849
+ }).receive("timeout", () => {
1850
+ observer.error(/* @__PURE__ */ new Error("Timed out joining channel"));
1851
+ this.cleanup();
1852
+ });
1853
+ return () => {
1854
+ this.cleanup();
1855
+ };
1856
+ });
1857
+ }
1858
+ cleanup() {
1859
+ if (this.activeChannel) {
1860
+ this.activeChannel.leave();
1861
+ this.activeChannel = null;
1862
+ }
1863
+ if (this.socket) {
1864
+ this.socket.disconnect();
1865
+ this.socket = null;
1866
+ }
1867
+ }
1868
+ };
1869
+
1720
1870
  //#endregion
1721
1871
  exports.AgentRegistry = AgentRegistry;
1722
1872
  exports.ContextStore = ContextStore;
1723
1873
  exports.CopilotKitCore = CopilotKitCore;
1724
1874
  exports.CopilotKitCoreErrorCode = CopilotKitCoreErrorCode;
1725
1875
  exports.CopilotKitCoreRuntimeConnectionStatus = CopilotKitCoreRuntimeConnectionStatus;
1876
+ exports.IntelligenceAgent = IntelligenceAgent;
1726
1877
  exports.ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;
1727
1878
  exports.RunHandler = RunHandler;
1728
1879
  exports.StateManager = StateManager;