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

New Components - tldr #14499

Merged
merged 3 commits into from
Nov 5, 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
40 changes: 40 additions & 0 deletions components/tldr/actions/summarize-text/summarize-text.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import tldr from "../../tldr.app.mjs";

export default {
key: "tldr-summarize-text",
name: "Summarize Text",
description: "Reads in a piece of text and distills the main points. [See the documentation](https://runtldr.com/documentation)",
version: "0.0.1",
type: "action",
props: {
tldr,
inputText: {
type: "string",
label: "Text to Summarize",
description: "The text that needs to be summarized.",
},
responseStyle: {
type: "string",
label: "Response Style",
description: "Style of the response (e.g., Funny, Serious).",
},
responseLength: {
type: "integer",
label: "Response Length",
description: "Length of the response summary.",
},
},
Comment on lines +9 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation constraints to props.

Consider adding the following improvements to make the props more robust:

  1. Add options to responseStyle to limit valid values (e.g., ["Funny", "Serious"])
  2. Add min and max to responseLength to prevent unreasonable values
  3. Add min and max length constraints to inputText
 responseStyle: {
   type: "string",
   label: "Response Style",
   description: "Style of the response (e.g., Funny, Serious).",
+  options: ["Funny", "Serious", "Professional", "Casual"],
+  optional: true,
+  default: "Professional",
 },
 responseLength: {
   type: "integer",
   label: "Response Length",
   description: "Length of the response summary.",
+  min: 50,
+  max: 500,
+  optional: true,
+  default: 200,
 },
 inputText: {
   type: "string",
   label: "Text to Summarize",
   description: "The text that needs to be summarized.",
+  min: 1,
+  max: 5000,
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
props: {
tldr,
inputText: {
type: "string",
label: "Text to Summarize",
description: "The text that needs to be summarized.",
},
responseStyle: {
type: "string",
label: "Response Style",
description: "Style of the response (e.g., Funny, Serious).",
},
responseLength: {
type: "integer",
label: "Response Length",
description: "Length of the response summary.",
},
},
props: {
tldr,
inputText: {
type: "string",
label: "Text to Summarize",
description: "The text that needs to be summarized.",
min: 1,
max: 5000,
},
responseStyle: {
type: "string",
label: "Response Style",
description: "Style of the response (e.g., Funny, Serious).",
options: ["Funny", "Serious", "Professional", "Casual"],
optional: true,
default: "Professional",
},
responseLength: {
type: "integer",
label: "Response Length",
description: "Length of the response summary.",
min: 50,
max: 500,
optional: true,
default: 200,
},
},

async run({ $ }) {
const response = await this.tldr.summarize({
$,
data: {
inputText: this.inputText,
responseLength: this.responseLength,
responseStyle: this.responseStyle,
},
});

$.export("$summary", `Successfully summarized the text with the following input: "${this.inputText}"`);
return response;
},
Comment on lines +27 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling and input validation.

The run method needs the following improvements:

  1. Add try-catch block for API call
  2. Validate inputs before making the API call
  3. Include more details in the success message
 async run({ $ }) {
+  // Validate inputs
+  if (!this.inputText?.trim()) {
+    throw new Error("Input text cannot be empty");
+  }
+
+  try {
     const response = await this.tldr.summarize({
       $,
       data: {
         inputText: this.inputText,
         responseLength: this.responseLength,
         responseStyle: this.responseStyle,
       },
     });
 
-    $.export("$summary", `Successfully summarized the text with the following input: "${this.inputText}"`);
+    const truncatedInput = this.inputText.slice(0, 50) + (this.inputText.length > 50 ? "..." : "");
+    $.export("$summary", `Successfully summarized ${this.inputText.length} characters of text. Preview: "${truncatedInput}"`);
     return response;
+  } catch (error) {
+    throw new Error(`Failed to summarize text: ${error.message}`);
+  }
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
async run({ $ }) {
const response = await this.tldr.summarize({
$,
data: {
inputText: this.inputText,
responseLength: this.responseLength,
responseStyle: this.responseStyle,
},
});
$.export("$summary", `Successfully summarized the text with the following input: "${this.inputText}"`);
return response;
},
async run({ $ }) {
// Validate inputs
if (!this.inputText?.trim()) {
throw new Error("Input text cannot be empty");
}
try {
const response = await this.tldr.summarize({
$,
data: {
inputText: this.inputText,
responseLength: this.responseLength,
responseStyle: this.responseStyle,
},
});
const truncatedInput = this.inputText.slice(0, 50) + (this.inputText.length > 50 ? "..." : "");
$.export("$summary", `Successfully summarized ${this.inputText.length} characters of text. Preview: "${truncatedInput}"`);
return response;
} catch (error) {
throw new Error(`Failed to summarize text: ${error.message}`);
}
},

};
7 changes: 5 additions & 2 deletions components/tldr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/tldr",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream TLDR Components",
"main": "tldr.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"
}
}
}
32 changes: 27 additions & 5 deletions components/tldr/tldr.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: "tldr",
propDefinitions: {},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_headers() {
return {
"Authorization": `Bearer ${this.$auth.api_key}`,
"Content-Type": "application/json",
};
},
_baseUrl() {
return "https://runtldr.com/apis/v1";
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
Comment on lines +16 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and request timeout.

The request method should include error handling and a timeout to prevent hanging requests.

 _makeRequest({
   $ = this, path, ...opts
 }) {
   return axios($, {
     url: this._baseUrl() + path,
     headers: this._headers(),
+    timeout: 30000, // 30 seconds timeout
     ...opts,
-  });
+  }).catch(err => {
+    const status = err.response?.status;
+    const message = err.response?.data?.message || err.message;
+    throw new Error(`Request failed with status ${status}: ${message}`);
+  });
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
timeout: 30000, // 30 seconds timeout
...opts,
}).catch(err => {
const status = err.response?.status;
const message = err.response?.data?.message || err.message;
throw new Error(`Request failed with status ${status}: ${message}`);
});
},

summarize(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/summarize",
...opts,
});
Comment on lines +25 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation and JSDoc documentation.

The summarize method should validate required parameters and include documentation about expected options.

+/**
+ * Summarize text using TLDR API
+ * @param {Object} opts - Options for summarization
+ * @param {string} opts.text - Text to summarize
+ * @param {string} [opts.style] - Response style (optional)
+ * @param {number} [opts.length] - Response length (optional)
+ * @returns {Promise<Object>} Summarization response
+ */
 summarize(opts = {}) {
+  if (!opts.text) {
+    throw new Error("Text parameter is required");
+  }
   return this._makeRequest({
     method: "POST",
     path: "/summarize",
     ...opts,
   });
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
summarize(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/summarize",
...opts,
});
/**
* Summarize text using TLDR API
* @param {Object} opts - Options for summarization
* @param {string} opts.text - Text to summarize
* @param {string} [opts.style] - Response style (optional)
* @param {number} [opts.length] - Response length (optional)
* @returns {Promise<Object>} Summarization response
*/
summarize(opts = {}) {
if (!opts.text) {
throw new Error("Text parameter is required");
}
return this._makeRequest({
method: "POST",
path: "/summarize",
...opts,
});
}

},
},
};
};
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