PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
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 0.7.0 All 126 releases
extendify / src / Agent / state / workflows.js

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

123 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { deepMerge } from '@shared/lib/utils';
3 import { create } from 'zustand';
4 import { persist, devtools } from 'zustand/middleware';
5 import { workflows } from '@agent/workflows/workflows';
6
7 const state = (set, get) => ({
8 workflow: null,
9 block: null, // data-extendify-agent-block-id + details about the block
10 setBlock: (block) => set({ block }),
11 domToolEnabled: false,
12 setDomToolEnabled: (enabled) => {
13 if (get().block) return; // can't disable if a block is set
14 set({ domToolEnabled: enabled });
15 },
16 getWorkflow: () => {
17 const curr = get().workflow;
18 // Workflows may define a "parent" workflow via templateId
19 const currId = curr?.templateId || curr?.id;
20 const wf = workflows.find(({ id }) => id === currId);
21 if (!wf?.id) return curr || null;
22 return { ...deepMerge(curr, wf || {}), id: curr?.id };
23 },
24 // Gets the workflows available to the user
25 // TODO: maybe we need to have a way to include a
26 // workflow regardless of the block being active?
27 getAvailableWorkflows: () => {
28 let wfs = workflows.filter(({ available }) => available());
29 // If a block is set, only include those with 'block'
30 const blockWorkflows = wfs.filter(({ requires }) =>
31 requires?.includes('block'),
32 );
33 if (get().block) return blockWorkflows;
34 // otherwise remove all of the above
35 return wfs.filter(({ id }) => !blockWorkflows.some((w) => w.id === id));
36 },
37 getWorkflowsByFeature: ({ requires } = {}) => {
38 if (!requires) return workflows.filter(({ available }) => available());
39 // e.g. requires: ['block']
40 return workflows.filter(
41 ({ available, requires: workflowRequires }) =>
42 available() &&
43 (!requires || workflowRequires?.some((s) => requires.includes(s))),
44 );
45 },
46 workflowData: null,
47 // This is the history of the results
48 // { answerId: '', summary: '', canceled: false, reason: '', error: false, completed: false, whenFinishedTool: null }[]
49 workflowHistory: window.extAgentData?.workflowHistory || [],
50 // Data for the tool component that shows up at the end of a workflow
51 whenFinishedToolProps: null,
52 getWhenFinishedToolProps: () => {
53 const { whenFinishedToolProps } = get();
54 if (!whenFinishedToolProps) return null;
55 return {
56 ...whenFinishedToolProps,
57 onConfirm: (props = {}) => {
58 window.dispatchEvent(
59 new CustomEvent('extendify-agent:workflow-confirm', {
60 detail: { ...props, whenFinishedToolProps },
61 }),
62 );
63 },
64 onCancel: () => {
65 window.dispatchEvent(
66 new CustomEvent('extendify-agent:workflow-cancel', {
67 detail: { whenFinishedToolProps },
68 }),
69 );
70 },
71 };
72 },
73 addWorkflowResult: (data) => {
74 if (data.status === 'completed') {
75 set((state) => {
76 const max = Math.max(0, state.workflowHistory.length - 10);
77 return {
78 workflowHistory: [data, ...state.workflowHistory.toSpliced(0, max)],
79 };
80 });
81 }
82 const workflowId = get().workflow?.id;
83 if (!workflowId) return;
84 // Persist it to the server
85 const path = '/extendify/v1/agent/workflows';
86 apiFetch({
87 method: 'POST',
88 keepalive: true,
89 path,
90 data: { workflowId, ...data },
91 });
92 },
93 mergeWorkflowData: (data) => {
94 set((state) => {
95 if (!state.workflowData) return { workflowData: data };
96 return {
97 workflowData: { ...state.workflowData, ...data },
98 };
99 });
100 },
101 setWorkflow: (workflow) =>
102 set({
103 workflow: workflow
104 ? { ...workflow, startingPage: window.location.href }
105 : null,
106 workflowData: null,
107 whenFinishedToolProps: null,
108 }),
109 setWhenFinishedToolProps: (whenFinishedToolProps) =>
110 set({ whenFinishedToolProps }),
111 });
112
113 export const useWorkflowStore = create()(
114 persist(devtools(state, { name: 'Extendify Agent Workflows' }), {
115 name: `extendify-agent-workflows-${window.extSharedData.siteId}`,
116 partialize: (state) => {
117 // eslint-disable-next-line
118 const { block, workflowHistory, ...rest } = state;
119 return { ...rest };
120 },
121 }),
122 );
123