-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathpatch_generated_score_compat.py
More file actions
280 lines (250 loc) 路 9.27 KB
/
Copy pathpatch_generated_score_compat.py
File metadata and controls
280 lines (250 loc) 路 9.27 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
278
279
280
#!/usr/bin/env python3
"""Preserve the legacy score-create API after Fern moves it to ``scores``.
Fern owns ``langfuse/api`` and regenerates it from the main Langfuse repository.
This postprocessor is intentionally narrow and fail-closed: it only patches the
known generated shape where ``scores.create`` exists and
``legacy.score_v1.create`` no longer does.
"""
from __future__ import annotations
import argparse
import ast
from pathlib import Path
CLIENT_IMPORTS = """\
from ...commons.types.create_score_value import CreateScoreValue
from ...commons.types.score_data_type import ScoreDataType
from ...scores.client import (
AsyncScoresClient as CanonicalAsyncScoresClient,
ScoresClient as CanonicalScoresClient,
OMIT,
)
from ...scores.types.create_score_response import CreateScoreResponse
from ...scores.types.create_score_source import CreateScoreSource
"""
SYNC_CREATE_METHOD = '''\
def create(
self,
*,
name: str,
value: CreateScoreValue,
id: typing.Optional[str] = OMIT,
trace_id: typing.Optional[str] = OMIT,
session_id: typing.Optional[str] = OMIT,
observation_id: typing.Optional[str] = OMIT,
dataset_run_id: typing.Optional[str] = OMIT,
comment: typing.Optional[str] = OMIT,
metadata: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
environment: typing.Optional[str] = OMIT,
queue_id: typing.Optional[str] = OMIT,
data_type: typing.Optional[ScoreDataType] = OMIT,
config_id: typing.Optional[str] = OMIT,
source: typing.Optional[CreateScoreSource] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> CreateScoreResponse:
"""**Deprecated compatibility alias.** Use ``client.scores.create``."""
return CanonicalScoresClient(
client_wrapper=self._raw_client._client_wrapper
).create(
name=name,
value=value,
id=id,
trace_id=trace_id,
session_id=session_id,
observation_id=observation_id,
dataset_run_id=dataset_run_id,
comment=comment,
metadata=metadata,
environment=environment,
queue_id=queue_id,
data_type=data_type,
config_id=config_id,
source=source,
request_options=request_options,
)
'''
ASYNC_CREATE_METHOD = '''\
async def create(
self,
*,
name: str,
value: CreateScoreValue,
id: typing.Optional[str] = OMIT,
trace_id: typing.Optional[str] = OMIT,
session_id: typing.Optional[str] = OMIT,
observation_id: typing.Optional[str] = OMIT,
dataset_run_id: typing.Optional[str] = OMIT,
comment: typing.Optional[str] = OMIT,
metadata: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
environment: typing.Optional[str] = OMIT,
queue_id: typing.Optional[str] = OMIT,
data_type: typing.Optional[ScoreDataType] = OMIT,
config_id: typing.Optional[str] = OMIT,
source: typing.Optional[CreateScoreSource] = OMIT,
request_options: typing.Optional[RequestOptions] = None,
) -> CreateScoreResponse:
"""**Deprecated compatibility alias.** Use ``client.scores.create``."""
return await CanonicalAsyncScoresClient(
client_wrapper=self._raw_client._client_wrapper
).create(
name=name,
value=value,
id=id,
trace_id=trace_id,
session_id=session_id,
observation_id=observation_id,
dataset_run_id=dataset_run_id,
comment=comment,
metadata=metadata,
environment=environment,
queue_id=queue_id,
data_type=data_type,
config_id=config_id,
source=source,
request_options=request_options,
)
'''
TYPE_NAMES = (
"CreateScoreRequest",
"CreateScoreResponse",
"CreateScoreSource",
)
LEGACY_ALIAS_MARKER = (
"**Deprecated compatibility alias.** Use ``client.scores.create``."
)
def _replace_once(contents: str, marker: str, replacement: str, *, path: Path) -> str:
count = contents.count(marker)
if count != 1:
raise RuntimeError(
f"Expected exactly one compatibility marker in {path}: {marker!r}; "
f"found {count}"
)
return contents.replace(marker, replacement, 1)
def _append_type_exports(path: Path, imports_by_type: dict[str, str]) -> None:
exports_marker = "# Score-create compatibility aliases (LFE-10397)."
contents = path.read_text()
if exports_marker in contents:
return
imports = "\n".join(
f"from {module_name} import {type_name}"
for type_name, module_name in imports_by_type.items()
)
names = ", ".join(repr(type_name) for type_name in TYPE_NAMES)
defines_all = any(
(
isinstance(node, ast.Assign)
and any(
isinstance(target, ast.Name) and target.id == "__all__"
for target in node.targets
)
)
or (
isinstance(node, ast.AnnAssign)
and isinstance(node.target, ast.Name)
and node.target.id == "__all__"
)
for node in ast.parse(contents).body
)
exports = (
f"__all__ = [*__all__, {names}]" if defines_all else f"__all__ = [{names}]"
)
path.write_text(f"{contents.rstrip()}\n\n{exports_marker}\n{imports}\n{exports}\n")
def _create_type_aliases(types_dir: Path) -> None:
types_dir.mkdir(parents=True, exist_ok=True)
for type_name in TYPE_NAMES:
module_name = "".join(
f"_{character.lower()}" if character.isupper() else character
for character in type_name
).lstrip("_")
alias_path = types_dir / f"{module_name}.py"
alias_path.write_text(
"# This compatibility alias is applied after Fern generation.\n\n"
f"from ....scores.types.{module_name} import {type_name}\n\n"
f'__all__ = ["{type_name}"]\n'
)
def patch_generated_api(api_root: Path) -> bool:
canonical_client_path = api_root / "scores" / "client.py"
legacy_client_path = api_root / "legacy" / "score_v1" / "client.py"
canonical_client = canonical_client_path.read_text()
legacy_client = legacy_client_path.read_text()
canonical_has_create = (
"\n def create(" in canonical_client
and "\n async def create(" in canonical_client
)
legacy_has_create = (
"\n def create(" in legacy_client
and "\n async def create(" in legacy_client
)
if legacy_has_create:
if LEGACY_ALIAS_MARKER in legacy_client and canonical_has_create:
return False
if canonical_has_create:
raise RuntimeError(
"Both canonical and legacy generated score clients define create; "
"refusing to introduce a third compatibility surface"
)
return False
if not canonical_has_create:
raise RuntimeError(
"Neither generated score client defines both sync and async create methods"
)
legacy_client = _replace_once(
legacy_client,
"from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper\n",
"from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper\n"
f"{CLIENT_IMPORTS}",
path=legacy_client_path,
)
legacy_client = _replace_once(
legacy_client,
"\n def delete(",
f"\n{SYNC_CREATE_METHOD} def delete(",
path=legacy_client_path,
)
legacy_client = _replace_once(
legacy_client,
"\n async def delete(",
f"\n{ASYNC_CREATE_METHOD} async def delete(",
path=legacy_client_path,
)
legacy_client_path.write_text(legacy_client)
legacy_package_path = api_root / "legacy" / "score_v1" / "__init__.py"
legacy_parent_package_path = api_root / "legacy" / "__init__.py"
legacy_types_path = api_root / "legacy" / "score_v1" / "types" / "__init__.py"
_create_type_aliases(legacy_types_path.parent)
if not legacy_types_path.exists():
legacy_types_path.write_text(
"# This package is generated by Fern and extended for compatibility.\n\n"
"__all__: list[str] = []\n"
)
_append_type_exports(
legacy_types_path,
{
"CreateScoreRequest": ".create_score_request",
"CreateScoreResponse": ".create_score_response",
"CreateScoreSource": ".create_score_source",
},
)
_append_type_exports(
legacy_package_path, {type_name: ".types" for type_name in TYPE_NAMES}
)
_append_type_exports(
legacy_parent_package_path,
{type_name: ".score_v1" for type_name in TYPE_NAMES},
)
return True
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--api-root",
type=Path,
default=Path("langfuse/api"),
help="Path to the freshly generated Fern Python package",
)
args = parser.parse_args()
changed = patch_generated_api(args.api_root)
print(
"Patched legacy score create compatibility alias."
if changed
else "No patch needed."
)
if __name__ == "__main__":
main()