pi-querit 1.1.0 → 1.1.1
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/LICENSE +21 -21
- package/README.md +186 -186
- package/package.json +63 -63
- package/src/client.ts +385 -385
- package/src/config.ts +200 -200
- package/src/format.ts +101 -105
- package/src/index.ts +378 -378
- package/src/output.ts +138 -138
- package/src/sanitize.ts +97 -97
- package/src/setup.ts +330 -330
package/src/setup.ts
CHANGED
|
@@ -1,330 +1,330 @@
|
|
|
1
|
-
import { DynamicBorder, type ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
3
|
-
Container,
|
|
4
|
-
Input,
|
|
5
|
-
Spacer,
|
|
6
|
-
Text,
|
|
7
|
-
type Focusable,
|
|
8
|
-
type TUI,
|
|
9
|
-
} from "@earendil-works/pi-tui";
|
|
10
|
-
import {
|
|
11
|
-
COUNTRY_VALUES,
|
|
12
|
-
LANGUAGE_VALUES,
|
|
13
|
-
type QueritConfig,
|
|
14
|
-
type QueritCountry,
|
|
15
|
-
type QueritLanguage,
|
|
16
|
-
type QueritSearchDefaults,
|
|
17
|
-
} from "./config.js";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Masks the API key during rendering by temporarily swapping the Input value
|
|
21
|
-
* with asterisks. Safe because pi-tui's Input.setValue is a pure field
|
|
22
|
-
* assignment (no callbacks) and Input.render is a pure computation.
|
|
23
|
-
*/
|
|
24
|
-
class MaskedInput extends Input {
|
|
25
|
-
override render(width: number): string[] {
|
|
26
|
-
const secret = this.getValue();
|
|
27
|
-
this.setValue("*".repeat(secret.length));
|
|
28
|
-
try {
|
|
29
|
-
return super.render(width);
|
|
30
|
-
} finally {
|
|
31
|
-
this.setValue(secret);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function promptForApiKey(ctx: ExtensionCommandContext): Promise<string | undefined> {
|
|
37
|
-
if (ctx.mode !== "tui") {
|
|
38
|
-
ctx.ui.notify("/querit-setup requires Pi's interactive TUI.", "error");
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return ctx.ui.custom<string | undefined>((tui, theme, _keybindings, done) => {
|
|
43
|
-
const container = new Container();
|
|
44
|
-
const input = new MaskedInput();
|
|
45
|
-
input.onSubmit = (value) => done(value.trim() || undefined);
|
|
46
|
-
input.onEscape = () => done(undefined);
|
|
47
|
-
|
|
48
|
-
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
49
|
-
container.addChild(new Spacer(1));
|
|
50
|
-
container.addChild(new Text(theme.fg("accent", theme.bold("Configure Querit")), 1, 0));
|
|
51
|
-
container.addChild(new Text("Enter your Querit API key. Input is masked and is not added to chat history.", 1, 0));
|
|
52
|
-
container.addChild(new Spacer(1));
|
|
53
|
-
container.addChild(input);
|
|
54
|
-
container.addChild(new Spacer(1));
|
|
55
|
-
container.addChild(new Text(theme.fg("dim", "Enter to validate and continue • Esc to cancel"), 1, 0));
|
|
56
|
-
container.addChild(new Spacer(1));
|
|
57
|
-
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
58
|
-
|
|
59
|
-
return new FocusableContainer(container, input, tui);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export type SetupMode = "replace-key" | "search-defaults";
|
|
64
|
-
|
|
65
|
-
export function maskApiKeyHint(apiKey: string): string {
|
|
66
|
-
const trimmed = apiKey.trim();
|
|
67
|
-
return trimmed.length <= 4 ? "****" : `…${trimmed.slice(-4)}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export async function promptForSetupMode(
|
|
71
|
-
ctx: ExtensionCommandContext,
|
|
72
|
-
config: QueritConfig,
|
|
73
|
-
): Promise<SetupMode | undefined> {
|
|
74
|
-
if (ctx.mode !== "tui") {
|
|
75
|
-
ctx.ui.notify("/querit-setup requires Pi's interactive TUI.", "error");
|
|
76
|
-
return undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const choice = await ctx.ui.select(
|
|
80
|
-
`Querit is already configured (API key ${maskApiKeyHint(config.apiKey)}). What would you like to change?`,
|
|
81
|
-
[
|
|
82
|
-
"Replace API key (full re-setup)",
|
|
83
|
-
"Change search defaults",
|
|
84
|
-
],
|
|
85
|
-
);
|
|
86
|
-
if (!choice) return undefined;
|
|
87
|
-
return choice.startsWith("Replace") ? "replace-key" : "search-defaults";
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export async function promptForSearchDefaults(
|
|
91
|
-
ctx: ExtensionCommandContext,
|
|
92
|
-
current: QueritSearchDefaults = {},
|
|
93
|
-
): Promise<QueritSearchDefaults | undefined> {
|
|
94
|
-
if (ctx.mode !== "tui") return {};
|
|
95
|
-
|
|
96
|
-
const next: QueritSearchDefaults = { ...current };
|
|
97
|
-
|
|
98
|
-
const countChoice = await ctx.ui.select(
|
|
99
|
-
"Default result count per search",
|
|
100
|
-
scalarOptions(current.count === undefined ? undefined : String(current.count), ["3", "5", "10", "20"]),
|
|
101
|
-
);
|
|
102
|
-
if (countChoice === undefined) return undefined;
|
|
103
|
-
if (isResetChoice(countChoice)) delete next.count;
|
|
104
|
-
else if (!isKeepChoice(countChoice)) {
|
|
105
|
-
const parsed = Number.parseInt(countChoice, 10);
|
|
106
|
-
if (Number.isInteger(parsed) && parsed >= 1 && parsed <= 20) next.count = parsed;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const timeChoice = await ctx.ui.select(
|
|
110
|
-
"Default time range filter",
|
|
111
|
-
scalarOptions(current.timeRange, [
|
|
112
|
-
"d7 (past 7 days)",
|
|
113
|
-
"w2 (past 2 weeks)",
|
|
114
|
-
"m3 (past 3 months)",
|
|
115
|
-
"y1 (past year)",
|
|
116
|
-
]),
|
|
117
|
-
);
|
|
118
|
-
if (timeChoice === undefined) return undefined;
|
|
119
|
-
if (isResetChoice(timeChoice)) delete next.timeRange;
|
|
120
|
-
else if (!isKeepChoice(timeChoice)) next.timeRange = timeChoice.split(" ", 1)[0];
|
|
121
|
-
|
|
122
|
-
const contentChoice = await ctx.ui.select(
|
|
123
|
-
"Include sentence-level content excerpts by default?",
|
|
124
|
-
scalarOptions(
|
|
125
|
-
current.includeContent === undefined ? undefined : current.includeContent ? "yes" : "no",
|
|
126
|
-
["Yes, include excerpts", "No, snippets only"],
|
|
127
|
-
),
|
|
128
|
-
);
|
|
129
|
-
if (contentChoice === undefined) return undefined;
|
|
130
|
-
if (isResetChoice(contentChoice)) delete next.includeContent;
|
|
131
|
-
else if (!isKeepChoice(contentChoice)) next.includeContent = contentChoice.startsWith("Yes");
|
|
132
|
-
|
|
133
|
-
const countries = await promptForEnumList(ctx, {
|
|
134
|
-
title: `Default countries filter${formatCurrentList(current.countries)}`,
|
|
135
|
-
hint: `Comma-separated country names. Valid values: ${COUNTRY_VALUES.join(", ")}.`,
|
|
136
|
-
allowed: COUNTRY_VALUES,
|
|
137
|
-
});
|
|
138
|
-
if (countries === undefined) return undefined;
|
|
139
|
-
if (countries.cleared) delete next.countries;
|
|
140
|
-
else if (countries.values !== undefined) next.countries = countries.values as QueritCountry[];
|
|
141
|
-
|
|
142
|
-
const languages = await promptForEnumList(ctx, {
|
|
143
|
-
title: `Default languages filter${formatCurrentList(current.languages)}`,
|
|
144
|
-
hint: `Comma-separated language names. Valid values: ${LANGUAGE_VALUES.join(", ")}.`,
|
|
145
|
-
allowed: LANGUAGE_VALUES,
|
|
146
|
-
});
|
|
147
|
-
if (languages === undefined) return undefined;
|
|
148
|
-
if (languages.cleared) delete next.languages;
|
|
149
|
-
else if (languages.values !== undefined) next.languages = languages.values as QueritLanguage[];
|
|
150
|
-
|
|
151
|
-
const includeDomains = await promptForDomainChoice(ctx, {
|
|
152
|
-
message: "Restrict results to specific domains? (include whitelist)",
|
|
153
|
-
current: current.includeDomains,
|
|
154
|
-
customTitle: "Include only these domains (whitelist)",
|
|
155
|
-
customHint: "Only these domains will return results. Example: github.com, stackoverflow.com, developer.mozilla.org. Comma-separated; 'none' clears.",
|
|
156
|
-
});
|
|
157
|
-
if (includeDomains === undefined) return undefined;
|
|
158
|
-
if (includeDomains.cleared) delete next.includeDomains;
|
|
159
|
-
else if (includeDomains.values !== undefined) next.includeDomains = includeDomains.values;
|
|
160
|
-
const excludeDomains = await promptForDomainChoice(ctx, {
|
|
161
|
-
message: "Exclude specific domains from results? (blacklist)",
|
|
162
|
-
current: current.excludeDomains,
|
|
163
|
-
preset: EXCLUDE_DOMAIN_PRESET,
|
|
164
|
-
customTitle: "Exclude these domains (blacklist)",
|
|
165
|
-
customHint: "These domains will never return results. Comma-separated; 'none' clears.",
|
|
166
|
-
});
|
|
167
|
-
if (excludeDomains === undefined) return undefined;
|
|
168
|
-
if (excludeDomains.cleared) delete next.excludeDomains;
|
|
169
|
-
else if (excludeDomains.values !== undefined) next.excludeDomains = excludeDomains.values;
|
|
170
|
-
|
|
171
|
-
return next;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const EXCLUDE_DOMAIN_PRESET = {
|
|
175
|
-
label: "Noise blockers (pinterest.com, facebook.com, instagram.com, tiktok.com)",
|
|
176
|
-
values: ["pinterest.com", "facebook.com", "instagram.com", "tiktok.com"],
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
interface ListPromptResult {
|
|
180
|
-
cleared: boolean;
|
|
181
|
-
values?: string[];
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
async function promptForEnumList(
|
|
185
|
-
ctx: ExtensionCommandContext,
|
|
186
|
-
options: { title: string; hint: string; allowed: readonly string[] },
|
|
187
|
-
): Promise<ListPromptResult | undefined> {
|
|
188
|
-
for (;;) {
|
|
189
|
-
const raw = await promptForPlainText(ctx, options.title, options.hint);
|
|
190
|
-
if (raw === undefined) return undefined;
|
|
191
|
-
const trimmed = raw.trim();
|
|
192
|
-
if (trimmed === "") return { cleared: false };
|
|
193
|
-
if (trimmed.toLowerCase() === "none") return { cleared: true };
|
|
194
|
-
const values = splitList(trimmed);
|
|
195
|
-
const invalid = values.filter((value) => !options.allowed.includes(value));
|
|
196
|
-
if (invalid.length > 0) {
|
|
197
|
-
ctx.ui.notify(`Unknown values: ${invalid.join(", ")}. ${options.hint}`, "error");
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
return { cleared: false, values: [...new Set(values)] };
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
async function promptForDomainList(
|
|
205
|
-
ctx: ExtensionCommandContext,
|
|
206
|
-
options: { title: string; hint: string },
|
|
207
|
-
): Promise<ListPromptResult | undefined> {
|
|
208
|
-
for (;;) {
|
|
209
|
-
const raw = await promptForPlainText(ctx, options.title, options.hint);
|
|
210
|
-
if (raw === undefined) return undefined;
|
|
211
|
-
const trimmed = raw.trim();
|
|
212
|
-
if (trimmed === "") return { cleared: false };
|
|
213
|
-
if (trimmed.toLowerCase() === "none") return { cleared: true };
|
|
214
|
-
const values = splitList(trimmed);
|
|
215
|
-
const invalid = values.filter((value) => value.length > 253 || /\s/u.test(value) || !value.includes("."));
|
|
216
|
-
if (invalid.length > 0) {
|
|
217
|
-
ctx.ui.notify(`Invalid domains: ${invalid.join(", ")}. ${options.hint}`, "error");
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
return { cleared: false, values: [...new Set(values)] };
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
async function promptForDomainChoice(
|
|
225
|
-
ctx: ExtensionCommandContext,
|
|
226
|
-
options: {
|
|
227
|
-
message: string;
|
|
228
|
-
current: string[] | undefined;
|
|
229
|
-
preset?: { label: string; values: string[] };
|
|
230
|
-
customTitle: string;
|
|
231
|
-
customHint: string;
|
|
232
|
-
},
|
|
233
|
-
): Promise<ListPromptResult | undefined> {
|
|
234
|
-
const hasCurrent = options.current !== undefined && options.current.length > 0;
|
|
235
|
-
const selectOptions: string[] = [];
|
|
236
|
-
if (hasCurrent) {
|
|
237
|
-
selectOptions.push(`Keep current (${options.current?.join(", ")})`);
|
|
238
|
-
selectOptions.push("Reset (no domain filter)");
|
|
239
|
-
} else {
|
|
240
|
-
selectOptions.push("Skip (no domain filter)");
|
|
241
|
-
}
|
|
242
|
-
if (options.preset) selectOptions.push(options.preset.label);
|
|
243
|
-
selectOptions.push("Enter a custom list…");
|
|
244
|
-
|
|
245
|
-
const choice = await ctx.ui.select(options.message, selectOptions);
|
|
246
|
-
if (choice === undefined) return undefined;
|
|
247
|
-
if (isKeepChoice(choice)) return { cleared: false };
|
|
248
|
-
if (isResetChoice(choice)) return { cleared: true };
|
|
249
|
-
if (options.preset && choice === options.preset.label) {
|
|
250
|
-
return { cleared: false, values: [...options.preset.values] };
|
|
251
|
-
}
|
|
252
|
-
return promptForDomainList(ctx, { title: options.customTitle, hint: options.customHint });
|
|
253
|
-
}
|
|
254
|
-
async function promptForPlainText(
|
|
255
|
-
ctx: ExtensionCommandContext,
|
|
256
|
-
title: string,
|
|
257
|
-
hint: string,
|
|
258
|
-
): Promise<string | undefined> {
|
|
259
|
-
return ctx.ui.custom<string | undefined>((tui, theme, _keybindings, done) => {
|
|
260
|
-
const container = new Container();
|
|
261
|
-
const input = new Input();
|
|
262
|
-
input.onSubmit = (value) => done(value);
|
|
263
|
-
input.onEscape = () => done(undefined);
|
|
264
|
-
|
|
265
|
-
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
266
|
-
container.addChild(new Spacer(1));
|
|
267
|
-
container.addChild(new Text(theme.fg("accent", theme.bold(title)), 1, 0));
|
|
268
|
-
container.addChild(new Text(hint, 1, 0));
|
|
269
|
-
container.addChild(new Spacer(1));
|
|
270
|
-
container.addChild(input);
|
|
271
|
-
container.addChild(new Spacer(1));
|
|
272
|
-
container.addChild(new Text(theme.fg("dim", "Enter = keep • 'none' = clear • Esc = cancel setup"), 1, 0));
|
|
273
|
-
container.addChild(new Spacer(1));
|
|
274
|
-
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
275
|
-
|
|
276
|
-
return new FocusableContainer(container, input, tui);
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
function scalarOptions(currentLabel: string | undefined, values: string[]): string[] {
|
|
281
|
-
if (currentLabel === undefined) return ["Skip (use API default)", ...values];
|
|
282
|
-
return [`Keep current (${currentLabel})`, "Reset to API default", ...values];
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function isKeepChoice(choice: string): boolean {
|
|
286
|
-
return choice.startsWith("Keep current");
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function isResetChoice(choice: string): boolean {
|
|
290
|
-
return choice.startsWith("Reset") || choice.startsWith("Skip");
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function formatCurrentList(values: string[] | undefined): string {
|
|
294
|
-
return values === undefined || values.length === 0 ? "" : ` (current: ${values.join(", ")})`;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
function splitList(value: string): string[] {
|
|
298
|
-
return value.split(",").map((entry) => entry.trim().toLowerCase()).filter((entry) => entry.length > 0);
|
|
299
|
-
}
|
|
300
|
-
class FocusableContainer implements Focusable {
|
|
301
|
-
private _focused = false;
|
|
302
|
-
|
|
303
|
-
constructor(
|
|
304
|
-
private readonly container: Container,
|
|
305
|
-
private readonly input: Input,
|
|
306
|
-
private readonly tui: TUI,
|
|
307
|
-
) {}
|
|
308
|
-
|
|
309
|
-
get focused(): boolean {
|
|
310
|
-
return this._focused;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
set focused(value: boolean) {
|
|
314
|
-
this._focused = value;
|
|
315
|
-
this.input.focused = value;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
render(width: number): string[] {
|
|
319
|
-
return this.container.render(width);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
handleInput(data: string): void {
|
|
323
|
-
this.input.handleInput(data);
|
|
324
|
-
this.tui.requestRender();
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
invalidate(): void {
|
|
328
|
-
this.container.invalidate();
|
|
329
|
-
}
|
|
330
|
-
}
|
|
1
|
+
import { DynamicBorder, type ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
Container,
|
|
4
|
+
Input,
|
|
5
|
+
Spacer,
|
|
6
|
+
Text,
|
|
7
|
+
type Focusable,
|
|
8
|
+
type TUI,
|
|
9
|
+
} from "@earendil-works/pi-tui";
|
|
10
|
+
import {
|
|
11
|
+
COUNTRY_VALUES,
|
|
12
|
+
LANGUAGE_VALUES,
|
|
13
|
+
type QueritConfig,
|
|
14
|
+
type QueritCountry,
|
|
15
|
+
type QueritLanguage,
|
|
16
|
+
type QueritSearchDefaults,
|
|
17
|
+
} from "./config.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Masks the API key during rendering by temporarily swapping the Input value
|
|
21
|
+
* with asterisks. Safe because pi-tui's Input.setValue is a pure field
|
|
22
|
+
* assignment (no callbacks) and Input.render is a pure computation.
|
|
23
|
+
*/
|
|
24
|
+
class MaskedInput extends Input {
|
|
25
|
+
override render(width: number): string[] {
|
|
26
|
+
const secret = this.getValue();
|
|
27
|
+
this.setValue("*".repeat(secret.length));
|
|
28
|
+
try {
|
|
29
|
+
return super.render(width);
|
|
30
|
+
} finally {
|
|
31
|
+
this.setValue(secret);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function promptForApiKey(ctx: ExtensionCommandContext): Promise<string | undefined> {
|
|
37
|
+
if (ctx.mode !== "tui") {
|
|
38
|
+
ctx.ui.notify("/querit-setup requires Pi's interactive TUI.", "error");
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return ctx.ui.custom<string | undefined>((tui, theme, _keybindings, done) => {
|
|
43
|
+
const container = new Container();
|
|
44
|
+
const input = new MaskedInput();
|
|
45
|
+
input.onSubmit = (value) => done(value.trim() || undefined);
|
|
46
|
+
input.onEscape = () => done(undefined);
|
|
47
|
+
|
|
48
|
+
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
49
|
+
container.addChild(new Spacer(1));
|
|
50
|
+
container.addChild(new Text(theme.fg("accent", theme.bold("Configure Querit")), 1, 0));
|
|
51
|
+
container.addChild(new Text("Enter your Querit API key. Input is masked and is not added to chat history.", 1, 0));
|
|
52
|
+
container.addChild(new Spacer(1));
|
|
53
|
+
container.addChild(input);
|
|
54
|
+
container.addChild(new Spacer(1));
|
|
55
|
+
container.addChild(new Text(theme.fg("dim", "Enter to validate and continue • Esc to cancel"), 1, 0));
|
|
56
|
+
container.addChild(new Spacer(1));
|
|
57
|
+
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
58
|
+
|
|
59
|
+
return new FocusableContainer(container, input, tui);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type SetupMode = "replace-key" | "search-defaults";
|
|
64
|
+
|
|
65
|
+
export function maskApiKeyHint(apiKey: string): string {
|
|
66
|
+
const trimmed = apiKey.trim();
|
|
67
|
+
return trimmed.length <= 4 ? "****" : `…${trimmed.slice(-4)}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function promptForSetupMode(
|
|
71
|
+
ctx: ExtensionCommandContext,
|
|
72
|
+
config: QueritConfig,
|
|
73
|
+
): Promise<SetupMode | undefined> {
|
|
74
|
+
if (ctx.mode !== "tui") {
|
|
75
|
+
ctx.ui.notify("/querit-setup requires Pi's interactive TUI.", "error");
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const choice = await ctx.ui.select(
|
|
80
|
+
`Querit is already configured (API key ${maskApiKeyHint(config.apiKey)}). What would you like to change?`,
|
|
81
|
+
[
|
|
82
|
+
"Replace API key (full re-setup)",
|
|
83
|
+
"Change search defaults",
|
|
84
|
+
],
|
|
85
|
+
);
|
|
86
|
+
if (!choice) return undefined;
|
|
87
|
+
return choice.startsWith("Replace") ? "replace-key" : "search-defaults";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function promptForSearchDefaults(
|
|
91
|
+
ctx: ExtensionCommandContext,
|
|
92
|
+
current: QueritSearchDefaults = {},
|
|
93
|
+
): Promise<QueritSearchDefaults | undefined> {
|
|
94
|
+
if (ctx.mode !== "tui") return {};
|
|
95
|
+
|
|
96
|
+
const next: QueritSearchDefaults = { ...current };
|
|
97
|
+
|
|
98
|
+
const countChoice = await ctx.ui.select(
|
|
99
|
+
"Default result count per search",
|
|
100
|
+
scalarOptions(current.count === undefined ? undefined : String(current.count), ["3", "5", "10", "20"]),
|
|
101
|
+
);
|
|
102
|
+
if (countChoice === undefined) return undefined;
|
|
103
|
+
if (isResetChoice(countChoice)) delete next.count;
|
|
104
|
+
else if (!isKeepChoice(countChoice)) {
|
|
105
|
+
const parsed = Number.parseInt(countChoice, 10);
|
|
106
|
+
if (Number.isInteger(parsed) && parsed >= 1 && parsed <= 20) next.count = parsed;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const timeChoice = await ctx.ui.select(
|
|
110
|
+
"Default time range filter",
|
|
111
|
+
scalarOptions(current.timeRange, [
|
|
112
|
+
"d7 (past 7 days)",
|
|
113
|
+
"w2 (past 2 weeks)",
|
|
114
|
+
"m3 (past 3 months)",
|
|
115
|
+
"y1 (past year)",
|
|
116
|
+
]),
|
|
117
|
+
);
|
|
118
|
+
if (timeChoice === undefined) return undefined;
|
|
119
|
+
if (isResetChoice(timeChoice)) delete next.timeRange;
|
|
120
|
+
else if (!isKeepChoice(timeChoice)) next.timeRange = timeChoice.split(" ", 1)[0];
|
|
121
|
+
|
|
122
|
+
const contentChoice = await ctx.ui.select(
|
|
123
|
+
"Include sentence-level content excerpts by default?",
|
|
124
|
+
scalarOptions(
|
|
125
|
+
current.includeContent === undefined ? undefined : current.includeContent ? "yes" : "no",
|
|
126
|
+
["Yes, include excerpts", "No, snippets only"],
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
if (contentChoice === undefined) return undefined;
|
|
130
|
+
if (isResetChoice(contentChoice)) delete next.includeContent;
|
|
131
|
+
else if (!isKeepChoice(contentChoice)) next.includeContent = contentChoice.startsWith("Yes");
|
|
132
|
+
|
|
133
|
+
const countries = await promptForEnumList(ctx, {
|
|
134
|
+
title: `Default countries filter${formatCurrentList(current.countries)}`,
|
|
135
|
+
hint: `Comma-separated country names. Valid values: ${COUNTRY_VALUES.join(", ")}.`,
|
|
136
|
+
allowed: COUNTRY_VALUES,
|
|
137
|
+
});
|
|
138
|
+
if (countries === undefined) return undefined;
|
|
139
|
+
if (countries.cleared) delete next.countries;
|
|
140
|
+
else if (countries.values !== undefined) next.countries = countries.values as QueritCountry[];
|
|
141
|
+
|
|
142
|
+
const languages = await promptForEnumList(ctx, {
|
|
143
|
+
title: `Default languages filter${formatCurrentList(current.languages)}`,
|
|
144
|
+
hint: `Comma-separated language names. Valid values: ${LANGUAGE_VALUES.join(", ")}.`,
|
|
145
|
+
allowed: LANGUAGE_VALUES,
|
|
146
|
+
});
|
|
147
|
+
if (languages === undefined) return undefined;
|
|
148
|
+
if (languages.cleared) delete next.languages;
|
|
149
|
+
else if (languages.values !== undefined) next.languages = languages.values as QueritLanguage[];
|
|
150
|
+
|
|
151
|
+
const includeDomains = await promptForDomainChoice(ctx, {
|
|
152
|
+
message: "Restrict results to specific domains? (include whitelist)",
|
|
153
|
+
current: current.includeDomains,
|
|
154
|
+
customTitle: "Include only these domains (whitelist)",
|
|
155
|
+
customHint: "Only these domains will return results. Example: github.com, stackoverflow.com, developer.mozilla.org. Comma-separated; 'none' clears.",
|
|
156
|
+
});
|
|
157
|
+
if (includeDomains === undefined) return undefined;
|
|
158
|
+
if (includeDomains.cleared) delete next.includeDomains;
|
|
159
|
+
else if (includeDomains.values !== undefined) next.includeDomains = includeDomains.values;
|
|
160
|
+
const excludeDomains = await promptForDomainChoice(ctx, {
|
|
161
|
+
message: "Exclude specific domains from results? (blacklist)",
|
|
162
|
+
current: current.excludeDomains,
|
|
163
|
+
preset: EXCLUDE_DOMAIN_PRESET,
|
|
164
|
+
customTitle: "Exclude these domains (blacklist)",
|
|
165
|
+
customHint: "These domains will never return results. Comma-separated; 'none' clears.",
|
|
166
|
+
});
|
|
167
|
+
if (excludeDomains === undefined) return undefined;
|
|
168
|
+
if (excludeDomains.cleared) delete next.excludeDomains;
|
|
169
|
+
else if (excludeDomains.values !== undefined) next.excludeDomains = excludeDomains.values;
|
|
170
|
+
|
|
171
|
+
return next;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const EXCLUDE_DOMAIN_PRESET = {
|
|
175
|
+
label: "Noise blockers (pinterest.com, facebook.com, instagram.com, tiktok.com)",
|
|
176
|
+
values: ["pinterest.com", "facebook.com", "instagram.com", "tiktok.com"],
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
interface ListPromptResult {
|
|
180
|
+
cleared: boolean;
|
|
181
|
+
values?: string[];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function promptForEnumList(
|
|
185
|
+
ctx: ExtensionCommandContext,
|
|
186
|
+
options: { title: string; hint: string; allowed: readonly string[] },
|
|
187
|
+
): Promise<ListPromptResult | undefined> {
|
|
188
|
+
for (;;) {
|
|
189
|
+
const raw = await promptForPlainText(ctx, options.title, options.hint);
|
|
190
|
+
if (raw === undefined) return undefined;
|
|
191
|
+
const trimmed = raw.trim();
|
|
192
|
+
if (trimmed === "") return { cleared: false };
|
|
193
|
+
if (trimmed.toLowerCase() === "none") return { cleared: true };
|
|
194
|
+
const values = splitList(trimmed);
|
|
195
|
+
const invalid = values.filter((value) => !options.allowed.includes(value));
|
|
196
|
+
if (invalid.length > 0) {
|
|
197
|
+
ctx.ui.notify(`Unknown values: ${invalid.join(", ")}. ${options.hint}`, "error");
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
return { cleared: false, values: [...new Set(values)] };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function promptForDomainList(
|
|
205
|
+
ctx: ExtensionCommandContext,
|
|
206
|
+
options: { title: string; hint: string },
|
|
207
|
+
): Promise<ListPromptResult | undefined> {
|
|
208
|
+
for (;;) {
|
|
209
|
+
const raw = await promptForPlainText(ctx, options.title, options.hint);
|
|
210
|
+
if (raw === undefined) return undefined;
|
|
211
|
+
const trimmed = raw.trim();
|
|
212
|
+
if (trimmed === "") return { cleared: false };
|
|
213
|
+
if (trimmed.toLowerCase() === "none") return { cleared: true };
|
|
214
|
+
const values = splitList(trimmed);
|
|
215
|
+
const invalid = values.filter((value) => value.length > 253 || /\s/u.test(value) || !value.includes("."));
|
|
216
|
+
if (invalid.length > 0) {
|
|
217
|
+
ctx.ui.notify(`Invalid domains: ${invalid.join(", ")}. ${options.hint}`, "error");
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
return { cleared: false, values: [...new Set(values)] };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function promptForDomainChoice(
|
|
225
|
+
ctx: ExtensionCommandContext,
|
|
226
|
+
options: {
|
|
227
|
+
message: string;
|
|
228
|
+
current: string[] | undefined;
|
|
229
|
+
preset?: { label: string; values: string[] };
|
|
230
|
+
customTitle: string;
|
|
231
|
+
customHint: string;
|
|
232
|
+
},
|
|
233
|
+
): Promise<ListPromptResult | undefined> {
|
|
234
|
+
const hasCurrent = options.current !== undefined && options.current.length > 0;
|
|
235
|
+
const selectOptions: string[] = [];
|
|
236
|
+
if (hasCurrent) {
|
|
237
|
+
selectOptions.push(`Keep current (${options.current?.join(", ")})`);
|
|
238
|
+
selectOptions.push("Reset (no domain filter)");
|
|
239
|
+
} else {
|
|
240
|
+
selectOptions.push("Skip (no domain filter)");
|
|
241
|
+
}
|
|
242
|
+
if (options.preset) selectOptions.push(options.preset.label);
|
|
243
|
+
selectOptions.push("Enter a custom list…");
|
|
244
|
+
|
|
245
|
+
const choice = await ctx.ui.select(options.message, selectOptions);
|
|
246
|
+
if (choice === undefined) return undefined;
|
|
247
|
+
if (isKeepChoice(choice)) return { cleared: false };
|
|
248
|
+
if (isResetChoice(choice)) return { cleared: true };
|
|
249
|
+
if (options.preset && choice === options.preset.label) {
|
|
250
|
+
return { cleared: false, values: [...options.preset.values] };
|
|
251
|
+
}
|
|
252
|
+
return promptForDomainList(ctx, { title: options.customTitle, hint: options.customHint });
|
|
253
|
+
}
|
|
254
|
+
async function promptForPlainText(
|
|
255
|
+
ctx: ExtensionCommandContext,
|
|
256
|
+
title: string,
|
|
257
|
+
hint: string,
|
|
258
|
+
): Promise<string | undefined> {
|
|
259
|
+
return ctx.ui.custom<string | undefined>((tui, theme, _keybindings, done) => {
|
|
260
|
+
const container = new Container();
|
|
261
|
+
const input = new Input();
|
|
262
|
+
input.onSubmit = (value) => done(value);
|
|
263
|
+
input.onEscape = () => done(undefined);
|
|
264
|
+
|
|
265
|
+
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
266
|
+
container.addChild(new Spacer(1));
|
|
267
|
+
container.addChild(new Text(theme.fg("accent", theme.bold(title)), 1, 0));
|
|
268
|
+
container.addChild(new Text(hint, 1, 0));
|
|
269
|
+
container.addChild(new Spacer(1));
|
|
270
|
+
container.addChild(input);
|
|
271
|
+
container.addChild(new Spacer(1));
|
|
272
|
+
container.addChild(new Text(theme.fg("dim", "Enter = keep • 'none' = clear • Esc = cancel setup"), 1, 0));
|
|
273
|
+
container.addChild(new Spacer(1));
|
|
274
|
+
container.addChild(new DynamicBorder((text: string) => theme.fg("accent", text)));
|
|
275
|
+
|
|
276
|
+
return new FocusableContainer(container, input, tui);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function scalarOptions(currentLabel: string | undefined, values: string[]): string[] {
|
|
281
|
+
if (currentLabel === undefined) return ["Skip (use API default)", ...values];
|
|
282
|
+
return [`Keep current (${currentLabel})`, "Reset to API default", ...values];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function isKeepChoice(choice: string): boolean {
|
|
286
|
+
return choice.startsWith("Keep current");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function isResetChoice(choice: string): boolean {
|
|
290
|
+
return choice.startsWith("Reset") || choice.startsWith("Skip");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function formatCurrentList(values: string[] | undefined): string {
|
|
294
|
+
return values === undefined || values.length === 0 ? "" : ` (current: ${values.join(", ")})`;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function splitList(value: string): string[] {
|
|
298
|
+
return value.split(",").map((entry) => entry.trim().toLowerCase()).filter((entry) => entry.length > 0);
|
|
299
|
+
}
|
|
300
|
+
class FocusableContainer implements Focusable {
|
|
301
|
+
private _focused = false;
|
|
302
|
+
|
|
303
|
+
constructor(
|
|
304
|
+
private readonly container: Container,
|
|
305
|
+
private readonly input: Input,
|
|
306
|
+
private readonly tui: TUI,
|
|
307
|
+
) {}
|
|
308
|
+
|
|
309
|
+
get focused(): boolean {
|
|
310
|
+
return this._focused;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
set focused(value: boolean) {
|
|
314
|
+
this._focused = value;
|
|
315
|
+
this.input.focused = value;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
render(width: number): string[] {
|
|
319
|
+
return this.container.render(width);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
handleInput(data: string): void {
|
|
323
|
+
this.input.handleInput(data);
|
|
324
|
+
this.tui.requestRender();
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
invalidate(): void {
|
|
328
|
+
this.container.invalidate();
|
|
329
|
+
}
|
|
330
|
+
}
|