| @@ -5,12 +5,23 @@ | ||
| 5 | 5 | pickWorkflow, |
| 6 | 6 | recordAgentActivity, |
| 7 | 7 | } from '@agent/api'; |
| 8 | 8 | import { Chat } from '@agent/Chat'; |
| 9 | -import { Canvas, useCanvasOpen } from '@agent/components/Canvas'; | |
| 9 | +import { | |
| 10 | + Canvas, | |
| 11 | + useCanvasAssist, | |
| 12 | + useCanvasOpen, | |
| 13 | +} from '@agent/components/Canvas'; | |
| 10 | 14 | import { ChatInput } from '@agent/components/ChatInput'; |
| 11 | 15 | import { ChatMessages } from '@agent/components/ChatMessages'; |
| 16 | +import { StatusIndicator } from '@agent/components/messages/StatusIndicator'; | |
| 12 | 17 | import { UsageMessage } from '@agent/components/messages/UsageMessage'; |
| 18 | +import { useFollowUpHistory } from '@agent/follow-ups/history'; | |
| 19 | +import { | |
| 20 | + cardMessageIndex, | |
| 21 | + pickNextCard, | |
| 22 | + toolCallsThisRun, | |
| 23 | +} from '@agent/follow-ups/pick-next'; | |
| 13 | 24 | import { useLockPost } from '@agent/hooks/useLockPost'; |
| 14 | 25 | import { isAbilityTool, isAbilityWorkflow } from '@agent/lib/abilities'; |
| 15 | 26 | import { |
| 16 | 27 | getClientToolFallbackReply, |
| @@ -22,11 +33,11 @@ | ||
| 22 | 33 | import { useCanvasStore } from '@agent/state/canvas'; |
| 23 | 34 | import { useChatStore } from '@agent/state/chat'; |
| 24 | 35 | import { useGlobalStore } from '@agent/state/global'; |
| 25 | 36 | import { useStatusStore } from '@agent/state/status'; |
| 26 | -import { useSuggestionsStore } from '@agent/state/suggestions'; | |
| 27 | 37 | import { useWorkflowStore } from '@agent/state/workflows'; |
| 28 | 38 | import { hasRunComponent } from '@agent/workflows/abilities/components/run'; |
| 39 | +import startOnboardingWorkflow from '@agent/workflows/misc/start-onboarding'; | |
| 29 | 40 | import { useQuickEditStore } from '@quick-edit/state/store'; |
| 30 | 41 | import { digest } from '@shared/api/digest'; |
| 31 | 42 | import { |
| 32 | 43 | useCallback, |
| @@ -54,8 +65,25 @@ | ||
| 54 | 65 | ]); |
| 55 | 66 | return result; |
| 56 | 67 | }; |
| 57 | 68 | |
| 69 | +// cleanup() swaps in a fresh controller; a signal read after the wait is never aborted. | |
| 70 | +const canceledDuring = async (ms) => { | |
| 71 | + const { signal } = controller; | |
| 72 | + await new Promise((resolve) => setTimeout(resolve, ms)); | |
| 73 | + return signal.aborted; | |
| 74 | +}; | |
| 75 | + | |
| 76 | +const nextSteps = (workflowId, status, messages) => { | |
| 77 | + if (status === 'completed') { | |
| 78 | + useFollowUpHistory.getState().markDone(workflowId); | |
| 79 | + } | |
| 80 | + const card = pickNextCard({ workflowId, status, messages }); | |
| 81 | + return card | |
| 82 | + ? { followUp: card.id, followUpData: card.data } | |
| 83 | + : { followUp: null }; | |
| 84 | +}; | |
| 85 | + | |
| 58 | 86 | export const Agent = () => { |
| 59 | 87 | const { addMessage, updateMessage, popMessage, messages } = useChatStore(); |
| 60 | 88 | const { pushStatus, clearStatuses, leavingPage } = useStatusStore(); |
| 61 | 89 | const { |
| @@ -85,10 +113,11 @@ | ||
| 85 | 113 | }); |
| 86 | 114 | const [loop, setLoop] = useState(0); |
| 87 | 115 | const workflow = getWorkflow(); |
| 88 | 116 | const canvasOpen = useCanvasOpen(); |
| 117 | + const canvasAssist = useCanvasAssist(); | |
| 118 | + const canvasNoticeShown = useRef(false); | |
| 89 | 119 | const chatAvailable = useMemo(() => isChatAvailable(), [isChatAvailable]); |
| 90 | - const { addSuggestions, getSuggestions } = useSuggestionsStore(); | |
| 91 | 120 | // Options render only while their message is last; a reply dismisses them. |
| 92 | 121 | const lastMessage = messages.at(-1); |
| 93 | 122 | const qaSuggestions = |
| 94 | 123 | lastMessage?.type === 'message' && |
| @@ -96,12 +125,24 @@ | ||
| 96 | 125 | Array.isArray(lastMessage.details?.qaSuggestions) |
| 97 | 126 | ? lastMessage.details.qaSuggestions |
| 98 | 127 | : null; |
| 99 | 128 | |
| 129 | + useEffect(() => { | |
| 130 | + const { messages, updateMessage } = useChatStore.getState(); | |
| 131 | + const index = cardMessageIndex(messages); | |
| 132 | + const { id, details } = messages[index] ?? {}; | |
| 133 | + if (!id || details.followUp !== undefined || details.followUpClosed) return; | |
| 134 | + const { workflowId, status } = details; | |
| 135 | + const steps = nextSteps(workflowId, status, messages.slice(0, index)); | |
| 136 | + if (steps.followUp) updateMessage(id, steps); | |
| 137 | + }, []); | |
| 138 | + | |
| 100 | 139 | // Without this the input stays disabled for as long as the canvas is open. |
| 101 | 140 | useEffect(() => { |
| 102 | - if (canvasOpen) setCanType(true); | |
| 103 | - }, [canvasOpen]); | |
| 141 | + if (canvasOpen && canvasAssist) setCanType(true); | |
| 142 | + // A workflow reached by example carries no sessionId to key this on. | |
| 143 | + if (!canvasOpen) canvasNoticeShown.current = false; | |
| 144 | + }, [canvasOpen, canvasAssist]); | |
| 104 | 145 | |
| 105 | 146 | const cleanup = useCallback(() => { |
| 106 | 147 | setCanType(true); |
| 107 | 148 | agentWorking.current = false; |
| @@ -111,9 +152,14 @@ | ||
| 111 | 152 | agentBlock && setBlock(null); |
| 112 | 153 | // A class or pin left up freezes Quick Edit hover site-wide. |
| 113 | 154 | document |
| 114 | 155 | .querySelector('.wp-site-blocks') |
| 115 | - ?.classList.remove('extendify-agent-working', 'extendify-agent-busy'); | |
| 156 | + ?.classList.remove( | |
| 157 | + 'extendify-agent-working', | |
| 158 | + 'extendify-agent-busy', | |
| 159 | + 'extendify-agent-waiting', | |
| 160 | + 'extendify-agent-task', | |
| 161 | + ); | |
| 116 | 162 | setCommittedSelection(null); |
| 117 | 163 | clearStatuses(); |
| 118 | 164 | window.dispatchEvent(new Event('extendify-agent:remove-block-highlight')); |
| 119 | 165 | }, [setBlock, clearStatuses]); |
| @@ -135,13 +181,14 @@ | ||
| 135 | 181 | |
| 136 | 182 | const findAgent = useCallback( |
| 137 | 183 | async (options = {}) => { |
| 138 | 184 | pushStatus('calling-agent'); |
| 185 | + const { signal } = controller; | |
| 139 | 186 | const response = await withMinDuration( |
| 140 | 187 | pickWorkflow({ |
| 141 | 188 | // A mid-turn drop clears the block after this closure was made. |
| 142 | 189 | workflows: getAvailableWorkflows().map((w) => w.id), |
| 143 | - options: { signal: controller.signal, ...options }, | |
| 190 | + options: { signal, ...options }, | |
| 144 | 191 | }).catch(async (error) => { |
| 145 | 192 | devmode && console.error(error); |
| 146 | 193 | if (error?.response?.status === 429) { |
| 147 | 194 | updateRetryAfter(error?.response?.headers?.get('Retry-After')); |
| @@ -149,15 +196,9 @@ | ||
| 149 | 196 | pushStatus('credits-exhausted'); |
| 150 | 197 | return; |
| 151 | 198 | } |
| 152 | 199 | setCanType(true); |
| 153 | - if (error === 'Workflow aborted') { | |
| 154 | - addMessage('workflow', { | |
| 155 | - status: 'canceled', | |
| 156 | - suggestions: getSuggestions(), | |
| 157 | - }); | |
| 158 | - return; | |
| 159 | - } | |
| 200 | + if (error === 'Workflow aborted') return; | |
| 160 | 201 | |
| 161 | 202 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 162 | 203 | addMessage('message', { |
| 163 | 204 | role: 'assistant', |
| @@ -171,9 +212,9 @@ | ||
| 171 | 212 | return; |
| 172 | 213 | }), |
| 173 | 214 | 500, |
| 174 | 215 | ); |
| 175 | - if (!response) return; | |
| 216 | + if (!response || signal.aborted) return; | |
| 176 | 217 | |
| 177 | 218 | const { workflow: wf, reply } = response; |
| 178 | 219 | if (wf?.id) setWorkflow(wf); |
| 179 | 220 | if (reply) { |
| @@ -187,9 +228,8 @@ | ||
| 187 | 228 | pushStatus, |
| 188 | 229 | updateRetryAfter, |
| 189 | 230 | setWorkflow, |
| 190 | 231 | getAvailableWorkflows, |
| 191 | - getSuggestions, | |
| 192 | 232 | ], |
| 193 | 233 | ); |
| 194 | 234 | |
| 195 | 235 | // The backend caps tool runs; nothing here bounds the loop. |
| @@ -235,8 +275,13 @@ | ||
| 235 | 275 | followup: !!response.tool, |
| 236 | 276 | agent: workflow?.agent, |
| 237 | 277 | }); |
| 238 | 278 | } |
| 279 | + // The model is never told what is on screen around the canvas. | |
| 280 | + if (response.cannotHelp && !canvasNoticeShown.current) { | |
| 281 | + canvasNoticeShown.current = true; | |
| 282 | + addMessage('canvas-notice', {}); | |
| 283 | + } | |
| 239 | 284 | if (!response.tool) break; |
| 240 | 285 | const { id, inputs, labels } = response.tool; |
| 241 | 286 | pushStatus('tool-started', labels?.started); |
| 242 | 287 | const result = await callTool({ |
| @@ -257,18 +302,33 @@ | ||
| 257 | 302 | setCanType(true); |
| 258 | 303 | }, [addMessage, pushStatus, whenFinishedToolProps, workflow]); |
| 259 | 304 | |
| 260 | 305 | const handleSubmit = useCallback( |
| 261 | - async (message) => { | |
| 262 | - // Suggestions reach the agent without the textarea; disabling it isn't enough. | |
| 306 | + async (message, { hidden = false } = {}) => { | |
| 307 | + // chat-submit events skip the textarea; disabling it isn't enough. | |
| 263 | 308 | if (useQuickEditStore.getState().selected) return; |
| 309 | + if (whenFinishedToolProps?.processing) return; | |
| 264 | 310 | setWaitingOnToolOrUser(false); |
| 265 | 311 | agentWorking.current = false; |
| 266 | - addMessage('message', { role: 'user', content: message }); | |
| 312 | + addMessage('message', { role: 'user', content: message, hidden }); | |
| 267 | 313 | |
| 268 | 314 | // Without this a typed message would drop the workflow and close the canvas. |
| 269 | - if (canvasOpen) return handleCanvasMessage(); | |
| 315 | + if (canvasOpen && canvasAssist) return handleCanvasMessage(); | |
| 270 | 316 | |
| 317 | + // A staged tool the user walked away from still owes its receipt. | |
| 318 | + if (workflow?.id && whenFinishedToolProps?.id) { | |
| 319 | + const { answerId, whenFinishedTool } = | |
| 320 | + whenFinishedToolProps.agentResponse || {}; | |
| 321 | + addMessage('workflow', { | |
| 322 | + status: 'canceled', | |
| 323 | + label: whenFinishedTool?.labels?.cancel, | |
| 324 | + agent: workflow.agent, | |
| 325 | + workflowId: workflow.id, | |
| 326 | + answerId, | |
| 327 | + followUp: null, | |
| 328 | + }); | |
| 329 | + } | |
| 330 | + | |
| 271 | 331 | // Let some phrases auto load workflows |
| 272 | 332 | const bypass = getWorkflowByExample(message); |
| 273 | 333 | if (bypass?.example?.agentResponse) { |
| 274 | 334 | // whenFinishedTool → inline input UI; none → ask for a typed reply. |
| @@ -296,9 +356,9 @@ | ||
| 296 | 356 | |
| 297 | 357 | // Skip the network find-agent when the staged block makes the edit certain. |
| 298 | 358 | const localPick = localPickWorkflow({ block }); |
| 299 | 359 | if (localPick) { |
| 300 | - await new Promise((resolve) => setTimeout(resolve, 500)); | |
| 360 | + if (await canceledDuring(500)) return; | |
| 301 | 361 | setWorkflow(localPick); |
| 302 | 362 | return; |
| 303 | 363 | } |
| 304 | 364 | |
| @@ -306,8 +366,9 @@ | ||
| 306 | 366 | }, |
| 307 | 367 | [ |
| 308 | 368 | addMessage, |
| 309 | 369 | block, |
| 370 | + canvasAssist, | |
| 310 | 371 | canvasOpen, |
| 311 | 372 | findAgent, |
| 312 | 373 | handleCanvasMessage, |
| 313 | 374 | mergeWorkflowData, |
| @@ -326,9 +387,9 @@ | ||
| 326 | 387 | if (!agentResponse) return; |
| 327 | 388 | setWorkflow(workflow); |
| 328 | 389 | setCanType(false); |
| 329 | 390 | agentWorking.current = true; |
| 330 | - await new Promise((resolve) => setTimeout(resolve, 750)); | |
| 391 | + if (await canceledDuring(750)) return; | |
| 331 | 392 | addMessage('message', { |
| 332 | 393 | role: 'assistant', |
| 333 | 394 | content: agentResponse.reply, |
| 334 | 395 | }); |
| @@ -352,9 +413,9 @@ | ||
| 352 | 413 | // Without this the loop calls the backend before the user has typed. |
| 353 | 414 | setWaitingOnToolOrUser(true); |
| 354 | 415 | setCanType(false); |
| 355 | 416 | agentWorking.current = true; |
| 356 | - await new Promise((resolve) => setTimeout(resolve, 750)); | |
| 417 | + if (await canceledDuring(750)) return; | |
| 357 | 418 | addMessage('message', { |
| 358 | 419 | role: 'assistant', |
| 359 | 420 | content: agentResponse.reply, |
| 360 | 421 | // A workflow example can carry its own suggestions; none still asks. |
| @@ -372,12 +433,14 @@ | ||
| 372 | 433 | useEffect(() => { |
| 373 | 434 | // Allow external messages to trigger the agent |
| 374 | 435 | const handleMessage = ({ detail }) => { |
| 375 | 436 | if (!detail?.message) return; |
| 376 | - handleSubmit(detail.message); | |
| 437 | + handleSubmit(detail.message, { hidden: detail.hidden }); | |
| 377 | 438 | }; |
| 378 | 439 | // Allow external code to clear the block and workflow |
| 379 | 440 | const handleCleanup = () => { |
| 441 | + // cleanup() resets canType and agentWorking, so read them before it runs. | |
| 442 | + const interrupted = agentWorking.current || !canType; | |
| 380 | 443 | controller.abort('Workflow aborted'); |
| 381 | 444 | cleanup(); |
| 382 | 445 | // Deferred a frame: the input stays disabled until the cancel lands. |
| 383 | 446 | requestAnimationFrame(() => |
| @@ -384,15 +447,15 @@ | ||
| 384 | 447 | document.querySelector('#extendify-agent-chat-textarea')?.focus(), |
| 385 | 448 | ); |
| 386 | 449 | |
| 387 | 450 | // An options panel can outlive its workflow; cancel must still clear it. |
| 388 | - if (!workflow?.id && !qaSuggestions) return; | |
| 451 | + if (!workflow?.id && !qaSuggestions && !interrupted) return; | |
| 389 | 452 | setWorkflow(null); |
| 390 | 453 | addMessage('workflow', { |
| 391 | 454 | status: 'canceled', |
| 392 | 455 | agent: workflow?.agent, |
| 393 | 456 | workflowId: workflow?.id, |
| 394 | - suggestions: getSuggestions(), | |
| 457 | + ...nextSteps(workflow?.id, 'canceled'), | |
| 395 | 458 | }); |
| 396 | 459 | return; |
| 397 | 460 | }; |
| 398 | 461 | window.addEventListener('extendify-agent:cancel-workflow', handleCleanup); |
| @@ -410,25 +473,52 @@ | ||
| 410 | 473 | setWorkflow, |
| 411 | 474 | addMessage, |
| 412 | 475 | workflow, |
| 413 | 476 | qaSuggestions, |
| 414 | - getSuggestions, | |
| 477 | + canType, | |
| 415 | 478 | ]); |
| 416 | 479 | |
| 480 | + // Dispatching before chat-submit has a listener drops the workflow. | |
| 481 | + useEffect(() => { | |
| 482 | + if (!startOnboardingWorkflow.available()) return; | |
| 483 | + window.dispatchEvent( | |
| 484 | + new CustomEvent('extendify-agent:chat-submit', { | |
| 485 | + detail: { message: startOnboardingWorkflow.example.text, hidden: true }, | |
| 486 | + }), | |
| 487 | + ); | |
| 488 | + }, []); | |
| 489 | + | |
| 490 | + // ChatMessages hides a confirm restored by a reload; left open, the workflow would sit unfinishable. | |
| 491 | + useEffect(() => { | |
| 492 | + const { getWorkflow, reloadedToolProps } = useWorkflowStore.getState(); | |
| 493 | + const { whenFinished, needsRedirect } = getWorkflow() ?? {}; | |
| 494 | + if (!reloadedToolProps?.id || !whenFinished?.component) return; | |
| 495 | + if (whenFinished.canvas || needsRedirect?.()) return; | |
| 496 | + window.dispatchEvent(new Event('extendify-agent:cancel-workflow')); | |
| 497 | + }, []); | |
| 498 | + | |
| 417 | 499 | // Handle whenFinished component confirm/cancel |
| 418 | 500 | useEffect(() => { |
| 419 | 501 | const handleConfirm = async ({ detail }) => { |
| 420 | 502 | if (toolWorking.current) return; |
| 421 | - setWhenFinishedToolProps(null); | |
| 422 | 503 | toolWorking.current = true; |
| 423 | 504 | const { data, whenFinishedToolProps, shouldRefreshPage, redirectUrl } = |
| 424 | 505 | detail ?? {}; |
| 506 | + // Staged until the tool returns, so the chat shows it saving. | |
| 507 | + setWhenFinishedToolProps( | |
| 508 | + whenFinishedToolProps | |
| 509 | + ? { ...whenFinishedToolProps, processing: true } | |
| 510 | + : null, | |
| 511 | + ); | |
| 425 | 512 | const { whenFinishedTool, answerId, redirectTo } = |
| 426 | 513 | whenFinishedToolProps?.agentResponse || {}; |
| 427 | 514 | const { id, labels } = whenFinishedTool || {}; |
| 428 | 515 | // Staged unanswered so its own component can show the run in progress. |
| 429 | 516 | const runMessageId = id ? addMessage('tool', { id, inputs: data }) : null; |
| 430 | - if (!hasRunComponent(id)) pushStatus('workflow-tool-processing'); | |
| 517 | + // In the chat, the saving state shows this label instead. | |
| 518 | + if (!hasRunComponent(id) && workflow?.whenFinished?.canvas) { | |
| 519 | + pushStatus('workflow-tool-processing', labels?.started); | |
| 520 | + } | |
| 431 | 521 | // Not all workflows have a tool at the end (e.g. tours) |
| 432 | 522 | const toolResponse = await callTool?.({ tool: id, inputs: data }).catch( |
| 433 | 523 | (error) => { |
| 434 | 524 | const { sessionId } = workflow || {}; |
| @@ -444,8 +534,9 @@ | ||
| 444 | 534 | return { error: { message: error?.message, code: error?.code } }; |
| 445 | 535 | }, |
| 446 | 536 | ); |
| 447 | 537 | toolWorking.current = false; |
| 538 | + setWhenFinishedToolProps(null); | |
| 448 | 539 | if (runMessageId) updateMessage(runMessageId, { result: toolResponse }); |
| 449 | 540 | if (toolResponse?.refused) { |
| 450 | 541 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 451 | 542 | const refusalMessages = { |
| @@ -453,13 +544,8 @@ | ||
| 453 | 544 | 'no-op': __( |
| 454 | 545 | "That edit came back unchanged, which wasn't expected. Please try rephrasing what you'd like to change.", |
| 455 | 546 | 'extendify-local', |
| 456 | 547 | ), |
| 457 | - // translators: Shown when the user asked the AI agent to edit a block that lives in a site template (e.g. the header or footer), which the agent cannot save yet. | |
| 458 | - 'template-part': __( | |
| 459 | - "That block is part of your site's template, like the header or footer, and I can't make changes there yet.", | |
| 460 | - 'extendify-local', | |
| 461 | - ), | |
| 462 | 548 | }; |
| 463 | 549 | addMessage('message', { |
| 464 | 550 | role: 'assistant', |
| 465 | 551 | content: |
| @@ -483,9 +569,15 @@ | ||
| 483 | 569 | setLoop((prev) => prev + 1); |
| 484 | 570 | return; |
| 485 | 571 | } |
| 486 | 572 | |
| 487 | - addSuggestions(whenFinishedToolProps.agentResponse?.recommendations); | |
| 573 | + const url = getRedirectUrl(redirectTo, whenFinishedToolProps?.inputs); | |
| 574 | + // Reloading here unmounts a run component still watching its ability finish. | |
| 575 | + const refreshForAbility = | |
| 576 | + isAbilityTool(id) && shouldRefreshPage !== false; | |
| 577 | + const willReload = | |
| 578 | + url || redirectUrl || shouldRefreshPage || refreshForAbility; | |
| 579 | + // A card shown now would flash just before the reload. | |
| 488 | 580 | addMessage('workflow', { |
| 489 | 581 | status: 'completed', |
| 490 | 582 | label: labels?.confirm, |
| 491 | 583 | agent: workflow.agent, |
| @@ -490,9 +582,9 @@ | ||
| 490 | 582 | label: labels?.confirm, |
| 491 | 583 | agent: workflow.agent, |
| 492 | 584 | workflowId: workflow.id, |
| 493 | 585 | answerId, |
| 494 | - suggestions: getSuggestions(), | |
| 586 | + ...(willReload ? {} : nextSteps(workflow.id, 'completed')), | |
| 495 | 587 | }); |
| 496 | 588 | // A chat message persists, so the next turn's model knows the save was partial. |
| 497 | 589 | const refusedCount = toolResponse?.refusedOperations?.length; |
| 498 | 590 | if (refusedCount) { |
| @@ -512,13 +604,9 @@ | ||
| 512 | 604 | } |
| 513 | 605 | setWorkflow(null); |
| 514 | 606 | useCanvasStore.getState().endSession(); |
| 515 | 607 | |
| 516 | - const url = getRedirectUrl(redirectTo, whenFinishedToolProps?.inputs); | |
| 517 | - const refreshForAbility = isAbilityTool(id); | |
| 518 | - if (url || redirectUrl || shouldRefreshPage || refreshForAbility) { | |
| 519 | - return doReload(url || redirectUrl); | |
| 520 | - } | |
| 608 | + if (willReload) return doReload(url || redirectUrl); | |
| 521 | 609 | cleanup(); |
| 522 | 610 | }; |
| 523 | 611 | const handleCancel = ({ detail }) => { |
| 524 | 612 | if (toolWorking.current) return; |
| @@ -531,9 +619,9 @@ | ||
| 531 | 619 | label: whenFinishedTool?.labels?.cancel, |
| 532 | 620 | agent: workflow.agent, |
| 533 | 621 | workflowId: workflow.id, |
| 534 | 622 | answerId, |
| 535 | - suggestions: getSuggestions(), | |
| 623 | + ...nextSteps(workflow.id, 'canceled'), | |
| 536 | 624 | }); |
| 537 | 625 | setWorkflow(null); |
| 538 | 626 | useCanvasStore.getState().endSession(); |
| 539 | 627 | cleanup(); |
| @@ -558,18 +646,9 @@ | ||
| 558 | 646 | handleCancel, |
| 559 | 647 | ); |
| 560 | 648 | window.removeEventListener('extendify-agent:workflow-retry', handleRetry); |
| 561 | 649 | }; |
| 562 | - }, [ | |
| 563 | - addMessage, | |
| 564 | - pushStatus, | |
| 565 | - popMessage, | |
| 566 | - cleanup, | |
| 567 | - setWorkflow, | |
| 568 | - workflow, | |
| 569 | - getSuggestions, | |
| 570 | - addSuggestions, | |
| 571 | - ]); | |
| 650 | + }, [addMessage, pushStatus, popMessage, cleanup, setWorkflow, workflow]); | |
| 572 | 651 | |
| 573 | 652 | useEffect(() => { |
| 574 | 653 | const handleClose = () => setOpen(false); |
| 575 | 654 | const handleOpen = () => setOpen(true); |
| @@ -589,9 +668,26 @@ | ||
| 589 | 668 | if (open) return; |
| 590 | 669 | if (block) setBlock(null); |
| 591 | 670 | }, [open, block, setBlock]); |
| 592 | 671 | |
| 672 | + // A pick answers select-block's "which one?"; making the user also type stalls it. | |
| 673 | + const previousBlock = useRef(block); | |
| 593 | 674 | useEffect(() => { |
| 675 | + const picked = block && !previousBlock.current; | |
| 676 | + previousBlock.current = block; | |
| 677 | + if (!picked || workflow?.id !== 'select-block') return; | |
| 678 | + if (!waitingOnToolOrUser || !canType || whenFinishedToolProps?.id) return; | |
| 679 | + handleSubmit('This is the block I mean.', { hidden: true }); | |
| 680 | + }, [ | |
| 681 | + block, | |
| 682 | + workflow?.id, | |
| 683 | + waitingOnToolOrUser, | |
| 684 | + canType, | |
| 685 | + whenFinishedToolProps?.id, | |
| 686 | + handleSubmit, | |
| 687 | + ]); | |
| 688 | + | |
| 689 | + useEffect(() => { | |
| 594 | 690 | if (waitingOnToolOrUser || !open || !workflow?.id) return; |
| 595 | 691 | // Some workflows require they dont change pages |
| 596 | 692 | const theyMoved = workflow?.startingPage !== window.location.href; |
| 597 | 693 | // Requires a block to be selected |
| @@ -602,9 +698,9 @@ | ||
| 602 | 698 | addMessage('workflow', { |
| 603 | 699 | status: 'canceled', |
| 604 | 700 | agent: workflow.agent, |
| 605 | 701 | workflowId: workflow.id, |
| 606 | - suggestions: getSuggestions(), | |
| 702 | + ...nextSteps(workflow.id, 'canceled'), | |
| 607 | 703 | }); |
| 608 | 704 | setWorkflow(null); |
| 609 | 705 | cleanup(); |
| 610 | 706 | return; |
| @@ -679,9 +775,9 @@ | ||
| 679 | 775 | // translators: Shown while the AI agent deselects a block that can't serve the request. |
| 680 | 776 | __('Removing selected block', 'extendify-local'), |
| 681 | 777 | ); |
| 682 | 778 | // findAgent pushes its own status right away; let this one read first. |
| 683 | - await new Promise((resolve) => setTimeout(resolve, 2500)); | |
| 779 | + if (await canceledDuring(2500)) return; | |
| 684 | 780 | agentWorking.current = false; |
| 685 | 781 | await findAgent(); |
| 686 | 782 | return; |
| 687 | 783 | } |
| @@ -701,9 +797,8 @@ | ||
| 701 | 797 | status: 'completed', |
| 702 | 798 | inputs, |
| 703 | 799 | workflowId: workflow.id, |
| 704 | 800 | }); |
| 705 | - addSuggestions(agentResponse.recommendations); | |
| 706 | 801 | setWorkflow(null); |
| 707 | 802 | addMessage('workflow', { |
| 708 | 803 | status: 'completed', |
| 709 | 804 | agent: workflow.agent, |
| @@ -708,9 +803,9 @@ | ||
| 708 | 803 | status: 'completed', |
| 709 | 804 | agent: workflow.agent, |
| 710 | 805 | workflowId: workflow.id, |
| 711 | 806 | answerId, |
| 712 | - suggestions: getSuggestions(), | |
| 807 | + ...nextSteps(workflow.id, 'completed'), | |
| 713 | 808 | }); |
| 714 | 809 | cleanup(); |
| 715 | 810 | } |
| 716 | 811 | return; |
| @@ -716,19 +811,21 @@ | ||
| 716 | 811 | return; |
| 717 | 812 | } |
| 718 | 813 | // If we're done, it means the AI has the answer |
| 719 | 814 | if (agentResponse.status !== 'in-progress') { |
| 720 | - const { recommendations, status } = agentResponse; | |
| 721 | - const isCompleted = status === 'completed'; | |
| 722 | - if (recommendations) addSuggestions(recommendations); | |
| 815 | + const status = | |
| 816 | + agentResponse.status === 'completed' ? 'completed' : 'canceled'; | |
| 817 | + // A reply that changed nothing isn't a finished task to follow up. | |
| 818 | + const didWork = | |
| 819 | + toolCallsThisRun(useChatStore.getState().messages).length > 0; | |
| 723 | 820 | setWorkflow(null); |
| 724 | 821 | cleanup(); |
| 725 | 822 | addMessage('workflow', { |
| 726 | - status: isCompleted ? 'completed' : 'canceled', | |
| 823 | + status, | |
| 727 | 824 | agent: workflow.agent, |
| 728 | 825 | workflowId: workflow.id, |
| 729 | 826 | answerId, |
| 730 | - suggestions: getSuggestions(), | |
| 827 | + ...(didWork ? nextSteps(workflow.id, status) : { followUp: null }), | |
| 731 | 828 | }); |
| 732 | 829 | return; |
| 733 | 830 | } |
| 734 | 831 | if (sessionId && sessionId !== workflow.sessionId) { |
| @@ -748,9 +845,10 @@ | ||
| 748 | 845 | setWaitingOnToolOrUser(true); |
| 749 | 846 | setCanType(false); |
| 750 | 847 | return; |
| 751 | 848 | } |
| 752 | - pushStatus('tool-started', labels?.started); | |
| 849 | + // A reply sent with the tool clears the status, blanking the line. | |
| 850 | + pushStatus('agent-working'); | |
| 753 | 851 | const toolData = await Promise.all([ |
| 754 | 852 | callTool({ tool: id, inputs }), |
| 755 | 853 | new Promise((resolve) => setTimeout(resolve, 3000)), |
| 756 | 854 | ]) |
| @@ -781,8 +879,9 @@ | ||
| 781 | 879 | id, |
| 782 | 880 | inputs, |
| 783 | 881 | result: toolData, |
| 784 | 882 | label: labels?.confirm, |
| 883 | + started: labels?.started, | |
| 785 | 884 | }); |
| 786 | 885 | setWaitingOnToolOrUser(false); |
| 787 | 886 | agentWorking.current = false; |
| 788 | 887 | setLoop((prev) => prev + 1); // Trigger next loop |
| @@ -828,10 +927,8 @@ | ||
| 828 | 927 | setWhenFinishedToolProps, |
| 829 | 928 | block, |
| 830 | 929 | setBlock, |
| 831 | 930 | findAgent, |
| 832 | - addSuggestions, | |
| 833 | - getSuggestions, | |
| 834 | 931 | ]); |
| 835 | 932 | |
| 836 | 933 | useEffect(() => { |
| 837 | 934 | if (!canType) return; |
| @@ -837,9 +934,15 @@ | ||
| 837 | 934 | if (!canType) return; |
| 838 | 935 | document.querySelector('#extendify-agent-chat-textarea')?.focus(); |
| 839 | 936 | }, [canType]); |
| 840 | 937 | |
| 841 | - const busy = !canType || !chatAvailable || workflow?.id; | |
| 938 | + // A task waiting on a reply leaves the page pickable so the user can answer by pointing. | |
| 939 | + const waiting = | |
| 940 | + canType && | |
| 941 | + chatAvailable && | |
| 942 | + Boolean(workflow?.id) && | |
| 943 | + !whenFinishedToolProps?.id; | |
| 944 | + const busy = !canType || !chatAvailable || (workflow?.id && !waiting); | |
| 842 | 945 | // `busy` is true at rest; 429 is blocked, not working. |
| 843 | 946 | const working = !canType && chatAvailable; |
| 844 | 947 | |
| 845 | 948 | return ( |
| @@ -844,9 +947,14 @@ | ||
| 844 | 947 | |
| 845 | 948 | return ( |
| 846 | 949 | <> |
| 847 | 950 | <Canvas /> |
| 848 | - <Chat busy={busy} working={working}> | |
| 951 | + <Chat | |
| 952 | + busy={busy} | |
| 953 | + working={working} | |
| 954 | + waiting={waiting} | |
| 955 | + taskActive={Boolean(workflow?.id)} | |
| 956 | + > | |
| 849 | 957 | <div className="relative z-50 flex h-full flex-col justify-between overflow-auto"> |
| 850 | 958 | <ChatMessages |
| 851 | 959 | redirectComponent={ |
| 852 | 960 | workflow?.needsRedirect?.() ? workflow.redirectComponent : null |
| @@ -861,11 +969,16 @@ | ||
| 861 | 969 | }} |
| 862 | 970 | /> |
| 863 | 971 | </div> |
| 864 | 972 | <div className="p-4 pb-2 pt-0"> |
| 973 | + <StatusIndicator /> | |
| 865 | 974 | <ChatInput |
| 866 | 975 | disabled={ |
| 867 | - !canType || !chatAvailable || !!qaSuggestions || leavingPage | |
| 976 | + !canType || | |
| 977 | + !chatAvailable || | |
| 978 | + !!qaSuggestions || | |
| 979 | + leavingPage || | |
| 980 | + whenFinishedToolProps?.processing | |
| 868 | 981 | } |
| 869 | 982 | handleSubmit={handleSubmit} |
| 870 | 983 | /> |
| 871 | 984 | </div> |