MCPcopy Create free account
hub / github.com/Roy3838/Observer / sendMessage

Function sendMessage

app/src/components/BenchmarkPanel.tsx:176–235  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

174
175 // Send the whole conversation to the active backend; the reply streams into the last message
176 const sendMessage = async () => {
177 const text = input.trim();
178 if (!text || isGenerating || !backendInfo.backend) return;
179
180 const userMsg: ChatMessage = { role: 'user', text, image: capturedImage ?? undefined };
181 const history = [...messages.filter(m => !m.error), userMsg];
182 const payload = history.map(m => ({
183 role: m.role,
184 content: m.image
185 ? [{ type: 'image' as const, image: m.image }, { type: 'text' as const, text: m.text }]
186 : m.text,
187 }));
188
189 setMessages([...messages, userMsg, { role: 'assistant', text: '' }]);
190 setInput('');
191 setCapturedImage(null);
192 setIsGenerating(true);
193 abortControllerRef.current = new AbortController();
194
195 const patchReply = (patch: (m: ChatMessage) => ChatMessage) =>
196 setMessages(prev => prev.map((m, i) => (i === prev.length - 1 ? patch(m) : m)));
197 const onToken = (token: string) => patchReply(m => ({ ...m, text: m.text + token }));
198
199 try {
200 if (backendInfo.backend === 'llamacpp') {
201 await NativeLlmManager.getInstance().generate(payload, onToken);
202 const info = await NativeLlmManager.getInstance().getDebugInfo();
203 patchReply(m => ({ ...m, metrics: info.engine.lastMetrics }));
204 } else if (backendInfo.backend === 'transformers') {
205 const startTime = performance.now();
206 let tokensGenerated = 0;
207 let firstTokenTime = 0;
208 await GemmaModelManager.getInstance().generate(payload, (token) => {
209 if (tokensGenerated === 0) firstTokenTime = performance.now() - startTime;
210 tokensGenerated++;
211 onToken(token);
212 });
213 const totalTime = performance.now() - startTime;
214 patchReply(m => ({
215 ...m,
216 metrics: {
217 tokensGenerated,
218 promptTokens: 0, // Not available from Transformers.js
219 timeToFirstTokenMs: firstTokenTime,
220 totalGenerationTimeMs: totalTime,
221 tokensPerSecond: tokensGenerated / (totalTime / 1000),
222 },
223 }));
224 }
225 // TODO: Add Ollama support
226 } catch (e) {
227 const errorMessage = e instanceof Error ? e.message : String(e);
228 if (errorMessage !== 'Aborted') {
229 patchReply(m => ({ ...m, text: `Error: ${errorMessage}`, error: true }));
230 }
231 } finally {
232 setIsGenerating(false);
233 abortControllerRef.current = null;

Callers 1

BenchmarkPanelFunction · 0.85

Calls 6

patchReplyFunction · 0.85
onTokenFunction · 0.85
filterMethod · 0.80
getDebugInfoMethod · 0.80
generateMethod · 0.65
getInstanceMethod · 0.45

Tested by

no test coverage detected