@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
77 lines • 3.64 kB
TypeScript
/**
* The `media_placeholders` transform (one choke point, not five).
*
* A client that advertised `MEDIA_PLACEHOLDERS_CAPABILITY` must never receive
* `{type:"image", data:<base64>}` inside a tool result — four inline images once
* overflowed a 4 MiB socket queue and froze the desktop. Rather than patching each
* wire path that can carry a tool result (`message_start`/`message_end`,
* `turn_end.toolResults`, `agent_end`, `entry_appended`, and the `get_messages`,
* `get_entries`, `get_tree`, `get_state`, `open_session` responses — a list that has
* already been wrong once), every record is funnelled through this pure transform at
* `SessionEventWriter.enqueue`.
*
* Two hard rules:
* - Type-gate the media walk. JSON-value normalization is shared with writer
* acceptance; media projection itself never walks `message_update` partials.
* - Copy-on-write. Responses hand out live agent state by reference
* (`success(id, "get_messages", { messages: session.messages })`), so the input graph
* is never mutated and unchanged JSON sub-trees keep their identity. When nothing changed
* the SAME record reference comes back, which lets the caller reuse the line it
* already serialized.
*
* User-authored images (`prompt.images`) stay intact: they are bounded by the client
* that sent them; the fan-out hazard is tool output.
*/
type RpcRecord = Record<string, unknown>;
/** Why a host could not keep a tool image on disk for the client to read. */
export type MediaUnavailableReason = "image_too_large" | "session_limit" | "storage_error";
/** Where a persisted image lives (absolute), or why there is no file. */
export type PersistedMedia = {
readonly path: string;
} | {
readonly unavailableReason: MediaUnavailableReason;
};
/**
* Writes one image's bytes to the session's media directory and reports the outcome. Returns
* `undefined` for a session that has nowhere durable to keep them: its placeholder carries neither field.
*/
export type MediaPersister = (ref: {
readonly toolCallId: string;
readonly contentIndex: number;
}, block: {
readonly data: string;
readonly mimeType?: string;
}) => PersistedMedia | undefined;
/** The block a capable client receives in place of inline image bytes. */
export interface ImageRefBlock {
readonly type: "image_ref";
readonly mimeType: string;
readonly byteLength: number;
readonly ref: {
readonly toolCallId: string;
readonly contentIndex: number;
};
/** Absolute path of the stored bytes; written before this placeholder was emitted. */
readonly path?: string;
readonly unavailableReason?: MediaUnavailableReason;
}
/**
* Decoded byte length of a base64 string, derived from its length alone.
*
* Decoding a multi-MiB payload just to report its size would defeat the purpose of
* omitting it.
*/
export declare function base64ByteLength(data: string): number;
/** True when this record type can carry a tool result at all (cheap gate). */
export declare function carriesMedia(record: RpcRecord): boolean;
/**
* Replace inline tool-result images with `image_ref` placeholders.
*
* Returns the SAME reference when nothing changed, so the caller can skip the second
* serialization entirely.
*/
export declare function omitInlineMedia<T extends object>(record: T, persist?: MediaPersister): T;
/** `omitInlineMedia` for a value that is already JSON-normal: type gate first, no normalization walk. */
export declare function omitNormalInlineMedia<T extends object>(record: T, persist?: MediaPersister): T;
export {};
//# sourceMappingURL=media-placeholders.d.ts.map