UNPKG

ttc-ai-client

Version:

TypeScript client sdk for TTC AI services with decorators and schema validation.

320 lines 12.8 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.RPCClient = exports._scid = void 0; const socket_io_client_1 = require("socket.io-client"); let sid = ""; const _scid = () => { if (sid) return sid; const key = 'ttc_scid_ai'; try { sid = localStorage ? localStorage.getItem(key) : null; } catch (error) { sid = null; } if (!sid) { sid = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); try { localStorage.setItem(key, sid); } catch (error) { } } return sid; }; exports._scid = _scid; class RPCClient { constructor(url, token_cb, socket_cb) { this.ttcCore = { /** * sends a message to the ai * * @param {conversation_id: string, message: string, scid: string} * @returns {Promise<rpcResponseType<any>>} */ async chatAssistant(conversation_id, message, src_conversation_id) { return await _a.apiCallback('ttcCore.chatAssistant', [conversation_id, message, src_conversation_id]); }, /** * sends a message to the ai * * @param {conversation_id: string, message: string} * @returns {Promise<rpcResponseType<any>>} */ async chatAI(conversation_id, message) { return await _a.apiCallback('ttcCore.chatAI', [conversation_id, message]); }, /** * sends a function response to the ai * * @param {conversation_id: string, response: any[], image_urls: any[]} * @returns {Promise<rpcResponseType<any>>} */ async functionResponse(conversation_id, response, image_urls) { return await _a.apiCallback('ttcCore.functionResponse', [conversation_id, response, image_urls]); }, /** * get chat history * * @param {conversation_id: string, limit: number, page: number} * @returns {Promise<rpcResponseType<any>>} */ async fetchChatHistory(conversation_id, limit, page) { return await _a.apiCallback('ttcCore.fetchChatHistory', [conversation_id, limit, page]); }, /** * take a note * * @param {conversation_id: string, note: string} * @returns {Promise<rpcResponseType<any>>} */ async takeNote(conversation_id, note) { return await _a.apiCallback('ttcCore.takeNote', [conversation_id, note]); }, /** * search chat history * * @param {conversation_id: string, startIndex: number, endIndex: number, query: string} * @returns {Promise<rpcResponseType<any>>} */ async searchChatHistory(conversation_id, startIndex, endIndex, query) { return await _a.apiCallback('ttcCore.searchChatHistory', [conversation_id, startIndex, endIndex, query]); }, /** * sets a function for the app * * @param {conversation_id: string, func: Partial<{ }> } * @returns {Promise<rpcResponseType<any>>} */ async setAppFunctions(conversation_id, func) { return await _a.apiCallback('ttcCore.setAppFunctions', [conversation_id, func]); }, /** * Generates a chat token * * @param {uuid: string} * @returns {Promise<rpcResponseType<any>>} */ async createChat(uuid) { return await _a.apiCallback('ttcCore.createChat', [uuid]); }, /** * Updates a chat * * @param {chatId: string, metadata: Partial<{ }> } * @returns {Promise<rpcResponseType<any>>} */ async updateChat(chatId, metadata) { return await _a.apiCallback('ttcCore.updateChat', [chatId, metadata]); }, /** * Fetches all chats for an app * * @param {page: number, limit: number} * @returns {Promise<rpcResponseType<string[]>>} */ async fetchChats(page, limit) { return await _a.apiCallback('ttcCore.fetchChats', [page, limit]); }, /** * Fetches a chat * * @param {chatId: string} * @returns {Promise<rpcResponseType<{ id: string; inputTokens: number; outputTokens: number; metadata?: any }>>} */ async fetchChat(chatId) { return await _a.apiCallback('ttcCore.fetchChat', [chatId]); }, /** * Updates a chat * * @param {chatId: string} * @returns {Promise<rpcResponseType<any>>} */ async deleteChat(chatId) { return await _a.apiCallback('ttcCore.deleteChat', [chatId]); }, /** * Clears chat history, soft clears messages but retains system prompt and functions, hard clears everything * * @param {conversation_id: string, mode: string} * @returns {Promise<rpcResponseType<string>>} */ async clearChatHistory(conversation_id, mode) { return await _a.apiCallback('ttcCore.clearChatHistory', [conversation_id, mode]); }, /** * fetches an emote * * @param {} * @returns {Promise<rpcResponseType<{ emote: string; modelId?: string }>>} */ async fetchEmote() { return await _a.apiCallback('ttcCore.fetchEmote'); }, /** * searches longterm memory notes * * @param {conversation_id: string, startIndex: number, endIndex: number, query: string} * @returns {Promise<rpcResponseType<any[]>>} */ async searchLongtermMemory(conversation_id, startIndex, endIndex, query) { return await _a.apiCallback('ttcCore.searchLongtermMemory', [conversation_id, startIndex, endIndex, query]); }, /** * stores a longterm memory note for the assistant * * @param {conversation_id: string, note: string} * @returns {Promise<rpcResponseType<string>>} */ async storeLongtermMemory(conversation_id, note) { return await _a.apiCallback('ttcCore.storeLongtermMemory', [conversation_id, note]); }, /** * Adds a remote origin for an app * * @param {app_id: string} * @returns {Promise<rpcResponseType<{ url: string; name: string }[]>>} */ async fetchOrigins(app_id) { return await _a.apiCallback('ttcCore.fetchOrigins', [app_id]); }, }; this.AppOAuth = { /** * Generate OAuth authorization URL for app integration. Platform embeds this URL to start OAuth flow. * * @param {appId: string, redirectUri: string, scopes: string, state: string} * @returns {Promise<rpcResponseType<{ authorizationUrl: string; state: string }>>} */ async generateAuthorizationUrl(appId, redirectUri, scopes, state) { return await _a.apiCallback('AppOAuth.generateAuthorizationUrl', [appId, redirectUri, scopes, state]); }, /** * Exchange authorization code for access token. Called by platform backend after receiving code. * * @param {code: string, appId: string, redirectUri: string} * @returns {Promise<rpcResponseType<{ accessToken: string; tokenType: string; expiresIn: number; appUserId: string; emote: any }>>} */ async exchangeCodeForToken(code, appId, redirectUri) { return await _a.apiCallback('AppOAuth.exchangeCodeForToken', [code, appId, redirectUri]); }, /** * Refresh an expired access token for continued API access. * * @param {appUserId: string, appId: string} * @returns {Promise<rpcResponseType<{ accessToken: string; tokenType: string; expiresIn: number; appUserId: string }>>} */ async refreshToken(appUserId, appId) { return await _a.apiCallback('AppOAuth.refreshToken', [appUserId, appId]); }, }; this.Media = { /** * Uploads an image to S3 and returns its URL. Replaces any existing image for the conversation. * * @param {appId: string, conversationId: string, file: any} * @returns {Promise<rpcResponseType<{ url: string }>>} */ async uploadImage(appId, conversationId, file) { return await _a.mediaCallback('Media.uploadImage', [appId, conversationId], file); }, }; this.TCC = { /** * Invokes a tentarcles critical command * * @param {command: string, conversation_id: string} * @returns {Promise<rpcResponseType<any>>} */ async invoke(command, conversation_id) { return await _a.apiCallback('TCC.invoke', [command, conversation_id]); }, /** * Changes the model for a conversation, use with caution, may discontinue chat if model is not compatible * * @param {conversation_id: string, model_id: string} * @returns {Promise<rpcResponseType<any>>} */ async change_to_model(conversation_id, model_id, type) { return await _a.apiCallback('TCC.change_to_model', [conversation_id, model_id, type]); }, }; _a.token_cb = token_cb; _a.url = url; if (socket_cb) this.connectSocket(url, socket_cb); } async connectSocket(url, socket_cb) { try { const socket = (0, socket_io_client_1.io)(url, { auth: { authorization: await _a.token_cb(), _scid: (0, exports._scid)() } }); if (socket_cb) socket_cb(socket); } catch (error) { console.error('Error initializing socket:', error.message); } } } exports.RPCClient = RPCClient; _a = RPCClient; RPCClient.url = 'https://api.tentarclesai.com'; RPCClient.apiCallback = async (method, params) => { try { if (!_a.token_cb) throw new Error('RPCClient.token_cb is not set'); const token = await _a.token_cb(); const response = await fetch(`${_a.url}/rpc`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}:${(0, exports._scid)()}`, }, body: JSON.stringify({ method, params }) }); if (!response.ok) { throw new Error(`API error: ${response.status} ${response.statusText}`); } return await response.json(); } catch (error) { console.error("Error calling API:", error); throw error; } }; RPCClient.mediaCallback = async (method, params, file) => { try { if (!_a.token_cb) throw new Error('RPCClient.token_cb is not set'); const token = await _a.token_cb(); const formData = new FormData(); formData.append('method', method); if (params) formData.append('params', JSON.stringify(params)); if (file) formData.append('file', file); const response = await fetch(`${_a.url}/rpc`, { method: 'POST', headers: { Authorization: `Bearer ${token}:${(0, exports._scid)()}`, }, body: formData }); if (!response.ok) { throw new Error(`Media API error: ${response.status} ${response.statusText}`); } return await response.json(); } catch (error) { console.error("Error calling Media API:", error); throw error; } }; //# sourceMappingURL=ttc_server.js.map