docs(website): 完善文档页面结构
- 添加快速开始页面,包含安装、配置、启动指南 - 添加核心功能文档: Agent 系统、工具系统、MCP 集成 - 添加 API 参考文档: REST API、WebSocket、SDK 使用 - 更新侧边栏导航,重新组织文档分类 - 更新 .gitignore 允许 website docs 页面
This commit is contained in:
+4
-1
@@ -49,5 +49,8 @@ packages/desktop/src-tauri/Cargo.lock
|
||||
ai-open/
|
||||
|
||||
# Design docs (internal)
|
||||
docs/
|
||||
/docs/
|
||||
.astro/
|
||||
|
||||
# Exclude website docs pages from ignore
|
||||
!packages/website/src/pages/docs/
|
||||
|
||||
@@ -10,24 +10,41 @@ const navigation = [
|
||||
title: '开始',
|
||||
items: [
|
||||
{ title: '概览', href: '/docs' },
|
||||
{ title: '快速开始', href: '/docs/quickstart' },
|
||||
{ title: '架构设计', href: '/docs/architecture' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '已完成功能',
|
||||
title: '核心功能',
|
||||
items: [
|
||||
{ title: 'Agent 系统', href: '/docs/core/agent' },
|
||||
{ title: '工具系统', href: '/docs/core/tools' },
|
||||
{ title: '编辑模式', href: '/docs/features/edit-modes' },
|
||||
{ title: 'Checkpoint 系统', href: '/docs/features/checkpoint' },
|
||||
{ title: '多模型支持', href: '/docs/features/multi-model' },
|
||||
{ title: '会话管理', href: '/docs/features/chat-history' },
|
||||
{ title: '仓库地图', href: '/docs/features/repo-map' },
|
||||
{ title: '浏览器 GUI', href: '/docs/features/browser-gui' },
|
||||
{ title: 'MCP 集成', href: '/docs/core/mcp' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '规划中功能',
|
||||
title: 'API 参考',
|
||||
items: [
|
||||
{ title: 'REST API', href: '/docs/api/rest' },
|
||||
{ title: 'WebSocket', href: '/docs/api/websocket' },
|
||||
{ title: 'SDK 使用', href: '/docs/api/sdk' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '更多功能',
|
||||
items: [
|
||||
{ title: '多模型支持', href: '/docs/features/multi-model' },
|
||||
{ title: '仓库地图', href: '/docs/features/repo-map' },
|
||||
{ title: '浏览器 GUI', href: '/docs/features/browser-gui' },
|
||||
{ title: '配置管理', href: '/docs/features/configuration' },
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '规划中',
|
||||
items: [
|
||||
{ title: 'Linting 集成', href: '/docs/features/linting' },
|
||||
{ title: '测试集成', href: '/docs/features/testing' },
|
||||
{ title: 'Watch 模式', href: '/docs/features/watch-mode' },
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="REST API" description="AI Terminal Assistant REST API 参考文档">
|
||||
<h1>REST API</h1>
|
||||
|
||||
<p>
|
||||
AI Terminal Assistant Server 提供完整的 REST API,用于会话管理、消息发送、工具操作等。
|
||||
默认运行在 <code>http://localhost:3000</code>。
|
||||
</p>
|
||||
|
||||
<h2>认证</h2>
|
||||
|
||||
<p>远程模式下需要 Token 认证:</p>
|
||||
|
||||
<pre><code class="language-bash"># Header 方式
|
||||
curl -H "Authorization: Bearer your_token" http://localhost:3000/api/sessions
|
||||
|
||||
# Query 参数方式 (用于 WebSocket)
|
||||
ws://localhost:3000/api/ws/session-id?token=your_token</code></pre>
|
||||
|
||||
<h2>会话管理</h2>
|
||||
|
||||
<h3>列出会话</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/sessions</code></pre>
|
||||
|
||||
<p><strong>响应:</strong></p>
|
||||
<pre><code class="language-json">{
|
||||
"success": true,
|
||||
"data": [
|
||||
{
|
||||
"id": "session-123",
|
||||
"name": "my-session",
|
||||
"status": "idle",
|
||||
"createdAt": "2025-01-01T00:00:00Z",
|
||||
"updatedAt": "2025-01-01T00:00:00Z"
|
||||
}
|
||||
]
|
||||
}</code></pre>
|
||||
|
||||
<h3>创建会话</h3>
|
||||
|
||||
<pre><code class="language-http">POST /api/sessions
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "my-session",
|
||||
"workDir": "/path/to/project"
|
||||
}</code></pre>
|
||||
|
||||
<p><strong>响应:</strong></p>
|
||||
<pre><code class="language-json">{
|
||||
"success": true,
|
||||
"data": {
|
||||
"id": "session-123",
|
||||
"name": "my-session",
|
||||
"status": "idle",
|
||||
"workDir": "/path/to/project"
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h3>获取会话详情</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/sessions/:id</code></pre>
|
||||
|
||||
<h3>删除会话</h3>
|
||||
|
||||
<pre><code class="language-http">DELETE /api/sessions/:id</code></pre>
|
||||
|
||||
<h2>消息管理</h2>
|
||||
|
||||
<h3>获取消息历史</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/sessions/:id/messages</code></pre>
|
||||
|
||||
<p><strong>响应:</strong></p>
|
||||
<pre><code class="language-json">{
|
||||
"success": true,
|
||||
"data": [
|
||||
{
|
||||
"id": "msg-001",
|
||||
"role": "user",
|
||||
"content": "你好",
|
||||
"timestamp": 1704067200000
|
||||
},
|
||||
{
|
||||
"id": "msg-002",
|
||||
"role": "assistant",
|
||||
"content": "你好!有什么可以帮助你的?",
|
||||
"timestamp": 1704067201000
|
||||
}
|
||||
]
|
||||
}</code></pre>
|
||||
|
||||
<h3>发送消息 (非流式)</h3>
|
||||
|
||||
<pre><code class="language-http">POST /api/sessions/:id/messages
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"content": "帮我写一个排序函数"
|
||||
}</code></pre>
|
||||
|
||||
<p><strong>注意:</strong> 推荐使用 WebSocket 进行流式对话。</p>
|
||||
|
||||
<h2>工具操作</h2>
|
||||
|
||||
<h3>列出可用工具</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/tools</code></pre>
|
||||
|
||||
<p><strong>响应:</strong></p>
|
||||
<pre><code class="language-json">{
|
||||
"success": true,
|
||||
"data": [
|
||||
{
|
||||
"name": "read_file",
|
||||
"description": "读取文件内容",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": { "type": "string" }
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}</code></pre>
|
||||
|
||||
<h3>获取工具详情</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/tools/:name</code></pre>
|
||||
|
||||
<h3>执行工具</h3>
|
||||
|
||||
<pre><code class="language-http">POST /api/tools/:name/execute
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"sessionId": "session-123",
|
||||
"params": {
|
||||
"path": "/project/src/index.ts"
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h2>文件操作</h2>
|
||||
|
||||
<h3>获取工作目录信息</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/files</code></pre>
|
||||
|
||||
<h3>列出目录内容</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/files/list?path=/project/src</code></pre>
|
||||
|
||||
<h3>读取文件</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/files/read?path=/project/package.json</code></pre>
|
||||
|
||||
<h3>获取文件信息</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/files/stat?path=/project/src/index.ts</code></pre>
|
||||
|
||||
<h3>获取目录树</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/files/tree?path=/project&depth=3</code></pre>
|
||||
|
||||
<h2>配置管理</h2>
|
||||
|
||||
<h3>获取配置</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/config</code></pre>
|
||||
|
||||
<p><strong>响应:</strong></p>
|
||||
<pre><code class="language-json">{
|
||||
"success": true,
|
||||
"data": {
|
||||
"model": "claude-sonnet-4-20250514",
|
||||
"maxTokens": 4096,
|
||||
"temperature": 0.7,
|
||||
"workDir": "/project"
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h3>更新配置</h3>
|
||||
|
||||
<pre><code class="language-http">PUT /api/config
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"model": "claude-opus-4-20250514",
|
||||
"maxTokens": 8192
|
||||
}</code></pre>
|
||||
|
||||
<h2>检查点操作</h2>
|
||||
|
||||
<h3>列出检查点</h3>
|
||||
|
||||
<pre><code class="language-http">GET /api/sessions/:id/checkpoints</code></pre>
|
||||
|
||||
<h3>创建检查点</h3>
|
||||
|
||||
<pre><code class="language-http">POST /api/sessions/:id/checkpoints
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"description": "保存当前状态"
|
||||
}</code></pre>
|
||||
|
||||
<h3>恢复检查点</h3>
|
||||
|
||||
<pre><code class="language-http">POST /api/sessions/:id/checkpoints/:cpId/restore</code></pre>
|
||||
|
||||
<h2>健康检查</h2>
|
||||
|
||||
<pre><code class="language-http">GET /health</code></pre>
|
||||
|
||||
<p><strong>响应:</strong></p>
|
||||
<pre><code class="language-json">{
|
||||
"status": "ok",
|
||||
"timestamp": 1704067200000
|
||||
}</code></pre>
|
||||
|
||||
<h2>错误响应</h2>
|
||||
|
||||
<p>所有错误响应遵循统一格式:</p>
|
||||
|
||||
<pre><code class="language-json">{
|
||||
"success": false,
|
||||
"error": {
|
||||
"code": "NOT_FOUND",
|
||||
"message": "Session not found"
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h2>状态码</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>状态码</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>200</td><td>成功</td></tr>
|
||||
<tr><td>201</td><td>创建成功</td></tr>
|
||||
<tr><td>400</td><td>请求参数错误</td></tr>
|
||||
<tr><td>401</td><td>未认证</td></tr>
|
||||
<tr><td>403</td><td>权限不足</td></tr>
|
||||
<tr><td>404</td><td>资源不存在</td></tr>
|
||||
<tr><td>500</td><td>服务器错误</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>相关文档</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/api/websocket" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">WebSocket API</h3>
|
||||
<p class="text-sm text-gray-400">实时流式对话</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/api/sdk" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">SDK 使用</h3>
|
||||
<p class="text-sm text-gray-400">JavaScript/TypeScript SDK</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,315 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="SDK 使用" description="AI Terminal Assistant JavaScript/TypeScript SDK">
|
||||
<h1>SDK 使用</h1>
|
||||
|
||||
<p>
|
||||
AI Terminal Assistant 提供 JavaScript/TypeScript SDK,简化与 Server 的交互。
|
||||
SDK 封装了 REST API 和 WebSocket 通信。
|
||||
</p>
|
||||
|
||||
<h2>安装</h2>
|
||||
|
||||
<pre><code class="language-bash"># npm
|
||||
npm install @ai-assistant/sdk
|
||||
|
||||
# pnpm
|
||||
pnpm add @ai-assistant/sdk
|
||||
|
||||
# yarn
|
||||
yarn add @ai-assistant/sdk</code></pre>
|
||||
|
||||
<h2>快速开始</h2>
|
||||
|
||||
<pre><code class="language-typescript">import { AIAssistantClient } from '@ai-assistant/sdk';
|
||||
|
||||
// 创建客户端
|
||||
const client = new AIAssistantClient({
|
||||
baseUrl: 'http://localhost:3000',
|
||||
token: 'your-token' // 可选,远程模式需要
|
||||
});
|
||||
|
||||
// 创建会话
|
||||
const session = await client.sessions.create({
|
||||
name: 'my-session',
|
||||
workDir: '/path/to/project'
|
||||
});
|
||||
|
||||
// 流式对话
|
||||
await client.chat(session.id, '帮我写一个 Hello World', {
|
||||
onChunk: (chunk) => {
|
||||
process.stdout.write(chunk);
|
||||
},
|
||||
onToolCall: (tool) => {
|
||||
console.log(`调用工具: ${tool.name}`);
|
||||
},
|
||||
onComplete: () => {
|
||||
console.log('完成');
|
||||
}
|
||||
});</code></pre>
|
||||
|
||||
<h2>客户端配置</h2>
|
||||
|
||||
<pre><code class="language-typescript">interface ClientConfig {
|
||||
// 服务器地址
|
||||
baseUrl: string;
|
||||
|
||||
// 认证 Token (远程模式需要)
|
||||
token?: string;
|
||||
|
||||
// 请求超时 (毫秒)
|
||||
timeout?: number;
|
||||
|
||||
// 自动重连
|
||||
autoReconnect?: boolean;
|
||||
|
||||
// 最大重连次数
|
||||
maxReconnectAttempts?: number;
|
||||
}
|
||||
|
||||
const client = new AIAssistantClient({
|
||||
baseUrl: 'http://localhost:3000',
|
||||
token: 'your-token',
|
||||
timeout: 30000,
|
||||
autoReconnect: true,
|
||||
maxReconnectAttempts: 5
|
||||
});</code></pre>
|
||||
|
||||
<h2>会话管理</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 列出所有会话
|
||||
const sessions = await client.sessions.list();
|
||||
|
||||
// 创建会话
|
||||
const session = await client.sessions.create({
|
||||
name: 'coding-session',
|
||||
workDir: '/project'
|
||||
});
|
||||
|
||||
// 获取会话
|
||||
const session = await client.sessions.get('session-id');
|
||||
|
||||
// 删除会话
|
||||
await client.sessions.delete('session-id');
|
||||
|
||||
// 获取消息历史
|
||||
const messages = await client.sessions.getMessages('session-id');</code></pre>
|
||||
|
||||
<h2>对话交互</h2>
|
||||
|
||||
<h3>流式对话</h3>
|
||||
|
||||
<pre><code class="language-typescript">// 基本用法
|
||||
await client.chat(sessionId, '你好', {
|
||||
onChunk: (chunk) => console.log(chunk)
|
||||
});
|
||||
|
||||
// 完整回调
|
||||
await client.chat(sessionId, '帮我优化代码', {
|
||||
onChunk: (chunk) => {
|
||||
// 文本块
|
||||
process.stdout.write(chunk);
|
||||
},
|
||||
onToolCall: (tool) => {
|
||||
// 工具调用开始
|
||||
console.log(`\\n[调用 ${tool.name}]`);
|
||||
},
|
||||
onToolResult: (result) => {
|
||||
// 工具执行结果
|
||||
console.log(`[结果: ${result.success ? '成功' : '失败'}]`);
|
||||
},
|
||||
onPermissionRequest: async (request) => {
|
||||
// 权限请求
|
||||
return confirm(`允许 ${request.tool}?`);
|
||||
},
|
||||
onComplete: () => {
|
||||
// 完成
|
||||
console.log('\\n--- 完成 ---');
|
||||
},
|
||||
onError: (error) => {
|
||||
// 错误
|
||||
console.error('错误:', error.message);
|
||||
}
|
||||
});</code></pre>
|
||||
|
||||
<h3>发送带附件的消息</h3>
|
||||
|
||||
<pre><code class="language-typescript">// 发送图片
|
||||
await client.chat(sessionId, [
|
||||
{ type: 'text', text: '这个 UI 有什么问题?' },
|
||||
{ type: 'image', path: '/path/to/screenshot.png' }
|
||||
]);
|
||||
|
||||
// 发送文件引用
|
||||
await client.chat(sessionId, [
|
||||
{ type: 'text', text: '帮我优化这个文件' },
|
||||
{ type: 'file', path: '/project/src/index.ts' }
|
||||
]);</code></pre>
|
||||
|
||||
<h3>取消生成</h3>
|
||||
|
||||
<pre><code class="language-typescript">// 开始对话
|
||||
const controller = client.chat(sessionId, '写一篇长文章', {
|
||||
onChunk: (chunk) => console.log(chunk)
|
||||
});
|
||||
|
||||
// 取消
|
||||
setTimeout(() => {
|
||||
controller.cancel();
|
||||
}, 5000);</code></pre>
|
||||
|
||||
<h2>工具操作</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 列出可用工具
|
||||
const tools = await client.tools.list();
|
||||
|
||||
// 获取工具详情
|
||||
const tool = await client.tools.get('read_file');
|
||||
|
||||
// 手动执行工具
|
||||
const result = await client.tools.execute('read_file', {
|
||||
sessionId: 'session-id',
|
||||
params: {
|
||||
path: '/project/package.json'
|
||||
}
|
||||
});</code></pre>
|
||||
|
||||
<h2>文件操作</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 列出目录
|
||||
const files = await client.files.list('/project/src');
|
||||
|
||||
// 读取文件
|
||||
const content = await client.files.read('/project/package.json');
|
||||
|
||||
// 获取文件信息
|
||||
const stat = await client.files.stat('/project/src/index.ts');
|
||||
|
||||
// 获取目录树
|
||||
const tree = await client.files.tree('/project', { depth: 3 });</code></pre>
|
||||
|
||||
<h2>配置管理</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 获取配置
|
||||
const config = await client.config.get();
|
||||
|
||||
// 更新配置
|
||||
await client.config.update({
|
||||
model: 'claude-opus-4-20250514',
|
||||
maxTokens: 8192
|
||||
});</code></pre>
|
||||
|
||||
<h2>检查点操作</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 列出检查点
|
||||
const checkpoints = await client.checkpoints.list(sessionId);
|
||||
|
||||
// 创建检查点
|
||||
const cp = await client.checkpoints.create(sessionId, {
|
||||
description: '保存当前状态'
|
||||
});
|
||||
|
||||
// 恢复检查点
|
||||
await client.checkpoints.restore(sessionId, checkpointId);
|
||||
|
||||
// 查看差异
|
||||
const diff = await client.checkpoints.diff(sessionId, checkpointId);</code></pre>
|
||||
|
||||
<h2>事件监听</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 监听状态变化
|
||||
client.on('status', (status) => {
|
||||
console.log(`状态: ${status}`);
|
||||
});
|
||||
|
||||
// 监听连接事件
|
||||
client.on('connected', () => {
|
||||
console.log('已连接');
|
||||
});
|
||||
|
||||
client.on('disconnected', () => {
|
||||
console.log('已断开');
|
||||
});
|
||||
|
||||
// 监听错误
|
||||
client.on('error', (error) => {
|
||||
console.error('错误:', error);
|
||||
});</code></pre>
|
||||
|
||||
<h2>TypeScript 类型</h2>
|
||||
|
||||
<pre><code class="language-typescript">import type {
|
||||
Session,
|
||||
Message,
|
||||
Tool,
|
||||
Checkpoint,
|
||||
Config,
|
||||
ChatOptions,
|
||||
ToolCallInfo,
|
||||
PermissionRequest
|
||||
} from '@ai-assistant/sdk';
|
||||
|
||||
// 所有类型都有完整的 TypeScript 定义</code></pre>
|
||||
|
||||
<h2>错误处理</h2>
|
||||
|
||||
<pre><code class="language-typescript">import { AIAssistantError, NetworkError, AuthError } from '@ai-assistant/sdk';
|
||||
|
||||
try {
|
||||
await client.chat(sessionId, 'hello');
|
||||
} catch (error) {
|
||||
if (error instanceof AuthError) {
|
||||
console.error('认证失败,请检查 Token');
|
||||
} else if (error instanceof NetworkError) {
|
||||
console.error('网络错误,请检查连接');
|
||||
} else if (error instanceof AIAssistantError) {
|
||||
console.error('服务错误:', error.message);
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h2>在 React 中使用</h2>
|
||||
|
||||
<pre><code class="language-typescript">import { useState, useEffect } from 'react';
|
||||
import { AIAssistantClient } from '@ai-assistant/sdk';
|
||||
|
||||
function useChat(sessionId: string) {
|
||||
const [messages, setMessages] = useState<string[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const client = useMemo(() => new AIAssistantClient({
|
||||
baseUrl: 'http://localhost:3000'
|
||||
}), []);
|
||||
|
||||
const sendMessage = async (content: string) => {
|
||||
setIsLoading(true);
|
||||
let response = '';
|
||||
|
||||
await client.chat(sessionId, content, {
|
||||
onChunk: (chunk) => {
|
||||
response += chunk;
|
||||
setMessages(prev => [...prev.slice(0, -1), response]);
|
||||
},
|
||||
onComplete: () => {
|
||||
setIsLoading(false);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return { messages, sendMessage, isLoading };
|
||||
}</code></pre>
|
||||
|
||||
<h2>相关文档</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/api/rest" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">REST API</h3>
|
||||
<p class="text-sm text-gray-400">底层 REST API 文档</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/api/websocket" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">WebSocket API</h3>
|
||||
<p class="text-sm text-gray-400">底层 WebSocket 协议</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,265 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="WebSocket API" description="AI Terminal Assistant WebSocket 实时通信协议">
|
||||
<h1>WebSocket API</h1>
|
||||
|
||||
<p>
|
||||
WebSocket 是与 AI Terminal Assistant 进行实时对话的推荐方式。
|
||||
支持流式响应、工具调用反馈、状态更新等。
|
||||
</p>
|
||||
|
||||
<h2>连接</h2>
|
||||
|
||||
<pre><code class="language-javascript">// 基本连接
|
||||
const ws = new WebSocket('ws://localhost:3000/api/ws/session-123');
|
||||
|
||||
// 带认证连接
|
||||
const ws = new WebSocket('ws://localhost:3000/api/ws/session-123?token=your_token');
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log('已连接');
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
console.log('连接关闭');
|
||||
};</code></pre>
|
||||
|
||||
<h2>消息格式</h2>
|
||||
|
||||
<h3>客户端 → 服务端</h3>
|
||||
|
||||
<pre><code class="language-typescript">interface ClientMessage {
|
||||
type: 'message' | 'cancel' | 'tool_response' | 'permission_response';
|
||||
payload: any;
|
||||
}</code></pre>
|
||||
|
||||
<h3>服务端 → 客户端</h3>
|
||||
|
||||
<pre><code class="language-typescript">interface ServerMessage {
|
||||
type: 'chunk' | 'tool_call' | 'tool_result' | 'done' | 'error' |
|
||||
'status' | 'permission_request';
|
||||
payload: any;
|
||||
}</code></pre>
|
||||
|
||||
<h2>发送消息</h2>
|
||||
|
||||
<pre><code class="language-javascript">// 发送用户消息
|
||||
ws.send(JSON.stringify({
|
||||
type: 'message',
|
||||
payload: {
|
||||
content: '帮我写一个快速排序函数'
|
||||
}
|
||||
}));
|
||||
|
||||
// 发送带图片的消息
|
||||
ws.send(JSON.stringify({
|
||||
type: 'message',
|
||||
payload: {
|
||||
content: [
|
||||
{ type: 'text', text: '这个界面有什么问题?' },
|
||||
{ type: 'image', source: { type: 'base64', data: '...' } }
|
||||
]
|
||||
}
|
||||
}));</code></pre>
|
||||
|
||||
<h2>接收消息</h2>
|
||||
|
||||
<pre><code class="language-javascript">ws.onmessage = (event) => {
|
||||
const message = JSON.parse(event.data);
|
||||
|
||||
switch (message.type) {
|
||||
case 'chunk':
|
||||
// 流式文本块
|
||||
process.stdout.write(message.payload.content);
|
||||
break;
|
||||
|
||||
case 'tool_call':
|
||||
// AI 调用了工具
|
||||
console.log(`工具调用: ${message.payload.name}`);
|
||||
console.log(`参数: ${JSON.stringify(message.payload.params)}`);
|
||||
break;
|
||||
|
||||
case 'tool_result':
|
||||
// 工具执行结果
|
||||
console.log(`工具结果: ${message.payload.result}`);
|
||||
break;
|
||||
|
||||
case 'permission_request':
|
||||
// 权限请求
|
||||
handlePermissionRequest(message.payload);
|
||||
break;
|
||||
|
||||
case 'done':
|
||||
// 对话完成
|
||||
console.log('\\n对话完成');
|
||||
break;
|
||||
|
||||
case 'error':
|
||||
// 错误
|
||||
console.error(`错误: ${message.payload.message}`);
|
||||
break;
|
||||
}
|
||||
};</code></pre>
|
||||
|
||||
<h2>取消生成</h2>
|
||||
|
||||
<pre><code class="language-javascript">// 取消当前生成
|
||||
ws.send(JSON.stringify({
|
||||
type: 'cancel',
|
||||
payload: {}
|
||||
}));</code></pre>
|
||||
|
||||
<h2>权限响应</h2>
|
||||
|
||||
<pre><code class="language-javascript">// 当收到 permission_request 时
|
||||
function handlePermissionRequest(request) {
|
||||
// 显示权限请求给用户
|
||||
const approved = confirm(`允许执行 ${request.tool}?`);
|
||||
|
||||
// 发送权限响应
|
||||
ws.send(JSON.stringify({
|
||||
type: 'permission_response',
|
||||
payload: {
|
||||
requestId: request.id,
|
||||
approved: approved
|
||||
}
|
||||
}));
|
||||
}</code></pre>
|
||||
|
||||
<h2>完整示例</h2>
|
||||
|
||||
<pre><code class="language-javascript">class ChatClient {
|
||||
constructor(sessionId, token) {
|
||||
this.sessionId = sessionId;
|
||||
this.token = token;
|
||||
this.ws = null;
|
||||
}
|
||||
|
||||
connect() {
|
||||
const url = `ws://localhost:3000/api/ws/${this.sessionId}`;
|
||||
this.ws = new WebSocket(this.token ? `${url}?token=${this.token}` : url);
|
||||
|
||||
this.ws.onopen = () => {
|
||||
console.log('已连接到 AI Assistant');
|
||||
};
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
this.handleMessage(JSON.parse(event.data));
|
||||
};
|
||||
|
||||
this.ws.onerror = (error) => {
|
||||
console.error('WebSocket 错误:', error);
|
||||
};
|
||||
|
||||
this.ws.onclose = () => {
|
||||
console.log('连接已关闭');
|
||||
};
|
||||
}
|
||||
|
||||
send(content) {
|
||||
this.ws.send(JSON.stringify({
|
||||
type: 'message',
|
||||
payload: { content }
|
||||
}));
|
||||
}
|
||||
|
||||
cancel() {
|
||||
this.ws.send(JSON.stringify({
|
||||
type: 'cancel',
|
||||
payload: {}
|
||||
}));
|
||||
}
|
||||
|
||||
handleMessage(message) {
|
||||
switch (message.type) {
|
||||
case 'chunk':
|
||||
this.onChunk?.(message.payload.content);
|
||||
break;
|
||||
case 'tool_call':
|
||||
this.onToolCall?.(message.payload);
|
||||
break;
|
||||
case 'tool_result':
|
||||
this.onToolResult?.(message.payload);
|
||||
break;
|
||||
case 'done':
|
||||
this.onDone?.();
|
||||
break;
|
||||
case 'error':
|
||||
this.onError?.(message.payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
this.ws?.close();
|
||||
}
|
||||
}
|
||||
|
||||
// 使用示例
|
||||
const client = new ChatClient('session-123', 'your-token');
|
||||
|
||||
client.onChunk = (content) => process.stdout.write(content);
|
||||
client.onToolCall = (tool) => console.log(`\\n[调用工具: ${tool.name}]`);
|
||||
client.onDone = () => console.log('\\n--- 完成 ---');
|
||||
|
||||
client.connect();
|
||||
client.send('帮我分析这个项目的结构');</code></pre>
|
||||
|
||||
<h2>心跳保活</h2>
|
||||
|
||||
<pre><code class="language-javascript">// 服务端会定期发送 ping,客户端需要响应 pong
|
||||
// 大多数 WebSocket 库会自动处理
|
||||
|
||||
// 如果需要手动处理:
|
||||
setInterval(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify({ type: 'ping' }));
|
||||
}
|
||||
}, 30000);</code></pre>
|
||||
|
||||
<h2>重连策略</h2>
|
||||
|
||||
<pre><code class="language-javascript">class ReconnectingClient extends ChatClient {
|
||||
constructor(sessionId, token) {
|
||||
super(sessionId, token);
|
||||
this.reconnectAttempts = 0;
|
||||
this.maxReconnectAttempts = 5;
|
||||
}
|
||||
|
||||
connect() {
|
||||
super.connect();
|
||||
|
||||
this.ws.onclose = () => {
|
||||
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
||||
const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
|
||||
console.log(`${delay/1000}秒后重连...`);
|
||||
setTimeout(() => {
|
||||
this.reconnectAttempts++;
|
||||
this.connect();
|
||||
}, delay);
|
||||
}
|
||||
};
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.reconnectAttempts = 0;
|
||||
console.log('已连接');
|
||||
};
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h2>相关文档</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/api/rest" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">REST API</h3>
|
||||
<p class="text-sm text-gray-400">会话和工具管理</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/api/sdk" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">SDK 使用</h3>
|
||||
<p class="text-sm text-gray-400">封装好的客户端 SDK</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,351 @@
|
||||
---
|
||||
import DocsLayout from '../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="GUI + Server/Client 架构设计" description="融合 OpenHands 和 OpenCode 的架构优势,实现多端统一的 AI 编程助手">
|
||||
<h1>GUI + Server/Client 架构设计方案</h1>
|
||||
|
||||
<blockquote>
|
||||
融合 OpenHands 和 OpenCode 的架构优势,实现多端统一的 AI 编程助手
|
||||
</blockquote>
|
||||
|
||||
<h2>1. 设计目标</h2>
|
||||
<ul>
|
||||
<li><strong>Web 端</strong>: 浏览器访问,手机/电脑快速使用</li>
|
||||
<li><strong>桌面端</strong>: macOS/Windows 原生应用,深度使用体验</li>
|
||||
<li><strong>远程控制</strong>: 电脑运行 Server,手机 App 远程驱动</li>
|
||||
<li><strong>CLI/TUI</strong>: 保留终端使用方式</li>
|
||||
</ul>
|
||||
|
||||
<h2>2. 整体架构图</h2>
|
||||
<pre><code>┌─────────────────────────────────────────────────────────────┐
|
||||
│ 客户端层 │
|
||||
├─────────────┬─────────────┬─────────────┬──────────────────┤
|
||||
│ Web App │ Tauri App │ TUI CLI │ Mobile (PWA) │
|
||||
│ (React) │ (macOS/Win) │ (Terminal) │ (响应式Web) │
|
||||
└──────┬──────┴──────┬──────┴──────┬──────┴────────┬─────────┘
|
||||
│ │ │ │
|
||||
└─────────────┴──────┬──────┴───────────────┘
|
||||
│
|
||||
┌───────▼───────┐
|
||||
│ 统一 API │
|
||||
│ REST + WS/SSE │
|
||||
└───────┬───────┘
|
||||
│
|
||||
┌───────────────────────────▼─────────────────────────────────┐
|
||||
│ Server 核心层 │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │
|
||||
│ │ Session │ │ Agent │ │ Tools │ │ Event Bus (SSE) │ │
|
||||
│ │ Manager │ │ Core │ │ Registry│ │ │ │
|
||||
│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │
|
||||
│ │ LLM │ │ LSP │ │ File │ │ Checkpoint │ │
|
||||
│ │Provider │ │ Service │ │ System │ │ System │ │
|
||||
│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────┘</code></pre>
|
||||
|
||||
<h2>3. 通信协议</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>协议</th>
|
||||
<th>用途</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>REST API</td>
|
||||
<td>CRUD 操作:会话管理、配置、历史记录</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>WebSocket</td>
|
||||
<td>双向实时:对话流、工具执行反馈</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>SSE</td>
|
||||
<td>单向推送:状态更新、日志流、进度通知</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>4. Monorepo 目录结构</h2>
|
||||
<pre><code>ai-terminal-assistant/ # 一个 Git 仓库,多个包
|
||||
├── packages/
|
||||
│ ├── core/ # @ai-assistant/core
|
||||
│ │ └── src/
|
||||
│ │ ├── agent/ # Agent 系统
|
||||
│ │ ├── tools/ # 工具注册
|
||||
│ │ ├── editors/ # 编辑器系统
|
||||
│ │ ├── lsp/ # LSP 集成
|
||||
│ │ ├── checkpoint/ # 检查点系统
|
||||
│ │ └── hooks/ # Hook 系统
|
||||
│ │
|
||||
│ ├── server/ # @ai-assistant/server
|
||||
│ │ └── src/
|
||||
│ │ ├── routes/ # API 路由
|
||||
│ │ ├── ws.ts # WebSocket
|
||||
│ │ └── sse.ts # SSE 事件流
|
||||
│ │
|
||||
│ ├── cli/ # @ai-assistant/cli
|
||||
│ │ └── src/
|
||||
│ │ ├── commands/ # 命令: serve, attach
|
||||
│ │ └── tui/ # 终端 UI 组件
|
||||
│ │
|
||||
│ ├── web/ # @ai-assistant/web
|
||||
│ │ └── src/
|
||||
│ │ ├── api/ # API Client
|
||||
│ │ ├── components/ # React 组件
|
||||
│ │ └── pages/ # 页面
|
||||
│ │
|
||||
│ └── desktop/ # @ai-assistant/desktop
|
||||
│ ├── src/ # 复用 web 组件
|
||||
│ └── src-tauri/ # Tauri Rust 后端
|
||||
│
|
||||
├── pnpm-workspace.yaml
|
||||
└── package.json</code></pre>
|
||||
|
||||
<h2>5. 包依赖关系</h2>
|
||||
<pre><code> ┌─────────────┐
|
||||
│ sdk │ ← 对外发布,无依赖
|
||||
└─────────────┘
|
||||
▲
|
||||
┌──────────────────┼──────────────────┐
|
||||
│ │ │
|
||||
┌───────┴───────┐ ┌───────┴───────┐ ┌───────┴───────┐
|
||||
│ web │ │ desktop │ │ cli │
|
||||
│ (React SPA) │ │ (Tauri) │ │ (终端入口) │
|
||||
└───────┬───────┘ └───────┬───────┘ └───────┬───────┘
|
||||
│ │ │
|
||||
└──────────────────┼──────────────────┘
|
||||
│
|
||||
┌──────▼──────┐
|
||||
│ server │ ← HTTP API
|
||||
└──────┬──────┘
|
||||
│
|
||||
┌──────▼──────┐
|
||||
│ core │ ← Agent/Tools/LSP
|
||||
└─────────────┘</code></pre>
|
||||
|
||||
<h2>6. Server API 设计</h2>
|
||||
|
||||
<h3>6.1 REST API</h3>
|
||||
<pre><code class="language-typescript">// 会话管理
|
||||
GET /api/sessions // 列出所有会话
|
||||
POST /api/sessions // 创建新会话
|
||||
GET /api/sessions/:id // 获取会话详情
|
||||
DELETE /api/sessions/:id // 删除会话
|
||||
|
||||
// 消息交互
|
||||
POST /api/sessions/:id/messages // 发送消息
|
||||
GET /api/sessions/:id/messages // 获取历史消息
|
||||
|
||||
// 工具操作
|
||||
GET /api/tools // 列出可用工具
|
||||
POST /api/tools/:name/execute // 执行工具
|
||||
|
||||
// 配置
|
||||
GET /api/config // 获取配置
|
||||
PUT /api/config // 更新配置</code></pre>
|
||||
|
||||
<h3>6.2 WebSocket 协议</h3>
|
||||
<pre><code class="language-typescript">// 客户端 -> 服务端
|
||||
interface ClientMessage {
|
||||
type: "message" | "cancel" | "tool_response";
|
||||
sessionId: string;
|
||||
payload: any;
|
||||
}
|
||||
|
||||
// 服务端 -> 客户端
|
||||
interface ServerMessage {
|
||||
type: "chunk" | "tool_call" | "tool_result" | "done" | "error";
|
||||
sessionId: string;
|
||||
payload: any;
|
||||
}</code></pre>
|
||||
|
||||
<h2>7. 技术选型</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>组件</th>
|
||||
<th>选择</th>
|
||||
<th>理由</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Server 框架</td>
|
||||
<td>Hono + Bun</td>
|
||||
<td>TypeScript 原生,SSE/WS 支持好,性能优秀</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>桌面框架</td>
|
||||
<td>Tauri</td>
|
||||
<td>比 Electron 轻 10x,安全性好,Rust 后端</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Web 框架</td>
|
||||
<td>React + Vite</td>
|
||||
<td>生态成熟,可复用到 Tauri</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>TUI 框架</td>
|
||||
<td>blessed</td>
|
||||
<td>功能完整,组件化开发</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Monorepo</td>
|
||||
<td>pnpm workspaces</td>
|
||||
<td>简单高效,原生支持</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>状态管理</td>
|
||||
<td>Zustand</td>
|
||||
<td>轻量,支持多端</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>8. 运行模式</h2>
|
||||
|
||||
<h3>8.1 本地模式 (默认)</h3>
|
||||
<pre><code class="language-bash"># TUI 模式 - 内嵌 Server
|
||||
ai-assistant
|
||||
|
||||
# Web 模式 - 启动本地 Web UI
|
||||
ai-assistant --web
|
||||
ai-assistant --web --port 3000</code></pre>
|
||||
|
||||
<h3>8.2 Server 模式 (远程控制)</h3>
|
||||
<pre><code class="language-bash"># 启动 headless server
|
||||
ai-assistant serve --port 3000 --host 0.0.0.0
|
||||
|
||||
# 从其他设备连接
|
||||
ai-assistant attach --host 192.168.1.100:3000
|
||||
|
||||
# 或通过浏览器访问
|
||||
# http://192.168.1.100:3000</code></pre>
|
||||
|
||||
<h3>8.3 桌面模式</h3>
|
||||
<pre><code class="language-bash"># 启动桌面应用 (内嵌 Server)
|
||||
ai-assistant-desktop
|
||||
|
||||
# 桌面应用连接远程 Server
|
||||
ai-assistant-desktop --remote 192.168.1.100:3000</code></pre>
|
||||
|
||||
<h2>9. 实现路线图</h2>
|
||||
|
||||
<h3>Phase 1: Server 层 (基础) ✅ 已完成</h3>
|
||||
<ul>
|
||||
<li>✅ 重构为 Monorepo 结构 (pnpm workspaces)</li>
|
||||
<li>✅ 迁移现有代码到 packages/core/</li>
|
||||
<li>✅ 创建 packages/server/ 模块</li>
|
||||
<li>✅ 实现基础 REST API</li>
|
||||
<li>✅ 实现 WebSocket 实时通信</li>
|
||||
<li>✅ 实现 SSE 事件推送</li>
|
||||
<li>✅ 集成 core 模块 Agent 调用</li>
|
||||
<li>✅ 添加认证机制 (token-based)</li>
|
||||
</ul>
|
||||
|
||||
<h3>Phase 2: CLI 重构 ✅ 已完成</h3>
|
||||
<ul>
|
||||
<li>✅ 创建 packages/cli/ 模块</li>
|
||||
<li>✅ 实现 serve 命令启动 Server</li>
|
||||
<li>✅ 实现 attach 命令连接远程 Server</li>
|
||||
<li>✅ 重构 TUI 组件复用 Server API</li>
|
||||
</ul>
|
||||
|
||||
<h3>Phase 3: Web 前端 ✅ 已完成</h3>
|
||||
<ul>
|
||||
<li>✅ 创建 packages/web/ 模块</li>
|
||||
<li>✅ 实现基础 UI 组件</li>
|
||||
<li>✅ 对话界面</li>
|
||||
<li>✅ 文件浏览器</li>
|
||||
<li>✅ 配置面板</li>
|
||||
</ul>
|
||||
|
||||
<h3>Phase 4: 桌面应用 ✅ 已完成</h3>
|
||||
<ul>
|
||||
<li>✅ 创建 packages/desktop/ 模块</li>
|
||||
<li>✅ 复用 Web 前端代码</li>
|
||||
<li>✅ 添加系统托盘</li>
|
||||
<li>✅ 实现本地文件访问</li>
|
||||
</ul>
|
||||
|
||||
<h3>Phase 5: 移动端优化 ✅ 已完成</h3>
|
||||
<ul>
|
||||
<li>✅ Web 响应式适配</li>
|
||||
<li>✅ PWA 支持</li>
|
||||
</ul>
|
||||
|
||||
<h2>10. 安全考虑</h2>
|
||||
|
||||
<h3>10.1 本地模式</h3>
|
||||
<ul>
|
||||
<li>默认绑定 127.0.0.1</li>
|
||||
<li>无需认证</li>
|
||||
</ul>
|
||||
|
||||
<h3>10.2 远程模式</h3>
|
||||
<ul>
|
||||
<li>强制 HTTPS (或提供警告)</li>
|
||||
<li>Token 认证</li>
|
||||
<li>可选:OAuth2 支持</li>
|
||||
<li>操作审计日志</li>
|
||||
<li>敏感操作二次确认</li>
|
||||
</ul>
|
||||
|
||||
<h3>10.3 权限控制</h3>
|
||||
<pre><code class="language-typescript">interface Permission {
|
||||
// 文件系统访问范围
|
||||
allowedPaths: string[];
|
||||
deniedPaths: string[];
|
||||
|
||||
// 工具权限
|
||||
allowedTools: string[];
|
||||
|
||||
// 命令执行
|
||||
allowShellCommands: boolean;
|
||||
shellCommandPatterns: string[];
|
||||
}</code></pre>
|
||||
|
||||
<h2>11. 已实现的 API 端点</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>方法</th>
|
||||
<th>路径</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>GET</td><td>/health</td><td>健康检查</td></tr>
|
||||
<tr><td>GET</td><td>/api/sessions</td><td>列出会话</td></tr>
|
||||
<tr><td>POST</td><td>/api/sessions</td><td>创建会话</td></tr>
|
||||
<tr><td>GET</td><td>/api/sessions/:id</td><td>获取会话</td></tr>
|
||||
<tr><td>DELETE</td><td>/api/sessions/:id</td><td>删除会话</td></tr>
|
||||
<tr><td>GET</td><td>/api/sessions/:id/messages</td><td>获取消息</td></tr>
|
||||
<tr><td>POST</td><td>/api/sessions/:id/messages</td><td>发送消息</td></tr>
|
||||
<tr><td>GET</td><td>/api/sessions/:id/events</td><td>SSE 事件流</td></tr>
|
||||
<tr><td>GET</td><td>/api/tools</td><td>列出工具</td></tr>
|
||||
<tr><td>GET</td><td>/api/tools/:name</td><td>工具详情</td></tr>
|
||||
<tr><td>POST</td><td>/api/tools/:name/execute</td><td>执行工具</td></tr>
|
||||
<tr><td>GET</td><td>/api/config</td><td>获取配置</td></tr>
|
||||
<tr><td>PUT/PATCH</td><td>/api/config</td><td>更新配置</td></tr>
|
||||
<tr><td>GET</td><td>/api/files</td><td>获取工作目录信息</td></tr>
|
||||
<tr><td>GET</td><td>/api/files/list</td><td>列出目录内容</td></tr>
|
||||
<tr><td>GET</td><td>/api/files/read</td><td>读取文件内容</td></tr>
|
||||
<tr><td>GET</td><td>/api/files/tree</td><td>获取目录树</td></tr>
|
||||
<tr><td>WS</td><td>/api/ws/:sessionId</td><td>WebSocket</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>12. 参考资料</h2>
|
||||
<ul>
|
||||
<li><a href="https://github.com/OpenHands/OpenHands" target="_blank">OpenHands 源码</a></li>
|
||||
<li><a href="https://github.com/sst/opencode" target="_blank">OpenCode 源码</a></li>
|
||||
<li><a href="https://hono.dev/" target="_blank">Hono 文档</a></li>
|
||||
<li><a href="https://tauri.app/" target="_blank">Tauri 文档</a></li>
|
||||
<li><a href="https://bun.sh/" target="_blank">Bun 文档</a></li>
|
||||
</ul>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,231 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="Agent 系统" description="AI Terminal Assistant Agent 引擎详解">
|
||||
<h1>Agent 系统</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 核心功能 - 完整实现</p>
|
||||
|
||||
<p>
|
||||
Agent 是 AI Terminal Assistant 的核心引擎,负责处理用户消息、调用工具、管理上下文。
|
||||
基于 Vercel AI SDK 构建,支持流式响应和多种 LLM 提供商。
|
||||
</p>
|
||||
|
||||
<h2>核心特性</h2>
|
||||
|
||||
<ul>
|
||||
<li><strong>流式响应</strong> - 实时输出 AI 生成内容</li>
|
||||
<li><strong>工具调用</strong> - 自动执行 23+ 内置工具</li>
|
||||
<li><strong>多轮对话</strong> - 完整的上下文管理</li>
|
||||
<li><strong>Vision 支持</strong> - 处理图片输入</li>
|
||||
<li><strong>Plan 模式</strong> - 复杂任务规划</li>
|
||||
<li><strong>多模型支持</strong> - Claude、OpenAI、DeepSeek 等</li>
|
||||
</ul>
|
||||
|
||||
<h2>架构概览</h2>
|
||||
|
||||
<pre><code class="language-text">┌─────────────────────────────────────────────────────────────┐
|
||||
│ Agent │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │
|
||||
│ │ Provider│ │ Tools │ │ Session │ │ Checkpoint │ │
|
||||
│ │ Registry│ │ Registry│ │ Manager │ │ Manager │ │
|
||||
│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │
|
||||
│ │ Hooks │ │ MCP │ │ LSP │ │ RepoMap │ │
|
||||
│ │ Manager │ │ Client │ │ Service │ │ Generator │ │
|
||||
│ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────────┘</code></pre>
|
||||
|
||||
<h2>基本用法</h2>
|
||||
|
||||
<pre><code class="language-typescript">import { Agent, toolRegistry } from '@ai-assistant/core';
|
||||
|
||||
// 创建 Agent 实例
|
||||
const agent = new Agent({
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
model: 'claude-sonnet-4-20250514',
|
||||
maxTokens: 4096,
|
||||
temperature: 0.7
|
||||
});
|
||||
|
||||
// 流式对话
|
||||
const response = await agent.chat('帮我优化这段代码', {
|
||||
onChunk: (chunk) => {
|
||||
// 处理流式输出
|
||||
process.stdout.write(chunk.content);
|
||||
},
|
||||
onToolCall: (toolCall) => {
|
||||
// 工具调用回调
|
||||
console.log(`调用工具: ${toolCall.name}`);
|
||||
},
|
||||
onComplete: (response) => {
|
||||
// 完成回调
|
||||
console.log('对话完成');
|
||||
}
|
||||
});</code></pre>
|
||||
|
||||
<h2>配置选项</h2>
|
||||
|
||||
<pre><code class="language-typescript">interface AgentConfig {
|
||||
// 必需
|
||||
apiKey: string; // API 密钥
|
||||
|
||||
// 模型配置
|
||||
model?: string; // 模型名称 (默认: claude-sonnet-4-20250514)
|
||||
maxTokens?: number; // 最大输出 token (默认: 4096)
|
||||
temperature?: number; // 温度参数 (默认: 0.7)
|
||||
|
||||
// 系统配置
|
||||
systemPrompt?: string; // 自定义系统提示词
|
||||
tools?: Tool[]; // 可用工具列表
|
||||
workDir?: string; // 工作目录
|
||||
|
||||
// 高级配置
|
||||
hooks?: HookConfig; // Hook 配置
|
||||
checkpointDir?: string; // 检查点目录
|
||||
mcpServers?: MCPServerConfig[]; // MCP 服务器配置
|
||||
}</code></pre>
|
||||
|
||||
<h2>Agent 预设</h2>
|
||||
|
||||
<p>内置多种 Agent 预设,适用于不同场景:</p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>预设</th>
|
||||
<th>说明</th>
|
||||
<th>适用场景</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><code>general</code></td>
|
||||
<td>通用编程助手</td>
|
||||
<td>日常编程任务</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>plan</code></td>
|
||||
<td>任务规划模式</td>
|
||||
<td>复杂任务分解</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>code-reviewer</code></td>
|
||||
<td>代码审查专家</td>
|
||||
<td>代码审查、优化建议</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>refactor</code></td>
|
||||
<td>重构专家</td>
|
||||
<td>代码重构、架构优化</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>消息处理流程</h2>
|
||||
|
||||
<pre><code class="language-text">用户消息
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ 消息预处理 │ ← 文件提及解析、上下文注入
|
||||
└────────┬────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ RepoMap 生成 │ ← 智能代码上下文
|
||||
└────────┬────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ LLM 调用 │ ← 流式响应
|
||||
└────────┬────────┘
|
||||
│
|
||||
┌────┴────┐
|
||||
│ │
|
||||
▼ ▼
|
||||
文本 工具调用
|
||||
输出 │
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ 工具执行 │ ← Checkpoint 自动创建
|
||||
└────────┬────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ 结果返回 LLM │
|
||||
└────────┬────────┘
|
||||
│
|
||||
▼
|
||||
继续生成...</code></pre>
|
||||
|
||||
<h2>上下文管理</h2>
|
||||
|
||||
<p>Agent 使用三层存储架构管理消息:</p>
|
||||
|
||||
<pre><code class="language-typescript">// 消息层级
|
||||
interface Message {
|
||||
id: string;
|
||||
role: 'user' | 'assistant' | 'system';
|
||||
parts: Part[]; // 消息包含多个部分
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
// 部分类型
|
||||
type Part =
|
||||
| TextPart // 文本内容
|
||||
| ToolPart // 工具调用
|
||||
| ReasoningPart // 推理过程
|
||||
| ImagePart; // 图片内容</code></pre>
|
||||
|
||||
<h2>Vision 支持</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 发送带图片的消息
|
||||
await agent.chat([
|
||||
{
|
||||
type: 'text',
|
||||
content: '这个界面有什么问题?'
|
||||
},
|
||||
{
|
||||
type: 'image',
|
||||
source: {
|
||||
type: 'file',
|
||||
path: '/path/to/screenshot.png'
|
||||
}
|
||||
}
|
||||
]);</code></pre>
|
||||
|
||||
<h2>代码位置</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>模块</th>
|
||||
<th>文件</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>Agent 主类</td><td><code>core/agent.ts</code></td><td>Agent 核心实现</td></tr>
|
||||
<tr><td>配置管理</td><td><code>core/config.ts</code></td><td>配置加载和验证</td></tr>
|
||||
<tr><td>类型定义</td><td><code>types/index.ts</code></td><td>所有类型定义</td></tr>
|
||||
<tr><td>Provider</td><td><code>providers/</code></td><td>多模型提供商</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>相关文档</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/core/tools" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">工具系统</h3>
|
||||
<p class="text-sm text-gray-400">了解 23+ 内置工具</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/features/checkpoint" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">Checkpoint 系统</h3>
|
||||
<p class="text-sm text-gray-400">安全的代码回滚机制</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,258 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="MCP 集成" description="Model Context Protocol 集成详解">
|
||||
<h1>MCP 集成</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 核心功能 - 完整实现</p>
|
||||
|
||||
<p>
|
||||
Model Context Protocol (MCP) 是一个开放协议,允许 AI 应用与外部数据源和工具进行标准化交互。
|
||||
AI Terminal Assistant 完整支持 MCP,可以通过外部服务器扩展 AI 能力。
|
||||
</p>
|
||||
|
||||
<h2>核心特性</h2>
|
||||
|
||||
<ul>
|
||||
<li><strong>工具扩展</strong> - 通过 MCP 服务器添加新工具</li>
|
||||
<li><strong>资源访问</strong> - 访问外部数据源</li>
|
||||
<li><strong>提示模板</strong> - 使用预定义的提示模板</li>
|
||||
<li><strong>自动发现</strong> - 动态发现服务器能力</li>
|
||||
</ul>
|
||||
|
||||
<h2>架构概览</h2>
|
||||
|
||||
<pre><code class="language-text">┌─────────────────────────────────────────────────────────────┐
|
||||
│ AI Terminal Assistant │
|
||||
├─────────────────────────────────────────────────────────────┤
|
||||
│ MCP Client │
|
||||
│ ┌───────────┐ ┌───────────┐ ┌───────────┐ │
|
||||
│ │ Tools │ │ Resources │ │ Prompts │ │
|
||||
│ │ Manager │ │ Manager │ │ Manager │ │
|
||||
│ └─────┬─────┘ └─────┬─────┘ └─────┬─────┘ │
|
||||
│ │ │ │ │
|
||||
│ └──────────────┼──────────────┘ │
|
||||
│ │ │
|
||||
│ ┌────────▼────────┐ │
|
||||
│ │ Transport Layer │ ← stdio / SSE / WebSocket│
|
||||
│ └────────┬────────┘ │
|
||||
└───────────────────────┼─────────────────────────────────────┘
|
||||
│
|
||||
┌───────────────┼───────────────┐
|
||||
│ │ │
|
||||
▼ ▼ ▼
|
||||
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
|
||||
│ Filesystem │ │ GitHub │ │ Custom │
|
||||
│ Server │ │ Server │ │ Server │
|
||||
└─────────────┘ └─────────────┘ └─────────────┘</code></pre>
|
||||
|
||||
<h2>配置 MCP 服务器</h2>
|
||||
|
||||
<h3>配置文件</h3>
|
||||
|
||||
<p>在项目根目录创建 <code>mcp.json</code> 或在 <code>.ai-assistant/</code> 目录下配置:</p>
|
||||
|
||||
<pre><code class="language-json">{
|
||||
"mcpServers": {
|
||||
"filesystem": {
|
||||
"command": "npx",
|
||||
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/dir"],
|
||||
"env": {}
|
||||
},
|
||||
"github": {
|
||||
"command": "npx",
|
||||
"args": ["-y", "@modelcontextprotocol/server-github"],
|
||||
"env": {
|
||||
"GITHUB_TOKEN": "your_github_token"
|
||||
}
|
||||
},
|
||||
"sqlite": {
|
||||
"command": "uvx",
|
||||
"args": ["mcp-server-sqlite", "--db-path", "/path/to/database.db"]
|
||||
}
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h3>代码配置</h3>
|
||||
|
||||
<pre><code class="language-typescript">import { MCPClient } from '@ai-assistant/core/mcp';
|
||||
|
||||
// 创建 MCP 客户端
|
||||
const mcpClient = new MCPClient();
|
||||
|
||||
// 连接服务器
|
||||
await mcpClient.connect({
|
||||
name: 'filesystem',
|
||||
command: 'npx',
|
||||
args: ['-y', '@modelcontextprotocol/server-filesystem', '/project'],
|
||||
env: {}
|
||||
});
|
||||
|
||||
// 获取可用工具
|
||||
const tools = await mcpClient.listTools();
|
||||
console.log('可用工具:', tools);
|
||||
|
||||
// 调用工具
|
||||
const result = await mcpClient.callTool('read_file', {
|
||||
path: '/project/package.json'
|
||||
});</code></pre>
|
||||
|
||||
<h2>官方 MCP 服务器</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>服务器</th>
|
||||
<th>功能</th>
|
||||
<th>安装命令</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><code>@modelcontextprotocol/server-filesystem</code></td>
|
||||
<td>文件系统访问</td>
|
||||
<td><code>npx -y @modelcontextprotocol/server-filesystem</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>@modelcontextprotocol/server-github</code></td>
|
||||
<td>GitHub API</td>
|
||||
<td><code>npx -y @modelcontextprotocol/server-github</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>@modelcontextprotocol/server-postgres</code></td>
|
||||
<td>PostgreSQL 数据库</td>
|
||||
<td><code>npx -y @modelcontextprotocol/server-postgres</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>mcp-server-sqlite</code></td>
|
||||
<td>SQLite 数据库</td>
|
||||
<td><code>uvx mcp-server-sqlite</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>@modelcontextprotocol/server-slack</code></td>
|
||||
<td>Slack 集成</td>
|
||||
<td><code>npx -y @modelcontextprotocol/server-slack</code></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>工具自动注册</h2>
|
||||
|
||||
<p>MCP 服务器的工具会自动注册到 Agent 的工具系统:</p>
|
||||
|
||||
<pre><code class="language-typescript">// 初始化 Agent 时传入 MCP 配置
|
||||
const agent = new Agent({
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
mcpServers: [
|
||||
{
|
||||
name: 'github',
|
||||
command: 'npx',
|
||||
args: ['-y', '@modelcontextprotocol/server-github']
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
// Agent 可以直接使用 MCP 工具
|
||||
await agent.chat('创建一个新的 GitHub issue');
|
||||
// AI 将自动调用 github 服务器的 create_issue 工具</code></pre>
|
||||
|
||||
<h2>资源访问</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 列出可用资源
|
||||
const resources = await mcpClient.listResources();
|
||||
|
||||
// 读取资源
|
||||
const content = await mcpClient.readResource('file:///project/README.md');
|
||||
|
||||
// 订阅资源变化
|
||||
mcpClient.subscribeResource('file:///project/config.json', (update) => {
|
||||
console.log('配置已更新:', update);
|
||||
});</code></pre>
|
||||
|
||||
<h2>提示模板</h2>
|
||||
|
||||
<pre><code class="language-typescript">// 列出可用提示模板
|
||||
const prompts = await mcpClient.listPrompts();
|
||||
|
||||
// 获取提示
|
||||
const prompt = await mcpClient.getPrompt('code-review', {
|
||||
file: 'src/index.ts'
|
||||
});
|
||||
|
||||
// 使用提示
|
||||
await agent.chat(prompt.messages);</code></pre>
|
||||
|
||||
<h2>创建自定义 MCP 服务器</h2>
|
||||
|
||||
<pre><code class="language-typescript">import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
|
||||
const server = new Server(
|
||||
{ name: 'my-server', version: '1.0.0' },
|
||||
{ capabilities: { tools: {} } }
|
||||
);
|
||||
|
||||
// 注册工具
|
||||
server.setRequestHandler('tools/list', async () => ({
|
||||
tools: [
|
||||
{
|
||||
name: 'my_tool',
|
||||
description: '自定义工具',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
input: { type: 'string' }
|
||||
},
|
||||
required: ['input']
|
||||
}
|
||||
}
|
||||
]
|
||||
}));
|
||||
|
||||
server.setRequestHandler('tools/call', async (request) => {
|
||||
if (request.params.name === 'my_tool') {
|
||||
return {
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: `处理结果: ${request.params.arguments.input}`
|
||||
}]
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);</code></pre>
|
||||
|
||||
<h2>代码位置</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>模块</th>
|
||||
<th>文件</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>MCP 客户端</td><td><code>mcp/client.ts</code></td><td>MCP 客户端实现</td></tr>
|
||||
<tr><td>协议定义</td><td><code>mcp/protocol.ts</code></td><td>协议类型定义</td></tr>
|
||||
<tr><td>传输层</td><td><code>mcp/transport.ts</code></td><td>stdio/SSE 传输</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>相关文档</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/core/tools" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">工具系统</h3>
|
||||
<p class="text-sm text-gray-400">了解内置工具系统</p>
|
||||
</a>
|
||||
|
||||
<a href="https://modelcontextprotocol.io" target="_blank" rel="noopener" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">MCP 官方文档 ↗</h3>
|
||||
<p class="text-sm text-gray-400">Model Context Protocol 官方文档</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,280 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="工具系统" description="AI Terminal Assistant 23+ 内置工具详解">
|
||||
<h1>工具系统</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 核心功能 - 23+ 内置工具</p>
|
||||
|
||||
<p>
|
||||
工具系统是 AI Terminal Assistant 的核心能力,允许 AI 执行各种操作:
|
||||
文件读写、代码搜索、Shell 命令、Git 操作等。
|
||||
</p>
|
||||
|
||||
<h2>工具分类</h2>
|
||||
|
||||
<h3>文件操作</h3>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工具名</th>
|
||||
<th>功能</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>read_file</code></td><td>读取文件</td><td>支持文本、图片、PDF</td></tr>
|
||||
<tr><td><code>write_file</code></td><td>写入文件</td><td>创建或覆盖文件</td></tr>
|
||||
<tr><td><code>edit_file</code></td><td>编辑文件</td><td>Search-Replace 模式</td></tr>
|
||||
<tr><td><code>delete_file</code></td><td>删除文件</td><td>安全删除</td></tr>
|
||||
<tr><td><code>move_file</code></td><td>移动/重命名</td><td>文件移动</td></tr>
|
||||
<tr><td><code>copy_file</code></td><td>复制文件</td><td>文件复制</td></tr>
|
||||
<tr><td><code>list_directory</code></td><td>列出目录</td><td>目录浏览</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>代码搜索</h3>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工具名</th>
|
||||
<th>功能</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>grep</code></td><td>内容搜索</td><td>正则表达式搜索</td></tr>
|
||||
<tr><td><code>glob</code></td><td>文件匹配</td><td>按模式查找文件</td></tr>
|
||||
<tr><td><code>find_definition</code></td><td>定义查找</td><td>LSP 集成</td></tr>
|
||||
<tr><td><code>find_references</code></td><td>引用查找</td><td>LSP 集成</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>Shell 执行</h3>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工具名</th>
|
||||
<th>功能</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>bash</code></td><td>执行命令</td><td>Shell 命令执行</td></tr>
|
||||
<tr><td><code>background_bash</code></td><td>后台执行</td><td>长时间运行任务</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>Git 操作</h3>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工具名</th>
|
||||
<th>功能</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>git_status</code></td><td>查看状态</td><td>工作区状态</td></tr>
|
||||
<tr><td><code>git_diff</code></td><td>查看差异</td><td>文件变更对比</td></tr>
|
||||
<tr><td><code>git_log</code></td><td>提交历史</td><td>查看 Git 日志</td></tr>
|
||||
<tr><td><code>git_commit</code></td><td>提交更改</td><td>创建 Git 提交</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>Web 工具</h3>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工具名</th>
|
||||
<th>功能</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>web_search</code></td><td>网页搜索</td><td>Tavily API 集成</td></tr>
|
||||
<tr><td><code>web_fetch</code></td><td>获取网页</td><td>抓取网页内容</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3>系统工具</h3>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工具名</th>
|
||||
<th>功能</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>think</code></td><td>思考</td><td>AI 内部推理</td></tr>
|
||||
<tr><td><code>ask_user</code></td><td>询问用户</td><td>交互式确认</td></tr>
|
||||
<tr><td><code>checkpoint</code></td><td>检查点</td><td>创建恢复点</td></tr>
|
||||
<tr><td><code>todo</code></td><td>任务管理</td><td>任务追踪</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>工具注册</h2>
|
||||
|
||||
<pre><code class="language-typescript">import { toolRegistry } from '@ai-assistant/core/tools';
|
||||
import { z } from 'zod';
|
||||
|
||||
// 注册自定义工具
|
||||
toolRegistry.register({
|
||||
name: 'my_custom_tool',
|
||||
description: '自定义工具描述',
|
||||
parameters: z.object({
|
||||
input: z.string().describe('输入参数'),
|
||||
options: z.object({
|
||||
format: z.enum(['json', 'text']).optional()
|
||||
}).optional()
|
||||
}),
|
||||
execute: async (params) => {
|
||||
// 工具实现
|
||||
const result = await doSomething(params.input);
|
||||
return {
|
||||
success: true,
|
||||
result: result
|
||||
};
|
||||
}
|
||||
});</code></pre>
|
||||
|
||||
<h2>工具权限</h2>
|
||||
|
||||
<p>工具执行遵循权限控制机制:</p>
|
||||
|
||||
<pre><code class="language-typescript">interface PermissionConfig {
|
||||
// 允许的路径
|
||||
allowedPaths: string[];
|
||||
deniedPaths: string[];
|
||||
|
||||
// 工具权限
|
||||
allowedTools: string[];
|
||||
deniedTools: string[];
|
||||
|
||||
// Shell 命令
|
||||
allowShellCommands: boolean;
|
||||
shellCommandPatterns: string[];
|
||||
|
||||
// 自动批准规则
|
||||
autoApproveRules: AutoApproveRule[];
|
||||
}</code></pre>
|
||||
|
||||
<h2>工具执行流程</h2>
|
||||
|
||||
<pre><code class="language-text">AI 请求工具调用
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ 权限检查 │ ← 检查工具是否允许
|
||||
└────────┬────────┘
|
||||
│
|
||||
允许 │ 拒绝
|
||||
┌────┴────┐
|
||||
│ │
|
||||
▼ ▼
|
||||
┌──────────┐ 返回
|
||||
│ 创建检查点 │ 错误
|
||||
└────┬─────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ 执行工具 │
|
||||
└────────┬────────┘
|
||||
│
|
||||
成功 │ 失败
|
||||
┌────┴────┐
|
||||
│ │
|
||||
▼ ▼
|
||||
返回 返回
|
||||
结果 错误</code></pre>
|
||||
|
||||
<h2>工具描述文件</h2>
|
||||
|
||||
<p>每个工具都有独立的描述文件,位于 <code>tools/descriptions/</code>:</p>
|
||||
|
||||
<pre><code class="language-markdown"># read_file
|
||||
|
||||
读取指定路径的文件内容。
|
||||
|
||||
## 参数
|
||||
|
||||
- `path` (string, 必需): 文件的绝对路径
|
||||
- `encoding` (string, 可选): 文件编码,默认 utf-8
|
||||
|
||||
## 返回值
|
||||
|
||||
文件内容字符串,或 base64 编码的二进制数据。
|
||||
|
||||
## 示例
|
||||
|
||||
读取配置文件:
|
||||
```json
|
||||
{
|
||||
"path": "/project/config.json"
|
||||
}
|
||||
```</code></pre>
|
||||
|
||||
<h2>MCP 工具扩展</h2>
|
||||
|
||||
<p>通过 Model Context Protocol 集成外部工具:</p>
|
||||
|
||||
<pre><code class="language-typescript">// 配置 MCP 服务器
|
||||
const mcpConfig = {
|
||||
servers: [
|
||||
{
|
||||
name: 'filesystem',
|
||||
command: 'npx',
|
||||
args: ['-y', '@modelcontextprotocol/server-filesystem']
|
||||
},
|
||||
{
|
||||
name: 'github',
|
||||
command: 'npx',
|
||||
args: ['-y', '@modelcontextprotocol/server-github']
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
// 初始化后,MCP 工具自动注册到 toolRegistry</code></pre>
|
||||
|
||||
<h2>代码位置</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>模块</th>
|
||||
<th>文件</th>
|
||||
<th>说明</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>工具注册</td><td><code>tools/registry.ts</code></td><td>工具注册系统</td></tr>
|
||||
<tr><td>文件工具</td><td><code>tools/file.ts</code></td><td>文件操作</td></tr>
|
||||
<tr><td>搜索工具</td><td><code>tools/search.ts</code></td><td>代码搜索</td></tr>
|
||||
<tr><td>Shell 工具</td><td><code>tools/bash.ts</code></td><td>命令执行</td></tr>
|
||||
<tr><td>Git 工具</td><td><code>tools/git.ts</code></td><td>Git 操作</td></tr>
|
||||
<tr><td>工具描述</td><td><code>tools/descriptions/</code></td><td>工具文档</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>相关文档</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/core/agent" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">Agent 系统</h3>
|
||||
<p class="text-sm text-gray-400">了解 Agent 如何调用工具</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/core/mcp" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">MCP 集成</h3>
|
||||
<p class="text-sm text-gray-400">扩展更多外部工具</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,67 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="浏览器 GUI" description="Web 图形界面">
|
||||
<h1>浏览器 GUI (Browser GUI)</h1>
|
||||
|
||||
<p>基于浏览器的图形界面,允许用户通过 Web 界面与 AI 交互。对于不熟悉命令行的用户更加友好,也支持更丰富的展示效果。</p>
|
||||
|
||||
<h2>核心功能</h2>
|
||||
<ul>
|
||||
<li><strong>Web 界面</strong> - React SPA 应用</li>
|
||||
<li><strong>实时通信</strong> - WebSocket 支持流式输出</li>
|
||||
<li><strong>文件浏览</strong> - 可视化文件选择</li>
|
||||
<li><strong>Diff 展示</strong> - 彩色代码差异展示</li>
|
||||
<li><strong>Markdown 渲染</strong> - 富文本消息展示</li>
|
||||
</ul>
|
||||
|
||||
<h2>技术栈</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>组件</th>
|
||||
<th>选择</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>前端框架</td><td>React + TypeScript</td></tr>
|
||||
<tr><td>构建工具</td><td>Vite</td></tr>
|
||||
<tr><td>样式</td><td>Tailwind CSS</td></tr>
|
||||
<tr><td>状态管理</td><td>Zustand</td></tr>
|
||||
<tr><td>实时通信</td><td>WebSocket</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>核心组件</h2>
|
||||
<ul>
|
||||
<li><strong>Sidebar</strong> - 会话列表管理</li>
|
||||
<li><strong>ChatMessage</strong> - 消息展示(支持流式)</li>
|
||||
<li><strong>ChatInput</strong> - 输入框(支持多行)</li>
|
||||
<li><strong>FileBrowser</strong> - 文件浏览器</li>
|
||||
<li><strong>ConfigPanel</strong> - 配置面板</li>
|
||||
</ul>
|
||||
|
||||
<h2>启动命令</h2>
|
||||
<pre><code class="language-bash"># 开发模式
|
||||
pnpm web:dev
|
||||
|
||||
# 构建生产版本
|
||||
pnpm web:build
|
||||
|
||||
# 预览构建结果
|
||||
pnpm web:preview</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<p><strong>优先级</strong>: 低 | <strong>状态</strong>: 已完成</p>
|
||||
|
||||
<h3>已完成功能</h3>
|
||||
<ul>
|
||||
<li>基础 UI 组件</li>
|
||||
<li>对话界面</li>
|
||||
<li>文件浏览器</li>
|
||||
<li>配置面板</li>
|
||||
<li>响应式布局</li>
|
||||
<li>PWA 支持</li>
|
||||
</ul>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,91 @@
|
||||
---
|
||||
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>
|
||||
@@ -0,0 +1,96 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="Checkpoint 系统" description="Shadow Git 检查点系统">
|
||||
<h1>Checkpoint 系统</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 已完成 - 核心功能及增强功能全部实现</p>
|
||||
|
||||
<p>完整的检查点系统,基于 Shadow Git 架构,提供安全可靠的代码恢复能力。代码位于 <code>packages/core/src/checkpoint/</code>。</p>
|
||||
|
||||
<h2>当前架构</h2>
|
||||
<pre><code>┌─────────────────────────────────────────────────────────────┐
|
||||
│ CheckpointManager │
|
||||
│ - createCheckpoint() - rollback() │
|
||||
│ - listCheckpoints() - undo() │
|
||||
│ - getDiff() - cleanup() │
|
||||
└─────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────┐
|
||||
│ ShadowGit │
|
||||
│ Storage: ~/.ai-assist/checkpoints/<cwdHash>/.git/ │
|
||||
│ - createCommit() - resetHard() │
|
||||
│ - getDiffSummary() - checkoutFiles() │
|
||||
│ - getFileDiff() - getFileContent() │
|
||||
└─────────────────────────────────────────────────────────────┘</code></pre>
|
||||
|
||||
<h2>已有功能</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>功能</th><th>状态</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>Shadow Git 隔离存储</td><td>已实现</td></tr>
|
||||
<tr><td>自动检查点触发</td><td>已实现</td></tr>
|
||||
<tr><td>手动检查点</td><td>已实现</td></tr>
|
||||
<tr><td>差异查看</td><td>已实现</td></tr>
|
||||
<tr><td>完整回滚</td><td>已实现</td></tr>
|
||||
<tr><td>选择性回滚</td><td>已实现</td></tr>
|
||||
<tr><td>自动清理</td><td>已实现</td></tr>
|
||||
<tr><td>事件系统</td><td>已实现</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>已实现功能</h2>
|
||||
|
||||
<h3>核心安全 (P0)</h3>
|
||||
<ul>
|
||||
<li>✅ <strong>Unrevert 功能</strong> - 支持撤销最近一次回滚操作 (<code>checkpoint-rollback.ts</code>)</li>
|
||||
<li>✅ <strong>7点安全检查</strong> - 完整的安全检查机制 (<code>safety.ts</code>)</li>
|
||||
</ul>
|
||||
|
||||
<h3>可用性增强 (P1)</h3>
|
||||
<ul>
|
||||
<li>✅ <strong>会话级跟踪</strong> - SessionTracker 跟踪所有修改 (<code>session-tracker.ts</code>)</li>
|
||||
<li>✅ <strong>并发控制</strong> - 文件锁防止多实例冲突</li>
|
||||
</ul>
|
||||
|
||||
<h3>体验优化 (P2)</h3>
|
||||
<ul>
|
||||
<li>✅ <strong>LFS 大文件支持</strong> - 自动检测和排除 LFS 文件 (<code>lfs.ts</code>)</li>
|
||||
<li>✅ <strong>工作区路径验证</strong> - 阻止敏感目录操作 (<code>path-validator.ts</code>)</li>
|
||||
<li>✅ <strong>智能提交消息</strong> - 人类可读的消息生成 (<code>commit-message.ts</code>)</li>
|
||||
</ul>
|
||||
|
||||
<h3>待实现 (P3)</h3>
|
||||
<ul>
|
||||
<li>📋 <strong>WebSocket 实时事件</strong> - 前端实时状态更新</li>
|
||||
<li>📋 <strong>可视化 UI</strong> - 检查点管理面板</li>
|
||||
</ul>
|
||||
|
||||
<h2>三种恢复模式</h2>
|
||||
<pre><code class="language-typescript">enum RestoreMode {
|
||||
AI_CHANGES_ONLY = 'ai_changes_only', // 仅恢复 AI 修改
|
||||
WORKSPACE_ONLY = 'workspace_only', // 仅恢复工作区变更
|
||||
FULL = 'full' // 完整恢复
|
||||
}</code></pre>
|
||||
|
||||
<h2>模块结构</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>模块</th><th>功能</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td><code>manager.ts</code></td><td>检查点核心管理器</td></tr>
|
||||
<tr><td><code>shadow-git.ts</code></td><td>Shadow Git 架构实现</td></tr>
|
||||
<tr><td><code>safety.ts</code></td><td>7点安全检查机制</td></tr>
|
||||
<tr><td><code>session-tracker.ts</code></td><td>会话级别追踪和统计</td></tr>
|
||||
<tr><td><code>checkpoint-rollback.ts</code></td><td>回滚和撤销回滚功能</td></tr>
|
||||
<tr><td><code>lfs.ts</code></td><td>Git LFS 大文件支持</td></tr>
|
||||
<tr><td><code>path-validator.ts</code></td><td>工作区路径验证</td></tr>
|
||||
<tr><td><code>commit-message.ts</code></td><td>智能提交消息生成</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,90 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="配置管理" description="多来源配置和验证系统">
|
||||
<h1>配置管理 (Configuration Management)</h1>
|
||||
|
||||
<p>灵活的配置管理系统,支持多种配置来源和优先级,让用户可以根据项目、环境和个人偏好定制工具行为。</p>
|
||||
|
||||
<h2>配置来源(优先级从高到低)</h2>
|
||||
<ol>
|
||||
<li><strong>命令行参数</strong> - 最高优先级</li>
|
||||
<li><strong>环境变量</strong> - <code>AI_ASSISTANT_*</code> 前缀</li>
|
||||
<li><strong>项目配置文件</strong> - <code>.ai-assistant.yml</code></li>
|
||||
<li><strong>用户配置文件</strong> - <code>~/.ai-assistant.yml</code></li>
|
||||
<li><strong>默认值</strong> - 代码中的默认配置</li>
|
||||
</ol>
|
||||
|
||||
<h2>配置文件格式</h2>
|
||||
<pre><code class="language-yaml"># .ai-assistant.yml
|
||||
|
||||
# 模型设置
|
||||
model: claude-3-5-sonnet
|
||||
|
||||
# 自动化
|
||||
auto-commit: true
|
||||
auto-lint: true
|
||||
auto-test: false
|
||||
|
||||
# 编辑模式
|
||||
edit-mode: search-replace
|
||||
|
||||
# Git 集成
|
||||
git:
|
||||
enabled: true
|
||||
auto-commit:
|
||||
enabled: true
|
||||
mode: batch
|
||||
batch-delay: 3000
|
||||
|
||||
# Linting
|
||||
linting:
|
||||
enabled: true
|
||||
auto-fix: true
|
||||
max-fix-attempts: 3
|
||||
|
||||
# 测试
|
||||
testing:
|
||||
enabled: true
|
||||
command: npm test
|
||||
auto-run: false
|
||||
|
||||
# 历史记录
|
||||
history:
|
||||
enabled: true
|
||||
save-dir: .ai-assistant/history
|
||||
max-sessions: 100
|
||||
|
||||
# 日志
|
||||
log-level: info</code></pre>
|
||||
|
||||
<h2>环境变量</h2>
|
||||
<pre><code class="language-bash"># 通过环境变量设置
|
||||
export AI_ASSISTANT_MODEL=gpt-4
|
||||
export AI_ASSISTANT_AUTO_COMMIT=true
|
||||
export AI_ASSISTANT_API_KEY=sk-xxx</code></pre>
|
||||
|
||||
<h2>命令行参数</h2>
|
||||
<pre><code class="language-bash">ai-assistant --model claude-3-opus --auto-lint --no-auto-commit</code></pre>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
<pre><code class="language-typescript">export class ConfigManager {
|
||||
private sources: ConfigSource[] = [];
|
||||
|
||||
addSource(source: ConfigSource): void;
|
||||
load(): Promise<AgentConfig>;
|
||||
get<K extends keyof AgentConfig>(key: K): AgentConfig[K];
|
||||
set<K extends keyof AgentConfig>(key: K, value: AgentConfig[K]): void;
|
||||
}
|
||||
|
||||
export interface ConfigSource {
|
||||
name: string;
|
||||
priority: number;
|
||||
load(): Promise<Partial<AgentConfig>>;
|
||||
}</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<p><strong>优先级</strong>: 高 | <strong>状态</strong>: 待实现</p>
|
||||
<p>配置管理是基础架构,应该尽早实现以支持其他功能的可配置性。</p>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,92 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="编辑模式" description="多种代码编辑模式设计">
|
||||
<h1>编辑模式 (Edit Modes)</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 已完成 - 三种编辑模式全部实现</p>
|
||||
|
||||
<p>AI 以不同的方式修改代码,在准确性、效率和 token 消耗之间有不同的权衡。编辑系统位于 <code>packages/core/src/editors/</code>。</p>
|
||||
|
||||
<h2>支持的编辑模式</h2>
|
||||
|
||||
<h3>1. Whole 模式</h3>
|
||||
<ul>
|
||||
<li><strong>描述</strong>: AI 返回完整的文件内容</li>
|
||||
<li><strong>优点</strong>: 最简单、最可靠,不会出现部分应用错误</li>
|
||||
<li><strong>缺点</strong>: 消耗大量 tokens,对于大文件不经济</li>
|
||||
<li><strong>适用场景</strong>: 小文件、需要大量修改的文件</li>
|
||||
</ul>
|
||||
|
||||
<h3>2. Diff 模式</h3>
|
||||
<ul>
|
||||
<li><strong>描述</strong>: AI 返回统一 diff 格式 (unified diff)</li>
|
||||
<li><strong>优点</strong>: token 消耗适中,修改清晰可见</li>
|
||||
<li><strong>缺点</strong>: diff 格式可能出错,需要准确的行号</li>
|
||||
<li><strong>适用场景</strong>: 中等大小文件的局部修改</li>
|
||||
</ul>
|
||||
|
||||
<h3>3. Search-Replace 模式</h3>
|
||||
<ul>
|
||||
<li><strong>描述</strong>: 使用搜索/替换块</li>
|
||||
<li><strong>优点</strong>: 不依赖行号,更容错</li>
|
||||
<li><strong>缺点</strong>: 搜索文本可能匹配到多处</li>
|
||||
<li><strong>适用场景</strong>: 代码重构、多处类似修改</li>
|
||||
</ul>
|
||||
|
||||
<h3>4. Architect 模式</h3>
|
||||
<ul>
|
||||
<li><strong>描述</strong>: 两阶段编辑 - 架构师 AI 规划,编辑器 AI 执行</li>
|
||||
<li><strong>优点</strong>: 规划与执行分离,可使用不同模型</li>
|
||||
<li><strong>缺点</strong>: 需要两次 AI 调用,延迟更高</li>
|
||||
<li><strong>适用场景</strong>: 复杂的多文件重构</li>
|
||||
</ul>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
|
||||
<pre><code class="language-typescript">// src/editors/types.ts
|
||||
export type EditMode = 'whole' | 'diff' | 'search-replace';
|
||||
|
||||
export interface EditResult {
|
||||
success: boolean;
|
||||
filePath: string;
|
||||
originalContent?: string;
|
||||
newContent?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface Editor {
|
||||
mode: EditMode;
|
||||
parse(response: string): Edit[];
|
||||
apply(edit: Edit): Promise<EditResult>;
|
||||
}</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>功能</th>
|
||||
<th>状态</th>
|
||||
<th>代码位置</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>Whole 模式</td><td>✅ 已实现</td><td><code>editors/whole.ts</code></td></tr>
|
||||
<tr><td>Diff 模式</td><td>✅ 已实现</td><td><code>editors/diff.ts</code></td></tr>
|
||||
<tr><td>Search-Replace 模式</td><td>✅ 已实现</td><td><code>editors/search-replace.ts</code></td></tr>
|
||||
<tr><td>统一编辑接口</td><td>✅ 已实现</td><td><code>editors/index.ts</code></td></tr>
|
||||
<tr><td>编辑解析器</td><td>✅ 已实现</td><td><code>editors/parsers.ts</code></td></tr>
|
||||
<tr><td>编辑验证器</td><td>✅ 已实现</td><td><code>editors/validator.ts</code></td></tr>
|
||||
<tr><td>编辑应用器</td><td>✅ 已实现</td><td><code>editors/applier.ts</code></td></tr>
|
||||
<tr><td>Architect 模式</td><td>📋 规划中</td><td>-</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>相关模块</h2>
|
||||
<ul>
|
||||
<li><strong>parsers.ts</strong> - 解析不同格式的编辑指令</li>
|
||||
<li><strong>validator.ts</strong> - 验证编辑内容的正确性</li>
|
||||
<li><strong>applier.ts</strong> - 应用编辑到文件并支持预览</li>
|
||||
</ul>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="Linting 集成" description="代码编辑后自动运行 linter 并修复">
|
||||
<h1>Linting 集成 (Linting Integration)</h1>
|
||||
|
||||
<p>在 AI 编辑代码后自动运行 linter,检测并修复语法和风格错误,确保 AI 生成的代码符合项目标准。</p>
|
||||
|
||||
<h2>工作流程</h2>
|
||||
<ol>
|
||||
<li>AI 编辑代码文件</li>
|
||||
<li>自动运行配置的 linter</li>
|
||||
<li>解析 linter 输出</li>
|
||||
<li>如果有错误,将错误反馈给 AI</li>
|
||||
<li>AI 尝试修复错误</li>
|
||||
<li>重复直到通过或达到最大尝试次数</li>
|
||||
</ol>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
|
||||
<h3>类型定义</h3>
|
||||
<pre><code class="language-typescript">export interface LintError {
|
||||
file: string;
|
||||
line: number;
|
||||
column: number;
|
||||
severity: 'error' | 'warning' | 'info';
|
||||
message: string;
|
||||
rule?: string;
|
||||
source: string;
|
||||
}
|
||||
|
||||
export interface LinterConfig {
|
||||
enabled: boolean;
|
||||
command: string;
|
||||
args?: string[];
|
||||
parseOutput: (output: string) => LintError[];
|
||||
}</code></pre>
|
||||
|
||||
<h3>默认 Linter 配置</h3>
|
||||
<pre><code class="language-typescript">export const DEFAULT_LINTERS = {
|
||||
'.ts': {
|
||||
command: 'npx',
|
||||
args: ['eslint', '--format', 'json', '{file}'],
|
||||
parseOutput: parseESLintOutput,
|
||||
},
|
||||
'.py': {
|
||||
command: 'python',
|
||||
args: ['-m', 'flake8', '--format', 'json', '{file}'],
|
||||
parseOutput: parseFlake8Output,
|
||||
},
|
||||
'.go': {
|
||||
command: 'golint',
|
||||
args: ['{file}'],
|
||||
parseOutput: parseGolintOutput,
|
||||
},
|
||||
'.rs': {
|
||||
command: 'cargo',
|
||||
args: ['clippy', '--message-format', 'json'],
|
||||
parseOutput: parseCargoOutput,
|
||||
},
|
||||
};</code></pre>
|
||||
|
||||
<h2>配置示例</h2>
|
||||
<pre><code class="language-yaml"># .ai-assistant.yml
|
||||
linting:
|
||||
enabled: true
|
||||
autoFix: true
|
||||
maxFixAttempts: 3
|
||||
ignoreRules: ['no-console']
|
||||
ignorePatterns: ['*.test.ts']</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<p><strong>优先级</strong>: 高 | <strong>状态</strong>: 待实现</p>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,107 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="多模型支持" description="多 AI 模型提供商支持">
|
||||
<h1>多模型支持 (Multi-Model Support)</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 已完成 - Provider 注册系统已实现</p>
|
||||
|
||||
<p>通过 Provider 注册系统支持多种 AI 模型提供商。代码位于 <code>packages/core/src/provider/</code>。</p>
|
||||
|
||||
<h2>支持的提供商</h2>
|
||||
<ul>
|
||||
<li><strong>Anthropic</strong> - Claude 3/3.5/4 系列</li>
|
||||
<li><strong>OpenAI</strong> - GPT-4, GPT-3.5</li>
|
||||
<li><strong>Google</strong> - Gemini 系列</li>
|
||||
<li><strong>DeepSeek</strong> - DeepSeek 系列</li>
|
||||
<li><strong>Azure OpenAI</strong></li>
|
||||
<li><strong>本地模型</strong> - Ollama, llama.cpp</li>
|
||||
<li><strong>OpenRouter</strong> - 多模型代理</li>
|
||||
</ul>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
|
||||
<h3>模型配置</h3>
|
||||
<pre><code class="language-typescript">export interface ModelConfig {
|
||||
id: string;
|
||||
provider: ModelProvider;
|
||||
name: string;
|
||||
capabilities: {
|
||||
maxContextTokens: number;
|
||||
maxOutputTokens: number;
|
||||
supportsImages: boolean;
|
||||
supportsTools: boolean;
|
||||
supportStreaming: boolean;
|
||||
};
|
||||
editFormat: EditMode;
|
||||
useRepoMap: boolean;
|
||||
pricing?: {
|
||||
inputPer1kTokens: number;
|
||||
outputPer1kTokens: number;
|
||||
};
|
||||
}
|
||||
|
||||
export type ModelProvider =
|
||||
| 'anthropic'
|
||||
| 'openai'
|
||||
| 'google'
|
||||
| 'deepseek'
|
||||
| 'azure'
|
||||
| 'ollama'
|
||||
| 'openrouter';</code></pre>
|
||||
|
||||
<h3>Architect 模式</h3>
|
||||
<pre><code class="language-typescript">export class ArchitectMode {
|
||||
private plannerModel: ModelConfig; // 规划模型 (更强)
|
||||
private editorModel: ModelConfig; // 执行模型 (更快/便宜)
|
||||
|
||||
async execute(task: Task): Promise<Result> {
|
||||
// 1. 使用规划模型分析任务
|
||||
const plan = await this.plan(task);
|
||||
// 2. 使用编辑模型执行每个步骤
|
||||
const results = await this.executeSteps(plan.steps);
|
||||
return this.summarize(results);
|
||||
}
|
||||
}</code></pre>
|
||||
|
||||
<h2>配置示例</h2>
|
||||
<pre><code class="language-typescript">const config = {
|
||||
models: {
|
||||
default: 'claude-3-5-sonnet',
|
||||
taskModels: {
|
||||
planning: 'claude-3-opus',
|
||||
coding: 'claude-3-5-sonnet',
|
||||
review: 'gpt-4',
|
||||
quick: 'claude-3-haiku',
|
||||
},
|
||||
architect: {
|
||||
planner: 'claude-3-opus',
|
||||
editor: 'claude-3-haiku',
|
||||
},
|
||||
},
|
||||
};</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>功能</th><th>状态</th><th>代码位置</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>Provider 注册系统</td><td>✅ 已实现</td><td><code>provider/registry.ts</code></td></tr>
|
||||
<tr><td>Anthropic 支持</td><td>✅ 已实现</td><td><code>provider/builtin/</code></td></tr>
|
||||
<tr><td>OpenAI 兼容提供商</td><td>✅ 已实现</td><td><code>provider/utils.ts</code></td></tr>
|
||||
<tr><td>Provider 配置管理</td><td>✅ 已实现</td><td><code>provider/config.ts</code></td></tr>
|
||||
<tr><td>连接测试</td><td>✅ 已实现</td><td><code>provider/utils.ts</code></td></tr>
|
||||
<tr><td>Architect 模式</td><td>📋 规划中</td><td>-</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>模块结构</h2>
|
||||
<ul>
|
||||
<li><strong>registry.ts</strong> - Provider 注册中心,管理所有模型提供商</li>
|
||||
<li><strong>builtin/</strong> - 内置提供商实现(Anthropic、OpenAI 等)</li>
|
||||
<li><strong>config.ts</strong> - 提供商配置管理</li>
|
||||
<li><strong>utils.ts</strong> - 连接测试和 OpenAI 兼容适配</li>
|
||||
</ul>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="仓库地图" description="基于 AST 和 PageRank 的代码仓库地图">
|
||||
<h1>仓库地图 (Repo Map)</h1>
|
||||
|
||||
<p class="text-green-400 font-medium">✅ 已完成 - AST 解析和 PageRank 排序已实现</p>
|
||||
|
||||
<p>使用 tree-sitter 进行源代码分析,生成精确的代码结构图。代码位于 <code>packages/core/src/repomap/</code>。</p>
|
||||
|
||||
<h2>核心功能</h2>
|
||||
|
||||
<h3>1. Tree-sitter 解析</h3>
|
||||
<ul>
|
||||
<li>支持多种编程语言</li>
|
||||
<li>提取类、函数、变量定义</li>
|
||||
<li>分析代码引用关系</li>
|
||||
</ul>
|
||||
|
||||
<h3>2. 智能排序</h3>
|
||||
<ul>
|
||||
<li>按相关性排序文件</li>
|
||||
<li>PageRank 算法分析引用关系</li>
|
||||
<li>优先显示最相关的代码</li>
|
||||
</ul>
|
||||
|
||||
<h3>3. Token 优化</h3>
|
||||
<ul>
|
||||
<li>根据 context window 动态裁剪</li>
|
||||
<li>只包含必要的符号信息</li>
|
||||
<li>自动省略不相关的代码</li>
|
||||
</ul>
|
||||
|
||||
<h2>增强建议</h2>
|
||||
|
||||
<h3>引用分析</h3>
|
||||
<pre><code class="language-typescript">export interface Reference {
|
||||
from: SymbolLocation;
|
||||
to: SymbolLocation;
|
||||
type: 'call' | 'import' | 'extend' | 'implement';
|
||||
}
|
||||
|
||||
export class ReferenceAnalyzer {
|
||||
analyzeReferences(files: FileSymbols[]): Reference[];
|
||||
buildReferenceGraph(references: Reference[]): ReferenceGraph;
|
||||
}</code></pre>
|
||||
|
||||
<h3>相关性排序</h3>
|
||||
<pre><code class="language-typescript">export class RelevanceRanker {
|
||||
rankByRelevance(
|
||||
targetFiles: string[],
|
||||
allFiles: FileSymbols[],
|
||||
references: Reference[]
|
||||
): RankedFile[];
|
||||
|
||||
calculateRelevance(file: FileSymbols, context: TaskContext): number;
|
||||
}</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr><th>功能</th><th>状态</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr><td>Tree-sitter AST 解析</td><td>✅ 已实现</td></tr>
|
||||
<tr><td>PageRank 相关性排序</td><td>✅ 已实现</td></tr>
|
||||
<tr><td>多语言支持</td><td>✅ 已实现</td></tr>
|
||||
<tr><td>Token 预算管理</td><td>✅ 已实现</td></tr>
|
||||
<tr><td>符号提取</td><td>✅ 已实现</td></tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="测试集成" description="代码修改后自动运行测试并修复">
|
||||
<h1>测试集成 (Test Integration)</h1>
|
||||
|
||||
<p>在代码修改后自动运行测试,并在测试失败时将错误信息反馈给 AI 进行修复,形成"编辑-测试-修复"的闭环。</p>
|
||||
|
||||
<h2>工作流程</h2>
|
||||
<ol>
|
||||
<li>AI 编辑代码</li>
|
||||
<li>运行相关测试</li>
|
||||
<li>如果测试失败,解析错误信息</li>
|
||||
<li>将错误反馈给 AI</li>
|
||||
<li>AI 修复代码</li>
|
||||
<li>重复直到测试通过或达到限制</li>
|
||||
</ol>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
|
||||
<h3>类型定义</h3>
|
||||
<pre><code class="language-typescript">export interface TestResult {
|
||||
success: boolean;
|
||||
passed: number;
|
||||
failed: number;
|
||||
skipped: number;
|
||||
duration: number;
|
||||
failures: TestFailure[];
|
||||
output: string;
|
||||
}
|
||||
|
||||
export interface TestFailure {
|
||||
testName: string;
|
||||
file?: string;
|
||||
line?: number;
|
||||
message: string;
|
||||
stack?: string;
|
||||
expected?: string;
|
||||
actual?: string;
|
||||
}</code></pre>
|
||||
|
||||
<h3>默认测试配置</h3>
|
||||
<pre><code class="language-typescript">export const DEFAULT_TEST_CONFIGS = {
|
||||
jest: {
|
||||
command: 'npx',
|
||||
args: ['jest', '--json', '--testLocationInResults'],
|
||||
testFilePatterns: ['**/*.test.ts', '**/*.spec.ts'],
|
||||
},
|
||||
vitest: {
|
||||
command: 'npx',
|
||||
args: ['vitest', 'run', '--reporter=json'],
|
||||
testFilePatterns: ['**/*.test.ts', '**/*.spec.ts'],
|
||||
},
|
||||
pytest: {
|
||||
command: 'python',
|
||||
args: ['-m', 'pytest', '--tb=short', '-v'],
|
||||
testFilePatterns: ['**/test_*.py', '**/*_test.py'],
|
||||
},
|
||||
};</code></pre>
|
||||
|
||||
<h2>配置示例</h2>
|
||||
<pre><code class="language-yaml"># .ai-assistant.yml
|
||||
testing:
|
||||
enabled: true
|
||||
runner: vitest
|
||||
autoRun: true
|
||||
autoFix: true
|
||||
maxFixAttempts: 3
|
||||
timeout: 60000
|
||||
runRelatedOnly: true</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<p><strong>优先级</strong>: 高 | <strong>状态</strong>: 待实现</p>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="语音输入" description="语音转文字与 AI 交互">
|
||||
<h1>语音输入 (Voice Input)</h1>
|
||||
|
||||
<p>支持语音输入功能,用户可以通过语音与 AI 交互。对于快速描述需求、解放双手编程等场景非常有用。</p>
|
||||
|
||||
<h2>核心功能</h2>
|
||||
<ul>
|
||||
<li><strong>语音录制</strong> - 录制用户语音</li>
|
||||
<li><strong>语音转文字</strong> - 使用 Whisper API 转换</li>
|
||||
<li><strong>自动发送</strong> - 转换后自动发送给 AI</li>
|
||||
<li><strong>多语言支持</strong> - 支持多种语言的语音</li>
|
||||
</ul>
|
||||
|
||||
<h2>支持的提供商</h2>
|
||||
<ul>
|
||||
<li><strong>OpenAI Whisper</strong> - 云端语音识别</li>
|
||||
<li><strong>Google Speech</strong> - Google 语音 API</li>
|
||||
<li><strong>Azure Speech</strong> - Azure 语音服务</li>
|
||||
<li><strong>Local Whisper</strong> - 本地 whisper.cpp</li>
|
||||
</ul>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
<pre><code class="language-typescript">export interface VoiceConfig {
|
||||
enabled: boolean;
|
||||
provider: 'whisper' | 'google' | 'azure' | 'local';
|
||||
language?: string;
|
||||
sampleRate: number;
|
||||
channels: number;
|
||||
silenceThreshold: number;
|
||||
silenceDuration: number;
|
||||
}
|
||||
|
||||
export interface VoiceManager {
|
||||
startRecording(): Promise<void>;
|
||||
stopAndTranscribe(): Promise<string>;
|
||||
voiceToText(): Promise<string>;
|
||||
}</code></pre>
|
||||
|
||||
<h2>配置示例</h2>
|
||||
<pre><code class="language-yaml"># .ai-assistant.yml
|
||||
voice:
|
||||
enabled: false
|
||||
provider: whisper
|
||||
language: zh-CN
|
||||
sampleRate: 16000
|
||||
channels: 1
|
||||
silenceThreshold: 0.5
|
||||
silenceDuration: 1500
|
||||
hotkey: space</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<p><strong>优先级</strong>: 低 | <strong>状态</strong>: 待实现</p>
|
||||
<p>语音输入是"锦上添花"的功能,建议在核心功能完善后再考虑实现。</p>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,72 @@
|
||||
---
|
||||
import DocsLayout from '../../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="Watch 模式" description="文件监控和 AI 注释处理">
|
||||
<h1>Watch 模式 (Watch Mode)</h1>
|
||||
|
||||
<p>允许用户在代码中添加特殊注释(如 <code># AI: fix this</code>),文件保存时 AI 会自动检测并处理这些注释请求。提供无需离开编辑器就能与 AI 交互的方式。</p>
|
||||
|
||||
<h2>核心功能</h2>
|
||||
<ul>
|
||||
<li><strong>文件监控</strong> - 监控项目文件变化</li>
|
||||
<li><strong>注释检测</strong> - 识别特殊 AI 注释</li>
|
||||
<li><strong>自动处理</strong> - 根据注释内容自动执行任务</li>
|
||||
<li><strong>结果反馈</strong> - 将修改写回文件</li>
|
||||
</ul>
|
||||
|
||||
<h2>支持的注释格式</h2>
|
||||
<pre><code class="language-python"># AI: 请优化这个函数
|
||||
# AI! 这里有 bug,请修复
|
||||
# FIXME: AI 请重构这段代码
|
||||
# TODO: AI 添加错误处理</code></pre>
|
||||
|
||||
<pre><code class="language-typescript">// AI: 请优化这个函数
|
||||
// AI! 这里有 bug,请修复
|
||||
/* AI: 重构这段代码 */</code></pre>
|
||||
|
||||
<h2>设计方案</h2>
|
||||
|
||||
<h3>类型定义</h3>
|
||||
<pre><code class="language-typescript">export interface AIComment {
|
||||
content: string;
|
||||
line: number;
|
||||
column: number;
|
||||
filePath: string;
|
||||
original: string;
|
||||
type: 'request' | 'todo' | 'fixme' | 'question';
|
||||
}
|
||||
|
||||
export interface WatchConfig {
|
||||
enabled: boolean;
|
||||
patterns: string[];
|
||||
ignorePatterns: string[];
|
||||
debounceMs: number;
|
||||
autoProcess: boolean;
|
||||
confirmBeforeEdit: boolean;
|
||||
}</code></pre>
|
||||
|
||||
<h2>使用示例</h2>
|
||||
<pre><code class="language-typescript">// 在代码中添加注释
|
||||
function calculateTotal(items: Item[]): number {
|
||||
// AI: 请优化这个函数的性能
|
||||
let total = 0;
|
||||
for (const item of items) {
|
||||
total += item.price * item.quantity;
|
||||
}
|
||||
return total;
|
||||
}</code></pre>
|
||||
|
||||
<h2>配置示例</h2>
|
||||
<pre><code class="language-yaml"># .ai-assistant.yml
|
||||
watch:
|
||||
enabled: false
|
||||
patterns: ['**/*.ts', '**/*.tsx', '**/*.py']
|
||||
ignorePatterns: ['node_modules/**', 'dist/**']
|
||||
debounceMs: 1000
|
||||
autoProcess: true
|
||||
confirmBeforeEdit: false</code></pre>
|
||||
|
||||
<h2>实现状态</h2>
|
||||
<p><strong>优先级</strong>: 中 | <strong>状态</strong>: 待实现</p>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,194 @@
|
||||
---
|
||||
import DocsLayout from '../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="文档概览" description="AI Terminal Assistant 文档概览">
|
||||
<h1>AI Terminal Assistant 文档</h1>
|
||||
|
||||
<p>
|
||||
欢迎来到 AI Terminal Assistant 文档。这是一个功能完整的终端 AI 编程助手,
|
||||
支持流式对话、工具调用、代码检查点、多模型支持等特性。
|
||||
</p>
|
||||
|
||||
<h2>项目简介</h2>
|
||||
|
||||
<p>
|
||||
AI Terminal Assistant 采用 Monorepo 架构,核心引擎已实现完整的 Agent 系统、
|
||||
工具链、检查点和扩展机制。支持 Web、桌面、CLI 多端访问。
|
||||
</p>
|
||||
|
||||
<div class="not-prose my-6 rounded-xl border border-gray-800 bg-gray-900/50 p-6">
|
||||
<pre class="text-sm text-gray-300 overflow-x-auto"><code>ai-terminal-assistant/
|
||||
├── packages/
|
||||
│ ├── core/ # 核心引擎:Agent、工具、LSP、Checkpoint
|
||||
│ ├── server/ # HTTP 服务器:REST API、WebSocket、SSE
|
||||
│ ├── cli/ # 命令行界面
|
||||
│ ├── web/ # React 前端
|
||||
│ └── desktop/ # Tauri 桌面应用
|
||||
└── docs/ # 设计文档</code></pre>
|
||||
</div>
|
||||
|
||||
<h2>核心能力</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose my-6">
|
||||
<div class="rounded-xl border border-gray-800 bg-gray-900/50 p-5">
|
||||
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-gradient-to-br from-blue-500 to-cyan-500 text-lg">⚡</div>
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">Agent 系统</h3>
|
||||
<p class="text-sm text-gray-400">完整的 Agent 引擎,支持流式响应、工具调用、Vision 处理、Plan 模式</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl border border-gray-800 bg-gray-900/50 p-5">
|
||||
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-gradient-to-br from-green-500 to-emerald-500 text-lg">🛠️</div>
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">工具系统</h3>
|
||||
<p class="text-sm text-gray-400">23+ 内置工具(Shell、文件、Git、搜索、Web 等),支持动态加载和语义搜索</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl border border-gray-800 bg-gray-900/50 p-5">
|
||||
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-gradient-to-br from-purple-500 to-violet-500 text-lg">💾</div>
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">Checkpoint 系统</h3>
|
||||
<p class="text-sm text-gray-400">Shadow Git 架构、7点安全检查、创建/恢复/撤销恢复</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-xl border border-gray-800 bg-gray-900/50 p-5">
|
||||
<div class="mb-3 flex h-10 w-10 items-center justify-center rounded-lg bg-gradient-to-br from-pink-500 to-rose-500 text-lg">🔌</div>
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">MCP 集成</h3>
|
||||
<p class="text-sm text-gray-400">完整的 Model Context Protocol 支持,通过外部工具扩展 AI 能力</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>快速导航</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/quickstart" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-primary-500/50 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">🚀 快速开始</h3>
|
||||
<p class="text-sm text-gray-400">5 分钟内启动并运行</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/architecture" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-primary-500/50 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">🏗️ 架构设计</h3>
|
||||
<p class="text-sm text-gray-400">了解系统架构和模块设计</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/core/agent" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-primary-500/50 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">🤖 Agent 系统</h3>
|
||||
<p class="text-sm text-gray-400">深入了解 AI Agent 引擎</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/api/rest" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-primary-500/50 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">📡 API 参考</h3>
|
||||
<p class="text-sm text-gray-400">REST API 和 WebSocket 文档</p>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<h2>功能状态</h2>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>功能</th>
|
||||
<th>状态</th>
|
||||
<th>描述</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><a href="/docs/core/agent">Agent 系统</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>流式响应、工具调用、多轮对话、Vision 支持</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/core/tools">工具系统</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>23+ 内置工具,支持动态加载</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/edit-modes">编辑模式</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>Whole、Search-Replace、Diff 三种模式</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/checkpoint">Checkpoint</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>Shadow Git 架构,7点安全检查</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/chat-history">会话管理</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>三层存储、自动保存、消息压缩</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/core/mcp">MCP 集成</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>完整的 Model Context Protocol 支持</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/multi-model">多模型支持</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>Claude、OpenAI、DeepSeek 等</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/browser-gui">Web GUI</a></td>
|
||||
<td><span class="text-green-400">✅ 已完成</span></td>
|
||||
<td>React 前端 + WebSocket 实时通信</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/configuration">配置管理</a></td>
|
||||
<td><span class="text-yellow-400">🔧 部分实现</span></td>
|
||||
<td>支持配置加载,完整多层配置待完善</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/linting">Linting 集成</a></td>
|
||||
<td><span class="text-gray-400">📋 规划中</span></td>
|
||||
<td>代码编辑后自动运行 linter</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="/docs/features/testing">测试集成</a></td>
|
||||
<td><span class="text-gray-400">📋 规划中</span></td>
|
||||
<td>代码修改后自动运行测试</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2>开发路线图</h2>
|
||||
|
||||
<h3>✅ 已完成</h3>
|
||||
<ul>
|
||||
<li><strong>Phase 1</strong> - 核心引擎:Agent、工具、编辑、检查点</li>
|
||||
<li><strong>Phase 2</strong> - Server 层:REST API、WebSocket、SSE</li>
|
||||
<li><strong>Phase 3</strong> - 前端应用:Web、桌面、CLI</li>
|
||||
<li><strong>Phase 4</strong> - 扩展系统:MCP、Hooks、Commands</li>
|
||||
</ul>
|
||||
|
||||
<h3>🚧 进行中</h3>
|
||||
<ol>
|
||||
<li>配置管理增强 - 完善多层配置系统</li>
|
||||
<li>LSP 深度集成 - 代码诊断和补全</li>
|
||||
</ol>
|
||||
|
||||
<h3>📋 计划中</h3>
|
||||
<ol>
|
||||
<li>Linting 集成 - 代码编辑后自动运行 linter</li>
|
||||
<li>测试集成 - 代码修改后自动运行测试</li>
|
||||
<li>Watch 模式 - 文件监控和 AI 注释处理</li>
|
||||
<li>语音输入 - 语音转文字交互</li>
|
||||
</ol>
|
||||
|
||||
<h2>获取帮助</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-3 not-prose mt-6">
|
||||
<a href="https://github.com" target="_blank" rel="noopener" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-5 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-base font-semibold text-white">GitHub</h3>
|
||||
<p class="text-sm text-gray-400">源代码和 Issues</p>
|
||||
</a>
|
||||
|
||||
<a href="https://discord.com" target="_blank" rel="noopener" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-5 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-base font-semibold text-white">Discord</h3>
|
||||
<p class="text-sm text-gray-400">社区讨论</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/quickstart" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-5 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-base font-semibold text-white">快速开始</h3>
|
||||
<p class="text-sm text-gray-400">开始使用</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
@@ -0,0 +1,140 @@
|
||||
---
|
||||
import DocsLayout from '../../layouts/DocsLayout.astro';
|
||||
---
|
||||
|
||||
<DocsLayout title="快速开始" description="AI Terminal Assistant 快速开始指南">
|
||||
<h1>快速开始</h1>
|
||||
|
||||
<p>
|
||||
本指南将帮助你在几分钟内启动并运行 AI Terminal Assistant。
|
||||
</p>
|
||||
|
||||
<h2>前置要求</h2>
|
||||
|
||||
<ul>
|
||||
<li><strong>Node.js</strong> 18+ 或 <strong>Bun</strong> 1.0+</li>
|
||||
<li><strong>pnpm</strong> 8+ (推荐) 或 npm/yarn</li>
|
||||
<li><strong>Anthropic API Key</strong> (用于 Claude 模型)</li>
|
||||
</ul>
|
||||
|
||||
<h2>安装</h2>
|
||||
|
||||
<h3>1. 克隆仓库</h3>
|
||||
|
||||
<pre><code class="language-bash"># 克隆项目
|
||||
git clone https://github.com/your-username/ai-terminal-assistant.git
|
||||
cd ai-terminal-assistant
|
||||
|
||||
# 安装依赖
|
||||
pnpm install</code></pre>
|
||||
|
||||
<h3>2. 配置环境变量</h3>
|
||||
|
||||
<pre><code class="language-bash"># 创建 .env 文件
|
||||
cp .env.example .env
|
||||
|
||||
# 编辑 .env 文件,添加你的 API Key
|
||||
ANTHROPIC_API_KEY=your_api_key_here</code></pre>
|
||||
|
||||
<h3>3. 构建项目</h3>
|
||||
|
||||
<pre><code class="language-bash"># 构建所有包
|
||||
pnpm build
|
||||
|
||||
# 或只构建需要的包
|
||||
pnpm --filter @ai-assistant/core build
|
||||
pnpm --filter @ai-assistant/server build</code></pre>
|
||||
|
||||
<h2>启动服务</h2>
|
||||
|
||||
<h3>方式一:开发模式</h3>
|
||||
|
||||
<pre><code class="language-bash"># 启动 HTTP 服务器 (端口 3000)
|
||||
pnpm server:dev
|
||||
|
||||
# 在另一个终端启动 Web UI (端口 5173)
|
||||
cd packages/web && pnpm dev</code></pre>
|
||||
|
||||
<p>然后在浏览器中打开 <code>http://localhost:5173</code>。</p>
|
||||
|
||||
<h3>方式二:CLI 命令</h3>
|
||||
|
||||
<pre><code class="language-bash"># 启动服务器
|
||||
cd packages/cli && bun run src/index.ts serve --port 3000
|
||||
|
||||
# 从另一个终端连接
|
||||
cd packages/cli && bun run src/index.ts attach http://localhost:3000</code></pre>
|
||||
|
||||
<h3>方式三:远程模式</h3>
|
||||
|
||||
<pre><code class="language-bash"># 启动可远程访问的服务器 (自动生成 Token)
|
||||
bun run packages/server/src/bin/server.ts --host 0.0.0.0 --port 3000
|
||||
|
||||
# 使用自定义 Token
|
||||
bun run packages/server/src/bin/server.ts --host 0.0.0.0 --token your_secret_token</code></pre>
|
||||
|
||||
<h2>基本使用</h2>
|
||||
|
||||
<h3>创建会话</h3>
|
||||
|
||||
<pre><code class="language-bash"># REST API 创建会话
|
||||
curl -X POST http://localhost:3000/api/sessions \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name": "my-session"}'</code></pre>
|
||||
|
||||
<h3>发送消息</h3>
|
||||
|
||||
<p>通过 WebSocket 连接到 <code>/api/ws/:sessionId</code> 进行实时对话:</p>
|
||||
|
||||
<pre><code class="language-javascript">const ws = new WebSocket('ws://localhost:3000/api/ws/session-id');
|
||||
|
||||
ws.onopen = () => {
|
||||
ws.send(JSON.stringify({
|
||||
type: 'message',
|
||||
payload: { content: '帮我写一个 Hello World 函数' }
|
||||
}));
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'chunk') {
|
||||
console.log(data.payload.content);
|
||||
}
|
||||
};</code></pre>
|
||||
|
||||
<h2>项目结构</h2>
|
||||
|
||||
<pre><code class="language-text">ai-terminal-assistant/
|
||||
├── packages/
|
||||
│ ├── core/ # 核心引擎:Agent、工具、LSP、Checkpoint
|
||||
│ ├── server/ # HTTP 服务器:REST API、WebSocket、SSE
|
||||
│ ├── cli/ # 命令行界面:serve、attach 命令
|
||||
│ ├── web/ # React 前端:Vite + Tailwind CSS
|
||||
│ └── desktop/ # Tauri 桌面应用
|
||||
├── docs/ # 设计文档
|
||||
└── pnpm-workspace.yaml</code></pre>
|
||||
|
||||
<h2>下一步</h2>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 not-prose mt-6">
|
||||
<a href="/docs/architecture" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">架构设计</h3>
|
||||
<p class="text-sm text-gray-400">了解系统架构和模块设计</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/core/agent" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">Agent 系统</h3>
|
||||
<p class="text-sm text-gray-400">深入了解 AI Agent 引擎</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/core/tools" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">工具系统</h3>
|
||||
<p class="text-sm text-gray-400">探索 23+ 内置工具</p>
|
||||
</a>
|
||||
|
||||
<a href="/docs/api/rest" class="block rounded-xl border border-gray-800 bg-gray-900/50 p-6 transition hover:border-gray-700 hover:bg-gray-900">
|
||||
<h3 class="mb-2 text-lg font-semibold text-white">API 参考</h3>
|
||||
<p class="text-sm text-gray-400">完整的 REST API 文档</p>
|
||||
</a>
|
||||
</div>
|
||||
</DocsLayout>
|
||||
Reference in New Issue
Block a user