buddy-agent-auth 0.1.0.dev1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. buddy_agent_auth/__init__.py +5 -0
  2. buddy_agent_auth/account_store.py +4 -0
  3. buddy_agent_auth/backend_profile.py +4 -0
  4. buddy_agent_auth/client.py +4 -0
  5. buddy_agent_auth/locking.py +81 -0
  6. buddy_agent_auth/providers/__init__.py +4 -0
  7. buddy_agent_auth/providers/base.py +9 -0
  8. buddy_agent_auth/providers/codebuddy/__init__.py +17 -0
  9. buddy_agent_auth/providers/codebuddy/account_store.py +506 -0
  10. buddy_agent_auth/providers/codebuddy/backend_profile.py +198 -0
  11. buddy_agent_auth/providers/codebuddy/client.py +666 -0
  12. buddy_agent_auth/providers/trae/__init__.py +27 -0
  13. buddy_agent_auth/providers/trae/aha_cache_codec.py +84 -0
  14. buddy_agent_auth/providers/trae/aha_device_cache.py +45 -0
  15. buddy_agent_auth/providers/trae/aha_device_service.py +325 -0
  16. buddy_agent_auth/providers/trae/aha_native_backend.py +61 -0
  17. buddy_agent_auth/providers/trae/aha_native_lifecycle.py +226 -0
  18. buddy_agent_auth/providers/trae/aha_native_semantics.py +67 -0
  19. buddy_agent_auth/providers/trae/aha_registration.py +157 -0
  20. buddy_agent_auth/providers/trae/aha_tiny_storage.py +135 -0
  21. buddy_agent_auth/providers/trae/app_storage.py +195 -0
  22. buddy_agent_auth/providers/trae/auth.py +362 -0
  23. buddy_agent_auth/providers/trae/client_metadata.py +102 -0
  24. buddy_agent_auth/providers/trae/credential_decrypt.py +111 -0
  25. buddy_agent_auth/providers/trae/credentials.py +39 -0
  26. buddy_agent_auth/providers/trae/device_hardware.py +377 -0
  27. buddy_agent_auth/providers/trae/diagnostics.py +38 -0
  28. buddy_agent_auth/providers/trae/login.py +415 -0
  29. buddy_agent_auth/providers/trae/login_identity.py +134 -0
  30. buddy_agent_auth/providers/trae/native_identity.py +111 -0
  31. buddy_agent_auth-0.1.0.dev1.dist-info/METADATA +40 -0
  32. buddy_agent_auth-0.1.0.dev1.dist-info/RECORD +34 -0
  33. buddy_agent_auth-0.1.0.dev1.dist-info/WHEEL +5 -0
  34. buddy_agent_auth-0.1.0.dev1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,198 @@
