mcpField
U
ui-agent-patterns

ui-agent-patterns

github.com/ormus-solutionsAgent 工作流12387 次安装3 周前更新
代码仓库

关于 ui-agent-patterns

ui-agent-patterns 是一项用于规划 AI Agent 界面生成、评审和优化工作流的技能,介绍单 Agent、专门化 Agent 群组及分层委派模式,并涵盖记忆、模式和权限。

主要功能

  1. 01单 Agent 与 Agent 群组模式
  2. 02分层子 Agent 委派
  3. 03设计到代码工作流
  4. 04迭代评审与优化
  5. 05设计决策记忆
  6. 06Agent 模式与权限

适用场景

  1. 01选择界面生成架构
  2. 02协调 Agent 处理设计系统
  3. 03通过评审循环优化界面
  4. 04并行生成多个设计方案

MCP 相关

该技能将 MCP 列为工具集成标准,并提及 browser-devtools-mcp 作为相关集成。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/ormus-solutions/libreuiux-claude-code --skill ui-agent-patterns

安装后在 Claude Code 中输入/ui-agent-patterns确认已加载。

详细介绍

UI Agent 模式

用于编排 AI Agent 生成、优化和维护用户界面的模式。这项技能将 Karpathy 的“新编程词汇”与实用的 UI/UX 开发工作流衔接起来。


何时使用此技能

  • 将复杂的 UI 生成任务委派给专门的 Agent
  • 在合成主控架构与专门 Agent 架构之间做出选择
  • 为设计系统编排多 Agent 工作流
  • 管理研究、设计和实现 Agent 之间的交接
  • 构建用于迭代优化 UI 的 Agent 流水线
  • 将 UI 生成能力扩展到单个 Agent 的能力范围之外

核心概念

新编程词汇

Karpathy 的见解:大模型引入了超越函数和对象的新编程原语:

原语描述UI 应用
Agents由大模型驱动的自主工作者UI 生成器、审查者、优化器
Subagents委派的专门人员组件构建者、无障碍检查器
Prompts作为代码的指令设计规范、组件约定
Contexts共享状态和知识设计令牌、品牌指南
Memory持续学习风格偏好、过往决策
Modes行为配置草稿模式、生产模式、审计模式
Permissions能力边界只读审查与代码修改
Tools外部能力Figma API、浏览器 DevTools、屏幕截图捕获
Plugins模块化扩展设计系统加载器、组件库
Skills可复用知识本文件——整理成文的专业知识
Hooks生命周期拦截器提交前设计检查、渲染后审计
MCPModel Context Protocol工具集成标准
Workflows编排后的序列从设计到代码的流水线

Agent 架构模式

模式 1:合成主控架构

由单个强大的 Agent 处理完整的 UI 生成任务。

适用场景:

  • 简单且定义明确的 UI 任务
  • 各项决策紧密耦合
  • 速度至关重要
  • 上下文窗口足以容纳整个任务

结构:

[User Request]
      |
      v
+------------------+
|  Synthesis-Master |
|  (Full Context)  |
+------------------+
      |
      v
[Complete UI Output]

实现:

class SynthesisMasterAgent:
    """
    Single agent handling all UI generation aspects.
    Best for: Landing pages, simple forms, atomic components
    """

    def __init__(self, model: str = "claude-sonnet-4-5-20250929"):
        self.context = {
            "design_tokens": load_design_tokens(),
            "brand_guidelines": load_brand_context(),
            "component_library": load_component_docs(),
            "accessibility_rules": load_a11y_rules(),
        }

    async def generate(self, request: UIRequest) -> UIOutput:
        prompt = f"""
        You are a senior UI engineer and designer. Generate a complete,
        production-ready component based on this request.

        Context:
        - Design Tokens: {self.context['design_tokens']}
        - Brand Guidelines: {self.context['brand_guidelines']}

        Request: {request.description}

        Output requirements:
        1. React/TypeScript component
        2. Tailwind CSS styling
        3. Accessibility attributes
        4. Responsive breakpoints
        5. Dark mode support
        """

        return await self.model.generate(prompt)

优点:

  • 编排更简单
  • 无交接开销
  • 语气和风格一致
  • 延迟更低

