Newer
Older
navi-1 / webclient / src / api / index.js
const BASE = import.meta.env.DEV ? '/api' : ''

async function request(method, path, body) {
  const opts = {
    method,
    headers: {}
  }
  if (body !== undefined) {
    opts.headers['Content-Type'] = 'application/json'
    opts.body = JSON.stringify(body)
  }
  const res = await fetch(`${BASE}${path}`, opts)
  if (!res.ok) {
    const text = await res.text().catch(() => '')
    throw new Error(`${method} ${path} → ${res.status}: ${text}`)
  }
  if (res.status === 204) return null
  return res.json()
}

// ─── Profiles ──────────────────────────────────────────────────────────────
export function getProfiles() {
  return request('GET', '/agents/profiles')
}

// ─── Sessions ──────────────────────────────────────────────────────────────
export function getSessions() {
  return request('GET', '/sessions')
}

export function getSession(id) {
  return request('GET', `/sessions/${id}`)
}

export function createSession(profileId) {
  return request('POST', '/sessions', { profile_id: profileId })
}

export function deleteSession(id) {
  return request('DELETE', `/sessions/${id}`)
}

export function pinSession(id, pinned) {
  return request('PATCH', `/sessions/${id}/pin`, { pinned })
}

export function stopSession(id) {
  return request('POST', `/sessions/${id}/stop`)
}

// ─── Files ─────────────────────────────────────────────────────────────────
export async function uploadFile(sessionId, file) {
  const form = new FormData()
  form.append('file', file)
  const res = await fetch(`${BASE}/sessions/${sessionId}/files`, {
    method: 'POST',
    body: form
  })
  if (!res.ok) {
    const text = await res.text().catch(() => '')
    throw new Error(`Upload failed: ${res.status}: ${text}`)
  }
  return res.json()
}