@rol1n/pi-kit 0.0.0-stage → 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.
- package/README.md +98 -2
- package/extensions/ask-u-questions/README.md +32 -0
- package/extensions/ask-u-questions/answers.ts +53 -0
- package/extensions/ask-u-questions/dialog-ui.ts +57 -0
- package/extensions/ask-u-questions/index.ts +88 -0
- package/extensions/ask-u-questions/multi-choice.ts +260 -0
- package/extensions/ask-u-questions/results.ts +83 -0
- package/extensions/ask-u-questions/schema.ts +17 -0
- package/extensions/ask-u-questions/single-choice.ts +182 -0
- package/extensions/ask-u-questions/tool-renderers.ts +69 -0
- package/extensions/ask-u-questions/types.ts +45 -0
- package/extensions/ask-u-questions/ui-lock.ts +24 -0
- package/extensions/continue-signal/README.md +28 -0
- package/extensions/continue-signal/index.ts +51 -0
- package/extensions/continue-signal/renderer.ts +21 -0
- package/extensions/custom-providers/README.md +28 -0
- package/extensions/custom-providers/asr/handlers/dashscope.ts +154 -0
- package/extensions/custom-providers/asr/index.ts +37 -0
- package/extensions/custom-providers/asr/models.ts +18 -0
- package/extensions/custom-providers/asr/types.ts +20 -0
- package/extensions/custom-providers/chat/index.ts +33 -0
- package/extensions/custom-providers/chat/magpie.ts +258 -0
- package/extensions/custom-providers/chat/models.ts +111 -0
- package/extensions/custom-providers/classifier/index.ts +6 -0
- package/extensions/custom-providers/classifier/models.ts +1 -0
- package/extensions/custom-providers/image/handlers/dashscope.ts +240 -0
- package/extensions/custom-providers/image/handlers/openrouter.ts +185 -0
- package/extensions/custom-providers/image/index.ts +96 -0
- package/extensions/custom-providers/image/models.ts +71 -0
- package/extensions/custom-providers/image/types.ts +22 -0
- package/extensions/custom-providers/index.ts +50 -0
- package/extensions/custom-providers/types.ts +17 -0
- package/extensions/image-generate/README.md +29 -0
- package/extensions/image-generate/files.ts +49 -0
- package/extensions/image-generate/index.ts +200 -0
- package/extensions/image-generate/model-selector.ts +150 -0
- package/extensions/image-generate/models.ts +19 -0
- package/extensions/memory-log/README.md +54 -0
- package/extensions/memory-log/agent/consolidator/prompt.ts +47 -0
- package/extensions/memory-log/agent/consolidator/tools.ts +160 -0
- package/extensions/memory-log/agent/cost.ts +32 -0
- package/extensions/memory-log/agent/index.ts +42 -0
- package/extensions/memory-log/agent/observer/prompt.ts +73 -0
- package/extensions/memory-log/agent/observer/tool.ts +73 -0
- package/extensions/memory-log/commands/compact.ts +35 -0
- package/extensions/memory-log/commands/consolidate.ts +47 -0
- package/extensions/memory-log/commands/status.ts +50 -0
- package/extensions/memory-log/config.ts +253 -0
- package/extensions/memory-log/debug-log.ts +76 -0
- package/extensions/memory-log/hooks/compaction-hook.ts +161 -0
- package/extensions/memory-log/hooks/compaction-trigger.ts +160 -0
- package/extensions/memory-log/hooks/consolidator-trigger.ts +143 -0
- package/extensions/memory-log/hooks/observer-trigger.ts +176 -0
- package/extensions/memory-log/ids.ts +78 -0
- package/extensions/memory-log/index.ts +149 -0
- package/extensions/memory-log/ledger/fold.ts +77 -0
- package/extensions/memory-log/ledger/index.ts +7 -0
- package/extensions/memory-log/ledger/pool.ts +38 -0
- package/extensions/memory-log/ledger/progress.ts +181 -0
- package/extensions/memory-log/ledger/projection.ts +115 -0
- package/extensions/memory-log/ledger/render.ts +38 -0
- package/extensions/memory-log/ledger/serialize.ts +161 -0
- package/extensions/memory-log/ledger/types.ts +201 -0
- package/extensions/memory-log/memory/index-render.ts +45 -0
- package/extensions/memory-log/memory/paths.ts +120 -0
- package/extensions/memory-log/memory/session.ts +64 -0
- package/extensions/memory-log/runtime.ts +127 -0
- package/extensions/memory-log/spawn/launch.ts +133 -0
- package/extensions/memory-log/spawn/runs.ts +83 -0
- package/extensions/memory-log/tokens.ts +66 -0
- package/extensions/memory-log/ui/status-controller.ts +225 -0
- package/extensions/memory-log/ui/timeline.ts +103 -0
- package/extensions/model-vision/README.md +28 -0
- package/extensions/multi-harness/README.md +63 -0
- package/extensions/multi-harness/adapters.mjs +72 -0
- package/extensions/multi-harness/harness-done.mjs +14 -0
- package/extensions/multi-harness/index.ts +375 -0
- package/extensions/multi-harness/ipc.mjs +37 -0
- package/extensions/multi-harness/opencode-plugin.mjs +99 -0
- package/extensions/multi-harness/process-runner.mjs +115 -0
- package/extensions/multi-harness/turn-hook.mjs +29 -0
- package/extensions/multi-harness/worker-bridge.ts +52 -0
- package/extensions/oai-subquota/README.md +30 -0
- package/extensions/oai-subquota/api.ts +27 -0
- package/extensions/oai-subquota/auth.ts +34 -0
- package/extensions/oai-subquota/cache.ts +30 -0
- package/extensions/oai-subquota/index.ts +276 -0
- package/extensions/oai-subquota/snapshot.ts +167 -0
- package/extensions/oai-subquota/types.ts +40 -0
- package/extensions/oai-subquota/ui.ts +183 -0
- package/extensions/prompt-snipaste/README.md +33 -0
- package/extensions/prompt-snipaste/index.ts +143 -0
- package/extensions/prompt-snipaste/menu.ts +159 -0
- package/extensions/prompt-snipaste/renderers.ts +88 -0
- package/extensions/prompt-snipaste/snippets.ts +203 -0
- package/extensions/prompt-snipaste/types.ts +19 -0
- package/extensions/screenshot/README.md +25 -0
- package/extensions/srp-theme/README.md +24 -0
- package/extensions/srp-theme/footer.ts +194 -0
- package/extensions/srp-theme/index.ts +172 -0
- package/extensions/srp-theme/tps-meter.ts +442 -0
- package/extensions/subagent/README.md +35 -0
- package/extensions/subagent/bridge.ts +171 -0
- package/extensions/subagent/index.ts +590 -0
- package/extensions/subagent/mux.ts +621 -0
- package/extensions/subagent/session.ts +166 -0
- package/extensions/subagent/skills/subagent/SKILL.md +70 -0
- package/extensions/subagent/ui.ts +100 -0
- package/extensions/tui-asr/README.md +28 -0
- package/extensions/tui-asr/audio.ts +40 -0
- package/extensions/tui-asr/clipboard.ts +30 -0
- package/extensions/tui-asr/dictation.ts +205 -0
- package/extensions/tui-asr/index.ts +181 -0
- package/extensions/tui-asr/input.ts +58 -0
- package/extensions/tui-asr/model-selector.ts +163 -0
- package/extensions/tui-asr/providers.ts +25 -0
- package/extensions/tui-asr/status.ts +79 -0
- package/extensions/web-reach/README.md +37 -0
- package/extensions/web-reach/html.ts +83 -0
- package/extensions/web-reach/index.ts +82 -0
- package/extensions/web-reach/page.ts +132 -0
- package/extensions/web-reach/pdf.ts +42 -0
- package/extensions/web-reach/reader.ts +42 -0
- package/extensions/web-reach/rsc.ts +161 -0
- package/extensions/web-reach/search.ts +85 -0
- package/extensions/web-reach/transport.ts +35 -0
- package/extensions/windows-wheel-fix/README.md +21 -0
- package/extensions/windows-wheel-fix/index.ts +44 -0
- package/package.json +93 -4
- package/prompts/ask-questions.md +8 -0
- package/prompts/delegate-exploration.md +7 -0
- package/prompts/diagnose-report.md +7 -0
- package/prompts/frontend-tech-preset.md +18 -0
- package/prompts/orchestrator-mode.md +7 -0
- package/prompts/session-kickoff.md +7 -0
- package/prompts/verify-not-assume.md +7 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tui-asr — Terminal streaming ASR voice dictation extension for Pi.
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* 1. Streaming ASR: Real-time recognition using Aliyun DashScope Paraformer WebSocket.
|
|
6
|
+
* 2. Visual metering: Animated braille volume level waveform (● ⣀⣄ listening…).
|
|
7
|
+
* 3. Native Pi auth: Resolves keys from `/login dashscope` (auth.json) or DASHSCOPE_API_KEY.
|
|
8
|
+
* 4. Model selector: Native Pi-style searchable TUI selector dialog (/asr model).
|
|
9
|
+
* 5. Input injection: Directly inserts text into active editor or clipboard.
|
|
10
|
+
*
|
|
11
|
+
* Keybindings:
|
|
12
|
+
* - Alt+M: Start / Stop voice dictation
|
|
13
|
+
* - Alt+N: Cancel current dictation
|
|
14
|
+
*
|
|
15
|
+
* Commands:
|
|
16
|
+
* - /asr: Toggle voice dictation
|
|
17
|
+
* - /asr model [name]: Interactive searchable model selector
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { type ExtensionAPI, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
21
|
+
import type { AutocompleteItem } from "@earendil-works/pi-tui";
|
|
22
|
+
import { ALL_ASR_MODELS, resolveProviderApiKey, type AsrModelDefinition } from "./providers.ts";
|
|
23
|
+
import { createDictationController } from "./dictation.ts";
|
|
24
|
+
import { SearchableAsrModelSelectorComponent } from "./model-selector.ts";
|
|
25
|
+
|
|
26
|
+
/** Default startup ASR model ID */
|
|
27
|
+
const DEFAULT_ASR_MODEL = "paraformer-realtime-v2";
|
|
28
|
+
|
|
29
|
+
/** Default shortcuts */
|
|
30
|
+
const TOGGLE_SHORTCUTS = ["alt+m"];
|
|
31
|
+
const CANCEL_SHORTCUTS = ["alt+n"];
|
|
32
|
+
|
|
33
|
+
export default function (pi: ExtensionAPI) {
|
|
34
|
+
let activeModel = DEFAULT_ASR_MODEL;
|
|
35
|
+
|
|
36
|
+
// TUI state
|
|
37
|
+
let tuiHandle: any = null;
|
|
38
|
+
let removeInputListener: (() => void) | null = null;
|
|
39
|
+
let lastCtx: ExtensionContext | null = null;
|
|
40
|
+
|
|
41
|
+
const dictation = createDictationController(() => activeModel, () => tuiHandle);
|
|
42
|
+
const { startDictation, stopDictation, cancelDictation, toggleDictation, cleanup } = dictation;
|
|
43
|
+
|
|
44
|
+
// Key interception
|
|
45
|
+
const ensureTuiAttached = (ctx: ExtensionContext) => {
|
|
46
|
+
lastCtx = ctx;
|
|
47
|
+
if (ctx.mode === "tui" && !removeInputListener && typeof ctx.ui?.onTerminalInput === "function") {
|
|
48
|
+
removeInputListener = ctx.ui.onTerminalInput((keyData) => {
|
|
49
|
+
const matchesAny = (list: string[]) =>
|
|
50
|
+
list.some((s) => {
|
|
51
|
+
const raw = s.toLowerCase().trim();
|
|
52
|
+
if (raw === "alt+m" && (keyData === "\x1bm" || keyData === "µ")) return true;
|
|
53
|
+
if (raw === "alt+n" && (keyData === "\x1bn" || keyData === "Å")) return true;
|
|
54
|
+
return false;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (matchesAny(TOGGLE_SHORTCUTS)) {
|
|
58
|
+
if (dictation.state === "idle") startDictation(ctx);
|
|
59
|
+
else if (dictation.state === "recording") stopDictation();
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (matchesAny(CANCEL_SHORTCUTS)) {
|
|
64
|
+
if (dictation.state !== "idle") {
|
|
65
|
+
cancelDictation();
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return undefined;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
pi.on("session_start", (_event, ctx) => {
|
|
76
|
+
ensureTuiAttached(ctx);
|
|
77
|
+
if (ctx.mode === "tui") {
|
|
78
|
+
ctx.ui.setWidget("tui-asr-handle", (tui: any) => {
|
|
79
|
+
tuiHandle = tui;
|
|
80
|
+
return {
|
|
81
|
+
render: () => [],
|
|
82
|
+
invalidate: () => {},
|
|
83
|
+
dispose: () => {
|
|
84
|
+
tuiHandle = null;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Register shortcuts
|
|
92
|
+
pi.registerShortcut("alt+m", {
|
|
93
|
+
description: "Toggle voice dictation (tui-asr)",
|
|
94
|
+
handler: async (ctx) => {
|
|
95
|
+
ensureTuiAttached(ctx);
|
|
96
|
+
await toggleDictation(ctx);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
pi.registerShortcut("alt+n", {
|
|
101
|
+
description: "Cancel voice dictation (tui-asr)",
|
|
102
|
+
handler: async (ctx) => {
|
|
103
|
+
ensureTuiAttached(ctx);
|
|
104
|
+
cancelDictation();
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Register command /asr
|
|
109
|
+
pi.registerCommand("asr", {
|
|
110
|
+
description: "Voice dictation control and model switching (/asr [model])",
|
|
111
|
+
getArgumentCompletions: (prefix: string): AutocompleteItem[] | null => {
|
|
112
|
+
const trimmed = prefix.trimStart();
|
|
113
|
+
if (!trimmed || "model".startsWith(trimmed)) {
|
|
114
|
+
return [{ value: "model", label: "model", description: "Select ASR voice model in TUI" }];
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
},
|
|
118
|
+
handler: async (args, ctx) => {
|
|
119
|
+
ensureTuiAttached(ctx);
|
|
120
|
+
const parts = args.trim().split(/\s+/).filter(Boolean);
|
|
121
|
+
const sub = parts[0]?.toLowerCase();
|
|
122
|
+
|
|
123
|
+
if (sub === "model") {
|
|
124
|
+
const modelArg = parts[1]?.toLowerCase();
|
|
125
|
+
if (modelArg) {
|
|
126
|
+
const matched = ALL_ASR_MODELS.find(
|
|
127
|
+
(m) =>
|
|
128
|
+
m.id.toLowerCase() === modelArg ||
|
|
129
|
+
m.name.toLowerCase() === modelArg ||
|
|
130
|
+
m.id.toLowerCase().includes(modelArg),
|
|
131
|
+
);
|
|
132
|
+
if (matched) {
|
|
133
|
+
activeModel = matched.id;
|
|
134
|
+
const isLogged = Boolean(await resolveProviderApiKey(matched.provider, ctx));
|
|
135
|
+
const extra = isLogged ? "" : ` (No key detected: please run /login ${matched.provider})`;
|
|
136
|
+
ctx.ui.notify(`ASR model switched to: ${matched.name} (${matched.id})${extra}`, "info");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Open searchable model selector TUI
|
|
142
|
+
const loginStatusMap = new Map<string, boolean>();
|
|
143
|
+
for (const item of ALL_ASR_MODELS) {
|
|
144
|
+
if (!loginStatusMap.has(item.provider)) {
|
|
145
|
+
const key = await resolveProviderApiKey(item.provider, ctx);
|
|
146
|
+
loginStatusMap.set(item.provider, Boolean(key));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const selected = await ctx.ui.custom<AsrModelDefinition | undefined>((tui, theme, _kb, done) => {
|
|
151
|
+
return new SearchableAsrModelSelectorComponent(
|
|
152
|
+
tui,
|
|
153
|
+
theme,
|
|
154
|
+
ALL_ASR_MODELS,
|
|
155
|
+
activeModel,
|
|
156
|
+
(model) => done(model),
|
|
157
|
+
() => done(undefined),
|
|
158
|
+
(provider) => loginStatusMap.get(provider) ?? false,
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (!selected) return;
|
|
163
|
+
|
|
164
|
+
activeModel = selected.id;
|
|
165
|
+
const isLogged = loginStatusMap.get(selected.provider);
|
|
166
|
+
const extra = isLogged ? "" : ` (No key detected: please run /login ${selected.provider})`;
|
|
167
|
+
ctx.ui.notify(`ASR model switched to: ${selected.name} (${selected.id})${extra}`, "info");
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Default action: toggle dictation
|
|
172
|
+
await toggleDictation(ctx);
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
pi.on("session_shutdown", () => {
|
|
177
|
+
if (dictation.state !== "idle") cleanup();
|
|
178
|
+
removeInputListener?.();
|
|
179
|
+
removeInputListener = null;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { copyToClipboard } from "./clipboard.ts";
|
|
3
|
+
|
|
4
|
+
interface EditorLike {
|
|
5
|
+
getText(): string;
|
|
6
|
+
setText(text: string): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type Target =
|
|
10
|
+
| { kind: "editor"; editor: EditorLike }
|
|
11
|
+
| { kind: "typable"; component: { handleInput(data: string): void } };
|
|
12
|
+
|
|
13
|
+
const asEditorLike = (value: any): EditorLike | null =>
|
|
14
|
+
value && typeof value.getText === "function" && typeof value.setText === "function" ? value : null;
|
|
15
|
+
|
|
16
|
+
function resolveTarget(tuiHandle: any): Target | null {
|
|
17
|
+
const focused = tuiHandle?.focusedComponent;
|
|
18
|
+
if (!focused) return null;
|
|
19
|
+
const editor = asEditorLike(focused) ?? asEditorLike(focused.editor);
|
|
20
|
+
if (editor) return { kind: "editor", editor };
|
|
21
|
+
if (typeof focused.handleInput === "function") return { kind: "typable", component: focused };
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Insert a final transcript into the focused input, with editor and clipboard fallbacks. */
|
|
26
|
+
export function insertTranscript(ctx: ExtensionContext, tuiHandle: any, text: string): void {
|
|
27
|
+
// 1. Fallback: append to main editor if no TUI handle
|
|
28
|
+
if (!tuiHandle) {
|
|
29
|
+
const current = ctx.ui.getEditorText() ?? "";
|
|
30
|
+
const sep = current && !/\s$/.test(current) ? " " : "";
|
|
31
|
+
ctx.ui.setEditorText(current + sep + text);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// 2. Insert into focused editor or typable component
|
|
36
|
+
const target = resolveTarget(tuiHandle);
|
|
37
|
+
if (target?.kind === "editor") {
|
|
38
|
+
const current = target.editor.getText() ?? "";
|
|
39
|
+
const sep = current && !/\s$/.test(current) ? " " : "";
|
|
40
|
+
target.editor.setText(current + sep + text);
|
|
41
|
+
tuiHandle.requestRender?.();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (target?.kind === "typable") {
|
|
46
|
+
target.component.handleInput(text);
|
|
47
|
+
tuiHandle.requestRender?.();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 3. Fallback: copy to clipboard if no input focused
|
|
52
|
+
const copied = copyToClipboard(text);
|
|
53
|
+
if (copied) {
|
|
54
|
+
ctx.ui.notify("Dictated text copied to clipboard (no input focused)", "info");
|
|
55
|
+
} else {
|
|
56
|
+
ctx.ui.notify(`Dictated: ${text}`, "info");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Input, fuzzyFilter, getKeybindings } from "@earendil-works/pi-tui";
|
|
2
|
+
import type { AsrModelDefinition } from "./providers.ts";
|
|
3
|
+
|
|
4
|
+
export class SearchableAsrModelSelectorComponent {
|
|
5
|
+
private searchInput = new Input();
|
|
6
|
+
private selectedIndex = 0;
|
|
7
|
+
private filteredModels: AsrModelDefinition[] = [];
|
|
8
|
+
private _focused = true;
|
|
9
|
+
private cachedLines?: string[];
|
|
10
|
+
private cachedWidth = -1;
|
|
11
|
+
|
|
12
|
+
get focused() {
|
|
13
|
+
return this._focused;
|
|
14
|
+
}
|
|
15
|
+
set focused(value: boolean) {
|
|
16
|
+
this._focused = value;
|
|
17
|
+
this.searchInput.focused = value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private tui: any,
|
|
22
|
+
private theme: any,
|
|
23
|
+
private models: AsrModelDefinition[],
|
|
24
|
+
private currentModelId: string,
|
|
25
|
+
private onSelect: (model: AsrModelDefinition) => void,
|
|
26
|
+
private onCancel: () => void,
|
|
27
|
+
private getLoginStatus?: (provider: string) => boolean,
|
|
28
|
+
) {
|
|
29
|
+
this.filteredModels = [...models];
|
|
30
|
+
const curIdx = models.findIndex((m) => m.id === currentModelId);
|
|
31
|
+
this.selectedIndex = curIdx >= 0 ? curIdx : 0;
|
|
32
|
+
this.searchInput.focused = true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private refresh() {
|
|
36
|
+
this.cachedLines = undefined;
|
|
37
|
+
this.tui.requestRender?.();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
filterModels(query: string) {
|
|
41
|
+
const q = query.trim().toLowerCase();
|
|
42
|
+
if (q) {
|
|
43
|
+
this.filteredModels = fuzzyFilter(
|
|
44
|
+
this.models,
|
|
45
|
+
q,
|
|
46
|
+
(m) => `${m.id} ${m.name} ${m.provider} ${m.description}`,
|
|
47
|
+
);
|
|
48
|
+
} else {
|
|
49
|
+
this.filteredModels = [...this.models];
|
|
50
|
+
}
|
|
51
|
+
this.selectedIndex = q ? 0 : Math.min(this.selectedIndex, Math.max(0, this.filteredModels.length - 1));
|
|
52
|
+
this.refresh();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
render(width: number): string[] {
|
|
56
|
+
if (this.cachedLines && this.cachedWidth === width) return this.cachedLines;
|
|
57
|
+
|
|
58
|
+
const lines: string[] = [];
|
|
59
|
+
const border = this.theme.fg("accent", "─".repeat(Math.max(10, width)));
|
|
60
|
+
|
|
61
|
+
lines.push(border);
|
|
62
|
+
lines.push("");
|
|
63
|
+
|
|
64
|
+
// Scope & credentials status line (matching Pi native selector screenshot)
|
|
65
|
+
const scopeTitle = `${this.theme.fg("muted", "Scope: ")}${this.theme.fg("accent", "all")}${this.theme.fg("muted", " | dashscope")}`;
|
|
66
|
+
lines.push(` ${scopeTitle}`);
|
|
67
|
+
|
|
68
|
+
if (this.getLoginStatus) {
|
|
69
|
+
const loggedIn = this.getLoginStatus("dashscope");
|
|
70
|
+
const authStatus = loggedIn
|
|
71
|
+
? this.theme.fg("success", "✓ DashScope credentials: ready (via /login dashscope)")
|
|
72
|
+
: this.theme.fg("warning", "! DashScope credentials: not logged in (run /login dashscope)");
|
|
73
|
+
lines.push(` ${authStatus}`);
|
|
74
|
+
}
|
|
75
|
+
lines.push("");
|
|
76
|
+
|
|
77
|
+
// Search input line (> █)
|
|
78
|
+
for (const l of this.searchInput.render(Math.max(1, width - 4))) {
|
|
79
|
+
lines.push(` ${l}`);
|
|
80
|
+
}
|
|
81
|
+
lines.push("");
|
|
82
|
+
|
|
83
|
+
// Model list (<cursor><check><model-id> [<provider>] [badges])
|
|
84
|
+
if (this.filteredModels.length === 0) {
|
|
85
|
+
lines.push(` ${this.theme.fg("warning", "No matching ASR models found")}`);
|
|
86
|
+
} else {
|
|
87
|
+
const maxVisible = 6;
|
|
88
|
+
const total = this.filteredModels.length;
|
|
89
|
+
const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(maxVisible / 2), total - maxVisible));
|
|
90
|
+
const endIndex = Math.min(startIndex + maxVisible, total);
|
|
91
|
+
|
|
92
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
93
|
+
const item = this.filteredModels[i];
|
|
94
|
+
const isSelected = i === this.selectedIndex;
|
|
95
|
+
const isCurrent = item.id === this.currentModelId;
|
|
96
|
+
|
|
97
|
+
const cursor = isSelected ? this.theme.fg("accent", "→ ") : " ";
|
|
98
|
+
const check = isCurrent ? this.theme.fg("accent", "✓ ") : " ";
|
|
99
|
+
const modelText = isSelected
|
|
100
|
+
? (this.theme.bold ? this.theme.bold(this.theme.fg("accent", item.id)) : this.theme.fg("accent", item.id))
|
|
101
|
+
: this.theme.fg("text", item.id);
|
|
102
|
+
const providerBadge = this.theme.fg("muted", `[${item.provider}]`);
|
|
103
|
+
const currentBadge = isCurrent ? this.theme.fg("muted", " · current") : "";
|
|
104
|
+
|
|
105
|
+
lines.push(` ${cursor}${check}${modelText} ${providerBadge}${currentBadge}`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (startIndex > 0 || endIndex < total) {
|
|
109
|
+
lines.push(` ${this.theme.fg("muted", `(${this.selectedIndex + 1}/${total})`)}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
lines.push("");
|
|
113
|
+
|
|
114
|
+
// Selected model detail
|
|
115
|
+
const selected = this.filteredModels[this.selectedIndex];
|
|
116
|
+
if (selected) {
|
|
117
|
+
lines.push(` ${this.theme.fg("muted", "Model Name: ")}${selected.name}`);
|
|
118
|
+
lines.push(` ${this.theme.fg("muted", "Description: ")}${selected.description}`);
|
|
119
|
+
}
|
|
120
|
+
lines.push("");
|
|
121
|
+
|
|
122
|
+
// Keybinding footer hints
|
|
123
|
+
lines.push(this.theme.fg("dim", " Enter to select · Escape/Ctrl+C to cancel · ↑↓ to navigate"));
|
|
124
|
+
lines.push(border);
|
|
125
|
+
|
|
126
|
+
this.cachedLines = lines;
|
|
127
|
+
this.cachedWidth = width;
|
|
128
|
+
return lines;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
invalidate() {
|
|
132
|
+
this.cachedLines = undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
handleInput(keyData: string) {
|
|
136
|
+
const kb = getKeybindings();
|
|
137
|
+
if (kb.matches(keyData, "tui.select.up") || keyData === "k") {
|
|
138
|
+
if (this.filteredModels.length === 0) return;
|
|
139
|
+
this.selectedIndex = this.selectedIndex === 0 ? this.filteredModels.length - 1 : this.selectedIndex - 1;
|
|
140
|
+
this.refresh();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (kb.matches(keyData, "tui.select.down") || keyData === "j") {
|
|
144
|
+
if (this.filteredModels.length === 0) return;
|
|
145
|
+
this.selectedIndex = this.selectedIndex === this.filteredModels.length - 1 ? 0 : this.selectedIndex + 1;
|
|
146
|
+
this.refresh();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (kb.matches(keyData, "tui.select.confirm") || keyData === "\n" || keyData === "\r") {
|
|
150
|
+
const selected = this.filteredModels[this.selectedIndex];
|
|
151
|
+
if (selected) {
|
|
152
|
+
this.onSelect(selected);
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (kb.matches(keyData, "tui.select.cancel") || keyData === "\x1b" || keyData === "\x03") {
|
|
157
|
+
this.onCancel();
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
this.searchInput.handleInput(keyData);
|
|
161
|
+
this.filterModels(this.searchInput.getValue());
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export { ALL_ASR_MODELS, createAsrProvider, type AsrModelDefinition, type StreamingAsrProvider } from "../custom-providers/asr/index.ts";
|
|
4
|
+
|
|
5
|
+
export async function resolveProviderApiKey(provider: string, ctx?: ExtensionContext | null): Promise<string> {
|
|
6
|
+
if (ctx?.modelRegistry) {
|
|
7
|
+
const candidates = provider === "dashscope" ? ["dashscope", "aliyun"] : [provider];
|
|
8
|
+
for (const id of candidates) {
|
|
9
|
+
try {
|
|
10
|
+
const key = await ctx.modelRegistry.getApiKeyForProvider(id);
|
|
11
|
+
if (key?.trim()) return key.trim();
|
|
12
|
+
} catch {}
|
|
13
|
+
try {
|
|
14
|
+
const auth = await ctx.modelRegistry.getProviderAuth(id);
|
|
15
|
+
if (auth?.auth?.apiKey?.trim()) return auth.auth.apiKey.trim();
|
|
16
|
+
} catch {}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const envKey = process.env[`${provider.toUpperCase()}_API_KEY`];
|
|
20
|
+
if (envKey?.trim()) return envKey.trim();
|
|
21
|
+
if (provider === "dashscope") {
|
|
22
|
+
return process.env.ALIYUN_API_KEY?.trim() || "";
|
|
23
|
+
}
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
const BRAILLE_FRAMES = ["⠀", "⣀", "⣄", "⣤", "⣦", "⣶", "⣷", "⣿"];
|
|
4
|
+
const METER_CELLS = 4;
|
|
5
|
+
const METER_TICK_MS = 80;
|
|
6
|
+
|
|
7
|
+
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
8
|
+
const SPINNER_INTERVAL_MS = 80;
|
|
9
|
+
|
|
10
|
+
function rmsToBlock(rms: number): string {
|
|
11
|
+
const norm = Math.min(1, Math.max(0, rms / 8000));
|
|
12
|
+
const idx = Math.min(BRAILLE_FRAMES.length - 1, Math.floor(norm * BRAILLE_FRAMES.length));
|
|
13
|
+
return BRAILLE_FRAMES[idx];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Own animation timers for one dictation controller. */
|
|
17
|
+
export function createStatusDisplay(getActiveContext: () => ExtensionContext | null) {
|
|
18
|
+
// Meter animation
|
|
19
|
+
let meter: number[] = new Array(METER_CELLS).fill(0);
|
|
20
|
+
let currentLevel = 0;
|
|
21
|
+
let meterTimer: NodeJS.Timeout | null = null;
|
|
22
|
+
|
|
23
|
+
// Spinner animation
|
|
24
|
+
let spinnerFrame = 0;
|
|
25
|
+
let spinnerTimer: NodeJS.Timeout | null = null;
|
|
26
|
+
|
|
27
|
+
const setStatus = (msg: string | undefined) => {
|
|
28
|
+
const activeCtx = getActiveContext();
|
|
29
|
+
if (!activeCtx) return;
|
|
30
|
+
activeCtx.ui.setStatus("tui-asr", msg);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const stopSpinner = () => {
|
|
34
|
+
if (spinnerTimer) {
|
|
35
|
+
clearInterval(spinnerTimer);
|
|
36
|
+
spinnerTimer = null;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const stopMeter = () => {
|
|
41
|
+
if (meterTimer) {
|
|
42
|
+
clearInterval(meterTimer);
|
|
43
|
+
meterTimer = null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Start braille audio level meter animation */
|
|
48
|
+
const startMeter = () => {
|
|
49
|
+
stopMeter();
|
|
50
|
+
meter = new Array(METER_CELLS).fill(0);
|
|
51
|
+
currentLevel = 0;
|
|
52
|
+
const render = () => {
|
|
53
|
+
const dot = getActiveContext()?.ui.theme.fg("error", "●") ?? "●";
|
|
54
|
+
setStatus(`${dot} ${meter.map(rmsToBlock).join("")} listening…`);
|
|
55
|
+
};
|
|
56
|
+
render();
|
|
57
|
+
meterTimer = setInterval(() => {
|
|
58
|
+
meter.shift();
|
|
59
|
+
meter.push(currentLevel);
|
|
60
|
+
render();
|
|
61
|
+
}, METER_TICK_MS);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Start finalizing spinner */
|
|
65
|
+
const startSpinner = (suffix: string) => {
|
|
66
|
+
stopSpinner();
|
|
67
|
+
spinnerFrame = 0;
|
|
68
|
+
setStatus(`${SPINNER_FRAMES[0]} ${suffix}`);
|
|
69
|
+
spinnerTimer = setInterval(() => {
|
|
70
|
+
spinnerFrame = (spinnerFrame + 1) % SPINNER_FRAMES.length;
|
|
71
|
+
setStatus(`${SPINNER_FRAMES[spinnerFrame]} ${suffix}`);
|
|
72
|
+
}, SPINNER_INTERVAL_MS);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
setStatus, startMeter, stopMeter, startSpinner, stopSpinner,
|
|
77
|
+
setLevel(level: number) { currentLevel = level; },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# web-reach
|
|
2
|
+
|
|
3
|
+
Web search and page extraction tools for Pi. Registers `web_search` through Exa's public MCP endpoint and `web_fetch` for readable Markdown or raw responses.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pi install npm:@rol1n/web-reach
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Also included in `@rol1n/pi-kit`. Avoid enabling another extension that registers the same tool names unless overriding them is intentional.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{"query":"Pi coding agent package resource filtering"}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{"url":"https://example.com/docs","mode":"readable"}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The first object is for `web_search`; the second is for `web_fetch`. Fetch mode defaults to `readable` and also accepts `raw`. Extraction includes HTML-to-Markdown, JSON formatting, PDF handling, and a Jina Reader fallback for pages that need it. Returned text is limited to 35,000 characters.
|
|
24
|
+
|
|
25
|
+
## Configuration
|
|
26
|
+
|
|
27
|
+
| Environment variable | Default |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `SRP_WEB_SEARCH_TIMEOUT_MS` | 60000 |
|
|
30
|
+
| `SRP_WEB_FETCH_TIMEOUT_MS` | 20000 |
|
|
31
|
+
| `SRP_WEB_FETCH_MAX_BYTES` | 4194304 (4 MiB) |
|
|
32
|
+
|
|
33
|
+
No separate search API key is configured; availability and rate limits depend on the public Exa and Jina services. PDF extraction optionally imports `unpdf` if it is available. Without that package the implementation falls back to a basic text extraction path, with lower PDF text quality.
|
|
34
|
+
|
|
35
|
+
## Source layout
|
|
36
|
+
|
|
37
|
+
`index.ts` registers the tools and re-exports `searchWeb` and `fetchUrl`. `search.ts` handles Exa MCP requests and response parsing; `transport.ts` shares timeout, cancellation, environment, and truncation helpers. `page.ts` coordinates page fetching and extraction, using `rsc.ts` for Next.js data, `html.ts` for semantic Markdown, `pdf.ts` for optional PDF extraction, and `reader.ts` for the Jina Reader fallback.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export function extractTitleFromHtml(html: string): string {
|
|
2
|
+
const match = html.match(/<title[^>]*>([^<]+)<\/title>/i);
|
|
3
|
+
return match?.[1]?.trim() ?? "";
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function isLikelyJSRendered(html: string): boolean {
|
|
7
|
+
const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
|
|
8
|
+
if (!bodyMatch) return false;
|
|
9
|
+
const textContent = bodyMatch[1]
|
|
10
|
+
.replace(/<script[\s\S]*?<\/script>/gi, "")
|
|
11
|
+
.replace(/<style[\s\S]*?<\/style>/gi, "")
|
|
12
|
+
.replace(/<[^>]+>/g, "")
|
|
13
|
+
.replace(/\s+/g, " ")
|
|
14
|
+
.trim();
|
|
15
|
+
const scriptCount = (html.match(/<script/gi) || []).length;
|
|
16
|
+
return textContent.length < 300 && scriptCount >= 2;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function htmlToMarkdown(html: string): string {
|
|
20
|
+
let s = html.replace(/<(script|style|noscript|svg|canvas|form|nav|footer|header|aside)[\s\S]*?<\/\1>/gi, " ");
|
|
21
|
+
s = s.replace(/<!--[\s\S]*?-->/g, " ");
|
|
22
|
+
|
|
23
|
+
// Headings
|
|
24
|
+
s = s.replace(/<h1[^>]*>([\s\S]*?)<\/h1>/gi, "\n\n# $1\n\n");
|
|
25
|
+
s = s.replace(/<h2[^>]*>([\s\S]*?)<\/h2>/gi, "\n\n## $1\n\n");
|
|
26
|
+
s = s.replace(/<h3[^>]*>([\s\S]*?)<\/h3>/gi, "\n\n### $1\n\n");
|
|
27
|
+
s = s.replace(/<h4[^>]*>([\s\S]*?)<\/h4>/gi, "\n\n#### $1\n\n");
|
|
28
|
+
s = s.replace(/<h5[^>]*>([\s\S]*?)<\/h5>/gi, "\n\n##### $1\n\n");
|
|
29
|
+
s = s.replace(/<h6[^>]*>([\s\S]*?)<\/h6>/gi, "\n\n###### $1\n\n");
|
|
30
|
+
|
|
31
|
+
// Code blocks and inline code
|
|
32
|
+
s = s.replace(/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/gi, "\n```\n$1\n```\n");
|
|
33
|
+
s = s.replace(/<pre[^>]*>([\s\S]*?)<\/pre>/gi, "\n```\n$1\n```\n");
|
|
34
|
+
s = s.replace(/<code[^>]*>([\s\S]*?)<\/code>/gi, " `$1` ");
|
|
35
|
+
|
|
36
|
+
// Blockquotes and emphasis
|
|
37
|
+
s = s.replace(/<blockquote[^>]*>([\s\S]*?)<\/blockquote>/gi, "\n> $1\n");
|
|
38
|
+
s = s.replace(/<(strong|b)[^>]*>([\s\S]*?)<\/\1>/gi, "**$2**");
|
|
39
|
+
s = s.replace(/<(em|i)[^>]*>([\s\S]*?)<\/\1>/gi, "*$2*");
|
|
40
|
+
|
|
41
|
+
// Links
|
|
42
|
+
s = s.replace(/<a\s+[^>]*href=["']([^"']+)["'][^>]*>([\s\S]*?)<\/a>/gi, (_, href, text) => {
|
|
43
|
+
const t = text.replace(/<[^>]*>/g, "").trim();
|
|
44
|
+
if (!t) return "";
|
|
45
|
+
if (href.startsWith("#") || href.startsWith("javascript:")) return t;
|
|
46
|
+
return `[${t}](${href})`;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Lists
|
|
50
|
+
s = s.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, "\n- $1");
|
|
51
|
+
s = s.replace(/<\/(ul|ol)>/gi, "\n\n");
|
|
52
|
+
|
|
53
|
+
// Tables
|
|
54
|
+
s = s.replace(/<tr[^>]*>([\s\S]*?)<\/tr>/gi, "\n$1 |");
|
|
55
|
+
s = s.replace(/<t[dh][^>]*>([\s\S]*?)<\/t[dh]>/gi, " | $1");
|
|
56
|
+
s = s.replace(/<\/(table|thead|tbody)>/gi, "\n\n");
|
|
57
|
+
|
|
58
|
+
// Paragraphs and line breaks
|
|
59
|
+
s = s.replace(/<\/(p|div|section|article)>/gi, "\n\n");
|
|
60
|
+
s = s.replace(/<br\s*\/?>/gi, "\n");
|
|
61
|
+
|
|
62
|
+
// Strip remaining tags
|
|
63
|
+
s = s.replace(/<[^>]*>/g, " ");
|
|
64
|
+
|
|
65
|
+
// HTML entity decoding
|
|
66
|
+
s = s
|
|
67
|
+
.replace(/ /g, " ")
|
|
68
|
+
.replace(/&/g, "&")
|
|
69
|
+
.replace(/</g, "<")
|
|
70
|
+
.replace(/>/g, ">")
|
|
71
|
+
.replace(/"/g, '"')
|
|
72
|
+
.replace(/'/g, "'")
|
|
73
|
+
.replace(/…/g, "…")
|
|
74
|
+
.replace(/—/g, "—")
|
|
75
|
+
.replace(/–/g, "–");
|
|
76
|
+
|
|
77
|
+
return s
|
|
78
|
+
.split("\n")
|
|
79
|
+
.map((line) => line.trimEnd())
|
|
80
|
+
.filter((line, i, arr) => !(line === "" && arr[i - 1] === ""))
|
|
81
|
+
.join("\n")
|
|
82
|
+
.trim();
|
|
83
|
+
}
|