Skip to content

Commit

Permalink
jsdoc for chatapi
Browse files Browse the repository at this point in the history
  • Loading branch information
XaviPeiro committed Sep 3, 2024
1 parent fa26f00 commit 80f26b3
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,23 @@ async function makeApiRequest<RESP, REQ = null>(
}

export class Session {
/**
* Represents a session with an API, managing messages and interactions within a specific function group or functions.
*/

private readonly _apiBaseUrl: string;
private readonly _apiKey: string;
private _messages: ApiMessage[] = [];
private _messageIds: Set<string> = new Set();
readonly sessionId: string;
readonly functionGroup: string;

/**
* @param {string} apiBaseUrl - The base URL for the API.
* @param {string} apiKey - The API key for authentication.
* @param {string} sessionId - The unique identifier for the session.
* @param {string} functionGroup - The function group associated with the session.
*/
constructor(
apiBaseUrl: string,
apiKey: string,
Expand All @@ -99,6 +109,12 @@ export class Session {
this.functionGroup = functionGroup;
}

/**
* Submits a message to the API for the current session.
* @private
* @param {ApiMessagePayload} payload - The payload to submit.
* @returns {Promise<void>}
*/
private async _submitMessage(payload: ApiMessagePayload) {
await makeApiRequest<null, ApiSubmitMessage>(
this._apiBaseUrl,
Expand All @@ -111,6 +127,12 @@ export class Session {
);
}

/**
* Starts a new session by submitting an initial message to the ai-engine API.
* @param {string} objective - The primary goal of the session.
* @param {string} [context] - Additional context for the session.
* @returns {Promise<void>}
*/
async start(objective: string, context?: string): Promise<void> {
await this._submitMessage({
type: "start",
Expand All @@ -122,6 +144,12 @@ export class Session {
});
}

/**
* Submits message with the selected task to the ai-engine API.
* @param {TaskSelectionMessage} selection - The task selection message to submit.
* @param {TaskOption[]} options - The selected task options.
* @returns {Promise<void>}
*/
async submitTaskSelection(
selection: TaskSelectionMessage,
options: TaskOption[],
Expand All @@ -138,6 +166,12 @@ export class Session {
});
}

/**
* Send an answer to a question asked by the agent.
* @param {AgentMessage} query - The agent's message to respond to.
* @param {string} response - The user's response.
* @returns {Promise<void>}
*/
async submitResponse(query: AgentMessage, response: string): Promise<void> {
await this._submitMessage({
type: "user_message",
Expand All @@ -148,6 +182,11 @@ export class Session {
});
}

/**
* Submits a confirmation message for an agent's question to the ai-engine AP.
* @param {ConfirmationMessage} confirmation - The confirmation message to submit.
* @returns {Promise<void>}
*/
async submitConfirmation(confirmation: ConfirmationMessage): Promise<void> {
await this._submitMessage({
type: "user_message",
Expand All @@ -158,6 +197,13 @@ export class Session {
});
}

/**
* Rejects a confirmation with a given reason and submits the response to the API.
* The counterpart of `submit_confirmation`.
* @param {ConfirmationMessage} confirmation - The confirmation message to reject.
* @param {string} reason - The reason for rejecting the confirmation.
* @returns {Promise<void>}
*/
async rejectConfirmation(
confirmation: ConfirmationMessage,
reason: string,
Expand All @@ -171,6 +217,10 @@ export class Session {
});
}

/**
* Retrieves new messages for the session from the API.
* @returns {Promise<Message[]>} A list of new messages parsed from the API response.
*/
async getMessages(): Promise<Message[]> {
let queryParams = "";
if (this._messages.length > 0) {
Expand Down Expand Up @@ -251,6 +301,10 @@ export class Session {
return newMessages;
}

/**
* Deletes the current session associated with the current session_id in the ai-engine API.
* @returns {Promise<void>}
*/
async delete(): Promise<void> {
await makeApiRequest<void>(
this._apiBaseUrl,
Expand All @@ -261,6 +315,13 @@ export class Session {
);
}

/**
* Executes a set of functions within the session.
* @param {string[]} functionIds - The IDs of the functions to execute.
* @param {string} objective - The objective of the function execution.
* @param {string} context - Additional context for the function execution.
* @returns {Promise<void>}
*/
async execute_function(
functionIds: string[],
objective: string,
Expand All @@ -275,6 +336,7 @@ export class Session {
});
}
}

export class AiEngine {
private readonly _apiBaseUrl: string;
private readonly _apiKey: string;
Expand Down

0 comments on commit 80f26b3

Please sign in to comment.