mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 06:04:51 +00:00
Add blockImages setting to prevent images from being sent to LLM providers
- Setting controls filtering at convertToLlm layer (defense-in-depth) - Images are always stored in session, filtered dynamically based on current setting - Toggle mid-session works: LLM sees/doesn't see images already in session - Fixed SettingsManager.save() to handle inMemory mode for all setters Closes #492
This commit is contained in:
parent
b582a6b70d
commit
1fc2a912d4
8 changed files with 80 additions and 127 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Added `blockImages` setting to prevent images from being sent to LLM providers. Provides defense-in-depth blocking at Read tool, CLI file processor, AgentSession, and convertToLlm layer. ([#492](https://github.com/badlogic/pi-mono/pull/492))
|
- Added `blockImages` setting to prevent images from being sent to LLM providers. Provides defense-in-depth blocking at Read tool, CLI file processor, AgentSession, and convertToLlm layer. ([#492](https://github.com/badlogic/pi-mono/pull/492) by [@jsinge97](https://github.com/jsinge97))
|
||||||
|
|
||||||
## [0.37.2] - 2026-01-05
|
## [0.37.2] - 2026-01-05
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@ export interface ProcessedFiles {
|
||||||
export interface ProcessFileOptions {
|
export interface ProcessFileOptions {
|
||||||
/** Whether to auto-resize images to 2000x2000 max. Default: true */
|
/** Whether to auto-resize images to 2000x2000 max. Default: true */
|
||||||
autoResizeImages?: boolean;
|
autoResizeImages?: boolean;
|
||||||
/** When true, skip image files with warning. Default: false */
|
|
||||||
blockImages?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Process @file arguments into text content and image attachments */
|
/** Process @file arguments into text content and image attachments */
|
||||||
|
|
@ -50,11 +48,6 @@ export async function processFileArguments(fileArgs: string[], options?: Process
|
||||||
const mimeType = await detectSupportedImageMimeTypeFromFile(absolutePath);
|
const mimeType = await detectSupportedImageMimeTypeFromFile(absolutePath);
|
||||||
|
|
||||||
if (mimeType) {
|
if (mimeType) {
|
||||||
// Check if images are blocked
|
|
||||||
if (options?.blockImages) {
|
|
||||||
console.warn(chalk.yellow(`[blockImages] Skipping image file: ${absolutePath}`));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Handle image file
|
// Handle image file
|
||||||
const content = await readFile(absolutePath);
|
const content = await readFile(absolutePath);
|
||||||
const base64Content = content.toString("base64");
|
const base64Content = content.toString("base64");
|
||||||
|
|
|
||||||
|
|
@ -592,13 +592,7 @@ export class AgentSession {
|
||||||
// Add user message
|
// Add user message
|
||||||
const userContent: (TextContent | ImageContent)[] = [{ type: "text", text: expandedText }];
|
const userContent: (TextContent | ImageContent)[] = [{ type: "text", text: expandedText }];
|
||||||
if (options?.images) {
|
if (options?.images) {
|
||||||
const blockImages = this.settingsManager.getBlockImages();
|
userContent.push(...options.images);
|
||||||
if (blockImages) {
|
|
||||||
// Log warning for blocked images
|
|
||||||
console.warn(`[blockImages] Blocked ${options.images.length} image(s) from being sent to provider`);
|
|
||||||
} else {
|
|
||||||
userContent.push(...options.images);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
messages.push({
|
messages.push({
|
||||||
role: "user",
|
role: "user",
|
||||||
|
|
|
||||||
|
|
@ -426,9 +426,8 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
||||||
time("discoverContextFiles");
|
time("discoverContextFiles");
|
||||||
|
|
||||||
const autoResizeImages = settingsManager.getImageAutoResize();
|
const autoResizeImages = settingsManager.getImageAutoResize();
|
||||||
const blockImages = settingsManager.getBlockImages();
|
|
||||||
// Create ALL built-in tools for the registry (extensions can enable any of them)
|
// Create ALL built-in tools for the registry (extensions can enable any of them)
|
||||||
const allBuiltInToolsMap = createAllTools(cwd, { read: { autoResizeImages, blockImages } });
|
const allBuiltInToolsMap = createAllTools(cwd, { read: { autoResizeImages } });
|
||||||
// Determine initially active built-in tools (default: read, bash, edit, write)
|
// Determine initially active built-in tools (default: read, bash, edit, write)
|
||||||
const defaultActiveToolNames: ToolName[] = ["read", "bash", "edit", "write"];
|
const defaultActiveToolNames: ToolName[] = ["read", "bash", "edit", "write"];
|
||||||
const initialActiveToolNames: ToolName[] = options.tools
|
const initialActiveToolNames: ToolName[] = options.tools
|
||||||
|
|
@ -607,30 +606,42 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
||||||
const promptTemplates = options.promptTemplates ?? discoverPromptTemplates(cwd, agentDir);
|
const promptTemplates = options.promptTemplates ?? discoverPromptTemplates(cwd, agentDir);
|
||||||
time("discoverPromptTemplates");
|
time("discoverPromptTemplates");
|
||||||
|
|
||||||
// Create convertToLlm wrapper that filters images if blockImages is enabled
|
// Create convertToLlm wrapper that filters images if blockImages is enabled (defense-in-depth)
|
||||||
const convertToLlmWithBlockImages = blockImages
|
const convertToLlmWithBlockImages = (messages: AgentMessage[]): Message[] => {
|
||||||
? (messages: AgentMessage[]): Message[] => {
|
const converted = convertToLlm(messages);
|
||||||
const converted = convertToLlm(messages);
|
// Check setting dynamically so mid-session changes take effect
|
||||||
let totalFiltered = 0;
|
if (!settingsManager.getBlockImages()) {
|
||||||
// Filter out ImageContent from all messages as defense-in-depth
|
return converted;
|
||||||
const filtered = converted.map((msg) => {
|
}
|
||||||
if (msg.role === "user" || msg.role === "toolResult") {
|
// Filter out ImageContent from all messages, replacing with text placeholder
|
||||||
const content = msg.content;
|
return converted.map((msg) => {
|
||||||
if (Array.isArray(content)) {
|
if (msg.role === "user" || msg.role === "toolResult") {
|
||||||
const originalLength = content.length;
|
const content = msg.content;
|
||||||
const filteredContent = content.filter((c) => c.type !== "image");
|
if (Array.isArray(content)) {
|
||||||
totalFiltered += originalLength - filteredContent.length;
|
const hasImages = content.some((c) => c.type === "image");
|
||||||
return { ...msg, content: filteredContent };
|
if (hasImages) {
|
||||||
}
|
const filteredContent = content
|
||||||
|
.map((c) =>
|
||||||
|
c.type === "image" ? { type: "text" as const, text: "Image reading is disabled." } : c,
|
||||||
|
)
|
||||||
|
.filter(
|
||||||
|
(c, i, arr) =>
|
||||||
|
// Dedupe consecutive "Image reading is disabled." texts
|
||||||
|
!(
|
||||||
|
c.type === "text" &&
|
||||||
|
c.text === "Image reading is disabled." &&
|
||||||
|
i > 0 &&
|
||||||
|
arr[i - 1].type === "text" &&
|
||||||
|
(arr[i - 1] as { type: "text"; text: string }).text === "Image reading is disabled."
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return { ...msg, content: filteredContent };
|
||||||
}
|
}
|
||||||
return msg;
|
|
||||||
});
|
|
||||||
if (totalFiltered > 0) {
|
|
||||||
console.warn(`[blockImages] Defense-in-depth: filtered ${totalFiltered} image(s) at convertToLlm layer`);
|
|
||||||
}
|
}
|
||||||
return filtered;
|
|
||||||
}
|
}
|
||||||
: convertToLlm;
|
return msg;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
agent = new Agent({
|
agent = new Agent({
|
||||||
initialState: {
|
initialState: {
|
||||||
|
|
|
||||||
|
|
@ -171,23 +171,23 @@ export class SettingsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private save(): void {
|
private save(): void {
|
||||||
if (!this.persist || !this.settingsPath) return;
|
if (this.persist && this.settingsPath) {
|
||||||
|
try {
|
||||||
|
const dir = dirname(this.settingsPath);
|
||||||
|
if (!existsSync(dir)) {
|
||||||
|
mkdirSync(dir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
// Save only global settings (project settings are read-only)
|
||||||
const dir = dirname(this.settingsPath);
|
writeFileSync(this.settingsPath, JSON.stringify(this.globalSettings, null, 2), "utf-8");
|
||||||
if (!existsSync(dir)) {
|
} catch (error) {
|
||||||
mkdirSync(dir, { recursive: true });
|
console.error(`Warning: Could not save settings file: ${error}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save only global settings (project settings are read-only)
|
|
||||||
writeFileSync(this.settingsPath, JSON.stringify(this.globalSettings, null, 2), "utf-8");
|
|
||||||
|
|
||||||
// Re-merge project settings into active settings
|
|
||||||
const projectSettings = this.loadProjectSettings();
|
|
||||||
this.settings = deepMergeSettings(this.globalSettings, projectSettings);
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Warning: Could not save settings file: ${error}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Always re-merge to update active settings (needed for both file and inMemory modes)
|
||||||
|
const projectSettings = this.loadProjectSettings();
|
||||||
|
this.settings = deepMergeSettings(this.globalSettings, projectSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
getLastChangelogVersion(): string | undefined {
|
getLastChangelogVersion(): string | undefined {
|
||||||
|
|
@ -408,11 +408,6 @@ export class SettingsManager {
|
||||||
this.globalSettings.images = {};
|
this.globalSettings.images = {};
|
||||||
}
|
}
|
||||||
this.globalSettings.images.blockImages = blocked;
|
this.globalSettings.images.blockImages = blocked;
|
||||||
// Also update active settings for inMemory mode
|
|
||||||
if (!this.settings.images) {
|
|
||||||
this.settings.images = {};
|
|
||||||
}
|
|
||||||
this.settings.images.blockImages = blocked;
|
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,10 @@ export interface ReadToolDetails {
|
||||||
export interface ReadToolOptions {
|
export interface ReadToolOptions {
|
||||||
/** Whether to auto-resize images to 2000x2000 max. Default: true */
|
/** Whether to auto-resize images to 2000x2000 max. Default: true */
|
||||||
autoResizeImages?: boolean;
|
autoResizeImages?: boolean;
|
||||||
/** When true, return text message instead of image content. Default: false */
|
|
||||||
blockImages?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createReadTool(cwd: string, options?: ReadToolOptions): AgentTool<typeof readSchema> {
|
export function createReadTool(cwd: string, options?: ReadToolOptions): AgentTool<typeof readSchema> {
|
||||||
const autoResizeImages = options?.autoResizeImages ?? true;
|
const autoResizeImages = options?.autoResizeImages ?? true;
|
||||||
const blockImages = options?.blockImages ?? false;
|
|
||||||
return {
|
return {
|
||||||
name: "read",
|
name: "read",
|
||||||
label: "read",
|
label: "read",
|
||||||
|
|
@ -78,39 +75,29 @@ export function createReadTool(cwd: string, options?: ReadToolOptions): AgentToo
|
||||||
let details: ReadToolDetails | undefined;
|
let details: ReadToolDetails | undefined;
|
||||||
|
|
||||||
if (mimeType) {
|
if (mimeType) {
|
||||||
// Check if images are blocked
|
// Read as image (binary)
|
||||||
if (blockImages) {
|
const buffer = await readFile(absolutePath);
|
||||||
|
const base64 = buffer.toString("base64");
|
||||||
|
|
||||||
|
if (autoResizeImages) {
|
||||||
|
// Resize image if needed
|
||||||
|
const resized = await resizeImage({ type: "image", data: base64, mimeType });
|
||||||
|
const dimensionNote = formatDimensionNote(resized);
|
||||||
|
|
||||||
|
let textNote = `Read image file [${resized.mimeType}]`;
|
||||||
|
if (dimensionNote) {
|
||||||
|
textNote += `\n${dimensionNote}`;
|
||||||
|
}
|
||||||
|
|
||||||
content = [
|
content = [
|
||||||
{
|
{ type: "text", text: textNote },
|
||||||
type: "text",
|
{ type: "image", data: resized.data, mimeType: resized.mimeType },
|
||||||
text: `[Image file detected: ${absolutePath}]\nImage reading is disabled. The 'blockImages' setting is enabled.`,
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
} else {
|
} else {
|
||||||
// Read as image (binary)
|
content = [
|
||||||
const buffer = await readFile(absolutePath);
|
{ type: "text", text: `Read image file [${mimeType}]` },
|
||||||
const base64 = buffer.toString("base64");
|
{ type: "image", data: base64, mimeType },
|
||||||
|
];
|
||||||
if (autoResizeImages) {
|
|
||||||
// Resize image if needed
|
|
||||||
const resized = await resizeImage({ type: "image", data: base64, mimeType });
|
|
||||||
const dimensionNote = formatDimensionNote(resized);
|
|
||||||
|
|
||||||
let textNote = `Read image file [${resized.mimeType}]`;
|
|
||||||
if (dimensionNote) {
|
|
||||||
textNote += `\n${dimensionNote}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
content = [
|
|
||||||
{ type: "text", text: textNote },
|
|
||||||
{ type: "image", data: resized.data, mimeType: resized.mimeType },
|
|
||||||
];
|
|
||||||
} else {
|
|
||||||
content = [
|
|
||||||
{ type: "text", text: `Read image file [${mimeType}]` },
|
|
||||||
{ type: "image", data: base64, mimeType },
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Read as text
|
// Read as text
|
||||||
|
|
|
||||||
|
|
@ -250,7 +250,7 @@ export class SettingsSelectorComponent extends Container {
|
||||||
items.splice(autoResizeIndex + 1, 0, {
|
items.splice(autoResizeIndex + 1, 0, {
|
||||||
id: "block-images",
|
id: "block-images",
|
||||||
label: "Block images",
|
label: "Block images",
|
||||||
description: "Prevent images from being sent to LLM providers (restart session for full effect)",
|
description: "Prevent images from being sent to LLM providers",
|
||||||
currentValue: config.blockImages ? "true" : "false",
|
currentValue: config.blockImages ? "true" : "false",
|
||||||
values: ["true", "false"],
|
values: ["true", "false"],
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -54,42 +54,27 @@ describe("blockImages setting", () => {
|
||||||
rmSync(testDir, { recursive: true, force: true });
|
rmSync(testDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should return text message when blockImages is true", async () => {
|
it("should always read images (filtering happens at convertToLlm layer)", async () => {
|
||||||
// Create test image
|
// Create test image
|
||||||
const imagePath = join(testDir, "test.png");
|
const imagePath = join(testDir, "test.png");
|
||||||
writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
|
writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
|
||||||
|
|
||||||
const tool = createReadTool(testDir, { blockImages: true });
|
const tool = createReadTool(testDir);
|
||||||
const result = await tool.execute("test-1", { path: imagePath });
|
const result = await tool.execute("test-1", { path: imagePath });
|
||||||
|
|
||||||
expect(result.content).toHaveLength(1);
|
|
||||||
expect(result.content[0].type).toBe("text");
|
|
||||||
const textContent = result.content[0] as { type: "text"; text: string };
|
|
||||||
expect(textContent.text).toContain("Image reading is disabled");
|
|
||||||
expect(textContent.text).toContain("blockImages");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should return image content when blockImages is false", async () => {
|
|
||||||
// Create test image
|
|
||||||
const imagePath = join(testDir, "test.png");
|
|
||||||
writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
|
|
||||||
|
|
||||||
const tool = createReadTool(testDir, { blockImages: false });
|
|
||||||
const result = await tool.execute("test-2", { path: imagePath });
|
|
||||||
|
|
||||||
// Should have text note + image content
|
// Should have text note + image content
|
||||||
expect(result.content.length).toBeGreaterThanOrEqual(1);
|
expect(result.content.length).toBeGreaterThanOrEqual(1);
|
||||||
const hasImage = result.content.some((c) => c.type === "image");
|
const hasImage = result.content.some((c) => c.type === "image");
|
||||||
expect(hasImage).toBe(true);
|
expect(hasImage).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should read text files normally even when blockImages is true", async () => {
|
it("should read text files normally", async () => {
|
||||||
// Create test text file
|
// Create test text file
|
||||||
const textPath = join(testDir, "test.txt");
|
const textPath = join(testDir, "test.txt");
|
||||||
writeFileSync(textPath, "Hello, world!");
|
writeFileSync(textPath, "Hello, world!");
|
||||||
|
|
||||||
const tool = createReadTool(testDir, { blockImages: true });
|
const tool = createReadTool(testDir);
|
||||||
const result = await tool.execute("test-3", { path: textPath });
|
const result = await tool.execute("test-2", { path: textPath });
|
||||||
|
|
||||||
expect(result.content).toHaveLength(1);
|
expect(result.content).toHaveLength(1);
|
||||||
expect(result.content[0].type).toBe("text");
|
expect(result.content[0].type).toBe("text");
|
||||||
|
|
@ -110,35 +95,23 @@ describe("blockImages setting", () => {
|
||||||
rmSync(testDir, { recursive: true, force: true });
|
rmSync(testDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should skip image files when blockImages is true", async () => {
|
it("should always process images (filtering happens at convertToLlm layer)", async () => {
|
||||||
// Create test image
|
// Create test image
|
||||||
const imagePath = join(testDir, "test.png");
|
const imagePath = join(testDir, "test.png");
|
||||||
writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
|
writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
|
||||||
|
|
||||||
const result = await processFileArguments([imagePath], { blockImages: true });
|
const result = await processFileArguments([imagePath]);
|
||||||
|
|
||||||
expect(result.images).toHaveLength(0);
|
|
||||||
// Text should be empty since image was skipped
|
|
||||||
expect(result.text).toBe("");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should include image files when blockImages is false", async () => {
|
|
||||||
// Create test image
|
|
||||||
const imagePath = join(testDir, "test.png");
|
|
||||||
writeFileSync(imagePath, Buffer.from(TINY_PNG_BASE64, "base64"));
|
|
||||||
|
|
||||||
const result = await processFileArguments([imagePath], { blockImages: false });
|
|
||||||
|
|
||||||
expect(result.images).toHaveLength(1);
|
expect(result.images).toHaveLength(1);
|
||||||
expect(result.images[0].type).toBe("image");
|
expect(result.images[0].type).toBe("image");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should process text files normally when blockImages is true", async () => {
|
it("should process text files normally", async () => {
|
||||||
// Create test text file
|
// Create test text file
|
||||||
const textPath = join(testDir, "test.txt");
|
const textPath = join(testDir, "test.txt");
|
||||||
writeFileSync(textPath, "Hello, world!");
|
writeFileSync(textPath, "Hello, world!");
|
||||||
|
|
||||||
const result = await processFileArguments([textPath], { blockImages: true });
|
const result = await processFileArguments([textPath]);
|
||||||
|
|
||||||
expect(result.images).toHaveLength(0);
|
expect(result.images).toHaveLength(0);
|
||||||
expect(result.text).toContain("Hello, world!");
|
expect(result.text).toContain("Hello, world!");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue