1
/**2
* Image offload executor for the compaction seam. When an image-capable route3
* fails a request with `IMAGE_OFFLOAD_REQUIRED`, the plugin records one4
* `image/offload` decision selecting the oldest retained input occurrences5
* and retries through the agent or compaction summary error waterfall. Every route6
* sends placeholder text for those occurrences in subsequent requests.7
*8
* @module @deepseek-ai/dsh-compaction-image-offload9
*/11
import type { Context } from '@deepseek-ai/cordis'12
import type {} from '@deepseek-ai/dsh-compaction'13
import type { RequestErrorAction } from '@deepseek-ai/dsh-agent'14
import { IMAGE_OFFLOAD_REQUIRED_CODE, LlmError } from '@deepseek-ai/dsh-llm'15
import { offloadOldestImages } from './image-offload.ts'16
import { imageOffloadProjection } from './projection.ts'18
export const name = 'compaction-image-offload'19
export const inject = ['agents', 'sessions']21
/**22
* Mount agent and summary recovery listeners without configuration.23
* @param ctx - the plugin context.24
*/25
export function apply(ctx: Context): void {26
ctx.sessions.registerMessageProjection(imageOffloadProjection)27
ctx.on('agent/request-error', ({ agent, failure }, next): Promise<RequestErrorAction> => {28
if (failure.code !== IMAGE_OFFLOAD_REQUIRED_CODE || failure.offloadImages === undefined) return next()29
// A durable surface repair, not a provider retry: it spends no retry budget and logs no retry event.30
if (!offloadOldestImages(agent.session, agent.session.surface.nodes, failure.offloadImages)) return next()31
return Promise.resolve<RequestErrorAction>({ kind: 'retry' })32
})33
ctx.on('compaction/summary-error', ({ session, sourceEventSeqs, error, signal }, next) => {34
if (!(error instanceof LlmError)35
|| error.code !== IMAGE_OFFLOAD_REQUIRED_CODE36
|| error.failure.offloadImages === undefined) return next()37
signal?.throwIfAborted()38
if (!offloadOldestImages(session, sourceEventSeqs, error.failure.offloadImages)) return next()39
return true40
})41
}