<?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-orchestrator-workers" name="AI Orchestrator–Workers" isExecutable="true">
    <bpmn:extensionElements>
      <zeebe:versionTag value="1.0.0"/>
    </bpmn:extensionElements>
    <bpmn:startEvent id="question-received" name="Research question received">
      <bpmn:outgoing>Flow_question-received_orchestrator</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:adHocSubProcess id="orchestrator" name="Orchestrator">
      <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 lead a research team. Break the question into subtasks, delegate each to the best worker, and write the final report from their results. Finish with JSON: {&quot;report&quot;: string, &quot;confidence&quot;: number between 0 and 1}." target="data.systemPrompt.prompt"/>
          <zeebe:input source="=question" target="data.userPrompt.prompt"/>
          <zeebe:input source="in-process" target="data.memory.storage.type"/>
          <zeebe:input source="20" 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_question-received_orchestrator</bpmn:incoming>
      <bpmn:outgoing>Flow_orchestrator_confident</bpmn:outgoing>
      <bpmn:serviceTask id="research-web" zeebe:modelerTemplate="io.camunda.connectors.agenticai.aiagent.v1" zeebe:modelerTemplateVersion="1" name="Research the web">
        <bpmn:documentation>Search the web and return the most relevant findings with their sources.</bpmn:documentation>
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="io.camunda.agenticai:aiagent: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 research topics on the web and cite sources." target="data.systemPrompt.prompt"/>
            <zeebe:input source="=fromAi(toolCall.instructions, &quot;What to research&quot;)" target="data.userPrompt.prompt"/>
            <zeebe:input source="in-process" target="data.memory.storage.type"/>
            <zeebe:input source="5" target="data.limits.maxModelCalls"/>
            <zeebe:input source="text" target="data.response.format.type"/>
            <zeebe:output source="=agent.responseText" target="toolCallResult"/>
          </zeebe:ioMapping>
          <zeebe:taskHeaders>
            <zeebe:header key="elementTemplateVersion" value="1"/>
            <zeebe:header key="elementTemplateId" value="io.camunda.connectors.agenticai.aiagent.v1"/>
            <zeebe:header key="resultVariable" value="agent"/>
          </zeebe:taskHeaders>
        </bpmn:extensionElements>
      </bpmn:serviceTask>
      <bpmn:serviceTask id="summarise-source" zeebe:modelerTemplate="io.camunda.connectors.agenticai.aiagent.v1" zeebe:modelerTemplateVersion="1" name="Summarise source">
        <bpmn:documentation>Summarise a long source document into key points.</bpmn:documentation>
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="io.camunda.agenticai:aiagent: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 summarise documents into short, factual key points." target="data.systemPrompt.prompt"/>
            <zeebe:input source="=fromAi(toolCall.document, &quot;The text to summarise&quot;)" target="data.userPrompt.prompt"/>
            <zeebe:input source="in-process" target="data.memory.storage.type"/>
            <zeebe:input source="5" target="data.limits.maxModelCalls"/>
            <zeebe:input source="text" target="data.response.format.type"/>
            <zeebe:output source="=agent.responseText" target="toolCallResult"/>
          </zeebe:ioMapping>
          <zeebe:taskHeaders>
            <zeebe:header key="elementTemplateVersion" value="1"/>
            <zeebe:header key="elementTemplateId" value="io.camunda.connectors.agenticai.aiagent.v1"/>
            <zeebe:header key="resultVariable" value="agent"/>
          </zeebe:taskHeaders>
        </bpmn:extensionElements>
      </bpmn:serviceTask>
      <bpmn:serviceTask id="analyse-data" zeebe:modelerTemplate="io.camunda.connectors.agenticai.aiagent.v1" zeebe:modelerTemplateVersion="1" name="Analyse data">
        <bpmn:documentation>Analyse a table of numbers and describe the trend.</bpmn:documentation>
        <bpmn:extensionElements>
          <zeebe:taskDefinition type="io.camunda.agenticai:aiagent: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 data analyst. Describe trends and outliers." target="data.systemPrompt.prompt"/>
            <zeebe:input source="=fromAi(toolCall.data, &quot;The data as CSV&quot;)" target="data.userPrompt.prompt"/>
            <zeebe:input source="in-process" target="data.memory.storage.type"/>
            <zeebe:input source="5" target="data.limits.maxModelCalls"/>
            <zeebe:input source="text" target="data.response.format.type"/>
            <zeebe:output source="=agent.responseText" target="toolCallResult"/>
          </zeebe:ioMapping>
          <zeebe:taskHeaders>
            <zeebe:header key="elementTemplateVersion" value="1"/>
            <zeebe:header key="elementTemplateId" value="io.camunda.connectors.agenticai.aiagent.v1"/>
            <zeebe:header key="resultVariable" value="agent"/>
          </zeebe:taskHeaders>
        </bpmn:extensionElements>
      </bpmn:serviceTask>
    </bpmn:adHocSubProcess>
    <bpmn:boundaryEvent id="orchestrator-failed" name="Agent failed" attachedToRef="orchestrator">
      <bpmn:outgoing>Flow_orchestrator-failed_notify-research-failed</bpmn:outgoing>
      <bpmn:errorEventDefinition errorRef="Error_AGENT_FAILED"/>
    </bpmn:boundaryEvent>
    <bpmn:serviceTask id="notify-research-failed" name="Notify requester: no report">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="requester-notify"/>
        <zeebe:taskHeaders>
          <zeebe:header key="template" value="research-failed"/>
        </zeebe:taskHeaders>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_orchestrator-failed_notify-research-failed</bpmn:incoming>
      <bpmn:outgoing>Flow_notify-research-failed_research-failed</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="research-failed" name="No report">
      <bpmn:incoming>Flow_notify-research-failed_research-failed</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:exclusiveGateway id="confident" name="Confident?" default="Flow_confident_expert-review">
      <bpmn:incoming>Flow_orchestrator_confident</bpmn:incoming>
      <bpmn:outgoing>Flow_confident_confident_join</bpmn:outgoing>
      <bpmn:outgoing>Flow_confident_expert-review</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:userTask id="expert-review" name="Expert review">
      <bpmn:extensionElements>
        <zeebe:userTask/>
        <zeebe:assignmentDefinition candidateGroups="analysts"/>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_confident_expert-review</bpmn:incoming>
      <bpmn:outgoing>Flow_expert-review_confident_join</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:serviceTask id="publish-report" name="Publish report">
      <bpmn:extensionElements>
        <zeebe:taskDefinition type="wiki-page-publish"/>
        <zeebe:ioMapping>
          <zeebe:input source="=agent.responseJson.report" target="pageBody"/>
          <zeebe:output source="=pageUrl" target="reportUrl"/>
        </zeebe:ioMapping>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_confident_join_publish-report</bpmn:incoming>
      <bpmn:outgoing>Flow_publish-report_report-published</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="report-published" name="Report published">
      <bpmn:incoming>Flow_publish-report_report-published</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:exclusiveGateway id="confident_join">
      <bpmn:incoming>Flow_confident_confident_join</bpmn:incoming>
      <bpmn:incoming>Flow_expert-review_confident_join</bpmn:incoming>
      <bpmn:outgoing>Flow_confident_join_publish-report</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:sequenceFlow id="Flow_question-received_orchestrator" sourceRef="question-received" targetRef="orchestrator"/>
    <bpmn:sequenceFlow id="Flow_orchestrator-failed_notify-research-failed" sourceRef="orchestrator-failed" targetRef="notify-research-failed"/>
    <bpmn:sequenceFlow id="Flow_notify-research-failed_research-failed" sourceRef="notify-research-failed" targetRef="research-failed"/>
    <bpmn:sequenceFlow id="Flow_orchestrator_confident" sourceRef="orchestrator" targetRef="confident"/>
    <bpmn:sequenceFlow id="Flow_confident_confident_join" sourceRef="confident" targetRef="confident_join" name="publish">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">=agent.responseJson.confidence &gt;= 0.7</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="Flow_confident_expert-review" sourceRef="confident" targetRef="expert-review" name="review"/>
    <bpmn:sequenceFlow id="Flow_expert-review_confident_join" sourceRef="expert-review" targetRef="confident_join"/>
    <bpmn:sequenceFlow id="Flow_publish-report_report-published" sourceRef="publish-report" targetRef="report-published"/>
    <bpmn:sequenceFlow id="Flow_confident_join_publish-report" sourceRef="confident_join" targetRef="publish-report"/>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="ai-orchestrator-workers_di">
    <bpmndi:BPMNPlane id="ai-orchestrator-workers_di_plane" bpmnElement="ai-orchestrator-workers">
      <bpmndi:BPMNShape id="question-received_di" bpmnElement="question-received">
        <dc:Bounds x="93" y="266" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="20" y="306" width="182" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orchestrator_di" bpmnElement="orchestrator" isExpanded="true">
        <dc:Bounds x="229" y="20" width="200" height="528"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="orchestrator-failed_di" bpmnElement="orchestrator-failed">
        <dc:Bounds x="311" y="530" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="287" y="570" width="84" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="notify-research-failed_di" bpmnElement="notify-research-failed">
        <dc:Bounds x="529" y="628" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="research-failed_di" bpmnElement="research-failed">
        <dc:Bounds x="761" y="650" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="748" y="690" width="63" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confident_di" bpmnElement="confident">
        <dc:Bounds x="554" y="259" width="50" height="50"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="544" y="313" width="70" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="expert-review_di" bpmnElement="expert-review">
        <dc:Bounds x="729" y="244" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="publish-report_di" bpmnElement="publish-report">
        <dc:Bounds x="1079" y="244" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="report-published_di" bpmnElement="report-published">
        <dc:Bounds x="1279" y="266" width="36" height="36"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="1241" y="306" width="112" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="confident_join_di" bpmnElement="confident_join">
        <dc:Bounds x="929" y="259" width="50" height="50"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="research-web_di" bpmnElement="research-web">
        <dc:Bounds x="279" y="98" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="summarise-source_di" bpmnElement="summarise-source">
        <dc:Bounds x="279" y="258" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="analyse-data_di" bpmnElement="analyse-data">
        <dc:Bounds x="279" y="418" width="100" height="80"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="Flow_question-received_orchestrator_di" bpmnElement="Flow_question-received_orchestrator">
        <di:waypoint x="129" y="284"/>
        <di:waypoint x="229" y="284"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_orchestrator-failed_notify-research-failed_di" bpmnElement="Flow_orchestrator-failed_notify-research-failed">
        <di:waypoint x="329" y="566"/>
        <di:waypoint x="329" y="668"/>
        <di:waypoint x="529" y="668"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_notify-research-failed_research-failed_di" bpmnElement="Flow_notify-research-failed_research-failed">
        <di:waypoint x="629" y="668"/>
        <di:waypoint x="761" y="668"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_orchestrator_confident_di" bpmnElement="Flow_orchestrator_confident">
        <di:waypoint x="429" y="284"/>
        <di:waypoint x="554" y="284"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_confident_confident_join_di" bpmnElement="Flow_confident_confident_join">
        <di:waypoint x="579" y="259"/>
        <di:waypoint x="579" y="204"/>
        <di:waypoint x="954" y="204"/>
        <di:waypoint x="954" y="259"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="742" y="180" width="49" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_confident_expert-review_di" bpmnElement="Flow_confident_expert-review">
        <di:waypoint x="604" y="284"/>
        <di:waypoint x="729" y="284"/>
        <bpmndi:BPMNLabel>
          <dc:Bounds x="646" y="260" width="42" height="14"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_expert-review_confident_join_di" bpmnElement="Flow_expert-review_confident_join">
        <di:waypoint x="829" y="284"/>
        <di:waypoint x="929" y="284"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_publish-report_report-published_di" bpmnElement="Flow_publish-report_report-published">
        <di:waypoint x="1179" y="284"/>
        <di:waypoint x="1279" y="284"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_confident_join_publish-report_di" bpmnElement="Flow_confident_join_publish-report">
        <di:waypoint x="979" y="284"/>
        <di:waypoint x="1079" y="284"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

