/** * 可选 AI 摘要。 * * 定位:**默认关闭**。打开后会给当天的前 N 条生成一句中文摘要,显示在侧边栏与日报里。 * 它直接提升"高价值索引"的可用性 —— 只有标题的列表要一条条点进去才知道值不值得看。 * * 这个文件刻意做成**可注入 stream** 的形状:`summarizeItems` 只依赖一个函数, * 所以它的全部失败路径(超时、空输出、非 stop 终止、单条炸掉)都能用假 stream 测到, * 不需要真调模型。 * * 三条从实践里得来的约束(参考 dsh-rss 的实测经验): * 1. **`maxTokens` 要留足推理预算**:推理 token 与正文**共享**上限。 * 把它设成 200 会让推理模型还没写正文就耗尽预算,finish 变成 `max-tokens`,该条判失败。 * 默认 1024,且失败原因里会带上实际的终止原因,方便判断是不是撞了上限。 * 2. **单条失败必须隔离**:20 条里挂 1 条,另外 19 条照常出。 * 3. **按条目缓存**:重复条目(跨天重复的新闻)不该重复付钱。 */ /** 生成选项。刻意只声明我们真正用到的字段,避免把 dsh-llm 的完整类型拖进来。 */ export interface StreamRequest { provider: string; model: string; system?: string; messages: Array<{ role: 'user'; content: string; }>; maxTokens?: number; temperature?: number; signal?: AbortSignal; } /** 我们关心的事件子集:文本增量、推理增量、终止原因。 */ export interface StreamChunkLike { type: string; text?: string; reason?: { kind?: string; failure?: { message?: string; code?: string; }; }; } export type StreamFn = (options: StreamRequest) => AsyncIterable; export interface SummarySource { id: string; title: string; sourceName: string; summary?: string; platform?: string; topic?: string; } export interface SummaryOutcome { text: string; model: string; } export interface SummaryFailure { error: string; } export interface SummaryOptions { provider: string; model: string; maxItems: number; concurrency: number; timeoutMs: number; maxTokens: number; stream: StreamFn; signal?: AbortSignal; /** 注入时钟,便于测试。 */ now?: () => number; } /** 摘要长度上限(字符)。一句中文摘要超过这个数就已经不像"一句话"了。 */ export declare const SUMMARY_MAX_CHARS = 60; export declare function buildSummaryPrompt(item: SummarySource): { system: string; user: string; }; /** * 清洗模型输出。 * * 模型很爱加的东西:引号、`摘要:` 前缀、换行、Markdown 星号、末尾的"(共 42 字)"。 * 这些都是渲染噪音,一处不洗就会在列表里显得很脏。 */ export declare function cleanSummary(raw: string, maxChars?: number): string | null; /** * 生成一条摘要。 * * 关键在终止判断上:只有 `finish.reason.kind === 'stop'` 才算干净的完成。 * 但**已经拿到可用文本时不必因为终止原因不干净就丢弃** —— 推理模型经常在 * `max-tokens` 下也已经把正文写完了。没拿到文本时,终止原因就是唯一的诊断线索, * 要原样带出去(含 error / aborted 的 failure 说明)。 */ export declare function summarizeOne(item: SummarySource, options: SummaryOptions): Promise; export interface SummaryBatchResult { /** id → 摘要文本 */ summaries: Map; /** id → 失败原因。用来在日报里显式标注"这几条没摘要成功 + 为什么"。 */ failures: Map; skipped: number; } /** * 批量生成。并发受限、单条失败隔离。 * * 为什么用"游标 + N 个 worker"而不是 `Promise.all(items.map(...))`: * 后者会一次性把所有请求打出去。20 条摘要对推理模型来说可能是几十秒的并发压力, * 撞限流只会让更多条失败。 */ export declare function summarizeItems(items: readonly SummarySource[], options: SummaryOptions): Promise; export interface AiCacheEntry { text: string; model: string; at: string; } export interface AiCache { version: number; entries: Record; } export declare function emptyCache(): AiCache; /** 读缓存并顺手清理过期/超量项。返回的是**可直接使用**的缓存。 */ export declare function readCache(raw: unknown, now: Date, ttlDays: number, maxEntries: number): AiCache; export declare function mergeCache(cache: AiCache, additions: Map, now: Date): AiCache;