PluginProbe
Extendify / 3.1.0
Extendify v3.1.0
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 0.6.0 All 127 releases
extendify / src / Agent / state / workflows.js

workflows.js in Extendify 3.1.0, at src/Agent/state/workflows.js

110 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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';
5 import { useQuickEditStore } from '@quick-edit/state/store';
6 import { deepMerge } from '@shared/lib/utils';
7 import { create } from 'zustand';
8 import { devtools, persist } from 'zustand/middleware';
9
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 // Used to check case-insensitive matches for workflow examples
20 const collator = new Intl.Collator(undefined, {
21 sensitivity: 'base',
22 usage: 'search',
23 });
24
25 const state = (set, get) => ({
26 workflow: null,
27 // Data for the tool component that shows up at the end of a workflow
28 whenFinishedToolProps: null,
29 getWorkflow: () => {
30 const curr = get().workflow;
31 // Workflows may define a "parent" workflow via templateId
32 const currId = curr?.templateId || curr?.id;
33 const wf = workflows.find(({ id }) => id === currId);
34 if (!wf?.id) return curr || null;
35 return { ...deepMerge(curr, wf || {}), id: curr?.id };
36 },
37 getWorkflowByExample: (example) => {
38 const wf = workflows.find(
39 ({ example: ex }) => collator.compare(ex?.text, example) === 0,
40 );
41 if (!wf?.id) return null;
42 return wf;
43 },
44 // Gets the workflows available to the user
45 // TODO: maybe we need to have a way to include a
46 // workflow regardless of the block being active?
47 getAvailableWorkflows: () => {
48 const wfs = workflows.filter(({ available }) => available());
49 // If a block is set, only include those with 'block'
50 const blockWorkflows = wfs.filter(({ requires }) =>
51 requires?.includes('block'),
52 );
53 if (useQuickEditStore.getState().agentBlock) return blockWorkflows;
54 // otherwise remove all of the above
55 return wfs.filter(({ id }) => !blockWorkflows.some((w) => w.id === id));
56 },
57 getWorkflowsByFeature: ({ requires } = {}) => {
58 if (!requires) return workflows.filter(({ available }) => available());
59 // e.g. requires: ['block']
60 return workflows.filter(
61 ({ available, requires: workflowRequires }) =>
62 available() &&
63 (!requires || workflowRequires?.some((s) => requires.includes(s))),
64 );
65 },
66 workflowData: null,
67 mergeWorkflowData: (data) => {
68 set((state) => {
69 if (!state.workflowData) return { workflowData: data };
70 return {
71 workflowData: { ...state.workflowData, ...data },
72 };
73 });
74 },
75 setWorkflow: (workflow) => {
76 const agentBlockCode = useQuickEditStore.getState().agentBlockCode;
77 set({
78 workflow: workflow
79 ? { ...workflow, startingPage: window.location.href }
80 : null,
81 // If a block is selected, add it to the workflow data
82 // previousContent is named this way for legacy reasons
83 workflowData: agentBlockCode ? { previousContent: agentBlockCode } : null,
84 whenFinishedToolProps: null,
85 });
86 },
87 setWhenFinishedToolProps: (whenFinishedToolProps) =>
88 set({ whenFinishedToolProps }),
89 });
90
91 export const useWorkflowStore = create()(
92 persist(devtools(state, { name: 'Extendify Agent Workflows' }), {
93 name: `extendify-agent-workflows-${window.extSharedData.siteId}`,
94 merge: (persistedState, currentState) => {
95 // if we are in onboarding mode, add the starting workflow
96 if (onboarding) {
97 return {
98 ...currentState,
99 ...persistedState,
100 workflow: isChangeSiteDesignWorkflowAvailable()
101 ? changeSiteDesignWorkflow
102 : variationsWorkflow,
103 whenFinishedToolProps: onboardingToolProps,
104 };
105 }
106 return { ...currentState, ...persistedState };
107 },
108 }),
109 );
110