( workspaceId: string, editMessageId?: string, messageText?: string )
| 11 | * Usage: bun debug send-message <workspace-id> [--edit <message-id>] [--message <text>] |
| 12 | */ |
| 13 | export function sendMessageCommand( |
| 14 | workspaceId: string, |
| 15 | editMessageId?: string, |
| 16 | messageText?: string |
| 17 | ) { |
| 18 | console.log(`\n=== Send Message Debug Tool ===\n`); |
| 19 | console.log(`Workspace: ${workspaceId}`); |
| 20 | if (editMessageId) { |
| 21 | console.log(`Edit Mode: Editing message ${editMessageId}`); |
| 22 | } |
| 23 | console.log(); |
| 24 | |
| 25 | // Load chat history to verify message exists if editing |
| 26 | const sessionDir = defaultConfig.getSessionDir(workspaceId); |
| 27 | const chatHistoryPath = path.join(sessionDir, "chat.jsonl"); |
| 28 | |
| 29 | if (!fs.existsSync(chatHistoryPath)) { |
| 30 | console.error(`❌ No chat history found at: ${chatHistoryPath}`); |
| 31 | console.log("\nAvailable workspaces:"); |
| 32 | const sessionsDir = getMuxSessionsDir(); |
| 33 | if (fs.existsSync(sessionsDir)) { |
| 34 | const sessions = fs.readdirSync(sessionsDir); |
| 35 | sessions.forEach((session) => console.log(` - ${session}`)); |
| 36 | } |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // Read and parse messages |
| 41 | // Note: We use a more flexible type here because the on-disk format includes workspaceId |
| 42 | // which is not part of the MuxMessage type (it's metadata that gets stripped) |
| 43 | const data = fs.readFileSync(chatHistoryPath, "utf-8"); |
| 44 | const messages: Array<MuxMessage & { workspaceId?: string }> = data |
| 45 | .split("\n") |
| 46 | .filter((line) => line.trim()) |
| 47 | .map((line) => JSON.parse(line) as MuxMessage & { workspaceId?: string }); |
| 48 | |
| 49 | if (messages.length === 0) { |
| 50 | console.log("❌ No messages in chat history"); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | // Check for workspace ID mismatches |
| 55 | const workspaceIds = new Set(messages.map((m) => m.workspaceId)); |
| 56 | if (workspaceIds.size > 1) { |
| 57 | console.log(`ℹ️ INFO: Multiple workspace IDs found in message history`); |
| 58 | console.log(` Current workspace: ${workspaceId}`); |
| 59 | console.log(` Message workspace IDs: ${Array.from(workspaceIds).join(", ")}`); |
| 60 | console.log(` This can occur after workspace renames. Messages are migrated during rename.\n`); |
| 61 | } else if (workspaceIds.size === 1 && !workspaceIds.has(workspaceId)) { |
| 62 | console.log(`ℹ️ INFO: Workspace ID mismatch detected`); |
| 63 | console.log(` Current workspace: ${workspaceId}`); |
| 64 | console.log(` Message workspace IDs: ${Array.from(workspaceIds)[0] ?? "unknown"}`); |
| 65 | console.log(` This workspace may have been renamed. Workspace IDs in messages are cosmetic`); |
| 66 | console.log(` and get updated automatically during history operations.\n`); |
| 67 | } |
| 68 | |
| 69 | console.log(`📝 Chat History (${messages.length} messages):\n`); |
| 70 |
no test coverage detected