# SmartSession

Namespace: **Wisej.AI**

Assembly: **Wisej.AI** (3.5.0.0)

* [SmartObject](/ai/components/api/wisej.ai.smartobject.md)
  * [SmartSession](/ai/components/api/smartsession.md)

Represents a session that manages interactions with a smart hub and endpoint, handling prompts, messages, and tools within the session context.

{% tabs %}
{% tab title="C#" %}

```csharp
public class SmartSession : SmartObject
```

{% endtab %}

{% tab title="VB.NET" %}

```visual-basic
Public Class SmartSession
    Inherits SmartObject
```

{% endtab %}
{% endtabs %}

The [SmartSession](/ai/components/api/smartsession.md) class is responsible for managing the lifecycle of a session, including initialization, message handling, and tool usage. It supports asynchronous operations for asking questions and processing responses, and it can handle context overflow through truncation or summarization strategies.

## Constructors

### ![](/files/ptrKjmmRoQB76pvrIqh0) SmartSession(hub, systemPrompt)

Initializes a new instance of the [SmartSession](/ai/components/api/smartsession.md) class with the specified hub and optional system prompt.

| Name             | Type                                             | Description                                                  |
| ---------------- | ------------------------------------------------ | ------------------------------------------------------------ |
| **hub**          | [SmartHub](/ai/components/api/smarthub.md)       | The smart hub associated with the session.                   |
| **systemPrompt** | [SmartPrompt](/ai/components/api/smartprompt.md) | The optional system prompt for the session. Default is null. |

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the hub is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) SmartSession(endpoint, systemPrompt)

Initializes a new instance of the [SmartSession](/ai/components/api/smartsession.md) class with the specified endpoint and optional system prompt.

| Name             | Type                                                 | Description                                                  |
| ---------------- | ---------------------------------------------------- | ------------------------------------------------------------ |
| **endpoint**     | [SmartEndpoint](/ai/components/api/smartendpoint.md) | The smart endpoint associated with the session.              |
| **systemPrompt** | [SmartPrompt](/ai/components/api/smartprompt.md)     | The optional system prompt for the session. Default is null. |

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the endpoint is null.

## Properties

### ![](/files/ptrKjmmRoQB76pvrIqh0) Endpoint

[SmartEndpoint](/ai/components/api/smartendpoint.md): Gets the smart endpoint associated with the session.

### ![](/files/ptrKjmmRoQB76pvrIqh0) HasModelOptions

