@steerable/agent-shell-web 0.14.2 → 0.14.4

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,4 +1,11 @@
1
- import { useState, type ReactNode } from 'react';
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useRef,
5
+ useState,
6
+ type ReactNode,
7
+ type RefObject,
8
+ } from 'react';
2
9
  import {
3
10
  LuCheck,
4
11
  LuChevronDown,
@@ -24,42 +31,99 @@ import { t } from '@/i18n';
24
31
  * 与专家选择器并排(项目在前)。
25
32
  *
26
33
  * 两个导出组件共用同一套按钮 + 上开菜单视觉:
27
- * - ChatProjectBadge — 会话内:显示当前项目(无项目时显示"选择项目"),
34
+ * - ChatProjectBadge — 会话内:显示当前项目(无项目时弱化为文件夹加号图标按钮),
28
35
  * 可关联/移动到其他项目、修改项目目录、移出项目。
29
36
  * - ProjectPickerButton — 落地页(还没有 chat):受控选择器,选中的
30
37
  * projectId 在首次发消息建会话时一并传入。
31
38
  *
32
- * 菜单用透明全屏 backdrop 关外击(一次性菜单,比 click-outside 管线简单)。
39
+ * 菜单用 pointerdown 外击与 Escape 关闭,不铺全屏遮罩,避免吞掉相邻控件的首次点击。
33
40
  */
34
41
 
35
42
  /* ---------------- 共享内部件 ---------------- */
36
43
 
44
+ function useMenuDismiss(
45
+ rootRef: RefObject<HTMLElement | null>,
46
+ open: boolean,
47
+ onClose: () => void,
48
+ ) {
49
+ useEffect(() => {
50
+ if (!open) return;
51
+ const handlePointerDown = (event: PointerEvent) => {
52
+ const target = event.target;
53
+ if (target instanceof Node && rootRef.current?.contains(target)) {
54
+ return;
55
+ }
56
+ onClose();
57
+ };
58
+ const handleKeyDown = (event: KeyboardEvent) => {
59
+ if (event.key === 'Escape') {
60
+ event.stopPropagation();
61
+ onClose();
62
+ }
63
+ };
64
+ window.addEventListener('pointerdown', handlePointerDown);
65
+ window.addEventListener('keydown', handleKeyDown);
66
+ return () => {
67
+ window.removeEventListener('pointerdown', handlePointerDown);
68
+ window.removeEventListener('keydown', handleKeyDown);
69
+ };
70
+ }, [rootRef, open, onClose]);
71
+ }
72
+
37
73
  function BadgeButton({
38
74
  label,
39
75
  active,
76
+ compact = false,
40
77
  open,
41
78
  busy,
42
79
  onClick,
43
80
  title,
44
81
  }: {
45
82
  label: string;
46
- /** true = 已关联/已选中项目(实心图标);false = 未选择(虚线感 + Plus 图标)。 */
83
+ /** true = 已关联/已选中项目(实心图标);false = 未选择(轻态无边框 + Plus 图标)。 */
47
84
  active: boolean;
85
+ /** true = 会话内未关联项目时仅渲染紧凑图标,避免常驻「选择项目」文字噪音。 */
86
+ compact?: boolean;
48
87
  open: boolean;
49
88
  busy: boolean;
50
89
  onClick: () => void;
51
90
  title?: string;
52
91
  }) {
92
+ if (compact) {
93
+ return (
94
+ <button
95
+ type="button"
96
+ onClick={onClick}
97
+ disabled={busy}
98
+ title={title}
99
+ aria-label={label}
100
+ aria-haspopup="menu"
101
+ aria-expanded={open}
102
+ data-testid="project-badge-button"
103
+ className="inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-agent-muted-foreground/65 transition-colors hover:bg-agent-foreground/5 hover:text-agent-foreground disabled:opacity-60"
104
+ >
105
+ {busy ? (
106
+ <LuLoaderCircle className="h-3.5 w-3.5 shrink-0 animate-spin" />
107
+ ) : (
108
+ <LuFolderPlus className="h-3.5 w-3.5 shrink-0" />
109
+ )}
110
+ </button>
111
+ );
112
+ }
113
+
53
114
  return (
54
115
  <button
55
116
  type="button"
56
117
  onClick={onClick}
57
118
  disabled={busy}
58
119
  title={title}
59
- className={`flex max-w-full items-center gap-1.5 rounded-full px-2 py-0.5 text-[11px] transition-colors disabled:opacity-60 ${
120
+ aria-haspopup="menu"
121
+ aria-expanded={open}
122
+ 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 ${
60
124
  active
61
- ? 'text-agent-muted-foreground/80 hover:bg-agent-foreground/5 hover:text-agent-foreground'
62
- : 'border border-dashed border-agent-border text-agent-muted-foreground/70 hover:border-agent-muted-foreground/50 hover:text-agent-foreground'
125
+ ? 'text-agent-muted-foreground/85'
126
+ : 'text-agent-muted-foreground/65'
63
127
  }`}
64
128
  >
65
129
  {busy ? (
@@ -78,22 +142,17 @@ function BadgeButton({
78
142
  }
79
143
 
80
144
  function MenuShell({
81
- onClose,
82
145
  children,
83
146
  }: {
84
- onClose: () => void;
85
147
  children: ReactNode;
86
148
  }) {
87
149
  return (
88
- <>
89
- <div className="fixed inset-0 z-40 cursor-default" onClick={onClose} />
90
- <div
91
- role="menu"
92
- 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"
93
- >
94
- {children}
95
- </div>
96
- </>
150
+ <div
151
+ 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"
153
+ >
154
+ {children}
155
+ </div>
97
156
  );
98
157
  }
99
158
 
@@ -174,7 +233,7 @@ export function ChatProjectBadge({
174
233
  onChatProjectChanged,
175
234
  }: {
176
235
  chatId: string;
177
- /** 当前会话绑定的项目;null = 无项目会话(徽章变为"选择项目")。 */
236
+ /** 当前会话绑定的项目;null = 无项目会话(弱化为文件夹加号图标)。 */
178
237
  project: LocalProject | null;
179
238
  /** 全部项目(用于关联列表)。 */
180
239
  projects: LocalProject[];
@@ -187,6 +246,9 @@ export function ChatProjectBadge({
187
246
  const [createOpen, setCreateOpen] = useState(false);
188
247
  const [busy, setBusy] = useState(false);
189
248
  const [error, setError] = useState<string | null>(null);
249
+ const rootRef = useRef<HTMLDivElement>(null);
250
+ const closeMenu = useCallback(() => setOpen(false), []);
251
+ useMenuDismiss(rootRef, open, closeMenu);
190
252
 
191
253
  const openCreate = () => {
192
254
  setOpen(false);
@@ -229,10 +291,11 @@ export function ChatProjectBadge({
229
291
  });
230
292
 
231
293
  return (
232
- <div className="relative">
294
+ <div ref={rootRef} className="relative">
233
295
  <BadgeButton
234
296
  label={project ? project.name : t('Choose a project')}
235
297
  active={Boolean(project)}
298
+ compact={!project}
236
299
  open={open}
237
300
  busy={busy}
238
301
  onClick={() => {
@@ -249,7 +312,7 @@ export function ChatProjectBadge({
249
312
  />
250
313
 
251
314
  {open && (
252
- <MenuShell onClose={() => setOpen(false)}>
315
+ <MenuShell>
253
316
  {project ? (
254
317
  <>
255
318
  <MenuSectionLabel>{t('Current project')}</MenuSectionLabel>
@@ -368,6 +431,9 @@ export function ProjectPickerButton({
368
431
  }) {
369
432
  const [open, setOpen] = useState(false);
370
433
  const [createOpen, setCreateOpen] = useState(false);
434
+ const rootRef = useRef<HTMLDivElement>(null);
435
+ const closeMenu = useCallback(() => setOpen(false), []);
436
+ useMenuDismiss(rootRef, open, closeMenu);
371
437
  const selected = value ? (projects.find((p) => p.id === value) ?? null) : null;
372
438
 
373
439
  const openCreate = () => {
@@ -376,7 +442,7 @@ export function ProjectPickerButton({
376
442
  };
377
443
 
378
444
  return (
379
- <div className="relative">
445
+ <div ref={rootRef} className="relative">
380
446
  <BadgeButton
381
447
  label={selected ? selected.name : t('Choose a project')}
382
448
  active={Boolean(selected)}
@@ -393,7 +459,7 @@ export function ProjectPickerButton({
393
459
  />
394
460
 
395
461
  {open && (
396
- <MenuShell onClose={() => setOpen(false)}>
462
+ <MenuShell>
397
463
  <MenuSectionLabel onAdd={openCreate}>{t('Project for new chat')}</MenuSectionLabel>
398
464
  <button
399
465
  type="button"
@@ -1,5 +1,4 @@
1
1
  import { useCallback, useEffect, useRef, useState } from 'react';
2
- import { LuMinimize2 } from 'react-icons/lu';
3
2
  import { t } from '@/i18n';
4
3
  import { hasHostBridge } from '@/lib/host-bridge';
5
4
  import {
@@ -14,7 +13,7 @@ export interface ContextUsagePillProps {
14
13
  chatId: string;
15
14
  messageCount: number;
16
15
  isStreaming: boolean;
17
- /** Optional latest in-memory turn usage when streaming just finished. */
16
+ /** Optional latest in-memory turn usage (note: `promptTokens` is cumulative across rounds). */
18
17
  livePromptTokens?: number;
19
18
  liveCachedPromptTokens?: number;
20
19
  onCompacted?: (result: CompactChatResult) => void;
@@ -30,6 +29,41 @@ function formatMegabytes(bytes: number): string {
30
29
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
31
30
  }
32
31
 
32
+ function ContextRing({ ratio }: { ratio: number }) {
33
+ const clamped = Math.max(0, Math.min(1, ratio));
34
+ const radius = 4.5;
35
+ const circumference = 2 * Math.PI * radius;
36
+ const dashOffset = circumference * (1 - clamped);
37
+ return (
38
+ <svg
39
+ viewBox="0 0 12 12"
40
+ aria-hidden="true"
41
+ className="h-3 w-3 shrink-0 -rotate-90"
42
+ >
43
+ <circle
44
+ cx="6"
45
+ cy="6"
46
+ r={radius}
47
+ fill="none"
48
+ stroke="currentColor"
49
+ strokeWidth="1.5"
50
+ className="opacity-25"
51
+ />
52
+ <circle
53
+ cx="6"
54
+ cy="6"
55
+ r={radius}
56
+ fill="none"
57
+ stroke="currentColor"
58
+ strokeWidth="1.5"
59
+ strokeLinecap="round"
60
+ strokeDasharray={circumference}
61
+ strokeDashoffset={dashOffset}
62
+ />
63
+ </svg>
64
+ );
65
+ }
66
+
33
67
  export function ContextUsagePill({
34
68
  chatId,
35
69
  messageCount,
@@ -81,35 +115,53 @@ export function ContextUsagePill({
81
115
  return () => window.removeEventListener('pointerdown', handlePointerDown);
82
116
  }, [open, refreshStatus]);
83
117
 
84
- if (!hasHostBridge() || messageCount === 0) {
118
+ if (!hasHostBridge() || messageCount === 0 || !status) {
85
119
  return null;
86
120
  }
87
121
 
88
- const maxTokens = status?.maxContextTokens ?? 131_072;
89
- const estTokens = Math.max(status?.estimatedTokens ?? 0, livePromptTokens ?? 0);
90
- const maxBytes = status?.maxRequestBytes ?? 4_194_304;
91
- const estBytes = status?.estimatedBytes ?? 0;
92
- const promptTokens = livePromptTokens ?? status?.promptTokens ?? 0;
93
- const cachedTokens = liveCachedPromptTokens ?? status?.cachedPromptTokens ?? 0;
122
+ const maxTokens = status.maxContextTokens || 131_072;
123
+ // Always use single-window `estimatedTokens` from `context-status`.
124
+ // `livePromptTokens` / `status.promptTokens` come from `TurnUsage.promptTokens`,
125
+ // which sums prompt tokens across every tool-loop round in the turn (e.g. 4.6M).
126
+ const estTokens = Math.max(0, status.estimatedTokens ?? 0);
127
+ const maxBytes = status.maxRequestBytes || 4_194_304;
128
+ const estBytes = Math.max(0, status.estimatedBytes ?? 0);
129
+ const rawPromptTokens = livePromptTokens ?? status.promptTokens ?? 0;
130
+ const cachedTokens = liveCachedPromptTokens ?? status.cachedPromptTokens ?? 0;
131
+ const singleStepPromptTokens =
132
+ rawPromptTokens > maxTokens
133
+ ? Math.max(estTokens, cachedTokens)
134
+ : rawPromptTokens;
94
135
  const cacheHitPct =
95
- promptTokens > 0 ? Math.min(100, Math.round((cachedTokens / promptTokens) * 100)) : null;
136
+ singleStepPromptTokens > 0 && cachedTokens <= singleStepPromptTokens
137
+ ? Math.min(100, Math.round((cachedTokens / singleStepPromptTokens) * 100))
138
+ : null;
96
139
 
97
140
  const tokenRatio = maxTokens > 0 ? Math.min(1, estTokens / maxTokens) : 0;
98
141
  const byteRatio = maxBytes > 0 ? Math.min(1, estBytes / maxBytes) : 0;
99
- const dominantRatio = Math.max(tokenRatio, byteRatio);
100
- const dominantPct = Math.round(dominantRatio * 100);
142
+ const isByteDominant = byteRatio > tokenRatio;
143
+ const dominantRatio = isByteDominant ? byteRatio : tokenRatio;
144
+ const rawPct = Math.round(dominantRatio * 100);
145
+ const dominantPct = estTokens > 0 || estBytes > 0 ? Math.max(1, rawPct) : 0;
146
+ const isElevated = dominantRatio >= 0.7;
147
+ const dominantSummary = isByteDominant
148
+ ? formatMegabytes(estBytes)
149
+ : formatCompactCount(estTokens);
150
+ const tooltipDetail = isByteDominant
151
+ ? `${formatCompactCount(estTokens)} / ${formatCompactCount(maxTokens)} · ${formatMegabytes(estBytes)} / ${formatMegabytes(maxBytes)}`
152
+ : `${formatCompactCount(estTokens)} / ${formatCompactCount(maxTokens)}`;
101
153
 
102
154
  const toneClass =
103
155
  dominantRatio >= 0.85
104
- ? 'border-red-500/40 bg-red-500/10 text-red-600 dark:text-red-400'
105
- : dominantRatio >= 0.7
106
- ? 'border-amber-500/40 bg-amber-500/10 text-amber-600 dark:text-amber-400'
107
- : 'border-agent-border/60 bg-agent-muted/40 text-agent-muted-foreground hover:text-agent-foreground';
156
+ ? 'border-red-500/40 bg-red-500/10 px-2 text-red-600 dark:text-red-400'
157
+ : isElevated
158
+ ? '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';
108
160
 
109
161
  const barColor =
110
162
  dominantRatio >= 0.85
111
163
  ? 'bg-red-500'
112
- : dominantRatio >= 0.7
164
+ : isElevated
113
165
  ? 'bg-amber-500'
114
166
  : 'bg-agent-primary';
115
167
 
@@ -144,12 +196,12 @@ export function ContextUsagePill({
144
196
  type="button"
145
197
  data-testid="context-usage-pill"
146
198
  onClick={() => setOpen((prev) => !prev)}
147
- title={t('Context window and payload usage')}
148
- className={`inline-flex items-center gap-1 rounded-full border px-2 py-0.5 text-[11px] leading-tight transition-colors ${toneClass}`}
199
+ title={`${t('Context window and payload usage')} (${tooltipDetail})`}
200
+ className={`inline-flex h-6 items-center gap-1 rounded-full border text-[11px] leading-tight tabular-nums transition-colors ${toneClass}`}
149
201
  >
150
- <LuMinimize2 className="h-3 w-3 shrink-0 opacity-80" />
202
+ <ContextRing ratio={dominantRatio} />
151
203
  <span>
152
- {formatCompactCount(estTokens)} · {dominantPct}%
204
+ {isElevated ? `${dominantSummary} · ${dominantPct}%` : `${dominantPct}%`}
153
205
  </span>
154
206
  </button>
155
207
 
@@ -67,10 +67,10 @@ export function ExecPolicyPicker({
67
67
  title={t(current.description)}
68
68
  onClick={() => setOpen((next) => !next)}
69
69
  className={[
70
- 'inline-flex h-6 max-w-[140px] items-center gap-1 rounded-full border px-1.5 text-[12px] leading-[1.45] transition-colors disabled:cursor-not-allowed disabled:opacity-70',
70
+ 'inline-flex h-6 max-w-[140px] items-center gap-1 rounded-full border px-2 text-[12px] leading-[1.45] transition-colors disabled:cursor-not-allowed disabled:opacity-70',
71
71
  isFull
72
72
  ? 'border-amber-400/50 bg-amber-400/10 text-amber-700 dark:text-amber-400'
73
- : 'border-agent-border bg-agent-canvas text-agent-foreground hover:bg-agent-foreground/5',
73
+ : 'border-transparent bg-transparent text-agent-muted-foreground hover:bg-agent-foreground/5 hover:text-agent-foreground',
74
74
  ].join(' ')}
75
75
  >
76
76
  {isFull ? (