(userInput = "", onStream = (text = "") => {})
| 32 | } |
| 33 | |
| 34 | async sendMessage(userInput = "", onStream = (text = "") => {}) { |
| 35 | try { |
| 36 | const openai = new OpenAI({ |
| 37 | baseURL: "https://api.deepseek.com", |
| 38 | apiKey: this.apiKey |
| 39 | }); |
| 40 | |
| 41 | this.messages.push({ role: "user", content: userInput }); |
| 42 | |
| 43 | const completion = await openai.chat.completions.create({ |
| 44 | messages: /** @type {any} */ (this.messages), |
| 45 | model: "deepseek-chat", |
| 46 | stream: true |
| 47 | }); |
| 48 | |
| 49 | let fullResponse = ""; |
| 50 | |
| 51 | for await (const chunk of completion) { |
| 52 | const content = chunk.choices[0]?.delta?.content || ""; |
| 53 | if (content) { |
| 54 | fullResponse += content; |
| 55 | onStream(content); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Add assistant reply to conversation history |
| 60 | this.messages.push({ role: "assistant", content: fullResponse }); |
| 61 | |
| 62 | return { |
| 63 | content: fullResponse |
| 64 | }; |
| 65 | } catch (error) { |
| 66 | console.error("Request error: ", error.message); |
| 67 | throw error; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Clear conversation history (keep system prompt) |
| 72 | clearHistory() { |
no test coverage detected