Open History to see the last commit
1import type { HookRecord, RunRecord } from '../../store/hook-registry'
2
3export type HookCategoryId =
4 | 'chat'
5 | 'structured'
6 | 'image'
7 | 'audio'
8 | 'video'
9 | 'speech'
10 | 'transcription'
11 | 'summarize'
12 | 'text'
13 | 'other'
14
15export interface HookCategoryGroup {
16 id: HookCategoryId
17 label: string
18 hooks: Array<HookRecord>
19}
20
21export interface HookDashboardSummary {
22 total: number
23 active: number
24 running: number
25 categories: number
26 tools: number
27 runs: number
28}
29
30const hookCategoryOrder: Array<HookCategoryId> = [
31 'chat',
32 'structured',
33 'image',
34 'audio',
35 'video',
36 'speech',
37 'transcription',
38 'summarize',
39 'text',
40 'other',
41]
42
43const hookCategoryLabels: Record<HookCategoryId, string> = {
44 chat: 'Chat',
45 structured: 'Structured',
46 image: 'Image',
47 audio: 'Audio',
48 video: 'Video',
49 speech: 'Speech',
50 transcription: 'Transcription',
51 summarize: 'Summarize',
52 text: 'Text',
53 other: 'Other',
54}
55
56export function visibleHooks(hooks: Array<HookRecord>): Array<HookRecord> {
57 return hooks
58}
59
60export function getHookDisplayName(hook: HookRecord): string {
61 return hook.displayName ?? hook.hookName
62}
63
64/**
65 * Stable identity string shown under the hook title.
66 *
67 * Prefer `threadId` when present. For generation hooks it is the single
68 * scope that the app chooses, and it is often the registry key too. Fall back
69 * to the registry `id` (the devtools hook id) for short-lived hooks that do
70 * not name a thread.
71 */
72export function getHookIdentityLabel(hook: HookRecord): string {
73 return hook.threadId ?? hook.id
74}
75
76/**
77 * Secondary subtitle under the title: either just the identity, or
78 * `hookName - identity` when a custom display name is set so the technical
79 * name stays visible.
80 */
81export function getHookIdentitySubtitle(hook: HookRecord): string {
82 const identity = getHookIdentityLabel(hook)
83 if (hook.displayName) {
84 return `${hook.hookName} - ${identity}`
85 }
86 return identity
87}
88
89export function groupHooksByCategory(
90 hooks: Array<HookRecord>,
91): Array<HookCategoryGroup> {
92 const groups = new Map<HookCategoryId, Array<HookRecord>>()
93
94 for (const hook of hooks) {
95 const category = inferHookCategory(hook)
96 groups.set(category, [...(groups.get(category) ?? []), hook])
97 }
98
99 return hookCategoryOrder.flatMap((id) => {
100 const categoryHooks = groups.get(id)
101 if (!categoryHooks?.length) return []
102
103 return [
104 {
105 id,
106 label: hookCategoryLabels[id],
107 hooks: [...categoryHooks].sort(
108 (a, b) => a.registeredAt - b.registeredAt,
109 ),
110 },
111 ]
112 })
113}
114
115export function createHookDashboardSummary(
116 hooks: Array<HookRecord>,
117 runs: Record<string, RunRecord>,
118): HookDashboardSummary {
119 return {
120 total: hooks.length,
121 active: hooks.length,
122 running: hooks.filter((hook) => isHookRunning(hook, runs)).length,
123 categories: groupHooksByCategory(hooks).length,
124 tools: hooks.reduce((count, hook) => count + hook.tools.length, 0),
125 runs: Object.keys(runs).length,
126 }
127}
128
129export function isHookRunning(
130 hook: HookRecord,
131 runs: Record<string, RunRecord>,
132): boolean {
133 if (hook.lifecycle === 'streaming') {
134 return true
135 }
136
137 return hook.runIds.some((runId) => {
138 const run = runs[runId]
139 return run ? isRunActive(run) : false
140 })
141}
142
143function isRunActive(run: RunRecord): boolean {
144 return (
145 run.status === 'created' ||
146 run.status === 'started' ||
147 run.status === 'updated'
148 )
149}
150
151function inferHookCategory(hook: HookRecord): HookCategoryId {
152 const hookName = hook.hookName.toLowerCase()
153
154 if (hookName.includes('speech')) return 'speech'
155 if (hookName.includes('transcription')) return 'transcription'
156 if (hookName.includes('summarize')) return 'summarize'
157
158 return hook.outputKind ?? 'other'
159}