@vfarcic/dot-ai
Version:
AI-powered development productivity platform that enhances software development workflows through intelligent automation and AI-driven assistance
155 lines (154 loc) • 5.76 kB
JavaScript
;
/**
* Platform Utilities
*
* Shared utility functions for platform operations and tools.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.execAsync = void 0;
exports.getScriptsDir = getScriptsDir;
exports.extractJsonFromAIResponse = extractJsonFromAIResponse;
exports.extractContentFromMarkdownCodeBlocks = extractContentFromMarkdownCodeBlocks;
exports.extractJsonArrayFromAIResponse = extractJsonArrayFromAIResponse;
const child_process_1 = require("child_process");
const util_1 = require("util");
const path = __importStar(require("path"));
exports.execAsync = (0, util_1.promisify)(child_process_1.exec);
/**
* Get the scripts directory path, works in both development and installed npm package
*/
function getScriptsDir() {
// In CommonJS (after TypeScript compilation), __dirname is available
// Go up from dist/core/ to project root, then into scripts/
return path.join(__dirname, '..', '..', 'scripts');
}
/**
* Extract JSON object from AI response with robust parsing
* Handles markdown code blocks and finds proper JSON boundaries
*/
function extractJsonFromAIResponse(aiResponse) {
let jsonContent = aiResponse;
// First try to find JSON wrapped in code blocks
const codeBlockMatch = aiResponse.match(/```(?:json)?\s*(\{[\s\S]*?\})\s*```/);
if (codeBlockMatch) {
jsonContent = codeBlockMatch[1];
}
else {
// Try to find JSON that starts with { and find the matching closing }
const startIndex = aiResponse.indexOf('{');
if (startIndex !== -1) {
let braceCount = 0;
let endIndex = startIndex;
for (let i = startIndex; i < aiResponse.length; i++) {
if (aiResponse[i] === '{')
braceCount++;
if (aiResponse[i] === '}')
braceCount--;
if (braceCount === 0) {
endIndex = i;
break;
}
}
if (endIndex > startIndex) {
jsonContent = aiResponse.substring(startIndex, endIndex + 1);
}
}
}
try {
return JSON.parse(jsonContent.trim());
}
catch (error) {
throw new Error(`Failed to parse JSON from AI response: ${error}`, {
cause: error,
});
}
}
/**
* Extract content from markdown code blocks in AI responses
* Handles various code block formats: ```yaml, ```yml, ```json, or plain ```
*/
function extractContentFromMarkdownCodeBlocks(content, language) {
// Create regex pattern for the specified language or any language
const languagePattern = language ? `(?:${language})` : '(?:yaml|yml|json)?';
const regex = new RegExp(`\`\`\`${languagePattern}\\s*([\\s\\S]*?)\\s*\`\`\``, 'g');
const match = regex.exec(content);
if (match && match[1]) {
return match[1].trim();
}
// Return original content if no code blocks found
return content.trim();
}
/**
* Extract JSON array from AI response with robust parsing
* Handles markdown code blocks and finds proper array boundaries
*/
function extractJsonArrayFromAIResponse(aiResponse) {
let jsonContent = aiResponse;
// First try to find JSON array wrapped in code blocks
const codeBlockMatch = aiResponse.match(/```(?:json)?\s*(\[[\s\S]*?\])\s*```/);
if (codeBlockMatch) {
jsonContent = codeBlockMatch[1];
}
else {
// Try to find JSON array that starts with [ and find the matching closing ]
const startIndex = aiResponse.indexOf('[');
if (startIndex !== -1) {
let bracketCount = 0;
let endIndex = startIndex;
for (let i = startIndex; i < aiResponse.length; i++) {
if (aiResponse[i] === '[')
bracketCount++;
if (aiResponse[i] === ']')
bracketCount--;
if (bracketCount === 0) {
endIndex = i;
break;
}
}
if (bracketCount === 0) {
jsonContent = aiResponse.substring(startIndex, endIndex + 1);
}
}
}
try {
return JSON.parse(jsonContent.trim());
}
catch (error) {
throw new Error(`Failed to parse JSON array from AI response: ${error}`, {
cause: error,
});
}
}