AsyncLocalStorage
that allows you to access the session throughout your application code. This is particularly useful for tracking LLM interactions and tool calls.
session.flush()
method to ensure all events are sent to the Mindlytics platform before the function execution ends.
yourData
that you supply will be sent to your handlers as a second argument. You can use this to gain access to contextual information if needed.
MLSDK_API_KEY
and MLSDK_PROJECT_ID
are checked. You can obtain these values from your account on the Mindlytics SaaS portal. The full set of options for the Client
constructor are:
baseUrl
points to the Mindlytics production server address. This can be changed by specifying baseUrl
or by setting the environment variable MLSDK_SERVER_BASE
.
Returns:
An instance of the Mindlytics client object. This is used primarily to create sessions, but has two other methods for identifying users and managing aliasing outside of normal sessions.
deviceId
if you know it. If the device id has been used to create sessions (see below) then all sessions and associated events with this device id now belong to this user.
Returns:
A Mindlytics user object.
id
and all previous ids become aliases for this user and can be used to refer to this user in the future.
Returns:
A Mindlytics user object.
Session
object. The sessionId
is required and can be a new session id or an existing session id. Session ids should be globally unique with respect to your api key and project id. You need a session to send events. To create a session you must supply either a userId
or a deviceId
. If you do not know the user id at the time a session is started, then you must at least supply a device id which should represent the unique device (eg. mobile phone, browser, etc) the user is coming from. You may also supply a conversationId
which will be attached to events sent using this session instance. This is optional, since you can attach it to individual events.
attributes
is optional and if specified, the user-defined attributes are associated with the session.
properties
are optional. Supported property types are string, number and boolean. Complex data types are not supported.
attributes
is optional and if specified, the user-defined attributes are associated with the conversation.
session.end()
will automatically close any open conversations, but you can call it if you like. The attributes
is optional and if specified, the user-defined attributes are merged with any existing conversation attributes.
A note on conversation ids. When you callclient.createSession()
you can pass an optionalconversationId
and if you do, this value will be automatically applied as the “conversation_id” to all methods that accept a “conversation_id”. If you do not supply aconversationId
toclient.createSession()
then you must supply it to the events that are related to conversations. Supplying to individual events could allow you to support multiple conversations within a single session.
assistant_id
might be useful in a multi-agent system to keep track agent participation. model
/prompt_tokens
/completion_tokens
if specified will cause the Mindlytics service to calculate the cost of this turn, and accumulate the total cost of a conversation. It uses an online database of popular models to retrieve the most current cost data. Alternatively you can specify cost
directly if you know it or the model you are using is not a common LLM. And you can add your own custom properties.