缺点:

  • 受上下文窗口限制
  • 存在单点故障
  • 难以扩展复杂度
  • 缺少专业知识

模式 2:专门 Agent 群组

多个专门的 Agent 协作处理 UI 任务。

适用场景:

  • 复杂的设计系统
  • 需要不同专业知识的任务
  • 并行处理有益
  • 通过专业分工提升质量

结构:

[User Request]
      |
      v
+------------------+
|   Orchestrator   |
+------------------+
      |
      +-----------------+----------------+----------------+
      |                 |                |                |
      v                 v                v                v
+----------+     +----------+     +----------+     +----------+
| Research |     |  Design  |     |   Code   |     |  Review  |
|  Agent   |     |  Agent   |     |  Agent   |     |  Agent   |
+----------+     +----------+     +----------+     +----------+
      |                 |                |                |
      v                 v                v                v
  [Context]        [Wireframe]      [Component]       [Audit]

专门 Agent 定义:

# Agent 1: Research Agent
class UIResearchAgent:
    """
    Gathers context and prior art before design begins.
    """

    permissions = ["read_codebase", "search_web", "read_figma"]

    async def research(self, request: UIRequest) -> ResearchContext:
        return {
            "existing_patterns": await self.find_similar_components(),
            "competitive_analysis": await self.analyze_competitors(),
            "user_research": await self.gather_user_insights(),
            "technical_constraints": await self.identify_constraints(),
        }

# Agent 2: Design Agent
class UIDesignAgent:
    """
    Produces design specifications and wireframes.
    """

    permissions = ["generate_images", "access_design_tokens"]

    async def design(self, context: ResearchContext) -> DesignSpec:
        return {
            "layout": await self.generate_layout(),
            "spacing": await self.calculate_spacing(),
            "typography": await self.select_typography(),
            "colors": await self.derive_color_scheme(),
            "interactions": await self.define_interactions(),
        }

# Agent 3: Implementation Agent
class UIImplementationAgent:
    """
    Translates designs into production code.
    """

    permissions = ["write_code", "access_component_library"]

    async def implement(self, spec: DesignSpec) -> CodeOutput:
        return await self.generate_component(
            framework="react",
            styling="tailwind",
            typescript=True,
            spec=spec
        )

# Agent 4: Review Agent
class UIReviewAgent:
    """
    Audits output for quality, accessibility, and standards.
    """

    permissions = ["read_code", "run_tests", "access_browser"]
    mode = "audit"  # Read-only, cannot modify

    async def review(self, code: CodeOutput) -> ReviewReport:
        return {
            "accessibility": await self.audit_a11y(),
            "performance": await self.audit_performance(),
            "design_fidelity": await self.compare_to_spec(),
            "code_quality": await self.lint_and_analyze(),
        }

模式 3:分层委派

主 Agent 将特定子任务委派给子 Agent。

适用场景:

  • 包含许多组件的复杂页面
  • 需要并行生成组件
  • 不同组件需要不同专业知识

结构:

[User Request: "Create a dashboard"]
             |
             v
    +------------------+
    |   Master Agent   |
    | (Task Planning)  |
    +------------------+
             |
    +--------+--------+--------+
    |        |        |        |
    v        v        v        v
[Header] [Sidebar] [Charts] [Tables]
Subagent Subagent Subagent Subagent
    |        |        |        |
    v        v        v        v
  [JSX]    [JSX]    [JSX]    [JSX]
             |
             v
    +------------------+
    |   Master Agent   |
    |  (Integration)   |
    +------------------+
             |
             v
     [Complete Dashboard]

实现:

