Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
2.7 kB · 85 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import * as os from "node:os";import type { ImageContent, TextContent } from "@earendil-works/pi-ai";import { getImageDimensions, imageFallback } from "@earendil-works/pi-tui";import stripAnsi from "strip-ansi";import { sanitizeBinaryOutput } from "../../utils/shell.js";
export function shortenPath(path: unknown): string { if (typeof path !== "string") return ""; const home = os.homedir(); if (path.startsWith(home)) { return `~${path.slice(home.length)}`; } return path;}
export function str(value: unknown): string | null { if (typeof value === "string") return value; if (value == null) return ""; return null;}
export function replaceTabs(text: string): string { return text.replace(/\t/g, " ");}
export interface TextOutputOptions { /** Whether image fallbacks should parse image dimensions from base64 data. */ includeImageDimensions?: boolean;}
export function getTextOutput( result: { content: Array<{ type: string; text?: string; data?: string; mimeType?: string }> } | undefined, showImages: boolean, options: TextOutputOptions = {},): string { if (!result) return "";
const textBlocks = result.content.filter((c) => c.type === "text"); const imageBlocks = result.content.filter((c) => c.type === "image");
let output = textBlocks.map((c) => sanitizeBinaryOutput(stripAnsi(c.text || "")).replace(/\r/g, "")).join("\n");
const includeImageDimensions = options.includeImageDimensions ?? true; if (imageBlocks.length > 0 && !showImages) { const imageIndicators = imageBlocks .map((img) => { const mimeType = img.mimeType ?? "image/unknown"; const dims = includeImageDimensions && img.data && img.mimeType ? (getImageDimensions(img.data, img.mimeType) ?? undefined) : undefined; return imageFallback(mimeType, dims); }) .join("\n"); output = output ? `${output}\n${imageIndicators}` : imageIndicators; }
return output;}
export type ToolRenderResultLike<TDetails> = { content: (TextContent | ImageContent)[]; details: TDetails;};
export function invalidArgText(theme: { fg: (name: any, text: string) => string }): string { return theme.fg("error", "[invalid arg]");}
export function formatToolDuration(durationMs: number): string { if (durationMs < 1000) { return `${Math.round(durationMs)}ms`; } const totalSeconds = Math.floor(durationMs / 1000); if (totalSeconds < 60) { return `${(durationMs / 1000).toFixed(1)}s`; } const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; if (minutes < 60) { return `${minutes}m ${seconds.toString().padStart(2, "0")}s`; } const hours = Math.floor(minutes / 60); return `${hours}h ${(minutes % 60).toString().padStart(2, "0")}m`;}