UNPKG

@bestdefense/bd-agent

Version:

An AI-powered coding assistant CLI that connects to AWS Bedrock

143 lines 5.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.StreamingMarkdownRenderer = void 0; exports.renderMarkdown = renderMarkdown; exports.renderDiff = renderDiff; exports.renderInlineDiff = renderInlineDiff; const marked_1 = require("marked"); const cli_highlight_1 = require("cli-highlight"); const chalk_1 = __importDefault(require("chalk")); const { markedTerminal } = require('marked-terminal'); // Configure marked with terminal renderer marked_1.marked.use(markedTerminal({ // Code block rendering with syntax highlighting code: (code, language) => { try { if (language) { const highlighted = (0, cli_highlight_1.highlight)(code, { language }); return highlighted; } return chalk_1.default.gray(code); } catch (error) { return chalk_1.default.gray(code); } }, codespan: (code) => chalk_1.default.bgGray.white(` ${code} `), firstHeading: (text) => chalk_1.default.bold.blue(text), listitem: (text) => ` • ${text}`, strong: chalk_1.default.bold, em: chalk_1.default.italic })); function renderMarkdown(content) { return (0, marked_1.marked)(content); } // Function to display diffs with red/green highlighting function renderDiff(oldContent, newContent, context = 3) { const oldLines = oldContent.split('\n'); const newLines = newContent.split('\n'); let output = ''; let i = 0; let j = 0; // Simple diff algorithm (for now, just show all old as red and all new as green) // In a real implementation, we'd use a proper diff algorithm if (oldLines.join('\n') !== newLines.join('\n')) { output += chalk_1.default.red('--- Original\n'); output += chalk_1.default.green('+++ Modified\n'); output += '\n'; // Show removed lines for (const line of oldLines) { output += chalk_1.default.red(`- ${line}\n`); } output += '\n'; // Show added lines for (const line of newLines) { output += chalk_1.default.green(`+ ${line}\n`); } } return output; } // Function to render inline diffs (for edit operations) function renderInlineDiff(oldStr, newStr) { return `${chalk_1.default.red.strikethrough(oldStr)} → ${chalk_1.default.green(newStr)}`; } // Stream-friendly markdown rendering that handles partial content class StreamingMarkdownRenderer { buffer = ''; inCodeBlock = false; codeBlockLanguage = ''; codeBlockContent = ''; addChunk(chunk) { this.buffer += chunk; let output = ''; // Process complete lines const lines = this.buffer.split('\n'); this.buffer = lines.pop() || ''; // Keep incomplete line in buffer for (const line of lines) { output += this.processLine(line + '\n'); } return output; } flush() { const output = this.buffer ? this.processLine(this.buffer) : ''; this.buffer = ''; if (this.inCodeBlock && this.codeBlockContent) { // Close any open code block return output + this.renderCodeBlock(); } return output; } processLine(line) { // Check for code block markers const codeBlockMatch = line.match(/^```(\w*)/); if (codeBlockMatch) { if (!this.inCodeBlock) { // Starting a code block this.inCodeBlock = true; this.codeBlockLanguage = codeBlockMatch[1] || ''; this.codeBlockContent = ''; return ''; // Don't output the marker } else { // Ending a code block this.inCodeBlock = false; const output = this.renderCodeBlock(); this.codeBlockContent = ''; this.codeBlockLanguage = ''; return output; } } if (this.inCodeBlock) { this.codeBlockContent += line; return ''; // Buffer code block content } // For non-code content, apply basic formatting return this.applyInlineFormatting(line); } renderCodeBlock() { try { if (this.codeBlockLanguage) { const highlighted = (0, cli_highlight_1.highlight)(this.codeBlockContent, { language: this.codeBlockLanguage }); return highlighted + '\n'; } return chalk_1.default.gray(this.codeBlockContent) + '\n'; } catch (error) { return chalk_1.default.gray(this.codeBlockContent) + '\n'; } } applyInlineFormatting(text) { // Apply basic inline formatting text = text.replace(/`([^`]+)`/g, (_, code) => chalk_1.default.bgGray.white(` ${code} `)); text = text.replace(/\*\*([^*]+)\*\*/g, (_, bold) => chalk_1.default.bold(bold)); text = text.replace(/\*([^*]+)\*/g, (_, italic) => chalk_1.default.italic(italic)); return text; } } exports.StreamingMarkdownRenderer = StreamingMarkdownRenderer; //# sourceMappingURL=markdown-renderer.js.map