Repository navigation
Expand file tree
/
Copy pathRuntimeDebugPanel.tsx
More file actions
137 lines (124 loc) · 4.09 KB
/
Copy pathRuntimeDebugPanel.tsx
File metadata and controls
137 lines (124 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { useEffect, useState } from "react";
import { extractAIAttributes } from "@domglyph/ai-contract";
import { Box, Stack, Text } from "@domglyph/primitives";
type RuntimeDebugPanelProps = {
showAIView: boolean;
};
type RuntimeSnapshot = {
screenContext: unknown;
actions: readonly unknown[];
events: readonly unknown[];
formSchema: unknown;
metadataSummary: Array<{ id: string; role: string; attrs: Record<string, string> }>;
};
const emptySnapshot: RuntimeSnapshot = {
screenContext: null,
actions: [],
events: [],
formSchema: null,
metadataSummary: []
};
export function RuntimeDebugPanel({ showAIView }: RuntimeDebugPanelProps) {
const [snapshot, setSnapshot] = useState<RuntimeSnapshot>(emptySnapshot);
useEffect(() => {
const updateSnapshot = () => {
const runtime = window.__DOMGLYPH__ ?? null;
const metadataSummary = Array.from(document.querySelectorAll<HTMLElement>("[data-ai-id]"))
.slice(0, 14)
.map((element) => {
const attrs = extractAIAttributes(element);
return {
id: attrs["data-ai-id"] ?? "(missing)",
role: attrs["data-ai-role"] ?? "(no role)",
attrs: Object.fromEntries(
Object.entries(attrs).filter(([, value]) => value !== undefined)
) as Record<string, string>
};
});
setSnapshot({
screenContext: runtime?.getScreenContext() ?? null,
actions: runtime?.getAvailableActions() ?? [],
events: runtime?.getRecentEvents() ?? [],
formSchema: runtime?.getFormSchema("customer-profile-form") ?? null,
metadataSummary
});
};
updateSnapshot();
const intervalId = window.setInterval(updateSnapshot, 600);
return () => window.clearInterval(intervalId);
}, []);
return (
<aside className="runtime-panel">
<Stack gap="var(--demo-space-4)">
<Stack gap="var(--demo-space-2)">
<Text as="p" className="eyebrow">
Runtime panel
</Text>
<Text as="h2" className="section-title">
What DOMglyph sees
</Text>
<Text as="p" className="section-copy">
Reads the same browser runtime API that an external agent or developer would inspect.
</Text>
</Stack>
<RuntimeCard
title="Screen context"
body="Current screen id, sections, visible actions, and entity types."
data={snapshot.screenContext}
/>
<RuntimeCard
title="Available actions"
body="Action metadata discovered from rendered buttons and dialog controls."
data={snapshot.actions}
/>
<RuntimeCard
title="Recent events"
body="Click, form submit, field update, and banner events observed by the runtime."
data={snapshot.events}
/>
<RuntimeCard
title="Form schema"
body='Extracted from the profile form using getFormSchema("customer-profile-form").'
data={snapshot.formSchema}
/>
<RuntimeCard
title={showAIView ? "AI metadata summary" : "Metadata summary"}
body="Sample of discovered data-ai-* attributes currently mounted in the DOM."
data={snapshot.metadataSummary}
/>
<Box className="console-panel">
<Text as="h3" className="console-title">
Console shortcuts
</Text>
<pre className="code-block">{`window.__DOMGLYPH__.getScreenContext()
window.__DOMGLYPH__.getAvailableActions()
window.__DOMGLYPH__.getRecentEvents()
window.__DOMGLYPH__.getFormSchema("customer-profile-form")`}</pre>
</Box>
</Stack>
</aside>
);
}
function RuntimeCard({
title,
body,
data
}: {
title: string;
body: string;
data: unknown;
}) {
return (
<Box className="runtime-card">
<Stack gap="var(--demo-space-2)">
<Text as="h3" className="runtime-title">
{title}
</Text>
<Text as="p" className="runtime-copy">
{body}
</Text>
<pre className="code-block">{JSON.stringify(data, null, 2)}</pre>
</Stack>
</Box>
);
}