nya-proxy 0.1.2__tar.gz → 0.2.0__tar.gz

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 (58) hide show
  1. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/PKG-INFO +27 -39
  2. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/README.md +23 -33
  3. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/__init__.py +12 -12
  4. nya_proxy-0.2.0/nya/_version.py +1 -0
  5. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/logger.py +1 -1
  6. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/models.py +15 -1
  7. nya_proxy-0.2.0/nya/config/__init__.py +6 -0
  8. nya_proxy-0.1.2/nya_proxy/server/config.py → nya_proxy-0.2.0/nya/config/manager.py +91 -35
  9. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/config.yaml +1 -1
  10. nya_proxy-0.2.0/nya/core/factory.py +239 -0
  11. nya_proxy-0.2.0/nya/core/handler.py +236 -0
  12. nya_proxy-0.2.0/nya/core/proxy.py +336 -0
  13. nya_proxy-0.1.2/nya_proxy/core/request_executor.py → nya_proxy-0.2.0/nya/core/request.py +244 -177
  14. nya_proxy-0.2.0/nya/core/response.py +201 -0
  15. nya_proxy-0.2.0/nya/core/streaming.py +269 -0
  16. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/dashboard/api.py +17 -13
  17. nya_proxy-0.2.0/nya/html/favicon.ico +0 -0
  18. {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/index.html +8 -13
  19. {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/static/js/dashboard.js +15 -7
  20. nya_proxy-0.2.0/nya/html/static/logo.svg +398 -0
  21. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/integrations/openai.py +1 -6
  22. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/schema.json +2 -2
  23. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/server/app.py +165 -163
  24. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/server/auth.py +7 -14
  25. nya_proxy-0.1.2/nya_proxy/services/key_manager.py → nya_proxy-0.2.0/nya/services/key.py +2 -2
  26. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/services/metrics.py +1 -1
  27. nya_proxy-0.1.2/nya_proxy/services/request_queue.py → nya_proxy-0.2.0/nya/services/queue.py +2 -2
  28. nya_proxy-0.2.0/nya/utils/__init__.py +0 -0
  29. nya_proxy-0.1.2/nya_proxy/common/utils.py → nya_proxy-0.2.0/nya/utils/helper.py +34 -11
  30. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/nya_proxy.egg-info/PKG-INFO +27 -39
  31. nya_proxy-0.2.0/nya_proxy.egg-info/SOURCES.txt +89 -0
  32. nya_proxy-0.2.0/nya_proxy.egg-info/entry_points.txt +2 -0
  33. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/nya_proxy.egg-info/requires.txt +1 -1
  34. nya_proxy-0.2.0/nya_proxy.egg-info/top_level.txt +1 -0
  35. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/pyproject.toml +12 -16
  36. nya_proxy-0.1.2/nya_proxy/_version.py +0 -1
  37. nya_proxy-0.1.2/nya_proxy/core/handler.py +0 -627
  38. nya_proxy-0.1.2/nya_proxy/core/response_processor.py +0 -393
  39. nya_proxy-0.1.2/nya_proxy/pipeline/stages/execute_request.py +0 -370
  40. nya_proxy-0.1.2/nya_proxy.egg-info/SOURCES.txt +0 -77
  41. nya_proxy-0.1.2/nya_proxy.egg-info/entry_points.txt +0 -2
  42. nya_proxy-0.1.2/nya_proxy.egg-info/top_level.txt +0 -1
  43. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/LICENSE +0 -0
  44. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/__init__.py +0 -0
  45. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/constants.py +0 -0
  46. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/common/exceptions.py +0 -0
  47. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/core/__init__.py +0 -0
  48. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/dashboard/__init__.py +0 -0
  49. {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/login.html +0 -0
  50. {nya_proxy-0.1.2/nya_proxy/templates → nya_proxy-0.2.0/nya/html}/static/css/styles.css +0 -0
  51. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/integrations/__init__.py +0 -0
  52. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/server/__init__.py +0 -0
  53. {nya_proxy-0.1.2/nya_proxy → nya_proxy-0.2.0/nya}/services/__init__.py +0 -0
  54. /nya_proxy-0.1.2/nya_proxy/services/load_balancer.py → /nya_proxy-0.2.0/nya/services/lb.py +0 -0
  55. /nya_proxy-0.1.2/nya_proxy/services/rate_limiter.py → /nya_proxy-0.2.0/nya/services/limit.py +0 -0
  56. /nya_proxy-0.1.2/nya_proxy/core/header_utils.py → /nya_proxy-0.2.0/nya/utils/header.py +0 -0
  57. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/nya_proxy.egg-info/dependency_links.txt +0 -0
  58. {nya_proxy-0.1.2 → nya_proxy-0.2.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nya-proxy
3
- Version: 0.1.2
3
+ Version: 0.2.0
4
4
  Summary: A lightweight, flexible API proxy with dynamic token rotation, load balancing, and rate limiting capabilities.
5
5
  Author-email: "Nya Foundation Team (k3scat)" <k3scat@gmail.com>
6
6
  License-Expression: MIT
@@ -9,19 +9,16 @@ Project-URL: Bug Tracker, https://github.com/Nya-Foundation/nyaproxy/issues
9
9
  Classifier: Development Status :: 4 - Beta
10
10
  Classifier: Intended Audience :: Developers
11
11
  Classifier: Programming Language :: Python :: 3
12
- Classifier: Programming Language :: Python :: 3.8
13
- Classifier: Programming Language :: Python :: 3.9
14
- Classifier: Programming Language :: Python :: 3.10
15
12
  Classifier: Programming Language :: Python :: 3.11
16
13
  Classifier: Programming Language :: Python :: 3.12
17
- Requires-Python: >=3.8
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Python: >=3.11
18
16
  Description-Content-Type: text/markdown
19
17
  License-File: LICENSE
20
18
  Requires-Dist: fastapi>=0.95.0
21
19
  Requires-Dist: uvicorn>=0.22.0
22
20
  Requires-Dist: httpx>=0.24.0
23
21
  Requires-Dist: httpx-socks>=0.10.0
24
- Requires-Dist: nekoconf==0.1.8
25
22
  Requires-Dist: jinja2>=3.1.2
26
23
  Requires-Dist: setuptools>=42.0.0
27
24
  Requires-Dist: watchfiles>=0.18.0
@@ -29,6 +26,7 @@ Requires-Dist: brotli>=1.0.9
29
26
  Requires-Dist: jmespath>=1.0.1
30
27
  Requires-Dist: colorlog>=6.9.0
31
28
  Requires-Dist: orjson<4,>=3.10
29
+ Requires-Dist: nekoconf[remote,schema,server]==1.0.2
32
30
  Provides-Extra: dev
33
31
  Requires-Dist: pytest>=7.0.0; extra == "dev"
34
32
  Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
@@ -43,34 +41,24 @@ Dynamic: license-file
43
41
 
44
42
  # 🐾 NyaProxy - Universal API Proxy
45
43
 
46
- [![PyPI version](https://img.shields.io/pypi/v/nya-proxy.svg)](https://pypi.org/project/nya-proxy/)
47
- [![Python versions](https://img.shields.io/pypi/pyversions/nya-proxy.svg)](https://pypi.org/project/nya-proxy/)
48
- [![License](https://img.shields.io/github/license/Nya-Foundation/nyaproxy.svg)](https://github.com/Nya-Foundation/nyaproxy/blob/main/LICENSE)
49
- [![Code Coverage](https://codecov.io/gh/Nya-Foundation/nyaproxy/branch/main/graph/badge.svg)](https://codecov.io/gh/Nya-Foundation/nyaproxy)
50
- [![CodeQL & Dependencies Scan](https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml/badge.svg)](https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml)
51
- [![CI/CD Builds](https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml/badge.svg)](https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml)
52
- [![Docker](https://img.shields.io/docker/pulls/k3scat/nya-proxy)](https://hub.docker.com/r/k3scat/nya-proxy)
53
-
54
- <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/images/banner.png" alt="NyaProxy Banner"/>
55
-
56
- ## Table of Contents
57
- - [Introduction](#-introduction)
58
- - [Core Capabilities](#-core-capabilities)
59
- - [Quick Start](#-quick-start)
60
- - [Service Endpoints](#-service-endpoints)
61
- - [API Configuration](#-api-configuration)
62
- - [OpenAI-Compatible APIs](#openai-compatible-apis-gemini-anthropic-etc)
63
- - [Generic REST APIs](#generic-rest-apis)
64
- - [Security Features](#-security-features)
65
- - [Multiple API Keys Support](#multiple-api-keys-support)
66
- - [Advanced Features](#advanced-features)
67
- - [Dynamic Header Substitution](#dynamic-header-substitution)
68
- - [Request Body Substitution](#request-body-substitution)
69
- - [Management Interfaces](#-management-interfaces)
70
- - [Reference Architecture](#-reference-architecture)
71
- - [Future Roadmap](#-future-roadmap)
72
- - [Community](#-community)
73
- - [Project Growth](#-project-growth)
44
+ <div align="center">
45
+ <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/banner.png" alt="NyaProxy Banner" width="800" />
46
+
47
+ <h3>Your purr-fect Swiss Army Knife for API Proxy Management~</h3>
48
+
49
+ <div>
50
+ <a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/v/nya-proxy.svg" alt="PyPI version"/></a>
51
+ <a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/pyversions/nya-proxy.svg" alt="Python versions"/></a>
52
+ <a href="https://github.com/Nya-Foundation/nyaproxy/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Nya-Foundation/nyaproxy.svg" alt="License"/></a>
53
+ </div>
54
+
55
+ <div>
56
+ <a href="https://codecov.io/gh/Nya-Foundation/nyaproxy"><img src="https://codecov.io/gh/Nya-Foundation/nyaproxy/branch/main/graph/badge.svg" alt="Code Coverage"/></a>
57
+ <a href="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml"><img src="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml/badge.svg" alt="CodeQL & Dependencies Scan"/></a>
58
+ <a href="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml"><img src="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml/badge.svg" alt="CI/CD Builds"/></a>
59
+ <a href="https://hub.docker.com/r/k3scat/nya-proxy"><img src="https://img.shields.io/docker/pulls/k3scat/nya-proxy" alt="Docker"/></a>
60
+ </div>
61
+ </div>
74
62
 
75
63
  ## 🌈 Introduction
76
64
 
@@ -199,7 +187,7 @@ Configuration reference can be found under [Configs folder](configs/) folder
199
187
 
200
188
  ```yaml
201
189
  # Basic config.yaml example for Gemini API
202
- nya_proxy:
190
+ server:
203
191
  host: 0.0.0.0
204
192
  port: 8080
205
193
  api_key:
@@ -353,7 +341,7 @@ novelai:
353
341
  NyaProxy supports using multiple API keys for authentication:
354
342
 
355
343
  ```yaml
356
- nya_proxy:
344
+ server:
357
345
  api_key:
358
346
  - your_master_key_for_admin_access
359
347
  - another_api_key_for_proxy_only
@@ -416,7 +404,7 @@ For detailed configuration options and examples, see the [Request Body Substitut
416
404
  ## 🖥️ Management Interfaces
417
405
 
418
406
  ### Real-time Metrics Dashboard
419
- <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/images/dashboard_ui.png" width="800" alt="Dashboard UI"/>
407
+ <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/dashboard_ui.png" width="800" alt="Dashboard UI"/>
420
408
 
421
409
  Monitor at `http://localhost:8080/dashboard`:
422
410
  - Request volumes and response times
@@ -425,7 +413,7 @@ Monitor at `http://localhost:8080/dashboard`:
425
413
  - Error rates and status codes
426
414
 
427
415
  ### Visual Configuration Interface
428
- <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/images/config_ui.png" width="800" alt="Configuration UI"/>
416
+ <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/config_ui.png" width="800" alt="Configuration UI"/>
429
417
 
430
418
  Manage at `http://localhost:8080/config`:
431
419
  - Live configuration editing
@@ -461,7 +449,7 @@ F --> H[🔍 Custom Metrics API]
461
449
  [![Discord](https://img.shields.io/discord/1365929019714834493)](https://discord.gg/jXAxVPSs7K)
462
450
 
463
451
  > [!NOTE]
464
- > Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com)
452
+ > Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com) or join our discord community at [Nya Foundation](https://discord.gg/jXAxVPSs7K)
465
453
 
466
454
  ## 📈 Project Growth
467
455
 
@@ -1,33 +1,23 @@
1
1
  # 🐾 NyaProxy - Universal API Proxy
2
2
 
3
- [![PyPI version](https://img.shields.io/pypi/v/nya-proxy.svg)](https://pypi.org/project/nya-proxy/)
4
- [![Python versions](https://img.shields.io/pypi/pyversions/nya-proxy.svg)](https://pypi.org/project/nya-proxy/)
5
- [![License](https://img.shields.io/github/license/Nya-Foundation/nyaproxy.svg)](https://github.com/Nya-Foundation/nyaproxy/blob/main/LICENSE)
6
- [![Code Coverage](https://codecov.io/gh/Nya-Foundation/nyaproxy/branch/main/graph/badge.svg)](https://codecov.io/gh/Nya-Foundation/nyaproxy)
7
- [![CodeQL & Dependencies Scan](https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml/badge.svg)](https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml)
8
- [![CI/CD Builds](https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml/badge.svg)](https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml)
9
- [![Docker](https://img.shields.io/docker/pulls/k3scat/nya-proxy)](https://hub.docker.com/r/k3scat/nya-proxy)
10
-
11
- <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/images/banner.png" alt="NyaProxy Banner"/>
12
-
13
- ## Table of Contents
14
- - [Introduction](#-introduction)
15
- - [Core Capabilities](#-core-capabilities)
16
- - [Quick Start](#-quick-start)
17
- - [Service Endpoints](#-service-endpoints)
18
- - [API Configuration](#-api-configuration)
19
- - [OpenAI-Compatible APIs](#openai-compatible-apis-gemini-anthropic-etc)
20
- - [Generic REST APIs](#generic-rest-apis)
21
- - [Security Features](#-security-features)
22
- - [Multiple API Keys Support](#multiple-api-keys-support)
23
- - [Advanced Features](#advanced-features)
24
- - [Dynamic Header Substitution](#dynamic-header-substitution)
25
- - [Request Body Substitution](#request-body-substitution)
26
- - [Management Interfaces](#-management-interfaces)
27
- - [Reference Architecture](#-reference-architecture)
28
- - [Future Roadmap](#-future-roadmap)
29
- - [Community](#-community)
30
- - [Project Growth](#-project-growth)
3
+ <div align="center">
4
+ <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/banner.png" alt="NyaProxy Banner" width="800" />
5
+
6
+ <h3>Your purr-fect Swiss Army Knife for API Proxy Management~</h3>
7
+
8
+ <div>
9
+ <a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/v/nya-proxy.svg" alt="PyPI version"/></a>
10
+ <a href="https://pypi.org/project/nya-proxy/"><img src="https://img.shields.io/pypi/pyversions/nya-proxy.svg" alt="Python versions"/></a>
11
+ <a href="https://github.com/Nya-Foundation/nyaproxy/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Nya-Foundation/nyaproxy.svg" alt="License"/></a>
12
+ </div>
13
+
14
+ <div>
15
+ <a href="https://codecov.io/gh/Nya-Foundation/nyaproxy"><img src="https://codecov.io/gh/Nya-Foundation/nyaproxy/branch/main/graph/badge.svg" alt="Code Coverage"/></a>
16
+ <a href="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml"><img src="https://github.com/nya-foundation/nyaproxy/actions/workflows/scan.yml/badge.svg" alt="CodeQL & Dependencies Scan"/></a>
17
+ <a href="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml"><img src="https://github.com/Nya-Foundation/nyaproxy/actions/workflows/publish.yml/badge.svg" alt="CI/CD Builds"/></a>
18
+ <a href="https://hub.docker.com/r/k3scat/nya-proxy"><img src="https://img.shields.io/docker/pulls/k3scat/nya-proxy" alt="Docker"/></a>
19
+ </div>
20
+ </div>
31
21
 
32
22
  ## 🌈 Introduction
33
23
 
@@ -156,7 +146,7 @@ Configuration reference can be found under [Configs folder](configs/) folder
156
146
 
157
147
  ```yaml
158
148
  # Basic config.yaml example for Gemini API
159
- nya_proxy:
149
+ server:
160
150
  host: 0.0.0.0
161
151
  port: 8080
162
152
  api_key:
@@ -310,7 +300,7 @@ novelai:
310
300
  NyaProxy supports using multiple API keys for authentication:
311
301
 
312
302
  ```yaml
313
- nya_proxy:
303
+ server:
314
304
  api_key:
315
305
  - your_master_key_for_admin_access
316
306
  - another_api_key_for_proxy_only
@@ -373,7 +363,7 @@ For detailed configuration options and examples, see the [Request Body Substitut
373
363
  ## 🖥️ Management Interfaces
374
364
 
375
365
  ### Real-time Metrics Dashboard
376
- <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/images/dashboard_ui.png" width="800" alt="Dashboard UI"/>
366
+ <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/dashboard_ui.png" width="800" alt="Dashboard UI"/>
377
367
 
378
368
  Monitor at `http://localhost:8080/dashboard`:
379
369
  - Request volumes and response times
@@ -382,7 +372,7 @@ Monitor at `http://localhost:8080/dashboard`:
382
372
  - Error rates and status codes
383
373
 
384
374
  ### Visual Configuration Interface
385
- <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/images/config_ui.png" width="800" alt="Configuration UI"/>
375
+ <img src="https://raw.githubusercontent.com/Nya-Foundation/NyaProxy/main/assets/config_ui.png" width="800" alt="Configuration UI"/>
386
376
 
387
377
  Manage at `http://localhost:8080/config`:
388
378
  - Live configuration editing
@@ -418,7 +408,7 @@ F --> H[🔍 Custom Metrics API]
418
408
  [![Discord](https://img.shields.io/discord/1365929019714834493)](https://discord.gg/jXAxVPSs7K)
419
409
 
420
410
  > [!NOTE]
421
- > Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com)
411
+ > Need support? Contact [k3scat@gmail.com](mailto:k3scat@gmail.com) or join our discord community at [Nya Foundation](https://discord.gg/jXAxVPSs7K)
422
412
 
423
413
  ## 📈 Project Growth
424
414
 
@@ -5,22 +5,22 @@ NyaProxy - A cute and simple low-level API proxy with dynamic token rotation.
5
5
  from ._version import __version__
6
6
  from .common.logger import getLogger
7
7
  from .common.models import NyaRequest
8
- from .common.utils import format_elapsed_time
9
- from .core.handler import NyaProxyCore
10
- from .core.header_utils import HeaderUtils
11
- from .core.request_executor import RequestExecutor
12
- from .core.response_processor import ResponseProcessor
13
- from .dashboard.api import DashboardAPI
14
8
 
15
9
  # Import key components for easier access
16
- from .server.config import ConfigError, ConfigManager
17
- from .services.key_manager import KeyManager
18
- from .services.load_balancer import LoadBalancer
10
+ from .config.manager import ConfigError, ConfigManager
11
+ from .core.proxy import NyaProxyCore
12
+ from .core.request import RequestExecutor
13
+ from .core.response import ResponseProcessor
14
+ from .dashboard.api import DashboardAPI
15
+ from .services.key import KeyManager
16
+ from .services.lb import LoadBalancer
17
+ from .services.limit import RateLimiter
19
18
  from .services.metrics import MetricsCollector
20
- from .services.rate_limiter import RateLimiter
21
- from .services.request_queue import RequestQueue
19
+ from .services.queue import RequestQueue
20
+ from .utils.header import HeaderUtils
21
+ from .utils.helper import format_elapsed_time
22
22
 
23
- # Define __all__ to control what is imported with "from nya_proxy import *"
23
+ # Define __all__ to control what is imported with "from nya import *"
24
24
  __all__ = [
25
25
  # Core application
26
26
  "ConfigManager",
@@ -0,0 +1 @@
1
+ __version__ = "0.2.0"
@@ -19,7 +19,7 @@ _loggers = {}
19
19
 
20
20
 
21
21
  def get_logger(
22
- name: str = "nya_proxy",
22
+ name: str = "nya",
23
23
  log_config: Dict[str, Any] = {
24
24
  "enable": True,
25
25
  "level": "INFO",
@@ -47,7 +47,7 @@ class NyaRequest:
47
47
  # Required request fields
48
48
  method: str
49
49
 
50
- # original url from the request, contains the full path of nya_proxy
50
+ # original url from the request, contains the full path of nya
51
51
  _url: Union["URL", str]
52
52
 
53
53
  # final url to be requested, differ from _url since the request is proxied
@@ -75,3 +75,17 @@ class NyaRequest:
75
75
  # Whether the original request ask for a streaming response (openai)
76
76
  _is_streaming: bool = False
77
77
  _config: AdvancedConfig = field(default_factory=AdvancedConfig)
78
+
79
+ @staticmethod
80
+ async def from_request(request: "Request") -> "NyaRequest":
81
+ """
82
+ Create a NyaRequest instance from a FastAPI Request object.
83
+ """
84
+ return NyaRequest(
85
+ method=request.method,
86
+ _url=request.url,
87
+ headers=request.headers,
88
+ content=await request.body(),
89
+ _raw=request,
90
+ _added_at=time.time(),
91
+ )
@@ -0,0 +1,6 @@
1
+ from .manager import ConfigError, ConfigManager
2
+
3
+ __all__ = [
4
+ "ConfigManager",
5
+ "ConfigError",
6
+ ]
@@ -6,7 +6,7 @@ import logging
6
6
  import os
7
7
  from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union, cast
8
8
 
9
- from nekoconf import NekoConfigClient, NekoConfigServer
9
+ from nekoconf import NekoConfigServer, NekoConfigWrapper
10
10
 
11
11
 
12
12
  class ConfigError(Exception):
@@ -64,48 +64,81 @@ class ApiSettingDescriptor(Generic[T]):
64
64
  class ConfigManager:
65
65
  """
66
66
  Manages configuration for NyaProxy using NekoConf.
67
- Provides helper methods to access configuration values.
67
+ Implements the singleton pattern to ensure only one instance exists.
68
68
  """
69
69
 
70
+ _instance = None
71
+ _initialized = False
72
+
73
+ def __new__(cls, *args, **kwargs):
74
+ """Create a singleton instance."""
75
+ if cls._instance is None:
76
+ cls._instance = super(ConfigManager, cls).__new__(cls)
77
+ return cls._instance
78
+
70
79
  def __init__(
71
80
  self,
72
- config_path: str,
81
+ config_path: Optional[str] = None,
73
82
  schema_path: Optional[str] = None,
83
+ remote_url: Optional[str] = None,
84
+ remote_api_key: Optional[str] = None,
74
85
  logger: Optional[logging.Logger] = None,
75
86
  ):
76
87
  """
77
- Initialize the configuration manager.
88
+ Initialize the configuration manager (once).
78
89
 
79
90
  Args:
80
91
  config_file: Path to the configuration file
81
- logger: Optional logger instance
92
+ schema_file: Path to the schema file for validation (optional)
93
+ remote_url: URL for remote configuration (optional)
94
+ remote_api_key: API key for remote configuration (optional)
95
+ logger: Logger instance for logging (optional)
82
96
  """
83
- self.config: NekoConfigClient = None
97
+ # Skip initialization if already initialized
98
+ if self._initialized:
99
+ return
100
+
101
+ self.config: NekoConfigWrapper = None
84
102
  self.server: NekoConfigServer = None
85
103
 
86
104
  self.config_path = config_path
87
105
  self.schema_path = schema_path
106
+ self.remote_url = remote_url
107
+ self.remote_api_key = remote_api_key
88
108
 
89
109
  self.logger = logger or logging.getLogger(__name__)
90
110
 
91
- if not os.path.exists(config_path):
111
+ if config_path and not os.path.exists(config_path):
92
112
  raise ConfigError(f"Configuration file not found: {config_path}")
93
113
 
94
- try:
95
- self.config = self._init_config_client()
96
- self.server = NekoConfigServer(
97
- config=self.config.config, logger=self.logger
98
- )
99
- except Exception as e:
100
- error_msg = f"Failed to load configuration from {config_path}: {str(e)}"
101
- self.logger.error(f"Failed to load configuration: {error_msg}")
102
- raise ConfigError(error_msg)
114
+ self.config = self._init_config_client()
115
+ self.server = self._init_config_server()
116
+
117
+ # Mark as initialized
118
+ ConfigManager._initialized = True
119
+
120
+ @classmethod
121
+ def reset(cls):
122
+ """Reset the singleton instance (useful for testing)."""
123
+ cls._instance = None
124
+ cls._initialized = False
125
+
126
+ @classmethod
127
+ def get_instance(cls) -> "ConfigManager":
128
+ """Get the singleton instance, creating it if needed."""
129
+ if cls._instance is None:
130
+ cls._instance = ConfigManager()
131
+ return cls._instance
103
132
 
104
- def _init_config_client(self) -> NekoConfigClient:
105
- """Initialize the NekoConfigClient."""
106
- client = NekoConfigClient(
133
+ def _init_config_client(self) -> NekoConfigWrapper:
134
+ """Initialize the NekoConfigWrapper."""
135
+ client = NekoConfigWrapper(
107
136
  config_path=self.config_path,
108
137
  schema_path=self.schema_path,
138
+ env_override_enabled=True,
139
+ env_prefix="NYA",
140
+ remote_url=self.remote_url,
141
+ remote_api_key=self.remote_api_key,
109
142
  logger=self.logger,
110
143
  )
111
144
 
@@ -117,41 +150,64 @@ class ConfigManager:
117
150
  self.logger.info("Configuration loaded and validated successfully")
118
151
  return client
119
152
 
153
+ def _init_config_server(self) -> NekoConfigServer:
154
+
155
+ if self.remote_url is not None:
156
+ self.logger.warning(
157
+ "Remote Config URL is set. NekoConfigServer will not be initialized on this local instance."
158
+ )
159
+ return None
160
+
161
+ if self.config is None:
162
+ self.logger.debug("ConfigManager is not initialized. skipping server init.")
163
+ return None
164
+
165
+ try:
166
+ server = NekoConfigServer(
167
+ config=self.config.config, logger=self.logger, register_signals=True
168
+ )
169
+ except Exception as e:
170
+ error_msg = f"Failed to load configuration from {self.config.config.config_path}: {str(e)}"
171
+ self.logger.error(f"Failed to load configuration: {error_msg}")
172
+ raise ConfigError(error_msg)
173
+
174
+ return server
175
+
120
176
  def get_port(self) -> int:
121
177
  """Get the port for the proxy server."""
122
- return self.config.get_int("nya_proxy.port", 8080)
178
+ return self.config.get_int("server.port", 8080)
123
179
 
124
180
  def get_host(self) -> str:
125
181
  """Get the host for the proxy server."""
126
- return self.config.get_str("nay_proxy.host", "0.0.0.0")
182
+ return self.config.get_str("server.host", "0.0.0.0")
127
183
 
128
184
  def get_debug_level(self) -> str:
129
185
  """Get the debug level for logging."""
130
- return self.config.get_str("nya_proxy.debug_level", "INFO")
186
+ return self.config.get_str("server.debug_level", "INFO")
131
187
 
132
188
  def get_dashboard_enabled(self) -> bool:
133
189
  """Check if dashboard is enabled."""
134
- return self.config.get_bool("nya_proxy.dashboard.enabled", True)
190
+ return self.config.get_bool("server.dashboard.enabled", True)
135
191
 
136
192
  def get_queue_enabled(self) -> bool:
137
193
  """Check if request queuing is enabled."""
138
- return self.config.get_bool("nya_proxy.queue.enabled", True)
194
+ return self.config.get_bool("server.queue.enabled", True)
139
195
 
140
196
  def get_retry_mode(self) -> str:
141
197
  """Get the retry mode for failed requests."""
142
- return self.config.get_str("nya_proxy.retry.mode", "default")
198
+ return self.config.get_str("server.retry.mode", "default")
143
199
 
144
200
  def get_retry_config(self) -> Dict[str, Any]:
145
201
  """Get the retry configuration."""
146
- return self.config.get_dict("nya_proxy.retry", {})
202
+ return self.config.get_dict("server.retry", {})
147
203
 
148
204
  def get_queue_size(self) -> int:
149
205
  """Get the maximum queue size."""
150
- return self.config.get_int("nya_proxy.queue.max_size", 100)
206
+ return self.config.get_int("server.queue.max_size", 100)
151
207
 
152
208
  def get_queue_expiry(self) -> int:
153
209
  """Get the default expiry time for queued requests in seconds."""
154
- return self.config.get_int("nya_proxy.queue.expiry_seconds", 300)
210
+ return self.config.get_int("server.queue.expiry_seconds", 300)
155
211
 
156
212
  def get_api_key(self) -> Union[None, str, List[str]]:
157
213
  """
@@ -162,7 +218,7 @@ class ConfigManager:
162
218
  or a list of strings for multiple keys
163
219
  """
164
220
 
165
- api_key = self.config.get("nya_proxy.api_key", None)
221
+ api_key = self.config.get("server.api_key", None)
166
222
 
167
223
  if api_key is None:
168
224
  return None
@@ -200,18 +256,18 @@ class ConfigManager:
200
256
  def get_logging_config(self) -> Dict[str, Any]:
201
257
  """Get the logging configuration."""
202
258
  return {
203
- "enabled": self.config.get_bool("nya_proxy.logging.enabled", True),
204
- "level": self.config.get_str("nya_proxy.logging.level", "INFO"),
205
- "log_file": self.config.get_str("nya_proxy.logging.log_file", "app.log"),
259
+ "enabled": self.config.get_bool("server.logging.enabled", True),
260
+ "level": self.config.get_str("server.logging.level", "INFO"),
261
+ "log_file": self.config.get_str("server.logging.log_file", "app.log"),
206
262
  }
207
263
 
208
264
  def get_proxy_enabled(self) -> bool:
209
265
  """Check if the proxy is enabled."""
210
- return self.config.get_bool("nya_proxy.proxy.enabled", False)
266
+ return self.config.get_bool("server.proxy.enabled", False)
211
267
 
212
268
  def get_proxy_address(self) -> str:
213
269
  """Get the proxy address."""
214
- return self.config.get_str("nya_proxy.proxy.address", "")
270
+ return self.config.get_str("server.proxy.address", "")
215
271
 
216
272
  def get_default_settings(self) -> Dict[str, Any]:
217
273
  """Get the default settings for endpoints."""
@@ -224,7 +280,7 @@ class ConfigManager:
224
280
  Returns:
225
281
  Default timeout in seconds or 10 if not specified
226
282
  """
227
- return self.config.get_int("nya_proxy.timeouts.request_timeout_seconds", 30)
283
+ return self.config.get_int("server.timeouts.request_timeout_seconds", 30)
228
284
 
229
285
  def get_default_setting(self, setting_path: str, default_value: Any = None) -> Any:
230
286
  """
@@ -1,4 +1,4 @@
1
- nya_proxy:
1
+ server:
2
2
  host: 0.0.0.0
3
3
  port: 8080
4
4
  api_key: