-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathschemas.py
More file actions
277 lines (208 loc) · 8.59 KB
/
schemas.py
File metadata and controls
277 lines (208 loc) · 8.59 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""Language-agnostic schemas for AI service communication.
This module defines standardized payload schemas that work across all supported
languages (Python, JavaScript, TypeScript, and future languages).
Design principles:
1. General fields that apply to any language
2. Language-specific fields grouped in a nested object
3. Backward compatible with existing backend
4. Extensible for future languages without breaking changes
"""
from __future__ import annotations
from dataclasses import dataclass, field
from enum import Enum
from typing import Any
class ModuleSystem(str, Enum):
"""Module system used by the code."""
COMMONJS = "commonjs" # JavaScript/Node.js require/exports
ESM = "esm" # ES Modules import/export
PYTHON = "python" # Python import system
UNKNOWN = "unknown"
class TestFramework(str, Enum):
"""Supported test frameworks."""
# Python
PYTEST = "pytest"
UNITTEST = "unittest"
# JavaScript/TypeScript
JEST = "jest"
MOCHA = "mocha"
VITEST = "vitest"
@dataclass
class LanguageInfo:
"""Language-specific information.
General fields that describe the programming language and its environment.
This is designed to be extensible for future languages.
"""
# Core language identifier
name: str # "python", "javascript", "typescript", "rust", etc.
# Language version (format varies by language)
# - Python: "3.11.0"
# - JavaScript/TypeScript: "ES2022", "ES2023"
# - Rust: "1.70.0"
version: str | None = None
# Module system (primarily for JS/TS, but could apply to others)
module_system: ModuleSystem = ModuleSystem.UNKNOWN
# File extension (for generated files)
# - Python: ".py"
# - JavaScript: ".js", ".mjs", ".cjs"
# - TypeScript: ".ts", ".mts", ".cts"
file_extension: str = ""
# Type system info (for typed languages)
has_type_annotations: bool = False
type_checker: str | None = None # "mypy", "typescript", "pyright", etc.
@dataclass
class TestInfo:
"""Test-related information."""
# Test framework being used
framework: TestFramework
# Timeout for test execution (seconds)
timeout: int = 60
# Test file path patterns (for discovery)
test_patterns: list[str] = field(default_factory=list)
# Path to test files relative to project root
tests_root: str = "tests"
@dataclass
class OptimizeRequest:
"""Request payload for code optimization.
This schema is designed to be language-agnostic while supporting
language-specific fields through the `language_info` object.
"""
# === Core required fields ===
source_code: str # Code to optimize
trace_id: str # Unique identifier for this optimization run
# === Language information ===
language_info: LanguageInfo
# === Optional context ===
dependency_code: str = "" # Read-only context code
module_path: str = "" # Path to the module being optimized
# === Function metadata ===
is_async: bool = False # Whether function is async/await
is_numerical_code: bool | None = None # Whether code does numerical computation
# === Generation parameters ===
n_candidates: int = 5 # Number of optimization candidates
# === Metadata ===
codeflash_version: str = ""
experiment_metadata: dict[str, Any] | None = None
repo_owner: str | None = None
repo_name: str | None = None
current_username: str | None = None
# === React-specific ===
is_react_component: bool = False
react_context: str = ""
def to_payload(self) -> dict[str, Any]:
"""Convert to API payload dict, maintaining backward compatibility."""
payload = {
"source_code": self.source_code,
"trace_id": self.trace_id,
"language": self.language_info.name,
"dependency_code": self.dependency_code,
"is_async": self.is_async,
"n_candidates": self.n_candidates,
"codeflash_version": self.codeflash_version,
"experiment_metadata": self.experiment_metadata,
"repo_owner": self.repo_owner,
"repo_name": self.repo_name,
"current_username": self.current_username,
"is_numerical_code": self.is_numerical_code,
}
# Add language-specific fields
if self.language_info.version:
payload["language_version"] = self.language_info.version
# Backward compat: always include python_version
import platform
payload["python_version"] = platform.python_version()
# Module system for JS/TS
if self.language_info.module_system != ModuleSystem.UNKNOWN:
payload["module_system"] = self.language_info.module_system.value
# React-specific fields
if self.is_react_component:
payload["is_react_component"] = True
if self.react_context:
payload["react_context"] = self.react_context
return payload
@dataclass
class TestGenRequest:
"""Request payload for test generation.
This schema is designed to be language-agnostic while supporting
language-specific fields through the `language_info` and `test_info` objects.
"""
# === Core required fields ===
source_code: str # Code being tested
function_name: str # Name of function to generate tests for
trace_id: str # Unique identifier
# === Language information ===
language_info: LanguageInfo
# === Test information ===
test_info: TestInfo
# === Path information ===
module_path: str = "" # Path to source module
test_module_path: str = "" # Path for generated test
# === Function metadata ===
helper_function_names: list[str] = field(default_factory=list)
is_async: bool = False
is_numerical_code: bool | None = None
# === Generation parameters ===
test_index: int = 0 # Index when generating multiple tests
# === Metadata ===
codeflash_version: str = ""
# === React-specific ===
is_react_component: bool = False
def to_payload(self) -> dict[str, Any]:
"""Convert to API payload dict, maintaining backward compatibility."""
payload = {
"source_code_being_tested": self.source_code,
"function_to_optimize": {"function_name": self.function_name, "is_async": self.is_async},
"helper_function_names": self.helper_function_names,
"module_path": self.module_path,
"test_module_path": self.test_module_path,
"test_framework": self.test_info.framework.value,
"test_timeout": self.test_info.timeout,
"trace_id": self.trace_id,
"test_index": self.test_index,
"language": self.language_info.name,
"codeflash_version": self.codeflash_version,
"is_async": self.is_async,
"is_numerical_code": self.is_numerical_code,
}
# Add language version
if self.language_info.version:
payload["language_version"] = self.language_info.version
# Backward compat: always include python_version
import platform
payload["python_version"] = platform.python_version()
# Module system for JS/TS
if self.language_info.module_system != ModuleSystem.UNKNOWN:
payload["module_system"] = self.language_info.module_system.value
# React-specific fields
if self.is_react_component:
payload["is_react_component"] = True
return payload
# === Helper functions to create language info ===
def python_language_info(version: str | None = None) -> LanguageInfo:
"""Create LanguageInfo for Python."""
import platform
return LanguageInfo(
name="python",
version=version or platform.python_version(),
module_system=ModuleSystem.PYTHON,
file_extension=".py",
has_type_annotations=True,
type_checker="mypy",
)
def javascript_language_info(
module_system: ModuleSystem = ModuleSystem.COMMONJS, version: str = "ES2022"
) -> LanguageInfo:
"""Create LanguageInfo for JavaScript."""
ext = ".mjs" if module_system == ModuleSystem.ESM else ".js"
return LanguageInfo(
name="javascript", version=version, module_system=module_system, file_extension=ext, has_type_annotations=False
)
def typescript_language_info(module_system: ModuleSystem = ModuleSystem.ESM, version: str = "ES2022") -> LanguageInfo:
"""Create LanguageInfo for TypeScript."""
return LanguageInfo(
name="typescript",
version=version,
module_system=module_system,
file_extension=".ts",
has_type_annotations=True,
type_checker="typescript",
)