<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:modeler="http://camunda.org/schema/modeler/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="@bpmnkit/core" exporterVersion="0.0.23" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.9.0">
  <bpmn:error id="Error_AGENT_FAILED" name="AGENT_FAILED" errorCode="AGENT_FAILED"/>
  <bpmn:process id="ai-agent-tool-loop" name="AI Agent with Tools" isExecutable="true">
    <bpmn:extensionElements>
      <zeebe:versionTag value="1.0.0"/>
    </bpmn:extensionElements>
    <bpmn:startEvent id="message-received" name="Customer message received">
      <bpmn:outgoing>Flow_message-received_support-agent</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:adHocSubProcess id="support-agent" name="Support agent">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="io.camunda.agenticai:aiagent-job-worker:1"/>
        <zeebe:ioMapping>
          <zeebe:input source="anthropic" target="provider.type"/>
          <zeebe:input source="{{secrets.ANTHROPIC_API_KEY}}" target="provider.anthropic.authentication.apiKey"/>
          <zeebe:input source="claude-sonnet-4-5" target="provider.anthropic.model.model"/>
          <zeebe:input source="You are a support agent for an online shop. Use the tools; never guess order data. Finish with JSON: {&quot;answer&quot;: string, &quot;resolved&quot;: boolean}." target="data.systemPrompt.prompt"/>
          <zeebe:input source="=customerMessage" target="data.userPrompt.prompt"/>
          <zeebe:input source="in-process" target="data.memory.storage.type"/>
          <zeebe:input source="12" target="data.limits.maxModelCalls"/>
          <zeebe:input source="json" target="data.response.format.type"/>
          <zeebe:input source="=true" target="data.response.format.parseJson"/>
          <zeebe:output source="=agent" target="agent"/>
        </zeebe:ioMapping>
        <zeebe:taskHeaders>
          <zeebe:header key="errorExpression" value="=if error != null then bpmnError(&quot;AGENT_FAILED&quot;, error.message) else null"/>
        </zeebe:taskHeaders>
        <zeebe:adHoc outputCollection="toolCallResults" outputElement="={&#10;  id: toolCall._meta.id,&#10;  name: toolCall._meta.name,&#10;  content: toolCallResult&#10;}"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_message-received_support-agent</bpmn:incoming>
      <bpmn:outgoing>Flow_support-agent_resolved-gw</bpmn:outgoing>
      <bpmn:serviceTask id="search-kb" name="Search knowledge base">
        <bpmn:documentation>Search the help centre for articles that answer a question.</bpmn:documentation>
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="kb-search"/>
          <zeebe:ioMapping>
            <zeebe:input source="=fromAi(toolCall.query, &quot;Search terms&quot;)" target="query"/>
            <zeebe:output source="=articles" target="toolCallResult"/>
          </zeebe:ioMapping>
        </bpmn:extensionElements>
      </bpmn:serviceTask>
      <bpmn:serviceTask id="lookup-order" name="Look up order">
        <bpmn:documentation>Get the status, items and tracking link of an order.</bpmn:documentation>
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="order-lookup"/>
          <zeebe:ioMapping>
            <zeebe:input source="=fromAi(toolCall.orderId, &quot;The order number&quot;)" target="orderId"/>
            <zeebe:output source="=order" target="toolCallResult"/>
          </zeebe:ioMapping>
        </bpmn:extensionElements>
      </bpmn:serviceTask>
      <bpmn:serviceTask id="create-ticket" name="Create ticket">
        <bpmn:documentation>Open a ticket for the warehouse team when an order needs manual action.</bpmn:documentation>
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="ticket-create"/>
          <zeebe:ioMapping>
            <zeebe:input source="=fromAi(toolCall.summary, &quot;One-line summary of the problem&quot;)" target="summary"/>
            <zeebe:output source="=ticketId" target="toolCallResult"/>
          </zeebe:ioMapping>
        </bpmn:extensionElements>
      </bpmn:serviceTask>
    </bpmn:adHocSubProcess>
    <bpmn:boundaryEvent id="agent-failed" name="Agent failed" attachedToRef="support-agent">
      <bpmn:outgoing>Flow_agent-failed_queue-for-human</bpmn:outgoing>
      <bpmn:errorEventDefinition errorRef="Error_AGENT_FAILED"/>
    </bpmn:boundaryEvent>
    <bpmn:serviceTask id="queue-for-human" name="Queue for a person">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="helpdesk-ticket-assign"/>
        <zeebe:taskHeaders>
          <zeebe:header key="queue" value="support"/>
        </zeebe:taskHeaders>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_agent-failed_queue-for-human</bpmn:incoming>
      <bpmn:outgoing>Flow_queue-for-human_queued</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="queued" name="Queued for a person">
      <bpmn:incoming>Flow_queue-for-human_queued</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:exclusiveGateway id="resolved-gw" name="Resolved?" default="Flow_resolved-gw_human-takeover">
      <bpmn:incoming>Flow_support-agent_resolved-gw</bpmn:incoming>
      <bpmn:outgoing>Flow_resolved-gw_send-answer</bpmn:outgoing>
      <bpmn:outgoing>Flow_resolved-gw_human-takeover</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:serviceTask id="send-answer" name="Send answer">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="email-send"/>
        <zeebe:ioMapping>
          <zeebe:input source="=agent.responseJson.answer" target="body"/>
        </zeebe:ioMapping>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_resolved-gw_send-answer</bpmn:incoming>
      <bpmn:outgoing>Flow_send-answer_answered</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="answered" name="Answered">
      <bpmn:incoming>Flow_send-answer_answered</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:userTask id="human-takeover" name="Take over conversation">
      <bpmn:extensionElements>
        <zeebe:userTask/>
        <zeebe:assignmentDefinition candidateGroups="support"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_resolved-gw_human-takeover</bpmn:incoming>
      <bpmn:outgoing>Flow_human-takeover_handed-over</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:endEvent id="handed-over" name="Handed to a person">
      <bpmn:incoming>Flow_human-takeover_handed-over</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_message-received_support-agent" sourceRef="message-received" targetRef="support-agent"/>
    <bpmn:sequenceFlow id="Flow_agent-failed_queue-for-human" sourceRef="agent-failed" targetRef="queue-for-human"/>
    <bpmn:sequenceFlow id="Flow_queue-for-human_queued" sourceRef="queue-for-human" targetRef="queued"/>
    <bpmn:sequenceFlow id="Flow_support-agent_resolved-gw" sourceRef="support-agent" targetRef="resolved-gw"/>
    <bpmn:sequenceFlow id="Flow_resolved-gw_send-answer" sourceRef="resolved-gw" targetRef="send-answer" name="answer">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">=agent.responseJson.resolved</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="Flow_send-answer_answered" sourceRef="send-answer" targetRef="answered"/>
    <bpmn:sequenceFlow id="Flow_resolved-gw_human-takeover" sourceRef="resolved-gw" targetRef="human-takeover" name="handoff"/>
    <bpmn:sequenceFlow id="Flow_human-takeover_handed-over" sourceRef="human-takeover" targetRef="handed-over"/>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="ai-agent-tool-loop_di">
    <bpmndi:BPMNPlane id="ai-agent-tool-loop_di_plane" bpmnElement="ai-agent-tool-loop">
      <bpmndi:BPMNShape id="message-received_di" bpmnElement="message-received">
        <dc:Bounds x="90" y="266" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="20" y="306" width="175" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="support-agent_di" bpmnElement="support-agent" isExpanded="true">
        <dc:Bounds x="226" y="20" width="200" height="528"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="agent-failed_di" bpmnElement="agent-failed">
        <dc:Bounds x="308" y="530" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="284" y="570" width="84" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="queue-for-human_di" bpmnElement="queue-for-human">
        <dc:Bounds x="526" y="788" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="queued_di" bpmnElement="queued">
        <dc:Bounds x="758" y="810" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="709" y="850" width="133" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="resolved-gw_di" bpmnElement="resolved-gw">
        <dc:Bounds x="551" y="259" width="50" height="50"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="544" y="313" width="63" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="send-answer_di" bpmnElement="send-answer">
        <dc:Bounds x="726" y="628" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="answered_di" bpmnElement="answered">
        <dc:Bounds x="926" y="650" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="916" y="690" width="56" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="human-takeover_di" bpmnElement="human-takeover">
        <dc:Bounds x="726" y="244" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="handed-over_di" bpmnElement="handed-over">
        <dc:Bounds x="926" y="266" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="881" y="306" width="126" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="search-kb_di" bpmnElement="search-kb">
        <dc:Bounds x="276" y="98" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="lookup-order_di" bpmnElement="lookup-order">
        <dc:Bounds x="276" y="258" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="create-ticket_di" bpmnElement="create-ticket">
        <dc:Bounds x="276" y="418" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="Flow_message-received_support-agent_di" bpmnElement="Flow_message-received_support-agent">
        <di:waypoint x="126" y="284"/>
        <di:waypoint x="226" y="284"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_agent-failed_queue-for-human_di" bpmnElement="Flow_agent-failed_queue-for-human">
        <di:waypoint x="326" y="566"/>
        <di:waypoint x="326" y="828"/>
        <di:waypoint x="526" y="828"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_queue-for-human_queued_di" bpmnElement="Flow_queue-for-human_queued">
        <di:waypoint x="626" y="828"/>
        <di:waypoint x="758" y="828"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_support-agent_resolved-gw_di" bpmnElement="Flow_support-agent_resolved-gw">
        <di:waypoint x="426" y="284"/>
        <di:waypoint x="551" y="284"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_resolved-gw_send-answer_di" bpmnElement="Flow_resolved-gw_send-answer">
        <di:waypoint x="576" y="309"/>
        <di:waypoint x="576" y="668"/>
        <di:waypoint x="726" y="668"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="552" y="482" width="42" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_send-answer_answered_di" bpmnElement="Flow_send-answer_answered">
        <di:waypoint x="826" y="668"/>
        <di:waypoint x="926" y="668"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_resolved-gw_human-takeover_di" bpmnElement="Flow_resolved-gw_human-takeover">
        <di:waypoint x="601" y="284"/>
        <di:waypoint x="726" y="284"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="639" y="260" width="49" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_human-takeover_handed-over_di" bpmnElement="Flow_human-takeover_handed-over">
        <di:waypoint x="826" y="284"/>
        <di:waypoint x="926" y="284"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

