mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:48:26 +00:00
fix(image): allow workspace and sandbox media paths (#15541)
This commit is contained in:
@@ -150,6 +150,75 @@ describe("image tool implicit imageModel config", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("allows workspace images outside default local media roots", async () => {
|
||||
const workspaceParent = await fs.mkdtemp(
|
||||
path.join(process.cwd(), ".openclaw-workspace-image-"),
|
||||
);
|
||||
try {
|
||||
const workspaceDir = path.join(workspaceParent, "workspace");
|
||||
await fs.mkdir(workspaceDir, { recursive: true });
|
||||
const imagePath = path.join(workspaceDir, "photo.png");
|
||||
const pngB64 =
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/woAAn8B9FD5fHAAAAAASUVORK5CYII=";
|
||||
await fs.writeFile(imagePath, Buffer.from(pngB64, "base64"));
|
||||
|
||||
const fetch = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
statusText: "OK",
|
||||
headers: new Headers(),
|
||||
json: async () => ({
|
||||
content: "ok",
|
||||
base_resp: { status_code: 0, status_msg: "" },
|
||||
}),
|
||||
});
|
||||
// @ts-expect-error partial global
|
||||
global.fetch = fetch;
|
||||
vi.stubEnv("MINIMAX_API_KEY", "minimax-test");
|
||||
|
||||
const agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-"));
|
||||
const cfg: OpenClawConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
model: { primary: "minimax/MiniMax-M2.1" },
|
||||
imageModel: { primary: "minimax/MiniMax-VL-01" },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const withoutWorkspace = createImageTool({ config: cfg, agentDir });
|
||||
expect(withoutWorkspace).not.toBeNull();
|
||||
if (!withoutWorkspace) {
|
||||
throw new Error("expected image tool");
|
||||
}
|
||||
await expect(
|
||||
withoutWorkspace.execute("t0", {
|
||||
prompt: "Describe the image.",
|
||||
image: imagePath,
|
||||
}),
|
||||
).rejects.toThrow(/Local media path is not under an allowed directory/i);
|
||||
|
||||
const withWorkspace = createImageTool({ config: cfg, agentDir, workspaceDir });
|
||||
expect(withWorkspace).not.toBeNull();
|
||||
if (!withWorkspace) {
|
||||
throw new Error("expected image tool");
|
||||
}
|
||||
|
||||
await expect(
|
||||
withWorkspace.execute("t1", {
|
||||
prompt: "Describe the image.",
|
||||
image: imagePath,
|
||||
}),
|
||||
).resolves.toMatchObject({
|
||||
content: [{ type: "text", text: "ok" }],
|
||||
});
|
||||
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
await fs.rm(workspaceParent, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("sandboxes image paths like the read tool", async () => {
|
||||
const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-image-sandbox-"));
|
||||
const agentDir = path.join(stateDir, "agent");
|
||||
|
||||
@@ -5,7 +5,7 @@ import type { OpenClawConfig } from "../../config/config.js";
|
||||
import type { SandboxFsBridge } from "../sandbox/fs-bridge.js";
|
||||
import type { AnyAgentTool } from "./common.js";
|
||||
import { resolveUserPath } from "../../utils.js";
|
||||
import { loadWebMedia } from "../../web/media.js";
|
||||
import { getDefaultLocalRoots, loadWebMedia } from "../../web/media.js";
|
||||
import { ensureAuthProfileStore, listProfilesForProvider } from "../auth-profiles.js";
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER } from "../defaults.js";
|
||||
import { minimaxUnderstandImage } from "../minimax-vlm.js";
|
||||
@@ -325,6 +325,7 @@ async function runImagePrompt(params: {
|
||||
export function createImageTool(options?: {
|
||||
config?: OpenClawConfig;
|
||||
agentDir?: string;
|
||||
workspaceDir?: string;
|
||||
sandbox?: ImageSandboxConfig;
|
||||
/** If true, the model has native vision capability and images in the prompt are auto-injected */
|
||||
modelHasVision?: boolean;
|
||||
@@ -351,6 +352,19 @@ export function createImageTool(options?: {
|
||||
? "Analyze an image with a vision model. Only use this tool when the image was NOT already provided in the user's message. Images mentioned in the prompt are automatically visible to you."
|
||||
: "Analyze an image with the configured image model (agents.defaults.imageModel). Provide a prompt and image path or URL.";
|
||||
|
||||
const localRoots = (() => {
|
||||
const roots = getDefaultLocalRoots();
|
||||
const workspaceDir = options?.workspaceDir?.trim();
|
||||
if (!workspaceDir) {
|
||||
return roots;
|
||||
}
|
||||
const normalized = workspaceDir.startsWith("~") ? resolveUserPath(workspaceDir) : workspaceDir;
|
||||
if (!roots.includes(normalized)) {
|
||||
roots.push(normalized);
|
||||
}
|
||||
return roots;
|
||||
})();
|
||||
|
||||
return {
|
||||
label: "Image",
|
||||
name: "image",
|
||||
@@ -441,10 +455,14 @@ export function createImageTool(options?: {
|
||||
: sandboxConfig
|
||||
? await loadWebMedia(resolvedPath ?? resolvedImage, {
|
||||
maxBytes,
|
||||
localRoots: "any",
|
||||
readFile: (filePath) =>
|
||||
sandboxConfig.bridge.readFile({ filePath, cwd: sandboxConfig.root }),
|
||||
})
|
||||
: await loadWebMedia(resolvedPath ?? resolvedImage, maxBytes);
|
||||
: await loadWebMedia(resolvedPath ?? resolvedImage, {
|
||||
maxBytes,
|
||||
localRoots,
|
||||
});
|
||||
if (media.kind !== "image") {
|
||||
throw new Error(`Unsupported media type: ${media.kind}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user