@copilotkitnext/core 1.54.1-next.4 → 1.54.1-next.6

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.umd.js CHANGED
@@ -593,9 +593,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
593
593
  function isZodError(error) {
594
594
  return error !== null && typeof error === "object" && "name" in error && error.name === "ZodError";
595
595
  }
596
+ function isAbortError(error) {
597
+ return (error instanceof DOMException || error instanceof Error) && error.name === "AbortError";
598
+ }
596
599
  function withAbortErrorHandling(observable) {
597
600
  return observable.pipe((0, rxjs_operators.catchError)((error) => {
598
- if (isZodError(error)) return rxjs.EMPTY;
601
+ if (isZodError(error) || isAbortError(error)) return rxjs.EMPTY;
599
602
  throw error;
600
603
  }));
601
604
  }
@@ -1514,6 +1517,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1514
1517
  constructor(core) {
1515
1518
  this.core = core;
1516
1519
  _defineProperty(this, "_tools", []);
1520
+ _defineProperty(this, "_runAbortController", null);
1521
+ _defineProperty(this, "_runDepth", 0);
1522
+ }
1523
+ /**
1524
+ * Abort the current run. Called by `CopilotKitCore.stopAgent()` to signal
1525
+ * that in-flight tool handlers should stop and `processAgentResult` should
1526
+ * not start a follow-up run.
1527
+ */
1528
+ abortCurrentRun() {
1529
+ var _this$_runAbortContro;
1530
+ (_this$_runAbortContro = this._runAbortController) === null || _this$_runAbortContro === void 0 || _this$_runAbortContro.abort();
1517
1531
  }
1518
1532
  /**
1519
1533
  * Typed access to CopilotKitCore's internal ("friend") methods.
@@ -1609,6 +1623,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1609
1623
  if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
1610
1624
  if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this._internal.headers };
1611
1625
  if (agent.detachActiveRun) await agent.detachActiveRun();
1626
+ const isTopLevel = this._runDepth === 0;
1627
+ let originalAbortRun;
1628
+ if (isTopLevel) {
1629
+ this._runAbortController = new AbortController();
1630
+ const controller = this._runAbortController;
1631
+ originalAbortRun = agent.abortRun.bind(agent);
1632
+ agent.abortRun = () => {
1633
+ controller.abort();
1634
+ originalAbortRun();
1635
+ };
1636
+ }
1637
+ this._runDepth++;
1612
1638
  try {
1613
1639
  const runAgentResult = await agent.runAgent({
1614
1640
  forwardedProps: {
@@ -1618,7 +1644,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1618
1644
  tools: this.buildFrontendTools(agent.agentId),
1619
1645
  context: Object.values(this._internal.context)
1620
1646
  }, this.createAgentErrorSubscriber(agent));
1621
- return this.processAgentResult({
1647
+ return await this.processAgentResult({
1622
1648
  runAgentResult,
1623
1649
  agent
1624
1650
  });
@@ -1632,12 +1658,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1632
1658
  context
1633
1659
  });
1634
1660
  return { newMessages: [] };
1661
+ } finally {
1662
+ this._runDepth--;
1663
+ if (isTopLevel && originalAbortRun) agent.abortRun = originalAbortRun;
1635
1664
  }
1636
1665
  }
1637
1666
  /**
1638
1667
  * Process agent result and execute tools
1639
1668
  */
1640
1669
  async processAgentResult({ runAgentResult, agent }) {
1670
+ var _this$_runAbortContro2;
1641
1671
  const { newMessages } = runAgentResult;
1642
1672
  const agentId = agent.agentId;
1643
1673
  let needsFollowUp = false;
@@ -1660,7 +1690,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1660
1690
  }
1661
1691
  }
1662
1692
  }
1663
- if (needsFollowUp) {
1693
+ if (needsFollowUp && !((_this$_runAbortContro2 = this._runAbortController) === null || _this$_runAbortContro2 === void 0 ? void 0 : _this$_runAbortContro2.signal.aborted)) {
1664
1694
  await this._internal.waitForPendingFrameworkUpdates();
1665
1695
  return await this.runAgent({ agent });
1666
1696
  }
@@ -1707,9 +1737,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1707
1737
  });
1708
1738
  }, "Subscriber onToolExecutionStart error:");
1709
1739
  if (!errorMessage) try {
1740
+ var _this$_runAbortContro3;
1710
1741
  const result = await tool.handler(parsedArgs, {
1711
1742
  toolCall,
1712
- agent
1743
+ agent,
1744
+ signal: (_this$_runAbortContro3 = this._runAbortController) === null || _this$_runAbortContro3 === void 0 ? void 0 : _this$_runAbortContro3.signal
1713
1745
  });
1714
1746
  if (result === void 0 || result === null) toolCallResult = "";
1715
1747
  else if (typeof result === "string") toolCallResult = result;
@@ -2482,6 +2514,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2482
2514
  return this.runHandler.connectAgent(params);
2483
2515
  }