class HierarchicalUIOrchestrator:
    """
    Master agent that delegates to specialized subagents.
    """

    def __init__(self):
        self.subagents = {
            "header": HeaderComponentAgent(),
            "sidebar": SidebarComponentAgent(),
            "charts": DataVisualizationAgent(),
            "tables": DataTableAgent(),
            "forms": FormBuilderAgent(),
        }

    async def generate_page(self, request: PageRequest) -> PageOutput:
        # Step 1: Plan the page structure
        plan = await self.plan_page_structure(request)

        # Step 2: Delegate component generation in parallel
        component_tasks = []
        for component in plan.components:
            agent = self.subagents[component.type]
            task = agent.generate(component.spec)
            component_tasks.append(task)

        components = await asyncio.gather(*component_tasks)

        # Step 3: Integrate components into cohesive page
        page = await self.integrate_components(components, plan.layout)

        # Step 4: Final coherence review
        return await self.ensure_coherence(page)

    async def plan_page_structure(self, request: PageRequest) -> PagePlan:
        """
        Master agent determines page structure and delegation.
        """
        prompt = f"""
        Analyze this page request and create a component breakdown:

        Request: {request.description}

        For each component, specify:
        1. Component type (header, sidebar, chart, table, form, etc.)
        2. Component requirements
        3. Data dependencies
        4. Layout position

        Return as structured JSON.
        """
        return await self.model.generate(prompt, format="json")

多 Agent 工作流模式

工作流 1:从设计到代码的流水线

从设计意图到生产代码的顺序工作流。

class DesignToCodePipeline:
    """
    Complete workflow from natural language to deployed UI.
    """

    stages = [
        ("interpret", InterpretationAgent()),    # NL -> Design Intent
        ("design", DesignAgent()),               # Intent -> Wireframe
        ("specify", SpecificationAgent()),       # Wireframe -> Spec
        ("implement", ImplementationAgent()),    # Spec -> Code
        ("review", ReviewAgent()),               # Code -> Audit
        ("refine", RefinementAgent()),           # Audit -> Final Code
    ]

    async def run(self, request: str) -> CodeOutput:
        context = {"request": request}

        for stage_name, agent in self.stages:
            result = await agent.process(context)
            context[stage_name] = result

            # Allow early exit on critical issues
            if result.has_blocking_issues:
                return self.handle_blocker(stage_name, result)

        return context["refine"]
工作流 2:迭代优化循环

通过多轮处理优化 UI 的 Agent 循环。

class IterativeRefinementWorkflow:
    """
    Generate -> Review -> Refine loop until quality threshold met.
    """

    def __init__(self, max_iterations: int = 5):
        self.generator = UIGeneratorAgent()
        self.reviewer = UIReviewerAgent()
        self.refiner = UIRefinerAgent()
        self.max_iterations = max_iterations
        self.quality_threshold = 0.85

    async def run(self, request: UIRequest) -> RefinedOutput:
        # Initial generation
        current = await self.generator.generate(request)

        for iteration in range(self.max_iterations):
            # Review current version
            review = await self.reviewer.review(current)

            # Check if quality threshold met
            if review.score >= self.quality_threshold:
                return current

            # Refine based on feedback
            current = await self.refiner.refine(
                current=current,
                feedback=review.feedback,
                priority=review.critical_issues
            )

        # Return best effort after max iterations
        return current
工作流 3:并行生成变体

生成多个设计变体以供比较。

class ParallelVariantWorkflow:
    """
    Generate multiple design variants in parallel for A/B consideration.
    """

    async def generate_variants(
        self,
        request: UIRequest,
        variant_count: int = 3
    ) -> list[DesignVariant]:

        # Define variant strategies
        strategies = [
            {"style": "minimal", "focus": "whitespace"},
            {"style": "bold", "focus": "typography"},
            {"style": "playful", "focus": "interactions"},
        ][:variant_count]

        # Generate in parallel
        tasks = [
            self.generate_variant(request, strategy)
            for strategy in strategies
        ]

        variants = await asyncio.gather(*tasks)

        # Score and rank variants
        scored = await self.score_variants(variants, request.criteria)

        return sorted(scored, key=lambda v: v.score, reverse=True)

Agent 记忆模式

模式:设计决策记忆

持久化设计决策,确保不同会话之间的一致性。

