export async function sendAIRequest(userMessage: string, mode: string, apiKey: string, sessionId?: string): Promise<{ result: string; sessionId: string }> {
  const resp = await fetch('/api/ai', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ userMessage, apiKey, mode, ...(sessionId ? { sessionId } : {}) }),
  })
  if (!resp.ok) {
    const err = await resp.json().catch(() => ({ error: 'request failed' }))
    throw new Error(err.error || 'request failed')
  }
  return resp.json()
}

export async function listSessions(): Promise<any[]> {
  const resp = await fetch('/api/ai/sessions')
  if (!resp.ok) return []
  return resp.json()
}
