# TikChip Sourcing & Quotation Comparison
# 关键词寻源 -> 认证供应商/型号匹配 -> 拉取阶梯报价 -> 生成比价表 -> 可选发起 RFQ
#
# 适用框架：AutoGen / Semantic Kernel / Dify / 自研 Agent
# 底层协议：TikChip Agent Gateway（REST / MCP）。所有工具均指向同一份 agent-manifest。
# 密钥：TIKCHIP_API_KEY（在 https://www.tikchip.cn/developers/api-key 申请）

skill:
  id: tikchip-sourcing
  name: TikChip Sourcing & Quotation Comparison
  version: 1.0.0
  author: TikChip Technology
  license: MIT
  description: >-
    根据关键词/参数在 TikChip 上寻源电子元器件，匹配认证供应商与型号，拉取阶梯价与交期，
    生成多供应商/多型号比价表，并可选地发起 RFQ 询盘。
  tags: [b2b, sourcing, procurement, quotation, electronic-components]
  homepage: https://www.tikchip.cn/developers

runtime:
  transport: rest            # 也可声明 mcp，由框架内部走 @tikchip/mcp-server
  api_base: ${TIKCHIP_API_BASE:-https://api.tikchip.cn}
  auth:
    type: api_key
    header: Authorization
    scheme: Bearer
    env: TIKCHIP_API_KEY
  rate_limits:
    per_minute: 60
    daily_inquiry: 20
  retry:
    max_attempts: 3
    backoff: exponential
    honor_retry_after: true

# ---------------------------------------------------------------- 输入
inputs:
  keyword:
    type: string
    required: true
    description: 寻源关键词 / 型号，例如 "STM32F103C8T6"、"LDO regulator 3.3V"
  manufacturer:
    type: string
    required: false
    description: 指定品牌，例如 "STMicroelectronics"
  specs:
    type: object
    required: false
    description: 参数过滤，例如 {"package":"LQFP48","voltage":"3.3V"}
  quantity:
    type: integer
    default: 1000
    description: 目标采购量，用于计算对应阶梯价
  in_stock_only:
    type: boolean
    default: true
    description: 仅返回有货型号
  max_candidates:
    type: integer
    default: 10
    description: 最多比对候选型号数量
  certs:
    type: array
    required: false
    description: 需要供应商具备的认证，例如 ["RoHS","REACH","ISO9001"]
  auto_inquiry:
    type: boolean
    default: false
    description: 是否对最优候选自动发起 RFQ
  target_price:
    type: number
    required: false
    description: 目标单价（自动询盘时使用）
  buyer:
    type: object
    required: false
    description: 采购方信息（auto_inquiry 时必填）
    properties:
      email:   { type: string, example: buyer@acme.com }
      company: { type: string, example: ACME Electronics }
      name:    { type: string, required: false }
      phone:   { type: string, required: false }

# ---------------------------------------------------------------- 输出
outputs:
  candidates: array          # 候选型号：{mpn, brand, package, stock, lead_time, unit_price}
  comparison_table:          # 比价表（按单价升序）
    - { rank: 1, mpn: string, brand: string, unit_price: number, currency: string,
        stock: integer, lead_time_days: integer, certs: array, datasheet_url: string }
  best_offer:
    mpn: string
    unit_price: number
    reason: string           # 例如 "lowest_price_in_stock_with_rohs"
  inquiry:
    inquiry_id: string
    expected_reply_hours: integer

# ---------------------------------------------------------------- 工具映射（= agent-manifest）
tools:
  search_components:      { method: GET,  path: /api/v1/dev/search }
  get_component_detail:   { method: GET,  path: "/api/v1/dev/component/{mpn}" }
  compare_components:     { method: POST, path: /api/v1/dev/compare }
  check_stock:            { method: GET,  path: /api/v1/dev/stock }
  recommend_alternatives: { method: GET,  path: /api/v1/dev/alternatives }
  submit_inquiry:         { method: POST, path: /api/v1/dev/inquiry }

# ---------------------------------------------------------------- 工作流
workflow:
  - id: source
    name: 关键词寻源
    tool: search_components
    args:
      keyword: ${inputs.keyword}
      manufacturer: ${inputs.manufacturer}
      specs: ${inputs.specs}
      in_stock_only: ${inputs.in_stock_only}
      size: ${inputs.max_candidates}
    output: search_result      # -> { items: [{mpn, brand, package, price_tiers, stock, stock_status, datasheet_url}] }

  - id: collect_quotes
    name: 拉取候选详情与阶梯价
    foreach: ${search_result.items}
    as: item
    tool: get_component_detail
    args:
      mpn: ${item.mpn}
      quantity: ${inputs.quantity}
    output: candidate_details   # 收集 [{mpn, params, price_tiers, stock, lead_time, certs, datasheet_url}]

  - id: filter_certs
    name: 认证过滤
    when: ${inputs.certs} != null
    action: filter
    foreach: ${candidate_details}
    as: c
    expression: |
      # 保留满足全部所需认证的候选；缺失认证的候选降级到 waitlist
      all(${inputs.certs}.map(cert => cert in c.certs))
    on_pass: match_candidates
    on_fail: waitlist

  - id: build_comparison
    name: 生成比价表
    action: compute
    foreach: ${match_candidates}
    as: c
    expression: |
      row = {
        mpn: c.mpn,
        brand: c.brand,
        unit_price: price_at(c.price_tiers, ${inputs.quantity}),
        currency: "USD",
        stock: c.stock,
        lead_time_days: c.lead_time_days,
        certs: c.certs,
        datasheet_url: c.datasheet_url
      };
      push(${comparison_rows}, row)
    output: comparison_rows

  - id: rank
    name: 排序并选最优
    action: sort
    input: ${comparison_rows}
    by: ["unit_price asc", "lead_time_days asc", "stock desc"]
    output: comparison_table
    then:
      best_offer:
        mpn: ${comparison_table[0].mpn}
        unit_price: ${comparison_table[0].unit_price}
        reason: lowest_price_in_stock

  - id: alternatives
    name: 补充替代型号（可选）
    when: len(${comparison_table}) < 3
    tool: recommend_alternatives
    args:
      mpn: ${inputs.keyword}
      type: functional
      limit: 5
    output: alternatives

  - id: inquiry
    name: 对最优候选发起 RFQ
    when: ${inputs.auto_inquiry} == true AND ${comparison_table} != null
    tool: submit_inquiry
    args:
      items:
        - mpn: ${best_offer.mpn}
          qty: ${inputs.quantity}
      target_price: ${inputs.target_price}
      currency: USD
      incoterm: FOB
      certs: ${inputs.certs}
      note: "Auto-generated by tikchip-sourcing: sourcing request for ${inputs.keyword}."
      contact: ${inputs.buyer}
    output: inquiry

  - id: report
    name: 输出报告
    action: emit
    args:
      candidates: ${candidate_details}
      comparison_table: ${comparison_table}
      best_offer: ${best_offer}
      inquiry: ${inquiry}

examples:
  - name: 手工运行（CLI 等价命令）
    run: |
      tikchip-cli search "STM32F103C8T6" --stock-only --format=json
      tikchip-cli detail STM32F103C8T6 --quantity 5000
      tikchip-cli compare STM32F103C8T6 STM32F103C8T6TR STM32F103CBT6
      tikchip-cli rfq create --sku=STM32F103C8T6 --qty=5000 --target-price=12.5 --email=buyer@acme.com

  - name: Agent 对话（Workbuddy，经 WebMCP）
    prompt: >-
      帮我在 TikChip 上找 5000 片 STM32F103C8T6（要 RoHS），拉出所有在货型号的阶梯价，
      按单价排个比价表，选出最优的那家，并替我发一封询盘问交期。
