Creates an instance of the TinyAiInstance class.
Initializes internal variables and sets up the base configuration for handling AI models, session history, and content generation. This class can operate in single-session mode or support multiple sessions.
| Name | Type | Description |
|---|---|---|
isSingle |
boolean |
If true, configures the instance to manage only a single session. Default is false. |
- Sets up:
- API key and error handling state
- Empty history object
- Placeholder for AI models and pagination token
- Internal generator and model fetcher function references
- Defines a method
#_insertIntoHistory(id, data)to update history entries by ID - Defines
_partTypeswith handlers for:text: Accepts valid stringsinlineData: Accepts objects withmime_typeanddataas strings
- In single-session mode (
isSingle === true):- Automatically calls
startDataId('main', true) - Disables methods
startDataId,stopDataId, andselectDataIdafter initialization
- Automatically calls
This method sets a session history ID as the active session. It can also clear the current selection by passing null.
To select or deselect a session ID from the session history for further operations, such as continuing a conversation or displaying previous interactions.
| Name | Type | Description |
|---|---|---|
id |
string | null |
The session history ID to select, or null to deselect it. |
-
If an
idis provided:- Checks if
this.history[id]exists. - If it exists, assigns it to
this.#_selectedHistoryand emits the'selectDataId'event. - Returns
trueon success,falseif the ID is invalid or not found.
- Checks if
-
If
idisnull:- Clears the currently selected session by setting
this.#_selectedHistorytonull. - Emits
'selectDataId'withnull. - Returns
true.
- Clears the currently selected session by setting
| Type | Description |
|---|---|
boolean |
true if the selection or deselection succeeded, false if the ID was invalid. |
// Select an existing session
tinyAi.selectDataId("session_123"); // true
// Try selecting a non-existing session
tinyAi.selectDataId("invalid_id"); // false
// Deselect the current session
tinyAi.selectDataId(null); // trueThis method helps manage session state within the history, allowing components to react to session changes through the emitted event.
This method returns the session history ID to be used in operations. It can either return the provided id (if valid) or the currently selected one.
To retrieve the appropriate session ID, especially when working in multi-session mode. It checks if the passed id is usable or if it should fall back to the currently selected session.
| Name | Type | Optional | Description |
|---|---|---|---|
id |
string |
Yes | An optional session ID. If omitted or ignored, the default selected ID is returned. |
- If an
idis provided and the instance is not in single-session mode (!this._isSingle), it returns the providedid. - Otherwise, it returns the current selected session ID (
this.#_selectedHistory).
| Type | Description |
|---|---|
string | null |
The chosen session ID or null if none is currently selected. |
// In multi-session mode:
tinyAi.getId("session_123"); // returns "session_123"
// In single-session mode or if no ID provided:
tinyAi.getId(); // returns currently selected session ID or nullThis function simplifies conditional logic elsewhere in the code by abstracting how the correct session ID should be determined, particularly useful when toggling between single and multi-session behavior.