@copilotkit/core 1.62.2-canary.1783457132 → 1.62.3

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
@@ -543,12 +543,12 @@ function hasCredentials(agent) {
543
543
  function isZodError(error) {
544
544
  return error !== null && typeof error === "object" && "name" in error && error.name === "ZodError";
545
545
  }
546
- function isAbortError(error) {
546
+ function isAbortError$1(error) {
547
547
  return (error instanceof DOMException || error instanceof Error) && error.name === "AbortError";
548
548
  }
549
549
  function withAbortErrorHandling(observable) {
550
550
  return observable.pipe(catchError((error) => {
551
- if (isZodError(error) || isAbortError(error)) return EMPTY;
551
+ if (isZodError(error) || isAbortError$1(error)) return EMPTY;
552
552
  throw error;
553
553
  }));
554
554
  }
@@ -896,6 +896,7 @@ var AgentRegistry = class {
896
896
  _runtimeMode = RUNTIME_MODE_SSE;
897
897
  _intelligence;
898
898
  _threadEndpoints;
899
+ _suggestions;
899
900
  _a2uiEnabled = false;
900
901
  _a2uiAgents;
901
902
  _openGenerativeUIEnabled = false;
@@ -945,6 +946,9 @@ var AgentRegistry = class {
945
946
  get threadEndpoints() {
946
947
  return this._threadEndpoints;
947
948
  }
949
+ get suggestions() {
950
+ return this._suggestions;
951
+ }
948
952
  get a2uiEnabled() {
949
953
  return this._a2uiEnabled;
950
954
  }
@@ -1132,6 +1136,7 @@ var AgentRegistry = class {
1132
1136
  this._runtimeMode = RUNTIME_MODE_SSE;
1133
1137
  this._intelligence = void 0;
1134
1138
  this._threadEndpoints = void 0;
1139
+ this._suggestions = void 0;
1135
1140
  this._a2uiEnabled = false;
1136
1141
  this._a2uiAgents = void 0;
1137
1142
  this._openGenerativeUIEnabled = false;
@@ -1179,6 +1184,7 @@ var AgentRegistry = class {
1179
1184
  this._runtimeMode = runtimeInfoResponse.mode ?? RUNTIME_MODE_SSE;
1180
1185
  this._intelligence = runtimeInfoResponse.intelligence;
1181
1186
  this._threadEndpoints = runtimeInfoResponse.threadEndpoints;
1187
+ this._suggestions = runtimeInfoResponse.suggestions;
1182
1188
  const a2uiInfo = runtimeInfoResponse.a2ui;
1183
1189
  this._a2uiEnabled = a2uiInfo?.enabled ?? runtimeInfoResponse.a2uiEnabled ?? false;
1184
1190
  this._a2uiAgents = a2uiInfo?.enabled ? a2uiInfo.agents : void 0;
@@ -1194,6 +1200,7 @@ var AgentRegistry = class {
1194
1200
  this._runtimeMode = RUNTIME_MODE_SSE;
1195
1201
  this._intelligence = void 0;
1196
1202
  this._threadEndpoints = void 0;
1203
+ this._suggestions = void 0;
1197
1204
  this._a2uiEnabled = false;
1198
1205
  this._a2uiAgents = void 0;
1199
1206
  this._openGenerativeUIEnabled = false;
@@ -1433,38 +1440,81 @@ var SuggestionEngine = class {
1433
1440
  };
1434
1441
  }
1435
1442
  /**
1436
- * Generate suggestions using a provider agent
1443
+ * Generate suggestions by running a provider agent and extracting the
1444
+ * `copilotkitSuggest` tool call from its streamed messages.
1445
+ *
1446
+ * Two transports, one set of mechanics: both seed an agent with the
1447
+ * consumer's messages + state and the instruction, then `runAgent` with
1448
+ * forced `copilotkitSuggest` tool choice, parsing suggestions as messages
1449
+ * stream in via `onMessagesChanged`.
1450
+ *
1451
+ * - **Stateless** (runtime advertises `suggestions` and transport isn't
1452
+ * `single`): a stock `HttpAgent` pointed at `/agent/:id/suggest`. That
1453
+ * endpoint runs the provider directly and streams AG-UI SSE **without**
1454
+ * persisting a thread, and a plain `HttpAgent` only ever speaks REST SSE —
1455
+ * it never routes through the Intelligence websocket delegate (which would
1456
+ * persist a thread). This is the path that removes the thread flood.
1457
+ * - **Fallback** (capability absent → old runtime, or `single` transport):
1458
+ * clone the provider agent and run it client-side, as before.
1459
+ *
1460
+ * Gated to non-`single` transports because this client only builds the
1461
+ * multi-route `/agent/:id/suggest` URL; single-route deployments (not the
1462
+ * persisting-thread/Intelligence case) fall through to the clone fallback.
1437
1463
  */
1438
1464
  async generateSuggestions(suggestionId, config, consumerAgentId) {
1439
- let agent = void 0;
1465
+ let runHandle = void 0;
1466
+ let aborted = false;
1440
1467
  try {
1441
- const suggestionsProviderAgent = this.core.getAgent(config.providerAgentId ?? "default");
1442
- if (!suggestionsProviderAgent) throw new Error(`Suggestions provider agent not found: ${config.providerAgentId}`);
1443
- const suggestionsConsumerAgent = this.core.getAgent(consumerAgentId);
1468
+ const friends = this.core;
1469
+ const resolvedProviderAgentId = config.providerAgentId ?? "default";
1470
+ const suggestionsProviderAgent = friends.getAgent(resolvedProviderAgentId);
1471
+ if (!suggestionsProviderAgent) throw new Error(`Suggestions provider agent not found: ${resolvedProviderAgentId}`);
1472
+ const suggestionsConsumerAgent = friends.getAgent(consumerAgentId);
1444
1473
  if (!suggestionsConsumerAgent) throw new Error(`Suggestions consumer agent not found: ${consumerAgentId}`);
1445
- agent = suggestionsProviderAgent.clone();
1446
- agent.threadId = suggestionId;
1447
- agent.messages = JSON.parse(JSON.stringify(suggestionsConsumerAgent.messages));
1448
- agent.state = JSON.parse(JSON.stringify(suggestionsConsumerAgent.state));
1474
+ const instructionContent = [
1475
+ `Suggest what the user could say next. Provide clear, highly relevant suggestions by calling the \`copilotkitSuggest\` tool.`,
1476
+ `Provide at least ${config.minSuggestions ?? 1} and at most ${config.maxSuggestions ?? 3} suggestions.`,
1477
+ `The user has the following tools available: ${JSON.stringify(friends.buildFrontendTools(consumerAgentId))}.`,
1478
+ ` ${config.instructions}`
1479
+ ].join("\n");
1449
1480
  this._suggestions[consumerAgentId] = {
1450
1481
  ...this._suggestions[consumerAgentId],
1451
1482
  [suggestionId]: []
1452
1483
  };
1453
- this._runningSuggestions[consumerAgentId] = [...this._runningSuggestions[consumerAgentId] ?? [], agent];
1454
- agent.addMessage({
1484
+ const seededMessages = JSON.parse(JSON.stringify(suggestionsConsumerAgent.messages));
1485
+ const seededState = JSON.parse(JSON.stringify(suggestionsConsumerAgent.state ?? {}));
1486
+ const useStateless = this.core.suggestions === true && this.core.runtimeTransport !== "single";
1487
+ let suggestionAgent;
1488
+ if (useStateless) {
1489
+ const suggestUrl = `${this.core.runtimeUrl}/agent/${encodeURIComponent(resolvedProviderAgentId)}/suggest`;
1490
+ const credentials = this.core.credentials;
1491
+ suggestionAgent = new HttpAgent({
1492
+ agentId: resolvedProviderAgentId,
1493
+ url: suggestUrl,
1494
+ headers: { ...this.core.headers },
1495
+ ...credentials ? { fetch: (url, requestInit) => fetch(url, {
1496
+ ...requestInit,
1497
+ credentials
1498
+ }) } : {}
1499
+ });
1500
+ } else suggestionAgent = suggestionsProviderAgent.clone();
1501
+ suggestionAgent.threadId = suggestionId;
1502
+ suggestionAgent.setMessages(seededMessages);
1503
+ suggestionAgent.setState(seededState);
1504
+ runHandle = { abortRun: () => {
1505
+ aborted = true;
1506
+ suggestionAgent.abortRun();
1507
+ } };
1508
+ this._runningSuggestions[consumerAgentId] = [...this._runningSuggestions[consumerAgentId] ?? [], runHandle];
1509
+ suggestionAgent.addMessage({
1455
1510
  id: suggestionId,
1456
1511
  role: "user",
1457
- content: [
1458
- `Suggest what the user could say next. Provide clear, highly relevant suggestions by calling the \`copilotkitSuggest\` tool.`,
1459
- `Provide at least ${config.minSuggestions ?? 1} and at most ${config.maxSuggestions ?? 3} suggestions.`,
1460
- `The user has the following tools available: ${JSON.stringify(this.core.buildFrontendTools(consumerAgentId))}.`,
1461
- ` ${config.instructions}`
1462
- ].join("\n")
1512
+ content: instructionContent
1463
1513
  });
1464
- await agent.runAgent({
1465
- context: this.core.getContextForAgent(consumerAgentId),
1514
+ await suggestionAgent.runAgent({
1515
+ context: friends.getContextForAgent(consumerAgentId),
1466
1516
  forwardedProps: {
1467
- ...this.core.properties,
1517
+ ...friends.properties,
1468
1518
  toolChoice: {
1469
1519
  type: "function",
1470
1520
  function: { name: "copilotkitSuggest" }
@@ -1475,18 +1525,15 @@ var SuggestionEngine = class {
1475
1525
  this.extractSuggestions(messages, suggestionId, consumerAgentId, true);
1476
1526
  } });
1477
1527
  } catch (error) {
1478
- console.warn("Error generating suggestions:", error);
1528
+ if (!isAbortError(error) && !aborted) console.warn("Error generating suggestions:", error);
1479
1529
  } finally {
1480
1530
  this.finalizeSuggestions(suggestionId, consumerAgentId);
1481
1531
  const runningAgents = this._runningSuggestions[consumerAgentId];
1482
- if (agent && runningAgents) {
1483
- const filteredAgents = runningAgents.filter((a) => a !== agent);
1484
- this._runningSuggestions[consumerAgentId] = filteredAgents;
1485
- if (filteredAgents.length === 0) {
1486
- delete this._runningSuggestions[consumerAgentId];
1487
- await this.notifySuggestionsFinishedLoading(consumerAgentId);
1488
- }
1489
- }
1532
+ const remaining = runHandle ? (runningAgents ?? []).filter((a) => a !== runHandle) : runningAgents ?? [];
1533
+ if (remaining.length === 0) {
1534
+ delete this._runningSuggestions[consumerAgentId];
1535
+ await this.notifySuggestionsFinishedLoading(consumerAgentId);
1536
+ } else this._runningSuggestions[consumerAgentId] = remaining;
1490
1537
  }
1491
1538
  }
1492
1539
  /**
@@ -1606,6 +1653,13 @@ var SuggestionEngine = class {
1606
1653
  }
1607
1654
  };
1608
1655
  /**
1656
+ * Detects an `AbortController`-driven cancellation so the stateless path can
1657
+ * treat it as an expected (non-error) outcome on clear/reload.
1658
+ */
1659
+ function isAbortError(error) {
1660
+ return typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
1661
+ }
1662
+ /**
1609
1663
  * Type guard for dynamic suggestions config
1610
1664
  */
1611
1665
  function isDynamicSuggestionsConfig(config) {
@@ -3786,6 +3840,9 @@ var CopilotKitCore = class {
3786
3840
  get threadEndpoints() {
3787
3841
  return this.agentRegistry.threadEndpoints;
3788
3842
  }
3843
+ get suggestions() {
3844
+ return this.agentRegistry.suggestions;
3845
+ }
3789
3846
  get a2uiEnabled() {
3790
3847
  return this.agentRegistry.a2uiEnabled;
3791
3848
  }
@@ -4408,6 +4465,7 @@ const initialThreadState = {
4408
4465
  isLoading: false,
4409
4466
  isFetchingNextPage: false,
4410
4467
  error: null,
4468
+ fetchMoreError: null,
4411
4469
  context: null,
4412
4470
  sessionId: 0,
4413
4471
  metadataCredentialsRequested: false,
@@ -4493,6 +4551,7 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4493
4551
  isLoading: Boolean(context),
4494
4552
  isFetchingNextPage: false,
4495
4553
  error: null,
4554
+ fetchMoreError: null,
4496
4555
  metadataCredentialsRequested: false,
4497
4556
  metadataJoinCode: null,
4498
4557
  nextCursor: null,
@@ -4504,6 +4563,7 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4504
4563
  isLoading: false,
4505
4564
  isFetchingNextPage: false,
4506
4565
  error: null,
4566
+ fetchMoreError: null,
4507
4567
  metadataCredentialsRequested: false,
4508
4568
  metadataJoinCode: null,
4509
4569
  nextCursor: null,
@@ -4514,7 +4574,8 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4514
4574
  return {
4515
4575
  ...state,
4516
4576
  isLoading: true,
4517
- error: null
4577
+ error: null,
4578
+ fetchMoreError: null
4518
4579
  };
4519
4580
  }), on(threadRestEvents.listSucceeded, (state, { sessionId, threads, joinCode, nextCursor }) => {
4520
4581
  if (sessionId !== state.sessionId) return state;
@@ -4524,6 +4585,7 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4524
4585
  threads: sortThreadsByRecency(threads),
4525
4586
  isLoading: false,
4526
4587
  error: null,
4588
+ fetchMoreError: null,
4527
4589
  metadataJoinCode: joinCode,
4528
4590
  metadataCredentialsRequested: joinCodeChanged ? false : state.metadataCredentialsRequested,
4529
4591
  nextCursor
@@ -4543,6 +4605,7 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4543
4605
  ...state,
4544
4606
  threads: merged,
4545
4607
  isFetchingNextPage: false,
4608
+ fetchMoreError: null,
4546
4609
  nextCursor
4547
4610
  };
4548
4611
  }), on(threadRestEvents.nextPageFailed, (state, { sessionId, error }) => {
@@ -4550,7 +4613,7 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4550
4613
  return {
4551
4614
  ...state,
4552
4615
  isFetchingNextPage: false,
4553
- error
4616
+ fetchMoreError: error
4554
4617
  };
4555
4618
  }), on(threadRestEvents.metadataCredentialsFailed, (state, { sessionId }) => {
4556
4619
  if (sessionId !== state.sessionId) return state;
@@ -4568,7 +4631,8 @@ const threadReducer = createReducer(initialThreadState, on(threadAdapterEvents.c
4568
4631
  if (!state.nextCursor || state.isFetchingNextPage) return state;
4569
4632
  return {
4570
4633
  ...state,
4571
- isFetchingNextPage: true
4634
+ isFetchingNextPage: true,
4635
+ fetchMoreError: null
4572
4636
  };
4573
4637
  }), on(threadAdapterEvents.renameRequested, (state, { threadId, name }) => {
4574
4638
  const existing = state.threads.find((thread) => thread.id === threadId);
@@ -4697,6 +4761,7 @@ function createThreadSelectors() {
4697
4761
  threads: createSelector((state) => state.threads),
4698
4762
  isLoading: createSelector((state) => state.isLoading),
4699
4763
  error: createSelector((state) => state.error),
4764
+ fetchMoreError: createSelector((state) => state.fetchMoreError),
4700
4765
  hasNextPage: createSelector((state) => state.nextCursor != null),
4701
4766
  isFetchingNextPage: createSelector((state) => state.isFetchingNextPage),
4702
4767
  isMutating: createSelector((state) => state.inFlightMutationCount > 0)
@@ -4706,6 +4771,7 @@ const standaloneSelectors = createThreadSelectors();
4706
4771
  const selectThreads = standaloneSelectors.threads;
4707
4772
  const selectThreadsIsLoading = standaloneSelectors.isLoading;
4708
4773
  const selectThreadsError = standaloneSelectors.error;
4774
+ const selectFetchMoreError = standaloneSelectors.fetchMoreError;
4709
4775
  const selectHasNextPage = standaloneSelectors.hasNextPage;
4710
4776
  const selectIsFetchingNextPage = standaloneSelectors.isFetchingNextPage;
4711
4777
  const selectIsMutating = standaloneSelectors.isMutating;
@@ -5079,6 +5145,7 @@ const ɵcreateThreadSelectors = createThreadSelectors;
5079
5145
  const ɵselectThreads = selectThreads;
5080
5146
  const ɵselectThreadsIsLoading = selectThreadsIsLoading;
5081
5147
  const ɵselectThreadsError = selectThreadsError;
5148
+ const ɵselectFetchMoreError = selectFetchMoreError;
5082
5149
  const ɵselectHasNextPage = selectHasNextPage;
5083
5150
  const ɵselectIsFetchingNextPage = selectIsFetchingNextPage;
5084
5151
  const ɵselectIsMutating = selectIsMutating;
@@ -5136,5 +5203,5 @@ function ɵisCopilotKitFeature(name) {
5136
5203
  }
5137
5204
 
5138
5205
  //#endregion
5139
- export { AgentRegistry, AgentThreadLockedError, ContextStore, CopilotKitCore, CopilotKitCoreErrorCode, CopilotKitCoreRuntimeConnectionStatus, IntelligenceAgent, MEMORY_ERROR_REGISTRY, MemoryError, ProxiedCopilotRuntimeAgent, RunHandler, StateManager, SuggestionEngine, ToolCallStatus, completePartialMarkdown, createActionGroup, createEffect, createReducer, createSelector, createStore, empty, ensureObjectArgs, isRunCompletionAware, ofType, on, parseToolArguments, props, select, ɵCOPILOTKIT_FEATURES, ɵMAX_SOCKET_RETRIES, createMemoryStore as ɵcreateMemoryStore, ɵcreateThreadSelectors, createThreadStore as ɵcreateThreadStore, ɵisCopilotKitFeature, isRetryableStatus as ɵisRetryableMemoryStatus, ɵjoinPhoenixChannel$, ɵmapMemoryMetadataEvent, ɵmemoryAdapterEvents, ɵmemoryDomainEvents, ɵmemoryReducer, ɵmemoryRestEvents, ɵobservePhoenixEvent$, ɵobservePhoenixJoinOutcome$, ɵobservePhoenixSocketHealth$, ɵobservePhoenixSocketSignals$, ɵphoenixChannel$, ɵphoenixSocket$, ɵselectHasNextPage, ɵselectIsFetchingNextPage, ɵselectIsMutating, ɵselectMemories, ɵselectMemoriesAvailable, ɵselectMemoriesError, ɵselectMemoriesIsLoading, ɵselectMemoriesIsMutating, ɵselectMemoriesRealtimeStatus, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading, ɵthreadAdapterEvents };
5206
+ export { AgentRegistry, AgentThreadLockedError, ContextStore, CopilotKitCore, CopilotKitCoreErrorCode, CopilotKitCoreRuntimeConnectionStatus, IntelligenceAgent, MEMORY_ERROR_REGISTRY, MemoryError, ProxiedCopilotRuntimeAgent, RunHandler, StateManager, SuggestionEngine, ToolCallStatus, completePartialMarkdown, createActionGroup, createEffect, createReducer, createSelector, createStore, empty, ensureObjectArgs, isAbortError, isRunCompletionAware, ofType, on, parseToolArguments, props, select, ɵCOPILOTKIT_FEATURES, ɵMAX_SOCKET_RETRIES, createMemoryStore as ɵcreateMemoryStore, ɵcreateThreadSelectors, createThreadStore as ɵcreateThreadStore, ɵisCopilotKitFeature, isRetryableStatus as ɵisRetryableMemoryStatus, ɵjoinPhoenixChannel$, ɵmapMemoryMetadataEvent, ɵmemoryAdapterEvents, ɵmemoryDomainEvents, ɵmemoryReducer, ɵmemoryRestEvents, ɵobservePhoenixEvent$, ɵobservePhoenixJoinOutcome$, ɵobservePhoenixSocketHealth$, ɵobservePhoenixSocketSignals$, ɵphoenixChannel$, ɵphoenixSocket$, ɵselectFetchMoreError, ɵselectHasNextPage, ɵselectIsFetchingNextPage, ɵselectIsMutating, ɵselectMemories, ɵselectMemoriesAvailable, ɵselectMemoriesError, ɵselectMemoriesIsLoading, ɵselectMemoriesIsMutating, ɵselectMemoriesRealtimeStatus, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading, ɵthreadAdapterEvents };
5140
5207
  //# sourceMappingURL=index.mjs.map