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 that is the single
68 * app-chosen scope (and, after the client prefers `threadId` over deprecated
69 * `id`, often equals the registry key too). Fall back to the registry `id`
70 * (devtools hook id / legacy client id) for ephemeral hooks that never named
71 * a thread.
72 */
73export function getHookIdentityLabel(hook: HookRecord): string {
74 return hook.threadId ?? hook.id
75}
76
77/**
78 * Secondary subtitle under the title: either just the identity, or
79 * `hookName - identity` when a custom display name is set so the technical
80 * name stays visible.
81 */
82export function getHookIdentitySubtitle(hook: HookRecord): string {
83 const identity = getHookIdentityLabel(hook)
84 if (hook.displayName) {
85 return `${hook.hookName} - ${identity}`
86 }
87 return identity
88}
89
90export function groupHooksByCategory(
91 hooks: Array<HookRecord>,
92): Array<HookCategoryGroup> {
93 const groups = new Map<HookCategoryId, Array<HookRecord>>()
94
95 for (const hook of hooks) {
96 const category = inferHookCategory(hook)
97 groups.set(category, [...(groups.get(category) ?? []), hook])
98 }
99
100 return hookCategoryOrder.flatMap((id) => {
101 const categoryHooks = groups.get(id)
102 if (!categoryHooks?.length) return []
103
104 return [
105 {
106 id,
107 label: hookCategoryLabels[id],
108 hooks: [...categoryHooks].sort(
109 (a, b) => a.registeredAt - b.registeredAt,
110 ),
111 },
112 ]
113 })
114}
115
116export function createHookDashboardSummary(
117 hooks: Array<HookRecord>,
118 runs: Record<string, RunRecord>,
119): HookDashboardSummary {
120 return {
121 total: hooks.length,
122 active: hooks.length,
123 running: hooks.filter((hook) => isHookRunning(hook, runs)).length,
124 categories: groupHooksByCategory(hooks).length,
125 tools: hooks.reduce((count, hook) => count + hook.tools.length, 0),
126 runs: Object.keys(runs).length,
127 }
128}
129
130export function isHookRunning(
131 hook: HookRecord,
132 runs: Record<string, RunRecord>,
133): boolean {
134 if (hook.lifecycle === 'streaming') {
135 return true
136 }
137
138 return hook.runIds.some((runId) => {
139 const run = runs[runId]
140 return run ? isRunActive(run) : false
141 })
142}
143
144function isRunActive(run: RunRecord): boolean {
145 return (
146 run.status === 'created' ||
147 run.status === 'started' ||
148 run.status === 'updated'
149 )
150}
151
152function inferHookCategory(hook: HookRecord): HookCategoryId {
153 const hookName = hook.hookName.toLowerCase()
154
155 if (hookName.includes('speech')) return 'speech'
156 if (hookName.includes('transcription')) return 'transcription'
157 if (hookName.includes('summarize')) return 'summarize'
158
159 return hook.outputKind ?? 'other'
160}