@kabmo-labs/kabmo 0.10.1 → 0.11.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/dist/client/index.html +1 -1
- package/dist/client/index.js +306 -306
- package/dist/client/static/js/index-bq6qeayr.css +1 -0
- package/dist/client/static/js/{index-135sdc46.js → index-nqvfpgm2.js} +649 -648
- package/package.json +1 -1
- package/src/client/components/common/code-surface/theme.ts +11 -5
- package/src/client/components/workspace/chat-timeline/chat-input/access-levels.ts +11 -2
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/ApprovalBody.tsx +65 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/Header.tsx +21 -2
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/SelectBody.tsx +35 -19
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/approval-gate.test.tsx +145 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/index.tsx +32 -104
- package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/shortcuts.tsx +166 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.test.ts +30 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Proc.tsx +33 -27
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +51 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +6 -0
- package/src/client/components/workspace/diff-panel/folding.test.ts +252 -0
- package/src/client/components/workspace/diff-panel/folding.ts +134 -0
- package/src/client/components/workspace/diff-panel/merge.ts +5 -0
- package/src/client/components/workspace/git-panel/InitRepository.tsx +49 -0
- package/src/client/components/workspace/git-panel/commit-modal.test.ts +193 -0
- package/src/client/components/workspace/git-panel/index.test.ts +3 -125
- package/src/client/components/workspace/git-panel/index.tsx +7 -1
- package/src/client/components/workspace/git-panel/init-state.test.ts +145 -0
- package/src/client/data/samples/virtual-devices-session.ts +8 -2
- package/src/client/data/settings/omp-schema.json +25 -3
- package/src/client/hooks/chat/timeline/ask-frames.test.ts +33 -0
- package/src/client/hooks/chat/timeline/ask-frames.ts +9 -1
- package/src/server/lib/fs/git-actions-worktree.ts +25 -2
- package/src/server/lib/fs/git-actions.ts +3 -3
- package/src/server/lib/fs/git-status-read.test.ts +20 -0
- package/src/server/lib/fs/git-status-read.ts +43 -2
- package/src/server/lib/omp/session/messages.test.ts +21 -0
- package/src/server/routes/fs/git-action.test.ts +4 -0
- package/src/server/routes/fs/git-init-action.test.ts +125 -0
- package/src/shared/lib/chat/notice-row.test.ts +25 -0
- package/src/shared/lib/chat/notice-row.ts +22 -1
- package/src/shared/lib/chat/omp/approval-gate.test.ts +99 -0
- package/src/shared/lib/chat/omp/approval-gate.ts +129 -0
- package/src/shared/lib/omp/config/schema.test.ts +12 -0
- package/src/shared/lib/omp/session/job-progress.test.ts +76 -0
- package/src/shared/lib/omp/session/job-progress.ts +84 -0
- package/src/shared/lib/omp/session/mapper.ts +3 -6
- package/src/shared/lib/omp/session/messages-map.test.ts +15 -6
- package/src/shared/lib/omp/session/messages-map.ts +19 -1
- package/src/shared/lib/omp/session/proc.test.ts +23 -0
- package/src/shared/lib/omp/session/proc.ts +17 -0
- package/src/shared/lib/omp/session/wait.test.ts +22 -0
- package/src/shared/lib/omp/session/wait.ts +14 -0
- package/src/test-support/file-explorer.test-util.ts +1 -1
- package/dist/client/static/js/index-snd9wk6w.css +0 -1
package/src/client/components/workspace/chat-timeline/tool-renderers/extension-dialog/shortcuts.tsx
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The keyboard shortcuts of omp's blocking dialog, and the per-method chrome.
|
|
8
|
+
*
|
|
9
|
+
* Both are about WHICH dialog is on screen rather than what it draws, so they
|
|
10
|
+
* live here and the dialog component keeps the markup.
|
|
11
|
+
*
|
|
12
|
+
* The shortcuts are a `window` listener, not a keydown on the panel: omp
|
|
13
|
+
* blocks the whole agent on this dialog, and the reader's focus may be in the
|
|
14
|
+
* custom-answer field, in the panel itself, or nowhere — so the dialog
|
|
15
|
+
* answers the key wherever it lands. The one exception is a key pressed
|
|
16
|
+
* INSIDE the custom-answer field: those keys belong to the text the user is
|
|
17
|
+
* typing, not to the dialog.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { useEffect } from 'preact/hooks';
|
|
21
|
+
import { AlertCircle, FileText, HelpCircle, ListFilter, Terminal } from 'lucide-preact';
|
|
22
|
+
import type { ComponentChildren, RefObject } from 'preact';
|
|
23
|
+
|
|
24
|
+
export interface DialogShortcuts {
|
|
25
|
+
/** The dialog's method — which shortcuts apply. */
|
|
26
|
+
method: string;
|
|
27
|
+
/** The choices a `select` offers, in order. */
|
|
28
|
+
options: string[];
|
|
29
|
+
/** The currently highlighted choice. */
|
|
30
|
+
selectedOption: string | null;
|
|
31
|
+
/** Highlight a choice. */
|
|
32
|
+
onSelectOption: (option: string) => void;
|
|
33
|
+
/** The custom-answer field, when the dialog has one. */
|
|
34
|
+
customInputRef: RefObject<HTMLInputElement>;
|
|
35
|
+
/** Answer the dialog (Enter, or ⌘↵ in an editor). */
|
|
36
|
+
onSubmit: () => void;
|
|
37
|
+
/** Dismiss the dialog (Escape). */
|
|
38
|
+
onCancel: () => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Install the dialog's keyboard shortcuts for as long as it is on screen. */
|
|
42
|
+
export function useDialogShortcuts({
|
|
43
|
+
method,
|
|
44
|
+
options,
|
|
45
|
+
selectedOption,
|
|
46
|
+
onSelectOption,
|
|
47
|
+
customInputRef,
|
|
48
|
+
onSubmit,
|
|
49
|
+
onCancel,
|
|
50
|
+
}: DialogShortcuts) {
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
const handleKeyDown = (e: globalThis.KeyboardEvent) => {
|
|
53
|
+
if (e.key === 'Escape') {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
e.stopPropagation();
|
|
56
|
+
onCancel();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (method === 'select' && options.length > 0) {
|
|
61
|
+
// Typing in the custom "Other" input must not trigger number/arrow/Enter
|
|
62
|
+
// shortcuts — those keys belong to the text field while focused.
|
|
63
|
+
const typingCustom = e.target instanceof HTMLInputElement && e.target === customInputRef.current;
|
|
64
|
+
if (typingCustom) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const num = parseInt(e.key, 10);
|
|
69
|
+
if (!isNaN(num) && num >= 1 && num <= options.length) {
|
|
70
|
+
e.preventDefault();
|
|
71
|
+
onSelectOption(options[num - 1]);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (e.key === 'ArrowDown') {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
const currentIdx = options.findIndex((o) => o === selectedOption);
|
|
78
|
+
const nextIdx = currentIdx < options.length - 1 ? currentIdx + 1 : 0;
|
|
79
|
+
onSelectOption(options[nextIdx]);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (e.key === 'ArrowUp') {
|
|
83
|
+
e.preventDefault();
|
|
84
|
+
const currentIdx = options.findIndex((o) => o === selectedOption);
|
|
85
|
+
const prevIdx = currentIdx > 0 ? currentIdx - 1 : options.length - 1;
|
|
86
|
+
onSelectOption(options[prevIdx]);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (e.key === 'Enter' && selectedOption) {
|
|
91
|
+
e.preventDefault();
|
|
92
|
+
onSubmit();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (method === 'confirm') {
|
|
98
|
+
if (e.key === 'Enter') {
|
|
99
|
+
e.preventDefault();
|
|
100
|
+
onSubmit();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (method === 'editor') {
|
|
106
|
+
if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
|
|
107
|
+
e.preventDefault();
|
|
108
|
+
onSubmit();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
window.addEventListener('keydown', handleKeyDown);
|
|
115
|
+
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
116
|
+
}, [onCancel, onSubmit, method, options, selectedOption, onSelectOption, customInputRef]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The chrome of one dialog method: its icon, its name and its badge. */
|
|
120
|
+
export interface DialogMethodMeta {
|
|
121
|
+
icon: ComponentChildren;
|
|
122
|
+
label: string;
|
|
123
|
+
badge: string;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The chrome of one dialog method.
|
|
128
|
+
*
|
|
129
|
+
* The badge counts the CHOICES a `select` offers — and for an approval gate
|
|
130
|
+
* that count is the whole decision ("Approve", "Deny"), so it is stated the
|
|
131
|
+
* same way as a question's.
|
|
132
|
+
*/
|
|
133
|
+
export function dialogMethodMeta(method: string, optionCount: number): DialogMethodMeta {
|
|
134
|
+
switch (method) {
|
|
135
|
+
case 'select':
|
|
136
|
+
return {
|
|
137
|
+
icon: <ListFilter size={16} className="text-ink" />,
|
|
138
|
+
label: 'Decision Required',
|
|
139
|
+
badge: `${optionCount} Choices`,
|
|
140
|
+
};
|
|
141
|
+
case 'confirm':
|
|
142
|
+
return {
|
|
143
|
+
icon: <AlertCircle size={16} className="text-ink" />,
|
|
144
|
+
label: 'Confirmation Required',
|
|
145
|
+
badge: 'Confirm Action',
|
|
146
|
+
};
|
|
147
|
+
case 'input':
|
|
148
|
+
return {
|
|
149
|
+
icon: <Terminal size={16} className="text-ink" />,
|
|
150
|
+
label: 'Prompt Input',
|
|
151
|
+
badge: 'Text Input',
|
|
152
|
+
};
|
|
153
|
+
case 'editor':
|
|
154
|
+
return {
|
|
155
|
+
icon: <FileText size={16} className="text-ink" />,
|
|
156
|
+
label: 'Buffer Editor',
|
|
157
|
+
badge: 'Multiline Buffer',
|
|
158
|
+
};
|
|
159
|
+
default:
|
|
160
|
+
return {
|
|
161
|
+
icon: <HelpCircle size={16} className="text-ink" />,
|
|
162
|
+
label: 'Agent Request',
|
|
163
|
+
badge: 'Input',
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -224,6 +224,36 @@ describe('Proc panel — a cancelled job', () => {
|
|
|
224
224
|
});
|
|
225
225
|
});
|
|
226
226
|
|
|
227
|
+
describe('Proc panel — running-task telemetry', () => {
|
|
228
|
+
// The same snapshot fields the wait card draws (omp 18.13.0). This row has no
|
|
229
|
+
// percent chip of its own, so the line carries the estimate too.
|
|
230
|
+
test('draws requests, spend, context fill and stage for a running task', async () => {
|
|
231
|
+
const el = await mount({
|
|
232
|
+
type: 'read',
|
|
233
|
+
name: 'read',
|
|
234
|
+
target: 'proc://bg_11',
|
|
235
|
+
input: { path: 'proc://bg_11' },
|
|
236
|
+
output: 'bg_11 [task] — running',
|
|
237
|
+
status: 'running',
|
|
238
|
+
details: {
|
|
239
|
+
proc: {
|
|
240
|
+
job: {
|
|
241
|
+
id: 'bg_11', type: 'task', status: 'running', label: 'refactor', durationMs: 90_000,
|
|
242
|
+
completionPercent: 55, requests: 90, cost: 18.4,
|
|
243
|
+
contextTokens: 163_840, contextWindow: 1_048_576, stage: 'implementing',
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
const text = el.textContent ?? '';
|
|
249
|
+
expect(text).toContain('90 req');
|
|
250
|
+
expect(text).toContain('$18.40');
|
|
251
|
+
expect(text).toContain('ctx 163.8K/1.0M');
|
|
252
|
+
expect(text).toContain('~55%');
|
|
253
|
+
expect(text).toContain('stage: implementing');
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
227
257
|
describe('Proc panel — legacy hub tool', () => {
|
|
228
258
|
test('renders a hub daemon from the top level of details', async () => {
|
|
229
259
|
const el = await mount({
|
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
} from '@/shared/lib/omp/session/proc';
|
|
38
38
|
import { FallbackOutput } from '@/client/components/workspace/chat-timeline/tool-renderers/shared/FallbackOutput';
|
|
39
39
|
import { ToolPanelHeader } from '@/client/components/workspace/chat-timeline/tool-renderers/shared/ToolPanelHeader';
|
|
40
|
+
import { jobProgressLine } from '@/shared/lib/omp/session/job-progress';
|
|
40
41
|
|
|
41
42
|
/** Tone → class list. A toned chip is text-only; the ink palette has no room
|
|
42
43
|
* for a filled pill in a dense row. */
|
|
@@ -92,35 +93,40 @@ function JobRow({ job }: { job: ProcJob }) {
|
|
|
92
93
|
const tone = jobTone(job.status);
|
|
93
94
|
const running = job.status === 'running';
|
|
94
95
|
const duration = typeof job.durationMs === 'number' ? formatProcDuration(job.durationMs) : undefined;
|
|
96
|
+
// This row has no percent chip, so the line carries the estimate itself.
|
|
97
|
+
const progress = jobProgressLine(job, { includePercent: true });
|
|
95
98
|
return (
|
|
96
|
-
<div className="
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
{job.resolvedModel && (
|
|
116
|
-
<span className="hidden max-w-[120px] truncate font-mono text-[9px] text-ink/40 sm:inline-block">
|
|
117
|
-
{job.resolvedModel}
|
|
99
|
+
<div className="space-y-0.5 px-3 py-1.5">
|
|
100
|
+
<div className="flex items-center gap-2 text-[11.5px]">
|
|
101
|
+
{running ? (
|
|
102
|
+
<Loader2 size={11} className="shrink-0 animate-spin text-ink/50" />
|
|
103
|
+
) : job.status === 'failed' ? (
|
|
104
|
+
<CircleSlash size={11} className="shrink-0 text-error" />
|
|
105
|
+
) : job.status === 'cancelled' ? (
|
|
106
|
+
<CircleSlash size={11} className="shrink-0 text-warning" />
|
|
107
|
+
) : (
|
|
108
|
+
<CheckCircle2 size={11} className="shrink-0 text-success" />
|
|
109
|
+
)}
|
|
110
|
+
{job.id && <span className="shrink-0 font-mono text-[10px] font-semibold text-ink/80">{job.id}</span>}
|
|
111
|
+
{job.type && (
|
|
112
|
+
<span className="shrink-0 rounded bg-ink/5 px-1 py-0.2 font-mono text-[8.5px] uppercase tracking-wider text-ink/45">
|
|
113
|
+
{job.type}
|
|
114
|
+
</span>
|
|
115
|
+
)}
|
|
116
|
+
<span className="min-w-0 flex-1 truncate text-ink/85" title={job.label}>
|
|
117
|
+
{job.label ?? job.resultText ?? job.errorText ?? ''}
|
|
118
118
|
</span>
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
119
|
+
{job.resolvedModel && (
|
|
120
|
+
<span className="hidden max-w-[120px] truncate font-mono text-[9px] text-ink/40 sm:inline-block">
|
|
121
|
+
{job.resolvedModel}
|
|
122
|
+
</span>
|
|
123
|
+
)}
|
|
124
|
+
{duration && <span className="shrink-0 font-mono text-[9px] text-ink/40">{duration}</span>}
|
|
125
|
+
{job.status && (
|
|
126
|
+
<span className={`shrink-0 font-mono text-[9px] font-semibold ${TONE_CLASSES[tone]}`}>{job.status}</span>
|
|
127
|
+
)}
|
|
128
|
+
</div>
|
|
129
|
+
{progress && <div className="truncate font-mono text-[9px] text-ink/40">{progress}</div>}
|
|
124
130
|
</div>
|
|
125
131
|
);
|
|
126
132
|
}
|
|
@@ -140,6 +140,57 @@ describe('Wait panel — a blocking call', () => {
|
|
|
140
140
|
});
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
+
describe('Wait panel — running-task telemetry', () => {
|
|
144
|
+
// omp 18.13.0 lifts a task's `requests`, `cost`, context fill and `stage`
|
|
145
|
+
// into the job snapshot, which is what makes omp's own row read
|
|
146
|
+
// `90 req · $18.40 · ctx 160K/1M · stage: implementing`. The card has to draw
|
|
147
|
+
// them, or the telemetry reaches the wire and goes nowhere.
|
|
148
|
+
const TELEMETRY: Partial<ToolCallData> & Pick<ToolCallData, 'type'> = {
|
|
149
|
+
type: 'wait',
|
|
150
|
+
name: 'wait',
|
|
151
|
+
status: 'running',
|
|
152
|
+
output: '',
|
|
153
|
+
details: {
|
|
154
|
+
op: 'wait',
|
|
155
|
+
jobs: [{
|
|
156
|
+
id: 'bg_3', type: 'task', status: 'running', label: 'refactor', durationMs: 18_446_000,
|
|
157
|
+
completionPercent: 55, requests: 90, cost: 18.4,
|
|
158
|
+
contextTokens: 163_840, contextWindow: 1_048_576, stage: 'implementing',
|
|
159
|
+
}],
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
test('draws the telemetry line under the row', async () => {
|
|
164
|
+
const el = await mount(TELEMETRY);
|
|
165
|
+
const text = el.textContent ?? '';
|
|
166
|
+
expect(text).toContain('90 req');
|
|
167
|
+
expect(text).toContain('$18.40');
|
|
168
|
+
expect(text).toContain('ctx 163.8K/1.0M');
|
|
169
|
+
expect(text).toContain('stage: implementing');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('the percent stays on the row and out of the telemetry line', async () => {
|
|
173
|
+
// The row already shows `55%` as its own chip, so the line must not repeat
|
|
174
|
+
// it — twice would read as a second estimate.
|
|
175
|
+
const el = await mount(TELEMETRY);
|
|
176
|
+
const line = [...el.querySelectorAll('.font-mono')].map((node) => node.textContent ?? '').find((t) => t.includes('req'));
|
|
177
|
+
expect(line?.includes('~55%')).toBe(false);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('a settled job draws no telemetry, so a stored snapshot never looks live', async () => {
|
|
181
|
+
const el = await mount({
|
|
182
|
+
...TELEMETRY,
|
|
183
|
+
status: 'success',
|
|
184
|
+
durationMs: 18_446_000,
|
|
185
|
+
details: {
|
|
186
|
+
op: 'wait',
|
|
187
|
+
jobs: [{ ...TELEMETRY.details!.jobs[0], status: 'completed', exitCode: 0 }],
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
expect(el.textContent ?? '').not.toContain('req');
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
143
194
|
describe('Wait panel — the 30-minute cap', () => {
|
|
144
195
|
test('states the ceiling and the remaining budget while blocking', async () => {
|
|
145
196
|
// omp caps one `wait` at 30 minutes and takes no argument to raise it, so
|
|
@@ -49,6 +49,7 @@ import {
|
|
|
49
49
|
} from '@/shared/lib/omp/session/wait';
|
|
50
50
|
import { FallbackOutput } from '@/client/components/workspace/chat-timeline/tool-renderers/shared/FallbackOutput';
|
|
51
51
|
import { ToolPanelHeader } from '@/client/components/workspace/chat-timeline/tool-renderers/shared/ToolPanelHeader';
|
|
52
|
+
import { jobProgressLine } from '@/shared/lib/omp/session/job-progress';
|
|
52
53
|
|
|
53
54
|
const TONE_CLASSES = {
|
|
54
55
|
ok: 'text-success',
|
|
@@ -132,6 +133,8 @@ function JobRow({ job }: { job: WaitJob }) {
|
|
|
132
133
|
const running = job.status === 'running';
|
|
133
134
|
const label = job.label ?? job.resultText ?? job.errorText ?? job.id ?? '';
|
|
134
135
|
const percent = running ? job.completionPercent : undefined;
|
|
136
|
+
// The percent is already a chip in this row, so the progress line leaves it out.
|
|
137
|
+
const progress = jobProgressLine(job);
|
|
135
138
|
return (
|
|
136
139
|
<div className="space-y-1 px-3 py-2">
|
|
137
140
|
<div className="flex items-center gap-2 text-[11.5px]">
|
|
@@ -173,6 +176,9 @@ function JobRow({ job }: { job: WaitJob }) {
|
|
|
173
176
|
{running && percent !== undefined && (
|
|
174
177
|
<ProgressTrack percent={percent} active />
|
|
175
178
|
)}
|
|
179
|
+
{progress && (
|
|
180
|
+
<div className="truncate font-mono text-[9px] text-ink/40">{progress}</div>
|
|
181
|
+
)}
|
|
176
182
|
{job.resolvedModel && (
|
|
177
183
|
<div className="truncate font-mono text-[9px] text-ink/40">{job.resolvedModel}</div>
|
|
178
184
|
)}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Folding and brace detection in the DIFF bodies.
|
|
8
|
+
*
|
|
9
|
+
* Two layers, because each can be wrong on its own:
|
|
10
|
+
*
|
|
11
|
+
* - The RULE (`braceFoldRange`) is pure, and every way it fails is a boundary:
|
|
12
|
+
* an opener that does not really close, a pair that hides nothing, a cap that
|
|
13
|
+
* would cut a block at a line the reader never chose.
|
|
14
|
+
* - The MOUNTED STACK is what proves the feature exists at all. `foldGutter()`
|
|
15
|
+
* without a `foldService` draws a gutter and offers no marker; a `foldEffect`
|
|
16
|
+
* dispatched into a state with no `foldState` field is discarded with no
|
|
17
|
+
* error; and `bracketMatching()` is a `ViewPlugin`, so nothing at the state
|
|
18
|
+
* level can see it. Both diff shapes are mounted for that reason — the split
|
|
19
|
+
* view has TWO states, and each one needs its own field and service.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
|
|
23
|
+
import { Window } from 'happy-dom';
|
|
24
|
+
import { EditorState } from '@codemirror/state';
|
|
25
|
+
import { EditorView } from '@codemirror/view';
|
|
26
|
+
import { foldEffect, foldState, foldable } from '@codemirror/language';
|
|
27
|
+
|
|
28
|
+
import { braceFoldRange, diffFoldingExtensions } from '@/client/components/workspace/diff-panel/folding';
|
|
29
|
+
import { mountSplitDiff, mountUnifiedDiff } from '@/client/components/workspace/diff-panel/merge';
|
|
30
|
+
import { MAX_FOLD_LINES } from '@/client/components/common/code-surface/fold';
|
|
31
|
+
import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
|
|
32
|
+
|
|
33
|
+
const EXTRA_GLOBALS = ['MutationObserver', 'requestAnimationFrame', 'cancelAnimationFrame'] as const;
|
|
34
|
+
const native: Partial<Record<(typeof EXTRA_GLOBALS)[number], unknown>> = {};
|
|
35
|
+
|
|
36
|
+
let host: HTMLElement;
|
|
37
|
+
|
|
38
|
+
beforeAll(() => {
|
|
39
|
+
const win = new Window({ url: 'http://localhost' });
|
|
40
|
+
installDomGlobals(win);
|
|
41
|
+
const target = globalThis as unknown as Record<string, unknown>;
|
|
42
|
+
for (const key of EXTRA_GLOBALS) {
|
|
43
|
+
native[key] = target[key];
|
|
44
|
+
target[key] = (win as unknown as Record<string, unknown>)[key];
|
|
45
|
+
}
|
|
46
|
+
host = document.body.appendChild(document.createElement('div'));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterAll(() => {
|
|
50
|
+
const target = globalThis as unknown as Record<string, unknown>;
|
|
51
|
+
for (const key of EXTRA_GLOBALS) {
|
|
52
|
+
if (native[key] === undefined) delete target[key];
|
|
53
|
+
else target[key] = native[key];
|
|
54
|
+
}
|
|
55
|
+
restoreDomGlobals();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
function stateOf(doc: string): EditorState {
|
|
59
|
+
return EditorState.create({ doc, extensions: diffFoldingExtensions });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The fold for line `number` (1-based), as `[startLine, endLine]` or null. */
|
|
63
|
+
function foldAt(state: EditorState, number: number): [number, number] | null {
|
|
64
|
+
const line = state.doc.line(number);
|
|
65
|
+
const range = foldable(state, line.from, line.to);
|
|
66
|
+
if (!range) return null;
|
|
67
|
+
return [state.doc.lineAt(range.from).number, state.doc.lineAt(range.to).number];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function diffOptions(original: string, next: string) {
|
|
71
|
+
return {
|
|
72
|
+
original,
|
|
73
|
+
next,
|
|
74
|
+
language: 'typescript',
|
|
75
|
+
wordWrap: false,
|
|
76
|
+
collapse: 'full' as const,
|
|
77
|
+
scrollTestId: 'diff-fold-test',
|
|
78
|
+
onScroller: () => {},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The view inside the FIRST editor of a mounted host. */
|
|
83
|
+
function viewIn(element: Element): EditorView {
|
|
84
|
+
const editor = EditorView.findFromDOM(element as HTMLElement);
|
|
85
|
+
if (!editor) throw new Error('no EditorView inside the mounted host');
|
|
86
|
+
return editor;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function foldMarkers(element: Element): string[] {
|
|
90
|
+
// `foldGutter` draws an `initialSpacer` marker that reserves the gutter's
|
|
91
|
+
// width — a real `.cm-gutterElement` with `visibility: hidden`. Only the
|
|
92
|
+
// visible ones are markers the reader can click.
|
|
93
|
+
return Array.from(element.querySelectorAll('.cm-foldGutter .cm-gutterElement span'))
|
|
94
|
+
.filter((node) => !(node.parentElement?.getAttribute('style') ?? '').includes('hidden'))
|
|
95
|
+
.map((node) => node.textContent ?? '');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
describe('braceFoldRange', () => {
|
|
99
|
+
test('a brace block folds down to the line before its closing brace', () => {
|
|
100
|
+
const state = stateOf('function f() {\n a();\n b();\n}\nafter();\n');
|
|
101
|
+
expect(foldAt(state, 1)).toEqual([1, 3]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('the fold starts after the opener LINE, so the opener stays visible', () => {
|
|
105
|
+
const state = stateOf('function f() {\n a();\n}\n');
|
|
106
|
+
const line = state.doc.line(1);
|
|
107
|
+
const range = braceFoldRange(state, line.from, line.to);
|
|
108
|
+
expect(range?.from).toBe(line.to);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('a pair that closes on the NEXT line hides nothing and does not fold', () => {
|
|
112
|
+
const state = stateOf('call(\n a);\nother();\n');
|
|
113
|
+
// The `(` closes on line 2, so the range would hide nothing at all.
|
|
114
|
+
expect(braceFoldRange(state, 0, state.doc.line(1).to)).toBeNull();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('an opener with no closer is refused by the brace rule, not folded to the end', () => {
|
|
118
|
+
const state = stateOf('function f() {\n a();\n');
|
|
119
|
+
// The brace rule itself must answer null; `foldAt` would still fold the
|
|
120
|
+
// body by INDENTATION, which is the fallback doing its job.
|
|
121
|
+
expect(braceFoldRange(state, 0, state.doc.line(1).to)).toBeNull();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test('a same-line pair is skipped in favour of the outer block', () => {
|
|
125
|
+
const state = stateOf('function f(a, b) {\n call(a);\n}\n');
|
|
126
|
+
// `(` closes on line 1, so the `{` is the pair that spans lines.
|
|
127
|
+
expect(foldAt(state, 1)).toEqual([1, 2]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('a nested block folds to its OWN closer', () => {
|
|
131
|
+
const state = stateOf('if (a) {\n if (b) {\n c();\n }\n d();\n}\n');
|
|
132
|
+
expect(foldAt(state, 1)).toEqual([1, 5]);
|
|
133
|
+
expect(foldAt(state, 2)).toEqual([2, 3]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('a bracket list folds the same way a brace block does', () => {
|
|
137
|
+
const state = stateOf('const x = [\n 1,\n 2,\n];\nnext();\n');
|
|
138
|
+
expect(foldAt(state, 1)).toEqual([1, 3]);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('a one-line block does not fold', () => {
|
|
142
|
+
const state = stateOf('if (a) { b(); }\nnext();\n');
|
|
143
|
+
expect(foldAt(state, 1)).toBeNull();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('mismatched bracket types do not swallow each other', () => {
|
|
147
|
+
// A `(` counter that also counted `}` would pair this `{` with the `)`.
|
|
148
|
+
const state = stateOf('call(a, {\n b: 1,\n});\nnext();\n');
|
|
149
|
+
expect(foldAt(state, 1)).toEqual([1, 2]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('a block past the cap is REFUSED rather than truncated', () => {
|
|
153
|
+
const state = stateOf('function f() {\n' + ' x();\n'.repeat(MAX_FOLD_LINES + 10) + '}\n');
|
|
154
|
+
expect(foldAt(state, 1)).toBeNull();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('a block inside the cap still folds', () => {
|
|
158
|
+
const state = stateOf('function f() {\n' + ' x();\n'.repeat(50) + '}\n');
|
|
159
|
+
expect(foldAt(state, 1)).toEqual([1, 51]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('the indentation rule still answers where no bracket spans lines', () => {
|
|
163
|
+
const state = stateOf('key:\n a: 1\n b: 2\nnext: 1\n');
|
|
164
|
+
// No brace on line 1: this fold is `indentFoldService`'s.
|
|
165
|
+
expect(foldAt(state, 1)).toEqual([1, 3]);
|
|
166
|
+
expect(braceFoldRange(state, 0, state.doc.line(1).to)).toBeNull();
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('the mounted diff stack', () => {
|
|
171
|
+
test('a unified diff installs the fold state and offers a marker', () => {
|
|
172
|
+
const element = host.appendChild(document.createElement('div'));
|
|
173
|
+
const handle = mountUnifiedDiff(element, diffOptions('const a = 1;\n', 'function g() {\n a();\n}\n'));
|
|
174
|
+
try {
|
|
175
|
+
const view = viewIn(element);
|
|
176
|
+
expect(view.state.field(foldState, false)).toBeDefined();
|
|
177
|
+
expect(foldMarkers(element)).toHaveLength(1);
|
|
178
|
+
} finally {
|
|
179
|
+
handle.destroy();
|
|
180
|
+
element.remove();
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('a folded block in a unified diff draws the placeholder', () => {
|
|
185
|
+
const element = host.appendChild(document.createElement('div'));
|
|
186
|
+
const handle = mountUnifiedDiff(element, diffOptions('const a = 1;\n', 'function g() {\n a();\n b();\n}\n'));
|
|
187
|
+
try {
|
|
188
|
+
const view = viewIn(element);
|
|
189
|
+
expect(view.state.doc.lines).toBe(5);
|
|
190
|
+
const before = view.contentDOM.querySelectorAll('.cm-line').length;
|
|
191
|
+
view.dispatch({
|
|
192
|
+
effects: foldEffect.of({ from: view.state.doc.line(1).to, to: view.state.doc.line(3).to }),
|
|
193
|
+
});
|
|
194
|
+
expect(view.contentDOM.querySelector('.cm-foldPlaceholder')).not.toBeNull();
|
|
195
|
+
expect(view.contentDOM.querySelectorAll('.cm-line').length).toBeLessThan(before);
|
|
196
|
+
} finally {
|
|
197
|
+
handle.destroy();
|
|
198
|
+
element.remove();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('a unified diff paints both ends of the caret’s brace pair', () => {
|
|
203
|
+
const element = host.appendChild(document.createElement('div'));
|
|
204
|
+
const doc = 'function g() {\n a();\n}\n';
|
|
205
|
+
const handle = mountUnifiedDiff(element, diffOptions('const a = 1;\n', doc));
|
|
206
|
+
try {
|
|
207
|
+
const view = viewIn(element);
|
|
208
|
+
view.dispatch({ selection: { anchor: doc.indexOf('{') + 1 } });
|
|
209
|
+
const painted = Array.from(
|
|
210
|
+
view.contentDOM.querySelectorAll('.cm-matchingBracket'),
|
|
211
|
+
).map((node) => node.textContent);
|
|
212
|
+
expect(painted).toEqual(['{', '}']);
|
|
213
|
+
} finally {
|
|
214
|
+
handle.destroy();
|
|
215
|
+
element.remove();
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('the guide draws the rows between the caret’s brace and its pair', () => {
|
|
220
|
+
const element = host.appendChild(document.createElement('div'));
|
|
221
|
+
const doc = 'function g() {\n a();\n b();\n}\n';
|
|
222
|
+
const handle = mountUnifiedDiff(element, diffOptions('const a = 1;\n', doc));
|
|
223
|
+
try {
|
|
224
|
+
const view = viewIn(element);
|
|
225
|
+
view.dispatch({ selection: { anchor: doc.indexOf('{') + 1 } });
|
|
226
|
+
// The two body rows; the opener and the closer keep their own lines.
|
|
227
|
+
expect(view.contentDOM.querySelectorAll('.cm-line.cm-bracket-guide')).toHaveLength(2);
|
|
228
|
+
} finally {
|
|
229
|
+
handle.destroy();
|
|
230
|
+
element.remove();
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('BOTH halves of a split diff fold independently', () => {
|
|
235
|
+
const element = host.appendChild(document.createElement('div'));
|
|
236
|
+
const handle = mountSplitDiff(element, diffOptions('before();\n', 'function g() {\n a();\n}\n'));
|
|
237
|
+
try {
|
|
238
|
+
const editors = Array.from(element.querySelectorAll('.cm-editor'));
|
|
239
|
+
expect(editors).toHaveLength(2);
|
|
240
|
+
for (const editor of editors) {
|
|
241
|
+
const view = viewIn(editor);
|
|
242
|
+
expect(view.state.field(foldState, false)).toBeDefined();
|
|
243
|
+
}
|
|
244
|
+
// Only the new side has a brace block, so only it offers a marker.
|
|
245
|
+
expect(foldMarkers(editors[0] as Element)).toHaveLength(0);
|
|
246
|
+
expect(foldMarkers(editors[1] as Element)).toHaveLength(1);
|
|
247
|
+
} finally {
|
|
248
|
+
handle.destroy();
|
|
249
|
+
element.remove();
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
});
|