Repository Public
1// SP Mail Next — couche d'intelligence (facultative, coupée par défaut) : réglages et consentement, résumé de fil, suggestions de réponse,2// aide à la rédaction, tri, et « insights » calculés sans aucun modèle. Le courrier est passé au modèle comme une donnée non fiable ;3// le modèle n'a aucun outil ; rien n'est envoyé, déplacé ni supprimé par ce code : les routes ne rendent que des suggestions.4import crypto from "node:crypto"5import type { FastifyInstance } from "fastify"6import { z } from "zod"7import { config } from "../config.js"8import { sql } from "../db.js"9import { folders, jmapOk, roleFolder, type Folder, type Mbx } from "../mailjmap.js"10import { htmlToText } from "../sanitize.js"11import { seal, unseal } from "../stalwart.js"12import { audit, bad, HttpError, parse, zUuid } from "../util.js"13import { nextUser, userMailbox } from "./common.js"14import type { NextWorker } from "./index.js"15import { AiInvalidOutput, available, providerOf, type AiProvider, type CompleteInput, type CompleteResult, type ProviderName } from "./ai-providers.js"16import {17 CATEGORIES, clampSummary, detectLanguage, extractOut, extractPrompt, isAutomated, isNewsletter, needsReply, newBoundary, RepliesOut, repliesPrompt, stripQuoted, SubjectsOut,18 summarizePrompt, SummaryOut, threadDigest, TONES, TriageOut, triagePrompt, unansweredSent, WRITE_ACTIONS, WRITE_LANGS, writePrompt,19 type Category, type ExtractSchema, type InsightEmail, type Lang, type MailText, type Tone,20} from "./ai-prompts.js"21import { beat } from "./ops.js"22import { dropIndex, embeddingStatus, semanticRoutes, semanticWorkers } from "./semantic.js"2324// ------------------------------------------------------------------ réglages25export const FEATURES = ["summary", "reply", "write", "triage", "semantic"] as const26export type Feature = (typeof FEATURES)[number]27export type AiSettings = { provider: "off" | ProviderName; consent_external_at: Date | null; features: Record<Feature, boolean>; language: "auto" | Lang }28// L'indexation sémantique reste à activer explicitement ; les autres fonctions suivent le choix d'un fournisseur.29const DEFAULT_FEATURES: Record<Feature, boolean> = { summary: true, reply: true, write: true, triage: true, semantic: false }3031export async function loadSettings(userId: string): Promise<AiSettings> {32 const [r] = await sql`select provider, consent_external_at, features, language from ai_settings where user_id = ${userId}`33 const f = (r?.features ?? {}) as Partial<Record<Feature, boolean>>34 return {35 provider: (r?.provider ?? "off") as AiSettings["provider"], consent_external_at: r?.consent_external_at ?? null, language: (r?.language ?? "auto") as AiSettings["language"],36 features: Object.fromEntries(FEATURES.map((k) => [k, typeof f[k] === "boolean" ? f[k] : DEFAULT_FEATURES[k]])) as Record<Feature, boolean>,37 }38}39const present = async (s: AiSettings) => ({ ...s, available: { ...available(), semantic: (await embeddingStatus()).available } })4041// ------------------------------------------------------------------ garde : fournisseur, fonction, consentement, débit42const minute = new Map<string, { n: number; reset: number }>()43/** Limite par utilisateur, indépendante de l'échelle de test des autres limiteurs : N appels par minute, M par 24 h (journal d'usage). */44async function limit(userId: string) {45 const now = Date.now()46 let b = minute.get(userId)47 if (!b || b.reset < now) { b = { n: 0, reset: now + 60_000 }; minute.set(userId, b) }48 if (++b.n > config.aiRatePerMinute) throw bad("rate_limited", 429, "Too many AI requests; retry in a minute")49 const [{ n }] = await sql`select count(*)::int as n from ai_usage where user_id = ${userId} and at > now() - interval '24 hours' and status <> 'cached'`50 if (n >= config.aiRatePerDay) throw bad("rate_limited", 429, "Daily AI request limit reached")51}5253export type Gate = { settings: AiSettings; provider: AiProvider }54/** 409 `ai_disabled` (fournisseur « off » ou fonction coupée), 412 `consent_required` (fournisseur externe sans consentement), 503 si non configuré. */55export async function aiGate(userId: string, feature: Feature, opts: { countRate?: boolean } = {}): Promise<Gate> {56 const settings = await loadSettings(userId)57 if (settings.provider === "off") throw bad("ai_disabled", 409, "Intelligence features are turned off for this account")58 if (!settings.features[feature]) throw bad("ai_disabled", 409, `The "${feature}" feature is not enabled`)59 if (settings.provider === "anthropic" && !settings.consent_external_at) throw bad("consent_required", 412, "Using an external provider requires explicit consent")60 if (!available()[settings.provider]) throw bad("ai_not_configured", 503, `The "${settings.provider}" provider is not configured on this server`)61 if (opts.countRate !== false) await limit(userId)62 return { settings, provider: providerOf(settings.provider) }63}64/** Pour les automatismes (règles) : la fonction est-elle utilisable, sans lever d'erreur ni consommer de débit ? */65export async function aiEnabled(userId: string, feature: Feature): Promise<boolean> {66 const s = await loadSettings(userId)67 return s.provider !== "off" && s.features[feature] && (s.provider !== "anthropic" || !!s.consent_external_at) && available()[s.provider]68}6970/** Appel au fournisseur, journalisé : utilisateur, fonction, fournisseur, modèle, jetons, durée, état. Jamais de contenu. */71async function run<T>(userId: string, feature: string, p: AiProvider, input: CompleteInput<T>): Promise<CompleteResult<T>> {72 const t0 = Date.now()73 const log = (status: string, model: string, i: number, o: number, code: string | null) =>74 sql`insert into ai_usage (user_id, feature, provider, model, input_tokens, output_tokens, duration_ms, status, error_code)75 values (${userId}, ${feature}, ${p.name}, ${model}, ${i}, ${o}, ${Date.now() - t0}, ${status}, ${code})`.catch(() => undefined)76 try {77 const r = await p.complete(input)78 await log("ok", r.model, r.usage.input_tokens, r.usage.output_tokens, null)79 return r80 } catch (e) {81 if (e instanceof AiInvalidOutput) await log("invalid_output", e.model, e.usage.input_tokens, e.usage.output_tokens, e.code)82 else if (e instanceof HttpError) await log(e.code === "ai_refused" ? "refused" : "error", p.model, 0, 0, e.code)83 else await log("error", p.model, 0, 0, "internal")84 throw e85 }86}8788// ------------------------------------------------------------------ cache court, scellé89const cacheKey = (...parts: string[]) => crypto.createHash("sha256").update(parts.join("\u0000")).digest()90async function cacheGet<T>(userId: string, kind: string, key: Buffer): Promise<{ value: T; provider: string; model: string } | null> {91 const [r] = await sql`select value_enc, provider, model from ai_cache where user_id = ${userId} and kind = ${kind} and key = ${key} and expires_at > now()`92 if (!r) return null93 try { return { value: JSON.parse(unseal(Buffer.from(r.value_enc))) as T, provider: r.provider, model: r.model } } catch { return null }94}95async function cachePut(userId: string, kind: string, key: Buffer, p: { provider: string; model: string }, value: unknown, ttlHours: number) {96 await sql`insert into ai_cache (user_id, kind, key, provider, model, value_enc, expires_at)97 values (${userId}, ${kind}, ${key}, ${p.provider}, ${p.model}, ${seal(JSON.stringify(value))}, now() + make_interval(hours => ${ttlHours}))98 on conflict (user_id, kind, key) do update set provider = excluded.provider, model = excluded.model, value_enc = excluded.value_enc, created_at = now(), expires_at = excluded.expires_at`99}100101// ------------------------------------------------------------------ lecture du courrier (JMAP) et mise en texte102type E = Record<string, any> & { id: string }103const HEADERS = ["header:List-Id:asText", "header:List-Unsubscribe:asRaw", "header:Precedence:asText", "header:Auto-Submitted:asText"]104export const SUMMARY_PROPS = ["threadId", "mailboxIds", "keywords", "from", "to", "cc", "subject", "preview", "receivedAt", "size", "hasAttachment", ...HEADERS]105const BODY_PROPS = [...SUMMARY_PROPS, "replyTo", "sentAt", "messageId", "inReplyTo", "references", "textBody", "htmlBody", "bodyValues", "attachments", "header:Delivered-To:asText:all"]106107/** Messages complets (texte et HTML plafonnés à 60 ko par partie). Des pièces jointes, seules les métadonnées sont demandées. */108export async function loadEmails(mb: Mbx, ids: string[]): Promise<E[]> {109 if (!ids.length) return []110 const [g] = await jmapOk(mb, [["Email/get", { ids, properties: BODY_PROPS, bodyProperties: ["partId", "type", "name", "size"], fetchTextBodyValues: true, fetchHTMLBodyValues: true, maxBodyValueBytes: 60_000 }]])111 return g.list as E[]112}113/** Messages d'un fil, du plus ancien au plus récent (les 40 derniers au plus). 404 si le fil est inconnu. */114export async function loadThread(mb: Mbx, threadId: string): Promise<E[]> {115 const [t] = await jmapOk(mb, [["Thread/get", { ids: [threadId] }]])116 const ids = ((t.list as { emailIds: string[] }[])[0]?.emailIds ?? []).slice(-40)117 if (!ids.length) throw bad("not_found", 404)118 return (await loadEmails(mb, ids)).sort((a, b) => Date.parse(a.receivedAt) - Date.parse(b.receivedAt))119}120const addr = (l: { name?: string | null; email?: string | null }[] | null | undefined) => (l ?? []).map((a) => (a.name ? `${a.name} <${a.email}>` : String(a.email ?? ""))).join(", ")121/** Corps en texte brut : partie texte si elle existe, sinon HTML assaini puis aplati. */122export function bodyText(e: E): string {123 const values = (e.bodyValues ?? {}) as Record<string, { value: string }>124 const from = (parts: { partId: string; type: string }[] | undefined, want: string) => (parts ?? []).filter((p) => p.type === want).map((p) => values[p.partId]?.value ?? "").join("\n").trim()125 return from(e.textBody, "text/plain") || htmlToText([...(e.htmlBody ?? []), ...(e.textBody ?? [])].filter((p: { type: string }) => p.type === "text/html").map((p: { partId: string }) => values[p.partId]?.value ?? "").join("\n")) || String(e.preview ?? "")126}127export function mailText(e: E): MailText {128 return {129 from: addr(e.from), to: addr(e.to), cc: addr(e.cc) || undefined, date: String(e.receivedAt ?? ""), subject: String(e.subject ?? ""), text: bodyText(e),130 attachments: ((e.attachments ?? []) as { name?: string | null }[]).map((a) => String(a.name ?? "")).filter(Boolean),131 }132}133const people = (l: { name?: string | null; email?: string | null }[] | null | undefined) => (l ?? []).map((x) => ({ email: String(x.email ?? ""), name: x.name ?? null }))134/** Résumé d'un message, dans la forme commune aux recherches (`/api/next/search`). */135export function summaryOf(mailboxId: string, e: E, fl: Folder[] = []) {136 const boxIds = Object.keys(e.mailboxIds ?? {})137 return {138 mailbox_id: mailboxId, email_id: e.id, thread_id: e.threadId as string, from: people(e.from), to: people(e.to), subject: String(e.subject ?? ""), preview: String(e.preview ?? ""), received_at: e.receivedAt as string,139 keywords: Object.keys(e.keywords ?? {}).sort(), mailbox_ids: boxIds, has_attachment: !!e.hasAttachment, size: Number(e.size ?? 0),140 folder_roles: [...new Set(boxIds.map((id) => fl.find((f) => f.id === id)?.role).filter((r): r is string => !!r))],141 }142}143const insightOf = (e: E): InsightEmail => ({144 id: e.id, threadId: e.threadId, from: e.from, to: e.to, cc: e.cc, receivedAt: e.receivedAt, keywords: e.keywords,145 listId: e["header:List-Id:asText"], listUnsubscribe: e["header:List-Unsubscribe:asRaw"], precedence: e["header:Precedence:asText"], autoSubmitted: e["header:Auto-Submitted:asText"],146})147export const headerFacts = (e: E) => { const i = insightOf(e); return { is_newsletter: isNewsletter(i), automated: isAutomated(i), list: String(i.listId ?? "") } }148const pickLang = (s: AiSettings, asked: Lang | undefined, sample: string): Lang => asked ?? (s.language !== "auto" ? s.language : detectLanguage(sample, "en"))149/** Adresses propres à une boîte (principale et alias) : sert à reconnaître « moi » dans un fil. */150export async function ownAddresses(mb: Mbx): Promise<Set<string>> {151 return new Set([mb.address.toLowerCase(), ...(await sql`select address from addresses where mailbox_id = ${mb.id}`).map((a) => String(a.address))])152}153154// ------------------------------------------------------------------ fonctions (réutilisées par les règles)155export async function summarizeThread(userId: string, mb: Mbx, threadId: string, asked?: Lang, refresh = false) {156 const { settings, provider } = await aiGate(userId, "summary")157 const thread = await loadThread(mb, threadId)158 const last = thread[thread.length - 1]159 const lang = pickLang(settings, asked, bodyText(last))160 const key = cacheKey("summary", mb.id, threadId, last.id, provider.name, provider.model, lang)161 // `refresh` : nouveau calcul demandé par l'utilisateur ; il passe par `run` (limites de débit et de volume) et remplace l'entrée du cache.162 const hit = refresh ? null : await cacheGet<ReturnType<typeof clampSummary>>(userId, "summary", key)163 if (hit) {164 await sql`insert into ai_usage (user_id, feature, provider, model, status) values (${userId}, 'summary', ${hit.provider}, ${hit.model}, 'cached')`165 return { ...hit.value, provider: hit.provider, model: hit.model, language: lang, cached: true }166 }167 const b = newBoundary()168 const digest = threadDigest(thread.map(mailText))169 const p = summarizePrompt(b, digest.text, lang)170 const r = await run(userId, "summary", provider, { system: p.system, messages: [{ role: "user", content: p.user }], json: SummaryOut, maxTokens: 1500 })171 const value = clampSummary(r.json!)172 await cachePut(userId, "summary", key, r, value, 24)173 return { ...value, provider: r.provider, model: r.model, language: lang, cached: false }174}175176/** Trois suggestions de réponse au message donné. Ne crée ni brouillon ni envoi : le client les insère dans le compositeur. */177export async function suggestReplies(userId: string, mb: Mbx, emailId: string, tone: Tone, asked?: Lang) {178 const { settings, provider } = await aiGate(userId, "reply")179 const [target] = await loadEmails(mb, [emailId])180 if (!target) throw bad("not_found", 404)181 const thread = (await loadThread(mb, target.threadId)).filter((e) => Date.parse(e.receivedAt) <= Date.parse(target.receivedAt) && e.id !== target.id)182 const lang = pickLang(settings, asked, bodyText(target))183 const b = newBoundary()184 const digest = threadDigest([...thread, target].map(mailText))185 const p = repliesPrompt(b, digest.text, tone, lang, { name: mb.display_name, address: mb.address })186 const schema = RepliesOut.refine((v) => v.suggestions.filter((s) => s.text.trim()).length >= 3)187 const r = await run(userId, "reply", provider, { system: p.system, messages: [{ role: "user", content: p.user }], json: schema, maxTokens: 2200 })188 const suggestions = r.json!.suggestions.filter((s) => s.text.trim()).slice(0, 3).map((s) => ({ ...(s.subject?.trim() ? { subject: s.subject.trim().replace(/[\r\n]+/g, " ").slice(0, 200) } : {}), text: s.text.trim().slice(0, 8000) }))189 return { suggestions, provider: r.provider, model: r.model, language: lang }190}191192export type TriageItem = { email_id: string; category: Category; needs_action: boolean; important: boolean; reason: string }193/** Tri de messages (25 au plus) : un seul appel pour ceux qui ne sont pas en cache ; le résultat n'est jamais écrit sur le message. */194export async function triageEmails(userId: string, mb: Mbx, emailIds: string[]): Promise<{ data: TriageItem[]; provider: string; model: string }> {195 const { provider } = await aiGate(userId, "triage")196 const ids = [...new Set(emailIds)].slice(0, 25)197 const emails = (await loadEmails(mb, ids)).sort((x, y) => ids.indexOf(x.id) - ids.indexOf(y.id)) // dans l'ordre demandé198 if (!emails.length) throw bad("not_found", 404)199 const out = new Map<string, TriageItem>()200 const todo: E[] = []201 for (const e of emails) {202 const hit = await cacheGet<Omit<TriageItem, "email_id">>(userId, "triage", cacheKey("triage", mb.id, e.id, provider.name, provider.model))203 if (hit) out.set(e.id, { email_id: e.id, ...hit.value }); else todo.push(e)204 }205 let model = provider.model206 if (todo.length) {207 const per = Math.min(1200, Math.floor(22_000 / todo.length) - 200)208 const b = newBoundary()209 const items = todo.map((e) => { const m = mailText(e); const f = headerFacts(e); return `From: ${m.from}\nTo: ${m.to}\nSubject: ${m.subject}\nMailing-list headers: ${f.is_newsletter ? "yes" : "no"}\n\n${stripQuoted(m.text, 200).slice(0, per)}` })210 const p = triagePrompt(b, items)211 const schema = TriageOut.refine((v) => todo.every((_, i) => v.items.some((x) => x.n === i + 1)))212 const r = await run(userId, "triage", provider, { system: p.system, messages: [{ role: "user", content: p.user }], json: schema, maxTokens: 150 + 90 * todo.length })213 model = r.model214 for (const [i, e] of todo.entries()) {215 const x = r.json!.items.find((y) => y.n === i + 1)!216 const value = { category: x.category, needs_action: x.needs_action, important: x.important, reason: x.reason.trim().slice(0, 300) }217 out.set(e.id, { email_id: e.id, ...value })218 await cachePut(userId, "triage", cacheKey("triage", mb.id, e.id, provider.name, provider.model), r, value, 7 * 24)219 }220 } else await sql`insert into ai_usage (user_id, feature, provider, model, status) values (${userId}, 'triage', ${provider.name}, ${provider.model}, 'cached')`221 return { data: emails.map((e) => out.get(e.id)!), provider: provider.name, model }222}223224/** Extraction de champs d'un message vers du JSON validé (action « extract » des règles). */225export async function extractFields(userId: string, mb: Mbx, emailId: string, schema: ExtractSchema) {226 const { provider } = await aiGate(userId, "triage")227 const [e] = await loadEmails(mb, [emailId])228 if (!e) throw bad("not_found", 404)229 const b = newBoundary()230 const p = extractPrompt(b, threadDigest([mailText(e)], { total: 12_000 }).text, schema)231 const r = await run(userId, "extract", provider, { system: p.system, messages: [{ role: "user", content: p.user }], json: extractOut(schema), maxTokens: 800 })232 return { data: r.json as Record<string, unknown>, provider: r.provider, model: r.model }233}234235// ------------------------------------------------------------------ routes236const zFeatures = z.object({ summary: z.boolean(), reply: z.boolean(), write: z.boolean(), triage: z.boolean(), semantic: z.boolean() }).partial().strict()237const zLang = z.enum(["fr", "en"])238const zJmapId = z.string().min(1).max(255).regex(/^[A-Za-z0-9_-]+$/)239240export function aiRoutes(app: FastifyInstance) {241 semanticRoutes(app)242243 app.get("/api/next/ai/settings", async (req) => present(await loadSettings((await nextUser(req, "ai", 240)).userId)))244245 // Passer à un fournisseur externe exige `consent_external: true` DANS la même requête ; le consentement et tout changement de fournisseur sont journalisés.246 app.put("/api/next/ai/settings", async (req) => {247 const a = await nextUser(req, "ai", 60)248 const b = parse(z.object({ provider: z.enum(["off", "local", "anthropic"]).optional(), features: zFeatures.optional(), language: z.enum(["auto", "fr", "en"]).optional(), consent_external: z.boolean().optional() }).strict(), req.body)249 const cur = await loadSettings(a.userId)250 const provider = b.provider ?? cur.provider251 let consent = cur.consent_external_at252 if (provider === "anthropic") {253 if (b.consent_external === false) throw bad("consent_required", 412, "The external provider cannot stay selected without consent")254 // Toute (re)sélection du fournisseur externe redemande le consentement : il n'est jamais hérité d'un ancien réglage.255 if (b.consent_external === true) consent = new Date()256 else if (cur.provider !== "anthropic" || !consent) throw bad("consent_required", 412, 'Selecting an external provider requires "consent_external": true in the same request')257 } else consent = null258 if (provider !== "off" && !available()[provider]) throw bad("provider_unavailable", 409, `The "${provider}" provider is not configured on this server`)259 const features = { ...cur.features, ...(b.features ?? {}) }260 await sql`insert into ai_settings (user_id, provider, consent_external_at, features, language, updated_at)261 values (${a.userId}, ${provider}, ${consent}, ${sql.json(features as never)}, ${b.language ?? cur.language}, now())262 on conflict (user_id) do update set provider = excluded.provider, consent_external_at = excluded.consent_external_at, features = excluded.features, language = excluded.language, updated_at = now()`263 if (provider === "anthropic" && b.consent_external === true) await audit(req, a.userId, "ai.consent_external", a.userId, { provider, model: config.aiAnthropicModel })264 if (cur.provider === "anthropic" && provider !== "anthropic") await audit(req, a.userId, "ai.consent_withdrawn", a.userId, { provider })265 if (provider !== cur.provider) await audit(req, a.userId, "ai.provider", a.userId, { from: cur.provider, to: provider })266 // Changer de fournisseur ou couper : les sorties gardées en cache sont effacées ; couper l'index sémantique l'efface aussi.267 if (provider !== cur.provider) await sql`delete from ai_cache where user_id = ${a.userId}`268 if (cur.features.semantic && (!features.semantic || provider === "off")) await dropIndex(a.userId)269 return present(await loadSettings(a.userId))270 })271272 app.get("/api/next/ai/usage", async (req) => {273 const a = await nextUser(req, "ai", 120)274 const { days } = parse(z.object({ days: z.coerce.number().int().min(1).max(90).default(30) }), req.query)275 const rows = await sql`select to_char(date_trunc('day', at at time zone 'utc'), 'YYYY-MM-DD') as day, feature, provider, model, status, count(*)::int as requests,276 sum(input_tokens)::int as input_tokens, sum(output_tokens)::int as output_tokens from ai_usage where user_id = ${a.userId} and at > now() - make_interval(days => ${days}) group by 1, 2, 3, 4, 5 order by 1 desc, 2`277 const [{ n }] = await sql`select count(*)::int as n from ai_usage where user_id = ${a.userId} and at > now() - interval '24 hours' and status <> 'cached'`278 return { data: rows, limits: { per_minute: config.aiRatePerMinute, per_day: config.aiRatePerDay, used_24h: n } }279 })280281 app.post("/api/next/ai/summarize", async (req) => {282 const a = await nextUser(req, "ai", 120)283 const b = parse(z.object({ mailbox_id: zUuid, thread_id: zJmapId, language: zLang.optional(), refresh: z.boolean().optional() }).strict(), req.body)284 return summarizeThread(a.userId, await userMailbox(a.userId, b.mailbox_id), b.thread_id, b.language, b.refresh === true)285 })286287 app.post("/api/next/ai/replies", async (req) => {288 const a = await nextUser(req, "ai", 120)289 const b = parse(z.object({ mailbox_id: zUuid, email_id: zJmapId, tone: z.enum(TONES).default("professional"), language: zLang.optional() }).strict(), req.body)290 return suggestReplies(a.userId, await userMailbox(a.userId, b.mailbox_id), b.email_id, b.tone, b.language)291 })292293 app.post("/api/next/ai/write", async (req) => {294 const a = await nextUser(req, "ai", 120)295 const b = parse(z.object({296 action: z.enum(WRITE_ACTIONS), text: z.string().trim().min(1).max(20_000), tone: z.enum([...TONES, "friendly", "formal"]).optional(),297 target_language: z.enum(Object.keys(WRITE_LANGS) as [keyof typeof WRITE_LANGS, ...(keyof typeof WRITE_LANGS)[]]).optional(), context: z.string().max(8000).optional(),298 }).strict(), req.body)299 if (b.action === "tone" && !b.tone) throw bad("invalid_request", 400, "tone: required when action is \"tone\"")300 if (b.action === "translate" && !b.target_language) throw bad("invalid_request", 400, "target_language: required when action is \"translate\"")301 const { provider } = await aiGate(a.userId, "write")302 const p = writePrompt(newBoundary(), { action: b.action, text: b.text, tone: b.tone, target: b.target_language, context: b.context })303 const maxTokens = Math.min(6000, Math.max(400, Math.ceil(b.text.length / 2) * (b.action === "expand" ? 3 : 1) + 400))304 if (b.action === "subject") {305 const r = await run(a.userId, "write", provider, { system: p.system, messages: [{ role: "user", content: p.user }], json: SubjectsOut.refine((v) => v.subjects.some((s) => s.trim())), maxTokens: 400 })306 return { subjects: r.json!.subjects.map((s) => s.trim().replace(/[\r\n]+/g, " ").slice(0, 200)).filter(Boolean).slice(0, 5), provider: r.provider, model: r.model }307 }308 const r = await run(a.userId, "write", provider, { system: p.system, messages: [{ role: "user", content: p.user }], maxTokens })309 return { text: r.text.slice(0, 60_000), provider: r.provider, model: r.model }310 })311312 app.post("/api/next/ai/triage", async (req) => {313 const a = await nextUser(req, "ai", 120)314 const b = parse(z.object({ mailbox_id: zUuid, email_ids: z.array(zJmapId).min(1).max(25) }).strict(), req.body)315 return triageEmails(a.userId, await userMailbox(a.userId, b.mailbox_id), b.email_ids)316 })317318 // Boîte intelligente sans modèle : infolettres (en-têtes de liste), réponses attendues, envois restés sans réponse. Fonctionne IA coupée.319 app.get("/api/next/inbox/insights", async (req) => {320 const a = await nextUser(req, "insights", 120)321 const q = parse(z.object({ mailbox_id: zUuid, reply_after_hours: z.coerce.number().min(0).max(24 * 30).default(24), sent_after_days: z.coerce.number().min(0).max(90).default(3), limit: z.coerce.number().int().min(1).max(200).default(100) }), req.query)322 const mb = await userMailbox(a.userId, q.mailbox_id)323 const [inboxId, sentId] = await Promise.all([roleFolder(mb, "inbox"), roleFolder(mb, "sent")])324 const list = async (folder: string | null, filter: Record<string, unknown> = {}) => {325 if (!folder) return [] as E[]326 const r = await jmapOk(mb, [327 ["Email/query", { filter: { inMailbox: folder, ...filter }, sort: [{ property: "receivedAt", isAscending: false }], limit: q.limit }, "q"],328 ["Email/get", { "#ids": { resultOf: "q", name: "Email/query", path: "/ids" }, properties: SUMMARY_PROPS }, "g"],329 ])330 return r[1].list as E[]331 }332 const now = Date.now()333 const [inbox, sent] = await Promise.all([list(inboxId), list(sentId, { after: new Date(now - 45 * 86400_000).toISOString().replace(/\.\d{3}Z$/, "Z") })])334 const threadIds = [...new Set([...inbox, ...sent].map((e) => e.threadId as string))]335 const last = new Map<string, string>()336 if (threadIds.length) {337 const [t] = await jmapOk(mb, [["Thread/get", { ids: threadIds }]])338 for (const th of t.list as { id: string; emailIds: string[] }[]) last.set(th.id, th.emailIds[th.emailIds.length - 1])339 }340 const own = await ownAddresses(mb)341 const byId = new Map([...inbox, ...sent].map((e) => [e.id, e]))342 const fl = await folders(mb, false, [...byId.values()].flatMap((e) => Object.keys(e.mailboxIds ?? {})))343 const pick = (l: InsightEmail[]) => l.map((i) => summaryOf(mb.id, byId.get(i.id)!, fl))344 return {345 mailbox_id: mb.id, ai: false,346 newsletters: inbox.filter((e) => isNewsletter(insightOf(e))).map((e) => summaryOf(mb.id, e, fl)),347 needs_reply: pick(needsReply(inbox.map(insightOf), last, own, now, q.reply_after_hours * 3600_000)),348 unanswered_sent: pick(unansweredSent(sent.map(insightOf), last, own, now, q.sent_after_days * 86400_000)),349 generated_at: new Date(now).toISOString(),350 }351 })352}353354export function aiWorkers(): NextWorker[] {355 return [356 ...semanticWorkers(),357 // Purge : entrées de cache expirées, journal d'usage au-delà de 90 jours.358 { name: "ai cache purge", intervalMs: 3600_000, run: async () => {359 await sql`delete from ai_cache where expires_at < now()`360 await sql`delete from ai_usage where at < now() - interval '90 days'`361 beat("ai cache purge", 3600_000)362 } },363 ]364}365void CATEGORIES366