fix: génération asynchrone (fire-and-forget + polling)
Some checks failed
Deploy to Production / Build and Deploy (push) Failing after 23s
Some checks failed
Deploy to Production / Build and Deploy (push) Failing after 23s
Le problème : la route POST /api/agents/run-for-note bloquait le thread
HTTP pendant toute la durée de génération (2-5 min), provoquant un
spinner infini sans résultat.
Solution :
- POST retourne immédiatement avec { agentId } et lance executeAgent
en arrière-plan (fire-and-forget, sans await)
- GET /api/agents/run-for-note?agentId= retourne le statut de la
dernière agentAction (running | success | failure) + canvasId/noteId
- Le client poll le statut toutes les 3 secondes jusqu'au résultat,
le toast persistant Sonner se met à jour automatiquement
- Cleanup du polling au démontage du composant
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -264,6 +264,8 @@ export function ContextualAIChat({
|
||||
|
||||
// ── Generate slides / diagram ────────────────────────────────────────────────
|
||||
|
||||
const generatePollRef = useRef<ReturnType<typeof setInterval> | null>(null)
|
||||
|
||||
const handleGenerate = async (type: 'slides' | 'diagram') => {
|
||||
if (!noteId) {
|
||||
toast.error(t('ai.generate.noNoteId') || 'Note non sauvegardée')
|
||||
@@ -272,7 +274,7 @@ export function ContextualAIChat({
|
||||
setGenerateLoading(type)
|
||||
setGenerateResult(null)
|
||||
|
||||
// Persistent loading toast — survives navigation (Sonner is layout-level)
|
||||
// Persistent loading toast — layout-level (Sonner), survives navigation
|
||||
const toastId = toast.loading(
|
||||
type === 'slides'
|
||||
? (t('ai.generate.toastLoading.slides') || '⏳ Génération de la présentation en cours…')
|
||||
@@ -281,6 +283,7 @@ export function ContextualAIChat({
|
||||
)
|
||||
|
||||
try {
|
||||
// POST starts the agent immediately and returns agentId (fire-and-forget on server)
|
||||
const res = await fetch('/api/agents/run-for-note', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -292,38 +295,69 @@ export function ContextualAIChat({
|
||||
const data = await res.json()
|
||||
if (!res.ok || !data.success) {
|
||||
toast.error(data.error || t('ai.generate.error') || 'Erreur lors de la génération', { id: toastId })
|
||||
setGenerateLoading(null)
|
||||
return
|
||||
}
|
||||
setGenerateResult({ type, canvasId: data.canvasId, noteId: data.noteId })
|
||||
|
||||
if (type === 'slides' && data.canvasId) {
|
||||
toast.success(t('ai.generate.slidesReady') || 'Présentation générée !', {
|
||||
id: toastId,
|
||||
duration: 10000,
|
||||
description: t('ai.generate.toastSuccessSlides') || 'Cliquez sur le bouton Télécharger dans le panneau IA.',
|
||||
action: { label: t('ai.generate.downloadPptx') || 'Télécharger', onClick: () => window.open(`/api/canvas/download?id=${data.canvasId}`, '_blank') },
|
||||
})
|
||||
} else if (type === 'diagram' && data.canvasId) {
|
||||
toast.success(t('ai.generate.diagramReady') || 'Diagramme généré !', {
|
||||
id: toastId,
|
||||
duration: 10000,
|
||||
description: t('ai.generate.toastSuccessDiagram') || 'Votre diagramme est disponible dans le Lab.',
|
||||
action: { label: t('ai.generate.openDiagram') || 'Ouvrir', onClick: () => window.location.href = `/lab?canvas=${data.canvasId}` },
|
||||
})
|
||||
} else {
|
||||
toast.success(type === 'slides'
|
||||
? (t('ai.generate.slidesReady') || 'Présentation générée !')
|
||||
: (t('ai.generate.diagramReady') || 'Diagramme généré !'),
|
||||
{ id: toastId }
|
||||
)
|
||||
}
|
||||
const { agentId } = data as { agentId: string }
|
||||
|
||||
// Poll status every 3 s until terminal state
|
||||
if (generatePollRef.current) clearInterval(generatePollRef.current)
|
||||
generatePollRef.current = setInterval(async () => {
|
||||
try {
|
||||
const pollRes = await fetch(`/api/agents/run-for-note?agentId=${agentId}`)
|
||||
const poll = await pollRes.json()
|
||||
|
||||
if (poll.status === 'success') {
|
||||
clearInterval(generatePollRef.current!)
|
||||
generatePollRef.current = null
|
||||
setGenerateLoading(null)
|
||||
setGenerateResult({ type, canvasId: poll.canvasId, noteId: poll.noteId })
|
||||
|
||||
if (type === 'slides' && poll.canvasId) {
|
||||
toast.success(t('ai.generate.slidesReady') || 'Présentation générée !', {
|
||||
id: toastId, duration: 10000,
|
||||
description: t('ai.generate.toastSuccessSlides') || 'Cliquez sur Télécharger dans le panneau IA.',
|
||||
action: { label: t('ai.generate.downloadPptx') || 'Télécharger', onClick: () => window.open(`/api/canvas/download?id=${poll.canvasId}`, '_blank') },
|
||||
})
|
||||
} else if (type === 'diagram' && poll.canvasId) {
|
||||
toast.success(t('ai.generate.diagramReady') || 'Diagramme généré !', {
|
||||
id: toastId, duration: 10000,
|
||||
description: t('ai.generate.toastSuccessDiagram') || 'Votre diagramme est disponible dans le Lab.',
|
||||
action: { label: t('ai.generate.openDiagram') || 'Ouvrir', onClick: () => { window.location.href = `/lab?canvas=${poll.canvasId}` } },
|
||||
})
|
||||
} else {
|
||||
toast.success(type === 'slides'
|
||||
? (t('ai.generate.slidesReady') || 'Présentation générée !')
|
||||
: (t('ai.generate.diagramReady') || 'Diagramme généré !'),
|
||||
{ id: toastId }
|
||||
)
|
||||
}
|
||||
} else if (poll.status === 'failure') {
|
||||
clearInterval(generatePollRef.current!)
|
||||
generatePollRef.current = null
|
||||
setGenerateLoading(null)
|
||||
toast.error(poll.error || t('ai.generate.error') || 'Erreur lors de la génération', { id: toastId })
|
||||
}
|
||||
// If still 'running', do nothing — next poll will check again
|
||||
} catch {
|
||||
// Network error during poll — keep polling
|
||||
}
|
||||
}, 3000)
|
||||
|
||||
} catch {
|
||||
toast.error(t('ai.generate.error') || 'Erreur lors de la génération', { id: toastId })
|
||||
} finally {
|
||||
setGenerateLoading(null)
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup polling on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (generatePollRef.current) clearInterval(generatePollRef.current)
|
||||
}
|
||||
}, [])
|
||||
|
||||
// ── Resource tab handlers ────────────────────────────────────────────────────
|
||||
|
||||
const handleScrapeUrl = async () => {
|
||||
|
||||
Reference in New Issue
Block a user