7f683a8aed
- 添加快速开始页面,包含安装、配置、启动指南 - 添加核心功能文档: Agent 系统、工具系统、MCP 集成 - 添加 API 参考文档: REST API、WebSocket、SDK 使用 - 更新侧边栏导航,重新组织文档分类 - 更新 .gitignore 允许 website docs 页面
92 lines
3.5 KiB
Plaintext
92 lines
3.5 KiB
Plaintext
---
|
|
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
|
---
|
|
|
|
<DocsLayout title="会话管理" description="会话存储、消息管理和上下文压缩">
|
|
<h1>会话管理 (Session Management)</h1>
|
|
|
|
<p class="text-green-400 font-medium">✅ 已完成 - 三层存储架构已实现</p>
|
|
|
|
<p>完整的会话管理系统,包括消息存储、自动保存、上下文压缩等功能。代码位于 <code>packages/core/src/session/</code> 和 <code>packages/core/src/context/</code>。</p>
|
|
|
|
<h2>已实现功能</h2>
|
|
<ul>
|
|
<li>✅ <strong>三层存储架构</strong> - Message → Part → Storage 分层设计</li>
|
|
<li>✅ <strong>自动保存</strong> - 会话自动持久化到文件</li>
|
|
<li>✅ <strong>消息转换</strong> - API 格式适配和消息格式转换</li>
|
|
<li>✅ <strong>上下文压缩</strong> - Token 计数和消息压缩管理</li>
|
|
<li>✅ <strong>消息片段系统</strong> - 支持 Text、Tool、Reasoning、File 等多种类型</li>
|
|
</ul>
|
|
|
|
<h2>设计方案</h2>
|
|
|
|
<h3>类型定义</h3>
|
|
<pre><code class="language-typescript">export interface ChatMessage {
|
|
id: string;
|
|
timestamp: Date;
|
|
role: 'user' | 'assistant' | 'system';
|
|
content: string;
|
|
metadata?: {
|
|
model?: string;
|
|
tokens?: number;
|
|
toolCalls?: ToolCall[];
|
|
editedFiles?: string[];
|
|
};
|
|
}
|
|
|
|
export interface ChatSession {
|
|
id: string;
|
|
startTime: Date;
|
|
endTime?: Date;
|
|
messages: ChatMessage[];
|
|
workdir: string;
|
|
summary?: string;
|
|
}</code></pre>
|
|
|
|
<h3>历史管理器</h3>
|
|
<pre><code class="language-typescript">export class ChatHistoryManager {
|
|
startSession(workdir: string): ChatSession;
|
|
addMessage(message: ChatMessage): void;
|
|
getContext(tokenBudget: number): ChatMessage[];
|
|
search(query: string): SearchResult[];
|
|
restoreSession(sessionId: string): ChatSession | null;
|
|
save(): Promise<void>;
|
|
load(): Promise<void>;
|
|
}</code></pre>
|
|
|
|
<h2>配置示例</h2>
|
|
<pre><code class="language-yaml"># .ai-assistant.yml
|
|
history:
|
|
enabled: true
|
|
saveDir: .ai-assistant/history
|
|
maxSessions: 100
|
|
maxMessagesPerSession: 1000
|
|
autoSave: true
|
|
saveInterval: 5000
|
|
exportFormat: markdown
|
|
contextTokenBudget: 50000</code></pre>
|
|
|
|
<h2>模块结构</h2>
|
|
<table>
|
|
<thead>
|
|
<tr><th>模块</th><th>位置</th><th>功能</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr><td>SessionManager</td><td><code>session/manager.ts</code></td><td>会话管理,消息存储,项目管理</td></tr>
|
|
<tr><td>SessionStorage</td><td><code>session/storage/</code></td><td>三层存储结构,持久化存储</td></tr>
|
|
<tr><td>MessageConverter</td><td><code>session/message-converter.ts</code></td><td>消息格式转换,API 格式适配</td></tr>
|
|
<tr><td>Parts System</td><td><code>session/parts.ts</code></td><td>消息片段系统(Text、Tool、Reasoning 等)</td></tr>
|
|
<tr><td>CompressionManager</td><td><code>context/manager.ts</code></td><td>上下文压缩管理</td></tr>
|
|
<tr><td>TokenCounter</td><td><code>context/token-counter.ts</code></td><td>Token 计数工具</td></tr>
|
|
<tr><td>Prune</td><td><code>context/prune.ts</code></td><td>消息清理策略</td></tr>
|
|
<tr><td>Compaction</td><td><code>context/compaction.ts</code></td><td>消息压缩策略</td></tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<h2>待完善功能</h2>
|
|
<ul>
|
|
<li>📋 <strong>历史搜索</strong> - 搜索之前的对话内容</li>
|
|
<li>📋 <strong>跨会话恢复</strong> - 从历史记录恢复完整上下文</li>
|
|
</ul>
|
|
</DocsLayout>
|