incognitolm commited on
Commit
d4758d3
·
verified ·
1 Parent(s): 3adb92a

Update server/wsHandler.js

Browse files
Files changed (1) hide show
  1. server/wsHandler.js +15 -12
server/wsHandler.js CHANGED
@@ -1,3 +1,4 @@
 
1
  import { safeSend, broadcastToUser } from './helpers.js';
2
  import { LIGHTNING_BASE, PUBLIC_URL } from './config.js';
3
  import { sessionStore, deviceSessionStore } from './sessionStore.js';
@@ -246,18 +247,20 @@ async function autoName(prompt, accessToken) {
246
  try {
247
  const text = typeof prompt === 'string' ? prompt
248
  : (Array.isArray(prompt) ? (prompt.find(p => p.type === 'text')?.text || '') : String(prompt));
249
- const h = { 'Content-Type': 'application/json' };
250
- if (accessToken) h.Authorization = `Bearer ${accessToken}`;
251
- const r = await fetch(`${LIGHTNING_BASE}/gen/chat/completions`, {
252
- method: 'POST', headers: h,
253
- body: JSON.stringify({ model: 'lightning',
254
- messages: [
255
- { role: 'system', content: 'Generate a 2-4 word title of the conversation. No quotes, just a summariaing title' },
256
- { role: 'user', content: text.slice(0, 200) }],
257
- max_tokens: 20, stream: false }),
258
  });
259
- if (!r.ok) return text.slice(0, 40) || 'New Chat';
260
- const d = await r.json();
261
- return d.choices?.[0]?.message?.content?.trim() || 'New Chat';
 
 
 
 
 
 
 
262
  } catch { return 'New Chat'; }
263
  }
 
1
+ import OpenAI from 'openai';
2
  import { safeSend, broadcastToUser } from './helpers.js';
3
  import { LIGHTNING_BASE, PUBLIC_URL } from './config.js';
4
  import { sessionStore, deviceSessionStore } from './sessionStore.js';
 
247
  try {
248
  const text = typeof prompt === 'string' ? prompt
249
  : (Array.isArray(prompt) ? (prompt.find(p => p.type === 'text')?.text || '') : String(prompt));
250
+ const client = new OpenAI({
251
+ apiKey: accessToken || 'no-key',
252
+ baseURL: `${LIGHTNING_BASE}/gen`,
253
+ defaultHeaders: accessToken ? { Authorization: `Bearer ${accessToken}` } : {},
 
 
 
 
 
254
  });
255
+ const res = await client.chat.completions.create({
256
+ model: 'lightning',
257
+ messages: [
258
+ { role: 'system', content: 'Generate a 2-4 word title of the conversation. No quotes, just a summarising title.' },
259
+ { role: 'user', content: text.slice(0, 200) },
260
+ ],
261
+ max_tokens: 20,
262
+ stream: false,
263
+ });
264
+ return res.choices?.[0]?.message?.content?.trim() || 'New Chat';
265
  } catch { return 'New Chat'; }
266
  }