-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
163 lines (138 loc) · 4.75 KB
/
background.js
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
// background.js
import DocumentProcessor from './document-processor.js';
import DeductionFinder from './deduction-finder.js';
import TaxAssistant from './tax-assistant.js';
const documentProcessor = new DocumentProcessor();
const deductionFinder = new DeductionFinder();
const taxAssistant = new TaxAssistant('your-api-key-here');
let userContext = {
taxYear: new Date().getFullYear() - 1,
filingStatus: null,
income: null,
documents: [],
deductions: [],
taxProfile: {}
};
/////////////////////////////////////////////////
// Modern ES modules approach for browser
let model = null;
// Initialize the Gemini AI model
async function initGeminiAI() {
try {
const { GoogleGenerativeAI } = await import('https://cdn.jsdelivr.net/npm/@google/[email protected]/dist/browser/index.js');
// there are issues with the import, so I have to use the CDN link.
const apiKey = process.env.GEMINI_API_KEY;
const genAI = new GoogleGenerativeAI(apiKey);
model = genAI.getGenerativeModel({
model: "gemini-2.0-flash",
});
console.log("Gemini AI initialized successfully");
} catch (error) {
console.error("Failed to initialize Gemini AI:", error);
}
}
initGeminiAI();
////////////////////////////
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
console.log('Received message:', message);
switch (message.action) {
case 'processDocument':
handleDocumentProcessing(message.document)
.then(result => sendResponse(result))
.catch(error => sendResponse({success: false, error: error.message}));
break;
case 'findDeductions':
const deductions = deductionFinder.analyzeDocuments(userContext.documents);
userContext.deductions = deductions;
sendResponse({success: true, deductions: deductions});
break;
case 'askAssistant':
if (!model) {
await initGeminiAI();
if (!model) {
sendResponse({ answer: "Sorry, I couldn't initialize the AI assistant. Please try again later." });
break;
}
}
try {
const chatSession = model.startChat({
generationConfig: {
temperature: 1,
topP: 0.95,
topK: 40,
maxOutputTokens: 8192,
},
history: [],
});
const result = await chatSession.sendMessage(`Please return an appropriate response to the query - ${message.question}, based on the Indian financial context`);
sendResponse({ answer: result.response.text() });
} catch (error) {
console.error("Error with AI assistant:", error);
sendResponse({ answer: "Sorry, I encountered an error processing your request." });
}
break;
case 'updateUserContext':
userContext = {...userContext, ...message.data};
sendResponse({success: true, context: userContext});
break;
case 'getDocuments':
sendResponse({success: true, documents: userContext.documents});
break;
case 'getDeductions':
sendResponse({success: true, deductions: userContext.deductions});
break;
case 'fillForm':
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, {
action: 'fillForm',
data: message.data,
formType: message.formType
}, sendResponse);
} else {
sendResponse({success: false, error: 'No active tab found'});
}
});
break;
case 'openAssistant':
chrome.action.openPopup();
sendResponse({success: true});
break;
default:
sendResponse({success: false, error: 'Unknown action'});
}
return true;
});
// Handle document processing
async function handleDocumentProcessing(document) {
try {
const processedDoc = await documentProcessor.processDocument(document);
userContext.documents.push(processedDoc);
// After processing, automatically analyze for deductions
const newDeductions = deductionFinder.analyzeDocuments([processedDoc]);
userContext.deductions = [...userContext.deductions, ...newDeductions];
return {
success: true,
document: processedDoc,
deductions: newDeductions
};
} catch (error) {
console.error('Error processing document:', error);
return {success: false, error: error.message};
}
}
chrome.runtime.onInstalled.addListener(details => {
if (details.reason === 'install') {
chrome.storage.local.set({
onboardingComplete: false,
userPreferences: {
autoFill: true,
saveDocuments: true,
notificationEnabled: true
}
});
chrome.tabs.create({
url: 'onboarding.html'
});
}
});