import type { ToolDefinition, ToolFunction } from '@conveniencepro/ctp-core';
// Define the result type
interface TextReverserResult {
reversed: string;
originalLength: number;
isPalindrome: boolean;
}
// Tool definition
export const textReverserDefinition: ToolDefinition = {
id: 'text-reverser',
name: 'Text Reverser',
description: 'Reverse any text string and check if it is a palindrome.',
category: 'editors',
tags: ['text', 'reverse', 'palindrome', 'string'],
method: 'POST',
parameters: [
{
name: 'text',
type: 'textarea',
label: 'Input Text',
description: 'Text to reverse',
required: true,
placeholder: 'Enter text to reverse...',
},
{
name: 'ignoreSpaces',
type: 'boolean',
label: 'Ignore Spaces',
description: 'Ignore spaces when checking for palindrome',
required: false,
defaultValue: false,
},
],
outputDescription: 'Reversed text with palindrome detection',
example: {
input: { text: 'hello', ignoreSpaces: false },
output: { reversed: 'olleh', originalLength: 5, isPalindrome: false },
},
executionMode: 'client',
};
// Tool implementation
export const textReverserFn: ToolFunction<TextReverserResult> = (params) => {
const text = params.text as string;
const ignoreSpaces = params.ignoreSpaces === true;
if (!text) {
return {
success: false,
error: 'Text is required',
errorCode: 'MISSING_REQUIRED',
};
}
const reversed = text.split('').reverse().join('');
// Check palindrome
const normalizedOriginal = ignoreSpaces
? text.toLowerCase().replace(/\s/g, '')
: text.toLowerCase();
const normalizedReversed = ignoreSpaces
? reversed.toLowerCase().replace(/\s/g, '')
: reversed.toLowerCase();
return {
success: true,
data: {
reversed,
originalLength: text.length,
isPalindrome: normalizedOriginal === normalizedReversed,
},
};
};
export default {
definition: textReverserDefinition,
fn: textReverserFn,
};