Skip to content

Commit 3a45667

Browse files
committed
feat(image-generation): add image_generate tool
1 parent 916db21 commit 3a45667

17 files changed

Lines changed: 949 additions & 8 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import type { OpenClawConfig } from "../config/config.js";
3+
import { createOpenClawTools } from "./openclaw-tools.js";
4+
5+
vi.mock("../plugins/tools.js", () => ({
6+
resolvePluginTools: () => [],
7+
}));
8+
9+
function asConfig(value: unknown): OpenClawConfig {
10+
return value as OpenClawConfig;
11+
}
12+
13+
describe("openclaw tools image generation registration", () => {
14+
it("registers image_generate when image-generation config is present", () => {
15+
const tools = createOpenClawTools({
16+
config: asConfig({
17+
agents: {
18+
defaults: {
19+
imageGenerationModel: {
20+
primary: "openai/gpt-image-1",
21+
},
22+
},
23+
},
24+
}),
25+
agentDir: "/tmp/openclaw-agent-main",
26+
});
27+
28+
expect(tools.map((tool) => tool.name)).toContain("image_generate");
29+
});
30+
31+
it("omits image_generate when image-generation config is absent", () => {
32+
const tools = createOpenClawTools({
33+
config: asConfig({}),
34+
agentDir: "/tmp/openclaw-agent-main",
35+
});
36+
37+
expect(tools.map((tool) => tool.name)).not.toContain("image_generate");
38+
});
39+
});

‎src/agents/openclaw-tools.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { createCanvasTool } from "./tools/canvas-tool.js";
1212
import type { AnyAgentTool } from "./tools/common.js";
1313
import { createCronTool } from "./tools/cron-tool.js";
1414
import { createGatewayTool } from "./tools/gateway-tool.js";
15+
import { createImageGenerateTool } from "./tools/image-generate-tool.js";
1516
import { createImageTool } from "./tools/image-tool.js";
1617
import { createMessageTool } from "./tools/message-tool.js";
1718
import { createNodesTool } from "./tools/nodes-tool.js";
@@ -103,6 +104,13 @@ export function createOpenClawTools(
103104
modelHasVision: options?.modelHasVision,
104105
})
105106
: null;
107+
const imageGenerateTool = createImageGenerateTool({
108+
config: options?.config,
109+
agentDir: options?.agentDir,
110+
workspaceDir,
111+
sandbox,
112+
fsPolicy: options?.fsPolicy,
113+
});
106114
const pdfTool = options?.agentDir?.trim()
107115
? createPdfTool({
108116
config: options?.config,
@@ -163,6 +171,7 @@ export function createOpenClawTools(
163171
agentChannel: options?.agentChannel,
164172
config: options?.config,
165173
}),
174+
...(imageGenerateTool ? [imageGenerateTool] : []),
166175
createGatewayTool({
167176
agentSessionKey: options?.agentSessionKey,
168177
config: options?.config,

‎src/agents/pi-embedded-subscribe.tools.media.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { describe, expect, it } from "vitest";
2-
import { extractToolResultMediaPaths } from "./pi-embedded-subscribe.tools.js";
2+
import {
3+
extractToolResultMediaPaths,
4+
isToolResultMediaTrusted,
5+
} from "./pi-embedded-subscribe.tools.js";
36

47
describe("extractToolResultMediaPaths", () => {
58
it("returns empty array for null/undefined", () => {
@@ -229,4 +232,8 @@ describe("extractToolResultMediaPaths", () => {
229232
};
230233
expect(extractToolResultMediaPaths(result)).toEqual(["/tmp/page1.png", "/tmp/page2.png"]);
231234
});
235+
236+
it("trusts image_generate local MEDIA paths", () => {
237+
expect(isToolResultMediaTrusted("image_generate")).toBe(true);
238+
});
232239
});

‎src/agents/pi-embedded-subscribe.tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const TRUSTED_TOOL_RESULT_MEDIA = new Set([
142142
"exec",
143143
"gateway",
144144
"image",
145+
"image_generate",
145146
"memory_get",
146147
"memory_search",
147148
"message",

‎src/agents/system-prompt.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export function buildAgentSystemPrompt(params: {
268268
session_status:
269269
"Show a /status-equivalent status card (usage + time + Reasoning/Verbose/Elevated); use for model-use questions (📊 session_status); optional per-session model override",
270270
image: "Analyze an image with the configured image model",
271+
image_generate: "Generate images with the configured image-generation model",
271272
};
272273

273274
const toolOrder = [
@@ -295,6 +296,7 @@ export function buildAgentSystemPrompt(params: {
295296
"subagents",
296297
"session_status",
297298
"image",
299+
"image_generate",
298300
];
299301

300302
const rawToolNames = (params.toolNames ?? []).map((tool) => tool.trim());

‎src/agents/tool-catalog.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ describe("tool-catalog", () => {
77
expect(policy).toBeDefined();
88
expect(policy!.allow).toContain("web_search");
99
expect(policy!.allow).toContain("web_fetch");
10+
expect(policy!.allow).toContain("image_generate");
1011
});
1112
});

‎src/agents/tool-catalog.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ const CORE_TOOL_DEFINITIONS: CoreToolDefinition[] = [
233233
profiles: ["coding"],
234234
includeInOpenClawGroup: true,
235235
},
236+
{
237+
id: "image_generate",
238+
label: "image_generate",
239+
description: "Image generation",
240+
sectionId: "media",
241+
profiles: ["coding"],
242+
includeInOpenClawGroup: true,
243+
},
236244
{
237245
id: "tts",
238246
label: "tts",

0 commit comments

Comments
 (0)