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/state/workflows.js +60 -76 3.0.4 → 3.2.2 View file →
@@ -1,22 +1,12 @@
1 -import { isChangeSiteDesignWorkflowAvailable } from '@agent/lib/util';
2 -import changeSiteDesignWorkflow from '@agent/workflows/theme/change-site-design';
3 -import variationsWorkflow from '@agent/workflows/theme/change-theme-variation';
4 -import { workflows } from '@agent/workflows/workflows';
1 +import { isAbilityWorkflow } from '@agent/lib/abilities';
2 +import { AbilityRun } from '@agent/workflows/abilities/components/run';
3 +import { abilityWorkflows, workflows } from '@agent/workflows/workflows';
4 +import { useQuickEditStore } from '@quick-edit/state/store';
5 5 import { deepMerge } from '@shared/lib/utils';
6 -import apiFetch from '@wordpress/api-fetch';
7 6 import { create } from 'zustand';
8 7 import { devtools, persist } from 'zustand/middleware';
9 8
10 -const onboarding = window.extAgentData.chatHistory?.length === 0;
11 -const agentResponse = isChangeSiteDesignWorkflowAvailable()
12 - ? changeSiteDesignWorkflow.example?.agentResponse
13 - : variationsWorkflow.example?.agentResponse;
14 -const onboardingToolProps = {
15 - ...agentResponse.whenFinishedTool,
16 - agentResponse,
17 -};
18 -
19 9 // Used to check case-insensitive matches for workflow examples
20 10 const collator = new Intl.Collator(undefined, {
21 11 sensitivity: 'base',
22 12 usage: 'search',
@@ -25,24 +15,25 @@
25 15 const state = (set, get) => ({
26 16 workflow: null,
27 17 // Data for the tool component that shows up at the end of a workflow
28 18 whenFinishedToolProps: null,
29 - block: null, // data-extendify-agent-block-id + details about the block
30 - setBlock: (block) => set({ block, blockCode: null }),
31 - // Used as "previousContent" in workflows that need it
32 - blockCode: null,
33 - setBlockCode: (blockCode) => set({ blockCode }),
34 - domToolEnabled: false,
35 - setDomToolEnabled: (enabled) => {
36 - if (get().block) return; // can't disable if a block is set
37 - set({ domToolEnabled: enabled });
38 - },
19 + // A staged tool restored from storage; its confirm never renders.
20 + reloadedToolProps: null,
39 21 getWorkflow: () => {
40 22 const curr = get().workflow;
41 23 // Workflows may define a "parent" workflow via templateId
42 24 const currId = curr?.templateId || curr?.id;
43 25 const wf = workflows.find(({ id }) => id === currId);
44 - if (!wf?.id) return curr || null;
26 + if (!wf?.id) {
27 + if (!curr) return null;
28 + if (!isAbilityWorkflow(curr.id)) return curr;
29 + // The backend never sends whenFinished, so this supplies the gate.
30 + const ability = deepMerge(
31 + { whenFinished: { component: AbilityRun } },
32 + abilityWorkflows[curr.id] ?? {},
33 + );
34 + return { ...deepMerge(curr, ability), id: curr.id };
35 + }
45 36 return { ...deepMerge(curr, wf || {}), id: curr?.id };
46 37 },
47 38 getWorkflowByExample: (example) => {
48 39 const wf = workflows.find(
@@ -48,8 +39,9 @@
48 39 const wf = workflows.find(
49 40 ({ example: ex }) => collator.compare(ex?.text, example) === 0,
50 41 );
51 42 if (!wf?.id) return null;
43 + if (!wf.available()) return null;
52 44 return wf;
53 45 },
54 46 // Gets the workflows available to the user
55 47 // TODO: maybe we need to have a way to include a
@@ -59,9 +51,9 @@
59 51 // If a block is set, only include those with 'block'
60 52 const blockWorkflows = wfs.filter(({ requires }) =>
61 53 requires?.includes('block'),
62 54 );
63 - if (get().block) return blockWorkflows;
55 + if (useQuickEditStore.getState().agentBlock) return blockWorkflows;
64 56 // otherwise remove all of the above
65 57 return wfs.filter(({ id }) => !blockWorkflows.some((w) => w.id === id));
66 58 },
67 59 getWorkflowsByFeature: ({ requires } = {}) => {
@@ -73,37 +65,8 @@
73 65 (!requires || workflowRequires?.some((s) => requires.includes(s))),
74 66 );
75 67 },
76 68 workflowData: null,
77 - // This is the history of the results
78 - // { answerId: '', canceled: false, reason: '', error: false, completed: false, whenFinishedTool: null }[]
79 - workflowHistory: window.extAgentData?.workflowHistory || [],
80 - addWorkflowResult: (data) => {
81 - const workflowId = get().workflow?.id;
82 -
83 - if (data.status === 'completed') {
84 - set((state) => {
85 - const max = Math.max(0, state.workflowHistory.length - 10);
86 -
87 - return {
88 - workflowHistory: [
89 - { workflowId, ...data },
90 - ...state.workflowHistory.toSpliced(0, max),
91 - ],
92 - };
93 - });
94 - }
95 -
96 - if (!workflowId) return;
97 - // Persist it to the server
98 - const path = '/extendify/v1/agent/workflows';
99 - apiFetch({
100 - method: 'POST',
101 - keepalive: true,
102 - path,
103 - data: { workflowId, ...data },
104 - });
105 - },
106 69 mergeWorkflowData: (data) => {
107 70 set((state) => {
108 71 if (!state.workflowData) return { workflowData: data };
109 72 return {
@@ -110,22 +73,43 @@
110 73 workflowData: { ...state.workflowData, ...data },
111 74 };
112 75 });
113 76 },
114 - setWorkflow: (workflow) =>
77 + setWorkflow: (workflow) => {
78 + const agentBlockCode = useQuickEditStore.getState().agentBlockCode;
79 + const started = workflow
80 + ? { ...workflow, startingPage: window.location.href }
81 + : null;
115 82 set({
116 - workflow: workflow
117 - ? { ...workflow, startingPage: window.location.href }
118 - : null,
83 + workflow: started,
119 84 // If a block is selected, add it to the workflow data
120 85 // previousContent is named this way for legacy reasons
121 - workflowData: get().blockCode
122 - ? { previousContent: get().blockCode }
123 - : null,
86 + workflowData: agentBlockCode ? { previousContent: agentBlockCode } : null,
124 87 whenFinishedToolProps: null,
125 - }),
88 + });
89 + if (!started) return;
90 + const workflowStarted = get().getWorkflow();
91 + window.dispatchEvent(
92 + new CustomEvent('extendify-workflow::started', {
93 + detail: workflowStarted,
94 + }),
95 + );
96 + try {
97 + workflowStarted?.onStarted?.(workflowStarted);
98 + } catch {
99 + // A workflow that can't read the page still has to start.
100 + }
101 + },
126 102 setWhenFinishedToolProps: (whenFinishedToolProps) =>
127 103 set({ whenFinishedToolProps }),
104 + // Declared up front it would hide the workflow while nothing is staged.
105 + // Not setWorkflow: that resets workflowData and drops the run's data.
106 + requireBlock: () =>
107 + set((state) =>
108 + state.workflow
109 + ? { workflow: { ...state.workflow, requires: ['block'] } }
110 + : {},
111 + ),
128 112 });
129 113
130 114 export const useWorkflowStore = create()(
131 115 persist(devtools(state, { name: 'Extendify Agent Workflows' }), {
@@ -130,23 +114,23 @@
130 114 export const useWorkflowStore = create()(
131 115 persist(devtools(state, { name: 'Extendify Agent Workflows' }), {
132 116 name: `extendify-agent-workflows-${window.extSharedData.siteId}`,
133 117 merge: (persistedState, currentState) => {
134 - // if we are in onboarding mode, add the starting workflow
135 - if (onboarding) {
118 + const merged = { ...currentState, ...persistedState };
119 + // A reload can't carry the staged block a block-patching workflow
120 + // depends on, so a rehydrated-open one is always stale. A canvas
121 + // session is stale too: its field values never persist.
122 + const id = merged.workflow?.templateId || merged.workflow?.id;
123 + const registered = workflows.find((wf) => wf.id === id);
124 + if (id === 'block-patching' || registered?.whenFinished?.canvas) {
136 125 return {
137 - ...currentState,
138 - ...persistedState,
139 - workflow: isChangeSiteDesignWorkflowAvailable()
140 - ? changeSiteDesignWorkflow
141 - : variationsWorkflow,
142 - whenFinishedToolProps: onboardingToolProps,
126 + ...merged,
127 + workflow: null,
128 + whenFinishedToolProps: null,
129 + workflowData: null,
143 130 };
144 131 }
145 - return { ...currentState, ...persistedState };
132 + return { ...merged, reloadedToolProps: merged.whenFinishedToolProps };
146 133 },
147 - partialize: (state) => {
148 - const { block, workflowHistory, ...rest } = state;
149 - return { ...rest };
150 - },
134 + partialize: ({ reloadedToolProps, ...persisted }) => persisted,
151 135 }),
152 136 );