File size: 5,106 Bytes
6c50d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""Configuration loading utilities for AutoRestTest."""

import os
import re
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict

import tomli as tomllib
from dotenv import load_dotenv
from pydantic import BaseModel, ConfigDict

# Load environment variables from .env file for custom header interpolation
load_dotenv()


CONFIG_FILE_NAME = "configurations.toml"
CONFIG_DIR = Path(__file__).resolve().parent
AUTORESTTEST_DIR = CONFIG_DIR.parent
PROJECT_ROOT = AUTORESTTEST_DIR.parent.parent
CONFIG_PATH = PROJECT_ROOT / CONFIG_FILE_NAME


class SpecConfig(BaseModel):
    location: str
    recursion_limit: int = 50
    strict_validation: bool = True


class LLMConfig(BaseModel):
    engine: str
    creative_temperature: float
    strict_temperature: float
    api_base: str = "https://api.openai.com/v1"
    max_tokens: int = 20000


class HeaderAgentConfig(BaseModel):
    enabled: bool


class AgentsConfig(BaseModel):
    header: HeaderAgentConfig


class ValueAgentConfig(BaseModel):
    parallelize: bool = True
    max_workers: int = 4


class AgentCombinationConfig(BaseModel):
    max_combinations: int = 12
    max_total_combinations: int = 3000
    base_samples_per_size: int = 200
    combination_seed: int = 42
    value: ValueAgentConfig = ValueAgentConfig()


class CacheConfig(BaseModel):
    use_cached_graph: bool
    use_cached_table: bool


class QLearningConfig(BaseModel):
    learning_rate: float
    discount_factor: float
    max_exploration: float


class RequestGenerationConfig(BaseModel):
    time_duration: int
    mutation_rate: float


class ApiConfig(BaseModel):
    """API URL configuration. Override the spec URL with custom host/port."""
    override_url: bool = False
    host: str = "localhost"
    port: int = 8080


class CustomHeadersConfig(BaseModel):
    """Custom static headers. Supports ${VAR_NAME} env var interpolation."""

    model_config = ConfigDict(extra="allow")

    def get_headers(self) -> Dict[str, str]:
        headers = {}
        for key, value in (self.model_extra or {}).items():
            if isinstance(value, str):
                headers[key] = re.sub(
                    r"\$\{([^}]+)\}", lambda m: os.getenv(m.group(1), ""), value
                )
            else:
                headers[key] = str(value)
        return headers


class Config(BaseModel):
    spec: SpecConfig
    llm: LLMConfig
    agents: AgentsConfig
    agent: AgentCombinationConfig
    cache: CacheConfig
    q_learning: QLearningConfig
    request_generation: RequestGenerationConfig
    api: ApiConfig = ApiConfig()
    custom_headers: CustomHeadersConfig = CustomHeadersConfig()

    model_config = ConfigDict(frozen=True)

    @property
    def specification_location(self) -> str:
        return self.spec.location

    @property
    def recursion_limit(self) -> int:
        return self.spec.recursion_limit

    @property
    def strict_validation(self) -> bool:
        return self.spec.strict_validation

    @property
    def openai_llm_engine(self) -> str:
        return self.llm.engine

    @property
    def creative_temperature(self) -> float:
        return self.llm.creative_temperature

    @property
    def strict_temperature(self) -> float:
        return self.llm.strict_temperature

    @property
    def llm_api_base(self) -> str:
        """Return LLM API base URL."""
        return self.llm.api_base

    @property
    def llm_max_tokens(self) -> int:
        """Return LLM max tokens. -1 means omit from API call."""
        return self.llm.max_tokens

    @property
    def enable_header_agent(self) -> bool:
        return self.agents.header.enabled

    @property
    def max_combinations(self) -> int:
        return self.agent.max_combinations

    @property
    def max_total_combinations(self) -> int:
        return self.agent.max_total_combinations

    @property
    def base_samples_per_size(self) -> int:
        return self.agent.base_samples_per_size

    @property
    def combination_seed(self) -> int:
        return self.agent.combination_seed

    @property
    def parallelize_value_generation(self) -> bool:
        return self.agent.value.parallelize

    @property
    def value_generation_workers(self) -> int:
        return self.agent.value.max_workers

    @property
    def static_headers(self) -> Dict[str, str]:
        """Return custom headers with env var interpolation applied."""
        return self.custom_headers.get_headers()

    @property
    def custom_api_url(self) -> str:
        """Construct API URL from host and port."""
        return f"http://{self.api.host}:{self.api.port}/"


def _load_raw_config() -> Dict[str, Any]:
    if not CONFIG_PATH.exists():
        raise FileNotFoundError(f"Configuration file not found: {CONFIG_PATH}")
    with CONFIG_PATH.open("rb") as fh:
        return tomllib.load(fh)


@lru_cache(maxsize=1)
def get_config() -> Config:
    """Return the cached configuration values."""
    return Config.model_validate(_load_raw_config())


__all__ = [
    "Config",
    "CONFIG_PATH",
    "PROJECT_ROOT",
    "get_config",
]