-
Notifications
You must be signed in to change notification settings - Fork 130
新增使用ai生成yaml功能 #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
新增使用ai生成yaml功能 #440
Conversation
- Add AiGenerateModal component for natural language YAML generation - Implement AIGenerate API endpoint in yaml_editor controller - Register AI generation route in cluster routes - Integrate AI button in EditorPanel with modal interface - Provide example prompts for common K8s resources (Nginx, Redis, MySQL, Node.js) - Support YAML validation and auto-formatting - Use existing AI service infrastructure for LLM integration Key features: - Natural language to YAML conversion - Real-time preview of generated YAML - Copy and confirmation options - Loading states and error handling - Seamless integration with Monaco editor
将AI生成YAML功能的路由从集群路由(/k8s/plugins/yaml_editor/ai/generate)移至管理路由(/mgm/plugins/yaml_editor/ai/generate),以符合功能的管理属性。前端相应更新API调用地址,并使用统一的fetcher工具进行请求。
清理AI生成模态组件中的console.log语句和多余空行,保持代码简洁
修正AI生成模态框中API响应处理逻辑,移除不必要的JSON.stringify调用并正确解析响应数据结构。使用类型化的接口定义确保类型安全,避免潜在的运行时错误。
📝 WalkthroughSummary by CodeRabbit发布说明
✏️ Tip: You can customize this high-level summary in your review settings. Walkthrough新增 AI 辅助 YAML 生成功能:前端添加 AiGenerateModal 并在编辑器集成,后端移除旧日志端点并新增 POST /mgm/plugins/ai/chat/yaml/generate,包含提示词模板与相应的请求/响应类型及 Swagger 文档更新。 Changes
Sequence Diagram(s)sequenceDiagram
participant User as 用户
participant Modal as AiGenerateModal<br/>前端
participant Editor as EditorPanel
participant Server as 后端API
participant AI as AI服务
User->>Modal: 输入生成提示并点击生成
Modal->>Server: POST /mgm/plugins/ai/chat/yaml/generate { prompt }
Server->>Server: 获取并渲染 YAML 生成提示模板
Server->>AI: 调用 AI Chat 无历史上下文
AI->>Server: 返回文本(含 YAML)
Server->>Server: 去除 Markdown 三反引号等标记
Server-->>Modal: 响应 YamlGenerateResponse { yaml }
Modal->>Modal: 显示生成的 YAML
User->>Modal: 点击“使用”
Modal->>Editor: onGenerateSuccess(yaml)
Editor->>Editor: monaco.setValue(yaml)
预计代码审查工作量🎯 4 (复杂) | ⏱️ ~50 分钟 可能相关的PR
诗句
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@pkg/plugins/modules/yaml_editor/controller/controller.go`:
- Around line 29-47: After parsing into aiGenerateRequest (variable req) using
c.ShouldBindJSON, add a non-empty validation for req.Prompt and return early
with an error response (e.g., via amis.WriteJsonError) if it's blank; this
should be placed before building the prompt string (variable prompt) so the
handler does not call the AI with an empty prompt, and ensure the error message
clearly indicates the prompt is required.
- Around line 49-53: The code creates a background context (ctx :=
context.Background()) before calling
service.GetChatService().ChatWithCtxNoHistory which ignores client cancellation
and timeouts; change to derive ctx from the request (use the incoming gin
context request context, e.g. c.Request.Context()), wrap it with a sensible
timeout using context.WithTimeout and defer the cancel, then pass that ctx into
ChatWithCtxNoHistory so the AI call respects client cancellations and the
configured timeout.
In `@ui/src/components/Amis/custom/YamlEditor/components/AiGenerateModal.tsx`:
- Around line 44-76: handleGenerate can be called repeatedly while a request is
in progress, causing duplicate requests; add a guard at the start of
handleGenerate that returns immediately if loading is true (use the existing
loading state variable) so the function exits when a request is in flight,
preserving the existing validation (prompt.trim()) logic and ensuring
setLoading(true) only runs when no request is active.
- Remove AI generation logic from yaml_editor plugin - Add YamlGenerate method to ai plugin controller - Register /k8s/plugins/ai/yaml/generate route in ai cluster routes - Update frontend to call new AI plugin API endpoint - Add proper type interfaces for API response handling - Import strings package for YAML cleaning operations Benefits: - Better separation of concerns - AI functionality centralized in ai plugin - Reuses existing AI service infrastructure - Maintains type safety without @ts-ignore Changes: - pkg/plugins/modules/ai/controller/chat.go: Add YamlGenerate method - pkg/plugins/modules/ai/route/mgm_api.go: Register yaml/generate route - pkg/plugins/modules/yaml_editor/controller/controller.go: Remove AI generation - pkg/plugins/modules/yaml_editor/route/route.go: Remove AI route - ui/src/components/Amis/custom/YamlEditor/components/AiGenerateModal.tsx: Update API path
将YAML生成API路由从集群路由移至管理路由,以匹配前端调用的端点路径。
将/k8s/plugins/ai/yaml/generate更正为/mgm/plugins/ai/chat/yaml/generate,以保持API端点命名一致性并修复路由映射错误。
将硬编码的YAML生成提示词提取为内置提示词模板,便于统一管理和维护。同时更新API路由路径以符合规范,使用模板渲染机制动态生成提示词内容。
清理不再使用的日志分析功能,包括删除对应的常量定义、路由、内置提示模板以及控制器方法。为保持代码库的整洁和可维护性,移除未使用的代码。同时,在类型选项列表中添加了“YAML生成”类型。
更新YAML生成接口的路由路径,从/k8s/plugins/ai/yaml/generate改为/mgm/plugins/ai/chat/yaml/generate 在Swagger文档中添加日志智能问答(/mgm/plugins/ai/chat/log/ask)和日志智能总结(/mgm/plugins/ai/chat/log/summary)接口 添加YAML生成接口(/mgm/plugins/ai/chat/yaml/generate)到Swagger文档 添加controller.ResourceData、controller.YamlGenerateRequest和controller.YamlGenerateResponse的类型定义
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@pkg/plugins/modules/ai/controller/chat.go`:
- Around line 449-471: In YamlGenerate, add the same AI-plugin enabled check
used elsewhere by calling
plugins.ManagerInstance().IsRunning(modules.PluginNameAI) and return an amis
JSON error if it is not running, and replace the use of context.Background()
when calling service.GetChatService().ChatWithCtxNoHistory with the
request-aware context from amis.GetContextWithUser(c) so user identity and
cancellation propagate; locate these changes around the YamlGenerate function
where templateStr is rendered and before calling ChatWithCtxNoHistory.
In `@pkg/plugins/modules/swagger/docs.go`:
- Around line 10276-10303: Swagger shows two "in: body" parameters (data and
question) which violates OpenAPI 2.0; update the handler annotation so LogAsk in
pkg/plugins/modules/ai/controller/chat.go uses a single body parameter that
contains both fields (e.g. controller.ResourceData with Data and Question),
remove the extra `in: body` parameter annotation, and regenerate the Swagger
docs so the generated docs (pkg/plugins/modules/swagger/docs.go) only contain
one body parameter for the /mgm/plugins/ai/chat/log/ask POST.
🧹 Nitpick comments (2)
pkg/plugins/modules/ai/controller/chat.go (2)
473-478: Markdown 代码块标记清理可考虑大小写变体当前逻辑使用
strings.TrimPrefix区分大小写,但 AI 模型可能返回\``YAML或```Yaml` 等变体。♻️ 可选优化:使用大小写不敏感的处理
// 清理可能存在的 markdown 代码块标记 result = strings.TrimSpace(result) - result = strings.TrimPrefix(result, "```yaml") - result = strings.TrimPrefix(result, "```") + if strings.HasPrefix(strings.ToLower(result), "```yaml") { + result = result[7:] // len("```yaml") == 7 + } else if strings.HasPrefix(result, "```") { + result = result[3:] + } result = strings.TrimSuffix(result, "```") result = strings.TrimSpace(result)
480-493: 建议使用定义的响应类型以保持一致性已定义
YamlGenerateResponse结构体,但实际返回使用的是response.H{"yaml": result}。虽然两者产生相同的 JSON 结构,但使用定义的类型更具类型安全性且与 Swagger 文档保持一致。♻️ 可选:使用定义的类型
- amis.WriteJsonData(c, response.H{ - "yaml": result, - }) + amis.WriteJsonData(c, YamlGenerateResponse{ + Yaml: result, + })
将日志智能问答接口的两个独立请求参数 `data` 和 `question` 合并为一个名为 `request` 的单一参数,以简化 API 调用。 在 YamlGenerate 方法中添加 AI 插件启用状态检查,并统一使用用户上下文。
No description provided.