mcpField
B
bicep-diagrams

bicep-diagrams

github.com/eraserlabs云与 DevOps45536 次安装29 周前更新
代码仓库

关于 bicep-diagrams

bicep-diagrams 是将 Azure Bicep 文件转换为云架构图的 Skill。它解析资源及其关系,并通过 Eraser API 渲染图表,适合审查或记录基础设施。

主要功能

  1. 01解析 Bicep 资源与模块
  2. 02映射依赖和资源关系
  3. 03生成 Eraser DSL
  4. 04通过 Eraser API 渲染图表

适用场景

  1. 01根据 Bicep 文件可视化 Azure 部署
  2. 02展示模块边界和资源依赖
  3. 03记录网络、计算与存储布局

安装

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

$npx skills add https://github.com/eraserlabs/eraser-io --skill bicep-diagrams

安装后在 Claude Code 中输入/bicep-diagrams确认已加载。

详细介绍

Bicep 图表生成器

直接根据 Azure Bicep 文件生成架构图。Bicep 是一种用于以声明式方式部署 Azure 资源的领域特定语言(DSL)。

适用场景

在以下情况下启用此技能:

  • 用户有 Bicep 文件(.bicep),并想要可视化基础设施
  • 用户要求“为我的 Bicep 绘制图表”或“可视化此 Bicep 基础设施”
  • 用户提到 Bicep 或 Azure Bicep
  • 用户想查看通过 Bicep 部署的资源架构

工作原理

此技能通过解析 Bicep 代码并直接调用 Eraser API,生成专用于 Bicep 的图表:

  1. 解析 Bicep 文件:识别资源声明、模块、参数和输出
  2. 提取关系:映射依赖关系、资源引用和模块层次结构
  3. 生成 Eraser DSL:根据 Bicep 资源创建 Eraser DSL 代码
  4. 调用 Eraser API:使用 /api/render/elements,并设置 diagramType: "cloud-architecture-diagram"

Instructions

