detect-language.ts•4.5 kB
import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod, propsValidation } from '@activepieces/pieces-common';
import { edenAiApiCall } from '../common/client';
import { createStaticDropdown } from '../common/providers';
import { z } from 'zod';
const LANGUAGE_DETECTION_PROVIDERS = [
{ label: 'Amazon', value: 'amazon' },
{ label: 'Google', value: 'google' },
{ label: 'Microsoft', value: 'microsoft' },
{ label: 'ModernMT', value: 'modernmt' },
{ label: 'OpenAI', value: 'openai' },
{ label: 'XAI Grok', value: 'xai' },
{ label: 'OneAI', value: 'oneai' },
];
function normalizeLanguageDetectionResponse(provider: string, response: any) {
const providerResult = response[provider];
if (!providerResult) {
return { provider, languages: [], status: 'fail', raw: response };
}
const languages = (providerResult.items || []).map((item: any) => ({
language: item.language || '',
display_name: item.display_name || '',
confidence: item.confidence || 0,
}));
return {
provider,
languages,
status: providerResult.status || 'success',
original_response: providerResult.original_response || null,
raw: response,
};
}
export const detectLanguageAction = createAction({
name: 'detect_language',
displayName: 'Detect Language of Text',
description: 'Detect the language used in a text using Eden AI. Supports multiple providers and models.',
props: {
provider: Property.Dropdown({
displayName: 'Provider',
description: 'The AI provider to use for language detection.',
required: true,
refreshers: [],
options: createStaticDropdown(LANGUAGE_DETECTION_PROVIDERS),
}),
text: Property.LongText({
displayName: 'Text to Analyze',
description: 'The text to detect language for.',
required: true,
}),
model: Property.ShortText({
displayName: 'Specific Model',
description: 'Specific model to use (e.g., gpt-4o, grok-2-latest). Leave empty for default.',
required: false,
}),
fallback_providers: Property.MultiSelectDropdown({
displayName: 'Fallback Providers',
description: 'Alternative providers to try if the main provider fails (up to 5).',
required: false,
refreshers: [],
options: createStaticDropdown(LANGUAGE_DETECTION_PROVIDERS),
}),
show_original_response: Property.Checkbox({
displayName: 'Include Original Response',
description: 'Include the raw provider response in the output for debugging.',
required: false,
defaultValue: false,
}),
},
async run({ auth, propsValue }) {
await propsValidation.validateZod(propsValue, {
provider: z.string().min(1, 'Provider is required'),
text: z.string().min(1, 'Text is required'),
model: z.string().nullish(),
fallback_providers: z.array(z.string()).max(5).nullish(),
show_original_response: z.boolean().nullish(),
});
const {
provider,
text,
model,
fallback_providers,
show_original_response
} = propsValue;
const body: Record<string, any> = {
providers: provider,
text,
};
if (show_original_response) body['show_original_response'] = true;
if (fallback_providers && fallback_providers.length > 0) {
body['fallback_providers'] = fallback_providers.slice(0, 5);
}
if (model) {
body['settings'] = { [provider]: model };
}
try {
const response = await edenAiApiCall({
apiKey: auth as string,
method: HttpMethod.POST,
resourceUri: '/translation/language_detection',
body,
});
if (!response || typeof response !== 'object') {
throw new Error('Invalid response from Eden AI API.');
}
return normalizeLanguageDetectionResponse(provider, response);
} catch (err: any) {
if (err.response?.body?.error) {
throw new Error(`Eden AI API error: ${err.response.body.error}`);
}
if (err.response?.status === 429) {
throw new Error('Rate limit exceeded. Please try again later.');
}
if (err.response?.status === 401) {
throw new Error('Invalid API key. Please check your Eden AI credentials.');
}
if (err.response?.status === 400) {
throw new Error('Invalid request. Please check your input text and parameters.');
}
throw new Error(`Failed to detect language: ${err.message || err}`);
}
},
});