Skip to content
Merged
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
61 changes: 60 additions & 1 deletion src/main/ipc/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function registerIpcHandlers() {
}
});

// AI context handler
// AI context handlers
ipcMain.handle('browsing:getContext', async (event, limit?: number) => {
try {
if (limit !== undefined) {
Expand All @@ -194,6 +194,43 @@ export function registerIpcHandlers() {
}
});

// Get current page context (URL, title, selected text, etc.)
ipcMain.handle('page:getContext', async (event, pageInfo: any) => {
try {
// Validate page info
if (!pageInfo || typeof pageInfo !== 'object') {
return { url: '', title: '' }; // Return empty context if invalid
}

const context: any = {};

if (pageInfo.url) {
validateString(pageInfo.url, 'Page URL', 2048);
context.url = pageInfo.url;
}

if (pageInfo.title) {
validateString(pageInfo.title, 'Page title', 1024);
context.title = pageInfo.title;
}

if (pageInfo.selectedText) {
validateString(pageInfo.selectedText, 'Selected text', 50000);
context.selectedText = pageInfo.selectedText;
}

if (pageInfo.content) {
validateString(pageInfo.content, 'Page content', 100000);
context.content = pageInfo.content;
}

return context;
} catch (error: any) {
console.error('page:getContext validation error:', error.message);
throw error;
}
});

// Tab session handlers
ipcMain.handle('tabs:save', async (event, tabs: Tab[]) => {
try {
Expand Down Expand Up @@ -322,12 +359,23 @@ export function registerIpcHandlers() {
validateString(options.system, 'System prompt', 10000);
}

// Validate context if provided
if (options.context) {
if (options.context.page?.url) {
validateString(options.context.page.url, 'Page URL', 2048);
}
if (options.context.page?.title) {
validateString(options.context.page.title, 'Page title', 1024);
}
}

// Stream response tokens back to renderer
const generator = ollamaService.generate({
model: options.model,
prompt: options.prompt,
images: options.images,
system: options.system,
context: options.context,
stream: true,
});

Expand Down Expand Up @@ -365,10 +413,21 @@ export function registerIpcHandlers() {
}
}

// Validate context if provided
if (options.context) {
if (options.context.page?.url) {
validateString(options.context.page.url, 'Page URL', 2048);
}
if (options.context.page?.title) {
validateString(options.context.page.title, 'Page title', 1024);
}
}

// Stream response tokens back to renderer
const generator = ollamaService.chat({
model: options.model,
messages: options.messages,
context: options.context,
stream: true,
});

Expand Down
1 change: 1 addition & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ALLOWED_INVOKE_CHANNELS = [
'bookmark:deleteByUrl',
'bookmark:update',
'browsing:getContext',
'page:getContext',
'tabs:save',
'tabs:load',
'tabs:clear',
Expand Down
Loading