2484
2516
  stopAgent(params) {
2517
+ this.runHandler.abortCurrentRun();
2485
2518
  params.agent.abortRun();
2486
2519
  }
2487
2520
  async runAgent(params) {
@@ -2981,15 +3014,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2981
3014
  const initialThreadState = {
2982
3015
  threads: [],
2983
3016
  isLoading: false,
3017
+ isFetchingNextPage: false,
2984
3018
  error: null,
2985
3019
  context: null,
2986
3020
  sessionId: 0,
2987
- metadataCredentialsRequested: false
3021
+ metadataCredentialsRequested: false,
3022
+ nextCursor: null
2988
3023
  };
2989
3024
  const threadAdapterEvents = createActionGroup("Thread Adapter", {
2990
3025
  started: empty(),
2991
3026
  stopped: empty(),
2992
3027
  contextChanged: props(),
3028
+ fetchNextPageRequested: empty(),
2993
3029
  renameRequested: props(),
2994
3030
  archiveRequested: props(),
2995
3031
  deleteRequested: props()
@@ -2998,6 +3034,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2998
3034
  listRequested: props(),
2999
3035
  listSucceeded: props(),
3000
3036
  listFailed: props(),
3037
+ nextPageSucceeded: props(),
3038
+ nextPageFailed: props(),
3001
3039
  metadataCredentialsRequested: props(),
3002
3040
  metadataCredentialsSucceeded: props(),
3003
3041
  metadataCredentialsFailed: props(),
@@ -3030,14 +3068,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3030
3068
  sessionId: state.sessionId + 1,
3031
3069
  threads: [],
3032
3070
  isLoading: Boolean(context),
3071
+ isFetchingNextPage: false,
3033
3072
  error: null,
3034
- metadataCredentialsRequested: false
3073
+ metadataCredentialsRequested: false,
3074
+ nextCursor: null
3035
3075
  })), on(threadAdapterEvents.stopped, (state) => ({
3036
3076
  ...state,
3037
3077
  threads: [],
3038
3078
  isLoading: false,
3079
+ isFetchingNextPage: false,
3039
3080
  error: null,
3040
- metadataCredentialsRequested: false
3081
+ metadataCredentialsRequested: false,
3082
+ nextCursor: null
3041
3083
  })), on(threadRestEvents.listRequested, (state, { sessionId }) => {
3042
3084
  if (sessionId !== state.sessionId || !state.context) return state;
3043
3085
  return {
@@ -3045,13 +3087,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3045
3087
  isLoading: true,
3046
3088
  error: null
3047
3089
  };
3048
- }), on(threadRestEvents.listSucceeded, (state, { sessionId, threads }) => {
3090
+ }), on(threadRestEvents.listSucceeded, (state, { sessionId, threads, nextCursor }) => {
3049
3091
  if (sessionId !== state.sessionId) return state;
3050
3092
  return {
3051
3093
  ...state,
3052
3094
  threads: sortThreadsByUpdatedAt(threads),
3053
3095
  isLoading: false,
3054
- error: null
3096
+ error: null,
3097
+ nextCursor
3055
3098
  };
3056
3099
  }), on(threadRestEvents.listFailed, (state, { sessionId, error }) => {
3057
3100
  if (sessionId !== state.sessionId) return state;
@@ -3060,6 +3103,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3060
3103
  isLoading: false,
3061
3104
  error
3062
3105
  };
3106
+ }), on(threadRestEvents.nextPageSucceeded, (state, { sessionId, threads, nextCursor }) => {
3107
+ if (sessionId !== state.sessionId) return state;
3108
+ let merged = state.threads;
3109
+ for (const thread of threads) merged = upsertThread(merged, thread);
3110
+ return {
3111
+ ...state,
3112
+ threads: merged,
3113
+ isFetchingNextPage: false,
3114
+ nextCursor
3115
+ };
3116
+ }), on(threadRestEvents.nextPageFailed, (state, { sessionId, error }) => {
3117
+ if (sessionId !== state.sessionId) return state;
3118
+ return {
3119
+ ...state,
3120
+ isFetchingNextPage: false,
3121
+ error
3122
+ };
3063
3123
  }), on(threadRestEvents.metadataCredentialsFailed, (state, { sessionId, error }) => {
3064
3124
  if (sessionId !== state.sessionId) return state;
3065
3125
  return {
@@ -3072,6 +3132,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3072
3132
  ...state,
3073
3133
  metadataCredentialsRequested: true
3074
3134
  };
3135
+ }), on(threadAdapterEvents.fetchNextPageRequested, (state) => {
3136
+ if (!state.nextCursor || state.isFetchingNextPage) return state;
3137
+ return {
3138
+ ...state,
3139
+ isFetchingNextPage: true
3140
+ };
3075
3141
  }), on(threadRestEvents.mutationFinished, (state, { outcome }) => ({
3076
3142
  ...state,
3077
3143
  error: outcome.ok ? state.error : outcome.error
@@ -3091,6 +3157,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3091
3157
  const selectThreads = createSelector((state) => state.threads);
3092
3158
  const selectThreadsIsLoading = createSelector((state) => state.isLoading);
3093
3159
  const selectThreadsError = createSelector((state) => state.error);
3160
+ const selectHasNextPage = createSelector((state) => state.nextCursor != null);
3161
+ const selectIsFetchingNextPage = createSelector((state) => state.isFetchingNextPage);
3094
3162
  let threadRequestId = 0;
3095
3163
  function createThreadRequestId() {
3096
3164
  threadRequestId += 1;
@@ -3098,11 +3166,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3098
3166
  }
3099
3167
  function createThreadFetchObservable(environment, context, sessionId) {
3100
3168
  return (0, rxjs.defer)(() => {
3101
- const params = new URLSearchParams({
3169
+ const params = {
3102
3170
  userId: context.userId,
3103
3171
  agentId: context.agentId
3104
- });
3105
- return (0, rxjs_fetch.fromFetch)(`${context.runtimeUrl}/threads?${params.toString()}`, {
3172
+ };
3173
+ if (context.includeArchived) params.includeArchived = "true";
3174
+ if (context.limit != null) params.limit = String(context.limit);
3175
+ const qs = new URLSearchParams(params);
3176
+ return (0, rxjs_fetch.fromFetch)(`${context.runtimeUrl}/threads?${qs.toString()}`, {
3106
3177
  selector: (response) => {
3107
3178
  if (!response.ok) throw new Error(`Failed to fetch threads: ${response.status}`);
3108
3179
  return response.json();
@@ -3115,10 +3186,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3115
3186
  with: () => {
3116
3187
  throw new Error("Request timed out");
3117
3188
  }
3118
- }), (0, rxjs_operators.map)((data) => threadRestEvents.listSucceeded({
3119
- sessionId,
3120
- threads: data.threads
3121
- })), (0, rxjs_operators.catchError)((error) => {
3189
+ }), (0, rxjs_operators.map)((data) => {
3190
+ var _data$nextCursor;
3191
+ return threadRestEvents.listSucceeded({
3192
+ sessionId,
3193
+ threads: data.threads,
3194
+ nextCursor: (_data$nextCursor = data.nextCursor) !== null && _data$nextCursor !== void 0 ? _data$nextCursor : null
3195
+ });
3196
+ }), (0, rxjs_operators.catchError)((error) => {
3122
3197
  return (0, rxjs.of)(threadRestEvents.listFailed({
3123
3198
  sessionId,
3124
3199
  error: error instanceof Error ? error : new Error(String(error))
@@ -3242,16 +3317,55 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3242
3317
  createEffect((actions$, state$) => actions$.pipe(ofType(threadSocketEvents.metadataReceived), (0, rxjs_operators.withLatestFrom)(state$), (0, rxjs_operators.filter)(([action, state]) => {
3243
3318
  var _state$context3;
3244
3319
  return action.sessionId === state.sessionId && action.payload.userId === ((_state$context3 = state.context) === null || _state$context3 === void 0 ? void 0 : _state$context3.userId);
3245
- }), (0, rxjs_operators.map)(([action]) => {
3320
+ }), (0, rxjs_operators.map)(([action, state]) => {
3321
+ var _state$context4;
3246
3322
  if (action.payload.operation === "deleted") return threadDomainEvents.threadDeleted({
3247
3323
  sessionId: action.sessionId,
3248
3324
  threadId: action.payload.deleted.id
3249
3325
  });
3326
+ if (action.payload.operation === "archived" && !((_state$context4 = state.context) === null || _state$context4 === void 0 ? void 0 : _state$context4.includeArchived)) return threadDomainEvents.threadDeleted({
3327
+ sessionId: action.sessionId,
3328
+ threadId: action.payload.threadId
3329
+ });
3250
3330
  return threadDomainEvents.threadUpserted({
3251
3331
  sessionId: action.sessionId,
3252
3332
  thread: action.payload.thread
3253
3333
  });
3254
3334
  }))),
3335
+ createEffect((actions$, state$) => actions$.pipe(ofType(threadAdapterEvents.fetchNextPageRequested), (0, rxjs_operators.withLatestFrom)(state$), (0, rxjs_operators.filter)(([, state]) => Boolean(state.context) && Boolean(state.nextCursor)), (0, rxjs_operators.switchMap)(([, state]) => {
3336
+ const context = state.context;
3337
+ const params = {
3338
+ userId: context.userId,
3339
+ agentId: context.agentId,
3340
+ cursor: state.nextCursor
3341
+ };
3342
+ if (context.includeArchived) params.includeArchived = "true";
3343
+ if (context.limit != null) params.limit = String(context.limit);
3344
+ return (0, rxjs_fetch.fromFetch)(`${context.runtimeUrl}/threads?${new URLSearchParams(params).toString()}`, {
3345
+ selector: (response) => {
3346
+ if (!response.ok) throw new Error(`Failed to fetch next page: ${response.status}`);
3347
+ return response.json();
3348
+ },
3349
+ fetch: environment.fetch,
3350
+ method: "GET",
3351
+ headers: { ...context.headers }
3352
+ }).pipe((0, rxjs_operators.timeout)({
3353
+ first: REQUEST_TIMEOUT_MS,
3354
+ with: () => {
3355
+ throw new Error("Request timed out");
3356
+ }
3357
+ }), (0, rxjs_operators.map)((data) => {
3358
+ var _data$nextCursor2;
3359
+ return threadRestEvents.nextPageSucceeded({
3360
+ sessionId: state.sessionId,
3361
+ threads: data.threads,
3362
+ nextCursor: (_data$nextCursor2 = data.nextCursor) !== null && _data$nextCursor2 !== void 0 ? _data$nextCursor2 : null
3363
+ });
3364
+ }), (0, rxjs_operators.catchError)((error) => (0, rxjs.of)(threadRestEvents.nextPageFailed({
3365
+ sessionId: state.sessionId,
3366
+ error: error instanceof Error ? error : new Error(String(error))
3367
+ }))), (0, rxjs_operators.takeUntil)(actions$.pipe(ofType(threadAdapterEvents.contextChanged, threadAdapterEvents.stopped))));
3368
+ }))),
3255
3369
  createEffect((actions$, state$) => actions$.pipe(ofType(threadAdapterEvents.renameRequested, threadAdapterEvents.archiveRequested, threadAdapterEvents.deleteRequested), (0, rxjs_operators.withLatestFrom)(state$), (0, rxjs_operators.mergeMap)(([action, state]) => {
3256
3370
  const context = state.context;
3257
3371
  if (!(context === null || context === void 0 ? void 0 : context.runtimeUrl)) {
@@ -3314,6 +3428,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3314
3428
  setContext(context) {
3315
3429
  store.dispatch(threadAdapterEvents.contextChanged({ context }));
3316
3430
  },
3431
+ fetchNextPage() {
3432
+ store.dispatch(threadAdapterEvents.fetchNextPageRequested());
3433
+ },
3317
3434
  renameThread(threadId, name) {
3318
3435
  return trackMutation(threadAdapterEvents.renameRequested({
3319
3436
  requestId: createThreadRequestId(),
@@ -3343,6 +3460,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
3343
3460
  const ɵselectThreads = selectThreads;
3344
3461
  const ɵselectThreadsIsLoading = selectThreadsIsLoading;
3345
3462
  const ɵselectThreadsError = selectThreadsError;
3463
+ const ɵselectHasNextPage = selectHasNextPage;
3464
+ const ɵselectIsFetchingNextPage = selectIsFetchingNextPage;
3346
3465
 
3347
3466
  //#endregion
3348
3467
  exports.AgentRegistry = AgentRegistry;
@@ -3377,6 +3496,8 @@ exports.ɵobservePhoenixSocketHealth$ = ɵobservePhoenixSocketHealth$;
3377
3496
  exports.ɵobservePhoenixSocketSignals$ = ɵobservePhoenixSocketSignals$;
3378
3497
  exports.ɵphoenixChannel$ = ɵphoenixChannel$;
3379
3498
  exports.ɵphoenixSocket$ = ɵphoenixSocket$;
3499
+ exports.ɵselectHasNextPage = ɵselectHasNextPage;
3500
+ exports.ɵselectIsFetchingNextPage = ɵselectIsFetchingNextPage;
3380
3501
  exports.ɵselectThreads = ɵselectThreads;
3381
3502
  exports.ɵselectThreadsError = ɵselectThreadsError;
3382
3503
  exports.ɵselectThreadsIsLoading = ɵselectThreadsIsLoading;