@dudko.dev/agent-web-react 0.0.2 → 0.0.4
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 +40 -0
- package/dist/index.cjs +378 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +171 -2
- package/dist/index.d.ts +171 -2
- package/dist/index.js +358 -18
- package/dist/index.js.map +1 -1
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -252,6 +252,46 @@ function CloudAgent() {
|
|
|
252
252
|
The [`demo/`](demo/) app does exactly this — see
|
|
253
253
|
[`demo/src/providers.ts`](demo/src/providers.ts).
|
|
254
254
|
|
|
255
|
+
## The `useMcp` hook — connect a remote MCP server
|
|
256
|
+
|
|
257
|
+
Let the user name their own MCP server at runtime and hand its tools to the
|
|
258
|
+
agent. Auth is either a static header or the full OAuth 2.1 flow with **dynamic
|
|
259
|
+
client registration** — no client ID to configure, and the access token is
|
|
260
|
+
refreshed for you when it expires mid-run.
|
|
261
|
+
|
|
262
|
+
```tsx
|
|
263
|
+
const mcp = useMcp({ clientName: 'my-app' })
|
|
264
|
+
|
|
265
|
+
await mcp.connect({ url, oauth: true }) // or { url, headers: { Authorization } }
|
|
266
|
+
|
|
267
|
+
// The server wants a sign-in: send the user off from a real click.
|
|
268
|
+
{mcp.status === 'needs-authorization' && <button onClick={mcp.authorize}>Authorize</button>}
|
|
269
|
+
|
|
270
|
+
// Then just merge the tools into any agent config.
|
|
271
|
+
const config = { model, tools: { ...myTools, ...mcp.tools } }
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
`useMcp` returns `{ status, tools, catalog, error, authorizationUrl,
|
|
275
|
+
oauthSupported, completingAuthorization, connect, disconnect, authorize,
|
|
276
|
+
forgetAuthorization, checkOAuthSupport }`. `status` is
|
|
277
|
+
`idle | connecting | connected | needs-authorization | error`.
|
|
278
|
+
|
|
279
|
+
The OAuth round-trip navigates away from your app, so the hook persists what it
|
|
280
|
+
needs and, on the way back, finishes the code exchange, strips `?code=…` from
|
|
281
|
+
the address bar and reconnects before rendering — `completingAuthorization`
|
|
282
|
+
covers that window.
|
|
283
|
+
|
|
284
|
+
Two notes:
|
|
285
|
+
|
|
286
|
+
- The connector lives in the core's optional `@dudko.dev/agent-web/mcp` subpath
|
|
287
|
+
and is loaded with a **dynamic import**, so apps that never call `connect`
|
|
288
|
+
don't pay for `@modelcontextprotocol/sdk`. Install it alongside the core when
|
|
289
|
+
you do use MCP.
|
|
290
|
+
- `oauthSupported` is `false` on cores older than `@dudko.dev/agent-web@0.0.9`,
|
|
291
|
+
which introduced `BrowserOAuthProvider`; header auth still works there. The
|
|
292
|
+
peer floor is `>=0.0.11` — that is the first core whose own peer ranges
|
|
293
|
+
resolve against AI SDK v7.
|
|
294
|
+
|
|
255
295
|
## Components
|
|
256
296
|
|
|
257
297
|
All components are optional and styled by `styles.css` (class-prefixed `awr-`,
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
@@ -32,16 +42,22 @@ __export(src_exports, {
|
|
|
32
42
|
UsageBadge: () => UsageBadge,
|
|
33
43
|
VaultCredentialStore: () => import_agent_web4.VaultCredentialStore,
|
|
34
44
|
agentStateReducer: () => agentStateReducer,
|
|
45
|
+
claimOAuthCallback: () => claimOAuthCallback,
|
|
35
46
|
createAgent: () => import_agent_web4.createAgent,
|
|
36
47
|
createInitialAgentState: () => createInitialAgentState,
|
|
37
48
|
createWebLLMModel: () => import_agent_web4.createWebLLMModel,
|
|
38
49
|
defineTool: () => import_agent_web4.defineTool,
|
|
50
|
+
describeMcpResult: () => describeMcpResult,
|
|
51
|
+
isUnauthorizedError: () => isUnauthorizedError,
|
|
39
52
|
isWebGPUAvailable: () => import_agent_web4.isWebGPUAvailable,
|
|
40
53
|
preloadWebLLMModel: () => import_agent_web4.preloadWebLLMModel,
|
|
54
|
+
readCallbackParams: () => readCallbackParams,
|
|
55
|
+
stripOAuthParams: () => stripOAuthParams,
|
|
41
56
|
unloadWebLLMModel: () => import_agent_web4.unloadWebLLMModel,
|
|
42
57
|
useAgent: () => useAgent,
|
|
43
58
|
useAgentContext: () => useAgentContext,
|
|
44
59
|
useCredentials: () => useCredentials,
|
|
60
|
+
useMcp: () => useMcp,
|
|
45
61
|
useOptionalAgentContext: () => useOptionalAgentContext,
|
|
46
62
|
useWebLLMModel: () => useWebLLMModel
|
|
47
63
|
});
|
|
@@ -356,18 +372,352 @@ var useCredentials = (store) => {
|
|
|
356
372
|
return { store: resolved, setKey, removeKey, hasKey, version };
|
|
357
373
|
};
|
|
358
374
|
|
|
359
|
-
// src/hooks/use-
|
|
375
|
+
// src/hooks/use-mcp.ts
|
|
360
376
|
var import_react3 = require("react");
|
|
361
|
-
var
|
|
362
|
-
var
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
377
|
+
var OAUTH_UNSUPPORTED = "The installed @dudko.dev/agent-web has no MCP OAuth support. Upgrade the core to a version exporting BrowserOAuthProvider, or use a static Authorization header.";
|
|
378
|
+
var PENDING_KEY = "agent-web-react:mcp:pending";
|
|
379
|
+
var OAUTH_KEY = "agent-web-react:mcp:oauth";
|
|
380
|
+
var PENDING_TTL_MS = 10 * 60 * 1e3;
|
|
381
|
+
var modulePromise;
|
|
382
|
+
var loadMcp = () => {
|
|
383
|
+
modulePromise ??= import("@dudko.dev/agent-web/mcp").then((m) => m).catch((err) => {
|
|
384
|
+
modulePromise = void 0;
|
|
385
|
+
throw err;
|
|
386
|
+
});
|
|
387
|
+
return modulePromise;
|
|
388
|
+
};
|
|
389
|
+
var describeMcpResult = (result) => {
|
|
390
|
+
if (!result) return { status: "error", error: "The connector returned no result for the server" };
|
|
391
|
+
if (result.connected) return { status: "connected" };
|
|
392
|
+
if (result.needsAuthorization) return { status: "needs-authorization" };
|
|
393
|
+
return { status: "error", error: result.error ?? "Could not connect to the MCP server" };
|
|
394
|
+
};
|
|
395
|
+
var OAUTH_PARAMS = ["code", "state", "error", "error_description", "iss"];
|
|
396
|
+
var readCallbackParams = (href) => {
|
|
397
|
+
const target = href ?? (typeof globalThis.location !== "undefined" ? globalThis.location.href : void 0);
|
|
398
|
+
if (!target) return void 0;
|
|
399
|
+
let url;
|
|
400
|
+
try {
|
|
401
|
+
url = new URL(target);
|
|
402
|
+
} catch {
|
|
403
|
+
return void 0;
|
|
404
|
+
}
|
|
405
|
+
const params = new URLSearchParams(url.search);
|
|
406
|
+
const hash = url.hash.startsWith("#") ? url.hash.slice(1) : url.hash;
|
|
407
|
+
const q = hash.indexOf("?");
|
|
408
|
+
if (q >= 0) {
|
|
409
|
+
for (const [k, v] of new URLSearchParams(hash.slice(q + 1))) {
|
|
410
|
+
if (!params.has(k)) params.set(k, v);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
const code = params.get("code") ?? void 0;
|
|
414
|
+
const error = params.get("error") ?? void 0;
|
|
415
|
+
if (!code && !error) return void 0;
|
|
416
|
+
return {
|
|
417
|
+
code,
|
|
418
|
+
state: params.get("state") ?? void 0,
|
|
419
|
+
error,
|
|
420
|
+
errorDescription: params.get("error_description") ?? void 0
|
|
421
|
+
};
|
|
422
|
+
};
|
|
423
|
+
var stripOAuthParams = (href) => {
|
|
424
|
+
const url = new URL(href);
|
|
425
|
+
for (const key of OAUTH_PARAMS) url.searchParams.delete(key);
|
|
426
|
+
const hash = url.hash.startsWith("#") ? url.hash.slice(1) : url.hash;
|
|
427
|
+
const q = hash.indexOf("?");
|
|
428
|
+
if (q >= 0) {
|
|
429
|
+
const params = new URLSearchParams(hash.slice(q + 1));
|
|
430
|
+
for (const key of OAUTH_PARAMS) params.delete(key);
|
|
431
|
+
const rest = params.toString();
|
|
432
|
+
url.hash = rest ? `${hash.slice(0, q)}?${rest}` : hash.slice(0, q);
|
|
433
|
+
}
|
|
434
|
+
return url.toString();
|
|
435
|
+
};
|
|
436
|
+
var claimedCallbacks = /* @__PURE__ */ new Set();
|
|
437
|
+
var claimOAuthCallback = (callback) => {
|
|
438
|
+
const key = `${callback.code ?? ""}|${callback.state ?? ""}|${callback.error ?? ""}`;
|
|
439
|
+
if (claimedCallbacks.has(key)) return false;
|
|
440
|
+
claimedCallbacks.add(key);
|
|
441
|
+
return true;
|
|
442
|
+
};
|
|
443
|
+
var readRecord = (key) => {
|
|
444
|
+
try {
|
|
445
|
+
const raw = globalThis.localStorage?.getItem(key);
|
|
446
|
+
return raw ? JSON.parse(raw) : void 0;
|
|
447
|
+
} catch {
|
|
448
|
+
return void 0;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
var writeRecord = (key, value) => {
|
|
452
|
+
try {
|
|
453
|
+
if (value) globalThis.localStorage?.setItem(key, JSON.stringify(value));
|
|
454
|
+
else globalThis.localStorage?.removeItem(key);
|
|
455
|
+
} catch {
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
var defaultRedirectUrl = () => typeof globalThis.location === "undefined" ? "" : `${globalThis.location.origin}${globalThis.location.pathname}`;
|
|
459
|
+
var isUnauthorizedError = (err, mod) => {
|
|
460
|
+
if (!(err instanceof Error)) return false;
|
|
461
|
+
if (mod?.UnauthorizedError && err instanceof mod.UnauthorizedError) return true;
|
|
462
|
+
return err.constructor?.name === "UnauthorizedError";
|
|
463
|
+
};
|
|
464
|
+
var watchAuthorization = (tools, mod, onUnauthorized) => {
|
|
465
|
+
const entries = Object.entries(tools).map(([name, value]) => {
|
|
466
|
+
const tool = value;
|
|
467
|
+
if (typeof tool.execute !== "function") return [name, value];
|
|
468
|
+
const execute = tool.execute.bind(tool);
|
|
469
|
+
const watched = (args, options) => {
|
|
470
|
+
try {
|
|
471
|
+
const out = execute(args, options);
|
|
472
|
+
if (out && typeof out.then === "function") {
|
|
473
|
+
return out.then(void 0, (err) => {
|
|
474
|
+
if (isUnauthorizedError(err, mod)) onUnauthorized();
|
|
475
|
+
throw err;
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
return out;
|
|
479
|
+
} catch (err) {
|
|
480
|
+
if (isUnauthorizedError(err, mod)) onUnauthorized();
|
|
481
|
+
throw err;
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
return [name, { ...value, execute: watched }];
|
|
485
|
+
});
|
|
486
|
+
return Object.fromEntries(entries);
|
|
487
|
+
};
|
|
488
|
+
var useMcp = (options = {}) => {
|
|
489
|
+
const [status, setStatus] = (0, import_react3.useState)("idle");
|
|
490
|
+
const [tools, setTools] = (0, import_react3.useState)(void 0);
|
|
491
|
+
const [catalog, setCatalog] = (0, import_react3.useState)([]);
|
|
367
492
|
const [error, setError] = (0, import_react3.useState)(void 0);
|
|
493
|
+
const [authorizationUrl, setAuthorizationUrl] = (0, import_react3.useState)(void 0);
|
|
494
|
+
const [oauthSupported, setOauthSupported] = (0, import_react3.useState)(void 0);
|
|
495
|
+
const [completingAuthorization, setCompletingAuthorization] = (0, import_react3.useState)(
|
|
496
|
+
() => readCallbackParams() !== void 0
|
|
497
|
+
);
|
|
498
|
+
const connectionRef = (0, import_react3.useRef)(void 0);
|
|
499
|
+
const providerRef = (0, import_react3.useRef)(void 0);
|
|
500
|
+
const epochRef = (0, import_react3.useRef)(0);
|
|
368
501
|
const optionsRef = (0, import_react3.useRef)(options);
|
|
369
502
|
optionsRef.current = options;
|
|
370
|
-
const
|
|
503
|
+
const closeConnection = (0, import_react3.useCallback)(async () => {
|
|
504
|
+
const open = connectionRef.current;
|
|
505
|
+
connectionRef.current = void 0;
|
|
506
|
+
if (open) await open.close().catch(() => {
|
|
507
|
+
});
|
|
508
|
+
}, []);
|
|
509
|
+
const checkOAuthSupport = (0, import_react3.useCallback)(async () => {
|
|
510
|
+
try {
|
|
511
|
+
const mod = await loadMcp();
|
|
512
|
+
const supported = typeof mod.BrowserOAuthProvider === "function";
|
|
513
|
+
setOauthSupported(supported);
|
|
514
|
+
return supported;
|
|
515
|
+
} catch {
|
|
516
|
+
setOauthSupported(false);
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
}, []);
|
|
520
|
+
const connect = (0, import_react3.useCallback)(
|
|
521
|
+
async (opts) => {
|
|
522
|
+
const epoch = ++epochRef.current;
|
|
523
|
+
const current = () => epochRef.current === epoch;
|
|
524
|
+
await closeConnection();
|
|
525
|
+
if (!current()) return;
|
|
526
|
+
setStatus("connecting");
|
|
527
|
+
setError(void 0);
|
|
528
|
+
setAuthorizationUrl(void 0);
|
|
529
|
+
setTools(void 0);
|
|
530
|
+
setCatalog([]);
|
|
531
|
+
try {
|
|
532
|
+
const mod = await loadMcp();
|
|
533
|
+
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
534
|
+
const name = opts.name ?? "mcp";
|
|
535
|
+
let authProvider;
|
|
536
|
+
if (opts.oauth) {
|
|
537
|
+
if (!mod.BrowserOAuthProvider) throw new Error(OAUTH_UNSUPPORTED);
|
|
538
|
+
const oauth = typeof opts.oauth === "object" ? opts.oauth : {};
|
|
539
|
+
const record = {
|
|
540
|
+
url: opts.url,
|
|
541
|
+
name,
|
|
542
|
+
redirectUrl: oauth.redirectUrl ?? defaultRedirectUrl(),
|
|
543
|
+
scope: oauth.scope,
|
|
544
|
+
clientName: oauth.clientName ?? optionsRef.current.clientName,
|
|
545
|
+
startedAt: Date.now()
|
|
546
|
+
};
|
|
547
|
+
authProvider = new mod.BrowserOAuthProvider({
|
|
548
|
+
serverUrl: record.url,
|
|
549
|
+
redirectUrl: record.redirectUrl,
|
|
550
|
+
clientName: record.clientName,
|
|
551
|
+
scope: record.scope
|
|
552
|
+
});
|
|
553
|
+
providerRef.current = authProvider;
|
|
554
|
+
writeRecord(PENDING_KEY, record);
|
|
555
|
+
writeRecord(OAUTH_KEY, record);
|
|
556
|
+
} else {
|
|
557
|
+
providerRef.current = void 0;
|
|
558
|
+
}
|
|
559
|
+
const connection = await mod.connectMcpHttp(
|
|
560
|
+
{
|
|
561
|
+
[name]: {
|
|
562
|
+
url: opts.url,
|
|
563
|
+
headers: opts.oauth ? void 0 : opts.headers,
|
|
564
|
+
authProvider
|
|
565
|
+
}
|
|
566
|
+
},
|
|
567
|
+
{ clientName: optionsRef.current.clientName, onLog: optionsRef.current.onLog }
|
|
568
|
+
);
|
|
569
|
+
if (!current()) {
|
|
570
|
+
await connection.close().catch(() => {
|
|
571
|
+
});
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const outcome = describeMcpResult(connection.results[0]);
|
|
575
|
+
if (outcome.status !== "connected") {
|
|
576
|
+
await connection.close().catch(() => {
|
|
577
|
+
});
|
|
578
|
+
if (outcome.status === "needs-authorization") {
|
|
579
|
+
const url = authProvider?.authorizationUrl;
|
|
580
|
+
setAuthorizationUrl(url ? String(url) : void 0);
|
|
581
|
+
setStatus("needs-authorization");
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
throw new Error(outcome.error);
|
|
585
|
+
}
|
|
586
|
+
connectionRef.current = connection;
|
|
587
|
+
writeRecord(PENDING_KEY, void 0);
|
|
588
|
+
setTools(
|
|
589
|
+
watchAuthorization(connection.tools, mod, () => {
|
|
590
|
+
if (!current()) return;
|
|
591
|
+
const url = providerRef.current?.authorizationUrl;
|
|
592
|
+
if (url) setAuthorizationUrl(String(url));
|
|
593
|
+
setStatus("needs-authorization");
|
|
594
|
+
})
|
|
595
|
+
);
|
|
596
|
+
setCatalog(connection.catalog);
|
|
597
|
+
setStatus("connected");
|
|
598
|
+
} catch (err) {
|
|
599
|
+
if (!current()) return;
|
|
600
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
601
|
+
setStatus("error");
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
[closeConnection]
|
|
605
|
+
);
|
|
606
|
+
const disconnect = (0, import_react3.useCallback)(async () => {
|
|
607
|
+
epochRef.current++;
|
|
608
|
+
await closeConnection();
|
|
609
|
+
setTools(void 0);
|
|
610
|
+
setCatalog([]);
|
|
611
|
+
setAuthorizationUrl(void 0);
|
|
612
|
+
setError(void 0);
|
|
613
|
+
setStatus("idle");
|
|
614
|
+
}, [closeConnection]);
|
|
615
|
+
const authorize = (0, import_react3.useCallback)(() => {
|
|
616
|
+
if (authorizationUrl && typeof globalThis.location !== "undefined") {
|
|
617
|
+
globalThis.location.href = authorizationUrl;
|
|
618
|
+
}
|
|
619
|
+
}, [authorizationUrl]);
|
|
620
|
+
const forgetAuthorization = (0, import_react3.useCallback)(async () => {
|
|
621
|
+
const record = readRecord(OAUTH_KEY);
|
|
622
|
+
writeRecord(PENDING_KEY, void 0);
|
|
623
|
+
writeRecord(OAUTH_KEY, void 0);
|
|
624
|
+
let provider = providerRef.current;
|
|
625
|
+
if (!provider && record) {
|
|
626
|
+
const mod = await loadMcp().catch(() => void 0);
|
|
627
|
+
if (mod?.BrowserOAuthProvider) {
|
|
628
|
+
provider = new mod.BrowserOAuthProvider({
|
|
629
|
+
serverUrl: record.url,
|
|
630
|
+
redirectUrl: record.redirectUrl,
|
|
631
|
+
clientName: record.clientName,
|
|
632
|
+
scope: record.scope
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
await provider?.reset().catch(() => {
|
|
637
|
+
});
|
|
638
|
+
providerRef.current = void 0;
|
|
639
|
+
await disconnect();
|
|
640
|
+
}, [disconnect]);
|
|
641
|
+
(0, import_react3.useEffect)(() => {
|
|
642
|
+
const callback = readCallbackParams();
|
|
643
|
+
if (!callback) {
|
|
644
|
+
setCompletingAuthorization(false);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const pending = readRecord(PENDING_KEY);
|
|
648
|
+
const fresh = pending !== void 0 && (pending.startedAt === void 0 || Date.now() - pending.startedAt < PENDING_TTL_MS);
|
|
649
|
+
if (!pending || !fresh) {
|
|
650
|
+
if (pending) writeRecord(PENDING_KEY, void 0);
|
|
651
|
+
setCompletingAuthorization(false);
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
if (!claimOAuthCallback(callback)) {
|
|
655
|
+
setCompletingAuthorization(false);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (typeof globalThis.history !== "undefined") {
|
|
659
|
+
globalThis.history.replaceState(null, "", stripOAuthParams(globalThis.location.href));
|
|
660
|
+
}
|
|
661
|
+
void (async () => {
|
|
662
|
+
try {
|
|
663
|
+
const mod = await loadMcp();
|
|
664
|
+
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
665
|
+
if (!mod.BrowserOAuthProvider || !mod.finishMcpOAuth) throw new Error(OAUTH_UNSUPPORTED);
|
|
666
|
+
const provider = new mod.BrowserOAuthProvider({
|
|
667
|
+
serverUrl: pending.url,
|
|
668
|
+
redirectUrl: pending.redirectUrl ?? defaultRedirectUrl(),
|
|
669
|
+
clientName: pending.clientName,
|
|
670
|
+
scope: pending.scope
|
|
671
|
+
});
|
|
672
|
+
providerRef.current = provider;
|
|
673
|
+
await mod.finishMcpOAuth(provider, callback);
|
|
674
|
+
writeRecord(PENDING_KEY, void 0);
|
|
675
|
+
setCompletingAuthorization(false);
|
|
676
|
+
await connect({ url: pending.url, name: pending.name, oauth: true });
|
|
677
|
+
} catch (err) {
|
|
678
|
+
writeRecord(PENDING_KEY, void 0);
|
|
679
|
+
setError(err instanceof Error ? err.message : String(err));
|
|
680
|
+
setStatus("error");
|
|
681
|
+
} finally {
|
|
682
|
+
setCompletingAuthorization(false);
|
|
683
|
+
}
|
|
684
|
+
})();
|
|
685
|
+
}, [connect]);
|
|
686
|
+
(0, import_react3.useEffect)(
|
|
687
|
+
() => () => {
|
|
688
|
+
epochRef.current++;
|
|
689
|
+
void closeConnection();
|
|
690
|
+
},
|
|
691
|
+
[closeConnection]
|
|
692
|
+
);
|
|
693
|
+
return {
|
|
694
|
+
status,
|
|
695
|
+
tools,
|
|
696
|
+
catalog,
|
|
697
|
+
error,
|
|
698
|
+
authorizationUrl,
|
|
699
|
+
oauthSupported,
|
|
700
|
+
completingAuthorization,
|
|
701
|
+
connect,
|
|
702
|
+
disconnect,
|
|
703
|
+
authorize,
|
|
704
|
+
forgetAuthorization,
|
|
705
|
+
checkOAuthSupport
|
|
706
|
+
};
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
// src/hooks/use-webllm-model.ts
|
|
710
|
+
var import_react4 = require("react");
|
|
711
|
+
var import_agent_web3 = require("@dudko.dev/agent-web");
|
|
712
|
+
var useWebLLMModel = (modelId, options) => {
|
|
713
|
+
const [model, setModel] = (0, import_react4.useState)(void 0);
|
|
714
|
+
const [loading, setLoading] = (0, import_react4.useState)(false);
|
|
715
|
+
const [progress, setProgress] = (0, import_react4.useState)(0);
|
|
716
|
+
const [text, setText] = (0, import_react4.useState)("");
|
|
717
|
+
const [error, setError] = (0, import_react4.useState)(void 0);
|
|
718
|
+
const optionsRef = (0, import_react4.useRef)(options);
|
|
719
|
+
optionsRef.current = options;
|
|
720
|
+
const load = (0, import_react4.useCallback)(async () => {
|
|
371
721
|
if (!(0, import_agent_web3.isWebGPUAvailable)()) {
|
|
372
722
|
setError("WebGPU is not available in this browser.");
|
|
373
723
|
return void 0;
|
|
@@ -413,22 +763,22 @@ var useWebLLMModel = (modelId, options) => {
|
|
|
413
763
|
};
|
|
414
764
|
|
|
415
765
|
// src/context.tsx
|
|
416
|
-
var
|
|
766
|
+
var import_react5 = require("react");
|
|
417
767
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
418
|
-
var AgentContext = (0,
|
|
768
|
+
var AgentContext = (0, import_react5.createContext)(null);
|
|
419
769
|
var AgentProvider = ({ config, children, ...options }) => {
|
|
420
770
|
const agent = useAgent(config, options);
|
|
421
771
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AgentContext.Provider, { value: agent, children });
|
|
422
772
|
};
|
|
423
773
|
var useAgentContext = () => {
|
|
424
|
-
const ctx = (0,
|
|
774
|
+
const ctx = (0, import_react5.useContext)(AgentContext);
|
|
425
775
|
if (!ctx) throw new Error("useAgentContext must be used within an <AgentProvider>");
|
|
426
776
|
return ctx;
|
|
427
777
|
};
|
|
428
|
-
var useOptionalAgentContext = () => (0,
|
|
778
|
+
var useOptionalAgentContext = () => (0, import_react5.useContext)(AgentContext);
|
|
429
779
|
|
|
430
780
|
// src/components/AgentChat.tsx
|
|
431
|
-
var
|
|
781
|
+
var import_react7 = require("react");
|
|
432
782
|
|
|
433
783
|
// src/components/icons.tsx
|
|
434
784
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
@@ -458,7 +808,7 @@ var AlertIcon = ({ size, className }) => /* @__PURE__ */ (0, import_jsx_runtime2
|
|
|
458
808
|
var ToolIcon = ({ size, className }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("svg", { ...base(size), className, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("path", { d: "M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76Z" }) });
|
|
459
809
|
|
|
460
810
|
// src/components/Composer.tsx
|
|
461
|
-
var
|
|
811
|
+
var import_react6 = require("react");
|
|
462
812
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
463
813
|
var Composer = ({
|
|
464
814
|
onSubmit,
|
|
@@ -468,7 +818,7 @@ var Composer = ({
|
|
|
468
818
|
placeholder = "Ask the agent to do something\u2026",
|
|
469
819
|
className
|
|
470
820
|
}) => {
|
|
471
|
-
const [text, setText] = (0,
|
|
821
|
+
const [text, setText] = (0, import_react6.useState)("");
|
|
472
822
|
const submit = () => {
|
|
473
823
|
const value = text.trim();
|
|
474
824
|
if (!value || busy || disabled) return;
|
|
@@ -652,8 +1002,8 @@ var AgentChat = ({
|
|
|
652
1002
|
"<AgentChat> needs a `controller` prop from useAgent(), or an enclosing <AgentProvider>."
|
|
653
1003
|
);
|
|
654
1004
|
}
|
|
655
|
-
const bodyRef = (0,
|
|
656
|
-
(0,
|
|
1005
|
+
const bodyRef = (0, import_react7.useRef)(null);
|
|
1006
|
+
(0, import_react7.useEffect)(() => {
|
|
657
1007
|
const el = bodyRef.current;
|
|
658
1008
|
if (el) el.scrollTop = el.scrollHeight;
|
|
659
1009
|
}, [agent.messages, agent.steps, agent.finalText, agent.modelLoad]);
|
|
@@ -716,7 +1066,7 @@ var PlanView = ({ plan, showThought = true, className }) => {
|
|
|
716
1066
|
};
|
|
717
1067
|
|
|
718
1068
|
// src/components/ApiKeyForm.tsx
|
|
719
|
-
var
|
|
1069
|
+
var import_react8 = require("react");
|
|
720
1070
|
var import_jsx_runtime10 = require("react/jsx-runtime");
|
|
721
1071
|
var ApiKeyForm = ({
|
|
722
1072
|
credentials,
|
|
@@ -726,10 +1076,10 @@ var ApiKeyForm = ({
|
|
|
726
1076
|
onChange,
|
|
727
1077
|
className
|
|
728
1078
|
}) => {
|
|
729
|
-
const [value, setValue] = (0,
|
|
730
|
-
const [present, setPresent] = (0,
|
|
731
|
-
const [busy, setBusy] = (0,
|
|
732
|
-
(0,
|
|
1079
|
+
const [value, setValue] = (0, import_react8.useState)("");
|
|
1080
|
+
const [present, setPresent] = (0, import_react8.useState)(false);
|
|
1081
|
+
const [busy, setBusy] = (0, import_react8.useState)(false);
|
|
1082
|
+
(0, import_react8.useEffect)(() => {
|
|
733
1083
|
let active = true;
|
|
734
1084
|
credentials.hasKey(credentialRef).then((has) => {
|
|
735
1085
|
if (active) setPresent(has);
|
|
@@ -828,16 +1178,22 @@ var import_agent_web4 = require("@dudko.dev/agent-web");
|
|
|
828
1178
|
UsageBadge,
|
|
829
1179
|
VaultCredentialStore,
|
|
830
1180
|
agentStateReducer,
|
|
1181
|
+
claimOAuthCallback,
|
|
831
1182
|
createAgent,
|
|
832
1183
|
createInitialAgentState,
|
|
833
1184
|
createWebLLMModel,
|
|
834
1185
|
defineTool,
|
|
1186
|
+
describeMcpResult,
|
|
1187
|
+
isUnauthorizedError,
|
|
835
1188
|
isWebGPUAvailable,
|
|
836
1189
|
preloadWebLLMModel,
|
|
1190
|
+
readCallbackParams,
|
|
1191
|
+
stripOAuthParams,
|
|
837
1192
|
unloadWebLLMModel,
|
|
838
1193
|
useAgent,
|
|
839
1194
|
useAgentContext,
|
|
840
1195
|
useCredentials,
|
|
1196
|
+
useMcp,
|
|
841
1197
|
useOptionalAgentContext,
|
|
842
1198
|
useWebLLMModel
|
|
843
1199
|
});
|