diff --git a/client/src/components/Skills/dialogs/CreateSkillDialog.tsx b/client/src/components/Skills/dialogs/CreateSkillDialog.tsx index b4047702bb..f276b428cd 100644 --- a/client/src/components/Skills/dialogs/CreateSkillDialog.tsx +++ b/client/src/components/Skills/dialogs/CreateSkillDialog.tsx @@ -1,10 +1,5 @@ import { useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; -import { - SKILL_NAME_PATTERN, - SKILL_NAME_MAX_LENGTH, - SKILL_DESCRIPTION_MAX_LENGTH, -} from 'librechat-data-provider'; import { Input, Label, @@ -14,6 +9,12 @@ import { TextareaAutosize, useToastContext, } from '@librechat/client'; +import { + SKILL_NAME_PATTERN, + SKILL_NAME_MAX_LENGTH, + SKILL_BODY_MAX_LENGTH, + SKILL_DESCRIPTION_MAX_LENGTH, +} from 'librechat-data-provider'; import type { TSkill } from 'librechat-data-provider'; import type { FormEvent } from 'react'; import { useCreateSkillMutation } from '~/data-provider'; @@ -40,6 +41,11 @@ interface FormValues { body: string; } +interface SkillValidationIssue { + field: string; + code: string; +} + /** * Minimal create-skill dialog matching Claude.ai's "Write skill instructions" * modal: name, description, instructions. No category, no invocation mode. @@ -78,9 +84,48 @@ export default function CreateSkillDialog({ navigate(`/skills/${skill._id}`); }, onError: (error: unknown) => { - const message = - (error as { response?: { data?: { message?: string } } })?.response?.data?.message ?? - localize('com_ui_skill_create_error'); + const response = ( + error as { + response?: { + status?: number; + data?: { error?: string; message?: string; issues?: SkillValidationIssue[] }; + }; + } + )?.response; + const getIssueMessage = ({ field, code }: SkillValidationIssue) => { + if (field === 'name' && code === 'REQUIRED') { + return localize('com_ui_skill_name_required'); + } + if (field === 'name' && code === 'TOO_LONG') { + return localize('com_ui_skill_name_too_long', { 0: SKILL_NAME_MAX_LENGTH }); + } + if (field === 'name' && code === 'INVALID_FORMAT') { + return localize('com_ui_skill_name_invalid'); + } + if (field === 'name' && (code === 'RESERVED_PREFIX' || code === 'RESERVED_WORD')) { + return localize('com_ui_skill_name_reserved'); + } + if (field === 'description' && code === 'REQUIRED') { + return localize('com_ui_skill_description_required'); + } + if (field === 'description' && code === 'TOO_LONG') { + return localize('com_ui_skill_description_too_long', { + 0: SKILL_DESCRIPTION_MAX_LENGTH, + }); + } + if (field === 'body' && code === 'TOO_LONG') { + return localize('com_ui_skill_instructions_too_long', { 0: SKILL_BODY_MAX_LENGTH }); + } + return localize('com_ui_skill_validation_error'); + }; + const data = response?.data; + let message = data?.message || localize('com_ui_skill_create_error'); + if (response?.status === 409) { + message = localize('com_ui_skill_name_exists'); + } + if (data?.issues?.length) { + message = data.issues.map(getIssueMessage).join('; '); + } showToast({ status: 'error', message }); }, }); @@ -166,6 +211,8 @@ export default function CreateSkillDialog({ maxRows={4} placeholder={localize('com_ui_skill_description_placeholder')} aria-label={localize('com_ui_description')} + aria-invalid={errors.description ? 'true' : 'false'} + aria-describedby={errors.description ? 'create-skill-description-error' : undefined} className="w-full resize-none rounded-xl border border-border-medium bg-transparent px-3 py-2 text-sm text-text-primary placeholder:text-text-secondary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring-primary" {...register('description', { required: localize('com_ui_skill_description_required'), @@ -177,6 +224,15 @@ export default function CreateSkillDialog({ }, })} /> + {errors.description && ( +
+ {errors.description.message} +
+ )} {/* Instructions (body) */} @@ -204,9 +260,10 @@ export default function CreateSkillDialog({ type="submit" variant="submit" disabled={submitDisabled} + aria-busy={createSkill.isLoading} className={cn(submitDisabled && 'opacity-50')} > - {localize('com_ui_create')} + {localize(createSkill.isLoading ? 'com_ui_creating' : 'com_ui_create')} diff --git a/client/src/components/Skills/dialogs/__tests__/CreateSkillDialog.spec.tsx b/client/src/components/Skills/dialogs/__tests__/CreateSkillDialog.spec.tsx index 83ed90fe1c..82b4b0c48d 100644 --- a/client/src/components/Skills/dialogs/__tests__/CreateSkillDialog.spec.tsx +++ b/client/src/components/Skills/dialogs/__tests__/CreateSkillDialog.spec.tsx @@ -6,6 +6,7 @@ const mockMutate = jest.fn(); const mockNavigate = jest.fn(); const mockSetIsOpen = jest.fn(); const mockShowToast = jest.fn(); +let mockOnError: ((error: unknown) => void) | undefined; jest.mock('react-router-dom', () => ({ ...jest.requireActual('react-router-dom'), @@ -47,10 +48,13 @@ jest.mock('@librechat/client', () => { }); jest.mock('~/data-provider', () => ({ - useCreateSkillMutation: () => ({ - mutate: mockMutate, - isLoading: false, - }), + useCreateSkillMutation: (options: { onError: (error: unknown) => void }) => { + mockOnError = options.onError; + return { + mutate: mockMutate, + isLoading: false, + }; + }, })); jest.mock('~/hooks', () => ({ @@ -61,6 +65,9 @@ jest.mock('~/hooks', () => ({ com_ui_skill_instructions: 'Instructions', com_ui_cancel: 'Cancel', com_ui_create: 'Create', + com_ui_skill_create_error: 'Failed to create skill', + com_ui_skill_name_exists: 'A skill with this name already exists', + com_ui_skill_name_reserved: 'This skill name is reserved', }; return translations[key] ?? key; }, @@ -73,6 +80,41 @@ jest.mock('~/utils', () => ({ describe('CreateSkillDialog', () => { beforeEach(() => { jest.clearAllMocks(); + mockOnError = undefined; + }); + + it('localizes structured validation errors', () => { + render(