Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159/** Message extraction helpers for child Pi result forwarding. @module */
//region Types
/** Minimal text block shape used by Pi assistant messages. */type TextBlockLike = { /** Content block discriminator. */ readonly type: 'text'; /** Text emitted by assistant. */ readonly text: string;};
/** Minimal assistant message shape needed by spawn-pi. */type AssistantMessageLike = { /** Message role discriminator. */ readonly role: 'assistant'; /** Assistant content blocks or legacy plain text. */ readonly content: string | readonly unknown[];};
//endregion Types
//region Type guards
/** Detects text content blocks in unknown Pi message content. @param value - content block candidate. @returns whether value is text block. @example ```typescript isTextBlock({ type: 'text', text: 'done' }); ``` */function isTextBlock(value: unknown,): value is TextBlockLike { if ((value === null) || ((typeof value) !== 'object')) return false; return ('type' in value) && (value.type === 'text') && ('text' in value) && ((typeof value.text) === 'string');}
/** Detects assistant messages in unknown Pi message arrays. @param value - message candidate. @returns whether value is assistant message. @example ```typescript isAssistantMessage({ role: 'assistant', content: [] }); ``` */function isAssistantMessage(value: unknown,): value is AssistantMessageLike { if ((value === null) || ((typeof value) !== 'object')) return false; return ('role' in value) && (value.role === 'assistant') && ('content' in value) && (((typeof value.content) === 'string') || Array.isArray(value.content,));}
//endregion Type guards
//region Extraction
/** Extracts text from assistant message content. @param content - {@link AssistantMessageLike} content. @returns joined text blocks, or content itself for legacy string content. @example ```typescript assistantContentText([{ type: 'text', text: 'done' }]); ``` */function assistantContentText(content: AssistantMessageLike['content'],): string { if ((typeof content) === 'string') return content;
return content .filter(function keepTextBlock(block,): block is TextBlockLike { return isTextBlock(block,); },) .map(function blockText(block,): string { return block.text; },) .join('\n',);}
/** Extracts last assistant text from Pi agent-end messages, using {@link isAssistantMessage} to filter candidates. @param messages - messages emitted by agent loop. @returns last assistant text, or empty string when no text exists. @example ```typescript extractLastAssistantText([{ role: 'assistant', content: [{ type: 'text', text: 'done' }] }]); ``` */function extractLastAssistantText(messages: readonly unknown[],): string { for (let index = messages.length - 1; index >= 0; index -= 1) { /** Candidate message inspected from newest to oldest. */ const message = messages[index]; if (!isAssistantMessage(message,)) continue;
/** Text content emitted by candidate assistant message. */ const text = assistantContentText(message.content,); if (text.length > 0) return text; }
return '';}
//endregion Extraction
export { assistantContentText, extractLastAssistantText, isAssistantMessage, isTextBlock,};
export type { AssistantMessageLike, TextBlockLike,};