Skip to main content
Glama

mcp-google-sheets

moderate-text.ts8.94 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 MODERATION_PROVIDERS = [ { label: 'Microsoft', value: 'microsoft' }, { label: 'OpenAI', value: 'openai' }, { label: 'Google', value: 'google' }, ]; const MODERATION_LANGUAGES = [ { label: 'Auto Detection', value: 'auto-detect' }, { label: 'Afrikaans', value: 'af' }, { label: 'Albanian', value: 'sq' }, { label: 'Amharic', value: 'am' }, { label: 'Arabic', value: 'ar' }, { label: 'Armenian', value: 'hy' }, { label: 'Assamese', value: 'as' }, { label: 'Azerbaijani', value: 'az' }, { label: 'Basque', value: 'eu' }, { label: 'Belarusian', value: 'be' }, { label: 'Bengali', value: 'bn' }, { label: 'Bosnian', value: 'bs' }, { label: 'Breton', value: 'br' }, { label: 'Bulgarian', value: 'bg' }, { label: 'Catalan', value: 'ca' }, { label: 'Central Kurdish', value: 'ckb' }, { label: 'Cherokee', value: 'chr' }, { label: 'Chinese', value: 'zh' }, { label: 'Croatian', value: 'hr' }, { label: 'Czech', value: 'cs' }, { label: 'Danish', value: 'da' }, { label: 'Dutch', value: 'nl' }, { label: 'English', value: 'en' }, { label: 'Estonian', value: 'et' }, { label: 'Filipino', value: 'fil' }, { label: 'Finnish', value: 'fi' }, { label: 'French', value: 'fr' }, { label: 'Fulah', value: 'ff' }, { label: 'Galician', value: 'gl' }, { label: 'Georgian', value: 'ka' }, { label: 'German', value: 'de' }, { label: 'Gujarati', value: 'gu' }, { label: 'Hausa', value: 'ha' }, { label: 'Hebrew', value: 'he' }, { label: 'Hindi', value: 'hi' }, { label: 'Hungarian', value: 'hu' }, { label: 'Icelandic', value: 'is' }, { label: 'Igbo', value: 'ig' }, { label: 'Indonesian', value: 'id' }, { label: 'Inuktitut', value: 'iu' }, { label: 'Irish', value: 'ga' }, { label: 'Italian', value: 'it' }, { label: 'Japanese', value: 'ja' }, { label: 'Kannada', value: 'kn' }, { label: 'Kazakh', value: 'kk' }, { label: 'Khmer', value: 'km' }, { label: 'Kinyarwanda', value: 'rw' }, { label: 'Kirghiz', value: 'ky' }, { label: 'Konkani', value: 'kok' }, { label: 'Korean', value: 'ko' }, { label: 'Lao', value: 'lo' }, { label: 'Latvian', value: 'lv' }, { label: 'Lithuanian', value: 'lt' }, { label: 'Luxembourgish', value: 'lb' }, { label: 'Macedonian', value: 'mk' }, { label: 'Malay', value: 'ms' }, { label: 'Malayalam', value: 'ml' }, { label: 'Maltese', value: 'mt' }, { label: 'Maori', value: 'mi' }, { label: 'Marathi', value: 'mr' }, { label: 'Modern Greek', value: 'el' }, { label: 'Mongolian', value: 'mn' }, { label: 'Nepali', value: 'ne' }, { label: 'Norwegian Bokmål', value: 'nb' }, { label: 'Norwegian Nynorsk', value: 'nn' }, { label: 'Oriya', value: 'or' }, { label: 'Panjabi', value: 'pa' }, { label: 'Pedi', value: 'nso' }, { label: 'Persian', value: 'fa' }, { label: 'Polish', value: 'pl' }, { label: 'Portuguese', value: 'pt' }, { label: 'Pushto', value: 'ps' }, { label: 'Quechua', value: 'qu' }, { label: 'Romanian', value: 'ro' }, { label: 'Russian', value: 'ru' }, { label: 'Scottish Gaelic', value: 'gd' }, { label: 'Serbian', value: 'sr' }, { label: 'Sindhi', value: 'sd' }, { label: 'Sinhala', value: 'si' }, { label: 'Slovak', value: 'sk' }, { label: 'Slovenian', value: 'sl' }, { label: 'Southern Sotho', value: 'st' }, { label: 'Spanish', value: 'es' }, { label: 'Swahili', value: 'sw' }, { label: 'Swedish', value: 'sv' }, { label: 'Tajik', value: 'tg' }, { label: 'Tamil', value: 'ta' }, { label: 'Tatar', value: 'tt' }, { label: 'Telugu', value: 'te' }, { label: 'Thai', value: 'th' }, { label: 'Tigrinya', value: 'ti' }, { label: 'Tswana', value: 'tn' }, { label: 'Turkish', value: 'tr' }, { label: 'Turkmen', value: 'tk' }, { label: 'Uighur', value: 'ug' }, { label: 'Ukrainian', value: 'uk' }, { label: 'Urdu', value: 'ur' }, { label: 'Uzbek', value: 'uz' }, { label: 'Vietnamese', value: 'vi' }, { label: 'Welsh', value: 'cy' }, { label: 'Wolof', value: 'wo' }, { label: 'Xhosa', value: 'xh' }, { label: 'Yoruba', value: 'yo' }, { label: 'Zulu', value: 'zu' }, ]; function normalizeModerationResponse(provider: string, response: any) { const providerResult = response[provider]; if (!providerResult) { return { provider, nsfw_likelihood: 0, items: [], status: 'fail', raw: response }; } const items = (providerResult.items || []).map((item: any) => ({ label: item.label || '', likelihood: item.likelihood || 0, category: item.category || '', subcategory: item.subcategory || '', likelihood_score: item.likelihood_score || 0, })); return { provider, nsfw_likelihood: providerResult.nsfw_likelihood || 0, nsfw_likelihood_score: providerResult.nsfw_likelihood_score || 0, items, status: providerResult.status || 'success', original_response: providerResult.original_response || null, raw: response, }; } export const moderateTextAction = createAction({ name: 'moderate_text', displayName: 'Moderate Text', description: 'Detect explicit or policy-violating text using Eden AI. Supports multiple providers, languages, and models.', props: { provider: Property.Dropdown({ displayName: 'Provider', description: 'The AI provider to use for text moderation.', required: true, refreshers: [], options: createStaticDropdown(MODERATION_PROVIDERS), }), text: Property.LongText({ displayName: 'Text to Moderate', description: 'The text to analyze for explicit or policy-violating content.', required: true, }), language: Property.Dropdown({ displayName: 'Text Language', description: 'The language of the input text. Choose "Auto Detection" if unsure.', required: false, refreshers: [], options: createStaticDropdown(MODERATION_LANGUAGES), defaultValue: 'auto-detect', }), model: Property.ShortText({ displayName: 'Specific Model', description: 'Specific model to use (e.g., text-moderation-latest, text-moderation-stable). 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(MODERATION_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'), language: z.string().nullish(), model: z.string().nullish(), fallback_providers: z.array(z.string()).max(5).nullish(), show_original_response: z.boolean().nullish(), }); const { provider, text, language, model, fallback_providers, show_original_response } = propsValue; const body: Record<string, any> = { providers: provider, text, }; if (language && language !== 'auto-detect') body['language'] = language; 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: '/text/moderation', body, }); if (!response || typeof response !== 'object') { throw new Error('Invalid response from Eden AI API.'); } return normalizeModerationResponse(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 moderate text: ${err.message || err}`); } }, });

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/activepieces/activepieces'

If you have feedback or need assistance with the MCP directory API, please join our Discord server