File size: 1,614 Bytes
4dcc016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const RAW_API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://127.0.0.1:8000'
const API_BASE = RAW_API_BASE.replace(/\/+$/, '')
const WS_BASE = API_BASE.replace(/^http/, 'ws')

async function request(path, options = {}) {
  const response = await fetch(`${API_BASE}${path}`, {
    headers: {
      'Content-Type': 'application/json',
      ...(options.headers || {}),
    },
    ...options,
  })

  if (!response.ok) {
    let message = `Request failed with status ${response.status}`
    try {
      const payload = await response.json()
      message = payload.detail || message
    } catch {
      // Keep default error message when the payload is not JSON.
    }
    throw new Error(message)
  }

  return response.json()
}

export function createOrAttachSession(jobId, { restart = false } = {}) {
  return request('/terminal/sessions', {
    method: 'POST',
    body: JSON.stringify({ job_id: jobId, restart }),
  })
}

export function stopTerminalSession(sessionId) {
  return request(`/terminal/sessions/${sessionId}/stop`, {
    method: 'POST',
  })
}

export function sendTerminalInput(sessionId, data, appendNewline = true) {
  return request(`/terminal/sessions/${sessionId}/input`, {
    method: 'POST',
    body: JSON.stringify({ data, append_newline: appendNewline }),
  })
}

export function resizeTerminalSession(sessionId, cols, rows) {
  return request(`/terminal/sessions/${sessionId}/resize`, {
    method: 'POST',
    body: JSON.stringify({ cols, rows }),
  })
}

export function openTerminalSocket(sessionId) {
  return new WebSocket(`${WS_BASE}/terminal/sessions/${sessionId}/stream`)
}