package OpenClawArchitecture {
    port def HttpPort {
        doc /* Carries request and response traffic over HTTP or HTTPS. */
        direction = both;
    }

    port def WebSocketPort {
        doc /* Carries bidirectional gateway session traffic over WebSocket. */
        direction = both;
    }

    port def MetricsPort {
        doc /* Carries metrics scrape traffic from an observability collector. */
        direction = both;
    }

    part def OpenClawGatewayDef {
        doc /* Hosts the OpenClaw gateway runtime, control interface, provider integrations, and channel ingress endpoints. */
        attribute env: String = "production";
        attribute kind: String = "internal";
        attribute domain: String = "application";
        attribute stable: Boolean = true;
        attribute deployment: String = "kustomize, docker-compose, fly.io";
        attribute image: String = "ghcr.io/openclaw/openclaw:slim";
        attribute version: String = "2026.7.2";
        attribute namespace: String = "openclaw";
        attribute replicas: Integer = 1;
        attribute gatewayPort: Integer = 18789;
        attribute bridgePort: Integer = 18790;
        attribute msteamsPort: Integer = 3978;
        attribute bindMode: String = "loopback in Kubernetes, lan in Compose and Fly";
        attribute user: String = "node";
        attribute healthPath: String = "/healthz";
        attribute readinessPath: String = "/readyz";

        port gateway: HttpPort {
            doc /* Accepts authenticated Control UI and gateway HTTP requests on port 18789. */
            direction = in;
        }
        port websocket: WebSocketPort {
            doc /* Maintains bidirectional gateway sessions for interactive clients and CLI access. */
            direction = both;
        }
        port bridge: HttpPort {
            doc /* Accepts bridge integration traffic on port 18790. */
            direction = in;
        }
        port msteams: HttpPort {
            doc /* Accepts Microsoft Teams integration traffic on port 3978. */
            direction = in;
        }
        port metrics: MetricsPort {
            doc /* Exposes authenticated Prometheus-compatible metrics through the gateway. */
            direction = in;
        }
        port llm: HttpPort {
            doc /* Sends model inference requests to configured hosted language-model providers. */
            direction = out;
        }
        port localModel: HttpPort {
            doc /* Sends model requests to host-local Ollama or LM Studio services. */
            direction = out;
        }
        port otlp: HttpPort {
            doc /* Exports OpenTelemetry traces, metrics, and logs using OTLP over HTTP. */
            direction = out;
        }

        part k8sHome {
            doc /* Persists Kubernetes gateway state, configuration, agent workspace, and runtime data. */
            attribute kind: String = "owned";
            attribute domain: String = "storage";
            attribute type: String = "pvc";
            attribute name: String = "openclaw-home-pvc";
            attribute size: String = "10Gi";
            attribute accessMode: String = "ReadWriteOnce";
            attribute mountPath: String = "/home/node/.openclaw";
        }

        part flyData {
            doc /* Persists gateway state for the private Fly.io deployment. */
            attribute kind: String = "owned";
            attribute domain: String = "storage";
            attribute type: String = "volume";
            attribute name: String = "openclaw_data";
            attribute mountPath: String = "/data";
        }

        part tmpVolume {
            doc /* Provides writable temporary filesystem space while the gateway root filesystem remains read-only. */
            attribute kind: String = "owned";
            attribute domain: String = "storage";
            attribute type: String = "emptyDir";
            attribute mountPath: String = "/tmp";
        }

        part config {
            doc /* Supplies gateway configuration and agent instructions to the Kubernetes pod. */
            attribute kind: String = "owned";
            attribute domain: String = "application";
            attribute type: String = "configmap";
            attribute name: String = "openclaw-config";
            attribute files: String = "openclaw.json, AGENTS.md";
        }

        part secrets {
            doc /* Supplies gateway authentication and provider credentials through Kubernetes secret references. */
            attribute kind: String = "owned";
            attribute domain: String = "identity";
            attribute type: String = "secret";
            attribute name: String = "openclaw-secrets";
            attribute keys: String = "OPENCLAW_GATEWAY_TOKEN, ANTHROPIC_API_KEY, OPENAI_API_KEY, GEMINI_API_KEY, OPENROUTER_API_KEY";
        }

        part initConfig {
            doc /* Copies ConfigMap content into the persistent OpenClaw home before gateway startup. */
            attribute kind: String = "owned";
            attribute domain: String = "application";
            attribute type: String = "init-container";
            attribute image: String = "busybox:1.37";
            attribute name: String = "init-config";
        }

        part service {
            doc /* Provides the Kubernetes ClusterIP network identity for gateway port 18789. */
            attribute kind: String = "owned";
            attribute domain: String = "ingress";
            attribute type: String = "ClusterIP";
            attribute name: String = "openclaw";
            attribute fqdn: String = "openclaw.openclaw.svc.cluster.local";
            attribute port: Integer = 18789;
            attribute targetPort: Integer = 18789;
        }
    }

    part def OpenClawCliDef {
        doc /* Provides the interactive OpenClaw command-line runtime attached to the gateway network namespace. */
        attribute env: String = "production";
        attribute kind: String = "internal";
        attribute domain: String = "application";
        attribute stable: Boolean = true;
        attribute deployment: String = "docker-compose";
        attribute image: String = "openclaw:local";
        attribute networkMode: String = "service:openclaw-gateway";
        attribute entrypoint: String = "node dist/index.js";

        port gateway: WebSocketPort {
            doc /* Opens interactive gateway sessions through the shared gateway network namespace. */
            direction = out;
        }
    }

    part def EksDef {
        doc /* Provides the AWS-managed Kubernetes control plane and cluster runtime for the Kubernetes deployment. */
        attribute kind: String = "third-party";
        attribute domain: String = "cloud";
        attribute stable: Boolean = true;
        attribute type: String = "EKS";
        attribute platform: String = "AWS";
    }

    part def OpenAIProviderDef {
        doc /* Provides hosted OpenAI language-model inference over the OpenAI API. */
        attribute kind: String = "third-party";
        attribute domain: String = "external";
        attribute stable: Boolean = true;
        attribute fqdn: String = "api.openai.com";
        attribute authentication: String = "OPENAI_API_KEY";

        port api: HttpPort {
            doc /* Accepts OpenClaw model requests and returns OpenAI inference responses. */
            direction = in;
        }
    }

    part def AnthropicProviderDef {
        doc /* Provides hosted Anthropic language-model inference over the Anthropic API. */
        attribute kind: String = "third-party";
        attribute domain: String = "external";
        attribute stable: Boolean = true;
        attribute fqdn: String = "api.anthropic.com";
        attribute authentication: String = "ANTHROPIC_API_KEY";

        port api: HttpPort {
            doc /* Accepts OpenClaw model requests and returns Anthropic inference responses. */
            direction = in;
        }
    }

    part def GeminiProviderDef {
        doc /* Provides hosted Google Gemini language-model inference over the Google Generative Language API. */
        attribute kind: String = "third-party";
        attribute domain: String = "external";
        attribute stable: Boolean = true;
        attribute fqdn: String = "generativelanguage.googleapis.com";
        attribute authentication: String = "GEMINI_API_KEY";

        port api: HttpPort {
            doc /* Accepts OpenClaw model requests and returns Gemini inference responses. */
            direction = in;
        }
    }

    part def OpenRouterProviderDef {
        doc /* Provides routed hosted language-model inference through OpenRouter. */
        attribute kind: String = "third-party";
        attribute domain: String = "external";
        attribute stable: Boolean = true;
        attribute fqdn: String = "openrouter.ai";
        attribute endpoint: String = "/api/v1";
        attribute authentication: String = "OPENROUTER_API_KEY";

        port api: HttpPort {
            doc /* Accepts OpenClaw model requests and returns OpenRouter inference responses. */
            direction = in;
        }
    }

    part def LocalModelServiceDef {
        doc /* Provides an operator-hosted local language model reachable from Docker containers. */
        attribute kind: String = "external";
        attribute domain: String = "application";
        attribute stable: Boolean = true;
        attribute fqdn: String = "host.docker.internal";
        attribute supportedServices: String = "Ollama, LM Studio";

        port api: HttpPort {
            doc /* Accepts local-model inference requests from the gateway. */
            direction = in;
        }
    }

    part def OpenTelemetryCollectorDef {
        doc /* Receives OpenTelemetry signal exports from the gateway through a configured OTLP endpoint. */
        attribute kind: String = "external";
        attribute domain: String = "operations";
        attribute stable: Boolean = true;
        attribute protocol: String = "OTLP/HTTP";
        attribute endpointConfiguration: String = "OTEL_EXPORTER_OTLP_ENDPOINT and signal-specific endpoint variables";

        port otlp: HttpPort {
            doc /* Accepts OTLP trace, metric, and log exports from OpenClaw. */
            direction = in;
        }
    }

    part def PrometheusDef {
        doc /* Collects Prometheus-compatible metrics exposed by the OpenClaw gateway. */
        attribute kind: String = "external";
        attribute domain: String = "operations";
        attribute stable: Boolean = true;
        attribute type: String = "prometheus";

        port scrape: MetricsPort {
            doc /* Initiates metrics scrapes against the gateway metrics endpoint. */
            direction = out;
        }
    }

    part def OperatorBrowserDef {
        doc /* Provides the operator web client for accessing the authenticated OpenClaw Control UI. */
        attribute kind: String = "third-party";
        attribute domain: String = "ingress";
        attribute stable: Boolean = true;
        attribute endpoint: String = "http://localhost:18789";

        port gateway: HttpPort {
            doc /* Sends browser HTTP requests to the OpenClaw gateway service. */
            direction = out;
        }
    }

    part MonitoredSuite {
        doc /* Represents the deployed OpenClaw runtime and its platform, provider, ingress, storage, and observability relationships. */

        part openclawGateway : OpenClawGatewayDef;
        part openclawCli : OpenClawCliDef;
        part eks : EksDef;
        part openai : OpenAIProviderDef;
        part anthropic : AnthropicProviderDef;
        part gemini : GeminiProviderDef;
        part openrouter : OpenRouterProviderDef;
        part localModel : LocalModelServiceDef;
        part otelCollector : OpenTelemetryCollectorDef;
        part prometheus : PrometheusDef;
        part operatorBrowser : OperatorBrowserDef;

        connect openclawCli.gateway to openclawGateway.websocket;
        connect operatorBrowser.gateway to openclawGateway.gateway;
        connect openclawGateway.llm to openai.api;
        connect openclawGateway.llm to anthropic.api;
        connect openclawGateway.llm to gemini.api;
        connect openclawGateway.llm to openrouter.api;
        connect openclawGateway.localModel to localModel.api;
        connect openclawGateway.otlp to otelCollector.otlp;
        connect prometheus.scrape to openclawGateway.metrics;
    }

    view def ArchitectureView {
        doc /* Exposes the complete OpenClaw deployment architecture and its external relationships. */
        expose MonitoredSuite;
    }

    view architecture : ArchitectureView {
        doc /* Presents the monitored OpenClaw architecture for structural review. */
        expose MonitoredSuite;
    }
}