feat: createWorkflowAndStoreMetadata for robot access via api
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
import { readFile, readFiles } from "../workflow-management/storage";
|
import { readFile, readFiles } from "../workflow-management/storage";
|
||||||
import { Router, Request, Response } from 'express';
|
import { Router, Request, Response } from 'express';
|
||||||
|
import { chromium } from "playwright";
|
||||||
import { requireAPIKey } from "../middlewares/api";
|
import { requireAPIKey } from "../middlewares/api";
|
||||||
import Robot from "../models/Robot";
|
import Robot from "../models/Robot";
|
||||||
import Run from "../models/Run";
|
import Run from "../models/Run";
|
||||||
const router = Router();
|
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 formatRecording = (recordingData: any) => {
|
||||||
const recordingMeta = recordingData.recording_meta;
|
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;
|
export default router;
|
||||||
Reference in New Issue
Block a user