# Y4.2 — `mindmap` generator (docs/RFC_CONTENT_STUDIO.md §3, §4.3).
#
# Source (a URL, fetched with the http node's SSRF filter, or pasted
# text) → a labelled tree (llm, json_schema) → mind map as SVG, PNG
# (render_svg) and an interactive markmap page → one `Deliverable`.
#
#   yanshi workflow run examples/generators/mindmap.yml \
#     --input url https://example.org/article --output-dir out
name: mindmap
description: "A mind map of a source: SVG, PNG and an interactive HTML page"

inputs:
  url:
    description: "Page to map (fetched with SSRF protection); leave empty to use `text`"
    default: ""
  text:
    description: "Source text, used when `url` is empty"
    default: ""
  focus:
    description: "What the map should organise"
    default: "the main ideas and how they relate"
  depth:
    description: "Levels below the root (2-4 reads best)"
    default: 3
  chunks:
    description: "Notebook passages to work from instead of url / text: [{chunk_id, source_id, content}]. Citations then point at these ids."
    default: []

nodes:
  - id: options
    type: template
    parameters:
      output_format: json
      template: '{"has_url": {{ url != "" }}}'

  # `url` (the workflow input) reaches the node directly.
  - id: fetch
    type: http
    dependencies: ["options"]
    run_if: "{{ nodes.options.outputs.has_url }}"

  - id: source
    type: template
    input_mapping:
      page: "{{ nodes.fetch.outputs.body }}"
    parameters:
      # With notebook `chunks`, every passage is labelled with its id so
      # the model can say where each quote comes from.
      template: "{% if chunks | length > 0 %}{% for c in chunks %}[{{ c.chunk_id }}] {{ c.content }}\n\n{% endfor %}{% elif page %}{{ page | striptags | truncate(length=24000) }}{% else %}{{ text | truncate(length=24000) }}{% endif %}"

  - id: tree_prompt
    type: template
    dependencies: ["source"]
    input_mapping:
      source_text: "{{ nodes.source.outputs.output }}"
    parameters:
      template: |
        Build a mind map of the source below, organising {{ focus }}.
        The root is the subject in a few words; go at most {{ depth }}
        levels below it, 3-6 children per node, labels of at most six
        words. Use only what the source supports. When the source's
        passages carry [ids], give every first- and second-level node the
        chunk_id of the passage it summarises.

        SOURCE:
        {{ source_text }}

  - id: tree
    type: llm
    dependencies: ["tree_prompt"]
    input_mapping:
      prompt: "{{ nodes.tree_prompt.outputs.output }}"
    parameters:
      system: "You organise ideas into mind maps. Answer with JSON only."
      json_schema:
        $defs:
          node:
            type: object
            required: [label]
            properties:
              label: { type: string }
              chunk_id: { type: string }
              children: { type: array, items: { $ref: "#/$defs/node" } }
        $ref: "#/$defs/node"

  - id: map_svg
    type: render_mindmap
    dependencies: ["tree"]
    input_mapping:
      tree: "{{ nodes.tree.outputs.output }}"

  - id: map_png
    type: render_svg
    dependencies: ["map_svg"]
    input_mapping:
      svg: "{{ nodes.map_svg.outputs.map_svg_output }}"
    parameters:
      width: 1600

  - id: map_html
    type: render_mindmap
    dependencies: ["tree"]
    input_mapping:
      tree: "{{ nodes.tree.outputs.output }}"
    parameters:
      format: html

  - id: deliverable
    type: template
    dependencies: ["map_svg", "map_png", "map_html"]
    input_mapping:
      map: "{{ nodes.tree.outputs.output }}"
      svg: "{{ nodes.map_svg.outputs.map_svg_output }}"
      png: "{{ nodes.map_png.outputs.map_png_output }}"
      html: "{{ nodes.map_html.outputs.map_html_output }}"
    parameters:
      output_format: json
      template: |
        {"deliverable": {
          "kind": "mindmap",
          "title": {{ map.label | json_encode() }},
          "content": {"tree": {{ map | json_encode() }}},
          "citations": [{% set_global first = true %}{% for b in map.children | default(value=[]) %}{% for n in [b] | concat(with=b.children | default(value=[])) %}{% if n.chunk_id %}{% set_global sid = "" %}{% set_global passage = "" %}{% for c in chunks %}{% if c.chunk_id == n.chunk_id %}{% set_global sid = c.source_id %}{% set_global passage = c.content %}{% endif %}{% endfor %}{% if passage %}{% if not first %},{% endif %}{% set_global first = false %}{"source_id": {{ sid | json_encode() }}, "chunk_id": {{ n.chunk_id | json_encode() }}, "quote": {{ passage | truncate(length=300) | json_encode() }}}{% endif %}{% endif %}{% endfor %}{% endfor %}],
          "files": [
            {"node_id": "map_svg", "name": {{ svg | split(pat="/") | last | json_encode() }}, "mime_type": "image/svg+xml", "role": "primary"},
            {"node_id": "map_png", "name": {{ png | split(pat="/") | last | json_encode() }}, "mime_type": "image/png", "role": "preview"},
            {"node_id": "map_html", "name": {{ html | split(pat="/") | last | json_encode() }}, "mime_type": "text/html", "role": "asset"}
          ]
        }}
