@yourgpt/copilot-sdk 1.4.31 → 1.4.33

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 { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-55EABH45.js';
1
+ import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-JM7PB2LP.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';
@@ -416,7 +416,7 @@ var AbstractChat = class {
416
416
  * Dynamic context from useAIContext hook
417
417
  */
418
418
  this.dynamicContext = "";
419
- this.isDisposed = false;
419
+ this._isDisposed = false;
420
420
  this.config = {
421
421
  runtimeUrl: init.runtimeUrl,
422
422
  llm: init.llm,
@@ -884,18 +884,35 @@ ${this.dynamicContext}`.trim() : this.config.systemPrompt,
884
884
  isAsyncIterable(value) {
885
885
  return value !== null && typeof value === "object" && Symbol.asyncIterator in value;
886
886
  }
887
+ /**
888
+ * Whether this instance has been disposed
889
+ */
890
+ get disposed() {
891
+ return this._isDisposed;
892
+ }
887
893
  /**
888
894
  * Dispose and cleanup
895
+ * Note: Event handlers are NOT cleared to support React StrictMode revive()
889
896
  */
890
897
  dispose() {
891
- if (this.isDisposed) {
898
+ if (this._isDisposed) {
892
899
  this.debug("dispose() called but already disposed - ignoring");
893
900
  return;
894
901
  }
895
- this.debug("dispose() - clearing event handlers");
896
- this.isDisposed = true;
902
+ this.debug("dispose() - stopping active requests");
903
+ this._isDisposed = true;
897
904
  this.stop();
898
- this.eventHandlers.clear();
905
+ }
906
+ /**
907
+ * Revive a disposed instance (for React StrictMode compatibility)
908
+ * This allows reusing an instance after dispose() was called
909
+ */
910
+ revive() {
911
+ if (!this._isDisposed) {
912
+ return;
913
+ }
914
+ this.debug("revive() - restoring disposed instance");
915
+ this._isDisposed = false;
899
916
  }
900
917
  };
901
918
 
@@ -910,10 +927,18 @@ var AbstractAgentLoop = class {
910
927
  this._isCancelled = false;
911
928
  // Cancellation support
912
929
  this.abortController = null;
913
- // Registered tools
930
+ // Registered tools with reference counting for React StrictMode compatibility
931
+ // Tools are never fully removed during a session - only marked as inactive
914
932
  this.registeredTools = /* @__PURE__ */ new Map();
915
933
  // Pending approvals - resolve with approval result including extraData
916
934
  this.pendingApprovals = /* @__PURE__ */ new Map();
935
+ // ============================================
936
+ // Cleanup
937
+ // ============================================
938
+ /**
939
+ * Dispose of resources
940
+ */
941
+ this._isDisposed = false;
917
942
  this.config = config;
918
943
  this.callbacks = callbacks;
919
944
  this._maxIterations = config.maxIterations ?? 20;
@@ -963,7 +988,7 @@ var AbstractAgentLoop = class {
963
988
  );
964
989
  }
965
990
  get tools() {
966
- return Array.from(this.registeredTools.values());
991
+ return Array.from(this.registeredTools.values()).filter((entry) => entry.active).map((entry) => entry.tool);
967
992
  }
968
993
  // ============================================
969
994
  // Private setters with callbacks
@@ -997,22 +1022,42 @@ var AbstractAgentLoop = class {
997
1022
  // Tool Registration
998
1023
  // ============================================
999
1024
  /**
1000
- * Register a tool
1025
+ * Register a tool (reference counted for React StrictMode compatibility)
1026
+ * Tools are never fully removed - ref counting ensures StrictMode works correctly
1001
1027
  */
1002
1028
  registerTool(tool) {
1003
- this.registeredTools.set(tool.name, tool);
1029
+ const existing = this.registeredTools.get(tool.name);
1030
+ if (existing) {
1031
+ existing.tool = tool;
1032
+ existing.refCount++;
1033
+ existing.active = true;
1034
+ } else {
1035
+ this.registeredTools.set(tool.name, {
1036
+ tool,
1037
+ refCount: 1,
1038
+ active: true
1039
+ });
1040
+ }
1004
1041
  }
1005
1042
  /**
1006
- * Unregister a tool
1043
+ * Unregister a tool (reference counted for React StrictMode compatibility)
1044
+ * Tool is only marked inactive when refCount reaches 0, never deleted
1007
1045
  */
1008
1046
  unregisterTool(name) {
1009
- this.registeredTools.delete(name);
1047
+ const entry = this.registeredTools.get(name);
1048
+ if (entry) {
1049
+ entry.refCount = Math.max(0, entry.refCount - 1);
1050
+ if (entry.refCount === 0) {
1051
+ entry.active = false;
1052
+ }
1053
+ }
1010
1054
  }
1011
1055
  /**
1012
- * Get a registered tool
1056
+ * Get a registered tool (returns active tools, or inactive if forExecution=true)
1013
1057
  */
1014
1058
  getTool(name) {
1015
- return this.registeredTools.get(name);
1059
+ const entry = this.registeredTools.get(name);
1060
+ return entry?.tool;
1016
1061
  }
1017
1062
  // ============================================
1018
1063
  // Tool Execution
@@ -1054,7 +1099,7 @@ var AbstractAgentLoop = class {
1054
1099
  * Execute a single tool
1055
1100
  */
1056
1101
  async executeSingleTool(toolCall) {
1057
- const tool = this.registeredTools.get(toolCall.name);
1102
+ const tool = this.getTool(toolCall.name);
1058
1103
  const execution = {
1059
1104
  id: toolCall.id,
1060
1105
  toolCallId: toolCall.id,
@@ -1265,20 +1310,39 @@ var AbstractAgentLoop = class {
1265
1310
  updateCallbacks(callbacks) {
1266
1311
  this.callbacks = { ...this.callbacks, ...callbacks };
1267
1312
  }
1268
- // ============================================
1269
- // Cleanup
1270
- // ============================================
1271
1313
  /**
1272
- * Dispose of resources
1314
+ * Whether this instance has been disposed
1315
+ */
1316
+ get disposed() {
1317
+ return this._isDisposed;
1318
+ }
1319
+ /**
1320
+ * Dispose and cleanup
1321
+ * Note: Tools are NOT cleared to support React StrictMode revive()
1273
1322
  */
1274
1323
  dispose() {
1324
+ if (this._isDisposed) {
1325
+ return;
1326
+ }
1327
+ this._isDisposed = true;
1275
1328
  for (const [_id, pending] of this.pendingApprovals) {
1276
1329
  pending.resolve({ approved: false });
1277
1330
  }
1278
1331
  this.pendingApprovals.clear();
1279
- this.registeredTools.clear();
1280
1332
  this._toolExecutions = [];
1281
1333
  }
1334
+ /**
1335
+ * Revive a disposed instance (for React StrictMode compatibility)
1336
+ * Tools are preserved across dispose/revive cycle
1337
+ */
1338
+ revive() {
1339
+ if (!this._isDisposed) {
1340
+ return;
1341
+ }
1342
+ this._isDisposed = false;
1343
+ this._isCancelled = false;
1344
+ this._maxIterationsReached = false;
1345
+ }
1282
1346
  };
1283
1347
 
1284
1348
  // src/chat/ChatWithTools.ts
@@ -1330,13 +1394,11 @@ var ChatWithTools = class {
1330
1394
  wireEvents() {
1331
1395
  this.debug("Wiring up toolCalls event handler");
1332
1396
  this.chat.on("toolCalls", async (event) => {
1333
- this.debug("\u{1F3AF} toolCalls event handler FIRED", event);
1334
1397
  const toolCalls = event.toolCalls;
1335
1398
  if (!toolCalls?.length) {
1336
- this.debug("No tool calls in event");
1337
1399
  return;
1338
1400
  }
1339
- this.debug("Tool calls received:", toolCalls);
1401
+ this.debug("Tool calls received:", toolCalls.length);
1340
1402
  const toolCallInfos = toolCalls.map((tc) => {
1341
1403
  const tcAny = tc;
1342
1404
  const name = tcAny.function?.name ?? tcAny.name ?? "";
@@ -1541,6 +1603,21 @@ var ChatWithTools = class {
1541
1603
  // ============================================
1542
1604
  // Cleanup
1543
1605
  // ============================================
1606
+ /**
1607
+ * Whether this instance has been disposed
1608
+ */
1609
+ get disposed() {
1610
+ return this.chat.disposed;
1611
+ }
1612
+ /**
1613
+ * Revive a disposed instance (for React StrictMode compatibility)
1614
+ * This allows reusing an instance after dispose() was called,
1615
+ * preserving registered tools and state
1616
+ */
1617
+ revive() {
1618
+ this.chat.revive();
1619
+ this.agentLoop.revive();
1620
+ }
1544
1621
  /**
1545
1622
  * Dispose and cleanup
1546
1623
  */
@@ -1672,6 +1749,12 @@ var ReactChatState = class {
1672
1749
  dispose() {
1673
1750
  this.subscribers.clear();
1674
1751
  }
1752
+ /**
1753
+ * Revive after dispose (for React StrictMode compatibility)
1754
+ * Subscribers will be re-added automatically via useSyncExternalStore
1755
+ */
1756
+ revive() {
1757
+ }
1675
1758
  };
1676
1759
  function createReactChatState(initialMessages) {
1677
1760
  return new ReactChatState(initialMessages);
@@ -1697,6 +1780,13 @@ var ReactChatWithTools = class extends ChatWithTools {
1697
1780
  super.dispose();
1698
1781
  this.reactState.dispose();
1699
1782
  }
1783
+ /**
1784
+ * Revive a disposed instance (for React StrictMode compatibility)
1785
+ */
1786
+ revive() {
1787
+ super.revive();
1788
+ this.reactState.revive();
1789
+ }
1700
1790
  };
1701
1791
 
1702
1792
  // src/react/utils/context-tree.ts
@@ -1808,6 +1898,10 @@ function CopilotProvider({
1808
1898
  }, [toolsConfig]);
1809
1899
  const [toolExecutions, setToolExecutions] = useState([]);
1810
1900
  const chatRef = useRef(null);
1901
+ if (chatRef.current !== null && chatRef.current.disposed) {
1902
+ chatRef.current.revive();
1903
+ debugLog("Revived disposed instance (React StrictMode)");
1904
+ }
1811
1905
  if (chatRef.current === null) {
1812
1906
  const uiInitialMessages = initialMessages?.map(
1813
1907
  (m) => ({
@@ -2415,16 +2509,6 @@ function useTools(tools) {
2415
2509
  };
2416
2510
  }, [toolsKey]);
2417
2511
  }
2418
- var CopilotContext2 = createContext(null);
2419
- function useCopilotContext() {
2420
- const context = useContext(CopilotContext2);
2421
- if (!context) {
2422
- throw new Error("useCopilotContext must be used within a CopilotProvider");
2423
- }
2424
- return context;
2425
- }
2426
-
2427
- // src/react/hooks/useToolWithSchema.ts
2428
2512
  function convertZodSchema(schema, _toolName) {
2429
2513
  try {
2430
2514
  const zodWithJsonSchema = z;
@@ -2445,7 +2529,7 @@ function convertZodSchema(schema, _toolName) {
2445
2529
  return zodObjectToInputSchema(schema);
2446
2530
  }
2447
2531
  function useToolWithSchema(config, dependencies = []) {
2448
- const { registerTool, unregisterTool } = useCopilotContext();
2532
+ const { registerTool, unregisterTool } = useCopilot();
2449
2533
  const configRef = useRef(config);
2450
2534
  configRef.current = config;
2451
2535
  const inputSchema = useMemo(() => {
@@ -2481,7 +2565,7 @@ function useToolWithSchema(config, dependencies = []) {
2481
2565
  }, [config.name, inputSchema, ...dependencies]);
2482
2566
  }
2483
2567
  function useToolsWithSchema(tools, dependencies = []) {
2484
- const { registerTool, unregisterTool } = useCopilotContext();
2568
+ const { registerTool, unregisterTool } = useCopilot();
2485
2569
  const toolsRef = useRef(tools);
2486
2570
  toolsRef.current = tools;
2487
2571
  useEffect(() => {
@@ -2523,6 +2607,16 @@ function useToolsWithSchema(tools, dependencies = []) {
2523
2607
  };
2524
2608
  }, [tools.map((t) => t.name).join(","), ...dependencies]);
2525
2609
  }
2610
+ var CopilotContext2 = createContext(null);
2611
+ function useCopilotContext() {
2612
+ const context = useContext(CopilotContext2);
2613
+ if (!context) {
2614
+ throw new Error("useCopilotContext must be used within a CopilotProvider");
2615
+ }
2616
+ return context;
2617
+ }
2618
+
2619
+ // src/react/hooks/useToolExecutor.ts
2526
2620
  function useToolExecutor() {
2527
2621
  const {
2528
2622
  registeredTools,
@@ -2863,7 +2957,7 @@ function formatKnowledgeResultsForAI(results) {
2863
2957
 
2864
2958
  // src/react/hooks/useKnowledgeBase.ts
2865
2959
  function useKnowledgeBase(config) {
2866
- const { registerTool, unregisterTool } = useCopilotContext();
2960
+ const { registerTool, unregisterTool } = useCopilot();
2867
2961
  const configRef = useRef(config);
2868
2962
  configRef.current = config;
2869
2963
  const handleSearch = useCallback(
@@ -3667,6 +3761,13 @@ var ReactChat = class extends AbstractChat {
3667
3761
  super.dispose();
3668
3762
  this.reactState.dispose();
3669
3763
  }
3764
+ /**
3765
+ * Revive a disposed instance (for React StrictMode compatibility)
3766
+ */
3767
+ revive() {
3768
+ super.revive();
3769
+ this.reactState.revive();
3770
+ }
3670
3771
  };
3671
3772
  function createReactChat(config) {
3672
3773
  return new ReactChat(config);
@@ -3674,6 +3775,9 @@ function createReactChat(config) {
3674
3775
  function useChat(config) {
3675
3776
  const chatRef = useRef(null);
3676
3777
  const [input, setInput] = useState("");
3778
+ if (chatRef.current !== null && chatRef.current.disposed) {
3779
+ chatRef.current.revive();
3780
+ }
3677
3781
  if (chatRef.current === null) {
3678
3782
  chatRef.current = createReactChat({
3679
3783
  runtimeUrl: config.runtimeUrl,
@@ -3759,5 +3863,5 @@ function useChat(config) {
3759
3863
  }
3760
3864
 
3761
3865
  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 };
3762
- //# sourceMappingURL=chunk-CGBPUUOT.js.map
3763
- //# sourceMappingURL=chunk-CGBPUUOT.js.map
3866
+ //# sourceMappingURL=chunk-CJ7UWN2Y.js.map
3867
+ //# sourceMappingURL=chunk-CJ7UWN2Y.js.map