Open History to see the last commit
1import { For, Show, createMemo } from 'solid-js'
2import { useAIStore } from '../../store/ai-context'
3import { useStyles } from '../../styles/use-styles'
4import { getHookUnseenEventCount } from '../../store/hook-registry'
5import {
6 getHookDisplayName,
7 getHookIdentitySubtitle,
8 groupHooksByCategory,
9 isHookRunning,
10 visibleHooks,
11} from './hook-dashboard-model'
12import type { HookRecord } from '../../store/hook-registry'
13import type { Component } from 'solid-js'
14
15export const HookDashboard: Component = () => {
16 const { state, selectHook, selectConversation, clearHooks } = useAIStore()
17 const styles = useStyles()
18
19 const hooks = createMemo(() =>
20 visibleHooks(Object.values(state.hooks.hooks)).sort(
21 (a, b) => a.registeredAt - b.registeredAt,
22 ),
23 )
24 const hookGroups = createMemo(() => groupHooksByCategory(hooks()))
25
26 const activeCount = createMemo(() => hooks().length)
27 const toolCount = createMemo(() =>
28 hooks().reduce((count, hook) => count + hook.tools.length, 0),
29 )
30 const messageCount = createMemo(() =>
31 Object.values(state.conversations).reduce(
32 (count, conversation) => count + conversation.messages.length,
33 0,
34 ),
35 )
36 const runCount = createMemo(() => Object.keys(state.hooks.runs).length)
37 const totalTokens = createMemo(() =>
38 Object.values(state.conversations).reduce(
39 (count, conversation) => count + (conversation.usage?.totalTokens ?? 0),
40 0,
41 ),
42 )
43 const runningCount = createMemo(
44 () =>
45 Object.values(state.hooks.runs).filter(
46 (run) => run.status === 'started' || run.status === 'updated',
47 ).length,
48 )
49 const unseenUpdateCount = createMemo(() =>
50 hooks().reduce(
51 (count, hook) => count + getHookUnseenEventCount(state.hooks, hook.id),
52 0,
53 ),
54 )
55
56 const handleSelect = (hook: HookRecord) => {
57 selectHook(hook.id)
58 const conversationId = hook.id || hook.clientId || hook.threadId
59 if (conversationId && state.conversations[conversationId]) {
60 selectConversation(conversationId)
61 }
62 }
63
64 return (
65 <div
66 class={styles().hookDashboard.container}
67 data-testid="ai-devtools-hook-sidebar"
68 >
69 <div class={styles().hookDashboard.summary}>
70 <div
71 class={styles().hookDashboard.summaryItem}
72 data-testid="ai-devtools-sidebar-active-hooks"
73 >
74 <span class={styles().hookDashboard.summaryValue}>
75 {activeCount()}
76 </span>
77 <span class={styles().hookDashboard.summaryLabel}>active</span>
78 </div>
79 <div
80 class={styles().hookDashboard.summaryItem}
81 data-testid="ai-devtools-sidebar-tools"
82 >
83 <span class={styles().hookDashboard.summaryValue}>{toolCount()}</span>
84 <span class={styles().hookDashboard.summaryLabel}>tools</span>
85 </div>
86 <div
87 class={styles().hookDashboard.summaryItem}
88 data-testid="ai-devtools-sidebar-runs"
89 >
90 <span class={styles().hookDashboard.summaryValue}>{runCount()}</span>
91 <span class={styles().hookDashboard.summaryLabel}>runs</span>
92 </div>
93 <div
94 class={styles().hookDashboard.summaryItem}
95 data-testid="ai-devtools-sidebar-messages"
96 >
97 <span class={styles().hookDashboard.summaryValue}>
98 {messageCount()}
99 </span>
100 <span class={styles().hookDashboard.summaryLabel}>messages</span>
101 </div>
102 <div
103 class={styles().hookDashboard.summaryItem}
104 data-testid="ai-devtools-sidebar-tokens"
105 >
106 <span class={styles().hookDashboard.summaryValue}>
107 {totalTokens()}
108 </span>
109 <span class={styles().hookDashboard.summaryLabel}>tokens</span>
110 </div>
111 <div
112 class={styles().hookDashboard.summaryItem}
113 data-testid="ai-devtools-sidebar-running-hooks"
114 >
115 <span class={styles().hookDashboard.summaryValue}>
116 {runningCount()}
117 </span>
118 <span class={styles().hookDashboard.summaryLabel}>running</span>
119 </div>
120 <div class={styles().hookDashboard.summaryItem}>
121 <span class={styles().hookDashboard.summaryValue}>
122 {unseenUpdateCount()}
123 </span>
124 <span class={styles().hookDashboard.summaryLabel}>updates</span>
125 </div>
126 <button
127 class={styles().hookDashboard.clearButton}
128 type="button"
129 onClick={clearHooks}
130 disabled={hooks().length === 0}
131 >
132 Clear
133 </button>
134 </div>
135
136 <Show
137 when={hooks().length > 0}
138 fallback={
139 <div class={styles().hookDashboard.empty}>
140 No AI hooks registered yet.
141 </div>
142 }
143 >
144 <div class={styles().hookDashboard.list}>
145 <button
146 type="button"
147 data-testid="ai-devtools-dashboard-row"
148 class={`${styles().hookDashboard.row} ${
149 state.hooks.activeHookId === null
150 ? styles().hookDashboard.rowSelected
151 : ''
152 }`}
153 onClick={() => selectHook(null)}
154 >
155 <div class={styles().hookDashboard.rowMain}>
156 <div class={styles().hookDashboard.rowTitleLine}>
157 <span class={styles().hookDashboard.rowTitle}>Dashboard</span>
158 </div>
159 <span class={styles().hookDashboard.rowId}>All hooks</span>
160 </div>
161 <div class={styles().hookDashboard.rowMeta}>
162 <span class={styles().hookDashboard.countBadge}>
163 {hookGroups().length} categories
164 </span>
165 <span class={styles().hookDashboard.countBadge}>
166 {hooks().length} hooks
167 </span>
168 </div>
169 </button>
170 <For each={hookGroups()}>
171 {(group) => (
172 <section
173 class={styles().hookDashboard.categorySection}
174 data-testid="ai-devtools-hook-category"
175 data-category={group.id}
176 >
177 <div class={styles().hookDashboard.categoryHeader}>
178 <span class={styles().hookDashboard.categoryLabel}>
179 {group.label}
180 </span>
181 <span class={styles().hookDashboard.categoryCount}>
182 {group.hooks.length}
183 </span>
184 </div>
185 <div class={styles().hookDashboard.categoryRows}>
186 <For each={group.hooks}>
187 {(hook) => {
188 const isSelected = createMemo(
189 () => state.hooks.activeHookId === hook.id,
190 )
191 const unseenCount = createMemo(() =>
192 getHookUnseenEventCount(state.hooks, hook.id),
193 )
194 const running = createMemo(() =>
195 isHookRunning(hook, state.hooks.runs),
196 )
197 const hasUnseenUpdates = createMemo(
198 () => unseenCount() > 0 && !isSelected(),
199 )
200 const hasBackgroundActivity = createMemo(
201 () => running() && !isSelected(),
202 )
203
204 return (
205 <button
206 type="button"
207 data-testid="ai-devtools-hook-row"
208 data-hook-id={hook.id}
209 data-hook-name={hook.hookName}
210 data-display-name={getHookDisplayName(hook)}
211 class={`${styles().hookDashboard.row} ${
212 isSelected()
213 ? styles().hookDashboard.rowSelected
214 : ''
215 } ${
216 hasBackgroundActivity()
217 ? styles().hookDashboard.rowLive
218 : ''
219 } ${
220 hasUnseenUpdates()
221 ? styles().hookDashboard.rowUpdating
222 : ''
223 }`}
224 onClick={() => handleSelect(hook)}
225 >
226 <div class={styles().hookDashboard.rowMain}>
227 <div class={styles().hookDashboard.rowTitleLine}>
228 <span
229 class={styles().hookDashboard.rowTitle}
230 data-testid="ai-devtools-hook-display-name"
231 >
232 {getHookDisplayName(hook)}
233 </span>
234 <Show when={hasBackgroundActivity()}>
235 <span
236 class={styles().hookDashboard.liveIndicator}
237 aria-label="Hook is receiving updates"
238 />
239 </Show>
240 <Show when={hasUnseenUpdates()}>
241 <span
242 class={styles().hookDashboard.updateBadge}
243 >
244 {unseenCount()} new
245 </span>
246 </Show>
247 </div>
248 <span class={styles().hookDashboard.rowId}>
249 {getHookIdentitySubtitle(hook)}
250 </span>
251 </div>
252 <div class={styles().hookDashboard.rowMeta}>
253 <span
254 class={`${styles().hookDashboard.lifecycleBadge} ${
255 hook.lifecycle === 'streaming'
256 ? styles().hookDashboard.lifecycleStreaming
257 : hook.lifecycle === 'errored'
258 ? styles().hookDashboard.lifecycleErrored
259 : ''
260 }`}
261 >
262 {hook.lifecycle}
263 </span>
264 <Show when={hook.outputKind}>
265 <span class={styles().hookDashboard.kindBadge}>
266 {hook.outputKind}
267 </span>
268 </Show>
269 <Show when={hook.tools.length > 0}>
270 <span class={styles().hookDashboard.countBadge}>
271 {hook.tools.length} tools
272 </span>
273 </Show>
274 <Show when={hook.runIds.length > 0}>
275 <span class={styles().hookDashboard.countBadge}>
276 {hook.runIds.length} runs
277 </span>
278 </Show>
279 </div>
280 </button>
281 )
282 }}
283 </For>
284 </div>
285 </section>
286 )}
287 </For>
288 </div>
289 </Show>
290 </div>
291 )
292}