UNPKG

betach

Version:
169 lines (141 loc) 3.59 kB
import Config from "./Config.js"; import Chat from "./Chat.js"; /** * @file Client.js * @description Client class for Character AI * @exports Client * @class Client * @extends Config */ export default class Client extends Config { /** * Character AI Categories * @returns {Promise<Object>} categories */ async getCategories() { const url = `${this.getBase()}/chat/character/categories/`; const body = await fetch(url, { method: "GET" } ); const res = await body.json(); if (!Array.isArray(res?.categories)) { throw new Error("Received invalid data from API"); } return res.categories; } /** * Character AI User Config * @returns {Promise<Object>} user config */ async getUserConfig() { const url = `${this.getBase()}/chat/config/`; const body = await fetch(url, { method: "GET", headers: this.getHeaders() } ); const res = await body.json(); return res.config; } /** * User info * @returns {Promise<Object>} user */ async getUser() { const url = `${this.getBase()}/chat/user/`; const body = await fetch(url, { method: "GET", headers: this.getHeaders() } ); const res = await body.json(); return res.user; } /** * Featured characters * @returns {Promise<Object>} featured characters */ async getFeatured() { const url = `${this.getBase()}/chat/characters/featured_v2/`; const body = await fetch(url, { method: "GET", headers: this.getHeaders() } ); const res = await body.json(); return res.characters; } /** * Character AI Characters * @param {boolean} curated * @returns {Promise<Object>} characters */ async getCharactersByCategories(curated = false) { const categories = curated ? "curated_categories" : "categories"; const url = `${this.getBase()}/chat/${categories}/characters/`; const body = await fetch(url, { method: "GET", headers: this.getHeaders(), } ); const property = curated ? "characters_by_curated_category" : "characters_by_category"; const res = await body.json(); return res[property]; } /** * Character info * @returns {Promise<Object>} character */ async getCharaterInfo() { const url = `${this.getBase()}/chat/character/info/`; const body = await fetch(url, { method: "POST", headers: this.getHeaders(), body: JSON.stringify({ external_id: this.getCharacter() }) } ); const res = await body.json(); return res.character; } /** * Chat with Character * @returns {Promise<Chat>} chat */ async continueOrCreateChat() { const url = `${this.getBase()}/chat/history/continue/`; const body = await fetch(url, { method: "POST", headers: this.getHeaders(), body: JSON.stringify({ character_external_id: this.getCharacter(), history_external_id: null, }) } ); let input = await body.json(); if (input.status === "No Such History") { const body = await fetch("https://beta.character.ai/chat/history/create/", { method: "POST", headers: this.getHeaders(), body: JSON.stringify({ character_external_id: this.getCharacter(), history_external_id: null, }) } ); input = await body.json(); } return new Chat(this, input); } }