@fragments-sdk/viewer 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +84 -0
- package/index.html +28 -0
- package/package.json +71 -0
- package/src/__tests__/a11y-fixes.test.ts +358 -0
- package/src/__tests__/jsx-parser.test.ts +502 -0
- package/src/__tests__/render-utils.test.ts +232 -0
- package/src/__tests__/style-utils.test.ts +404 -0
- package/src/app/index.ts +1 -0
- package/src/assets/fragments-logo.ts +4 -0
- package/src/assets/fragments_logo.png +0 -0
- package/src/components/AccessibilityPanel.tsx +1457 -0
- package/src/components/ActionCapture.tsx +172 -0
- package/src/components/ActionsPanel.tsx +332 -0
- package/src/components/AllVariantsPreview.tsx +78 -0
- package/src/components/App.tsx +604 -0
- package/src/components/BottomPanel.tsx +288 -0
- package/src/components/CodePanel.naming.test.tsx +59 -0
- package/src/components/CodePanel.tsx +118 -0
- package/src/components/CommandPalette.tsx +392 -0
- package/src/components/ComponentDocView.tsx +164 -0
- package/src/components/ComponentGraph.tsx +380 -0
- package/src/components/ComponentHeader.tsx +88 -0
- package/src/components/ContractPanel.tsx +241 -0
- package/src/components/DeviceMockup.tsx +156 -0
- package/src/components/EmptyVariantMessage.tsx +54 -0
- package/src/components/ErrorBoundary.tsx +97 -0
- package/src/components/FigmaEmbed.tsx +238 -0
- package/src/components/FragmentEditor.tsx +525 -0
- package/src/components/FragmentRenderer.tsx +61 -0
- package/src/components/HeaderSearch.tsx +24 -0
- package/src/components/HealthDashboard.tsx +441 -0
- package/src/components/HmrStatusIndicator.tsx +61 -0
- package/src/components/Icons.tsx +479 -0
- package/src/components/InteractionsPanel.tsx +757 -0
- package/src/components/IsolatedPreviewFrame.tsx +390 -0
- package/src/components/IsolatedRender.tsx +113 -0
- package/src/components/KeyboardShortcutsHelp.tsx +53 -0
- package/src/components/LandingPage.tsx +420 -0
- package/src/components/Layout.tsx +27 -0
- package/src/components/LeftSidebar.tsx +472 -0
- package/src/components/LoadErrorMessage.tsx +102 -0
- package/src/components/MultiViewportPreview.tsx +527 -0
- package/src/components/NoVariantsMessage.tsx +59 -0
- package/src/components/PanelShell.tsx +161 -0
- package/src/components/PerformancePanel.tsx +304 -0
- package/src/components/PreviewArea.tsx +254 -0
- package/src/components/PreviewAside.tsx +168 -0
- package/src/components/PreviewFrameHost.tsx +304 -0
- package/src/components/PreviewToolbar.tsx +80 -0
- package/src/components/PropsEditor.tsx +506 -0
- package/src/components/PropsTable.tsx +111 -0
- package/src/components/RelationsSection.tsx +88 -0
- package/src/components/ResizablePanel.tsx +271 -0
- package/src/components/RightSidebar.tsx +102 -0
- package/src/components/RuntimeToolsRegistrar.tsx +17 -0
- package/src/components/ScreenshotButton.tsx +90 -0
- package/src/components/ShadowPreview.tsx +204 -0
- package/src/components/Sidebar.tsx +169 -0
- package/src/components/SkeletonLoader.tsx +161 -0
- package/src/components/ThemeProvider.tsx +42 -0
- package/src/components/Toast.tsx +3 -0
- package/src/components/TokenStylePanel.tsx +699 -0
- package/src/components/TopToolbar.tsx +159 -0
- package/src/components/Untitled +1 -0
- package/src/components/UsageSection.tsx +95 -0
- package/src/components/VariantMatrix.tsx +391 -0
- package/src/components/VariantRenderer.tsx +131 -0
- package/src/components/VariantTabs.tsx +40 -0
- package/src/components/ViewerHeader.tsx +69 -0
- package/src/components/ViewerStateSync.tsx +52 -0
- package/src/components/ViewportSelector.tsx +172 -0
- package/src/components/WebMCPDevTools.tsx +503 -0
- package/src/components/WebMCPIntegration.tsx +47 -0
- package/src/components/WebMCPStatusIndicator.tsx +60 -0
- package/src/components/_future/CreatePage.tsx +835 -0
- package/src/components/viewer-utils.ts +16 -0
- package/src/composition-renderer.ts +381 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/ui.ts +166 -0
- package/src/entry.tsx +335 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useA11yCache.ts +383 -0
- package/src/hooks/useA11yService.ts +364 -0
- package/src/hooks/useActions.ts +138 -0
- package/src/hooks/useAppState.ts +147 -0
- package/src/hooks/useCompiledFragments.ts +42 -0
- package/src/hooks/useFigmaIntegration.ts +132 -0
- package/src/hooks/useHmrStatus.ts +109 -0
- package/src/hooks/useKeyboardShortcuts.ts +270 -0
- package/src/hooks/usePreviewBridge.ts +347 -0
- package/src/hooks/useScrollSpy.ts +78 -0
- package/src/hooks/useShadowStyles.ts +221 -0
- package/src/hooks/useUrlState.ts +318 -0
- package/src/hooks/useViewSettings.ts +111 -0
- package/src/intelligence/healthReport.ts +505 -0
- package/src/intelligence/styleDrift.ts +340 -0
- package/src/intelligence/usageScanner.ts +309 -0
- package/src/jsx-parser.ts +486 -0
- package/src/preview-frame-entry.tsx +25 -0
- package/src/preview-frame.html +148 -0
- package/src/render-template.html +68 -0
- package/src/render-utils.ts +311 -0
- package/src/shared/ComponentDocContent.module.scss +10 -0
- package/src/shared/ComponentDocContent.module.scss.d.ts +2 -0
- package/src/shared/ComponentDocContent.tsx +274 -0
- package/src/shared/DocsHeaderBar.tsx +129 -0
- package/src/shared/DocsPageAsideHost.tsx +89 -0
- package/src/shared/DocsPageShell.tsx +124 -0
- package/src/shared/DocsSearchCommand.tsx +99 -0
- package/src/shared/DocsSidebarNav.tsx +66 -0
- package/src/shared/PropsTable.module.scss +68 -0
- package/src/shared/PropsTable.module.scss.d.ts +2 -0
- package/src/shared/PropsTable.tsx +76 -0
- package/src/shared/VariantPreviewCard.module.scss +114 -0
- package/src/shared/VariantPreviewCard.module.scss.d.ts +2 -0
- package/src/shared/VariantPreviewCard.tsx +137 -0
- package/src/shared/docs-data/index.ts +32 -0
- package/src/shared/docs-data/mcp-configs.ts +72 -0
- package/src/shared/docs-data/palettes.ts +75 -0
- package/src/shared/docs-data/setup-examples.ts +55 -0
- package/src/shared/docs-layout.scss +28 -0
- package/src/shared/docs-layout.scss.d.ts +2 -0
- package/src/shared/index.ts +34 -0
- package/src/shared/types.ts +53 -0
- package/src/style-utils.ts +414 -0
- package/src/styles/globals.css +278 -0
- package/src/types/a11y.ts +197 -0
- package/src/utils/a11y-fixes.ts +509 -0
- package/src/utils/actionExport.ts +372 -0
- package/src/utils/colorSchemes.ts +201 -0
- package/src/utils/contrast.ts +246 -0
- package/src/utils/detectRelationships.ts +256 -0
- package/src/webmcp/__tests__/analytics.test.ts +108 -0
- package/src/webmcp/analytics.ts +165 -0
- package/src/webmcp/index.ts +3 -0
- package/src/webmcp/posthog-bridge.ts +39 -0
- package/src/webmcp/runtime-tools.ts +152 -0
- package/src/webmcp/scan-utils.ts +135 -0
- package/src/webmcp/use-tool-analytics.ts +69 -0
- package/src/webmcp/viewer-state.ts +45 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback, useRef } from "react";
|
|
2
|
+
import {
|
|
3
|
+
Tabs,
|
|
4
|
+
Select,
|
|
5
|
+
Textarea,
|
|
6
|
+
Button,
|
|
7
|
+
CodeBlock,
|
|
8
|
+
Badge,
|
|
9
|
+
Card,
|
|
10
|
+
Stack,
|
|
11
|
+
Box,
|
|
12
|
+
Text,
|
|
13
|
+
} from "@fragments-sdk/ui";
|
|
14
|
+
import { useWebMCPContext } from "@fragments-sdk/webmcp/react";
|
|
15
|
+
import type {
|
|
16
|
+
WebMCPTool,
|
|
17
|
+
JSONSchemaInput,
|
|
18
|
+
ToolCallEvent,
|
|
19
|
+
} from "@fragments-sdk/webmcp";
|
|
20
|
+
import { SPEC_VERSION } from "@fragments-sdk/webmcp";
|
|
21
|
+
import { useToolAnalytics } from "../webmcp/use-tool-analytics.js";
|
|
22
|
+
|
|
23
|
+
function generateInputSkeleton(tool: WebMCPTool): string {
|
|
24
|
+
const schema = tool.inputSchema as JSONSchemaInput | undefined;
|
|
25
|
+
if (!schema?.properties) return "{}";
|
|
26
|
+
const obj: Record<string, unknown> = {};
|
|
27
|
+
for (const [key, prop] of Object.entries(schema.properties)) {
|
|
28
|
+
if (prop.default !== undefined) obj[key] = prop.default;
|
|
29
|
+
else if (prop.enum?.length) obj[key] = prop.enum[0];
|
|
30
|
+
else if (prop.type === "string") obj[key] = "";
|
|
31
|
+
else if (prop.type === "number" || prop.type === "integer") obj[key] = 0;
|
|
32
|
+
else if (prop.type === "boolean") obj[key] = false;
|
|
33
|
+
else if (prop.type === "array") obj[key] = [];
|
|
34
|
+
else obj[key] = null;
|
|
35
|
+
}
|
|
36
|
+
return JSON.stringify(obj, null, 2);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function formatTimestamp(ts: number): string {
|
|
40
|
+
return new Date(ts).toLocaleTimeString();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function formatDuration(ms: number): string {
|
|
44
|
+
if (ms < 1000) return `${ms}ms`;
|
|
45
|
+
if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`;
|
|
46
|
+
return `${(ms / 60_000).toFixed(1)}m`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const MAX_ACTIVITY_ENTRIES = 50;
|
|
50
|
+
|
|
51
|
+
export function WebMCPDevTools() {
|
|
52
|
+
const { shimRegistry, isShim } = useWebMCPContext();
|
|
53
|
+
const { summary, refreshSummary } = useToolAnalytics();
|
|
54
|
+
const [tools, setTools] = useState<WebMCPTool[]>([]);
|
|
55
|
+
const [selectedTool, setSelectedTool] = useState<string>("");
|
|
56
|
+
const [inputJson, setInputJson] = useState("{}");
|
|
57
|
+
const [executing, setExecuting] = useState(false);
|
|
58
|
+
const [result, setResult] = useState<{ type: "success" | "error"; data: string } | null>(null);
|
|
59
|
+
const [activity, setActivity] = useState<ToolCallEvent[]>([]);
|
|
60
|
+
const [expandedEntry, setExpandedEntry] = useState<number | null>(null);
|
|
61
|
+
const activityRef = useRef(activity);
|
|
62
|
+
activityRef.current = activity;
|
|
63
|
+
|
|
64
|
+
// Sync tools from registry
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!shimRegistry) return;
|
|
67
|
+
|
|
68
|
+
const update = () => {
|
|
69
|
+
setTools(Array.from(shimRegistry.tools.values()));
|
|
70
|
+
};
|
|
71
|
+
update();
|
|
72
|
+
|
|
73
|
+
shimRegistry.addEventListener("toolchange", update);
|
|
74
|
+
return () => shimRegistry.removeEventListener("toolchange", update);
|
|
75
|
+
}, [shimRegistry]);
|
|
76
|
+
|
|
77
|
+
// Subscribe to toolcall events
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (!shimRegistry) return;
|
|
80
|
+
|
|
81
|
+
const onToolCall = (event: ToolCallEvent) => {
|
|
82
|
+
setActivity((prev) => [event, ...prev].slice(0, MAX_ACTIVITY_ENTRIES));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
shimRegistry.addEventListener("toolcall", onToolCall);
|
|
86
|
+
return () => shimRegistry.removeEventListener("toolcall", onToolCall);
|
|
87
|
+
}, [shimRegistry]);
|
|
88
|
+
|
|
89
|
+
// Update input skeleton when tool selection changes
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (!selectedTool) return;
|
|
92
|
+
const tool = tools.find((t) => t.name === selectedTool);
|
|
93
|
+
if (tool) {
|
|
94
|
+
setInputJson(generateInputSkeleton(tool));
|
|
95
|
+
setResult(null);
|
|
96
|
+
}
|
|
97
|
+
}, [selectedTool, tools]);
|
|
98
|
+
|
|
99
|
+
// Auto-select first tool
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (tools.length > 0 && !selectedTool) {
|
|
102
|
+
setSelectedTool(tools[0].name);
|
|
103
|
+
}
|
|
104
|
+
}, [tools, selectedTool]);
|
|
105
|
+
|
|
106
|
+
const handleExecute = useCallback(async () => {
|
|
107
|
+
const tool = tools.find((t) => t.name === selectedTool);
|
|
108
|
+
if (!tool) return;
|
|
109
|
+
|
|
110
|
+
setExecuting(true);
|
|
111
|
+
setResult(null);
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const input = JSON.parse(inputJson);
|
|
115
|
+
const output = await tool.execute(input, {} as any);
|
|
116
|
+
setResult({
|
|
117
|
+
type: "success",
|
|
118
|
+
data: JSON.stringify(output, null, 2),
|
|
119
|
+
});
|
|
120
|
+
} catch (err) {
|
|
121
|
+
setResult({
|
|
122
|
+
type: "error",
|
|
123
|
+
data: err instanceof Error ? err.message : String(err),
|
|
124
|
+
});
|
|
125
|
+
} finally {
|
|
126
|
+
setExecuting(false);
|
|
127
|
+
}
|
|
128
|
+
}, [tools, selectedTool, inputJson]);
|
|
129
|
+
|
|
130
|
+
const currentTool = tools.find((t) => t.name === selectedTool);
|
|
131
|
+
const schema = currentTool?.inputSchema as JSONSchemaInput | undefined;
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<Stack direction="column" gap="md" style={{ height: "100%" }}>
|
|
135
|
+
<Tabs defaultValue="tools">
|
|
136
|
+
<Tabs.List variant="underline">
|
|
137
|
+
<Tabs.Tab value="tools">Tools</Tabs.Tab>
|
|
138
|
+
<Tabs.Tab value="execute">Execute</Tabs.Tab>
|
|
139
|
+
<Tabs.Tab value="activity">
|
|
140
|
+
Activity{activity.length > 0 ? ` (${activity.length})` : ""}
|
|
141
|
+
</Tabs.Tab>
|
|
142
|
+
<Tabs.Tab value="analytics">Analytics</Tabs.Tab>
|
|
143
|
+
</Tabs.List>
|
|
144
|
+
|
|
145
|
+
{/* Tab 1: Tool Catalog */}
|
|
146
|
+
<Tabs.Panel value="tools">
|
|
147
|
+
<Stack direction="column" gap="md" style={{ paddingTop: "12px" }}>
|
|
148
|
+
{tools.length === 0 ? (
|
|
149
|
+
<Text color="secondary">No tools registered yet.</Text>
|
|
150
|
+
) : (
|
|
151
|
+
<>
|
|
152
|
+
<Select
|
|
153
|
+
value={selectedTool}
|
|
154
|
+
onValueChange={setSelectedTool}
|
|
155
|
+
placeholder="Select a tool"
|
|
156
|
+
>
|
|
157
|
+
<Select.Trigger />
|
|
158
|
+
<Select.Content>
|
|
159
|
+
{tools.map((tool) => (
|
|
160
|
+
<Select.Item key={tool.name} value={tool.name}>
|
|
161
|
+
{tool.name}
|
|
162
|
+
</Select.Item>
|
|
163
|
+
))}
|
|
164
|
+
</Select.Content>
|
|
165
|
+
</Select>
|
|
166
|
+
|
|
167
|
+
{currentTool && (
|
|
168
|
+
<Card variant="outlined">
|
|
169
|
+
<Card.Body>
|
|
170
|
+
<Stack direction="column" gap="sm">
|
|
171
|
+
<Text weight="semibold">{currentTool.name}</Text>
|
|
172
|
+
<Text color="secondary" size="sm">
|
|
173
|
+
{currentTool.description}
|
|
174
|
+
</Text>
|
|
175
|
+
{currentTool.annotations?.readOnlyHint && (
|
|
176
|
+
<div>
|
|
177
|
+
<Badge variant="info" size="sm">
|
|
178
|
+
readOnly
|
|
179
|
+
</Badge>
|
|
180
|
+
</div>
|
|
181
|
+
)}
|
|
182
|
+
|
|
183
|
+
{schema?.properties && (
|
|
184
|
+
<Box style={{ marginTop: "8px" }}>
|
|
185
|
+
<Text size="xs" weight="semibold" color="secondary" style={{ marginBottom: "4px" }}>
|
|
186
|
+
Parameters
|
|
187
|
+
</Text>
|
|
188
|
+
<Stack direction="column" gap="xs">
|
|
189
|
+
{Object.entries(schema.properties).map(
|
|
190
|
+
([name, prop]) => (
|
|
191
|
+
<Box
|
|
192
|
+
key={name}
|
|
193
|
+
padding="sm"
|
|
194
|
+
rounded="sm"
|
|
195
|
+
background="secondary"
|
|
196
|
+
>
|
|
197
|
+
<Stack direction="row" gap="xs" align="center">
|
|
198
|
+
<Text
|
|
199
|
+
size="xs"
|
|
200
|
+
weight="medium"
|
|
201
|
+
font="mono"
|
|
202
|
+
>
|
|
203
|
+
{name}
|
|
204
|
+
</Text>
|
|
205
|
+
<Text size="2xs" color="tertiary">
|
|
206
|
+
{prop.type}
|
|
207
|
+
</Text>
|
|
208
|
+
{schema.required?.includes(name) && (
|
|
209
|
+
<Badge variant="warning" size="sm">
|
|
210
|
+
required
|
|
211
|
+
</Badge>
|
|
212
|
+
)}
|
|
213
|
+
</Stack>
|
|
214
|
+
{prop.description && (
|
|
215
|
+
<Text size="2xs" color="secondary" style={{ marginTop: "2px" }}>
|
|
216
|
+
{prop.description}
|
|
217
|
+
</Text>
|
|
218
|
+
)}
|
|
219
|
+
{prop.enum && (
|
|
220
|
+
<Text size="2xs" color="tertiary" font="mono" style={{ marginTop: "2px" }}>
|
|
221
|
+
enum: {prop.enum.join(" | ")}
|
|
222
|
+
</Text>
|
|
223
|
+
)}
|
|
224
|
+
{prop.default !== undefined && (
|
|
225
|
+
<Text size="2xs" color="tertiary" style={{ marginTop: "2px" }}>
|
|
226
|
+
default: {JSON.stringify(prop.default)}
|
|
227
|
+
</Text>
|
|
228
|
+
)}
|
|
229
|
+
</Box>
|
|
230
|
+
)
|
|
231
|
+
)}
|
|
232
|
+
</Stack>
|
|
233
|
+
</Box>
|
|
234
|
+
)}
|
|
235
|
+
</Stack>
|
|
236
|
+
</Card.Body>
|
|
237
|
+
</Card>
|
|
238
|
+
)}
|
|
239
|
+
</>
|
|
240
|
+
)}
|
|
241
|
+
</Stack>
|
|
242
|
+
</Tabs.Panel>
|
|
243
|
+
|
|
244
|
+
{/* Tab 2: Manual Execution */}
|
|
245
|
+
<Tabs.Panel value="execute">
|
|
246
|
+
<Stack direction="column" gap="md" style={{ paddingTop: "12px" }}>
|
|
247
|
+
<Select
|
|
248
|
+
value={selectedTool}
|
|
249
|
+
onValueChange={setSelectedTool}
|
|
250
|
+
placeholder="Select a tool"
|
|
251
|
+
>
|
|
252
|
+
<Select.Trigger />
|
|
253
|
+
<Select.Content>
|
|
254
|
+
{tools.map((tool) => (
|
|
255
|
+
<Select.Item key={tool.name} value={tool.name}>
|
|
256
|
+
{tool.name}
|
|
257
|
+
</Select.Item>
|
|
258
|
+
))}
|
|
259
|
+
</Select.Content>
|
|
260
|
+
</Select>
|
|
261
|
+
|
|
262
|
+
<Textarea
|
|
263
|
+
label="Input (JSON)"
|
|
264
|
+
rows={8}
|
|
265
|
+
value={inputJson}
|
|
266
|
+
onChange={setInputJson}
|
|
267
|
+
resize="vertical"
|
|
268
|
+
style={{ fontFamily: "monospace", fontSize: "12px" }}
|
|
269
|
+
/>
|
|
270
|
+
|
|
271
|
+
<Button
|
|
272
|
+
variant="primary"
|
|
273
|
+
onClick={handleExecute}
|
|
274
|
+
disabled={executing || !selectedTool}
|
|
275
|
+
data-testid="webmcp-execute-btn"
|
|
276
|
+
>
|
|
277
|
+
{executing ? "Executing..." : "Execute"}
|
|
278
|
+
</Button>
|
|
279
|
+
|
|
280
|
+
{result && (
|
|
281
|
+
<Box style={{ marginTop: "4px" }}>
|
|
282
|
+
{result.type === "success" ? (
|
|
283
|
+
<CodeBlock
|
|
284
|
+
code={result.data}
|
|
285
|
+
language="json"
|
|
286
|
+
maxHeight={300}
|
|
287
|
+
wordWrap
|
|
288
|
+
showCopy
|
|
289
|
+
compact
|
|
290
|
+
/>
|
|
291
|
+
) : (
|
|
292
|
+
<Box
|
|
293
|
+
padding="sm"
|
|
294
|
+
rounded="md"
|
|
295
|
+
style={{
|
|
296
|
+
backgroundColor: "var(--bg-danger, #fef2f2)",
|
|
297
|
+
}}
|
|
298
|
+
>
|
|
299
|
+
<Text size="sm" style={{ color: "var(--color-danger, #dc2626)" }}>
|
|
300
|
+
{result.data}
|
|
301
|
+
</Text>
|
|
302
|
+
</Box>
|
|
303
|
+
)}
|
|
304
|
+
</Box>
|
|
305
|
+
)}
|
|
306
|
+
</Stack>
|
|
307
|
+
</Tabs.Panel>
|
|
308
|
+
|
|
309
|
+
{/* Tab 3: Activity Log */}
|
|
310
|
+
<Tabs.Panel value="activity">
|
|
311
|
+
<Stack direction="column" gap="xs" style={{ paddingTop: "12px" }}>
|
|
312
|
+
{activity.length === 0 ? (
|
|
313
|
+
<Text color="secondary" size="sm">
|
|
314
|
+
No tool calls yet. Execute a tool or wait for an agent to call
|
|
315
|
+
one.
|
|
316
|
+
</Text>
|
|
317
|
+
) : (
|
|
318
|
+
activity.map((entry, i) => (
|
|
319
|
+
<Card
|
|
320
|
+
key={`${entry.timestamp}-${i}`}
|
|
321
|
+
variant="outlined"
|
|
322
|
+
style={{ padding: "8px 12px", cursor: "pointer" }}
|
|
323
|
+
onClick={() => setExpandedEntry(expandedEntry === i ? null : i)}
|
|
324
|
+
>
|
|
325
|
+
<Stack direction="row" gap="xs" align="center">
|
|
326
|
+
<Badge
|
|
327
|
+
size="sm"
|
|
328
|
+
variant={entry.error ? "error" : "success"}
|
|
329
|
+
>
|
|
330
|
+
{entry.toolName}
|
|
331
|
+
</Badge>
|
|
332
|
+
<Text size="2xs" color="tertiary">
|
|
333
|
+
{formatTimestamp(entry.timestamp)}
|
|
334
|
+
</Text>
|
|
335
|
+
<Text size="2xs" color="secondary">
|
|
336
|
+
{entry.durationMs}ms
|
|
337
|
+
</Text>
|
|
338
|
+
</Stack>
|
|
339
|
+
{expandedEntry === i && (
|
|
340
|
+
<Stack direction="column" gap="xs" style={{ marginTop: "8px" }}>
|
|
341
|
+
<Text size="2xs" weight="semibold" color="secondary">
|
|
342
|
+
Input
|
|
343
|
+
</Text>
|
|
344
|
+
<CodeBlock
|
|
345
|
+
code={JSON.stringify(entry.input, null, 2)}
|
|
346
|
+
language="json"
|
|
347
|
+
compact
|
|
348
|
+
maxHeight={150}
|
|
349
|
+
/>
|
|
350
|
+
<Text size="2xs" weight="semibold" color="secondary">
|
|
351
|
+
{entry.error ? "Error" : "Output"}
|
|
352
|
+
</Text>
|
|
353
|
+
<CodeBlock
|
|
354
|
+
code={
|
|
355
|
+
entry.error
|
|
356
|
+
? entry.error
|
|
357
|
+
: JSON.stringify(entry.output, null, 2)
|
|
358
|
+
}
|
|
359
|
+
language={entry.error ? "plaintext" : "json"}
|
|
360
|
+
compact
|
|
361
|
+
maxHeight={150}
|
|
362
|
+
/>
|
|
363
|
+
</Stack>
|
|
364
|
+
)}
|
|
365
|
+
</Card>
|
|
366
|
+
))
|
|
367
|
+
)}
|
|
368
|
+
</Stack>
|
|
369
|
+
</Tabs.Panel>
|
|
370
|
+
|
|
371
|
+
{/* Tab 4: Analytics */}
|
|
372
|
+
<Tabs.Panel value="analytics">
|
|
373
|
+
<Stack direction="column" gap="md" style={{ paddingTop: "12px" }}>
|
|
374
|
+
<Stack direction="row" gap="sm" align="center">
|
|
375
|
+
<Text weight="semibold" size="sm">Session Analytics</Text>
|
|
376
|
+
<Button variant="ghost" size="sm" onClick={refreshSummary}>
|
|
377
|
+
Refresh
|
|
378
|
+
</Button>
|
|
379
|
+
</Stack>
|
|
380
|
+
|
|
381
|
+
{!summary || summary.totalCalls === 0 ? (
|
|
382
|
+
<Text color="secondary" size="sm">
|
|
383
|
+
No tool calls recorded yet. Analytics will appear after tools are used.
|
|
384
|
+
</Text>
|
|
385
|
+
) : (
|
|
386
|
+
<Stack direction="column" gap="md">
|
|
387
|
+
{/* Summary Cards */}
|
|
388
|
+
<Stack direction="row" gap="sm" style={{ flexWrap: 'wrap' }}>
|
|
389
|
+
<Card variant="outlined" style={{ flex: '1 1 120px', padding: '12px' }}>
|
|
390
|
+
<Card.Body>
|
|
391
|
+
<Text size="2xs" color="tertiary">Total Calls</Text>
|
|
392
|
+
<Text size="lg" weight="bold">{summary.totalCalls}</Text>
|
|
393
|
+
</Card.Body>
|
|
394
|
+
</Card>
|
|
395
|
+
<Card variant="outlined" style={{ flex: '1 1 120px', padding: '12px' }}>
|
|
396
|
+
<Card.Body>
|
|
397
|
+
<Text size="2xs" color="tertiary">Unique Tools</Text>
|
|
398
|
+
<Text size="lg" weight="bold">{summary.uniqueTools}</Text>
|
|
399
|
+
</Card.Body>
|
|
400
|
+
</Card>
|
|
401
|
+
<Card variant="outlined" style={{ flex: '1 1 120px', padding: '12px' }}>
|
|
402
|
+
<Card.Body>
|
|
403
|
+
<Text size="2xs" color="tertiary">Error Rate</Text>
|
|
404
|
+
<Text size="lg" weight="bold">{(summary.errorRate * 100).toFixed(0)}%</Text>
|
|
405
|
+
</Card.Body>
|
|
406
|
+
</Card>
|
|
407
|
+
<Card variant="outlined" style={{ flex: '1 1 120px', padding: '12px' }}>
|
|
408
|
+
<Card.Body>
|
|
409
|
+
<Text size="2xs" color="tertiary">Duration</Text>
|
|
410
|
+
<Text size="lg" weight="bold">{formatDuration(summary.sessionDurationMs)}</Text>
|
|
411
|
+
</Card.Body>
|
|
412
|
+
</Card>
|
|
413
|
+
</Stack>
|
|
414
|
+
|
|
415
|
+
{/* Tool Breakdown */}
|
|
416
|
+
<Box>
|
|
417
|
+
<Text size="xs" weight="semibold" color="secondary" style={{ marginBottom: "8px" }}>
|
|
418
|
+
Tool Usage
|
|
419
|
+
</Text>
|
|
420
|
+
<Stack direction="column" gap="xs">
|
|
421
|
+
{Object.entries(summary.toolStats)
|
|
422
|
+
.sort(([, a], [, b]) => b.callCount - a.callCount)
|
|
423
|
+
.map(([name, stat]) => (
|
|
424
|
+
<Box
|
|
425
|
+
key={name}
|
|
426
|
+
padding="sm"
|
|
427
|
+
rounded="sm"
|
|
428
|
+
background="secondary"
|
|
429
|
+
>
|
|
430
|
+
<Stack direction="row" gap="sm" align="center" justify="between">
|
|
431
|
+
<Text size="xs" weight="medium" font="mono">
|
|
432
|
+
{name}
|
|
433
|
+
</Text>
|
|
434
|
+
<Stack direction="row" gap="xs">
|
|
435
|
+
<Badge size="sm" variant="info">{stat.callCount}x</Badge>
|
|
436
|
+
<Text size="2xs" color="tertiary">avg {stat.avgDuration}ms</Text>
|
|
437
|
+
{stat.errorCount > 0 && (
|
|
438
|
+
<Badge size="sm" variant="error">{stat.errorCount} err</Badge>
|
|
439
|
+
)}
|
|
440
|
+
</Stack>
|
|
441
|
+
</Stack>
|
|
442
|
+
<Text size="2xs" color="tertiary" style={{ marginTop: "2px" }}>
|
|
443
|
+
p50: {stat.p50}ms | p95: {stat.p95}ms
|
|
444
|
+
</Text>
|
|
445
|
+
</Box>
|
|
446
|
+
))}
|
|
447
|
+
</Stack>
|
|
448
|
+
</Box>
|
|
449
|
+
|
|
450
|
+
{/* Common Flows */}
|
|
451
|
+
{summary.commonFlows.length > 0 && (
|
|
452
|
+
<Box>
|
|
453
|
+
<Text size="xs" weight="semibold" color="secondary" style={{ marginBottom: "8px" }}>
|
|
454
|
+
Common Flows
|
|
455
|
+
</Text>
|
|
456
|
+
<Stack direction="column" gap="xs">
|
|
457
|
+
{summary.commonFlows.map((flow, i) => (
|
|
458
|
+
<Box
|
|
459
|
+
key={i}
|
|
460
|
+
padding="sm"
|
|
461
|
+
rounded="sm"
|
|
462
|
+
background="secondary"
|
|
463
|
+
>
|
|
464
|
+
<Stack direction="row" gap="xs" align="center">
|
|
465
|
+
<Text size="2xs" font="mono">
|
|
466
|
+
{flow.sequence.join(' → ')}
|
|
467
|
+
</Text>
|
|
468
|
+
<Badge size="sm" variant="info">{flow.count}x</Badge>
|
|
469
|
+
</Stack>
|
|
470
|
+
</Box>
|
|
471
|
+
))}
|
|
472
|
+
</Stack>
|
|
473
|
+
</Box>
|
|
474
|
+
)}
|
|
475
|
+
</Stack>
|
|
476
|
+
)}
|
|
477
|
+
</Stack>
|
|
478
|
+
</Tabs.Panel>
|
|
479
|
+
</Tabs>
|
|
480
|
+
|
|
481
|
+
{/* Protocol Status Footer */}
|
|
482
|
+
<Box
|
|
483
|
+
borderTop
|
|
484
|
+
style={{
|
|
485
|
+
marginTop: "auto",
|
|
486
|
+
paddingTop: "12px",
|
|
487
|
+
}}
|
|
488
|
+
>
|
|
489
|
+
<Stack direction="row" gap="sm" align="center">
|
|
490
|
+
<Badge variant="success" size="sm" dot>
|
|
491
|
+
{isShim ? "Connected" : "Native"}
|
|
492
|
+
</Badge>
|
|
493
|
+
<Text size="2xs" color="tertiary">
|
|
494
|
+
W3C Draft
|
|
495
|
+
</Text>
|
|
496
|
+
<Text size="2xs" color="tertiary">
|
|
497
|
+
{tools.length} tool{tools.length !== 1 ? "s" : ""}
|
|
498
|
+
</Text>
|
|
499
|
+
</Stack>
|
|
500
|
+
</Box>
|
|
501
|
+
</Stack>
|
|
502
|
+
);
|
|
503
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useEffect, type ReactNode } from "react";
|
|
2
|
+
import { WebMCPProvider, useWebMCPContext } from "@fragments-sdk/webmcp/react";
|
|
3
|
+
import { useFragmentTools } from "@fragments-sdk/webmcp/fragments";
|
|
4
|
+
import { useCompiledFragments } from "../hooks/useCompiledFragments.js";
|
|
5
|
+
import { useToolAnalytics } from "../webmcp/use-tool-analytics.js";
|
|
6
|
+
import { RuntimeToolsRegistrar } from "./RuntimeToolsRegistrar.js";
|
|
7
|
+
|
|
8
|
+
function FragmentToolsRegistrar() {
|
|
9
|
+
const { data } = useCompiledFragments();
|
|
10
|
+
const { shimRegistry } = useWebMCPContext();
|
|
11
|
+
const { count } = useFragmentTools(data);
|
|
12
|
+
|
|
13
|
+
// Expose registry on window for E2E testing (dev-only)
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (!shimRegistry || typeof window === "undefined") return;
|
|
16
|
+
(window as any).__WEBMCP_REGISTRY__ = shimRegistry;
|
|
17
|
+
(window as any).__WEBMCP_TOOL_COUNT__ = shimRegistry.tools.size;
|
|
18
|
+
|
|
19
|
+
const onToolChange = () => {
|
|
20
|
+
(window as any).__WEBMCP_TOOL_COUNT__ = shimRegistry.tools.size;
|
|
21
|
+
};
|
|
22
|
+
shimRegistry.addEventListener("toolchange", onToolChange);
|
|
23
|
+
return () => shimRegistry.removeEventListener("toolchange", onToolChange);
|
|
24
|
+
}, [shimRegistry]);
|
|
25
|
+
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function AnalyticsWiring() {
|
|
30
|
+
useToolAnalytics();
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface WebMCPIntegrationProps {
|
|
35
|
+
children: ReactNode;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function WebMCPIntegration({ children }: WebMCPIntegrationProps) {
|
|
39
|
+
return (
|
|
40
|
+
<WebMCPProvider devShim>
|
|
41
|
+
<FragmentToolsRegistrar />
|
|
42
|
+
<RuntimeToolsRegistrar />
|
|
43
|
+
<AnalyticsWiring />
|
|
44
|
+
{children}
|
|
45
|
+
</WebMCPProvider>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
import { useWebMCPContext } from "@fragments-sdk/webmcp/react";
|
|
3
|
+
import { Badge, Drawer } from "@fragments-sdk/ui";
|
|
4
|
+
import { WebMCPDevTools } from "./WebMCPDevTools.js";
|
|
5
|
+
|
|
6
|
+
export function WebMCPStatusIndicator() {
|
|
7
|
+
const { shimRegistry, isSupported, isShim } = useWebMCPContext();
|
|
8
|
+
const [toolCount, setToolCount] = useState(0);
|
|
9
|
+
const [drawerOpen, setDrawerOpen] = useState(false);
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!shimRegistry) return;
|
|
13
|
+
|
|
14
|
+
const update = () => setToolCount(shimRegistry.tools.size);
|
|
15
|
+
update();
|
|
16
|
+
|
|
17
|
+
shimRegistry.addEventListener("toolchange", update);
|
|
18
|
+
return () => shimRegistry.removeEventListener("toolchange", update);
|
|
19
|
+
}, [shimRegistry]);
|
|
20
|
+
|
|
21
|
+
if (!isSupported || toolCount === 0) return null;
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<>
|
|
25
|
+
<button
|
|
26
|
+
onClick={() => setDrawerOpen(true)}
|
|
27
|
+
title={`WebMCP: ${toolCount} tool${toolCount !== 1 ? "s" : ""} registered`}
|
|
28
|
+
data-testid="webmcp-status"
|
|
29
|
+
style={{
|
|
30
|
+
display: "inline-flex",
|
|
31
|
+
alignItems: "center",
|
|
32
|
+
background: "none",
|
|
33
|
+
border: "none",
|
|
34
|
+
padding: 0,
|
|
35
|
+
cursor: "pointer",
|
|
36
|
+
}}
|
|
37
|
+
>
|
|
38
|
+
<Badge variant="success" size="sm" dot>
|
|
39
|
+
WebMCP {toolCount}
|
|
40
|
+
</Badge>
|
|
41
|
+
</button>
|
|
42
|
+
|
|
43
|
+
<Drawer open={drawerOpen} onOpenChange={setDrawerOpen} modal={false}>
|
|
44
|
+
<Drawer.Content side="right" size="lg">
|
|
45
|
+
<Drawer.Close />
|
|
46
|
+
<Drawer.Header>
|
|
47
|
+
<Drawer.Title>WebMCP DevTools</Drawer.Title>
|
|
48
|
+
<Drawer.Description>
|
|
49
|
+
{toolCount} tool{toolCount !== 1 ? "s" : ""} registered via{" "}
|
|
50
|
+
{isShim ? "dev shim" : "native API"}
|
|
51
|
+
</Drawer.Description>
|
|
52
|
+
</Drawer.Header>
|
|
53
|
+
<Drawer.Body>
|
|
54
|
+
<WebMCPDevTools />
|
|
55
|
+
</Drawer.Body>
|
|
56
|
+
</Drawer.Content>
|
|
57
|
+
</Drawer>
|
|
58
|
+
</>
|
|
59
|
+
);
|
|
60
|
+
}
|