Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ipc-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const IpcMessages = {
CHAT_DELETE_CHAT: "clippy_chat_delete_chat",
CHAT_DELETE_ALL_CHATS: "clippy_chat_delete_all_chats",
CHAT_NEW_CHAT: "clippy_chat_new_chat",
CHAT_EXPORT_FOR_CLAUDE: "clippy_chat_export_for_claude",

// Clipboard
CLIPBOARD_WRITE: "clippy_clipboard_write",
Expand Down
43 changes: 42 additions & 1 deletion src/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { clipboard, Data, ipcMain } from "electron";
import { clipboard, Data, dialog, ipcMain } from "electron";
import fs from "fs";
import {
toggleChatWindow,
maximizeChatWindow,
Expand Down Expand Up @@ -95,9 +96,49 @@ export function setupIpcListeners() {
ipcMain.handle(IpcMessages.CHAT_DELETE_ALL_CHATS, () =>
getChatManager().deleteAllChats(),
);
ipcMain.handle(
IpcMessages.CHAT_EXPORT_FOR_CLAUDE,
async (_, chatWithMessages: ChatWithMessages) => {
const result = await dialog.showSaveDialog({
title: "Export Chat for Claude Code",
defaultPath: "chat-context.md",
filters: [{ name: "Markdown", extensions: ["md"] }],
});

if (result.canceled || !result.filePath) {
return false;
}

const content = formatChatForClaude(chatWithMessages);
await fs.promises.writeFile(result.filePath, content, "utf8");
return true;
},
);

// Clipboard
ipcMain.handle(IpcMessages.CLIPBOARD_WRITE, (_, data: Data) =>
clipboard.write(data, "clipboard"),
);
}

function formatChatForClaude(chatWithMessages: ChatWithMessages): string {
const date = new Date(chatWithMessages.chat.createdAt).toLocaleString();
const lines: string[] = [
"# Clippy Chat Context",
"",
`> Exported from Clippy on ${date}`,
"",
"## Conversation",
"",
];

for (const message of chatWithMessages.messages) {
if (!message.content) {
continue;
}
const speaker = message.sender === "user" ? "**User**" : "**Clippy**";
lines.push(`${speaker}: ${message.content}`, "");
}

return lines.join("\n");
}
1 change: 1 addition & 0 deletions src/renderer/clippyApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type ClippyApi = {
deleteAllChats: () => Promise<void>;
onNewChat: (callback: () => void) => void;
offNewChat: () => void;
exportChatForClaude: (chatWithMessages: ChatWithMessages) => Promise<boolean>;
// Clipboard
clipboardWrite: (data: Data) => Promise<void>;
};
Expand Down
41 changes: 41 additions & 0 deletions src/renderer/components/BubbleWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import { Chat } from "./Chat";
import { Settings } from "./Settings";
import { useBubbleView } from "../contexts/BubbleViewContext";
import { Chats } from "./Chats";
import { useChat } from "../contexts/ChatContext";
import { MessageRecord } from "../../types/interfaces";

export function Bubble() {
const { currentView, setCurrentView } = useBubbleView();
const { messages, currentChatRecord } = useChat();
const [isMaximized, setIsMaximized] = useState(false);
const [isExporting, setIsExporting] = useState(false);

const containerStyle = {
width: "calc(100% - 6px)",
Expand Down Expand Up @@ -57,6 +61,30 @@ export function Bubble() {
}
}, [setCurrentView, currentView]);

const handleExportForClaude = useCallback(async () => {
if (messages.length === 0) {
return;
}

setIsExporting(true);
try {
const messageRecords: MessageRecord[] = messages.map((m) => ({
id: m.id,
content: m.content,
sender: m.sender,
createdAt: m.createdAt,
}));
await clippyApi.exportChatForClaude({
chat: currentChatRecord,
messages: messageRecords,
});
} catch (error) {
console.error("Failed to export chat:", error);
} finally {
setIsExporting(false);
}
}, [messages, currentChatRecord]);

return (
<div className="bubble-container window" style={containerStyle}>
<div className="app-drag title-bar">
Expand All @@ -82,6 +110,19 @@ export function Bubble() {
>
Settings
</button>
{currentView === "chat" && messages.length > 0 && (
<button
style={{
marginRight: "8px",
paddingLeft: "8px",
paddingRight: "8px",
}}
onClick={handleExportForClaude}
disabled={isExporting}
>
Export for Claude
</button>
)}
<button
aria-label="Minimize"
onClick={() => clippyApi.minimizeChatWindow()}
Expand Down
32 changes: 32 additions & 0 deletions src/renderer/components/Chats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const Chats: React.FC<SettingsProps> = ({ onClose }) => {
deleteAllChats,
} = useChat();
const [isDeleting, setIsDeleting] = useState(false);
const [isExporting, setIsExporting] = useState(false);
const [selectedChatIndex, setSelectedChatIndex] = useState<number | null>(
null,
);
Expand Down Expand Up @@ -95,6 +96,31 @@ export const Chats: React.FC<SettingsProps> = ({ onClose }) => {
}
};

const handleExportForClaude = async () => {
if (
selectedChatIndex === null ||
selectedChatIndex >= chatsWithPreview.length
) {
return;
}

const chatId = chatsWithPreview[selectedChatIndex].id;
const chatWithMessages = await clippyApi.getChatWithMessages(chatId);

if (!chatWithMessages) {
return;
}

setIsExporting(true);
try {
await clippyApi.exportChatForClaude(chatWithMessages);
} catch (error) {
console.error("Failed to export chat:", error);
} finally {
setIsExporting(false);
}
};

const columns = [
{ key: "preview", header: "Preview" },
{ key: "lastUpdated", header: "Last Updated" },
Expand Down Expand Up @@ -126,6 +152,12 @@ export const Chats: React.FC<SettingsProps> = ({ onClose }) => {
>
Restore Chat
</button>
<button
onClick={handleExportForClaude}
disabled={isExporting || selectedChatIndex === null}
>
Export for Claude
</button>
<button
onClick={handleDeleteSelected}
disabled={isDeleting || selectedChatIndex === null}
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ const clippyApi: ClippyApi = {
offNewChat: () => {
ipcRenderer.removeAllListeners(IpcMessages.CHAT_NEW_CHAT);
},
exportChatForClaude: (chatWithMessages: ChatWithMessages) =>
ipcRenderer.invoke(IpcMessages.CHAT_EXPORT_FOR_CLAUDE, chatWithMessages),

// App
getVersions: () => ipcRenderer.invoke(IpcMessages.APP_GET_VERSIONS),
Expand Down