@dudko.dev/agent-web-react 0.0.18 → 0.0.20
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 +34 -4
- package/dist/index.cjs +290 -792
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +144 -271
- package/dist/index.d.ts +144 -271
- package/dist/index.js +278 -764
- package/dist/index.js.map +1 -1
- package/dist/mcp.cjs +711 -0
- package/dist/mcp.cjs.map +1 -0
- package/dist/mcp.d.cts +265 -0
- package/dist/mcp.d.ts +265 -0
- package/dist/mcp.js +668 -0
- package/dist/mcp.js.map +1 -0
- package/dist/styles.css +99 -0
- package/package.json +13 -3
package/dist/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// src/hooks/use-agent.ts
|
|
2
2
|
import { useCallback, useEffect, useReducer, useRef, useState } from "react";
|
|
3
3
|
import {
|
|
4
|
-
createAgent
|
|
4
|
+
createAgent,
|
|
5
|
+
createRunControl
|
|
5
6
|
} from "@dudko.dev/agent-web";
|
|
6
7
|
|
|
7
8
|
// src/state.ts
|
|
@@ -44,6 +45,9 @@ var createInitialAgentState = () => ({
|
|
|
44
45
|
runMessageId: void 0,
|
|
45
46
|
approvals: [],
|
|
46
47
|
subagents: [],
|
|
48
|
+
queued: [],
|
|
49
|
+
notes: [],
|
|
50
|
+
undelivered: [],
|
|
47
51
|
skills: [],
|
|
48
52
|
discoveredTools: [],
|
|
49
53
|
compactions: [],
|
|
@@ -68,24 +72,24 @@ var patchStep = (steps, id, fn) => steps.map((s) => s.id === id ? fn(s) : s);
|
|
|
68
72
|
var patchApproval = (approvals, id, fn) => approvals.map((a) => a.id === id ? fn(a) : a);
|
|
69
73
|
var patchSubagent = (subagents, id, fn) => subagents.map((s) => s.id === id ? fn(s) : s);
|
|
70
74
|
var runningStepId = (steps) => [...steps].reverse().find((s) => s.status === "running")?.id;
|
|
71
|
-
var applySubagentEvent = (
|
|
75
|
+
var applySubagentEvent = (view, event) => {
|
|
72
76
|
switch (event.type) {
|
|
73
77
|
case "step.start":
|
|
74
78
|
return {
|
|
75
|
-
...
|
|
76
|
-
steps:
|
|
79
|
+
...view,
|
|
80
|
+
steps: view.steps + 1,
|
|
77
81
|
activity: `step ${event.index}/${event.total}: ${event.step.description}`
|
|
78
82
|
};
|
|
79
83
|
case "step.tool-call":
|
|
80
|
-
return { ...
|
|
84
|
+
return { ...view, toolCalls: view.toolCalls + 1, activity: `\u2192 ${event.name}` };
|
|
81
85
|
case "final.text-delta":
|
|
82
|
-
return { ...
|
|
86
|
+
return { ...view, text: view.text + event.delta, activity: "writing the answer" };
|
|
83
87
|
case "final":
|
|
84
|
-
return { ...
|
|
88
|
+
return { ...view, text: event.text };
|
|
85
89
|
case "usage":
|
|
86
|
-
return { ...
|
|
90
|
+
return { ...view, usage: addUsage(view.usage, event.usage) };
|
|
87
91
|
default:
|
|
88
|
-
return
|
|
92
|
+
return view;
|
|
89
93
|
}
|
|
90
94
|
};
|
|
91
95
|
var finalizeAssistant = (messages, content, extra = {}) => {
|
|
@@ -147,11 +151,20 @@ var applyEvent = (state, event, ctx) => {
|
|
|
147
151
|
planThought: state.planThought || event.plan.thought
|
|
148
152
|
};
|
|
149
153
|
case "plan.revised":
|
|
150
|
-
return {
|
|
154
|
+
return {
|
|
155
|
+
...state,
|
|
156
|
+
plan: event.plan,
|
|
157
|
+
// `replan.decision` came first: keep its "because the user spoke" flag.
|
|
158
|
+
replan: {
|
|
159
|
+
mode: "revise",
|
|
160
|
+
reason: event.reason,
|
|
161
|
+
...state.replan?.byUser ? { byUser: true } : {}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
151
164
|
case "plan.step-added":
|
|
152
165
|
return state;
|
|
153
166
|
case "step.start": {
|
|
154
|
-
const
|
|
167
|
+
const view = {
|
|
155
168
|
id: event.step.id,
|
|
156
169
|
index: event.index,
|
|
157
170
|
total: event.total,
|
|
@@ -162,7 +175,7 @@ var applyEvent = (state, event, ctx) => {
|
|
|
162
175
|
status: "running",
|
|
163
176
|
blocked: false
|
|
164
177
|
};
|
|
165
|
-
return { ...state, steps: [...state.steps,
|
|
178
|
+
return { ...state, steps: [...state.steps, view] };
|
|
166
179
|
}
|
|
167
180
|
case "step.reasoning-delta":
|
|
168
181
|
return {
|
|
@@ -247,7 +260,27 @@ var applyEvent = (state, event, ctx) => {
|
|
|
247
260
|
}))
|
|
248
261
|
};
|
|
249
262
|
case "replan.decision":
|
|
250
|
-
return {
|
|
263
|
+
return {
|
|
264
|
+
...state,
|
|
265
|
+
replan: {
|
|
266
|
+
mode: event.mode,
|
|
267
|
+
reason: event.reason,
|
|
268
|
+
...event.byUser ? { byUser: true } : {}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
case "message.queued":
|
|
272
|
+
return state.queued.some((q) => q.id === event.id) ? state : { ...state, queued: [...state.queued, { id: event.id, text: event.text }] };
|
|
273
|
+
case "message.delivered": {
|
|
274
|
+
const anchor = event.at === "round" ? runningStepId(state.steps) : event.at === "step" ? state.steps[state.steps.length - 1]?.id : void 0;
|
|
275
|
+
return {
|
|
276
|
+
...state,
|
|
277
|
+
queued: state.queued.filter((q) => q.id !== event.id),
|
|
278
|
+
notes: [
|
|
279
|
+
...state.notes,
|
|
280
|
+
{ id: event.id, text: event.text, at: event.at, ...anchor ? { stepId: anchor } : {} }
|
|
281
|
+
]
|
|
282
|
+
};
|
|
283
|
+
}
|
|
251
284
|
case "final.text-delta": {
|
|
252
285
|
const finalText = state.finalText + event.delta;
|
|
253
286
|
return { ...state, finalText, messages: streamAssistant(state.messages, finalText) };
|
|
@@ -317,6 +350,7 @@ var applyEvent = (state, event, ctx) => {
|
|
|
317
350
|
name: event.name,
|
|
318
351
|
task: event.task,
|
|
319
352
|
status: "running",
|
|
353
|
+
...event.profile ? { profile: event.profile } : {},
|
|
320
354
|
stepId: runningStepId(state.steps),
|
|
321
355
|
activity: "starting",
|
|
322
356
|
steps: 0,
|
|
@@ -351,8 +385,8 @@ var applyEvent = (state, event, ctx) => {
|
|
|
351
385
|
...state,
|
|
352
386
|
subagents: patchSubagent(state.subagents, event.id, (s) => ({
|
|
353
387
|
...s,
|
|
354
|
-
status: "error",
|
|
355
|
-
activity: "failed",
|
|
388
|
+
status: event.stopped ? "stopped" : "error",
|
|
389
|
+
activity: event.stopped ? "stopped" : "failed",
|
|
356
390
|
error: event.error
|
|
357
391
|
}))
|
|
358
392
|
};
|
|
@@ -413,6 +447,7 @@ var syncRunMessage = (state) => {
|
|
|
413
447
|
const patch = {};
|
|
414
448
|
if (m.steps !== state.steps) patch.steps = state.steps;
|
|
415
449
|
if (m.subagents !== state.subagents) patch.subagents = state.subagents;
|
|
450
|
+
if (state.notes.length > 0 && m.notes !== state.notes) patch.notes = state.notes;
|
|
416
451
|
if (m.usage !== state.usage) patch.usage = state.usage;
|
|
417
452
|
if ((m.reasoning ?? "") !== state.finalReasoning) patch.reasoning = state.finalReasoning;
|
|
418
453
|
if (m.skills !== state.skills) patch.skills = state.skills;
|
|
@@ -442,6 +477,8 @@ var agentStateReducer = (state, action) => {
|
|
|
442
477
|
const events = cap > 0 ? [...state.events, action.event].slice(-cap) : [...state.events, action.event];
|
|
443
478
|
return { ...next, events };
|
|
444
479
|
}
|
|
480
|
+
case "undelivered":
|
|
481
|
+
return { ...state, queued: [], undelivered: action.texts };
|
|
445
482
|
case "load": {
|
|
446
483
|
const maxSeq = action.messages.reduce((n, m) => {
|
|
447
484
|
const k = Number(/^msg-(\d+)$/.exec(m.id)?.[1] ?? -1);
|
|
@@ -468,6 +505,9 @@ var useAgent = (config, options = {}) => {
|
|
|
468
505
|
const [state, dispatch] = useReducer(agentStateReducer, void 0, createInitialAgentState);
|
|
469
506
|
const agentRef = useRef(void 0);
|
|
470
507
|
const abortRef = useRef(void 0);
|
|
508
|
+
const controlRef = useRef(void 0);
|
|
509
|
+
const stateRef = useRef(state);
|
|
510
|
+
stateRef.current = state;
|
|
471
511
|
const [generation, setGeneration] = useState(0);
|
|
472
512
|
const configRef = useRef(config);
|
|
473
513
|
configRef.current = config;
|
|
@@ -540,16 +580,21 @@ var useAgent = (config, options = {}) => {
|
|
|
540
580
|
},
|
|
541
581
|
[settleAll]
|
|
542
582
|
);
|
|
583
|
+
const runRef = useRef(void 0);
|
|
543
584
|
const run = useCallback(
|
|
544
585
|
async (goal, runOptions) => {
|
|
545
586
|
const agent = agentRef.current;
|
|
546
587
|
if (!agent || abortRef.current) return void 0;
|
|
547
588
|
const controller = new AbortController();
|
|
548
589
|
abortRef.current = controller;
|
|
590
|
+
const control = createRunControl();
|
|
591
|
+
controlRef.current = control;
|
|
592
|
+
let left = [];
|
|
549
593
|
dispatch({ type: "status", status: "running" });
|
|
550
594
|
try {
|
|
551
|
-
|
|
595
|
+
const result = await agent.run(goal, {
|
|
552
596
|
signal: controller.signal,
|
|
597
|
+
control,
|
|
553
598
|
sessionId: sessionRef.current,
|
|
554
599
|
...runOptions?.images?.length ? { images: runOptions.images } : {},
|
|
555
600
|
...runOptions?.files?.length ? { files: runOptions.files } : {},
|
|
@@ -569,7 +614,10 @@ var useAgent = (config, options = {}) => {
|
|
|
569
614
|
}
|
|
570
615
|
}
|
|
571
616
|
});
|
|
617
|
+
left = result.undelivered ?? [];
|
|
618
|
+
return result;
|
|
572
619
|
} catch (err) {
|
|
620
|
+
left = control.pending();
|
|
573
621
|
dispatch({
|
|
574
622
|
type: "event",
|
|
575
623
|
event: { type: "error", phase: "run", error: errMessage(err) },
|
|
@@ -578,12 +626,36 @@ var useAgent = (config, options = {}) => {
|
|
|
578
626
|
return void 0;
|
|
579
627
|
} finally {
|
|
580
628
|
abortRef.current = void 0;
|
|
629
|
+
controlRef.current = void 0;
|
|
581
630
|
settleAll({ approved: false, reason: "the run ended" });
|
|
582
631
|
dispatch({ type: "status", ...buildStatusRef.current });
|
|
632
|
+
if (left.length > 0) {
|
|
633
|
+
if (!controller.signal.aborted && optionsRef.current.followUp !== false) {
|
|
634
|
+
dispatch({ type: "undelivered", texts: [] });
|
|
635
|
+
void runRef.current?.(left.join("\n\n"));
|
|
636
|
+
} else {
|
|
637
|
+
dispatch({ type: "undelivered", texts: left });
|
|
638
|
+
}
|
|
639
|
+
} else {
|
|
640
|
+
dispatch({ type: "undelivered", texts: [] });
|
|
641
|
+
}
|
|
583
642
|
}
|
|
584
643
|
},
|
|
585
644
|
[settleAll]
|
|
586
645
|
);
|
|
646
|
+
runRef.current = run;
|
|
647
|
+
const send = useCallback((text) => controlRef.current?.send(text) ?? false, []);
|
|
648
|
+
const stopSubagent = useCallback(
|
|
649
|
+
(id) => controlRef.current?.stopSubagent(id) ?? false,
|
|
650
|
+
[]
|
|
651
|
+
);
|
|
652
|
+
const sendUndelivered = useCallback(() => {
|
|
653
|
+
const texts = stateRef.current.undelivered;
|
|
654
|
+
if (texts.length === 0) return;
|
|
655
|
+
dispatch({ type: "undelivered", texts: [] });
|
|
656
|
+
void runRef.current?.(texts.join("\n\n"));
|
|
657
|
+
}, []);
|
|
658
|
+
const discardUndelivered = useCallback(() => dispatch({ type: "undelivered", texts: [] }), []);
|
|
587
659
|
const stop = useCallback(() => {
|
|
588
660
|
abortRef.current?.abort();
|
|
589
661
|
settleAll({ approved: false, reason: "stopped by the user" });
|
|
@@ -637,6 +709,10 @@ var useAgent = (config, options = {}) => {
|
|
|
637
709
|
...state,
|
|
638
710
|
run,
|
|
639
711
|
stop,
|
|
712
|
+
send,
|
|
713
|
+
stopSubagent,
|
|
714
|
+
sendUndelivered,
|
|
715
|
+
discardUndelivered,
|
|
640
716
|
reset,
|
|
641
717
|
reload,
|
|
642
718
|
agent: agentRef.current,
|
|
@@ -952,665 +1028,6 @@ var useCredentials = (store) => {
|
|
|
952
1028
|
return { store: resolved, setKey, removeKey, hasKey, version };
|
|
953
1029
|
};
|
|
954
1030
|
|
|
955
|
-
// src/hooks/use-mcp.ts
|
|
956
|
-
import { useCallback as useCallback6, useEffect as useEffect5, useRef as useRef4, useState as useState6 } from "react";
|
|
957
|
-
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.";
|
|
958
|
-
var PENDING_KEY = "agent-web-react:mcp:pending";
|
|
959
|
-
var OAUTH_KEY = "agent-web-react:mcp:oauth";
|
|
960
|
-
var PENDING_TTL_MS = 10 * 60 * 1e3;
|
|
961
|
-
var modulePromise;
|
|
962
|
-
var loadMcp = () => {
|
|
963
|
-
modulePromise ??= import("@dudko.dev/agent-web/mcp").then((m) => m).catch((err) => {
|
|
964
|
-
modulePromise = void 0;
|
|
965
|
-
throw err;
|
|
966
|
-
});
|
|
967
|
-
return modulePromise;
|
|
968
|
-
};
|
|
969
|
-
var describeMcpResult = (result) => {
|
|
970
|
-
if (!result) return { status: "error", error: "The connector returned no result for the server" };
|
|
971
|
-
if (result.connected) return { status: "connected" };
|
|
972
|
-
if (result.needsAuthorization) return { status: "needs-authorization" };
|
|
973
|
-
return { status: "error", error: result.error ?? "Could not connect to the MCP server" };
|
|
974
|
-
};
|
|
975
|
-
var OAUTH_PARAMS = ["code", "state", "error", "error_description", "iss"];
|
|
976
|
-
var readCallbackParams = (href) => {
|
|
977
|
-
const target = href ?? (typeof globalThis.location !== "undefined" ? globalThis.location.href : void 0);
|
|
978
|
-
if (!target) return void 0;
|
|
979
|
-
let url;
|
|
980
|
-
try {
|
|
981
|
-
url = new URL(target);
|
|
982
|
-
} catch {
|
|
983
|
-
return void 0;
|
|
984
|
-
}
|
|
985
|
-
const params = new URLSearchParams(url.search);
|
|
986
|
-
const hash = url.hash.startsWith("#") ? url.hash.slice(1) : url.hash;
|
|
987
|
-
const q = hash.indexOf("?");
|
|
988
|
-
if (q >= 0) {
|
|
989
|
-
for (const [k, v] of new URLSearchParams(hash.slice(q + 1))) {
|
|
990
|
-
if (!params.has(k)) params.set(k, v);
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
const code = params.get("code") ?? void 0;
|
|
994
|
-
const error = params.get("error") ?? void 0;
|
|
995
|
-
if (!code && !error) return void 0;
|
|
996
|
-
return {
|
|
997
|
-
code,
|
|
998
|
-
state: params.get("state") ?? void 0,
|
|
999
|
-
error,
|
|
1000
|
-
errorDescription: params.get("error_description") ?? void 0
|
|
1001
|
-
};
|
|
1002
|
-
};
|
|
1003
|
-
var stripOAuthParams = (href) => {
|
|
1004
|
-
const url = new URL(href);
|
|
1005
|
-
for (const key of OAUTH_PARAMS) url.searchParams.delete(key);
|
|
1006
|
-
const hash = url.hash.startsWith("#") ? url.hash.slice(1) : url.hash;
|
|
1007
|
-
const q = hash.indexOf("?");
|
|
1008
|
-
if (q >= 0) {
|
|
1009
|
-
const params = new URLSearchParams(hash.slice(q + 1));
|
|
1010
|
-
for (const key of OAUTH_PARAMS) params.delete(key);
|
|
1011
|
-
const rest = params.toString();
|
|
1012
|
-
url.hash = rest ? `${hash.slice(0, q)}?${rest}` : hash.slice(0, q);
|
|
1013
|
-
}
|
|
1014
|
-
return url.toString();
|
|
1015
|
-
};
|
|
1016
|
-
var claimedCallbacks = /* @__PURE__ */ new Set();
|
|
1017
|
-
var claimOAuthCallback = (callback) => {
|
|
1018
|
-
const key = `${callback.code ?? ""}|${callback.state ?? ""}|${callback.error ?? ""}`;
|
|
1019
|
-
if (claimedCallbacks.has(key)) return false;
|
|
1020
|
-
claimedCallbacks.add(key);
|
|
1021
|
-
return true;
|
|
1022
|
-
};
|
|
1023
|
-
var readRecord = (key) => {
|
|
1024
|
-
try {
|
|
1025
|
-
const raw = globalThis.localStorage?.getItem(key);
|
|
1026
|
-
return raw ? JSON.parse(raw) : void 0;
|
|
1027
|
-
} catch {
|
|
1028
|
-
return void 0;
|
|
1029
|
-
}
|
|
1030
|
-
};
|
|
1031
|
-
var writeRecord = (key, value) => {
|
|
1032
|
-
try {
|
|
1033
|
-
if (value) globalThis.localStorage?.setItem(key, JSON.stringify(value));
|
|
1034
|
-
else globalThis.localStorage?.removeItem(key);
|
|
1035
|
-
} catch {
|
|
1036
|
-
}
|
|
1037
|
-
};
|
|
1038
|
-
var defaultRedirectUrl = () => typeof globalThis.location === "undefined" ? "" : `${globalThis.location.origin}${globalThis.location.pathname}`;
|
|
1039
|
-
var isUnauthorizedError = (err, mod) => {
|
|
1040
|
-
if (!(err instanceof Error)) return false;
|
|
1041
|
-
if (mod?.UnauthorizedError && err instanceof mod.UnauthorizedError) return true;
|
|
1042
|
-
return err.constructor?.name === "UnauthorizedError";
|
|
1043
|
-
};
|
|
1044
|
-
var watchAuthorization = (tools, mod, onUnauthorized) => {
|
|
1045
|
-
const entries = Object.entries(tools).map(([name, value]) => {
|
|
1046
|
-
const tool = value;
|
|
1047
|
-
if (typeof tool.execute !== "function") return [name, value];
|
|
1048
|
-
const execute = tool.execute.bind(tool);
|
|
1049
|
-
const watched = (args, options) => {
|
|
1050
|
-
try {
|
|
1051
|
-
const out = execute(args, options);
|
|
1052
|
-
if (out && typeof out.then === "function") {
|
|
1053
|
-
return out.then(void 0, (err) => {
|
|
1054
|
-
if (isUnauthorizedError(err, mod)) onUnauthorized();
|
|
1055
|
-
throw err;
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
return out;
|
|
1059
|
-
} catch (err) {
|
|
1060
|
-
if (isUnauthorizedError(err, mod)) onUnauthorized();
|
|
1061
|
-
throw err;
|
|
1062
|
-
}
|
|
1063
|
-
};
|
|
1064
|
-
return [name, { ...value, execute: watched }];
|
|
1065
|
-
});
|
|
1066
|
-
return Object.fromEntries(entries);
|
|
1067
|
-
};
|
|
1068
|
-
var useMcp = (options = {}) => {
|
|
1069
|
-
const [status, setStatus] = useState6("idle");
|
|
1070
|
-
const [tools, setTools] = useState6(void 0);
|
|
1071
|
-
const [catalog, setCatalog] = useState6([]);
|
|
1072
|
-
const [error, setError] = useState6(void 0);
|
|
1073
|
-
const [authorizationUrl, setAuthorizationUrl] = useState6(void 0);
|
|
1074
|
-
const [oauthSupported, setOauthSupported] = useState6(void 0);
|
|
1075
|
-
const [completingAuthorization, setCompletingAuthorization] = useState6(
|
|
1076
|
-
() => readCallbackParams() !== void 0
|
|
1077
|
-
);
|
|
1078
|
-
const connectionRef = useRef4(void 0);
|
|
1079
|
-
const providerRef = useRef4(void 0);
|
|
1080
|
-
const epochRef = useRef4(0);
|
|
1081
|
-
const optionsRef = useRef4(options);
|
|
1082
|
-
optionsRef.current = options;
|
|
1083
|
-
const closeConnection = useCallback6(async () => {
|
|
1084
|
-
const open = connectionRef.current;
|
|
1085
|
-
connectionRef.current = void 0;
|
|
1086
|
-
if (open) await open.close().catch(() => {
|
|
1087
|
-
});
|
|
1088
|
-
}, []);
|
|
1089
|
-
const checkOAuthSupport = useCallback6(async () => {
|
|
1090
|
-
try {
|
|
1091
|
-
const mod = await loadMcp();
|
|
1092
|
-
const supported = typeof mod.BrowserOAuthProvider === "function";
|
|
1093
|
-
setOauthSupported(supported);
|
|
1094
|
-
return supported;
|
|
1095
|
-
} catch {
|
|
1096
|
-
setOauthSupported(false);
|
|
1097
|
-
return false;
|
|
1098
|
-
}
|
|
1099
|
-
}, []);
|
|
1100
|
-
const connect = useCallback6(
|
|
1101
|
-
async (opts) => {
|
|
1102
|
-
const epoch = ++epochRef.current;
|
|
1103
|
-
const current = () => epochRef.current === epoch;
|
|
1104
|
-
await closeConnection();
|
|
1105
|
-
if (!current()) return;
|
|
1106
|
-
setStatus("connecting");
|
|
1107
|
-
setError(void 0);
|
|
1108
|
-
setAuthorizationUrl(void 0);
|
|
1109
|
-
setTools(void 0);
|
|
1110
|
-
setCatalog([]);
|
|
1111
|
-
try {
|
|
1112
|
-
const mod = await loadMcp();
|
|
1113
|
-
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
1114
|
-
const name = opts.name ?? "mcp";
|
|
1115
|
-
let authProvider;
|
|
1116
|
-
if (opts.oauth) {
|
|
1117
|
-
if (!mod.BrowserOAuthProvider) throw new Error(OAUTH_UNSUPPORTED);
|
|
1118
|
-
const oauth = typeof opts.oauth === "object" ? opts.oauth : {};
|
|
1119
|
-
const record = {
|
|
1120
|
-
url: opts.url,
|
|
1121
|
-
name,
|
|
1122
|
-
redirectUrl: oauth.redirectUrl ?? defaultRedirectUrl(),
|
|
1123
|
-
scope: oauth.scope,
|
|
1124
|
-
clientName: oauth.clientName ?? optionsRef.current.clientName,
|
|
1125
|
-
startedAt: Date.now()
|
|
1126
|
-
};
|
|
1127
|
-
authProvider = new mod.BrowserOAuthProvider({
|
|
1128
|
-
serverUrl: record.url,
|
|
1129
|
-
redirectUrl: record.redirectUrl,
|
|
1130
|
-
clientName: record.clientName,
|
|
1131
|
-
scope: record.scope
|
|
1132
|
-
});
|
|
1133
|
-
providerRef.current = authProvider;
|
|
1134
|
-
writeRecord(PENDING_KEY, record);
|
|
1135
|
-
writeRecord(OAUTH_KEY, record);
|
|
1136
|
-
} else {
|
|
1137
|
-
providerRef.current = void 0;
|
|
1138
|
-
}
|
|
1139
|
-
const connection = await mod.connectMcpHttp(
|
|
1140
|
-
{
|
|
1141
|
-
[name]: {
|
|
1142
|
-
url: opts.url,
|
|
1143
|
-
headers: opts.oauth ? void 0 : opts.headers,
|
|
1144
|
-
authProvider
|
|
1145
|
-
}
|
|
1146
|
-
},
|
|
1147
|
-
{ clientName: optionsRef.current.clientName, onLog: optionsRef.current.onLog }
|
|
1148
|
-
);
|
|
1149
|
-
if (!current()) {
|
|
1150
|
-
await connection.close().catch(() => {
|
|
1151
|
-
});
|
|
1152
|
-
return;
|
|
1153
|
-
}
|
|
1154
|
-
const outcome = describeMcpResult(connection.results[0]);
|
|
1155
|
-
if (outcome.status !== "connected") {
|
|
1156
|
-
await connection.close().catch(() => {
|
|
1157
|
-
});
|
|
1158
|
-
if (outcome.status === "needs-authorization") {
|
|
1159
|
-
const url = authProvider?.authorizationUrl;
|
|
1160
|
-
setAuthorizationUrl(url ? String(url) : void 0);
|
|
1161
|
-
setStatus("needs-authorization");
|
|
1162
|
-
return;
|
|
1163
|
-
}
|
|
1164
|
-
throw new Error(outcome.error);
|
|
1165
|
-
}
|
|
1166
|
-
connectionRef.current = connection;
|
|
1167
|
-
writeRecord(PENDING_KEY, void 0);
|
|
1168
|
-
setTools(
|
|
1169
|
-
watchAuthorization(connection.tools, mod, () => {
|
|
1170
|
-
if (!current()) return;
|
|
1171
|
-
const url = providerRef.current?.authorizationUrl;
|
|
1172
|
-
if (url) setAuthorizationUrl(String(url));
|
|
1173
|
-
setStatus("needs-authorization");
|
|
1174
|
-
})
|
|
1175
|
-
);
|
|
1176
|
-
setCatalog(connection.catalog);
|
|
1177
|
-
setStatus("connected");
|
|
1178
|
-
} catch (err) {
|
|
1179
|
-
if (!current()) return;
|
|
1180
|
-
setError(err instanceof Error ? err.message : String(err));
|
|
1181
|
-
setStatus("error");
|
|
1182
|
-
}
|
|
1183
|
-
},
|
|
1184
|
-
[closeConnection]
|
|
1185
|
-
);
|
|
1186
|
-
const disconnect = useCallback6(async () => {
|
|
1187
|
-
epochRef.current++;
|
|
1188
|
-
await closeConnection();
|
|
1189
|
-
setTools(void 0);
|
|
1190
|
-
setCatalog([]);
|
|
1191
|
-
setAuthorizationUrl(void 0);
|
|
1192
|
-
setError(void 0);
|
|
1193
|
-
setStatus("idle");
|
|
1194
|
-
}, [closeConnection]);
|
|
1195
|
-
const authorize = useCallback6(() => {
|
|
1196
|
-
if (authorizationUrl && typeof globalThis.location !== "undefined") {
|
|
1197
|
-
globalThis.location.href = authorizationUrl;
|
|
1198
|
-
}
|
|
1199
|
-
}, [authorizationUrl]);
|
|
1200
|
-
const forgetAuthorization = useCallback6(async () => {
|
|
1201
|
-
const record = readRecord(OAUTH_KEY);
|
|
1202
|
-
writeRecord(PENDING_KEY, void 0);
|
|
1203
|
-
writeRecord(OAUTH_KEY, void 0);
|
|
1204
|
-
let provider = providerRef.current;
|
|
1205
|
-
if (!provider && record) {
|
|
1206
|
-
const mod = await loadMcp().catch(() => void 0);
|
|
1207
|
-
if (mod?.BrowserOAuthProvider) {
|
|
1208
|
-
provider = new mod.BrowserOAuthProvider({
|
|
1209
|
-
serverUrl: record.url,
|
|
1210
|
-
redirectUrl: record.redirectUrl,
|
|
1211
|
-
clientName: record.clientName,
|
|
1212
|
-
scope: record.scope
|
|
1213
|
-
});
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
await provider?.reset().catch(() => {
|
|
1217
|
-
});
|
|
1218
|
-
providerRef.current = void 0;
|
|
1219
|
-
await disconnect();
|
|
1220
|
-
}, [disconnect]);
|
|
1221
|
-
useEffect5(() => {
|
|
1222
|
-
const callback = readCallbackParams();
|
|
1223
|
-
if (!callback) {
|
|
1224
|
-
setCompletingAuthorization(false);
|
|
1225
|
-
return;
|
|
1226
|
-
}
|
|
1227
|
-
const pending = readRecord(PENDING_KEY);
|
|
1228
|
-
const fresh = pending !== void 0 && (pending.startedAt === void 0 || Date.now() - pending.startedAt < PENDING_TTL_MS);
|
|
1229
|
-
if (!pending || !fresh) {
|
|
1230
|
-
if (pending) writeRecord(PENDING_KEY, void 0);
|
|
1231
|
-
setCompletingAuthorization(false);
|
|
1232
|
-
return;
|
|
1233
|
-
}
|
|
1234
|
-
if (!claimOAuthCallback(callback)) {
|
|
1235
|
-
setCompletingAuthorization(false);
|
|
1236
|
-
return;
|
|
1237
|
-
}
|
|
1238
|
-
if (typeof globalThis.history !== "undefined") {
|
|
1239
|
-
globalThis.history.replaceState(null, "", stripOAuthParams(globalThis.location.href));
|
|
1240
|
-
}
|
|
1241
|
-
void (async () => {
|
|
1242
|
-
try {
|
|
1243
|
-
const mod = await loadMcp();
|
|
1244
|
-
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
1245
|
-
if (!mod.BrowserOAuthProvider || !mod.finishMcpOAuth) throw new Error(OAUTH_UNSUPPORTED);
|
|
1246
|
-
const provider = new mod.BrowserOAuthProvider({
|
|
1247
|
-
serverUrl: pending.url,
|
|
1248
|
-
redirectUrl: pending.redirectUrl ?? defaultRedirectUrl(),
|
|
1249
|
-
clientName: pending.clientName,
|
|
1250
|
-
scope: pending.scope
|
|
1251
|
-
});
|
|
1252
|
-
providerRef.current = provider;
|
|
1253
|
-
await mod.finishMcpOAuth(provider, callback);
|
|
1254
|
-
writeRecord(PENDING_KEY, void 0);
|
|
1255
|
-
setCompletingAuthorization(false);
|
|
1256
|
-
await connect({ url: pending.url, name: pending.name, oauth: true });
|
|
1257
|
-
} catch (err) {
|
|
1258
|
-
writeRecord(PENDING_KEY, void 0);
|
|
1259
|
-
setError(err instanceof Error ? err.message : String(err));
|
|
1260
|
-
setStatus("error");
|
|
1261
|
-
} finally {
|
|
1262
|
-
setCompletingAuthorization(false);
|
|
1263
|
-
}
|
|
1264
|
-
})();
|
|
1265
|
-
}, [connect]);
|
|
1266
|
-
useEffect5(
|
|
1267
|
-
() => () => {
|
|
1268
|
-
epochRef.current++;
|
|
1269
|
-
void closeConnection();
|
|
1270
|
-
},
|
|
1271
|
-
[closeConnection]
|
|
1272
|
-
);
|
|
1273
|
-
return {
|
|
1274
|
-
status,
|
|
1275
|
-
tools,
|
|
1276
|
-
catalog,
|
|
1277
|
-
error,
|
|
1278
|
-
authorizationUrl,
|
|
1279
|
-
oauthSupported,
|
|
1280
|
-
completingAuthorization,
|
|
1281
|
-
connect,
|
|
1282
|
-
disconnect,
|
|
1283
|
-
authorize,
|
|
1284
|
-
forgetAuthorization,
|
|
1285
|
-
checkOAuthSupport
|
|
1286
|
-
};
|
|
1287
|
-
};
|
|
1288
|
-
|
|
1289
|
-
// src/hooks/use-mcp-servers.ts
|
|
1290
|
-
import { useCallback as useCallback7, useEffect as useEffect6, useMemo as useMemo3, useRef as useRef5, useState as useState7 } from "react";
|
|
1291
|
-
var DEFAULT_KEY = "agent-web-react:mcp-servers";
|
|
1292
|
-
var sanitize = (s) => s.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 32) || "mcp";
|
|
1293
|
-
var view = (s, patch = {}) => ({
|
|
1294
|
-
name: s.name,
|
|
1295
|
-
url: s.url,
|
|
1296
|
-
auth: s.auth,
|
|
1297
|
-
status: "idle",
|
|
1298
|
-
catalog: [],
|
|
1299
|
-
...patch
|
|
1300
|
-
});
|
|
1301
|
-
var useMcpServers = (options = {}) => {
|
|
1302
|
-
const storageKey = options.storageKey ?? DEFAULT_KEY;
|
|
1303
|
-
const pendingKey = `${storageKey}:pending`;
|
|
1304
|
-
const persist = options.persist !== false;
|
|
1305
|
-
const [servers, setServers] = useState7(
|
|
1306
|
-
() => persist ? (readRecord(storageKey) ?? []).map((s) => view(s)) : []
|
|
1307
|
-
);
|
|
1308
|
-
const [oauthSupported, setOauthSupported] = useState7(void 0);
|
|
1309
|
-
const [completingAuthorization, setCompleting] = useState7(
|
|
1310
|
-
() => readCallbackParams() !== void 0 && readRecord(pendingKey) !== void 0
|
|
1311
|
-
);
|
|
1312
|
-
const [toolsVersion, setToolsVersion] = useState7(0);
|
|
1313
|
-
const liveRef = useRef5(/* @__PURE__ */ new Map());
|
|
1314
|
-
const specsRef = useRef5(/* @__PURE__ */ new Map());
|
|
1315
|
-
if (specsRef.current.size === 0 && servers.length > 0) {
|
|
1316
|
-
for (const s of servers)
|
|
1317
|
-
specsRef.current.set(s.name, { name: s.name, url: s.url, auth: s.auth });
|
|
1318
|
-
}
|
|
1319
|
-
const optionsRef = useRef5(options);
|
|
1320
|
-
optionsRef.current = options;
|
|
1321
|
-
const unmountedRef = useRef5(false);
|
|
1322
|
-
const saveList = useCallback7(() => {
|
|
1323
|
-
if (!persist) return;
|
|
1324
|
-
writeRecord(storageKey, [...specsRef.current.values()]);
|
|
1325
|
-
}, [persist, storageKey]);
|
|
1326
|
-
const patch = useCallback7((name, p) => {
|
|
1327
|
-
if (unmountedRef.current) return;
|
|
1328
|
-
setServers((list) => list.map((s) => s.name === name ? { ...s, ...p } : s));
|
|
1329
|
-
}, []);
|
|
1330
|
-
const live = (name) => {
|
|
1331
|
-
let l = liveRef.current.get(name);
|
|
1332
|
-
if (!l) {
|
|
1333
|
-
l = { epoch: 0 };
|
|
1334
|
-
liveRef.current.set(name, l);
|
|
1335
|
-
}
|
|
1336
|
-
return l;
|
|
1337
|
-
};
|
|
1338
|
-
const closeServer = useCallback7(async (name) => {
|
|
1339
|
-
const l = liveRef.current.get(name);
|
|
1340
|
-
if (!l) return;
|
|
1341
|
-
l.epoch += 1;
|
|
1342
|
-
const open = l.connection;
|
|
1343
|
-
l.connection = void 0;
|
|
1344
|
-
if (l.tools) {
|
|
1345
|
-
l.tools = void 0;
|
|
1346
|
-
setToolsVersion((v) => v + 1);
|
|
1347
|
-
}
|
|
1348
|
-
if (open) await open.close().catch(() => {
|
|
1349
|
-
});
|
|
1350
|
-
}, []);
|
|
1351
|
-
const providerFor = (mod, spec) => {
|
|
1352
|
-
if (!mod.BrowserOAuthProvider) throw new Error(OAUTH_UNSUPPORTED);
|
|
1353
|
-
return new mod.BrowserOAuthProvider({
|
|
1354
|
-
serverUrl: spec.url,
|
|
1355
|
-
redirectUrl: spec.oauth?.redirectUrl ?? defaultRedirectUrl(),
|
|
1356
|
-
clientName: spec.oauth?.clientName ?? optionsRef.current.clientName,
|
|
1357
|
-
scope: spec.oauth?.scope
|
|
1358
|
-
});
|
|
1359
|
-
};
|
|
1360
|
-
const connectServer = useCallback7(
|
|
1361
|
-
async (name) => {
|
|
1362
|
-
const spec = specsRef.current.get(name);
|
|
1363
|
-
if (!spec) return;
|
|
1364
|
-
await closeServer(name);
|
|
1365
|
-
const l = live(name);
|
|
1366
|
-
const epoch = ++l.epoch;
|
|
1367
|
-
const current = () => !unmountedRef.current && l.epoch === epoch;
|
|
1368
|
-
if (spec.auth === "bearer" && !l.token) {
|
|
1369
|
-
patch(name, { status: "idle", error: "Enter the bearer token to connect." });
|
|
1370
|
-
return;
|
|
1371
|
-
}
|
|
1372
|
-
patch(name, {
|
|
1373
|
-
status: "connecting",
|
|
1374
|
-
error: void 0,
|
|
1375
|
-
authorizationUrl: void 0,
|
|
1376
|
-
catalog: []
|
|
1377
|
-
});
|
|
1378
|
-
try {
|
|
1379
|
-
const mod = await loadMcp();
|
|
1380
|
-
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
1381
|
-
let provider;
|
|
1382
|
-
if (spec.auth === "oauth") {
|
|
1383
|
-
provider = providerFor(mod, spec);
|
|
1384
|
-
l.provider = provider;
|
|
1385
|
-
}
|
|
1386
|
-
const connection = await mod.connectMcpHttp(
|
|
1387
|
-
{
|
|
1388
|
-
[name]: {
|
|
1389
|
-
url: spec.url,
|
|
1390
|
-
headers: spec.auth === "bearer" && l.token ? { Authorization: `Bearer ${l.token}` } : void 0,
|
|
1391
|
-
authProvider: provider,
|
|
1392
|
-
connectTimeoutMs: optionsRef.current.connectTimeoutMs
|
|
1393
|
-
}
|
|
1394
|
-
},
|
|
1395
|
-
{
|
|
1396
|
-
clientName: optionsRef.current.clientName,
|
|
1397
|
-
onLog: optionsRef.current.onLog,
|
|
1398
|
-
connectTimeoutMs: optionsRef.current.connectTimeoutMs
|
|
1399
|
-
}
|
|
1400
|
-
);
|
|
1401
|
-
if (!current()) {
|
|
1402
|
-
await connection.close().catch(() => {
|
|
1403
|
-
});
|
|
1404
|
-
return;
|
|
1405
|
-
}
|
|
1406
|
-
const outcome = describeMcpResult(connection.results[0]);
|
|
1407
|
-
if (outcome.status !== "connected") {
|
|
1408
|
-
await connection.close().catch(() => {
|
|
1409
|
-
});
|
|
1410
|
-
if (outcome.status === "needs-authorization") {
|
|
1411
|
-
const url = provider?.authorizationUrl;
|
|
1412
|
-
patch(name, {
|
|
1413
|
-
status: "needs-authorization",
|
|
1414
|
-
authorizationUrl: url ? String(url) : void 0
|
|
1415
|
-
});
|
|
1416
|
-
return;
|
|
1417
|
-
}
|
|
1418
|
-
throw new Error(outcome.error);
|
|
1419
|
-
}
|
|
1420
|
-
l.connection = connection;
|
|
1421
|
-
l.tools = watchAuthorization(connection.tools, mod, () => {
|
|
1422
|
-
if (!current()) return;
|
|
1423
|
-
const url = l.provider?.authorizationUrl;
|
|
1424
|
-
patch(name, {
|
|
1425
|
-
status: "needs-authorization",
|
|
1426
|
-
authorizationUrl: url ? String(url) : void 0
|
|
1427
|
-
});
|
|
1428
|
-
});
|
|
1429
|
-
setToolsVersion((v) => v + 1);
|
|
1430
|
-
patch(name, { status: "connected", catalog: connection.catalog });
|
|
1431
|
-
} catch (err) {
|
|
1432
|
-
if (!current()) return;
|
|
1433
|
-
patch(name, { status: "error", error: err instanceof Error ? err.message : String(err) });
|
|
1434
|
-
}
|
|
1435
|
-
},
|
|
1436
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1437
|
-
[closeServer, patch, pendingKey]
|
|
1438
|
-
);
|
|
1439
|
-
const add = useCallback7(
|
|
1440
|
-
async (input) => {
|
|
1441
|
-
let name = sanitize(input.name);
|
|
1442
|
-
const taken = specsRef.current.get(name);
|
|
1443
|
-
if (taken && taken.url !== input.url) {
|
|
1444
|
-
let n = 2;
|
|
1445
|
-
while (specsRef.current.has(`${name}_${n}`)) n += 1;
|
|
1446
|
-
name = `${name}_${n}`;
|
|
1447
|
-
}
|
|
1448
|
-
const spec = {
|
|
1449
|
-
name,
|
|
1450
|
-
url: input.url,
|
|
1451
|
-
auth: input.auth ?? "none",
|
|
1452
|
-
...input.oauth ? { oauth: input.oauth } : {}
|
|
1453
|
-
};
|
|
1454
|
-
specsRef.current.set(name, spec);
|
|
1455
|
-
live(name).token = input.auth === "bearer" ? input.token : void 0;
|
|
1456
|
-
setServers(
|
|
1457
|
-
(list) => list.some((s) => s.name === name) ? list.map((s) => s.name === name ? view(spec) : s) : [...list, view(spec)]
|
|
1458
|
-
);
|
|
1459
|
-
saveList();
|
|
1460
|
-
await connectServer(name);
|
|
1461
|
-
return name;
|
|
1462
|
-
},
|
|
1463
|
-
[connectServer, saveList]
|
|
1464
|
-
);
|
|
1465
|
-
const remove = useCallback7(
|
|
1466
|
-
async (name) => {
|
|
1467
|
-
await closeServer(name);
|
|
1468
|
-
liveRef.current.delete(name);
|
|
1469
|
-
specsRef.current.delete(name);
|
|
1470
|
-
saveList();
|
|
1471
|
-
setServers((list) => list.filter((s) => s.name !== name));
|
|
1472
|
-
},
|
|
1473
|
-
[closeServer, saveList]
|
|
1474
|
-
);
|
|
1475
|
-
const reconnect = useCallback7(
|
|
1476
|
-
async (name, token) => {
|
|
1477
|
-
if (token !== void 0) live(name).token = token;
|
|
1478
|
-
await connectServer(name);
|
|
1479
|
-
},
|
|
1480
|
-
[connectServer]
|
|
1481
|
-
);
|
|
1482
|
-
const authorize = useCallback7(
|
|
1483
|
-
(name) => {
|
|
1484
|
-
const url = servers.find((s) => s.name === name)?.authorizationUrl;
|
|
1485
|
-
const spec = specsRef.current.get(name);
|
|
1486
|
-
if (!url || !spec || typeof globalThis.location === "undefined") return;
|
|
1487
|
-
const pending = {
|
|
1488
|
-
name,
|
|
1489
|
-
url: spec.url,
|
|
1490
|
-
redirectUrl: spec.oauth?.redirectUrl ?? defaultRedirectUrl(),
|
|
1491
|
-
scope: spec.oauth?.scope,
|
|
1492
|
-
clientName: spec.oauth?.clientName ?? optionsRef.current.clientName,
|
|
1493
|
-
startedAt: Date.now()
|
|
1494
|
-
};
|
|
1495
|
-
writeRecord(pendingKey, pending);
|
|
1496
|
-
globalThis.location.href = url;
|
|
1497
|
-
},
|
|
1498
|
-
[servers, pendingKey]
|
|
1499
|
-
);
|
|
1500
|
-
const forgetAuthorization = useCallback7(
|
|
1501
|
-
async (name) => {
|
|
1502
|
-
const spec = specsRef.current.get(name);
|
|
1503
|
-
const l = liveRef.current.get(name);
|
|
1504
|
-
let provider = l?.provider;
|
|
1505
|
-
if (!provider && spec?.auth === "oauth") {
|
|
1506
|
-
const mod = await loadMcp().catch(() => void 0);
|
|
1507
|
-
if (mod?.BrowserOAuthProvider) provider = providerFor(mod, spec);
|
|
1508
|
-
}
|
|
1509
|
-
await provider?.reset().catch(() => {
|
|
1510
|
-
});
|
|
1511
|
-
if (l) l.provider = void 0;
|
|
1512
|
-
const pending = readRecord(pendingKey);
|
|
1513
|
-
if (pending?.name === name) writeRecord(pendingKey, void 0);
|
|
1514
|
-
await closeServer(name);
|
|
1515
|
-
patch(name, { status: "idle", authorizationUrl: void 0, catalog: [], error: void 0 });
|
|
1516
|
-
},
|
|
1517
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1518
|
-
[closeServer, patch, pendingKey]
|
|
1519
|
-
);
|
|
1520
|
-
const checkOAuthSupport = useCallback7(async () => {
|
|
1521
|
-
try {
|
|
1522
|
-
const mod = await loadMcp();
|
|
1523
|
-
const supported = typeof mod.BrowserOAuthProvider === "function";
|
|
1524
|
-
setOauthSupported(supported);
|
|
1525
|
-
return supported;
|
|
1526
|
-
} catch {
|
|
1527
|
-
setOauthSupported(false);
|
|
1528
|
-
return false;
|
|
1529
|
-
}
|
|
1530
|
-
}, []);
|
|
1531
|
-
useEffect6(() => {
|
|
1532
|
-
unmountedRef.current = false;
|
|
1533
|
-
const callback = readCallbackParams();
|
|
1534
|
-
const pending = readRecord(pendingKey);
|
|
1535
|
-
const fresh = pending !== void 0 && Date.now() - pending.startedAt < PENDING_TTL_MS;
|
|
1536
|
-
const ours = Boolean(callback && pending && fresh && claimOAuthCallback(callback));
|
|
1537
|
-
if (callback && pending && !fresh) writeRecord(pendingKey, void 0);
|
|
1538
|
-
if (ours && typeof globalThis.history !== "undefined") {
|
|
1539
|
-
globalThis.history.replaceState(null, "", stripOAuthParams(globalThis.location.href));
|
|
1540
|
-
}
|
|
1541
|
-
const autoConnect = optionsRef.current.autoConnect !== false;
|
|
1542
|
-
void (async () => {
|
|
1543
|
-
if (ours && pending && callback) {
|
|
1544
|
-
try {
|
|
1545
|
-
const mod = await loadMcp();
|
|
1546
|
-
setOauthSupported(typeof mod.BrowserOAuthProvider === "function");
|
|
1547
|
-
if (!mod.finishMcpOAuth) throw new Error(OAUTH_UNSUPPORTED);
|
|
1548
|
-
const spec = specsRef.current.get(pending.name) ?? {
|
|
1549
|
-
name: pending.name,
|
|
1550
|
-
url: pending.url,
|
|
1551
|
-
auth: "oauth"
|
|
1552
|
-
};
|
|
1553
|
-
if (!specsRef.current.has(spec.name)) {
|
|
1554
|
-
specsRef.current.set(spec.name, spec);
|
|
1555
|
-
setServers((list) => [...list, view(spec)]);
|
|
1556
|
-
saveList();
|
|
1557
|
-
}
|
|
1558
|
-
const provider = providerFor(mod, {
|
|
1559
|
-
...spec,
|
|
1560
|
-
oauth: {
|
|
1561
|
-
redirectUrl: pending.redirectUrl,
|
|
1562
|
-
scope: pending.scope,
|
|
1563
|
-
clientName: pending.clientName
|
|
1564
|
-
}
|
|
1565
|
-
});
|
|
1566
|
-
live(spec.name).provider = provider;
|
|
1567
|
-
await mod.finishMcpOAuth(provider, callback);
|
|
1568
|
-
writeRecord(pendingKey, void 0);
|
|
1569
|
-
} catch (err) {
|
|
1570
|
-
writeRecord(pendingKey, void 0);
|
|
1571
|
-
patch(pending.name, {
|
|
1572
|
-
status: "error",
|
|
1573
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1574
|
-
});
|
|
1575
|
-
}
|
|
1576
|
-
}
|
|
1577
|
-
setCompleting(false);
|
|
1578
|
-
if (autoConnect || ours) {
|
|
1579
|
-
await Promise.all(
|
|
1580
|
-
[...specsRef.current.keys()].filter((name) => autoConnect || name === pending?.name).map((name) => connectServer(name))
|
|
1581
|
-
);
|
|
1582
|
-
}
|
|
1583
|
-
})();
|
|
1584
|
-
return () => {
|
|
1585
|
-
unmountedRef.current = true;
|
|
1586
|
-
for (const name of liveRef.current.keys()) void closeServer(name);
|
|
1587
|
-
};
|
|
1588
|
-
}, []);
|
|
1589
|
-
const tools = useMemo3(() => {
|
|
1590
|
-
const merged = {};
|
|
1591
|
-
for (const l of liveRef.current.values()) if (l.tools) Object.assign(merged, l.tools);
|
|
1592
|
-
return merged;
|
|
1593
|
-
}, [toolsVersion]);
|
|
1594
|
-
const catalog = useMemo3(
|
|
1595
|
-
() => servers.filter((s) => s.status === "connected").flatMap((s) => s.catalog),
|
|
1596
|
-
[servers]
|
|
1597
|
-
);
|
|
1598
|
-
return {
|
|
1599
|
-
servers,
|
|
1600
|
-
tools,
|
|
1601
|
-
catalog,
|
|
1602
|
-
connectedCount: servers.filter((s) => s.status === "connected").length,
|
|
1603
|
-
completingAuthorization,
|
|
1604
|
-
oauthSupported,
|
|
1605
|
-
checkOAuthSupport,
|
|
1606
|
-
add,
|
|
1607
|
-
remove,
|
|
1608
|
-
reconnect,
|
|
1609
|
-
authorize,
|
|
1610
|
-
forgetAuthorization
|
|
1611
|
-
};
|
|
1612
|
-
};
|
|
1613
|
-
|
|
1614
1031
|
// src/hooks/use-webllm-model.ts
|
|
1615
1032
|
import {
|
|
1616
1033
|
createWebLLMModel,
|
|
@@ -1621,21 +1038,21 @@ import {
|
|
|
1621
1038
|
} from "@dudko.dev/agent-web";
|
|
1622
1039
|
|
|
1623
1040
|
// src/hooks/use-local-model.ts
|
|
1624
|
-
import { useCallback as
|
|
1041
|
+
import { useCallback as useCallback6, useRef as useRef4, useState as useState6 } from "react";
|
|
1625
1042
|
var keyOf = (id, tokens) => tokens ? `${id}@${tokens}` : id;
|
|
1626
1043
|
var useLocalModel = (modelId, engine, options) => {
|
|
1627
|
-
const [loaded, setLoaded] =
|
|
1628
|
-
const [pending, setPending] =
|
|
1629
|
-
const [failure, setFailure] =
|
|
1630
|
-
const engineRef =
|
|
1044
|
+
const [loaded, setLoaded] = useState6(void 0);
|
|
1045
|
+
const [pending, setPending] = useState6();
|
|
1046
|
+
const [failure, setFailure] = useState6();
|
|
1047
|
+
const engineRef = useRef4(engine);
|
|
1631
1048
|
engineRef.current = engine;
|
|
1632
|
-
const loadedRef =
|
|
1633
|
-
const inflightRef =
|
|
1049
|
+
const loadedRef = useRef4(void 0);
|
|
1050
|
+
const inflightRef = useRef4(
|
|
1634
1051
|
void 0
|
|
1635
1052
|
);
|
|
1636
1053
|
const windowTokens = options?.contextWindowTokens;
|
|
1637
1054
|
const key = keyOf(modelId, windowTokens);
|
|
1638
|
-
const seqRef =
|
|
1055
|
+
const seqRef = useRef4(0);
|
|
1639
1056
|
const setHeld = (next) => {
|
|
1640
1057
|
loadedRef.current = next;
|
|
1641
1058
|
setLoaded(next);
|
|
@@ -1646,7 +1063,7 @@ var useLocalModel = (modelId, engine, options) => {
|
|
|
1646
1063
|
} catch {
|
|
1647
1064
|
}
|
|
1648
1065
|
};
|
|
1649
|
-
const load =
|
|
1066
|
+
const load = useCallback6(() => {
|
|
1650
1067
|
const id = modelId;
|
|
1651
1068
|
if (loadedRef.current?.key === key) return Promise.resolve(loadedRef.current.model);
|
|
1652
1069
|
if (inflightRef.current?.key === key) return inflightRef.current.promise;
|
|
@@ -1698,7 +1115,7 @@ var useLocalModel = (modelId, engine, options) => {
|
|
|
1698
1115
|
inflightRef.current = { key, promise };
|
|
1699
1116
|
return promise;
|
|
1700
1117
|
}, [modelId, key]);
|
|
1701
|
-
const unload =
|
|
1118
|
+
const unload = useCallback6(async () => {
|
|
1702
1119
|
seqRef.current++;
|
|
1703
1120
|
inflightRef.current = void 0;
|
|
1704
1121
|
setPending(void 0);
|
|
@@ -1760,13 +1177,20 @@ var createWebLLMEngine = (options) => {
|
|
|
1760
1177
|
};
|
|
1761
1178
|
|
|
1762
1179
|
// src/labels.tsx
|
|
1763
|
-
import { createContext, useContext, useMemo as
|
|
1180
|
+
import { createContext, useContext, useMemo as useMemo3 } from "react";
|
|
1764
1181
|
|
|
1765
1182
|
// src/composer.ts
|
|
1766
1183
|
var parseSlash = (text) => {
|
|
1767
1184
|
const m = /^\/([\w-]*)(?:\s+([\s\S]*))?$/.exec(text.trimStart());
|
|
1768
1185
|
return m ? { name: m[1].toLowerCase(), args: (m[2] ?? "").trim() } : void 0;
|
|
1769
1186
|
};
|
|
1187
|
+
var steerIntent = (input) => {
|
|
1188
|
+
if (!input.busy) return "idle";
|
|
1189
|
+
if (!input.steering) return "ignore";
|
|
1190
|
+
if (!input.raw && !input.hasAttachments) return "ignore";
|
|
1191
|
+
if (parseSlash(input.raw)) return "ignore";
|
|
1192
|
+
return input.hasAttachments ? "attachments" : "steer";
|
|
1193
|
+
};
|
|
1770
1194
|
var filterCommands = (commands, query) => {
|
|
1771
1195
|
const q = query.toLowerCase();
|
|
1772
1196
|
if (!q) return commands;
|
|
@@ -1964,6 +1388,20 @@ var defaultLabels = {
|
|
|
1964
1388
|
consentWaiting: "waiting for your consent",
|
|
1965
1389
|
consentAllowed: "allowed",
|
|
1966
1390
|
consentDenied: (reason) => `denied${reason ? ` \u2014 ${reason}` : ""}`,
|
|
1391
|
+
placeholderBusy: "Tell the agent something while it works\u2026",
|
|
1392
|
+
steer: "Send to the agent",
|
|
1393
|
+
noAttachmentsWhileBusy: "Files can\u2019t be added while the agent is working \u2014 send them with your next message.",
|
|
1394
|
+
queuedWaiting: "waiting for the agent\u2019s next step",
|
|
1395
|
+
noteAt: {
|
|
1396
|
+
round: "you said, between tool calls",
|
|
1397
|
+
step: "you said, after this step",
|
|
1398
|
+
final: "you said, before the answer"
|
|
1399
|
+
},
|
|
1400
|
+
undelivered: (n) => `Not delivered (${plural(n, "message", "messages")})`,
|
|
1401
|
+
sendUndelivered: "Send now",
|
|
1402
|
+
discardUndelivered: "Discard",
|
|
1403
|
+
stopSubagent: "Stop this subagent",
|
|
1404
|
+
subagentStopped: "stopped",
|
|
1967
1405
|
failed: "failed",
|
|
1968
1406
|
toolCostTitle: "Estimated tokens of the arguments and of the result fed back to the model (chars / 4), and the call's duration"
|
|
1969
1407
|
};
|
|
@@ -1972,7 +1410,8 @@ var NESTED = [
|
|
|
1972
1410
|
"modes",
|
|
1973
1411
|
"commandGroups",
|
|
1974
1412
|
"commandDescriptions",
|
|
1975
|
-
"thinkingLevels"
|
|
1413
|
+
"thinkingLevels",
|
|
1414
|
+
"noteAt"
|
|
1976
1415
|
];
|
|
1977
1416
|
var mergeLabels = (base2, over) => {
|
|
1978
1417
|
if (!over) return base2;
|
|
@@ -1985,12 +1424,12 @@ var mergeLabels = (base2, over) => {
|
|
|
1985
1424
|
var LabelsContext = createContext(defaultLabels);
|
|
1986
1425
|
var AgentLabelsProvider = ({ labels, children }) => {
|
|
1987
1426
|
const parent = useContext(LabelsContext);
|
|
1988
|
-
const value =
|
|
1427
|
+
const value = useMemo3(() => mergeLabels(parent, labels), [parent, labels]);
|
|
1989
1428
|
return /* @__PURE__ */ jsx(LabelsContext.Provider, { value, children });
|
|
1990
1429
|
};
|
|
1991
1430
|
var useLabels = (over) => {
|
|
1992
1431
|
const ctx = useContext(LabelsContext);
|
|
1993
|
-
return
|
|
1432
|
+
return useMemo3(() => mergeLabels(ctx, over), [ctx, over]);
|
|
1994
1433
|
};
|
|
1995
1434
|
|
|
1996
1435
|
// src/image-resize.ts
|
|
@@ -2058,10 +1497,10 @@ var useAgentContext = () => {
|
|
|
2058
1497
|
var useOptionalAgentContext = () => useContext2(AgentContext);
|
|
2059
1498
|
|
|
2060
1499
|
// src/components/AgentChat.tsx
|
|
2061
|
-
import { useEffect as
|
|
1500
|
+
import { useEffect as useEffect7, useRef as useRef7, useState as useState9 } from "react";
|
|
2062
1501
|
|
|
2063
1502
|
// src/components/AgentComposer.tsx
|
|
2064
|
-
import { useEffect as
|
|
1503
|
+
import { useEffect as useEffect5, useLayoutEffect, useMemo as useMemo4, useRef as useRef5, useState as useState7 } from "react";
|
|
2065
1504
|
|
|
2066
1505
|
// src/components/icons.tsx
|
|
2067
1506
|
import { jsx as jsx3, jsxs } from "react/jsx-runtime";
|
|
@@ -2140,9 +1579,9 @@ var thinkingDefaults = (l) => [
|
|
|
2140
1579
|
var MODES = ["autopilot", "ask-writes", "ask-all", "read-only"];
|
|
2141
1580
|
var ModeIcon = ({ mode }) => mode === "autopilot" ? /* @__PURE__ */ jsx4(BoltIcon, { size: 14 }) : mode === "read-only" ? /* @__PURE__ */ jsx4(EyeIcon, { size: 14 }) : /* @__PURE__ */ jsx4(ShieldIcon, { size: 14 });
|
|
2142
1581
|
var attachSeq = 0;
|
|
2143
|
-
var useIsoLayoutEffect = typeof window === "undefined" ?
|
|
1582
|
+
var useIsoLayoutEffect = typeof window === "undefined" ? useEffect5 : useLayoutEffect;
|
|
2144
1583
|
var useMenuPlacement = (open, anchor, align) => {
|
|
2145
|
-
const [style, setStyle] =
|
|
1584
|
+
const [style, setStyle] = useState7();
|
|
2146
1585
|
useIsoLayoutEffect(() => {
|
|
2147
1586
|
if (!open) return;
|
|
2148
1587
|
const place = () => {
|
|
@@ -2207,6 +1646,8 @@ var AgentComposer = ({
|
|
|
2207
1646
|
showAgents = true,
|
|
2208
1647
|
showApprovalMode = true,
|
|
2209
1648
|
showNotices = true,
|
|
1649
|
+
steering = true,
|
|
1650
|
+
showQueued = true,
|
|
2210
1651
|
compactWidth = 460,
|
|
2211
1652
|
toolbarExtra,
|
|
2212
1653
|
toolbarEnd,
|
|
@@ -2217,19 +1658,19 @@ var AgentComposer = ({
|
|
|
2217
1658
|
const L = useLabels(labelsOverride);
|
|
2218
1659
|
const MODE_CHIP = L.modes;
|
|
2219
1660
|
const DEFAULT_THINKING = thinkingDefaults(L);
|
|
2220
|
-
const [text, setText] =
|
|
2221
|
-
const [pending, setPending] =
|
|
2222
|
-
const [menu, setMenu] =
|
|
2223
|
-
const [active, setActive] =
|
|
2224
|
-
const [dismissed, setDismissed] =
|
|
2225
|
-
const [attachError, setAttachError] =
|
|
2226
|
-
const [dragging, setDragging] =
|
|
2227
|
-
const rootRef =
|
|
2228
|
-
const modelRef =
|
|
2229
|
-
const modeRef =
|
|
2230
|
-
const [dropRect, setDropRect] =
|
|
2231
|
-
const [compactBar, setCompactBar] =
|
|
2232
|
-
|
|
1661
|
+
const [text, setText] = useState7("");
|
|
1662
|
+
const [pending, setPending] = useState7([]);
|
|
1663
|
+
const [menu, setMenu] = useState7(void 0);
|
|
1664
|
+
const [active, setActive] = useState7(0);
|
|
1665
|
+
const [dismissed, setDismissed] = useState7(() => /* @__PURE__ */ new Set());
|
|
1666
|
+
const [attachError, setAttachError] = useState7();
|
|
1667
|
+
const [dragging, setDragging] = useState7(false);
|
|
1668
|
+
const rootRef = useRef5(null);
|
|
1669
|
+
const modelRef = useRef5(null);
|
|
1670
|
+
const modeRef = useRef5(null);
|
|
1671
|
+
const [dropRect, setDropRect] = useState7();
|
|
1672
|
+
const [compactBar, setCompactBar] = useState7(false);
|
|
1673
|
+
useEffect5(() => {
|
|
2233
1674
|
const el = rootRef.current;
|
|
2234
1675
|
if (!el || typeof ResizeObserver === "undefined") return;
|
|
2235
1676
|
const ro = new ResizeObserver(
|
|
@@ -2238,8 +1679,8 @@ var AgentComposer = ({
|
|
|
2238
1679
|
ro.observe(el);
|
|
2239
1680
|
return () => ro.disconnect();
|
|
2240
1681
|
}, [compactWidth]);
|
|
2241
|
-
const fileRef =
|
|
2242
|
-
const inputRef =
|
|
1682
|
+
const fileRef = useRef5(null);
|
|
1683
|
+
const inputRef = useRef5(null);
|
|
2243
1684
|
const busy = agent.isRunning;
|
|
2244
1685
|
const ready = agent.isReady && !disabled;
|
|
2245
1686
|
const caps = agent.agent?.capabilities;
|
|
@@ -2248,10 +1689,10 @@ var AgentComposer = ({
|
|
|
2248
1689
|
onFinal: (heard) => setText((t) => t.trim() ? `${t.trimEnd()} ${heard}` : heard)
|
|
2249
1690
|
});
|
|
2250
1691
|
const showMic = speech !== false && stt.supported;
|
|
2251
|
-
const [startedAt, setStartedAt] =
|
|
2252
|
-
const [now, setNow] =
|
|
2253
|
-
const [lastElapsed, setLastElapsed] =
|
|
2254
|
-
|
|
1692
|
+
const [startedAt, setStartedAt] = useState7();
|
|
1693
|
+
const [now, setNow] = useState7(() => Date.now());
|
|
1694
|
+
const [lastElapsed, setLastElapsed] = useState7();
|
|
1695
|
+
useEffect5(() => {
|
|
2255
1696
|
if (!busy) {
|
|
2256
1697
|
if (startedAt !== void 0) setLastElapsed(Date.now() - startedAt);
|
|
2257
1698
|
setStartedAt(void 0);
|
|
@@ -2264,7 +1705,7 @@ var AgentComposer = ({
|
|
|
2264
1705
|
return () => clearInterval(id);
|
|
2265
1706
|
}, [busy]);
|
|
2266
1707
|
const elapsed = startedAt !== void 0 ? now - startedAt : lastElapsed;
|
|
2267
|
-
const allCommands =
|
|
1708
|
+
const allCommands = useMemo4(() => {
|
|
2268
1709
|
const out = [];
|
|
2269
1710
|
if (builtinCommands) {
|
|
2270
1711
|
out.push(
|
|
@@ -2344,8 +1785,8 @@ var AgentComposer = ({
|
|
|
2344
1785
|
const commandsStyle = useMenuPlacement(paletteOpen, rootRef, "stretch");
|
|
2345
1786
|
const modelStyle = useMenuPlacement(menu === "model", modelRef, "left");
|
|
2346
1787
|
const modeStyle = useMenuPlacement(menu === "mode", modeRef, "right");
|
|
2347
|
-
|
|
2348
|
-
|
|
1788
|
+
useEffect5(() => setActive(0), [text, menu]);
|
|
1789
|
+
useEffect5(() => {
|
|
2349
1790
|
if (!menu) return;
|
|
2350
1791
|
const onDown = (e) => {
|
|
2351
1792
|
if (!rootRef.current?.contains(e.target)) setMenu(void 0);
|
|
@@ -2438,9 +1879,9 @@ var AgentComposer = ({
|
|
|
2438
1879
|
e.preventDefault();
|
|
2439
1880
|
void addFiles(pasted);
|
|
2440
1881
|
};
|
|
2441
|
-
const addFilesRef =
|
|
1882
|
+
const addFilesRef = useRef5(addFiles);
|
|
2442
1883
|
addFilesRef.current = addFiles;
|
|
2443
|
-
|
|
1884
|
+
useEffect5(() => {
|
|
2444
1885
|
if (attachments === false) return;
|
|
2445
1886
|
const target = dropZone === "window" ? globalThis.window : dropZone === "self" ? rootRef.current : dropZone.current;
|
|
2446
1887
|
if (!target) return;
|
|
@@ -2484,7 +1925,17 @@ var AgentComposer = ({
|
|
|
2484
1925
|
}, [attachments, dropZone]);
|
|
2485
1926
|
const submit = () => {
|
|
2486
1927
|
const raw = text.trim();
|
|
2487
|
-
|
|
1928
|
+
const intent = steerIntent({ raw, busy, steering, hasAttachments: pending.length > 0 });
|
|
1929
|
+
if (intent === "steer") {
|
|
1930
|
+
if (agent.send(raw)) setText("");
|
|
1931
|
+
return;
|
|
1932
|
+
}
|
|
1933
|
+
if (intent === "attachments") {
|
|
1934
|
+
setAttachError(L.noAttachmentsWhileBusy);
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
if (intent === "ignore") return;
|
|
1938
|
+
if (!ready) return;
|
|
2488
1939
|
const parsed = parseSlash(raw);
|
|
2489
1940
|
if (parsed) {
|
|
2490
1941
|
const cmd = allCommands.find((c) => c.name === parsed.name);
|
|
@@ -2582,6 +2033,24 @@ var AgentComposer = ({
|
|
|
2582
2033
|
const thinkingOptions = thinking?.options ?? DEFAULT_THINKING;
|
|
2583
2034
|
const thinkingLabel = thinking ? thinkingOptions.find((o) => o.id === thinking.value)?.label ?? thinking.value : void 0;
|
|
2584
2035
|
const canSend = ready && (text.trim().length > 0 || pending.length > 0);
|
|
2036
|
+
const canSteer = busy && steering && text.trim().length > 0;
|
|
2037
|
+
const queuedStrip = showQueued && (agent.queued.length > 0 || agent.undelivered.length > 0) ? /* @__PURE__ */ jsxs2("div", { className: "awr-queued", children: [
|
|
2038
|
+
agent.queued.map((q) => /* @__PURE__ */ jsxs2("div", { className: "awr-queued__item", role: "status", children: [
|
|
2039
|
+
/* @__PURE__ */ jsx4(ClockIcon, { size: 13, className: "awr-queued__icon" }),
|
|
2040
|
+
/* @__PURE__ */ jsx4("span", { className: "awr-queued__text", title: q.text, children: q.text }),
|
|
2041
|
+
/* @__PURE__ */ jsx4("span", { className: "awr-queued__state", children: L.queuedWaiting })
|
|
2042
|
+
] }, q.id)),
|
|
2043
|
+
agent.undelivered.length > 0 && /* @__PURE__ */ jsxs2("div", { className: "awr-queued__item awr-queued__item--undelivered", role: "status", children: [
|
|
2044
|
+
/* @__PURE__ */ jsx4(AlertIcon, { size: 13, className: "awr-queued__icon" }),
|
|
2045
|
+
/* @__PURE__ */ jsxs2("span", { className: "awr-queued__text", title: agent.undelivered.join("\n\n"), children: [
|
|
2046
|
+
L.undelivered(agent.undelivered.length),
|
|
2047
|
+
": ",
|
|
2048
|
+
agent.undelivered.join(" \xB7 ")
|
|
2049
|
+
] }),
|
|
2050
|
+
/* @__PURE__ */ jsx4("button", { type: "button", className: "awr-linkbtn", onClick: agent.sendUndelivered, children: L.sendUndelivered }),
|
|
2051
|
+
/* @__PURE__ */ jsx4("button", { type: "button", className: "awr-linkbtn", onClick: agent.discardUndelivered, children: L.discardUndelivered })
|
|
2052
|
+
] })
|
|
2053
|
+
] }) : null;
|
|
2585
2054
|
return /* @__PURE__ */ jsxs2(
|
|
2586
2055
|
"div",
|
|
2587
2056
|
{
|
|
@@ -2594,6 +2063,7 @@ var AgentComposer = ({
|
|
|
2594
2063
|
ref: rootRef,
|
|
2595
2064
|
children: [
|
|
2596
2065
|
dragging && /* @__PURE__ */ jsx4("div", { className: "awr-dropnote", style: dropRect, "aria-hidden": "true", children: L.dropHint }),
|
|
2066
|
+
queuedStrip,
|
|
2597
2067
|
notices.length > 0 && /* @__PURE__ */ jsx4("div", { className: "awr-notices", children: notices.map((n) => /* @__PURE__ */ jsxs2("div", { className: `awr-notice awr-notice--${n.tone}`, role: "status", children: [
|
|
2598
2068
|
/* @__PURE__ */ jsx4("span", { children: n.text }),
|
|
2599
2069
|
/* @__PURE__ */ jsx4(
|
|
@@ -2666,7 +2136,7 @@ var AgentComposer = ({
|
|
|
2666
2136
|
onChange: (e) => setText(e.target.value),
|
|
2667
2137
|
onKeyDown,
|
|
2668
2138
|
onPaste,
|
|
2669
|
-
placeholder: stt.listening ? L.listening : placeholder ?? L.placeholder,
|
|
2139
|
+
placeholder: stt.listening ? L.listening : busy && steering ? L.placeholderBusy : placeholder ?? L.placeholder,
|
|
2670
2140
|
disabled,
|
|
2671
2141
|
rows: 2,
|
|
2672
2142
|
"aria-label": L.message
|
|
@@ -2843,6 +2313,7 @@ var AgentComposer = ({
|
|
|
2843
2313
|
))
|
|
2844
2314
|
] })
|
|
2845
2315
|
] }),
|
|
2316
|
+
canSteer && /* @__PURE__ */ jsx4("button", { type: "button", className: "awr-sendbtn", onClick: submit, "aria-label": L.steer, children: /* @__PURE__ */ jsx4(SendIcon, { size: 16 }) }),
|
|
2846
2317
|
busy ? /* @__PURE__ */ jsx4(
|
|
2847
2318
|
"button",
|
|
2848
2319
|
{
|
|
@@ -3064,7 +2535,7 @@ var ContextMeter = ({
|
|
|
3064
2535
|
};
|
|
3065
2536
|
|
|
3066
2537
|
// src/components/FilesPanel.tsx
|
|
3067
|
-
import { Fragment as Fragment3, useEffect as
|
|
2538
|
+
import { Fragment as Fragment3, useEffect as useEffect6, useRef as useRef6, useState as useState8 } from "react";
|
|
3068
2539
|
import { Fragment as Fragment4, jsx as jsx7, jsxs as jsxs5 } from "react/jsx-runtime";
|
|
3069
2540
|
var PREVIEW_CHARS = 2e4;
|
|
3070
2541
|
var FilesPanel = ({
|
|
@@ -3081,9 +2552,9 @@ var FilesPanel = ({
|
|
|
3081
2552
|
}) => {
|
|
3082
2553
|
const L = useLabels(labels);
|
|
3083
2554
|
const fs = useVirtualFiles(vfs, prefix);
|
|
3084
|
-
const [preview, setPreview] =
|
|
3085
|
-
const inputRef =
|
|
3086
|
-
|
|
2555
|
+
const [preview, setPreview] = useState8();
|
|
2556
|
+
const inputRef = useRef6(null);
|
|
2557
|
+
useEffect6(() => {
|
|
3087
2558
|
if (preview && !fs.files.some((f) => f.path === preview.path)) setPreview(void 0);
|
|
3088
2559
|
}, [fs.files, preview]);
|
|
3089
2560
|
const open = async (f) => {
|
|
@@ -3217,6 +2688,7 @@ var ToolCallRow = ({ call, showUsage = true, labels }) => {
|
|
|
3217
2688
|
};
|
|
3218
2689
|
var SubagentRow = ({
|
|
3219
2690
|
sub,
|
|
2691
|
+
onStop,
|
|
3220
2692
|
labels
|
|
3221
2693
|
}) => {
|
|
3222
2694
|
const L = useLabels(labels);
|
|
@@ -3224,10 +2696,33 @@ var SubagentRow = ({
|
|
|
3224
2696
|
/* @__PURE__ */ jsxs6("div", { className: "awr-subagent__head", children: [
|
|
3225
2697
|
/* @__PURE__ */ jsx8(BotIcon, { size: 13, className: "awr-subagent__icon" }),
|
|
3226
2698
|
/* @__PURE__ */ jsx8("span", { className: "awr-subagent__name", children: sub.name }),
|
|
2699
|
+
sub.profile && /* @__PURE__ */ jsx8("span", { className: "awr-subagent__profile", children: sub.profile }),
|
|
3227
2700
|
/* @__PURE__ */ jsx8("span", { className: "awr-subagent__task", title: sub.task, children: previewValue(sub.task, 90) }),
|
|
3228
|
-
sub.status === "running" && /* @__PURE__ */ jsx8(SpinnerIcon, { size: 12, className: "awr-tool__spin" })
|
|
2701
|
+
sub.status === "running" && /* @__PURE__ */ jsx8(SpinnerIcon, { size: 12, className: "awr-tool__spin" }),
|
|
2702
|
+
sub.status === "running" && onStop && /* @__PURE__ */ jsx8(
|
|
2703
|
+
"button",
|
|
2704
|
+
{
|
|
2705
|
+
type: "button",
|
|
2706
|
+
className: "awr-iconbtn awr-subagent__stop",
|
|
2707
|
+
"aria-label": L.stopSubagent,
|
|
2708
|
+
title: L.stopSubagent,
|
|
2709
|
+
onClick: () => onStop(sub.id),
|
|
2710
|
+
children: /* @__PURE__ */ jsx8(StopIcon, { size: 12 })
|
|
2711
|
+
}
|
|
2712
|
+
)
|
|
3229
2713
|
] }),
|
|
3230
|
-
/* @__PURE__ */ jsx8("div", { className: "awr-subagent__activity", children: sub.status === "error" ? sub.error : sub.status === "done" ? previewValue(sub.text, 160) : `${sub.activity}${sub.toolCalls ? ` \xB7 ${L.toolCalls(sub.toolCalls)}` : ""}` })
|
|
2714
|
+
/* @__PURE__ */ jsx8("div", { className: "awr-subagent__activity", children: sub.status === "error" ? sub.error : sub.status === "stopped" ? L.subagentStopped : sub.status === "done" ? previewValue(sub.text, 160) : `${sub.activity}${sub.toolCalls ? ` \xB7 ${L.toolCalls(sub.toolCalls)}` : ""}` })
|
|
2715
|
+
] });
|
|
2716
|
+
};
|
|
2717
|
+
var UserNoteRow = ({
|
|
2718
|
+
note,
|
|
2719
|
+
labels
|
|
2720
|
+
}) => {
|
|
2721
|
+
const L = useLabels(labels);
|
|
2722
|
+
return /* @__PURE__ */ jsxs6("li", { className: `awr-note awr-note--${note.at}`, children: [
|
|
2723
|
+
/* @__PURE__ */ jsx8(SendIcon, { size: 12, className: "awr-note__icon" }),
|
|
2724
|
+
/* @__PURE__ */ jsx8("span", { className: "awr-note__where", children: L.noteAt[note.at] }),
|
|
2725
|
+
/* @__PURE__ */ jsx8("q", { className: "awr-note__text", children: note.text })
|
|
3231
2726
|
] });
|
|
3232
2727
|
};
|
|
3233
2728
|
var decisionLabel = (a, L) => a.status === "pending" ? L.consentWaiting : a.status === "approved" ? L.consentAllowed : L.consentDenied(a.reason);
|
|
@@ -3235,6 +2730,8 @@ var StepItem = ({
|
|
|
3235
2730
|
step,
|
|
3236
2731
|
subagents,
|
|
3237
2732
|
approvals,
|
|
2733
|
+
notes,
|
|
2734
|
+
onStopSubagent,
|
|
3238
2735
|
showToolCalls,
|
|
3239
2736
|
showUsage,
|
|
3240
2737
|
showThoughts,
|
|
@@ -3268,13 +2765,17 @@ var StepItem = ({
|
|
|
3268
2765
|
/* @__PURE__ */ jsx8("code", { className: "awr-tool__name", children: a.name }),
|
|
3269
2766
|
/* @__PURE__ */ jsx8("span", { className: "awr-tool__args", children: decisionLabel(a, L) })
|
|
3270
2767
|
] }, a.id)) }),
|
|
3271
|
-
subagents.length > 0 && /* @__PURE__ */ jsx8("ul", { className: "awr-subagents", children: subagents.map((sub) => /* @__PURE__ */ jsx8(SubagentRow, { sub }, sub.id)) }),
|
|
2768
|
+
subagents.length > 0 && /* @__PURE__ */ jsx8("ul", { className: "awr-subagents", children: subagents.map((sub) => /* @__PURE__ */ jsx8(SubagentRow, { sub, onStop: onStopSubagent }, sub.id)) }),
|
|
2769
|
+
notes.length > 0 && /* @__PURE__ */ jsx8("ul", { className: "awr-notes", children: notes.map((n) => /* @__PURE__ */ jsx8(UserNoteRow, { note: n }, n.id)) }),
|
|
3272
2770
|
step.blocked && step.summary && /* @__PURE__ */ jsx8("p", { className: "awr-step__blocked", children: step.summary })
|
|
3273
2771
|
] });
|
|
3274
2772
|
var StepList = ({
|
|
3275
2773
|
steps,
|
|
3276
2774
|
subagents = [],
|
|
3277
2775
|
approvals = [],
|
|
2776
|
+
notes = [],
|
|
2777
|
+
showNotes = true,
|
|
2778
|
+
onStopSubagent,
|
|
3278
2779
|
showToolCalls = true,
|
|
3279
2780
|
showUsage = true,
|
|
3280
2781
|
showThoughts = true,
|
|
@@ -3284,20 +2785,27 @@ var StepList = ({
|
|
|
3284
2785
|
}) => {
|
|
3285
2786
|
const L = useLabels(labels);
|
|
3286
2787
|
if (steps.length === 0) return null;
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
2788
|
+
const known = new Set(steps.map((s) => s.id));
|
|
2789
|
+
const atEnd = showNotes ? notes.filter((n) => !n.stepId || !known.has(n.stepId)) : [];
|
|
2790
|
+
return /* @__PURE__ */ jsxs6("ol", { className: ["awr-steps", className].filter(Boolean).join(" "), children: [
|
|
2791
|
+
steps.map((step) => /* @__PURE__ */ jsx8(
|
|
2792
|
+
StepItem,
|
|
2793
|
+
{
|
|
2794
|
+
step,
|
|
2795
|
+
subagents: subagents.filter((s) => s.stepId === step.id),
|
|
2796
|
+
approvals: approvals.filter((a) => a.stepId === step.id && a.status !== "pending"),
|
|
2797
|
+
notes: showNotes ? notes.filter((n) => n.stepId === step.id) : [],
|
|
2798
|
+
onStopSubagent,
|
|
2799
|
+
showToolCalls,
|
|
2800
|
+
showUsage,
|
|
2801
|
+
showThoughts,
|
|
2802
|
+
renderToolCall,
|
|
2803
|
+
L
|
|
2804
|
+
},
|
|
2805
|
+
step.id
|
|
2806
|
+
)),
|
|
2807
|
+
atEnd.length > 0 && /* @__PURE__ */ jsx8("li", { className: "awr-step awr-step--notes", children: /* @__PURE__ */ jsx8("ul", { className: "awr-notes", children: atEnd.map((n) => /* @__PURE__ */ jsx8(UserNoteRow, { note: n }, n.id)) }) })
|
|
2808
|
+
] });
|
|
3301
2809
|
};
|
|
3302
2810
|
|
|
3303
2811
|
// src/components/MessageList.tsx
|
|
@@ -3333,6 +2841,8 @@ var MessageList = ({
|
|
|
3333
2841
|
showToolCalls = true,
|
|
3334
2842
|
showUsage = true,
|
|
3335
2843
|
showThoughts = true,
|
|
2844
|
+
showNotes = true,
|
|
2845
|
+
onStopSubagent,
|
|
3336
2846
|
activityOpen = false,
|
|
3337
2847
|
liveExtra,
|
|
3338
2848
|
renderContent,
|
|
@@ -3364,6 +2874,9 @@ var MessageList = ({
|
|
|
3364
2874
|
steps,
|
|
3365
2875
|
subagents: m.subagents,
|
|
3366
2876
|
approvals: m.approvals,
|
|
2877
|
+
notes: m.notes,
|
|
2878
|
+
showNotes,
|
|
2879
|
+
onStopSubagent: m.pending ? onStopSubagent : void 0,
|
|
3367
2880
|
showToolCalls,
|
|
3368
2881
|
showUsage,
|
|
3369
2882
|
showThoughts,
|
|
@@ -3505,6 +3018,8 @@ var AgentChatInner = ({
|
|
|
3505
3018
|
showToolCalls = true,
|
|
3506
3019
|
showUsage = true,
|
|
3507
3020
|
showThoughts = true,
|
|
3021
|
+
showNotes = true,
|
|
3022
|
+
stoppableSubagents = true,
|
|
3508
3023
|
showTotalUsage = true,
|
|
3509
3024
|
showStatus = true,
|
|
3510
3025
|
showApprovalMode = true,
|
|
@@ -3537,7 +3052,7 @@ var AgentChatInner = ({
|
|
|
3537
3052
|
}
|
|
3538
3053
|
const L = useLabels();
|
|
3539
3054
|
const external = isHistoryHook(history) ? history : void 0;
|
|
3540
|
-
const [ownStore] =
|
|
3055
|
+
const [ownStore] = useState9(
|
|
3541
3056
|
() => history instanceof ChatHistoryStore ? history : new ChatHistoryStore()
|
|
3542
3057
|
);
|
|
3543
3058
|
const own = useChatHistory(agent, {
|
|
@@ -3545,9 +3060,9 @@ var AgentChatInner = ({
|
|
|
3545
3060
|
enabled: Boolean(history) && !external
|
|
3546
3061
|
});
|
|
3547
3062
|
const chats = history ? external ?? own : void 0;
|
|
3548
|
-
const rootRef =
|
|
3549
|
-
const [narrow, setNarrow] =
|
|
3550
|
-
|
|
3063
|
+
const rootRef = useRef7(null);
|
|
3064
|
+
const [narrow, setNarrow] = useState9(void 0);
|
|
3065
|
+
useEffect7(() => {
|
|
3551
3066
|
const el = rootRef.current;
|
|
3552
3067
|
if (!el || typeof ResizeObserver === "undefined") return setNarrow(false);
|
|
3553
3068
|
const measure = () => setNarrow(el.getBoundingClientRect().width < narrowWidth);
|
|
@@ -3556,9 +3071,9 @@ var AgentChatInner = ({
|
|
|
3556
3071
|
ro.observe(el);
|
|
3557
3072
|
return () => ro.disconnect();
|
|
3558
3073
|
}, [narrowWidth]);
|
|
3559
|
-
const [historyChoice, setHistoryOpen] =
|
|
3074
|
+
const [historyChoice, setHistoryOpen] = useState9(defaultHistoryOpen);
|
|
3560
3075
|
const historyOpen = historyChoice ?? narrow === false;
|
|
3561
|
-
const [filesOpen, setFilesOpen] =
|
|
3076
|
+
const [filesOpen, setFilesOpen] = useState9(defaultFilesOpen);
|
|
3562
3077
|
const toggleHistory = () => {
|
|
3563
3078
|
setHistoryOpen(!historyOpen);
|
|
3564
3079
|
if (narrow && !historyOpen) setFilesOpen(false);
|
|
@@ -3567,13 +3082,13 @@ var AgentChatInner = ({
|
|
|
3567
3082
|
setFilesOpen((o) => !o);
|
|
3568
3083
|
if (narrow && !filesOpen) setHistoryOpen(false);
|
|
3569
3084
|
};
|
|
3570
|
-
const bodyRef =
|
|
3571
|
-
const stickRef =
|
|
3572
|
-
|
|
3085
|
+
const bodyRef = useRef7(null);
|
|
3086
|
+
const stickRef = useRef7(true);
|
|
3087
|
+
useEffect7(() => {
|
|
3573
3088
|
const el = bodyRef.current;
|
|
3574
3089
|
if (el && stickRef.current) el.scrollTop = el.scrollHeight;
|
|
3575
3090
|
}, [agent.messages, agent.steps, agent.finalText, agent.modelLoad, agent.pendingApprovals]);
|
|
3576
|
-
const [compacting, setCompacting] =
|
|
3091
|
+
const [compacting, setCompacting] = useState9(false);
|
|
3577
3092
|
const compact = () => {
|
|
3578
3093
|
setCompacting(true);
|
|
3579
3094
|
void agent.compact().finally(() => setCompacting(false));
|
|
@@ -3687,6 +3202,8 @@ var AgentChatInner = ({
|
|
|
3687
3202
|
showToolCalls,
|
|
3688
3203
|
showUsage,
|
|
3689
3204
|
showThoughts,
|
|
3205
|
+
showNotes,
|
|
3206
|
+
onStopSubagent: stoppableSubagents ? agent.stopSubagent : void 0,
|
|
3690
3207
|
activityOpen,
|
|
3691
3208
|
liveExtra,
|
|
3692
3209
|
renderContent,
|
|
@@ -3758,7 +3275,7 @@ var AgentChatInner = ({
|
|
|
3758
3275
|
};
|
|
3759
3276
|
|
|
3760
3277
|
// src/components/Composer.tsx
|
|
3761
|
-
import { useState as
|
|
3278
|
+
import { useState as useState10 } from "react";
|
|
3762
3279
|
import { jsx as jsx13, jsxs as jsxs12 } from "react/jsx-runtime";
|
|
3763
3280
|
var Composer = ({
|
|
3764
3281
|
onSubmit,
|
|
@@ -3768,7 +3285,7 @@ var Composer = ({
|
|
|
3768
3285
|
placeholder = "Ask the agent to do something\u2026",
|
|
3769
3286
|
className
|
|
3770
3287
|
}) => {
|
|
3771
|
-
const [text, setText] =
|
|
3288
|
+
const [text, setText] = useState10("");
|
|
3772
3289
|
const submit = () => {
|
|
3773
3290
|
const value = text.trim();
|
|
3774
3291
|
if (!value || busy || disabled) return;
|
|
@@ -3881,7 +3398,7 @@ var ApprovalModeSwitch = ({
|
|
|
3881
3398
|
);
|
|
3882
3399
|
|
|
3883
3400
|
// src/components/ApiKeyForm.tsx
|
|
3884
|
-
import { useEffect as
|
|
3401
|
+
import { useEffect as useEffect8, useState as useState11 } from "react";
|
|
3885
3402
|
import { jsx as jsx16, jsxs as jsxs14 } from "react/jsx-runtime";
|
|
3886
3403
|
var ApiKeyForm = ({
|
|
3887
3404
|
credentials,
|
|
@@ -3891,10 +3408,10 @@ var ApiKeyForm = ({
|
|
|
3891
3408
|
onChange,
|
|
3892
3409
|
className
|
|
3893
3410
|
}) => {
|
|
3894
|
-
const [value, setValue] =
|
|
3895
|
-
const [present, setPresent] =
|
|
3896
|
-
const [busy, setBusy] =
|
|
3897
|
-
|
|
3411
|
+
const [value, setValue] = useState11("");
|
|
3412
|
+
const [present, setPresent] = useState11(false);
|
|
3413
|
+
const [busy, setBusy] = useState11(false);
|
|
3414
|
+
useEffect8(() => {
|
|
3898
3415
|
let active = true;
|
|
3899
3416
|
credentials.hasKey(credentialRef).then((has) => {
|
|
3900
3417
|
if (active) setPresent(has);
|
|
@@ -3984,6 +3501,7 @@ import {
|
|
|
3984
3501
|
defineSkill,
|
|
3985
3502
|
parseSkillMarkdown,
|
|
3986
3503
|
loadSkillFromUrl,
|
|
3504
|
+
createRunControl as createRunControl2,
|
|
3987
3505
|
createSubagentTool,
|
|
3988
3506
|
serveSubagentWorker,
|
|
3989
3507
|
markReadOnly,
|
|
@@ -4028,27 +3546,26 @@ export {
|
|
|
4028
3546
|
ToolApprovalPrompt,
|
|
4029
3547
|
ToolCallRow,
|
|
4030
3548
|
UsageBadge,
|
|
3549
|
+
UserNoteRow,
|
|
4031
3550
|
VaultCredentialStore2 as VaultCredentialStore,
|
|
4032
3551
|
VirtualFileSystem,
|
|
4033
3552
|
agentStateReducer,
|
|
4034
3553
|
attachmentKindOf,
|
|
4035
3554
|
attachmentRefusal,
|
|
4036
3555
|
chatTitle,
|
|
4037
|
-
claimOAuthCallback,
|
|
4038
3556
|
createAgent2 as createAgent,
|
|
4039
3557
|
createFileTools,
|
|
4040
3558
|
createInitialAgentState,
|
|
3559
|
+
createRunControl2 as createRunControl,
|
|
4041
3560
|
createSubagentTool,
|
|
4042
3561
|
createWebLLMEngine,
|
|
4043
3562
|
createWebLLMModel2 as createWebLLMModel,
|
|
4044
3563
|
defaultLabels,
|
|
4045
3564
|
defineSkill,
|
|
4046
3565
|
defineTool,
|
|
4047
|
-
describeMcpResult,
|
|
4048
3566
|
downscaleImage,
|
|
4049
3567
|
filterCommands,
|
|
4050
3568
|
formatElapsed,
|
|
4051
|
-
isUnauthorizedError,
|
|
4052
3569
|
isWebGPUAvailable2 as isWebGPUAvailable,
|
|
4053
3570
|
loadSkillFromUrl,
|
|
4054
3571
|
markReadOnly,
|
|
@@ -4058,9 +3575,8 @@ export {
|
|
|
4058
3575
|
parseSkillMarkdown,
|
|
4059
3576
|
parseSlash,
|
|
4060
3577
|
preloadWebLLMModel2 as preloadWebLLMModel,
|
|
4061
|
-
readCallbackParams,
|
|
4062
3578
|
serveSubagentWorker,
|
|
4063
|
-
|
|
3579
|
+
steerIntent,
|
|
4064
3580
|
supportsImages,
|
|
4065
3581
|
supportsPdf,
|
|
4066
3582
|
unloadWebLLMModel2 as unloadWebLLMModel,
|
|
@@ -4071,8 +3587,6 @@ export {
|
|
|
4071
3587
|
useCredentials,
|
|
4072
3588
|
useLabels,
|
|
4073
3589
|
useLocalModel,
|
|
4074
|
-
useMcp,
|
|
4075
|
-
useMcpServers,
|
|
4076
3590
|
useOptionalAgentContext,
|
|
4077
3591
|
useSpeechToText,
|
|
4078
3592
|
useVirtualFiles,
|