pi-ext
bugabingas pi extensions
owned by bugabinga
refactor(q): rewrite heuristic extractor, add 112 tests, benchmark, cleanup dead code
e402aadc14a94804cd3a419bb33a2a89d563260f
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
- Drop English QUESTION_WORD_RE — language-agnostic, Unicode ? marks only
- Add code fence tracking, URL stripping, heading/blockquote stripping
- Expand TABLE_LINE_RE to cover markdown pipes, double-line box, ASCII art
- Add configurable extractor model via q.model in settings.json
- Optimize: string split, early ? gate, merged guard regex, in-place dedup
- Remove: looksLikeQuestion, authFailureMessage, isRecord, sendAnswerWith,
randomItem wrapper, duplicated parseModelRef, redundant try/catch
- Add bench.extract.ts baseline (~2000 lines p50 0.2ms)
Changed files (5)
extensions/q/__tests__/bench.extract.ts
Addednew file mode 100644 1 +import { heuristicExtract }
from "../extract.js";
5 +const SHORT_PLAIN
= `Here's the analysis of the codebase. 9 +The project uses a monorepo layout with the following packages: 11 +- Core library in \`packages/core\` 12 +- CLI tool in \`packages/cli\` 13 +- Extension API in \`packages/api\` 17 +All packages share a common tsconfig. The build uses esbuild for bundling. 21 +We use Bun's built-in test runner. Coverage is tracked via \`bun test --coverage\`. 25 +The core library is stable. The CLI tool needs more work on the argument parser. 26 +The extension API is experimental and may change. 28 +Which package should we focus on first?`;
30 +const MEDIUM_MIXED
= Array.
from({ length:
4 }, (_, section)
=> {
31 + const headers
= [
"Preflight checks",
"Build pipeline",
"Test results",
"Deployment"];
32 + return `## ${headers[section]} 34 +Here's the status for ${headers[section]}: 36 +| Check | Status | Notes | 37 +|-------|--------|-------| 38 +| Lint | ✅ | No warnings | 39 +| Types | ✅ | Strict mode | 41 +| Tests | ❌ | 3 failures | 50 +const defaultConfig: Config = { 58 +- Port can be overridden via env var 59 +- Host defaults to localhost 60 +- Verbose mode logs all requests 63 +curl https://api.example.com?query=test&format=json 66 +> This should work in production. You can configure it via environment variables. 67 +> Does not require authentication for local development. 69 +Should we proceed with ${headers[section].toLowerCase()}?`;
72 +const LONG
= Array.
from({ length:
40 }, (_, i)
=> {
73 + const lines
: string[]
= [];
74 + lines.
push(
`## Section ${i + 1}`);
76 + lines.
push(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore.");
78 + lines.
push(
"```javascript");
79 + lines.
push(
`const result = items.filter(x => x.active !== null ? x.value : defaultValue);`);
80 + lines.
push(
`// See https://docs.example.com?section=api for details`);
83 + lines.
push(
"| Name | Type | Default |");
84 + lines.
push(
"|------|------|---------|");
85 + lines.
push(
`| field${i} | string | "value${i}" |`);
86 + lines.
push(
`| active | boolean | ${i % 2 === 0 ? "true" : "false"} |`);
88 + lines.
push(
"┌──────────┬──────────┐");
89 + lines.
push(
"│ Key │ Value │");
90 + lines.
push(
"├──────────┼──────────┤");
91 + lines.
push(
`│ config │ ${i} │`);
92 + lines.
push(
"└──────────┴──────────┘");
94 + lines.
push(
"- First item with some detail");
95 + lines.
push(
"- Second item with more text");
96 + lines.
push(
"- Third item");
98 + lines.
push(
"> This should work fine. Can be configured via env vars.");
99 + lines.
push(
"> Does not require authentication.");
101 + lines.
push(
`See https://api.example.com/v2/endpoint?param=value&other=${i} for reference.`);
103 + if (i
% 10 === 9) {
104 + lines.
push(
`What should we do about section ${i + 1}?`);
105 + lines.
push(
"- Option A");
106 + lines.
push(
"- Option B");
107 + lines.
push(
"- Option C");
109 + return lines.
join(
"\n");
112 +const PATHOLOGICAL_TABLE
= ` Here's every failure mode in order of the flow: 116 + ┌──────────────────────────────┬────────────────┬──────────────────────────────────────────┐ 117 + │ What goes wrong │ Prompt │ Gap? │ 119 + ├──────────────────────────────┼────────────────┼──────────────────────────────────────────┤ 120 + │ Dirty worktree │ Stops │ ✅ │ 121 + │ (unstaged/uncommitted) │ │ │ 122 + ├──────────────────────────────┼────────────────┼──────────────────────────────────────────┤ 123 + │ Ongoing merge/rebase in │ Stops │ ✅ │ 125 + ├──────────────────────────────┼────────────────┼──────────────────────────────────────────┤ 126 + │ Detached HEAD (git) │ — │ Gap — prompt doesn't check │ 127 + └──────────────────────────────┴────────────────┴──────────────────────────────────────────┘ 131 + ┌─────────────────────────┬─────────────────┬──────────────────────────────────────────────┐ 132 + │ What goes wrong │ Prompt reaction │ Gap? │ 133 + ├─────────────────────────┼─────────────────┼──────────────────────────────────────────────┤ 134 + │ Network / auth failure │ Fails loudly │ ✅ │ 135 + ├─────────────────────────┼─────────────────┼──────────────────────────────────────────────┤ 136 + │ Remote force-pushed │ — │ Gap — fetch succeeds │ 137 + └─────────────────────────┴─────────────────┴──────────────────────────────────────────────┘ 139 + Want me to patch these into the prompt?`;
143 +function bench(name
: string,
fn: ()
=> void, iterations
: number) {
145 + for (
let i
= 0; i
< Math.
min(iterations,
100); i
++)
fn();
147 + const times
: number[]
= [];
148 + for (
let i
= 0; i
< iterations; i
++) {
149 + const start
= performance.
now();
151 + times.
push(
performance.
now()
- start);
153 + times.
sort((a, b)
=> a
- b);
154 + const mean
= times.
reduce((a, b)
=> a
+ b,
0)
/ times.
length;
155 + const p50
= times[
Math.
floor(times.
length * 0.5)];
156 + const p99
= times[
Math.
floor(times.
length * 0.99)];
157 + const min
= times[
0];
159 + `${name.padEnd(35)} mean=${mean.toFixed(3).padStart(8)}ms p50=${p50.toFixed(3).padStart(8)}ms p99=${p99.toFixed(3).padStart(8)}ms min=${min.toFixed(3).padStart(8)}ms (n=${iterations})`,
163 +console.
log(
"\n=== heuristicExtract baseline ===\n");
165 +bench(
"short plain (~50 lines)", ()
=> heuristicExtract(SHORT_PLAIN),
10000);
166 +bench(
"medium mixed (~200 lines)", ()
=> heuristicExtract(MEDIUM_MIXED),
5000);
167 +bench(
"long (~2000 lines)", ()
=> heuristicExtract(LONG),
1000);
168 +bench(
"pathological table", ()
=> heuristicExtract(PATHOLOGICAL_TABLE),
10000);
extensions/q/__tests__/extract.test.ts
Modified 1 1 import { describe, expect, test }
from "vitest";
2 -import { extractMarkedAnswer, heuristicExtract }
from "../extract.js";
7 +}
from "../extract.js";
4 -describe(
"heuristicExtract", ()
=> {
5 - test(
"extracts questions with explicit suggestions", ()
=> {
6 - const result
= heuristicExtract(
`Which database should we use? 12 +function qs(text
: string)
: string[] {
13 + return heuristicExtract(text).
map((q)
=> q.question);
17 +function suggestionsFor(text
: string, idx
= 0)
: string[] {
18 + return heuristicExtract(text)[idx]?.suggestions
?? [];
23 +describe(
"code fences", ()
=> {
24 + test(
"#1 ternary ? inside backtick fence", ()
=> {
25 + expect(
qs(
"```\nconst x = a ? b : c;\n```")).
toEqual([]);
27 + test(
"#2 ? inside backtick fence", ()
=> {
28 + expect(
qs(
"```\nreturn what?\n```")).
toEqual([]);
30 + test(
"#3 question outside fence after code", ()
=> {
32 + qs(
"```\nconst x = 1;\n```\n\nWhat should we do next?"),
33 + ).
toEqual([
"What should we do next?"]);
35 + test(
"#4 tilde fence skips content", ()
=> {
36 + expect(
qs(
"~~~\nwhat?\n~~~")).
toEqual([]);
38 + test(
"#5 fence with language tag", ()
=> {
39 + expect(
qs(
"```typescript\ntype X = Y | null?;\n```")).
toEqual([]);
41 + test(
"#6 multiple fences extract only outside", ()
=> {
43 + qs(
"```\nwhat?\n```\nReal question?\n```\nother?\n```"),
44 + ).
toEqual([
"Real question?"]);
46 + test(
"#7 SQL LIKE in fence", ()
=> {
47 + expect(
qs(
"```sql\nSELECT * WHERE name LIKE '%what?%';\n```")).
toEqual(
51 + test(
"#8 URL in fence", ()
=> {
52 + expect(
qs(
'```bash\ncurl "https://api.com?query=test"\n```')).
toEqual(
56 + test(
"#9 regex in fence", ()
=> {
57 + expect(
qs(
"```\n/pattern?/\n```")).
toEqual([]);
59 + test(
"#10 optional chaining in fence", ()
=> {
60 + expect(
qs(
"```\nobj?.method()?.chain()\n```")).
toEqual([]);
62 + test(
"#11 question before and after fence", ()
=> {
64 + qs(
"What port?\n```\nx = 1\n```\nWhat host?"),
65 + ).
toEqual([
"What port?",
"What host?"]);
67 + test(
"#12 unclosed fence skips everything after", ()
=> {
68 + expect(
qs(
"```\nwhat?\nno closing")).
toEqual([]);
70 + test(
"#13 fence with 4 backticks", ()
=> {
71 + expect(
qs(
"````\nwhat?\n````")).
toEqual([]);
73 + test(
"#14 question in fence, real question after", ()
=> {
74 + const originalTableInput
= ` 80 + expect(
qs(originalTableInput)).
toEqual([
"What should we do?"]);
82 + test(
"#15 diff in code fence", ()
=> {
84 + qs(
"```diff\n- old code?\n+ new code\n```"),
91 +describe(
"URLs", ()
=> {
92 + test(
"#16 standalone URL with query param", ()
=> {
93 + expect(
qs(
"https://example.com?foo=bar")).
toEqual([]);
95 + test(
"#17 URL with query in sentence", ()
=> {
96 + expect(
qs(
"See https://example.com?foo=bar for details.")).
toEqual([]);
98 + test(
"#18 API URL with auth param", ()
=> {
99 + expect(
qs(
"The URL is https://api.example.com/v2?auth=token")).
toEqual(
103 + test(
"#19 multiple URLs with ?", ()
=> {
105 + qs(
"Try https://a.com?x=1 and https://b.com?y=2"),
108 + test(
"#20 HTTP URL", ()
=> {
109 + expect(
qs(
"http://example.com?query=test")).
toEqual([]);
111 + test(
"#21 URL at end of real question", ()
=> {
113 + qs(
"Which endpoint? https://api.com?x=1"),
114 + ).
toEqual([
"Which endpoint?"]);
116 + test(
"#22 URL then real question on next line", ()
=> {
118 + qs(
"See https://api.com?x=1\nWhat should we do?"),
119 + ).
toEqual([
"What should we do?"]);
121 + test(
"#23 URL with fragment and query", ()
=> {
122 + expect(
qs(
"https://docs.com/page?section=intro#top")).
toEqual([]);
124 + test(
"#24 URL with multiple params", ()
=> {
126 + qs(
"https://api.com?a=1&b=2&c=what?"),
129 + test(
"#25 real question with URL context", ()
=> {
131 + qs(
"Visit https://status.com for info.\n\nWhich service?"),
132 + ).
toEqual([
"Which service?"]);
138 +describe(
"tables", ()
=> {
139 + test(
"#26 pipe table header", ()
=> {
140 + expect(
qs(
"| Header | Col? |")).
toEqual([]);
142 + test(
"#27 pipe table separator", ()
=> {
143 + expect(
qs(
"|---|---|")).
toEqual([]);
145 + test(
"#28 pipe table row with ?", ()
=> {
146 + expect(
qs(
"| does | this | work? |")).
toEqual([]);
148 + test(
"#29 unicode box table with ?", ()
=> {
149 + const table
= `┌──────┬──────┐ 154 + expect(
qs(table)).
toEqual([]);
156 + test(
"#30 double-line box table", ()
=> {
157 + expect(
qs(
"╔══════╗\n║ What? ║\n╚══════╝")).
toEqual([]);
159 + test(
"#31 ASCII art table", ()
=> {
160 + expect(
qs(
"+------+------+------+")).
toEqual([]);
162 + test(
"#32 ASCII art table border with content", ()
=> {
164 + qs(
"+------+\n| What |\n+------+"),
167 + test(
"#33 table then real question", ()
=> {
169 + qs(
"| a | b |\n|---|---|\n| 1 | 2 |\n\nWhich column?"),
170 + ).
toEqual([
"Which column?"]);
172 + test(
"#34 markdown table with bold", ()
=> {
173 + expect(
qs(
"| **does** | **work?** |")).
toEqual([]);
175 + test(
"#35 full markdown table", ()
=> {
176 + const table
= `| Name | Type | 180 + expect(
qs(table)).
toEqual([]);
182 + test(
"#36 original failing input from user", ()
=> {
183 + const input
= ` Here's every failure mode in order of the flow: 185 + ┌──────────────────────────────┬────────────────┬──────────────────────────────────────────┐ 186 + │ What goes wrong │ Prompt │ Gap? │ 188 + ├──────────────────────────────┼────────────────┼──────────────────────────────────────────┤ 189 + │ Dirty worktree │ Stops │ ✅ │ 190 + └──────────────────────────────┴────────────────┴──────────────────────────────────────────┘ 192 + Want me to patch these into the prompt?`;
193 + expect(
qs(input)).
toEqual([
"Want me to patch these into the prompt?"]);
199 +describe(
"headings", ()
=> {
200 + test(
"#37 heading with question word, no ?", ()
=> {
201 + expect(
qs(
"## How to install")).
toEqual([]);
203 + test(
"#38 heading with question word, no ? (what)", ()
=> {
204 + expect(
qs(
"### What's new in v2")).
toEqual([]);
206 + test(
"#39 heading with question word, no ? (when)", ()
=> {
207 + expect(
qs(
"## When to use this")).
toEqual([]);
209 + test(
"#40 heading with question word, no ? (where)", ()
=> {
210 + expect(
qs(
"### Where to find docs")).
toEqual([]);
212 + test(
"#41 heading with question word, no ? (why)", ()
=> {
213 + expect(
qs(
"## Why this approach")).
toEqual([]);
215 + test(
"#42 heading WITH ? is a question", ()
=> {
216 + expect(
qs(
"## Which database?")).
toEqual([
"Which database?"]);
218 + test(
"#43 heading with # prefix and ?", ()
=> {
219 + expect(
qs(
"# What port?")).
toEqual([
"What port?"]);
221 + test(
"#44 multiple headings mixed", ()
=> {
223 + qs(
"## How to install\n### Which version?\n## Why"),
224 + ).
toEqual([
"Which version?"]);
230 +describe(
"blockquotes", ()
=> {
231 + test(
"#45 blockquote statement (no ?)", ()
=> {
232 + expect(
qs(
"> Should work fine in production")).
toEqual([]);
234 + test(
"#46 blockquote statement with can", ()
=> {
235 + expect(
qs(
"> Can be configured via env vars")).
toEqual([]);
237 + test(
"#47 blockquote with real question", ()
=> {
238 + expect(
qs(
"> What do you think?")).
toEqual([
"What do you think?"]);
240 + test(
"#48 blockquote with which question", ()
=> {
241 + expect(
qs(
"> Which approach?")).
toEqual([
"Which approach?"]);
243 + test(
"#49 nested blockquote with ?", ()
=> {
244 + expect(
qs(
"> > What about this?")).
toEqual([
"What about this?"]);
246 + test(
"#50 admonition is not a question (no ?)", ()
=> {
247 + expect(
qs(
"> [!NOTE] Some info")).
toEqual([]);
253 +describe(
"unicode question marks", ()
=> {
254 + test(
"#51 CJK fullwidth ?", ()
=> {
255 + expect(
qs(
"用哪个数据库?")).
toEqual([
"用哪个数据库?"]);
257 + test(
"#52 Arabic ؟", ()
=> {
258 + expect(
qs(
"ما هذا؟")).
toEqual([
"ما هذا؟"]);
260 + test(
"#53 irony mark ⸮", ()
=> {
261 + expect(
qs(
"Oh really⸮")).
toEqual([
"Oh really⸮"]);
263 + test(
"#54 inverted + normal ¿...?", ()
=> {
264 + expect(
qs(
"¿Cuál base de datos?")).
toEqual([
"¿Cuál base de datos?"]);
266 + test(
"#55 Japanese ?", ()
=> {
267 + expect(
qs(
"どれを選びますか?")).
toEqual([
"どれを選びますか?"]);
269 + test(
"#56 Korean with ASCII ?", ()
=> {
270 + expect(
qs(
"어느 것을 사용합니까?")).
toEqual([
"어느 것을 사용합니까?"]);
272 + test(
"#57 Hindi ?", ()
=> {
273 + expect(
qs(
"कौन सा डेटाबेस?")).
toEqual([
"कौन सा डेटाबेस?"]);
275 + test(
"#58 Thai ?", ()
=> {
276 + expect(
qs(
"ใช้ฐานข้อมูลใด?")).
toEqual([
"ใช้ฐานข้อมูลใด?"]);
278 + test(
"#59 Vietnamese ?", ()
=> {
279 + expect(
qs(
"Dùng cơ sở dữ liệu nào?")).
toEqual([
280 + "Dùng cơ sở dữ liệu nào?",
283 + test(
"#60 mixed Unicode question marks", ()
=> {
285 + qs(
"Which one?\n用哪个?\nما هذا؟"),
286 + ).
toEqual([
"Which one?",
"用哪个?",
"ما هذا؟"]);
292 +describe(
"no question-word heuristic", ()
=> {
293 + test(
"#61 should-statement without ?", ()
=> {
294 + expect(
qs(
"This should work fine.")).
toEqual([]);
296 + test(
"#62 does-statement without ?", ()
=> {
297 + expect(
qs(
"That does the trick.")).
toEqual([]);
299 + test(
"#63 can-statement without ?", ()
=> {
300 + expect(
qs(
"You can use this pattern.")).
toEqual([]);
302 + test(
"#64 will-statement without ?", ()
=> {
303 + expect(
qs(
"It will be available soon.")).
toEqual([]);
305 + test(
"#65 would-statement without ?", ()
=> {
306 + expect(
qs(
"This would be the approach.")).
toEqual([]);
308 + test(
"#66 could-statement without ?", ()
=> {
309 + expect(
qs(
"We could try both.")).
toEqual([]);
311 + test(
"#67 did-statement without ?", ()
=> {
312 + expect(
qs(
"I did the migration.")).
toEqual([]);
314 + test(
"#68 heading with how (no ?)", ()
=> {
315 + expect(
qs(
"## How to configure")).
toEqual([]);
317 + test(
"#69 heading with should (no ?)", ()
=> {
318 + expect(
qs(
"### Should work")).
toEqual([]);
320 + test(
"#70 heading with does (no ?)", ()
=> {
321 + expect(
qs(
"## Does not apply")).
toEqual([]);
327 +describe(
"suggestion scoping", ()
=> {
328 + test(
"#71 two questions with separate suggestions", ()
=> {
329 + const result
= heuristicExtract(
`Which DB? 10 -Question: Should auth be required? 14 336 expect(result).
toEqual([
16 - question:
"Which database should we use?",
17 - suggestions: [
"Postgres",
"SQLite"],
19 - { question:
"Should auth be required?", suggestions: [
"yes",
"no"] },
337 + { question:
"Which DB?", suggestions: [
"Postgres",
"SQLite"] },
338 + { question:
"Which port?", suggestions: [
"5432",
"3306"] },
341 + test(
"#72 suggestions after blank line attach to latest", ()
=> {
342 + const result
= heuristicExtract(
`Which DB? 23 - test(
"deduplicates suggestions case-insensitively", ()
=> {
24 - const result
= heuristicExtract(
`What now? 28 - expect(result[
0]?.suggestions).
toEqual([
"Yes",
"No"]);
345 + expect(result[
0]?.suggestions).
toEqual([
"Postgres"]);
347 + test(
"#73 suggestions before first question ignored", ()
=> {
348 + const result
= heuristicExtract(
`- Postgres\n\nWhich DB?`);
349 + expect(result[
0]?.suggestions).
toEqual([]);
351 + test(
"#74 three questions, suggestion on last only", ()
=> {
352 + const result
= heuristicExtract(
`Abc? 356 + expect(result[
2]?.suggestions).
toEqual([
"X"]);
358 + test(
"#75 alternating Q-suggestion-Q-suggestion", ()
=> {
359 + const result
= heuristicExtract(
`Q1 here? 363 + expect(result[
0]?.suggestions).
toEqual([
"A"]);
364 + expect(result[
1]?.suggestions).
toEqual([
"B"]);
366 + test(
"#76 prose between suggestions still attaches", ()
=> {
367 + const result
= heuristicExtract(
`Which DB?\n- A\n\ntext\n- B`);
368 + expect(result[
0]?.suggestions).
toEqual([
"A",
"B"]);
370 + test(
"#77 heading between question and suggestion", ()
=> {
371 + const result
= heuristicExtract(
`Which DB?\n### Section\n- Postgres`);
372 + expect(result[
0]?.suggestions).
toEqual([
"Postgres"]);
374 + test(
"#78 MAX_SUGGESTIONS caps at 8", ()
=> {
375 + const result
= heuristicExtract(
376 + `Which?\n${Array.from({ length: 20 }, (_, i) => `- opt${i}`).join("\n")}`,
378 + expect(result[
0]?.suggestions.
length).
toBe(
8);
380 + test(
"#79 code fence between question and suggestion", ()
=> {
381 + const result
= heuristicExtract(
382 + `Which DB?\n\`\`\`\ncode\n\`\`\`\n- Postgres`,
384 + expect(result[
0]?.suggestions).
toEqual([
"Postgres"]);
386 + test(
"#80 table between question and suggestion", ()
=> {
387 + const result
= heuristicExtract(
388 + `Which DB?\n| a | b |\n---|---|\n- Postgres`,
390 + expect(result[
0]?.suggestions).
toEqual([
"Postgres"]);
397 +describe(
"hasQuestionMark + cleanQuestion", ()
=> {
398 + test(
"#81 standard question", ()
=> {
399 + expect(
hasQuestionMark(
"What port?")).
toBe(
true);
401 + test(
"#82 heading without ?", ()
=> {
402 + expect(
hasQuestionMark(
cleanQuestion(
"## How to install"))).
toBe(
false);
404 + test(
"#83 blockquote without ?", ()
=> {
405 + expect(
hasQuestionMark(
cleanQuestion(
"> Should work fine"))).
toBe(
false);
407 + test(
"#84 URL only", ()
=> {
408 + expect(
hasQuestionMark(
cleanQuestion(
"https://example.com?x=1"))).
toBe(
false);
410 + test(
"#85 ternary without fence", ()
=> {
411 + expect(
hasQuestionMark(
"Use value ? yes : no")).
toBe(
true);
413 + test(
"#86 CJK fullwidth ?", ()
=> {
414 + expect(
hasQuestionMark(
"用哪个?")).
toBe(
true);
416 + test(
"#87 Arabic ؟", ()
=> {
417 + expect(
hasQuestionMark(
"ما هذا؟")).
toBe(
true);
419 + test(
"#88 heading with ? IS a question", ()
=> {
420 + expect(
hasQuestionMark(
cleanQuestion(
"## Which database?"))).
toBe(
true);
422 + test(
"#89 blockquote with ? IS a question", ()
=> {
423 + expect(
hasQuestionMark(
cleanQuestion(
"> What do you think?"))).
toBe(
true);
425 + test(
"#90 numbered question", ()
=> {
426 + expect(
hasQuestionMark(
cleanQuestion(
"1. What port?"))).
toBe(
true);
432 +describe(
"real LLM patterns", ()
=> {
433 + test(
"#91 error in code + real question", ()
=> {
434 + const input
= `I got an error: 437 +Error: file not found? 441 + expect(
qs(input)).
toEqual([
"What should I do?"]);
443 + test(
"#92 numbered options after question", ()
=> {
444 + const input
= `Which approach? 448 + const result
= heuristicExtract(input);
449 + expect(result[
0]?.question).
toBe(
"Which approach?");
450 + expect(result[
0]?.suggestions).
toEqual([
"Quick fix",
"Full refactor"]);
452 + test(
"#93 standard Q&A pattern", ()
=> {
453 + const input
= `What would you like? 457 + const result
= heuristicExtract(input);
458 + expect(result[
0]?.question).
toBe(
"What would you like?");
459 + expect(result[
0]?.suggestions).
toEqual([
"Option A",
"Option B"]);
461 + test(
"#94 let me know is not a question", ()
=> {
462 + expect(
qs(
"Let me know if you have questions.")).
toEqual([]);
464 + test(
"#95 rhetorical question IS a question", ()
=> {
465 + expect(
qs(
"Does that make sense?")).
toEqual([
"Does that make sense?"]);
467 + test(
"#96 any questions IS a question", ()
=> {
468 + expect(
qs(
"Any questions?")).
toEqual([
"Any questions?"]);
470 + test(
"#97 multi-section with multiple questions", ()
=> {
471 + const input
= `## Preflight 483 + expect(
qs(input)).
toEqual([
"What branch?",
"Which environment?"]);
485 + test(
"#98 question with inline suggestion format", ()
=> {
487 + qs(
"Which DB? (Postgres/SQLite)"),
488 + ).
toEqual([
"Which DB? (Postgres/SQLite)"]);
490 + test(
"#99 diff in code then question", ()
=> {
491 + const input
= `Here's the change: 498 +Does this look correct? 502 + const result
= heuristicExtract(input);
503 + expect(result[
0]?.question).
toBe(
"Does this look correct?");
504 + expect(result[
0]?.suggestions).
toEqual([
"Yes",
"No"]);
506 + test(
"#100 question with emoji prefix", ()
=> {
507 + expect(
qs(
"❓ Which database?")).
toEqual([
"❓ Which database?"]);
513 +describe(
"edge cases", ()
=> {
514 + test(
"#101 empty input", ()
=> {
515 + expect(
qs(
"")).
toEqual([]);
517 + test(
"#102 only whitespace", ()
=> {
518 + expect(
qs(
" \n \n ")).
toEqual([]);
520 + test(
"#103 only newlines", ()
=> {
521 + expect(
qs(
"\n\n\n")).
toEqual([]);
523 + test(
"#104 ??? too short after clean", ()
=> {
524 + expect(
qs(
"???")).
toEqual([]);
526 + test(
"#105 Why? is 4 chars, extracted", ()
=> {
527 + expect(
qs(
"Why?")).
toEqual([
"Why?"]);
529 + test(
"#106 How? is 4 chars, extracted", ()
=> {
530 + expect(
qs(
"How?")).
toEqual([
"How?"]);
532 + test(
"#107 BOM prefix", ()
=> {
533 + expect(
qs(
"\uFEFFWhat port?")).
toEqual([
"What port?"]);
535 + test(
"#108 ANSI escape codes", ()
=> {
536 + expect(
qs(
"\x1B[31mWhat port?\x1B[0m")).
toEqual([
"What port?"]);
538 + test(
"#109 non-breaking space", ()
=> {
539 + expect(
qs(
"What\u00A0port?")).
toEqual([
"What port?"]);
541 + test(
"#110 zero-width space", ()
=> {
542 + expect(
qs(
"\u200BWhat\u200Bport?")).
toEqual([
"What port?"]);
32 548 describe(
"extractMarkedAnswer", ()
=> {
33 549 test(
"returns latest q-answer marker", ()
=> {
34 550 const result
= extractMarkedAnswer(
`<q-answer>draft</q-answer> extensions/q/extract.ts
Modified 7 7 import { ANSWER_RE, MAX_SUGGESTIONS }
from "./constants.js";
8 8 import type { QuestionItem }
from "./types.js";
10 +const TABLE_LINE_RE
= /^[│┌┐└┘├┤┬┴┼╞╡╟╢╤╧╪─━┅┈╔╗╚╝║╠╣╦╩╬|]|\+[-=+]+/;
10 11 const QUESTION_PREFIX_RE
= /^\s*(?:[-*+]\s+|\d+[.)]\s+|[A-Za-z][.)]\s+)/;
11 12 const QUESTION_LABEL_RE
= /^\s*(?:Q(?:uestion)?\s*[:.-]\s*)/i;
12 13 const SUGGESTION_RE
= 13 14 /^\s*(?:[-*+]\s+|\d+[.)]\s+|[A-Za-z][.)]\s+|(?:Option|Answer|Choice)\s+[A-Za-z0-9]+\s*[:.)-]\s*)(.+?)\s*$/i;
14 -const QUESTION_WORD_RE
= 15 - /^(?:should|do|does|did|can|could|would|will|which|what|when|where|why|how)\b/i;
16 +const QUESTION_MARK_RE
= /[?؟?⸮]/;
17 +const HEADING_RE
= /^#{1,6}\s*/;
18 +const BLOCKQUOTE_RE
= /^(?:>\s*)+/;
19 +const FENCE_RE
= /^(`{3,}|~{3,})/;
22 +const ANSI_ESCAPE_RE
= /\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\))/g;
23 +const NEEDS_CLEAN_RE
= /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\u200B\u200C\u200D\uFEFF\u00A0\x1B]| {2}/;
17 25 function safeDisplayText(text
: string)
: string {
26 + if (
!NEEDS_CLEAN_RE.
test(text))
return text.
trim();
20 - /\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\))/g,
28 + .
replace(ANSI_ESCAPE_RE,
"")
23 29 .
replace(
/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g,
" ")
30 + .
replace(
/[\u200B\u200C\u200D\uFEFF\u00A0]/g,
" ")
24 31 .
replace(
/\s+/g,
" ")
68 76 export function extractMarkedAnswer(text
: string)
: string | undefined {
69 - let found
: string | undefined;
70 - for (
const match
of text.
matchAll(ANSWER_RE)) found
= match[
1]?.
trim();
71 - return found
|| undefined;
77 + let match
: RegExpExecArray | null;
78 + let last
: string | undefined;
79 + while ((match
= ANSWER_RE.
exec(text))
!== null) last
= match[
1]?.
trim();
74 83 export function lastMarkedAnswer(ctx
: ExtensionContext)
: string | undefined {
95 106 export function cleanQuestion(line
: string)
: string {
96 107 return safeDisplayText(
97 - line.
replace(QUESTION_PREFIX_RE,
"").
replace(QUESTION_LABEL_RE,
""),
109 + .
replace(HEADING_RE,
"")
110 + .
replace(BLOCKQUOTE_RE,
"")
111 + .
replace(QUESTION_PREFIX_RE,
"")
112 + .
replace(QUESTION_LABEL_RE,
"")
113 + .
replace(
/https?:\/\/\S+/g,
""),
108 124 return suggestion
|| undefined;
111 -export function looksLikeQuestion(line
: string)
: boolean {
112 - const cleaned
= cleanQuestion(line);
113 - return cleaned.
includes(
"?")
|| QUESTION_WORD_RE.
test(cleaned);
129 +export function hasQuestionMark(line
: string)
: boolean {
130 + return QUESTION_MARK_RE.
test(line);
116 144 export function heuristicExtract(text
: string)
: QuestionItem[] {
117 145 const questions
: QuestionItem[]
= [];
118 146 let current
: QuestionItem | undefined;
147 + let inFence
= false;
120 - for (
const raw
of text.
split(
/\r?\n/)) {
121 - const line
= raw.
trim();
122 - if (
!line)
continue;
149 + for (
const raw
of text.
split(
"\n")) {
150 + const line
= raw.
replace(
/\r$/,
"").
trim();
151 + if (FENCE_RE.
test(line)) { inFence
= !inFence;
continue; }
152 + if (inFence)
continue;
153 + if (
!line
|| TABLE_LINE_RE.
test(line))
continue;
124 - const question
= looksLikeQuestion(line)
? cleanQuestion(line)
: undefined;
125 - if (question
&& question.
length > 3) {
126 - current
= { question, suggestions: [] };
127 - questions.
push(current);
156 + if (
hasQuestionMark(line)) {
157 + const question
= cleanQuestion(line);
159 + if (question.
length > 3 && hasQuestionMark(question)) {
160 + current
= { question, suggestions: [] };
161 + questions.
push(current);
131 166 const suggestion
= cleanSuggestion(line);
132 - if (suggestion
&& current
&& !looksLikeQuestion(suggestion))
167 + if (suggestion
&& current
&& !hasQuestionMark(suggestion))
133 168 current.suggestions.
push(suggestion);
136 - return questions.
map((q)
=> ({
138 - suggestions:
unique(q.suggestions).
slice(
0, MAX_SUGGESTIONS),
172 + for (
let i
= 0; i
< questions.
length; i
++) {
173 + questions[i]
!.suggestions
= unique(questions[i]
!.suggestions).
slice(
0, MAX_SUGGESTIONS);
142 -function authFailureMessage(auth
: { ok
: boolean }, provider
: string)
: string {
143 - return auth.ok
? `No API key for ${provider}` : "auth failed";
146 180 function parseExtractorJson(raw
: string)
: QuestionItem[] {
147 181 const json
= raw.
match(
/\{[\s\S]*\}/)?.[
0]
?? raw;
164 198 .
filter((q)
=> q.question.
length > 0);
201 +function parseModelRef(ref
: string)
: { provider
: string; id
: string }
| undefined {
202 + const sep
= ref.
indexOf(
"/");
203 + if (sep
<= 0 || sep
=== ref.
length - 1)
return undefined;
204 + return { provider: ref.
slice(
0, sep), id: ref.
slice(sep
+ 1) };
167 207 export async function llmExtract(
168 208 ctx
: ExtensionCommandContext,
170 211 )
: Promise<
QuestionItem[]> {
171 - if (
!ctx.model)
throw new Error(
"No model selected");
172 - const auth
= await ctx.modelRegistry.
getApiKeyAndHeaders(ctx.model);
212 + const parsed
= modelRef
? parseModelRef(modelRef)
: undefined;
213 + const resolved
= parsed
214 + ? ctx.modelRegistry.
find(parsed.provider, parsed.
id)
216 + if (
!resolved)
throw new Error(modelRef
? `Model not found: ${modelRef}` : "No model selected");
218 + const auth
= await ctx.modelRegistry.
getApiKeyAndHeaders(resolved);
173 219 if (
!auth.ok
|| !auth.apiKey)
174 - throw new Error(
authFailureMessage(auth, ctx.model.provider));
220 + throw new Error(auth.ok
? `No API key for ${resolved.provider}` : "auth failed");
176 222 const userMessage
: Message = {
179 225 timestamp:
Date.
now(),
181 227 const response
= await complete(
183 229 { systemPrompt: EXTRACTOR_PROMPT, messages: [userMessage] },
185 231 apiKey: auth.apiKey,
199 - return parseExtractorJson(raw);
202 - `LLM extractor returned invalid JSON: ${(err as Error).message}`,
245 + return parseExtractorJson(raw);
extensions/q/index.ts
Modified 29 29 } from
"./types.js";
30 30 import { runWizard }
from "./wizard-ui.js";
32 -function sendAnswerWith(pi
: ExtensionAPI)
: SendAnswer {
33 - return (answer)
=> pi.
sendUserMessage([{ type:
"text", text: answer }]);
32 +type SettingsAPI = ExtensionAPI & {
33 + settingsManager
?: {
getSettings()
: Record<
string,
unknown>
| undefined };
36 -const NO_QUESTIONS_MESSAGES
= [
37 - "🐾 no questions found uwu — paws empty",
38 - "🫧 no questions found uwu — just vibes",
39 - "🌸 no questions found uwu — bloom later",
40 - "🦝 no questions found uwu — trash panda found nada",
41 - "✨ no questions found uwu — sparkle only",
44 -function randomItem(items
: readonly string[])
: string {
45 - return items[
Math.
floor(
Math.
random()
* items.
length)]
?? items[
0]
?? "";
36 +function readSettings(pi
: ExtensionAPI)
: { model
: string | undefined } {
37 + const raw
= (pi
as SettingsAPI).settingsManager?.
getSettings?.();
39 + raw
&& typeof raw
=== "object" && !Array.
isArray(raw)
&& "q" in raw
&& typeof raw.q
=== "object" && raw.q
!== null && !Array.
isArray(raw.q)
40 + ? raw.q
as Record<
string,
unknown>
44 + typeof q.model
=== "string" && q.model.
trim()
? q.model.
trim()
: undefined,
48 48 function randomNoQuestionsMessage()
: string {
49 - return randomItem(NO_QUESTIONS_MESSAGES);
50 + "🐾 no questions found uwu — paws empty",
51 + "🫧 no questions found uwu — just vibes",
52 + "🌸 no questions found uwu — bloom later",
53 + "🦝 no questions found uwu — trash panda found nada",
54 + "✨ no questions found uwu — sparkle only",
56 + return msgs[
Math.
floor(
Math.
random()
* msgs.
length)];
52 59 function newWizardState(
65 72 async function extractQuestions(
66 73 ctx
: ExtensionCommandContext,
68 76 )
: Promise<
QuestionItem[]> {
69 77 const questions
= heuristicExtract(text);
70 78 if (questions.
length === 0)
return [];
71 79 if (questions.
every((q)
=> q.suggestions.
length > 0))
return questions;
73 - const extracted
= await llmExtract(ctx, text);
81 + const extracted
= await llmExtract(ctx, text, modelRef);
74 82 return mergeSuggestions(questions, extracted);
109 117 ctx: ExtensionCommandContext,
110 118 sendAnswer: SendAnswer,
112 121 if (
!ctx.hasUI)
return ctx.ui.
notify(
"/q requires interactive mode",
"error");
113 122 await ctx.
waitForIdle();
118 127 let questions
: Awaited<
ReturnType<
typeof extractQuestions>>;
120 - questions
= await extractQuestions(ctx, assistant.
text);
129 + questions
= await extractQuestions(ctx, assistant.
text, modelRef);
123 132 `question extraction failed: ${(err as Error).message}`,
329 338 return { systemPrompt:
event.systemPrompt
+ discussionPrompt(meta) };
341 + const settings
= readSettings(pi);
332 343 pi.
registerCommand(
"q", {
333 344 description:
"Answer question suggestions from last assistant message",
334 - handler: (args, ctx)
=> startQ(args, ctx,
sendAnswerWith(pi)),
345 + handler: (args, ctx)
=> 346 + startQ(args, ctx, (a)
=> pi.
sendUserMessage([{ type:
"text", text: a }]), settings.model),
337 349 pi.
registerCommand(
"b", {
extensions/q/session-state.ts
Modified70 70 payload: ReturnPayload,
72 72 const { snapshot: target }
= payload;
73 - const errors
: string[]
= [];
74 - if (ctx.sessionManager.
getSessionFile()
!== target.originalSessionFile)
75 - errors.
push(
"session file mismatch");
76 - if (ctx.sessionManager.
getSessionId()
!== target.originalSessionId)
77 - errors.
push(
"session id mismatch");
78 - if (ctx.sessionManager.
getLeafId()
!== target.returnHeadId)
79 - errors.
push(
"original leaf moved");
80 - if (ctx.sessionManager.
getEntries().
length !== target.returnEntryCount)
81 - errors.
push(
"entry count changed");
73 + const checks
: [
boolean,
string][]
= [
74 + [ctx.sessionManager.
getSessionFile()
!== target.originalSessionFile,
"session file mismatch"],
75 + [ctx.sessionManager.
getSessionId()
!== target.originalSessionId,
"session id mismatch"],
76 + [ctx.sessionManager.
getLeafId()
!== target.returnHeadId,
"original leaf moved"],
77 + [ctx.sessionManager.
getEntries().
length !== target.returnEntryCount,
"entry count changed"],
79 + const errors
= checks.
filter(([fail])
=> fail).
map(([_, msg])
=> msg);
83 81 const wizard
= latestWizard(ctx, payload.wizardId);
84 82 if (
!wizard) errors.
push(
"pending wizard not found");
85 - else if (wizard.
status !== "waiting-fork")
86 - errors.
push(
`wizard is ${wizard.status}`);
87 - else if (wizard.
index !== payload.questionIndex)
88 - errors.
push(
"question index mismatch");
83 + else if (wizard.
status !== "waiting-fork") errors.
push(
`wizard is ${wizard.status}`);
84 + else if (wizard.
index !== payload.questionIndex) errors.
push(
"question index mismatch");