Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
/**
* Table rows in the shapes models send them, including `{Header: value}`
* records, as arrays of cells in header order.
*/
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value)
}
/** Header text as it is displayed: a year sent as 2026 is still a header. */
export function headerText(header: unknown): string {
return header === null || header === undefined ? '' : String(header)
}
/** Header names for a warning: the first three, then how many more. */
export function columnNames(headers: string[], columns: number[]): string {
const names = columns.map(c => `'${headers[c]?.trim() || `column ${c + 1}`}'`)
if (names.length === 1) return names[0]
if (names.length <= 3) return `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`
return `${names.slice(0, 3).join(', ')} and ${names.length - 3} more column(s)`
}
/** A record key as it is matched against a header: case and surrounding space aside. */
export function headerKey(key: unknown): string {
return headerText(key).trim().toLowerCase()
}
/**
* `rows` as arrays of cells. A record is read by header name, exactly or
* ignoring case and surrounding space; a key that names no header but is a
* 0-based column index, such as "0", fills that column. Without headers its
* values are taken in key order. Anything else is dropped. Each kind of repair
* is reported once in `warnings`, prefixed with `label`.
*/
export function normalizeTableRows(
rows: unknown,
headers: unknown[] | undefined,
label: string,
warnings: string[]
): unknown[][] {
if (rows === undefined || rows === null) return []
if (!Array.isArray(rows)) {
warnings.push(`${label}: rows must be an array of rows; they were left out.`)
return []
}
const names = (headers ?? []).map(headerKey)
const columnAt = (key: string): number | undefined => {
const t = key.trim()
return /^\d{1,9}$/.test(t) && Number(t) < names.length ? Number(t) : undefined
}
let fromRecords = 0
let dropped = 0
const unmatched = new Set<string>()
const out: unknown[][] = []
for (const row of rows) {
if (Array.isArray(row)) {
out.push(row)
} else if (isRecord(row)) {
fromRecords++
if (names.length === 0) {
out.push(Object.values(row))
continue
}
const byName = new Map(Object.entries(row).map(([k, v]) => [headerKey(k), v]))
const byIndex = new Map<number, unknown>()
for (const [key, value] of Object.entries(row)) {
if (names.includes(headerKey(key))) continue
const column = columnAt(key)
if (column === undefined) unmatched.add(key)
else byIndex.set(column, value)
}
out.push(
(headers ?? []).map((h, i) =>
Object.prototype.hasOwnProperty.call(row, headerText(h))
? row[headerText(h)]
: byName.has(names[i])
? byName.get(names[i])
: byIndex.get(i)
)
)
} else {
dropped++
}
}
if (fromRecords > 0) {
warnings.push(
`${label}: ${fromRecords} row(s) were objects and were read by header name; send each ` +
'row as an array of cells in header order.'
)
}
if (unmatched.size > 0) {
warnings.push(
`${label}: the key(s) ${[...unmatched].map(k => `'${k}'`).join(', ')} match no header, so ` +
'those values were left out.'
)
}
if (dropped > 0) {
warnings.push(
`${label}: ${dropped} row(s) were neither an array nor an object and were left out.`
)
}
return out
}