@steerable/agent-shell-web 0.14.4 → 0.14.5

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.
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  useCallback,
3
3
  useEffect,
4
+ useMemo,
4
5
  useRef,
5
6
  useState,
6
7
  type ReactNode,
@@ -10,11 +11,13 @@ import {
10
11
  LuCheck,
11
12
  LuChevronDown,
12
13
  LuFolder,
14
+ LuFolderOpen,
13
15
  LuFolderPen,
14
16
  LuFolderPlus,
15
17
  LuFolderX,
16
18
  LuLoaderCircle,
17
19
  LuPlus,
20
+ LuSearch,
18
21
  } from 'react-icons/lu';
19
22
  import { CreateProjectModal } from '@/components/CreateProjectModal';
20
23
  import { getHostBridge, hasHostBridge } from '@/lib/host-bridge';
@@ -27,18 +30,51 @@ import {
27
30
  import { t } from '@/i18n';
28
31
 
29
32
  /**
30
- * 项目徽章 / 选择器(Codex 式 cwd 指示),渲染在输入框上方的 meta 行里,
31
- * 与专家选择器并排(项目在前)。
32
- *
33
- * 两个导出组件共用同一套按钮 + 上开菜单视觉:
34
- * - ChatProjectBadge — 会话内:显示当前项目(无项目时弱化为文件夹加号图标按钮),
35
- * 可关联/移动到其他项目、修改项目目录、移出项目。
36
- * - ProjectPickerButton — 落地页(还没有 chat):受控选择器,选中的
37
- * projectId 在首次发消息建会话时一并传入。
38
- *
39
- * 菜单用 pointerdown 外击与 Escape 关闭,不铺全屏遮罩,避免吞掉相邻控件的首次点击。
33
+ * 项目徽章 / 选择器(WorkBuddy 式项目与本地目录选择器):
34
+ * - ProjectPickerButton — 新建首屏下托盘选择器(搜索项目 + 新建项目 + 选择本地文件夹一步绑定)
35
+ * - ChatProjectBadge — 会话内项目徽章(切换/移动到其他项目、一键关联本地文件夹、修改目录、移出项目)
40
36
  */
41
37
 
38
+ function normalizeFolderPath(raw: string): string {
39
+ return raw.replace(/[\\/]+$/, '') || raw;
40
+ }
41
+
42
+ async function pickOrCreateProjectFromDirectory(
43
+ projects: readonly LocalProject[],
44
+ onProjectsChanged: () => void | Promise<void>,
45
+ ): Promise<LocalProject | null> {
46
+ if (!hasHostBridge()) return null;
47
+ const result = await getHostBridge()!.local?.selectDirectory({
48
+ title: t('Choose local folder'),
49
+ });
50
+ if (!result || result.canceled || result.filePaths.length === 0) return null;
51
+
52
+ const chosen = normalizeFolderPath(result.filePaths[0]!);
53
+ const existing = projects.find(
54
+ (p) => normalizeFolderPath(p.folderPath) === chosen,
55
+ );
56
+ if (existing) return existing;
57
+
58
+ const baseName =
59
+ chosen.split(/[\\/]/).filter(Boolean).pop() || t('Untitled project');
60
+ const created = await createProject({ name: baseName, folderPath: chosen });
61
+ await onProjectsChanged();
62
+ return created.project ?? null;
63
+ }
64
+
65
+ function filterProjectsByQuery(
66
+ list: readonly LocalProject[],
67
+ query: string,
68
+ ): LocalProject[] {
69
+ const q = query.trim().toLowerCase();
70
+ if (!q) return [...list];
71
+ return list.filter(
72
+ (p) =>
73
+ p.name.toLowerCase().includes(q) ||
74
+ p.folderPath.toLowerCase().includes(q),
75
+ );
76
+ }
77
+
42
78
  /* ---------------- 共享内部件 ---------------- */
43
79
 
44
80
  function useMenuDismiss(
@@ -80,9 +116,7 @@ function BadgeButton({
80
116
  title,
81
117
  }: {
82
118
  label: string;
83
- /** true = 已关联/已选中项目(实心图标);false = 未选择(轻态无边框 + Plus 图标)。 */
84
119
  active: boolean;
85
- /** true = 会话内未关联项目时仅渲染紧凑图标,避免常驻「选择项目」文字噪音。 */
86
120
  compact?: boolean;
87
121
  open: boolean;
88
122
  busy: boolean;
@@ -120,22 +154,22 @@ function BadgeButton({
120
154
  aria-haspopup="menu"
121
155
  aria-expanded={open}
122
156
  data-testid="project-badge-button"
123
- className={`inline-flex h-6 max-w-full items-center gap-1.5 rounded-full px-2 text-[11px] leading-none transition-colors hover:bg-agent-foreground/5 hover:text-agent-foreground disabled:opacity-60 ${
157
+ className={`inline-flex h-6 max-w-full items-center gap-1.5 rounded-full px-2 text-[12px] leading-[1.45] transition-colors hover:bg-agent-foreground/5 hover:text-agent-foreground disabled:opacity-60 ${
124
158
  active
125
- ? 'text-agent-muted-foreground/85'
126
- : 'text-agent-muted-foreground/65'
159
+ ? 'text-agent-foreground/90 font-medium'
160
+ : 'text-agent-muted-foreground'
127
161
  }`}
128
162
  >
129
163
  {busy ? (
130
- <LuLoaderCircle className="h-3 w-3 shrink-0 animate-spin" />
164
+ <LuLoaderCircle className="h-3.5 w-3.5 shrink-0 animate-spin" />
131
165
  ) : active ? (
132
- <LuFolder className="h-3 w-3 shrink-0" />
166
+ <LuFolder className="h-3.5 w-3.5 shrink-0" />
133
167
  ) : (
134
- <LuFolderPlus className="h-3 w-3 shrink-0" />
168
+ <LuFolderPlus className="h-3.5 w-3.5 shrink-0" />
135
169
  )}
136
- <span className="max-w-[220px] truncate font-medium">{label}</span>
170
+ <span className="max-w-[180px] truncate">{label}</span>
137
171
  <LuChevronDown
138
- className={`h-3 w-3 shrink-0 transition-transform ${open ? 'rotate-180' : ''}`}
172
+ className={`h-3 w-3 shrink-0 opacity-75 transition-transform ${open ? 'rotate-180' : ''}`}
139
173
  />
140
174
  </button>
141
175
  );
@@ -149,13 +183,36 @@ function MenuShell({
149
183
  return (
150
184
  <div
151
185
  role="menu"
152
- className="absolute bottom-full left-0 z-50 mb-1 w-64 overflow-hidden rounded-agent-md border border-agent-border bg-agent-canvas p-1 shadow-lg"
186
+ className="absolute bottom-full left-0 z-50 mb-1.5 w-64 overflow-hidden rounded-agent-lg border border-agent-border bg-agent-canvas p-1.5 shadow-lg"
153
187
  >
154
188
  {children}
155
189
  </div>
156
190
  );
157
191
  }
158
192
 
193
+ function ProjectSearchBox({
194
+ value,
195
+ onChange,
196
+ }: {
197
+ value: string;
198
+ onChange: (next: string) => void;
199
+ }) {
200
+ return (
201
+ <div className="mb-1 flex items-center gap-1.5 rounded-agent-md bg-agent-muted/60 px-2 py-1">
202
+ <LuSearch className="h-3.5 w-3.5 shrink-0 text-agent-muted-foreground" />
203
+ <input
204
+ type="text"
205
+ value={value}
206
+ onChange={(e) => onChange(e.target.value)}
207
+ placeholder={t('Search projects…')}
208
+ aria-label={t('Search projects…')}
209
+ data-testid="project-search-input"
210
+ className="min-w-0 flex-1 border-0 bg-transparent text-xs text-agent-foreground placeholder:text-agent-muted-foreground/70 outline-none"
211
+ />
212
+ </div>
213
+ );
214
+ }
215
+
159
216
  function MenuSectionLabel({
160
217
  children,
161
218
  onAdd,
@@ -197,6 +254,21 @@ function CreateProjectMenuItem({ onClick }: { onClick: () => void }) {
197
254
  );
198
255
  }
199
256
 
257
+ function ChooseLocalFolderMenuItem({ onClick }: { onClick: () => void }) {
258
+ return (
259
+ <button
260
+ type="button"
261
+ role="menuitem"
262
+ data-testid="pick-local-folder"
263
+ onClick={onClick}
264
+ className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs text-agent-foreground transition-colors hover:bg-agent-foreground/5"
265
+ >
266
+ <LuFolderOpen className="h-3.5 w-3.5 shrink-0 text-agent-muted-foreground" />
267
+ {t('Choose local folder')}
268
+ </button>
269
+ );
270
+ }
271
+
200
272
  function ProjectMenuRow({
201
273
  project,
202
274
  selected,
@@ -233,31 +305,36 @@ export function ChatProjectBadge({
233
305
  onChatProjectChanged,
234
306
  }: {
235
307
  chatId: string;
236
- /** 当前会话绑定的项目;null = 无项目会话(弱化为文件夹加号图标)。 */
237
308
  project: LocalProject | null;
238
- /** 全部项目(用于关联列表)。 */
239
309
  projects: LocalProject[];
240
- /** 项目本身被修改(改目录)后回调,让父组件刷新项目列表。 */
241
310
  onProjectsChanged: () => void | Promise<void>;
242
- /** 会话归属变化后回调(通常是 refreshChats)。 */
243
311
  onChatProjectChanged: () => void | Promise<void>;
244
312
  }) {
245
313
  const [open, setOpen] = useState(false);
314
+ const [query, setQuery] = useState('');
246
315
  const [createOpen, setCreateOpen] = useState(false);
247
316
  const [busy, setBusy] = useState(false);
248
317
  const [error, setError] = useState<string | null>(null);
249
318
  const rootRef = useRef<HTMLDivElement>(null);
250
- const closeMenu = useCallback(() => setOpen(false), []);
319
+ const closeMenu = useCallback(() => {
320
+ setOpen(false);
321
+ setQuery('');
322
+ }, []);
251
323
  useMenuDismiss(rootRef, open, closeMenu);
252
324
 
253
325
  const openCreate = () => {
254
- setOpen(false);
326
+ closeMenu();
255
327
  setCreateOpen(true);
256
328
  };
257
329
 
258
- const otherProjects = project
259
- ? projects.filter((p) => p.id !== project.id)
260
- : projects;
330
+ const otherProjects = useMemo(
331
+ () => (project ? projects.filter((p) => p.id !== project.id) : projects),
332
+ [project, projects],
333
+ );
334
+ const filteredOtherProjects = useMemo(
335
+ () => filterProjectsByQuery(otherProjects, query),
336
+ [otherProjects, query],
337
+ );
261
338
 
262
339
  const run = async (action: () => Promise<void>) => {
263
340
  if (busy) return;
@@ -265,7 +342,7 @@ export function ChatProjectBadge({
265
342
  setError(null);
266
343
  try {
267
344
  await action();
268
- setOpen(false);
345
+ closeMenu();
269
346
  } catch (err) {
270
347
  setError(err instanceof Error ? err.message : String(err));
271
348
  } finally {
@@ -279,6 +356,17 @@ export function ChatProjectBadge({
279
356
  await onChatProjectChanged();
280
357
  });
281
358
 
359
+ const pickLocalFolder = () =>
360
+ run(async () => {
361
+ const target = await pickOrCreateProjectFromDirectory(
362
+ projects,
363
+ onProjectsChanged,
364
+ );
365
+ if (!target) return;
366
+ await updateChatProject(chatId, target.id);
367
+ await onChatProjectChanged();
368
+ });
369
+
282
370
  const changeFolder = () =>
283
371
  run(async () => {
284
372
  if (!hasHostBridge() || !project) return;
@@ -300,7 +388,8 @@ export function ChatProjectBadge({
300
388
  busy={busy}
301
389
  onClick={() => {
302
390
  setError(null);
303
- setOpen((v) => !v);
391
+ if (open) closeMenu();
392
+ else setOpen(true);
304
393
  }}
305
394
  title={
306
395
  project
@@ -313,6 +402,10 @@ export function ChatProjectBadge({
313
402
 
314
403
  {open && (
315
404
  <MenuShell>
405
+ {otherProjects.length > 0 && (
406
+ <ProjectSearchBox value={query} onChange={setQuery} />
407
+ )}
408
+
316
409
  {project ? (
317
410
  <>
318
411
  <MenuSectionLabel>{t('Current project')}</MenuSectionLabel>
@@ -324,7 +417,7 @@ export function ChatProjectBadge({
324
417
  <span className="min-w-0 flex-1 truncate font-medium">
325
418
  {project.name}
326
419
  </span>
327
- <LuCheck className="h-3 w-3 shrink-0 text-agent-muted-foreground" />
420
+ <LuCheck className="h-3.5 w-3.5 shrink-0 text-agent-muted-foreground" />
328
421
  </div>
329
422
  <div
330
423
  className="truncate px-2 pb-1 text-[10px] font-mono text-agent-muted-foreground/70"
@@ -343,24 +436,34 @@ export function ChatProjectBadge({
343
436
  <div className="mx-1 my-1 border-t border-agent-border/60" />
344
437
  )}
345
438
  {project && <MenuSectionLabel>{t('Move to')}</MenuSectionLabel>}
346
- <div className="max-h-40 overflow-y-auto">
347
- {otherProjects.map((p) => (
348
- <ProjectMenuRow
349
- key={p.id}
350
- project={p}
351
- onClick={() => void moveTo(p.id)}
352
- />
353
- ))}
354
- </div>
439
+ {filteredOtherProjects.length > 0 ? (
440
+ <div className="max-h-40 overflow-y-auto">
441
+ {filteredOtherProjects.map((p) => (
442
+ <ProjectMenuRow
443
+ key={p.id}
444
+ project={p}
445
+ onClick={() => void moveTo(p.id)}
446
+ />
447
+ ))}
448
+ </div>
449
+ ) : (
450
+ <div className="px-2 py-2 text-center text-[11px] text-agent-muted-foreground/70">
451
+ {t('No matching projects')}
452
+ </div>
453
+ )}
355
454
  </>
356
455
  )}
456
+
357
457
  {!project && projects.length === 0 && (
358
- <>
359
- <div className="px-2 py-1.5 text-[11px] text-agent-muted-foreground/70">
360
- {t('No projects yet')}
361
- </div>
362
- <CreateProjectMenuItem onClick={openCreate} />
363
- </>
458
+ <div className="px-2 py-1.5 text-[11px] text-agent-muted-foreground/70">
459
+ {t('No projects yet')}
460
+ </div>
461
+ )}
462
+
463
+ <div className="mx-1 my-1 border-t border-agent-border/60" />
464
+ <CreateProjectMenuItem onClick={openCreate} />
465
+ {hasHostBridge() && (
466
+ <ChooseLocalFolderMenuItem onClick={() => void pickLocalFolder()} />
364
467
  )}
365
468
 
366
469
  {project && (
@@ -423,32 +526,59 @@ export function ProjectPickerButton({
423
526
  onProjectsChanged,
424
527
  }: {
425
528
  projects: LocalProject[];
426
- /** 当前选中的 projectId;null = 无项目。 */
427
529
  value: string | null;
428
530
  onChange: (projectId: string | null) => void;
429
- /** 新建项目后刷新列表。 */
430
531
  onProjectsChanged: () => void | Promise<void>;
431
532
  }) {
432
533
  const [open, setOpen] = useState(false);
534
+ const [query, setQuery] = useState('');
433
535
  const [createOpen, setCreateOpen] = useState(false);
536
+ const [busy, setBusy] = useState(false);
434
537
  const rootRef = useRef<HTMLDivElement>(null);
435
- const closeMenu = useCallback(() => setOpen(false), []);
538
+ const closeMenu = useCallback(() => {
539
+ setOpen(false);
540
+ setQuery('');
541
+ }, []);
436
542
  useMenuDismiss(rootRef, open, closeMenu);
437
543
  const selected = value ? (projects.find((p) => p.id === value) ?? null) : null;
544
+ const filteredProjects = useMemo(
545
+ () => filterProjectsByQuery(projects, query),
546
+ [projects, query],
547
+ );
438
548
 
439
549
  const openCreate = () => {
440
- setOpen(false);
550
+ closeMenu();
441
551
  setCreateOpen(true);
442
552
  };
443
553
 
554
+ const handlePickLocalFolder = async () => {
555
+ if (busy) return;
556
+ setBusy(true);
557
+ try {
558
+ const target = await pickOrCreateProjectFromDirectory(
559
+ projects,
560
+ onProjectsChanged,
561
+ );
562
+ if (target) {
563
+ onChange(target.id);
564
+ closeMenu();
565
+ }
566
+ } finally {
567
+ setBusy(false);
568
+ }
569
+ };
570
+
444
571
  return (
445
572
  <div ref={rootRef} className="relative">
446
573
  <BadgeButton
447
574
  label={selected ? selected.name : t('Choose a project')}
448
575
  active={Boolean(selected)}
449
576
  open={open}
450
- busy={false}
451
- onClick={() => setOpen((v) => !v)}
577
+ busy={busy}
578
+ onClick={() => {
579
+ if (open) closeMenu();
580
+ else setOpen(true);
581
+ }}
452
582
  title={
453
583
  selected
454
584
  ? `${selected.folderPath}\n${t('New chats will be linked to this project')}`
@@ -460,42 +590,57 @@ export function ProjectPickerButton({
460
590
 
461
591
  {open && (
462
592
  <MenuShell>
593
+ {projects.length > 0 && (
594
+ <ProjectSearchBox value={query} onChange={setQuery} />
595
+ )}
463
596
  <MenuSectionLabel onAdd={openCreate}>{t('Project for new chat')}</MenuSectionLabel>
464
- <button
465
- type="button"
466
- role="menuitem"
467
- onClick={() => {
468
- onChange(null);
469
- setOpen(false);
470
- }}
471
- className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs text-agent-foreground transition-colors hover:bg-agent-foreground/5"
472
- >
473
- <LuFolderX className="h-3.5 w-3.5 shrink-0 text-agent-muted-foreground" />
474
- <span className="min-w-0 flex-1 truncate">{t('No project')}</span>
475
- {!selected && (
476
- <LuCheck className="h-3 w-3 shrink-0 text-agent-muted-foreground" />
477
- )}
478
- </button>
479
- <div className="max-h-40 overflow-y-auto">
480
- {projects.map((p) => (
481
- <ProjectMenuRow
482
- key={p.id}
483
- project={p}
484
- selected={p.id === value}
485
- onClick={() => {
486
- onChange(p.id);
487
- setOpen(false);
488
- }}
489
- />
490
- ))}
491
- </div>
492
- {projects.length === 0 && (
493
- <>
494
- <div className="px-2 py-1.5 text-[11px] text-agent-muted-foreground/70">
495
- {t('No projects yet')}
597
+ {!query.trim() && (
598
+ <button
599
+ type="button"
600
+ role="menuitem"
601
+ onClick={() => {
602
+ onChange(null);
603
+ closeMenu();
604
+ }}
605
+ className="flex w-full items-center gap-2 rounded px-2 py-1.5 text-left text-xs text-agent-foreground transition-colors hover:bg-agent-foreground/5"
606
+ >
607
+ <LuFolderX className="h-3.5 w-3.5 shrink-0 text-agent-muted-foreground" />
608
+ <span className="min-w-0 flex-1 truncate">{t('No project')}</span>
609
+ {!selected && (
610
+ <LuCheck className="h-3 w-3 shrink-0 text-agent-muted-foreground" />
611
+ )}
612
+ </button>
613
+ )}
614
+ {projects.length > 0 ? (
615
+ filteredProjects.length > 0 ? (
616
+ <div className="max-h-40 overflow-y-auto">
617
+ {filteredProjects.map((p) => (
618
+ <ProjectMenuRow
619
+ key={p.id}
620
+ project={p}
621
+ selected={p.id === value}
622
+ onClick={() => {
623
+ onChange(p.id);
624
+ closeMenu();
625
+ }}
626
+ />
627
+ ))}
496
628
  </div>
497
- <CreateProjectMenuItem onClick={openCreate} />
498
- </>
629
+ ) : (
630
+ <div className="px-2 py-2 text-center text-[11px] text-agent-muted-foreground/70">
631
+ {t('No matching projects')}
632
+ </div>
633
+ )
634
+ ) : (
635
+ <div className="px-2 py-1.5 text-[11px] text-agent-muted-foreground/70">
636
+ {t('No projects yet')}
637
+ </div>
638
+ )}
639
+
640
+ <div className="mx-1 my-1 border-t border-agent-border/60" />
641
+ <CreateProjectMenuItem onClick={openCreate} />
642
+ {hasHostBridge() && (
643
+ <ChooseLocalFolderMenuItem onClick={() => void handlePickLocalFolder()} />
499
644
  )}
500
645
  </MenuShell>
501
646
  )}
@@ -144,6 +144,8 @@ export function ContextUsagePill({
144
144
  const rawPct = Math.round(dominantRatio * 100);
145
145
  const dominantPct = estTokens > 0 || estBytes > 0 ? Math.max(1, rawPct) : 0;
146
146
  const isElevated = dominantRatio >= 0.7;
147
+ const isQuietLow = dominantRatio < 0.5;
148
+ const showByteDominantHint = isByteDominant && byteRatio - tokenRatio >= 0.25;
147
149
  const dominantSummary = isByteDominant
148
150
  ? formatMegabytes(estBytes)
149
151
  : formatCompactCount(estTokens);
@@ -156,7 +158,9 @@ export function ContextUsagePill({
156
158
  ? 'border-red-500/40 bg-red-500/10 px-2 text-red-600 dark:text-red-400'
157
159
  : isElevated
158
160
  ? 'border-amber-500/40 bg-amber-500/10 px-2 text-amber-600 dark:text-amber-400'
159
- : 'border-transparent bg-transparent px-1.5 text-agent-muted-foreground/80 hover:bg-agent-foreground/5 hover:text-agent-foreground';
161
+ : isQuietLow
162
+ ? 'border-transparent bg-transparent px-1 text-agent-muted-foreground/75 hover:bg-agent-foreground/5 hover:text-agent-foreground'
163
+ : 'border-transparent bg-transparent px-1.5 text-agent-muted-foreground/80 hover:bg-agent-foreground/5 hover:text-agent-foreground';
160
164
 
161
165
  const barColor =
162
166
  dominantRatio >= 0.85
@@ -196,19 +200,22 @@ export function ContextUsagePill({
196
200
  type="button"
197
201
  data-testid="context-usage-pill"
198
202
  onClick={() => setOpen((prev) => !prev)}
203
+ aria-label={`${t('Context window and payload usage')} (${dominantPct}%)`}
199
204
  title={`${t('Context window and payload usage')} (${tooltipDetail})`}
200
205
  className={`inline-flex h-6 items-center gap-1 rounded-full border text-[11px] leading-tight tabular-nums transition-colors ${toneClass}`}
201
206
  >
202
207
  <ContextRing ratio={dominantRatio} />
203
- <span>
204
- {isElevated ? `${dominantSummary} · ${dominantPct}%` : `${dominantPct}%`}
205
- </span>
208
+ {!isQuietLow && (
209
+ <span>
210
+ {isElevated ? `${dominantSummary} · ${dominantPct}%` : `${dominantPct}%`}
211
+ </span>
212
+ )}
206
213
  </button>
207
214
 
208
215
  {open && (
209
216
  <div
210
217
  data-testid="context-usage-popover"
211
- className="absolute bottom-full left-0 z-50 mb-2 w-72 rounded-agent-lg border border-agent-border bg-agent-canvas p-3 text-xs shadow-lg"
218
+ className="absolute bottom-full right-0 z-50 mb-2 w-72 max-w-[calc(100vw-2rem)] rounded-agent-lg border border-agent-border bg-agent-canvas p-3 text-xs shadow-lg"
212
219
  >
213
220
  <div className="font-medium text-agent-foreground">
214
221
  {t('Context budget')}
@@ -245,6 +252,14 @@ export function ContextUsagePill({
245
252
  style={{ width: `${Math.max(2, Math.round(byteRatio * 100))}%` }}
246
253
  />
247
254
  </div>
255
+ {showByteDominantHint && (
256
+ <div
257
+ data-testid="context-byte-dominant-hint"
258
+ className="mt-1 text-[11px] text-agent-muted-foreground"
259
+ >
260
+ {t('Primarily used by embedded images or attachments.')}
261
+ </div>
262
+ )}
248
263
  </div>
249
264
 
250
265
  {cacheHitPct !== null && (