Repository Public
1// SP Mail Next — inscription du module « ai » dans la coquille. L'intelligence est facultative et coupée par défaut : chaque surface2// (résumé, suggestions, assistant d'écriture, tri, recherche par le sens) n'est inscrite QUE pendant que son réglage est actif et3// retirée dès qu'il ne l'est plus. Les pastilles d'aperçu de la boîte, calculées sans modèle, sont toujours là.4import { lazy, Suspense } from "react"5import { t } from "../../i18n"6import { aiOn, aiSearch, aiSettings, aiUi, confirmedSettings, resetTriage, triageQueue, type AiFeature, type TriageCategory } from "../../next/ai"7import type { Setup } from "../../next/features"8import { resetInsights } from "../../next/insights"9import { setSemanticProvider } from "../../next/semantic"10import { S, type AppState } from "../../state/app"11import { registerComposerTool, registerListHeader, registerReaderPanel, registerRowGlyphs, type ComposerToolProps, type RowGlyph, type RowGlyphsProvider } from "../../state/registry"12import { navigate } from "../../state/session"13import { registerCommand } from "../commands"14import type { IconName } from "../kit"1516const InsightChips = lazy(() => import("./InsightChips"))17const SummaryPanel = lazy(() => import("./SummaryPanel"))18const ReplySuggestions = lazy(() => import("./ReplySuggestions"))19const AiWriteMenu = lazy(() => import("./AiWriteMenu"))20/** L'outil apporte sa propre limite d'attente : la fenêtre de rédaction n'a pas à savoir qu'il est chargé à la demande. */21function WriteTool(props: ComposerToolProps) {22 return (23 <Suspense fallback={null}>24 <AiWriteMenu {...props} />25 </Suspense>26 )27}2829/** Boîte de réception d'une boîte précise, sans recherche : seul endroit des pastilles d'aperçu et du tri. */30const inInbox = (s: AppState): boolean => s.route.name === "mail" && !s.route.all && s.route.view === "inbox" && !s.route.q && !!s.mb3132const CATEGORY_ICON: Partial<Record<TriageCategory, IconName>> = { personal: "user", work: "building", newsletter: "list", notification: "bell", transaction: "file", social: "users" }33const CATEGORY_LABEL: Record<TriageCategory, "nx.ai.category.personal" | "nx.ai.category.work" | "nx.ai.category.newsletter" | "nx.ai.category.notification" | "nx.ai.category.transaction" | "nx.ai.category.social" | "nx.ai.category.other"> = {34 personal: "nx.ai.category.personal", work: "nx.ai.category.work", newsletter: "nx.ai.category.newsletter", notification: "nx.ai.category.notification", transaction: "nx.ai.category.transaction", social: "nx.ai.category.social", other: "nx.ai.category.other",35}36/** Pastilles de catégorie des lignes visibles : résultat connu, sinon le message rejoint le prochain lot (jamais d'attente). */37const triageGlyphs = (): RowGlyphsProvider => (email) => {38 const s = S.get()39 if (!email.id || !inInbox(s)) return []40 const mb = email.mailboxId ?? s.mb41 const e = mb ? triageQueue.want(mb, email.id) : undefined42 if (!e) return []43 const out: RowGlyph[] = []44 const icon = CATEGORY_ICON[e.category]45 if (e.important) out.push({ id: "ai-important", icon: "bolt", label: t("nx.ai.triage.important") })46 if (icon) out.push({ id: `ai-${e.category}`, icon, label: t("nx.ai.triage.category", { category: t(CATEGORY_LABEL[e.category]) }) })47 return out48}4950export const setupAi: Setup = () => {51 const active = new Map<string, () => void>()52 const toggle = (key: string, on: boolean, register: () => () => void) => {53 if (on && !active.has(key)) active.set(key, register())54 else if (!on && active.has(key)) {55 active.get(key)!()56 active.delete(key)57 }58 }59 let triageRev = aiUi.get().triageRev60 let semanticModel = ""6162 const sync = () => {63 const s = confirmedSettings()64 const on = (f: AiFeature) => aiOn(s, f)65 toggle("summary", on("summary"), () => {66 const offPanel = registerReaderPanel({ id: "ai-summary", slot: "top", component: SummaryPanel, order: 10 })67 const offCmd = registerCommand(() => ({68 id: "ai.summarize", section: "actions", title: t("nx.ai.cmd.summarize"), icon: "sparkle", keywords: ["ia", "ai", "résumé", "summary", "résumer"],69 when: (st) => st.route.name === "mail" && !!st.open && st.open.status === "ready" && st.online,70 run: () => aiUi.set((u) => ({ summaryAsk: u.summaryAsk + 1 })),71 }))72 return () => {73 offPanel()74 offCmd()75 void import("./SummaryPanel").then((m) => m.forgetSummaries())76 }77 })78 toggle("reply", on("reply"), () => registerReaderPanel({ id: "ai-replies", slot: "bottom", component: ReplySuggestions, order: 10 }))79 toggle("write", on("write"), () => registerComposerTool({ id: "ai-write", component: WriteTool, order: 10 }))80 toggle("triage", on("triage"), () => {81 let off = registerRowGlyphs(triageGlyphs())82 // un lot arrivé : nouvelle fonction inscrite → les lignes visibles se redessinent avec leurs pastilles83 const offUi = aiUi.subscribe(() => {84 const rev = aiUi.get().triageRev85 if (rev === triageRev) return86 triageRev = rev87 off()88 off = registerRowGlyphs(triageGlyphs())89 })90 return () => {91 offUi()92 off()93 resetTriage()94 }95 })96 toggle("semantic", on("semantic"), () => {97 setSemanticProvider({98 label: () => (semanticModel ? `${t("nx.ai.provider.embed")} · ${semanticModel}` : t("nx.ai.provider.embed")),99 search: async (q, mailboxIds, signal) => {100 const r = await aiSearch(q, mailboxIds, signal)101 semanticModel = r.semantic.model102 return r.data103 },104 })105 return () => setSemanticProvider(null)106 })107 }108109 // toujours présents quand le serveur a la couche : aperçus sans modèle, et le chemin vers les réglages110 const offHeader = registerListHeader({ id: "ai-insights", when: inInbox, component: InsightChips, order: 10 })111 const offSettingsCmd = registerCommand(() => ({112 id: "ai.settings", section: "settings", title: `${t("nav.settings")} : ${t("nx.settings.tab.intelligence")}`, icon: "sparkle", keywords: ["ia", "ai", "intelligence", "anthropic", "maclustr"],113 run: () => navigate("/settings/intelligence"),114 }))115 const offStore = aiSettings.store.subscribe(sync)116 void aiSettings.refresh()117 sync()118119 return () => {120 offStore()121 offHeader()122 offSettingsCmd()123 for (const off of active.values()) off()124 active.clear()125 resetInsights()126 }127}128