@yourgpt/copilot-sdk 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-QWQELTEB.js';
1
+ import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-QSEGNATZ.js';
2
2
  import { createContext, useContext, useCallback, useEffect, useState, useRef, useSyncExternalStore, useMemo } from 'react';
3
3
  import { jsx } from 'react/jsx-runtime';
4
4
  import * as z from 'zod';
@@ -1720,6 +1720,9 @@ function CopilotProvider({
1720
1720
  const clearMessages = useCallback(() => {
1721
1721
  chatRef.current?.clearMessages();
1722
1722
  }, []);
1723
+ const setMessages = useCallback((messages2) => {
1724
+ chatRef.current?.setMessages(messages2);
1725
+ }, []);
1723
1726
  const regenerate = useCallback(async (messageId) => {
1724
1727
  await chatRef.current?.regenerate(messageId);
1725
1728
  }, []);
@@ -1761,6 +1764,7 @@ function CopilotProvider({
1761
1764
  sendMessage,
1762
1765
  stop,
1763
1766
  clearMessages,
1767
+ setMessages,
1764
1768
  regenerate,
1765
1769
  // Tool execution
1766
1770
  registerTool,
@@ -1790,6 +1794,7 @@ function CopilotProvider({
1790
1794
  sendMessage,
1791
1795
  stop,
1792
1796
  clearMessages,
1797
+ setMessages,
1793
1798
  regenerate,
1794
1799
  registerTool,
1795
1800
  unregisterTool,
@@ -2854,6 +2859,425 @@ function useDevLogger() {
2854
2859
  ]);
2855
2860
  }
2856
2861
 
2862
+ // src/react/internal/ReactThreadManagerState.ts
2863
+ var ReactThreadManagerState = class {
2864
+ constructor(initialThreads) {
2865
+ this._threads = [];
2866
+ this._currentThreadId = null;
2867
+ this._currentThread = null;
2868
+ this._loadStatus = "idle";
2869
+ this._error = void 0;
2870
+ // Callbacks for React subscriptions (useSyncExternalStore)
2871
+ this.subscribers = /* @__PURE__ */ new Set();
2872
+ // ============================================
2873
+ // Subscription (for useSyncExternalStore)
2874
+ // ============================================
2875
+ /**
2876
+ * Subscribe to state changes.
2877
+ * Returns an unsubscribe function.
2878
+ *
2879
+ * @example
2880
+ * ```tsx
2881
+ * const threads = useSyncExternalStore(
2882
+ * state.subscribe,
2883
+ * () => state.threads
2884
+ * );
2885
+ * ```
2886
+ */
2887
+ this.subscribe = (callback) => {
2888
+ this.subscribers.add(callback);
2889
+ return () => {
2890
+ this.subscribers.delete(callback);
2891
+ };
2892
+ };
2893
+ if (initialThreads) {
2894
+ this._threads = initialThreads;
2895
+ }
2896
+ }
2897
+ // ============================================
2898
+ // Getters
2899
+ // ============================================
2900
+ get threads() {
2901
+ return this._threads;
2902
+ }
2903
+ get currentThreadId() {
2904
+ return this._currentThreadId;
2905
+ }
2906
+ get currentThread() {
2907
+ return this._currentThread;
2908
+ }
2909
+ get loadStatus() {
2910
+ return this._loadStatus;
2911
+ }
2912
+ get error() {
2913
+ return this._error;
2914
+ }
2915
+ // ============================================
2916
+ // Setters (trigger reactivity)
2917
+ // ============================================
2918
+ set threads(value) {
2919
+ this._threads = value;
2920
+ this.notify();
2921
+ }
2922
+ // ============================================
2923
+ // Mutations
2924
+ // ============================================
2925
+ setThreads(threads) {
2926
+ this._threads = threads;
2927
+ this.notify();
2928
+ }
2929
+ setCurrentThread(thread) {
2930
+ this._currentThread = thread;
2931
+ this._currentThreadId = thread?.id ?? null;
2932
+ this.notify();
2933
+ }
2934
+ setCurrentThreadId(id) {
2935
+ this._currentThreadId = id;
2936
+ this.notify();
2937
+ }
2938
+ addThread(thread) {
2939
+ this._threads = [thread, ...this._threads];
2940
+ this.notify();
2941
+ }
2942
+ updateThread(id, updates) {
2943
+ this._threads = this._threads.map(
2944
+ (t) => t.id === id ? { ...t, ...updates } : t
2945
+ );
2946
+ if (updates.updatedAt) {
2947
+ this._threads = [...this._threads].sort(
2948
+ (a, b) => b.updatedAt.getTime() - a.updatedAt.getTime()
2949
+ );
2950
+ }
2951
+ if (this._currentThread?.id === id) {
2952
+ this._currentThread = { ...this._currentThread, ...updates };
2953
+ }
2954
+ this.notify();
2955
+ }
2956
+ removeThread(id) {
2957
+ this._threads = this._threads.filter((t) => t.id !== id);
2958
+ if (this._currentThreadId === id) {
2959
+ this._currentThreadId = null;
2960
+ this._currentThread = null;
2961
+ }
2962
+ this.notify();
2963
+ }
2964
+ setLoadStatus(status) {
2965
+ this._loadStatus = status;
2966
+ this.notify();
2967
+ }
2968
+ setError(error) {
2969
+ this._error = error;
2970
+ this.notify();
2971
+ }
2972
+ // ============================================
2973
+ // Snapshots (for useSyncExternalStore)
2974
+ // ============================================
2975
+ getThreadsSnapshot() {
2976
+ return this._threads;
2977
+ }
2978
+ getCurrentThreadSnapshot() {
2979
+ return this._currentThread;
2980
+ }
2981
+ getLoadStatusSnapshot() {
2982
+ return this._loadStatus;
2983
+ }
2984
+ getErrorSnapshot() {
2985
+ return this._error;
2986
+ }
2987
+ // ============================================
2988
+ // Private Methods
2989
+ // ============================================
2990
+ notify() {
2991
+ this.subscribers.forEach((cb) => cb());
2992
+ }
2993
+ /**
2994
+ * Cleanup subscriptions
2995
+ */
2996
+ dispose() {
2997
+ this.subscribers.clear();
2998
+ }
2999
+ };
3000
+ function createReactThreadManagerState(initialThreads) {
3001
+ return new ReactThreadManagerState(initialThreads);
3002
+ }
3003
+
3004
+ // src/react/internal/ReactThreadManager.ts
3005
+ var _ReactThreadManager = class _ReactThreadManager extends ThreadManager {
3006
+ constructor(config = {}, callbacks = {}) {
3007
+ const reactState = new ReactThreadManagerState();
3008
+ super({ ...config, state: reactState }, callbacks);
3009
+ // ============================================
3010
+ // Subscription Methods (for useSyncExternalStore)
3011
+ // ============================================
3012
+ /**
3013
+ * Subscribe to state changes
3014
+ * Use with useSyncExternalStore
3015
+ */
3016
+ this.subscribe = (callback) => {
3017
+ return this.state.subscribe(callback);
3018
+ };
3019
+ // ============================================
3020
+ // Snapshot Getters (for useSyncExternalStore)
3021
+ // ============================================
3022
+ /**
3023
+ * Get threads snapshot
3024
+ */
3025
+ this.getThreadsSnapshot = () => {
3026
+ return this.state.getThreadsSnapshot();
3027
+ };
3028
+ /**
3029
+ * Get current thread snapshot
3030
+ */
3031
+ this.getCurrentThreadSnapshot = () => {
3032
+ return this.state.getCurrentThreadSnapshot();
3033
+ };
3034
+ /**
3035
+ * Get current thread ID snapshot
3036
+ */
3037
+ this.getCurrentThreadIdSnapshot = () => {
3038
+ return this.state.currentThreadId;
3039
+ };
3040
+ /**
3041
+ * Get load status snapshot
3042
+ */
3043
+ this.getLoadStatusSnapshot = () => {
3044
+ return this.state.getLoadStatusSnapshot();
3045
+ };
3046
+ /**
3047
+ * Get error snapshot
3048
+ */
3049
+ this.getErrorSnapshot = () => {
3050
+ return this.state.getErrorSnapshot();
3051
+ };
3052
+ /**
3053
+ * Get isLoading snapshot
3054
+ */
3055
+ this.getIsLoadingSnapshot = () => {
3056
+ return this.state.getLoadStatusSnapshot() === "loading";
3057
+ };
3058
+ /**
3059
+ * Get threads snapshot for server (always empty for hydration consistency)
3060
+ */
3061
+ this.getThreadsServerSnapshot = () => {
3062
+ return _ReactThreadManager.EMPTY_THREADS;
3063
+ };
3064
+ /**
3065
+ * Get current thread snapshot for server (always null)
3066
+ */
3067
+ this.getCurrentThreadServerSnapshot = () => {
3068
+ return null;
3069
+ };
3070
+ /**
3071
+ * Get current thread ID snapshot for server (always null)
3072
+ */
3073
+ this.getCurrentThreadIdServerSnapshot = () => {
3074
+ return null;
3075
+ };
3076
+ /**
3077
+ * Get load status snapshot for server (always "idle")
3078
+ */
3079
+ this.getLoadStatusServerSnapshot = () => {
3080
+ return _ReactThreadManager.IDLE_STATUS;
3081
+ };
3082
+ /**
3083
+ * Get error snapshot for server (always undefined)
3084
+ */
3085
+ this.getErrorServerSnapshot = () => {
3086
+ return void 0;
3087
+ };
3088
+ /**
3089
+ * Get isLoading snapshot for server (always false)
3090
+ */
3091
+ this.getIsLoadingServerSnapshot = () => {
3092
+ return false;
3093
+ };
3094
+ }
3095
+ // ============================================
3096
+ // Cleanup
3097
+ // ============================================
3098
+ /**
3099
+ * Dispose of the manager
3100
+ */
3101
+ async dispose() {
3102
+ this.state.dispose();
3103
+ await super.dispose();
3104
+ }
3105
+ };
3106
+ // ============================================
3107
+ // Server Snapshots (for SSR - stable cached values)
3108
+ // ============================================
3109
+ // Cached values for server snapshots (must be stable references)
3110
+ _ReactThreadManager.EMPTY_THREADS = [];
3111
+ _ReactThreadManager.IDLE_STATUS = "idle";
3112
+ var ReactThreadManager = _ReactThreadManager;
3113
+ function createReactThreadManager(config, callbacks) {
3114
+ return new ReactThreadManager(config, callbacks);
3115
+ }
3116
+
3117
+ // src/react/hooks/useThreadManager.ts
3118
+ var defaultManager = null;
3119
+ function getDefaultManager() {
3120
+ if (!defaultManager) {
3121
+ defaultManager = createReactThreadManager();
3122
+ }
3123
+ return defaultManager;
3124
+ }
3125
+ var internalManager = null;
3126
+ function getInternalManager(config) {
3127
+ if (!internalManager) {
3128
+ internalManager = createReactThreadManager(
3129
+ {
3130
+ adapter: config.adapter,
3131
+ saveDebounce: config.saveDebounce,
3132
+ autoLoad: config.autoLoad,
3133
+ autoRestoreLastThread: config.autoRestoreLastThread
3134
+ },
3135
+ config.callbacks
3136
+ );
3137
+ }
3138
+ return internalManager;
3139
+ }
3140
+ function useThreadManager(config) {
3141
+ const manager = useMemo(() => {
3142
+ if (!config) {
3143
+ return getDefaultManager();
3144
+ }
3145
+ if (!config.adapter) {
3146
+ return getInternalManager(config);
3147
+ }
3148
+ console.log("[useThreadManager] Creating new manager with custom adapter");
3149
+ return createReactThreadManager(
3150
+ {
3151
+ adapter: config.adapter,
3152
+ saveDebounce: config.saveDebounce,
3153
+ autoLoad: config.autoLoad,
3154
+ autoRestoreLastThread: config.autoRestoreLastThread
3155
+ },
3156
+ config.callbacks
3157
+ );
3158
+ }, [
3159
+ config?.adapter,
3160
+ config?.saveDebounce,
3161
+ config?.autoLoad,
3162
+ config?.autoRestoreLastThread
3163
+ // Note: callbacks are intentionally not in deps to avoid recreating manager
3164
+ ]);
3165
+ const threads = useSyncExternalStore(
3166
+ manager.subscribe,
3167
+ manager.getThreadsSnapshot,
3168
+ manager.getThreadsServerSnapshot
3169
+ // SSR - always empty array
3170
+ );
3171
+ const currentThread = useSyncExternalStore(
3172
+ manager.subscribe,
3173
+ manager.getCurrentThreadSnapshot,
3174
+ manager.getCurrentThreadServerSnapshot
3175
+ // SSR - always null
3176
+ );
3177
+ const currentThreadId = useSyncExternalStore(
3178
+ manager.subscribe,
3179
+ manager.getCurrentThreadIdSnapshot,
3180
+ manager.getCurrentThreadIdServerSnapshot
3181
+ // SSR - always null
3182
+ );
3183
+ const loadStatus = useSyncExternalStore(
3184
+ manager.subscribe,
3185
+ manager.getLoadStatusSnapshot,
3186
+ manager.getLoadStatusServerSnapshot
3187
+ // SSR - always "idle"
3188
+ );
3189
+ const error = useSyncExternalStore(
3190
+ manager.subscribe,
3191
+ manager.getErrorSnapshot,
3192
+ manager.getErrorServerSnapshot
3193
+ // SSR - always undefined
3194
+ );
3195
+ const isLoading = useSyncExternalStore(
3196
+ manager.subscribe,
3197
+ manager.getIsLoadingSnapshot,
3198
+ manager.getIsLoadingServerSnapshot
3199
+ // SSR - always false
3200
+ );
3201
+ useEffect(() => {
3202
+ return () => {
3203
+ if (config?.adapter && manager !== defaultManager && manager !== internalManager) {
3204
+ manager.dispose();
3205
+ }
3206
+ };
3207
+ }, [manager, config]);
3208
+ useEffect(() => {
3209
+ const handleBeforeUnload = () => {
3210
+ if (manager.hasPendingChanges) {
3211
+ manager.saveNow().catch(() => {
3212
+ });
3213
+ }
3214
+ };
3215
+ if (typeof window !== "undefined") {
3216
+ window.addEventListener("beforeunload", handleBeforeUnload);
3217
+ return () => {
3218
+ window.removeEventListener("beforeunload", handleBeforeUnload);
3219
+ };
3220
+ }
3221
+ }, [manager]);
3222
+ const createThread = useCallback(
3223
+ (options) => manager.createThread(options),
3224
+ [manager]
3225
+ );
3226
+ const switchThread = useCallback(
3227
+ (id) => manager.switchThread(id),
3228
+ [manager]
3229
+ );
3230
+ const updateCurrentThread = useCallback(
3231
+ (updates) => manager.updateCurrentThread(updates),
3232
+ [manager]
3233
+ );
3234
+ const deleteThread = useCallback(
3235
+ (id) => manager.deleteThread(id),
3236
+ [manager]
3237
+ );
3238
+ const clearCurrentThread = useCallback(
3239
+ () => manager.clearCurrentThread(),
3240
+ [manager]
3241
+ );
3242
+ const refreshThreads = useCallback(() => manager.loadThreads(), [manager]);
3243
+ const saveNow = useCallback(() => manager.saveNow(), [manager]);
3244
+ const clearAllThreads = useCallback(
3245
+ () => manager.clearAllThreads(),
3246
+ [manager]
3247
+ );
3248
+ const messages = useMemo(
3249
+ () => currentThread?.messages ?? [],
3250
+ [currentThread]
3251
+ );
3252
+ const setMessages = useCallback(
3253
+ (newMessages) => updateCurrentThread({ messages: newMessages }),
3254
+ [updateCurrentThread]
3255
+ );
3256
+ const hasPendingChanges = manager.hasPendingChanges;
3257
+ return {
3258
+ // State
3259
+ threads,
3260
+ currentThread,
3261
+ currentThreadId,
3262
+ isLoading,
3263
+ loadStatus,
3264
+ error,
3265
+ // Actions
3266
+ createThread,
3267
+ switchThread,
3268
+ updateCurrentThread,
3269
+ deleteThread,
3270
+ clearCurrentThread,
3271
+ refreshThreads,
3272
+ saveNow,
3273
+ clearAllThreads,
3274
+ // Utilities
3275
+ messages,
3276
+ setMessages,
3277
+ hasPendingChanges
3278
+ };
3279
+ }
3280
+
2857
3281
  // src/react/utils/permission-storage.ts
2858
3282
  var DEFAULT_KEY_PREFIX = "yourgpt-permissions";
2859
3283
  function createPermissionStorage(config) {
@@ -3102,6 +3526,6 @@ function useChat(config) {
3102
3526
  };
3103
3527
  }
3104
3528
 
3105
- export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, createPermissionStorage, createReactChat, createReactChatState, createSessionPermissionCache, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useSuggestions, useSupportedMediaTypes, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
3106
- //# sourceMappingURL=chunk-FO75W5UI.js.map
3107
- //# sourceMappingURL=chunk-FO75W5UI.js.map
3529
+ export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, ReactThreadManager, ReactThreadManagerState, createPermissionStorage, createReactChat, createReactChatState, createReactThreadManager, createReactThreadManagerState, createSessionPermissionCache, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useSuggestions, useSupportedMediaTypes, useThreadManager, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
3530
+ //# sourceMappingURL=chunk-XAVZZVUL.js.map
3531
+ //# sourceMappingURL=chunk-XAVZZVUL.js.map