1 478
async function sendRichReport(sock, jid, report, opts = {}) {
const { Boom } = await import('@hapi/boom')
const { generateMessageIDV2 } = await import('./generics.js')
const { buildRichContextInfo, tokenizeCode } = await import('./message-composer.js')
const LAYOUT_SINGLE = 'GenAISingleLayoutViewModel'
const T_MARKDOWN = 'GenAIMarkdownTextUXPrimitive'
const T_CODE = 'GenAICodeUXPrimitive'
const T_TABLE = 'GenAITableUXPrimitive'
const markdownPrimitive = (text) => ({
text: String(text ?? ''),
__typename: T_MARKDOWN
})
const codePrimitive = (code, language = 'javascript') => ({
language,
code_blocks: tokenizeCode(String(code ?? ''), language),
__typename: T_CODE
})
const tablePrimitive = (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 }
})
const markdownSection = (text) => singleLayout(markdownPrimitive(text))
const codeSection = (code, language = 'javascript') => singleLayout(codePrimitive(code, language))
const tableSection = (rows) => singleLayout(tablePrimitive(rows))
const serverSupportsRich = (s) => {
try {
const props = s?.serverProps ?? {}
return props.richResponse === true || props.richResponses === true || props.genAIRichResponse === true
} catch {
return false
}
}
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(markdownSection(body))
for (const cb of codeBlocks) {
sections.push(codeSection(cb.code, cb.language ?? 'javascript'))
}
for (const rows of tables) {
sections.push(tableSection(rows))
}
const all = []
if (title) all.push(markdownSection(title))
all.push(...sections)
if (footer) all.push(markdownSection(footer))
if (all.length === 0) {
throw new Boom('buildGenAIRichMessage: no sections to send', { statusCode: 400 })
}
const ctxInfo = buildRichContextInfo(quoted, { mentions, botJid })
const message = {
botForwardedMessage: {
message: { richResponse: { sections: all } },
contextInfo: ctxInfo
}
}
const messageId = generateMessageIDV2()
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}_`)
const text = parts.join('\n\n')
const messageId = generateMessageIDV2()
await sock.relayMessage(jid, { conversation: text }, { messageId })
return { messageId, mode: 'plain' }
}12 نوامبر, 02:34
12 نوامبر, ~02:35
