@kabmo-labs/kabmo 0.23.0 → 0.24.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 +679 -679
- package/dist/client/static/js/{index-ph0q59rs.js → index-jcr5twtp.js} +769 -767
- package/dist/client/static/js/index-z3w7n85q.css +1 -0
- package/package.json +1 -1
- package/src/client/components/common/Modal.tsx +2 -6
- package/src/client/components/layout/desktop-layout/WorkspacePanels.tsx +7 -1
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.test.tsx +20 -0
- package/src/client/components/layout/session-sidebar/about-modal/HarnessTable.tsx +23 -7
- package/src/client/components/layout/session-sidebar/about-modal/index.test.tsx +122 -0
- package/src/client/components/layout/session-sidebar/about-modal/index.tsx +27 -11
- package/src/client/components/settings/categories/harness-settings/EnvProviderForm.tsx +31 -0
- package/src/client/components/settings/categories/harness-settings/provider-panel.test.tsx +49 -0
- package/src/client/components/workspace/chat-timeline/ChatOverlays.tsx +5 -2
- package/src/client/components/workspace/chat-timeline/MessageList.test.ts +14 -0
- package/src/client/components/workspace/chat-timeline/chat-input/index.tsx +4 -3
- package/src/client/components/workspace/chat-timeline/index.tsx +2 -1
- package/src/client/components/workspace/chat-timeline/pending-harness.test.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/FrameControl.tsx +11 -20
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/GroupedAsk.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/QuestionBlock.tsx +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/drafts.ts +1 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/ask-panel/index.tsx +2 -1
- package/src/client/components/workspace/file-explorer/index.test.ts +25 -0
- package/src/client/components/workspace/file-explorer/index.tsx +9 -3
- package/src/client/components/workspace/model-dropdown/harness-models.test.ts +17 -0
- package/src/client/components/workspace/model-dropdown/harness-models.ts +33 -18
- package/src/client/components/workspace/model-dropdown/index.tsx +3 -3
- package/src/client/components/workspace/model-dropdown/popover-metrics.ts +4 -1
- package/src/client/data/settings/omp-schema.json +4 -4
- package/src/client/hooks/chat/timeline/actions.ts +2 -2
- package/src/client/hooks/chat/timeline/ask-frames.ts +5 -4
- package/src/client/hooks/chat/timeline/new-chat-open.test.ts +86 -0
- package/src/client/hooks/chat/timeline/new-chat-open.ts +23 -5
- package/src/client/hooks/chat/timeline/new-chat.test.ts +32 -4
- package/src/client/hooks/chat/timeline/new-chat.ts +18 -6
- package/src/client/hooks/chat/timeline/rewind-actions.test.ts +24 -1
- package/src/client/hooks/chat/timeline/rewind-actions.ts +15 -11
- package/src/client/hooks/chat/timeline/run-identity.test.ts +52 -0
- package/src/client/hooks/chat/timeline/run-identity.ts +23 -11
- package/src/client/hooks/chat/timeline/send-execute.test.ts +17 -0
- package/src/client/hooks/chat/timeline/send-props.ts +5 -3
- package/src/client/hooks/chat/timeline/send-steer.test.ts +1 -3
- package/src/client/hooks/chat/timeline/send.test-util.ts +1 -1
- package/src/client/hooks/chat/timeline/send.test.ts +1 -3
- package/src/client/hooks/chat/timeline/send.ts +8 -3
- package/src/client/hooks/chat/timeline/session-load.test-util.ts +6 -2
- package/src/client/hooks/chat/timeline/session-load.test.ts +31 -0
- package/src/client/hooks/chat/timeline/session-load.ts +6 -14
- package/src/client/hooks/chat/timeline/session-shapes.ts +32 -0
- package/src/client/hooks/models/harness-catalog.ts +114 -0
- package/src/client/hooks/models/provider-writes.test.ts +81 -0
- package/src/client/hooks/models/provider-writes.ts +10 -3
- package/src/client/hooks/models/use-harnesses-controller.ts +57 -0
- package/src/client/hooks/models/use-harnesses-signal.test.ts +128 -0
- package/src/client/hooks/models/use-harnesses.test.ts +2 -1
- package/src/client/hooks/models/use-harnesses.ts +48 -114
- package/src/client/tailwind.css +9 -1
- package/src/server/lib/harness/acp/activation.test.ts +81 -0
- package/src/server/lib/harness/acp/activation.ts +72 -0
- package/src/server/lib/harness/acp/frames.test.ts +50 -1
- package/src/server/lib/harness/acp/frames.ts +17 -5
- package/src/server/lib/harness/acp/mapper.ts +6 -4
- package/src/server/lib/harness/acp/session-config.test.ts +73 -0
- package/src/server/lib/harness/acp/session-config.ts +87 -6
- package/src/server/lib/harness/acp/session-handshake.ts +9 -2
- package/src/server/lib/harness/acp/session-idle.ts +51 -0
- package/src/server/lib/harness/acp/session-notifications.ts +19 -3
- package/src/server/lib/harness/acp/session-observers.test.ts +9 -0
- package/src/server/lib/harness/acp/session-options.ts +33 -0
- package/src/server/lib/harness/acp/session-reaper.test.ts +96 -0
- package/src/server/lib/harness/acp/session.test-util.ts +31 -2
- package/src/server/lib/harness/acp/session.test.ts +65 -3
- package/src/server/lib/harness/acp/session.ts +20 -20
- package/src/server/lib/harness/acp/turn.ts +31 -7
- package/src/server/lib/harness/auto-probe.server.test.ts +121 -0
- package/src/server/lib/harness/auto-probe.server.ts +178 -0
- package/src/server/lib/harness/config.server.test.ts +1 -1
- package/src/server/lib/harness/config.server.ts +1 -1
- package/src/server/lib/harness/launch-table.ts +195 -0
- package/src/server/lib/harness/launch.test.ts +37 -0
- package/src/server/lib/harness/launch.ts +13 -115
- package/src/server/lib/harness/provider-entry.ts +32 -0
- package/src/server/lib/harness/provider-file.server.ts +3 -0
- package/src/server/lib/harness/provider-sources.server.test.ts +45 -0
- package/src/server/lib/harness/provider-sources.server.ts +26 -2
- package/src/server/lib/harness/provider-write.server.ts +21 -32
- package/src/server/lib/harness/provider-zcode.server.test.ts +270 -0
- package/src/server/lib/harness/provider-zcode.server.ts +324 -0
- package/src/server/lib/harness/update-state.server.ts +15 -20
- package/src/server/lib/lifecycle/after-listen.ts +7 -0
- package/src/server/lib/models/registry-refresh.server.test.ts +87 -0
- package/src/server/lib/models/registry-refresh.server.ts +98 -0
- package/src/server/lib/omp/rpc/child-config.ts +5 -4
- package/src/server/lib/omp/rpc/constants.test.ts +2 -1
- package/src/server/lib/omp/rpc/forked-resume.test.ts +14 -3
- package/src/server/lib/omp/session/delete-flow.server.ts +15 -8
- package/src/server/lib/omp/session/delete.server.ts +12 -0
- package/src/server/routes/chat/rewind.test.ts +147 -0
- package/src/server/routes/chat/rewind.ts +29 -7
- package/src/server/routes/chat/session.test.ts +165 -10
- package/src/server/routes/chat/session.ts +60 -7
- package/src/server/routes/harness/import-action.ts +18 -4
- package/src/server/routes/harness/root.ts +8 -49
- package/src/server/routes/harness/toggle.test.ts +20 -0
- package/src/server/routes/harness/update.ts +49 -7
- package/src/server/routes/harness/versions.ts +5 -1
- package/src/server/routes/sessions/delete-acp.test.ts +227 -0
- package/src/server/routes/settings/model-override.ts +5 -2
- package/src/server/routes/settings/provider-entry.ts +2 -2
- package/src/server/routes/settings/provider-model.ts +2 -2
- package/src/server/routes/settings/provider-models.ts +2 -2
- package/src/server/routes/settings/provider-refresh.ts +6 -4
- package/src/server/routes/settings/providers.ts +3 -1
- package/src/server/routes/settings/settings-config-routes.test.ts +10 -2
- package/src/shared/lib/chat/ask-form/fields.ts +76 -0
- package/src/shared/lib/chat/ask-form/index.ts +323 -0
- package/src/shared/lib/chat/ask-form/tokens.ts +199 -0
- package/src/shared/lib/chat/ask-form/types.ts +43 -0
- package/src/shared/lib/chat/ask-form.test.ts +137 -0
- package/src/shared/lib/chat/ask-questions.test.ts +65 -1
- package/src/shared/lib/chat/ask-questions.ts +51 -145
- package/src/shared/lib/chat/ask-results.ts +198 -0
- package/src/shared/lib/chat/timeline/omp-callbacks.ts +13 -1
- package/src/shared/lib/chat/timeline/prompt-error.test.ts +146 -0
- package/src/shared/lib/chat/timeline/prompt-error.ts +88 -0
- package/src/shared/lib/harness/catalog.test.ts +1 -1
- package/src/shared/lib/harness/config-fields.test.ts +4 -0
- package/src/shared/lib/harness/config-fields.ts +19 -4
- package/src/shared/lib/harness/dialects.test.ts +8 -0
- package/src/shared/lib/harness/dialects.ts +12 -0
- package/src/shared/lib/harness/provider-facts.test.ts +32 -8
- package/src/shared/lib/harness/provider-facts.ts +46 -11
- package/src/shared/lib/harness/providers.ts +9 -0
- package/src/shared/lib/harness/rewind.ts +24 -0
- package/src/shared/lib/harness/update-plan.test.ts +51 -1
- package/src/shared/lib/harness/update-plan.ts +47 -7
- package/src/shared/lib/harness/updates.test.ts +22 -0
- package/src/shared/lib/harness/updates.ts +24 -1
- package/src/shared/lib/models/client.ts +10 -1
- package/src/shared/lib/omp/dialog-methods.ts +4 -3
- package/src/shared/lib/theme/css.ts +2 -0
- package/src/shared/lib/theme/ink-scale.ts +21 -1
- package/src/shared/lib/theme/theme.test.ts +25 -9
- package/src/shared/lib/ui/safe-area.test.ts +28 -0
- package/src/shared/lib/ui/safe-area.ts +42 -0
- package/src/shared/types/harness.ts +30 -4
- package/src/test-support/file-explorer-render.test-util.ts +37 -0
- package/src/test-support/file-explorer.test-util.ts +20 -18
- package/src/test-support/harness-table.test-util.ts +6 -3
- package/dist/client/static/js/index-jhmg4w91.css +0 -1
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Reads the HTML `<form>` omp's `ask` tool takes since 18.18.3.
|
|
8
|
+
*
|
|
9
|
+
* The tool used to take `questions[]`; it now takes one `{ html }` string and
|
|
10
|
+
* the RPC host walks the form's named controls one dialog at a time. Kabmo
|
|
11
|
+
* draws every question of the call at once, so it needs the same list omp
|
|
12
|
+
* derived: the field's own text, else the sentence right before it, else its
|
|
13
|
+
* `<fieldset>` legend, else a lone option's label, else the control name, plus
|
|
14
|
+
* the option rows and the free-text "Other" row omp appends. This mirrors
|
|
15
|
+
* `fieldQuestions`/`formAnswers` in omp's `pi-tui` (`html-form/data.ts`), which
|
|
16
|
+
* the browser cannot import.
|
|
17
|
+
*
|
|
18
|
+
* The parser is deliberately small: agent-written forms use labels, fieldsets,
|
|
19
|
+
* paragraphs, selects and plain inputs. Anything it cannot read degrades to the
|
|
20
|
+
* control's `name`, never to a blank card.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { AskQuestion } from '@/shared/lib/chat/ask-form/types';
|
|
24
|
+
import { toQuestions, type DraftField, type DraftOption } from '@/shared/lib/chat/ask-form/fields';
|
|
25
|
+
import {
|
|
26
|
+
BLOCK,
|
|
27
|
+
findChild,
|
|
28
|
+
hasControl,
|
|
29
|
+
HEADING,
|
|
30
|
+
matchingClose,
|
|
31
|
+
nameLabel,
|
|
32
|
+
oneLine,
|
|
33
|
+
SKIPPED,
|
|
34
|
+
textOf,
|
|
35
|
+
tokenize,
|
|
36
|
+
type Token,
|
|
37
|
+
} from '@/shared/lib/chat/ask-form/tokens';
|
|
38
|
+
|
|
39
|
+
const IGNORED_INPUTS: Record<string, true> = {
|
|
40
|
+
button: true,
|
|
41
|
+
file: true,
|
|
42
|
+
image: true,
|
|
43
|
+
reset: true,
|
|
44
|
+
submit: true,
|
|
45
|
+
hidden: true,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Pre-pass: ids of controls, and the `<label for>` text that names them. */
|
|
49
|
+
function readLabelTargets(tokens: Token[]): Map<string, string> {
|
|
50
|
+
const ids = new Set<string>();
|
|
51
|
+
for (const token of tokens) {
|
|
52
|
+
if (token.kind !== 'open') continue;
|
|
53
|
+
if (token.tag !== 'input' && token.tag !== 'select' && token.tag !== 'textarea') continue;
|
|
54
|
+
if (token.attrs.id) ids.add(token.attrs.id);
|
|
55
|
+
}
|
|
56
|
+
const labels = new Map<string, string>();
|
|
57
|
+
for (let index = 0; index < tokens.length; index += 1) {
|
|
58
|
+
const token = tokens[index];
|
|
59
|
+
if (token.kind !== 'open' || token.tag !== 'label' || token.selfClosing) continue;
|
|
60
|
+
const target = token.attrs.for;
|
|
61
|
+
if (!target || !ids.has(target) || labels.has(target)) continue;
|
|
62
|
+
labels.set(target, textOf(tokens, index + 1, matchingClose(tokens, index, 'label')));
|
|
63
|
+
}
|
|
64
|
+
return labels;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Fields of one form, in document order, with the question text omp derives. */
|
|
68
|
+
export function parseAskForm(html: string): AskQuestion[] {
|
|
69
|
+
if (!html.trim()) return [];
|
|
70
|
+
const form = /<form\b[^>]*>([\s\S]*)<\/form>/i.exec(html);
|
|
71
|
+
const tokens = tokenize(form ? form[1] : html);
|
|
72
|
+
const labelFor = readLabelTargets(tokens);
|
|
73
|
+
|
|
74
|
+
const fields: DraftField[] = [];
|
|
75
|
+
const groups = new Map<string, DraftField>();
|
|
76
|
+
let section = '';
|
|
77
|
+
let caption = '';
|
|
78
|
+
let inline = '';
|
|
79
|
+
|
|
80
|
+
const flush = () => {
|
|
81
|
+
const text = oneLine(inline);
|
|
82
|
+
inline = '';
|
|
83
|
+
if (text) caption = text;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const addField = (field: DraftField) => {
|
|
87
|
+
flush();
|
|
88
|
+
fields.push(field);
|
|
89
|
+
caption = '';
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const controlLabel = (attrs: Record<string, string>, wrapping: string | undefined): string => {
|
|
93
|
+
const own = wrapping !== undefined ? oneLine(wrapping) : '';
|
|
94
|
+
if (own) return own;
|
|
95
|
+
const detached = attrs.id ? labelFor.get(attrs.id) : undefined;
|
|
96
|
+
return detached ? oneLine(detached) : '';
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const addInput = (attrs: Record<string, string>, wrapping: string | undefined) => {
|
|
100
|
+
const type = (attrs.type ?? 'text').toLowerCase();
|
|
101
|
+
if (IGNORED_INPUTS[type]) return;
|
|
102
|
+
const name = attrs.name ?? '';
|
|
103
|
+
if (type === 'radio' || type === 'checkbox') {
|
|
104
|
+
const value = attrs.value ?? 'on';
|
|
105
|
+
const option: DraftOption = {
|
|
106
|
+
value,
|
|
107
|
+
label: controlLabel(attrs, wrapping) || value || name,
|
|
108
|
+
checked: 'checked' in attrs,
|
|
109
|
+
disabled: 'disabled' in attrs,
|
|
110
|
+
};
|
|
111
|
+
const key = `${type}\u0000${name}`;
|
|
112
|
+
const group = groups.get(key);
|
|
113
|
+
if (group) {
|
|
114
|
+
group.options.push(option);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const field: DraftField = {
|
|
118
|
+
name,
|
|
119
|
+
kind: 'choice',
|
|
120
|
+
multi: type === 'checkbox',
|
|
121
|
+
select: false,
|
|
122
|
+
label: '',
|
|
123
|
+
options: [option],
|
|
124
|
+
value: '',
|
|
125
|
+
multiline: false,
|
|
126
|
+
caption,
|
|
127
|
+
section,
|
|
128
|
+
};
|
|
129
|
+
groups.set(key, field);
|
|
130
|
+
addField(field);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
addField({
|
|
134
|
+
name,
|
|
135
|
+
kind: 'text',
|
|
136
|
+
multi: false,
|
|
137
|
+
select: false,
|
|
138
|
+
label: controlLabel(attrs, wrapping) || nameLabel(name),
|
|
139
|
+
options: [],
|
|
140
|
+
value: attrs.value ?? '',
|
|
141
|
+
multiline: false,
|
|
142
|
+
caption,
|
|
143
|
+
section,
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const addSelect = (attrs: Record<string, string>, inner: Token[], wrapping: string | undefined) => {
|
|
148
|
+
const options: DraftOption[] = [];
|
|
149
|
+
for (let index = 0; index < inner.length; index += 1) {
|
|
150
|
+
const token = inner[index];
|
|
151
|
+
if (token.kind !== 'open' || token.tag !== 'option') continue;
|
|
152
|
+
const end = token.selfClosing ? index + 1 : matchingClose(inner, index, 'option');
|
|
153
|
+
const text = textOf(inner, index + 1, end);
|
|
154
|
+
options.push({
|
|
155
|
+
value: token.attrs.value ?? text,
|
|
156
|
+
label: text,
|
|
157
|
+
checked: 'selected' in token.attrs,
|
|
158
|
+
disabled: 'disabled' in token.attrs,
|
|
159
|
+
});
|
|
160
|
+
index = end;
|
|
161
|
+
}
|
|
162
|
+
const multi = 'multiple' in attrs;
|
|
163
|
+
if (!multi && options.length > 0 && !options.some((option) => option.checked)) {
|
|
164
|
+
const first = options.find((option) => !option.disabled);
|
|
165
|
+
if (first) first.checked = true;
|
|
166
|
+
}
|
|
167
|
+
addField({
|
|
168
|
+
name: attrs.name ?? '',
|
|
169
|
+
kind: 'choice',
|
|
170
|
+
multi,
|
|
171
|
+
select: true,
|
|
172
|
+
label: controlLabel(attrs, wrapping),
|
|
173
|
+
options,
|
|
174
|
+
value: '',
|
|
175
|
+
multiline: false,
|
|
176
|
+
caption,
|
|
177
|
+
section,
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Walk a token range. `quiet` is a `<label>` that wraps controls: its text is
|
|
183
|
+
* the control's LABEL (already passed as `wrapping`), never a block of the
|
|
184
|
+
* form, so it must not caption the field it names.
|
|
185
|
+
*/
|
|
186
|
+
const walk = (
|
|
187
|
+
start: number,
|
|
188
|
+
end: number,
|
|
189
|
+
wrapping: string | undefined,
|
|
190
|
+
depth: number,
|
|
191
|
+
quiet = false,
|
|
192
|
+
): void => {
|
|
193
|
+
const settle = () => {
|
|
194
|
+
if (!quiet) flush();
|
|
195
|
+
};
|
|
196
|
+
for (let index = start; index < end; index += 1) {
|
|
197
|
+
const token = tokens[index];
|
|
198
|
+
if (token.kind === 'text') {
|
|
199
|
+
if (!quiet) inline += token.text;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const tag = token.tag;
|
|
203
|
+
if (tag.startsWith('/')) {
|
|
204
|
+
if (BLOCK[tag.slice(1)] || HEADING[tag.slice(1)] || tag === '/legend' || tag === '/pre' || tag === '/table') {
|
|
205
|
+
settle();
|
|
206
|
+
}
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (token.selfClosing) {
|
|
210
|
+
if (tag === 'br' && !quiet) inline += '\n';
|
|
211
|
+
else if (tag === 'hr') settle();
|
|
212
|
+
else if (tag === 'input') addInput(token.attrs, wrapping);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const close = matchingClose(tokens, index, tag);
|
|
216
|
+
if (SKIPPED[tag]) {
|
|
217
|
+
index = close;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (tag === 'label') {
|
|
221
|
+
const target = token.attrs.for;
|
|
222
|
+
if (target && labelFor.has(target)) {
|
|
223
|
+
index = close;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
walk(index + 1, close, textOf(tokens, index + 1, close), depth, true);
|
|
227
|
+
index = close;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (tag === 'fieldset') {
|
|
231
|
+
settle();
|
|
232
|
+
const legendAt = findChild(tokens, index + 1, close, 'legend');
|
|
233
|
+
if (depth === 0 && !quiet) {
|
|
234
|
+
// omp starts a NEW section at a top-level fieldset: the blocks before
|
|
235
|
+
// it (a `<p>` caption included) do not reach the fields inside, which
|
|
236
|
+
// borrow the legend instead. Keeping the caption made a form's
|
|
237
|
+
// explanatory paragraph caption the first field — and since that
|
|
238
|
+
// paragraph is not the title omp shows, the live dialog could not be
|
|
239
|
+
// matched back to the question.
|
|
240
|
+
caption = '';
|
|
241
|
+
section = legendAt === -1 ? '' : textOf(tokens, legendAt + 1, matchingClose(tokens, legendAt, 'legend'));
|
|
242
|
+
if (legendAt === -1) {
|
|
243
|
+
walk(index + 1, close, wrapping, depth + 1);
|
|
244
|
+
} else {
|
|
245
|
+
walk(index + 1, legendAt, wrapping, depth + 1);
|
|
246
|
+
walk(matchingClose(tokens, legendAt, 'legend') + 1, close, wrapping, depth + 1);
|
|
247
|
+
}
|
|
248
|
+
section = '';
|
|
249
|
+
} else {
|
|
250
|
+
walk(index + 1, close, wrapping, depth + 1, quiet);
|
|
251
|
+
}
|
|
252
|
+
settle();
|
|
253
|
+
index = close;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (tag === 'legend' || HEADING[tag]) {
|
|
257
|
+
settle();
|
|
258
|
+
walk(index + 1, close, wrapping, depth, quiet);
|
|
259
|
+
settle();
|
|
260
|
+
index = close;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (BLOCK[tag]) {
|
|
264
|
+
settle();
|
|
265
|
+
walk(index + 1, close, wrapping, depth, quiet);
|
|
266
|
+
settle();
|
|
267
|
+
index = close;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (tag === 'ul' || tag === 'ol' || tag === 'pre' || tag === 'table') {
|
|
271
|
+
settle();
|
|
272
|
+
// A list of controls is layout; any other list is a block, which clears
|
|
273
|
+
// the caption the next field would otherwise borrow.
|
|
274
|
+
if ((tag === 'ul' || tag === 'ol') && hasControl(tokens, index + 1, close)) {
|
|
275
|
+
walk(index + 1, close, wrapping, depth, quiet);
|
|
276
|
+
} else if (!quiet) {
|
|
277
|
+
caption = '';
|
|
278
|
+
}
|
|
279
|
+
settle();
|
|
280
|
+
index = close;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (tag === 'input') {
|
|
284
|
+
addInput(token.attrs, wrapping);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (tag === 'select') {
|
|
288
|
+
addSelect(token.attrs, tokens.slice(index + 1, close), wrapping);
|
|
289
|
+
index = close;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (tag === 'textarea') {
|
|
293
|
+
addField({
|
|
294
|
+
name: token.attrs.name ?? '',
|
|
295
|
+
kind: 'text',
|
|
296
|
+
multi: false,
|
|
297
|
+
select: false,
|
|
298
|
+
label: controlLabel(token.attrs, wrapping) || nameLabel(token.attrs.name ?? ''),
|
|
299
|
+
options: [],
|
|
300
|
+
value: textOf(tokens, index + 1, close),
|
|
301
|
+
multiline: true,
|
|
302
|
+
caption,
|
|
303
|
+
section,
|
|
304
|
+
});
|
|
305
|
+
index = close;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (tag === 'img') {
|
|
309
|
+
settle();
|
|
310
|
+
if (!quiet) caption = '';
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
// Inline markup (`code`, `strong`, `em`, `a`, `span`, …): keep its text.
|
|
314
|
+
walk(index + 1, close, wrapping, depth, quiet);
|
|
315
|
+
index = close;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
walk(0, tokens.length, undefined, 0);
|
|
320
|
+
flush();
|
|
321
|
+
|
|
322
|
+
return toQuestions(fields);
|
|
323
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A tolerant tokenizer for agent-written form HTML, and the text helpers the
|
|
8
|
+
* field walk needs.
|
|
9
|
+
*
|
|
10
|
+
* Only what `ask-form/index.ts` reads: tags with attributes, text with entities
|
|
11
|
+
* decoded, and the `</tag>` that closes an open one. No tree is built — the walk
|
|
12
|
+
* holds its own label/fieldset context, which is the only structure the question
|
|
13
|
+
* derivation needs.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export type Token =
|
|
17
|
+
| { kind: 'text'; text: string }
|
|
18
|
+
| { kind: 'open'; tag: string; attrs: Record<string, string>; selfClosing: boolean };
|
|
19
|
+
|
|
20
|
+
const TAG = /<\/?([a-zA-Z][\w:-]*)((?:[^>"']|"[^"]*"|'[^']*')*)>/g;
|
|
21
|
+
const ATTR = /([^\s=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
22
|
+
|
|
23
|
+
/** Elements that start a line of their own: their boundaries end a text block. */
|
|
24
|
+
export const BLOCK: Record<string, true> = {
|
|
25
|
+
address: true,
|
|
26
|
+
article: true,
|
|
27
|
+
aside: true,
|
|
28
|
+
blockquote: true,
|
|
29
|
+
dd: true,
|
|
30
|
+
details: true,
|
|
31
|
+
dialog: true,
|
|
32
|
+
div: true,
|
|
33
|
+
dl: true,
|
|
34
|
+
dt: true,
|
|
35
|
+
figcaption: true,
|
|
36
|
+
figure: true,
|
|
37
|
+
footer: true,
|
|
38
|
+
header: true,
|
|
39
|
+
main: true,
|
|
40
|
+
nav: true,
|
|
41
|
+
p: true,
|
|
42
|
+
section: true,
|
|
43
|
+
summary: true,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Headings, which are text blocks with a weight — omp treats them as text. */
|
|
47
|
+
export const HEADING: Record<string, true> = {
|
|
48
|
+
h1: true,
|
|
49
|
+
h2: true,
|
|
50
|
+
h3: true,
|
|
51
|
+
h4: true,
|
|
52
|
+
h5: true,
|
|
53
|
+
h6: true,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** Elements whose content is never visible as text. */
|
|
57
|
+
export const SKIPPED: Record<string, true> = {
|
|
58
|
+
button: true,
|
|
59
|
+
head: true,
|
|
60
|
+
iframe: true,
|
|
61
|
+
link: true,
|
|
62
|
+
meta: true,
|
|
63
|
+
noscript: true,
|
|
64
|
+
script: true,
|
|
65
|
+
style: true,
|
|
66
|
+
template: true,
|
|
67
|
+
title: true,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const VOID: Record<string, true> = {
|
|
71
|
+
area: true,
|
|
72
|
+
base: true,
|
|
73
|
+
br: true,
|
|
74
|
+
col: true,
|
|
75
|
+
embed: true,
|
|
76
|
+
hr: true,
|
|
77
|
+
img: true,
|
|
78
|
+
input: true,
|
|
79
|
+
link: true,
|
|
80
|
+
meta: true,
|
|
81
|
+
source: true,
|
|
82
|
+
track: true,
|
|
83
|
+
wbr: true,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function decodeEntities(text: string): string {
|
|
87
|
+
return text
|
|
88
|
+
.replace(/ /gi, ' ')
|
|
89
|
+
.replace(/</gi, '<')
|
|
90
|
+
.replace(/>/gi, '>')
|
|
91
|
+
.replace(/"/gi, '"')
|
|
92
|
+
.replace(/'|'/gi, "'")
|
|
93
|
+
.replace(/&/gi, '&')
|
|
94
|
+
.replace(/&#(\d+);/g, (_, code: string) => String.fromCodePoint(Number(code)));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function parseAttrs(raw: string): Record<string, string> {
|
|
98
|
+
const attrs: Record<string, string> = {};
|
|
99
|
+
ATTR.lastIndex = 0;
|
|
100
|
+
let match: RegExpExecArray | null;
|
|
101
|
+
while ((match = ATTR.exec(raw))) {
|
|
102
|
+
const name = match[1].toLowerCase();
|
|
103
|
+
if (!name || name === '/') continue;
|
|
104
|
+
attrs[name] = decodeEntities(match[2] ?? match[3] ?? match[4] ?? '');
|
|
105
|
+
}
|
|
106
|
+
return attrs;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function tokenize(html: string): Token[] {
|
|
110
|
+
const tokens: Token[] = [];
|
|
111
|
+
TAG.lastIndex = 0;
|
|
112
|
+
let cursor = 0;
|
|
113
|
+
let match: RegExpExecArray | null;
|
|
114
|
+
while ((match = TAG.exec(html))) {
|
|
115
|
+
if (match.index > cursor) tokens.push({ kind: 'text', text: decodeEntities(html.slice(cursor, match.index)) });
|
|
116
|
+
cursor = match.index + match[0].length;
|
|
117
|
+
const closing = match[0].startsWith('</');
|
|
118
|
+
const tag = match[1].toLowerCase();
|
|
119
|
+
if (closing) {
|
|
120
|
+
tokens.push({ kind: 'open', tag: `/${tag}`, attrs: {}, selfClosing: false });
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const selfClosing = VOID[tag] === true || /\/\s*$/.test(match[2] ?? '');
|
|
124
|
+
tokens.push({ kind: 'open', tag, attrs: parseAttrs(match[2] ?? ''), selfClosing });
|
|
125
|
+
}
|
|
126
|
+
if (cursor < html.length) tokens.push({ kind: 'text', text: decodeEntities(html.slice(cursor)) });
|
|
127
|
+
return tokens;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Index of the `</tag>` that closes the open tag at `start`, or the last index. */
|
|
131
|
+
export function matchingClose(tokens: Token[], start: number, tag: string): number {
|
|
132
|
+
let depth = 0;
|
|
133
|
+
for (let index = start; index < tokens.length; index += 1) {
|
|
134
|
+
const token = tokens[index];
|
|
135
|
+
if (token.kind !== 'open') continue;
|
|
136
|
+
if (token.tag === tag && !token.selfClosing) depth += 1;
|
|
137
|
+
else if (token.tag === `/${tag}`) {
|
|
138
|
+
depth -= 1;
|
|
139
|
+
if (depth === 0) return index;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return tokens.length;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Visible text of a range, dropping nested controls; block boundaries become spaces. */
|
|
146
|
+
export function textOf(tokens: Token[], start: number, end: number): string {
|
|
147
|
+
const parts: string[] = [];
|
|
148
|
+
for (let index = start; index < end; index += 1) {
|
|
149
|
+
const token = tokens[index];
|
|
150
|
+
if (token.kind === 'text') {
|
|
151
|
+
parts.push(token.text);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const tag = token.tag.replace(/^\//, '');
|
|
155
|
+
if (token.tag.startsWith('/')) {
|
|
156
|
+
if (BLOCK[tag] || HEADING[tag] || tag === 'br' || tag === 'label') parts.push(' ');
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (SKIPPED[tag]) {
|
|
160
|
+
index = matchingClose(tokens, index, tag);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (tag === 'input' || tag === 'select' || tag === 'textarea') continue;
|
|
164
|
+
if (tag === 'br') parts.push('\n');
|
|
165
|
+
else if (tag === 'img') parts.push(token.attrs.alt ?? '');
|
|
166
|
+
else if (BLOCK[tag] || HEADING[tag]) parts.push(' ');
|
|
167
|
+
}
|
|
168
|
+
return oneLine(parts.join(''));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function oneLine(text: string): string {
|
|
172
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** `sentry_dsn` → `sentry dsn`, the label omp invents for a control with none. */
|
|
176
|
+
export function nameLabel(name: string): string {
|
|
177
|
+
return name ? name.replace(/[_-]+/g, ' ') : '';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** First element named `tag` in a range, or -1. */
|
|
181
|
+
export function findChild(tokens: Token[], start: number, end: number, tag: string): number {
|
|
182
|
+
for (let index = start; index < end; index += 1) {
|
|
183
|
+
const token = tokens[index];
|
|
184
|
+
if (token.kind !== 'open' || token.tag !== tag) continue;
|
|
185
|
+
return index;
|
|
186
|
+
}
|
|
187
|
+
return -1;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Whether a range contains a control, so a list is layout rather than prose. */
|
|
191
|
+
export function hasControl(tokens: Token[], start: number, end: number): boolean {
|
|
192
|
+
for (let index = start; index < end; index += 1) {
|
|
193
|
+
const token = tokens[index];
|
|
194
|
+
if (token.kind === 'open' && (token.tag === 'input' || token.tag === 'select' || token.tag === 'textarea')) {
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The `ask` card's own vocabulary, shared by the form parser
|
|
8
|
+
* (`ask-form/index.ts`), the question helpers (`ask-questions.ts`) and the
|
|
9
|
+
* result reader (`ask-results.ts`).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface AskOption {
|
|
13
|
+
label: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
preview?: string;
|
|
16
|
+
/** The control's `value`, when an HTML form spelled one ("Value" in JSON). */
|
|
17
|
+
value?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AskQuestion {
|
|
21
|
+
id?: string;
|
|
22
|
+
/** Short chip omp shows above the question ("Authentication"). */
|
|
23
|
+
header?: string;
|
|
24
|
+
question: string;
|
|
25
|
+
options: AskOption[];
|
|
26
|
+
multi: boolean;
|
|
27
|
+
/** Index of the option omp marks ` (Recommended)`. */
|
|
28
|
+
recommended?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Which control the question came from. The HTML form distinguishes a choice
|
|
31
|
+
* list from a text field, and an answer's rendered text means different
|
|
32
|
+
* things depending on it (`ask-form/index.ts`). Absent on the pre-18.18.3 shape,
|
|
33
|
+
* where every question was a choice.
|
|
34
|
+
*/
|
|
35
|
+
kind?: 'choice' | 'text';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface AskAnswer {
|
|
39
|
+
selectedOptions: string[];
|
|
40
|
+
customInput?: string;
|
|
41
|
+
/** omp auto-selected the answer after `ask.timeout` elapsed. */
|
|
42
|
+
timedOut?: boolean;
|
|
43
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, expect, test } from 'bun:test';
|
|
7
|
+
|
|
8
|
+
import { parseAskForm } from '@/shared/lib/chat/ask-form';
|
|
9
|
+
|
|
10
|
+
describe('parseAskForm', () => {
|
|
11
|
+
test('reads a radio group as one question with its legend, options and recorded pick', () => {
|
|
12
|
+
const questions = parseAskForm(`<form>
|
|
13
|
+
<fieldset>
|
|
14
|
+
<legend>Session store</legend>
|
|
15
|
+
<p>Sessions live in memory and are lost on restart.</p>
|
|
16
|
+
<label><input type="radio" name="store" value="sqlite" checked> SQLite</label>
|
|
17
|
+
<label><input type="radio" name="store" value="redis"> Redis</label>
|
|
18
|
+
</fieldset>
|
|
19
|
+
</form>`);
|
|
20
|
+
|
|
21
|
+
expect(questions).toHaveLength(1);
|
|
22
|
+
expect(questions[0].id).toBe('store');
|
|
23
|
+
expect(questions[0].question).toBe('Sessions live in memory and are lost on restart.');
|
|
24
|
+
expect(questions[0].kind).toBe('choice');
|
|
25
|
+
expect(questions[0].multi).toBe(false);
|
|
26
|
+
expect(questions[0].options.map((option) => option.label)).toEqual([
|
|
27
|
+
'SQLite',
|
|
28
|
+
'Redis',
|
|
29
|
+
'Other (type your own)',
|
|
30
|
+
]);
|
|
31
|
+
expect(questions[0].options.map((option) => option.value)).toEqual(['sqlite', 'redis', undefined]);
|
|
32
|
+
expect(questions[0].recommended).toBe(0);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('a fieldset starts its own section, so a paragraph before it does not caption the field', () => {
|
|
36
|
+
// omp's own tool example: the paragraph is context, the legend names the
|
|
37
|
+
// group. Borrowing the paragraph made the live dialog title unmatchable.
|
|
38
|
+
const questions = parseAskForm(`<form>
|
|
39
|
+
<p>Sessions live in memory and are lost on restart.</p>
|
|
40
|
+
<fieldset>
|
|
41
|
+
<legend>Session store</legend>
|
|
42
|
+
<label><input type="radio" name="store" value="sqlite" checked> SQLite</label>
|
|
43
|
+
<label><input type="radio" name="store" value="redis"> Redis</label>
|
|
44
|
+
</fieldset>
|
|
45
|
+
<label>Session lifetime (days) <input type="number" name="ttl_days" value="30"></label>
|
|
46
|
+
</form>`);
|
|
47
|
+
|
|
48
|
+
expect(questions.map((question) => question.question)).toEqual(['Session store', 'Session lifetime (days)']);
|
|
49
|
+
expect(questions[0].recommended).toBe(0);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('falls back to the legend, then to the control name, when nothing captions the field', () => {
|
|
53
|
+
const questions = parseAskForm(`<form>
|
|
54
|
+
<fieldset>
|
|
55
|
+
<legend>Authentication</legend>
|
|
56
|
+
<label>Token <input name="token"></label>
|
|
57
|
+
<input name="sentry_dsn">
|
|
58
|
+
</fieldset>
|
|
59
|
+
</form>`);
|
|
60
|
+
|
|
61
|
+
expect(questions.map((question) => question.question)).toEqual(['Token', 'sentry dsn']);
|
|
62
|
+
// The legend already titled the section; the caption is the nearest text block.
|
|
63
|
+
expect(questions[0].kind).toBe('text');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('reads a checkbox group as multi-select and keeps an explicit Other row', () => {
|
|
67
|
+
const questions = parseAskForm(`<form>
|
|
68
|
+
<p>Which sources?</p>
|
|
69
|
+
<label><input type="checkbox" name="sources" value="git"> Git</label>
|
|
70
|
+
<label><input type="checkbox" name="sources" value="jira" checked> Jira</label>
|
|
71
|
+
<label><input type="checkbox" name="sources" value="other"> Other (describe)</label>
|
|
72
|
+
</form>`);
|
|
73
|
+
|
|
74
|
+
expect(questions[0]).toMatchObject({ id: 'sources', multi: true, question: 'Which sources?' });
|
|
75
|
+
expect(questions[0].options.map((option) => option.label)).toEqual(['Git', 'Jira', 'Other (describe)']);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('a lone checkbox asks its own label and gains no Other row', () => {
|
|
79
|
+
const questions = parseAskForm(`<form>
|
|
80
|
+
<label><input type="checkbox" name="agree" value="yes"> I agree to the terms</label>
|
|
81
|
+
</form>`);
|
|
82
|
+
|
|
83
|
+
expect(questions).toHaveLength(1);
|
|
84
|
+
expect(questions[0]).toMatchObject({ id: 'agree', question: 'I agree to the terms', multi: true });
|
|
85
|
+
expect(questions[0].options.map((option) => option.label)).toEqual(['I agree to the terms']);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('reads a select with its options, first enabled preselected, and no Other row', () => {
|
|
89
|
+
const questions = parseAskForm(`<form>
|
|
90
|
+
<label>Session lifetime <input type="number" name="ttl_days" value="30" min="1"></label>
|
|
91
|
+
<fieldset>
|
|
92
|
+
<legend>Store</legend>
|
|
93
|
+
<select name="store">
|
|
94
|
+
<option value="sqlite">SQLite</option>
|
|
95
|
+
<option value="redis" disabled>Redis</option>
|
|
96
|
+
</select>
|
|
97
|
+
</fieldset>
|
|
98
|
+
</form>`);
|
|
99
|
+
|
|
100
|
+
expect(questions.map((question) => question.question)).toEqual(['Session lifetime', 'Store']);
|
|
101
|
+
expect(questions[0]).toMatchObject({ id: 'ttl_days', kind: 'text', multi: false });
|
|
102
|
+
expect(questions[1].options.map((option) => option.label)).toEqual(['SQLite', 'Redis']);
|
|
103
|
+
expect(questions[1].recommended).toBe(0);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('reads a textarea and a <label for> outside the control', () => {
|
|
107
|
+
const questions = parseAskForm(`<form>
|
|
108
|
+
<label for="notes">Notes</label>
|
|
109
|
+
<p>Anything else?</p>
|
|
110
|
+
<textarea id="notes" name="notes">line one</textarea>
|
|
111
|
+
</form>`);
|
|
112
|
+
|
|
113
|
+
expect(questions[0]).toMatchObject({ id: 'notes', question: 'Notes', kind: 'text' });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('keeps inline markup inside a label as text and drops unnamed controls', () => {
|
|
117
|
+
const questions = parseAskForm(`<form>
|
|
118
|
+
<label>Run <code>make</code> <strong>now</strong>? <input type="radio" name="run" value="yes"></label>
|
|
119
|
+
<label><input type="radio" name="run" value="no"> later</label>
|
|
120
|
+
<input value="unnamed">
|
|
121
|
+
</form>`);
|
|
122
|
+
|
|
123
|
+
expect(questions).toHaveLength(1);
|
|
124
|
+
// No caption, legend or lone option to name it, so omp falls back to the name.
|
|
125
|
+
expect(questions[0].question).toBe('run');
|
|
126
|
+
expect(questions[0].options.map((option) => option.label)).toEqual([
|
|
127
|
+
'Run make now?',
|
|
128
|
+
'later',
|
|
129
|
+
'Other (type your own)',
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('falls back to the lone option label when a radio has no caption at all', () => {
|
|
134
|
+
const questions = parseAskForm(`<form><label>Yes <input type="radio" name="ok" value="yes"></label></form>`);
|
|
135
|
+
expect(questions[0].question).toBe('Yes');
|
|
136
|
+
});
|
|
137
|
+
});
|