| 20 | const aiFlow = config.flows.analysis |
| 21 | |
| 22 | export async function executeAIProcessing(input: AIProcessingInput): Promise<{ |
| 23 | success: boolean |
| 24 | requestId?: string |
| 25 | error?: string |
| 26 | }> { |
| 27 | try { |
| 28 | console.log("Executing AI processing with Lamatic SDK") |
| 29 | console.log("Input:", input) |
| 30 | |
| 31 | // Prepare workflow input based on config schema |
| 32 | const workflowInput = Object.keys(config.flows.analysis.inputSchema).reduce( |
| 33 | (acc, key) => { |
| 34 | acc[key] = input[key as keyof AIProcessingInput] |
| 35 | return acc |
| 36 | }, |
| 37 | {} as Record<string, unknown>, |
| 38 | ) |
| 39 | |
| 40 | // Include data and outputFormat in workflow input |
| 41 | workflowInput.data = input.data |
| 42 | workflowInput.outputFormat = input.outputFormat |
| 43 | |
| 44 | console.log("Workflow input:", workflowInput) |
| 45 | |
| 46 | // Execute the workflow using Lamatic SDK |
| 47 | const response = await lamaticClient.executeFlow(aiFlow.workflowId, workflowInput) |
| 48 | |
| 49 | console.log("Lamatic SDK response:", response) |
| 50 | |
| 51 | const requestId = response?.result?.requestId || response?.requestId || response?.id |
| 52 | |
| 53 | if (!requestId) { |
| 54 | console.error("Response structure:", JSON.stringify(response, null, 2)) |
| 55 | throw new Error("No requestId returned from workflow") |
| 56 | } |
| 57 | |
| 58 | console.log("AI processing initiated with requestId:", requestId) |
| 59 | |
| 60 | return { |
| 61 | success: true, |
| 62 | requestId, |
| 63 | } |
| 64 | } catch (error) { |
| 65 | console.error("Error executing AI processing:", error) |
| 66 | |
| 67 | let errorMessage = "Unknown error occurred" |
| 68 | if (error instanceof Error) { |
| 69 | errorMessage = error.message |
| 70 | if (error.message.includes("fetch failed")) { |
| 71 | errorMessage = |
| 72 | "Network error: Unable to connect to Lamatic service. Please check your internet connection and try again." |
| 73 | } else if (error.message.includes("API key")) { |
| 74 | errorMessage = "Authentication error: Please check your LAMATIC_API_KEY configuration." |
| 75 | } else if (error.message.includes("timeout")) { |
| 76 | errorMessage = "Request timeout: The AI processing is taking longer than expected. Please try again." |
| 77 | } |
| 78 | } |
| 79 | |