/**
* Custom message types and transformers for the coding agent.
*
* Extends the base AgentMessage type with coding-agent specific message types,
* and provides a transformer to convert them to LLM-compatible messages.
*/
import type { ImageContent, Message, TextContent, ToolResultMessage } from "@bastani/pi-ai/compat";
import type { AgentMessage } from "@earendil-works/pi-agent-core";
export declare const BRANCH_SUMMARY_PREFIX = "The following is a summary of a branch that this conversation came back from:\n\n\n";
export declare const BRANCH_SUMMARY_SUFFIX = "";
/**
* Message type for bash executions via the ! command.
*/
export interface BashExecutionMessage {
role: "bashExecution";
command: string;
output: string;
exitCode: number | undefined;
cancelled: boolean;
truncated: boolean;
fullOutputPath?: string;
timestamp: number;
/** If true, this message is excluded from LLM context (!! prefix) */
excludeFromContext?: boolean;
}
/**
* Message type for extension-injected messages via sendMessage().
* These are custom messages that extensions can inject into the conversation.
*/
export interface CustomMessage {
role: "custom";
customType: string;
content: string | (TextContent | ImageContent)[];
display: boolean;
details?: T;
timestamp: number;
}
export type StageAdmittedCustomMessage = CustomMessage & {
/** Stable producer identity used for exactly-once workflow-stage admission. */
stageAdmissionKey?: string;
};
export interface BranchSummaryMessage {
role: "branchSummary";
summary: string;
fromId: string | null;
timestamp: number;
}
declare module "@earendil-works/pi-agent-core" {
interface CustomAgentMessages {
bashExecution: BashExecutionMessage;
custom: CustomMessage;
branchSummary: BranchSummaryMessage;
}
}
/** Normalize lax JavaScript/legacy message input before it reaches history or providers. */
export declare function normalizeMessageContent(message: T): T;
export declare function userLikeContentBlockIsLlmVisible(block: unknown): boolean;
/** Whether user/custom content survives provider conversion as a visible input. */
export declare function userLikeContentIsLlmVisible(content: unknown, deletedBlockIndexes?: ReadonlySet): boolean;
/** Whether an Atomic message emits an LLM-visible user-like turn boundary. */
export declare function messageStartsLlmUserTurn(message: AgentMessage, deletedBlockIndexes?: ReadonlySet): boolean;
/** Whether an Atomic message survives conversion into provider-visible context. */
export declare function messageIsLlmVisible(message: AgentMessage, deletedBlockIndexes?: ReadonlySet): boolean;
/**
* Convert a BashExecutionMessage to user message text for LLM context.
*/
export declare function bashExecutionToText(msg: BashExecutionMessage): string;
export declare const VERBATIM_COMPACTION_PREFIX = "The earlier conversation was compacted. Below is the verbatim transcript of the retained lines; elided spans are marked \"(filtered N lines)\".\n\n";
export declare const SUMMARY_COMPACTION_PREFIX = "The following is a summary of the earlier conversation:\n\n";
export declare function isVerbatimCompactionMessage(message: CustomMessage): boolean;
/**
* Create the visible custom-role boundary used to replay a verbatim compaction.
*
* `retainedTail` holds the serialized kept tail. Its text merges into the boundary text and
* its image blocks stay intact, so preserved content is not reduced to a placeholder.
*/
export declare function createVerbatimCompactionMessage(compactedText: string, tokensBefore: number, timestamp: string, details?: unknown, retainedTail?: readonly (TextContent | ImageContent)[]): CustomMessage;
export declare function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage;
/** Convert CustomMessageEntry to AgentMessage format */
export declare function createCustomMessage(customType: string, content: string | (TextContent | ImageContent)[], display: boolean, details: unknown | undefined, timestamp: string, excludeFromContext?: boolean, stageAdmissionKey?: string): StageAdmittedCustomMessage;
/**
* Keep every tool result adjacent to the assistant turn that announced it.
*
* A message admitted while a tool batch is still running (an Intercom delivery, a
* workflow lifecycle card, a durable stage admission) is persisted between the tool
* call and its result in raw history. The provider requires the results to follow the
* call directly, so derived context defers such messages until the batch is answered.
* The durable transcript keeps its admission order.
*/
export declare function deferMessagesInterleavedWithToolBatch(messages: TMessage[]): TMessage[];
/**
* Enforce the provider's tool-pairing invariant on a converted message list.
*
* A provider rejects orphaned/duplicate `tool_result` blocks and any later turn
* that crosses an unanswered `tool_use`. Keep exactly the first result for each
* announced id. If a persisted session continues after its engine died before
* recording every result, insert provider-only error results before that later
* turn. The repair exists only in derived context; the durable transcript stays
* unchanged, and the text makes no claim about whether admitted work completed
* or produced side effects.
*/
export interface ToolCallPairRepairOptions {
/** Close a final unanswered tool call when reopening an inactive session. */
repairTrailing?: boolean;
}
export declare function repairOrphanToolResults(messages: TMessage[], options?: ToolCallPairRepairOptions): (TMessage | ToolResultMessage)[];
/**
* Transform AgentMessages (including custom types) to LLM-compatible Messages.
*
* This is used by:
* - Agent's transormToLlm option (for prompt calls and queued messages)
* - Branch summarization (for summarizing abandoned branches)
* - Custom extensions and tools
*/
export declare function convertToLlm(messages: AgentMessage[]): Message[];
//# sourceMappingURL=messages.d.ts.map