@copilotkitnext/core 0.0.20 → 0.0.21

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 CHANGED
@@ -11,6 +11,7 @@ declare enum ToolCallStatus {
11
11
  Executing = "executing",
12
12
  Complete = "complete"
13
13
  }
14
+ type CopilotRuntimeTransport = "rest" | "single";
14
15
  type FrontendTool<T extends Record<string, unknown> = Record<string, unknown>> = {
15
16
  name: string;
16
17
  description?: string;
@@ -90,6 +91,7 @@ declare class AgentRegistry {
90
91
  private _runtimeUrl?;
91
92
  private _runtimeVersion?;
92
93
  private _runtimeConnectionStatus;
94
+ private _runtimeTransport;
93
95
  constructor(core: CopilotKitCore);
94
96
  /**
95
97
  * Get all agents as a readonly record
@@ -98,6 +100,7 @@ declare class AgentRegistry {
98
100
  get runtimeUrl(): string | undefined;
99
101
  get runtimeVersion(): string | undefined;
100
102
  get runtimeConnectionStatus(): CopilotKitCoreRuntimeConnectionStatus;
103
+ get runtimeTransport(): CopilotRuntimeTransport;
101
104
  /**
102
105
  * Initialize agents from configuration
103
106
  */
@@ -106,6 +109,7 @@ declare class AgentRegistry {
106
109
  * Set the runtime URL and update connection
107
110
  */
108
111
  setRuntimeUrl(runtimeUrl: string | undefined): void;
112
+ setRuntimeTransport(runtimeTransport: CopilotRuntimeTransport): void;
109
113
  /**
110
114
  * Set all agents at once (for development use)
111
115
  */
@@ -134,6 +138,7 @@ declare class AgentRegistry {
134
138
  * Update runtime connection and fetch remote agents
135
139
  */
136
140
  private updateRuntimeConnection;
141
+ private fetchRuntimeInfo;
137
142
  /**
138
143
  * Assign agent IDs to a record of agents
139
144
  */
@@ -231,6 +236,8 @@ declare class RunHandler {
231
236
  interface CopilotKitCoreConfig {
232
237
  /** The endpoint of the CopilotRuntime. */
233
238
  runtimeUrl?: string;
239
+ /** Transport style for CopilotRuntime endpoints. Defaults to REST. */
240
+ runtimeTransport?: CopilotRuntimeTransport;
234
241
  /** Mapping from agent name to its `AbstractAgent` instance. For development only - production requires CopilotRuntime. */
235
242
  agents__unsafe_dev_only?: Record<string, AbstractAgent>;
236
243
  /** Headers appended to every HTTP request made by `CopilotKitCore`. */
@@ -355,7 +362,7 @@ declare class CopilotKitCore {
355
362
  private suggestionEngine;
356
363
  private runHandler;
357
364
  private stateManager;
358
- constructor({ runtimeUrl, headers, properties, agents__unsafe_dev_only, tools, suggestionsConfig, }: CopilotKitCoreConfig);
365
+ constructor({ runtimeUrl, runtimeTransport, headers, properties, agents__unsafe_dev_only, tools, suggestionsConfig, }: CopilotKitCoreConfig);
359
366
  /**
360
367
  * Internal method used by delegate classes and subclasses to notify subscribers
361
368
  */
@@ -372,6 +379,8 @@ declare class CopilotKitCore {
372
379
  get tools(): Readonly<FrontendTool<any>[]>;
373
380
  get runtimeUrl(): string | undefined;
374
381
  setRuntimeUrl(runtimeUrl: string | undefined): void;
382
+ get runtimeTransport(): CopilotRuntimeTransport;
383
+ setRuntimeTransport(runtimeTransport: CopilotRuntimeTransport): void;
375
384
  get runtimeVersion(): string | undefined;
376
385
  get headers(): Readonly<Record<string, string>>;
377
386
  get properties(): Readonly<Record<string, unknown>>;
@@ -615,14 +624,19 @@ declare class StateManager {
615
624
 
616
625
  interface ProxiedCopilotRuntimeAgentConfig extends Omit<HttpAgentConfig, "url"> {
617
626
  runtimeUrl?: string;
627
+ transport?: CopilotRuntimeTransport;
618
628
  }
619
629
  declare class ProxiedCopilotRuntimeAgent extends HttpAgent {
620
630
  runtimeUrl?: string;
631
+ private transport;
632
+ private singleEndpointUrl?;
621
633
  constructor(config: ProxiedCopilotRuntimeAgentConfig);
622
634
  abortRun(): void;
623
635
  connect(input: RunAgentInput): Observable<BaseEvent>;
636
+ run(input: RunAgentInput): Observable<BaseEvent>;
637
+ private createSingleRouteRequestInit;
624
638
  }
625
639
 
626
640
  declare function completePartialMarkdown(input: string): string;
627
641
 
628
- export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreFriendsAccess, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreStopAgentParams, type CopilotKitCoreSubscriber, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, StateManager, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
642
+ export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreFriendsAccess, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreStopAgentParams, type CopilotKitCoreSubscriber, type CopilotRuntimeTransport, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, StateManager, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ declare enum ToolCallStatus {
11
11
  Executing = "executing",
12
12
  Complete = "complete"
13
13
  }
14
+ type CopilotRuntimeTransport = "rest" | "single";
14
15
  type FrontendTool<T extends Record<string, unknown> = Record<string, unknown>> = {
15
16
  name: string;
16
17
  description?: string;
@@ -90,6 +91,7 @@ declare class AgentRegistry {
90
91
  private _runtimeUrl?;
91
92
  private _runtimeVersion?;
92
93
  private _runtimeConnectionStatus;
94
+ private _runtimeTransport;
93
95
  constructor(core: CopilotKitCore);
94
96
  /**
95
97
  * Get all agents as a readonly record
@@ -98,6 +100,7 @@ declare class AgentRegistry {
98
100
  get runtimeUrl(): string | undefined;
99
101
  get runtimeVersion(): string | undefined;
100
102
  get runtimeConnectionStatus(): CopilotKitCoreRuntimeConnectionStatus;
103
+ get runtimeTransport(): CopilotRuntimeTransport;
101
104
  /**
102
105
  * Initialize agents from configuration
103
106
  */
@@ -106,6 +109,7 @@ declare class AgentRegistry {
106
109
  * Set the runtime URL and update connection
107
110
  */
108
111
  setRuntimeUrl(runtimeUrl: string | undefined): void;
112
+ setRuntimeTransport(runtimeTransport: CopilotRuntimeTransport): void;
109
113
  /**
110
114
  * Set all agents at once (for development use)
111
115
  */
@@ -134,6 +138,7 @@ declare class AgentRegistry {
134
138
  * Update runtime connection and fetch remote agents
135
139
  */
136
140
  private updateRuntimeConnection;
141
+ private fetchRuntimeInfo;
137
142
  /**
138
143
  * Assign agent IDs to a record of agents
139
144
  */
@@ -231,6 +236,8 @@ declare class RunHandler {
231
236
  interface CopilotKitCoreConfig {
232
237
  /** The endpoint of the CopilotRuntime. */
233
238
  runtimeUrl?: string;
239
+ /** Transport style for CopilotRuntime endpoints. Defaults to REST. */
240
+ runtimeTransport?: CopilotRuntimeTransport;
234
241
  /** Mapping from agent name to its `AbstractAgent` instance. For development only - production requires CopilotRuntime. */
235
242
  agents__unsafe_dev_only?: Record<string, AbstractAgent>;
236
243
  /** Headers appended to every HTTP request made by `CopilotKitCore`. */
@@ -355,7 +362,7 @@ declare class CopilotKitCore {
355
362
  private suggestionEngine;
356
363
  private runHandler;
357
364
  private stateManager;
358
- constructor({ runtimeUrl, headers, properties, agents__unsafe_dev_only, tools, suggestionsConfig, }: CopilotKitCoreConfig);
365
+ constructor({ runtimeUrl, runtimeTransport, headers, properties, agents__unsafe_dev_only, tools, suggestionsConfig, }: CopilotKitCoreConfig);
359
366
  /**
360
367
  * Internal method used by delegate classes and subclasses to notify subscribers
361
368
  */
@@ -372,6 +379,8 @@ declare class CopilotKitCore {
372
379
  get tools(): Readonly<FrontendTool<any>[]>;
373
380
  get runtimeUrl(): string | undefined;
374
381
  setRuntimeUrl(runtimeUrl: string | undefined): void;
382
+ get runtimeTransport(): CopilotRuntimeTransport;
383
+ setRuntimeTransport(runtimeTransport: CopilotRuntimeTransport): void;
375
384
  get runtimeVersion(): string | undefined;
376
385
  get headers(): Readonly<Record<string, string>>;
377
386
  get properties(): Readonly<Record<string, unknown>>;
@@ -615,14 +624,19 @@ declare class StateManager {
615
624
 
616
625
  interface ProxiedCopilotRuntimeAgentConfig extends Omit<HttpAgentConfig, "url"> {
617
626
  runtimeUrl?: string;
627
+ transport?: CopilotRuntimeTransport;
618
628
  }
619
629
  declare class ProxiedCopilotRuntimeAgent extends HttpAgent {
620
630
  runtimeUrl?: string;
631
+ private transport;
632
+ private singleEndpointUrl?;
621
633
  constructor(config: ProxiedCopilotRuntimeAgentConfig);
622
634
  abortRun(): void;
623
635
  connect(input: RunAgentInput): Observable<BaseEvent>;
636
+ run(input: RunAgentInput): Observable<BaseEvent>;
637
+ private createSingleRouteRequestInit;
624
638
  }
625
639
 
626
640
  declare function completePartialMarkdown(input: string): string;
627
641
 
628
- export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreFriendsAccess, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreStopAgentParams, type CopilotKitCoreSubscriber, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, StateManager, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
642
+ export { AgentRegistry, ContextStore, CopilotKitCore, type CopilotKitCoreAddAgentParams, type CopilotKitCoreConfig, type CopilotKitCoreConnectAgentParams, CopilotKitCoreErrorCode, type CopilotKitCoreFriendsAccess, type CopilotKitCoreGetSuggestionsResult, type CopilotKitCoreGetToolParams, type CopilotKitCoreRunAgentParams, CopilotKitCoreRuntimeConnectionStatus, type CopilotKitCoreStopAgentParams, type CopilotKitCoreSubscriber, type CopilotRuntimeTransport, type DynamicSuggestionsConfig, type FrontendTool, ProxiedCopilotRuntimeAgent, type ProxiedCopilotRuntimeAgentConfig, RunHandler, StateManager, type StaticSuggestionsConfig, type Suggestion, type SuggestionAvailability, SuggestionEngine, type SuggestionsConfig, ToolCallStatus, completePartialMarkdown };
package/dist/index.js CHANGED
@@ -42,20 +42,55 @@ var import_shared = require("@copilotkitnext/shared");
42
42
  var import_client = require("@ag-ui/client");
43
43
  var ProxiedCopilotRuntimeAgent = class extends import_client.HttpAgent {
44
44
  runtimeUrl;
45
+ transport;
46
+ singleEndpointUrl;
45
47
  constructor(config) {
48
+ const normalizedRuntimeUrl = config.runtimeUrl ? config.runtimeUrl.replace(/\/$/, "") : void 0;
49
+ const transport = config.transport ?? "rest";
50
+ const runUrl = transport === "single" ? normalizedRuntimeUrl ?? config.runtimeUrl ?? "" : `${normalizedRuntimeUrl ?? config.runtimeUrl}/agent/${encodeURIComponent(config.agentId ?? "")}/run`;
51
+ if (!runUrl) {
52
+ throw new Error("ProxiedCopilotRuntimeAgent requires a runtimeUrl when transport is set to 'single'.");
53
+ }
46
54
  super({
47
55
  ...config,
48
- url: `${config.runtimeUrl}/agent/${config.agentId}/run`
56
+ url: runUrl
49
57
  });
50
- this.runtimeUrl = config.runtimeUrl;
58
+ this.runtimeUrl = normalizedRuntimeUrl ?? config.runtimeUrl;
59
+ this.transport = transport;
60
+ if (this.transport === "single") {
61
+ this.singleEndpointUrl = this.runtimeUrl;
62
+ }
51
63
  }
52
64
  abortRun() {
53
- if (!this.runtimeUrl || !this.agentId || !this.threadId) {
65
+ if (!this.agentId || !this.threadId) {
54
66
  return;
55
67
  }
56
68
  if (typeof fetch === "undefined") {
57
69
  return;
58
70
  }
71
+ if (this.transport === "single") {
72
+ if (!this.singleEndpointUrl) {
73
+ return;
74
+ }
75
+ const headers = new Headers({ ...this.headers, "Content-Type": "application/json" });
76
+ void fetch(this.singleEndpointUrl, {
77
+ method: "POST",
78
+ headers,
79
+ body: JSON.stringify({
80
+ method: "agent/stop",
81
+ params: {
82
+ agentId: this.agentId,
83
+ threadId: this.threadId
84
+ }
85
+ })
86
+ }).catch((error) => {
87
+ console.error("ProxiedCopilotRuntimeAgent: stop request failed", error);
88
+ });
89
+ return;
90
+ }
91
+ if (!this.runtimeUrl) {
92
+ return;
93
+ }
59
94
  const stopPath = `${this.runtimeUrl}/agent/${encodeURIComponent(this.agentId)}/stop/${encodeURIComponent(this.threadId)}`;
60
95
  const origin = typeof window !== "undefined" && window.location ? window.location.origin : "http://localhost";
61
96
  const base = new URL(this.runtimeUrl, origin);
@@ -71,9 +106,64 @@ var ProxiedCopilotRuntimeAgent = class extends import_client.HttpAgent {
71
106
  });
72
107
  }
73
108
  connect(input) {
109
+ if (this.transport === "single") {
110
+ if (!this.singleEndpointUrl) {
111
+ throw new Error("Single endpoint transport requires a runtimeUrl");
112
+ }
113
+ const requestInit = this.createSingleRouteRequestInit(input, "agent/connect", {
114
+ agentId: this.agentId
115
+ });
116
+ const httpEvents2 = (0, import_client.runHttpRequest)(this.singleEndpointUrl, requestInit);
117
+ return (0, import_client.transformHttpEventStream)(httpEvents2);
118
+ }
74
119
  const httpEvents = (0, import_client.runHttpRequest)(`${this.runtimeUrl}/agent/${this.agentId}/connect`, this.requestInit(input));
75
120
  return (0, import_client.transformHttpEventStream)(httpEvents);
76
121
  }
122
+ run(input) {
123
+ if (this.transport === "single") {
124
+ if (!this.singleEndpointUrl) {
125
+ throw new Error("Single endpoint transport requires a runtimeUrl");
126
+ }
127
+ const requestInit = this.createSingleRouteRequestInit(input, "agent/run", {
128
+ agentId: this.agentId
129
+ });
130
+ const httpEvents = (0, import_client.runHttpRequest)(this.singleEndpointUrl, requestInit);
131
+ return (0, import_client.transformHttpEventStream)(httpEvents);
132
+ }
133
+ return super.run(input);
134
+ }
135
+ createSingleRouteRequestInit(input, method, params) {
136
+ if (!this.agentId) {
137
+ throw new Error("ProxiedCopilotRuntimeAgent requires agentId to make runtime requests");
138
+ }
139
+ const baseInit = super.requestInit(input);
140
+ const headers = new Headers(baseInit.headers ?? {});
141
+ headers.set("Content-Type", "application/json");
142
+ headers.set("Accept", headers.get("Accept") ?? "text/event-stream");
143
+ let originalBody = void 0;
144
+ if (typeof baseInit.body === "string") {
145
+ try {
146
+ originalBody = JSON.parse(baseInit.body);
147
+ } catch (error) {
148
+ console.warn("ProxiedCopilotRuntimeAgent: failed to parse request body for single route transport", error);
149
+ originalBody = void 0;
150
+ }
151
+ }
152
+ const envelope = {
153
+ method
154
+ };
155
+ if (params && Object.keys(params).length > 0) {
156
+ envelope.params = params;
157
+ }
158
+ if (originalBody !== void 0) {
159
+ envelope.body = originalBody;
160
+ }
161
+ return {
162
+ ...baseInit,
163
+ headers,
164
+ body: JSON.stringify(envelope)
165
+ };
166
+ }
77
167
  };
78
168
 
79
169
  // src/core/agent-registry.ts
@@ -87,6 +177,7 @@ var AgentRegistry = class {
87
177
  _runtimeUrl;
88
178
  _runtimeVersion;
89
179
  _runtimeConnectionStatus = "disconnected" /* Disconnected */;
180
+ _runtimeTransport = "rest";
90
181
  /**
91
182
  * Get all agents as a readonly record
92
183
  */
@@ -102,6 +193,9 @@ var AgentRegistry = class {
102
193
  get runtimeConnectionStatus() {
103
194
  return this._runtimeConnectionStatus;
104
195
  }
196
+ get runtimeTransport() {
197
+ return this._runtimeTransport;
198
+ }
105
199
  /**
106
200
  * Initialize agents from configuration
107
201
  */
@@ -121,6 +215,13 @@ var AgentRegistry = class {
121
215
  this._runtimeUrl = normalizedRuntimeUrl;
122
216
  void this.updateRuntimeConnection();
123
217
  }
218
+ setRuntimeTransport(runtimeTransport) {
219
+ if (this._runtimeTransport === runtimeTransport) {
220
+ return;
221
+ }
222
+ this._runtimeTransport = runtimeTransport;
223
+ void this.updateRuntimeConnection();
224
+ }
124
225
  /**
125
226
  * Set all agents at once (for development use)
126
227
  */
@@ -201,20 +302,19 @@ var AgentRegistry = class {
201
302
  this._runtimeConnectionStatus = "connecting" /* Connecting */;
202
303
  await this.notifyRuntimeStatusChanged("connecting" /* Connecting */);
203
304
  try {
204
- const response = await fetch(`${this.runtimeUrl}/info`, {
205
- headers: this.core.headers
206
- });
305
+ const runtimeInfoResponse = await this.fetchRuntimeInfo();
207
306
  const {
208
307
  version,
209
308
  ...runtimeInfo
210
- } = await response.json();
309
+ } = runtimeInfoResponse;
211
310
  const agents = Object.fromEntries(
212
311
  Object.entries(runtimeInfo.agents).map(([id, { description }]) => {
213
312
  const agent = new ProxiedCopilotRuntimeAgent({
214
313
  runtimeUrl: this.runtimeUrl,
215
314
  agentId: id,
216
315
  // Runtime agents always have their ID set correctly
217
- description
316
+ description,
317
+ transport: this._runtimeTransport
218
318
  });
219
319
  this.applyHeadersToAgent(agent);
220
320
  return [id, agent];
@@ -245,6 +345,36 @@ var AgentRegistry = class {
245
345
  });
246
346
  }
247
347
  }
348
+ async fetchRuntimeInfo() {
349
+ if (!this.runtimeUrl) {
350
+ throw new Error("Runtime URL is not set");
351
+ }
352
+ const baseHeaders = this.core.headers;
353
+ const headers = {
354
+ ...baseHeaders
355
+ };
356
+ if (this._runtimeTransport === "single") {
357
+ if (!headers["Content-Type"]) {
358
+ headers["Content-Type"] = "application/json";
359
+ }
360
+ const response2 = await fetch(this.runtimeUrl, {
361
+ method: "POST",
362
+ headers,
363
+ body: JSON.stringify({ method: "info" })
364
+ });
365
+ if ("ok" in response2 && !response2.ok) {
366
+ throw new Error(`Runtime info request failed with status ${response2.status}`);
367
+ }
368
+ return await response2.json();
369
+ }
370
+ const response = await fetch(`${this.runtimeUrl}/info`, {
371
+ headers
372
+ });
373
+ if ("ok" in response && !response.ok) {
374
+ throw new Error(`Runtime info request failed with status ${response.status}`);
375
+ }
376
+ return await response.json();
377
+ }
248
378
  /**
249
379
  * Assign agent IDs to a record of agents
250
380
  */
@@ -1351,6 +1481,7 @@ var CopilotKitCore = class {
1351
1481
  stateManager;
1352
1482
  constructor({
1353
1483
  runtimeUrl,
1484
+ runtimeTransport = "rest",
1354
1485
  headers = {},
1355
1486
  properties = {},
1356
1487
  agents__unsafe_dev_only = {},
@@ -1368,6 +1499,7 @@ var CopilotKitCore = class {
1368
1499
  this.runHandler.initialize(tools);
1369
1500
  this.suggestionEngine.initialize(suggestionsConfig);
1370
1501
  this.stateManager.initialize();
1502
+ this.agentRegistry.setRuntimeTransport(runtimeTransport);
1371
1503
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
1372
1504
  this.subscribe({
1373
1505
  onAgentsChanged: ({ agents }) => {
@@ -1429,6 +1561,12 @@ var CopilotKitCore = class {
1429
1561
  setRuntimeUrl(runtimeUrl) {
1430
1562
  this.agentRegistry.setRuntimeUrl(runtimeUrl);
1431
1563
  }
1564
+ get runtimeTransport() {
1565
+ return this.agentRegistry.runtimeTransport;
1566
+ }
1567
+ setRuntimeTransport(runtimeTransport) {
1568
+ this.agentRegistry.setRuntimeTransport(runtimeTransport);
1569
+ }
1432
1570
  get runtimeVersion() {
1433
1571
  return this.agentRegistry.runtimeVersion;
1434
1572
  }