@copilotkitnext/core 0.0.20 → 0.0.21-alpha.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.mjs CHANGED
@@ -10,20 +10,55 @@ import {
10
10
  } from "@ag-ui/client";
11
11
  var ProxiedCopilotRuntimeAgent = class extends HttpAgent {
12
12
  runtimeUrl;
13
+ transport;
14
+ singleEndpointUrl;
13
15
  constructor(config) {
16
+ const normalizedRuntimeUrl = config.runtimeUrl ? config.runtimeUrl.replace(/\/$/, "") : void 0;
17
+ const transport = config.transport ?? "rest";
18
+ const runUrl = transport === "single" ? normalizedRuntimeUrl ?? config.runtimeUrl ?? "" : `${normalizedRuntimeUrl ?? config.runtimeUrl}/agent/${encodeURIComponent(config.agentId ?? "")}/run`;
19
+ if (!runUrl) {
20
+ throw new Error("ProxiedCopilotRuntimeAgent requires a runtimeUrl when transport is set to 'single'.");
21
+ }
14
22
  super({
15
23
  ...config,
16
- url: `${config.runtimeUrl}/agent/${config.agentId}/run`
24
+ url: runUrl
17
25
  });
18
- this.runtimeUrl = config.runtimeUrl;
26
+ this.runtimeUrl = normalizedRuntimeUrl ?? config.runtimeUrl;
27
+ this.transport = transport;
28
+ if (this.transport === "single") {
29
+ this.singleEndpointUrl = this.runtimeUrl;
30
+ }
19
31
  }
20
32
  abortRun() {
21
- if (!this.runtimeUrl || !this.agentId || !this.threadId) {
33
+ if (!this.agentId || !this.threadId) {
22
34
  return;
23
35
  }
24
36
  if (typeof fetch === "undefined") {
25
37
  return;
26
38
  }
39
+ if (this.transport === "single") {
40
+ if (!this.singleEndpointUrl) {
41
+ return;
42
+ }
43
+ const headers = new Headers({ ...this.headers, "Content-Type": "application/json" });
44
+ void fetch(this.singleEndpointUrl, {
45
+ method: "POST",
46
+ headers,
47
+ body: JSON.stringify({
48
+ method: "agent/stop",
49
+ params: {
50
+ agentId: this.agentId,
51
+ threadId: this.threadId
52
+ }
53
+ })
54
+ }).catch((error) => {
55
+ console.error("ProxiedCopilotRuntimeAgent: stop request failed", error);
56
+ });
57
+ return;
58
+ }
59
+ if (!this.runtimeUrl) {
60
+ return;
61
+ }
27
62
  const stopPath = `${this.runtimeUrl}/agent/${encodeURIComponent(this.agentId)}/stop/${encodeURIComponent(this.threadId)}`;
28
63
  const origin = typeof window !== "undefined" && window.location ? window.location.origin : "http://localhost";
29
64
  const base = new URL(this.runtimeUrl, origin);
@@ -39,9 +74,64 @@ var ProxiedCopilotRuntimeAgent = class extends HttpAgent {
39
74
  });
40
75
  }
41
76
  connect(input) {
77
+ if (this.transport === "single") {
78
+ if (!this.singleEndpointUrl) {
79
+ throw new Error("Single endpoint transport requires a runtimeUrl");
80
+ }
81
+ const requestInit = this.createSingleRouteRequestInit(input, "agent/connect", {
82
+ agentId: this.agentId
83
+ });
84
+ const httpEvents2 = runHttpRequest(this.singleEndpointUrl, requestInit);
85
+ return transformHttpEventStream(httpEvents2);
86
+ }
42
87
  const httpEvents = runHttpRequest(`${this.runtimeUrl}/agent/${this.agentId}/connect`, this.requestInit(input));
43
88
  return transformHttpEventStream(httpEvents);
44
89
  }
90
+ run(input) {
91
+ if (this.transport === "single") {
92
+ if (!this.singleEndpointUrl) {
93
+ throw new Error("Single endpoint transport requires a runtimeUrl");
94
+ }
95
+ const requestInit = this.createSingleRouteRequestInit(input, "agent/run", {
96
+ agentId: this.agentId
97
+ });
98
+ const httpEvents = runHttpRequest(this.singleEndpointUrl, requestInit);
99
+ return transformHttpEventStream(httpEvents);
100
+ }
101
+ return super.run(input);
102
+ }
103
+ createSingleRouteRequestInit(input, method, params) {
104
+ if (!this.agentId) {
105
+ throw new Error("ProxiedCopilotRuntimeAgent requires agentId to make runtime requests");
106
+ }
107
+ const baseInit = super.requestInit(input);
108
+ const headers = new Headers(baseInit.headers ?? {});
109
+ headers.set("Content-Type", "application/json");
110
+ headers.set("Accept", headers.get("Accept") ?? "text/event-stream");
111
+ let originalBody = void 0;
112
+ if (typeof baseInit.body === "string") {
113
+ try {
114
+ originalBody = JSON.parse(baseInit.body);
115
+ } catch (error) {
116
+ console.warn("ProxiedCopilotRuntimeAgent: failed to parse request body for single route transport", error);
117
+ originalBody = void 0;
118
+ }
119
+ }
120
+ const envelope = {
121
+ method
122
+ };
123
+ if (params && Object.keys(params).length > 0) {
124
+ envelope.params = params;
125
+ }
126
+ if (originalBody !== void 0) {
127
+ envelope.body = originalBody;
128
+ }
129
+ return {
130
+ ...baseInit,
131
+ headers,
132
+ body: JSON.stringify(envelope)
133
+ };
134
+ }
45
135
  };
