repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix: harden extension text and aux LLM calls

c560492dbf18e4ea5ca3b153fc9c5c094f14a18e

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (7)

+48 / −22 text lines

Diff layout

extensions/angel/index.ts

Modified

+1 / −1 text lines

387387 systemPrompt: SYSTEM_PROMPT,
388388 messages: [{ role: "user", content: [{ type: "text", text: JSON.stringify(packet, null, 2) }], timestamp: Date.now() }],
389389 },
390 { apiKey: auth.apiKey, headers: auth.headers, signal },
390 { apiKey: auth.apiKey, headers: auth.headers, signal, cacheRetention: "none" },
391391 );
392392 const raw = res.content
393393 .filter((c): c is { type: "text"; text: string } => c.type === "text")

extensions/devil/index.ts

Modified

+1 / −1 text lines

599599 systemPrompt: SYNTHESIS_SYSTEM_PROMPT,
600600 messages: [{ role: "user", content: [{ type: "text", text: synthesisUserText }], timestamp: Date.now() }],
601601 },
602 { apiKey, headers, signal: timeoutSignal, reasoning: thinkingLevel },
602 { apiKey, headers, signal: timeoutSignal, reasoning: thinkingLevel, cacheRetention: "none" },
603603 ),
604604 );
605605 if (synthesisResponse.stopReason === "error") {

extensions/ghost/overlay.ts

Modified

+10 / −4 text lines

4343type ToolUpdateEvent = Extract<AgentSessionEvent, { type: "tool_execution_update" }>;
4444type ToolEndEvent = Extract<AgentSessionEvent, { type: "tool_execution_end" }>;
46function safeDisplayText(text: string): string {
47 return text
48 .replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\))/g, "")
49 .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, " ")
50 .trim();
51}
4653interface GhostOverlayLayout {
4754 innerWidth: number;
4855 frameHeight: number;
551558export function extractMessageText(message: { content: unknown }): string {
552 if (typeof message.content === "string") return message.content.trim();
559 if (typeof message.content === "string") return safeDisplayText(message.content);
553560 if (!Array.isArray(message.content)) return "";
555 return message.content
562 return safeDisplayText(message.content
556563 .filter((part): part is { type: "text"; text: string } =>
557564 typeof part === "object" &&
558565 part !== null &&
560567 typeof (part as { text?: unknown }).text === "string"
561568 )
562569 .map((part) => part.text)
563 .join("\n")
564 .trim();
570 .join("\n"));
567573export function formatGhostCwd(cwd: string): string {

extensions/pulse/index.ts

Modified

+1 / −1 text lines

250250 },
251251 ],
252252 },
253 { apiKey },
253 { apiKey, cacheRetention: "none" },
254254 );
256256 lastResponse = textFromResponse(response);

extensions/q/extract.ts

Modified

+13 / −5 text lines

