open-webui/src/lib/apis/files/index.ts

318 lines
5.8 KiB
TypeScript
Raw Normal View History

2024-06-18 20:50:18 +00:00
import { WEBUI_API_BASE_URL } from '$lib/constants';
2025-08-19 20:36:13 +00:00
import { splitStream } from '$lib/utils';
2024-06-18 20:50:18 +00:00
2025-05-23 20:36:30 +00:00
export const uploadFile = async (token: string, file: File, metadata?: object | null) => {
2024-06-18 20:50:18 +00:00
const data = new FormData();
data.append('file', file);
2025-05-23 20:36:30 +00:00
if (metadata) {
data.append('metadata', JSON.stringify(metadata));
}
2024-06-18 20:50:18 +00:00
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/`, {
method: 'POST',
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`
},
body: data
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-06-18 20:50:18 +00:00
return null;
});
if (error) {
throw error;
}
2025-08-19 20:36:13 +00:00
if (res) {
const status = await getFileProcessStatus(token, res.id);
if (status && status.ok) {
const reader = status.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(splitStream('\n'))
.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
try {
let lines = value.split('\n');
for (const line of lines) {
if (line !== '') {
console.log(line);
if (line === 'data: [DONE]') {
console.log(line);
} else {
let data = JSON.parse(line.replace(/^data: /, ''));
console.log(data);
if (data?.error) {
console.error(data.error);
res.error = data.error;
}
}
}
}
} catch (error) {
console.log(error);
}
}
}
}
if (error) {
throw error;
}
return res;
};
export const getFileProcessStatus = async (token: string, id: string) => {
const queryParams = new URLSearchParams();
queryParams.append('stream', 'true');
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}/process/status?${queryParams}`, {
method: 'GET',
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`
}
}).catch((err) => {
error = err.detail;
console.error(err);
return null;
});
if (error) {
throw error;
}
2024-06-18 20:50:18 +00:00
return res;
};
2024-10-05 00:22:00 +00:00
export const uploadDir = async (token: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/upload/dir`, {
method: 'POST',
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-06-18 20:50:18 +00:00
export const getFiles = async (token: string = '') => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-06-18 20:50:18 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
export const getFileById = async (token: string, id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-06-18 20:50:18 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-10-04 07:46:32 +00:00
export const updateFileDataContentById = async (token: string, id: string, content: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}/data/content/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
content: content
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-10-04 07:46:32 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-06-19 21:38:09 +00:00
export const getFileContentById = async (id: string) => {
2024-06-18 21:33:44 +00:00
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}/content`, {
method: 'GET',
headers: {
2024-06-19 21:38:09 +00:00
Accept: 'application/json'
},
credentials: 'include'
2024-06-18 21:33:44 +00:00
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return await res.blob();
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-06-18 21:33:44 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-06-18 20:50:18 +00:00
export const deleteFileById = async (token: string, id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/${id}`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-06-18 20:50:18 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-06-18 22:20:04 +00:00
export const deleteAllFiles = async (token: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/files/all`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.error(err);
2024-06-18 22:20:04 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};