@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,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var chunkCVKN4H62_cjs = require('./chunk-CVKN4H62.cjs');
3
+ var chunk4PRWNAXQ_cjs = require('./chunk-4PRWNAXQ.cjs');
4
4
  var react = require('react');
5
5
  var jsxRuntime = require('react/jsx-runtime');
6
6
  var z = require('zod');
@@ -438,7 +438,7 @@ var AbstractChat = class {
438
438
  * Dynamic context from useAIContext hook
439
439
  */
440
440
  this.dynamicContext = "";
441
- this.isDisposed = false;
441
+ this._isDisposed = false;
442
442
  this.config = {
443
443
  runtimeUrl: init.runtimeUrl,
444
444
  llm: init.llm,
@@ -906,18 +906,35 @@ ${this.dynamicContext}`.trim() : this.config.systemPrompt,
906
906
  isAsyncIterable(value) {
907
907
  return value !== null && typeof value === "object" && Symbol.asyncIterator in value;
908
908
  }
909
+ /**
910
+ * Whether this instance has been disposed
911
+ */
912
+ get disposed() {
913
+ return this._isDisposed;
914
+ }
909
915
  /**
910
916
  * Dispose and cleanup
917
+ * Note: Event handlers are NOT cleared to support React StrictMode revive()
911
918
  */
912
919
  dispose() {
913
- if (this.isDisposed) {
920
+ if (this._isDisposed) {
914
921
  this.debug("dispose() called but already disposed - ignoring");
915
922
  return;
916
923
  }
917
- this.debug("dispose() - clearing event handlers");
918
- this.isDisposed = true;
924
+ this.debug("dispose() - stopping active requests");
925
+ this._isDisposed = true;
919
926
  this.stop();
920
- this.eventHandlers.clear();
927
+ }
928
+ /**
929
+ * Revive a disposed instance (for React StrictMode compatibility)
930
+ * This allows reusing an instance after dispose() was called
931
+ */
932
+ revive() {
933
+ if (!this._isDisposed) {
934
+ return;
935
+ }
936
+ this.debug("revive() - restoring disposed instance");
937
+ this._isDisposed = false;
921
938
  }
922
939
  };
923
940
 
@@ -932,10 +949,18 @@ var AbstractAgentLoop = class {
932
949
  this._isCancelled = false;
933
950
  // Cancellation support
934
951
  this.abortController = null;
935
- // Registered tools
952
+ // Registered tools with reference counting for React StrictMode compatibility
953
+ // Tools are never fully removed during a session - only marked as inactive
936
954
  this.registeredTools = /* @__PURE__ */ new Map();
937
955
  // Pending approvals - resolve with approval result including extraData
938
956
  this.pendingApprovals = /* @__PURE__ */ new Map();
957
+ // ============================================
958
+ // Cleanup
959
+ // ============================================
960
+ /**
961
+ * Dispose of resources
962
+ */
963
+ this._isDisposed = false;
939
964
  this.config = config;
940
965
  this.callbacks = callbacks;
941
966
  this._maxIterations = config.maxIterations ?? 20;
@@ -985,7 +1010,7 @@ var AbstractAgentLoop = class {
985
1010
  );
986
1011
  }
987
1012
  get tools() {
988
- return Array.from(this.registeredTools.values());
1013
+ return Array.from(this.registeredTools.values()).filter((entry) => entry.active).map((entry) => entry.tool);
989
1014
  }
990
1015
  // ============================================
991
1016
  // Private setters with callbacks
@@ -1019,22 +1044,42 @@ var AbstractAgentLoop = class {
1019
1044
  // Tool Registration
1020
1045
  // ============================================
1021
1046
  /**
1022
- * Register a tool
1047
+ * Register a tool (reference counted for React StrictMode compatibility)
1048
+ * Tools are never fully removed - ref counting ensures StrictMode works correctly
1023
1049
  */
1024
1050
  registerTool(tool) {
1025
- this.registeredTools.set(tool.name, tool);
1051
+ const existing = this.registeredTools.get(tool.name);
1052
+ if (existing) {
1053
+ existing.tool = tool;
1054
+ existing.refCount++;
1055
+ existing.active = true;
1056
+ } else {
1057
+ this.registeredTools.set(tool.name, {
1058
+ tool,
1059
+ refCount: 1,
1060
+ active: true
1061
+ });
1062
+ }
1026
1063
  }
1027
1064
  /**
1028
- * Unregister a tool
1065
+ * Unregister a tool (reference counted for React StrictMode compatibility)
1066
+ * Tool is only marked inactive when refCount reaches 0, never deleted
1029
1067
  */
1030
1068
  unregisterTool(name) {
1031
- this.registeredTools.delete(name);
1069
+ const entry = this.registeredTools.get(name);
1070
+ if (entry) {
1071
+ entry.refCount = Math.max(0, entry.refCount - 1);
1072
+ if (entry.refCount === 0) {
1073
+ entry.active = false;
1074
+ }
1075
+ }
1032
1076
  }
1033
1077
  /**
1034
- * Get a registered tool
1078
+ * Get a registered tool (returns active tools, or inactive if forExecution=true)
1035
1079
  */
1036
1080
  getTool(name) {
1037
- return this.registeredTools.get(name);
1081
+ const entry = this.registeredTools.get(name);
1082
+ return entry?.tool;
1038
1083
  }
1039
1084
  // ============================================
1040
1085
  // Tool Execution
@@ -1076,7 +1121,7 @@ var AbstractAgentLoop = class {
1076
1121
  * Execute a single tool
1077
1122
  */
1078
1123
  async executeSingleTool(toolCall) {
1079
- const tool = this.registeredTools.get(toolCall.name);
1124
+ const tool = this.getTool(toolCall.name);
1080
1125
  const execution = {
1081
1126
  id: toolCall.id,
1082
1127
  toolCallId: toolCall.id,
@@ -1287,20 +1332,39 @@ var AbstractAgentLoop = class {
1287
1332
  updateCallbacks(callbacks) {
1288
1333
  this.callbacks = { ...this.callbacks, ...callbacks };
1289
1334
  }
1290
- // ============================================
1291
- // Cleanup
1292
- // ============================================
1293
1335
  /**
1294
- * Dispose of resources
1336
+ * Whether this instance has been disposed
1337
+ */
1338
+ get disposed() {
1339
+ return this._isDisposed;
1340
+ }
1341
+ /**
1342
+ * Dispose and cleanup
1343
+ * Note: Tools are NOT cleared to support React StrictMode revive()
1295
1344
  */
1296
1345
  dispose() {
1346
+ if (this._isDisposed) {
1347
+ return;
1348
+ }
1349
+ this._isDisposed = true;
1297
1350
  for (const [_id, pending] of this.pendingApprovals) {
1298
1351
  pending.resolve({ approved: false });
1299
1352
  }
1300
1353
  this.pendingApprovals.clear();
1301
- this.registeredTools.clear();
1302
1354
  this._toolExecutions = [];
1303
1355
  }
1356
+ /**
1357
+ * Revive a disposed instance (for React StrictMode compatibility)
1358
+ * Tools are preserved across dispose/revive cycle
1359
+ */
1360
+ revive() {
1361
+ if (!this._isDisposed) {
1362
+ return;
1363
+ }
1364
+ this._isDisposed = false;
1365
+ this._isCancelled = false;
1366
+ this._maxIterationsReached = false;
1367
+ }
1304
1368
  };
1305
1369
 
1306
1370
  // src/chat/ChatWithTools.ts
@@ -1352,13 +1416,11 @@ var ChatWithTools = class {
1352
1416
  wireEvents() {
1353
1417
  this.debug("Wiring up toolCalls event handler");
1354
1418
  this.chat.on("toolCalls", async (event) => {
1355
- this.debug("\u{1F3AF} toolCalls event handler FIRED", event);
1356
1419
  const toolCalls = event.toolCalls;
1357
1420
  if (!toolCalls?.length) {
1358
- this.debug("No tool calls in event");
1359
1421
  return;
1360
1422
  }
1361
- this.debug("Tool calls received:", toolCalls);
1423
+ this.debug("Tool calls received:", toolCalls.length);
1362
1424
  const toolCallInfos = toolCalls.map((tc) => {
1363
1425
  const tcAny = tc;
1364
1426
  const name = tcAny.function?.name ?? tcAny.name ?? "";
@@ -1563,6 +1625,21 @@ var ChatWithTools = class {
1563
1625
  // ============================================
1564
1626
  // Cleanup
1565
1627
  // ============================================
1628
+ /**
1629
+ * Whether this instance has been disposed
1630
+ */
1631
+ get disposed() {
1632
+ return this.chat.disposed;
1633
+ }
1634
+ /**
1635
+ * Revive a disposed instance (for React StrictMode compatibility)
1636
+ * This allows reusing an instance after dispose() was called,
1637
+ * preserving registered tools and state
1638
+ */
1639
+ revive() {
1640
+ this.chat.revive();
1641
+ this.agentLoop.revive();
1642
+ }
1566
1643
  /**
1567
1644
  * Dispose and cleanup
1568
1645
  */
@@ -1694,6 +1771,12 @@ var ReactChatState = class {
1694
1771
  dispose() {
1695
1772
  this.subscribers.clear();
1696
1773
  }
1774
+ /**
1775
+ * Revive after dispose (for React StrictMode compatibility)
1776
+ * Subscribers will be re-added automatically via useSyncExternalStore
1777
+ */
1778
+ revive() {
1779
+ }
1697
1780
  };
1698
1781
  function createReactChatState(initialMessages) {
1699
1782
  return new ReactChatState(initialMessages);
@@ -1719,6 +1802,13 @@ var ReactChatWithTools = class extends ChatWithTools {
1719
1802
  super.dispose();
1720
1803
  this.reactState.dispose();
1721
1804
  }
1805
+ /**
1806
+ * Revive a disposed instance (for React StrictMode compatibility)
1807
+ */
1808
+ revive() {
1809
+ super.revive();
1810
+ this.reactState.revive();
1811
+ }
1722
1812
  };
1723
1813
 
1724
1814
  // src/react/utils/context-tree.ts
@@ -1830,6 +1920,10 @@ function CopilotProvider({
1830
1920
  }, [toolsConfig]);
1831
1921
  const [toolExecutions, setToolExecutions] = react.useState([]);
1832
1922
  const chatRef = react.useRef(null);
1923
+ if (chatRef.current !== null && chatRef.current.disposed) {
1924
+ chatRef.current.revive();
1925
+ debugLog("Revived disposed instance (React StrictMode)");
1926
+ }
1833
1927
  if (chatRef.current === null) {
1834
1928
  const uiInitialMessages = initialMessages?.map(
1835
1929
  (m) => ({
@@ -2164,17 +2258,17 @@ function useAITools(options = {}) {
2164
2258
  const rememberedConsentRef = react.useRef(/* @__PURE__ */ new Set());
2165
2259
  react.useEffect(() => {
2166
2260
  if (!autoStart || !isEnabled) return;
2167
- if (consoleCapture && !chunkCVKN4H62_cjs.isConsoleCaptureActive()) {
2168
- chunkCVKN4H62_cjs.startConsoleCapture(consoleOptions);
2261
+ if (consoleCapture && !chunk4PRWNAXQ_cjs.isConsoleCaptureActive()) {
2262
+ chunk4PRWNAXQ_cjs.startConsoleCapture(consoleOptions);
2169
2263
  setActiveCaptures((prev) => ({ ...prev, console: true }));
2170
2264
  }
2171
- if (network && !chunkCVKN4H62_cjs.isNetworkCaptureActive()) {
2172
- chunkCVKN4H62_cjs.startNetworkCapture(networkOptions);
2265
+ if (network && !chunk4PRWNAXQ_cjs.isNetworkCaptureActive()) {
2266
+ chunk4PRWNAXQ_cjs.startNetworkCapture(networkOptions);
2173
2267
  setActiveCaptures((prev) => ({ ...prev, network: true }));
2174
2268
  }
2175
2269
  return () => {
2176
- chunkCVKN4H62_cjs.stopConsoleCapture();
2177
- chunkCVKN4H62_cjs.stopNetworkCapture();
2270
+ chunk4PRWNAXQ_cjs.stopConsoleCapture();
2271
+ chunk4PRWNAXQ_cjs.stopNetworkCapture();
2178
2272
  };
2179
2273
  }, [
2180
2274
  autoStart,
@@ -2189,12 +2283,12 @@ function useAITools(options = {}) {
2189
2283
  if (!screenshot) {
2190
2284
  throw new Error("Screenshot capture is not enabled");
2191
2285
  }
2192
- if (!chunkCVKN4H62_cjs.isScreenshotSupported()) {
2286
+ if (!chunk4PRWNAXQ_cjs.isScreenshotSupported()) {
2193
2287
  throw new Error(
2194
2288
  "Screenshot capture is not supported in this environment"
2195
2289
  );
2196
2290
  }
2197
- return chunkCVKN4H62_cjs.captureScreenshot({ ...screenshotOptions, ...opts });
2291
+ return chunk4PRWNAXQ_cjs.captureScreenshot({ ...screenshotOptions, ...opts });
2198
2292
  },
2199
2293
  [screenshot, screenshotOptions]
2200
2294
  );
@@ -2203,7 +2297,7 @@ function useAITools(options = {}) {
2203
2297
  if (!consoleCapture) {
2204
2298
  return { logs: [], totalCaptured: 0 };
2205
2299
  }
2206
- return chunkCVKN4H62_cjs.getConsoleLogs({ ...consoleOptions, ...opts });
2300
+ return chunk4PRWNAXQ_cjs.getConsoleLogs({ ...consoleOptions, ...opts });
2207
2301
  },
2208
2302
  [consoleCapture, consoleOptions]
2209
2303
  );
@@ -2212,7 +2306,7 @@ function useAITools(options = {}) {
2212
2306
  if (!network) {
2213
2307
  return { requests: [], totalCaptured: 0 };
2214
2308
  }
2215
- return chunkCVKN4H62_cjs.getNetworkRequests({ ...networkOptions, ...opts });
2309
+ return chunk4PRWNAXQ_cjs.getNetworkRequests({ ...networkOptions, ...opts });
2216
2310
  },
2217
2311
  [network, networkOptions]
2218
2312
  );
@@ -2305,23 +2399,23 @@ function useAITools(options = {}) {
2305
2399
  ]
2306
2400
  );
2307
2401
  const startCapturing = react.useCallback(() => {
2308
- if (consoleCapture && !chunkCVKN4H62_cjs.isConsoleCaptureActive()) {
2309
- chunkCVKN4H62_cjs.startConsoleCapture(consoleOptions);
2402
+ if (consoleCapture && !chunk4PRWNAXQ_cjs.isConsoleCaptureActive()) {
2403
+ chunk4PRWNAXQ_cjs.startConsoleCapture(consoleOptions);
2310
2404
  setActiveCaptures((prev) => ({ ...prev, console: true }));
2311
2405
  }
2312
- if (network && !chunkCVKN4H62_cjs.isNetworkCaptureActive()) {
2313
- chunkCVKN4H62_cjs.startNetworkCapture(networkOptions);
2406
+ if (network && !chunk4PRWNAXQ_cjs.isNetworkCaptureActive()) {
2407
+ chunk4PRWNAXQ_cjs.startNetworkCapture(networkOptions);
2314
2408
  setActiveCaptures((prev) => ({ ...prev, network: true }));
2315
2409
  }
2316
2410
  }, [consoleCapture, network, consoleOptions, networkOptions]);
2317
2411
  const stopCapturing = react.useCallback(() => {
2318
- chunkCVKN4H62_cjs.stopConsoleCapture();
2319
- chunkCVKN4H62_cjs.stopNetworkCapture();
2412
+ chunk4PRWNAXQ_cjs.stopConsoleCapture();
2413
+ chunk4PRWNAXQ_cjs.stopNetworkCapture();
2320
2414
  setActiveCaptures({ console: false, network: false });
2321
2415
  }, []);
2322
2416
  const clearCaptured = react.useCallback(() => {
2323
- chunkCVKN4H62_cjs.clearConsoleLogs();
2324
- chunkCVKN4H62_cjs.clearNetworkRequests();
2417
+ chunk4PRWNAXQ_cjs.clearConsoleLogs();
2418
+ chunk4PRWNAXQ_cjs.clearNetworkRequests();
2325
2419
  }, []);
2326
2420
  const formatForAI = react.useCallback((context) => {
2327
2421
  const parts = [];
@@ -2331,16 +2425,16 @@ function useAITools(options = {}) {
2331
2425
  );
2332
2426
  }
2333
2427
  if (context.consoleLogs && context.consoleLogs.logs.length > 0) {
2334
- parts.push(chunkCVKN4H62_cjs.formatLogsForAI(context.consoleLogs.logs));
2428
+ parts.push(chunk4PRWNAXQ_cjs.formatLogsForAI(context.consoleLogs.logs));
2335
2429
  }
2336
2430
  if (context.networkRequests && context.networkRequests.requests.length > 0) {
2337
- parts.push(chunkCVKN4H62_cjs.formatRequestsForAI(context.networkRequests.requests));
2431
+ parts.push(chunk4PRWNAXQ_cjs.formatRequestsForAI(context.networkRequests.requests));
2338
2432
  }
2339
2433
  return parts.length > 0 ? parts.join("\n\n---\n\n") : "No context captured.";
2340
2434
  }, []);
2341
2435
  const detectIntentFn = react.useCallback(
2342
2436
  (message) => {
2343
- const result = chunkCVKN4H62_cjs.detectIntent(message);
2437
+ const result = chunk4PRWNAXQ_cjs.detectIntent(message);
2344
2438
  result.suggestedTools = result.suggestedTools.filter((tool) => {
2345
2439
  if (tool === "screenshot") return screenshot;
2346
2440
  if (tool === "console") return consoleCapture;
@@ -2437,16 +2531,6 @@ function useTools(tools) {
2437
2531
  };
2438
2532
  }, [toolsKey]);
2439
2533
  }
2440
- var CopilotContext2 = react.createContext(null);
2441
- function useCopilotContext() {
2442
- const context = react.useContext(CopilotContext2);
2443
- if (!context) {
2444
- throw new Error("useCopilotContext must be used within a CopilotProvider");
2445
- }
2446
- return context;
2447
- }
2448
-
2449
- // src/react/hooks/useToolWithSchema.ts
2450
2534
  function convertZodSchema(schema, _toolName) {
2451
2535
  try {
2452
2536
  const zodWithJsonSchema = z__namespace;
@@ -2464,10 +2548,10 @@ function convertZodSchema(schema, _toolName) {
2464
2548
  }
2465
2549
  } catch {
2466
2550
  }
2467
- return chunkCVKN4H62_cjs.zodObjectToInputSchema(schema);
2551
+ return chunk4PRWNAXQ_cjs.zodObjectToInputSchema(schema);
2468
2552
  }
2469
2553
  function useToolWithSchema(config, dependencies = []) {
2470
- const { registerTool, unregisterTool } = useCopilotContext();
2554
+ const { registerTool, unregisterTool } = useCopilot();
2471
2555
  const configRef = react.useRef(config);
2472
2556
  configRef.current = config;
2473
2557
  const inputSchema = react.useMemo(() => {
@@ -2503,7 +2587,7 @@ function useToolWithSchema(config, dependencies = []) {
2503
2587
  }, [config.name, inputSchema, ...dependencies]);
2504
2588
  }
2505
2589
  function useToolsWithSchema(tools, dependencies = []) {
2506
- const { registerTool, unregisterTool } = useCopilotContext();
2590
+ const { registerTool, unregisterTool } = useCopilot();
2507
2591
  const toolsRef = react.useRef(tools);
2508
2592
  toolsRef.current = tools;
2509
2593
  react.useEffect(() => {
@@ -2545,6 +2629,16 @@ function useToolsWithSchema(tools, dependencies = []) {
2545
2629
  };
2546
2630
  }, [tools.map((t) => t.name).join(","), ...dependencies]);
2547
2631
  }
2632
+ var CopilotContext2 = react.createContext(null);
2633
+ function useCopilotContext() {
2634
+ const context = react.useContext(CopilotContext2);
2635
+ if (!context) {
2636
+ throw new Error("useCopilotContext must be used within a CopilotProvider");
2637
+ }
2638
+ return context;
2639
+ }
2640
+
2641
+ // src/react/hooks/useToolExecutor.ts
2548
2642
  function useToolExecutor() {
2549
2643
  const {
2550
2644
  registeredTools,
@@ -2762,7 +2856,7 @@ function useAgent(options) {
2762
2856
  if (!response.ok) {
2763
2857
  throw new Error(`Agent error: ${response.status}`);
2764
2858
  }
2765
- for await (const event of chunkCVKN4H62_cjs.streamSSE(response)) {
2859
+ for await (const event of chunk4PRWNAXQ_cjs.streamSSE(response)) {
2766
2860
  handleAgentEvent(event);
2767
2861
  }
2768
2862
  } catch (err) {
@@ -2885,7 +2979,7 @@ function formatKnowledgeResultsForAI(results) {
2885
2979
 
2886
2980
  // src/react/hooks/useKnowledgeBase.ts
2887
2981
  function useKnowledgeBase(config) {
2888
- const { registerTool, unregisterTool } = useCopilotContext();
2982
+ const { registerTool, unregisterTool } = useCopilot();
2889
2983
  const configRef = react.useRef(config);
2890
2984
  configRef.current = config;
2891
2985
  const handleSearch = react.useCallback(
@@ -3256,7 +3350,7 @@ function createReactThreadManagerState(initialThreads) {
3256
3350
  }
3257
3351
 
3258
3352
  // src/react/internal/ReactThreadManager.ts
3259
- var _ReactThreadManager = class _ReactThreadManager extends chunkCVKN4H62_cjs.ThreadManager {
3353
+ var _ReactThreadManager = class _ReactThreadManager extends chunk4PRWNAXQ_cjs.ThreadManager {
3260
3354
  constructor(config = {}, callbacks = {}) {
3261
3355
  const reactState = new ReactThreadManagerState();
3262
3356
  super({ ...config, state: reactState }, callbacks);
@@ -3689,6 +3783,13 @@ var ReactChat = class extends AbstractChat {
3689
3783
  super.dispose();
3690
3784
  this.reactState.dispose();
3691
3785
  }
3786
+ /**
3787
+ * Revive a disposed instance (for React StrictMode compatibility)
3788
+ */
3789
+ revive() {
3790
+ super.revive();
3791
+ this.reactState.revive();
3792
+ }
3692
3793
  };
3693
3794
  function createReactChat(config) {
3694
3795
  return new ReactChat(config);
@@ -3696,6 +3797,9 @@ function createReactChat(config) {
3696
3797
  function useChat(config) {
3697
3798
  const chatRef = react.useRef(null);
3698
3799
  const [input, setInput] = react.useState("");
3800
+ if (chatRef.current !== null && chatRef.current.disposed) {
3801
+ chatRef.current.revive();
3802
+ }
3699
3803
  if (chatRef.current === null) {
3700
3804
  chatRef.current = createReactChat({
3701
3805
  runtimeUrl: config.runtimeUrl,
@@ -3816,5 +3920,5 @@ exports.useToolExecutor = useToolExecutor;
3816
3920
  exports.useToolWithSchema = useToolWithSchema;
3817
3921
  exports.useTools = useTools;
3818
3922
  exports.useToolsWithSchema = useToolsWithSchema;
3819
- //# sourceMappingURL=chunk-FCWLHNLI.cjs.map
3820
- //# sourceMappingURL=chunk-FCWLHNLI.cjs.map
3923
+ //# sourceMappingURL=chunk-BLSI67J6.cjs.map
3924
+ //# sourceMappingURL=chunk-BLSI67J6.cjs.map