Skip to content
AstrBot
Main Navigation HomeBlogRoadmapHTTP API

English

简体中文

English

简体中文

Toggle dark mode

Intro & DeployMessaging PlatformsAI IntegrationUsageDevelopment
Sidebar Navigation

Introduction

What is AstrBot

Community

FAQ

Deployment

Script Deployment

Desktop Client

One-click Launcher

Docker

Kubernetes

BT Panel

1Panel

Source Deployment

Other Deployments

CasaOS

Compshare GPU

Community-provided Deployment

Support Us

Messaging Platforms

Quick Start

QQ Official Bot

Websockets

Webhook

OneBot v11

WeCom Application

WeCom AI Bot

WeChat Official Account

Personal WeChat

Lark

DingTalk

Telegram

LINE

Slack

Mattermost

Misskey

Discord

KOOK

Satori

Connect Satori

Using server-satori

Community-provided

Matrix

VoceChat

AI Integration

✨ Model Providers

MiraRouter

NewAPI

ShengSuanYun

AIHubMix

PPIO Cloud

SiliconFlow

TokenPony

302.AI

DeepSeek TTS

DeepSeek STT

Ollama

LMStudio

⚙️ Agent Runners

Built-in Agent Runner

Dify

Coze

Alibaba Bailian

DeerFlow

Usage

WebUI

AstrBot CLI

Built-in Commands

Plugins

Plugins

MCP

Skills

AstrBot Built-in AI

Agent Sandbox

SubAgent Orchestration

Proactive Capabilities

Web Search

SerpApi

DeepSeek Search API

Knowledge Base

Context Compression

Custom Rules

Agent Execution Mode

Unified Webhook Mode

Development

Plugin Development

🌠 Getting Started

Minimal Example

Listen to Message Events

Send Messages

Plugin Configuration

Plugin Views

Overview

Standalone plugin page

Interactive message component

Workbench page

Shared Bridge API

Backend Web APIs

Plugin Internationalization

AI

Models

Agents

Tools

Sessions and Conversations

Other APIs

Storage

HTML to Image

Session Control

Publish Plugin

Plugin Market Specification

Versions

2026-11-25

2026-06-27

Platform Adapter Integration

AstrBot HTTP API

API Scope–Endpoint Reference

AstrBot Configuration File

Others

Diagnostics

Self-hosted HTML to Image

Open Source Summer

OSPP 2025

On this page

Sessions and Conversations ​

A session is identified by the message source, event.unified_msg_origin (UMO). A conversation is identified by cid and stores a model message history. A session can create and switch between conversations.

To wait for a user's next message, see Session Control. This page covers reading and writing conversation history.

Conversation Manager ​

Use self.context.conversations to create, read, update, switch, and delete conversations. Conversation.history is a JSON string containing model messages.

_no_save excludes a message or content part from future model context, but does not prevent its raw content from being stored.

TIP

Starting with v4.30.0, the conversation API changes as follows:

  • self.context.conversation_manager remains a compatibility alias for self.context.conversations.
  • Replace event.conversation_events and get_conversation_events() with append_message() or append_plugin_event().
  • Replace update_conversation_title() and update_conversation_persona_id() with update_conversation(..., title=...) and update_conversation(..., persona_id=...).
  • Replace platform history links using llm_checkpoint_id with turn_id and context_event_id.

Appending Model Messages ​

py
conversations = self.context.conversations
message_id = await conversations.append_message(
    "The user prefers concise answers.",
    cid=target_cid,
)

content accepts text or a list of AstrBot ContentPart objects:

py
from astrbot.core.agent.message import ImageURLPart, TextPart

await conversations.append_message(
    [
        TextPart(text="Describe this image."),
        ImageURLPart(
            image_url=ImageURLPart.ImageURL(url="https://example.com/image.png")
        ),
    ],
    cid=target_cid,
)

Messages are stored with role="user" and included in future context. They do not change a running agent's context. The returned string is the saved message ID; pass message_id to identify a retry of the same submission.

Appending Plugin Events ​

py
event_id = await conversations.append_plugin_event(
    event_type="my_plugin.retrieval_completed",
    data={"document_ids": ["document-1"]},
    cid=target_cid,
)

Plugin events are stored with the conversation but do not enter model context. Use a plugin-prefixed event_type and a JSON-serializable dict for data. The returned string is the saved event ID; pass event_id to identify a retry of the same submission.

For mutable state that does not need an event history, use plugin KV storage. Include cid in the key for per-conversation state.

Both append methods require cid for an existing conversation and raise ValueError if it is invalid or missing from storage. Neither starts an agent turn.

To change the current execution's context, edit the relevant hook's ProviderRequest.contexts or runtime messages. Direct conversation writes, including update_conversation() and add_message_pair(), do not update a running agent's context and can cause ConversationConflictError if its stored history changes concurrently.

Getting the Current LLM Conversation History for a Session ​

py
# Before AstrBot v4.30.0:
# from astrbot.core.conversation_mgr import Conversation
from astrbot.api.provider import Conversation

uid = event.unified_msg_origin
conv_mgr = self.context.conversations
curr_cid = await conv_mgr.get_curr_conversation_id(uid)
conversation = await conv_mgr.get_conversation(uid, curr_cid)  # Conversation
Conversation Type Definition
py
@dataclass
class Conversation:
    """The conversation entity representing a chat session."""

    platform_id: str
    """The platform ID in AstrBot"""
    user_id: str
    """The user ID associated with the conversation."""
    cid: str
    """The conversation ID, in UUID format."""
    history: str = ""
    """The conversation history as a string."""
    title: str | None = ""
    """The title of the conversation. For now, it's only used in WebChat."""
    persona_id: str | None = ""
    """The persona ID associated with the conversation."""
    created_at: int = 0
    """The timestamp when the conversation was created."""
    updated_at: int = 0
    """The timestamp when the conversation was last updated."""

Quickly Adding LLM Records to a Conversation add_message_pair ​

py
# Before AstrBot v4.30.0:
# from astrbot.core.agent.message import AssistantMessageSegment, UserMessageSegment, TextPart
from astrbot.api.agents import (
    AssistantMessageSegment,
    UserMessageSegment,
    TextPart,
)

conv_mgr = self.context.conversations
provider_id = await self.context.get_current_chat_provider_id(event.unified_msg_origin)
curr_cid = await conv_mgr.get_curr_conversation_id(event.unified_msg_origin)
user_msg = UserMessageSegment(content=[TextPart(text="hi")])
llm_resp = await self.context.llm_generate(
    chat_provider_id=provider_id,  # Chat model ID
    contexts=[
        user_msg
    ],  # When prompt is not specified, contexts is used as input; if both prompt and contexts are provided, prompt is appended to the end of the LLM input
)
await conv_mgr.add_message_pair(
    cid=curr_cid,
    user_message=user_msg,
    assistant_message=AssistantMessageSegment(
        content=[TextPart(text=llm_resp.completion_text)]
    ),
)

WARNING

The example above uses unified_msg_origin as the conversation history storage key. When Isolate Conversation (platform_settings.unique_session) is enabled, the unified_msg_origin of group messages changes (only on platforms that support isolation), so the same group resolves to a different conversation depending on the setting. Use event.get_group_id() when you need to target a group; see Handling Message Events.

Edit this page on GitHub

Last updated:

Pager
PreviousTools
NextOther APIs

Deployed on Rainyun Logo