-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
1332 lines (1160 loc) · 55.6 KB
/
service-worker.js
File metadata and controls
1332 lines (1160 loc) · 55.6 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SideLlama Service Worker - Final Corrected Version
// Import shared utilities
importScripts('shared-utils.js');
importScripts('model-utils.js');
class OllamaService {
constructor() {
this.baseURL = 'http://localhost:11434';
this.settings = {};
this.activeRequests = new Map(); // Track multiple concurrent requests by ID
this.requestCounter = 0; // Generate unique request IDs
this.modelUsageStats = new Map(); // Track model usage for smart preloading
this.lastUsedModels = []; // Keep track of recently used models
this.modelCache = { models: null, timestamp: 0, ttl: 30000 }; // 30 second cache
this.initializeService();
}
async initializeService() {
await this.loadSettings();
await this.loadModelUsageStats();
console.log('🦙 SideLlama service initialized');
// Preload the most frequently used model on startup
this.preloadFrequentlyUsedModel();
}
async loadModelUsageStats() {
try {
const result = await chrome.storage.local.get('modelUsageStats');
if (result.modelUsageStats) {
this.modelUsageStats = new Map(result.modelUsageStats);
}
} catch (error) {
console.error('Failed to load model usage stats:', error);
}
}
async saveModelUsageStats() {
try {
// Limit the number of models tracked to prevent storage bloat
const statsArray = Array.from(this.modelUsageStats.entries());
const limitedStats = statsArray.slice(0, 20); // Keep only top 20 models
await chrome.storage.local.set({
modelUsageStats: limitedStats
});
} catch (error) {
console.error('Failed to save model usage stats:', error);
}
}
trackModelUsage(modelName) {
// Update usage count
const currentCount = this.modelUsageStats.get(modelName) || 0;
this.modelUsageStats.set(modelName, currentCount + 1);
// Update recently used models list
this.lastUsedModels = this.lastUsedModels.filter(m => m !== modelName);
this.lastUsedModels.unshift(modelName);
if (this.lastUsedModels.length > 5) {
this.lastUsedModels.pop();
}
// Save stats
this.saveModelUsageStats();
console.log(`🦙 Model usage tracked: ${modelName} (${currentCount + 1} times)`);
}
async preloadFrequentlyUsedModel() {
if (this.modelUsageStats.size === 0) return;
// Find the most frequently used model
let mostUsedModel = '';
let maxUsage = 0;
for (const [model, usage] of this.modelUsageStats) {
if (usage > maxUsage) {
maxUsage = usage;
mostUsedModel = model;
}
}
if (mostUsedModel && maxUsage > 2) { // Only preload if used more than twice
console.log(`🦙 Preloading frequently used model: ${mostUsedModel}`);
await this.preloadModel(mostUsedModel);
}
}
async preloadModel(modelName) {
try {
// Send empty message to load model into memory
await fetch(`${this.baseURL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: modelName,
messages: [],
stream: false
})
});
console.log(`🦙 Model preloaded: ${modelName}`);
} catch (error) {
console.warn(`Failed to preload model ${modelName}:`, error);
}
}
async loadSettings() {
const result = await chrome.storage.sync.get('sideLlamaSettings');
// Default settings that match settings.js
const defaultSettings = {
ollamaUrl: 'http://localhost:11434',
enableToolCalls: true,
autoManageToolCalls: true, // Smart auto-configuration based on model capabilities
enableThinking: true, // Enable thinking mode for compatible models
defaultModel: 'qwen2.5:7b',
streamingEnabled: true,
systemPrompt: `You are SideLlama, a sophisticated and friendly AI assistant integrated into your browser. You are a knowledgeable and helpful companion for a wide range of tasks, from quick questions to in-depth research.
**Your Persona:**
- You are intelligent, kind, and proactive. You can lead the conversation and suggest new directions.
- You are a master of markdown and use it to create beautiful, easy-to-read responses.
- You enjoy thoughtful discussions about science, philosophy, and technology.
**Your Capabilities:**
- **Web Search:** You can search the web for up-to-date information.
- **Vision:** You can analyze images and screenshots.
- **Code Generation:** You can write and format code in various languages. You will always add comments to your code to explain what it does.
- **Markdown Formatting:** You can create beautiful and easy-to-read responses using markdown.
**Output Format:**
- Use markdown for all responses.
- Use headings, lists, and tables to organize information.
- Use code blocks for code snippets, and always add comments to your code.
- Use bold and italics to emphasize key points.
- Keep your responses concise and to the point.`,
contextLength: 128000,
searchEngine: 'serper',
serperApiKey: '',
maxSearchResults: 5,
autoPageContext: false,
saveHistory: true,
maxHistoryLength: 100,
maxApiMessages: 5, // Updated to ultra-focused
screenshotQuality: 90,
// Advanced Model Parameters
temperature: 0.8,
topP: 0.9,
topK: 20,
seed: null,
repeatPenalty: 1.1,
enableAdvancedParams: false,
// Structured Outputs
outputFormat: 'auto',
enableStructuredOutput: false,
jsonSchema: '',
// Performance Settings
keepAlive: '5m',
showPerformanceStats: false,
autoRefreshModels: false,
// Thinking Display
showThinkingProcess: true
};
this.settings = result.sideLlamaSettings || defaultSettings;
// Merge any missing settings with defaults (for existing users)
this.settings = { ...defaultSettings, ...this.settings };
}
trimMessagesToContextLimit(messages) {
const maxMessages = this.settings.maxApiMessages || 5;
// Simple approach: keep system prompt + last N messages
if (messages.length <= maxMessages) {
return messages;
}
// Find system prompt and preserve it
const systemPrompt = messages.find(msg => msg.role === 'system');
const nonSystemMessages = messages.filter(msg => msg.role !== 'system');
// Take the most recent messages (minus 1 slot for system prompt if it exists)
const keepCount = systemPrompt ? maxMessages - 1 : maxMessages;
const recentMessages = nonSystemMessages.slice(-keepCount);
// Combine system prompt (if exists) + recent messages
const result = systemPrompt ? [systemPrompt, ...recentMessages] : recentMessages;
const trimmedCount = messages.length - result.length;
if (trimmedCount > 0) {
console.log(`🦙 Context trimmed: ${messages.length} → ${result.length} messages`);
// Send context info to sidepanel for UI display
this.sendToSidePanel({
type: 'CONTEXT_INFO',
data: {
messageCount: result.length,
trimmedCount: trimmedCount,
totalMessages: messages.length
}
});
}
return result;
}
async checkConnection() {
try {
const response = await fetch(`${this.baseURL}/api/tags`);
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
return { status: 'connected' };
} catch (error) {
return { status: 'error', error: error.message };
}
}
async loadModels(forceRefresh = false) {
// Check cache first to eliminate duplicate API calls
const now = Date.now();
if (!forceRefresh && this.modelCache.models && (now - this.modelCache.timestamp) < this.modelCache.ttl) {
return { success: true, models: this.modelCache.models };
}
try {
const response = await fetch(`${this.baseURL}/api/tags`);
const data = await response.json();
// Enhanced model info with capabilities
const enhancedModels = (data.models || []).map(model => ({
...model,
displayName: model.name,
size: this.formatBytes(model.size),
capabilities: this.getModelCapabilities(model),
family: model.details?.family || 'unknown',
parameterSize: model.details?.parameter_size || 'unknown'
}));
// Update cache
this.modelCache = {
models: enhancedModels,
timestamp: now,
ttl: 30000
};
return { success: true, models: enhancedModels };
} catch (error) {
return { success: false, error: error.message };
}
}
getModelCapabilities(model) {
// Handle both model objects and model name strings
const modelName = typeof model === 'string' ? model : model.name;
// Use shared utility to eliminate code duplication
return ModelUtils.getModelCapabilities(modelName);
}
isThinkingModel(modelName) {
// Use shared utility to eliminate code duplication
return ModelUtils.isThinkingModel(modelName);
}
formatBytes(bytes) {
// Use shared utility to eliminate code duplication
return SharedUtils.formatBytes(bytes);
}
async pullModel(modelName, progressCallback = null) {
try {
const response = await fetch(`${this.baseURL}/api/pull`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: modelName, stream: true })
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (!line.trim()) continue;
try {
const data = JSON.parse(line);
if (progressCallback) {
progressCallback(data);
}
if (data.status === 'success') {
return { success: true };
}
} catch (e) {
console.warn('Failed to parse pull progress:', line, e);
}
}
}
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async deleteModel(modelName) {
try {
const response = await fetch(`${this.baseURL}/api/delete`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: modelName })
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
return { success: true };
} catch (error) {
return { success: false, error: error.message };
}
}
async getModelInfo(modelName) {
try {
const response = await fetch(`${this.baseURL}/api/show`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ model: modelName })
});
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
const data = await response.json();
return { success: true, modelInfo: data };
} catch (error) {
return { success: false, error: error.message };
}
}
async getRunningModels() {
try {
const response = await fetch(`${this.baseURL}/api/ps`);
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
const data = await response.json();
return { success: true, models: data.models || [] };
} catch (error) {
return { success: false, error: error.message };
}
}
async sendMessage(data, sender) {
await this.loadSettings();
const { message, model, context, messages, image, images, imageAttachments } = data;
const streamingEnabled = this.settings.streamingEnabled;
// Generate unique request ID for this request
const requestId = ++this.requestCounter;
// Track model usage for smart preloading
this.trackModelUsage(model);
// Get tab ID - handle both direct calls and sidepanel context
let tabId = sender.tab?.id;
if (!tabId) {
// If no tab ID from sender, get active tab
try {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
tabId = tabs[0]?.id;
} catch (error) {
console.warn('Could not get active tab for context:', error);
}
}
// Trim messages to fit context length - use 128k max with proper trimming
let apiMessages = messages ? this.trimMessagesToContextLimit(messages) : [];
// Always ensure system prompt is present and current
const systemPromptContent = this.settings.systemPrompt || 'You are a helpful AI assistant named SideLlama.';
const existingSystemMsg = apiMessages.find(msg => msg.role === 'system');
if (existingSystemMsg) {
// Update existing system prompt to latest settings
existingSystemMsg.content = systemPromptContent;
console.log('🦙 Updated system prompt:', systemPromptContent.substring(0, 100) + '...');
} else {
// Add system prompt at the beginning
apiMessages.unshift({ role: 'system', content: systemPromptContent });
console.log('🦙 Added system prompt:', systemPromptContent.substring(0, 100) + '...');
}
// Add the current user message (check for duplicates first)
const lastMessage = apiMessages[apiMessages.length - 1];
if (!lastMessage || lastMessage.role !== 'user' || lastMessage.content !== message) {
apiMessages.push({ role: 'user', content: message });
}
// MODERATE FIX: Standardized image handling for all formats
const lastUserMessage = apiMessages[apiMessages.length - 1];
let processedImages = [];
try {
// Process imageAttachments format (from UI)
if (imageAttachments && imageAttachments.length > 0) {
processedImages = imageAttachments.map(att => {
if (!att.dataUrl || !att.dataUrl.includes(',')) {
throw new Error(`Invalid image data URL format for ${att.filename || 'unknown file'}`);
}
const base64Data = att.dataUrl.split(',')[1];
if (!base64Data) {
throw new Error(`No base64 data found for ${att.filename || 'unknown file'}`);
}
return base64Data;
});
}
// Process legacy images array format
else if (images && images.length > 0) {
processedImages = Array.isArray(images) ? images : [images];
}
// Process single image format
else if (image) {
processedImages = [image];
}
// Only add images if we have any
if (processedImages.length > 0) {
lastUserMessage.images = processedImages;
console.log(`🖼️ Added ${processedImages.length} images to message for model ${model}`);
}
} catch (error) {
throw new Error(`Image processing failed: ${error.message}`);
}
if (context) {
apiMessages.splice(-1, 0, { role: 'system', content: `Context: ${context.title} - ${context.content.substring(0, 4000)}...` });
}
const requestBody = {
model,
messages: apiMessages,
stream: streamingEnabled,
};
// Only add tools if enabled in settings, model supports tools, and no images are present
// Images and tools can't be used together in most vision models
// MODERATE FIX: Comprehensive image detection across all formats
const hasImages = apiMessages.some(msg => msg.images && msg.images.length > 0) ||
(imageAttachments && imageAttachments.length > 0) ||
(images && images.length > 0) ||
(image !== undefined && image !== null);
const modelSupportsTools = ModelUtils.supportsTools(model);
if (this.settings.enableToolCalls && modelSupportsTools && !hasImages) {
requestBody.tools = this.getBuiltInTools();
}
// Add thinking support for thinking models
if (this.isThinkingModel(model)) {
requestBody.think = this.settings.enableThinking !== false; // Default to true for thinking models
}
// Add advanced parameters if enabled
if (this.settings.enableAdvancedParams) {
const options = {};
if (this.settings.temperature !== undefined) {
options.temperature = this.settings.temperature;
}
if (this.settings.topP !== undefined) {
options.top_p = this.settings.topP;
}
if (this.settings.topK !== undefined) {
options.top_k = this.settings.topK;
}
if (this.settings.repeatPenalty !== undefined) {
options.repeat_penalty = this.settings.repeatPenalty;
}
if (this.settings.seed !== null && this.settings.seed !== undefined) {
options.seed = this.settings.seed;
}
if (Object.keys(options).length > 0) {
requestBody.options = options;
}
}
// Add structured output formatting
if (this.settings.enableStructuredOutput && this.settings.outputFormat !== 'auto') {
if (this.settings.outputFormat === 'json') {
requestBody.format = 'json';
} else if (this.settings.outputFormat === 'schema' && this.settings.jsonSchema) {
try {
// Validate JSON schema before sending
JSON.parse(this.settings.jsonSchema);
requestBody.format = this.settings.jsonSchema;
} catch (error) {
console.warn('Invalid JSON schema, falling back to normal format:', error);
}
}
}
// Add keep-alive setting
if (this.settings.keepAlive) {
requestBody.keep_alive = this.settings.keepAlive;
}
try {
// Create abort controller for this specific request
const abortController = new AbortController();
this.activeRequests.set(requestId, abortController);
// Debug: Log what we're sending to Ollama
const hasImagesInRequest = requestBody.messages.some(msg => msg.images && msg.images.length > 0);
console.log(`🚀 Sending request to Ollama:`, {
model: requestBody.model,
hasImages: hasImagesInRequest,
messageCount: requestBody.messages.length,
hasTools: !!requestBody.tools,
streaming: requestBody.stream
});
const response = await fetch(`${this.baseURL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestBody),
signal: abortController.signal
});
if (!response.ok) {
let errorDetail = `HTTP ${response.status}: ${response.statusText}`;
try {
const errorBody = await response.text();
if (errorBody) {
errorDetail += ` - ${errorBody}`;
}
} catch (e) {
// Ignore parsing errors
}
throw new Error(errorDetail);
}
if (streamingEnabled) {
await this.handleStreamingResponse(response, tabId, apiMessages, model);
} else {
const result = await response.json();
if (result.message.tool_calls) {
await this.handleToolCalls(result.message.tool_calls, apiMessages, model, tabId);
} else {
this.sendToSidePanel({ type: 'FINAL_RESPONSE', data: { success: true, message: result.message.content } });
}
}
return { success: true };
} catch (error) {
if (error.name === 'AbortError') {
// Don't send FINAL_RESPONSE for user-initiated stops - sidepanel handles this
console.log('🛑 Generation aborted by user');
return { success: false, error: 'Generation stopped by user' };
}
console.error('Failed to send message:', error);
this.sendToSidePanel({ type: 'FINAL_RESPONSE', data: { success: false, error: error.message } });
return { success: false, error: error.message };
} finally {
// Clean up the specific request
this.activeRequests.delete(requestId);
}
}
stopGeneration() {
// Abort all active requests
let abortedCount = 0;
for (const [requestId, abortController] of this.activeRequests) {
abortController.abort();
abortedCount++;
}
this.activeRequests.clear();
if (abortedCount > 0) {
return { success: true, message: `Stopped ${abortedCount} active generation(s)` };
} else {
return { success: false, message: 'No active generation to stop' };
}
}
async handleStreamingResponse(response, tabId, originalMessages, model) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let fullResponse = '';
let buffer = '';
let toolCalls = [];
// Performance tracking
const startTime = Date.now();
let tokenCount = 0;
let firstTokenTime = null;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (!line.trim()) continue;
try {
const data = JSON.parse(line);
// Get the new content chunk
const newContent = data.message?.content || '';
if (newContent) {
fullResponse += newContent;
tokenCount += newContent.split(/\s+/).length; // Rough token estimation
if (!firstTokenTime) {
firstTokenTime = Date.now();
}
}
if (data.message?.tool_calls) {
toolCalls = toolCalls.concat(data.message.tool_calls);
}
// Send ONLY the new content chunk for natural streaming
const streamData = {
content: newContent, // Send raw content (including <think> tags)
done: data.done
};
// Only send if there's actual new content or it's done
if (newContent || data.done) {
this.sendToSidePanel({
type: 'STREAMING_RESPONSE',
data: streamData
});
}
if (data.done) {
// Calculate performance stats
const endTime = Date.now();
const totalTime = endTime - startTime;
const timeToFirstToken = firstTokenTime ? firstTokenTime - startTime : totalTime;
const tokensPerSecond = tokenCount > 0 ? (tokenCount / (totalTime / 1000)) : 0;
// Send performance stats if enabled
if (this.settings.showPerformanceStats && tokenCount > 0) {
this.sendToSidePanel({
type: 'PERFORMANCE_STATS',
data: {
totalTime,
timeToFirstToken,
tokenCount,
tokensPerSecond: tokensPerSecond.toFixed(2),
model
}
});
}
if (toolCalls.length > 0) {
// Signal that tool calls were detected so streaming pauses instead of finishing
this.sendToSidePanel({
type: 'STREAMING_CHUNK',
data: {
done: true,
toolCallsDetected: true
}
});
await this.handleToolCalls(toolCalls, originalMessages, model, tabId);
}
return;
}
} catch (e) {
console.warn('Failed to parse streaming chunk:', line, e);
}
}
}
} catch (error) {
if (error.name === 'AbortError' || error.message?.includes('aborted')) {
console.log('🛑 Stream aborted by user');
// Don't send FINAL_RESPONSE for user-initiated stops - sidepanel handles this
} else {
console.error('Streaming error:', error);
this.sendToSidePanel({ type: 'FINAL_RESPONSE', data: { success: false, error: error.message || 'Streaming error occurred' } });
}
} finally {
// Clean up reader
try {
reader.releaseLock();
} catch (e) {
// Reader may already be released
}
}
}
async handleToolCalls(toolCalls, originalMessages, model, tabId) {
this.sendToSidePanel({ type: 'SYSTEM_MESSAGE', data: `🛠️ Using ${toolCalls.length} tool(s)...` });
let toolMessages = [...originalMessages, { role: 'assistant', content: null, tool_calls: toolCalls }];
for (const toolCall of toolCalls) {
const toolResult = await this.executeToolCall(toolCall, tabId);
toolMessages.push({ role: 'tool', tool_call_id: toolCall.id, content: JSON.stringify(toolResult.result || { error: toolResult.error }) });
}
const finalRequestBody = { model, messages: toolMessages, stream: true };
const finalResponse = await fetch(`${this.baseURL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(finalRequestBody)
});
if (!finalResponse.ok) throw new Error(`HTTP ${finalResponse.status}: ${finalResponse.statusText}`);
await this.handleStreamingResponse(finalResponse, tabId, toolMessages, model);
}
getBuiltInTools() {
return [
{
type: 'function',
function: {
name: 'web_search',
description: 'Accesses a web search engine to retrieve up-to-date information and relevant web pages. Use this tool when you need current facts, external data, or to verify information that is not part of your internal knowledge. Also use this tool when the user explicitly asks to "search for", "look up", or "find information about" a topic. The results will include titles, snippets, and URLs of relevant search results.',
parameters: {
type: 'object',
properties: {
query: {
type: 'string',
description: 'The precise and concise search query to be executed. Formulate this as a set of keywords or a short phrase that accurately reflects the information you are seeking. Avoid conversational language.'
}
},
required: ['query']
}
}
},
{
type: 'function',
function: {
name: 'scrape',
description: 'Scrapes a webpage for its content.',
parameters: {
type: 'object',
properties: {
url: {
type: 'string',
description: 'The URL of the webpage to scrape.'
}
},
required: ['url']
}
}
},
{
type: 'function',
function: {
name: 'get_page_context',
description: 'Get the context and content of the current webpage',
parameters: {
type: 'object',
properties: {}
}
}
}
];
}
async executeToolCall(toolCall, tabId) {
const { name, arguments: args } = toolCall.function;
try {
switch (name) {
case 'web_search': return await this.performWebSearch(args.query);
case 'scrape': return await this.scrapeWebpage(args.url);
case 'get_page_context': return await this.extractPageContext(tabId);
default: return { success: false, error: `Unknown tool: ${name}` };
}
} catch (error) {
return { success: false, error: error.message };
}
}
async scrapeWebpage(url) {
try {
const apiKey = this.settings.serperApiKey;
if (!apiKey) {
throw new Error('Serper API key not configured. Please add your API key in settings.');
}
const myHeaders = new Headers();
myHeaders.append("X-API-KEY", apiKey);
myHeaders.append("Content-Type", "application/json");
const raw = JSON.stringify({ "url": url });
const requestOptions = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow"
};
const response = await fetch("https://scrape.serper.dev/", requestOptions);
if (!response.ok) {
throw new Error(`Scraping service error: ${response.status} ${response.statusText}`);
}
const text = await response.text();
return { success: true, result: text };
} catch (error) {
console.error('Scraping failed:', error);
return { success: false, error: error.message };
}
}
async performWebSearch(query) {
await this.loadSettings(); // Ensure settings are up-to-date
const searchEngine = this.settings.searchEngine;
switch (searchEngine) {
case 'serper':
return this._performSerperSearch(query);
case 'duckduckgo':
return this._performDuckDuckGoSearch(query);
default:
return { success: false, error: `Unsupported search engine: ${searchEngine}` };
}
}
async _performSerperSearch(query) {
try {
const apiKey = this.settings.serperApiKey;
if (!apiKey) {
throw new Error('Serper API key not configured. Please add your API key in settings.');
}
// Validate and sanitize search query
const sanitizedQuery = this.sanitizeSearchQuery(query);
if (!sanitizedQuery) {
throw new Error('Invalid search query provided.');
}
const response = await fetch('https://google.serper.dev/search', { method: 'POST', headers: { 'X-API-KEY': apiKey, 'Content-Type': 'application/json' }, body: JSON.stringify({ q: sanitizedQuery }) });
if (!response.ok) throw new Error(`Serper API error: ${response.status}`);
const data = await response.json();
return { success: true, result: data.organic || [] };
} catch (error) {
return { success: false, error: error.message };
}
}
async _performDuckDuckGoSearch(query) {
try {
const sanitizedQuery = encodeURIComponent(this.sanitizeSearchQuery(query));
if (!sanitizedQuery) {
throw new Error('Invalid search query provided.');
}
const response = await fetch(`https://api.duckduckgo.com/?q=${sanitizedQuery}&format=json&pretty=1`);
if (!response.ok) throw new Error(`DuckDuckGo API error: ${response.status}`);
const data = await response.json();
const results = [];
if (data.AbstractText) {
results.push({
title: data.Heading || 'Summary',
snippet: data.AbstractText,
link: data.AbstractURL || `https://duckduckgo.com/?q=${sanitizedQuery}`
});
}
if (data.Results && Array.isArray(data.Results)) {
data.Results.forEach(res => {
results.push({
title: res.Text,
snippet: res.Text, // DuckDuckGo API often repeats text as snippet
link: res.FirstURL
});
});
}
// Limit results to maxSearchResults setting
const maxResults = this.settings.maxSearchResults || 5;
return { success: true, result: results.slice(0, maxResults) };
} catch (error) {
return { success: false, error: error.message };
}
}
sanitizeSearchQuery(query) {
if (!query || typeof query !== 'string') {
return null;
}
// Trim and limit length
const trimmed = query.trim();
if (trimmed.length === 0 || trimmed.length > 500) {
return null;
}
// Remove potentially dangerous characters while keeping useful ones
const sanitized = trimmed
.replace(/[<>'"&\x00-\x1F\x7F]/g, '') // Remove HTML/control chars
.replace(/\s+/g, ' ') // Normalize whitespace
.trim();
return sanitized.length > 0 ? sanitized : null;
}
async extractPageContext(tabId) {
try {
if (!tabId) throw new Error('Tab ID not available');
// Use content script to extract page context properly
const [result] = await chrome.scripting.executeScript({
target: { tabId },
func: () => {
// Try to get cached context first
const cached = sessionStorage.getItem('sideLlamaPageContext');
if (cached) {
return JSON.parse(cached);
}
// Extract page content if not cached
return {
title: document.title || 'Untitled Page',
url: window.location.href,
content: document.body.innerText.substring(0, 8000),
timestamp: Date.now()
};
}
});
if (!result?.result) {
throw new Error('No content extracted from page');
}
return { success: true, context: result.result };
} catch (error) {
console.error('Failed to extract page context:', error);
return { success: false, error: error.message };
}
}
async takeScreenshot(tabId = null) {
try {
console.log('🖼️ Starting screenshot capture...', { tabId });
// CRITICAL FIX: For sidepanel screenshots, we need to use a different approach
// because activeTab permission is not granted from sidepanel button clicks
let targetTab = null;
if (tabId) {
targetTab = await chrome.tabs.get(tabId);
} else {
// Get the current active tab if no tabId provided
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs.length === 0) {
throw new Error('No active tab found');
}
targetTab = tabs[0];
}
console.log('📋 Capturing screenshot for tab:', targetTab.id, 'in window:', targetTab.windowId);
// Try to capture screenshot - this works for context menu (has activeTab)
// but may fail for sidepanel button (no activeTab)
try {
const dataUrl = await chrome.tabs.captureVisibleTab(targetTab.windowId, {
format: 'png',
quality: this.settings.screenshotQuality || 90
});
if (dataUrl) {
console.log('✅ Screenshot captured successfully, size:', dataUrl.length);
return { success: true, result: { dataUrl } };
}
} catch (permissionError) {
console.log('⚠️ Direct screenshot failed, trying alternative approach:', permissionError.message);
// FALLBACK: Use content script to request user interaction
// This will prompt the user to grant permission
if (permissionError.message.includes('activeTab') || permissionError.message.includes('permission')) {
return {
success: false,
error: 'Screenshot requires permission. Please use the right-click context menu "Take Screenshot" option, or ensure you have tabs permission.',
needsPermission: true
};
}
throw permissionError;
}
throw new Error('Failed to capture screenshot - no data returned');
} catch (error) {
console.error('❌ Screenshot error:', error);