@fragments-sdk/cli 0.7.15 → 0.7.16
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/dist/bin.js +2 -2
- package/dist/init-DIZ6UNBL.js +806 -0
- package/dist/init-DIZ6UNBL.js.map +1 -0
- package/dist/{viewer-7I4WGVU3.js → viewer-QKIAPTPG.js} +67 -4
- package/dist/viewer-QKIAPTPG.js.map +1 -0
- package/package.json +3 -2
- package/src/commands/init-framework.ts +414 -0
- package/src/commands/init.ts +41 -1
- package/src/viewer/components/App.tsx +5 -0
- package/src/viewer/components/HealthDashboard.tsx +1 -1
- package/src/viewer/components/PropsTable.tsx +2 -2
- package/src/viewer/components/RuntimeToolsRegistrar.tsx +17 -0
- package/src/viewer/components/ViewerStateSync.tsx +52 -0
- package/src/viewer/components/WebMCPDevTools.tsx +509 -0
- package/src/viewer/components/WebMCPIntegration.tsx +47 -0
- package/src/viewer/components/WebMCPStatusIndicator.tsx +60 -0
- package/src/viewer/entry.tsx +6 -3
- package/src/viewer/hooks/useA11yService.ts +1 -135
- package/src/viewer/hooks/useCompiledFragments.ts +42 -0
- package/src/viewer/server.ts +58 -3
- package/src/viewer/vite-plugin.ts +18 -0
- package/src/viewer/webmcp/__tests__/analytics.test.ts +108 -0
- package/src/viewer/webmcp/analytics.ts +165 -0
- package/src/viewer/webmcp/index.ts +3 -0
- package/src/viewer/webmcp/posthog-bridge.ts +39 -0
- package/src/viewer/webmcp/runtime-tools.ts +152 -0
- package/src/viewer/webmcp/scan-utils.ts +135 -0
- package/src/viewer/webmcp/use-tool-analytics.ts +69 -0
- package/src/viewer/webmcp/viewer-state.ts +45 -0
- package/dist/init-V42FFMUJ.js +0 -498
- package/dist/init-V42FFMUJ.js.map +0 -1
- package/dist/viewer-7I4WGVU3.js.map +0 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { WebMCPTool, InputSchema } from '@fragments-sdk/webmcp';
|
|
2
|
+
import type { CompiledFragmentsFile } from '@fragments-sdk/context/types';
|
|
3
|
+
import { runAxeScan } from './scan-utils.js';
|
|
4
|
+
import { getViewerState } from './viewer-state.js';
|
|
5
|
+
|
|
6
|
+
export interface RuntimeToolsOptions {
|
|
7
|
+
compiledData?: CompiledFragmentsFile | null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function createRuntimeWebMCPTools(options: RuntimeToolsOptions = {}): WebMCPTool[] {
|
|
11
|
+
const tools: WebMCPTool[] = [];
|
|
12
|
+
|
|
13
|
+
// --- fragments_a11y ---
|
|
14
|
+
tools.push({
|
|
15
|
+
name: 'fragments_a11y',
|
|
16
|
+
description: 'Run an accessibility audit on the currently previewed component. Returns axe-core violations, a WCAG compliance score, and severity counts. Runs directly on the live DOM for accurate results.',
|
|
17
|
+
inputSchema: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
component: { type: 'string', description: 'Component name to audit' },
|
|
21
|
+
variant: { type: 'string', description: 'Specific variant to audit' },
|
|
22
|
+
standard: { type: 'string', enum: ['AA', 'AAA'], default: 'AA', description: 'WCAG compliance level' },
|
|
23
|
+
},
|
|
24
|
+
required: ['component'],
|
|
25
|
+
} as InputSchema,
|
|
26
|
+
annotations: { readOnlyHint: true },
|
|
27
|
+
execute: async (input) => {
|
|
28
|
+
const component = input.component as string;
|
|
29
|
+
const standard = (input.standard as string) || 'AA';
|
|
30
|
+
|
|
31
|
+
const result = await runAxeScan('[data-preview-container="true"]');
|
|
32
|
+
if (!result) {
|
|
33
|
+
return { error: true, message: 'No preview container found. Navigate to a component first.' };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const total = result.passes + result.violations.length + result.incomplete;
|
|
37
|
+
const wcagScore = total > 0 ? Math.round((result.passes / total) * 100) : 100;
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
component,
|
|
41
|
+
wcagScore,
|
|
42
|
+
standard,
|
|
43
|
+
violations: result.violations,
|
|
44
|
+
passCount: result.passes,
|
|
45
|
+
incompleteCount: result.incomplete,
|
|
46
|
+
counts: result.counts,
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// --- fragments_theme ---
|
|
52
|
+
tools.push({
|
|
53
|
+
name: 'fragments_theme',
|
|
54
|
+
description: 'Inspect the current runtime theme state. Returns the resolved theme (light/dark), seed configuration values, and computed CSS token values from the live DOM.',
|
|
55
|
+
inputSchema: {
|
|
56
|
+
type: 'object',
|
|
57
|
+
properties: {
|
|
58
|
+
category: { type: 'string', description: 'Filter tokens by category (e.g., "colors", "spacing")' },
|
|
59
|
+
search: { type: 'string', description: 'Search token names' },
|
|
60
|
+
includeComputed: { type: 'boolean', default: true, description: 'Include computed CSS values' },
|
|
61
|
+
},
|
|
62
|
+
} as InputSchema,
|
|
63
|
+
annotations: { readOnlyHint: true },
|
|
64
|
+
execute: async (input) => {
|
|
65
|
+
const category = input.category as string | undefined;
|
|
66
|
+
const search = input.search as string | undefined;
|
|
67
|
+
const includeComputed = input.includeComputed !== false;
|
|
68
|
+
|
|
69
|
+
const root = document.documentElement;
|
|
70
|
+
const computedStyle = getComputedStyle(root);
|
|
71
|
+
|
|
72
|
+
// Detect theme
|
|
73
|
+
const resolvedTheme = root.classList.contains('dark') ? 'dark' : 'light';
|
|
74
|
+
|
|
75
|
+
// Read seed values
|
|
76
|
+
const seed = {
|
|
77
|
+
brand: computedStyle.getPropertyValue('--fui-seed-brand').trim() || null,
|
|
78
|
+
neutral: computedStyle.getPropertyValue('--fui-seed-neutral').trim() || null,
|
|
79
|
+
radius: computedStyle.getPropertyValue('--fui-seed-radius').trim() || null,
|
|
80
|
+
density: computedStyle.getPropertyValue('--fui-seed-density').trim() || null,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Get tokens from compiled data
|
|
84
|
+
const compiledData = options.compiledData;
|
|
85
|
+
const tokenEntries: Array<{ name: string; value: string; computed?: string; category?: string }> = [];
|
|
86
|
+
|
|
87
|
+
if (compiledData?.tokens?.categories) {
|
|
88
|
+
for (const [cat, entries] of Object.entries(compiledData.tokens.categories)) {
|
|
89
|
+
if (category && cat.toLowerCase() !== category.toLowerCase()) continue;
|
|
90
|
+
|
|
91
|
+
for (const token of entries) {
|
|
92
|
+
if (search && !token.name.toLowerCase().includes(search.toLowerCase())) continue;
|
|
93
|
+
|
|
94
|
+
const entry: { name: string; value: string; computed?: string; category?: string } = {
|
|
95
|
+
name: token.name,
|
|
96
|
+
value: token.value,
|
|
97
|
+
category: cat,
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
if (includeComputed) {
|
|
101
|
+
entry.computed = computedStyle.getPropertyValue(token.name).trim() || token.value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
tokenEntries.push(entry);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
resolvedTheme,
|
|
111
|
+
seed,
|
|
112
|
+
tokens: tokenEntries,
|
|
113
|
+
tokenCount: tokenEntries.length,
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// --- fragments_context ---
|
|
119
|
+
tools.push({
|
|
120
|
+
name: 'fragments_context',
|
|
121
|
+
description: 'Get information about what the developer is currently viewing in the Fragments dev viewer. Returns the active component, variant, viewport settings, theme, and panel state.',
|
|
122
|
+
inputSchema: {
|
|
123
|
+
type: 'object',
|
|
124
|
+
properties: {
|
|
125
|
+
fields: {
|
|
126
|
+
type: 'array',
|
|
127
|
+
items: { type: 'string' },
|
|
128
|
+
description: 'Specific fields to return (e.g., ["currentComponent", "theme"]). Returns all fields if omitted.',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
} as InputSchema,
|
|
132
|
+
annotations: { readOnlyHint: true },
|
|
133
|
+
execute: async (input) => {
|
|
134
|
+
const state = getViewerState();
|
|
135
|
+
const fields = input.fields as string[] | undefined;
|
|
136
|
+
|
|
137
|
+
if (fields && fields.length > 0) {
|
|
138
|
+
const filtered: Record<string, unknown> = {};
|
|
139
|
+
for (const field of fields) {
|
|
140
|
+
if (field in state) {
|
|
141
|
+
filtered[field] = state[field as keyof typeof state];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return filtered;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return state;
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
return tools;
|
|
152
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Result } from 'axe-core';
|
|
2
|
+
import type { SerializedViolation } from '../types/a11y.js';
|
|
3
|
+
|
|
4
|
+
// Cache the axe-core module
|
|
5
|
+
let axeModule: typeof import('axe-core') | null = null;
|
|
6
|
+
|
|
7
|
+
export interface ScanResult {
|
|
8
|
+
violations: SerializedViolation[];
|
|
9
|
+
passes: number;
|
|
10
|
+
incomplete: number;
|
|
11
|
+
counts: {
|
|
12
|
+
critical: number;
|
|
13
|
+
serious: number;
|
|
14
|
+
moderate: number;
|
|
15
|
+
minor: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Convert axe-core Result to SerializedViolation
|
|
21
|
+
*/
|
|
22
|
+
export function serializeViolation(result: Result): SerializedViolation {
|
|
23
|
+
return {
|
|
24
|
+
id: result.id,
|
|
25
|
+
impact: result.impact || null,
|
|
26
|
+
description: result.description,
|
|
27
|
+
help: result.help,
|
|
28
|
+
helpUrl: result.helpUrl,
|
|
29
|
+
tags: result.tags,
|
|
30
|
+
nodes: result.nodes.map(node => ({
|
|
31
|
+
html: node.html,
|
|
32
|
+
target: node.target as string[],
|
|
33
|
+
failureSummary: node.failureSummary,
|
|
34
|
+
any: node.any?.map(check => ({
|
|
35
|
+
id: check.id,
|
|
36
|
+
data: check.data,
|
|
37
|
+
relatedNodes: check.relatedNodes?.map(rn => ({
|
|
38
|
+
html: rn.html,
|
|
39
|
+
target: rn.target as string[],
|
|
40
|
+
})),
|
|
41
|
+
impact: check.impact,
|
|
42
|
+
message: check.message,
|
|
43
|
+
})),
|
|
44
|
+
all: node.all?.map(check => ({
|
|
45
|
+
id: check.id,
|
|
46
|
+
data: check.data,
|
|
47
|
+
relatedNodes: check.relatedNodes?.map(rn => ({
|
|
48
|
+
html: rn.html,
|
|
49
|
+
target: rn.target as string[],
|
|
50
|
+
})),
|
|
51
|
+
impact: check.impact,
|
|
52
|
+
message: check.message,
|
|
53
|
+
})),
|
|
54
|
+
none: node.none?.map(check => ({
|
|
55
|
+
id: check.id,
|
|
56
|
+
data: check.data,
|
|
57
|
+
relatedNodes: check.relatedNodes?.map(rn => ({
|
|
58
|
+
html: rn.html,
|
|
59
|
+
target: rn.target as string[],
|
|
60
|
+
})),
|
|
61
|
+
impact: check.impact,
|
|
62
|
+
message: check.message,
|
|
63
|
+
})),
|
|
64
|
+
})),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Run axe-core scan on a target element
|
|
70
|
+
*/
|
|
71
|
+
export async function runAxeScan(targetSelector: string): Promise<ScanResult | null> {
|
|
72
|
+
// Load axe-core if not cached
|
|
73
|
+
if (!axeModule) {
|
|
74
|
+
axeModule = await import('axe-core');
|
|
75
|
+
}
|
|
76
|
+
// Handle both ESM default export and CommonJS module
|
|
77
|
+
const axe = (axeModule as { default?: typeof import('axe-core') }).default || axeModule;
|
|
78
|
+
|
|
79
|
+
const target = document.querySelector(targetSelector);
|
|
80
|
+
if (!target) {
|
|
81
|
+
console.warn(`[A11y] Target element not found: ${targetSelector}`);
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Configure axe-core
|
|
86
|
+
axe.configure({
|
|
87
|
+
rules: [
|
|
88
|
+
{ id: 'color-contrast', enabled: true },
|
|
89
|
+
{ id: 'image-alt', enabled: true },
|
|
90
|
+
{ id: 'button-name', enabled: true },
|
|
91
|
+
{ id: 'link-name', enabled: true },
|
|
92
|
+
{ id: 'label', enabled: true },
|
|
93
|
+
{ id: 'aria-valid-attr', enabled: true },
|
|
94
|
+
{ id: 'aria-valid-attr-value', enabled: true },
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// Run the scan
|
|
99
|
+
const results = await axe.run(target as HTMLElement, {
|
|
100
|
+
resultTypes: ['violations', 'passes', 'incomplete', 'inapplicable'],
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Count violations by severity
|
|
104
|
+
const counts = {
|
|
105
|
+
critical: 0,
|
|
106
|
+
serious: 0,
|
|
107
|
+
moderate: 0,
|
|
108
|
+
minor: 0,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
for (const violation of results.violations) {
|
|
112
|
+
switch (violation.impact) {
|
|
113
|
+
case 'critical':
|
|
114
|
+
counts.critical++;
|
|
115
|
+
break;
|
|
116
|
+
case 'serious':
|
|
117
|
+
counts.serious++;
|
|
118
|
+
break;
|
|
119
|
+
case 'moderate':
|
|
120
|
+
counts.moderate++;
|
|
121
|
+
break;
|
|
122
|
+
case 'minor':
|
|
123
|
+
default:
|
|
124
|
+
counts.minor++;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
violations: results.violations.map(serializeViolation),
|
|
131
|
+
passes: results.passes.length,
|
|
132
|
+
incomplete: results.incomplete.length,
|
|
133
|
+
counts,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, useCallback } from 'react';
|
|
2
|
+
import { useWebMCPContext } from '@fragments-sdk/webmcp/react';
|
|
3
|
+
import type { ToolCallEvent } from '@fragments-sdk/webmcp';
|
|
4
|
+
import { ToolAnalyticsCollector } from './analytics.js';
|
|
5
|
+
import { createPostHogBridge } from './posthog-bridge.js';
|
|
6
|
+
|
|
7
|
+
interface UseToolAnalyticsResult {
|
|
8
|
+
collector: ToolAnalyticsCollector;
|
|
9
|
+
summary: ReturnType<ToolAnalyticsCollector['toSummary']> | null;
|
|
10
|
+
refreshSummary: () => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function useToolAnalytics(): UseToolAnalyticsResult {
|
|
14
|
+
const { shimRegistry } = useWebMCPContext();
|
|
15
|
+
const collectorRef = useRef(new ToolAnalyticsCollector());
|
|
16
|
+
const [summary, setSummary] = useState<ReturnType<ToolAnalyticsCollector['toSummary']> | null>(null);
|
|
17
|
+
|
|
18
|
+
const refreshSummary = useCallback(() => {
|
|
19
|
+
setSummary(collectorRef.current.toSummary());
|
|
20
|
+
}, []);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (!shimRegistry) return;
|
|
24
|
+
|
|
25
|
+
const posthog = (window as any).posthog as { capture(event: string, properties?: Record<string, unknown>): void } | undefined;
|
|
26
|
+
const bridge = posthog
|
|
27
|
+
? createPostHogBridge(posthog, collectorRef.current)
|
|
28
|
+
: null;
|
|
29
|
+
|
|
30
|
+
const onToolCall = (event: ToolCallEvent) => {
|
|
31
|
+
collectorRef.current.record(event);
|
|
32
|
+
bridge?.onToolCall(event);
|
|
33
|
+
// Auto-refresh summary every 5 calls
|
|
34
|
+
const total = collectorRef.current.toSummary().totalCalls;
|
|
35
|
+
if (total % 5 === 0) {
|
|
36
|
+
setSummary(collectorRef.current.toSummary());
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
shimRegistry.addEventListener('toolcall', onToolCall);
|
|
41
|
+
|
|
42
|
+
// Fire session summary on page unload
|
|
43
|
+
const handleVisibilityChange = () => {
|
|
44
|
+
if (document.visibilityState === 'hidden') {
|
|
45
|
+
bridge?.onSessionEnd();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const handleBeforeUnload = () => {
|
|
50
|
+
bridge?.flush();
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
54
|
+
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
55
|
+
|
|
56
|
+
return () => {
|
|
57
|
+
shimRegistry.removeEventListener('toolcall', onToolCall);
|
|
58
|
+
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
59
|
+
window.removeEventListener('beforeunload', handleBeforeUnload);
|
|
60
|
+
bridge?.onSessionEnd();
|
|
61
|
+
};
|
|
62
|
+
}, [shimRegistry]);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
collector: collectorRef.current,
|
|
66
|
+
summary,
|
|
67
|
+
refreshSummary,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface ViewerState {
|
|
2
|
+
currentComponent: string | null;
|
|
3
|
+
currentVariant: string | null;
|
|
4
|
+
variantIndex: number;
|
|
5
|
+
totalVariants: number;
|
|
6
|
+
viewport: string;
|
|
7
|
+
zoom: number;
|
|
8
|
+
theme: string;
|
|
9
|
+
panels: {
|
|
10
|
+
activePanel: string;
|
|
11
|
+
panelOpen: boolean;
|
|
12
|
+
};
|
|
13
|
+
viewMode: {
|
|
14
|
+
matrixView: boolean;
|
|
15
|
+
multiViewport: boolean;
|
|
16
|
+
comparison: boolean;
|
|
17
|
+
};
|
|
18
|
+
designSystem: {
|
|
19
|
+
name: string;
|
|
20
|
+
componentCount: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DEFAULT_STATE: ViewerState = {
|
|
25
|
+
currentComponent: null,
|
|
26
|
+
currentVariant: null,
|
|
27
|
+
variantIndex: 0,
|
|
28
|
+
totalVariants: 0,
|
|
29
|
+
viewport: 'responsive',
|
|
30
|
+
zoom: 100,
|
|
31
|
+
theme: 'light',
|
|
32
|
+
panels: { activePanel: 'code', panelOpen: true },
|
|
33
|
+
viewMode: { matrixView: false, multiViewport: false, comparison: false },
|
|
34
|
+
designSystem: { name: 'Fragments', componentCount: 0 },
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
let currentState: ViewerState = { ...DEFAULT_STATE };
|
|
38
|
+
|
|
39
|
+
export function setViewerState(state: Partial<ViewerState>): void {
|
|
40
|
+
currentState = { ...currentState, ...state };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getViewerState(): ViewerState {
|
|
44
|
+
return currentState;
|
|
45
|
+
}
|