UNPKG

recallrai

Version:

Official Node.js SDK for RecallrAI - Revolutionary contextual memory system that enables AI assistants to form meaningful connections between conversations, just like human memory.

146 lines (145 loc) 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.User = void 0; const user_1 = require("./models/user"); const session_1 = require("./session"); const session_2 = require("./models/session"); const errors_1 = require("./errors"); class User { /** * Initialize a user. * * @param httpClient HTTP client for API communication * @param userData User data model with user information */ constructor(httpClient, userData) { this.http = httpClient; this.userData = userData; this.userId = userData.userId; this.metadata = userData.metadata; this.createdAt = userData.createdAt; this.lastActiveAt = userData.lastActiveAt; } /** * Update this user's metadata or ID. * * @param newMetadata New metadata to associate with the user * @param newUserId New ID for the user * @returns The updated user object * @throws {UserNotFoundError} If the user is not found * @throws {UserAlreadyExistsError} If a user with the new_user_id already exists */ async update(newMetadata, newUserId) { const data = {}; if (newMetadata !== undefined) { data.metadata = newMetadata; } if (newUserId !== undefined) { data.new_user_id = newUserId; } try { const response = await this.http.put(`/api/v1/users/${this.userId}`, data); const updatedData = user_1.UserModel.fromApiResponse(response.data); // Update internal state this.userData = updatedData; this.userId = updatedData.userId; this.metadata = updatedData.metadata; this.lastActiveAt = updatedData.lastActiveAt; return this; } catch (error) { if (error.status === 404) { throw new errors_1.UserNotFoundError(this.userId); } else if (error.status === 409) { throw new errors_1.UserAlreadyExistsError(newUserId); } throw new errors_1.RecallrAIError(`Failed to update user: ${error.message || 'Unknown error'}`, undefined, error.status); } } /** * Delete this user. * * @throws {UserNotFoundError} If the user is not found */ async delete() { try { const response = await this.http.delete(`/api/v1/users/${this.userId}`); if (response.status !== 204) { throw new errors_1.RecallrAIError('Failed to delete user', undefined, response.status); } } catch (error) { if (error.status === 404) { throw new errors_1.UserNotFoundError(this.userId); } throw error; } } /** * Create a new session for this user. * * @param autoProcessAfterMinutes Minutes to wait before auto-processing (-1 to disable) * @returns A Session object to interact with the created session * @throws {UserNotFoundError} If the user is not found */ async createSession(autoProcessAfterMinutes = -1) { try { const response = await this.http.post(`/api/v1/users/${this.userId}/sessions`, { auto_process_after_minutes: autoProcessAfterMinutes }); if (response.status !== 201) { throw new errors_1.RecallrAIError('Failed to create session', undefined, response.status); } const sessionId = response.data.session_id; return new session_1.Session(this.http, this.userId, sessionId); } catch (error) { if (error.status === 404) { throw new errors_1.UserNotFoundError(this.userId); } throw error; } } /** * Get an existing session for this user. * * @param sessionId ID of the session to retrieve * @returns A Session object to interact with the session * @throws {UserNotFoundError} If the user is not found * @throws {SessionNotFoundError} If the session is not found */ async getSession(sessionId) { // Verify the session exists by checking its status const session = new session_1.Session(this.http, this.userId, sessionId); try { await session.getStatus(); // This will raise appropriate errors if the session doesn't exist return session; } catch (error) { if (error instanceof errors_1.SessionNotFoundError) { throw error; } throw new errors_1.RecallrAIError(`Error retrieving session: ${error.message}`); } } /** * List sessions for this user with pagination. * * @param offset Number of records to skip * @param limit Maximum number of records to return * @returns List of sessions with pagination info * @throws {UserNotFoundError} If the user is not found */ async listSessions(offset = 0, limit = 10) { try { const response = await this.http.get(`/api/v1/users/${this.userId}/sessions`, { params: { offset, limit } }); return session_2.SessionList.fromApiResponse(response.data); } catch (error) { if (error.status === 404) { throw new errors_1.UserNotFoundError(this.userId); } throw new errors_1.RecallrAIError(`Failed to list sessions: ${error.message || 'Unknown error'}`, undefined, error.status); } } } exports.User = User;