Skip to content

Commit 9cf067a

Browse files
committed
feat: add image optimizer plugin to prevent oversized image API errors
Vendor a simplified zero-config image optimizer from https://github.com/kargnas/opencode-large-image-optimizer into kimaki's OpenCode plugin system. Prevents 'image dimensions exceed max allowed' errors that crash sessions permanently (Anthropic rejects >2000px in multi-image requests, and the oversized image persists in history breaking all subsequent messages). Plugin hooks into: - tool.execute.after (read tool attachments) - experimental.chat.messages.transform (clipboard paste file parts) Key design decisions: - Lazy import('sharp') so missing sharp doesn't break all other plugins - 2000px conservative floor (Anthropic many-image limit, images accumulate across turns in OpenCode history) - 4MB file size cap with progressive JPEG quality reduction - Zero config, always on for all providers - No logging (plugins must be silent per OpenCode plugin contract) Also adds 'no console.* in plugins' rule to KIMAKI_AGENTS.md. Related: anomalyco/opencode#12068, anomalyco/opencode#19525
1 parent 1c5edc9 commit 9cf067a

4 files changed

Lines changed: 199 additions & 0 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ the plugin does NOT receive `KIMAKI_BOT_TOKEN`. discord REST operations (user li
429429

430430
when adding new bot-side config that the plugin needs, add it as a `KIMAKI_*` env var in `opencode.ts` spawn env and read `process.env.KIMAKI_*` in the plugin. never import config.ts getters in the plugin.
431431

432+
**NEVER use `console.log`, `console.error`, or any `console.*` in plugin code.** opencode captures plugin stdout/stderr and it pollutes the opencode server output, breaking structured logging. plugins must be silent — fail gracefully and return null/undefined on errors instead of logging.
433+
432434
## skills folder
433435

434436
skills is a symlink to discord/skills. this is a folder of skills for kimaki. loaded by all kimaki users. some skills are synced from github repos. see discord/scripts/sync-skills.ts. so never manually update them. instead if need to updaste them start kimaki threads on those project, found via kimaki cli.

‎KIMAKI_AGENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ the plugin does NOT receive `KIMAKI_BOT_TOKEN`. discord REST operations (user li
427427

428428
when adding new bot-side config that the plugin needs, add it as a `KIMAKI_*` env var in `opencode.ts` spawn env and read `process.env.KIMAKI_*` in the plugin. never import config.ts getters in the plugin.
429429

430+
**NEVER use `console.log`, `console.error`, or any `console.*` in plugin code.** opencode captures plugin stdout/stderr and it pollutes the opencode server output, breaking structured logging. plugins must be silent — fail gracefully and return null/undefined on errors instead of logging.
431+
430432
## skills folder
431433

432434
skills is a symlink to discord/skills. this is a folder of skills for kimaki. loaded by all kimaki users. some skills are synced from github repos. see discord/scripts/sync-skills.ts. so never manually update them. instead if need to updaste them start kimaki threads on those project, found via kimaki cli.
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Optimizes oversized images before they reach the LLM API.
2+
// Prevents "image dimensions exceed max allowed" errors from Anthropic/Google/OpenAI.
3+
// Hooks into tool.execute.after (read) and experimental.chat.messages.transform (clipboard paste).
4+
// Uses sharp to resize images > 2000px and compress images > 4MB.
5+
// Vendored from https://github.com/kargnas/opencode-large-image-optimizer, simplified to zero-config.
6+
7+
import type { Plugin } from '@opencode-ai/plugin'
8+
9+
// Conservative safe floor for Anthropic many-image requests (20+ images = 2000px limit).
10+
// OpenCode resends history so image counts accumulate across turns — 2000px is safest.
11+
const MAX_DIMENSION = 2000
12+
// 4MB safe margin under Anthropic's 5MB limit
13+
const MAX_FILE_SIZE = 4 * 1024 * 1024
14+
const SUPPORTED_MIMES = new Set([
15+
'image/png',
16+
'image/jpeg',
17+
'image/jpg',
18+
'image/gif',
19+
'image/webp',
20+
])
21+
22+
// sharp is an optionalDependency — lazy-load to avoid breaking all plugins if missing
23+
type SharpFn = (input?: Buffer | string) => import('sharp').Sharp
24+
25+
let sharpFactory: SharpFn | null | undefined
26+
27+
async function getSharp(): Promise<SharpFn | null> {
28+
if (sharpFactory !== undefined) {
29+
return sharpFactory
30+
}
31+
try {
32+
const mod = await import('sharp')
33+
// sharp uses `export =` so it lands on .default in ESM interop
34+
const fn = typeof mod === 'function' ? mod : (mod as { default: SharpFn }).default
35+
if (typeof fn === 'function') {
36+
sharpFactory = fn
37+
} else {
38+
sharpFactory = null
39+
}
40+
} catch {
41+
sharpFactory = null
42+
}
43+
return sharpFactory
44+
}
45+
46+
function extractBase64Data(dataUrl: string): string | null {
47+
const match = dataUrl.match(/^data:[^;]+;base64,(.+)$/s)
48+
if (match?.[1]) {
49+
return match[1]
50+
}
51+
// raw base64 string (no data: prefix)
52+
if (/^[A-Za-z0-9+/]+={0,2}$/.test(dataUrl)) {
53+
return dataUrl
54+
}
55+
return null
56+
}
57+
58+
interface OptimizeResult {
59+
dataUrl: string
60+
mime: string
61+
}
62+
63+
async function optimizeImage(
64+
dataUrl: string,
65+
mime: string,
66+
): Promise<OptimizeResult | null> {
67+
const sharp = await getSharp()
68+
if (!sharp) {
69+
return null
70+
}
71+
72+
const rawBase64 = extractBase64Data(dataUrl)
73+
if (!rawBase64) {
74+
return null
75+
}
76+
77+
const inputBuffer = Buffer.from(rawBase64, 'base64')
78+
if (inputBuffer.length === 0) {
79+
return null
80+
}
81+
82+
const metadata = await sharp(inputBuffer).metadata()
83+
const width = metadata.width || 0
84+
const height = metadata.height || 0
85+
if (width === 0 || height === 0) {
86+
return null
87+
}
88+
89+
const needsResize = width > MAX_DIMENSION || height > MAX_DIMENSION
90+
const needsCompress = inputBuffer.length > MAX_FILE_SIZE
91+
if (!needsResize && !needsCompress) {
92+
return null
93+
}
94+
95+
let pipeline = sharp(inputBuffer)
96+
let outputMime = mime
97+
98+
if (needsResize) {
99+
pipeline = pipeline.resize(MAX_DIMENSION, MAX_DIMENSION, {
100+
fit: 'inside',
101+
withoutEnlargement: true,
102+
})
103+
}
104+
105+
let outputBuffer = await pipeline.toBuffer()
106+
107+
// if still over 4MB, convert to JPEG with progressive quality reduction
108+
if (outputBuffer.length > MAX_FILE_SIZE) {
109+
for (const quality of [100, 90, 80, 70]) {
110+
outputBuffer = await sharp(outputBuffer)
111+
.jpeg({ quality, mozjpeg: true })
112+
.toBuffer()
113+
outputMime = 'image/jpeg'
114+
if (outputBuffer.length <= MAX_FILE_SIZE) {
115+
break
116+
}
117+
}
118+
}
119+
120+
return {
121+
dataUrl: `data:${outputMime};base64,${outputBuffer.toString('base64')}`,
122+
mime: outputMime,
123+
}
124+
}
125+
126+
// runtime guard — tool.execute.after output type doesn't declare attachments
127+
function hasAttachments(
128+
value: unknown,
129+
): value is { attachments: Array<{ mime?: string; url?: string }> } {
130+
return (
131+
typeof value === 'object' &&
132+
value !== null &&
133+
'attachments' in value &&
134+
Array.isArray((value as { attachments?: unknown }).attachments)
135+
)
136+
}
137+
138+
const imageOptimizerPlugin: Plugin = async () => {
139+
return {
140+
'tool.execute.after': async (input, output) => {
141+
const tool = input.tool.toLowerCase()
142+
143+
// read tool: optimize image attachments
144+
if (tool === 'read' && hasAttachments(output)) {
145+
for (const att of output.attachments) {
146+
if (
147+
!att.mime ||
148+
!att.url ||
149+
!SUPPORTED_MIMES.has(att.mime.toLowerCase())
150+
) {
151+
continue
152+
}
153+
const result = await optimizeImage(att.url, att.mime).catch(
154+
() => null,
155+
)
156+
if (result) {
157+
att.url = result.dataUrl
158+
att.mime = result.mime
159+
}
160+
}
161+
}
162+
163+
},
164+
165+
// clipboard paste: optimize file parts in message history
166+
'experimental.chat.messages.transform': async (_input, output) => {
167+
if (!output.messages || !Array.isArray(output.messages)) {
168+
return
169+
}
170+
for (const msg of output.messages) {
171+
if (!msg.parts || !Array.isArray(msg.parts)) {
172+
continue
173+
}
174+
for (const part of msg.parts) {
175+
if (part.type !== 'file') {
176+
continue
177+
}
178+
if (!SUPPORTED_MIMES.has(part.mime.toLowerCase())) {
179+
continue
180+
}
181+
const result = await optimizeImage(part.url, part.mime).catch(
182+
() => null,
183+
)
184+
if (result) {
185+
part.url = result.dataUrl
186+
part.mime = result.mime
187+
}
188+
}
189+
}
190+
},
191+
}
192+
}
193+
194+
export { imageOptimizerPlugin }

‎discord/src/kimaki-opencode-plugin.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export { ipcToolsPlugin } from './ipc-tools-plugin.js'
1313
export { contextAwarenessPlugin } from './context-awareness-plugin.js'
1414
export { interruptOpencodeSessionOnUserMessage } from './opencode-interrupt-plugin.js'
1515
export { anthropicAuthPlugin } from './anthropic-auth-plugin.js'
16+
export { imageOptimizerPlugin } from './image-optimizer-plugin.js'
1617
export { kittyGraphicsPlugin } from 'kitty-graphics-agent'
1718
export { injectionGuardInternal as injectionGuard } from 'opencode-injection-guard'

0 commit comments

Comments
 (0)