@kabmo-labs/kabmo 0.13.0 → 0.14.0

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.
Files changed (133) hide show
  1. package/dist/client/index.html +1 -1
  2. package/dist/client/index.js +319 -319
  3. package/dist/client/static/js/{index-vzb1qz51.js → index-nfsg1jpt.js} +779 -779
  4. package/dist/client/static/js/index-tsgy3gd1.css +1 -0
  5. package/package.json +1 -1
  6. package/src/client/components/common/batch-selection/BatchActionBar.test.ts +42 -5
  7. package/src/client/components/common/batch-selection/BatchActionBar.tsx +57 -14
  8. package/src/client/components/common/code-surface/extensions.ts +7 -1
  9. package/src/client/components/common/code-surface/fold-placeholder.ts +43 -0
  10. package/src/client/components/common/session-row/StatusSlot.tsx +133 -0
  11. package/src/client/components/common/session-row/TrailingSlot.tsx +84 -0
  12. package/src/client/components/common/session-row/index.tsx +205 -0
  13. package/src/client/components/common/session-row/session-row.test.tsx +144 -0
  14. package/src/client/components/layout/session-sidebar/ClassicView.tsx +121 -0
  15. package/src/client/components/layout/session-sidebar/SessionsView.tsx +99 -112
  16. package/src/client/components/layout/session-sidebar/Toolbar.tsx +2 -2
  17. package/src/client/components/layout/session-sidebar/modern/ActionRow.tsx +149 -0
  18. package/src/client/components/layout/session-sidebar/modern/FilterChips.tsx +150 -0
  19. package/src/client/components/layout/session-sidebar/modern/SearchField.tsx +75 -0
  20. package/src/client/components/layout/session-sidebar/modern/Skeleton.tsx +55 -0
  21. package/src/client/components/layout/session-sidebar/modern/WorkspaceHeader.tsx +184 -0
  22. package/src/client/components/layout/session-sidebar/modern/WorkspaceSection.tsx +333 -0
  23. package/src/client/components/layout/session-sidebar/modern/index.tsx +147 -0
  24. package/src/client/components/layout/session-sidebar/variant-props.ts +73 -0
  25. package/src/client/components/mobile/SessionSidebar.tsx +137 -146
  26. package/src/client/components/mobile/mobile-session-sidebar/ClassicDrawer.tsx +140 -0
  27. package/src/client/components/mobile/mobile-session-sidebar/Toolbar.tsx +3 -3
  28. package/src/client/components/mobile/mobile-session-sidebar/use-drawer.ts +104 -0
  29. package/src/client/components/settings/Modal.tsx +5 -27
  30. package/src/client/components/settings/Sidebar.tsx +1 -1
  31. package/src/client/components/settings/apply-side-effects.test.ts +102 -0
  32. package/src/client/components/settings/apply-side-effects.ts +90 -0
  33. package/src/client/components/settings/categories/AppearanceSettings.tsx +11 -1
  34. package/src/client/components/settings/categories/ChatSettings.test.tsx +121 -0
  35. package/src/client/components/settings/categories/ChatSettings.tsx +80 -15
  36. package/src/client/components/settings/categories/appearance-settings/SidebarStyleSection.tsx +157 -0
  37. package/src/client/components/settings/categories/chat-settings/ComposerStyleSection.tsx +177 -0
  38. package/src/client/components/settings/categories/chat-settings/LayoutSection.tsx +1 -1
  39. package/src/client/components/settings/categories/chat-settings/SidebarList.tsx +73 -0
  40. package/src/client/components/settings/categories/chat-settings/ToolRenderSection.tsx +144 -0
  41. package/src/client/components/settings/categories/chat-settings/groups.test.ts +42 -0
  42. package/src/client/components/settings/categories/chat-settings/groups.ts +81 -0
  43. package/src/client/components/settings/categories/usage-settings/usage-and-picker.test.ts +1 -68
  44. package/src/client/components/workspace/chat-timeline/ComposerDock.tsx +7 -0
  45. package/src/client/components/workspace/chat-timeline/EmptyWorkspacePrompt.tsx +3 -0
  46. package/src/client/components/workspace/chat-timeline/MessageItem.tsx +14 -5
  47. package/src/client/components/workspace/chat-timeline/MessageList.tsx +90 -41
  48. package/src/client/components/workspace/chat-timeline/SystemNotice.tsx +13 -6
  49. package/src/client/components/workspace/chat-timeline/ThinkingSection.tsx +108 -67
  50. package/src/client/components/workspace/chat-timeline/ToolCallingSection.test.ts +28 -8
  51. package/src/client/components/workspace/chat-timeline/ToolCallingSection.tsx +48 -4
  52. package/src/client/components/workspace/chat-timeline/TurnRail.tsx +64 -0
  53. package/src/client/components/workspace/chat-timeline/block-chrome.test.tsx +158 -0
  54. package/src/client/components/workspace/chat-timeline/chat-input/AttachmentToolbar.tsx +1 -1
  55. package/src/client/components/workspace/chat-timeline/chat-input/ComposerNotice.tsx +50 -0
  56. package/src/client/components/workspace/chat-timeline/chat-input/ComposerRows.tsx +178 -0
  57. package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.tsx +1 -1
  58. package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +102 -122
  59. package/src/client/components/workspace/chat-timeline/chat-input/props.ts +7 -0
  60. package/src/client/components/workspace/chat-timeline/chat-input/styles/index.tsx +111 -0
  61. package/src/client/components/workspace/chat-timeline/chat-input/styles.test.tsx +181 -0
  62. package/src/client/components/workspace/chat-timeline/index.tsx +3 -0
  63. package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +1 -0
  64. package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/approval-gate.test.tsx +31 -0
  65. package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/index.tsx +1 -1
  66. package/src/client/components/workspace/chat-timeline/tool-renderers/index.tsx +2 -2
  67. package/src/client/components/workspace/chat-timeline/tool-renderers/plugin-renderers.ts +6 -1
  68. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ToolBody.tsx +98 -0
  69. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/ToolCardShell.tsx +3 -1
  70. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/status-glyph.tsx +56 -0
  71. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/toggle-label.ts +19 -0
  72. package/src/client/components/workspace/chat-timeline/tool-renderers/shared/tool-view.ts +132 -0
  73. package/src/client/components/workspace/chat-timeline/{ToolCallCard.plugin.test.ts → tool-renderers/styles/Card.plugin.test.ts} +3 -3
  74. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Card.tsx +60 -0
  75. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Compact.tsx +89 -0
  76. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Minimal.tsx +72 -0
  77. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/Trace.tsx +84 -0
  78. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/props.ts +22 -0
  79. package/src/client/components/workspace/chat-timeline/tool-renderers/styles/styles.test.tsx +153 -0
  80. package/src/client/components/workspace/diff-panel/folding.ts +6 -1
  81. package/src/client/components/workspace/model-dropdown/Item.plugin.test.ts +86 -12
  82. package/src/client/components/workspace/model-dropdown/Item.tsx +56 -19
  83. package/src/client/components/workspace/model-dropdown/Panel.tsx +21 -13
  84. package/src/client/components/workspace/model-dropdown/Section.test.tsx +114 -4
  85. package/src/client/components/workspace/model-dropdown/Section.tsx +21 -9
  86. package/src/client/components/workspace/model-dropdown/groups.test.ts +164 -1
  87. package/src/client/components/workspace/model-dropdown/groups.ts +94 -17
  88. package/src/client/components/workspace/model-dropdown/index.tsx +27 -19
  89. package/src/client/data/settings/omp-schema.json +151 -6
  90. package/src/client/hooks/chat/composer/style.ts +66 -0
  91. package/src/client/hooks/chat/timeline/tool-render-mode.ts +65 -0
  92. package/src/client/hooks/settings/providers/login.ts +11 -2
  93. package/src/client/hooks/ui/window-event.ts +20 -4
  94. package/src/client/hooks/workspace/batch-archive-run.ts +47 -0
  95. package/src/client/hooks/workspace/batch-archive.test.ts +59 -0
  96. package/src/client/hooks/workspace/batch-archive.ts +78 -0
  97. package/src/client/hooks/workspace/sidebar-batch.ts +95 -0
  98. package/src/client/hooks/workspace/sidebar-style.ts +66 -0
  99. package/src/client/tailwind.css +111 -0
  100. package/src/server/index.ts +10 -134
  101. package/src/server/lib/lifecycle/after-listen.ts +178 -0
  102. package/src/server/lib/lifecycle/rejection-guard.test.ts +79 -0
  103. package/src/server/lib/lifecycle/rejection-guard.ts +74 -0
  104. package/src/server/lib/lifecycle/tls.test.ts +7 -7
  105. package/src/server/lib/omp/rpc/frame-fold-frames.test.ts +131 -0
  106. package/src/server/lib/omp/rpc/frame-fold-host.test-util.ts +46 -0
  107. package/src/server/lib/omp/rpc/frame-fold.test.ts +1 -100
  108. package/src/server/lib/omp/rpc/frame-fold.ts +13 -0
  109. package/src/server/lib/plugins/background.server.ts +17 -1
  110. package/src/server/lib/plugins/background.test.ts +33 -0
  111. package/src/server/routes/sessions/batch-archive.test.ts +113 -0
  112. package/src/server/routes/sessions/batch-archive.ts +80 -0
  113. package/src/server/routes/sessions/index.ts +4 -0
  114. package/src/shared/lib/chat/composer/style.test.ts +126 -0
  115. package/src/shared/lib/chat/composer/style.ts +171 -0
  116. package/src/shared/lib/chat/composer/tui-only.test.ts +3 -0
  117. package/src/shared/lib/chat/composer/tui-only.ts +5 -0
  118. package/src/shared/lib/chat/timeline/block-chrome.ts +129 -0
  119. package/src/shared/lib/chat/timeline/tool-render-mode.test.ts +51 -0
  120. package/src/shared/lib/chat/timeline/tool-render-mode.ts +74 -0
  121. package/src/shared/lib/chat/timeline/trace-groups.test.ts +120 -0
  122. package/src/shared/lib/chat/timeline/trace-groups.ts +68 -0
  123. package/src/shared/lib/settings/defaults.ts +6 -0
  124. package/src/shared/lib/workspace/session-focus.test.ts +93 -0
  125. package/src/shared/lib/workspace/session-focus.ts +71 -0
  126. package/src/shared/lib/workspace/sidebar-filter-row.test.ts +73 -0
  127. package/src/shared/lib/workspace/sidebar-filter-row.ts +86 -0
  128. package/src/shared/lib/workspace/sidebar-style.test.ts +58 -0
  129. package/src/shared/lib/workspace/sidebar-style.ts +86 -0
  130. package/src/shared/types/omp/agent.ts +6 -0
  131. package/src/shared/types/settings/state.ts +31 -0
  132. package/dist/client/static/js/index-68w8h33q.css +0 -1
  133. package/src/client/components/workspace/chat-timeline/ToolCallCard.tsx +0 -194
@@ -22,6 +22,7 @@ import { Window } from 'happy-dom';
22
22
  import { h, render } from 'preact';
23
23
  import { act } from 'preact/test-utils';
24
24
  import { ModelDropdownSection } from '@/client/components/workspace/model-dropdown/Section';
25
+ import { buildPickerGroups } from '@/client/components/workspace/model-dropdown/groups';
25
26
  import { modelKey } from '@/shared/lib/models/identity';
26
27
  import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
27
28
  import type { AIModelOption } from '@/shared/types';
@@ -42,7 +43,7 @@ afterEach(() => {
42
43
  restoreDomGlobals();
43
44
  });
44
45
 
45
- function mountSection(): void {
46
+ function mountSection(overrides: Partial<Parameters<typeof ModelDropdownSection>[0]> = {}): void {
46
47
  act(() => {
47
48
  render(
48
49
  h(ModelDropdownSection, {
@@ -53,11 +54,13 @@ function mountSection(): void {
53
54
  onToggleCollapse: () => {},
54
55
  selectedModelKey: '',
55
56
  modelIndex: {},
57
+ ambiguousNames: new Set<string>(),
56
58
  focusedIndex: -1,
57
59
  onSelect: () => {},
58
60
  onToggleFavorite: () => {},
59
61
  onCycleThinking: () => {},
60
62
  onHoverItem: () => {},
63
+ ...overrides,
61
64
  }),
62
65
  container,
63
66
  );
@@ -120,12 +123,119 @@ describe('model picker section header columns', () => {
120
123
  expect(chevronSlot.querySelector('svg')).not.toBeNull();
121
124
  });
122
125
 
123
- test('a row carries its composite key, which the specs card anchors to', () => {
126
+ test('a row carries its row id, which the specs card anchors to', () => {
124
127
  mountSection();
125
128
 
126
129
  // Without this the card has no row to sit beside and falls back to the
127
- // panel's own top — the drift that made it describe a model rows away.
130
+ // panel's own top — the drift that made it describe a model rows away. The
131
+ // anchor is the ROW, not the model: one model is drawn twice.
128
132
  const row = container.querySelector('[role="button"]') as HTMLElement;
129
- expect(row.getAttribute('data-model-key')).toBe(modelKey(MODELS[0]));
133
+ expect(row.getAttribute('data-picker-row')).toBe(`DEEPSEEK::${modelKey(MODELS[0])}`);
134
+ });
135
+ });
136
+
137
+ describe('model picker rail rows and the provider tag', () => {
138
+ const SHARED: AIModelOption[] = [
139
+ { id: 'v4-pro', name: 'DeepSeek V4 Pro', provider: 'deepseek' },
140
+ { id: 'v4-pro', name: 'DeepSeek V4 Pro', provider: 'kenari' },
141
+ ];
142
+
143
+ /** The provider tag is the only `span` whose text is a bare provider slug. */
144
+ function tags(): HTMLElement[] {
145
+ return Array.from(container.querySelectorAll('[role="button"] span')).filter(
146
+ (span) => (span.getAttribute('title') ?? '').startsWith('Provider: '),
147
+ ) as HTMLElement[];
148
+ }
149
+
150
+ test('a rail row tags a name that two providers serve', () => {
151
+ mountSection({ id: 'favorites', title: 'FAVORITES', iconType: 'star', models: SHARED, ambiguousNames: new Set(['deepseek v4 pro']) });
152
+
153
+ const rendered = tags().map((tag) => tag.textContent);
154
+ expect(rendered).toEqual(['deepseek', 'kenari']);
155
+ });
156
+
157
+ test('a rail row stays untagged when the name is unique to one provider', () => {
158
+ mountSection({ id: 'recent', title: 'RECENT', iconType: 'recent', ambiguousNames: new Set() });
159
+
160
+ expect(tags()).toEqual([]);
161
+ });
162
+
163
+ test('a provider section never tags a row — its header already says so', () => {
164
+ // The same rows, drawn under the DEEPSEEK heading: a tag here would repeat
165
+ // the section title on every line.
166
+ mountSection({ models: SHARED, ambiguousNames: new Set(['deepseek v4 pro']) });
167
+
168
+ expect(tags()).toEqual([]);
169
+ });
170
+
171
+ test('the tag never shrinks or wraps, so `kenari` and `kenari-jatis` stay distinguishable', () => {
172
+ mountSection({
173
+ id: 'recent',
174
+ title: 'RECENT',
175
+ iconType: 'recent',
176
+ models: [{ id: 'x', name: 'Shared', provider: 'kenari-jatis' }],
177
+ ambiguousNames: new Set(['shared']),
178
+ });
179
+
180
+ const [tag] = tags();
181
+ expect(tag?.className).toContain('shrink-0');
182
+ expect(tag?.className).toContain('whitespace-nowrap');
183
+ // The name is the only cell that gives up width.
184
+ const name = container.querySelector('[role="button"] span[title^="Shared"]') as HTMLElement;
185
+ expect(name.className).toContain('truncate');
186
+ expect(name.getAttribute('title')).toBe('Shared · kenari-jatis');
187
+ });
188
+
189
+ test('each row anchors on its own id, not on the model it shows', () => {
190
+ mountSection({ id: 'favorites', title: 'FAVORITES', iconType: 'star', models: SHARED.slice(0, 1) });
191
+
192
+ const row = container.querySelector('[role="button"][data-picker-row]') as HTMLElement;
193
+ expect(row.getAttribute('data-picker-row')).toBe('favorites::deepseek:v4-pro');
194
+ });
195
+
196
+ test('the rail copy and the provider copy of one model report different rows', () => {
197
+ // The end-to-end shape of the bug: a favorite is drawn in the rail and again
198
+ // under its provider, and before row ids the two resolved to one index — so
199
+ // ArrowDown stepped over the rail and both rows highlighted together.
200
+ const groups = buildPickerGroups(SHARED, '', {}, ['deepseek:v4-pro'], []);
201
+ const hovered: number[] = [];
202
+ const onHoverItem = (index: number) => hovered.push(index);
203
+
204
+ act(() => {
205
+ render(
206
+ h(ModelDropdownSection, {
207
+ id: 'favorites',
208
+ title: 'FAVORITES',
209
+ iconType: 'star',
210
+ models: groups.favorites,
211
+ isCollapsed: false,
212
+ onToggleCollapse: () => {},
213
+ selectedModelKey: '',
214
+ modelIndex: groups.index,
215
+ ambiguousNames: groups.ambiguousNames,
216
+ focusedIndex: -1,
217
+ onSelect: () => {},
218
+ onToggleFavorite: () => {},
219
+ onCycleThinking: () => {},
220
+ onHoverItem,
221
+ }),
222
+ container,
223
+ );
224
+ });
225
+
226
+ // happy-dom has no `MouseEvent` on the test global, so the event comes off
227
+ // the document's own view; Preact proxies `onMouseEnter` through `mouseover`.
228
+ const view = container.ownerDocument.defaultView as unknown as {
229
+ Event: new (type: string, init: { bubbles: boolean }) => Event;
230
+ };
231
+ const row = container.querySelector('[role="button"][data-picker-row]') as HTMLElement;
232
+ act(() => {
233
+ row.dispatchEvent(new view.Event('mouseover', { bubbles: true }));
234
+ row.dispatchEvent(new view.Event('mouseenter', { bubbles: false }));
235
+ });
236
+
237
+ expect(hovered).toEqual([0]);
238
+ // …and the provider section's copy of the same model is a different index.
239
+ expect(groups.index['DEEPSEEK::deepseek:v4-pro']).not.toBe(0);
130
240
  });
131
241
  });
@@ -2,6 +2,7 @@ import type { TargetedMouseEvent } from 'preact';
2
2
  import { ChevronDown, ChevronUp, History, Star } from 'lucide-preact';
3
3
  import type { AIModelOption } from '@/shared/types';
4
4
  import { ModelDropdownItem } from '@/client/components/workspace/model-dropdown/Item';
5
+ import { normalizedModelName, rowIdFor } from '@/client/components/workspace/model-dropdown/groups';
5
6
  import { ProviderIconPlate } from '@/client/components/common/provider-icon/Plate';
6
7
  import { modelKey } from '@/shared/lib/models/identity';
7
8
 
@@ -14,13 +15,16 @@ interface ModelDropdownSectionProps {
14
15
  onToggleCollapse: (id: string) => void;
15
16
  /** Composite (`provider:id`) key of the active selection. */
16
17
  selectedModelKey: string;
17
- /** Composite key → index in the keyboard-navigation flat list. */
18
+ /** Row id (`section::provider:id`) → index in the keyboard-navigation flat list. */
18
19
  modelIndex: Record<string, number>;
20
+ /** Lower-cased model names served by more than one provider. */
21
+ ambiguousNames: ReadonlySet<string>;
19
22
  focusedIndex: number;
20
23
  onSelect: (model: AIModelOption) => void;
21
24
  onToggleFavorite: (model: AIModelOption, e: TargetedMouseEvent<HTMLElement>) => void;
22
25
  onCycleThinking: (model: AIModelOption, e: TargetedMouseEvent<HTMLElement>) => void;
23
- onHoverItem: (index: number, model: AIModelOption) => void;
26
+ /** Row index in the flat list; the panel resolves it to the row it renders. */
27
+ onHoverItem: (index: number) => void;
24
28
  }
25
29
 
26
30
  export function ModelDropdownSection({
@@ -32,6 +36,7 @@ export function ModelDropdownSection({
32
36
  onToggleCollapse,
33
37
  selectedModelKey,
34
38
  modelIndex,
39
+ ambiguousNames,
35
40
  focusedIndex,
36
41
  onSelect,
37
42
  onToggleFavorite,
@@ -92,20 +97,27 @@ export function ModelDropdownSection({
92
97
  {!isCollapsed && (
93
98
  <div className="space-y-0.5">
94
99
  {models.map((m) => {
95
- // Identity is provider + id: the same model id is served by several
96
- // providers, so an id-only match would focus/select their rows too.
97
- const key = modelKey(m);
98
- const index = modelIndex[key] ?? -1;
100
+ // Identity is per ROW, not per model: the same model id is served
101
+ // by several providers, and one model is drawn in a rail AND in its
102
+ // provider section. `section::provider:id` keeps every row's hover
103
+ // index and specs anchor its own.
104
+ const rowId = rowIdFor(id, m);
105
+ const index = modelIndex[rowId] ?? -1;
99
106
  return (
100
107
  <ModelDropdownItem
101
- key={`${id}-${key}`}
108
+ key={rowId}
109
+ rowId={rowId}
102
110
  model={m}
103
- isSelected={selectedModelKey === key}
111
+ // A rail has no provider heading above it, so a name shared by
112
+ // two providers would render twice identically. A provider
113
+ // section already says the provider in its header.
114
+ showProvider={iconType !== 'provider' && ambiguousNames.has(normalizedModelName(m))}
115
+ isSelected={selectedModelKey === modelKey(m)}
104
116
  isFocused={focusedIndex === index}
105
117
  onSelect={onSelect}
106
118
  onToggleFavorite={onToggleFavorite}
107
119
  onCycleThinking={onCycleThinking}
108
- onMouseEnter={() => onHoverItem(index, m)}
120
+ onMouseEnter={() => onHoverItem(index)}
109
121
  />
110
122
  );
111
123
  })}
@@ -15,6 +15,18 @@
15
15
  *
16
16
  * The guard is `String(… ?? 'unknown')`, so the row stays visible under a
17
17
  * stable section instead of vanishing from a list the user may have selected.
18
+ *
19
+ * The second rule pinned here is row identity: `section::provider:id`. A model
20
+ * is drawn in a rail AND in its provider section, and a `modelKey`-keyed index
21
+ * could describe only one of the two — so ArrowDown stepped over the rail rows
22
+ * and the specs card anchored to the wrong one.
23
+ *
24
+ * The third is the rails' LABEL: FAVORITES and RECENT have no provider heading,
25
+ * so `ambiguousNames` (the names more than one provider serves) is what decides
26
+ * whether a rail row carries a provider tag. The catalog-wide cases at the
27
+ * bottom moved here from `usage-and-picker.test.ts`, which had grown past the
28
+ * 350-line ceiling — grouping is this module's subject, and the usage panel is
29
+ * not.
18
30
  */
19
31
 
20
32
  import { describe, expect, test } from 'bun:test';
@@ -32,7 +44,7 @@ describe('buildPickerGroups', () => {
32
44
  expect(Object.keys(groups.byProvider).sort()).toEqual(['P', 'UNKNOWN']);
33
45
  expect(groups.byProvider.UNKNOWN?.map((m) => m.id)).toEqual(['b']);
34
46
  // It is still a row the user can see and pick.
35
- expect(groups.visibleFlatList.map((m) => m.id).sort()).toEqual(['a', 'b']);
47
+ expect(groups.visibleFlatList.map((row) => row.model.id).sort()).toEqual(['a', 'b']);
36
48
  });
37
49
 
38
50
  test('an empty provider string lands in the same section as a missing one', () => {
@@ -46,6 +58,73 @@ describe('buildPickerGroups', () => {
46
58
  expect(groups.filtered.map((m) => m.id)).toEqual(['b']);
47
59
  });
48
60
 
61
+ test('the same model drawn in a rail and in its section makes two rows', () => {
62
+ const rows = [
63
+ model({ id: 'v4', provider: 'deepseek', name: 'DeepSeek V4' }),
64
+ model({ id: 'v4', provider: 'kenari', name: 'DeepSeek V4' }),
65
+ ];
66
+ const groups = buildPickerGroups(rows, '', {}, ['deepseek:v4'], []);
67
+
68
+ // A rail row and its provider-section copy are different rows: without this
69
+ // the hover index held one of the two, ArrowDown skipped the rail, and both
70
+ // rows highlighted together.
71
+ expect(groups.visibleFlatList.map((row) => row.rowId)).toEqual([
72
+ 'favorites::deepseek:v4',
73
+ 'DEEPSEEK::deepseek:v4',
74
+ 'KENARI::kenari:v4',
75
+ ]);
76
+ expect(groups.index['favorites::deepseek:v4']).toBe(0);
77
+ expect(groups.index['DEEPSEEK::deepseek:v4']).toBe(1);
78
+ });
79
+
80
+ test('only a name two providers serve is ambiguous, so one row per duplicate is tagged', () => {
81
+ const groups = buildPickerGroups(
82
+ [
83
+ model({ id: 'v4', provider: 'deepseek', name: 'DeepSeek V4' }),
84
+ model({ id: 'v4', provider: 'kenari', name: 'DeepSeek V4' }),
85
+ model({ id: 'solo', provider: 'openai', name: 'GPT-5' }),
86
+ ],
87
+ 'gpt',
88
+ {},
89
+ [],
90
+ [],
91
+ );
92
+
93
+ // Computed over the whole catalog, not the filtered list: a tag that came and
94
+ // went as the reader typed made the rows jump.
95
+ expect([...groups.ambiguousNames]).toEqual(['deepseek v4']);
96
+ expect(groups.filtered).toHaveLength(1);
97
+ });
98
+
99
+ test('a repeated stored key draws one row, not two', () => {
100
+ const groups = buildPickerGroups(
101
+ [model({ id: 'a', provider: 'p' })],
102
+ '',
103
+ {},
104
+ [],
105
+ ['p:a', 'p:a'],
106
+ );
107
+
108
+ // Two copies of a row id would share one index and one anchor.
109
+ expect(groups.recent).toHaveLength(1);
110
+ expect(groups.visibleFlatList.map((row) => row.rowId)).toEqual(['recent::p:a', 'P::p:a']);
111
+ });
112
+
113
+ test('one provider spelled two ways does not make its model name ambiguous', () => {
114
+ const groups = buildPickerGroups(
115
+ [model({ id: 'a', provider: 'deepseek', name: 'Flash' }), model({ id: 'b', provider: 'DeepSeek', name: 'Flash' })],
116
+ '',
117
+ {},
118
+ [],
119
+ [],
120
+ );
121
+
122
+ // Both rows land in the one DEEPSEEK section, so a tag would claim an
123
+ // ambiguity the reader cannot see.
124
+ expect(Object.keys(groups.byProvider)).toEqual(['DEEPSEEK']);
125
+ expect([...groups.ambiguousNames]).toEqual([]);
126
+ });
127
+
49
128
  test('rows group by upper-cased provider and keep catalog order', () => {
50
129
  const groups = buildPickerGroups(
51
130
  [model({ id: 'a', provider: 'anthropic' }), model({ id: 'b', provider: 'Anthropic' })],
@@ -58,3 +137,87 @@ describe('buildPickerGroups', () => {
58
137
  expect(groups.byProvider.ANTHROPIC?.map((m) => m.id)).toEqual(['a', 'b']);
59
138
  });
60
139
  });
140
+
141
+ describe('buildPickerGroups', () => {
142
+ const model = (provider: string, id: string, name: string, extra: Partial<AIModelOption> = {}): AIModelOption =>
143
+ ({ id, name, provider, ...extra }) as AIModelOption;
144
+
145
+ const catalog: AIModelOption[] = [
146
+ model('deepseek', 'v4-flash', 'Flash', { contextWindow: 128000, capabilities: ['tools'] }),
147
+ model('kenari', 'v4-flash', 'Flash (Kenari)', { contextWindow: 64000 }),
148
+ model('openai', 'gpt-5', 'GPT-5'),
149
+ ];
150
+
151
+ test('a search filters on name, provider, context window and capability', () => {
152
+ expect(buildPickerGroups(catalog, 'flash', {}, [], []).filtered).toHaveLength(2);
153
+ expect(buildPickerGroups(catalog, 'OPENAI', {}, [], []).filtered.map((m) => m.id)).toEqual(['gpt-5']);
154
+ expect(buildPickerGroups(catalog, '64000', {}, [], []).filtered.map((m) => m.provider)).toEqual(['kenari']);
155
+ expect(buildPickerGroups(catalog, 'tools', {}, [], []).filtered.map((m) => m.provider)).toEqual(['deepseek']);
156
+ expect(buildPickerGroups(catalog, ' ', {}, [], []).filtered).toHaveLength(3);
157
+ });
158
+
159
+ test('the rails follow the stored key order, not the catalog order', () => {
160
+ const groups = buildPickerGroups(catalog, '', {}, ['openai:gpt-5', 'deepseek:v4-flash'], ['kenari:v4-flash', 'openai:gpt-5']);
161
+ expect(groups.favorites.map((m) => m.id)).toEqual(['gpt-5', 'v4-flash']);
162
+ expect(groups.recent.map((m) => m.provider)).toEqual(['kenari']);
163
+ });
164
+
165
+ test('the rails are narrowed by the search like every other section', () => {
166
+ const groups = buildPickerGroups(catalog, 'flash', {}, ['openai:gpt-5', 'deepseek:v4-flash'], []);
167
+ expect(groups.favorites.map((m) => m.provider)).toEqual(['deepseek']);
168
+ });
169
+
170
+ test('providers are grouped under upper-cased names in catalog order', () => {
171
+ const groups = buildPickerGroups(catalog, '', {}, [], []);
172
+ expect(Object.keys(groups.byProvider)).toEqual(['DEEPSEEK', 'KENARI', 'OPENAI']);
173
+ expect(groups.byProvider.DEEPSEEK).toHaveLength(1);
174
+ });
175
+
176
+ test('the flat list renders rails first, then provider sections', () => {
177
+ const groups = buildPickerGroups(catalog, '', {}, ['openai:gpt-5'], ['deepseek:v4-flash']);
178
+ // A rail model is NOT removed from its provider section: the flat list is the
179
+ // order the panel walks for hover/keyboard focus, and a model appearing once
180
+ // under a rail and again under its provider is the shipped behavior.
181
+ expect(groups.visibleFlatList.map((row) => `${row.model.provider}:${row.model.id}`)).toEqual([
182
+ 'openai:gpt-5', // favorites rail
183
+ 'deepseek:v4-flash', // recent rail
184
+ 'deepseek:v4-flash', // DEEPSEEK section
185
+ 'kenari:v4-flash', // KENARI section
186
+ 'openai:gpt-5', // OPENAI section
187
+ ]);
188
+ // …and each of those five positions is its OWN row, even where two of them
189
+ // carry the same model.
190
+ expect(new Set(groups.visibleFlatList.map((row) => row.rowId)).size).toBe(5);
191
+ });
192
+
193
+ test('a collapsed section is dropped from the flat list but keeps its group', () => {
194
+ const groups = buildPickerGroups(catalog, '', { favorites: true, KENARI: true }, ['openai:gpt-5'], []);
195
+ expect(groups.visibleFlatList.map((row) => row.model.provider)).toEqual(['deepseek', 'openai']);
196
+ expect(groups.byProvider.KENARI).toHaveLength(1);
197
+ });
198
+
199
+ test('the hover index is keyed by provider+id, so shared ids stay distinct', () => {
200
+ const groups = buildPickerGroups(catalog, '', {}, [], []);
201
+ expect(groups.index['DEEPSEEK::deepseek:v4-flash']).toBe(0);
202
+ expect(groups.index['KENARI::kenari:v4-flash']).toBe(1);
203
+ expect(groups.index['DEEPSEEK::deepseek:v4-flash'])
204
+ .not.toBe(groups.index['KENARI::kenari:v4-flash']);
205
+ });
206
+
207
+ test('a row drawn twice gets two indexes and two anchors', () => {
208
+ // The bug this pins: a favorite is drawn in the rail and again under its
209
+ // provider. A `modelKey`-keyed index held only one of them, so the rails
210
+ // were skipped by ArrowDown and both rows lit up together.
211
+ const groups = buildPickerGroups(catalog, '', {}, ['openai:gpt-5'], []);
212
+ const rail = groups.index['favorites::openai:gpt-5'];
213
+ const provider = groups.index['OPENAI::openai:gpt-5'];
214
+ expect(rail).toBe(0);
215
+ expect(provider).toBeGreaterThan(rail ?? -1);
216
+ });
217
+
218
+ test('only a name served by two providers is marked ambiguous', () => {
219
+ const ambiguousCatalog = [...catalog, model('kenari', 'v4-flash-2', 'Flash', {})];
220
+ expect([...buildPickerGroups(ambiguousCatalog, '', {}, [], []).ambiguousNames].sort()).toEqual(['flash']);
221
+ expect([...buildPickerGroups(catalog, '', {}, [], []).ambiguousNames]).toEqual([]);
222
+ });
223
+ });
@@ -6,18 +6,50 @@
6
6
  import type { AIModelOption } from '@/shared/types';
7
7
  import { modelKey } from '@/shared/lib/models/identity';
8
8
 
9
+ /** One rendered row: the model plus the section it is drawn in. */
10
+ export interface PickerRow {
11
+ /**
12
+ * Identity of the ROW, not of the model: `${section}::${modelKey}`.
13
+ *
14
+ * A model appears in a rail AND in its provider section (a favorite that is
15
+ * not removed from its provider list), so a `modelKey`-keyed map could only
16
+ * describe one of the two rows — the other's hover index pointed at its
17
+ * sibling, both lit up together, and the specs card anchored to whichever row
18
+ * `querySelector` found first.
19
+ */
20
+ rowId: string;
21
+ model: AIModelOption;
22
+ }
23
+
9
24
  export interface ModelPickerGroups {
10
25
  filtered: AIModelOption[];
11
26
  favorites: AIModelOption[];
12
27
  recent: AIModelOption[];
13
28
  /** Provider display name (upper-cased) → its models, in catalog order. */
14
29
  byProvider: Record<string, AIModelOption[]>;
30
+ /**
31
+ * Lower-cased model names served by MORE THAN ONE provider, over the whole
32
+ * catalog. Rows in a rail carry their provider tag only for these: an
33
+ * unconditional tag is noise on the 95% of rows whose name is already unique,
34
+ * and the rails have no provider header to disambiguate them.
35
+ */
36
+ ambiguousNames: ReadonlySet<string>;
15
37
  /** Sections expanded, in the order the panel renders them. */
16
- visibleFlatList: AIModelOption[];
17
- /** Composite key → index in `visibleFlatList`, for hover/keyboard focus. */
38
+ visibleFlatList: PickerRow[];
39
+ /** Row id → index in `visibleFlatList`, for hover/keyboard focus. */
18
40
  index: Record<string, number>;
19
41
  }
20
42
 
43
+ /** Row identity: a section id plus the composite model key. */
44
+ export function rowIdFor(section: string, model: { provider: string; id: string }): string {
45
+ return `${section}::${modelKey(model)}`;
46
+ }
47
+
48
+ /** The name a row displays, normalized for the ambiguity check. */
49
+ export function normalizedModelName(model: AIModelOption): string {
50
+ return String(model.name ?? '').trim().toLowerCase();
51
+ }
52
+
21
53
  /**
22
54
  * Models named by an ordered key list, in that order.
23
55
  *
@@ -30,24 +62,56 @@ function modelsInKeyOrder(models: AIModelOption[], keys: readonly string[]): AIM
30
62
  if (keys.length === 0) return [];
31
63
  const byKey = new Map(models.map((model) => [modelKey(model), model]));
32
64
  const ordered: AIModelOption[] = [];
65
+ // A key is claimed once. A stored list that repeats a key (hand-edited, or
66
+ // written by an older build) would otherwise draw the same row twice with the
67
+ // same row id, and the second copy would steal the first one's hover index
68
+ // and specs anchor.
69
+ const claimed = new Set<string>();
33
70
  for (const key of keys) {
71
+ if (claimed.has(key)) continue;
34
72
  const match = byKey.get(key);
35
- if (match) ordered.push(match);
73
+ if (!match) continue;
74
+ claimed.add(key);
75
+ ordered.push(match);
36
76
  }
37
77
  return ordered;
38
78
  }
39
79
 
80
+ /**
81
+ * Names that cannot identify a row on their own, computed over the UNFILTERED
82
+ * catalog: a tag that appeared only while the search narrowed the list would
83
+ * flicker as the reader types.
84
+ *
85
+ * The provider is compared case-insensitively for the same reason the sections
86
+ * are (`DEEPSEEK` heads both `deepseek` and `DeepSeek`): one provider spelled two
87
+ * ways is still one provider, and tagging those rows would claim an ambiguity
88
+ * that does not exist.
89
+ */
90
+ function findAmbiguousNames(models: AIModelOption[]): ReadonlySet<string> {
91
+ const providersByName = new Map<string, Set<string>>();
92
+ for (const model of models) {
93
+ const name = normalizedModelName(model);
94
+ if (!name) continue;
95
+ const providers = providersByName.get(name) ?? new Set<string>();
96
+ providers.add(String(model.provider || 'unknown').toUpperCase());
97
+ providersByName.set(name, providers);
98
+ }
99
+ const ambiguous = new Set<string>();
100
+ for (const [name, providers] of providersByName) {
101
+ if (providers.size > 1) ambiguous.add(name);
102
+ }
103
+ return ambiguous;
104
+ }
105
+
40
106
  /**
41
107
  * All derived picker state in one place: search filtering, the favorites/recent
42
- * rails, the per-provider grouping, and the flattened key→index map the
108
+ * rails, the per-provider grouping, and the flattened row→index map the
43
109
  * keyboard navigation addresses rows through.
44
110
  *
45
- * Keying `index` on the composite identity matters — the same model id is served
46
- * by several providers, so an id-only key collapsed their rows onto one index
47
- * and hovering one highlighted every other provider's row.
48
- *
49
- * A row never appears twice: a favorite that is also recent is only listed under
50
- * FAVORITES, and neither rail repeats the row in its provider section.
111
+ * Keying `index` on the ROW matters twice over: the same model id is served by
112
+ * several providers (an id-only key lit every provider's row), and one model is
113
+ * drawn in a rail and again in its provider section (a model-keyed index
114
+ * collapsed those two rows onto one entry).
51
115
  */
52
116
  export function buildPickerGroups(
53
117
  models: AIModelOption[],
@@ -85,17 +149,30 @@ export function buildPickerGroups(
85
149
  (byProvider[provider] ??= []).push(model);
86
150
  }
87
151
 
88
- const visibleFlatList: AIModelOption[] = [];
89
- if (!collapsedSections['favorites']) visibleFlatList.push(...favorites);
90
- if (!collapsedSections['recent']) visibleFlatList.push(...recent);
152
+ const visibleFlatList: PickerRow[] = [];
153
+ const push = (section: string, sectionModels: AIModelOption[]) => {
154
+ for (const model of sectionModels) {
155
+ visibleFlatList.push({ rowId: rowIdFor(section, model), model });
156
+ }
157
+ };
158
+ if (!collapsedSections['favorites']) push('favorites', favorites);
159
+ if (!collapsedSections['recent']) push('recent', recent);
91
160
  for (const [provider, groupModels] of Object.entries(byProvider)) {
92
- if (!collapsedSections[provider]) visibleFlatList.push(...groupModels);
161
+ if (!collapsedSections[provider]) push(provider, groupModels);
93
162
  }
94
163
 
95
164
  const index: Record<string, number> = {};
96
- visibleFlatList.forEach((model, i) => {
97
- index[modelKey(model)] = i;
165
+ visibleFlatList.forEach((row, i) => {
166
+ index[row.rowId] = i;
98
167
  });
99
168
 
100
- return { filtered, favorites, recent, byProvider, visibleFlatList, index };
169
+ return {
170
+ filtered,
171
+ favorites,
172
+ recent,
173
+ byProvider,
174
+ ambiguousNames: findAmbiguousNames(models),
175
+ visibleFlatList,
176
+ index,
177
+ };
101
178
  }