|
| 1 | +import { KeysService } from '../services/keys.service.js'; |
| 2 | +import {getUserIdentifier} from '../utils/common.js'; |
| 3 | +export class KeysController { |
| 4 | + constructor() { |
| 5 | + this.keysService = new KeysService(); |
| 6 | + } |
| 7 | + |
| 8 | + async saveKey(req, res) { |
| 9 | + try { |
| 10 | + const sid = req.cookies['overleaf.sid']; |
| 11 | + const userIdentifier = await getUserIdentifier(sid); |
| 12 | + const { name, baseUrl, apiKey } = req.body; |
| 13 | + await this.keysService.saveApiKey(userIdentifier, name, baseUrl, apiKey); |
| 14 | + res.status(200).json({ success: true }); |
| 15 | + } catch (error) { |
| 16 | + res.status(400).json({ success: false, data: error.message }); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + async deleteKey(req, res) { |
| 21 | + try { |
| 22 | + const sid = req.cookies['overleaf.sid']; |
| 23 | + const userIdentifier = await getUserIdentifier(sid); |
| 24 | + const { name } = req.body; |
| 25 | + const result = await this.keysService.deleteApiKey(userIdentifier, name); |
| 26 | + res.status(200).json({ success: true, data: result }); |
| 27 | + } catch (error) { |
| 28 | + console.log(error.message) |
| 29 | + res.status(400).json({ success: false, data: error.message }); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + async getLlmInfo(req, res) { |
| 34 | + |
| 35 | + try { |
| 36 | + const sid = req.cookies['overleaf.sid']; |
| 37 | + const userIdentifier = await getUserIdentifier(sid); |
| 38 | + const result = await this.keysService.getLlmInfo(userIdentifier); |
| 39 | + res.status(200).json({ success: true, data: result }); |
| 40 | + } catch (error) { |
| 41 | + res.status(400).json({ success: false, data: error.message }); |
| 42 | + } |
| 43 | + } |
| 44 | + async getUsingLlm(req, res) { |
| 45 | + try { |
| 46 | + const sid = req.cookies['overleaf.sid']; |
| 47 | + const userIdentifier = await getUserIdentifier(sid); |
| 48 | + const result = await this.keysService.getUsingLlm(userIdentifier); |
| 49 | + res.status(200).json({ success: true, data: result }); |
| 50 | + } catch (error) { |
| 51 | + res.status(400).json({ success: false, data: error.message }); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + async updateUsingLlm(req, res) { |
| 57 | + try { |
| 58 | + const sid = req.cookies['overleaf.sid']; |
| 59 | + const userIdentifier = await getUserIdentifier(sid); |
| 60 | + const { usingLlm } = req.body; |
| 61 | + await this.keysService.updateUsingLlm(userIdentifier, usingLlm); |
| 62 | + res.status(200).json({ success: true }); |
| 63 | + } catch (error) { |
| 64 | + res.status(400).json({ success: false, data: error.message }); |
| 65 | + } |
| 66 | + } |
| 67 | + async updateUsingModel(req, res) { |
| 68 | + try { |
| 69 | + const sid = req.cookies['overleaf.sid']; |
| 70 | + const userIdentifier = await getUserIdentifier(sid); |
| 71 | + const { name, chatOrCompletion, newModel } = req.body; |
| 72 | + await this.keysService.updateUsingModel(userIdentifier, name, chatOrCompletion, newModel); |
| 73 | + res.status(200).json({ success: true }); |
| 74 | + } catch (error) { |
| 75 | + res.status(400).json({ success: false, data: error.message }); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | + |
0 commit comments