@steerable/agent-shell-web 0.12.8 → 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.
- package/package.json +4 -4
- package/src/components/AgentSidebar.tsx +9 -4
- package/src/components/SafeRecoveryBanner.tsx +64 -0
- package/src/components/chat/ApprovalModal.tsx +196 -133
- package/src/components/chat/AssistantMessage.tsx +40 -25
- package/src/components/chat/ChatInput.tsx +15 -1
- package/src/components/chat/ContextUsagePill.tsx +249 -0
- package/src/components/chat/MemorySaveNoticeBar.tsx +88 -0
- package/src/components/chat/MessageList.tsx +39 -0
- package/src/components/chat/TurnFailureCard.tsx +8 -0
- package/src/components/chat/turn-failure.ts +15 -3
- package/src/components/settings/DiagnoseSettingsPanel.tsx +2 -0
- package/src/components/settings/MemorySettingsPanel.tsx +370 -0
- package/src/layouts/AgentLayout.tsx +47 -1
- package/src/lib/browser-dev-host-mock.ts +134 -0
- package/src/lib/host-bridge.ts +2 -0
- package/src/lib/local-api.ts +48 -0
- package/src/lib/safe-mode.ts +83 -0
- package/src/packs/registry.ts +7 -0
- package/src/pages/AgentPage.tsx +82 -13
- package/src/pages/SettingsPage.tsx +11 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { t } from '@/i18n';
|
|
3
|
+
import { getHostBridge, hasHostBridge } from '@/lib/host-bridge';
|
|
4
|
+
|
|
5
|
+
export type MemoryScopeTab = 'user' | 'workspace' | 'tenant';
|
|
6
|
+
export type MemoryPermissionMode = 'off' | 'ask' | 'auto';
|
|
7
|
+
|
|
8
|
+
export interface RefinementItemDto {
|
|
9
|
+
id: string;
|
|
10
|
+
normalizedHash: string;
|
|
11
|
+
scope: MemoryScopeTab;
|
|
12
|
+
content: string;
|
|
13
|
+
humanEdited: boolean;
|
|
14
|
+
status: 'active' | 'ordinary' | 'withdrawn';
|
|
15
|
+
createdAt: string;
|
|
16
|
+
expiresAt: string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface MemoryApiResponse {
|
|
20
|
+
permissionMode: MemoryPermissionMode;
|
|
21
|
+
scope: MemoryScopeTab;
|
|
22
|
+
scopeId: string;
|
|
23
|
+
contentMarkdown: string;
|
|
24
|
+
contentSha256: string;
|
|
25
|
+
bytesUsed: number;
|
|
26
|
+
maxBytes: number;
|
|
27
|
+
linesUsed: number;
|
|
28
|
+
maxLines: number;
|
|
29
|
+
refinements: RefinementItemDto[];
|
|
30
|
+
revokedHashes: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function requestMemoryApi(
|
|
34
|
+
method: 'GET' | 'PUT' | 'POST' | 'DELETE',
|
|
35
|
+
path: string,
|
|
36
|
+
body?: Record<string, unknown>,
|
|
37
|
+
): Promise<{ status: number; data: Record<string, unknown> }> {
|
|
38
|
+
if (hasHostBridge()) {
|
|
39
|
+
const bridge = getHostBridge();
|
|
40
|
+
if (!bridge?.localBackend) return { status: 503, data: {} };
|
|
41
|
+
try {
|
|
42
|
+
const res = await bridge.localBackend.request<Record<string, unknown>>({
|
|
43
|
+
method,
|
|
44
|
+
path,
|
|
45
|
+
body,
|
|
46
|
+
});
|
|
47
|
+
if (
|
|
48
|
+
res &&
|
|
49
|
+
typeof res === 'object' &&
|
|
50
|
+
typeof (res as { status?: unknown }).status === 'number' &&
|
|
51
|
+
'data' in res
|
|
52
|
+
) {
|
|
53
|
+
return {
|
|
54
|
+
status: Number((res as { status: number }).status),
|
|
55
|
+
data: ((res as { data?: unknown }).data ?? {}) as Record<string, unknown>,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return { status: 200, data: (res ?? {}) as Record<string, unknown> };
|
|
59
|
+
} catch (err) {
|
|
60
|
+
const status =
|
|
61
|
+
typeof (err as { status?: unknown })?.status === 'number'
|
|
62
|
+
? Number((err as { status: number }).status)
|
|
63
|
+
: 500;
|
|
64
|
+
return {
|
|
65
|
+
status,
|
|
66
|
+
data: { message: err instanceof Error ? err.message : String(err) },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (typeof fetch !== 'function' || !path.startsWith('http') && typeof window !== 'undefined' && window.location?.origin === 'null') {
|
|
71
|
+
return { status: 503, data: {} };
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const resp = await fetch(path, {
|
|
75
|
+
method,
|
|
76
|
+
headers: body ? { 'Content-Type': 'application/json' } : undefined,
|
|
77
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
78
|
+
});
|
|
79
|
+
const json = (await resp.json().catch(() => ({}))) as Record<string, unknown>;
|
|
80
|
+
return { status: resp.status, data: json };
|
|
81
|
+
} catch {
|
|
82
|
+
return { status: 503, data: {} };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface MemorySettingsPanelProps {
|
|
87
|
+
workspaceId?: string;
|
|
88
|
+
showTenantTab?: boolean;
|
|
89
|
+
isAdmin?: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function MemorySettingsPanel({
|
|
93
|
+
workspaceId = 'default',
|
|
94
|
+
showTenantTab = false,
|
|
95
|
+
isAdmin = true,
|
|
96
|
+
}: MemorySettingsPanelProps) {
|
|
97
|
+
const [scope, setScope] = useState<MemoryScopeTab>('user');
|
|
98
|
+
const [permissionMode, setPermissionMode] = useState<MemoryPermissionMode>('ask');
|
|
99
|
+
const [markdown, setMarkdown] = useState('');
|
|
100
|
+
const [baseHash, setBaseHash] = useState('');
|
|
101
|
+
const [bytesUsed, setBytesUsed] = useState(0);
|
|
102
|
+
const [maxBytes, setMaxBytes] = useState(16384);
|
|
103
|
+
const [refinements, setRefinements] = useState<RefinementItemDto[]>([]);
|
|
104
|
+
const [revokedHashes, setRevokedHashes] = useState<string[]>([]);
|
|
105
|
+
const [conflictBanner, setConflictBanner] = useState(false);
|
|
106
|
+
const [saving, setSaving] = useState(false);
|
|
107
|
+
|
|
108
|
+
const loadScope = useCallback(
|
|
109
|
+
async (targetScope: MemoryScopeTab) => {
|
|
110
|
+
const scopeParam =
|
|
111
|
+
targetScope === 'workspace' ? `&scopeId=${encodeURIComponent(workspaceId)}` : '';
|
|
112
|
+
const res = await requestMemoryApi(
|
|
113
|
+
'GET',
|
|
114
|
+
`/api/v2/memory?scope=${targetScope}${scopeParam}`,
|
|
115
|
+
);
|
|
116
|
+
if (res.status === 200) {
|
|
117
|
+
const d = res.data as unknown as MemoryApiResponse;
|
|
118
|
+
setPermissionMode(d.permissionMode ?? 'ask');
|
|
119
|
+
setMarkdown(d.contentMarkdown ?? '');
|
|
120
|
+
setBaseHash(d.contentSha256 ?? '');
|
|
121
|
+
setBytesUsed(d.bytesUsed ?? 0);
|
|
122
|
+
setMaxBytes(d.maxBytes || 16384);
|
|
123
|
+
setRefinements(Array.isArray(d.refinements) ? d.refinements : []);
|
|
124
|
+
setRevokedHashes(Array.isArray(d.revokedHashes) ? d.revokedHashes : []);
|
|
125
|
+
setConflictBanner(false);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
[workspaceId],
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
void loadScope(scope);
|
|
133
|
+
}, [scope, loadScope]);
|
|
134
|
+
|
|
135
|
+
const handleModeChange = async (nextMode: MemoryPermissionMode) => {
|
|
136
|
+
setPermissionMode(nextMode);
|
|
137
|
+
await requestMemoryApi('PUT', '/api/v2/memory', { permissionMode: nextMode });
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const readOnly = scope === 'tenant' && !isAdmin;
|
|
141
|
+
|
|
142
|
+
const handleSave = async () => {
|
|
143
|
+
if (readOnly) return;
|
|
144
|
+
setSaving(true);
|
|
145
|
+
try {
|
|
146
|
+
const res = await requestMemoryApi('PUT', '/api/v2/memory', {
|
|
147
|
+
scope,
|
|
148
|
+
...(scope === 'workspace' ? { scopeId: workspaceId } : {}),
|
|
149
|
+
contentMarkdown: markdown,
|
|
150
|
+
baseHash,
|
|
151
|
+
});
|
|
152
|
+
if (res.status === 409) {
|
|
153
|
+
setConflictBanner(true);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (res.status === 200) {
|
|
157
|
+
await loadScope(scope);
|
|
158
|
+
}
|
|
159
|
+
} finally {
|
|
160
|
+
setSaving(false);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const handleRefinementAction = async (
|
|
165
|
+
id: string,
|
|
166
|
+
action: 'renew' | 'convert' | 'withdraw',
|
|
167
|
+
) => {
|
|
168
|
+
await requestMemoryApi('POST', `/api/v2/memory/refinements/${encodeURIComponent(id)}/${action}`, {
|
|
169
|
+
workspaceId,
|
|
170
|
+
});
|
|
171
|
+
await loadScope(scope);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const activeRefinements = refinements.filter((r) => r.status === 'active');
|
|
175
|
+
const ratio = Math.min(100, Math.round((bytesUsed / Math.max(1, maxBytes)) * 100));
|
|
176
|
+
|
|
177
|
+
return (
|
|
178
|
+
<div
|
|
179
|
+
data-testid="memory-settings-panel"
|
|
180
|
+
className="space-y-4 rounded-xl border border-agent-border bg-agent-card p-4 text-xs"
|
|
181
|
+
>
|
|
182
|
+
{/* Top bar: permission mode + quota meter */}
|
|
183
|
+
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
184
|
+
<div className="flex items-center gap-1.5" role="radiogroup" aria-label={t('Memory mode')}>
|
|
185
|
+
{(
|
|
186
|
+
[
|
|
187
|
+
['off', t('Off')],
|
|
188
|
+
['ask', t('Ask before saving')],
|
|
189
|
+
['auto', t('Remember automatically')],
|
|
190
|
+
] as const
|
|
191
|
+
).map(([val, label]) => (
|
|
192
|
+
<button
|
|
193
|
+
key={val}
|
|
194
|
+
type="button"
|
|
195
|
+
data-testid={`memory-mode-${val}`}
|
|
196
|
+
onClick={() => void handleModeChange(val)}
|
|
197
|
+
className={`rounded-md px-2.5 py-1 text-xs transition ${
|
|
198
|
+
permissionMode === val
|
|
199
|
+
? 'bg-agent-foreground font-semibold text-agent-background'
|
|
200
|
+
: 'border border-agent-border text-agent-muted-foreground hover:text-agent-foreground'
|
|
201
|
+
}`}
|
|
202
|
+
>
|
|
203
|
+
{label}
|
|
204
|
+
</button>
|
|
205
|
+
))}
|
|
206
|
+
</div>
|
|
207
|
+
<div className="flex items-center gap-2 text-[11px] text-agent-muted-foreground">
|
|
208
|
+
<span data-testid="memory-quota-label">
|
|
209
|
+
{(bytesUsed / 1024).toFixed(1)} / {(maxBytes / 1024).toFixed(1)} KB
|
|
210
|
+
</span>
|
|
211
|
+
<div className="h-1.5 w-24 overflow-hidden rounded-full bg-agent-border">
|
|
212
|
+
<div className="h-full bg-agent-foreground" style={{ width: `${ratio}%` }} />
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
</div>
|
|
216
|
+
|
|
217
|
+
{/* Scope tabs */}
|
|
218
|
+
<div className="flex items-center gap-2 border-b border-agent-border pb-2">
|
|
219
|
+
<button
|
|
220
|
+
type="button"
|
|
221
|
+
data-testid="memory-tab-user"
|
|
222
|
+
onClick={() => setScope('user')}
|
|
223
|
+
className={`rounded px-2.5 py-1 ${
|
|
224
|
+
scope === 'user' ? 'bg-agent-foreground/10 font-semibold' : 'text-agent-muted-foreground'
|
|
225
|
+
}`}
|
|
226
|
+
>
|
|
227
|
+
{t('Personal memory')}
|
|
228
|
+
</button>
|
|
229
|
+
<button
|
|
230
|
+
type="button"
|
|
231
|
+
data-testid="memory-tab-workspace"
|
|
232
|
+
onClick={() => setScope('workspace')}
|
|
233
|
+
className={`rounded px-2.5 py-1 ${
|
|
234
|
+
scope === 'workspace'
|
|
235
|
+
? 'bg-agent-foreground/10 font-semibold'
|
|
236
|
+
: 'text-agent-muted-foreground'
|
|
237
|
+
}`}
|
|
238
|
+
>
|
|
239
|
+
{t('Project memory')}
|
|
240
|
+
</button>
|
|
241
|
+
{showTenantTab ? (
|
|
242
|
+
<button
|
|
243
|
+
type="button"
|
|
244
|
+
data-testid="memory-tab-tenant"
|
|
245
|
+
onClick={() => setScope('tenant')}
|
|
246
|
+
className={`rounded px-2.5 py-1 ${
|
|
247
|
+
scope === 'tenant'
|
|
248
|
+
? 'bg-agent-foreground/10 font-semibold'
|
|
249
|
+
: 'text-agent-muted-foreground'
|
|
250
|
+
}`}
|
|
251
|
+
>
|
|
252
|
+
{t('Department shared rules')}
|
|
253
|
+
</button>
|
|
254
|
+
) : null}
|
|
255
|
+
</div>
|
|
256
|
+
|
|
257
|
+
{/* Optimistic lock conflict banner */}
|
|
258
|
+
{conflictBanner ? (
|
|
259
|
+
<div
|
|
260
|
+
role="alert"
|
|
261
|
+
data-testid="memory-conflict-banner"
|
|
262
|
+
className="flex items-center justify-between gap-2 rounded-lg border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-amber-900 dark:text-amber-200"
|
|
263
|
+
>
|
|
264
|
+
<span>{t('Memory changed in another session. Reload before saving.')}</span>
|
|
265
|
+
<button
|
|
266
|
+
type="button"
|
|
267
|
+
data-testid="memory-conflict-reload"
|
|
268
|
+
onClick={() => void loadScope(scope)}
|
|
269
|
+
className="rounded bg-amber-600 px-2 py-1 text-white"
|
|
270
|
+
>
|
|
271
|
+
{t('Reload')}
|
|
272
|
+
</button>
|
|
273
|
+
</div>
|
|
274
|
+
) : null}
|
|
275
|
+
|
|
276
|
+
{/* MEMORY.md editor */}
|
|
277
|
+
<div className="space-y-2">
|
|
278
|
+
<textarea
|
|
279
|
+
rows={6}
|
|
280
|
+
readOnly={readOnly}
|
|
281
|
+
value={markdown}
|
|
282
|
+
onChange={(e) => setMarkdown(e.target.value)}
|
|
283
|
+
aria-label={t('Memory markdown editor')}
|
|
284
|
+
data-testid="memory-markdown-editor"
|
|
285
|
+
className="w-full rounded-lg border border-agent-border bg-agent-background p-2.5 font-mono text-xs"
|
|
286
|
+
/>
|
|
287
|
+
{!readOnly ? (
|
|
288
|
+
<div className="flex justify-end">
|
|
289
|
+
<button
|
|
290
|
+
type="button"
|
|
291
|
+
disabled={saving}
|
|
292
|
+
onClick={() => void handleSave()}
|
|
293
|
+
data-testid="memory-save-btn"
|
|
294
|
+
className="rounded-md bg-agent-foreground px-3 py-1.5 text-xs font-medium text-agent-background disabled:opacity-50"
|
|
295
|
+
>
|
|
296
|
+
{t('Save memory')}
|
|
297
|
+
</button>
|
|
298
|
+
</div>
|
|
299
|
+
) : (
|
|
300
|
+
<p className="text-[11px] text-agent-muted-foreground">
|
|
301
|
+
{t('Read-only: only administrators can edit department shared rules.')}
|
|
302
|
+
</p>
|
|
303
|
+
)}
|
|
304
|
+
</div>
|
|
305
|
+
|
|
306
|
+
{/* Active RSI refinement review queue */}
|
|
307
|
+
{activeRefinements.length > 0 ? (
|
|
308
|
+
<div className="space-y-2 border-t border-agent-border pt-3">
|
|
309
|
+
<div className="font-semibold text-agent-foreground">
|
|
310
|
+
{t('Refinement notes pending review (30-day expiry)')}
|
|
311
|
+
</div>
|
|
312
|
+
{activeRefinements.map((item) => (
|
|
313
|
+
<div
|
|
314
|
+
key={item.id}
|
|
315
|
+
data-testid={`memory-refinement-${item.id}`}
|
|
316
|
+
className="flex flex-wrap items-center justify-between gap-2 rounded-lg border border-agent-border p-2.5"
|
|
317
|
+
>
|
|
318
|
+
<div className="space-y-0.5">
|
|
319
|
+
<div className="text-agent-foreground">{item.content}</div>
|
|
320
|
+
<div className="font-mono text-[11px] text-agent-muted-foreground">
|
|
321
|
+
{item.id}
|
|
322
|
+
{item.expiresAt ? ` · ${t('Expires')} ${item.expiresAt.slice(0, 10)}` : ''}
|
|
323
|
+
</div>
|
|
324
|
+
</div>
|
|
325
|
+
{!readOnly ? (
|
|
326
|
+
<div className="flex items-center gap-1.5">
|
|
327
|
+
<button
|
|
328
|
+
type="button"
|
|
329
|
+
data-testid={`memory-renew-${item.id}`}
|
|
330
|
+
onClick={() => void handleRefinementAction(item.id, 'renew')}
|
|
331
|
+
className="rounded border border-agent-border px-2 py-1 hover:bg-agent-foreground/5"
|
|
332
|
+
>
|
|
333
|
+
{t('Renew 30d')}
|
|
334
|
+
</button>
|
|
335
|
+
<button
|
|
336
|
+
type="button"
|
|
337
|
+
data-testid={`memory-convert-${item.id}`}
|
|
338
|
+
onClick={() => void handleRefinementAction(item.id, 'convert')}
|
|
339
|
+
className="rounded border border-agent-border px-2 py-1 hover:bg-agent-foreground/5"
|
|
340
|
+
>
|
|
341
|
+
{t('Make ordinary')}
|
|
342
|
+
</button>
|
|
343
|
+
<button
|
|
344
|
+
type="button"
|
|
345
|
+
data-testid={`memory-withdraw-${item.id}`}
|
|
346
|
+
onClick={() => void handleRefinementAction(item.id, 'withdraw')}
|
|
347
|
+
className="rounded border border-rose-500/40 px-2 py-1 text-rose-600 hover:bg-rose-500/10"
|
|
348
|
+
>
|
|
349
|
+
{t('Withdraw')}
|
|
350
|
+
</button>
|
|
351
|
+
</div>
|
|
352
|
+
) : null}
|
|
353
|
+
</div>
|
|
354
|
+
))}
|
|
355
|
+
</div>
|
|
356
|
+
) : null}
|
|
357
|
+
|
|
358
|
+
{/* Revoked hashes count */}
|
|
359
|
+
{revokedHashes.length > 0 ? (
|
|
360
|
+
<div className="text-[11px] text-agent-muted-foreground" data-testid="memory-revoked-summary">
|
|
361
|
+
{t('{count} withdrawn items blocked from re-learning.', {
|
|
362
|
+
count: String(revokedHashes.length),
|
|
363
|
+
})}
|
|
364
|
+
</div>
|
|
365
|
+
) : null}
|
|
366
|
+
</div>
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export default MemorySettingsPanel;
|
|
@@ -8,11 +8,18 @@ import { TaskProcessPanel, type InspectedTask } from '@/components/chat/TaskProc
|
|
|
8
8
|
import { AskUserPromptProvider } from '@/components/chat/AskUserPromptProvider';
|
|
9
9
|
import { ApprovalPromptProvider } from '@/components/chat/ApprovalPromptProvider';
|
|
10
10
|
import { InsightsConsentBanner } from '@/components/settings/InsightsSettingsPanel';
|
|
11
|
+
import { SafeRecoveryBanner } from '@/components/SafeRecoveryBanner';
|
|
12
|
+
import {
|
|
13
|
+
fetchActiveApplication,
|
|
14
|
+
isSafeRecoveryMode,
|
|
15
|
+
type ActiveApplicationState,
|
|
16
|
+
type HiddenNavItem,
|
|
17
|
+
} from '@/lib/safe-mode';
|
|
11
18
|
import { trackBehavior } from '@/lib/insights';
|
|
12
19
|
import { getHostBridge, hasHostBridge } from '@/lib/host-bridge';
|
|
13
20
|
import { deleteChatIfEmpty, pruneEmptyChats } from '@/lib/local-api';
|
|
14
21
|
import { PORTABLE_CHATS_CHANGED_EVENT } from '@/lib/portable';
|
|
15
|
-
import { getPackChatSlots, type PackChatSlotContribution } from '@/packs/registry';
|
|
22
|
+
import { getPackChatSlots, getPackTopBanners, type PackChatSlotContribution } from '@/packs/registry';
|
|
16
23
|
import {
|
|
17
24
|
useChatsAndAgents,
|
|
18
25
|
type UseChatsAndAgentsResult,
|
|
@@ -694,6 +701,33 @@ function AgentLayoutContent() {
|
|
|
694
701
|
trackBehavior('app_open', { path: window.location.hash || '#/' });
|
|
695
702
|
}, []);
|
|
696
703
|
|
|
704
|
+
const [safeModeActive, setSafeModeActive] = useState<boolean>(() => isSafeRecoveryMode());
|
|
705
|
+
const [activeApp, setActiveApp] = useState<ActiveApplicationState | null>(null);
|
|
706
|
+
|
|
707
|
+
useEffect(() => {
|
|
708
|
+
const syncSafe = () => setSafeModeActive(isSafeRecoveryMode());
|
|
709
|
+
window.addEventListener('hashchange', syncSafe);
|
|
710
|
+
window.addEventListener('popstate', syncSafe);
|
|
711
|
+
return () => {
|
|
712
|
+
window.removeEventListener('hashchange', syncSafe);
|
|
713
|
+
window.removeEventListener('popstate', syncSafe);
|
|
714
|
+
};
|
|
715
|
+
}, []);
|
|
716
|
+
|
|
717
|
+
useEffect(() => {
|
|
718
|
+
let cancelled = false;
|
|
719
|
+
void fetchActiveApplication(safeModeActive).then((app) => {
|
|
720
|
+
if (!cancelled) setActiveApp(app);
|
|
721
|
+
});
|
|
722
|
+
return () => {
|
|
723
|
+
cancelled = true;
|
|
724
|
+
};
|
|
725
|
+
}, [safeModeActive]);
|
|
726
|
+
|
|
727
|
+
const hiddenNavItems: readonly HiddenNavItem[] = safeModeActive
|
|
728
|
+
? []
|
|
729
|
+
: (activeApp?.hiddenNavItems ?? []);
|
|
730
|
+
|
|
697
731
|
return (
|
|
698
732
|
<div className="relative flex h-full w-full bg-agent-muted/30">
|
|
699
733
|
{sidebarCollapsed ? (
|
|
@@ -719,6 +753,7 @@ function AgentLayoutContent() {
|
|
|
719
753
|
rightPanel={rightPanel}
|
|
720
754
|
onToggleRightPanel={toggleRightPanel}
|
|
721
755
|
onCollapse={toggleSidebarCollapsed}
|
|
756
|
+
hiddenNavItems={hiddenNavItems}
|
|
722
757
|
/>
|
|
723
758
|
</div>
|
|
724
759
|
{/* Drag handle — 1px wide, 4px hit area via padding. */}
|
|
@@ -736,6 +771,17 @@ function AgentLayoutContent() {
|
|
|
736
771
|
<div className="h-full min-w-0 flex-1 p-1.5">
|
|
737
772
|
<div className="flex h-full w-full">
|
|
738
773
|
<div className="flex h-full min-w-0 flex-1 flex-col overflow-hidden rounded-agent-lg bg-agent-canvas shadow-sm">
|
|
774
|
+
{safeModeActive && (
|
|
775
|
+
<SafeRecoveryBanner
|
|
776
|
+
onResetComplete={() => {
|
|
777
|
+
setSafeModeActive(false);
|
|
778
|
+
void fetchActiveApplication(false).then(setActiveApp);
|
|
779
|
+
}}
|
|
780
|
+
/>
|
|
781
|
+
)}
|
|
782
|
+
{getPackTopBanners().map((BannerComponent, idx) => (
|
|
783
|
+
<BannerComponent key={idx} />
|
|
784
|
+
))}
|
|
739
785
|
<div className="min-h-0 flex-1 overflow-hidden">
|
|
740
786
|
<Outlet
|
|
741
787
|
context={{
|
|
@@ -466,6 +466,67 @@ async function request<T>(input: LocalBackendRequestInput): Promise<T> {
|
|
|
466
466
|
return { tasks: [] } as T;
|
|
467
467
|
}
|
|
468
468
|
|
|
469
|
+
if (method === 'GET' && rest === '/context-status') {
|
|
470
|
+
const msgs = state.messagesByChatId[chatId] ?? [];
|
|
471
|
+
const rawChars = msgs.reduce((sum, m) => sum + (m.content?.length ?? 0), 0);
|
|
472
|
+
const hasSummary = msgs.some((m) => m.content?.includes('<summary>'));
|
|
473
|
+
const estimatedTokens = hasSummary
|
|
474
|
+
? Math.max(1_800, Math.round(rawChars / 3.5))
|
|
475
|
+
: Math.max(92_000, Math.round(rawChars / 3.5) + 90_000);
|
|
476
|
+
const estimatedBytes = Math.max(16_384, rawChars * 2);
|
|
477
|
+
return {
|
|
478
|
+
chatId,
|
|
479
|
+
recordId: `rec-${chatId}`,
|
|
480
|
+
estimatedTokens,
|
|
481
|
+
maxContextTokens: 128_000,
|
|
482
|
+
estimatedBytes,
|
|
483
|
+
maxRequestBytes: 4_194_304,
|
|
484
|
+
promptTokens: estimatedTokens,
|
|
485
|
+
cachedPromptTokens: Math.round(estimatedTokens * 0.62),
|
|
486
|
+
cacheCreationTokens: 0,
|
|
487
|
+
compactionCount: msgs.filter((m) => m.content?.includes('<summary>')).length,
|
|
488
|
+
compactionEpoch: msgs.filter((m) => m.content?.includes('<summary>')).length,
|
|
489
|
+
} as T;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (method === 'POST' && rest === '/compact') {
|
|
493
|
+
const body = (input.body ?? {}) as {
|
|
494
|
+
focus?: string;
|
|
495
|
+
keepHeadTurns?: number;
|
|
496
|
+
keepTailMessages?: number;
|
|
497
|
+
};
|
|
498
|
+
const msgs = state.messagesByChatId[chatId] ?? [];
|
|
499
|
+
const keepTail = Math.max(1, body.keepTailMessages ?? 2);
|
|
500
|
+
const tail = msgs.slice(-keepTail);
|
|
501
|
+
const preTokens = Math.max(92_000, msgs.length * 4_500);
|
|
502
|
+
const postTokens = Math.max(3_200, tail.length * 1_600);
|
|
503
|
+
const focusNote = body.focus?.trim()
|
|
504
|
+
? `Focused retention: ${body.focus.trim()}\n- `
|
|
505
|
+
: '';
|
|
506
|
+
const summaryText = `<summary>\n- ${focusNote}Compacted ${Math.max(1, msgs.length - tail.length)} earlier messages in the interactive browser demo while preserving active task state and deliverables.\n</summary>`;
|
|
507
|
+
const summaryMsg: MutableMessage = {
|
|
508
|
+
id: `msg-compact-${Date.now()}`,
|
|
509
|
+
chatId,
|
|
510
|
+
role: 'user',
|
|
511
|
+
content: summaryText,
|
|
512
|
+
createdAt: new Date().toISOString(),
|
|
513
|
+
messageMetadata: JSON.stringify({ compactionSummary: true, preTokens, postTokens }),
|
|
514
|
+
};
|
|
515
|
+
state.messagesByChatId[chatId] = [summaryMsg, ...tail];
|
|
516
|
+
const epochs = state.messagesByChatId[chatId].filter((m) =>
|
|
517
|
+
m.content?.includes('<summary>'),
|
|
518
|
+
).length;
|
|
519
|
+
return {
|
|
520
|
+
compacted: Math.max(1, msgs.length - tail.length),
|
|
521
|
+
preTokens,
|
|
522
|
+
postTokens,
|
|
523
|
+
preBytes: preTokens * 4,
|
|
524
|
+
postBytes: postTokens * 4,
|
|
525
|
+
summaryText,
|
|
526
|
+
compactionEpoch: epochs,
|
|
527
|
+
} as T;
|
|
528
|
+
}
|
|
529
|
+
|
|
469
530
|
if (method === 'GET' && rest === '/project-context') {
|
|
470
531
|
const chat = state.chats.find((c) => c.id === chatId);
|
|
471
532
|
const project = chat?.projectId
|
|
@@ -742,6 +803,79 @@ async function request<T>(input: LocalBackendRequestInput): Promise<T> {
|
|
|
742
803
|
} as T;
|
|
743
804
|
}
|
|
744
805
|
|
|
806
|
+
if (method === 'GET' && cleanPath === '/api/v2/auth/bootstrap') {
|
|
807
|
+
return {
|
|
808
|
+
applicationId: 'default',
|
|
809
|
+
safeMode: false,
|
|
810
|
+
brand: { displayName: 'Steerable Workbench' },
|
|
811
|
+
authMethods: [{ kind: 'local', id: 'local', label: 'Local session' }],
|
|
812
|
+
} as T;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
if (method === 'GET' && cleanPath === '/api/v2/applications') {
|
|
816
|
+
return {
|
|
817
|
+
activeApplicationId: 'default',
|
|
818
|
+
safeMode: false,
|
|
819
|
+
applications: [
|
|
820
|
+
{
|
|
821
|
+
id: 'default',
|
|
822
|
+
name: 'Default Workbench',
|
|
823
|
+
description: 'Standard full-featured agent workbench',
|
|
824
|
+
},
|
|
825
|
+
],
|
|
826
|
+
} as T;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
if (method === 'GET' && cleanPath === '/api/v2/memory') {
|
|
830
|
+
const scope = parseQuery(path).get('scope') ?? 'user';
|
|
831
|
+
return {
|
|
832
|
+
scope,
|
|
833
|
+
scopeId: scope === 'user' ? 'demo-user' : 'default',
|
|
834
|
+
permissionMode: 'ask',
|
|
835
|
+
markdown: '- Prefer concise tables with source citations.\n',
|
|
836
|
+
baseHash: 'demo00000001',
|
|
837
|
+
bytesUsed: 46,
|
|
838
|
+
maxBytes: 16_384,
|
|
839
|
+
linesUsed: 1,
|
|
840
|
+
maxLines: 200,
|
|
841
|
+
canWrite: true,
|
|
842
|
+
entries: [
|
|
843
|
+
{
|
|
844
|
+
id: 'mem-demo-1',
|
|
845
|
+
kind: 'ordinary',
|
|
846
|
+
content: 'Prefer concise tables with source citations.',
|
|
847
|
+
contentHash: 'demo00000001',
|
|
848
|
+
createdAt: new Date().toISOString(),
|
|
849
|
+
updatedAt: new Date().toISOString(),
|
|
850
|
+
humanEdited: true,
|
|
851
|
+
},
|
|
852
|
+
],
|
|
853
|
+
} as T;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
if (method === 'GET' && /^\/api\/v2\/chats\/[^/]+\/fact-chain$/.test(cleanPath)) {
|
|
857
|
+
const chatId = decodeURIComponent(cleanPath.split('/')[4] ?? 'demo');
|
|
858
|
+
return {
|
|
859
|
+
chatId,
|
|
860
|
+
evidenceStatus: 'complete',
|
|
861
|
+
requestNode: {
|
|
862
|
+
messageId: 'msg-demo-1',
|
|
863
|
+
promptSummary: 'Summarize weekly invoices and export a verified report.',
|
|
864
|
+
policyDigest: '9f86d081884c7d65',
|
|
865
|
+
},
|
|
866
|
+
toolNodes: [
|
|
867
|
+
{
|
|
868
|
+
id: 'tc-demo-1',
|
|
869
|
+
toolName: 'read_file',
|
|
870
|
+
argsSummary: '{"path":"invoices/2026-11.csv"}',
|
|
871
|
+
status: 'executed',
|
|
872
|
+
durationMs: 18,
|
|
873
|
+
},
|
|
874
|
+
],
|
|
875
|
+
deliverableNodes: [],
|
|
876
|
+
} as T;
|
|
877
|
+
}
|
|
878
|
+
|
|
745
879
|
if (method === 'GET' && cleanPath === '/api/v2/llm/presets') {
|
|
746
880
|
return {
|
|
747
881
|
presets: [
|
package/src/lib/host-bridge.ts
CHANGED
package/src/lib/local-api.ts
CHANGED
|
@@ -649,6 +649,54 @@ export async function forkChat(chatId: string) {
|
|
|
649
649
|
});
|
|
650
650
|
}
|
|
651
651
|
|
|
652
|
+
export interface CompactChatOptions {
|
|
653
|
+
focus?: string;
|
|
654
|
+
keepHeadTurns?: number;
|
|
655
|
+
keepTailMessages?: number;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
export interface CompactChatResult {
|
|
659
|
+
compacted: number;
|
|
660
|
+
queuedOnActiveTurn?: boolean;
|
|
661
|
+
preTokens?: number;
|
|
662
|
+
postTokens?: number;
|
|
663
|
+
preBytes?: number;
|
|
664
|
+
postBytes?: number;
|
|
665
|
+
summaryText?: string;
|
|
666
|
+
compactionEpoch?: number;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/** Authoritatively compact a chat session's context (Phase 10 R3). */
|
|
670
|
+
export async function compactChat(chatId: string, options: CompactChatOptions = {}) {
|
|
671
|
+
return bridge().localBackend.request<CompactChatResult>({
|
|
672
|
+
method: 'POST',
|
|
673
|
+
path: `/api/v2/chats/${encodeURIComponent(chatId)}/compact`,
|
|
674
|
+
body: options,
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
export interface ChatContextStatus {
|
|
679
|
+
chatId: string;
|
|
680
|
+
recordId: string;
|
|
681
|
+
estimatedTokens: number;
|
|
682
|
+
maxContextTokens: number;
|
|
683
|
+
estimatedBytes: number;
|
|
684
|
+
maxRequestBytes: number;
|
|
685
|
+
promptTokens: number;
|
|
686
|
+
cachedPromptTokens: number;
|
|
687
|
+
cacheCreationTokens: number;
|
|
688
|
+
compactionCount: number;
|
|
689
|
+
compactionEpoch: number;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** Read live token & wire-byte budget status for a chat (Phase 10 R3). */
|
|
693
|
+
export async function getChatContextStatus(chatId: string) {
|
|
694
|
+
return bridge().localBackend.request<ChatContextStatus>({
|
|
695
|
+
method: 'GET',
|
|
696
|
+
path: `/api/v2/chats/${encodeURIComponent(chatId)}/context-status`,
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
|
|
652
700
|
/** 用户重命名。409 表示会话锁被占用,调用方恢复旧名。 */
|
|
653
701
|
export async function updateChatTitle(chatId: string, title: string) {
|
|
654
702
|
return bridge().localBackend.request<{ id: string; title: string }>({
|