46
136
 
47
137
  // src/core/agent-registry.ts
@@ -55,6 +145,7 @@ var AgentRegistry = class {
55
145
  _runtimeUrl;
56
146
  _runtimeVersion;
57
147
  _runtimeConnectionStatus = "disconnected" /* Disconnected */;
148
+ _runtimeTransport = "rest";
58
149
  /**
59
150
  * Get all agents as a readonly record
60
151
  */
@@ -70,6 +161,9 @@ var AgentRegistry = class {
70
161
  get runtimeConnectionStatus() {
71
162
  return this._runtimeConnectionStatus;
72
163
  }
164
+ get runtimeTransport() {
165
+ return this._runtimeTransport;
166
+ }
73
167
  /**
74
168
  * Initialize agents from configuration
75
169
  */
@@ -89,6 +183,13 @@ var AgentRegistry = class {
89
183
  this._runtimeUrl = normalizedRuntimeUrl;
90
184
  void this.updateRuntimeConnection();
91
185
  }
186
+ setRuntimeTransport(runtimeTransport) {
187
+ if (this._runtimeTransport === runtimeTransport) {
188
+ return;
189
+ }
190
+ this._runtimeTransport = runtimeTransport;
191
+ void this.updateRuntimeConnection();
192
+ }
92
193
  /**
93
194
  * Set all agents at once (for development use)
94
195
  */
@@ -169,20 +270,19 @@ var AgentRegistry = class {
169
270
  this._runtimeConnectionStatus = "connecting" /* Connecting */;
170
271
  await this.notifyRuntimeStatusChanged("connecting" /* Connecting */);
171
272
  try {
172
- const response = await fetch(`${this.runtimeUrl}/info`, {
173
- headers: this.core.headers
174
- });
273
+ const runtimeInfoResponse = await this.fetchRuntimeInfo();
175
274
  const {
176
275
  version,
177
276
  ...runtimeInfo
178
- } = await response.json();
277
+ } = runtimeInfoResponse;
179
278
  const agents = Object.fromEntries(
180
279
  Object.entries(runtimeInfo.agents).map(([id, { description }]) => {
181
280
  const agent = new ProxiedCopilotRuntimeAgent({
182
281
  runtimeUrl: this.runtimeUrl,
183
282
  agentId: id,
184
283
  // Runtime agents always have their ID set correctly
185
- description
284
+ description,
285
+ transport: this._runtimeTransport
186
286
  });
187
287
  this.applyHeadersToAgent(agent);
188
288
  return [id, agent];
@@ -213,6 +313,36 @@ var AgentRegistry = class {
213
313
  });
214
314
  }
215
315
  }
316
+ async fetchRuntimeInfo() {
317
+ if (!this.runtimeUrl) {
318
+ throw new Error("Runtime URL is not set");
319
+ }
320
+ const baseHeaders = this.core.headers;
321
+ const headers = {
322
+ ...baseHeaders
323
+ };
324
+ if (this._runtimeTransport === "single") {
325
+ if (!headers["Content-Type"]) {
326
+ headers["Content-Type"] = "application/json";
327
+ }
328
+ const response2 = await fetch(this.runtimeUrl, {
329
+ method: "POST",
330
+ headers,
331
+ body: JSON.stringify({ method: "info" })
332
+ });
333
+ if ("ok" in response2 && !response2.ok) {
334
+ throw new Error(`Runtime info request failed with status ${response2.status}`);
335
+ }
336
+ return await response2.json();
337
+ }
338
+ const response = await fetch(`${this.runtimeUrl}/info`, {
339
+ headers
340
+ });
341
+ if ("ok" in response && !response.ok) {
342
+ throw new Error(`Runtime info request failed with status ${response.status}`);
343
+ }
344
+ return await response.json();
345
+ }
216
346
  /**
217
347
  * Assign agent IDs to a record of agents
218
348
  */
@@ -1319,6 +1449,7 @@ var CopilotKitCore = class {
1319
1449
  stateManager;
1320
1450
  constructor({
1321
1451
  runtimeUrl,
1452
+ runtimeTransport = "rest",
1322
1453
  headers = {},
1323
1454
  properties = {},
1324
1455
  agents__unsafe_dev_only = {},
@@ -1336,6 +1467,7 @@ var CopilotKitCore = class {
1336
1467
  this.runHandler.initialize(tools);
1337
1468
  this.suggestionEngine.initialize(suggestionsConfig);
1338
1469
  this.stateManager.initialize();
1470
+ this.agentRegistry.setRuntimeTransport(runtimeTransport);
1339
1471
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
1340
1472
  this.subscribe({
1341
1473
  onAgentsChanged: ({ agents }) => {
@@ -1397,6 +1529,12 @@ var CopilotKitCore = class {
1397
1529
  setRuntimeUrl(runtimeUrl) {
1398
1530
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
1399
1531
  }
1532
+ get runtimeTransport() {
1533
+ return this.agentRegistry.runtimeTransport;
1534
+ }
1535
+ setRuntimeTransport(runtimeTransport) {
1536
+ this.agentRegistry.setRuntimeTransport(runtimeTransport);
1537
+ }
1400
1538
  get runtimeVersion() {
1401
1539
  return this.agentRegistry.runtimeVersion;
1402
1540
  }