Stirling-PDF/frontend/src/hooks/tools/automate/useAutomateOperation.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-08-19 12:46:09 +01:00
import { useToolOperation } from '../shared/useToolOperation';
import { useCallback } from 'react';
2025-08-21 12:53:28 +01:00
import { executeAutomationSequence } from '../../../utils/automationExecutor';
2025-08-21 17:25:55 +01:00
import { useFlatToolRegistry } from '../../../data/useTranslatedToolRegistry';
2025-08-19 12:46:09 +01:00
interface AutomateParameters {
automationConfig?: any;
2025-08-21 17:25:55 +01:00
onStepStart?: (stepIndex: number, operationName: string) => void;
onStepComplete?: (stepIndex: number, resultFiles: File[]) => void;
onStepError?: (stepIndex: number, error: string) => void;
2025-08-19 12:46:09 +01:00
}
export function useAutomateOperation() {
2025-08-21 17:25:55 +01:00
const toolRegistry = useFlatToolRegistry();
2025-08-19 12:46:09 +01:00
const customProcessor = useCallback(async (params: AutomateParameters, files: File[]) => {
2025-08-21 12:53:28 +01:00
console.log('🚀 Starting automation execution via customProcessor', { params, files });
if (!params.automationConfig) {
throw new Error('No automation configuration provided');
}
// Execute the automation sequence and return the final results
const finalResults = await executeAutomationSequence(
params.automationConfig,
files,
2025-08-21 17:25:55 +01:00
toolRegistry,
2025-08-21 12:53:28 +01:00
(stepIndex: number, operationName: string) => {
console.log(`Step ${stepIndex + 1} started: ${operationName}`);
2025-08-21 17:25:55 +01:00
params.onStepStart?.(stepIndex, operationName);
2025-08-21 12:53:28 +01:00
},
(stepIndex: number, resultFiles: File[]) => {
console.log(`Step ${stepIndex + 1} completed with ${resultFiles.length} files`);
2025-08-21 17:25:55 +01:00
params.onStepComplete?.(stepIndex, resultFiles);
2025-08-21 12:53:28 +01:00
},
(stepIndex: number, error: string) => {
console.error(`Step ${stepIndex + 1} failed:`, error);
2025-08-21 17:25:55 +01:00
params.onStepError?.(stepIndex, error);
2025-08-21 12:53:28 +01:00
throw new Error(`Automation step ${stepIndex + 1} failed: ${error}`);
}
);
console.log(`✅ Automation completed, returning ${finalResults.length} files`);
return finalResults;
2025-08-21 17:25:55 +01:00
}, [toolRegistry]);
2025-08-19 12:46:09 +01:00
return useToolOperation<AutomateParameters>({
operationType: 'automate',
2025-08-21 12:53:28 +01:00
endpoint: '/api/v1/pipeline/handleData', // Not used with customProcessor
buildFormData: () => new FormData(), // Not used with customProcessor
2025-08-19 12:46:09 +01:00
customProcessor,
filePrefix: 'automated_'
});
}