-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
351 lines (308 loc) · 9.91 KB
/
Copy pathmain.js
File metadata and controls
351 lines (308 loc) · 9.91 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { app, BrowserWindow, ipcMain, protocol, net, shell } from "electron";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
import OpenAI from "openai";
import Store from "electron-store";
import { tools, toolFunctions } from "./tools/index.js";
import {
getFileContentForLLM,
processFileBufferForLLM,
} from "./helper/fileManager.js";
import crypto from "crypto";
import squirrelStartup from "electron-squirrel-startup";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
if (squirrelStartup) app.quit();
let mainWindow;
// Track the last known root directory to detect changes.
let lastKnownRootDir = null;
// Read default system prompt before initializing store
let defaultSystemPrompt = "";
try {
defaultSystemPrompt = fs.readFileSync(
path.join(__dirname, "DEFAULT_PROMPT.md"),
"utf-8"
);
} catch (error) {
console.error("Failed to read default system prompt:", error);
}
const store = new Store({
defaults: {
settings: {
apiKey: "",
modelName: "google/gemini-2.5-pro",
rootDir: "",
systemPrompt: defaultSystemPrompt,
sofficePath: "",
providerOrder: "",
},
},
});
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 600,
height: 800,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
mainWindow.loadFile("index.html");
};
app.whenReady().then(() => {
// Register sandbox protocol using modern API
protocol.handle("sandbox", (request) => {
// Parse URL properly instead of using deprecated substr
const parsedUrl = new URL(request.url);
const relativePath = parsedUrl.hostname + parsedUrl.pathname;
const sandboxDir = path.join(app.getPath("userData"), "sandbox");
const filePath = path.join(sandboxDir, relativePath);
// Security check: ensure the file is within sandbox directory
const normalizedPath = path.normalize(filePath);
if (!normalizedPath.startsWith(sandboxDir)) {
return new Response("File not found", { status: 404 });
}
// Return file response
return net.fetch(`file://${filePath}`);
});
createWindow();
});
ipcMain.handle("get-settings", () => {
return store.get("settings");
});
ipcMain.handle("set-settings", (event, settings) => {
store.set("settings", settings);
});
// IPC handler for opening external URLs
ipcMain.handle("open-external-url", async (event, url) => {
try {
await shell.openExternal(url);
return { success: true };
} catch (error) {
console.error("Failed to open external URL:", error);
return { success: false, error: error.message };
}
});
// IPC handler for processing pasted attachments
ipcMain.handle(
"process-attachment",
async (event, { fileBuffer, fileName }) => {
const settings = store.get("settings");
const toolContext = {
// rootDir is not strictly needed for buffer processing, but good to have
rootDir: settings.rootDir,
sofficePath: settings.sofficePath,
appDataDir: app.getPath("appData"),
};
try {
// Use the new buffer-based processing function
return await processFileBufferForLLM(
Buffer.from(fileBuffer),
fileName,
toolContext
);
} catch (error) {
// Propagate the error back to the renderer
throw new Error(
error.message || "An unknown error occurred during file processing."
);
}
}
);
ipcMain.handle("send-message", async (event, { messages }) => {
const settings = store.get("settings");
const { apiKey, modelName, systemPrompt, rootDir, providerOrder } = settings;
const logToRenderer = (payload) =>
mainWindow.webContents.send("debug-log", payload);
const updateHistory = (newHistory) =>
mainWindow.webContents.send("update-history", newHistory);
const openai = new OpenAI({
baseURL: "https://openrouter.ai/api/v1",
apiKey: apiKey,
});
let history = [...messages];
const currentRootDir = rootDir;
// Check if the root directory has changed since the last message.
if (currentRootDir !== lastKnownRootDir) {
history.push({
role: "user",
content: `\nNote: root directory has been changed to: "${currentRootDir}".`,
is_system_notification: true,
});
lastKnownRootDir = currentRootDir; // Update the last known state.
updateHistory(history); // Update the UI with the notification.
}
try {
// Main conversation loop
while (true) {
// Prepare messages with system prompt
const requestMessages = [...history];
if (
systemPrompt &&
(requestMessages.length === 0 || requestMessages[0].role !== "system")
) {
requestMessages.unshift({ role: "system", content: systemPrompt });
}
// Make API request
logToRenderer({
type: "API_REQUEST",
data: { modelName, messages: requestMessages, tools },
});
const providerOrderArr = providerOrder.split(",").map((p) => p.trim());
const response = await openai.chat.completions.create({
model: modelName,
messages: requestMessages,
provider: {
order: providerOrderArr,
},
tools: tools,
});
logToRenderer({ type: "API_SUCCESS", data: response });
// Add assistant message to history
const assistantMessage = response.choices[0].message;
history.push(assistantMessage);
// If no tool calls, we're done
if (!assistantMessage.tool_calls) {
updateHistory(history);
break;
}
// Show assistant message with tool calls immediately
updateHistory(history);
// Process tool calls
for (const toolCall of assistantMessage.tool_calls) {
const toolResult = await executeToolCall(
toolCall,
settings,
logToRenderer
);
if (toolResult && toolResult.is_file_viewer) {
// Also add a simplified success message for the tool call itself
// to let the model know the tool executed correctly.
history.push({
tool_call_id: toolCall.id,
role: "tool",
name: toolCall.function.name,
content: JSON.stringify({
success: true,
message: `File loaded into context.`,
}),
});
// This is a result from load_file, handle it specially
// Add the content message to history.
history.push({
role: toolResult.role,
content: toolResult.content,
is_file_viewer: true
});
} else {
// This is a regular tool result
history.push(toolResult);
}
// Update UI after each tool result
updateHistory(history);
}
}
return history;
} catch (error) {
logToRenderer({ type: "API_ERROR", data: error });
throw new Error(
`API Error: ${error.message || "Could not get a response from the model."
}`
);
}
});
// Helper function to execute a single tool call
async function executeToolCall(toolCall, settings, logToRenderer) {
const functionName = toolCall.function.name;
const sandboxDir = path.join(app.getPath("userData"), "sandbox");
if (!fs.existsSync(sandboxDir)) {
fs.mkdirSync(sandboxDir, { recursive: true });
}
const toolContext = {
...settings,
sandboxDir,
providerOrder: settings.providerOrder.split(",").map((p) => p.trim()),
appDataDir: app.getPath("appData"),
};
if (!toolFunctions[functionName]) {
return {
tool_call_id: toolCall.id,
role: "tool",
name: functionName,
content: JSON.stringify({ error: `Unknown tool: ${functionName}` }),
};
}
try {
let functionArgs = {};
if (toolCall.function.arguments) {
try {
functionArgs = JSON.parse(toolCall.function.arguments);
} catch (parseError) {
logToRenderer({
type: "TOOL_PARSE_WARNING",
data: {
functionName,
arguments: toolCall.function.arguments,
warning: "Failed to parse arguments, using empty object",
},
});
functionArgs = {};
}
}
const result = await toolFunctions[functionName](functionArgs, toolContext);
// This is the special handler for the raw file content from load_file
if (result && result.is_file_viewer) {
if (result.error) {
// If the tool returned an error object, format it for the model
return {
tool_call_id: toolCall.id,
role: "tool",
name: functionName,
content: JSON.stringify({ error: result.error }),
};
}
return result; // Pass the special file message object through
}
// For all other tools, stringify the result
const content =
typeof result === "object" ? JSON.stringify(result) : result.toString();
return {
tool_call_id: toolCall.id,
role: "tool",
name: functionName,
content: content,
};
} catch (error) {
const errorInfo = {
functionName,
args: toolCall.function.arguments,
message: error.message,
stack: error.stack,
};
logToRenderer({ type: "TOOL_ERROR", data: errorInfo });
let userErrorMessage = `Tool execution failed: ${error.message}`;
if (error.name === "SecurityError") {
userErrorMessage = `Security Error: ${error.message}`;
} else if (error.name === "NotFoundError") {
userErrorMessage = `File Not Found: ${error.message}`;
} else if (error.name === "ConfigurationError") {
userErrorMessage = `Configuration Error: ${error.message}. Please check your settings.`;
}
return {
tool_call_id: toolCall.id,
role: "tool",
name: functionName,
content: JSON.stringify({ error: userErrorMessage }),
};
}
}
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});