class DesignMemory:
    """
    Persistent memory of design decisions and preferences.
    """

    def __init__(self, project_id: str):
        self.project_id = project_id
        self.decisions = self.load_decisions()

    def remember_decision(self, decision: DesignDecision):
        """
        Store a design decision for future reference.

        Example decisions:
        - "Primary buttons use bg-blue-600, not bg-blue-500"
        - "Card corners are rounded-xl (12px)"
        - "Error states use red-600 with shake animation"
        """
        self.decisions.append({
            "timestamp": datetime.now(),
            "category": decision.category,
            "rule": decision.rule,
            "rationale": decision.rationale,
        })
        self.persist()

    def recall_relevant(self, context: str) -> list[DesignDecision]:
        """
        Retrieve decisions relevant to current context.
        """
        # Semantic search over past decisions
        return self.vector_search(context, top_k=5)

    def inject_into_prompt(self, base_prompt: str) -> str:
        """
        Augment prompt with relevant past decisions.
        """
        relevant = self.recall_relevant(base_prompt)

        if not relevant:
            return base_prompt

        decisions_context = "\n".join([
            f"- {d.rule} (Rationale: {d.rationale})"
            for d in relevant
        ])

        return f"""
        {base_prompt}

        ## Past Design Decisions (maintain consistency):
        {decisions_context}
        """

模式与权限

Agent 模式

为不同场景配置 Agent 行为:

class UIAgentModes:
    """
    Different operational modes for UI agents.
    """

    MODES = {
        "draft": {
            "description": "Fast, exploratory generation",
            "quality_threshold": 0.6,
            "iterations": 1,
            "include_comments": True,
            "placeholder_content": True,
        },
        "production": {
            "description": "High-quality, deployment-ready",
            "quality_threshold": 0.9,
            "iterations": 5,
            "include_comments": False,
            "placeholder_content": False,
        },
        "audit": {
            "description": "Read-only review mode",
            "can_modify": False,
            "generate_report": True,
        },
        "learning": {
            "description": "Explain decisions, teach patterns",
            "verbose_reasoning": True,
            "cite_sources": True,
        },
    }
权限边界

定义 Agent 可以和不可以执行的操作:

class AgentPermissions:
    """
    Capability boundaries for UI agents.
    """

    # File system permissions
    READ_CODEBASE = "read_codebase"
    WRITE_COMPONENTS = "write_components"
    WRITE_STYLES = "write_styles"
    MODIFY_CONFIG = "modify_config"

    # Tool permissions
    ACCESS_BROWSER = "access_browser"
    ACCESS_FIGMA = "access_figma"
    RUN_TESTS = "run_tests"
    DEPLOY_PREVIEW = "deploy_preview"

    # Common permission sets
    READONLY_REVIEWER = [READ_CODEBASE, ACCESS_BROWSER]
    COMPONENT_BUILDER = [READ_CODEBASE, WRITE_COMPONENTS, WRITE_STYLES]
    FULL_ACCESS = [READ_CODEBASE, WRITE_COMPONENTS, WRITE_STYLES,
                   MODIFY_CONFIG, ACCESS_BROWSER, RUN_TESTS]

应避免的反模式

1. 单体巨型提示词

问题:将所有指令塞进一个巨型提示词中 解决方案:使用分层委派,并让 Agent 专注于各自的任务

2. 上下文溢出

问题:完整设计系统超出上下文窗口 解决方案:使用 RAG 动态注入相关上下文

3. 缺少反馈循环

问题:单次生成,没有验证 解决方案:通过质量阈值实现审查与优化循环

4. 工作流硬编码

问题:僵化的流水线无法灵活调整 解决方案:根据任务复杂度进行动态编排

5. Agent 混乱无序

问题:Agent 过多,职责不明确 解决方案:明确划分职责,并制定清晰的交接协议


快速参考

场景推荐模式
简单组件Synthesis-Master
完整页面设计分层委派
设计系统工作专用 Agent 群组
快速原型设计草稿模式 + 单个 Agent
生产部署完整流水线并进行审查
A/B 测试设计并行生成变体

与 LibreUIUX 集成

此技能与以下工具结合使用效果最佳:

  • design-mastery/design-principles - 将设计原则提供给 Design Agent
  • archetypal-alchemy/jungian-archetypes - 为 UI 生成提供个性特征
  • context-management/design-system-context - 管理 Token
  • mcp-integrations/browser-devtools-mcp - 实时检查工具

“Agent 不是代码,而是化为可执行形式的意图。”

评论

支持 Markdown