-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[Components] jina_reader: new action component #14454
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
...nents/jina_reader/actions/convert-to-llm-friendly-input/convert-to-llm-friendly-input.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }, | ||
| }, | ||
| 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; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
|
||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.