From b961c348279cad44d7589c364043a7a264c2fc5c Mon Sep 17 00:00:00 2001 From: zyh Date: Sat, 19 Oct 2024 08:34:28 +0000 Subject: [PATCH] =?UTF-8?q?feat(docs):=20=E6=9B=B4=E6=96=B0=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E8=80=85=E6=8C=87=E5=8D=97=E4=B8=AD=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=92=8C=E8=BF=87=E6=97=B6=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/modules/上下文提供-ContextProvider.md | 133 ++++++++++----------- docs/开发者指南-DeveloperGuide.md | 41 ++++--- 2 files changed, 87 insertions(+), 87 deletions(-) diff --git a/docs/modules/上下文提供-ContextProvider.md b/docs/modules/上下文提供-ContextProvider.md index 022f040..a28c86a 100644 --- a/docs/modules/上下文提供-ContextProvider.md +++ b/docs/modules/上下文提供-ContextProvider.md @@ -2,105 +2,98 @@ ## 概述 -上下文提供模块是 Bolt 系统的关键组件之一,负责在用户发送对话时将项目的相关上下文信息提供给 AI 处理。这个模块确保 AI 能够获得足够的背景信息来理解用户的需求并提供准确的响应。 +上下文提供模块是 Bolt 系统的关键组件之一,负责在用户发送对话时将项目的相关上下文信息提供给 AI 处理。这个模块确保 AI 能够获得足够的背景信息来理解用户的需求并提供准确的响应。 ## 实现细节 -文件位置: `app/lib/context-provider/` +文件位置: `app/lib/.server/llm/` ### 主要功能 -1. 收集项目文件信息 -2. 提取相关代码片段 -3. 生成项目结构摘要 -4. 整合上下文信息 +1. 提供系统提示词 +2. 管理对话历史 +3. 处理文件修改信息 ### 核心方法 -#### 1. 收集文件信息 +#### 1. 获取系统提示词 ```typescript -async function collectFileInfo(fileSystem: FileSystem): Promise { - // 遍历文件系统,收集文件信息 +// app/lib/.server/llm/prompts.ts +export function getSystemPrompt() { +return You are an intelligent programmer, powered by Claude 3.5 Sonnet. You are happy to help answer any questions that the user has (usually they will be about coding).1. When the user is asking for edits to their code, please output a simplified version of the code block that highlights the changes necessary and adds comments to indicate where unchanged code has been skipped. For example:\\\language:file_path +// ... existing code ... +{{ edit_1 }} +// ... existing code ... +{{ edit_2 }} +// ... existing code ... +\\\The user can see the entire file, so they prefer to only read the updates to the code. Often this will mean that the start/end of the file will be skipped, but that's okay! Rewrite the entire file only if specifically requested. Always provide a brief explanation of the updates, unless the user specifically requests only the code.The current file is likely relevant to the edits, even if not specifically @ mentioned in the user's query.If you think that any of the imported files will likely need to change, please say so in your response.2. Do not lie or make up facts.3. If a user messages you in a foreign language, please respond in that language.4. Format your response in markdown.5. When writing out new code blocks, please specify the language ID after the initial backticks, like so: \\\python +{{ code }} +\\\6. When writing out code blocks for an existing file, please also specify the file path after the initial backticks and restate the method / class the codeblock belongs to, like so:\\\typescript:app/components/Ref.tsx +function AIChatHistory() { +... +{{ code }} +... } -```` +\\\7. For codeblocks used for explanation that aren't intended to be applied as edits, do not reference the file path. On the other hand, if the codeblock is intended to be applied as edits, please do reference the file path.8. Put code into same codeblocks if they are the same file.9. Keep users' comments, unless user specifically requests to modify them.; +} +``` +#### 2. 处理对话历史 -#### 2. 提取相关代码片段 +对话历史通过 `Messages` 类型来管理,包含用户和助手的消息。 ```typescript -function extractRelevantCode(files: FileInfo[], query: string): CodeSnippet[] { - // 基于用户查询提取相关代码片段 +// app/lib/.server/llm/stream-text.ts +export type Messages = Message[]; +interface Message { + role: 'user' | 'assistant'; + content: string; + toolInvocations?: ToolResult[]; } -```` +``` +#### 3. 处理文件修改信息 -#### 3. 生成项目结构摘要 +文件修改信息通过 `workbenchStore` 来管理和提供。 ```typescript -function generateProjectSummary(files: FileInfo[]): string { - // 生成项目结构的简洁摘要 +// app/components/chat/Chat.client.tsx +const fileModifications = workbenchStore.getFileModifcations(); +if (fileModifications !== undefined) { +const diff = fileModificationsToHTML(fileModifications); +append({ role: 'user', content: ${diff}\n\n${_input} }); +workbenchStore.resetAllFileModifications(); +} else { +append({ role: 'user', content: input }); } -```` - - -#### 4. 整合上下文信息 - -```typescript -async function prepareContext(query: string): Promise { - const files = await collectFileInfo(fileSystem); - const relevantCode = extractRelevantCode(files, query); - const projectSummary = generateProjectSummary(files); - - return { - projectSummary, - relevantCode, - query - }; -} -```` - +``` ## 使用方式 -在处理用户查询时,系统会自动调用上下文提供模块: +在处理用户查询时,系统会自动调用上下文提供模块: ```typescript -import { prepareContext } from '~/lib/context-provider'; -import { sendToAI } from '~/lib/ai-integration'; - -async function handleUserQuery(query: string) { - const context = await prepareContext(query); - const aiResponse = await sendToAI(context); - // 处理 AI 响应 +// app/routes/api.chat.ts +async function chatAction({ context, request }: ActionFunctionArgs) { + const { messages } = await request.json<{ messages: Messages }>(); + const result = await streamText(messages, context.cloudflare.env, options); + // ... } -```` - - -## 性能考虑 - -- 使��缓存机制来存储最近的文件信息,避免频繁的文件系统操作 -- 实现增量更新,只处理发生变化的文件 -- 使用异步处理和并行化来提高大型项目的处理速度 - -## 隐私和安全 - -- 确保只发送必要的项目信息给 AI -- 实现敏感信息过滤机制,如 API 密钥或个人信息 -- 遵守数据保护规定,不存储或传输用户的私密数据 - -## 可扩展性 - -上下文提供模块设计为可扩展的,允许轻松添加新的上下文收集方法: - -1. 实现新的上下文收集函数 -2. 在 `prepareContext` 函数中集成新的收集方法 -3. 更新 AI 集成模块以利用新的上下文信息 +``` ## 注意事项 -- 平衡上下文信息的详细程度和 AI 请求的大小 -- 定期更新上下文提供逻辑以适应项目结构的变化 -- 考虑不同编程语言和框架的特殊需求 +- 确保只发送必要的项目信息给 AI,保护用户隐私 +- 优化上下文收集的性能,特别是对于大型项目 +- 定期更新系统提示词以适应新的需求和功能 -通过这个强大的上下文提供模块,Bolt 系统能够为 AI 提供丰富的项目背景信息,从而生成更加准确和相关的响应,大大提高了 AI 辅助开发的效果和用户体验。 +## 可扩展性 + +上下文提供模块设计为可扩展的,允许轻松添加新的上下文收集方法: + +1. 在 `getSystemPrompt` 函数中添加新的指令或上下文信息 +2. 更新 `Message` 类型定义以包含新的上下文信息类型 +3. 在 `Chat.client.tsx` 中添加新的上下文收集逻辑 + +通过这个上下文提供模块,Bolt 系统能够为 AI 提供丰富的项目背景信息,从而生成更加准确和相关的响应,大大提高了 AI 辅助开发的效果和用户体验。 diff --git a/docs/开发者指南-DeveloperGuide.md b/docs/开发者指南-DeveloperGuide.md index 48329b3..72ed8ef 100644 --- a/docs/开发者指南-DeveloperGuide.md +++ b/docs/开发者指南-DeveloperGuide.md @@ -33,7 +33,7 @@ bolt.new/ - [WebContainer](./modules/webcontainer.md): 浏览器中的全栈运行环境 - [文件系统](./modules/文件系统-FileSystem.md): 管理项目文件 -- [编辑器](./modules/编辑器-Editor.md): 代码编辑功能 +- [编辑器](./modules/编辑器-Editor.md): 代码编��功能 - [AI 集成](./modules/AI集成-AIIntegration.md): 与 AI 模型交互 - [工作台](./modules/工作台-Workbench.md): 集成开发环境 - [聊天界面](./modules/聊天界面-ChatInterface.md): 用户与 AI 交互的主要入口 @@ -66,7 +66,7 @@ bolt.new/ ## 8. 构建和部署 - 构建项目: `pnpm build` -- 部署到 Cloudflare Pages: 通过 GitHub Actions 自动部署 +- 部署到 Cloudflare Pages: 通过 GitHub Actions 自动��署 ## 9. 贡献指南 @@ -106,7 +106,7 @@ Bolt 系统的一个关键特性是能够根据 AI 的响应执行各种任务 ### 主要功能 -1. 解析 AI 响应中的任务指令 +1. 解析 AI 响应中的���务指令 2. 执行相应的操作(如文件修改、安装依赖等) 3. 将执行过程实时显示在终端组件中 @@ -137,31 +137,38 @@ await actionRunner.run(aiResponse); ## 14. 项目上下文提供 -Bolt 系统的另一个关键特性是能够在用户发送对话时,自动收集并提供项目的相关上下文信息给 AI 处理。这个功能确保了 AI 能够基于当前项目状态提供准确的建议和解决方案。 +Bolt 系统的一个关键特性是能够在用户发送对话时,自动收集并提供项目的相关上下文信息给 AI 处理。这个功能确保了 AI 能够基于当前项目状态提供准确的建议和解决方案。 ### 实现细节 -- 位置: `app/lib/context-provider/` -- 主要功能: 收集文件信息、提取相关代码片段、生成项目结构摘要 +- 位置: `app/lib/.server/llm/` +- 主要功能: 提供系统提示词、管理对话历史、处理��件修改信息 ### 主要功能 -1. 收集项目文件信息 -2. 提取与用户查询相关的代码片段 -3. 生成项目结构的简洁摘要 -4. 整合上下文信息并提供给 AI +1. 获取系统提示词 +2. 处理对话历史 +3. 处理文件修改信息 ### 使用方式 系统会在处理用户查询时自动调用上下文提供模块: ```typescript -import { prepareContext } from '~/lib/context-provider'; -import { sendToAI } from '~/lib/ai-integration'; - -async function handleUserQuery(query: string) { - const context = await prepareContext(query); - const aiResponse = await sendToAI(context); - // 处理 AI 响应 +// app/routes/api.chat.ts +async function chatAction({ context, request }: ActionFunctionArgs) { + const { messages } = await request.json<{ messages: Messages }>(); + const result = await streamText(messages, context.cloudflare.env, options); + // ... } ``` + +### 注意事项 + +- 确保只发送必要的项目信息给 AI,保护用户隐私 +- 优化上下文收集的性能,特别是对于大型项目 +- 定期更新系统提示词以适应新的需求和功能 + +更多详细信息,请参阅 [上下文提供模块文档](./modules/上下文提供-ContextProvider.md)。 + +欢迎加入 Bolt 开发社区,一起打造下一代的 AI 辅助开发工具!