1
+ """Backend profiles and startup resolution for CodeBuddy regions.
2
+
3
+ The proxy talks to the same plugin protocol in both regions. Keeping the
4
+ region-specific values here means authentication, URL construction, session
5
+ validation, and model discovery can all use one resolved backend object.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from dataclasses import dataclass
11
+ import os
12
+ import pathlib
13
+ from types import MappingProxyType
14
+ from typing import Any, Mapping
15
+
16
+
17
+ def normalize_endpoint(endpoint: str) -> str:
18
+ """Return an endpoint without trailing slashes.
19
+
20
+ The existing client accepted arbitrary endpoint strings, so this helper
21
+ intentionally only normalizes the separator and does not impose a new
22
+ URL scheme validation policy.
23
+ """
24
+
25
+ value = str(endpoint).strip()
26
+ if not value:
27
+ raise ValueError("endpoint 不能为空")
28
+ normalized = value.rstrip("/")
29
+ if not normalized:
30
+ raise ValueError("endpoint 不能为空")
31
+ return normalized
32
+
33
+
34
+ @dataclass(frozen=True, slots=True)
35
+ class BackendProfile:
36
+ """Immutable region-specific CodeBuddy configuration."""
37
+
38
+ key: str
39
+ display_name: str
40
+ default_endpoint: str
41
+ auth_prefix: str
42
+ default_session_filename: str
43
+ models_resource: str
44
+ chat_path: str
45
+ enterprise_models_path: str
46
+
47
+ def __post_init__(self) -> None:
48
+ object.__setattr__(self, "default_endpoint", normalize_endpoint(self.default_endpoint))
49
+ if not self.auth_prefix.startswith("/"):
50
+ object.__setattr__(self, "auth_prefix", f"/{self.auth_prefix}")
51
+ if not self.chat_path.startswith("/"):
52
+ object.__setattr__(self, "chat_path", f"/{self.chat_path}")
53
+ if not self.enterprise_models_path.startswith("/"):
54
+ object.__setattr__(self, "enterprise_models_path", f"/{self.enterprise_models_path}")
55
+
56
+
57
+ @dataclass(frozen=True, slots=True)
58
+ class ResolvedBackend:
59
+ """A profile after endpoint and session-file precedence is resolved."""
60
+
61
+ profile: BackendProfile
62
+ endpoint: str
63
+ session_file: pathlib.Path
64
+
65
+ def __post_init__(self) -> None:
66
+ object.__setattr__(self, "endpoint", normalize_endpoint(self.endpoint))
67
+ object.__setattr__(self, "session_file", pathlib.Path(self.session_file).expanduser())
68
+
69
+ @property
70
+ def backend(self) -> BackendProfile:
71
+ """Alias useful to callers that refer to the profile as a backend."""
72
+
73
+ return self.profile
74
+
75
+ @property
76
+ def key(self) -> str:
77
+ return self.profile.key
78
+
79
+ @property
80
+ def display_name(self) -> str:
81
+ return self.profile.display_name
82
+
83
+ @property
84
+ def auth_prefix(self) -> str:
85
+ return self.profile.auth_prefix
86
+
87
+ @property
88
+ def models_resource(self) -> str:
89
+ return self.profile.models_resource
90
+
91
+ @property
92
+ def session_path(self) -> pathlib.Path:
93
+ """Alias for callers that use path terminology."""
94
+
95
+ return self.session_file
96
+
97
+ @property
98
+ def chat_path(self) -> str:
99
+ return self.profile.chat_path
100
+
101
+ @property
102
+ def enterprise_models_path(self) -> str:
103
+ return self.profile.enterprise_models_path
104
+
105
+
106
+ DOMESTIC_PROFILE = BackendProfile(
107
+ key="domestic",
108
+ display_name="国内版 CodeBuddy",
109
+ default_endpoint="https://copilot.tencent.com",
110
+ auth_prefix="/plugin",
111
+ default_session_filename=".codebuddy-session.json",
112
+ models_resource="models_config.domestic.json",
113
+ chat_path="/v2/chat/completions",
114
+ enterprise_models_path="/console/enterprises/{enterpriseId}/config/models",
115
+ )
116
+
117
+ GLOBAL_PROFILE = BackendProfile(
118
+ key="global",
119
+ display_name="国际版 CodeBuddy",
120
+ default_endpoint="https://www.codebuddy.ai",
121
+ auth_prefix="/plugin",
122
+ default_session_filename=".codebuddy-global-session.json",
123
+ models_resource="models_config.global.json",
124
+ chat_path="/v2/chat/completions",
125
+ enterprise_models_path="/console/enterprises/{enterpriseId}/config/models",
126
+ )
127
+
128
+ # Descriptive aliases keep the public module convenient without duplicating
129
+ # configuration values.
130
+ DOMESTIC_BACKEND = DOMESTIC_PROFILE
131
+ GLOBAL_BACKEND = GLOBAL_PROFILE
132
+ DOMESTIC = DOMESTIC_PROFILE
133
+ GLOBAL = GLOBAL_PROFILE
134
+ BACKEND_PROFILES: Mapping[str, BackendProfile] = MappingProxyType(
135
+ {
136
+ DOMESTIC_PROFILE.key: DOMESTIC_PROFILE,
137
+ GLOBAL_PROFILE.key: GLOBAL_PROFILE,
138
+ }
139
+ )
140
+
141
+
142
+ def profile_for_key(key: str) -> BackendProfile:
143
+ try:
144
+ return BACKEND_PROFILES[key]
145
+ except KeyError as exc:
146
+ raise ValueError(f"未知 CodeBuddy backend profile: {key}") from exc
147
+
148
+
149
+ def resolve_backend(
150
+ *,
151
+ global_mode: bool = False,
152
+ endpoint: str | None = None,
153
+ session_file: pathlib.Path | str | None = None,
154
+ environment: Mapping[str, str] | None = None,
155
+ profile: BackendProfile | None = None,
156
+ ) -> ResolvedBackend:
157
+ """Resolve profile, endpoint, and session path with CLI precedence.
158
+
159
+ Explicit ``endpoint`` wins first. ``--global`` selects the global
160
+ profile and therefore intentionally ignores ``CODEBUDDY_ENDPOINT`` when
161
+ no explicit endpoint is supplied. The environment variable is only a
162
+ domestic-mode fallback.
163
+ """
164
+
165
+ selected_profile = profile or (GLOBAL_PROFILE if global_mode else DOMESTIC_PROFILE)
166
+ if endpoint is not None:
167
+ resolved_endpoint = endpoint
168
+ elif profile is not None or global_mode:
169
+ resolved_endpoint = selected_profile.default_endpoint
170
+ else:
171
+ env = os.environ if environment is None else environment
172
+ resolved_endpoint = env.get("CODEBUDDY_ENDPOINT") or selected_profile.default_endpoint
173
+
174
+ resolved_session = (
175
+ pathlib.Path(session_file).expanduser()
176
+ if session_file is not None
177
+ else pathlib.Path.home() / selected_profile.default_session_filename
178
+ )
179
+ return ResolvedBackend(
180
+ profile=selected_profile,
181
+ endpoint=resolved_endpoint,
182
+ session_file=resolved_session,
183
+ )
184
+
185
+
186
+ def resolve_backend_from_args(
187
+ args: Any,
188
+ *,
189
+ environment: Mapping[str, str] | None = None,
190
+ ) -> ResolvedBackend:
191
+ """Resolve a parsed argparse namespace using the shared precedence rules."""
192
+
193
+ return resolve_backend(
194
+ global_mode=bool(getattr(args, "global_mode", False)),
195
+ endpoint=getattr(args, "endpoint", None),
196
+ session_file=getattr(args, "session_file", None),
197
+ environment=environment,
198
+ )