-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-worker.js
More file actions
232 lines (195 loc) · 6.07 KB
/
text-worker.js
File metadata and controls
232 lines (195 loc) · 6.07 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
// text-worker.js - Web Worker for text-based code explanation using lightweight LLM
// This runs in a separate thread for fast text processing
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.1';
// Configure Transformers.js environment
env.allowLocalModels = false;
env.allowRemoteModels = true;
// Global model instance
let textModelPipeline = null;
let modelLoading = false;
let modelLoaded = false;
// Model configuration for text-based code explanation
const TEXT_MODEL_CONFIG = {
primary: {
name: 'Xenova/codet5-small',
task: 'text2text-generation',
options: {
device: 'auto', // Prioritize WebGPU
}
},
fallback: {
name: 'Xenova/t5-small',
task: 'text2text-generation',
options: {
device: 'auto',
}
}
};
/**
* Initialize the text model pipeline
* @param {string} modelChoice - 'primary' or 'fallback'
* @returns {Promise<void>}
*/
async function initializeTextModel(modelChoice = 'primary') {
if (modelLoaded) {
return;
}
if (modelLoading) {
while (modelLoading) {
await new Promise(resolve => setTimeout(resolve, 100));
}
return;
}
modelLoading = true;
try {
const config = TEXT_MODEL_CONFIG[modelChoice];
console.log(`[Text Worker] Loading ${config.name}...`);
self.postMessage({
type: 'progress',
status: 'downloading',
message: `Loading text model...`,
progress: 0
});
textModelPipeline = await pipeline(
config.task,
config.name,
config.options
);
modelLoaded = true;
console.log(`[Text Worker] Model ${config.name} loaded successfully`);
self.postMessage({
type: 'progress',
status: 'ready',
message: 'Text model ready',
progress: 100
});
} catch (error) {
console.error('[Text Worker] Error loading model:', error);
if (modelChoice === 'primary') {
console.log('[Text Worker] Trying fallback model...');
self.postMessage({
type: 'progress',
status: 'downloading',
message: 'Primary text model failed, trying alternative...',
progress: 0
});
modelLoading = false;
return await initializeTextModel('fallback');
}
self.postMessage({
type: 'error',
error: `Failed to load text model: ${error.message}`
});
throw error;
} finally {
modelLoading = false;
}
}
/**
* Extract and explain code from text
* @param {string} codeText - The extracted code text
* @param {string} language - Detected programming language
* @returns {Promise<string>}
*/
async function explainCodeText(codeText, language = '') {
try {
if (!modelLoaded) {
await initializeTextModel();
}
console.log('[Text Worker] Explaining code...');
self.postMessage({
type: 'progress',
status: 'processing',
message: 'Analyzing code...',
progress: 50
});
// Create a focused prompt for code explanation
const prompt = `Explain this ${language ? language + ' ' : ''}code in simple terms: ${codeText}`;
const result = await textModelPipeline(prompt, {
max_new_tokens: 150,
temperature: 0.1,
do_sample: false,
});
console.log('[Text Worker] Explanation generated');
let explanation = '';
if (Array.isArray(result)) {
explanation = result[0]?.generated_text || result[0]?.text || '';
} else if (result.generated_text) {
explanation = result.generated_text;
} else if (result.text) {
explanation = result.text;
} else {
explanation = String(result);
}
explanation = explanation.trim();
if (!explanation) {
explanation = 'Unable to generate explanation for this code snippet.';
}
return explanation;
} catch (error) {
console.error('[Text Worker] Error explaining code:', error);
throw error;
}
}
/**
* Detect programming language from code text
* @param {string} codeText - The code text
* @returns {string} Detected language
*/
function detectLanguage(codeText) {
const languagePatterns = {
javascript: /\b(function|const|let|var|if|else|for|while|class|import|export)\b/,
python: /\b(def|class|import|from|if|elif|else|for|while|try|except|with)\b/,
java: /\b(public|private|class|void|int|String|if|else|for|while|import)\b/,
cpp: /\b(#include|int|void|class|if|else|for|while|std::)\b/,
csharp: /\b(using|namespace|class|void|int|string|if|else|for|while|public)\b/,
php: /\b(<\?php|function|if|else|foreach|echo|class)\b/,
ruby: /\b(def|class|if|else|end|puts|require)\b/,
go: /\b(package|func|import|if|else|for|fmt\.)\b/,
rust: /\b(fn|let|mut|if|else|for|while|use|mod|pub)\b/,
html: /<\/?[a-zA-Z][^>]*>/,
css: /\{[^}]*\}/,
sql: /\b(SELECT|FROM|WHERE|INSERT|UPDATE|DELETE|CREATE|TABLE)\b/i,
};
for (const [lang, pattern] of Object.entries(languagePatterns)) {
if (pattern.test(codeText)) {
return lang;
}
}
return 'unknown';
}
// Message handler
self.addEventListener('message', async (event) => {
const { type, data } = event.data;
try {
switch (type) {
case 'initialize':
await initializeTextModel(data?.modelChoice);
self.postMessage({ type: 'initialized', status: { loaded: modelLoaded, loading: modelLoading } });
break;
case 'explain':
const language = detectLanguage(data.codeText);
const explanation = await explainCodeText(data.codeText, language);
self.postMessage({
type: 'result',
explanation: explanation,
language: language,
codeText: data.codeText
});
break;
case 'status':
self.postMessage({ type: 'status', status: { loaded: modelLoaded, loading: modelLoading } });
break;
default:
self.postMessage({ type: 'error', error: `Unknown message type: ${type}` });
}
} catch (error) {
self.postMessage({
type: 'error',
error: error.message || 'Unknown error occurred'
});
}
});
// Initialize on worker start
console.log('[Text Worker] Worker started, ready to initialize text model');
self.postMessage({ type: 'ready' });