Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Components] jina_reader: new action component #14454

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import fs from "fs";
import path from "path";
import { ConfigurationError } from "@pipedream/platform";
import app from "../../jina_reader.app.mjs";

export default {
key: "jina_reader-convert-to-llm-friendly-input",
name: "Convert URL To LLM-Friendly Input",
description: "Converts a provided URL to an LLM-friendly input using Jina Reader. [See the documentation](https://github.com/jina-ai/reader)",
version: "0.0.1",
type: "action",
props: {
app,
url: {
type: "string",
label: "URL",
description: "The URL to convert to an LLM-friendly input.",
optional: true,
},
contentFormat: {
type: "string",
label: "Content Format",
description: "You can control the level of detail in the response to prevent over-filtering. The default pipeline is optimized for most websites and LLM input.",
optional: true,
options: [
"markdown",
"html",
"text",
"screenshot",
"pageshot",
],
},
timeout: {
type: "integer",
label: "Timeout",
description: "Maximum time to wait for the webpage to load. Note that this is NOT the total time for the whole end-to-end request.",
optional: true,
},
targetSelector: {
type: "string",
label: "Target Selector",
description: "Provide a list of CSS selector to focus on more specific parts of the page. Useful when your desired content doesn't show under the default settings. E.g., `body, .class, #id`.",
optional: true,
},
waitForSelector: {
type: "string",
label: "Wait For Selector",
description: "Provide a list of CSS selector to wait for specific elements to appear before returning. Useful when your desired content doesn't show under the default settings. E.g., `body, .class, #id`.",
optional: true,
},
excludedSelector: {
type: "string",
label: "Excluded Selector",
description: "Provide a list of CSS selector to remove the specified elements of the page. Useful when you want to exclude specific parts of the page like headers, footers, etc. E.g., `header, .class, #id`.",
optional: true,
},
jsonResponse: {
type: "boolean",
label: "JSON Response",
description: "The response will be in JSON format, containing the URL, title, content, and timestamp (if available). In Search mode, it returns a list of five entries, each following the described JSON structure. Keep in mind **JSON Response** will take piority over **Stream mode** if both are enabled.",
optional: true,
},
forwardCookie: {
type: "string",
label: "Forward Cookie",
description: "The API server can forward your custom cookie settings when accessing the URL, which is useful for pages requiring extra authentication. Note that requests with cookies will not be cached. E.g., `<cookie-name>=<cookie-value>, <cookie-name-1>=<cookie-value>; domain=<cookie-1-domain>`. [Learn more here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie).",
optional: true,
},
useProxyServer: {
type: "string",
label: "Proxy Server URL",
description: "The API server can utilize your proxy to access URLs, which is helpful for pages accessible only through specific proxies. E.g., `http://your_proxy_server.com`. [Learn more here](https://en.wikipedia.org/wiki/Proxy_server).",
optional: true,
},
bypassCache: {
type: "boolean",
label: "Bypass Cache",
description: "The API server caches both Read and Search mode contents for a certain amount of time. To bypass this cache, set this header to `true`.",
optional: true,
},
streamMode: {
type: "boolean",
label: "Stream Mode",
description: "Stream mode is beneficial for large target pages, allowing more time for the page to fully render. If standard mode results in incomplete content, consider using **Stream mode**. [Learn more here](https://github.com/jina-ai/reader?tab=readme-ov-file#streaming-mode). Keep in mind **JSON Response** will take piority over **Stream mode** if both are enabled.",
optional: true,
},
browserLocale: {
type: "string",
label: "Browser Locale",
description: "Control the browser locale to render the page. eg. `en-US`. [Learn more here](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language).",
optional: true,
},
iframeContent: {
type: "boolean",
label: "Iframe",
description: "Returning result will also include the content of the iframes on the page.",
optional: true,
},
shadowDomContent: {
type: "boolean",
label: "Include Shadow DOM Content",
description: "Returning result will also include the content of the shadow DOM on the page.",
optional: true,
},
pdf: {
type: "string",
label: "PDF File Path",
description: "The path to the pdf file saved to the `/tmp` directory (e.g. `/tmp/example.pdf`). [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).",
optional: true,
},
html: {
type: "string",
label: "HTML File Path",
description: "The path to the html file saved to the `/tmp` directory (e.g. `/tmp/example.html`). [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).",
optional: true,
},
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
methods: {
async readFileFromTmp(filePath, encoding) {
if (!filePath) {
return;
}
const resolvedPath = path.resolve(filePath);
if (!resolvedPath.startsWith("/tmp/")) {
throw new ConfigurationError(`${filePath} must be located in the '/tmp/' directory`);
}
return await fs.promises.readFile(resolvedPath, encoding);
},
},
async run({ $ }) {
const {
app,
readFileFromTmp,
url,
contentFormat,
timeout,
targetSelector,
waitForSelector,
excludedSelector,
jsonResponse,
forwardCookie,
useProxyServer,
bypassCache,
streamMode,
browserLocale,
iframeContent,
shadowDomContent,
pdf,
html,
} = this;

if (!url && !pdf && !html) {
throw new ConfigurationError("You must provide at least one of **URL**, **PDF File Path**, or **HTML File Path**.");
}

const response = await app.post({
$,
headers: {
"X-Return-Format": contentFormat,
"X-Timeout": timeout,
"X-Target-Selector": targetSelector,
"X-Wait-For-Selector": waitForSelector,
"X-Remove-Selector": excludedSelector,
"X-Set-Cookie": forwardCookie,
"X-Proxy-Url": useProxyServer,
"X-No-Cache": bypassCache,
"Accept": jsonResponse
? "application/json"
: streamMode
? "text/event-stream"
: undefined,
"X-Locale": browserLocale,
"X-With-Shadow-Dom": shadowDomContent,
"X-Iframe": iframeContent,
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
data: {
url,
pdf: await readFileFromTmp(pdf, "base64"),
html: await readFileFromTmp(html, "utf-8"),
},
});

$.export("$summary", "Converted URL to LLM-friendly input successfully.");
return response;
},
};
32 changes: 27 additions & 5 deletions components/jina_reader/jina_reader.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "jina_reader",
propDefinitions: {},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
getUrl() {
return "https://r.jina.ai/";
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
getHeaders(headers) {
return {
...headers,
"Content-Type": "application/json",
"Authorization": `Bearer ${this.$auth.api_key}`,
};
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
_makeRequest({
$ = this, headers, ...args
} = {}) {
return axios($, {
...args,
url: this.getUrl(),
headers: this.getHeaders(headers),
});
},
jcortes marked this conversation as resolved.
Show resolved Hide resolved
post(args = {}) {
return this._makeRequest({
method: "POST",
...args,
});
jcortes marked this conversation as resolved.
Show resolved Hide resolved
},
},
};
};
7 changes: 5 additions & 2 deletions components/jina_reader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/jina_reader",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Jina Reader Components",
"main": "jina_reader.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "3.0.3"
}
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading