|
| 1 | +import twin from "../../twin.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "twin-browse", |
| 5 | + name: "Browse", |
| 6 | + description: "Browse the internet with an AI web navigation agent that can find information for you. [See the documentation](https://docs.twin.so/api-reference/endpoint/browse)", |
| 7 | + version: "0.0.1", |
| 8 | + type: "action", |
| 9 | + props: { |
| 10 | + twin, |
| 11 | + startUrl: { |
| 12 | + type: "string", |
| 13 | + label: "Start URL", |
| 14 | + description: "The URL where the browsing task should begin", |
| 15 | + }, |
| 16 | + goal: { |
| 17 | + type: "string", |
| 18 | + label: "Goal", |
| 19 | + description: "The goal or objective of the browsing task. Example: \"Find the latest price of AAPL stock.\"", |
| 20 | + }, |
| 21 | + outputType: { |
| 22 | + type: "string", |
| 23 | + label: "Output Type", |
| 24 | + description: "The type of output expected from the task", |
| 25 | + options: [ |
| 26 | + "string", |
| 27 | + "url", |
| 28 | + "list[url]", |
| 29 | + ], |
| 30 | + default: "string", |
| 31 | + optional: true, |
| 32 | + }, |
| 33 | + callbackWithRerun: { |
| 34 | + type: "boolean", |
| 35 | + label: "Callback With Rerun", |
| 36 | + description: "Use the `$.flow.rerun` Node.js helper to rerun the step when the search is completed. This will increase execution time and credit usage as a result. [See the documentation](https://pipedream.com/docs/code/nodejs/rerun/#flow-rerun)", |
| 37 | + optional: true, |
| 38 | + }, |
| 39 | + }, |
| 40 | + async run({ $ }) { |
| 41 | + let response, completionCallbackUrl; |
| 42 | + const { run } = $.context; |
| 43 | + if (run.runs === 1) { |
| 44 | + if (this.callbackWithRerun) { |
| 45 | + ({ resume_url: completionCallbackUrl } = $.flow.rerun(600000, null, 1)); |
| 46 | + } |
| 47 | + response = await this.twin.browse({ |
| 48 | + $, |
| 49 | + data: { |
| 50 | + startUrl: this.startUrl, |
| 51 | + goal: this.goal, |
| 52 | + outputType: this.outputType, |
| 53 | + completionCallbackUrl, |
| 54 | + }, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + if (run.callback_request) { |
| 59 | + const { task_id: taskId } = run.callback_request.body; |
| 60 | + response = await this.twin.getTask({ |
| 61 | + taskId, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + if (response.status === "COMPLETED") { |
| 66 | + $.export("$summary", "Successfully completed browsing"); |
| 67 | + } |
| 68 | + return response; |
| 69 | + }, |
| 70 | +}; |
0 commit comments