From 821104b4c3d2e58460304ed6f2453e23d4ac5223 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Sat, 12 Oct 2024 15:48:41 +0530 Subject: [PATCH] feat: createWorkflowAndStoreMetadata for robot access via api --- server/src/api/record.ts | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/server/src/api/record.ts b/server/src/api/record.ts index 9d8dfa29..d5876a11 100644 --- a/server/src/api/record.ts +++ b/server/src/api/record.ts @@ -1,9 +1,14 @@ import { readFile, readFiles } from "../workflow-management/storage"; import { Router, Request, Response } from 'express'; +import { chromium } from "playwright"; import { requireAPIKey } from "../middlewares/api"; import Robot from "../models/Robot"; import Run from "../models/Run"; const router = Router(); +import { getDecryptedProxyConfig } from "../routes/proxy"; +import { uuid } from "uuidv4"; +import { createRemoteBrowserForRun, destroyRemoteBrowser } from "../browser-management/controller"; +import logger from "../logger"; const formatRecording = (recordingData: any) => { const recordingMeta = recordingData.recording_meta; @@ -164,4 +169,100 @@ router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: R } }); +async function createWorkflowAndStoreMetadata(id: string, userId: string) { + if (!id) { + id = uuid(); + } + + const recording = await Robot.findOne({ + where: { + 'recording_meta.id': id + }, + raw: true + }); + + if (!recording || !recording.recording_meta || !recording.recording_meta.id) { + return { + success: false, + error: 'Recording not found' + }; + } + + const proxyConfig = await getDecryptedProxyConfig(userId); + let proxyOptions: any = {}; + + if (proxyConfig.proxy_url) { + proxyOptions = { + server: proxyConfig.proxy_url, + ...(proxyConfig.proxy_username && proxyConfig.proxy_password && { + username: proxyConfig.proxy_username, + password: proxyConfig.proxy_password, + }), + }; + } + + try { + const browserId = createRemoteBrowserForRun({ + browser: chromium, + launchOptions: { + headless: true, + proxy: proxyOptions.server ? proxyOptions : undefined, + } + }); + + const run = await Run.create({ + status: 'Running', + name: recording.recording_meta.name, + robotId: recording.id, + robotMetaId: recording.recording_meta.id, + startedAt: new Date().toLocaleString(), + finishedAt: '', + browserId: id, + interpreterSettings: { maxConcurrency: 1, maxRepeats: 1, debug: true }, + log: '', + runId: id, + serializableOutput: {}, + binaryOutput: {}, + }); + + const plainRun = run.toJSON(); + + return { + browserId, + runId: plainRun.runId, + } + + } catch (e) { + const { message } = e as Error; + logger.log('info', `Error while scheduling a run with id: ${id}`); + console.log(message); + return { + success: false, + error: message, + }; + } + } + + +router.post("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => { + try { + + + const response = { + statusCode: 200, + messageCode: "success", + run: '', + }; + + res.status(200).json(response); + } catch (error) { + console.error("Error running robot:", error); + res.status(500).json({ + statusCode: 500, + messageCode: "error", + message: "Failed to run robot", + }); + } +}); + export default router; \ No newline at end of file