# Live-Presenter Keynote Technical Lecture Video Generator (`tech_slides_video`).
#
# End-to-end multimodal lecture studio:
#   1. Multi-source grounding (`web_search`, optional `repo_snapshot` & `pdf_extract`)
#   2. Structured Keynote SlideDeck + progressive step-by-step presenter script (`spoken` vs `caption`)
#   3. Parallel D2 vector architecture diagram (`figure`), AI editorial illustrations (`text_to_image`),
#      and optional presenter studio portrait / Veo keynote clip (`spotlight` | `pip` | `intro_outro`)
#   4. 1920x1080 vector slide rendering with chapter opener cards, non-overlapping code callouts,
#      spotlight rings, PiP safe zone, and global progress bar (`render_slides`)
#   5. Parallel per-step expressive TTS (`map -> tts` with vocal `instructions`)
#   6. Millisecond-accurate FFmpeg assembly with breathing pauses, smooth slide fades, rounded PiP
#      presenter overlay, SRT + WebVTT subtitles (`render_video`), plus `.pptx` & `.pdf` (`render_document`)
#
#   yanshi workflow run examples/generators/tech_slides_video.yml --timeout 15m \
#     --input topic "Rust Traits 深度讲解：零成本抽象与虚表内存模型" \
#     --output-dir runs/traits-lecture
name: tech_slides_video
description: "拟真人 PPT 演播级 1080p 技术精讲视频：支持联网/仓库/PDF 多源输入、毫秒级按步声画卡点、视线引导口播、D2 架构图与 AI 插图、聚光灯/画中画/片头出镜三模式及 PPTX/PDF 导出"

