UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

87 lines (86 loc) • 4.91 kB
/** * 批③ image-pipeline — original vs displayed size of a processed image (CC `imageResizer.ts` * `ImageDimensions`). `display*` = what the model actually sees; the Read tool renders the CC coordinate- * mapping meta text from the ratio, so vision coordinate reasoning survives a downsample. */ export interface ImageDimensions { originalWidth: number; originalHeight: number; displayWidth: number; displayHeight: number; } /** Result of {@link ImageDownsampler}: re-encoded (or passed-through) image + dimensions when known. */ export interface DownsampledImage { base64: string; /** Full mime form (`image/jpeg`), ready for an ImageContent block. */ mimeType: string; dimensions?: ImageDimensions; } /** * An image processor: the bytes the admission checked, in; the bytes the model gets, out — or undefined when the input * can't be processed (corrupt/unsupported), and the admission then judges the original bytes as they are. A * within-target image passes through UNCHANGED (with dimensions when known) — pass-through is not a failure. */ export type ImageDownsampler = (input: Buffer, mimeType: string) => Promise<DownsampledImage | undefined>; /** Minimal structural slice of the sharp API the pipeline uses (fresh instance per operation — CC note: * reusing an instance after toBuffer() silently skips format conversions on some native builds). */ type SharpFactory = (input: Buffer) => SharpOps; interface SharpOps { metadata: () => Promise<{ width?: number; height?: number; format?: string; orientation?: number; }>; rotate: () => SharpOps; resize: (w: number, h: number, o: { fit: "inside"; withoutEnlargement: boolean; }) => SharpOps; jpeg: (o: { quality: number; }) => SharpOps; png: (o: { compressionLevel: number; palette: boolean; }) => SharpOps; flatten: (o: { background: string; }) => SharpOps; toBuffer: () => Promise<Buffer>; } /** * The processor over an injected sharp factory (so both stages are unit-testable without the native dependency). Two * stages, in the order of `VS` (cli309.js:262027), after a frame that asks to be shown turned is turned upright: * 1. the frame ladder (CC `IGe`, called by FileReadTool `readImageWithTokenBudget`), toward `soft.frameEdge` and * `soft.rawBytes`: * a. fits already (raw ≤ rawBytes AND inside the frame) → pass through, dimensions attached; * b. dims fit but bytes over → full-resolution compression first (PNG lossless/palette for PNGs, then * JPEG quality ladder 80/60/40/20) — preserves resolution when possible; * c. dims over → constrain to the frame (aspect kept), then the same ladder on the resized image if still over; last * rung = ≤1000px wide JPEG q20; * d. metadata without dimensions → JPEG q80 when over rawBytes, else pass through (CC branch); * 2. the byte-target step ({@link toByteTarget}) when the ladder's bytes are over `soft.targetBytes`. A failure of this * step keeps the ladder's bytes (CC: "passing through unbudgeted"). * An image already inside the frame and at or under the target passes both stages byte-identical, so a second * admission of what the first one sent changes nothing. The caller's base64 gate stays the final arbiter. */ export declare function createImageDownsampler(sharp: SharpFactory): ImageDownsampler; /** * Production processor factory: dynamic `import("sharp")`. sharp is an OPTIONAL native dependency — declared under * `optionalDependencies`, never `dependencies` (arch decision, design/116 W5-1: core won't force a native module on * every consumer; RB-213 made the optional-ness VISIBLE to package managers without changing the decision — * `optionalDependencies` still installs best-effort and never fails the parent install if the native build fails). A * turnkey deployment that has it available gets the CC experience automatically; without it this resolves to undefined. */ export declare function sharpImageDownsampler(): Promise<ImageDownsampler | undefined>; /** The seat's value door (at `new Runner`): a processor, `false`, or absent — anything else is refused by name rather than read * as "detect" or "none" (`config.invalid_argument`). */ export declare function checkedImageProcessorSeat(value: unknown, seat: string): void; /** * The one reading of the image processor seat (`RunnerDeps.imageProcessor`): a function is the processor; `false` is * `"none"` (nothing is detected); absent detects sharp once per process ({@link sharpImageDownsampler}) and answers * `"none"` when it does not resolve. Every carrier that admits an image reads its processor here. */ export declare function resolveImageProcessor(seat: ImageDownsampler | false | undefined): Promise<ImageDownsampler | "none">; export {};