File size: 5,292 Bytes
5afb033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// On Hugging Face, we serve from the same origin. 
// On GitHub Pages, we point to the HF API.
const isGitHubPages = window.location.hostname.includes('github.io');
const BACKEND_URL = isGitHubPages 
  ? "https://butler-columnists-christopher-fantasy.trycloudflare.com" 
  : "";
 
const LIBRARIAN_KEY = "hellowork.1234";


export async function search(prompt, library = "all") {
  const res = await fetch(`${BACKEND_URL}/api/search`, {
    method: 'POST',
    headers: { 
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${LIBRARIAN_KEY}`,
      'x-librarian-key': LIBRARIAN_KEY
    },
    body: JSON.stringify({ prompt, library, top_k: 5 })
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function listBooks(library, query = null) {
  const res = await fetch(`${BACKEND_URL}/api/list`, {
    method: 'POST',
    headers: { 
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${LIBRARIAN_KEY}`,
      'x-librarian-key': LIBRARIAN_KEY
    },
    body: JSON.stringify({ library, query })
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function advancedSearch(params) {
  const res = await fetch(`${BACKEND_URL}/api/advanced-search`, {
    method: 'POST',
    headers: { 
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${LIBRARIAN_KEY}`,
      'x-librarian-key': LIBRARIAN_KEY
    },
    body: JSON.stringify(params)
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function sendWhatsAppMessage(instance, number, text) {
  const res = await fetch(`${BACKEND_URL}/api/whatsapp/send`, {
    method: 'POST',
    headers: { 
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${LIBRARIAN_KEY}`,
      'x-librarian-key': LIBRARIAN_KEY
    },
    body: JSON.stringify({ instance, number, text })
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function fetchInstances() {
  const res = await fetch(`${BACKEND_URL}/instance/fetchInstances`, {
    headers: { 'apikey': LIBRARIAN_KEY }
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function createInstance(name) {
  const res = await fetch(`${BACKEND_URL}/instance/create`, {
    method: 'POST',
    headers: { 
      'Content-Type': 'application/json',
      'apikey': LIBRARIAN_KEY
    },
    body: JSON.stringify({ 
      instanceName: name, 
      token: Math.random().toString(36).substring(2, 15),
      qrcode: true,
      integration: 'WHATSAPP-BAILEYS'
    })
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function requestPairingCode(name, number) {
  // Try the most likely endpoints for the latest Evolution API versions
  const endpoints = [
    `${BACKEND_URL}/instance/connect/${name}/pairingCode`,
    `${BACKEND_URL}/instance/connect/${name}/pairing_code`,
    `${BACKEND_URL}/instance/pairingcode/${name}`,
  ];

  for (const url of endpoints) {
    try {
      const res = await fetch(url, {
        method: 'POST',
        headers: { 
          'Content-Type': 'application/json',
          'apikey': LIBRARIAN_KEY
        },
        body: JSON.stringify({ phoneNumber: number })
      });
      if (res.ok) {
        return await res.json();
      }
    } catch (e) {
      console.log(`[api] Trying endpoint ${url} failed...`);
    }
  }
  throw new Error("Pairing code endpoint not found on this API version");
}

export async function connectInstance(name, number = null) {
  let url = `${BACKEND_URL}/instance/connect/${name}`;
  if (number) url += `?number=${number}`;
  
  const res = await fetch(url, {
    headers: { 'apikey': LIBRARIAN_KEY }
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function logoutInstance(name) {
  const res = await fetch(`${BACKEND_URL}/instance/logout/${name}`, {
    method: 'DELETE',
    headers: { 'apikey': LIBRARIAN_KEY }
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function deleteInstance(name) {
  const res = await fetch(`${BACKEND_URL}/instance/delete/${name}`, {
    method: 'DELETE',
    headers: { 'apikey': LIBRARIAN_KEY }
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function fetchOverdueBooks() {
  const res = await fetch(`${BACKEND_URL}/api/overdue`, {
    headers: { 
      'Authorization': `Bearer ${LIBRARIAN_KEY}`,
      'x-librarian-key': LIBRARIAN_KEY
    }
  });
  if (!res.ok) throw new Error(`Server error: ${res.status}`);
  return await res.json();
}

export async function getCachedQR(instanceName) {
  try {
    const res = await fetch(`${BACKEND_URL}/api/whatsapp/qr`);
    if (!res.ok) return null;
    const data = await res.json();
    
    // Only return the cache if it belongs to the requested instance
    if (data?.instance === instanceName) {
      return data;
    }
    return null;
  } catch (e) {
    console.error("[api] getCachedQR error:", e);
    return null;
  }
}