[Boolean](https://docs.microsoft.com/dotnet/api/system.boolean): Gets a value indicating whether the session has model options.

### ![](/files/ptrKjmmRoQB76pvrIqh0) Hub

[SmartHub](/ai/components/api/smarthub.md): Gets the smart hub associated with the session.

### ![](/files/ptrKjmmRoQB76pvrIqh0) IsDisposed

[Boolean](https://docs.microsoft.com/dotnet/api/system.boolean): Gets a value indicating whether the session has been disposed.

### ![](/files/ptrKjmmRoQB76pvrIqh0) Messages

[MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md): Gets the collection of messages in the session.

### ![](/files/ptrKjmmRoQB76pvrIqh0) ModelOptions

[Object](https://docs.microsoft.com/dotnet/api/system.object): Gets or sets the model options for the session.

### ![](/files/ptrKjmmRoQB76pvrIqh0) Response

[Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md): Gets the last response message from the assistant.

### ![](/files/ptrKjmmRoQB76pvrIqh0) SystemPrompt

[SmartPrompt](/ai/components/api/smartprompt.md): Gets the system prompt for the session.

## Methods

### ![](/files/ptrKjmmRoQB76pvrIqh0) AskAsync(question, image)

Asynchronously asks a question and returns the response message.

| Parameter                                  | Type                                                                | Description                                                      |
| ------------------------------------------ | ------------------------------------------------------------------- | ---------------------------------------------------------------- |
| **question**                               | [String](https://docs.microsoft.com/dotnet/api/system.string)       | The question to ask.                                             |
| **image** ![](/files/52BqKf9SltWQ97FbQSBF) | [Image](https://docs.microsoft.com/dotnet/api/system.drawing.image) | An optional image associated with the question. Default is null. |

**Returns:** [Task\<Message>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). A task representing the asynchronous operation, with a [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) as the result.

**Throws:**

* [ObjectDisposedException](https://docs.microsoft.com/dotnet/api/system.objectdisposedexception)\
  Thrown when the session is disposed.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the question is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) AskAsync(message)

Asynchronously asks a question using a message and returns the response message.

| Parameter   | Type                                                                        | Description                          |
| ----------- | --------------------------------------------------------------------------- | ------------------------------------ |
| **message** | [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) | The message containing the question. |

**Returns:** [Task\<Message>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). A task representing the asynchronous operation, with a [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) as the result.

**Throws:**

* [ObjectDisposedException](https://docs.microsoft.com/dotnet/api/system.objectdisposedexception)\
  Thrown when the session is disposed.
* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the message is null.

### ![](/files/KL0Ik37djZRr8a07Wopd) AskAsyncCore(question)

Asynchronously sends a question to the AI and processes the response.

| Parameter    | Type                                                                        | Description                                               |
| ------------ | --------------------------------------------------------------------------- | --------------------------------------------------------- |
| **question** | [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) | The message containing the question to be sent to the AI. |

**Returns:** [Task\<Message>](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task-1). A task representing the asynchronous operation, with a [Message](/ai/components/api/smartsession/wisej.ai.smartsession.message.md) as the result containing the AI's response.

This method handles the communication with the AI, including managing the session's message context, handling tool calls, and processing AI responses. It ensures that the session is not disposed and that the question is not null before proceeding. The method also manages the context window size and handles exceptions such as token limit exceedance.

```csharp

var response = await AskAsyncCore(new Message { Text = "What is the weather today?" });
Console.WriteLine(response.Text);

```

### ![](/files/KL0Ik37djZRr8a07Wopd) ConvertParameter(value)

Raises the [ConvertParameter](#convertparameter) event.

| Parameter | Type                                                          | Description |
| --------- | ------------------------------------------------------------- | ----------- |
| **value** | [Object](https://docs.microsoft.com/dotnet/api/system.object) |             |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string).

### ![](/files/ptrKjmmRoQB76pvrIqh0) Dispose()

Releases all resources used by the [SmartSession](/ai/components/api/smartsession.md).

### ![](/files/KL0Ik37djZRr8a07Wopd) GetParameterValue(parameter)

Gets the value of a parameter.

| Parameter     | Type                                                                          | Description                         |
| ------------- | ----------------------------------------------------------------------------- | ----------------------------------- |
| **parameter** | [Parameter](/ai/components/api/smartprompt/wisej.ai.smartprompt.parameter.md) | The parameter to get the value for. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). The value of the parameter as a string.

### ![](/files/KL0Ik37djZRr8a07Wopd) IsTerminated()

**Returns:** [Boolean](https://docs.microsoft.com/dotnet/api/system.boolean).

### ![](/files/ptrKjmmRoQB76pvrIqh0) PrepareSessionMessages(messages)

Prepares session messages by replacing parameters in the system and user messages.

| Parameter    | Type                                                                                            | Description                            |
| ------------ | ----------------------------------------------------------------------------------------------- | -------------------------------------- |
| **messages** | [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md) | The collection of messages to prepare. |

### ![](/files/ptrKjmmRoQB76pvrIqh0) RemoveTool(tool)

Removes a tool from the smart session.

| Parameter | Type                                                              | Description         |
| --------- | ----------------------------------------------------------------- | ------------------- |
| **tool**  | [Delegate](https://docs.microsoft.com/dotnet/api/system.delegate) | The tool to remove. |

**Returns:** [SmartSession](/ai/components/api/smartsession.md). The current [SmartSession](/ai/components/api/smartsession.md) instance.

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the tool is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) ReplaceParameters(prompt)

Replaces parameters in the given prompt with their values.

| Parameter  | Type                                                          | Description                                  |
| ---------- | ------------------------------------------------------------- | -------------------------------------------- |
| **prompt** | [String](https://docs.microsoft.com/dotnet/api/system.string) | The prompt containing parameters to replace. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). The prompt with parameters replaced by their values.

### ![](/files/KL0Ik37djZRr8a07Wopd) TrimMessagesAsync(messages)

Trims messages in the session based on the specified truncation strategy.

| Parameter    | Type                                                                                            | Description                         |
| ------------ | ----------------------------------------------------------------------------------------------- | ----------------------------------- |
| **messages** | [MessageCollection](/ai/components/api/smartsession/wisej.ai.smartsession.messagecollection.md) | The collection of messages to trim. |

**Returns:** [Task](https://docs.microsoft.com/dotnet/api/system.threading.tasks.task).

### ![](/files/ptrKjmmRoQB76pvrIqh0) UseTool(tool)

Registers a tool using a delegate.

| Parameter | Type                                                              | Description                                     |
| --------- | ----------------------------------------------------------------- | ----------------------------------------------- |
| **tool**  | [Delegate](https://docs.microsoft.com/dotnet/api/system.delegate) | The delegate representing the tool to register. |

**Returns:** [SmartSession](/ai/components/api/smartsession.md). The current [SmartSession](/ai/components/api/smartsession.md) instance.

**Throws:**

* [ArgumentNullException](https://docs.microsoft.com/dotnet/api/system.argumentnullexception)\
  Thrown when the tool is null.

### ![](/files/ptrKjmmRoQB76pvrIqh0) UseTools(target)

Registers the tools declared on the target.

| Parameter  | Type                                                          | Description                                     |
| ---------- | ------------------------------------------------------------- | ----------------------------------------------- |
| **target** | [Object](https://docs.microsoft.com/dotnet/api/system.object) | The target object containing tools to register. |

**Returns:** [SmartSession](/ai/components/api/smartsession.md). The current [SmartSession](/ai/components/api/smartsession.md) instance.

## Inherited By

| Name                                                                        | Description |
| --------------------------------------------------------------------------- | ----------- |
| [SmartRealtimeSession](/ai/components/api/wisej.ai.smartrealtimesession.md) |             |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/ai/components/api/smartsession.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
