1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
import { ipcMain, dialog, type BrowserWindow } from "electron";
import * as projects from "../db/projects";
import * as sessions from "../db/sessions";
import * as claude from "../claude";
import type { UserPermissionMode } from "../claude/phases";
import * as git from "../git/worktree";
export function registerIpcHandlers(mainWindow: BrowserWindow): void {
// Projects
ipcMain.handle("projects:list", () => projects.listProjects());
ipcMain.handle("projects:create", (_, name: string, path: string) =>
projects.createProject(name, path)
);
ipcMain.handle("projects:delete", (_, id: string) =>
projects.deleteProject(id)
);
// Sessions
ipcMain.handle("sessions:list", (_, projectId: string) =>
sessions.listSessions(projectId)
);
ipcMain.handle("sessions:create", async (_, projectId: string, name: string) => {
const session = sessions.createSession(projectId, name);
const project = projects.getProject(projectId);
if (project && git.isGitRepo(project.path)) {
try {
// Create git worktree for this session
git.createWorktree(project.path, session.id);
} catch (error) {
console.error("Failed to create worktree:", error);
// Continue without worktree - not fatal
}
}
return session;
});
ipcMain.handle("sessions:delete", (_, id: string) => {
const session = sessions.getSession(id);
if (session) {
const project = projects.getProject(session.project_id);
if (project) {
// Clean up worktree if exists
try {
git.removeWorktree(project.path, id);
} catch {
// Worktree might not exist, that's ok
}
// Clean up session artifacts
claude.clearSessionArtifacts(project.path, id);
}
}
sessions.deleteSession(id);
});
ipcMain.handle("sessions:get", (_, id: string) => sessions.getSession(id));
// Messages
ipcMain.handle("messages:list", (_, sessionId: string) =>
sessions.listMessages(sessionId)
);
// Chat
ipcMain.handle(
"chat:send",
async (_, sessionId: string, message: string) => {
const session = sessions.getSession(sessionId);
if (!session) throw new Error("Session not found");
// Store user message
sessions.addMessage(sessionId, "user", message);
await claude.sendMessage({
session,
message,
onMessage: (msg) => {
// Forward all messages to renderer
mainWindow.webContents.send("claude:message", sessionId, msg);
// Store assistant text messages
if (msg.type === "assistant") {
const content = (msg.message.content as Array<{ type: string; text?: string }>)
.filter((c) => c.type === "text" && c.text)
.map((c) => c.text!)
.join("\n");
if (content) {
sessions.addMessage(sessionId, "assistant", content);
}
}
},
});
}
);
ipcMain.handle("chat:interrupt", (_, sessionId: string) => {
claude.interruptSession(sessionId);
});
// Workflow
ipcMain.handle("workflow:review", async (_, sessionId: string) => {
const session = sessions.getSession(sessionId);
if (!session) throw new Error("Session not found");
await claude.triggerReview(session, (msg) => {
mainWindow.webContents.send("claude:message", sessionId, msg);
});
});
ipcMain.handle("workflow:advance", (_, sessionId: string) => {
const session = sessions.getSession(sessionId);
if (!session) throw new Error("Session not found");
return claude.advancePhase(session);
});
ipcMain.handle(
"workflow:setPermissionMode",
(_, sessionId: string, mode: string) => {
sessions.updateSession(sessionId, {
permission_mode: mode as UserPermissionMode,
});
}
);
// Session Artifacts (new session-specific API)
ipcMain.handle(
"artifact:readSession",
(_, projectPath: string, sessionId: string, filename: string) => {
return claude.readSessionArtifact(projectPath, sessionId, filename);
}
);
ipcMain.handle(
"artifact:writeSession",
(_, projectPath: string, sessionId: string, filename: string, content: string) => {
claude.writeSessionArtifact(projectPath, sessionId, filename, content);
}
);
// CLAUDE.md
ipcMain.handle("claudemd:read", (_, projectPath: string) => {
return claude.readClaudeMd(projectPath);
});
ipcMain.handle("claudemd:write", (_, projectPath: string, content: string) => {
claude.writeClaudeMd(projectPath, content);
});
// Legacy artifact API (for backward compatibility)
ipcMain.handle(
"artifact:read",
(_, projectPath: string, filename: string) => {
return claude.readArtifact(projectPath, filename);
}
);
ipcMain.handle(
"artifact:write",
(_, projectPath: string, filename: string, content: string) => {
claude.writeArtifact(projectPath, filename, content);
}
);
// Git
ipcMain.handle("git:isRepo", (_, projectPath: string) => {
return git.isGitRepo(projectPath);
});
ipcMain.handle("git:worktreeInfo", (_, projectPath: string, sessionId: string) => {
return git.getWorktreeInfo(projectPath, sessionId);
});
ipcMain.handle("git:commit", (_, worktreePath: string, message: string, files?: string[]) => {
git.commitChanges(worktreePath, message, files);
});
ipcMain.handle("git:hasChanges", (_, worktreePath: string) => {
return git.hasUncommittedChanges(worktreePath);
});
// Dialogs
ipcMain.handle("dialog:selectDirectory", async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ["openDirectory"],
});
return result.canceled ? null : result.filePaths[0];
});
}
|