|
1 | 1 | const BACKEND_URI = "";
|
2 | 2 |
|
3 |
| -import { |
4 |
| - ChatAppResponse, |
5 |
| - ChatAppResponseOrError, |
6 |
| - ChatAppRequest, |
7 |
| - Config, |
8 |
| - SimpleAPIResponse, |
9 |
| - HistoryListApiResponse, |
10 |
| - HistoryApiResponse, |
11 |
| -} from "./models"; |
| 3 | +import { ChatAppResponse, ChatAppResponseOrError, ChatAppRequest, Config, SimpleAPIResponse, HistoryListApiResponse, HistoryApiResponse } from "./models"; |
12 | 4 | import { useLogin, getToken, isUsingAppServicesLogin } from "../authConfig";
|
13 | 5 |
|
14 |
| -export async function getHeaders( |
15 |
| - idToken: string | undefined, |
16 |
| -): Promise<Record<string, string>> { |
17 |
| - // If using login and not using app services, add the id token of the logged in account as the authorization |
18 |
| - if (useLogin && !isUsingAppServicesLogin) { |
19 |
| - if (idToken) { |
20 |
| - return { Authorization: `Bearer ${idToken}` }; |
| 6 | +export async function getHeaders(idToken: string | undefined): Promise<Record<string, string>> { |
| 7 | + // If using login and not using app services, add the id token of the logged in account as the authorization |
| 8 | + if (useLogin && !isUsingAppServicesLogin) { |
| 9 | + if (idToken) { |
| 10 | + return { Authorization: `Bearer ${idToken}` }; |
| 11 | + } |
21 | 12 | }
|
22 |
| - } |
23 | 13 |
|
24 |
| - return {}; |
| 14 | + return {}; |
25 | 15 | }
|
26 | 16 |
|
27 | 17 | export async function configApi(): Promise<Config> {
|
28 |
| - const response = await fetch(`${BACKEND_URI}/config`, { |
29 |
| - method: "GET", |
30 |
| - }); |
| 18 | + const response = await fetch(`${BACKEND_URI}/config`, { |
| 19 | + method: "GET" |
| 20 | + }); |
31 | 21 |
|
32 |
| - return (await response.json()) as Config; |
| 22 | + return (await response.json()) as Config; |
33 | 23 | }
|
34 | 24 |
|
35 |
| -export async function askApi( |
36 |
| - request: ChatAppRequest, |
37 |
| - idToken: string | undefined, |
38 |
| -): Promise<ChatAppResponse> { |
39 |
| - const headers = await getHeaders(idToken); |
40 |
| - const response = await fetch(`${BACKEND_URI}/ask`, { |
41 |
| - method: "POST", |
42 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
43 |
| - body: JSON.stringify(request), |
44 |
| - }); |
45 |
| - |
46 |
| - if (response.status > 299 || !response.ok) { |
47 |
| - throw Error(`Request failed with status ${response.status}`); |
48 |
| - } |
49 |
| - const parsedResponse: ChatAppResponseOrError = await response.json(); |
50 |
| - if (parsedResponse.error) { |
51 |
| - throw Error(parsedResponse.error); |
52 |
| - } |
53 |
| - |
54 |
| - return parsedResponse as ChatAppResponse; |
| 25 | +export async function askApi(request: ChatAppRequest, idToken: string | undefined): Promise<ChatAppResponse> { |
| 26 | + const headers = await getHeaders(idToken); |
| 27 | + const response = await fetch(`${BACKEND_URI}/ask`, { |
| 28 | + method: "POST", |
| 29 | + headers: { ...headers, "Content-Type": "application/json" }, |
| 30 | + body: JSON.stringify(request) |
| 31 | + }); |
| 32 | + |
| 33 | + if (response.status > 299 || !response.ok) { |
| 34 | + throw Error(`Request failed with status ${response.status}`); |
| 35 | + } |
| 36 | + const parsedResponse: ChatAppResponseOrError = await response.json(); |
| 37 | + if (parsedResponse.error) { |
| 38 | + throw Error(parsedResponse.error); |
| 39 | + } |
| 40 | + |
| 41 | + return parsedResponse as ChatAppResponse; |
55 | 42 | }
|
56 | 43 |
|
57 |
| -export async function chatApi( |
58 |
| - request: ChatAppRequest, |
59 |
| - shouldStream: boolean, |
60 |
| - idToken: string | undefined, |
61 |
| -): Promise<Response> { |
62 |
| - let url = `${BACKEND_URI}/chat`; |
63 |
| - if (shouldStream) { |
64 |
| - url += "/stream"; |
65 |
| - } |
66 |
| - const headers = await getHeaders(idToken); |
67 |
| - return await fetch(url, { |
68 |
| - method: "POST", |
69 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
70 |
| - body: JSON.stringify(request), |
71 |
| - }); |
| 44 | +export async function chatApi(request: ChatAppRequest, shouldStream: boolean, idToken: string | undefined): Promise<Response> { |
| 45 | + let url = `${BACKEND_URI}/chat`; |
| 46 | + if (shouldStream) { |
| 47 | + url += "/stream"; |
| 48 | + } |
| 49 | + const headers = await getHeaders(idToken); |
| 50 | + return await fetch(url, { |
| 51 | + method: "POST", |
| 52 | + headers: { ...headers, "Content-Type": "application/json" }, |
| 53 | + body: JSON.stringify(request) |
| 54 | + }); |
72 | 55 | }
|
73 | 56 |
|
74 | 57 | export async function getSpeechApi(text: string): Promise<string | null> {
|
75 |
| - return await fetch("/speech", { |
76 |
| - method: "POST", |
77 |
| - headers: { |
78 |
| - "Content-Type": "application/json", |
79 |
| - }, |
80 |
| - body: JSON.stringify({ |
81 |
| - text: text, |
82 |
| - }), |
83 |
| - }) |
84 |
| - .then((response) => { |
85 |
| - if (response.status == 200) { |
86 |
| - return response.blob(); |
87 |
| - } else if (response.status == 400) { |
88 |
| - console.log("Speech synthesis is not enabled."); |
89 |
| - return null; |
90 |
| - } else { |
91 |
| - console.error("Unable to get speech synthesis."); |
92 |
| - return null; |
93 |
| - } |
| 58 | + return await fetch("/speech", { |
| 59 | + method: "POST", |
| 60 | + headers: { |
| 61 | + "Content-Type": "application/json" |
| 62 | + }, |
| 63 | + body: JSON.stringify({ |
| 64 | + text: text |
| 65 | + }) |
94 | 66 | })
|
95 |
| - .then((blob) => (blob ? URL.createObjectURL(blob) : null)); |
| 67 | + .then(response => { |
| 68 | + if (response.status == 200) { |
| 69 | + return response.blob(); |
| 70 | + } else if (response.status == 400) { |
| 71 | + console.log("Speech synthesis is not enabled."); |
| 72 | + return null; |
| 73 | + } else { |
| 74 | + console.error("Unable to get speech synthesis."); |
| 75 | + return null; |
| 76 | + } |
| 77 | + }) |
| 78 | + .then(blob => (blob ? URL.createObjectURL(blob) : null)); |
96 | 79 | }
|
97 | 80 |
|
98 | 81 | export function getCitationFilePath(citation: string): string {
|
99 |
| - return `${BACKEND_URI}/content/${citation}`; |
| 82 | + return `${BACKEND_URI}/content/${citation}`; |
100 | 83 | }
|
101 | 84 |
|
102 |
| -export async function uploadFileApi( |
103 |
| - request: FormData, |
104 |
| - idToken: string, |
105 |
| -): Promise<SimpleAPIResponse> { |
106 |
| - const response = await fetch("/upload", { |
107 |
| - method: "POST", |
108 |
| - headers: await getHeaders(idToken), |
109 |
| - body: request, |
110 |
| - }); |
111 |
| - |
112 |
| - if (!response.ok) { |
113 |
| - throw new Error(`Uploading files failed: ${response.statusText}`); |
114 |
| - } |
115 |
| - |
116 |
| - const dataResponse: SimpleAPIResponse = await response.json(); |
117 |
| - return dataResponse; |
| 85 | +export async function uploadFileApi(request: FormData, idToken: string): Promise<SimpleAPIResponse> { |
| 86 | + const response = await fetch("/upload", { |
| 87 | + method: "POST", |
| 88 | + headers: await getHeaders(idToken), |
| 89 | + body: request |
| 90 | + }); |
| 91 | + |
| 92 | + if (!response.ok) { |
| 93 | + throw new Error(`Uploading files failed: ${response.statusText}`); |
| 94 | + } |
| 95 | + |
| 96 | + const dataResponse: SimpleAPIResponse = await response.json(); |
| 97 | + return dataResponse; |
118 | 98 | }
|
119 | 99 |
|
120 |
| -export async function deleteUploadedFileApi( |
121 |
| - filename: string, |
122 |
| - idToken: string, |
123 |
| -): Promise<SimpleAPIResponse> { |
124 |
| - const headers = await getHeaders(idToken); |
125 |
| - const response = await fetch("/delete_uploaded", { |
126 |
| - method: "POST", |
127 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
128 |
| - body: JSON.stringify({ filename }), |
129 |
| - }); |
130 |
| - |
131 |
| - if (!response.ok) { |
132 |
| - throw new Error(`Deleting file failed: ${response.statusText}`); |
133 |
| - } |
134 |
| - |
135 |
| - const dataResponse: SimpleAPIResponse = await response.json(); |
136 |
| - return dataResponse; |
| 100 | +export async function deleteUploadedFileApi(filename: string, idToken: string): Promise<SimpleAPIResponse> { |
| 101 | + const headers = await getHeaders(idToken); |
| 102 | + const response = await fetch("/delete_uploaded", { |
| 103 | + method: "POST", |
| 104 | + headers: { ...headers, "Content-Type": "application/json" }, |
| 105 | + body: JSON.stringify({ filename }) |
| 106 | + }); |
| 107 | + |
| 108 | + if (!response.ok) { |
| 109 | + throw new Error(`Deleting file failed: ${response.statusText}`); |
| 110 | + } |
| 111 | + |
| 112 | + const dataResponse: SimpleAPIResponse = await response.json(); |
| 113 | + return dataResponse; |
137 | 114 | }
|
138 | 115 |
|
139 | 116 | export async function listUploadedFilesApi(idToken: string): Promise<string[]> {
|
140 |
| - const response = await fetch(`/list_uploaded`, { |
141 |
| - method: "GET", |
142 |
| - headers: await getHeaders(idToken), |
143 |
| - }); |
| 117 | + const response = await fetch(`/list_uploaded`, { |
| 118 | + method: "GET", |
| 119 | + headers: await getHeaders(idToken) |
| 120 | + }); |
144 | 121 |
|
145 |
| - if (!response.ok) { |
146 |
| - throw new Error(`Listing files failed: ${response.statusText}`); |
147 |
| - } |
| 122 | + if (!response.ok) { |
| 123 | + throw new Error(`Listing files failed: ${response.statusText}`); |
| 124 | + } |
148 | 125 |
|
149 |
| - const dataResponse: string[] = await response.json(); |
150 |
| - return dataResponse; |
| 126 | + const dataResponse: string[] = await response.json(); |
| 127 | + return dataResponse; |
151 | 128 | }
|
152 | 129 |
|
153 |
| -export async function postChatHistoryApi( |
154 |
| - item: any, |
155 |
| - idToken: string, |
156 |
| -): Promise<any> { |
157 |
| - const headers = await getHeaders(idToken); |
158 |
| - const response = await fetch("/chat_history", { |
159 |
| - method: "POST", |
160 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
161 |
| - body: JSON.stringify(item), |
162 |
| - }); |
163 |
| - |
164 |
| - if (!response.ok) { |
165 |
| - throw new Error(`Posting chat history failed: ${response.statusText}`); |
166 |
| - } |
167 |
| - |
168 |
| - const dataResponse: any = await response.json(); |
169 |
| - return dataResponse; |
| 130 | +export async function postChatHistoryApi(item: any, idToken: string): Promise<any> { |
| 131 | + const headers = await getHeaders(idToken); |
| 132 | + const response = await fetch("/chat_history", { |
| 133 | + method: "POST", |
| 134 | + headers: { ...headers, "Content-Type": "application/json" }, |
| 135 | + body: JSON.stringify(item) |
| 136 | + }); |
| 137 | + |
| 138 | + if (!response.ok) { |
| 139 | + throw new Error(`Posting chat history failed: ${response.statusText}`); |
| 140 | + } |
| 141 | + |
| 142 | + const dataResponse: any = await response.json(); |
| 143 | + return dataResponse; |
170 | 144 | }
|
171 | 145 |
|
172 |
| -export async function getChatHistoryListApi( |
173 |
| - count: number, |
174 |
| - continuationToken: string | undefined, |
175 |
| - idToken: string, |
176 |
| -): Promise<HistoryListApiResponse> { |
177 |
| - const headers = await getHeaders(idToken); |
178 |
| - let url = `${BACKEND_URI}/chat_history/sessions?count=${count}`; |
179 |
| - if (continuationToken) { |
180 |
| - url += `&continuationToken=${continuationToken}`; |
181 |
| - } |
182 |
| - |
183 |
| - const response = await fetch(url.toString(), { |
184 |
| - method: "GET", |
185 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
186 |
| - }); |
187 |
| - |
188 |
| - if (!response.ok) { |
189 |
| - throw new Error(`Getting chat histories failed: ${response.statusText}`); |
190 |
| - } |
191 |
| - |
192 |
| - const dataResponse: HistoryListApiResponse = await response.json(); |
193 |
| - return dataResponse; |
| 146 | +export async function getChatHistoryListApi(count: number, continuationToken: string | undefined, idToken: string): Promise<HistoryListApiResponse> { |
| 147 | + const headers = await getHeaders(idToken); |
| 148 | + let url = `${BACKEND_URI}/chat_history/sessions?count=${count}`; |
| 149 | + if (continuationToken) { |
| 150 | + url += `&continuationToken=${continuationToken}`; |
| 151 | + } |
| 152 | + |
| 153 | + const response = await fetch(url.toString(), { |
| 154 | + method: "GET", |
| 155 | + headers: { ...headers, "Content-Type": "application/json" } |
| 156 | + }); |
| 157 | + |
| 158 | + if (!response.ok) { |
| 159 | + throw new Error(`Getting chat histories failed: ${response.statusText}`); |
| 160 | + } |
| 161 | + |
| 162 | + const dataResponse: HistoryListApiResponse = await response.json(); |
| 163 | + return dataResponse; |
194 | 164 | }
|
195 | 165 |
|
196 |
| -export async function getChatHistoryApi( |
197 |
| - id: string, |
198 |
| - idToken: string, |
199 |
| -): Promise<HistoryApiResponse> { |
200 |
| - const headers = await getHeaders(idToken); |
201 |
| - const response = await fetch(`/chat_history/sessions/${id}`, { |
202 |
| - method: "GET", |
203 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
204 |
| - }); |
205 |
| - |
206 |
| - if (!response.ok) { |
207 |
| - throw new Error(`Getting chat history failed: ${response.statusText}`); |
208 |
| - } |
209 |
| - |
210 |
| - const dataResponse: HistoryApiResponse = await response.json(); |
211 |
| - return dataResponse; |
| 166 | +export async function getChatHistoryApi(id: string, idToken: string): Promise<HistoryApiResponse> { |
| 167 | + const headers = await getHeaders(idToken); |
| 168 | + const response = await fetch(`/chat_history/sessions/${id}`, { |
| 169 | + method: "GET", |
| 170 | + headers: { ...headers, "Content-Type": "application/json" } |
| 171 | + }); |
| 172 | + |
| 173 | + if (!response.ok) { |
| 174 | + throw new Error(`Getting chat history failed: ${response.statusText}`); |
| 175 | + } |
| 176 | + |
| 177 | + const dataResponse: HistoryApiResponse = await response.json(); |
| 178 | + return dataResponse; |
212 | 179 | }
|
213 | 180 |
|
214 |
| -export async function deleteChatHistoryApi( |
215 |
| - id: string, |
216 |
| - idToken: string, |
217 |
| -): Promise<any> { |
218 |
| - const headers = await getHeaders(idToken); |
219 |
| - const response = await fetch(`/chat_history/sessions/${id}`, { |
220 |
| - method: "DELETE", |
221 |
| - headers: { ...headers, "Content-Type": "application/json" }, |
222 |
| - }); |
223 |
| - |
224 |
| - if (!response.ok) { |
225 |
| - throw new Error(`Deleting chat history failed: ${response.statusText}`); |
226 |
| - } |
| 181 | +export async function deleteChatHistoryApi(id: string, idToken: string): Promise<any> { |
| 182 | + const headers = await getHeaders(idToken); |
| 183 | + const response = await fetch(`/chat_history/sessions/${id}`, { |
| 184 | + method: "DELETE", |
| 185 | + headers: { ...headers, "Content-Type": "application/json" } |
| 186 | + }); |
| 187 | + |
| 188 | + if (!response.ok) { |
| 189 | + throw new Error(`Deleting chat history failed: ${response.statusText}`); |
| 190 | + } |
227 | 191 | }
|
0 commit comments