|
| 1 | +import * as fs from "fs/promises" |
| 2 | +import * as path from "path" |
| 3 | +import { ClineMessage } from "../../shared/ExtensionMessage" |
| 4 | + |
| 5 | +export class ConversationSaver { |
| 6 | + private saveFolder: string |
| 7 | + private currentFilePath?: string |
| 8 | + |
| 9 | + constructor(saveFolder: string) { |
| 10 | + this.saveFolder = saveFolder |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * Creates a new conversation file with the given messages |
| 15 | + * @param messages The messages to save |
| 16 | + * @returns The path to the created file |
| 17 | + */ |
| 18 | + async saveConversation(messages: ClineMessage[]): Promise<string> { |
| 19 | + try { |
| 20 | + console.log("Attempting to save conversation to folder:", this.saveFolder) |
| 21 | + |
| 22 | + // Create save folder if it doesn't exist |
| 23 | + await fs.mkdir(this.saveFolder, { recursive: true }) |
| 24 | + console.log("Save folder created/verified") |
| 25 | + |
| 26 | + // Generate filename based on timestamp and first message |
| 27 | + const timestamp = new Date().toISOString().replace(/[:.]/g, "-") |
| 28 | + const firstMessage = messages.find((m) => m.type === "say" && m.say === "task") |
| 29 | + const taskText = firstMessage?.text?.slice(0, 50).replace(/[^a-zA-Z0-9]/g, "-") || "conversation" |
| 30 | + const filename = `${timestamp}-${taskText}.json` |
| 31 | + console.log("Generated filename:", filename) |
| 32 | + |
| 33 | + // Save to file |
| 34 | + this.currentFilePath = path.join(this.saveFolder, filename) |
| 35 | + console.log("Attempting to write to:", this.currentFilePath) |
| 36 | + |
| 37 | + await fs.writeFile( |
| 38 | + this.currentFilePath, |
| 39 | + JSON.stringify({ messages, lastUpdated: new Date().toISOString() }, null, 2), |
| 40 | + "utf-8", |
| 41 | + ) |
| 42 | + console.log("Successfully wrote file") |
| 43 | + |
| 44 | + return this.currentFilePath |
| 45 | + } catch (error) { |
| 46 | + console.error("Error saving conversation:", error) |
| 47 | + console.error("Error details:", error instanceof Error ? error.message : String(error)) |
| 48 | + console.error("Save folder was:", this.saveFolder) |
| 49 | + console.error("Current working directory:", process.cwd()) |
| 50 | + throw new Error("Failed to save conversation") |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Updates an existing conversation file with new messages |
| 56 | + * @param messages The updated messages to save |
| 57 | + * @returns The path to the updated file |
| 58 | + */ |
| 59 | + async updateConversation(messages: ClineMessage[]): Promise<string> { |
| 60 | + console.log("Attempting to update conversation") |
| 61 | + |
| 62 | + if (!this.currentFilePath) { |
| 63 | + console.log("No current file path, creating new conversation file") |
| 64 | + return await this.saveConversation(messages) |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + console.log("Updating existing file at:", this.currentFilePath) |
| 69 | + await fs.writeFile( |
| 70 | + this.currentFilePath, |
| 71 | + JSON.stringify({ messages, lastUpdated: new Date().toISOString() }, null, 2), |
| 72 | + "utf-8", |
| 73 | + ) |
| 74 | + console.log("Successfully updated conversation file") |
| 75 | + return this.currentFilePath |
| 76 | + } catch (error) { |
| 77 | + console.error("Error updating conversation:", error) |
| 78 | + console.error("Error details:", error instanceof Error ? error.message : String(error)) |
| 79 | + console.error("Current file path was:", this.currentFilePath) |
| 80 | + console.error("Current working directory:", process.cwd()) |
| 81 | + throw new Error("Failed to update conversation") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets the current conversation file path |
| 87 | + */ |
| 88 | + getCurrentFilePath(): string | undefined { |
| 89 | + return this.currentFilePath |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Updates the save folder path |
| 94 | + * @param newPath The new save folder path |
| 95 | + */ |
| 96 | + updateSaveFolder(newPath: string) { |
| 97 | + this.saveFolder = newPath |
| 98 | + // Reset current file path since we're changing folders |
| 99 | + this.currentFilePath = undefined |
| 100 | + } |
| 101 | +} |
0 commit comments