glin-profanity
Version:
Glin-Profanity is a lightweight and efficient npm package designed to detect and filter profane language in text inputs across multiple languages. Whether you’re building a chat application, a comment section, or any platform where user-generated content
225 lines (221 loc) • 6.9 kB
TypeScript
import { F as FilterConfig } from '../types-B9c_ik4k.js';
export { C as CheckProfanityResult, L as Language } from '../types-B9c_ik4k.js';
/**
* Vercel AI SDK Integration for glin-profanity
*
* Provides ready-to-use tools for the Vercel AI SDK.
* Compatible with Next.js, Remix, SvelteKit, and other frameworks.
*
* @example
* ```typescript
* import { generateText } from 'ai';
* import { openai } from '@ai-sdk/openai';
* import { profanityTools } from 'glin-profanity/ai/vercel';
*
* const { text, toolCalls } = await generateText({
* model: openai('gpt-4o'),
* prompt: 'Check if "Hello world" contains profanity',
* tools: profanityTools,
* });
* ```
*
* @packageDocumentation
* @module glin-profanity/ai/vercel
*/
/**
* Vercel AI SDK tool definition
*/
interface VercelAITool<TInput = unknown, TOutput = unknown> {
description: string;
inputSchema: unknown;
execute: (input: TInput) => Promise<TOutput>;
}
/**
* Tool input types
*/
interface CheckProfanityInput {
text: string;
languages?: string[];
detectLeetspeak?: boolean;
normalizeUnicode?: boolean;
}
interface CensorTextInput {
text: string;
replacement?: string;
languages?: string[];
}
interface BatchCheckInput {
texts: string[];
languages?: string[];
detectLeetspeak?: boolean;
}
interface AnalyzeContextInput {
text: string;
languages?: string[];
contextWindow?: number;
confidenceThreshold?: number;
}
/**
* Tool output types
*/
interface CheckProfanityOutput {
containsProfanity: boolean;
profaneWords: string[];
severityMap?: Record<string, number>;
wordCount: number;
}
interface CensorTextOutput {
originalText: string;
censoredText: string;
profaneWordsFound: string[];
wasModified: boolean;
}
interface BatchCheckOutput {
totalTexts: number;
flaggedCount: number;
cleanCount: number;
results: Array<{
index: number;
text: string;
containsProfanity: boolean;
profaneWords: string[];
}>;
}
interface AnalyzeContextOutput {
containsProfanity: boolean;
profaneWords: string[];
contextScore?: number;
matches?: unknown[];
reason?: string;
}
interface SupportedLanguagesOutput {
languages: string[];
count: number;
}
/**
* Creates a profanity check tool for Vercel AI SDK
*
* @example
* ```typescript
* import { generateText, tool } from 'ai';
* import { createCheckProfanityTool } from 'glin-profanity/ai/vercel';
*
* const { text } = await generateText({
* model: openai('gpt-4o'),
* prompt: 'Check this text for profanity: "Hello world"',
* tools: {
* checkProfanity: createCheckProfanityTool(),
* },
* });
* ```
*/
declare function createCheckProfanityTool(): VercelAITool<CheckProfanityInput, CheckProfanityOutput>;
/**
* Creates a censor text tool for Vercel AI SDK
*/
declare function createCensorTextTool(): VercelAITool<CensorTextInput, CensorTextOutput>;
/**
* Creates a batch check tool for Vercel AI SDK
*/
declare function createBatchCheckTool(): VercelAITool<BatchCheckInput, BatchCheckOutput>;
/**
* Creates a context analysis tool for Vercel AI SDK
*/
declare function createContextAnalysisTool(): VercelAITool<AnalyzeContextInput, AnalyzeContextOutput>;
/**
* Creates a supported languages tool for Vercel AI SDK
*/
declare function createSupportedLanguagesTool(): VercelAITool<Record<string, never>, SupportedLanguagesOutput>;
/**
* Pre-built profanity tools for Vercel AI SDK
*
* @example
* ```typescript
* import { generateText } from 'ai';
* import { openai } from '@ai-sdk/openai';
* import { profanityTools } from 'glin-profanity/ai/vercel';
*
* const { text, toolCalls } = await generateText({
* model: openai('gpt-4o'),
* prompt: 'Check if "Hello world" contains profanity',
* tools: profanityTools,
* });
* ```
*/
declare const profanityTools: {
checkProfanity: VercelAITool<CheckProfanityInput, CheckProfanityOutput>;
censorText: VercelAITool<CensorTextInput, CensorTextOutput>;
batchCheckProfanity: VercelAITool<BatchCheckInput, BatchCheckOutput>;
analyzeContext: VercelAITool<AnalyzeContextInput, AnalyzeContextOutput>;
getSupportedLanguages: VercelAITool<Record<string, never>, SupportedLanguagesOutput>;
};
/**
* Creates all profanity tools with custom configuration
*
* @returns Object containing all Vercel AI SDK-compatible tools
*/
declare function createAllProfanityTools(): {
checkProfanity: VercelAITool<CheckProfanityInput, CheckProfanityOutput>;
censorText: VercelAITool<CensorTextInput, CensorTextOutput>;
batchCheckProfanity: VercelAITool<BatchCheckInput, BatchCheckOutput>;
analyzeContext: VercelAITool<AnalyzeContextInput, AnalyzeContextOutput>;
getSupportedLanguages: VercelAITool<Record<string, never>, SupportedLanguagesOutput>;
};
/**
* Middleware for Next.js API routes to automatically check request bodies
*
* @example
* ```typescript
* // app/api/chat/route.ts
* import { profanityMiddleware } from 'glin-profanity/ai/vercel';
*
* export async function POST(req: Request) {
* const body = await req.json();
*
* // Check for profanity in user message
* const check = profanityMiddleware.checkMessage(body.message);
* if (check.blocked) {
* return Response.json({ error: check.reason }, { status: 400 });
* }
*
* // Continue with AI processing...
* }
* ```
*/
declare const profanityMiddleware: {
/**
* Check a message for profanity
*/
checkMessage(message: string, config?: Partial<FilterConfig>): {
blocked: boolean;
reason: string;
profaneWords: string[];
censoredMessage: string;
};
/**
* Censor a message (replace profanity with asterisks)
*/
censorMessage(message: string, replacement?: string, config?: Partial<FilterConfig>): {
original: string;
censored: string;
wasModified: boolean;
profaneWords: string[];
};
/**
* Check array of messages (useful for chat history)
*/
checkMessages(messages: Array<{
role: string;
content: string;
}>, config?: Partial<FilterConfig>): {
hasIssues: boolean;
flaggedMessages: {
index: number;
role: string;
containsProfanity: boolean;
profaneWords: string[];
}[];
totalMessages: number;
};
};
export { type AnalyzeContextInput, type AnalyzeContextOutput, type BatchCheckInput, type BatchCheckOutput, type CensorTextInput, type CensorTextOutput, type CheckProfanityInput, type CheckProfanityOutput, FilterConfig, type SupportedLanguagesOutput, type VercelAITool, createAllProfanityTools, createBatchCheckTool, createCensorTextTool, createCheckProfanityTool, createContextAnalysisTool, createSupportedLanguagesTool, profanityMiddleware, profanityTools };