Skip to content

Commit 342574f

Browse files
jsDoc tiny fix
1 parent 65493e0 commit 342574f

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

src/base.mjs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ import { objType } from 'tiny-essentials';
1515
* Documentation written with the assistance of OpenAI's ChatGPT.
1616
*/
1717
class TinyAiInstance extends EventEmitter {
18+
/**
19+
* @typedef {Object} AIContentData
20+
* @property {Array<Record<'text' | 'inlineData', string | { mime_type: string, data: string } | null>>} parts
21+
* @property {string|undefined} [role]
22+
* @property {string|number|undefined} [finishReason]
23+
*/
24+
25+
/**
26+
* @typedef {Record<string, any> & {
27+
* data: Array<AIContentData>,
28+
* ids: Array<string>,
29+
* tokens: { data: Array<TokenCount>; [key: string]: * },
30+
* hash: { data: Array<string>; [key: string]: * },
31+
* systemInstruction: string|null,
32+
* model: string|null
33+
* }} SessionData
34+
*/
35+
36+
/**
37+
* @typedef {{ count: number|null, hide?: boolean }} TokenCount
38+
*/
39+
1840
/** @type {string|null} */ #_apiKey = null;
1941
/** @type {function|null} */ #_getModels = null;
2042
/** @type {function|null} */ #_countTokens = null;
@@ -26,7 +48,7 @@ class TinyAiInstance extends EventEmitter {
2648
/** @type {string|null} */ _nextModelsPageToken = null;
2749

2850
/** @type {Array<*>} */ models = [];
29-
/** @type {Object.<string, Record<string, any>>} */ history = {};
51+
/** @type {Object.<string, SessionData>} */ history = {};
3052
_isSingle = false;
3153

3254
/**
@@ -458,13 +480,6 @@ class TinyAiInstance extends EventEmitter {
458480
return history && typeof history.model === 'string' ? history.model : null;
459481
}
460482

461-
/**
462-
* @typedef {Object} AIContentData
463-
* @property {Array<Record<'text' | 'inlineData', string | { mime_type: string, data: string } | null>>} parts
464-
* @property {string|undefined} [role]
465-
* @property {string|number|undefined} [finishReason]
466-
*/
467-
468483
/**
469484
* Build content data for an AI session.
470485
*
@@ -565,7 +580,7 @@ class TinyAiInstance extends EventEmitter {
565580
* Get model data from the list of models.
566581
*
567582
* @param {string} id - The model data id to search for in the models list.
568-
* @returns {Object|null} The model data if found, otherwise null.
583+
* @returns {Record<string, any>|null} The model data if found, otherwise null.
569584
*/
570585
getModelData(id) {
571586
const model = this.models.find((item) => item.id === id);
@@ -615,7 +630,7 @@ class TinyAiInstance extends EventEmitter {
615630
* @param {string} model.category.id - The unique identifier for the category.
616631
* @param {string} model.category.displayName - The display name of the category.
617632
* @param {number} model.category.index - The index of the category.
618-
* @returns {Object|null} The inserted model data, or null if the model already exists.
633+
* @returns {Record<string, any>|null} The inserted model data, or null if the model already exists.
619634
*/
620635
_insertNewModel(model) {
621636
if (!objType(model, 'object')) throw new Error('Model data must be a valid object.');
@@ -703,22 +718,26 @@ class TinyAiInstance extends EventEmitter {
703718
* Counts the tokens based on the provided data and model, using a defined token counting function.
704719
* If the function to count tokens is not set, an error is thrown.
705720
*
706-
* @param {Object} data - The data that needs to be tokenized.
721+
* @param {Record<string, any>} data - The data that needs to be tokenized.
707722
* @param {string} model - The model to use for counting tokens. If not provided, the default model is used.
708-
* @param {Object} controller - The controller that manages the process or settings for counting tokens.
723+
* @param {AbortController} controller - The controller that manages the process or settings for counting tokens.
709724
* @throws {Error} Throws an error if no token counting function is defined.
710-
* @returns {Object} The count of tokens.
725+
* @returns {Record<string, any>} The count of tokens.
711726
*/
712727
countTokens(data, model, controller) {
713728
if (typeof this.#_countTokens === 'function')
714729
return this.#_countTokens(this.#_apiKey, model || this.getModel(), controller, data);
715730
throw new Error('No count token api script defined.');
716731
}
717732

733+
/**
734+
* @typedef {{ text: string, hide?: boolean }} ErrorCode
735+
*/
736+
718737
/**
719738
* Sets the error codes for the current session.
720739
*
721-
* @param {Record<string|number, string|{ text: string, hide?: boolean }>} errors - The error codes to set, typically an object containing error code definitions.
740+
* @param {Record<string|number, string|ErrorCode>} errors - The error codes to set, typically an object containing error code definitions.
722741
* @returns {void}
723742
*/
724743
_setErrorCodes(errors) {
@@ -729,7 +748,7 @@ class TinyAiInstance extends EventEmitter {
729748
* Get error details based on the provided error code.
730749
*
731750
* @param {string|number} code - The error code to look up.
732-
* @returns {Object|null} An object containing the error message, or null if no error is found.
751+
* @returns {ErrorCode|null} An object containing the error message, or null if no error is found.
733752
*/
734753
getErrorCode(code) {
735754
if (this._errorCode) {
@@ -756,11 +775,11 @@ class TinyAiInstance extends EventEmitter {
756775
/**
757776
* Generates content for the AI session.
758777
*
759-
* @param {Object} data - The data for content generation.
778+
* @param {Record<string, any>} data - The data for content generation.
760779
* @param {string} model - The model to be used for content generation. If not provided, the default model is used.
761-
* @param {Object} controller - The controller managing the content generation process.
780+
* @param {AbortController} controller - The controller managing the content generation process.
762781
* @param {Function} [streamCallback] - The callback function for streaming content (optional).
763-
* @returns {Object} The generated content returned by the API.
782+
* @returns {Record<string, any>} The generated content returned by the API.
764783
* @throws {Error} If no content generator API script is defined.
765784
*/
766785
genContent(data, model, controller, streamCallback) {
@@ -814,7 +833,7 @@ class TinyAiInstance extends EventEmitter {
814833
* Get the data associated with a specific session history ID.
815834
*
816835
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
817-
* @returns {Record<string, any>|null} The data associated with the session ID, or `null` if no data exists for that ID.
836+
* @returns {SessionData|null} The data associated with the session ID, or `null` if no data exists for that ID.
818837
*/
819838
getData(id) {
820839
const selectedId = this.getId(id);
@@ -858,7 +877,7 @@ class TinyAiInstance extends EventEmitter {
858877
*
859878
* @param {number} msgIndex - The index of the message in the session history.
860879
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
861-
* @returns {Object|null} The token data associated with the message at the specified index, or `null` if the data is not found.
880+
* @returns {TokenCount|null} The token data associated with the message at the specified index, or `null` if the data is not found.
862881
*/
863882
getMsgTokensByIndex(msgIndex, id) {
864883
const history = this.getData(id);
@@ -877,7 +896,7 @@ class TinyAiInstance extends EventEmitter {
877896
*
878897
* @param {string} msgId - The unique ID of the message in the session history.
879898
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
880-
* @returns {Object|null} The token data associated with the message with the given ID, or `null` if the message is not found.
899+
* @returns {TokenCount|null} The token data associated with the message with the given ID, or `null` if the message is not found.
881900
*/
882901
getMsgTokensById(msgId, id) {
883902
const history = this.getData(id);
@@ -939,7 +958,7 @@ class TinyAiInstance extends EventEmitter {
939958
*
940959
* @param {number} index - The index of the data entry to retrieve.
941960
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
942-
* @returns {Object|null} The data entry at the specified index, or `null` if the index is out of bounds or no data exists for the given session ID.
961+
* @returns {AIContentData|null} The data entry at the specified index, or `null` if the index is out of bounds or no data exists for the given session ID.
943962
*/
944963
getMsgByIndex(index, id) {
945964
const history = this.getData(id);
@@ -952,7 +971,7 @@ class TinyAiInstance extends EventEmitter {
952971
*
953972
* @param {string} msgId - The ID of the message to retrieve.
954973
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
955-
* @returns {Object|null} The message data associated with the given ID, or `null` if the message ID is invalid or does not exist.
974+
* @returns {AIContentData|null} The message data associated with the given ID, or `null` if the message ID is invalid or does not exist.
956975
*/
957976
getMsgById(msgId, id) {
958977
const history = this.getData(id);
@@ -1014,8 +1033,8 @@ class TinyAiInstance extends EventEmitter {
10141033
* Replaces an entry at the specified index in the session history with new data.
10151034
*
10161035
* @param {number} index - The index of the entry to replace.
1017-
* @param {Object} [data] - The new data to replace the existing entry (optional).
1018-
* @param {number} [tokens] - The token count associated with the new entry (optional).
1036+
* @param {AIContentData} [data] - The new data to replace the existing entry (optional).
1037+
* @param {TokenCount} [tokens] - The token count associated with the new entry (optional).
10191038
* @param {string} [id] - The session ID (optional). If omitted, the currently selected session history ID will be used.
10201039
* @returns {boolean} `true` if the entry was successfully replaced, `false` if the index is invalid or the entry does not exist.
10211040
*/
@@ -1052,7 +1071,7 @@ class TinyAiInstance extends EventEmitter {
10521071
* Retrieve the data of the last entry in the session history.
10531072
*
10541073
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
1055-
* @returns {Object|null} The data of the last entry in the session history, or `null` if the history is empty or invalid.
1074+
* @returns {AIContentData|null} The data of the last entry in the session history, or `null` if the history is empty or invalid.
10561075
*/
10571076
getLastIndexData(id) {
10581077
const history = this.getData(id);
@@ -1077,7 +1096,7 @@ class TinyAiInstance extends EventEmitter {
10771096
* Retrieve the first entry in the session history.
10781097
*
10791098
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
1080-
* @returns {Object|null} The first entry of the session history, or `null` if no entry exists.
1099+
* @returns {AIContentData|null} The first entry of the session history, or `null` if no entry exists.
10811100
*/
10821101
getFirstIndexData(id) {
10831102
const history = this.getData(id);
@@ -1092,8 +1111,8 @@ class TinyAiInstance extends EventEmitter {
10921111
* **Note**: The `tokenData` parameter is optional and can be used to track token-related data associated with the new entry.
10931112
* This may include token counts, but this script does not manage token counting automatically. Developers must implement token management separately if necessary.
10941113
*
1095-
* @param {Object} data - The data to be added to the session history.
1096-
* @param {Object} [tokenData={count: null}] - Optional token-related data to be associated with the new entry. Defaults to `{count: null}`.
1114+
* @param {AIContentData} data - The data to be added to the session history.
1115+
* @param {TokenCount} [tokenData={count: null}] - Optional token-related data to be associated with the new entry. Defaults to `{count: null}`.
10971116
* @param {string} [id] - The session history ID. If omitted, the currently selected session ID will be used.
10981117
* @returns {number} The new ID of the added data entry.
10991118
* @throws {Error} If the provided session ID is invalid or the session ID does not exist in history.
@@ -1265,7 +1284,7 @@ class TinyAiInstance extends EventEmitter {
12651284
* Retrieves file data from the selected session history.
12661285
*
12671286
* @param {string} [id] - The session ID. If omitted, the currently selected session history ID will be used.
1268-
* @returns {Object|null} The file data, including MIME type and encoded content, or null if no file data is found.
1287+
* @returns {{data: string, mime: string}|null} The file data, including MIME type and encoded content, or null if no file data is found.
12691288
* @throws {Error} If no valid session history ID is found.
12701289
*/
12711290
getFileData(id) {
@@ -1367,7 +1386,7 @@ class TinyAiInstance extends EventEmitter {
13671386
*
13681387
* @param {string} id - The session ID for the new data session.
13691388
* @param {boolean} [selected=false] - A flag to indicate whether this session should be selected as the active session.
1370-
* @returns {Object} The newly created session data, which includes an empty data array, an empty IDs array, and null values for system instruction and model.
1389+
* @returns {SessionData} The newly created session data, which includes an empty data array, an empty IDs array, and null values for system instruction and model.
13711390
*/
13721391
startDataId(id, selected = false) {
13731392
this.history[id] = {

0 commit comments

Comments
 (0)