airsltd commited on
Commit
0f7bf78
·
1 Parent(s): b8edb83
.clinerules ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 自定义指令
2
+ CUSTOM_INSTRUCTIONS = """
3
+ # 必须遵守的约束
4
+ - 所有交流均采用中文
5
+ - 如果在开发中有不明确的需求,或者需要用户的任何帮助,请告知用户,如果是调研,请一次只提出一个问题
6
+ - 本项目使用conda进行环境管理,当前使用的环境为any-api,如果找不到此环境,请使用: conda create -n any-api python=3.12;如果找到环境,请使用: conda activate any-api激活环境
7
+
8
+ # 实现目标
9
+ - 使用python进行开发
10
+ - 当用户访问中转主机url:http://127.0.0.1:7860/v1/https/open.bigmodel.cn/api/paas/v4/chat/completions时,实际上中转主机会访问:https/open.bigmodel.cn/api/paas/v4/chat/completions,可以看出实际上对于主机来说,就是http://127.0.0.1:7860/v1/https/open.bigmodel.cn会跳转到 https://open.bigmodel.cn,就是https协议,域名open.bigmodel.cn,endpoint为:/api/paas/v4/chat/completions
11
+ - 要求实现多线程的模式实现代理功能
12
+ - 功能的实现,采用fastapi框架
13
+
14
+ # 知识参考
15
+ - 无
16
+
17
+ """
18
+
19
+
20
+ ---
21
+ description: Describes Cline's Memory Bank system, its structure, and workflows for maintaining project knowledge across sessions.
22
+ author: https://github.com/nickbaumann98
23
+ version: 1.0
24
+ tags: ["memory-bank", "knowledge-base", "core-behavior", "documentation-protocol"]
25
+ globs: ["memory-bank/**/*.md", "*"]
26
+ ---
27
+ # Cline's Memory Bank
28
+
29
+ I am Cline, an expert software engineer with a unique characteristic: my memory resets completely between sessions. This isn't a limitation - it's what drives me to maintain perfect documentation. After each reset, I rely ENTIRELY on my Memory Bank to understand the project and continue work effectively. I MUST read ALL memory bank files at the start of EVERY task - this is not optional.
30
+
31
+ ## Memory Bank Structure
32
+
33
+ The Memory Bank consists of core files and optional context files, all in Markdown format. Files build upon each other in a clear hierarchy:
34
+
35
+ ```mermaid
36
+ flowchart TD
37
+ PB[projectbrief.md] --> PC[productContext.md]
38
+ PB --> SP[systemPatterns.md]
39
+ PB --> TC[techContext.md]
40
+
41
+ PC --> AC[activeContext.md]
42
+ SP --> AC
43
+ TC --> AC
44
+
45
+ AC --> P[progress.md]
46
+ ```
47
+
48
+ ### Core Files (Required)
49
+ 1. `projectbrief.md`
50
+ - Foundation document that shapes all other files
51
+ - Created at project start if it doesn't exist
52
+ - Defines core requirements and goals
53
+ - Source of truth for project scope
54
+
55
+ 2. `productContext.md`
56
+ - Why this project exists
57
+ - Problems it solves
58
+ - How it should work
59
+ - User experience goals
60
+
61
+ 3. `activeContext.md`
62
+ - Current work focus
63
+ - Recent changes
64
+ - Next steps
65
+ - Active decisions and considerations
66
+ - Important patterns and preferences
67
+ - Learnings and project insights
68
+
69
+ 4. `systemPatterns.md`
70
+ - System architecture
71
+ - Key technical decisions
72
+ - Design patterns in use
73
+ - Component relationships
74
+ - Critical implementation paths
75
+
76
+ 5. `techContext.md`
77
+ - Technologies used
78
+ - Development setup
79
+ - Technical constraints
80
+ - Dependencies
81
+ - Tool usage patterns
82
+
83
+ 6. `progress.md`
84
+ - What works
85
+ - What's left to build
86
+ - Current status
87
+ - Known issues
88
+ - Evolution of project decisions
89
+
90
+ ### Additional Context
91
+ Create additional files/folders within memory-bank/ when they help organize:
92
+ - Complex feature documentation
93
+ - Integration specifications
94
+ - API documentation
95
+ - Testing strategies
96
+ - Deployment procedures
97
+
98
+ ## Core Workflows
99
+
100
+ ### Plan Mode
101
+ ```mermaid
102
+ flowchart TD
103
+ Start[Start] --> ReadFiles[Read Memory Bank]
104
+ ReadFiles --> CheckFiles{Files Complete?}
105
+
106
+ CheckFiles -->|No| Plan[Create Plan]
107
+ Plan --> Document[Document in Chat]
108
+
109
+ CheckFiles -->|Yes| Verify[Verify Context]
110
+ Verify --> Strategy[Develop Strategy]
111
+ Strategy --> Present[Present Approach]
112
+ ```
113
+
114
+ ### Act Mode
115
+ ```mermaid
116
+ flowchart TD
117
+ Start[Start] --> Context[Check Memory Bank]
118
+ Context --> Update[Update Documentation]
119
+ Update --> Execute[Execute Task]
120
+ Execute --> Document[Document Changes]
121
+ ```
122
+
123
+ ## Documentation Updates
124
+
125
+ Memory Bank updates occur when:
126
+ 1. Discovering new project patterns
127
+ 2. After implementing significant changes
128
+ 3. When user requests with **update memory bank** (MUST review ALL files)
129
+ 4. When context needs clarification
130
+
131
+ ```mermaid
132
+ flowchart TD
133
+ Start[Update Process]
134
+
135
+ subgraph Process
136
+ P1[Review ALL Files]
137
+ P2[Document Current State]
138
+ P3[Clarify Next Steps]
139
+ P4[Document Insights & Patterns]
140
+
141
+ P1 --> P2 --> P3 --> P4
142
+ end
143
+
144
+ Start --> Process
145
+ ```
146
+
147
+ Note: When triggered by **update memory bank**, I MUST review every memory bank file, even if some don't require updates. Focus particularly on activeContext.md and progress.md as they track current state.
148
+
149
+ REMEMBER: After every memory reset, I begin completely fresh. The Memory Bank is my only link to previous work. It must be maintained with precision and clarity, as my effectiveness depends entirely on its accuracy.
150
+
151
+ """
152
+ # 自动批准规则
153
+ AUTO_APPROVE = true
__init__.py ADDED
File without changes
readme.md → docs/用fastapi实现代理服务器api映射.md RENAMED
File without changes
global_state.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # global_state.py
2
+ _state = {}
3
+
4
+ def set(key, value):
5
+ _state[key] = value
6
+
7
+ def get(key):
8
+ return _state.get(key)
9
+
memory-bank/activeContext.md ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 活跃背景
2
+
3
+ ## 当前工作重点
4
+ - 正在进行Memory Bank核心文件的生成,以确保项目知识的持久化和可追溯性。
5
+
6
+ ## 最近的更改
7
+ - 已创建`projectbrief.md`,`productContext.md`,`systemPatterns.md`和`techContext.md`。
8
+
9
+ ## 下一步计划
10
+ - 创建`progress.md`文件。
11
+ - 完成所有Memory Bank核心文件的创建。
12
+ - 准备好根据用户指令,进入下一步的开发或规划阶段。
13
+
14
+ ## 活跃的决策和考虑事项
15
+ - 确保Memory Bank中的每个文件都准确地反映了项目的当前状态、目标和技术细节。
16
+ - 持续评估和更新Memory Bank,以适应项目进展和需求变化。
17
+
18
+ ## 重要模式和偏好
19
+ - 在FastAPI代理实现中,优先使用`httpx`进行异步HTTP请求转发,以保持与FastAPI异步特性的兼容性。
20
+ - 严格遵循URL解析规则,确保代理转发的准确性。
21
+
22
+ ## 项目见解
23
+ - URL解析和重构是代理功能的核心,其健壮性直接影响代理的可用性。
24
+ - 异步编程模型对于实现高性能的代理服务至关重要。
25
+ - 依赖项中存在`flask`和`requests`,在实际开发中需注意避免与FastAPI和`httpx`的混用,以保持代码库的一致性和性能。
memory-bank/productContext.md ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 产品背景
2
+
3
+ ## 为什么存在?
4
+ 在某些场景下,直接访问外部HTTPS API可能会面临跨域、证书、协议转换等问题。本项目旨在提供一个本地代理服务,简化这些复杂性,使得用户可以通过统一的HTTP接口访问各种外部HTTPS API。
5
+
6
+ ## 解决的问题
7
+ - **协议转换**:将HTTP请求透明地转换为HTTPS请求。
8
+ - **URL映射**:根据特定的URL路径规则,将本地代理URL映射到实际的外部API URL。
9
+ - **简化开发**:为开发者提供一个统一的、本地可访问的API入口,无需关心底层API的协议和域名细节。
10
+
11
+ ## 工作方式
12
+ 用户通过访问本地代理服务器的特定URL(例如:`http://127.0.0.1:7860/v1/https/open.bigmodel.cn/...`),代理服务器会解析URL,提取出目标协议(`https`)、目标域名(`open.bigmodel.cn`)和原始路径(`/api/paas/v4/chat/completions`),然后将请求转发到实际的外部API地址。
13
+
14
+ ## 用户体验目标
15
+ - **透明性**:用户无需感知代理的存在,感觉就像直接访问外部API一样。
16
+ - **易用性**:提供简单直观的URL结构,方便用户理解和使用。
17
+ - **高性能**:通过多线程处理,确保代理服务在高并发场景下依然响应迅速。
memory-bank/progress.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 项目进度
2
+
3
+ ## 已完成的工作
4
+ - **Memory Bank初始化**:已成功创建并填充所有核心Memory Bank文件,包括:
5
+ - `projectbrief.md`:项目简介和核心需求。
6
+ - `productContext.md`:产品背景、解决的问题和用户体验目标。
7
+ - `systemPatterns.md`:系统架构、关键技术决策和设计模式。
8
+ - `techContext.md`:技术栈、开发设置和依赖项。
9
+ - `activeContext.md`:当前工作重点、最近更改和下一步计划。
10
+ - `progress.md`:当前进度和已知问题。
11
+
12
+ ## 剩余的工作
13
+ - Memory Bank的核心文件生成任务已完成。
14
+ - 接下来将根据用户指令,开始FastAPI代理服务器的实际开发或进一步的规划。
15
+
16
+ ## 当前状态
17
+ - 项目的初始知识库已建立。
18
+ - 环境配置(conda环境`any-api`)和依赖安装(`requirements.txt`)是下一步开发前的准备工作。
19
+
20
+ ## 已知问题
21
+ - `requirements.txt`中包含了`flask`和`requests`,尽管FastAPI代理主要依赖`httpx`。这可能意味着存在未使用的依赖或历史遗留,在未来的代码清理或优化阶段可以考虑移除。目前不影响核心代理功能的实现。
memory-bank/projectbrief.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 项目简介
2
+
3
+ 本项目旨在开发一个基于FastAPI的多线程代理服务器。主要目标是将特定格式的HTTP请求(例如:`http://127.0.0.1:7860/v1/https/open.bigmodel.cn/api/paas/v4/chat/completions`)转发到其对应的HTTPS目标地址(例如:`https://open.bigmodel.cn/api/paas/v4/chat/completions`)。
4
+
5
+ ## 核心需求
6
+ - 实现HTTP到HTTPS的代理转发。
7
+ - 支持多线程处理请求以提高并发性能。
8
+ - 使用FastAPI框架进行开发。
9
+ - 环境管理使用conda,环境名为`any-api`。
10
+
11
+ ## 目标
12
+ - 提供一个稳定、高效的代理服务,用于转发API请求。
13
+ - 简化外部API的访问,特别是对于需要HTTPS协议的API。
memory-bank/systemPatterns.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 系统模式
2
+
3
+ ## 系统架构
4
+ 本项目采用客户端-代理服务器-目标API的架构模式。
5
+ - **客户端**:发起HTTP请求到代理服务器。
6
+ - **代理服务器**:基于FastAPI构建,接收客户端请求,解析URL,重构目标URL,并将请求转发到实际的外部API。
7
+ - **目标API**:外部的HTTPS API服务。
8
+
9
+ ```mermaid
10
+ graph LR
11
+ Client -- HTTP Request --> ProxyServer
12
+ ProxyServer -- HTTPS Request --> TargetAPI
13
+ TargetAPI -- Response --> ProxyServer
14
+ ProxyServer -- Response --> Client
15
+ ```
16
+
17
+ ## 关键技术决策
18
+ - **Web框架**:选择FastAPI,因为它支持异步操作,性能高,并且易于构建API。
19
+ - **HTTP客户端**:使用`httpx`库进行HTTP请求转发。`httpx`支持异步请求,与FastAPI的异步特性兼容良好。
20
+ - **并发模型**:利用FastAPI的异步特性和Python的`asyncio`实现多线程(或更准确地说是协程)并发处理,以提高代理服务的吞吐量。
21
+ - **URL解析与重构**:代理服务器的核心逻辑在于解析传入的URL,提取出协议、域名和路径,然后重新构造目标API的URL。例如,将`/v1/https/open.bigmodel.cn/api/paas/v4/chat/completions`解析为`https://open.bigmodel.cn/api/paas/v4/chat/completions`。
22
+
23
+ ## 设计模式
24
+ - **代理模式 (Proxy Pattern)**:本项目是代理模式的典型应用,代理服务器作为客户端和目标API之间的中介。
25
+ - **URL路由 (URL Routing)**:FastAPI的路由机制用于匹配传入的URL路径,并将其分派到相应的处理函数。
26
+
27
+ ## 组件关系
28
+ - **FastAPI应用**:作为整个代理服务的入口点。
29
+ - **路由处理器**:定义了处理不同URL模式的函数,例如处理`/v1/{protocol}/{domain}/{path:path}`的通用代理路由。
30
+ - **HTTP转发逻辑**:在路由处理器内部,负责使用`httpx`构建并发送请求到目标API,并处理响应。
memory-bank/techContext.md ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 技术背景
2
+
3
+ ## 使用的技术
4
+ - **编程语言**:Python 3.12
5
+ - **Web框架**:FastAPI
6
+ - **ASGI服务器**:Uvicorn (用于运行FastAPI应用)
7
+ - **HTTP客户端**:httpx (用于异步请求转发)
8
+ - **环境管理**:Conda
9
+
10
+ ## 开发设置
11
+ - **Conda环境**:`any-api`。如果不存在,需要通过`conda create -n any-api python=3.12`创建。
12
+ - **环境激活**:`conda activate any-api`。
13
+ - **依赖安装**:通过`pip install -r requirements.txt`安装所有项目依赖。
14
+
15
+ ## 技术约束
16
+ - **URL解析规则**:代理URL必须遵循`/v1/{protocol}/{domain}/{path:path}`的格式,以便正确解析目标API的URL。
17
+ - **并发模型**:虽然FastAPI本身是异步的,但为了处理高并发请求,需要确保请求转发逻辑是非阻塞的,并充分利用`asyncio`的协程能力。
18
+ - **错误处理**:需要考虑代理过程中可能出现的网络错误、超时、目标API响应错误等情况,并进行适当的错误处理和响应。
19
+
20
+ ## 依赖项
21
+ 根据`requirements.txt`文件:
22
+ - `flask` (虽然项目使用FastAPI,但`requirements.txt`中包含Flask,可能用于其他部分或历史遗留,在FastAPI代理中不直接使用)
23
+ - `httpx`
24
+ - `fastapi`
25
+ - `uvicorn`
26
+ - `requests` (可能用于同步请求,但推荐在FastAPI中使用`httpx`进行异步请求)
27
+ - `python-dotenv` (用于加载环境变量)
proxyserver-fastapi.py CHANGED
@@ -4,12 +4,22 @@ import httpx
4
  import uvicorn
5
  import asyncio, os
6
  from dotenv import load_dotenv
 
 
7
 
8
  # 加载 .env 文件中的环境变量
9
  load_dotenv()
10
 
 
 
 
 
 
 
11
  app = FastAPI()
12
 
 
 
13
  # Placeholder for Airs Platform Token (replace with actual validation logic)
14
  AIRS_PLATFORM_TOKEN = os.getenv("AIRS_PLATFORM_TOKEN", "")
15
 
 
4
  import uvicorn
5
  import asyncio, os
6
  from dotenv import load_dotenv
7
+ from pathlib import Path
8
+ from global_state import set
9
 
10
  # 加载 .env 文件中的环境变量
11
  load_dotenv()
12
 
13
+ # 获取当前文件的父目录的绝对路径,即:project_root
14
+ parent_dir = Path(__file__).resolve().parent
15
+ set('project_root', parent_dir)
16
+
17
+ from routers.api_v1 import router as api_router_v1
18
+
19
  app = FastAPI()
20
 
21
+ app.include_router(api_router_v1, prefix="/v1", tags=["api_router_v1"])
22
+
23
  # Placeholder for Airs Platform Token (replace with actual validation logic)
24
  AIRS_PLATFORM_TOKEN = os.getenv("AIRS_PLATFORM_TOKEN", "")
25
 
routers/api_v1.py ADDED
File without changes