@copilotkitnext/react 1.54.1-next.5 → 1.54.1

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.
Files changed (39) hide show
  1. package/dist/components/chat/CopilotChat.cjs +3 -2
  2. package/dist/components/chat/CopilotChat.cjs.map +1 -1
  3. package/dist/components/chat/CopilotChat.d.cts +2 -0
  4. package/dist/components/chat/CopilotChat.d.cts.map +1 -1
  5. package/dist/components/chat/CopilotChat.d.mts +2 -0
  6. package/dist/components/chat/CopilotChat.d.mts.map +1 -1
  7. package/dist/components/chat/CopilotChat.mjs +3 -2
  8. package/dist/components/chat/CopilotChat.mjs.map +1 -1
  9. package/dist/components/chat/CopilotPopup.cjs +1 -0
  10. package/dist/components/chat/CopilotPopup.cjs.map +1 -1
  11. package/dist/components/chat/CopilotPopup.mjs +1 -0
  12. package/dist/components/chat/CopilotPopup.mjs.map +1 -1
  13. package/dist/components/chat/CopilotSidebar.cjs +1 -0
  14. package/dist/components/chat/CopilotSidebar.cjs.map +1 -1
  15. package/dist/components/chat/CopilotSidebar.mjs +1 -0
  16. package/dist/components/chat/CopilotSidebar.mjs.map +1 -1
  17. package/dist/hooks/use-interrupt.cjs +6 -1
  18. package/dist/hooks/use-interrupt.cjs.map +1 -1
  19. package/dist/hooks/use-interrupt.d.cts.map +1 -1
  20. package/dist/hooks/use-interrupt.d.mts.map +1 -1
  21. package/dist/hooks/use-interrupt.mjs +7 -2
  22. package/dist/hooks/use-interrupt.mjs.map +1 -1
  23. package/dist/hooks/use-threads.cjs +24 -15
  24. package/dist/hooks/use-threads.cjs.map +1 -1
  25. package/dist/hooks/use-threads.d.cts +32 -16
  26. package/dist/hooks/use-threads.d.cts.map +1 -1
  27. package/dist/hooks/use-threads.d.mts +32 -16
  28. package/dist/hooks/use-threads.d.mts.map +1 -1
  29. package/dist/hooks/use-threads.mjs +25 -16
  30. package/dist/hooks/use-threads.mjs.map +1 -1
  31. package/dist/index.umd.js +53 -20
  32. package/dist/index.umd.js.map +1 -1
  33. package/dist/providers/CopilotChatConfigurationProvider.cjs +17 -2
  34. package/dist/providers/CopilotChatConfigurationProvider.cjs.map +1 -1
  35. package/dist/providers/CopilotChatConfigurationProvider.d.cts.map +1 -1
  36. package/dist/providers/CopilotChatConfigurationProvider.d.mts.map +1 -1
  37. package/dist/providers/CopilotChatConfigurationProvider.mjs +18 -3
  38. package/dist/providers/CopilotChatConfigurationProvider.mjs.map +1 -1
  39. package/package.json +7 -7
@@ -13,17 +13,17 @@ function useThreadStoreSelector(store, selector) {
13
13
  /**
14
14
  * React hook for listing and managing Intelligence platform threads.
15
15
  *
16
- * On mount the hook fetches the thread list for the given `userId`/`agentId`
17
- * pair. When the Intelligence platform exposes a WebSocket URL, it also opens
18
- * a realtime subscription so the `threads` array stays current without
19
- * polling — thread creates, renames, archives, and deletes from any client
20
- * are reflected immediately.
16
+ * On mount the hook fetches the thread list for the runtime-authenticated user
17
+ * and the given `agentId`. When the Intelligence platform exposes a WebSocket
18
+ * URL, it also opens a realtime subscription so the `threads` array stays
19
+ * current without polling — thread creates, renames, archives, and deletes
20
+ * from any client are reflected immediately.
21
21
  *
22
22
  * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return
23
23
  * promises that resolve once the platform confirms the operation and reject
24
24
  * with an `Error` on failure.
25
25
  *
26
- * @param input - User and agent identifiers that scope the thread list.
26
+ * @param input - Agent identifier and optional list controls.
27
27
  * @returns Thread list state and stable mutation callbacks.
28
28
  *
29
29
  * @example
@@ -32,7 +32,6 @@ function useThreadStoreSelector(store, selector) {
32
32
  *
33
33
  * function ThreadList() {
34
34
  * const { threads, isLoading, renameThread, deleteThread } = useThreads({
35
- * userId: "user-1",
36
35
  * agentId: "agent-1",
37
36
  * });
38
37
  *
@@ -52,12 +51,14 @@ function useThreadStoreSelector(store, selector) {
52
51
  * }
53
52
  * ```
54
53
  */
55
- function useThreads({ userId, agentId }) {
54
+ function useThreads({ agentId, includeArchived, limit }) {
56
55
  const { copilotkit } = require_CopilotKitProvider.useCopilotKit();
57
56
  const [store] = (0, react.useState)(() => (0, _copilotkitnext_core.ɵcreateThreadStore)({ fetch: globalThis.fetch }));
58
57
  const threads = useThreadStoreSelector(store, _copilotkitnext_core.ɵselectThreads);
59
58
  const storeIsLoading = useThreadStoreSelector(store, _copilotkitnext_core.ɵselectThreadsIsLoading);
60
59
  const storeError = useThreadStoreSelector(store, _copilotkitnext_core.ɵselectThreadsError);
60
+ const hasNextPage = useThreadStoreSelector(store, _copilotkitnext_core.ɵselectHasNextPage);
61
+ const isFetchingNextPage = useThreadStoreSelector(store, _copilotkitnext_core.ɵselectIsFetchingNextPage);
61
62
  const headersKey = (0, react.useMemo)(() => {
62
63
  return JSON.stringify(Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) => left.localeCompare(right)));
63
64
  }, [copilotkit.headers]);
@@ -78,8 +79,9 @@ function useThreads({ userId, agentId }) {
78
79
  runtimeUrl: copilotkit.runtimeUrl,
79
80
  headers: { ...copilotkit.headers },
80
81
  wsUrl: copilotkit.intelligence?.wsUrl,
81
- userId,
82
- agentId
82
+ agentId,
83
+ includeArchived,
84
+ limit
83
85
  } : null;
84
86
  store.setContext(context);
85
87
  }, [
@@ -87,17 +89,24 @@ function useThreads({ userId, agentId }) {
87
89
  copilotkit.runtimeUrl,
88
90
  headersKey,
89
91
  copilotkit.intelligence?.wsUrl,
90
- userId,
91
92
  agentId,
92
- copilotkit.headers
93
+ copilotkit.headers,
94
+ includeArchived,
95
+ limit
93
96
  ]);
97
+ const renameThread = (0, react.useCallback)((threadId, name) => store.renameThread(threadId, name), [store]);
98
+ const archiveThread = (0, react.useCallback)((threadId) => store.archiveThread(threadId), [store]);
99
+ const deleteThread = (0, react.useCallback)((threadId) => store.deleteThread(threadId), [store]);
94
100
  return {
95
101
  threads,
96
102
  isLoading,
97
103
  error,
98
- renameThread: (0, react.useCallback)((threadId, name) => store.renameThread(threadId, name), [store]),
99
- archiveThread: (0, react.useCallback)((threadId) => store.archiveThread(threadId), [store]),
100
- deleteThread: (0, react.useCallback)((threadId) => store.deleteThread(threadId), [store])
104
+ hasNextPage,
105
+ isFetchingNextPage,
106
+ fetchNextPage: (0, react.useCallback)(() => store.fetchNextPage(), [store]),
107
+ renameThread,
108
+ archiveThread,
109
+ deleteThread
101
110
  };
102
111
  }
103
112
 