inputs:
  topic:
    description: "技术精讲主题"
    default: "Rust Traits 深度讲解：零成本静态单态化与胖指针动态分发"
  audience:
    description: "目标听众群体"
    default: "后端研发工程师与系统架构师"
  narration_style:
    description: "讲师口播风格与现场节奏"
    default: "资深系统架构师现场 Keynote 讲演：逻辑层层递进、善用真实工程判据，自然引导听众视线聚焦屏幕左右栏代码行号与架构节点"
  source_text:
    description: "可选补充笔记或参考文本"
    default: ""
  source_repo:
    description: "可选 GitHub 仓库链接或本地源码目录（自动扫描代码树与模块依赖）"
    default: ""
  source_pdf:
    description: "可选论文/技术文档 PDF 路径或 arXiv 链接（自动分页提取正文）"
    default: ""
  enable_web_search:
    description: "是否联网搜索最新技术细节与工程实践支撑内容"
    default: true
  slide_count:
    description: "正片幻灯片章节页数（4-10）"
    default: 6
  spoken_language:
    description: "课件文字与口播语言"
    default: "中文"
  slide_theme:
    description: "幻灯片配色主题：'dark'（暗色开发者演播风）或 'light'（浅色学术讲义风）"
    default: "dark"
  presenter_mode:
    description: "主讲人呈现模式：'spotlight'（默认全屏聚光灯）| 'pip'（右下角主讲人画中画）| 'intro_outro'（片头片尾主讲人镜头 + 正片全屏幻灯片）"
    default: "spotlight"
  section_cards:
    description: "章节切换时自动插入 Keynote 风格大标题过渡卡与导语配音"
    default: true
  enable_d2_diagram:
    description: "为核心机制页自动编译高精度 D2 矢量架构图"
    default: true
  enable_illustrations:
    description: "为开篇引入或总结页调用文生图生成无文字概念插图"
    default: true
  chat_model:
    description: "主策划与脚本生成模型（推荐 gemini-2.5-flash / gemini-2.5-pro，兼容 qwen-plus / deepseek-v4-flash / glm-5.1 / kimi-k2.6）"
    default: "gemini-2.5-flash"
  tts_model:
    description: "语音合成模型（推荐 gemini-2.5-flash-preview-tts，兼容 qwen3-tts-flash / step-tts-mini / gpt-4o-mini-tts）"
    default: "gemini-2.5-flash-preview-tts"
  voice:
    description: "主讲人音色（如 Kore / Charon / Puck，跨厂商切换时自动归一化兜底）"
    default: "Kore"
  tts_instructions:
    description: "TTS 演播语气导演指令（Gemini TTS 与 OpenAI TTS 原生支持，其他厂商自动忽略）"
    default: "Speak as a seasoned, engaging systems architect delivering a live technical keynote at a brisk, natural conversational pace, with clear emphasis on technical terms and natural breathing cadence."
  image_model:
    description: "文生图模型（用于概念插图与主讲人形象，推荐 gemini-3.1-flash-image，兼容 wan2.5-t2i-preview / glm-image）"
    default: "gemini-3.1-flash-image"
  image_size:
    description: "生成插图画面比例或尺寸"
    default: "16:9"
  style_preset:
    description: "插图艺术指导预设：'editorial_tech' | 'isometric_3d' | 'flat_vector' | 'blueprint_schematic' | 'scientific_diagram' | 'cinematic_keynote'"
    default: "editorial_tech"
  visual_style:
    description: "AI 插图补充视觉艺术风格（与 style_preset 叠加生效）"
    default: "glowing copper-orange and cyan conduits, crisp architectural geometry, studio rim lighting"
  refine_prompt:
    description: "是否先经艺术总监模型精炼视觉隐喻提示词"
    default: true
  video_model:
    description: "可选动态主讲人视频模型（留空默认使用高清演播室讲师形象卡，兼容所有厂商 Key；如有视频权限可填 veo-3.1-fast-generate-001 / kling-4.0-flash / hailuo-02）"
    default: ""
  presenter_prompt:
    description: "主讲人演播室镜头画面提示词（仅在 pip / intro_outro 模式下使用）"
    default: "A confident software architect speaking naturally on a modern dark developer keynote stage, subtle warm rim light and soft cyan background bokeh, waist-up framing, calm hand gesture, photorealistic 16:9, strictly no text or logos"
  step_pause_ms:
    description: "页内动画步之间的自然呼吸停顿时长（毫秒）"
    default: 260
  slide_pause_ms:
    description: "翻页边界的自然留白时长（毫秒）"
    default: 550
  burn_subtitles:
    description: "是否将半透明演播底衬字幕直接烧录进视频帧（默认 false 封装可开关软字幕轨 + .srt + .vtt）"
    default: false
  export_pptx:
    description: "导出可编辑 PowerPoint (.pptx) 课件（含高清帧与讲师逐字稿备注）"
    default: true
  export_pdf:
    description: "导出高清排版讲义 (.pdf)"
    default: true

