mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 17:11:17 +00:00
feat(backend): add LLM-powered feed enhancement (#58)
* feat(backend): add LLM-powered feed enhancement Add enhancement harness that fills feed item slots and generates synthetic items via OpenRouter. - LLM client with 30s timeout, reusable SDK instance - Prompt builder with mini calendar and week overview - arktype schema validation + JSON Schema for structured output - Pure merge function with clock injection - Defensive fallback in feed endpoint on enhancement failure - Skips LLM call when no unfilled slots or no API key Co-authored-by: Ona <no-reply@ona.com> * refactor: move feed enhancement into UserSession Move enhancement logic from HTTP handler into UserSession so the transport layer has no knowledge of enhancement. UserSession.feed() handles refresh, enhancement, and caching in one place. - UserSession subscribes to engine updates and re-enhances eagerly - Enhancement cache tracks source identity to prevent stale results - UserSessionManager accepts config object with optional enhancer - HTTP handler simplified to just call session.feed() Co-authored-by: Ona <no-reply@ona.com> * test: add schema sync tests for arktype/JSON Schema drift Validates reference payloads against both the arktype schema (parseEnhancementResult) and the OpenRouter JSON Schema structure. Catches field additions/removals or type changes in either schema. Co-authored-by: Ona <no-reply@ona.com> * refactor: rename arktype schemas to match types Co-authored-by: Ona <no-reply@ona.com> --------- Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
51
apps/aris-backend/src/enhancement/enhance-feed.ts
Normal file
51
apps/aris-backend/src/enhancement/enhance-feed.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { FeedItem } from "@aris/core"
|
||||
|
||||
import type { LlmClient } from "./llm-client.ts"
|
||||
|
||||
import { mergeEnhancement } from "./merge.ts"
|
||||
import { buildPrompt, hasUnfilledSlots } from "./prompt-builder.ts"
|
||||
|
||||
/** Takes feed items, returns enhanced feed items. */
|
||||
export type FeedEnhancer = (items: FeedItem[]) => Promise<FeedItem[]>
|
||||
|
||||
export interface FeedEnhancerConfig {
|
||||
client: LlmClient
|
||||
/** Defaults to Date.now — override for testing */
|
||||
clock?: () => Date
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a FeedEnhancer that uses the provided LlmClient.
|
||||
*
|
||||
* Skips the LLM call when no items have unfilled slots.
|
||||
* Returns items unchanged on LLM failure.
|
||||
*/
|
||||
export function createFeedEnhancer(config: FeedEnhancerConfig): FeedEnhancer {
|
||||
const { client } = config
|
||||
const clock = config.clock ?? (() => new Date())
|
||||
|
||||
return async function enhanceFeed(items) {
|
||||
if (!hasUnfilledSlots(items)) {
|
||||
return items
|
||||
}
|
||||
|
||||
const currentTime = clock()
|
||||
const { systemPrompt, userMessage } = buildPrompt(items, currentTime)
|
||||
|
||||
let result
|
||||
try {
|
||||
result = await client.enhance({ systemPrompt, userMessage })
|
||||
} catch (err) {
|
||||
console.error("[enhancement] LLM call failed:", err)
|
||||
result = null
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
return items
|
||||
}
|
||||
|
||||
return mergeEnhancement(items, result, currentTime)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user