@@ -1 +1 @@
1
- {"version":3,"file":"use-threads.cjs","names":["useCopilotKit","ɵselectThreads","ɵselectThreadsIsLoading","ɵselectThreadsError"],"sources":["../../src/hooks/use-threads.tsx"],"sourcesContent":["import { useCopilotKit } from \"@/providers/CopilotKitProvider\";\nimport {\n ɵcreateThreadStore,\n ɵselectThreads,\n ɵselectThreadsError,\n ɵselectThreadsIsLoading,\n type ɵThread as CoreThread,\n type ɵThreadRuntimeContext,\n type ɵThreadStore,\n} from \"@copilotkitnext/core\";\nimport {\n useCallback,\n useEffect,\n useMemo,\n useState,\n useSyncExternalStore,\n} from \"react\";\n\n/**\n * A conversation thread managed by the Intelligence platform.\n *\n * Each thread has a unique `id`, an optional human-readable `name`, and\n * timestamp fields tracking creation and update times.\n */\nexport interface Thread extends CoreThread {}\n\n/**\n * Configuration for the {@link useThreads} hook.\n *\n * Both fields are required they scope the thread list and all mutations\n * to a specific user/agent pair on the Intelligence platform.\n */\nexport interface UseThreadsInput {\n /** The ID of the current user. Thread queries and mutations are scoped to this user. */\n userId: string;\n /** The ID of the agent whose threads to list and manage. */\n agentId: string;\n}\n\n/**\n * Return value of the {@link useThreads} hook.\n *\n * The `threads` array is kept in sync with the platform via a realtime\n * WebSocket subscription (when available) and is sorted most-recently-updated\n * first. Mutations reject with an `Error` if the platform request fails.\n */\nexport interface UseThreadsResult {\n /**\n * All non-archived threads for the current user/agent pair, sorted by\n * most recently updated first. Updated in realtime when the platform\n * pushes metadata events.\n */\n threads: Thread[];\n /**\n * `true` while the initial thread list is being fetched from the platform.\n * Subsequent realtime updates do not re-enter the loading state.\n */\n isLoading: boolean;\n /**\n * The most recent error from fetching threads or executing a mutation,\n * or `null` when there is no error. Reset to `null` on the next\n * successful fetch.\n */\n error: Error | null;\n /**\n * Rename a thread on the platform.\n * Resolves when the server confirms the update; rejects on failure.\n */\n renameThread: (threadId: string, name: string) => Promise<void>;\n /**\n * Archive a thread on the platform.\n * Archived threads are excluded from subsequent list results.\n * Resolves when the server confirms the update; rejects on failure.\n */\n archiveThread: (threadId: string) => Promise<void>;\n /**\n * Permanently delete a thread from the platform.\n * This is irreversible. Resolves when the server confirms deletion;\n * rejects on failure.\n */\n deleteThread: (threadId: string) => Promise<void>;\n}\n\nfunction useThreadStoreSelector<T>(\n store: ɵThreadStore,\n selector: (state: ReturnType<ɵThreadStore[\"getState\"]>) => T,\n): T {\n return useSyncExternalStore(\n useCallback(\n (onStoreChange) => {\n const subscription = store.select(selector).subscribe(onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, selector],\n ),\n () => selector(store.getState()),\n );\n}\n\n/**\n * React hook for listing and managing Intelligence platform threads.\n *\n * On mount the hook fetches the thread list for the given `userId`/`agentId`\n * pair. When the Intelligence platform exposes a WebSocket URL, it also opens\n * a realtime subscription so the `threads` array stays current without\n * polling — thread creates, renames, archives, and deletes from any client\n * are reflected immediately.\n *\n * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return\n * promises that resolve once the platform confirms the operation and reject\n * with an `Error` on failure.\n *\n * @param input - User and agent identifiers that scope the thread list.\n * @returns Thread list state and stable mutation callbacks.\n *\n * @example\n * ```tsx\n * import { useThreads } from \"@copilotkitnext/react\";\n *\n * function ThreadList() {\n * const { threads, isLoading, renameThread, deleteThread } = useThreads({\n * userId: \"user-1\",\n * agentId: \"agent-1\",\n * });\n *\n * if (isLoading) return <p>Loading…</p>;\n *\n * return (\n * <ul>\n * {threads.map((t) => (\n * <li key={t.id}>\n * {t.name ?? \"Untitled\"}\n * <button onClick={() => renameThread(t.id, \"New name\")}>Rename</button>\n * <button onClick={() => deleteThread(t.id)}>Delete</button>\n * </li>\n * ))}\n * </ul>\n * );\n * }\n * ```\n */\nexport function useThreads({\n userId,\n agentId,\n}: UseThreadsInput): UseThreadsResult {\n const { copilotkit } = useCopilotKit();\n\n const [store] = useState(() =>\n ɵcreateThreadStore({\n fetch: globalThis.fetch,\n }),\n );\n\n const threads = useThreadStoreSelector(store, ɵselectThreads);\n const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);\n const storeError = useThreadStoreSelector(store, ɵselectThreadsError);\n const headersKey = useMemo(() => {\n return JSON.stringify(\n Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) =>\n left.localeCompare(right),\n ),\n );\n }, [copilotkit.headers]);\n const runtimeError = useMemo(() => {\n if (copilotkit.runtimeUrl) {\n return null;\n }\n\n return new Error(\"Runtime URL is not configured\");\n }, [copilotkit.runtimeUrl]);\n const isLoading = runtimeError ? false : storeIsLoading;\n const error = runtimeError ?? storeError;\n\n useEffect(() => {\n store.start();\n return () => {\n store.stop();\n };\n }, [store]);\n\n useEffect(() => {\n const context: ɵThreadRuntimeContext | null = copilotkit.runtimeUrl\n ? {\n runtimeUrl: copilotkit.runtimeUrl,\n headers: { ...copilotkit.headers },\n wsUrl: copilotkit.intelligence?.wsUrl,\n userId,\n agentId,\n }\n : null;\n\n store.setContext(context);\n }, [\n store,\n copilotkit.runtimeUrl,\n headersKey,\n copilotkit.intelligence?.wsUrl,\n userId,\n agentId,\n copilotkit.headers,\n ]);\n\n const renameThread = useCallback(\n (threadId: string, name: string) => store.renameThread(threadId, name),\n [store],\n );\n\n const archiveThread = useCallback(\n (threadId: string) => store.archiveThread(threadId),\n [store],\n );\n\n const deleteThread = useCallback(\n (threadId: string) => store.deleteThread(threadId),\n [store],\n );\n\n return {\n threads,\n isLoading,\n error,\n renameThread,\n archiveThread,\n deleteThread,\n };\n}\n"],"mappings":";;;;;;AAmFA,SAAS,uBACP,OACA,UACG;AACH,gEAEK,kBAAkB;EACjB,MAAM,eAAe,MAAM,OAAO,SAAS,CAAC,UAAU,cAAc;AACpE,eAAa,aAAa,aAAa;IAEzC,CAAC,OAAO,SAAS,CAClB,QACK,SAAS,MAAM,UAAU,CAAC,CACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CH,SAAgB,WAAW,EACzB,QACA,WACoC;CACpC,MAAM,EAAE,eAAeA,0CAAe;CAEtC,MAAM,CAAC,gFACc,EACjB,OAAO,WAAW,OACnB,CAAC,CACH;CAED,MAAM,UAAU,uBAAuB,OAAOC,oCAAe;CAC7D,MAAM,iBAAiB,uBAAuB,OAAOC,6CAAwB;CAC7E,MAAM,aAAa,uBAAuB,OAAOC,yCAAoB;CACrE,MAAM,sCAA2B;AAC/B,SAAO,KAAK,UACV,OAAO,QAAQ,WAAW,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,WACtD,KAAK,cAAc,MAAM,CAC1B,CACF;IACA,CAAC,WAAW,QAAQ,CAAC;CACxB,MAAM,wCAA6B;AACjC,MAAI,WAAW,WACb,QAAO;AAGT,yBAAO,IAAI,MAAM,gCAAgC;IAChD,CAAC,WAAW,WAAW,CAAC;CAC3B,MAAM,YAAY,eAAe,QAAQ;CACzC,MAAM,QAAQ,gBAAgB;AAE9B,4BAAgB;AACd,QAAM,OAAO;AACb,eAAa;AACX,SAAM,MAAM;;IAEb,CAAC,MAAM,CAAC;AAEX,4BAAgB;EACd,MAAM,UAAwC,WAAW,aACrD;GACE,YAAY,WAAW;GACvB,SAAS,EAAE,GAAG,WAAW,SAAS;GAClC,OAAO,WAAW,cAAc;GAChC;GACA;GACD,GACD;AAEJ,QAAM,WAAW,QAAQ;IACxB;EACD;EACA,WAAW;EACX;EACA,WAAW,cAAc;EACzB;EACA;EACA,WAAW;EACZ,CAAC;AAiBF,QAAO;EACL;EACA;EACA;EACA,sCAlBC,UAAkB,SAAiB,MAAM,aAAa,UAAU,KAAK,EACtE,CAAC,MAAM,CACR;EAiBC,uCAdC,aAAqB,MAAM,cAAc,SAAS,EACnD,CAAC,MAAM,CACR;EAaC,sCAVC,aAAqB,MAAM,aAAa,SAAS,EAClD,CAAC,MAAM,CACR;EASA"}
1
+ {"version":3,"file":"use-threads.cjs","names":["useCopilotKit","ɵselectThreads","ɵselectThreadsIsLoading","ɵselectThreadsError","ɵselectHasNextPage","ɵselectIsFetchingNextPage"],"sources":["../../src/hooks/use-threads.tsx"],"sourcesContent":["import { useCopilotKit } from \"@/providers/CopilotKitProvider\";\nimport {\n ɵcreateThreadStore,\n ɵselectThreads,\n ɵselectThreadsError,\n ɵselectThreadsIsLoading,\n ɵselectHasNextPage,\n ɵselectIsFetchingNextPage,\n type ɵThread as CoreThread,\n type ɵThreadRuntimeContext,\n type ɵThreadStore,\n} from \"@copilotkitnext/core\";\nimport {\n useCallback,\n useEffect,\n useMemo,\n useState,\n useSyncExternalStore,\n} from \"react\";\n\n/**\n * A conversation thread managed by the Intelligence platform.\n *\n * Each thread has a unique `id`, an optional human-readable `name`, and\n * timestamp fields tracking creation and update times.\n */\nexport interface Thread extends CoreThread {}\n\n/**\n * Configuration for the {@link useThreads} hook.\n *\n * Thread operations are scoped to the runtime-authenticated user and the\n * provided agent on the Intelligence platform.\n */\nexport interface UseThreadsInput {\n /** The ID of the agent whose threads to list and manage. */\n agentId: string;\n /** When `true`, archived threads are included in the list. Defaults to `false`. */\n includeArchived?: boolean;\n /** Maximum number of threads to fetch per page. When set, enables cursor-based pagination. */\n limit?: number;\n}\n\n/**\n * Return value of the {@link useThreads} hook.\n *\n * The `threads` array is kept in sync with the platform via a realtime\n * WebSocket subscription (when available) and is sorted most-recently-updated\n * first. Mutations reject with an `Error` if the platform request fails.\n */\nexport interface UseThreadsResult {\n /**\n * Threads for the current user/agent pair, sorted by most recently\n * updated first. Updated in realtime when the platform pushes metadata\n * events. Includes archived threads only when `includeArchived` is set.\n */\n threads: Thread[];\n /**\n * `true` while the initial thread list is being fetched from the platform.\n * Subsequent realtime updates do not re-enter the loading state.\n */\n isLoading: boolean;\n /**\n * The most recent error from fetching threads or executing a mutation,\n * or `null` when there is no error. Reset to `null` on the next\n * successful fetch.\n */\n error: Error | null;\n /**\n * `true` when there are more threads available to fetch via\n * {@link fetchNextPage}. Only meaningful when `limit` is set.\n */\n hasNextPage: boolean;\n /**\n * `true` while a subsequent page of threads is being fetched.\n */\n isFetchingNextPage: boolean;\n /**\n * Fetch the next page of threads. No-op when {@link hasNextPage} is\n * `false` or a page fetch is already in progress.\n */\n fetchNextPage: () => void;\n /**\n * Rename a thread on the platform.\n * Resolves when the server confirms the update; rejects on failure.\n */\n renameThread: (threadId: string, name: string) => Promise<void>;\n /**\n * Archive a thread on the platform.\n * Archived threads are excluded from subsequent list results.\n * Resolves when the server confirms the update; rejects on failure.\n */\n archiveThread: (threadId: string) => Promise<void>;\n /**\n * Permanently delete a thread from the platform.\n * This is irreversible. Resolves when the server confirms deletion;\n * rejects on failure.\n */\n deleteThread: (threadId: string) => Promise<void>;\n}\n\nfunction useThreadStoreSelector<T>(\n store: ɵThreadStore,\n selector: (state: ReturnType<ɵThreadStore[\"getState\"]>) => T,\n): T {\n return useSyncExternalStore(\n useCallback(\n (onStoreChange) => {\n const subscription = store.select(selector).subscribe(onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, selector],\n ),\n () => selector(store.getState()),\n );\n}\n\n/**\n * React hook for listing and managing Intelligence platform threads.\n *\n * On mount the hook fetches the thread list for the runtime-authenticated user\n * and the given `agentId`. When the Intelligence platform exposes a WebSocket\n * URL, it also opens a realtime subscription so the `threads` array stays\n * current without polling — thread creates, renames, archives, and deletes\n * from any client are reflected immediately.\n *\n * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return\n * promises that resolve once the platform confirms the operation and reject\n * with an `Error` on failure.\n *\n * @param input - Agent identifier and optional list controls.\n * @returns Thread list state and stable mutation callbacks.\n *\n * @example\n * ```tsx\n * import { useThreads } from \"@copilotkitnext/react\";\n *\n * function ThreadList() {\n * const { threads, isLoading, renameThread, deleteThread } = useThreads({\n * agentId: \"agent-1\",\n * });\n *\n * if (isLoading) return <p>Loading…</p>;\n *\n * return (\n * <ul>\n * {threads.map((t) => (\n * <li key={t.id}>\n * {t.name ?? \"Untitled\"}\n * <button onClick={() => renameThread(t.id, \"New name\")}>Rename</button>\n * <button onClick={() => deleteThread(t.id)}>Delete</button>\n * </li>\n * ))}\n * </ul>\n * );\n * }\n * ```\n */\nexport function useThreads({\n agentId,\n includeArchived,\n limit,\n}: UseThreadsInput): UseThreadsResult {\n const { copilotkit } = useCopilotKit();\n\n const [store] = useState(() =>\n ɵcreateThreadStore({\n fetch: globalThis.fetch,\n }),\n );\n\n const threads = useThreadStoreSelector(store, ɵselectThreads);\n const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);\n const storeError = useThreadStoreSelector(store, ɵselectThreadsError);\n const hasNextPage = useThreadStoreSelector(store, ɵselectHasNextPage);\n const isFetchingNextPage = useThreadStoreSelector(\n store,\n ɵselectIsFetchingNextPage,\n );\n const headersKey = useMemo(() => {\n return JSON.stringify(\n Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) =>\n left.localeCompare(right),\n ),\n );\n }, [copilotkit.headers]);\n const runtimeError = useMemo(() => {\n if (copilotkit.runtimeUrl) {\n return null;\n }\n\n return new Error(\"Runtime URL is not configured\");\n }, [copilotkit.runtimeUrl]);\n const isLoading = runtimeError ? false : storeIsLoading;\n const error = runtimeError ?? storeError;\n\n useEffect(() => {\n store.start();\n return () => {\n store.stop();\n };\n }, [store]);\n\n useEffect(() => {\n const context: ɵThreadRuntimeContext | null = copilotkit.runtimeUrl\n ? {\n runtimeUrl: copilotkit.runtimeUrl,\n headers: { ...copilotkit.headers },\n wsUrl: copilotkit.intelligence?.wsUrl,\n agentId,\n includeArchived,\n limit,\n }\n : null;\n\n store.setContext(context);\n }, [\n store,\n copilotkit.runtimeUrl,\n headersKey,\n copilotkit.intelligence?.wsUrl,\n agentId,\n copilotkit.headers,\n includeArchived,\n limit,\n ]);\n\n const renameThread = useCallback(\n (threadId: string, name: string) => store.renameThread(threadId, name),\n [store],\n );\n\n const archiveThread = useCallback(\n (threadId: string) => store.archiveThread(threadId),\n [store],\n );\n\n const deleteThread = useCallback(\n (threadId: string) => store.deleteThread(threadId),\n [store],\n );\n\n const fetchNextPage = useCallback(() => store.fetchNextPage(), [store]);\n\n return {\n threads,\n isLoading,\n error,\n hasNextPage,\n isFetchingNextPage,\n fetchNextPage,\n renameThread,\n archiveThread,\n deleteThread,\n };\n}\n"],"mappings":";;;;;;AAqGA,SAAS,uBACP,OACA,UACG;AACH,gEAEK,kBAAkB;EACjB,MAAM,eAAe,MAAM,OAAO,SAAS,CAAC,UAAU,cAAc;AACpE,eAAa,aAAa,aAAa;IAEzC,CAAC,OAAO,SAAS,CAClB,QACK,SAAS,MAAM,UAAU,CAAC,CACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CH,SAAgB,WAAW,EACzB,SACA,iBACA,SACoC;CACpC,MAAM,EAAE,eAAeA,0CAAe;CAEtC,MAAM,CAAC,gFACc,EACjB,OAAO,WAAW,OACnB,CAAC,CACH;CAED,MAAM,UAAU,uBAAuB,OAAOC,oCAAe;CAC7D,MAAM,iBAAiB,uBAAuB,OAAOC,6CAAwB;CAC7E,MAAM,aAAa,uBAAuB,OAAOC,yCAAoB;CACrE,MAAM,cAAc,uBAAuB,OAAOC,wCAAmB;CACrE,MAAM,qBAAqB,uBACzB,OACAC,+CACD;CACD,MAAM,sCAA2B;AAC/B,SAAO,KAAK,UACV,OAAO,QAAQ,WAAW,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,WACtD,KAAK,cAAc,MAAM,CAC1B,CACF;IACA,CAAC,WAAW,QAAQ,CAAC;CACxB,MAAM,wCAA6B;AACjC,MAAI,WAAW,WACb,QAAO;AAGT,yBAAO,IAAI,MAAM,gCAAgC;IAChD,CAAC,WAAW,WAAW,CAAC;CAC3B,MAAM,YAAY,eAAe,QAAQ;CACzC,MAAM,QAAQ,gBAAgB;AAE9B,4BAAgB;AACd,QAAM,OAAO;AACb,eAAa;AACX,SAAM,MAAM;;IAEb,CAAC,MAAM,CAAC;AAEX,4BAAgB;EACd,MAAM,UAAwC,WAAW,aACrD;GACE,YAAY,WAAW;GACvB,SAAS,EAAE,GAAG,WAAW,SAAS;GAClC,OAAO,WAAW,cAAc;GAChC;GACA;GACA;GACD,GACD;AAEJ,QAAM,WAAW,QAAQ;IACxB;EACD;EACA,WAAW;EACX;EACA,WAAW,cAAc;EACzB;EACA,WAAW;EACX;EACA;EACD,CAAC;CAEF,MAAM,uCACH,UAAkB,SAAiB,MAAM,aAAa,UAAU,KAAK,EACtE,CAAC,MAAM,CACR;CAED,MAAM,wCACH,aAAqB,MAAM,cAAc,SAAS,EACnD,CAAC,MAAM,CACR;CAED,MAAM,uCACH,aAAqB,MAAM,aAAa,SAAS,EAClD,CAAC,MAAM,CACR;AAID,QAAO;EACL;EACA;EACA;EACA;EACA;EACA,4CARsC,MAAM,eAAe,EAAE,CAAC,MAAM,CAAC;EASrE;EACA;EACA;EACD"}
@@ -11,14 +11,16 @@ interface Thread extends ɵThread {}
11
11
  /**
12
12
  * Configuration for the {@link useThreads} hook.
13
13
  *
14
- * Both fields are required they scope the thread list and all mutations
15
- * to a specific user/agent pair on the Intelligence platform.
14
+ * Thread operations are scoped to the runtime-authenticated user and the
15
+ * provided agent on the Intelligence platform.
16
16
  */
