@yourgpt/copilot-sdk 2.0.0 → 2.0.2-beta.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.
- package/README.md +68 -27
- package/dist/{ThreadManager-JT0sqSSD.d.ts → ThreadManager-Dkp_eLty.d.ts} +1 -1
- package/dist/{ThreadManager-CUq5Ocu2.d.cts → ThreadManager-LfFRhr4e.d.cts} +1 -1
- package/dist/anthropic-6F5GRE3B.js +4 -0
- package/dist/anthropic-6F5GRE3B.js.map +1 -0
- package/dist/anthropic-DGalr_Fw.d.cts +17 -0
- package/dist/anthropic-DkCEDYOt.d.ts +17 -0
- package/dist/anthropic-NMTRABEH.cjs +21 -0
- package/dist/anthropic-NMTRABEH.cjs.map +1 -0
- package/dist/brave-DdnWb7Gb.d.cts +17 -0
- package/dist/brave-DsI9n7Wr.d.ts +17 -0
- package/dist/brave-OYKCOZEM.cjs +21 -0
- package/dist/brave-OYKCOZEM.cjs.map +1 -0
- package/dist/brave-XSASGGH2.js +4 -0
- package/dist/brave-XSASGGH2.js.map +1 -0
- package/dist/chunk-2FAWEBZS.cjs +88 -0
- package/dist/chunk-2FAWEBZS.cjs.map +1 -0
- package/dist/{chunk-CJ7UWN2Y.js → chunk-3YKHVLNP.js} +397 -7
- package/dist/chunk-3YKHVLNP.js.map +1 -0
- package/dist/chunk-4WWWMNUA.js +1142 -0
- package/dist/chunk-4WWWMNUA.js.map +1 -0
- package/dist/chunk-53UGJNHN.js +92 -0
- package/dist/chunk-53UGJNHN.js.map +1 -0
- package/dist/chunk-6BXQFCK3.js +79 -0
- package/dist/chunk-6BXQFCK3.js.map +1 -0
- package/dist/chunk-6T5XXJEP.cjs +80 -0
- package/dist/chunk-6T5XXJEP.cjs.map +1 -0
- package/dist/chunk-7W7QLZNC.js +72 -0
- package/dist/chunk-7W7QLZNC.js.map +1 -0
- package/dist/chunk-ASV6JLYG.cjs +99 -0
- package/dist/chunk-ASV6JLYG.cjs.map +1 -0
- package/dist/chunk-CBAHCI4R.cjs +76 -0
- package/dist/chunk-CBAHCI4R.cjs.map +1 -0
- package/dist/chunk-CEOMTQTP.js +85 -0
- package/dist/chunk-CEOMTQTP.js.map +1 -0
- package/dist/chunk-DABZYCVX.js +84 -0
- package/dist/chunk-DABZYCVX.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/{chunk-4PRWNAXQ.cjs → chunk-DUPNYVBP.cjs} +27 -89
- package/dist/chunk-DUPNYVBP.cjs.map +1 -0
- package/dist/chunk-DVC63PGD.cjs +1160 -0
- package/dist/chunk-DVC63PGD.cjs.map +1 -0
- package/dist/chunk-G4SF2PNQ.js +33 -0
- package/dist/chunk-G4SF2PNQ.js.map +1 -0
- package/dist/chunk-GANCV72Z.cjs +110 -0
- package/dist/chunk-GANCV72Z.cjs.map +1 -0
- package/dist/chunk-J4OMGO5O.js +66 -0
- package/dist/chunk-J4OMGO5O.js.map +1 -0
- package/dist/chunk-JEQ2X3Z6.cjs +12 -0
- package/dist/chunk-JEQ2X3Z6.cjs.map +1 -0
- package/dist/chunk-JO4BHPAD.cjs +40 -0
- package/dist/chunk-JO4BHPAD.cjs.map +1 -0
- package/dist/chunk-MEBXW75C.cjs +89 -0
- package/dist/chunk-MEBXW75C.cjs.map +1 -0
- package/dist/chunk-MNDGIW47.js +76 -0
- package/dist/chunk-MNDGIW47.js.map +1 -0
- package/dist/chunk-PPFHA6IL.js +83 -0
- package/dist/chunk-PPFHA6IL.js.map +1 -0
- package/dist/{chunk-BLSI67J6.cjs → chunk-RBZXLBGI.cjs} +425 -30
- package/dist/chunk-RBZXLBGI.cjs.map +1 -0
- package/dist/chunk-RQ74USYU.js +128 -0
- package/dist/chunk-RQ74USYU.js.map +1 -0
- package/dist/chunk-TX7CGITI.cjs +82 -0
- package/dist/chunk-TX7CGITI.cjs.map +1 -0
- package/dist/chunk-TXLIY7GF.cjs +132 -0
- package/dist/chunk-TXLIY7GF.cjs.map +1 -0
- package/dist/chunk-VD74IPKB.js +106 -0
- package/dist/chunk-VD74IPKB.js.map +1 -0
- package/dist/chunk-W73FBYIH.cjs +87 -0
- package/dist/chunk-W73FBYIH.cjs.map +1 -0
- package/dist/chunk-W74OTXXX.cjs +73 -0
- package/dist/chunk-W74OTXXX.cjs.map +1 -0
- package/dist/chunk-XGITAEXU.js +93 -0
- package/dist/chunk-XGITAEXU.js.map +1 -0
- package/dist/chunk-XWOHNY3F.cjs +96 -0
- package/dist/chunk-XWOHNY3F.cjs.map +1 -0
- package/dist/{chunk-JM7PB2LP.js → chunk-Z7PHGSJT.js} +10 -66
- package/dist/chunk-Z7PHGSJT.js.map +1 -0
- package/dist/core/index.cjs +156 -84
- package/dist/core/index.d.cts +16 -4
- package/dist/core/index.d.ts +16 -4
- package/dist/core/index.js +13 -1
- package/dist/exa-72KFY5A7.cjs +21 -0
- package/dist/exa-72KFY5A7.cjs.map +1 -0
- package/dist/exa-Dp9U-WTc.d.ts +17 -0
- package/dist/exa-NNVPBC2M.js +4 -0
- package/dist/exa-NNVPBC2M.js.map +1 -0
- package/dist/exa-jJSPhyUW.d.cts +17 -0
- package/dist/google-CHU2yycE.d.cts +17 -0
- package/dist/google-CTEK6SV2.js +4 -0
- package/dist/google-CTEK6SV2.js.map +1 -0
- package/dist/google-Da8IQxaI.d.ts +17 -0
- package/dist/google-IIUXFFVF.cjs +21 -0
- package/dist/google-IIUXFFVF.cjs.map +1 -0
- package/dist/index-DBNh0jhE.d.ts +206 -0
- package/dist/index-DOlhSb79.d.cts +206 -0
- package/dist/mcp/index.cjs +670 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +779 -0
- package/dist/mcp/index.d.ts +779 -0
- package/dist/mcp/index.js +574 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/openai-6KTCQ7PZ.cjs +21 -0
- package/dist/openai-6KTCQ7PZ.cjs.map +1 -0
- package/dist/openai-7W2PCNW5.js +4 -0
- package/dist/openai-7W2PCNW5.js.map +1 -0
- package/dist/openai-Cam8hF4f.d.ts +17 -0
- package/dist/openai-HVSCuXgO.d.cts +17 -0
- package/dist/react/index.cjs +75 -42
- package/dist/react/index.d.cts +270 -45
- package/dist/react/index.d.ts +270 -45
- package/dist/react/index.js +15 -2
- package/dist/searxng-AXLVGY7Z.js +4 -0
- package/dist/searxng-AXLVGY7Z.js.map +1 -0
- package/dist/searxng-EJKNY236.cjs +21 -0
- package/dist/searxng-EJKNY236.cjs.map +1 -0
- package/dist/searxng-K0qtY9vp.d.ts +17 -0
- package/dist/searxng-QGOte_Gq.d.cts +17 -0
- package/dist/serper-3JYJHJX6.js +4 -0
- package/dist/serper-3JYJHJX6.js.map +1 -0
- package/dist/serper-63FT4AOL.cjs +21 -0
- package/dist/serper-63FT4AOL.cjs.map +1 -0
- package/dist/serper-7Czya3PW.d.ts +17 -0
- package/dist/serper-JzdaSnS9.d.cts +17 -0
- package/dist/styles.css +38 -0
- package/dist/tavily-AWFP4RM7.cjs +21 -0
- package/dist/tavily-AWFP4RM7.cjs.map +1 -0
- package/dist/tavily-C8cXXojE.d.cts +17 -0
- package/dist/tavily-CIWAAZPH.js +4 -0
- package/dist/tavily-CIWAAZPH.js.map +1 -0
- package/dist/tavily-DdSGVgkE.d.ts +17 -0
- package/dist/themes/catppuccin.css +2 -0
- package/dist/themes/claude.css +2 -0
- package/dist/themes/linear.css +2 -0
- package/dist/themes/modern-minimal.css +2 -0
- package/dist/themes/posthog.css +2 -0
- package/dist/themes/supabase.css +2 -0
- package/dist/themes/twitter.css +2 -0
- package/dist/themes/vercel.css +2 -0
- package/dist/tools/anthropic/index.cjs +61 -0
- package/dist/tools/anthropic/index.cjs.map +1 -0
- package/dist/tools/anthropic/index.d.cts +67 -0
- package/dist/tools/anthropic/index.d.ts +67 -0
- package/dist/tools/anthropic/index.js +56 -0
- package/dist/tools/anthropic/index.js.map +1 -0
- package/dist/tools/brave/index.cjs +85 -0
- package/dist/tools/brave/index.cjs.map +1 -0
- package/dist/tools/brave/index.d.cts +91 -0
- package/dist/tools/brave/index.d.ts +91 -0
- package/dist/tools/brave/index.js +80 -0
- package/dist/tools/brave/index.js.map +1 -0
- package/dist/tools/exa/index.cjs +90 -0
- package/dist/tools/exa/index.cjs.map +1 -0
- package/dist/tools/exa/index.d.cts +92 -0
- package/dist/tools/exa/index.d.ts +92 -0
- package/dist/tools/exa/index.js +85 -0
- package/dist/tools/exa/index.js.map +1 -0
- package/dist/tools/google/index.cjs +81 -0
- package/dist/tools/google/index.cjs.map +1 -0
- package/dist/tools/google/index.d.cts +81 -0
- package/dist/tools/google/index.d.ts +81 -0
- package/dist/tools/google/index.js +76 -0
- package/dist/tools/google/index.js.map +1 -0
- package/dist/tools/openai/index.cjs +83 -0
- package/dist/tools/openai/index.cjs.map +1 -0
- package/dist/tools/openai/index.d.cts +84 -0
- package/dist/tools/openai/index.d.ts +84 -0
- package/dist/tools/openai/index.js +78 -0
- package/dist/tools/openai/index.js.map +1 -0
- package/dist/tools/searxng/index.cjs +85 -0
- package/dist/tools/searxng/index.cjs.map +1 -0
- package/dist/tools/searxng/index.d.cts +91 -0
- package/dist/tools/searxng/index.d.ts +91 -0
- package/dist/tools/searxng/index.js +80 -0
- package/dist/tools/searxng/index.js.map +1 -0
- package/dist/tools/serper/index.cjs +85 -0
- package/dist/tools/serper/index.cjs.map +1 -0
- package/dist/tools/serper/index.d.cts +91 -0
- package/dist/tools/serper/index.d.ts +91 -0
- package/dist/tools/serper/index.js +80 -0
- package/dist/tools/serper/index.js.map +1 -0
- package/dist/tools/tavily/index.cjs +91 -0
- package/dist/tools/tavily/index.cjs.map +1 -0
- package/dist/tools/tavily/index.d.cts +95 -0
- package/dist/tools/tavily/index.d.ts +95 -0
- package/dist/tools/tavily/index.js +86 -0
- package/dist/tools/tavily/index.js.map +1 -0
- package/dist/tools/web-search/index.cjs +31 -0
- package/dist/tools/web-search/index.cjs.map +1 -0
- package/dist/tools/web-search/index.d.cts +3 -0
- package/dist/tools/web-search/index.d.ts +3 -0
- package/dist/tools/web-search/index.js +14 -0
- package/dist/tools/web-search/index.js.map +1 -0
- package/dist/{types-BtAaOV07.d.cts → tools-EiPWA9Ay.d.cts} +34 -367
- package/dist/{types-BtAaOV07.d.ts → tools-EiPWA9Ay.d.ts} +34 -367
- package/dist/types-B20VCJXL.d.cts +347 -0
- package/dist/types-B20VCJXL.d.ts +347 -0
- package/dist/types-CKA6U74u.d.cts +441 -0
- package/dist/types-DG2ya08y.d.cts +367 -0
- package/dist/types-DG2ya08y.d.ts +367 -0
- package/dist/types-ZguuKEs_.d.cts +127 -0
- package/dist/types-ZguuKEs_.d.ts +127 -0
- package/dist/types-iBkPICvQ.d.ts +441 -0
- package/dist/ui/index.cjs +1069 -146
- package/dist/ui/index.cjs.map +1 -1
- package/dist/ui/index.d.cts +410 -4
- package/dist/ui/index.d.ts +410 -4
- package/dist/ui/index.js +1001 -94
- package/dist/ui/index.js.map +1 -1
- package/package.json +53 -2
- package/dist/chunk-4PRWNAXQ.cjs.map +0 -1
- package/dist/chunk-BLSI67J6.cjs.map +0 -1
- package/dist/chunk-CJ7UWN2Y.js.map +0 -1
- package/dist/chunk-JM7PB2LP.js.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-
|
|
1
|
+
import { ThreadManager, isConsoleCaptureActive, startConsoleCapture, isNetworkCaptureActive, startNetworkCapture, stopConsoleCapture, stopNetworkCapture, isScreenshotSupported, captureScreenshot, getConsoleLogs, getNetworkRequests, clearConsoleLogs, clearNetworkRequests, formatLogsForAI, formatRequestsForAI, detectIntent, streamSSE, zodObjectToInputSchema } from './chunk-Z7PHGSJT.js';
|
|
2
|
+
import { createMCPClient, MCPToolAdapter } from './chunk-4WWWMNUA.js';
|
|
2
3
|
import { createContext, useContext, useCallback, useEffect, useState, useRef, useSyncExternalStore, useMemo } from 'react';
|
|
3
4
|
import { jsx } from 'react/jsx-runtime';
|
|
4
5
|
import * as z from 'zod';
|
|
@@ -154,6 +155,63 @@ function processStreamChunk(chunk, state) {
|
|
|
154
155
|
toolCalls: parseToolCalls(chunk.toolCalls),
|
|
155
156
|
requiresAction: true
|
|
156
157
|
};
|
|
158
|
+
// Handle tool action events from server
|
|
159
|
+
case "action:start": {
|
|
160
|
+
const newResults = new Map(state.toolResults);
|
|
161
|
+
newResults.set(chunk.id, {
|
|
162
|
+
id: chunk.id,
|
|
163
|
+
name: chunk.name,
|
|
164
|
+
status: "executing"
|
|
165
|
+
});
|
|
166
|
+
return { ...state, toolResults: newResults };
|
|
167
|
+
}
|
|
168
|
+
case "action:args": {
|
|
169
|
+
const existing = state.toolResults.get(chunk.id);
|
|
170
|
+
if (existing) {
|
|
171
|
+
const newResults = new Map(state.toolResults);
|
|
172
|
+
try {
|
|
173
|
+
newResults.set(chunk.id, {
|
|
174
|
+
...existing,
|
|
175
|
+
args: JSON.parse(chunk.args)
|
|
176
|
+
});
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
return { ...state, toolResults: newResults };
|
|
180
|
+
}
|
|
181
|
+
return state;
|
|
182
|
+
}
|
|
183
|
+
case "action:end": {
|
|
184
|
+
const existing = state.toolResults.get(chunk.id);
|
|
185
|
+
const newResults = new Map(state.toolResults);
|
|
186
|
+
newResults.set(chunk.id, {
|
|
187
|
+
id: chunk.id,
|
|
188
|
+
name: chunk.name,
|
|
189
|
+
status: chunk.error ? "failed" : "completed",
|
|
190
|
+
args: existing?.args,
|
|
191
|
+
result: chunk.result,
|
|
192
|
+
error: chunk.error
|
|
193
|
+
});
|
|
194
|
+
return { ...state, toolResults: newResults };
|
|
195
|
+
}
|
|
196
|
+
case "tool:result": {
|
|
197
|
+
const existing = state.toolResults.get(chunk.id);
|
|
198
|
+
const newResults = new Map(state.toolResults);
|
|
199
|
+
newResults.set(chunk.id, {
|
|
200
|
+
id: chunk.id,
|
|
201
|
+
name: chunk.name,
|
|
202
|
+
status: chunk.result?.success ? "completed" : "failed",
|
|
203
|
+
args: existing?.args,
|
|
204
|
+
result: chunk.result,
|
|
205
|
+
error: chunk.result?.error
|
|
206
|
+
});
|
|
207
|
+
return { ...state, toolResults: newResults };
|
|
208
|
+
}
|
|
209
|
+
case "citation": {
|
|
210
|
+
return {
|
|
211
|
+
...state,
|
|
212
|
+
citations: [...state.citations, ...chunk.citations]
|
|
213
|
+
};
|
|
214
|
+
}
|
|
157
215
|
case "done":
|
|
158
216
|
return {
|
|
159
217
|
...state,
|
|
@@ -192,6 +250,8 @@ function createStreamState(messageId) {
|
|
|
192
250
|
content: "",
|
|
193
251
|
thinking: "",
|
|
194
252
|
toolCalls: [],
|
|
253
|
+
toolResults: /* @__PURE__ */ new Map(),
|
|
254
|
+
citations: [],
|
|
195
255
|
requiresAction: false,
|
|
196
256
|
finishReason: void 0
|
|
197
257
|
};
|
|
@@ -219,13 +279,31 @@ function streamStateToMessage(state) {
|
|
|
219
279
|
arguments: JSON.stringify(tc.args)
|
|
220
280
|
}
|
|
221
281
|
})) : void 0;
|
|
282
|
+
const toolExecutions = state.toolResults.size > 0 ? Array.from(state.toolResults.values()).map((tr) => ({
|
|
283
|
+
id: tr.id,
|
|
284
|
+
name: tr.name,
|
|
285
|
+
args: tr.args ?? {},
|
|
286
|
+
status: tr.status,
|
|
287
|
+
result: tr.result,
|
|
288
|
+
error: tr.error,
|
|
289
|
+
timestamp: Date.now()
|
|
290
|
+
})) : void 0;
|
|
291
|
+
const metadata = {};
|
|
292
|
+
if (toolExecutions) {
|
|
293
|
+
metadata.toolExecutions = toolExecutions;
|
|
294
|
+
}
|
|
295
|
+
if (state.citations && state.citations.length > 0) {
|
|
296
|
+
metadata.citations = state.citations;
|
|
297
|
+
}
|
|
222
298
|
return {
|
|
223
299
|
id: state.messageId,
|
|
224
300
|
role: "assistant",
|
|
225
301
|
content: state.content,
|
|
226
302
|
thinking: state.thinking || void 0,
|
|
227
303
|
toolCalls,
|
|
228
|
-
createdAt: /* @__PURE__ */ new Date()
|
|
304
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
305
|
+
// Store tool executions and citations in metadata for UI components
|
|
306
|
+
metadata: Object.keys(metadata).length > 0 ? metadata : void 0
|
|
229
307
|
};
|
|
230
308
|
}
|
|
231
309
|
function createEmptyAssistantMessage(id) {
|
|
@@ -398,11 +476,21 @@ function buildToolResultContentForAI(result, tool, args) {
|
|
|
398
476
|
case "full":
|
|
399
477
|
default:
|
|
400
478
|
if (aiContext) {
|
|
401
|
-
const {
|
|
479
|
+
const {
|
|
480
|
+
_aiResponseMode,
|
|
481
|
+
_aiContext,
|
|
482
|
+
_aiContent,
|
|
483
|
+
_uiResources,
|
|
484
|
+
...dataOnly
|
|
485
|
+
} = typedResult ?? {};
|
|
402
486
|
return `${aiContext}
|
|
403
487
|
|
|
404
488
|
Full data: ${JSON.stringify(dataOnly)}`;
|
|
405
489
|
}
|
|
490
|
+
if (typedResult?._uiResources) {
|
|
491
|
+
const { _uiResources, ...dataOnly } = typedResult;
|
|
492
|
+
return JSON.stringify(dataOnly);
|
|
493
|
+
}
|
|
406
494
|
return JSON.stringify(result);
|
|
407
495
|
}
|
|
408
496
|
}
|
|
@@ -3471,7 +3559,6 @@ function useThreadManager(config) {
|
|
|
3471
3559
|
if (!config.adapter) {
|
|
3472
3560
|
return getInternalManager(config);
|
|
3473
3561
|
}
|
|
3474
|
-
console.log("[useThreadManager] Creating new manager with custom adapter");
|
|
3475
3562
|
return createReactThreadManager(
|
|
3476
3563
|
{
|
|
3477
3564
|
adapter: config.adapter,
|
|
@@ -3603,6 +3690,309 @@ function useThreadManager(config) {
|
|
|
3603
3690
|
hasPendingChanges
|
|
3604
3691
|
};
|
|
3605
3692
|
}
|
|
3693
|
+
function useMCPClient(config) {
|
|
3694
|
+
const {
|
|
3695
|
+
autoConnect = true,
|
|
3696
|
+
onConnectionStateChange,
|
|
3697
|
+
onToolsChange,
|
|
3698
|
+
onElicitationRequest,
|
|
3699
|
+
onError,
|
|
3700
|
+
onNotification,
|
|
3701
|
+
...clientConfig
|
|
3702
|
+
} = config;
|
|
3703
|
+
const clientRef = useRef(null);
|
|
3704
|
+
const mountedRef = useRef(true);
|
|
3705
|
+
const [state, setState] = useState({
|
|
3706
|
+
connectionState: "disconnected",
|
|
3707
|
+
tools: []
|
|
3708
|
+
});
|
|
3709
|
+
const getClient = useCallback(() => {
|
|
3710
|
+
if (!clientRef.current) {
|
|
3711
|
+
clientRef.current = createMCPClient(clientConfig, {
|
|
3712
|
+
onConnectionStateChange: (newState) => {
|
|
3713
|
+
if (mountedRef.current) {
|
|
3714
|
+
setState((prev) => ({ ...prev, connectionState: newState }));
|
|
3715
|
+
onConnectionStateChange?.(newState);
|
|
3716
|
+
}
|
|
3717
|
+
},
|
|
3718
|
+
onToolsChange: (tools) => {
|
|
3719
|
+
if (mountedRef.current) {
|
|
3720
|
+
setState((prev) => ({ ...prev, tools }));
|
|
3721
|
+
onToolsChange?.(tools);
|
|
3722
|
+
}
|
|
3723
|
+
},
|
|
3724
|
+
onElicitationRequest,
|
|
3725
|
+
onError: (error) => {
|
|
3726
|
+
if (mountedRef.current) {
|
|
3727
|
+
setState((prev) => ({ ...prev, error: error.message }));
|
|
3728
|
+
onError?.(error);
|
|
3729
|
+
}
|
|
3730
|
+
},
|
|
3731
|
+
onNotification
|
|
3732
|
+
});
|
|
3733
|
+
}
|
|
3734
|
+
return clientRef.current;
|
|
3735
|
+
}, [
|
|
3736
|
+
clientConfig,
|
|
3737
|
+
onConnectionStateChange,
|
|
3738
|
+
onToolsChange,
|
|
3739
|
+
onElicitationRequest,
|
|
3740
|
+
onError,
|
|
3741
|
+
onNotification
|
|
3742
|
+
]);
|
|
3743
|
+
const connect = useCallback(async () => {
|
|
3744
|
+
const client = getClient();
|
|
3745
|
+
try {
|
|
3746
|
+
setState((prev) => ({
|
|
3747
|
+
...prev,
|
|
3748
|
+
connectionState: "connecting",
|
|
3749
|
+
error: void 0
|
|
3750
|
+
}));
|
|
3751
|
+
await client.connect();
|
|
3752
|
+
if (mountedRef.current) {
|
|
3753
|
+
const clientState = client.getState();
|
|
3754
|
+
setState({
|
|
3755
|
+
connectionState: "connected",
|
|
3756
|
+
tools: clientState.tools,
|
|
3757
|
+
serverInfo: clientState.serverInfo,
|
|
3758
|
+
serverCapabilities: clientState.serverCapabilities,
|
|
3759
|
+
lastActivity: Date.now()
|
|
3760
|
+
});
|
|
3761
|
+
}
|
|
3762
|
+
} catch (error) {
|
|
3763
|
+
if (mountedRef.current) {
|
|
3764
|
+
const errorMessage = error instanceof Error ? error.message : "Connection failed";
|
|
3765
|
+
setState((prev) => ({
|
|
3766
|
+
...prev,
|
|
3767
|
+
connectionState: "error",
|
|
3768
|
+
error: errorMessage
|
|
3769
|
+
}));
|
|
3770
|
+
}
|
|
3771
|
+
throw error;
|
|
3772
|
+
}
|
|
3773
|
+
}, [getClient]);
|
|
3774
|
+
const disconnect = useCallback(async () => {
|
|
3775
|
+
const client = clientRef.current;
|
|
3776
|
+
if (client) {
|
|
3777
|
+
await client.disconnect();
|
|
3778
|
+
if (mountedRef.current) {
|
|
3779
|
+
setState({
|
|
3780
|
+
connectionState: "disconnected",
|
|
3781
|
+
tools: []
|
|
3782
|
+
});
|
|
3783
|
+
}
|
|
3784
|
+
}
|
|
3785
|
+
}, []);
|
|
3786
|
+
const callTool = useCallback(
|
|
3787
|
+
async (name, args) => {
|
|
3788
|
+
const client = clientRef.current;
|
|
3789
|
+
if (!client || !client.isConnected()) {
|
|
3790
|
+
throw new Error("MCP client not connected");
|
|
3791
|
+
}
|
|
3792
|
+
const result = await client.callTool(name, args);
|
|
3793
|
+
if (mountedRef.current) {
|
|
3794
|
+
setState((prev) => ({ ...prev, lastActivity: Date.now() }));
|
|
3795
|
+
}
|
|
3796
|
+
return result;
|
|
3797
|
+
},
|
|
3798
|
+
[]
|
|
3799
|
+
);
|
|
3800
|
+
const refreshTools = useCallback(async () => {
|
|
3801
|
+
const client = clientRef.current;
|
|
3802
|
+
if (!client || !client.isConnected()) {
|
|
3803
|
+
throw new Error("MCP client not connected");
|
|
3804
|
+
}
|
|
3805
|
+
const tools = await client.refreshTools();
|
|
3806
|
+
if (mountedRef.current) {
|
|
3807
|
+
setState((prev) => ({ ...prev, tools, lastActivity: Date.now() }));
|
|
3808
|
+
}
|
|
3809
|
+
return tools;
|
|
3810
|
+
}, []);
|
|
3811
|
+
useEffect(() => {
|
|
3812
|
+
mountedRef.current = true;
|
|
3813
|
+
if (autoConnect) {
|
|
3814
|
+
connect().catch(() => {
|
|
3815
|
+
});
|
|
3816
|
+
}
|
|
3817
|
+
return () => {
|
|
3818
|
+
mountedRef.current = false;
|
|
3819
|
+
const client = clientRef.current;
|
|
3820
|
+
if (client) {
|
|
3821
|
+
client.disconnect().catch(() => {
|
|
3822
|
+
});
|
|
3823
|
+
clientRef.current = null;
|
|
3824
|
+
}
|
|
3825
|
+
};
|
|
3826
|
+
}, []);
|
|
3827
|
+
const isConnected = state.connectionState === "connected";
|
|
3828
|
+
const isLoading = state.connectionState === "connecting";
|
|
3829
|
+
return {
|
|
3830
|
+
state,
|
|
3831
|
+
connect,
|
|
3832
|
+
disconnect,
|
|
3833
|
+
callTool,
|
|
3834
|
+
refreshTools,
|
|
3835
|
+
isConnected,
|
|
3836
|
+
isLoading
|
|
3837
|
+
};
|
|
3838
|
+
}
|
|
3839
|
+
function useMCPTools(config) {
|
|
3840
|
+
const {
|
|
3841
|
+
prefixToolNames = true,
|
|
3842
|
+
autoRegister = true,
|
|
3843
|
+
...clientConfig
|
|
3844
|
+
} = config;
|
|
3845
|
+
const { registerTool, unregisterTool } = useCopilot();
|
|
3846
|
+
const registeredToolsRef = useRef([]);
|
|
3847
|
+
const mcpClient = useMCPClient(clientConfig);
|
|
3848
|
+
const toolAdapter = useMemo(
|
|
3849
|
+
() => new MCPToolAdapter(clientConfig.name),
|
|
3850
|
+
[clientConfig.name]
|
|
3851
|
+
);
|
|
3852
|
+
const toolDefinitions = useMemo(() => {
|
|
3853
|
+
if (!mcpClient.isConnected || mcpClient.state.tools.length === 0) {
|
|
3854
|
+
return [];
|
|
3855
|
+
}
|
|
3856
|
+
return mcpClient.state.tools.map(
|
|
3857
|
+
(tool) => toolAdapter.toToolDefinition(tool, {
|
|
3858
|
+
prefix: prefixToolNames,
|
|
3859
|
+
asServerTool: true,
|
|
3860
|
+
// MCP tools execute remotely
|
|
3861
|
+
callTool: mcpClient.callTool
|
|
3862
|
+
})
|
|
3863
|
+
);
|
|
3864
|
+
}, [
|
|
3865
|
+
mcpClient.isConnected,
|
|
3866
|
+
mcpClient.state.tools,
|
|
3867
|
+
mcpClient.callTool,
|
|
3868
|
+
toolAdapter,
|
|
3869
|
+
prefixToolNames
|
|
3870
|
+
]);
|
|
3871
|
+
useEffect(() => {
|
|
3872
|
+
if (!autoRegister) {
|
|
3873
|
+
return;
|
|
3874
|
+
}
|
|
3875
|
+
for (const toolName of registeredToolsRef.current) {
|
|
3876
|
+
unregisterTool(toolName);
|
|
3877
|
+
}
|
|
3878
|
+
registeredToolsRef.current = [];
|
|
3879
|
+
if (mcpClient.isConnected && toolDefinitions.length > 0) {
|
|
3880
|
+
for (const tool of toolDefinitions) {
|
|
3881
|
+
registerTool(tool);
|
|
3882
|
+
registeredToolsRef.current.push(tool.name);
|
|
3883
|
+
}
|
|
3884
|
+
}
|
|
3885
|
+
return () => {
|
|
3886
|
+
for (const toolName of registeredToolsRef.current) {
|
|
3887
|
+
unregisterTool(toolName);
|
|
3888
|
+
}
|
|
3889
|
+
registeredToolsRef.current = [];
|
|
3890
|
+
};
|
|
3891
|
+
}, [
|
|
3892
|
+
autoRegister,
|
|
3893
|
+
mcpClient.isConnected,
|
|
3894
|
+
toolDefinitions,
|
|
3895
|
+
registerTool,
|
|
3896
|
+
unregisterTool
|
|
3897
|
+
]);
|
|
3898
|
+
return {
|
|
3899
|
+
...mcpClient,
|
|
3900
|
+
toolDefinitions
|
|
3901
|
+
};
|
|
3902
|
+
}
|
|
3903
|
+
function useMCPUIIntents(config = {}) {
|
|
3904
|
+
const {
|
|
3905
|
+
onToolCall,
|
|
3906
|
+
onIntent,
|
|
3907
|
+
onPrompt,
|
|
3908
|
+
onNotify,
|
|
3909
|
+
onLink,
|
|
3910
|
+
requireConsent = { tool: false, link: true }
|
|
3911
|
+
} = config;
|
|
3912
|
+
const handleIntent = useCallback(
|
|
3913
|
+
async (intent, context) => {
|
|
3914
|
+
switch (intent.type) {
|
|
3915
|
+
case "tool": {
|
|
3916
|
+
if (requireConsent.tool) ;
|
|
3917
|
+
await onToolCall?.(intent.name, intent.arguments, context);
|
|
3918
|
+
break;
|
|
3919
|
+
}
|
|
3920
|
+
case "intent": {
|
|
3921
|
+
await onIntent?.(intent.action, intent.data, context);
|
|
3922
|
+
break;
|
|
3923
|
+
}
|
|
3924
|
+
case "prompt": {
|
|
3925
|
+
onPrompt?.(intent.text, context);
|
|
3926
|
+
break;
|
|
3927
|
+
}
|
|
3928
|
+
case "notify": {
|
|
3929
|
+
onNotify?.(intent.message, intent.level, context);
|
|
3930
|
+
break;
|
|
3931
|
+
}
|
|
3932
|
+
case "link": {
|
|
3933
|
+
const shouldContinue = onLink?.(intent.url, intent.newTab, context);
|
|
3934
|
+
if (shouldContinue === false) {
|
|
3935
|
+
break;
|
|
3936
|
+
}
|
|
3937
|
+
if (requireConsent.link) {
|
|
3938
|
+
const isSafeUrl = intent.url.startsWith("https://") || intent.url.startsWith("http://localhost");
|
|
3939
|
+
if (!isSafeUrl) {
|
|
3940
|
+
console.warn(
|
|
3941
|
+
"[MCP-UI] Blocked potentially unsafe link:",
|
|
3942
|
+
intent.url
|
|
3943
|
+
);
|
|
3944
|
+
break;
|
|
3945
|
+
}
|
|
3946
|
+
}
|
|
3947
|
+
if (typeof window !== "undefined") {
|
|
3948
|
+
if (intent.newTab !== false) {
|
|
3949
|
+
window.open(intent.url, "_blank", "noopener,noreferrer");
|
|
3950
|
+
} else {
|
|
3951
|
+
window.location.href = intent.url;
|
|
3952
|
+
}
|
|
3953
|
+
}
|
|
3954
|
+
break;
|
|
3955
|
+
}
|
|
3956
|
+
default: {
|
|
3957
|
+
console.warn(
|
|
3958
|
+
"[MCP-UI] Unknown intent type:",
|
|
3959
|
+
intent.type
|
|
3960
|
+
);
|
|
3961
|
+
}
|
|
3962
|
+
}
|
|
3963
|
+
},
|
|
3964
|
+
[onToolCall, onIntent, onPrompt, onNotify, onLink, requireConsent]
|
|
3965
|
+
);
|
|
3966
|
+
return useMemo(
|
|
3967
|
+
() => ({
|
|
3968
|
+
handleIntent
|
|
3969
|
+
}),
|
|
3970
|
+
[handleIntent]
|
|
3971
|
+
);
|
|
3972
|
+
}
|
|
3973
|
+
function createMessageIntentHandler(sendMessage) {
|
|
3974
|
+
return {
|
|
3975
|
+
onIntent: async (action, data) => {
|
|
3976
|
+
const dataStr = data ? ` with ${JSON.stringify(data)}` : "";
|
|
3977
|
+
await sendMessage(`User action: ${action}${dataStr}`);
|
|
3978
|
+
},
|
|
3979
|
+
onPrompt: (text) => {
|
|
3980
|
+
sendMessage(text);
|
|
3981
|
+
},
|
|
3982
|
+
onNotify: (message, level) => {
|
|
3983
|
+
if (level === "error") {
|
|
3984
|
+
sendMessage(`Error: ${message}`);
|
|
3985
|
+
}
|
|
3986
|
+
}
|
|
3987
|
+
};
|
|
3988
|
+
}
|
|
3989
|
+
function createToolIntentHandler(callTool) {
|
|
3990
|
+
return {
|
|
3991
|
+
onToolCall: async (name, args) => {
|
|
3992
|
+
await callTool(name, args);
|
|
3993
|
+
}
|
|
3994
|
+
};
|
|
3995
|
+
}
|
|
3606
3996
|
|
|
3607
3997
|
// src/react/utils/permission-storage.ts
|
|
3608
3998
|
var DEFAULT_KEY_PREFIX = "yourgpt-permissions";
|
|
@@ -3862,6 +4252,6 @@ function useChat(config) {
|
|
|
3862
4252
|
};
|
|
3863
4253
|
}
|
|
3864
4254
|
|
|
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 };
|
|
3866
|
-
//# sourceMappingURL=chunk-
|
|
3867
|
-
//# sourceMappingURL=chunk-
|
|
4255
|
+
export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, ReactThreadManager, ReactThreadManagerState, createMessageIntentHandler, createPermissionStorage, createReactChat, createReactChatState, createReactThreadManager, createReactThreadManagerState, createSessionPermissionCache, createToolIntentHandler, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useMCPClient, useMCPTools, useMCPUIIntents, useSuggestions, useSupportedMediaTypes, useThreadManager, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
|
|
4256
|
+
//# sourceMappingURL=chunk-3YKHVLNP.js.map
|
|
4257
|
+
//# sourceMappingURL=chunk-3YKHVLNP.js.map
|