import { Boom } from '@hapi/boom'
import { generateMessageIDV2 } from './generics.js'
import { buildRichContextInfo, tokenizeCode } from './message-composer.js'
const LAYOUT_SINGLE = 'GenAISingleLayoutViewModel'
const T_MARKDOWN = 'GenAIMarkdownTextUXPrimitive'
const T_CODE = 'GenAICodeUXPrimitive'
const T_TABLE = 'GenAITableUXPrimitive'
export const genAIMarkdownPrimitive = (text) => ({
text: String(text ?? ''),
__typename: T_MARKDOWN
})
export const genAICodePrimitive = (code, language = 'javascript') => ({
language,
code_blocks: tokenizeCode(String(code ?? ''), language),
__typename: T_CODE
})
export const genAITablePrimitive = (rows) => ({
rows: (rows ?? []).map((row) => ({
cells: Array.isArray(row)
? row.map((c) => ({ text: String(c ?? '') }))
: (row.cells ?? []).map((c) => ({ text: String(c?.text ?? c ?? '') }))
})),
__typename: T_TABLE
})
const singleLayout = (primitive) => ({
view_model: { primitive, __typename: LAYOUT_SINGLE }
})
export const genAIMarkdownSection = (text) => singleLayout(genAIMarkdownPrimitive(text))
export const genAICodeSection = (code, language = 'javascript') => singleLayout(genAICodePrimitive(code, language))
export const genAITableSection = (rows) => singleLayout(genAITablePrimitive(rows))
export const buildGenAIRichMessage = (sections, options = {}) => {
const { title, footer, quoted, mentions, botJid } = options
const all = []
if (title) all.push(genAIMarkdownSection(title))
all.push(...(sections ?? []))
if (footer) all.push(genAIMarkdownSection(footer))
if (all.length === 0) {
throw new Boom('buildGenAIRichMessage: no sections to send', { statusCode: 400 })
}
const ctxInfo = buildRichContextInfo(quoted, { mentions, botJid })
const richResponse = { sections: all }
const message = {
botForwardedMessage: {
message: { richResponse },
contextInfo: ctxInfo
}
}
return { message, messageId: generateMessageIDV2() }
}
const serverSupportsRich = (sock) => {
try {
const props = sock?.serverProps ?? {}
return props.richResponse === true || props.richResponses === true || props.genAIRichResponse === true
} catch {
return false
}
}
export async function sendRichReport(sock, jid, report, opts = {}) {
if (!sock) throw new Boom('sendRichReport: missing socket', { statusCode: 400 })
if (!jid) throw new Boom('sendRichReport: missing jid', { statusCode: 400 })
if (!report || (!report.body && !report.codeBlocks?.length && !report.tables?.length)) {
throw new Boom('sendRichReport: empty report', { statusCode: 400 })
}
const { title, body, codeBlocks = [], tables = [], footer } = report
const { quoted, mentions, botJid, forceRich = false } = opts
if (forceRich || serverSupportsRich(sock)) {
try {
const sections = []
if (body) sections.push(genAIMarkdownSection(body))
for (const cb of codeBlocks) {
sections.push(genAICodeSection(cb.code, cb.language ?? 'javascript'))
}
for (const rows of tables) {
sections.push(genAITableSection(rows))
}
const { message, messageId } = buildGenAIRichMessage(sections, {
title,
footer,
quoted,
mentions,
botJid
})
await sock.relayMessage(jid, message, { messageId })
return { messageId, mode: 'rich' }
} catch (err) {
sock?.logger?.warn({ err: err?.message, jid }, 'sendRichReport: rich failed, falling back to text')
}
}
const parts = []
if (title) parts.push(`*${title}*`)
if (body) parts.push(body)
for (const cb of codeBlocks) {
parts.push('
' + (cb.language ?? 'javascript') + '\n' + cb.code + '\n
`')
}
for (const rows of tables) {
const lines =
rows.map((r) => '| ' +
r.map(String).join(' | ') + ' |')
if (lines.length) {
const header = lines[0]
const sep = '|' + rows[0].map(() => ' --- ').join('|') + '|'
parts.push([header, sep, ...lines.slice(1)].join('\n'))
}
}
if (footer) parts.push(`_${footer}_`)