PluginProbe
Extendify / 3.2.2
Extendify v3.2.2
3.2.2 3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 All 128 releases
← All changes | src/Agent/Agent.jsx +566 -163 3.1.0 → 3.2.2 View file →
@@ -1,21 +1,43 @@
1 1 import {
2 2 callTool,
3 + handleCanvas,
3 4 handleWorkflow,
4 5 pickWorkflow,
5 6 recordAgentActivity,
6 7 } from '@agent/api';
7 8 import { Chat } from '@agent/Chat';
9 +import {
10 + Canvas,
11 + useCanvasAssist,
12 + useCanvasOpen,
13 +} from '@agent/components/Canvas';
8 14 import { ChatInput } from '@agent/components/ChatInput';
9 15 import { ChatMessages } from '@agent/components/ChatMessages';
16 +import { StatusIndicator } from '@agent/components/messages/StatusIndicator';
10 17 import { UsageMessage } from '@agent/components/messages/UsageMessage';
11 -import { PageDocument } from '@agent/components/PageDocument';
18 +import { useFollowUpHistory } from '@agent/follow-ups/history';
19 +import {
20 + cardMessageIndex,
21 + pickNextCard,
22 + toolCallsThisRun,
23 +} from '@agent/follow-ups/pick-next';
12 24 import { useLockPost } from '@agent/hooks/useLockPost';
25 +import { isAbilityTool, isAbilityWorkflow } from '@agent/lib/abilities';
26 +import {
27 + getClientToolFallbackReply,
28 + getClientTools,
29 +} from '@agent/lib/client-tools';
30 +import { localPickWorkflow } from '@agent/lib/local-pick';
13 31 import { getRedirectUrl } from '@agent/lib/redirects';
32 +import { doReload } from '@agent/lib/reload';
33 +import { useCanvasStore } from '@agent/state/canvas';
14 34 import { useChatStore } from '@agent/state/chat';
15 35 import { useGlobalStore } from '@agent/state/global';
16 -import { useSuggestionsStore } from '@agent/state/suggestions';
36 +import { useStatusStore } from '@agent/state/status';
17 37 import { useWorkflowStore } from '@agent/state/workflows';
38 +import { hasRunComponent } from '@agent/workflows/abilities/components/run';
39 +import startOnboardingWorkflow from '@agent/workflows/misc/start-onboarding';
18 40 import { useQuickEditStore } from '@quick-edit/state/store';
19 41 import { digest } from '@shared/api/digest';
20 42 import {
21 43 useCallback,
@@ -23,17 +45,48 @@
23 45 useMemo,
24 46 useRef,
25 47 useState,
26 48 } from '@wordpress/element';
27 -import { __ } from '@wordpress/i18n';
49 +import { __, _n, sprintf } from '@wordpress/i18n';
28 50
29 51 const devmode = window.extSharedData.devbuild;
52 +// Logged with tool errors: the banner sends users here and support needs to
53 +// know which site the console they paste came from.
54 +const { siteId } = window.extSharedData;
30 55 // Used to abort when wf canceled - reset in cleanup()
31 56 let controller = new AbortController();
32 57 const { postId } = window?.extAgentData?.context || {};
33 58
59 +// Floor a resolution so its result doesn't re-render over the still-animating
60 +// scroll-to-top, which leaves the chat scrolled to the wrong place.
61 +const withMinDuration = async (promise, ms) => {
62 + const [result] = await Promise.all([
63 + promise,
64 + new Promise((resolve) => setTimeout(resolve, ms)),
65 + ]);
66 + return result;
67 +};
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 +
34 86 export const Agent = () => {
35 - const { addMessage, popMessage } = useChatStore();
87 + const { addMessage, updateMessage, popMessage, messages } = useChatStore();
88 + const { pushStatus, clearStatuses, leavingPage } = useStatusStore();
36 89 const {
37 90 mergeWorkflowData,
38 91 getWorkflow,
39 92 getWorkflowByExample,
@@ -41,12 +94,12 @@
41 94 setWorkflow,
42 95 setWhenFinishedToolProps,
43 96 whenFinishedToolProps,
44 97 getAvailableWorkflows,
98 + requireBlock,
45 99 } = useWorkflowStore();
46 100 const block = useQuickEditStore((s) => s.agentBlock);
47 101 const setBlock = useQuickEditStore((s) => s.setAgentBlock);
48 - const workflowIds = getAvailableWorkflows().map((w) => w.id);
49 102 const { open, setOpen, updateRetryAfter, isChatAvailable } = useGlobalStore();
50 103 useLockPost({ postId, enabled: !!open });
51 104 const [canType, setCanType] = useState(true);
52 105 const agentWorking = useRef(false);
@@ -51,67 +104,118 @@
51 104 const [canType, setCanType] = useState(true);
52 105 const agentWorking = useRef(false);
53 106 const toolWorking = useRef(false);
54 107 const retrying = useRef(false);
55 - const [waitingOnToolOrUser, setWaitingOnToolOrUser] = useState(false);
108 + // Starting false would re-run the agent's last turn on every reload.
109 + const [waitingOnToolOrUser, setWaitingOnToolOrUser] = useState(() => {
110 + const last = useChatStore.getState().messages.at(-1);
111 + if (last?.type === 'tool') return !('result' in (last.details ?? {}));
112 + return last?.type === 'message' && last.details?.role === 'assistant';
113 + });
56 114 const [loop, setLoop] = useState(0);
57 115 const workflow = getWorkflow();
116 + const canvasOpen = useCanvasOpen();
117 + const canvasAssist = useCanvasAssist();
118 + const canvasNoticeShown = useRef(false);
58 119 const chatAvailable = useMemo(() => isChatAvailable(), [isChatAvailable]);
59 - const { addSuggestions, getSuggestions } = useSuggestionsStore();
120 + // Options render only while their message is last; a reply dismisses them.
121 + const lastMessage = messages.at(-1);
122 + const qaSuggestions =
123 + lastMessage?.type === 'message' &&
124 + lastMessage.details?.role === 'assistant' &&
125 + Array.isArray(lastMessage.details?.qaSuggestions)
126 + ? lastMessage.details.qaSuggestions
127 + : null;
60 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 +
139 + // Without this the input stays disabled for as long as the canvas is open.
140 + useEffect(() => {
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]);
145 +
61 146 const cleanup = useCallback(() => {
62 147 setCanType(true);
63 148 agentWorking.current = false;
64 149 setWaitingOnToolOrUser(false);
65 150 controller = new AbortController();
66 - block && setBlock(null);
151 + const { agentBlock, setCommittedSelection } = useQuickEditStore.getState();
152 + agentBlock && setBlock(null);
153 + // A class or pin left up freezes Quick Edit hover site-wide.
154 + document
155 + .querySelector('.wp-site-blocks')
156 + ?.classList.remove(
157 + 'extendify-agent-working',
158 + 'extendify-agent-busy',
159 + 'extendify-agent-waiting',
160 + 'extendify-agent-task',
161 + );
162 + setCommittedSelection(null);
163 + clearStatuses();
67 164 window.dispatchEvent(new Event('extendify-agent:remove-block-highlight'));
68 - // scrollIntoView below walks up and scrolls the page itself,
69 - // fighting useLayoutShift's scroll restore when closing.
70 - if (!useGlobalStore.getState().open) return;
71 - const c = Array.from(
72 - document.querySelectorAll(
73 - '#extendify-agent-chat-scroll-area div:last-child',
74 - ),
75 - )?.at(-1);
76 - c?.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
77 - c?.scrollBy({ top: -5, behavior: 'smooth' });
78 - }, [setBlock, block]);
165 + }, [setBlock, clearStatuses]);
79 166
167 + useEffect(() => {
168 + const handle = ({ detail }) => {
169 + if (!detail?.id) return;
170 + updateMessage(detail.id, { result: detail.result });
171 + setWaitingOnToolOrUser(false);
172 + agentWorking.current = false;
173 + // The main loop parks on a staged tool; leaving one strands the run.
174 + setWhenFinishedToolProps(null);
175 + setLoop((prev) => prev + 1);
176 + };
177 + window.addEventListener('extendify-agent:client-tool-done', handle);
178 + return () =>
179 + window.removeEventListener('extendify-agent:client-tool-done', handle);
180 + }, [updateMessage, setWhenFinishedToolProps]);
181 +
80 182 const findAgent = useCallback(
81 183 async (options = {}) => {
82 - addMessage('status', { type: 'calling-agent' });
83 - const response = await pickWorkflow({
84 - workflows: workflowIds,
85 - options: { signal: controller.signal, ...options },
86 - }).catch(async (error) => {
87 - devmode && console.error(error);
88 - if (error?.response?.status === 429) {
89 - updateRetryAfter(error?.response?.headers?.get('Retry-After'));
90 - setCanType(false);
91 - addMessage('status', { type: 'credits-exhausted' });
184 + pushStatus('calling-agent');
185 + const { signal } = controller;
186 + const response = await withMinDuration(
187 + pickWorkflow({
188 + // A mid-turn drop clears the block after this closure was made.
189 + workflows: getAvailableWorkflows().map((w) => w.id),
190 + options: { signal, ...options },
191 + }).catch(async (error) => {
192 + devmode && console.error(error);
193 + if (error?.response?.status === 429) {
194 + updateRetryAfter(error?.response?.headers?.get('Retry-After'));
195 + setCanType(false);
196 + pushStatus('credits-exhausted');
197 + return;
198 + }
199 + setCanType(true);
200 + if (error === 'Workflow aborted') return;
201 +
202 + await new Promise((resolve) => setTimeout(resolve, 1000));
203 + addMessage('message', {
204 + role: 'assistant',
205 + // translators: This message is shown when the AI agent fails to find a suitable workflow.
206 + content: __(
207 + 'Something went wrong while trying to start this request. Please try again.',
208 + 'extendify-local',
209 + ),
210 + error: true,
211 + });
92 212 return;
93 - }
94 - setCanType(true);
95 - if (error === 'Workflow aborted') {
96 - addMessage('status', { type: 'workflow-canceled' });
97 - return;
98 - }
213 + }),
214 + 500,
215 + );
216 + if (!response || signal.aborted) return;
99 217
100 - await new Promise((resolve) => setTimeout(resolve, 1000));
101 - addMessage('message', {
102 - role: 'assistant',
103 - // translators: This message is shown when the AI agent fails to find a suitable workflow.
104 - content: __(
105 - 'Something went wrong while trying to start this request. Please try again.',
106 - 'extendify-local',
107 - ),
108 - error: true,
109 - });
110 - return;
111 - });
112 - if (!response) return;
113 -
114 218 const { workflow: wf, reply } = response;
115 219 if (wf?.id) setWorkflow(wf);
116 220 if (reply) {
117 221 const data = { role: 'assistant', content: reply, agent: wf?.agent };
@@ -118,26 +222,121 @@
118 222 addMessage('message', data);
119 223 }
120 224 if (!wf?.id) setCanType(true);
121 225 },
122 - [addMessage, updateRetryAfter, setWorkflow, workflowIds],
226 + [
227 + addMessage,
228 + pushStatus,
229 + updateRetryAfter,
230 + setWorkflow,
231 + getAvailableWorkflows,
232 + ],
123 233 );
124 234
235 + // The backend caps tool runs; nothing here bounds the loop.
236 + const handleCanvasMessage = useCallback(async () => {
237 + setCanType(false);
238 + // cleanup() replaces the controller, so a cancel is lost between passes.
239 + const { signal } = controller;
240 + while (!signal.aborted) {
241 + pushStatus('agent-working');
242 + const response = await handleCanvas({
243 + toolId: whenFinishedToolProps?.id,
244 + sessionId: workflow?.sessionId,
245 + abilities: workflow?.abilities,
246 + options: { signal },
247 + }).catch((error) => {
248 + if (error === 'Workflow aborted') return null;
249 + const { sessionId } = workflow || {};
250 + digest({
251 + error,
252 + details: { source: 'agent', caller: 'handle-canvas', sessionId },
253 + });
254 + devmode && console.error(error);
255 + return { error: error.message };
256 + });
257 + // A request that settled before the abort still resolves with a reply.
258 + if (!response || signal.aborted) break;
259 + if (response.error) {
260 + addMessage('message', {
261 + role: 'assistant',
262 + // translators: Shown when the AI agent could not answer a question about the form it has open on screen.
263 + content: __(
264 + 'Sorry, something went wrong. Please try asking again.',
265 + 'extendify-local',
266 + ),
267 + error: true,
268 + });
269 + break;
270 + }
271 + if (response.reply) {
272 + addMessage('message', {
273 + role: 'assistant',
274 + content: response.reply,
275 + followup: !!response.tool,
276 + agent: workflow?.agent,
277 + });
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 + }
284 + if (!response.tool) break;
285 + const { id, inputs, labels } = response.tool;
286 + pushStatus('tool-started', labels?.started);
287 + const result = await callTool({
288 + tool: id,
289 + inputs,
290 + abilities: workflow?.abilities,
291 + }).catch((error) => {
292 + const { sessionId } = workflow || {};
293 + digest({
294 + error,
295 + details: { source: 'agent', caller: `canvas: ${id}`, sessionId },
296 + });
297 + console.error(`Extendify agent tool error: ${id}`, { siteId, error });
298 + return { error: { message: error?.message, code: error?.code } };
299 + });
300 + addMessage('tool', { id, inputs, result, label: labels?.confirm });
301 + }
302 + setCanType(true);
303 + }, [addMessage, pushStatus, whenFinishedToolProps, workflow]);
304 +
125 305 const handleSubmit = useCallback(
126 - async (message) => {
127 - // Save any in-flight QE canvas edits before the agent runs so
128 - // the user doesn't lose their work to a workflow that touches
129 - // the same block. No-op when no QE canvas is mounted.
130 - window.dispatchEvent(
131 - new CustomEvent('extendify-quick-edit:agent-submit'),
132 - );
306 + async (message, { hidden = false } = {}) => {
307 + // chat-submit events skip the textarea; disabling it isn't enough.
308 + if (useQuickEditStore.getState().selected) return;
309 + if (whenFinishedToolProps?.processing) return;
133 310 setWaitingOnToolOrUser(false);
134 311 agentWorking.current = false;
135 - addMessage('message', { role: 'user', content: message });
312 + addMessage('message', { role: 'user', content: message, hidden });
136 313
314 + // Without this a typed message would drop the workflow and close the canvas.
315 + if (canvasOpen && canvasAssist) return handleCanvasMessage();
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 +
137 331 // Let some phrases auto load workflows
138 332 const bypass = getWorkflowByExample(message);
139 - if (bypass?.example?.agentResponse) return handleBypass(bypass);
333 + if (bypass?.example?.agentResponse) {
334 + // whenFinishedTool → inline input UI; none → ask for a typed reply.
335 + return bypass.example.agentResponse.whenFinishedTool
336 + ? handleBypass(bypass)
337 + : handleInstantAsk(bypass);
338 + }
140 339
141 340 setCanType(false);
142 341 // If they typed while waiting on a redirect, reset the workflow
143 342 const redirect = workflow?.needsRedirect?.();
@@ -154,13 +353,25 @@
154 353 mergeWorkflowData(wfData);
155 354 return;
156 355 }
157 356
357 + // Skip the network find-agent when the staged block makes the edit certain.
358 + const localPick = localPickWorkflow({ block });
359 + if (localPick) {
360 + if (await canceledDuring(500)) return;
361 + setWorkflow(localPick);
362 + return;
363 + }
364 +
158 365 await findAgent().catch((e) => devmode && console.error(e));
159 366 },
160 367 [
161 368 addMessage,
369 + block,
370 + canvasAssist,
371 + canvasOpen,
162 372 findAgent,
373 + handleCanvasMessage,
163 374 mergeWorkflowData,
164 375 whenFinishedToolProps,
165 376 setWorkflow,
166 377 workflow,
@@ -176,9 +387,9 @@
176 387 if (!agentResponse) return;
177 388 setWorkflow(workflow);
178 389 setCanType(false);
179 390 agentWorking.current = true;
180 - await new Promise((resolve) => setTimeout(resolve, 750));
391 + if (await canceledDuring(750)) return;
181 392 addMessage('message', {
182 393 role: 'assistant',
183 394 content: agentResponse.reply,
184 395 });
@@ -192,22 +403,60 @@
192 403 value: { workflow: workflow?.id },
193 404 });
194 405 }, []);
195 406
407 + // Ask the user, then let the normal loop handle their typed reply.
408 + const handleInstantAsk = useCallback(async (workflow) => {
409 + const agentResponse = workflow.example?.agentResponse;
410 + cleanup();
411 + if (!agentResponse) return;
412 + setWorkflow(workflow);
413 + // Without this the loop calls the backend before the user has typed.
414 + setWaitingOnToolOrUser(true);
415 + setCanType(false);
416 + agentWorking.current = true;
417 + if (await canceledDuring(750)) return;
418 + addMessage('message', {
419 + role: 'assistant',
420 + content: agentResponse.reply,
421 + // A workflow example can carry its own suggestions; none still asks.
422 + qaSuggestions: agentResponse.qaSuggestions ?? [],
423 + });
424 + agentWorking.current = false;
425 + setCanType(true);
426 + recordAgentActivity({
427 + sessionId: workflow?.sessionId,
428 + action: 'workflow_instant_ask',
429 + value: { workflow: workflow?.id },
430 + });
431 + }, []);
432 +
196 433 useEffect(() => {
197 434 // Allow external messages to trigger the agent
198 435 const handleMessage = ({ detail }) => {
199 436 if (!detail?.message) return;
200 - handleSubmit(detail.message);
437 + handleSubmit(detail.message, { hidden: detail.hidden });
201 438 };
202 439 // Allow external code to clear the block and workflow
203 440 const handleCleanup = () => {
441 + // cleanup() resets canType and agentWorking, so read them before it runs.
442 + const interrupted = agentWorking.current || !canType;
204 443 controller.abort('Workflow aborted');
205 444 cleanup();
445 + // Deferred a frame: the input stays disabled until the cancel lands.
446 + requestAnimationFrame(() =>
447 + document.querySelector('#extendify-agent-chat-textarea')?.focus(),
448 + );
206 449
207 - if (!workflow?.id) return;
450 + // An options panel can outlive its workflow; cancel must still clear it.
451 + if (!workflow?.id && !qaSuggestions && !interrupted) return;
208 452 setWorkflow(null);
209 - addMessage('status', { type: 'workflow-canceled' });
453 + addMessage('workflow', {
454 + status: 'canceled',
455 + agent: workflow?.agent,
456 + workflowId: workflow?.id,
457 + ...nextSteps(workflow?.id, 'canceled'),
458 + });
210 459 return;
211 460 };
212 461 window.addEventListener('extendify-agent:cancel-workflow', handleCleanup);
213 462 window.addEventListener('extendify-agent:chat-submit', handleMessage);
@@ -217,22 +466,59 @@
217 466 handleCleanup,
218 467 );
219 468 window.removeEventListener('extendify-agent:chat-submit', handleMessage);
220 469 };
221 - }, [handleSubmit, cleanup, setWorkflow, addMessage, workflow]);
470 + }, [
471 + handleSubmit,
472 + cleanup,
473 + setWorkflow,
474 + addMessage,
475 + workflow,
476 + qaSuggestions,
477 + canType,
478 + ]);
222 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 +
223 499 // Handle whenFinished component confirm/cancel
224 500 useEffect(() => {
225 501 const handleConfirm = async ({ detail }) => {
226 502 if (toolWorking.current) return;
227 - setWhenFinishedToolProps(null);
228 - addMessage('status', { type: 'workflow-tool-processing' });
229 503 toolWorking.current = true;
230 504 const { data, whenFinishedToolProps, shouldRefreshPage, redirectUrl } =
231 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 + );
232 512 const { whenFinishedTool, answerId, redirectTo } =
233 513 whenFinishedToolProps?.agentResponse || {};
234 514 const { id, labels } = whenFinishedTool || {};
515 + // Staged unanswered so its own component can show the run in progress.
516 + const runMessageId = id ? addMessage('tool', { id, inputs: data }) : null;
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 + }
235 521 // Not all workflows have a tool at the end (e.g. tours)
236 522 const toolResponse = await callTool?.({ tool: id, inputs: data }).catch(
237 523 (error) => {
238 524 const { sessionId } = workflow || {};
@@ -243,22 +529,33 @@
243 529 caller: `when-finished: ${id}`,
244 530 sessionId,
245 531 },
246 532 });
247 - devmode && console.error(error);
248 - return { error: error.message };
533 + console.error(`Extendify agent tool error: ${id}`, { siteId, error });
534 + return { error: { message: error?.message, code: error?.code } };
249 535 },
250 536 );
251 537 toolWorking.current = false;
252 - if (toolResponse?.error) {
538 + setWhenFinishedToolProps(null);
539 + if (runMessageId) updateMessage(runMessageId, { result: toolResponse });
540 + if (toolResponse?.refused) {
253 541 await new Promise((resolve) => setTimeout(resolve, 1000));
542 + const refusalMessages = {
543 + // translators: Shown when the AI agent's edit produced no change to the block, which is unexpected.
544 + 'no-op': __(
545 + "That edit came back unchanged, which wasn't expected. Please try rephrasing what you'd like to change.",
546 + 'extendify-local',
547 + ),
548 + };
254 549 addMessage('message', {
255 550 role: 'assistant',
256 - // translators: This message is shown when the AI agent fails to confirm an action.
257 - content: __(
258 - 'Sorry, something went wrong attempting to call the tool. Please try again.',
259 - 'extendify-local',
260 - ),
551 + content:
552 + refusalMessages[toolResponse.reason] ??
553 + // translators: Shown when the AI agent could not safely apply an edit to the selected block.
554 + __(
555 + "I couldn't safely apply that edit to the selected block. Please re-select the block and try again.",
556 + 'extendify-local',
557 + ),
261 558 error: true,
262 559 });
263 560 setWorkflow(null);
264 561 cleanup();
@@ -263,48 +560,71 @@
263 560 setWorkflow(null);
264 561 cleanup();
265 562 return;
266 563 }
267 - addMessage('status', {
268 - label: labels?.confirm,
269 - type: 'workflow-tool-completed',
270 - });
271 - addSuggestions(whenFinishedToolProps.agentResponse?.recommendations);
564 + // Only loop back on error, so the model can recover. A clean
565 + // whenFinished tool means the workflow is done.
566 + if (toolResponse?.error) {
567 + setWaitingOnToolOrUser(false);
568 + agentWorking.current = false;
569 + setLoop((prev) => prev + 1);
570 + return;
571 + }
572 +
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.
272 580 addMessage('workflow', {
273 581 status: 'completed',
582 + label: labels?.confirm,
274 583 agent: workflow.agent,
584 + workflowId: workflow.id,
275 585 answerId,
276 - suggestions: getSuggestions(),
586 + ...(willReload ? {} : nextSteps(workflow.id, 'completed')),
277 587 });
588 + // A chat message persists, so the next turn's model knows the save was partial.
589 + const refusedCount = toolResponse?.refusedOperations?.length;
590 + if (refusedCount) {
591 + addMessage('message', {
592 + role: 'assistant',
593 + content: sprintf(
594 + // translators: %d is how many of the requested edits were not applied.
595 + _n(
596 + "Heads up — %d of those changes couldn't be applied. If something still looks the same, ask me to redo just that part.",
597 + "Heads up — %d of those changes couldn't be applied. If something still looks the same, ask me to redo just those parts.",
598 + refusedCount,
599 + 'extendify-local',
600 + ),
601 + refusedCount,
602 + ),
603 + });
604 + }
278 605 setWorkflow(null);
606 + useCanvasStore.getState().endSession();
279 607
280 - const url = getRedirectUrl(redirectTo, whenFinishedToolProps?.inputs);
281 -
282 - if (url || redirectUrl || shouldRefreshPage) {
283 - await new Promise((resolve) => setTimeout(resolve, 1000));
284 - }
285 -
286 - if (url) return window.location.assign(url);
287 - if (redirectUrl) return window.location.assign(redirectUrl);
288 - if (shouldRefreshPage) return window.location.reload();
289 - // Clean up if not redirecting
608 + if (willReload) return doReload(url || redirectUrl);
290 609 cleanup();
291 610 };
292 611 const handleCancel = ({ detail }) => {
293 612 if (toolWorking.current) return;
613 + // Without this a canvas closed mid-turn keeps replying into the chat.
614 + controller.abort('Workflow aborted');
294 615 const { answerId, whenFinishedTool } =
295 616 detail.whenFinishedToolProps?.agentResponse || {};
296 - addMessage('status', {
297 - type: 'workflow-canceled',
298 - label: whenFinishedTool?.labels?.cancel,
299 - });
300 617 addMessage('workflow', {
301 618 status: 'canceled',
619 + label: whenFinishedTool?.labels?.cancel,
302 620 agent: workflow.agent,
621 + workflowId: workflow.id,
303 622 answerId,
304 - suggestions: getSuggestions(),
623 + ...nextSteps(workflow.id, 'canceled'),
305 624 });
306 625 setWorkflow(null);
626 + useCanvasStore.getState().endSession();
307 627 cleanup();
308 628 };
309 629 const handleRetry = () => {
310 630 popMessage();
@@ -326,17 +646,9 @@
326 646 handleCancel,
327 647 );
328 648 window.removeEventListener('extendify-agent:workflow-retry', handleRetry);
329 649 };
330 - }, [
331 - addMessage,
332 - popMessage,
333 - cleanup,
334 - setWorkflow,
335 - workflow,
336 - getSuggestions,
337 - addSuggestions,
338 - ]);
650 + }, [addMessage, pushStatus, popMessage, cleanup, setWorkflow, workflow]);
339 651
340 652 useEffect(() => {
341 653 const handleClose = () => setOpen(false);
342 654 const handleOpen = () => setOpen(true);
@@ -356,9 +668,26 @@
356 668 if (open) return;
357 669 if (block) setBlock(null);
358 670 }, [open, block, setBlock]);
359 671
672 + // A pick answers select-block's "which one?"; making the user also type stalls it.
673 + const previousBlock = useRef(block);
360 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(() => {
361 690 if (waitingOnToolOrUser || !open || !workflow?.id) return;
362 691 // Some workflows require they dont change pages
363 692 const theyMoved = workflow?.startingPage !== window.location.href;
364 693 // Requires a block to be selected
@@ -368,9 +697,10 @@
368 697 if (cancelWorkflow) {
369 698 addMessage('workflow', {
370 699 status: 'canceled',
371 700 agent: workflow.agent,
372 - suggestions: getSuggestions(),
701 + workflowId: workflow.id,
702 + ...nextSteps(workflow.id, 'canceled'),
373 703 });
374 704 setWorkflow(null);
375 705 cleanup();
376 706 return;
@@ -386,20 +716,16 @@
386 716 if (agentWorking.current) return; // Prevent multiple calls
387 717 if (toolWorking.current) return;
388 718 setCanType(false);
389 719 agentWorking.current = true;
390 - addMessage('status', { type: 'agent-working' });
720 + pushStatus('agent-working');
391 721 const agentResponse = await handleWorkflow({
392 722 workflow,
393 723 workflowData,
394 724 options: { signal: controller.signal, retry: retrying.current },
395 725 }).catch((error) => {
396 - if (error === 'Workflow aborted') {
397 - addMessage('status', { type: 'workflow-canceled' });
398 - setWorkflow(null);
399 - cleanup();
400 - return;
401 - }
726 + // handleCleanup already added the canceled message
727 + if (error === 'Workflow aborted') return;
402 728 const { sessionId } = workflow || {};
403 729 digest({
404 730 error,
405 731 details: { source: 'agent', caller: `handle-workflow`, sessionId },
@@ -418,14 +744,20 @@
418 744 window.extAgentData.failedWorkflows.add(workflow.id);
419 745 throw new Error(`Error handling workflow: ${agentResponse.error}`);
420 746 }
421 747 // The ai sent back some text to show to the user
422 - if (agentResponse.reply) {
748 + const reply =
749 + agentResponse.reply ??
750 + (agentResponse.tool
751 + ? getClientToolFallbackReply(agentResponse.tool.id)
752 + : null);
753 + if (reply) {
423 754 addMessage('message', {
424 755 role: 'assistant',
425 - content: agentResponse.reply,
756 + content: reply,
426 757 followup: !!agentResponse.tool,
427 758 pageSuggestion: agentResponse.pageSuggestion,
759 + qaSuggestions: agentResponse.qaSuggestions,
428 760 agent: workflow.agent,
429 761 sessionId: workflow?.sessionId,
430 762 workflowId: workflow?.id,
431 763 language: workflow?.language,
@@ -430,8 +762,26 @@
430 762 workflowId: workflow?.id,
431 763 language: workflow?.language,
432 764 });
433 765 }
766 + // Set when the request needs something no block edit can do.
767 + if (agentResponse.dropSelection) {
768 + setBlock(null);
769 + window.dispatchEvent(
770 + new Event('extendify-agent:remove-block-highlight'),
771 + );
772 + setWorkflow(null);
773 + pushStatus(
774 + 'tool-started',
775 + // translators: Shown while the AI agent deselects a block that can't serve the request.
776 + __('Removing selected block', 'extendify-local'),
777 + );
778 + // findAgent pushes its own status right away; let this one read first.
779 + if (await canceledDuring(2500)) return;
780 + agentWorking.current = false;
781 + await findAgent();
782 + return;
783 + }
434 784 // This is at the end of the workflow
435 785 // and we are about to execute the final tool
436 786 if (agentResponse.whenFinishedTool?.id) {
437 787 setWhenFinishedToolProps({
@@ -439,17 +789,23 @@
439 789 agentResponse,
440 790 });
441 791 // If static, add it as a message
442 792 const { id, inputs, static: staticC } = agentResponse.whenFinishedTool;
443 - if (staticC) {
444 - addMessage('workflow-component', { id, status: 'completed', inputs });
445 - addSuggestions(agentResponse.recommendations);
793 + // A canvas workflow renders in the canvas and ends on close or submit.
794 + if (staticC && !workflow.whenFinished?.canvas) {
795 + addMessage('workflow-component', {
796 + id,
797 + status: 'completed',
798 + inputs,
799 + workflowId: workflow.id,
800 + });
446 801 setWorkflow(null);
447 802 addMessage('workflow', {
448 803 status: 'completed',
449 804 agent: workflow.agent,
805 + workflowId: workflow.id,
450 806 answerId,
451 - suggestions: getSuggestions(),
807 + ...nextSteps(workflow.id, 'completed'),
452 808 });
453 809 cleanup();
454 810 }
455 811 return;
@@ -455,18 +811,21 @@
455 811 return;
456 812 }
457 813 // If we're done, it means the AI has the answer
458 814 if (agentResponse.status !== 'in-progress') {
459 - const { recommendations, status } = agentResponse;
460 - const isCompleted = status === 'completed';
461 - 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;
462 820 setWorkflow(null);
463 821 cleanup();
464 822 addMessage('workflow', {
465 - status: isCompleted ? 'completed' : 'canceled',
823 + status,
466 824 agent: workflow.agent,
825 + workflowId: workflow.id,
467 826 answerId,
468 - suggestions: getSuggestions(),
827 + ...(didWork ? nextSteps(workflow.id, status) : { followUp: null }),
469 828 });
470 829 return;
471 830 }
472 831 if (sessionId && sessionId !== workflow.sessionId) {
@@ -477,9 +836,19 @@
477 836 mergeWorkflowData(agentResponse.inputs);
478 837 // Agent needs more info from a
479 838 if (agentResponse.tool) {
480 839 const { id, inputs, labels } = agentResponse.tool;
481 - addMessage('status', { label: labels?.started, type: 'tool-started' });
840 + // A client tool is answered by in-chat UI, so the turn ends here.
841 + if (getClientTools().includes(id)) {
842 + addMessage('tool', { id, inputs });
843 + // Clearing agentWorking here fires a second turn: the wait flag
844 + // has not committed yet.
845 + setWaitingOnToolOrUser(true);
846 + setCanType(false);
847 + return;
848 + }
849 + // A reply sent with the tool clears the status, blanking the line.
850 + pushStatus('agent-working');
482 851 const toolData = await Promise.all([
483 852 callTool({ tool: id, inputs }),
484 853 new Promise((resolve) => setTimeout(resolve, 3000)),
485 854 ])
@@ -493,17 +862,27 @@
493 862 caller: `in-progress: ${id}`,
494 863 sessionId,
495 864 },
496 865 });
497 - devmode && console.error(error);
498 - throw error;
866 + console.error(`Extendify agent tool error: ${id}`, {
867 + siteId,
868 + error,
869 + });
870 + // Don't throw; the loop hands the error to the model.
871 + return { error: { message: error?.message, code: error?.code } };
499 872 });
500 - addMessage('status', {
873 + // do-when-finished spreads first-class workflowData into the tool.
874 + if (!toolData?.error && !isAbilityWorkflow(workflow.id)) {
875 + mergeWorkflowData(toolData);
876 + }
877 + if (toolData?.stagedBlockIds?.length) requireBlock();
878 + addMessage('tool', {
879 + id,
880 + inputs,
881 + result: toolData,
501 882 label: labels?.confirm,
502 - type: 'tool-completed',
883 + started: labels?.started,
503 884 });
504 - await new Promise((resolve) => setTimeout(resolve, 1000));
505 - mergeWorkflowData(toolData);
506 885 setWaitingOnToolOrUser(false);
507 886 agentWorking.current = false;
508 887 setLoop((prev) => prev + 1); // Trigger next loop
509 888 return;
@@ -536,18 +915,20 @@
536 915 open,
537 916 workflow,
538 917 workflowData,
539 918 addMessage,
919 + pushStatus,
540 920 setWorkflow,
541 921 agentWorking,
542 922 waitingOnToolOrUser,
543 923 mergeWorkflowData,
924 + requireBlock,
544 925 canType,
545 926 whenFinishedToolProps,
546 927 setWhenFinishedToolProps,
547 928 block,
548 - addSuggestions,
549 - getSuggestions,
929 + setBlock,
930 + findAgent,
550 931 ]);
551 932
552 933 useEffect(() => {
553 934 if (!canType) return;
@@ -553,41 +934,63 @@
553 934 if (!canType) return;
554 935 document.querySelector('#extendify-agent-chat-textarea')?.focus();
555 936 }, [canType]);
556 937
557 - 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);
945 + // `busy` is true at rest; 429 is blocked, not working.
946 + const working = !canType && chatAvailable;
558 947
559 948 return (
560 - <Chat busy={busy}>
561 - <div className="relative z-50 flex h-full flex-col justify-between overflow-auto">
562 - <ChatMessages
563 - redirectComponent={
564 - workflow?.needsRedirect?.() ? workflow.redirectComponent : null
565 - }
566 - />
567 - <div>
568 - <div className="relative flex flex-col px-4 pb-2 pt-2.5 shadow-lg-flipped">
569 - {block ? <PageDocument busy={busy} blockId={block.id} /> : null}
570 - <UsageMessage
571 - onReady={() => {
572 - cleanup();
573 - addMessage('status', { type: 'credits-restored' });
574 - }}
575 - />
949 + <>
950 + <Canvas />
951 + <Chat
952 + busy={busy}
953 + working={working}
954 + waiting={waiting}
955 + taskActive={Boolean(workflow?.id)}
956 + >
957 + <div className="relative z-50 flex h-full flex-col justify-between overflow-auto">
958 + <ChatMessages
959 + redirectComponent={
960 + workflow?.needsRedirect?.() ? workflow.redirectComponent : null
961 + }
962 + />
963 + <div>
964 + <div className="relative flex flex-col px-4 pb-2 pt-2.5 shadow-lg-flipped">
965 + <UsageMessage
966 + onReady={() => {
967 + cleanup();
968 + pushStatus('credits-restored');
969 + }}
970 + />
971 + </div>
972 + <div className="p-4 pb-2 pt-0">
973 + <StatusIndicator />
974 + <ChatInput
975 + disabled={
976 + !canType ||
977 + !chatAvailable ||
978 + !!qaSuggestions ||
979 + leavingPage ||
980 + whenFinishedToolProps?.processing
981 + }
982 + handleSubmit={handleSubmit}
983 + />
984 + </div>
985 + <div className="text-pretty px-4 pb-2 text-center text-xss leading-none text-gray-700">
986 + {__(
987 + 'AI Agent can make mistakes. Check changes before saving.',
988 + 'extendify-local',
989 + )}
990 + </div>
576 991 </div>
577 - <div className="p-4 pb-2 pt-0">
578 - <ChatInput
579 - disabled={!canType || !chatAvailable}
580 - handleSubmit={handleSubmit}
581 - />
582 - </div>
583 - <div className="text-pretty px-4 pb-2 text-center text-xss leading-none text-gray-700">
584 - {__(
585 - 'AI Agent can make mistakes. Check changes before saving.',
586 - 'extendify-local',
587 - )}
588 - </div>
589 992 </div>
590 - </div>
591 - </Chat>
993 + </Chat>
994 + </>
592 995 );
593 996 };