Skip to content
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,31 @@
import app from "../../microsoft_azure_ai_translator.app.mjs";

export default {
key: "microsoft_azure_ai_translator-break-sentence",
name: "Break Sentence",
description: "Identifies the positioning of sentence boundaries in a piece of text. [See the documentation](https://learn.microsoft.com/en-us/azure/ai-services/translator/reference/v3-0-break-sentence)",
version: "0.0.1",
type: "action",
props: {
app,
text: {
propDefinition: [
app,
"text",
],
},
},

async run({ $ }) {
const response = await this.app.breakSentence({
$,
data: [
{
text: this.text,
},
],
});
$.export("$summary", "Successfully identified the number and length of the provided sentences ");
return response;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import app from "../../microsoft_azure_ai_translator.app.mjs";

export default {
key: "microsoft_azure_ai_translator-detect-language",
name: "Detect Language",
description: "Identifies the language of a piece of text. [See the documentation](https://learn.microsoft.com/en-us/azure/ai-services/translator/reference/v3-0-detect)",
version: "0.0.1",
type: "action",
props: {
app,
text: {
propDefinition: [
app,
"text",
],
},
},

async run({ $ }) {
const response = await this.app.detectLanguage({
$,
data: [
{
text: this.text,
},
],
});
$.export("$summary", `Successfully detected language of the provided text: '${response.language}'s`);
return response;
},
Comment on lines +19 to +30
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 20, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Critical: Add error handling and fix summary message.

The run method has several issues that need to be addressed:

  1. There's a typo in the success message ('${response.language}'s)
  2. Missing error handling for API calls
  3. No validation of the response structure

Apply these improvements:

   async run({ $ }) {
-    const response = await this.app.detectLanguage({
-      $,
-      data: [
-        {
-          text: this.text,
-        },
-      ],
-    });
-    $.export("$summary", `Successfully detected language of the provided text: '${response.language}'s`);
-    return response;
+    try {
+      const response = await this.app.detectLanguage({
+        $,
+        data: [
+          {
+            text: this.text,
+          },
+        ],
+      });
+      
+      if (!response?.language) {
+        throw new Error("Invalid response: Missing language detection result");
+      }
+      
+      $.export("$summary", `Successfully detected language of the provided text: '${response.language}'`);
+      return response;
+    } catch (error) {
+      throw new Error(`Language detection failed: ${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.app.detectLanguage({
$,
data: [
{
text: this.text,
},
],
});
$.export("$summary", `Successfully detected language of the provided text: '${response.language}'s`);
return response;
},
async run({ $ }) {
try {
const response = await this.app.detectLanguage({
$,
data: [
{
text: this.text,
},
],
});
if (!response?.language) {
throw new Error("Invalid response: Missing language detection result");
}
$.export("$summary", `Successfully detected language of the provided text: '${response.language}'`);
return response;
} catch (error) {
throw new Error(`Language detection failed: ${error.message}`);
}
},

Copy link
Collaborator

Choose a reason for hiding this comment

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

The first point seems to be a fair one, can you check that?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jcortes, you're right—the first point about the typo in the success message is valid. The extra 's' at the end of ${response.language}'s should be removed. Thanks for catching that!

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import app from "../../microsoft_azure_ai_translator.app.mjs";

export default {
key: "microsoft_azure_ai_translator-translate-text",
name: "Translate Text",
description: "Translate text into the specified language. [See the documentation](https://learn.microsoft.com/en-us/azure/ai-services/translator/reference/v3-0-translate)",
version: "0.0.1",
type: "action",
props: {
app,
text: {
propDefinition: [
app,
"text",
],
},
to: {
propDefinition: [
app,
"to",
],
},
from: {
propDefinition: [
app,
"from",
],
},
profanityAction: {
propDefinition: [
app,
"profanityAction",
],
},
includeAlignment: {
propDefinition: [
app,
"includeAlignment",
],
},
},

async run({ $ }) {
const response = await this.app.translateText({
$,
data: [
{
text: this.text,
},
],
params: {
from: this.from,
to: this.to,
profanityAction: this.profanityAction,
includeAlignment: this.includeAlignment,
},
});
$.export("$summary", "Successfully translated the provided text");
return response;
},
};
7 changes: 7 additions & 0 deletions components/microsoft_azure_ai_translator/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
PROFANITY_ACTIONS: [
"NoAction",
"Marked",
"Deleted",
],
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,112 @@
import { axios } from "@pipedream/platform";
import constants from "./common/constants.mjs";

export default {
type: "app",
app: "microsoft_azure_ai_translator",
propDefinitions: {},
propDefinitions: {
text: {
type: "string",
label: "Text",
description: "String that will be sent to the API",
},
to: {
type: "string",
label: "Output Language",
description: "Language of the output text",
async options() {
const response = await this.getLanguages();
return Object.entries(response.translation).map(([
key,
{ name },
]) => ({
label: name,
value: key,
}));
},
},
from: {
type: "string",
label: "Input Language",
description: "Language of the input text",
optional: true,
async options() {
const response = await this.getLanguages();
return Object.entries(response.translation).map(([
key,
{ name },
]) => ({
label: name,
value: key,
}));
},
},
profanityAction: {
type: "string",
label: "Profanity Action",
description: "Specifies how profanities should be treated",
options: constants.PROFANITY_ACTIONS,
},
includeAlignment: {
type: "boolean",
label: "Include Alignment",
description: "Specifies whether to include alignment projection from source text to translated text",
optional: true,
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return `${this.$auth.endpoint}`;
},
async _makeRequest(opts = {}) {
const {
$ = this,
path,
headers,
params,
...otherOpts
} = opts;
return axios($, {
...otherOpts,
url: this._baseUrl() + path,
headers: {
...headers,
"Ocp-Apim-Subscription-Key": `${this.$auth.api_key}`,
"Ocp-Apim-Subscription-Region": `${this.$auth.location}`,
"Content-Type": "application/json",
},
params: {
...params,
"api-version": "3.0",
},
});
},
async translateText(args = {}) {
return this._makeRequest({
path: "/translate",
method: "post",
...args,
});
},
async breakSentence(args = {}) {
return this._makeRequest({
path: "/breaksentence",
method: "post",
...args,
});
},
async detectLanguage(args = {}) {
return this._makeRequest({
path: "/detect",
method: "post",
...args,
});
},
async getLanguages(args = {}) {
return this._makeRequest({
path: "/languages",
...args,
});
},
},
};
7 changes: 5 additions & 2 deletions components/microsoft_azure_ai_translator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/microsoft_azure_ai_translator",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Microsoft Azure AI Translator Components",
"main": "microsoft_azure_ai_translator.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"
}
}
}
Loading
Loading