SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
1.7 KB · 52 lines typescript
Raw Blame History
1import type { ContentPart, UnifiedMessage } from "./types";23export function textOf(parts: ContentPart[] | undefined | null): string {4  if (!parts) return "";5  return parts6    .filter((p): p is Extract<ContentPart, { type: "text" }> => p.type === "text")7    .map((p) => p.text)8    .join("");9}1011export function hasImages(messages: UnifiedMessage[]): boolean {12  return messages.some((m) => m.content.some((p) => p.type === "image"));13}14export function hasFiles(messages: UnifiedMessage[]): boolean {15  return messages.some((m) => m.content.some((p) => p.type === "file"));16}1718export const IMAGE_MIME = new Set(["image/png", "image/jpeg", "image/webp", "image/gif"]);19export const TEXT_LIKE_MIME = new Set([20  "text/plain",21  "text/markdown",22  "text/csv",23  "application/json",24  "text/html",25  "text/xml",26  "application/xml",27  "text/x-python",28  "text/javascript",29  "application/javascript",30  "text/typescript",31  "application/typescript",32  "text/x-c",33  "text/x-java",34  "text/x-go",35  "text/x-rust",36  "text/x-sh",37  "application/x-yaml",38  "text/yaml",39]);4041export function isTextLike(mime: string, name?: string): boolean {42  if (TEXT_LIKE_MIME.has(mime) || mime.startsWith("text/")) return true;43  if (name && /\.(md|txt|csv|json|ts|tsx|js|jsx|py|go|rs|java|kt|swift|rb|php|c|cc|cpp|h|hpp|cs|sh|zsh|yaml|yml|toml|ini|sql|html|css|scss|xml|r|jl|lua|m|scala|dart|tex)$/i.test(name)) return true;44  return false;45}4647/** Fallback for providers that don't accept text files natively: inline the file content. */48export function inlineTextFile(name: string, base64: string): string {49  const text = Buffer.from(base64, "base64").toString("utf8");50  return `<file name="${name}">\n${text}\n</file>`;51}52