@@ -2,6 +2,49 @@ import objHash from 'object-hash';
22import { EventEmitter } from 'events' ;
33import { isJsonObject , objType } from 'tiny-essentials' ;
44import { Base64 } from 'js-base64' ;
5+
6+ /**
7+ * @typedef {Object } SessionDataContent
8+ * @property {AIContentData[] } data
9+ * @property {string[] } ids
10+ * @property {{ data: Array<TokenCount>; [key: string]: * } } tokens
11+ * @property {{ data: Array<string>; [key: string]: * } } hash
12+ * @property {string|null } systemInstruction
13+ * @property {{ name: string; type: string; }[] } [customList]
14+ * @property {string|null } model
15+ *
16+ */
17+
18+ /**
19+ * @typedef {Record<string, any> & SessionDataContent } SessionData
20+ */
21+
22+ /**
23+ * @typedef {Object } AiModel
24+ * @property {any } _response
25+ * @property {number } index
26+ * @property {string|null } name
27+ * @property {string|null } id
28+ * @property {string|null } displayName
29+ * @property {string|null } version
30+ * @property {string|null } description
31+ * @property {number|null } inputTokenLimit
32+ * @property {number|null } outputTokenLimit
33+ * @property {number|null } temperature
34+ * @property {number|null } maxTemperature
35+ * @property {number|null } topP
36+ * @property {number|null } topK
37+ * @property {string[] } [supportedGenerationMethods]
38+ */
39+
40+ /**
41+ * @typedef {Object } AiCategory
42+ * @property {string } category
43+ * @property {string } displayName
44+ * @property {number } index
45+ * @property {AiModel[] } data
46+ */
47+
548/**
649 * Tiny AI Server Communication API
750 * -----------------------------
@@ -231,17 +274,6 @@ class TinyAiInstance {
231274 * @property {string|number|undefined } [finishReason]
232275 */
233276
234- /**
235- * @typedef {Record<string, any> & {
236- * data: Array<AIContentData>,
237- * ids: Array<string>,
238- * tokens: { data: Array<TokenCount>; [key: string]: * },
239- * hash: { data: Array<string>; [key: string]: * },
240- * systemInstruction: string|null,
241- * model: string|null
242- * }} SessionData
243- */
244-
245277 /**
246278 * @typedef {{ count: number|null, hide?: boolean } } TokenCount
247279 */
@@ -256,7 +288,7 @@ class TinyAiInstance {
256288 /** @type {Record<string|number, string|{ text: string, hide?: boolean }> } */ _errorCode = { } ;
257289 /** @type {string|null } */ _nextModelsPageToken = null ;
258290
259- /** @type {Array<*> } */ models = [ ] ;
291+ /** @type {(AiModel|AiCategory)[] } */ models = [ ] ;
260292 /** @type {Object.<string, SessionData> } */ history = { } ;
261293 _isSingle = false ;
262294
@@ -348,7 +380,11 @@ class TinyAiInstance {
348380 const props = history . customList . find ( ( /** @type {* } */ item ) => item . name === name ) ;
349381 if ( ! props || typeof props . type !== 'string' || typeof props . name !== 'string' ) {
350382 if ( typeof history [ name ] === 'undefined' )
351- history . customList . push ( { name, type : objType ( value ) } ) ;
383+ history . customList . push ( {
384+ name,
385+ // @ts -ignore
386+ type : objType ( value ) ,
387+ } ) ;
352388 else throw new Error ( 'This value name is already being used!' ) ;
353389 } else if ( props . type !== objType ( value ) )
354390 throw new Error (
@@ -429,8 +465,7 @@ class TinyAiInstance {
429465 eraseCustomValue ( name , id ) {
430466 this . resetCustomValue ( name , id ) ;
431467 const history = this . getData ( id ) ;
432- if ( history ) {
433- // @ts -ignore
468+ if ( history && history . customList ) {
434469 const index = history . customList . findIndex ( ( item ) => item . name === name ) ;
435470 if ( index > - 1 ) history . customList . splice ( index , 1 ) ;
436471 return ;
@@ -778,7 +813,7 @@ class TinyAiInstance {
778813 /**
779814 * Get the list of models for the AI session.
780815 *
781- * @returns {Array<*> } The list of models.
816+ * @returns {(AiModel|AiCategory)[] } The list of models.
782817 */
783818 getModelsList ( ) {
784819 return Array . isArray ( this . models ) ? this . models : [ ] ;
@@ -788,13 +823,16 @@ class TinyAiInstance {
788823 * Get model data from the list of models.
789824 *
790825 * @param {string } id - The model data id to search for in the models list.
791- * @returns {Record<string, any> |null } The model data if found, otherwise null.
826+ * @returns {AiModel |null } The model data if found, otherwise null.
792827 */
793828 getModelData ( id ) {
829+ // @ts -ignore
794830 const model = this . models . find ( ( item ) => item . id === id ) ;
831+ // @ts -ignore
795832 if ( model ) return model ;
796833 else {
797834 for ( const index in this . models ) {
835+ // @ts -ignore
798836 if ( this . models [ index ] . category ) {
799837 // @ts -ignore
800838 const modelCategory = this . models [ index ] . data . find ( ( item ) => item . id === id ) ;
@@ -842,9 +880,9 @@ class TinyAiInstance {
842880 */
843881 _insertNewModel ( model ) {
844882 if ( ! isJsonObject ( model ) ) throw new Error ( 'Model data must be a valid object.' ) ;
845-
883+ // @ts -ignore
846884 if ( this . models . findIndex ( ( item ) => item . id === model . id ) < 0 ) {
847- /** @type {Record<string, any> } */
885+ /** @type {AiModel } */
848886 const newData = {
849887 _response : model . _response ,
850888 index : typeof model . index === 'number' ? model . index : 9999999 ,
@@ -879,6 +917,7 @@ class TinyAiInstance {
879917 typeof model . category . index === 'number'
880918 ) {
881919 // Check category
920+ /** @type {AiCategory|null } */
882921 // @ts -ignore
883922 let category = this . models . find ( ( item ) => item . category === model . category . id ) ;
884923 // Insert new category
@@ -963,7 +1002,6 @@ class TinyAiInstance {
9631002 const errData = this . _errorCode [ code ] ;
9641003 if ( errData ) {
9651004 if ( typeof errData === 'string' ) return { text : errData } ;
966- // @ts -ignore
9671005 else if ( isJsonObject ( errData ) && typeof errData . text === 'string' ) return errData ;
9681006 }
9691007 }
@@ -1455,7 +1493,6 @@ class TinyAiInstance {
14551493 this . history [ selectedId ] . file = {
14561494 mime,
14571495 data,
1458- // @ts -ignore
14591496 base64 : ! isBase64 ? Base64 . encode ( data ) : data ,
14601497 } ;
14611498 hash = objHash ( this . history [ selectedId ] . file ) ;
0 commit comments