export function messageToChatMessage()

in server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/chatDb/util.ts [100:126]


export function messageToChatMessage(msg: Message): ChatMessage[] {
    const chatMessages: ChatMessage[] = [
        {
            body: msg.body,
            type: msg.type,
            codeReference: msg.codeReference,
            relatedContent:
                msg.relatedContent && msg.relatedContent?.content.length > 0 ? msg.relatedContent : undefined,
        },
    ]

    // Check if there are any toolUses with explanations that should be displayed as directive messages
    if (msg.toolUses && msg.toolUses.length > 0) {
        for (const toolUse of msg.toolUses) {
            if (toolUse.input && typeof toolUse.input === 'object') {
                const input = toolUse.input as any
                if (input.explanation) {
                    chatMessages.push({
                        body: input.explanation,
                        type: 'directive',
                    })
                }
            }
        }
    }
    return chatMessages
}