17
17
  interface UseThreadsInput {
18
- /** The ID of the current user. Thread queries and mutations are scoped to this user. */
19
- userId: string;
20
18
  /** The ID of the agent whose threads to list and manage. */
21
19
  agentId: string;
20
+ /** When `true`, archived threads are included in the list. Defaults to `false`. */
21
+ includeArchived?: boolean;
22
+ /** Maximum number of threads to fetch per page. When set, enables cursor-based pagination. */
23
+ limit?: number;
22
24
  }
23
25
  /**
24
26
  * Return value of the {@link useThreads} hook.
@@ -29,9 +31,9 @@ interface UseThreadsInput {
29
31
  */
30
32
  interface UseThreadsResult {
31
33
  /**
32
- * All non-archived threads for the current user/agent pair, sorted by
33
- * most recently updated first. Updated in realtime when the platform
34
- * pushes metadata events.
34
+ * Threads for the current user/agent pair, sorted by most recently
35
+ * updated first. Updated in realtime when the platform pushes metadata
36
+ * events. Includes archived threads only when `includeArchived` is set.
35
37
  */
36
38
  threads: Thread[];
37
39
  /**
@@ -45,6 +47,20 @@ interface UseThreadsResult {
45
47
  * successful fetch.
46
48
  */
47
49
  error: Error | null;
50
+ /**
51
+ * `true` when there are more threads available to fetch via
52
+ * {@link fetchNextPage}. Only meaningful when `limit` is set.
53
+ */
54
+ hasNextPage: boolean;
55
+ /**
56
+ * `true` while a subsequent page of threads is being fetched.
57
+ */
58
+ isFetchingNextPage: boolean;
59
+ /**
60
+ * Fetch the next page of threads. No-op when {@link hasNextPage} is
61
+ * `false` or a page fetch is already in progress.
62
+ */
63
+ fetchNextPage: () => void;
48
64
  /**
49
65
  * Rename a thread on the platform.
50
66
  * Resolves when the server confirms the update; rejects on failure.
@@ -66,17 +82,17 @@ interface UseThreadsResult {
66
82
  /**
67
83
  * React hook for listing and managing Intelligence platform threads.
68
84
  *
69
- * On mount the hook fetches the thread list for the given `userId`/`agentId`
70
- * pair. When the Intelligence platform exposes a WebSocket URL, it also opens
71
- * a realtime subscription so the `threads` array stays current without
72
- * polling — thread creates, renames, archives, and deletes from any client
73
- * are reflected immediately.
85
+ * On mount the hook fetches the thread list for the runtime-authenticated user
86
+ * and the given `agentId`. When the Intelligence platform exposes a WebSocket
87
+ * URL, it also opens a realtime subscription so the `threads` array stays
88
+ * current without polling — thread creates, renames, archives, and deletes
89
+ * from any client are reflected immediately.
74
90
  *
75
91
  * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return
76
92
  * promises that resolve once the platform confirms the operation and reject
77
93
  * with an `Error` on failure.
78
94
  *
79
- * @param input - User and agent identifiers that scope the thread list.
95
+ * @param input - Agent identifier and optional list controls.
80
96
  * @returns Thread list state and stable mutation callbacks.
81
97
  *
82
98
  * @example
@@ -85,7 +101,6 @@ interface UseThreadsResult {
85
101
  *
86
102
  * function ThreadList() {
87
103
  * const { threads, isLoading, renameThread, deleteThread } = useThreads({
88
- * userId: "user-1",
89
104
  * agentId: "agent-1",
90
105
  * });
91
106
  *
@@ -106,8 +121,9 @@ interface UseThreadsResult {
106
121
  * ```
107
122
  */
108
123
  declare function useThreads({
109
- userId,
110
- agentId
124
+ agentId,
125
+ includeArchived,
126
+ limit
111
127
  }: UseThreadsInput): UseThreadsResult;
112
128
  //#endregion
113
129
  export { Thread, UseThreadsInput, UseThreadsResult, useThreads };
@@ -1 +1 @@
1
- {"version":3,"file":"use-threads.d.cts","names":[],"sources":["../../src/hooks/use-threads.tsx"],"mappings":";;;;;AAwBA;;;;UAAiB,MAAA,SAAe,OAAA;;;;;AAsBhC;;UAdiB,eAAA;EAoBN;EAlBT,MAAA;EAkCkD;EAhClD,OAAA;AAAA;;;;;;;;UAUe,gBAAA;EAsBA;;;;;EAhBf,OAAA,EAAS,MAAA;EA4BT;;;;EAvBA,SAAA;EAoFc;;;;;EA9Ed,KAAA,EAAO,KAAA;EAiFY;;;;EA5EnB,YAAA,GAAe,QAAA,UAAkB,IAAA,aAAiB,OAAA;EA2ElD;;;;;EArEA,aAAA,GAAgB,QAAA,aAAqB,OAAA;;;;;;EAMrC,YAAA,GAAe,QAAA,aAAqB,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6DtB,UAAA,CAAA;EACd,MAAA;EACA;AAAA,GACC,eAAA,GAAkB,gBAAA"}
1
+ {"version":3,"file":"use-threads.d.cts","names":[],"sources":["../../src/hooks/use-threads.tsx"],"mappings":";;;;;AA0BA;;;;UAAiB,MAAA,SAAe,OAAA;;;;;;;UAQf,eAAA;EAMV;EAJL,OAAA;EAc+B;EAZ/B,eAAA;EAkBS;EAhBT,KAAA;AAAA;;;;;;;;UAUe,gBAAA;EAiBR;;;;;EAXP,OAAA,EAAS,MAAA;EA8BwB;;;;EAzBjC,SAAA;EAqCA;;;;;EA/BA,KAAA,EAAO,KAAA;EA2FiB;;;;EAtFxB,WAAA;EA0FC;;;EAtFD,kBAAA;EAmFA;;;;EA9EA,aAAA;EAgFA;;;;EA3EA,YAAA,GAAe,QAAA,UAAkB,IAAA,aAAiB,OAAA;;;;;;EAMlD,aAAA,GAAgB,QAAA,aAAqB,OAAA;;;;;;EAMrC,YAAA,GAAe,QAAA,aAAqB,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4DtB,UAAA,CAAA;EACd,OAAA;EACA,eAAA;EACA;AAAA,GACC,eAAA,GAAkB,gBAAA"}
@@ -11,14 +11,16 @@ interface Thread extends ɵThread {}
11
11
  /**
12
12
  * Configuration for the {@link useThreads} hook.
13
13
  *
14
- * Both fields are required they scope the thread list and all mutations
15
- * to a specific user/agent pair on the Intelligence platform.
14
+ * Thread operations are scoped to the runtime-authenticated user and the
15
+ * provided agent on the Intelligence platform.
16
16
  */
17
17
  interface UseThreadsInput {
18
- /** The ID of the current user. Thread queries and mutations are scoped to this user. */
19
- userId: string;
20
18
  /** The ID of the agent whose threads to list and manage. */
21
19
  agentId: string;
20
+ /** When `true`, archived threads are included in the list. Defaults to `false`. */
21
+ includeArchived?: boolean;
22
+ /** Maximum number of threads to fetch per page. When set, enables cursor-based pagination. */
23
+ limit?: number;
22
24
  }
23
25
  /**
24
26
  * Return value of the {@link useThreads} hook.
@@ -29,9 +31,9 @@ interface UseThreadsInput {
29
31
  */
30
32
  interface UseThreadsResult {
31
33
  /**
32
- * All non-archived threads for the current user/agent pair, sorted by
33
- * most recently updated first. Updated in realtime when the platform
34
- * pushes metadata events.
34
+ * Threads for the current user/agent pair, sorted by most recently
35
+ * updated first. Updated in realtime when the platform pushes metadata
36
+ * events. Includes archived threads only when `includeArchived` is set.
35
37
  */
36
38
  threads: Thread[];
37
39
  /**
@@ -45,6 +47,20 @@ interface UseThreadsResult {
45
47
  * successful fetch.
46
48
  */
47
49
  error: Error | null;
50
+ /**
51
+ * `true` when there are more threads available to fetch via
52
+ * {@link fetchNextPage}. Only meaningful when `limit` is set.
53
+ */
54
+ hasNextPage: boolean;
55
+ /**
56
+ * `true` while a subsequent page of threads is being fetched.
57
+ */
58
+ isFetchingNextPage: boolean;
59
+ /**
60
+ * Fetch the next page of threads. No-op when {@link hasNextPage} is
61
+ * `false` or a page fetch is already in progress.
62
+ */
63
+ fetchNextPage: () => void;
48
64
  /**
49
65
  * Rename a thread on the platform.
50
66
  * Resolves when the server confirms the update; rejects on failure.
@@ -66,17 +82,17 @@ interface UseThreadsResult {
66
82
  /**
67
83
  * React hook for listing and managing Intelligence platform threads.
68
84
  *
69
- * On mount the hook fetches the thread list for the given `userId`/`agentId`
70
- * pair. When the Intelligence platform exposes a WebSocket URL, it also opens
71
- * a realtime subscription so the `threads` array stays current without
72
- * polling — thread creates, renames, archives, and deletes from any client
73
- * are reflected immediately.
85
+ * On mount the hook fetches the thread list for the runtime-authenticated user
86
+ * and the given `agentId`. When the Intelligence platform exposes a WebSocket
87
+ * URL, it also opens a realtime subscription so the `threads` array stays
88
+ * current without polling — thread creates, renames, archives, and deletes
89
+ * from any client are reflected immediately.
74
90
  *
75
91
  * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return
76
92
  * promises that resolve once the platform confirms the operation and reject
77
93
  * with an `Error` on failure.
78
94
  *
79
- * @param input - User and agent identifiers that scope the thread list.
95
+ * @param input - Agent identifier and optional list controls.
80
96
  * @returns Thread list state and stable mutation callbacks.
81
97
  *
82
98
  * @example
@@ -85,7 +101,6 @@ interface UseThreadsResult {
85
101
  *
86
102
  * function ThreadList() {
87
103
  * const { threads, isLoading, renameThread, deleteThread } = useThreads({
88
- * userId: "user-1",
89
104
  * agentId: "agent-1",
90
105
  * });
91
106
  *
@@ -106,8 +121,9 @@ interface UseThreadsResult {
106
121
  * ```
107
122
  */
108
123
  declare function useThreads({
109
- userId,
110
- agentId
124
+ agentId,
125
+ includeArchived,
126
+ limit
111
127
  }: UseThreadsInput): UseThreadsResult;
112
128
  //#endregion
113
129
  export { Thread, UseThreadsInput, UseThreadsResult, useThreads };
@@ -1 +1 @@
1
- {"version":3,"file":"use-threads.d.mts","names":[],"sources":["../../src/hooks/use-threads.tsx"],"mappings":";;;;;AAwBA;;;;UAAiB,MAAA,SAAe,OAAA;;;;;AAsBhC;;UAdiB,eAAA;EAoBN;EAlBT,MAAA;EAkCkD;EAhClD,OAAA;AAAA;;;;;;;;UAUe,gBAAA;EAsBA;;;;;EAhBf,OAAA,EAAS,MAAA;EA4BT;;;;EAvBA,SAAA;EAoFc;;;;;EA9Ed,KAAA,EAAO,KAAA;EAiFY;;;;EA5EnB,YAAA,GAAe,QAAA,UAAkB,IAAA,aAAiB,OAAA;EA2ElD;;;;;EArEA,aAAA,GAAgB,QAAA,aAAqB,OAAA;;;;;;EAMrC,YAAA,GAAe,QAAA,aAAqB,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6DtB,UAAA,CAAA;EACd,MAAA;EACA;AAAA,GACC,eAAA,GAAkB,gBAAA"}
1
+ {"version":3,"file":"use-threads.d.mts","names":[],"sources":["../../src/hooks/use-threads.tsx"],"mappings":";;;;;AA0BA;;;;UAAiB,MAAA,SAAe,OAAA;;;;;;;UAQf,eAAA;EAMV;EAJL,OAAA;EAc+B;EAZ/B,eAAA;EAkBS;EAhBT,KAAA;AAAA;;;;;;;;UAUe,gBAAA;EAiBR;;;;;EAXP,OAAA,EAAS,MAAA;EA8BwB;;;;EAzBjC,SAAA;EAqCA;;;;;EA/BA,KAAA,EAAO,KAAA;EA2FiB;;;;EAtFxB,WAAA;EA0FC;;;EAtFD,kBAAA;EAmFA;;;;EA9EA,aAAA;EAgFA;;;;EA3EA,YAAA,GAAe,QAAA,UAAkB,IAAA,aAAiB,OAAA;;;;;;EAMlD,aAAA,GAAgB,QAAA,aAAqB,OAAA;;;;;;EAMrC,YAAA,GAAe,QAAA,aAAqB,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4DtB,UAAA,CAAA;EACd,OAAA;EACA,eAAA;EACA;AAAA,GACC,eAAA,GAAkB,gBAAA"}
@@ -1,6 +1,6 @@
1
1
  import { useCopilotKit } from "../providers/CopilotKitProvider.mjs";
2
2
  import { useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
3
- import { ɵcreateThreadStore, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading } from "@copilotkitnext/core";
3
+ import { ɵcreateThreadStore, ɵselectHasNextPage, ɵselectIsFetchingNextPage, ɵselectThreads, ɵselectThreadsError, ɵselectThreadsIsLoading } from "@copilotkitnext/core";
4
4
 
5
5
  //#region src/hooks/use-threads.tsx
6
6
  function useThreadStoreSelector(store, selector) {
@@ -12,17 +12,17 @@ function useThreadStoreSelector(store, selector) {
12
12
  /**
13
13
  * React hook for listing and managing Intelligence platform threads.
14
14
  *
15
- * On mount the hook fetches the thread list for the given `userId`/`agentId`
16
- * pair. When the Intelligence platform exposes a WebSocket URL, it also opens
17
- * a realtime subscription so the `threads` array stays current without
18
- * polling — thread creates, renames, archives, and deletes from any client
19
- * are reflected immediately.
15
+ * On mount the hook fetches the thread list for the runtime-authenticated user
16
+ * and the given `agentId`. When the Intelligence platform exposes a WebSocket
17
+ * URL, it also opens a realtime subscription so the `threads` array stays
18
+ * current without polling — thread creates, renames, archives, and deletes
19
+ * from any client are reflected immediately.
20
20
  *
21
21
  * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return
22
22
  * promises that resolve once the platform confirms the operation and reject
23
23
  * with an `Error` on failure.
24
24
  *
25
- * @param input - User and agent identifiers that scope the thread list.
25
+ * @param input - Agent identifier and optional list controls.
26
26
  * @returns Thread list state and stable mutation callbacks.
27
27
  *
28
28
  * @example
@@ -31,7 +31,6 @@ function useThreadStoreSelector(store, selector) {
31
31
  *
32
32
  * function ThreadList() {
33
33
  * const { threads, isLoading, renameThread, deleteThread } = useThreads({
34
- * userId: "user-1",
35
34
  * agentId: "agent-1",
36
35
  * });
37
36
  *
@@ -51,12 +50,14 @@ function useThreadStoreSelector(store, selector) {
51
50
  * }
52
51
  * ```
53
52
  */
54
- function useThreads({ userId, agentId }) {
53
+ function useThreads({ agentId, includeArchived, limit }) {
55
54
  const { copilotkit } = useCopilotKit();
56
55
  const [store] = useState(() => ɵcreateThreadStore({ fetch: globalThis.fetch }));
57
56
  const threads = useThreadStoreSelector(store, ɵselectThreads);
58
57
  const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);
59
58
  const storeError = useThreadStoreSelector(store, ɵselectThreadsError);
59
+ const hasNextPage = useThreadStoreSelector(store, ɵselectHasNextPage);
60
+ const isFetchingNextPage = useThreadStoreSelector(store, ɵselectIsFetchingNextPage);
60
61
  const headersKey = useMemo(() => {
61
62
  return JSON.stringify(Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) => left.localeCompare(right)));
62
63
  }, [copilotkit.headers]);
@@ -77,8 +78,9 @@ function useThreads({ userId, agentId }) {
77
78
  runtimeUrl: copilotkit.runtimeUrl,
78
79
  headers: { ...copilotkit.headers },
79
80
  wsUrl: copilotkit.intelligence?.wsUrl,
80
- userId,
81
- agentId
81
+ agentId,
82
+ includeArchived,
83
+ limit
82
84
  } : null;
83
85
  store.setContext(context);
84
86
  }, [
@@ -86,17 +88,24 @@ function useThreads({ userId, agentId }) {
86
88
  copilotkit.runtimeUrl,
87
89
  headersKey,
88
90
  copilotkit.intelligence?.wsUrl,
89
- userId,
90
91
  agentId,
91
- copilotkit.headers
92
+ copilotkit.headers,
93
+ includeArchived,
94
+ limit
92
95
  ]);
96
+ const renameThread = useCallback((threadId, name) => store.renameThread(threadId, name), [store]);
97
+ const archiveThread = useCallback((threadId) => store.archiveThread(threadId), [store]);
98
+ const deleteThread = useCallback((threadId) => store.deleteThread(threadId), [store]);
93
99
  return {
94
100
  threads,
95
101
  isLoading,
96
102
  error,
97
- renameThread: useCallback((threadId, name) => store.renameThread(threadId, name), [store]),
98
- archiveThread: useCallback((threadId) => store.archiveThread(threadId), [store]),
99
- deleteThread: useCallback((threadId) => store.deleteThread(threadId), [store])
103
+ hasNextPage,
104
+ isFetchingNextPage,
105
+ fetchNextPage: useCallback(() => store.fetchNextPage(), [store]),
106
+ renameThread,
107
+ archiveThread,
108
+ deleteThread
100
109
  };
101
110
  }
102
111
 
@@ -1 +1 @@
1
- {"version":3,"file":"use-threads.mjs","names":[],"sources":["../../src/hooks/use-threads.tsx"],"sourcesContent":["import { useCopilotKit } from \"@/providers/CopilotKitProvider\";\nimport {\n ɵcreateThreadStore,\n ɵselectThreads,\n ɵselectThreadsError,\n ɵselectThreadsIsLoading,\n type ɵThread as CoreThread,\n type ɵThreadRuntimeContext,\n type ɵThreadStore,\n} from \"@copilotkitnext/core\";\nimport {\n useCallback,\n useEffect,\n useMemo,\n useState,\n useSyncExternalStore,\n} from \"react\";\n\n/**\n * A conversation thread managed by the Intelligence platform.\n *\n * Each thread has a unique `id`, an optional human-readable `name`, and\n * timestamp fields tracking creation and update times.\n */\nexport interface Thread extends CoreThread {}\n\n/**\n * Configuration for the {@link useThreads} hook.\n *\n * Both fields are required they scope the thread list and all mutations\n * to a specific user/agent pair on the Intelligence platform.\n */\nexport interface UseThreadsInput {\n /** The ID of the current user. Thread queries and mutations are scoped to this user. */\n userId: string;\n /** The ID of the agent whose threads to list and manage. */\n agentId: string;\n}\n\n/**\n * Return value of the {@link useThreads} hook.\n *\n * The `threads` array is kept in sync with the platform via a realtime\n * WebSocket subscription (when available) and is sorted most-recently-updated\n * first. Mutations reject with an `Error` if the platform request fails.\n */\nexport interface UseThreadsResult {\n /**\n * All non-archived threads for the current user/agent pair, sorted by\n * most recently updated first. Updated in realtime when the platform\n * pushes metadata events.\n */\n threads: Thread[];\n /**\n * `true` while the initial thread list is being fetched from the platform.\n * Subsequent realtime updates do not re-enter the loading state.\n */\n isLoading: boolean;\n /**\n * The most recent error from fetching threads or executing a mutation,\n * or `null` when there is no error. Reset to `null` on the next\n * successful fetch.\n */\n error: Error | null;\n /**\n * Rename a thread on the platform.\n * Resolves when the server confirms the update; rejects on failure.\n */\n renameThread: (threadId: string, name: string) => Promise<void>;\n /**\n * Archive a thread on the platform.\n * Archived threads are excluded from subsequent list results.\n * Resolves when the server confirms the update; rejects on failure.\n */\n archiveThread: (threadId: string) => Promise<void>;\n /**\n * Permanently delete a thread from the platform.\n * This is irreversible. Resolves when the server confirms deletion;\n * rejects on failure.\n */\n deleteThread: (threadId: string) => Promise<void>;\n}\n\nfunction useThreadStoreSelector<T>(\n store: ɵThreadStore,\n selector: (state: ReturnType<ɵThreadStore[\"getState\"]>) => T,\n): T {\n return useSyncExternalStore(\n useCallback(\n (onStoreChange) => {\n const subscription = store.select(selector).subscribe(onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, selector],\n ),\n () => selector(store.getState()),\n );\n}\n\n/**\n * React hook for listing and managing Intelligence platform threads.\n *\n * On mount the hook fetches the thread list for the given `userId`/`agentId`\n * pair. When the Intelligence platform exposes a WebSocket URL, it also opens\n * a realtime subscription so the `threads` array stays current without\n * polling — thread creates, renames, archives, and deletes from any client\n * are reflected immediately.\n *\n * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return\n * promises that resolve once the platform confirms the operation and reject\n * with an `Error` on failure.\n *\n * @param input - User and agent identifiers that scope the thread list.\n * @returns Thread list state and stable mutation callbacks.\n *\n * @example\n * ```tsx\n * import { useThreads } from \"@copilotkitnext/react\";\n *\n * function ThreadList() {\n * const { threads, isLoading, renameThread, deleteThread } = useThreads({\n * userId: \"user-1\",\n * agentId: \"agent-1\",\n * });\n *\n * if (isLoading) return <p>Loading…</p>;\n *\n * return (\n * <ul>\n * {threads.map((t) => (\n * <li key={t.id}>\n * {t.name ?? \"Untitled\"}\n * <button onClick={() => renameThread(t.id, \"New name\")}>Rename</button>\n * <button onClick={() => deleteThread(t.id)}>Delete</button>\n * </li>\n * ))}\n * </ul>\n * );\n * }\n * ```\n */\nexport function useThreads({\n userId,\n agentId,\n}: UseThreadsInput): UseThreadsResult {\n const { copilotkit } = useCopilotKit();\n\n const [store] = useState(() =>\n ɵcreateThreadStore({\n fetch: globalThis.fetch,\n }),\n );\n\n const threads = useThreadStoreSelector(store, ɵselectThreads);\n const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);\n const storeError = useThreadStoreSelector(store, ɵselectThreadsError);\n const headersKey = useMemo(() => {\n return JSON.stringify(\n Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) =>\n left.localeCompare(right),\n ),\n );\n }, [copilotkit.headers]);\n const runtimeError = useMemo(() => {\n if (copilotkit.runtimeUrl) {\n return null;\n }\n\n return new Error(\"Runtime URL is not configured\");\n }, [copilotkit.runtimeUrl]);\n const isLoading = runtimeError ? false : storeIsLoading;\n const error = runtimeError ?? storeError;\n\n useEffect(() => {\n store.start();\n return () => {\n store.stop();\n };\n }, [store]);\n\n useEffect(() => {\n const context: ɵThreadRuntimeContext | null = copilotkit.runtimeUrl\n ? {\n runtimeUrl: copilotkit.runtimeUrl,\n headers: { ...copilotkit.headers },\n wsUrl: copilotkit.intelligence?.wsUrl,\n userId,\n agentId,\n }\n : null;\n\n store.setContext(context);\n }, [\n store,\n copilotkit.runtimeUrl,\n headersKey,\n copilotkit.intelligence?.wsUrl,\n userId,\n agentId,\n copilotkit.headers,\n ]);\n\n const renameThread = useCallback(\n (threadId: string, name: string) => store.renameThread(threadId, name),\n [store],\n );\n\n const archiveThread = useCallback(\n (threadId: string) => store.archiveThread(threadId),\n [store],\n );\n\n const deleteThread = useCallback(\n (threadId: string) => store.deleteThread(threadId),\n [store],\n );\n\n return {\n threads,\n isLoading,\n error,\n renameThread,\n archiveThread,\n deleteThread,\n };\n}\n"],"mappings":";;;;;AAmFA,SAAS,uBACP,OACA,UACG;AACH,QAAO,qBACL,aACG,kBAAkB;EACjB,MAAM,eAAe,MAAM,OAAO,SAAS,CAAC,UAAU,cAAc;AACpE,eAAa,aAAa,aAAa;IAEzC,CAAC,OAAO,SAAS,CAClB,QACK,SAAS,MAAM,UAAU,CAAC,CACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CH,SAAgB,WAAW,EACzB,QACA,WACoC;CACpC,MAAM,EAAE,eAAe,eAAe;CAEtC,MAAM,CAAC,SAAS,eACd,mBAAmB,EACjB,OAAO,WAAW,OACnB,CAAC,CACH;CAED,MAAM,UAAU,uBAAuB,OAAO,eAAe;CAC7D,MAAM,iBAAiB,uBAAuB,OAAO,wBAAwB;CAC7E,MAAM,aAAa,uBAAuB,OAAO,oBAAoB;CACrE,MAAM,aAAa,cAAc;AAC/B,SAAO,KAAK,UACV,OAAO,QAAQ,WAAW,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,WACtD,KAAK,cAAc,MAAM,CAC1B,CACF;IACA,CAAC,WAAW,QAAQ,CAAC;CACxB,MAAM,eAAe,cAAc;AACjC,MAAI,WAAW,WACb,QAAO;AAGT,yBAAO,IAAI,MAAM,gCAAgC;IAChD,CAAC,WAAW,WAAW,CAAC;CAC3B,MAAM,YAAY,eAAe,QAAQ;CACzC,MAAM,QAAQ,gBAAgB;AAE9B,iBAAgB;AACd,QAAM,OAAO;AACb,eAAa;AACX,SAAM,MAAM;;IAEb,CAAC,MAAM,CAAC;AAEX,iBAAgB;EACd,MAAM,UAAwC,WAAW,aACrD;GACE,YAAY,WAAW;GACvB,SAAS,EAAE,GAAG,WAAW,SAAS;GAClC,OAAO,WAAW,cAAc;GAChC;GACA;GACD,GACD;AAEJ,QAAM,WAAW,QAAQ;IACxB;EACD;EACA,WAAW;EACX;EACA,WAAW,cAAc;EACzB;EACA;EACA,WAAW;EACZ,CAAC;AAiBF,QAAO;EACL;EACA;EACA;EACA,cAnBmB,aAClB,UAAkB,SAAiB,MAAM,aAAa,UAAU,KAAK,EACtE,CAAC,MAAM,CACR;EAiBC,eAfoB,aACnB,aAAqB,MAAM,cAAc,SAAS,EACnD,CAAC,MAAM,CACR;EAaC,cAXmB,aAClB,aAAqB,MAAM,aAAa,SAAS,EAClD,CAAC,MAAM,CACR;EASA"}
1
+ {"version":3,"file":"use-threads.mjs","names":[],"sources":["../../src/hooks/use-threads.tsx"],"sourcesContent":["import { useCopilotKit } from \"@/providers/CopilotKitProvider\";\nimport {\n ɵcreateThreadStore,\n ɵselectThreads,\n ɵselectThreadsError,\n ɵselectThreadsIsLoading,\n ɵselectHasNextPage,\n ɵselectIsFetchingNextPage,\n type ɵThread as CoreThread,\n type ɵThreadRuntimeContext,\n type ɵThreadStore,\n} from \"@copilotkitnext/core\";\nimport {\n useCallback,\n useEffect,\n useMemo,\n useState,\n useSyncExternalStore,\n} from \"react\";\n\n/**\n * A conversation thread managed by the Intelligence platform.\n *\n * Each thread has a unique `id`, an optional human-readable `name`, and\n * timestamp fields tracking creation and update times.\n */\nexport interface Thread extends CoreThread {}\n\n/**\n * Configuration for the {@link useThreads} hook.\n *\n * Thread operations are scoped to the runtime-authenticated user and the\n * provided agent on the Intelligence platform.\n */\nexport interface UseThreadsInput {\n /** The ID of the agent whose threads to list and manage. */\n agentId: string;\n /** When `true`, archived threads are included in the list. Defaults to `false`. */\n includeArchived?: boolean;\n /** Maximum number of threads to fetch per page. When set, enables cursor-based pagination. */\n limit?: number;\n}\n\n/**\n * Return value of the {@link useThreads} hook.\n *\n * The `threads` array is kept in sync with the platform via a realtime\n * WebSocket subscription (when available) and is sorted most-recently-updated\n * first. Mutations reject with an `Error` if the platform request fails.\n */\nexport interface UseThreadsResult {\n /**\n * Threads for the current user/agent pair, sorted by most recently\n * updated first. Updated in realtime when the platform pushes metadata\n * events. Includes archived threads only when `includeArchived` is set.\n */\n threads: Thread[];\n /**\n * `true` while the initial thread list is being fetched from the platform.\n * Subsequent realtime updates do not re-enter the loading state.\n */\n isLoading: boolean;\n /**\n * The most recent error from fetching threads or executing a mutation,\n * or `null` when there is no error. Reset to `null` on the next\n * successful fetch.\n */\n error: Error | null;\n /**\n * `true` when there are more threads available to fetch via\n * {@link fetchNextPage}. Only meaningful when `limit` is set.\n */\n hasNextPage: boolean;\n /**\n * `true` while a subsequent page of threads is being fetched.\n */\n isFetchingNextPage: boolean;\n /**\n * Fetch the next page of threads. No-op when {@link hasNextPage} is\n * `false` or a page fetch is already in progress.\n */\n fetchNextPage: () => void;\n /**\n * Rename a thread on the platform.\n * Resolves when the server confirms the update; rejects on failure.\n */\n renameThread: (threadId: string, name: string) => Promise<void>;\n /**\n * Archive a thread on the platform.\n * Archived threads are excluded from subsequent list results.\n * Resolves when the server confirms the update; rejects on failure.\n */\n archiveThread: (threadId: string) => Promise<void>;\n /**\n * Permanently delete a thread from the platform.\n * This is irreversible. Resolves when the server confirms deletion;\n * rejects on failure.\n */\n deleteThread: (threadId: string) => Promise<void>;\n}\n\nfunction useThreadStoreSelector<T>(\n store: ɵThreadStore,\n selector: (state: ReturnType<ɵThreadStore[\"getState\"]>) => T,\n): T {\n return useSyncExternalStore(\n useCallback(\n (onStoreChange) => {\n const subscription = store.select(selector).subscribe(onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, selector],\n ),\n () => selector(store.getState()),\n );\n}\n\n/**\n * React hook for listing and managing Intelligence platform threads.\n *\n * On mount the hook fetches the thread list for the runtime-authenticated user\n * and the given `agentId`. When the Intelligence platform exposes a WebSocket\n * URL, it also opens a realtime subscription so the `threads` array stays\n * current without polling — thread creates, renames, archives, and deletes\n * from any client are reflected immediately.\n *\n * Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return\n * promises that resolve once the platform confirms the operation and reject\n * with an `Error` on failure.\n *\n * @param input - Agent identifier and optional list controls.\n * @returns Thread list state and stable mutation callbacks.\n *\n * @example\n * ```tsx\n * import { useThreads } from \"@copilotkitnext/react\";\n *\n * function ThreadList() {\n * const { threads, isLoading, renameThread, deleteThread } = useThreads({\n * agentId: \"agent-1\",\n * });\n *\n * if (isLoading) return <p>Loading…</p>;\n *\n * return (\n * <ul>\n * {threads.map((t) => (\n * <li key={t.id}>\n * {t.name ?? \"Untitled\"}\n * <button onClick={() => renameThread(t.id, \"New name\")}>Rename</button>\n * <button onClick={() => deleteThread(t.id)}>Delete</button>\n * </li>\n * ))}\n * </ul>\n * );\n * }\n * ```\n */\nexport function useThreads({\n agentId,\n includeArchived,\n limit,\n}: UseThreadsInput): UseThreadsResult {\n const { copilotkit } = useCopilotKit();\n\n const [store] = useState(() =>\n ɵcreateThreadStore({\n fetch: globalThis.fetch,\n }),\n );\n\n const threads = useThreadStoreSelector(store, ɵselectThreads);\n const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);\n const storeError = useThreadStoreSelector(store, ɵselectThreadsError);\n const hasNextPage = useThreadStoreSelector(store, ɵselectHasNextPage);\n const isFetchingNextPage = useThreadStoreSelector(\n store,\n ɵselectIsFetchingNextPage,\n );\n const headersKey = useMemo(() => {\n return JSON.stringify(\n Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) =>\n left.localeCompare(right),\n ),\n );\n }, [copilotkit.headers]);\n const runtimeError = useMemo(() => {\n if (copilotkit.runtimeUrl) {\n return null;\n }\n\n return new Error(\"Runtime URL is not configured\");\n }, [copilotkit.runtimeUrl]);\n const isLoading = runtimeError ? false : storeIsLoading;\n const error = runtimeError ?? storeError;\n\n useEffect(() => {\n store.start();\n return () => {\n store.stop();\n };\n }, [store]);\n\n useEffect(() => {\n const context: ɵThreadRuntimeContext | null = copilotkit.runtimeUrl\n ? {\n runtimeUrl: copilotkit.runtimeUrl,\n headers: { ...copilotkit.headers },\n wsUrl: copilotkit.intelligence?.wsUrl,\n agentId,\n includeArchived,\n limit,\n }\n : null;\n\n store.setContext(context);\n }, [\n store,\n copilotkit.runtimeUrl,\n headersKey,\n copilotkit.intelligence?.wsUrl,\n agentId,\n copilotkit.headers,\n includeArchived,\n limit,\n ]);\n\n const renameThread = useCallback(\n (threadId: string, name: string) => store.renameThread(threadId, name),\n [store],\n );\n\n const archiveThread = useCallback(\n (threadId: string) => store.archiveThread(threadId),\n [store],\n );\n\n const deleteThread = useCallback(\n (threadId: string) => store.deleteThread(threadId),\n [store],\n );\n\n const fetchNextPage = useCallback(() => store.fetchNextPage(), [store]);\n\n return {\n threads,\n isLoading,\n error,\n hasNextPage,\n isFetchingNextPage,\n fetchNextPage,\n renameThread,\n archiveThread,\n deleteThread,\n };\n}\n"],"mappings":";;;;;AAqGA,SAAS,uBACP,OACA,UACG;AACH,QAAO,qBACL,aACG,kBAAkB;EACjB,MAAM,eAAe,MAAM,OAAO,SAAS,CAAC,UAAU,cAAc;AACpE,eAAa,aAAa,aAAa;IAEzC,CAAC,OAAO,SAAS,CAClB,QACK,SAAS,MAAM,UAAU,CAAC,CACjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CH,SAAgB,WAAW,EACzB,SACA,iBACA,SACoC;CACpC,MAAM,EAAE,eAAe,eAAe;CAEtC,MAAM,CAAC,SAAS,eACd,mBAAmB,EACjB,OAAO,WAAW,OACnB,CAAC,CACH;CAED,MAAM,UAAU,uBAAuB,OAAO,eAAe;CAC7D,MAAM,iBAAiB,uBAAuB,OAAO,wBAAwB;CAC7E,MAAM,aAAa,uBAAuB,OAAO,oBAAoB;CACrE,MAAM,cAAc,uBAAuB,OAAO,mBAAmB;CACrE,MAAM,qBAAqB,uBACzB,OACA,0BACD;CACD,MAAM,aAAa,cAAc;AAC/B,SAAO,KAAK,UACV,OAAO,QAAQ,WAAW,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,WACtD,KAAK,cAAc,MAAM,CAC1B,CACF;IACA,CAAC,WAAW,QAAQ,CAAC;CACxB,MAAM,eAAe,cAAc;AACjC,MAAI,WAAW,WACb,QAAO;AAGT,yBAAO,IAAI,MAAM,gCAAgC;IAChD,CAAC,WAAW,WAAW,CAAC;CAC3B,MAAM,YAAY,eAAe,QAAQ;CACzC,MAAM,QAAQ,gBAAgB;AAE9B,iBAAgB;AACd,QAAM,OAAO;AACb,eAAa;AACX,SAAM,MAAM;;IAEb,CAAC,MAAM,CAAC;AAEX,iBAAgB;EACd,MAAM,UAAwC,WAAW,aACrD;GACE,YAAY,WAAW;GACvB,SAAS,EAAE,GAAG,WAAW,SAAS;GAClC,OAAO,WAAW,cAAc;GAChC;GACA;GACA;GACD,GACD;AAEJ,QAAM,WAAW,QAAQ;IACxB;EACD;EACA,WAAW;EACX;EACA,WAAW,cAAc;EACzB;EACA,WAAW;EACX;EACA;EACD,CAAC;CAEF,MAAM,eAAe,aAClB,UAAkB,SAAiB,MAAM,aAAa,UAAU,KAAK,EACtE,CAAC,MAAM,CACR;CAED,MAAM,gBAAgB,aACnB,aAAqB,MAAM,cAAc,SAAS,EACnD,CAAC,MAAM,CACR;CAED,MAAM,eAAe,aAClB,aAAqB,MAAM,aAAa,SAAS,EAClD,CAAC,MAAM,CACR;AAID,QAAO;EACL;EACA;EACA;EACA;EACA;EACA,eARoB,kBAAkB,MAAM,eAAe,EAAE,CAAC,MAAM,CAAC;EASrE;EACA;EACA;EACD"}