| 1 | import fs from "fs"; |
| 2 | import { homedir } from "node:os"; |
| 3 | import { fileURLToPath, pathToFileURL } from "node:url"; |
| 4 | import path from "path"; |
| 5 | |
| 6 | import { languageForFilepath } from "../autocomplete/constants/AutocompleteLanguageInfo.js"; |
| 7 | import { ChatMessage, IDE } from "../index.js"; |
| 8 | import { renderChatMessage } from "../util/messageContent.js"; |
| 9 | import { getContinueGlobalPath } from "../util/paths.js"; |
| 10 | |
| 11 | // If useful elsewhere, helper funcs should move to core/util/index.ts or similar |
| 12 | function getOffsetDatetime(date: Date): Date { |
| 13 | const offset = date.getTimezoneOffset(); |
| 14 | const offsetHours = Math.floor(offset / 60); |
| 15 | const offsetMinutes = offset % 60; |
| 16 | date.setHours(date.getHours() - offsetHours); |
| 17 | date.setMinutes(date.getMinutes() - offsetMinutes); |
| 18 | |
| 19 | return date; |
| 20 | } |
| 21 | |
| 22 | function asBasicISOString(date: Date): string { |
| 23 | const isoString = date.toISOString(); |
| 24 | |
| 25 | return isoString.replace(/[-:]|(\.\d+Z)/g, ""); |
| 26 | } |
| 27 | |
| 28 | function reformatCodeBlocks(msgText: string): string { |
| 29 | const codeBlockFenceRegex = /```((.*?\.(\w+))\s*.*)\n/g; |
| 30 | msgText = msgText.replace( |
| 31 | codeBlockFenceRegex, |
| 32 | (match, metadata, filename, extension) => { |
| 33 | const lang = languageForFilepath(filename); |
| 34 | return `\`\`\`${extension}\n${lang.singleLineComment} ${metadata}\n`; |
| 35 | }, |
| 36 | ); |
| 37 | // Appease the markdown linter |
| 38 | return msgText.replace(/```\n```/g, "```\n\n```"); |
| 39 | } |
| 40 | |
| 41 | export function toMarkDown(history: ChatMessage[], time?: Date): string { |
| 42 | if (!time) { |
| 43 | time = new Date(); |
| 44 | } |
| 45 | let content = `### [Continue](https://continue.dev) session transcript\n Exported: ${time.toLocaleString()}`; |
| 46 | |
| 47 | for (const msg of history) { |
| 48 | let msgText = renderChatMessage(msg); |
| 49 | if (!msgText) { |
| 50 | continue; // Skip messages without content |
| 51 | } |
| 52 | |
| 53 | if (msg.role === "user" && msgText.search("```") > -1) { |
| 54 | msgText = reformatCodeBlocks(msgText); |
| 55 | } |
| 56 | |
| 57 | // format messages as blockquotes |
| 58 | msgText = msgText.replace(/^/gm, "> "); |
| 59 | |
| 60 | content += `\n\n#### ${ |
| 61 | msg.role === "user" ? "_User_" : "_Assistant_" |
| 62 | }\n\n${msgText}`; |
| 63 | } |
| 64 | return content; |
| 65 | } |
| 66 | |
| 67 | export async function shareSession( |
| 68 | ide: IDE, |
| 69 | history: ChatMessage[], |
| 70 | outputDir?: string, |
| 71 | ) { |
| 72 | const now = new Date(); |
| 73 | const content = toMarkDown(history, now); |
| 74 | |
| 75 | outputDir = outputDir ?? getContinueGlobalPath(); |
| 76 | |
| 77 | if (outputDir.startsWith("~")) { |
| 78 | outputDir = outputDir.replace(/^~/, homedir); |
| 79 | } else if ( |
| 80 | outputDir.startsWith("./") || |
| 81 | outputDir.startsWith(".\\") || |
| 82 | outputDir === "." |
| 83 | ) { |
| 84 | const workspaceDirs = await ide.getWorkspaceDirs(); |
| 85 | // Although the most common situation is to have one directory open in a |
| 86 | // workspace it's also possible to have just a file open without an |
| 87 | // associated directory or to use multi-root workspaces in which multiple |
| 88 | // folders are included. We default to using the first item in the list, if |
| 89 | // it exists. |
| 90 | const workspaceDirectory = workspaceDirs?.[0] || ""; |
| 91 | outputDir = outputDir.replace(/^./, fileURLToPath(workspaceDirectory)); |
| 92 | } |
| 93 | |
| 94 | if (!fs.existsSync(outputDir)) { |
| 95 | fs.mkdirSync(outputDir, { recursive: true }); |
| 96 | } |
| 97 | |
| 98 | const dtString = asBasicISOString(getOffsetDatetime(now)); |
| 99 | const outPath = path.join(outputDir, `${dtString}_session.md`); //TODO: more flexible naming? |
| 100 | |
| 101 | const fileUrl = pathToFileURL(outPath).toString(); // TODO switch from path to URI above ^ |
| 102 | await ide.writeFile(fileUrl, content); |
| 103 | await ide.openFile(fileUrl); |
| 104 | return fileUrl; |
| 105 | } |