Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@red-hat-developer-hub/backstage-plugin-intelligent-assistant-backend': patch
---

Replace `markitdown-ts` with `unpdf` for AI Notebooks PDF text extraction. The backend no longer depends on the platform-specific `@napi-rs/canvas` binary, so it loads on non-x86-64 architectures, and its install footprint and startup memory are significantly smaller. Unreadable or password-protected PDFs, and PDFs without a text layer, are now rejected with a 400 error that does not include the file content.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"form-data": "^4.0.6",
"http-proxy-middleware": "^3.0.7",
"knex": "^3.1.0",
"markitdown-ts": "^0.0.10",
"multer": "^2.2.0"
"multer": "^2.2.0",
"unpdf": "^1.9.1"
},
"devDependencies": {
"@backstage/backend-test-utils": "^1.11.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

import { InputError } from '@backstage/errors';

import { convertToMarkdown } from './markitdownClient';

jest.mock('markitdown-ts', () => {
const convertBuffer = jest.fn();
return {
MarkItDown: jest.fn(() => ({ convertBuffer })),
__mockConvertBuffer: convertBuffer,
};
});
import { extractText } from 'unpdf';

import { convertToMarkdown } from './documentConverter';

function getMockConvertBuffer(): jest.Mock {
return require('markitdown-ts').__mockConvertBuffer;
}
jest.mock('unpdf', () => ({
extractText: jest.fn(),
}));

const mockExtractText = extractText as jest.Mock;

describe('convertToMarkdown', () => {
beforeEach(() => {
Expand All @@ -49,7 +45,7 @@ describe('convertToMarkdown', () => {
);

expect(result).toBe(content);
expect(getMockConvertBuffer()).not.toHaveBeenCalled();
expect(mockExtractText).not.toHaveBeenCalled();
},
);

Expand All @@ -59,7 +55,7 @@ describe('convertToMarkdown', () => {
const result = await convertToMarkdown(buffer, 'data.JSON', 'JSON');

expect(result).toBe('{"key": "value"}');
expect(getMockConvertBuffer()).not.toHaveBeenCalled();
expect(mockExtractText).not.toHaveBeenCalled();
});

it('should handle empty buffer for plaintext', async () => {
Expand All @@ -80,80 +76,138 @@ describe('convertToMarkdown', () => {

expect(result).toBe(content);
});
});

describe('markitdown conversion', () => {
it('should convert non-plaintext files via markitdown', async () => {
getMockConvertBuffer().mockResolvedValue({
markdown: '# Converted Content',
});
it('should not normalize whitespace for plaintext', async () => {
const content = '\n{\r\n "a": 1\r\n\n\n\n}\n';

const result = await convertToMarkdown(
Buffer.from('binary data'),
'doc.pdf',
'pdf',
Buffer.from(content),
'data.json',
'json',
);

expect(result).toBe('# Converted Content');
expect(getMockConvertBuffer()).toHaveBeenCalledWith(expect.any(Buffer), {
file_extension: '.pdf',
});
expect(result).toBe(content);
});
});

it('should convert txt files via markitdown', async () => {
getMockConvertBuffer().mockResolvedValue({
markdown: 'plain text content',
});
describe('text files', () => {
it.each(['txt', 'md'])(
'should return text for %s files without calling unpdf',
async fileType => {
const result = await convertToMarkdown(
Buffer.from('# Heading\n\nParagraph'),
`notes.${fileType}`,
fileType,
);

expect(result).toBe('# Heading\n\nParagraph');
expect(mockExtractText).not.toHaveBeenCalled();
},
);

it('should normalize CRLF and CR line endings', async () => {
const result = await convertToMarkdown(
Buffer.from('plain text content'),
Buffer.from('a\r\nb\rc\n'),
'readme.txt',
'txt',
);

expect(result).toBe('plain text content');
expect(getMockConvertBuffer()).toHaveBeenCalled();
expect(result).toBe('a\nb\nc');
});

it('should convert md files via markitdown', async () => {
getMockConvertBuffer().mockResolvedValue({
markdown: '# Heading\n\nParagraph',
});

it('should trim and collapse runs of blank lines', async () => {
const result = await convertToMarkdown(
Buffer.from('# Heading\n\nParagraph'),
Buffer.from('\n\n # Title\n\n\n\n\nBody \n\n\n'),
'notes.md',
'md',
);

expect(result).toBe('# Heading\n\nParagraph');
expect(result).toBe('# Title\n\nBody');
});

it('should throw InputError when conversion produces no output', async () => {
getMockConvertBuffer().mockResolvedValue({ markdown: '' });
it('should strip a UTF-8 byte order mark', async () => {
const result = await convertToMarkdown(
Buffer.from('\uFEFFText with BOM\n'),
'bom.txt',
'txt',
);

expect(result).toBe('Text with BOM');
});

it.each([
['empty', ''],
['whitespace-only', ' \n\n\t\r\n'],
])('should throw InputError for %s text files', async (_label, content) => {
await expect(
convertToMarkdown(Buffer.from('data'), 'bad.pdf', 'pdf'),
convertToMarkdown(Buffer.from(content), 'empty.txt', 'txt'),
).rejects.toThrow(InputError);
await expect(
convertToMarkdown(Buffer.from('data'), 'bad.pdf', 'pdf'),
convertToMarkdown(Buffer.from(content), 'empty.txt', 'txt'),
).rejects.toThrow(/no output/i);
});
});

describe('pdf files', () => {
it('should extract text from pdf files via unpdf', async () => {
mockExtractText.mockResolvedValue({
totalPages: 1,
text: 'Extracted text',
});

const result = await convertToMarkdown(
Buffer.from('%PDF-1.7'),
'doc.pdf',
'pdf',
);

expect(result).toBe('Extracted text');
expect(mockExtractText).toHaveBeenCalledWith(expect.any(Uint8Array), {
mergePages: true,
});
});

it('should throw InputError when conversion returns null result', async () => {
getMockConvertBuffer().mockResolvedValue(null);
it('should normalize extracted text', async () => {
mockExtractText.mockResolvedValue({
totalPages: 2,
text: '\nPage one\r\n\n\n\nPage two\n\n',
});

await expect(
convertToMarkdown(Buffer.from('data'), 'bad.pdf', 'pdf'),
).rejects.toThrow(InputError);
const result = await convertToMarkdown(
Buffer.from('%PDF-1.7'),
'doc.pdf',
'pdf',
);

expect(result).toBe('Page one\n\nPage two');
});

it('should throw InputError when markdown field is undefined', async () => {
getMockConvertBuffer().mockResolvedValue({});
it('should throw InputError when no text is extracted', async () => {
mockExtractText.mockResolvedValue({ totalPages: 1, text: ' \n' });

await expect(
convertToMarkdown(Buffer.from('data'), 'bad.pdf', 'pdf'),
convertToMarkdown(Buffer.from('%PDF-1.7'), 'scanned.pdf', 'pdf'),
).rejects.toThrow(InputError);
await expect(
convertToMarkdown(Buffer.from('%PDF-1.7'), 'scanned.pdf', 'pdf'),
).rejects.toThrow(/no output/i);
});

it('should wrap extraction failures in InputError without echoing file content', async () => {
const content = '%PDF-1.4 secret file content';
mockExtractText.mockRejectedValue(new Error('Invalid PDF structure.'));

const error = await convertToMarkdown(
Buffer.from(content),
'broken.pdf',
'pdf',
).catch(e => e);

expect(error).toBeInstanceOf(InputError);
expect(error.message).toMatch(
/Failed to extract text from broken\.pdf.*Invalid PDF structure/,
);
expect(error.message).not.toContain(content);
});
});

Expand All @@ -174,7 +228,7 @@ describe('convertToMarkdown', () => {
});

it('should accept when extension matches declared type', async () => {
getMockConvertBuffer().mockResolvedValue({ markdown: 'ok' });
mockExtractText.mockResolvedValue({ totalPages: 1, text: 'ok' });

const result = await convertToMarkdown(
Buffer.from('data'),
Expand All @@ -200,20 +254,18 @@ describe('convertToMarkdown', () => {
);

expect(result).toBe(content);
expect(getMockConvertBuffer()).not.toHaveBeenCalled();
expect(mockExtractText).not.toHaveBeenCalled();
},
);

it('should accept filename without extension', async () => {
getMockConvertBuffer().mockResolvedValue({ markdown: 'ok' });

const result = await convertToMarkdown(
Buffer.from('data'),
'Makefile',
'txt',
);

expect(result).toBe('ok');
expect(result).toBe('data');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,39 @@

import { InputError } from '@backstage/errors';

import { MarkItDown } from 'markitdown-ts';

const markitdown = new MarkItDown();
import { extractText } from 'unpdf';

const PLAINTEXT_EXTENSIONS = new Set(['.json', '.yaml', '.yml', '.log']);
const TEXT_EXTENSIONS = new Set(['.txt', '.md']);

/**
* Convert a document buffer to markdown using markitdown-ts.
* Plain-text formats (json, yaml, log) are passed through as-is.
* Normalize line endings, trim, and collapse runs of blank lines.
* Matches the output previously produced by markitdown-ts.
Comment thread
Jdubrick marked this conversation as resolved.
Resolved
*/
const normalizeText = (text: string): string =>
text
.replace(/\r\n|\r/g, '\n')
.trim()
.replace(/\n{3,}/g, '\n\n');

async function extractPdfText(
Comment thread
Jdubrick marked this conversation as resolved.
Resolved
buffer: Buffer,
originalName: string,
): Promise<string> {
try {
const { text } = await extractText(new Uint8Array(buffer), {
mergePages: true,
});
return text;
} catch (error) {
throw new InputError(`Failed to extract text from ${originalName}`, error);
}
}

/**
* Convert an uploaded document buffer to text for the vector store.
* Plain-text formats (json, yaml, log) are passed through as-is,
* txt and md are normalized, and pdf text is extracted with unpdf.
*/
export async function convertToMarkdown(
Comment thread
Jdubrick marked this conversation as resolved.
Resolved
buffer: Buffer,
Expand All @@ -49,15 +73,21 @@ export async function convertToMarkdown(
return buffer.toString('utf-8');
}

const result = await markitdown.convertBuffer(buffer, {
file_extension: ext,
});
let text: string;
if (ext === '.pdf') {
text = await extractPdfText(buffer, originalName);
} else if (TEXT_EXTENSIONS.has(ext)) {
text = buffer.toString('utf-8');
} else {
throw new InputError(`Unsupported file type "${fileType}"`);
}

if (!result?.markdown) {
const markdown = normalizeText(text);
if (!markdown) {
throw new InputError(
`Markdown conversion produced no output for ${originalName}`,
Comment thread
Jdubrick marked this conversation as resolved.
Resolved
);
}

return result.markdown;
return markdown;
}
Loading
Loading