@mrclrchtr/supi-web 0.1.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.
@@ -0,0 +1,226 @@
1
+ // Generic settings overlay for SuPi extensions.
2
+ //
3
+ // Uses pi-tui's SettingsList with scope toggle (Tab), extension grouping,
4
+ // and search. Each extension declares its settings via registerSettings().
5
+
6
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
7
+ import { getSettingsListTheme } from "@earendil-works/pi-coding-agent";
8
+ import {
9
+ Container,
10
+ Input,
11
+ Key,
12
+ matchesKey,
13
+ type SettingItem,
14
+ SettingsList,
15
+ Text,
16
+ } from "@earendil-works/pi-tui";
17
+ import {
18
+ getRegisteredSettings,
19
+ type SettingsScope,
20
+ type SettingsSection,
21
+ } from "./settings-registry.ts";
22
+
23
+ // ── Input submenu component ──────────────────────────────────
24
+
25
+ /**
26
+ * Creates a pi-tui Input-backed submenu component with enter-to-confirm
27
+ * and escape-to-cancel handling.
28
+ *
29
+ * @param currentValue - Initial value for the text input.
30
+ * @param label - Label text displayed above the input.
31
+ * @param done - Callback invoked with the confirmed value, or undefined on cancel.
32
+ */
33
+ export function createInputSubmenu(
34
+ currentValue: string,
35
+ label: string,
36
+ done: (selectedValue?: string) => void,
37
+ ): {
38
+ render: (width: number) => string[];
39
+ invalidate: () => void;
40
+ handleInput: (data: string) => boolean;
41
+ } {
42
+ const input = new Input();
43
+ input.setValue(currentValue);
44
+
45
+ return {
46
+ render: (_width: number) => {
47
+ const lines = [` ${label}`];
48
+ lines.push(...input.render(_width));
49
+ lines.push(" enter confirm • esc cancel");
50
+ return lines;
51
+ },
52
+ invalidate: () => {
53
+ input.invalidate();
54
+ },
55
+ handleInput: (data: string) => {
56
+ if (matchesKey(data, Key.escape)) {
57
+ done();
58
+ return true;
59
+ }
60
+ if (matchesKey(data, Key.enter)) {
61
+ done(input.getValue());
62
+ return true;
63
+ }
64
+ input.handleInput(data);
65
+ return true;
66
+ },
67
+ };
68
+ }
69
+
70
+ // ── Types ────────────────────────────────────────────────────
71
+
72
+ interface OverlayState {
73
+ scope: SettingsScope;
74
+ cwd: string;
75
+ }
76
+
77
+ // ── Pure helpers ─────────────────────────────────────────────
78
+
79
+ function getScopeLabel(scope: SettingsScope): string {
80
+ return scope === "project" ? "Project" : "Global";
81
+ }
82
+
83
+ function buildFlatItems(
84
+ sections: SettingsSection[],
85
+ scope: SettingsScope,
86
+ cwd: string,
87
+ ): SettingItem[] {
88
+ const items: SettingItem[] = [];
89
+ for (const section of sections) {
90
+ const sectionItems = section.loadValues(scope, cwd);
91
+ for (const item of sectionItems) {
92
+ items.push({
93
+ ...item,
94
+ id: `${section.id}.${item.id}`,
95
+ label: `${section.label}: ${item.label}`,
96
+ });
97
+ }
98
+ }
99
+ return items;
100
+ }
101
+
102
+ function findSectionAndId(
103
+ sections: SettingsSection[],
104
+ flatId: string,
105
+ ): { section: SettingsSection; itemId: string } | null {
106
+ const dotIndex = flatId.indexOf(".");
107
+ if (dotIndex === -1) return null;
108
+ const sectionId = flatId.slice(0, dotIndex);
109
+ const itemId = flatId.slice(dotIndex + 1);
110
+ const section = sections.find((s) => s.id === sectionId);
111
+ if (!section) return null;
112
+ return { section, itemId };
113
+ }
114
+
115
+ // ── Component ────────────────────────────────────────────────
116
+
117
+ interface SettingsOverlayDeps {
118
+ state: OverlayState;
119
+ container: Container;
120
+ settingsList: SettingsList | null;
121
+ tui: Parameters<Parameters<ExtensionContext["ui"]["custom"]>[0]>[0];
122
+ theme: Parameters<Parameters<ExtensionContext["ui"]["custom"]>[0]>[1];
123
+ done: () => void;
124
+ }
125
+
126
+ function createSettingsList(deps: SettingsOverlayDeps): SettingsList {
127
+ const sections = getRegisteredSettings();
128
+ const items = buildFlatItems(sections, deps.state.scope, deps.state.cwd);
129
+ const onChange = (flatId: string, newValue: string) => {
130
+ const found = findSectionAndId(sections, flatId);
131
+ if (found) {
132
+ found.section.persistChange(deps.state.scope, deps.state.cwd, found.itemId, newValue);
133
+ }
134
+ // Re-read all values to reflect persisted changes, but keep the list
135
+ // instance (and its selectedIndex) intact.
136
+ const updatedItems = buildFlatItems(sections, deps.state.scope, deps.state.cwd);
137
+ for (const updated of updatedItems) {
138
+ const existing = items.find((i) => i.id === updated.id);
139
+ if (existing && existing.currentValue !== updated.currentValue) {
140
+ settingsList.updateValue(updated.id, updated.currentValue);
141
+ }
142
+ }
143
+ deps.tui.requestRender();
144
+ };
145
+ const settingsList = new SettingsList(
146
+ items,
147
+ Math.min(items.length + 4, 20),
148
+ getSettingsListTheme(),
149
+ onChange,
150
+ () => deps.done(),
151
+ { enableSearch: true },
152
+ );
153
+ return settingsList;
154
+ }
155
+
156
+ function rebuildSettingsList(deps: SettingsOverlayDeps): SettingsList {
157
+ const settingsList = createSettingsList(deps);
158
+ deps.settingsList = settingsList;
159
+
160
+ deps.container.clear();
161
+ deps.container.addChild(createHeaderComponent(deps));
162
+ deps.container.addChild(settingsList);
163
+
164
+ return settingsList;
165
+ }
166
+
167
+ function createHeaderComponent(deps: SettingsOverlayDeps): Text {
168
+ const { theme, state } = deps;
169
+ const scopeLabel = getScopeLabel(state.scope);
170
+ const otherScope = state.scope === "project" ? "Global" : "Project";
171
+ const headerText = new Text(
172
+ `${theme.fg("accent", theme.bold("SuPi Settings"))} ${theme.fg("text", `Scope: ${scopeLabel}`)} ${theme.fg("dim", `(tab → ${otherScope})`)}`,
173
+ 0,
174
+ 0,
175
+ );
176
+ return headerText;
177
+ }
178
+
179
+ function handleScopeToggle(deps: SettingsOverlayDeps): void {
180
+ deps.state.scope = deps.state.scope === "project" ? "global" : "project";
181
+ rebuildSettingsList(deps);
182
+ deps.tui.requestRender();
183
+ }
184
+
185
+ // ── Entry point ──────────────────────────────────────────────
186
+
187
+ export function openSettingsOverlay(ctx: ExtensionContext): void {
188
+ const sections = getRegisteredSettings();
189
+ if (sections.length === 0) {
190
+ ctx.ui.notify("No settings registered by SuPi extensions", "info");
191
+ return;
192
+ }
193
+
194
+ void ctx.ui.custom<void>((tui, theme, _kb, done) => {
195
+ const state: OverlayState = { scope: "project", cwd: ctx.cwd };
196
+ const container = new Container();
197
+
198
+ const deps: SettingsOverlayDeps = {
199
+ state,
200
+ container,
201
+ settingsList: null,
202
+ tui,
203
+ theme,
204
+ done,
205
+ };
206
+
207
+ rebuildSettingsList(deps);
208
+
209
+ const component = {
210
+ render: (width: number) => container.render(width),
211
+ invalidate: () => container.invalidate(),
212
+ handleInput: (data: string) => {
213
+ if (matchesKey(data, Key.tab)) {
214
+ handleScopeToggle(deps);
215
+ return true;
216
+ }
217
+ // Delegate input to the settings list (always set after rebuildSettingsList)
218
+ deps.settingsList?.handleInput?.(data);
219
+ deps.tui.requestRender();
220
+ return true;
221
+ },
222
+ };
223
+
224
+ return component;
225
+ });
226
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Shared terminal title formatting and signaling utilities.
3
+ *
4
+ * Centralized place for pi title convention (π prefix), completion (✓)
5
+ * and waiting (●) indicators, and the audible terminal bell.
6
+ */
7
+ import path from "node:path";
8
+
9
+ /** Unicode checkmark shown when the agent finishes a turn. */
10
+ export const DONE_SYMBOL = "\u2713";
11
+ /** Unicode dot shown when waiting for user input. */
12
+ export const WAITING_SYMBOL = "\u25CF";
13
+
14
+ /** Minimal UI surface needed for title operations. */
15
+ export interface TitleTarget {
16
+ ui: {
17
+ setTitle?(title: string): void;
18
+ };
19
+ }
20
+
21
+ /**
22
+ * Format pi's canonical terminal title from session name and cwd.
23
+ * Falls back gracefully when either is missing.
24
+ *
25
+ * @example
26
+ * formatTitle("my-session", "/home/projects/foo") // "π - my-session - foo"
27
+ * formatTitle(undefined, "/home/projects/foo") // "π - foo"
28
+ * formatTitle("my-session") // "π - my-session"
29
+ * formatTitle() // "π"
30
+ */
31
+ export function formatTitle(sessionName?: string, cwd?: string): string {
32
+ const base = cwd ? path.basename(cwd) : undefined;
33
+ if (sessionName && base) return `π - ${sessionName} - ${base}`;
34
+ if (sessionName) return `π - ${sessionName}`;
35
+ if (base) return `π - ${base}`;
36
+ return "π";
37
+ }
38
+
39
+ /** Sound the audible terminal bell (ASCII BEL). */
40
+ export function signalBell(): void {
41
+ process.stdout.write("\x07");
42
+ }
43
+
44
+ /**
45
+ * Set the terminal title to indicate the agent is waiting for user input.
46
+ * Prefixes with ● and sounds the terminal bell.
47
+ */
48
+ export function signalWaiting(ctx: TitleTarget, title: string): void {
49
+ ctx.ui.setTitle?.(`${WAITING_SYMBOL} ${title}`);
50
+ signalBell();
51
+ }
52
+
53
+ /**
54
+ * Set the terminal title to indicate the agent turn has completed.
55
+ * Prefixes with ✓ and sounds the terminal bell.
56
+ */
57
+ export function signalDone(ctx: TitleTarget, title: string): void {
58
+ ctx.ui.setTitle?.(`${DONE_SYMBOL} ${title}`);
59
+ signalBell();
60
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@mrclrchtr/supi-web",
3
+ "version": "0.1.0",
4
+ "description": "SuPi Web extension — fetch web pages as clean Markdown (web_fetch_md) and library docs via Context7 (web_docs_search, web_docs_fetch)",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/mrclrchtr/supi.git"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "keywords": [
14
+ "pi-package",
15
+ "pi",
16
+ "pi-coding-agent"
17
+ ],
18
+ "files": [
19
+ "src/**/*.ts",
20
+ "README.md"
21
+ ],
22
+ "dependencies": {
23
+ "@mrclrchtr/supi-core": "workspace:*",
24
+ "@upstash/context7-sdk": "^0.3.0",
25
+ "jsdom": "^26.1.0",
26
+ "@mozilla/readability": "^0.6.0",
27
+ "turndown": "^7.2.0",
28
+ "turndown-plugin-gfm": "^1.0.2"
29
+ },
30
+ "bundledDependencies": [
31
+ "@mrclrchtr/supi-core"
32
+ ],
33
+ "peerDependencies": {
34
+ "@earendil-works/pi-coding-agent": "*",
35
+ "typebox": "*"
36
+ },
37
+ "devDependencies": {
38
+ "vitest": "^4.1.5",
39
+ "@types/jsdom": "^21.1.7",
40
+ "@types/turndown": "^5.0.6",
41
+ "@mrclrchtr/supi-test-utils": "workspace:*"
42
+ },
43
+ "pi": {
44
+ "extensions": [
45
+ "./src/web.ts",
46
+ "./src/docs.ts"
47
+ ]
48
+ },
49
+ "main": "src/index.ts"
50
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Context7 API client — thin wrapper around @upstash/context7-sdk.
3
+ *
4
+ * API key is read automatically from CONTEXT7_API_KEY env var by the SDK.
5
+ */
6
+
7
+ import { Context7, Context7Error } from "@upstash/context7-sdk";
8
+
9
+ export { Context7Error };
10
+
11
+ export interface SearchResult {
12
+ id: string;
13
+ name: string;
14
+ description: string;
15
+ totalSnippets: number;
16
+ trustScore: number;
17
+ benchmarkScore: number;
18
+ versions?: string[];
19
+ }
20
+
21
+ export interface DocSnippet {
22
+ title: string;
23
+ content: string;
24
+ source: string;
25
+ }
26
+
27
+ const client = new Context7();
28
+
29
+ export async function searchLibrary(query: string, libraryName: string): Promise<SearchResult[]> {
30
+ const results = await client.searchLibrary(query, libraryName);
31
+ return results as unknown as SearchResult[];
32
+ }
33
+
34
+ export async function getContext(
35
+ query: string,
36
+ libraryId: string,
37
+ raw?: boolean,
38
+ ): Promise<string | DocSnippet[]> {
39
+ const options = raw ? ({ type: "json" } as const) : ({ type: "txt" } as const);
40
+ return client.getContext(query, libraryId, options) as Promise<string | DocSnippet[]>;
41
+ }
package/src/convert.ts ADDED
@@ -0,0 +1,219 @@
1
+ /**
2
+ * HTML → Markdown conversion via JSDOM + Readability + Turndown.
3
+ */
4
+
5
+ import { Readability } from "@mozilla/readability";
6
+ import { JSDOM, VirtualConsole } from "jsdom";
7
+ import Turndown from "turndown";
8
+ import { guessLanguage } from "./fetch.ts";
9
+
10
+ /**
11
+ * Convert HTML to clean Markdown.
12
+ *
13
+ * @param html - Raw HTML string.
14
+ * @param baseUrl - Base URL for resolving relative links.
15
+ * @param options.absLinks - Whether to absolutize links and image sources.
16
+ * @returns Clean Markdown string.
17
+ */
18
+ export async function htmlToMarkdown(
19
+ html: string,
20
+ baseUrl: string,
21
+ options: { absLinks?: boolean } = {},
22
+ ): Promise<string> {
23
+ const absLinks = options.absLinks ?? true;
24
+
25
+ if (!isHtml(html)) {
26
+ // Not HTML — wrap as fenced code block
27
+ return wrapAsCodeBlock(html, baseUrl);
28
+ }
29
+
30
+ const doc = createDocument(html, baseUrl);
31
+
32
+ // Remove script/style/noscript
33
+ for (const tag of ["script", "style", "noscript"]) {
34
+ for (const el of doc.querySelectorAll(tag)) {
35
+ el.remove();
36
+ }
37
+ }
38
+
39
+ // Extract article content with Readability
40
+ const readability = new Readability(doc);
41
+ const article = readability.parse();
42
+
43
+ const title = article?.title?.trim() || doc.title?.trim() || "";
44
+
45
+ // Use Readability content, or fall back to body
46
+ const contentHtml = article?.content || doc.body?.innerHTML || html;
47
+
48
+ // Re-parse content so we can manipulate it cleanly
49
+ const contentDoc = createDocument(`<html><body>${contentHtml}</body></html>`, baseUrl);
50
+ const body = contentDoc.body;
51
+
52
+ if (absLinks) {
53
+ absolutizeLinks(body, baseUrl);
54
+ }
55
+
56
+ const turndown = await createTurndown();
57
+ let markdown = turndown.turndown(body);
58
+ markdown = cleanMarkdown(markdown);
59
+
60
+ // Prepend title if not already present
61
+ if (title && !markdown.trimStart().startsWith("# ")) {
62
+ markdown = `# ${title}\n\n${markdown}`;
63
+ }
64
+
65
+ return normalizeWhitespace(markdown);
66
+ }
67
+
68
+ /**
69
+ * Wrap plain text in a fenced code block.
70
+ */
71
+ export function wrapAsCodeBlock(text: string, url: string): string {
72
+ const lang = guessLanguage(url);
73
+ const normalized = String(text || "")
74
+ .replace(/\r\n/g, "\n")
75
+ .replace(/\r/g, "\n");
76
+
77
+ // Find longest backtick sequence so we can choose a fence that won't conflict
78
+ const backticks = normalized.match(/`+/g) || [];
79
+ let maxTicks = 0;
80
+ for (const bt of backticks) {
81
+ maxTicks = Math.max(maxTicks, bt.length);
82
+ }
83
+ const fence = "`".repeat(Math.max(3, maxTicks + 1));
84
+ const body = normalized.endsWith("\n") ? normalized : `${normalized}\n`;
85
+ const prefix = lang ? `${fence}${lang}\n` : `${fence}\n`;
86
+ return normalizeWhitespace(`${prefix}${body}${fence}\n`);
87
+ }
88
+
89
+ function createDocument(html: string, url: string): Document {
90
+ const virtualConsole = new VirtualConsole();
91
+ return new JSDOM(html, { url, virtualConsole }).window.document;
92
+ }
93
+
94
+ function absolutizeLinks(root: Element, baseUrl: string): void {
95
+ for (const a of root.querySelectorAll("a[href]")) {
96
+ const resolved = resolveUrl(a.getAttribute("href") || "", baseUrl);
97
+ if (resolved) {
98
+ a.setAttribute("href", resolved);
99
+ } else {
100
+ a.removeAttribute("href");
101
+ }
102
+ }
103
+ for (const img of root.querySelectorAll("img[src]")) {
104
+ const resolved = resolveUrl(img.getAttribute("src") || "", baseUrl);
105
+ if (resolved) {
106
+ img.setAttribute("src", resolved);
107
+ } else {
108
+ img.removeAttribute("src");
109
+ }
110
+ }
111
+ }
112
+
113
+ function resolveUrl(href: string, baseUrl: string): string {
114
+ const trimmed = String(href || "").trim();
115
+ if (
116
+ !trimmed ||
117
+ trimmed.startsWith("#") ||
118
+ trimmed.startsWith("mailto:") ||
119
+ trimmed.startsWith("tel:")
120
+ ) {
121
+ return trimmed;
122
+ }
123
+ if (trimmed.startsWith("javascript:")) {
124
+ return "";
125
+ }
126
+ try {
127
+ const resolved = new URL(trimmed, baseUrl);
128
+ return resolved.protocol === "http:" || resolved.protocol === "https:"
129
+ ? resolved.toString()
130
+ : trimmed;
131
+ } catch {
132
+ return trimmed;
133
+ }
134
+ }
135
+
136
+ async function createTurndown(): Promise<Turndown> {
137
+ const td = new Turndown({
138
+ codeBlockStyle: "fenced",
139
+ headingStyle: "atx",
140
+ hr: "---",
141
+ bulletListMarker: "-",
142
+ emDelimiter: "_",
143
+ });
144
+
145
+ // Try loading GFM plugins
146
+ try {
147
+ // @ts-expect-error — turndown-plugin-gfm has no @types package
148
+ const gfmMod = await import("turndown-plugin-gfm");
149
+ const gfm = (gfmMod as unknown as { default?: unknown }).default ?? gfmMod;
150
+ const plugins: unknown[] = [];
151
+ if (gfm && typeof gfm === "object") {
152
+ const obj = gfm as Record<string, unknown>;
153
+ if (typeof obj.gfm === "function") plugins.push(obj.gfm);
154
+ if (typeof obj.tables === "function") plugins.push(obj.tables);
155
+ if (typeof obj.strikethrough === "function") plugins.push(obj.strikethrough);
156
+ if (typeof obj.taskListItems === "function") plugins.push(obj.taskListItems);
157
+ }
158
+ if (plugins.length > 0) {
159
+ td.use(plugins as [(turndown: Turndown) => void]);
160
+ }
161
+ } catch {
162
+ // GFM plugin optional
163
+ }
164
+
165
+ // Custom pre → fenced code rule
166
+ td.addRule("preToFenced", {
167
+ filter: ["pre"],
168
+ replacement(_content: string, node: Turndown.Node) {
169
+ const text = (node as unknown as HTMLElement).textContent ?? "";
170
+ return `\n\n\`\`\`\n${String(text).replace(/\n+$/g, "")}\n\`\`\`\n\n`;
171
+ },
172
+ });
173
+
174
+ return td;
175
+ }
176
+
177
+ function cleanMarkdown(text: string): string {
178
+ const lines = text.replace(/\r\n/g, "\n").split("\n");
179
+ const out: string[] = [];
180
+ let inCodeBlock = false;
181
+
182
+ for (const raw of lines) {
183
+ const trimmed = raw.trim();
184
+ if (trimmed.startsWith("```")) {
185
+ inCodeBlock = !inCodeBlock;
186
+ out.push(raw);
187
+ continue;
188
+ }
189
+ if (
190
+ !inCodeBlock &&
191
+ (/^(copy|copy page|copied!?|copy to clipboard)$/i.test(trimmed) ||
192
+ /^loading\.{3}$/i.test(trimmed))
193
+ ) {
194
+ continue;
195
+ }
196
+ out.push(raw);
197
+ }
198
+
199
+ return out.join("\n");
200
+ }
201
+
202
+ function normalizeWhitespace(text: string): string {
203
+ return `${String(text || "")
204
+ .replace(/\r\n/g, "\n")
205
+ .replace(/[ \t]+$/gm, "")
206
+ .replace(/\n{3,}/g, "\n\n")
207
+ .trim()}\n`;
208
+ }
209
+
210
+ function isHtml(text: string): boolean {
211
+ const trimmed = (text || "").trimStart().slice(0, 2000).toLowerCase();
212
+ return !!(
213
+ trimmed.startsWith("<!doctype html") ||
214
+ trimmed.startsWith("<html") ||
215
+ trimmed.startsWith("<?xml") ||
216
+ /<(head|body)\b/.test(trimmed) ||
217
+ (trimmed.startsWith("<") && /<\/(html|head|body)>/.test(trimmed))
218
+ );
219
+ }