nodes:
  - id: mode_flags
    type: template
    parameters:
      output_format: json
      template: |
        {
          "do_search": {{ enable_web_search }},
          "has_repo": {{ source_repo != "" }},
          "has_pdf": {{ source_pdf != "" }},
          "do_d2": {{ enable_d2_diagram }},
          "is_pip": {{ presenter_mode == "pip" }},
          "is_intro_outro": {{ presenter_mode == "intro_outro" }},
          "need_presenter": {{ presenter_mode == "pip" or presenter_mode == "intro_outro" }},
          "need_presenter_video": {{ (presenter_mode == "pip" or presenter_mode == "intro_outro") and video_model != "" }},
          "source_repo": {{ source_repo | json_encode() }},
          "source_pdf": {{ source_pdf | json_encode() }},
          "chat_model": {{ chat_model | json_encode() }},
          "slide_theme": {{ slide_theme | json_encode() }},
          "d2_theme": {% if slide_theme == "dark" %}"200"{% else %}"0"{% endif %},
          "enable_illustrations": {{ enable_illustrations }},
          "visual_style": {{ visual_style | json_encode() }},
          "image_model": {{ image_model | json_encode() }},
          "image_size": {{ image_size | json_encode() }},
          "video_model": {{ video_model | json_encode() }},
          "presenter_prompt": {{ presenter_prompt | json_encode() }},
          "burn_subtitles": {{ burn_subtitles }},
          "pptx": {{ export_pptx }},
          "pdf": {{ export_pdf }}
        }

  - id: search_query
    type: template
    parameters:
      template: "{{ topic }} 底层原理 核心机制 内存模型 工程最佳实践"

  - id: research
    type: web_search
    dependencies: ["mode_flags", "search_query"]
    run_if: "{{ nodes.mode_flags.outputs.do_search }}"
    input_mapping:
      query: "{{ nodes.search_query.outputs.output }}"
    parameters:
      max_results: 6

  - id: repo_scan
    type: repo_snapshot
    dependencies: ["mode_flags"]
    run_if: "{{ nodes.mode_flags.outputs.has_repo }}"
    input_mapping:
      source: "{{ nodes.mode_flags.outputs.source_repo }}"
    parameters:
      max_files: 60

  - id: pdf_scan
    type: pdf_extract
    dependencies: ["mode_flags"]
    run_if: "{{ nodes.mode_flags.outputs.has_pdf }}"
    input_mapping:
      source: "{{ nodes.mode_flags.outputs.source_pdf }}"
    parameters:
      max_pages: 25

  - id: grounded_context
    type: template
    dependencies: ["mode_flags"]
    input_mapping:
      web_md: "{{ nodes.research.outputs.markdown }}"
      repo_md: "{{ nodes.repo_scan.outputs.markdown }}"
      pdf_md: "{{ nodes.pdf_scan.outputs.markdown }}"
    parameters:
      template: |
        {% if source_text != "" %}
        【用户补充参考资料】
        {{ source_text | truncate(length=10000) }}
        {% endif %}
        {% if repo_md is defined and repo_md %}
        【源码仓库结构与依赖快照】
        {{ repo_md | truncate(length=12000) }}
        {% endif %}
        {% if pdf_md is defined and pdf_md %}
        【PDF 论文/技术文档摘录】
        {{ pdf_md | truncate(length=12000) }}
        {% endif %}
        {% if web_md is defined and web_md %}
        【联网检索技术资料】
        {{ web_md | truncate(length=10000) }}
        {% endif %}

  - id: presenter_portrait
    type: text_to_image
    dependencies: ["mode_flags"]
    run_if: "{{ nodes.mode_flags.outputs.need_presenter }}"
    input_mapping:
      visual: "{{ nodes.mode_flags.outputs.presenter_prompt }}"
      model: "{{ nodes.mode_flags.outputs.image_model }}"
      size: "{{ nodes.mode_flags.outputs.image_size }}"
    parameters:
      prompt: "{{visual}}"

  - id: presenter_clip
    type: text_to_video
    dependencies: ["mode_flags", "presenter_portrait"]
    run_if: "{{ nodes.mode_flags.outputs.need_presenter_video }}"
    input_mapping:
      model: "{{ nodes.mode_flags.outputs.video_model }}"
      image: "{{ nodes.presenter_portrait.outputs.presenter_portrait_image }}"
    parameters:
      prompt: "Subtle natural head nod, calm speaking lip movement, gentle keynote presenter hand gesture, steady studio camera, soft bokeh"
      aspect_ratio: "16:9"
      duration_seconds: 6

  - id: plan_prompt
    type: template
    dependencies: ["grounded_context"]
    input_mapping:
      ref_pack: "{{ nodes.grounded_context.outputs.output }}"
    parameters:
      template: |
        你是一位享誉业界的资深系统架构师与技术 Keynote 主讲人。请围绕主题「{{ topic }}」，面向「{{ audience }}」，以「{{ narration_style }}」风格，设计一套共 {{ slide_count }} 页幻灯片的演播级技术精讲课件（语言：{{ spoken_language }}，配色主题：{{ slide_theme }}）。

        核心设计准则（极其重要，直接决定声画卡点与视觉表现）：
        1. **工程级双栏可视化面板（拒绝空洞口号）**：
           - 每页必须提供 `left` 主面板与 `right` 备选结构化面板（当未启用外部插图或处于结构化对比页时直接并排渲染）：
             * `code`：真实可编译的核心代码（控制在 8-15 行，2 空格缩进），配合 1-3 个精炼 `callouts`（`{"line": 行号, "text": "≤8字批注", "tone": "rust|cyan|green"}`）；
             * `flowchart`：编译流水线、调用链或决策拓扑（4-6 个 `nodes` 含 `id`、`label` 与简短 `sublabel`，配 `edges` 有向边说明）；
             * `memory`：栈/堆/虚表/结构体真实字节偏移布局（严格控制在 2-3 个 `regions`，含 `name` 与 `slots: [{"id": "s1", "offset": "+0x00", "label": "data_ptr", "value": "0x7ffd..."}]`，配 `pointers: [{"from": "s1", "to": "h1", "label": "points to"}]`）；
             * `table`：多方案横向对比矩阵（单元格以 `✓` / `✗` / `⚠` 开头标识优劣，设 `highlight_col` 突出主角列）；
             * `bullets`：带 `badge` 标签的高密度架构卡片（可设 `grid: true` 排成 2×2 全景知识象限用于开篇或收官页）；
           - **严禁输出空面板**：即使本页 `visual_mode` 为 `"illustration"` 或 `"arch_diagram"`，`left` 主面板也必须完整填充对应类型数据（`bullets` 的 `items` 至少 3-4 条完整卡片、`code` 非空、`flowchart` 的 `nodes` 至少 4 个、`memory` 的 `regions` 至少 2 个、`table` 的 `rows` 至少 3 行），绝不允许出现空数组或空白左栏。
        2. **混合视觉编排（`visual_mode`）**：
           - 全套课件挑选恰好 1 页最核心的系统架构或数据流页设为 `visual_mode: "arch_diagram"`，并在顶层 `arch_diagram_spec` 中详细描述该页 D2 架构图的分层容器、核心组件与调用边（8-12 个节点）；
           - 挑选 1-2 页（如第 1 页破题引入或末页总结升华）设为 `visual_mode: "illustration"`，提供纯英文视觉隐喻提示词 `illustration_prompt`（严禁包含任何文字、字母或水印）与简短中文图注 `illustration_caption`；
           - 其余硬核机制拆解页全部设为 `visual_mode: "structured_panel"`（左右双栏硬核对照，如左 `code` + 右 `memory` / `flowchart`）。
        3. **拟真人逐动画步声画卡点脚本（`steps` 数组，每页严格 2-4 步）**：
           - 每一步对应视频里的一个渐进点亮画面与一段独立合成的语音，必须同时给出：
             * `caption`：屏幕底栏/字幕展示的凝练短句（≤32 字，可保留精准代码符号如 `&dyn Trait`）；
             * `spoken`：**真人讲师现场口播稿**（1-2 句自然流畅的口语化讲解，必须包含自然的视线引导词——例如“请大家看屏幕左侧第 2 到 4 行”“注意看右边虚表里的这个函数指针”“对比表格第二列我们可以发现”——并且把生硬符号转化为自然口语读法，如把 `->` 写成“返回”、把 `&dyn` 写成“dyn 动态引用”，严禁出现括号舞台提示词）；
             * `code_lines`：若本页含 `code` 面板，填写当前步聚光灯高亮的 `[起始行号, 结束行号]`（1-based）；
             * `focus_ids`：若本页含 `flowchart` 或 `memory` 面板，填写当前步重点点亮的节点/槽位 `id` 列表；
             * `left_reveal` / `right_reveal`：当前步揭晓的条目/节点数量；最后一步设 `show_takeaway: true`。
        4. 每页提供完整的 `notes`（汇总整页讲稿供 PowerPoint 备注页使用）与一句一针见血的 `takeaway` 工程判据。
        {% if ref_pack | trim != "" %}
        5. 充分吸收并忠实引用以下真实技术背景资料（严禁编造虚假 API）：
        {{ ref_pack | truncate(length=22000) }}
        {% endif %}

  - id: deck
    type: llm
    dependencies: ["plan_prompt", "mode_flags"]
    input_mapping:
      prompt: "{{ nodes.plan_prompt.outputs.output }}"
      model: "{{ nodes.mode_flags.outputs.chat_model }}"
    parameters:
      max_tokens: 16384
      system: "你是资深系统架构师与 Keynote 演播编导，仅输出符合 SlideDeck JSON Schema 的严格合法 JSON。"
      json_schema:
        type: object
        required: [title, slides]
        properties:
          title: { type: string }
          subtitle: { type: string }
          author: { type: string }
          theme: { type: string }
          arch_diagram_spec:
            type: string
            description: "核心机制页的 D2 矢量架构图绘制说明（指明分层容器、组件名与有向调用边）"
          slides:
            type: array
            minItems: 1
            maxItems: 12
            items:
              type: object
              required: [title, left, right, visual_mode, takeaway, notes, steps]
              properties:
                section: { type: string }
                title: { type: string }
                subtitle: { type: string }
                left:
                  type: object
                  required: [kind]
                  properties:
                    kind:
                      type: string
                      enum: [bullets, code, flowchart, memory, table]
                    title: { type: string }
                    grid: { type: boolean }
                    items:
                      type: array
                      items:
                        type: object
                        required: [heading, body]
                        properties:
                          badge: { type: string }
                          heading: { type: string }
                          body: { type: string }
                          tone: { type: string }
                    language: { type: string }
                    code: { type: string }
                    callouts:
                      type: array
                      items:
                        type: object
                        required: [line, text]
                        properties:
                          line: { type: integer }
                          text: { type: string }
                          tone: { type: string }
                    direction: { type: string }
                    nodes:
                      type: array
                      items:
                        type: object
                        required: [id, label]
                        properties:
                          id: { type: string }
                          label: { type: string }
                          sublabel: { type: string }
                          tone: { type: string }
                    edges:
                      type: array
                      items:
                        type: object
                        required: [from, to]
                        properties:
                          from: { type: string }
                          to: { type: string }
                          label: { type: string }
                          dashed: { type: boolean }
                    regions:
                      type: array
                      items:
                        type: object
                        required: [name, slots]
                        properties:
                          name: { type: string }
                          subtitle: { type: string }
                          tone: { type: string }
                          slots:
                            type: array
                            items:
                              type: object
                              required: [id, label, value]
                              properties:
                                id: { type: string }
                                offset: { type: string }
                                label: { type: string }
                                value: { type: string }
                    pointers:
                      type: array
                      items:
                        type: object
                        required: [from, to]
                        properties:
                          from: { type: string }
                          to: { type: string }
                          label: { type: string }
                          tone: { type: string }
                    highlight_col: { type: integer }
                    columns:
                      type: array
                      items: { type: string }
                    rows:
                      type: array
                      items:
                        type: array
                        items: { type: string }
                right:
                  type: object
                  required: [kind]
                  properties:
                    kind:
                      type: string
                      enum: [bullets, code, flowchart, memory, table]
                    title: { type: string }
                    grid: { type: boolean }
                    items:
                      type: array
                      items:
                        type: object
                        required: [heading, body]
                        properties:
                          badge: { type: string }
                          heading: { type: string }
                          body: { type: string }
                          tone: { type: string }
                    language: { type: string }
                    code: { type: string }
                    callouts:
                      type: array
                      items:
                        type: object
                        required: [line, text]
                        properties:
                          line: { type: integer }
                          text: { type: string }
                          tone: { type: string }
                    direction: { type: string }
                    nodes:
                      type: array
                      items:
                        type: object
                        required: [id, label]
                        properties:
                          id: { type: string }
                          label: { type: string }
                          sublabel: { type: string }
                          tone: { type: string }
                    edges:
                      type: array
                      items:
                        type: object
                        required: [from, to]
                        properties:
                          from: { type: string }
                          to: { type: string }
                          label: { type: string }
                          dashed: { type: boolean }
                    regions:
                      type: array
                      items:
                        type: object
                        required: [name, slots]
                        properties:
                          name: { type: string }
                          subtitle: { type: string }
                          tone: { type: string }
                          slots:
                            type: array
                            items:
                              type: object
                              required: [id, label, value]
                              properties:
                                id: { type: string }
                                offset: { type: string }
                                label: { type: string }
                                value: { type: string }
                    pointers:
                      type: array
                      items:
                        type: object
                        required: [from, to]
                        properties:
                          from: { type: string }
                          to: { type: string }
                          label: { type: string }
                          tone: { type: string }
                    highlight_col: { type: integer }
                    columns:
                      type: array
                      items: { type: string }
                    rows:
                      type: array
                      items:
                        type: array
                        items: { type: string }
                split_ratio: { type: number }
                visual_mode:
                  type: string
                  enum: [structured_panel, arch_diagram, illustration]
                illustration_prompt: { type: string }
                illustration_caption: { type: string }
                takeaway: { type: string }
                notes: { type: string }
                steps:
                  type: array
                  minItems: 1
                  maxItems: 5
                  items:
                    type: object
                    required: [caption, spoken]
                    properties:
                      caption: { type: string }
                      spoken: { type: string }
                      left_reveal: { type: integer }
                      right_reveal: { type: integer }
                      code_lines:
                        type: array
                        items: { type: integer }
                      focus_ids:
                        type: array
                        items: { type: string }
                      show_takeaway: { type: boolean }

  - id: arch_figure_desc
    type: template
    dependencies: ["deck"]
    input_mapping:
      plan: "{{ nodes.deck.outputs.output }}"
    parameters:
      template: |
        为主题「{{ plan.title }}」绘制一张结构紧凑清晰的 D2 系统架构与核心机制图（`direction: right`，严格控制在 5-8 个节点以内；若需分组容器请直接写 `group_id: "分组标题" { node_a: "组件名" }`，严禁使用 `container` 关键字，跨分组连线必须写在所有 `{}` 之外的顶层并带完整路径 `group_a.node_x -> group_b.node_y`；保持默认主题配色）：
        {{ plan.arch_diagram_spec | default(value=plan.title) }}

  - id: arch_figure
    type: figure
    dependencies: ["mode_flags", "arch_figure_desc"]
    run_if: "{{ nodes.mode_flags.outputs.do_d2 }}"
    input_mapping:
      description: "{{ nodes.arch_figure_desc.outputs.output }}"
      model: "{{ nodes.mode_flags.outputs.chat_model }}"
      d2_theme: "{{ nodes.mode_flags.outputs.d2_theme }}"
    parameters:
      language: d2
      png: true
      png_width: 1600
      max_attempts: 4

  - id: illustrate
    type: illustration
    dependencies: ["deck", "mode_flags"]
    input_mapping:
      deck: "{{ nodes.deck.outputs.output }}"
      arch_image: "{{ nodes.arch_figure.outputs.arch_figure_svg }}"
      model: "{{ nodes.mode_flags.outputs.image_model }}"
      prompt_model: "{{ nodes.mode_flags.outputs.chat_model }}"
      size: "{{ nodes.mode_flags.outputs.image_size }}"
      style: "{{ nodes.mode_flags.outputs.visual_style }}"
      theme: "{{ nodes.mode_flags.outputs.slide_theme }}"
      enabled: "{{ nodes.mode_flags.outputs.enable_illustrations }}"
    parameters:
      arch_title: "核心系统架构图 (D2)"
      max_images: 3
      max_concurrent: 3
      graceful_fallback: true

  - id: slides
    type: render_slides
    dependencies: ["illustrate", "mode_flags"]
    input_mapping:
      deck: "{{ nodes.illustrate.outputs.illustrate_deck }}"
      pip_safe_zone: "{{ nodes.mode_flags.outputs.is_pip }}"
      takeaway_only_bar: "{{ nodes.mode_flags.outputs.burn_subtitles }}"
    parameters:
      width: 1920
      progressive: true

  - id: tts_items
    type: template
    dependencies: ["slides", "illustrate", "mode_flags"]
    input_mapping:
      beats: "{{ nodes.slides.outputs.slides_steps }}"
      plan: "{{ nodes.illustrate.outputs.illustrate_deck }}"
      is_intro_outro: "{{ nodes.mode_flags.outputs.is_intro_outro }}"
      p_img: "{{ nodes.presenter_portrait.outputs.presenter_portrait_image }}"
      p_vid: "{{ nodes.presenter_clip.outputs.presenter_clip_video }}"
    parameters:
      output_format: json
      template: |
        {% set host_vid = p_vid | default(value="") %}{% set host_img = p_img | default(value="") %}{% set has_host = is_intro_outro and (host_vid != "" or host_img != "") %}{% set intro_spoken = "大家好，欢迎来到《" ~ plan.title ~ "》技术精讲。今天我们将从核心设计动机出发，结合底层代码与架构模型，一步步拆解这套关键工程机制。" %}{% set intro_cap = plan.title ~ " · 主讲开场导言" %}{% set outro_spoken = "以上就是《" ~ plan.title ~ "》的核心脉络与工程判据复盘。希望这套底层拆解能直接助力你的日常架构实践，感谢观看，我们下期再见。" %}{"items": [
          {% if has_host %}{
            "narration": {{ intro_spoken | json_encode() }},
            "caption": {{ intro_cap | json_encode() }},
            "image": {{ host_img | json_encode() }},
            "video": {{ host_vid | json_encode() }},
            "is_slide_start": true,
            "is_slide_end": true,
            "hide_pip": true,
            "tts_model": {{ tts_model | json_encode() }},
            "voice": {{ voice | json_encode() }},
            "instructions": {{ tts_instructions | json_encode() }}
          },{% endif %}
          {% for b in beats %}{% set beat_spoken = b.spoken | default(value=b.caption) %}{
            "narration": {{ beat_spoken | json_encode() }},
            "caption": {{ b.caption | json_encode() }},
            "image": {{ b.image | json_encode() }},
            "video": "",
            "is_slide_start": {{ b.is_slide_start | json_encode() }},
            "is_slide_end": {{ b.is_slide_end | json_encode() }},
            "hide_pip": false,
            "tts_model": {{ tts_model | json_encode() }},
            "voice": {{ voice | json_encode() }},
            "instructions": {{ tts_instructions | json_encode() }}
          }{% if not loop.last or has_host %},{% endif %}{% endfor %}
          {% if has_host %}{
            "narration": {{ outro_spoken | json_encode() }},
            "caption": "核心脉络复盘与工程结语",
            "image": {{ host_img | json_encode() }},
            "video": {{ host_vid | json_encode() }},
            "is_slide_start": true,
            "is_slide_end": true,
            "hide_pip": true,
            "tts_model": {{ tts_model | json_encode() }},
            "voice": {{ voice | json_encode() }},
            "instructions": {{ tts_instructions | json_encode() }}
          }{% endif %}
        ]}

  - id: narrate
    type: map
    dependencies: ["tts_items"]
    input_mapping:
      input_list: "{{ nodes.tts_items.outputs.items }}"
    parameters:
      parallel: true
      max_concurrent: 2
      template:
        - id: say
          type: tts
          input_mapping:
            input_template: "{{ item.narration }}"
            model: "{{ item.tts_model }}"
            voice: "{{ item.voice }}"
            instructions: "{{ item.instructions }}"
          parameters:
            response_format: wav

  - id: cut
    type: template
    dependencies: ["tts_items", "narrate", "mode_flags"]
    input_mapping:
      beats: "{{ nodes.tts_items.outputs.items }}"
      voiced: "{{ nodes.narrate.outputs.results }}"
      is_pip: "{{ nodes.mode_flags.outputs.is_pip }}"
      p_img: "{{ nodes.presenter_portrait.outputs.presenter_portrait_image }}"
      p_vid: "{{ nodes.presenter_clip.outputs.presenter_clip_video }}"
    parameters:
      output_format: json
      template: |
        {% set pip_v = p_vid | default(value="") %}{% set pip_i = p_img | default(value="") %}{
          "pip_video": {% if is_pip and pip_v != "" %}{{ pip_v | json_encode() }}{% else %}""{% endif %},
          "pip_image": {% if is_pip and pip_i != "" %}{{ pip_i | json_encode() }}{% else %}""{% endif %},
          "scenes": [
            {% for b in beats %}{% set r = voiced | nth(n=loop.index0) %}{
              "audio": {{ r.say.Ok.say_output.path | json_encode() }},
              "caption": {{ b.caption | json_encode() }},
              {% if b.video != "" %}"video": {{ b.video | json_encode() }},{% else %}"image": {{ b.image | json_encode() }},{% endif %}
              "is_slide_start": {{ b.is_slide_start | json_encode() }},
              "is_slide_end": {{ b.is_slide_end | json_encode() }},
              "hide_pip": {{ b.hide_pip | json_encode() }}
            }{% if not loop.last %},{% endif %}{% endfor %}
          ]
        }

  - id: film
    type: render_video
    dependencies: ["cut"]
    input_mapping:
      scenes: "{{ nodes.cut.outputs.scenes }}"
      pip_video: "{{ nodes.cut.outputs.pip_video }}"
      pip_image: "{{ nodes.cut.outputs.pip_image }}"
    parameters:
      width: 1920
      height: 1080
      transition: fade

  - id: deliverable
    type: template
    dependencies: ["illustrate", "film", "slides"]
    input_mapping:
      plan: "{{ nodes.illustrate.outputs.illustrate_deck }}"
      painted: "{{ nodes.illustrate.outputs.illustrate_images }}"
      mp4: "{{ nodes.film.outputs.film_output }}"
      srt: "{{ nodes.film.outputs.film_subtitles }}"
      vtt: "{{ nodes.film.outputs.film_vtt }}"
      seconds: "{{ nodes.film.outputs.film_seconds }}"
      html: "{{ nodes.slides.outputs.slides_html }}"
      arch_svg: "{{ nodes.arch_figure.outputs.arch_figure_svg }}"
      pptx: "{{ nodes.slides.outputs.slides_pptx }}"
      pdf: "{{ nodes.slides.outputs.slides_pdf }}"
    parameters:
      output_format: json
      template: |
        {"deliverable": {
          "kind": "video_overview",
          "title": {{ plan.title | json_encode() }},
          "content": {
            "slides": {{ plan.slides | json_encode() }},
            "seconds": {{ seconds | json_encode() }},
            "presenter_mode": {{ presenter_mode | json_encode() }}
          },
          "citations": [],
          "files": [
            {"node_id": "film", "name": {{ mp4 | split(pat="/") | last | json_encode() }}, "mime_type": "video/mp4", "role": "primary"},
            {"node_id": "film", "name": {{ srt | split(pat="/") | last | json_encode() }}, "mime_type": "application/x-subrip", "role": "asset"}{% if vtt is defined and vtt %},
            {"node_id": "film", "name": {{ vtt | split(pat="/") | last | json_encode() }}, "mime_type": "text/vtt", "role": "asset"}{% endif %},
            {"node_id": "slides", "name": {{ html | split(pat="/") | last | json_encode() }}, "mime_type": "text/html", "role": "preview"}{% if arch_svg is defined and arch_svg %},
            {"node_id": "arch_figure", "name": {{ arch_svg | split(pat="/") | last | json_encode() }}, "mime_type": "image/svg+xml", "role": "asset"}{% endif %}{% for img in painted %},
            {"node_id": "illustrate", "name": {{ img.path | split(pat="/") | last | json_encode() }}, "mime_type": {{ img.mime_type | default(value="image/png") | json_encode() }}, "role": "asset"}{% endfor %}{% if pptx is defined and pptx %},
            {"node_id": "slides", "name": {{ pptx | split(pat="/") | last | json_encode() }}, "mime_type": "application/vnd.openxmlformats-officedocument.presentationml.presentation", "role": "primary"}{% endif %}{% if pdf is defined and pdf %},
            {"node_id": "slides", "name": {{ pdf | split(pat="/") | last | json_encode() }}, "mime_type": "application/pdf", "role": "primary"}{% endif %}
          ]
        }}