当用户提供 Bicep 代码时:

  1. 解析 Bicep

    • 识别所有 resource 声明(例如 Microsoft.Compute/virtualMachines 等)
    • 提取 module 声明及其配置
    • 记录 param 和 output 定义
    • 识别 var 变量及其用法
  2. 映射关系

    • 跟踪资源依赖关系(例如 dependsOn 或隐式依赖)
    • 按类型对资源分组(计算、网络、存储等)
    • 将 VNet 识别为子网和资源的容器
    • 记录 Network Security Group、Key Vault 和其他安全资源
  3. 生成 Eraser DSL 将 Bicep 资源转换为 Eraser DSL:

    • 关键:标签格式规则
      • 标签必须位于单独一行中——绝不要在标签属性中使用换行符
      • 保持标签简洁易读——优先使用多个独立标签,而不是拼接过多元数据
      • 使用正确的换行格式排版 DSL(每个节点/分组占一行,但标签必须保持在单独一行中)
      • 如果包含 CIDR 块或 VM 规格等元数据,请将其放在同一个带引号的标签字符串中:[label: "VNet 10.0.0.0/16"]

    示例:

    myVNet [label: "VNet 10.0.0.0/16"] {
      subnet1 [label: "Subnet 1"] {
        myVM [icon: azure-vm, label: "Virtual Machine"]
        gateway [icon: azure-app-gateway]
      }
      subnet2 [label: "Subnet 2"] {
        database [icon: azure-sql]
      }
    }
    storage [icon: azure-storage]
    myVNet -> myVM
    myVM -> database
    
  4. 发起 HTTP 请求

    重要:生成 DSL 后,必须执行此 curl 命令。绝不要在生成 DSL 后不调用 API 就停止。

    关键:必须将下方 X-Skill-Source 标头中的值替换为你的 AI Agent 名称:

    • 如果你是 Claude(Anthropic),请使用:claude
    • 如果你在 Cursor 中运行,请使用:cursor
    • 如果你是 ChatGPT(OpenAI),请使用:chatgpt
    • 如果你是 Gemini(Google),请使用:gemini
    • 否则,请使用小写的模型/Agent 名称
    curl -X POST https://app.eraser.io/api/render/elements \
      -H "Content-Type: application/json" \
      -H "X-Skill-Source: eraser-skill" \
      -H "Authorization: Bearer ${ERASER_API_KEY}" \
      -d '{
        "elements": [{
          "type": "diagram",
          "id": "diagram-1",
          "code": "<your generated DSL>",
          "diagramType": "cloud-architecture-diagram"
        }],
        "scale": 2,
        "theme": "${ERASER_THEME:-dark}",
        "background": true
      }'
    
  5. 分析期间跟踪来源

    分析 Bicep 文件和资源以生成图表时,请跟踪:

    • 内部文件:记录读取的每个 Bicep 文件路径,以及提取的资源(例如,infra/main.bicep - VNet 和子网定义,infra/sql.bicep - SQL Database 配置)
    • 外部引用:记录查阅过的文档、示例或 URL(例如,Azure Bicep 文档、Azure 架构最佳实践)
    • 注释:针对每个来源,记录其对图表的贡献
  6. 处理响应

    关键:最简输出格式

    响应必须始终包含以下元素,并使用清晰的标题:

    1. 图表预览:使用标题显示

      ## Diagram
      ![{Title}]({imageUrl})
      

      使用 API 响应中的实际 imageUrl。

    2. 编辑器链接:使用标题显示

      ## Open in Eraser
      [Edit this diagram in the Eraser editor]({createEraserFileUrl})
      

      使用 API 响应中的实际 URL。

    3. 来源部分:简要列出分析过的文件/资源(如适用)

      ## Sources
      - `path/to/file` - What was extracted
      
    4. 图表代码部分:在带有 eraser 语言标记的代码块中提供 Eraser DSL

      ## Diagram Code
      ```eraser
      {DSL code here}
      
    5. 了解更多链接:You can learn more about Eraser at https://docs.eraser.io/docs/using-ai-agent-integrations

    其他内容规则:

    • 如果用户只要求生成图表,除以上 5 项外,不要包含任何其他内容
    • 如果用户明确要求更多内容(例如“解释架构”“提出改进建议”),可以添加相关内容
    • 不要添加未请求的部分,例如“概述”“安全注意事项”“测试”等

    默认输出应简短。图表本身已能说明问题。

  7. 处理模块

    • 如果使用了模块,请展示模块边界
    • 包含模块参数和输出
    • 展示模块与主资源的连接方式

Bicep 专属提示

  • 显示资源组:Bicep 部署以资源组为目标
  • 将 VNet 作为容器:显示包含子网和资源的 VNet
  • 包含依赖关系:显示 dependsOn 关系
  • 模块结构:如果使用了模块,请显示其边界
  • 参数:注明影响资源配置的关键参数
  • 使用 Azure 图标:指定使用 Azure 专属样式

示例:包含参数和模块的 Bicep

用户输入
@description('The name of the Virtual Network')
param vnetName string = 'myVNet'
@description('The address prefix for the VNet')
param vnetAddressPrefix string = '10.0.0.0/16'
@description('The address prefix for the subnet')
param subnetAddressPrefix string = '10.0.1.0/24'
@description('VM size')
param vmSize string = 'Standard_B1s'

// Main VNet resource
resource virtualNetwork 'Microsoft.Network/virtualNetworks@2021-05-01' = {
  name: vnetName
  location: resourceGroup().location
  properties: {
    addressSpace: {
      addressPrefixes: [vnetAddressPrefix]
    }
    subnets: [
      {
        name: 'subnet1'
        properties: {
          addressPrefix: subnetAddressPrefix
        }
      }
    ]
  }
}

// VM resource with dependsOn
resource virtualMachine 'Microsoft.Compute/virtualMachines@2021-11-01' = {
  name: 'myVM'
  location: resourceGroup().location
  properties: {
    hardwareProfile: {
      vmSize: vmSize
    }
  }
  dependsOn: [virtualNetwork]
}

// Module usage
module storageModule './modules/storage.bicep' = {
  name: 'storage'
  params: {
    location: resourceGroup().location
  }
}
预期行为
  1. 解析 Bicep:

    • 参数:vnetName、vnetAddressPrefix、subnetAddressPrefix、vmSize
    • 资源:包含子网的 VNet,以及带有 dependsOn 关系的 VM
    • 模块:带参数的存储模块
  2. 生成展示 Bicep 专属功能的 DSL:

    myVNet [label: "VNet 10.0.0.0/16"] {
      subnet1 [label: "Subnet 1 10.0.1.0/24"] {
        myVM [icon: azure-vm, label: "VM Standard_B1s"]
      }
    }
    
    storage-module [label: "Storage Module"] {
      storage-account [icon: azure-storage]
    }
    
    myVNet -> myVM
    

    重要:所有标签文本都必须放在引号内的单行中。Bicep 专属要求:将模块显示为容器,包含 dependsOn 关系,并注明资源配置中参数的用法。

  3. 使用 diagramType: "cloud-architecture-diagram" 调用 /api/render/elements

  4. 使用 diagramType: "cloud-architecture-diagram" 调用 /api/render/elements

结果

用户会收到一张展示以下内容的图表:

  • 将 VNet 显示为容器
  • 将子网嵌套在 VNet 内
  • 将 VM 放在子网中
  • 显示依赖关系
  • 使用恰当的 Azure 样式

评论

支持 Markdown