// Name: Contrarian Prompts
// Description: Generates contrarian prompts from dropped files
// Author: tayiorbeii
import '@johnlindquist/kit';
// Type definition for the file information
interface FileInfo {
path: string;
baseName: string;
isQuestionFile: boolean;
}
// Get dropped files, exit if none
const droppedFiles = await drop();
if (!droppedFiles || droppedFiles.length === 0) {
console.warn("No files were dropped.");
exit();
}
// Map dropped files to FileInfo objects
const fileInfos: FileInfo[] = droppedFiles.map((file) => {
const baseName = path.basename(file.path, path.extname(file.path)).replace(/-questions$/, '');
const isQuestionFile = path.basename(file.path).includes('-questions');
return {
path: file.path,
baseName,
isQuestionFile,
};
});
// Group files by base name
const groupedFiles: { [key: string]: FileInfo[] } = fileInfos.reduce((acc, fileInfo) => {
acc[fileInfo.baseName] = acc[fileInfo.baseName] || [];
acc[fileInfo.baseName].push(fileInfo);
return acc;
}, {} as { [key: string]: FileInfo[] });
const outputArray = [];
// Generate prompts for each group
for (const baseName in groupedFiles) {
if (!Object.hasOwn(groupedFiles, baseName)) continue; // Safe iteration over objects
const files = groupedFiles[baseName];
const sourceFile = files.find(file => !file.isQuestionFile);
const questionFile = files.find(file => file.isQuestionFile);
let userMessage = "";
if (sourceFile) {
const sourceContent = await readFile(sourceFile.path, 'utf-8');
userMessage += `<source material>\n${sourceContent}\n</source material>\n\n`;
}
userMessage += `<instructions>\nPerform web research to find contrarian viewpoints to the above, from 2024 and more recent. Summarize the contrarian points and provide links to their sources.\n\nAfter summarizing your research, provide a list of several questions that could be asked of the source material's author to engage in a lively debate on their writing.\n</instructions>\n\n<guidelines>\nAvoid questions about ethics, ethical dilemmas, bias, etc.\n</guidelines>`;
outputArray.push({
userMessage,
fileName: `${baseName}-contrarian.md`
});
}
// Sort output array by fileName
outputArray.sort((a, b) => a.fileName.localeCompare(b.fileName));
// Write output to a JSON file and reveal it
const outputPath = path.join(tmpPath(), "contrarian-prompts.json");
await writeFile(outputPath, JSON.stringify(outputArray, null, 2));
await revealFile(outputPath);