diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ffaa5d23..117bc22ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Docs: https://docs.openclaw.ai - WhatsApp: honor per-account `dmPolicy` overrides (account-level settings now take precedence over channel defaults for inbound DMs). (#10082) Thanks @mcaxtr. - Media: accept `MEDIA:`-prefixed paths (lenient whitespace) when loading outbound media to prevent `ENOENT` for tool-returned local media paths. (#13107) Thanks @mcaxtr. - Agents/Image tool: allow workspace-local image paths by including the active workspace directory in local media allowlists, and trust sandbox-validated paths in image loaders to prevent false "not under an allowed directory" rejections. (#15541) +- Agents/Image tool: propagate the effective workspace root into tool wiring so workspace-local image paths are accepted by default when running without an explicit `workspaceDir`. (#16722) - Cron/Slack: preserve agent identity (name and icon) when cron jobs deliver outbound messages. (#16242) Thanks @robbyczgw-cla. - Cron: prevent `cron list`/`cron status` from silently skipping past-due recurring jobs by using maintenance recompute semantics. (#16156) Thanks @zerone0x. - Cron: repair missing/corrupt `nextRunAtMs` for the updated job without globally recomputing unrelated due jobs during `cron update`. (#15750) diff --git a/src/agents/openclaw-tools.ts b/src/agents/openclaw-tools.ts index 5fc4bba92..4825ebccb 100644 --- a/src/agents/openclaw-tools.ts +++ b/src/agents/openclaw-tools.ts @@ -60,11 +60,12 @@ export function createOpenClawTools(options?: { /** If true, omit the message tool from the tool list. */ disableMessageTool?: boolean; }): AnyAgentTool[] { + const workspaceDir = options?.workspaceDir?.trim() || process.cwd(); const imageTool = options?.agentDir?.trim() ? createImageTool({ config: options?.config, agentDir: options.agentDir, - workspaceDir: options?.workspaceDir, + workspaceDir, sandbox: options?.sandboxRoot && options?.sandboxFsBridge ? { root: options.sandboxRoot, bridge: options.sandboxFsBridge } @@ -157,7 +158,7 @@ export function createOpenClawTools(options?: { const pluginTools = resolvePluginTools({ context: { config: options?.config, - workspaceDir: options?.workspaceDir, + workspaceDir, agentDir: options?.agentDir, agentId: resolveSessionAgentId({ sessionKey: options?.agentSessionKey, diff --git a/src/agents/pi-tools.ts b/src/agents/pi-tools.ts index ae162c85b..acffdedf5 100644 --- a/src/agents/pi-tools.ts +++ b/src/agents/pi-tools.ts @@ -321,7 +321,7 @@ export function createOpenClawCodingTools(options?: { pathPrepend: options?.exec?.pathPrepend ?? execConfig.pathPrepend, safeBins: options?.exec?.safeBins ?? execConfig.safeBins, agentId, - cwd: options?.workspaceDir, + cwd: workspaceRoot, allowBackground, scopeKey, sessionKey: options?.sessionKey, @@ -386,7 +386,7 @@ export function createOpenClawCodingTools(options?: { agentDir: options?.agentDir, sandboxRoot, sandboxFsBridge, - workspaceDir: options?.workspaceDir, + workspaceDir: workspaceRoot, sandboxed: !!sandbox, config: options?.config, pluginToolAllowlist: collectExplicitAllowlist([ diff --git a/src/agents/tools/image-tool.e2e.test.ts b/src/agents/tools/image-tool.e2e.test.ts index ee5c30a46..5a93a32b1 100644 --- a/src/agents/tools/image-tool.e2e.test.ts +++ b/src/agents/tools/image-tool.e2e.test.ts @@ -3,6 +3,7 @@ import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../../config/config.js"; +import { createOpenClawCodingTools } from "../pi-tools.js"; import { createHostSandboxFsBridge } from "../test-helpers/host-sandbox-fs-bridge.js"; import { __testing, createImageTool, resolveImageModelConfigForTool } from "./image-tool.js"; @@ -219,6 +220,64 @@ describe("image tool implicit imageModel config", () => { } }); + it("allows workspace images via createOpenClawCodingTools default workspace root", 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 tools = createOpenClawCodingTools({ config: cfg, agentDir }); + const tool = tools.find((candidate) => candidate.name === "image"); + expect(tool).not.toBeNull(); + if (!tool) { + throw new Error("expected image tool"); + } + + await expect( + tool.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"); diff --git a/src/agents/tools/image-tool.ts b/src/agents/tools/image-tool.ts index fb7c40d20..849853402 100644 --- a/src/agents/tools/image-tool.ts +++ b/src/agents/tools/image-tool.ts @@ -358,9 +358,14 @@ export function createImageTool(options?: { if (!workspaceDir) { return roots; } - const normalized = workspaceDir.startsWith("~") ? resolveUserPath(workspaceDir) : workspaceDir; - if (!roots.includes(normalized)) { - roots.push(normalized); + const expanded = workspaceDir.startsWith("~") ? resolveUserPath(workspaceDir) : workspaceDir; + const resolved = path.resolve(expanded); + // Defensive: never allow "/" as an implicit media root. + if (resolved === path.parse(resolved).root) { + return roots; + } + if (!roots.includes(resolved)) { + roots.push(resolved); } return roots; })();