Skip to content

NanoDSL: 添加完整的路由系统支持 #491

@phodal

Description

@phodal

背景

当前 NanoDSL 已支持简单的 Navigate(to="/path") 动作,但缺乏完整的路由系统支持,无法满足多页面应用的需求。需要扩展 PromptTemplate 以指导 AI 正确生成路由相关代码。

目标

  1. 扩展 NanoAction.Navigate 支持更丰富的路由功能
  2. 更新 Prompt 模板 引导模型正确生成路由代码
  3. 添加路由相关集成测试 验证模型对路由语法的理解

功能需求

Phase 1: 增强 Navigate Action

扩展 NanoAction.Navigate:

data class Navigate(
    val to: String,
    val params: Map<String, String>? = null,  // 路由参数
    val replace: Boolean = false,              // 替换当前历史记录
    val query: Map<String, String>? = null     // 查询参数
) : NanoAction()

NanoDSL 语法:

# 简单导航
Navigate(to="/home")

# 带参数导航
Navigate(to="/user/{id}", params={"id": state.userId})

# 带查询参数
Navigate(to="/search", query={"q": state.query, "page": "1"})

# 替换当前页面
Navigate(to="/login", replace=true)

Phase 2: 添加路由定义块

支持在组件中定义路由:

# 路由定义
routes:
    "/": HomePage
    "/login": LoginPage
    "/user/:id": UserProfile
    "/products": ProductList
    "/products/:id": ProductDetail

# 路由组件
component App:
    Router:
        routes << routes
        fallback: NotFoundPage

Phase 3: 更新 PromptTemplate

文件: xuiper-ui/src/main/resources/prompts/detailed.txt

添加路由语法参考节:

  • 基础导航
  • 带参数导航
  • 带查询字符串导航
  • 替换历史记录导航
  • 路由定义语法

文件: xuiper-ui/src/main/kotlin/cc/unitmesh/xuiper/prompt/PromptTemplate.kt

添加路由示例到 PromptTemplateRegistry

Phase 4: 添加路由集成测试

ID 名称 描述 难度
16 Simple Navigation 基础页面跳转 EASY
17 Parameterized Route 带参数的路由 MEDIUM
18 Multi-Page App 多页面应用完整路由 HARD
19 Conditional Navigation 条件导航(登录后跳转) MEDIUM

验收标准

  • Navigate Action 支持 params, query, replace 参数
  • Parser 能正确解析增强的 Navigate 语法
  • Prompt 模板包含路由语法说明和示例
  • 至少 4 个路由相关集成测试通过
  • 模型生成的路由代码可正确解析

实现顺序

  1. 更新 NanoAction.Navigate (NanoAction.kt)
  2. 更新 IndentParser (IndentParser.kt)
  3. 更新 Prompt 模板 (detailed.txt, standard.txt)
  4. 添加测试用例期望输出 (testcases/expect/16-*.nanodsl)
  5. 添加集成测试 (NanoDSLGenerationIntegrationTest.kt)
  6. 运行测试验证模型理解 (./gradlew :xuiper-ui:integrationTest)

风险与依赖

  • Parser 改动可能影响现有测试用例
  • 需要验证多个模型(DeepSeek、GPT-4o、Claude)的路由语法理解能力
  • 路由运行时实现需要与前端渲染层协调

相关文件

  • xuiper-ui/src/main/kotlin/cc/unitmesh/xuiper/action/NanoAction.kt
  • xuiper-ui/src/main/kotlin/cc/unitmesh/xuiper/prompt/PromptTemplate.kt
  • xuiper-ui/src/main/kotlin/cc/unitmesh/xuiper/parser/IndentParser.kt
  • xuiper-ui/src/main/resources/prompts/detailed.txt
  • xuiper-ui/src/integrationTest/kotlin/cc/unitmesh/xuiper/integration/NanoDSLGenerationIntegrationTest.kt

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions