mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 14:44:32 +00:00
refactor(media): share base64 mime sniff helper
This commit is contained in:
@@ -3,6 +3,7 @@ import { createEditTool, createReadTool, createWriteTool } from "@mariozechner/p
|
|||||||
import type { AnyAgentTool } from "./pi-tools.types.js";
|
import type { AnyAgentTool } from "./pi-tools.types.js";
|
||||||
import type { SandboxFsBridge } from "./sandbox/fs-bridge.js";
|
import type { SandboxFsBridge } from "./sandbox/fs-bridge.js";
|
||||||
import { detectMime } from "../media/mime.js";
|
import { detectMime } from "../media/mime.js";
|
||||||
|
import { sniffMimeFromBase64 } from "../media/sniff-mime-from-base64.js";
|
||||||
import { assertSandboxPath } from "./sandbox-paths.js";
|
import { assertSandboxPath } from "./sandbox-paths.js";
|
||||||
import { sanitizeToolResultImages } from "./tool-images.js";
|
import { sanitizeToolResultImages } from "./tool-images.js";
|
||||||
|
|
||||||
@@ -12,26 +13,6 @@ type ToolContentBlock = AgentToolResult<unknown>["content"][number];
|
|||||||
type ImageContentBlock = Extract<ToolContentBlock, { type: "image" }>;
|
type ImageContentBlock = Extract<ToolContentBlock, { type: "image" }>;
|
||||||
type TextContentBlock = Extract<ToolContentBlock, { type: "text" }>;
|
type TextContentBlock = Extract<ToolContentBlock, { type: "text" }>;
|
||||||
|
|
||||||
async function sniffMimeFromBase64(base64: string): Promise<string | undefined> {
|
|
||||||
const trimmed = base64.trim();
|
|
||||||
if (!trimmed) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const take = Math.min(256, trimmed.length);
|
|
||||||
const sliceLen = take - (take % 4);
|
|
||||||
if (sliceLen < 8) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const head = Buffer.from(trimmed.slice(0, sliceLen), "base64");
|
|
||||||
return await detectMime({ buffer: head });
|
|
||||||
} catch {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function rewriteReadImageHeader(text: string, mimeType: string): string {
|
function rewriteReadImageHeader(text: string, mimeType: string): string {
|
||||||
// pi-coding-agent uses: "Read image file [image/png]"
|
// pi-coding-agent uses: "Read image file [image/png]"
|
||||||
if (text.startsWith("Read image file [") && text.endsWith("]")) {
|
if (text.startsWith("Read image file [") && text.endsWith("]")) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { estimateBase64DecodedBytes } from "../media/base64.js";
|
import { estimateBase64DecodedBytes } from "../media/base64.js";
|
||||||
import { detectMime } from "../media/mime.js";
|
import { sniffMimeFromBase64 } from "../media/sniff-mime-from-base64.js";
|
||||||
|
|
||||||
export type ChatAttachment = {
|
export type ChatAttachment = {
|
||||||
type?: string;
|
type?: string;
|
||||||
@@ -31,26 +31,6 @@ function normalizeMime(mime?: string): string | undefined {
|
|||||||
return cleaned || undefined;
|
return cleaned || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sniffMimeFromBase64(base64: string): Promise<string | undefined> {
|
|
||||||
const trimmed = base64.trim();
|
|
||||||
if (!trimmed) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const take = Math.min(256, trimmed.length);
|
|
||||||
const sliceLen = take - (take % 4);
|
|
||||||
if (sliceLen < 8) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const head = Buffer.from(trimmed.slice(0, sliceLen), "base64");
|
|
||||||
return await detectMime({ buffer: head });
|
|
||||||
} catch {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isImageMime(mime?: string): boolean {
|
function isImageMime(mime?: string): boolean {
|
||||||
return typeof mime === "string" && mime.startsWith("image/");
|
return typeof mime === "string" && mime.startsWith("image/");
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/media/sniff-mime-from-base64.ts
Normal file
21
src/media/sniff-mime-from-base64.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { detectMime } from "./mime.js";
|
||||||
|
|
||||||
|
export async function sniffMimeFromBase64(base64: string): Promise<string | undefined> {
|
||||||
|
const trimmed = base64.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const take = Math.min(256, trimmed.length);
|
||||||
|
const sliceLen = take - (take % 4);
|
||||||
|
if (sliceLen < 8) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const head = Buffer.from(trimmed.slice(0, sliceLen), "base64");
|
||||||
|
return await detectMime({ buffer: head });
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user