{
  "entry_node_id": "triage",
  "exit_node_ids": [
    "finalize"
  ],
  "guards": {
    "max_total_steps": 12,
    "max_model_calls": 14,
    "max_retry_attempts": 4,
    "per_node_max_visits": {
      "draft": 3,
      "review": 3
    }
  },
  "compliance": {
    "regulated": false,
    "affects_clinical_output": true,
    "data_classification": "none"
  },
  "metadata": {
    "owner": "Demo Hospital",
    "tags": [
      "tutorial",
      "portal-reply"
    ]
  },
  "intended_use": {
    "statement": "Drafts a reply to a patient portal message for clinic staff to review before anything is sent.",
    "intended_users": [
      "Clinic nurse"
    ]
  },
  "input_schema": [
    {
      "key": "query",
      "label": "Patient message",
      "type": "text",
      "required": true,
      "description": "The text of the portal message. Use synthetic text only."
    }
  ],
  "examples": [
    {
      "name": "Refill question",
      "input": {
        "query": "Hello, this is Test Patient A. Can I get my usual refill sent to the pharmacy on Main Street?"
      }
    },
    {
      "name": "Urgent symptom",
      "input": {
        "query": "Test Patient B here. Since this morning I have chest pain that spreads to my arm."
      }
    }
  ],
  "nodes": [
    {
      "id": "triage",
      "name": "Triage",
      "description": "Decides whether the message is routine or urgent.",
      "routing_mode": "single",
      "routes": [
        {
          "name": "routine",
          "description": "Administrative or non-urgent question that a nurse can answer in writing."
        },
        {
          "name": "urgent",
          "description": "Any symptom that could need same-day clinical attention."
        }
      ],
      "inline_model_config": {
        "provider": "google",
        "model_name": "gemini-2.5-flash",
        "temperature": 0.1
      },
      "inline_prompt": {
        "template": "You triage patient portal messages for Demo Hospital.\nMessage: {{.query}}\n\nReturn JSON with the key \"category\" (one short label such as refill, appointment, results, symptom) and choose the route. When in doubt, choose urgent."
      },
      "output_schema": {
        "type": "object",
        "properties": {
          "route": {
            "type": "string"
          },
          "category": {
            "type": "string"
          }
        }
      },
      "retry_policy": {
        "max_attempts": 2,
        "backoff_ms": 500
      }
    },
    {
      "id": "draft",
      "name": "Drafter",
      "description": "Writes the reply, and rewrites it when the reviewer asks for changes.",
      "routing_mode": "none",
      "inline_model_config": {
        "provider": "google",
        "model_name": "gemini-2.5-flash",
        "temperature": 0.3
      },
      "inline_prompt": {
        "template": "Write a short reply (under 120 words, plain text) to this patient portal message for a nurse to review.\nMessage: {{.query}}\nCategory: {{.category}}\n{{if .feedback}}\nA reviewer rejected the previous draft.\nReviewer feedback: {{.feedback}}\nPrevious draft: {{.text}}\nRewrite the draft and fix every point.\n{{end}}\nRules: no diagnosis, no medication doses, no promises about timing."
      },
      "retry_policy": {
        "max_attempts": 2,
        "backoff_ms": 500
      }
    },
    {
      "id": "review",
      "name": "Reviewer",
      "description": "Checks the draft against the rules and approves it or asks for a rewrite.",
      "routing_mode": "single",
      "routes": [
        {
          "name": "approve",
          "description": "The draft follows every rule."
        },
        {
          "name": "revise",
          "description": "The draft breaks a rule; explain what to fix in \"feedback\"."
        }
      ],
      "inline_model_config": {
        "provider": "google",
        "model_name": "gemini-2.5-pro",
        "temperature": 0
      },
      "inline_prompt": {
        "template": "You review a draft reply to a patient portal message.\nMessage: {{.query}}\nDraft: {{.text}}\n\nRules: no diagnosis, no medication doses, no promises about timing, under 120 words.\nReturn JSON with the keys \"route\" and \"feedback\". Put the specific problems in \"feedback\" when you choose revise, and an empty string when you choose approve."
      },
      "output_schema": {
        "type": "object",
        "properties": {
          "route": {
            "type": "string"
          },
          "feedback": {
            "type": "string"
          }
        }
      },
      "retry_policy": {
        "max_attempts": 2,
        "backoff_ms": 500
      }
    },
    {
      "id": "escalate",
      "name": "Nurse takes over",
      "description": "Pauses the run until a nurse supplies the reply text.",
      "routing_mode": "none",
      "human_in_the_loop": true,
      "inline_model_config": {
        "provider": "google",
        "model_name": "gemini-2.5-flash"
      },
      "inline_prompt": {
        "template": "Human review step. The reply is supplied by a person: {{.query}}"
      }
    },
    {
      "id": "finalize",
      "name": "Finalize",
      "description": "Applies the clinic sign-off format to the approved text.",
      "routing_mode": "none",
      "inline_model_config": {
        "provider": "google",
        "model_name": "gemini-2.5-flash",
        "temperature": 0
      },
      "inline_prompt": {
        "template": "Return the reply below unchanged, followed on a new line by: \"Reviewed by clinic staff before sending.\"\n\n{{.text}}"
      }
    }
  ],
  "edges": [
    {
      "id": "e1",
      "source_node_id": "triage",
      "target_node_id": "draft",
      "kind": "route",
      "route_name": "routine"
    },
    {
      "id": "e2",
      "source_node_id": "triage",
      "target_node_id": "escalate",
      "kind": "route",
      "route_name": "urgent"
    },
    {
      "id": "e3",
      "source_node_id": "triage",
      "target_node_id": "escalate",
      "kind": "fallback"
    },
    {
      "id": "e4",
      "source_node_id": "draft",
      "target_node_id": "review",
      "kind": "deterministic"
    },
    {
      "id": "e5",
      "source_node_id": "review",
      "target_node_id": "finalize",
      "kind": "route",
      "route_name": "approve"
    },
    {
      "id": "e6",
      "source_node_id": "review",
      "target_node_id": "draft",
      "kind": "route",
      "route_name": "revise",
      "max_traversals": 2
    },
    {
      "id": "e7",
      "source_node_id": "review",
      "target_node_id": "escalate",
      "kind": "fallback"
    },
    {
      "id": "e8",
      "source_node_id": "escalate",
      "target_node_id": "finalize",
      "kind": "deterministic"
    }
  ]
}
