| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import type { ConversationMessage } from '@automaker/types'; |
|
|
| |
| |
| |
| |
| |
| |
| export function extractTextFromContent( |
| content: string | Array<{ type: string; text?: string; source?: object }> |
| ): string { |
| if (typeof content === 'string') { |
| return content; |
| } |
|
|
| |
| return content |
| .filter((block) => block.type === 'text') |
| .map((block) => block.text || '') |
| .join('\n'); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function normalizeContentBlocks( |
| content: string | Array<{ type: string; text?: string; source?: object }> |
| ): Array<{ type: string; text?: string; source?: object }> { |
| if (Array.isArray(content)) { |
| return content; |
| } |
| return [{ type: 'text', text: content }]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function formatHistoryAsText(history: ConversationMessage[]): string { |
| if (history.length === 0) { |
| return ''; |
| } |
|
|
| let historyText = 'Previous conversation:\n\n'; |
|
|
| for (const msg of history) { |
| const contentText = extractTextFromContent(msg.content); |
| const role = msg.role === 'user' ? 'User' : 'Assistant'; |
| historyText += `${role}: ${contentText}\n\n`; |
| } |
|
|
| historyText += '---\n\n'; |
| return historyText; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function convertHistoryToMessages(history: ConversationMessage[]): Array<{ |
| type: 'user' | 'assistant'; |
| session_id: string; |
| message: { |
| role: 'user' | 'assistant'; |
| content: Array<{ type: string; text?: string; source?: object }>; |
| }; |
| parent_tool_use_id: null; |
| }> { |
| return history.map((historyMsg) => ({ |
| type: historyMsg.role, |
| session_id: '', |
| message: { |
| role: historyMsg.role, |
| content: normalizeContentBlocks(historyMsg.content), |
| }, |
| parent_tool_use_id: null, |
| })); |
| } |
|
|