mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 00:51:20 +00:00
Each FeedSource implementation now sets sourceId on items it produces, allowing consumers to trace items back to their originating source. Co-authored-by: Ona <no-reply@ona.com>
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import type { FeedItem } from "@aelis/core"
|
|
|
|
import type { EnhancementResult } from "./schema.ts"
|
|
|
|
const ENHANCEMENT_SOURCE_ID = "aelis.enhancement"
|
|
|
|
/**
|
|
* Merges an EnhancementResult into feed items.
|
|
*
|
|
* - Writes slot content from slotFills into matching items
|
|
* - Appends synthetic items to the list
|
|
* - Returns a new array (no mutation)
|
|
* - Ignores fills for items/slots that don't exist
|
|
*/
|
|
export function mergeEnhancement(
|
|
items: FeedItem[],
|
|
result: EnhancementResult,
|
|
currentTime: Date,
|
|
): FeedItem[] {
|
|
const merged = items.map((item) => {
|
|
const fills = result.slotFills[item.id]
|
|
if (!fills || !item.slots) return item
|
|
|
|
const mergedSlots = { ...item.slots }
|
|
let changed = false
|
|
|
|
for (const [slotName, content] of Object.entries(fills)) {
|
|
if (slotName in mergedSlots && content !== null) {
|
|
mergedSlots[slotName] = { ...mergedSlots[slotName]!, content }
|
|
changed = true
|
|
}
|
|
}
|
|
|
|
return changed ? { ...item, slots: mergedSlots } : item
|
|
})
|
|
|
|
for (const synthetic of result.syntheticItems) {
|
|
merged.push({
|
|
id: synthetic.id,
|
|
sourceId: ENHANCEMENT_SOURCE_ID,
|
|
type: synthetic.type,
|
|
timestamp: currentTime,
|
|
data: { text: synthetic.text },
|
|
})
|
|
}
|
|
|
|
return merged
|
|
}
|