//clika-runtime/io.clika.modelverse/ChatSession
ChatSession
[common]
class ChatSession
A conversation over a GenerativeModel: the turns so far (messages), a send that appends the user turn and the reply, a respond that replies to the turns as they stand, the tools channel (tools, toolCalls, addToolResult) and the last reply's report. The whole conversation goes to the model on every turn, rendered through the checkpoint's chat template; the model's cache serves the earlier turns (GenerationReport.cachedPromptTokens). One session speaks at a time, on the model's own worker thread; a second session over the same model waits its turn. The session owns no weights: close ends it and leaves the model open. Built by GenerativeModel.chatSession.
Tools: the session's tools are offered on every turn, and a turn may offer its own list instead. A reply that calls tools ends with FinishReason.TOOL_CALLS; its calls are toolCalls (and ride the assistant turn as ChatMessage.toolCalls, each with an id, call_<n> when the model's format wrote none), and addToolResult appends a tool turn answering one, after which respond continues. A model that takes no tools (Capabilities.supportsTools false) refuses an offer before any turn is appended.
Properties
| Name | Summary |
|---|---|
| messages | [common] val messages: List<ChatMessage> The conversation so far, the system turn first when one was given; a copy. |
| model | [common] val model: GenerativeModel The model the session speaks through. |
| report | [common] var report: GenerationReport? The last reply's report; null before the first reply ends and after reset. |
| toolCalls | [common] var toolCalls: List<ToolCall> The calls the last reply made, ids filled in; empty when it made none, or before the first reply. |
| tools | [common] val tools: List<ToolDef> The tools every turn offers unless the turn names its own; empty offers none. |
Functions
| Name | Summary |
|---|---|
| addToolResult | [common] fun addToolResult(callId: String, content: String, name: String = "") Append a tool's result as a tool turn answering the call callId (a ToolCall.id from toolCalls), with the tool's name when given; a respond then continues the conversation. |
| close | [common] open fun close() End the session; the model stays open, and a later send or respond refuses. |
| reset | [common] fun reset(keepSystem: Boolean = true) Forget the conversation, the last report and its calls; keepSystem keeps the system turn. |
| respond | [common] fun respond(config: GenerationConfig = model.capabilities.defaults, tools: List<ToolDef>? = null): String Reply to the conversation as it stands, blocking: the last turn must be a user turn or a tool result. The reply's visible text is appended as the assistant turn and answered. [common] fun respond(listener: GenerationListener, config: GenerationConfig = model.capabilities.defaults, tools: List<ToolDef>? = null): GenerationHandle Reply to the conversation as it stands, streamed through listener; the assistant turn is appended when GenerationListener.onDone arrives. |
| send | [common] fun send(content: String, config: GenerationConfig = model.capabilities.defaults, images: List<ByteArray> = emptyList(), tools: List<ToolDef>? = null): String Send one user turn and block for the reply: content with images is appended, the conversation is sent under config (the model's defaults when omitted) with tools offered (the session's when null), and the reply's visible text is appended as the assistant turn and answered. A failure throws ModelverseException with the library's message and leaves the user turn in place, so respond may try again; an offer to a model that takes no tools throws IllegalArgumentException and appends nothing. [common] fun send(content: String, listener: GenerationListener, config: GenerationConfig = model.capabilities.defaults, images: List<ByteArray> = emptyList(), tools: List<ToolDef>? = null): GenerationHandle Send one user turn and stream the reply: listener hears the pieces on the model's worker thread (GenerationListener), each call the reply made through GenerationListener.onToolCall, and the assistant turn is appended when GenerationListener.onDone arrives, with the text streamed so far on a canceled reply. The returned handle cancels. |