# rye:signed:2026-03-17T03:48:47Z:d672bea4e79b77e52977295f31fa094b137d9a96eb7bef14921cb7d3f7f4932a:pInhmlMUMkN4UroILfFiziju3T80PI1_wK_8oRhQ7TQtuS8O4VPvfP7oLpjxEILbLwJJnhQtDDPTV_9e_biEDA==:6ea18199041a1ea8
# rye:unsigned
tool_id: handle_inbound
tool_type: graph
executor_id: rye/core/runtimes/state-graph/runtime
version: "1.1.0"
description: "Inbound email processing — route, classify, respond"
category: rye/email

permissions:
  - "rye.execute.tool.rye.email.router"
  - "rye.execute.directive.rye.email.draft_response"
  - "rye.execute.tool.rye.email.send"
  - "rye.execute.tool.rye.email.forward"

config_schema:
  type: object
  required: [email_id, from_address, to_address, subject, body]
  properties:
    email_id: { type: string }
    from_address: { type: string }
    to_address: { type: string }
    subject: { type: string }
    body: { type: string }
    thread_id: { type: string }
    in_reply_to: { type: string }

config:
  start: route
  max_steps: 6

  nodes:
    route:
      action:
        primary: execute
        item_type: tool
        item_id: rye/email/router
        params:
          from_address: "${inputs.from_address}"
          to_address: "${inputs.to_address}"
          subject: "${inputs.subject}"
          body: "${inputs.body}"
          thread_id: "${inputs.thread_id}"
          in_reply_to: "${inputs.in_reply_to}"
      assign:
        action: "${result.action}"
        forward_to: "${result.forward_to}"
        agent_inbox: "${result.agent_inbox}"
      next:
        - to: draft_reply
          when: { path: "state.action", op: "eq", value: "auto_reply" }
        - to: forward_email
          when: { path: "state.action", op: "eq", value: "forward" }
        - to: done   # suppress — no action

    draft_reply:
      action:
        primary: execute
        item_type: directive
        item_id: rye/email/draft_response
        params:
          email_body: "${inputs.body}"
          email_subject: "${inputs.subject}"
          from_name: "${inputs.from_address}"
          thread_id: "${inputs.thread_id}"
      assign:
        draft_body: "${result.draft_body}"
        draft_subject: "${result.draft_subject}"
      on_error: forward_email
      next: send_reply

    send_reply:
      action:
        primary: execute
        item_type: tool
        item_id: rye/email/send
        params:
          to: "${inputs.from_address}"
          subject: "${state.draft_subject}"
          body: "${state.draft_body}"
      assign:
        sent_email_id: "${result.email_id}"
      next: done

    forward_email:
      action:
        primary: execute
        item_type: tool
        item_id: rye/email/forward
        params:
          email_id: "${inputs.email_id}"
          forward_to: "${state.forward_to}"
          classification: "unknown_sender"
      assign:
        forwarded_email_id: "${result.forwarded_email_id}"
      next: done

    done:
      type: return
