|
| 1 | +import { GROK_API_KEY, GROK_MODEL, GROK_TEMPERATURE } from '$env/static/private' |
| 2 | +import { fetchRelevantHistory } from './history' |
| 3 | +import { logger } from './logger' |
| 4 | +import { openAiPrompt, systemContext } from './prompts' |
| 5 | +import type { Message, ToneType } from './types' |
| 6 | +import { formatMessagesAsText } from './utils' |
| 7 | + |
| 8 | +const API_URL = 'https://grok.x.ai/api/chat/completions' |
| 9 | +const DEFAULT_MODEL = 'grok-1' |
| 10 | +const DEFAULT_TEMPERATURE = 0.5 |
| 11 | + |
| 12 | +const getConfig = () => ({ |
| 13 | + model: GROK_MODEL || DEFAULT_MODEL, |
| 14 | + temperature: Number(GROK_TEMPERATURE || DEFAULT_TEMPERATURE), |
| 15 | + apiKey: GROK_API_KEY, |
| 16 | +}) |
| 17 | + |
| 18 | +export const getGrokReply = async ( |
| 19 | + messages: Message[], |
| 20 | + tone: ToneType, |
| 21 | + context: string |
| 22 | +): Promise<{ summary: string; replies: string[] }> => { |
| 23 | + const config = getConfig() |
| 24 | + if (!config.apiKey) { |
| 25 | + return { |
| 26 | + summary: 'Grok API key is not configured.', |
| 27 | + replies: ['Please set up your Grok API key in the .env file.'], |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + const historyContext = await fetchRelevantHistory(messages) |
| 32 | + const prompt = openAiPrompt(tone, historyContext) + '\n\n' + context |
| 33 | + |
| 34 | + const body = { |
| 35 | + model: config.model, |
| 36 | + messages: [ |
| 37 | + { role: 'system', content: systemContext }, |
| 38 | + { role: 'user', content: formatMessagesAsText(messages) }, |
| 39 | + { role: 'user', content: prompt }, |
| 40 | + ], |
| 41 | + temperature: config.temperature, |
| 42 | + tools: [ |
| 43 | + { |
| 44 | + type: 'function', |
| 45 | + function: { |
| 46 | + name: 'draft_replies', |
| 47 | + description: 'Generate a short summary and three suggested replies', |
| 48 | + parameters: { |
| 49 | + type: 'object', |
| 50 | + properties: { |
| 51 | + summary: { type: 'string' }, |
| 52 | + replies: { type: 'array', items: { type: 'string' } }, |
| 53 | + }, |
| 54 | + required: ['summary', 'replies'], |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + ], |
| 59 | + tool_choice: { type: 'function', function: { name: 'draft_replies' } }, |
| 60 | + } |
| 61 | + |
| 62 | + logger.debug({ body }, 'Sending request to Grok') |
| 63 | + logger.info('Sending request to Grok') |
| 64 | + |
| 65 | + try { |
| 66 | + const response = await fetch(API_URL, { |
| 67 | + method: 'POST', |
| 68 | + headers: { |
| 69 | + 'Content-Type': 'application/json', |
| 70 | + Authorization: `Bearer ${config.apiKey}`, |
| 71 | + }, |
| 72 | + body: JSON.stringify(body), |
| 73 | + }) |
| 74 | + |
| 75 | + if (!response.ok) { |
| 76 | + logger.error({ status: response.status }, 'Grok API error') |
| 77 | + throw new Error(`Grok API error code ${response.status}: ${response.statusText}`) |
| 78 | + } |
| 79 | + |
| 80 | + const { choices } = await response.json() |
| 81 | + const functionCall = choices[0]?.message?.tool_calls?.[0]?.function |
| 82 | + if (!functionCall) throw new Error('No function call in response') |
| 83 | + const { summary, replies } = JSON.parse(functionCall.arguments) |
| 84 | + |
| 85 | + if (!summary || !Array.isArray(replies)) { |
| 86 | + logger.error({ summary, replies }, 'Invalid response format from Grok') |
| 87 | + throw new Error('Invalid response format from Grok') |
| 88 | + } |
| 89 | + |
| 90 | + return { summary, replies } |
| 91 | + } catch (error) { |
| 92 | + logger.error({ error }, 'Failed to get Grok reply') |
| 93 | + return { |
| 94 | + summary: '', |
| 95 | + replies: ['(AI API error. Check your key and usage.)'], |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments