使用 Google AI Studio 构建 AI 代码分析器(并在 Cursor 中完成)
Source: Dev.to
请提供您希望翻译的具体文本内容,我将按照要求保留源链接、格式和代码块,仅翻译正文部分。
我构建的
Archlyze 是一个仅在浏览器运行的单页应用(SPA),使用 Google Gemini 分析源代码(Rust、Python、JS/TS、Go 等)。它提取组件和依赖关系,标记不同严重程度的问题,提供修复建议和单元测试,并通过 Gemini 生成流程图/UML/数据流图。我们从 AI Studio 中的详细提示开始(并通过 Perplexity 和 Gemini 进行优化),随后在 Cursor 中完成并扩展了该应用,加入了文件夹导入、.gitignore 解析、会话历史、模型选择和 Markdown 导出等功能。
演示
实时演示:
我的经验
在 AI Studio 中开始
我们首先使用不同的模型(Perplexity 和 Gemini)起草概念和提示,然后将其带入 Google AI Studio 构建第一个版本。早期反复出现的一个问题是 React 和工具链版本——生成的应用不断无法运行或构建,需要多次迭代(10 次以上)才实现可用的设置。
提示
Build 'RustFlow Analyzer'—a web app for comprehensive Rust file analysis. Users upload a .rs file (max 500 lines) or paste code.
Gemini 1.5 Pro performs deep analysis: extract all functions, structs, traits, impls; map dependencies and call relationships; identify ownership patterns, error handling, and common anti-patterns (unnecessary clones, unwrap() abuse, missing lifetimes).
Display results in three panels:
Code View: Original code with syntax highlighting (Monaco editor), clickable line numbers to jump to explanations
Analysis Panel: Collapsible sections for each major code block (functions/structs) with plain-English explanations, detected issues marked with warning/error badges, and best-practice suggestions
Visual Panel: Use Imagen to generate architecture diagrams—function call graphs (boxes + arrows showing invocation flow), struct relationship diagrams (ownership/borrowing visualized with solid/dashed lines), and module dependency trees. Include toggle buttons for diagram types (flowchart, UML, data flow). Style: professional developer docs aesthetic, Rust brand colors (orange #CE422B, dark gray), clean minimalist lines.
Features: file upload (.rs), smart truncation warning if >500 lines, 'Regenerate Diagram' button for each section, export full report as HTML with embedded images, dark/light theme, example files (basic HTTP server, CLI parser, async tokio app). Add 'Share Analysis' to generate unique URL with results cached. Loading states with progress indicators, error handling for invalid Rust syntax or oversized files. Deploy-ready SPA with responsive mobile layout思路的演进
在稳定了基础后,我们将范围从仅支持 Rust 扩展到多语言支持,并加入了文件夹导入、.gitignore 感知过滤、会话历史、依赖检测、问题严重性、自动修复、单元测试生成以及多种图表类型等功能。Cursor 在重构和扩展同一代码库时发挥了重要作用:AI Studio 提供了初始结构和 Gemini 集成,而 Cursor 帮助在我们增加复杂度时保持代码的一致性。
结构化输出和 “思考” 模型
最大的技术收获是 依赖严格的 JSON Schema 来进行分析。如果在 responseSchema 中没有明确的 required 字段和清晰的描述,Gemini 2.5 有时会返回极少或不一致的 JSON。正确定义 Schema 让分析结果足够可靠,以驱动 UI(组件、问题、依赖)。我还了解到 Gemini 2.5 的 “思考” 阶段会导致响应时间变长,因此我们加入了 可伸缩的超时(例如,小文件 60 秒,大文件 180 秒)以及加载状态,以避免在模型推理时出现卡顿感。
惊喜
- 该应用 完全不依赖后端——API 密钥存放在浏览器中,直接调用 Gemini,LocalStorage 负责设置和主题,使架构保持简洁。
- 图像生成(
gemini-2.5-flash)集成顺畅,支持即时生成图表。
总体而言,这条路线为从 “想法 + 提示” 到真正的 SPA 提供了坚实的路径:AI Studio 用于快速原型。
并集成 Gemini,然后使用 Cursor 加固并扩展为可用产品。