88const SUGGESTION_RE = /^\s*(?:[-*+]\s+|\d+[.)]\s+|[A-Za-z][.)]\s+|(?:Option|Answer|Choice)\s+[A-Za-z0-9]+\s*[:.)-]\s*)(.+?)\s*$/i;
99const QUESTION_WORD_RE = /^(?:should|do|does|did|can|could|would|will|which|what|when|where|why|how)\b/i;
11function safeDisplayText(text: string): string {
12 return text
13 .replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\))/g, "")
14 .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, " ")
15 .replace(/\s+/g, " ")
16 .trim();
17}
1119export const EXTRACTOR_PROMPT = `Extract questions and predefined answer suggestions from the assistant message.
1321Return ONLY JSON with this shape:
7482export function cleanQuestion(line: string): string {
75 return line.replace(QUESTION_PREFIX_RE, "").replace(QUESTION_LABEL_RE, "").trim();
83 return safeDisplayText(line.replace(QUESTION_PREFIX_RE, "").replace(QUESTION_LABEL_RE, ""));
7886export function cleanSuggestion(line: string): string | undefined {
79 const suggestion = line.match(SUGGESTION_RE)?.[1]?.trim().replace(/^"(.+)"$/, "$1");
87 const suggestion = safeDisplayText(line.match(SUGGESTION_RE)?.[1]?.trim().replace(/^"(.+)"$/, "$1") ?? "");
8088 return suggestion || undefined;
116124 const parsed = JSON.parse(json) as { questions?: Array<{ question?: unknown; suggestions?: unknown }> };
117125 return (parsed.questions ?? [])
118126 .map((q) => ({
119 question: typeof q.question === "string" ? q.question.trim() : "",
127 question: typeof q.question === "string" ? safeDisplayText(q.question) : "",
120128 suggestions: Array.isArray(q.suggestions)
121 ? unique(q.suggestions.filter((s): s is string => typeof s === "string").map((s) => s.trim()).filter(Boolean)).slice(0, MAX_SUGGESTIONS)
129 ? unique(q.suggestions.filter((s): s is string => typeof s === "string").map(safeDisplayText).filter(Boolean)).slice(0, MAX_SUGGESTIONS)
122130 : [],
123131 }))
124132 .filter((q) => q.question.length > 0);
133141 const response = await complete(
134142 ctx.model,
135143 { systemPrompt: EXTRACTOR_PROMPT, messages: [userMessage] },
136 { apiKey: auth.apiKey, headers: auth.headers, signal: ctx.signal, reasoning: "low" },
144 { apiKey: auth.apiKey, headers: auth.headers, signal: ctx.signal, reasoning: "low", cacheRetention: "none" },
137145 );
138146 const raw = response.content
139147 .filter((c): c is { type: "text"; text: string } => c.type === "text")

extensions/session-rename/index.ts

Modified

+1 / −0 text lines

245245 apiKey: auth.apiKey,
246246 headers: auth.headers,
247247 signal,
248 cacheRetention: "none",
248249 ...(model.reasoning ? { reasoning: "minimal" as const } : {}),
249250 },
250251 );

extensions/web/index.ts

Modified

+21 / −10 text lines

402402 return `⬇ ${mb}/${totalMb}MB (${percent}%)`;
405function safeTerminalText(text: string): string {
406 return text
407 .replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\))/g, "")
408 .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, " ");
409}
411function safeDisplayText(text: string): string {
412 return safeTerminalText(text).replace(/[\t\r\n ]+/g, " ").trim();
413}
405415/** Render expanded tool text: prefix + each line dimmed. Returns null if content is not text. */
406416function expandedText(result: { content: Array<{ type: string; text?: string }> }, prefix: string, theme: { fg: (style: string, text: string) => string }): string | null {
407417 const content = result.content[0];
408418 if (content?.type !== "text") return null;
409419 let text = prefix;
410 for (const line of content.text.split("\n")) {
420 for (const line of safeTerminalText(content.text).split("\n")) {
411421 text += `\n${theme.fg("dim", line)}`;
412422 }
413423 return text;
416426function hintBox(theme: { fg: (style: string, text: string) => string }, style: "warning" | "error", msg: string): Text {
417 return new Text(theme.fg(style, msg), 0, 0);
427 return new Text(theme.fg(style, safeDisplayText(msg)), 0, 0);
420430/** Extract error message from a tool result, falling back to content text or a default. */
496506 renderCall(args, theme) {
497507 let text = theme.fg("toolTitle", theme.bold("\u2B07 web_fetch "));
498 text += theme.fg("accent", args.url);
508 text += theme.fg("accent", safeDisplayText(args.url));
499509 return new Text(text, 0, 0);
500510 },
523533 if (result.isError || details?.kind === "error") {
524534 const content = result.content[0];
525 const msg = (details?.error) || (content?.type === "text" ? content.text : "Fetch failed");
535 const msg = safeDisplayText((details?.error) || (content?.type === "text" ? content.text : "Fetch failed"));
526536 return hintBox(theme, "error", `\u2717 ${msg}`);
527537 }
620630 }
622632 if (details?.preview) {
623 const lines = details.preview.split("\n");
633 const lines = safeTerminalText(details.preview).split("\n");
624634 const maxLines = expanded ? lines.length : 30;
625635 const shown = lines.slice(0, maxLines).join("\n");
626636 const truncated = lines.length > maxLines;
759769 renderCall(args, theme) {
760770 let text = theme.fg("toolTitle", theme.bold("\uD83D\uDD0D web_search "));
761 text += theme.fg("accent", `"${args.query}"`);
771 text += theme.fg("accent", `"${safeDisplayText(args.query)}"`);
762772 return new Text(text, 0, 0);
763773 },
783793 if (results && results.length > 0) {
784794 const top = results.slice(0, 5);
785795 for (const r of top) {
786 text += `\n${theme.fg("dim", "\u2022 " + r.title)}`;
796 text += `\n${theme.fg("dim", "\u2022 " + safeDisplayText(r.title))}`;
787797 }
788798 if (results.length > 5) {
789799 text += `\n${theme.fg("muted", `... +${results.length - 5} more`)}`;
878888 renderCall(args, theme) {
879889 let text = theme.fg("toolTitle", theme.bold("\uD83D\uDDBC understand_image "));
880 const preview = args.prompt.length > 50 ? args.prompt.substring(0, 50) + "..." : args.prompt;
890 const safePrompt = safeDisplayText(args.prompt);
891 const preview = safePrompt.length > 50 ? safePrompt.substring(0, 50) + "..." : safePrompt;
881892 text += theme.fg("muted", `"${preview}"`);
882 text += " " + theme.fg("dim", args.image_source);
893 text += " " + theme.fg("dim", safeDisplayText(args.image_source));
883894 return new Text(text, 0, 0);
884895 },
900911 } else {
901912 const content = result.content[0];
902913 if (content?.type === "text" && content.text) {
903 text += `\n${theme.fg("dim", content.text.substring(0, 100).replace(/\n/g, " ") + "...")}`;
914 text += `\n${theme.fg("dim", safeDisplayText(content.text).substring(0, 100) + "...")}`;
904915 }
905916 text += ` ${theme.fg("muted", `(${keyHint("expandTools", "to expand")})`)}`;
906917 }