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

497 lines
9 KiB
TypeScript
Raw Normal View History

2024-12-22 11:10:10 +00:00
import { WEBUI_API_BASE_URL } from '$lib/constants';
type ChannelForm = {
name: string;
data?: object;
meta?: object;
access_control?: object;
2024-12-23 08:25:25 +00:00
};
2024-12-22 11:10:10 +00:00
export const createNewChannel = async (token: string = '', channel: ChannelForm) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/channels/create`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ ...channel })
})
.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-12-22 11:10:10 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
export const getChannels = async (token: string = '') => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/channels/`, {
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-12-22 11:10:10 +00:00
return null;
});
if (error) {
throw error;
}
return res;
};
2024-12-23 02:40:01 +00:00
2024-12-23 05:20:24 +00:00
export const getChannelById = async (token: string = '', channel_id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_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-12-23 05:20:24 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-23 08:25:25 +00:00
};
2024-12-23 05:20:24 +00:00
2025-11-25 09:38:07 +00:00
export const getChannelUsersById = async (
token: string,
channel_id: string,
query?: string,
orderBy?: string,
direction?: string,
page = 1
) => {
let error = null;
let res = null;
const searchParams = new URLSearchParams();
searchParams.set('page', `${page}`);
if (query) {
searchParams.set('query', query);
}
if (orderBy) {
searchParams.set('order_by', orderBy);
}
if (direction) {
searchParams.set('direction', direction);
}
res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/users?${searchParams.toString()}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
}
}
)
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.error(err);
error = err.detail;
return null;
});
if (error) {
throw error;
}
return res;
};
2024-12-23 08:25:25 +00:00
export const updateChannelById = async (
token: string = '',
channel_id: string,
channel: ChannelForm
) => {
2024-12-23 06:09:51 +00:00
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ ...channel })
})
.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-12-23 06:09:51 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-23 08:25:25 +00:00
};
2024-12-23 06:09:51 +00:00
2024-12-23 06:15:29 +00:00
export const deleteChannelById = async (token: string = '', channel_id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/delete`, {
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-12-23 06:15:29 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-23 08:25:25 +00:00
};
2024-12-23 05:20:24 +00:00
2024-12-23 08:25:25 +00:00
export const getChannelMessages = async (
token: string = '',
channel_id: string,
skip: number = 0,
limit: number = 50
) => {
2024-12-23 02:40:01 +00:00
let error = null;
2024-12-23 08:25:25 +00:00
const res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages?skip=${skip}&limit=${limit}`,
{
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
2024-12-23 02:40:01 +00:00
}
2024-12-23 08:25:25 +00:00
)
2024-12-23 02:40:01 +00:00
.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-12-23 02:40:01 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-23 08:25:25 +00:00
};
2024-12-23 02:40:01 +00:00
2024-12-31 08:51:43 +00:00
export const getChannelThreadMessages = async (
token: string = '',
channel_id: string,
message_id: string,
skip: number = 0,
limit: number = 50
) => {
let error = null;
const res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/thread?skip=${skip}&limit=${limit}`,
{
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-12-31 08:51:43 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-31 10:26:30 +00:00
};
2024-12-31 08:51:43 +00:00
2024-12-23 02:40:01 +00:00
type MessageForm = {
2025-09-27 09:05:12 +00:00
reply_to_id?: string;
2024-12-31 10:05:11 +00:00
parent_id?: string;
2024-12-23 02:40:01 +00:00
content: string;
data?: object;
2024-12-23 08:25:25 +00:00
meta?: object;
};
2024-12-23 02:40:01 +00:00
export const sendMessage = async (token: string = '', channel_id: string, message: MessageForm) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/post`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ ...message })
})
.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-12-23 02:40:01 +00:00
return null;
});
if (error) {
throw error;
}
2024-12-23 08:12:55 +00:00
return res;
2024-12-23 08:25:25 +00:00
};
2024-12-23 08:12:55 +00:00
2024-12-23 08:25:25 +00:00
export const updateMessage = async (
token: string = '',
channel_id: string,
message_id: string,
message: MessageForm
) => {
2024-12-23 08:12:55 +00:00
let error = null;
2024-12-23 08:25:25 +00:00
const res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/update`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ ...message })
}
)
2024-12-23 08:12:55 +00:00
.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-12-23 08:12:55 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-23 08:25:25 +00:00
};
2024-12-23 08:12:55 +00:00
2024-12-31 10:26:30 +00:00
export const addReaction = async (
token: string = '',
channel_id: string,
message_id: string,
name: string
) => {
2024-12-31 07:06:34 +00:00
let error = null;
const res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/reactions/add`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ name })
}
)
.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-12-31 07:06:34 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-31 10:26:30 +00:00
};
2024-12-31 07:06:34 +00:00
2024-12-31 10:26:30 +00:00
export const removeReaction = async (
token: string = '',
channel_id: string,
message_id: string,
name: string
) => {
2024-12-31 07:06:34 +00:00
let error = null;
const res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/reactions/remove`,
{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({ name })
}
)
.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-12-31 07:06:34 +00:00
return null;
});
if (error) {
throw error;
}
return res;
2024-12-31 10:26:30 +00:00
};
2024-12-31 07:06:34 +00:00
2024-12-23 08:12:55 +00:00
export const deleteMessage = async (token: string = '', channel_id: string, message_id: string) => {
let error = null;
2024-12-23 08:25:25 +00:00
const res = await fetch(
`${WEBUI_API_BASE_URL}/channels/${channel_id}/messages/${message_id}/delete`,
{
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
2024-12-23 08:12:55 +00:00
}
2024-12-23 08:25:25 +00:00
)
2024-12-23 08:12:55 +00:00
.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-12-23 08:12:55 +00:00
return null;
});
if (error) {
throw error;
}
2024-12-23 02:40:01 +00:00
return res;
2024-12-23 08:25:25 +00:00
};