diff --git a/packages/api/src/artifacts/update.spec.ts b/packages/api/src/artifacts/update.spec.ts new file mode 100644 index 0000000000..8bdd8e964e --- /dev/null +++ b/packages/api/src/artifacts/update.spec.ts @@ -0,0 +1,57 @@ +import { replaceArtifactContent } from './update'; + +const artifactText = [ + ':::artifact{identifier="example" type="text/markdown" title="Example"}', + '```md', + 'ORIGINAL', + '```', + ':::', +].join('\n'); + +const artifact = { + start: 0, + end: artifactText.length, + source: 'text' as const, + text: artifactText, +}; + +describe('replaceArtifactContent', () => { + it('normalizes blank lines before a closing code and artifact fence', () => { + const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', 'UPDATED\n\n'); + + expect(result).toContain('UPDATED\n```\n:::'); + }); + + it('normalizes before a code fence separated from the artifact close by blank lines', () => { + const text = artifactText.replace('```\n:::', '```\n\n:::'); + const result = replaceArtifactContent( + text, + { ...artifact, end: text.length, text }, + 'ORIGINAL', + 'UPDATED\n\n', + ); + + expect(result).toContain('UPDATED\n```\n\n:::'); + }); + + it('handles long whitespace near-misses without backtracking', () => { + const updated = `\n\`\`\`${' \n'.repeat(100_000)}X`; + const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', updated); + + expect(result).toContain(updated); + }, 1_000); + + it('handles long consecutive-newline near-misses without backtracking', () => { + const updated = `\n\`\`\`${'\n'.repeat(100_000)}X`; + const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', updated); + + expect(result).toContain(updated); + }, 1_000); + + it('normalizes a near-limit newline run without per-line allocations', () => { + const updated = `UPDATED${'\n'.repeat(2_500_000)}`; + const result = replaceArtifactContent(artifactText, artifact, 'ORIGINAL', updated); + + expect(result).toContain('UPDATED\n```\n:::'); + }, 1_000); +}); diff --git a/packages/api/src/artifacts/update.ts b/packages/api/src/artifacts/update.ts index b38bf9670d..d9c9582827 100644 --- a/packages/api/src/artifacts/update.ts +++ b/packages/api/src/artifacts/update.ts @@ -54,8 +54,7 @@ const getCloseRange = ( } const markerStart = lineStart + contentStart; - const markerText = text.slice(markerStart); - if (!markerText.startsWith(ARTIFACT_END) || markerText.startsWith(ARTIFACT_START)) { + if (!text.startsWith(ARTIFACT_END, markerStart) || text.startsWith(ARTIFACT_START, markerStart)) { return null; } @@ -213,8 +212,38 @@ const replaceRange = ( return originalText.substring(0, start) + updated + separator + endText; }; +const isClosingArtifactFenceAt = (text: string, start: number): boolean => { + const marker = text[start]; + if (marker !== '`' && marker !== '~') { + return false; + } + + let markerEnd = start + 1; + while (markerEnd < text.length && text[markerEnd] === marker) { + markerEnd++; + } + if (markerEnd - start < 3) { + return false; + } + + let hasLineBreak = false; + for (let i = markerEnd; i < text.length; i++) { + if (text[i] === '\n') { + hasLineBreak = true; + continue; + } + if (/\s/.test(text[i])) { + continue; + } + return hasLineBreak && text.startsWith(ARTIFACT_END, i); + } + return false; +}; + const normalizeBeforeClosingArtifactFence = (text: string): string => - text.replace(/\n+(?=(?:`{3,}|~{3,})\s*\n\s*:::)/g, '\n'); + text.replace(/\n{2,}/g, (newlines, offset: number) => + isClosingArtifactFenceAt(text, offset + newlines.length) ? '\n' : newlines, + ); export const findAllArtifacts = (message: ArtifactMessage): ArtifactBoundary[] => { const artifacts: ArtifactBoundary[] = []; diff --git a/packages/data-schemas/src/methods/skill.spec.ts b/packages/data-schemas/src/methods/skill.spec.ts index 2ca08cd2f8..387ee66885 100644 --- a/packages/data-schemas/src/methods/skill.spec.ts +++ b/packages/data-schemas/src/methods/skill.spec.ts @@ -7,6 +7,7 @@ import { AccessRoleIds, PrincipalType, PermissionBits, + SKILL_BODY_MAX_LENGTH, } from 'librechat-data-provider'; import { partitionIssues, @@ -565,6 +566,31 @@ describe('skill validation helpers', () => { }); describe('Skill CRUD methods', () => { + it('rejects an oversized body before scanning its frontmatter on create', async () => { + const body = `---\nalways-apply: a${' '.repeat(SKILL_BODY_MAX_LENGTH)}b\n---`; + + await expect(methods.createSkill(makeSkillInput({ body }))).rejects.toMatchObject({ + code: 'SKILL_VALIDATION_FAILED', + issues: [expect.objectContaining({ field: 'body', code: 'TOO_LONG' })], + }); + }); + + it('rejects an oversized body before scanning its frontmatter on update', async () => { + const { skill } = await methods.createSkill(makeSkillInput()); + const body = `---\nalways-apply: a${' '.repeat(SKILL_BODY_MAX_LENGTH)}b\n---`; + + await expect( + methods.updateSkill({ + id: skill._id.toString(), + expectedVersion: 1, + update: { body }, + }), + ).rejects.toMatchObject({ + code: 'SKILL_VALIDATION_FAILED', + issues: [expect.objectContaining({ field: 'body', code: 'TOO_LONG' })], + }); + }); + it('creates a skill with version 1 and default fileCount 0', async () => { const { skill, warnings } = await methods.createSkill(makeSkillInput()); expect(skill.name).toBe('demo-skill'); diff --git a/packages/data-schemas/src/methods/skill.ts b/packages/data-schemas/src/methods/skill.ts index b269a571de..a38afdc576 100644 --- a/packages/data-schemas/src/methods/skill.ts +++ b/packages/data-schemas/src/methods/skill.ts @@ -1153,15 +1153,18 @@ export function createSkillMethods( normalizedFrontmatter && 'frontmatter' in normalizedFrontmatter ? normalizedFrontmatter.frontmatter : data.frontmatter; + const bodyIssues = validateSkillBody(data.body); /* Parse body's always-apply status once — reused for validation (below) and derivation in `resolveAlwaysApplyFromInput`. Avoids parsing the same YAML frontmatter block twice per create. */ const bodyAlwaysApply = - data.body !== undefined ? extractAlwaysApplyFromBody(data.body) : undefined; + bodyIssues.length === 0 && data.body !== undefined + ? extractAlwaysApplyFromBody(data.body) + : undefined; const issues: ValidationIssue[] = [ ...validateSkillName(data.name), ...validateSkillDescription(data.description), - ...validateSkillBody(data.body), + ...bodyIssues, ...validateSkillDisplayTitle(data.displayTitle), ...validateSkillFrontmatter(frontmatter), ...validateAlwaysApply(data.alwaysApply), @@ -1484,17 +1487,20 @@ export function createSkillMethods( ? normalizedFrontmatter.frontmatter : update.frontmatter; + const bodyIssues = update.body !== undefined ? validateSkillBody(update.body) : []; /* Parse body's always-apply status once — reused for validation (precedence-aware, below) and the derivation cascade further down. Avoids parsing the same YAML frontmatter block twice per update. */ const bodyAlwaysApply = - update.body !== undefined ? extractAlwaysApplyFromBody(update.body) : undefined; + bodyIssues.length === 0 && update.body !== undefined + ? extractAlwaysApplyFromBody(update.body) + : undefined; const issues: ValidationIssue[] = []; if (update.name !== undefined) issues.push(...validateSkillName(update.name)); if (update.description !== undefined) issues.push(...validateSkillDescription(update.description)); - if (update.body !== undefined) issues.push(...validateSkillBody(update.body)); + issues.push(...bodyIssues); if (update.displayTitle !== undefined) issues.push(...validateSkillDisplayTitle(update.displayTitle)); if (update.frontmatter !== undefined) issues.push(...validateSkillFrontmatter(frontmatter)); diff --git a/packages/data-schemas/src/utils/yaml.spec.ts b/packages/data-schemas/src/utils/yaml.spec.ts new file mode 100644 index 0000000000..5f5dd08a0e --- /dev/null +++ b/packages/data-schemas/src/utils/yaml.spec.ts @@ -0,0 +1,15 @@ +import { stripYamlTrailingComment } from './yaml'; + +describe('stripYamlTrailingComment', () => { + it('strips comments separated from an unquoted scalar by whitespace', () => { + expect(stripYamlTrailingComment('true # enabled by default')).toBe('true'); + expect(stripYamlTrailingComment('false\t# disabled')).toBe('false'); + expect(stripYamlTrailingComment('hashtag#value')).toBe('hashtag#value'); + expect(stripYamlTrailingComment(' # comment only')).toBe(''); + }); + + it('handles long comment-free whitespace runs in linear time', () => { + const value = `a${' '.repeat(100_000)}b`; + expect(stripYamlTrailingComment(value)).toBe(value); + }, 1_000); +}); diff --git a/packages/data-schemas/src/utils/yaml.ts b/packages/data-schemas/src/utils/yaml.ts index e33e1d793c..8e2c13909c 100644 --- a/packages/data-schemas/src/utils/yaml.ts +++ b/packages/data-schemas/src/utils/yaml.ts @@ -10,6 +10,10 @@ */ export function stripYamlTrailingComment(value: string): string { if (value.trimStart().startsWith('#')) return ''; - const match = value.match(/^(.*?)\s+#.*$/); - return match ? match[1] : value; + for (let i = 1; i < value.length; i++) { + if (value[i] === '#' && /\s/.test(value[i - 1])) { + return value.slice(0, i).trimEnd(); + } + } + return value; }