|
@@ -1,5 +1,5 @@
|
|
import os, json, time, asyncio
|
|
import os, json, time, asyncio
|
|
-from base_config import ai_key, path
|
|
|
|
|
|
+from base_config import ai_key, path, file_summary_app_id, commit_summary_app_id, filter_code_files_app_id, analysis_results_app_id
|
|
from fastapi import APIRouter, BackgroundTasks
|
|
from fastapi import APIRouter, BackgroundTasks
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
from pydantic import BaseModel
|
|
from pydantic import BaseModel
|
|
@@ -9,7 +9,8 @@ from http import HTTPStatus
|
|
from dashscope import Application
|
|
from dashscope import Application
|
|
|
|
|
|
from models.aiModels import Scan_Tasks, Commit_Summary_Tasks, File_Summary_Tasks
|
|
from models.aiModels import Scan_Tasks, Commit_Summary_Tasks, File_Summary_Tasks
|
|
-from models.gitModels import Repos
|
|
|
|
|
|
+
|
|
|
|
+from mcp_.client import MCPClient
|
|
|
|
|
|
airouter = APIRouter()
|
|
airouter = APIRouter()
|
|
class RequestCommit(BaseModel):
|
|
class RequestCommit(BaseModel):
|
|
@@ -28,6 +29,10 @@ class RequestFile(BaseModel):
|
|
repo_url: str
|
|
repo_url: str
|
|
file_path: str
|
|
file_path: str
|
|
|
|
|
|
|
|
+class RequestChat(BaseModel):
|
|
|
|
+ uuid: str
|
|
|
|
+ message: str
|
|
|
|
+
|
|
def generate_repo_path(uuid, repo_url):
|
|
def generate_repo_path(uuid, repo_url):
|
|
repo_name = repo_url.split("/")[-1].replace(".git", "")
|
|
repo_name = repo_url.split("/")[-1].replace(".git", "")
|
|
base_path = os.path.join(path, uuid)
|
|
base_path = os.path.join(path, uuid)
|
|
@@ -35,9 +40,8 @@ def generate_repo_path(uuid, repo_url):
|
|
|
|
|
|
async def file_summary(content):
|
|
async def file_summary(content):
|
|
response = Application.call(
|
|
response = Application.call(
|
|
- # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
|
|
api_key=ai_key,
|
|
api_key=ai_key,
|
|
- app_id='ef50d70cd4074a899a09875e6a6e36ea',
|
|
|
|
|
|
+ app_id=file_summary_app_id,
|
|
prompt=content)
|
|
prompt=content)
|
|
if response.status_code == HTTPStatus.OK:
|
|
if response.status_code == HTTPStatus.OK:
|
|
try:
|
|
try:
|
|
@@ -45,6 +49,7 @@ async def file_summary(content):
|
|
print(json_data)
|
|
print(json_data)
|
|
except json.JSONDecodeError:
|
|
except json.JSONDecodeError:
|
|
print("返回内容不是有效的 JSON 格式!")
|
|
print("返回内容不是有效的 JSON 格式!")
|
|
|
|
+ print(response.output.text)
|
|
json_data = {"summary": []}
|
|
json_data = {"summary": []}
|
|
else:
|
|
else:
|
|
print(f"请求失败: {response.message}")
|
|
print(f"请求失败: {response.message}")
|
|
@@ -53,9 +58,8 @@ async def file_summary(content):
|
|
|
|
|
|
async def commit_summary(content):
|
|
async def commit_summary(content):
|
|
response = Application.call(
|
|
response = Application.call(
|
|
- # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
|
|
api_key=ai_key,
|
|
api_key=ai_key,
|
|
- app_id='88426cc2301b44bea5d28d41d187ebf2',
|
|
|
|
|
|
+ app_id=commit_summary_app_id,
|
|
prompt=content)
|
|
prompt=content)
|
|
if response.status_code == HTTPStatus.OK:
|
|
if response.status_code == HTTPStatus.OK:
|
|
try:
|
|
try:
|
|
@@ -63,6 +67,7 @@ async def commit_summary(content):
|
|
print(json_data)
|
|
print(json_data)
|
|
except json.JSONDecodeError:
|
|
except json.JSONDecodeError:
|
|
print("返回内容不是有效的 JSON 格式!")
|
|
print("返回内容不是有效的 JSON 格式!")
|
|
|
|
+ print(response.output.text)
|
|
json_data = {"null": []}
|
|
json_data = {"null": []}
|
|
else:
|
|
else:
|
|
print(f"请求失败: {response.message}")
|
|
print(f"请求失败: {response.message}")
|
|
@@ -71,9 +76,8 @@ async def commit_summary(content):
|
|
|
|
|
|
def filter_code_files(prompt):
|
|
def filter_code_files(prompt):
|
|
response = Application.call(
|
|
response = Application.call(
|
|
- # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
|
|
api_key=ai_key,
|
|
api_key=ai_key,
|
|
- app_id='b0725a23eafd4422bfa7d5eff278af7c',
|
|
|
|
|
|
+ app_id=filter_code_files_app_id,
|
|
prompt=prompt)
|
|
prompt=prompt)
|
|
if response.status_code == HTTPStatus.OK:
|
|
if response.status_code == HTTPStatus.OK:
|
|
try:
|
|
try:
|
|
@@ -81,6 +85,7 @@ def filter_code_files(prompt):
|
|
print(json_data)
|
|
print(json_data)
|
|
except json.JSONDecodeError:
|
|
except json.JSONDecodeError:
|
|
print("返回内容不是有效的 JSON 格式!")
|
|
print("返回内容不是有效的 JSON 格式!")
|
|
|
|
+ print(response.output.text)
|
|
json_data={"files":[]}
|
|
json_data={"files":[]}
|
|
else:
|
|
else:
|
|
print(f"请求失败: {response.message}")
|
|
print(f"请求失败: {response.message}")
|
|
@@ -94,11 +99,9 @@ def analysis_results(local_path,path):
|
|
for line_num, line in enumerate(f, start=1):
|
|
for line_num, line in enumerate(f, start=1):
|
|
prompt+=f"{line_num}\t{line}"
|
|
prompt+=f"{line_num}\t{line}"
|
|
response = Application.call(
|
|
response = Application.call(
|
|
- # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
|
|
api_key=ai_key,
|
|
api_key=ai_key,
|
|
- app_id='b6edb4f5ff1c49f9855af27b14a0e8b4', # 替换为实际的应用 ID
|
|
|
|
|
|
+ app_id=analysis_results_app_id,
|
|
prompt=prompt)
|
|
prompt=prompt)
|
|
-
|
|
|
|
if response.status_code == HTTPStatus.OK:
|
|
if response.status_code == HTTPStatus.OK:
|
|
try:
|
|
try:
|
|
json_data = json.loads(response.output.text)
|
|
json_data = json.loads(response.output.text)
|
|
@@ -110,7 +113,7 @@ def analysis_results(local_path,path):
|
|
else:
|
|
else:
|
|
print(f"请求失败: {response.message}")
|
|
print(f"请求失败: {response.message}")
|
|
json_data = {"summary":None}
|
|
json_data = {"summary":None}
|
|
- json_data["path"]=file_path
|
|
|
|
|
|
+ json_data["path"]=path
|
|
|
|
|
|
return json_data
|
|
return json_data
|
|
|
|
|
|
@@ -146,7 +149,6 @@ async def get_code_files(path):
|
|
chunks = [files[i * 500: (i + 1) * 500]
|
|
chunks = [files[i * 500: (i + 1) * 500]
|
|
for i in range(0, len(files) // 500 + 1)]
|
|
for i in range(0, len(files) // 500 + 1)]
|
|
# 提交所有批次任务
|
|
# 提交所有批次任务
|
|
- # futures = [executor.submit(process_batch1, chunk) for chunk in chunks]
|
|
|
|
tasks = [process_batch1(chunk) for chunk in chunks]
|
|
tasks = [process_batch1(chunk) for chunk in chunks]
|
|
futures = await asyncio.gather(*tasks, return_exceptions=True)
|
|
futures = await asyncio.gather(*tasks, return_exceptions=True)
|
|
# 实时获取已完成任务的结果
|
|
# 实时获取已完成任务的结果
|
|
@@ -171,9 +173,23 @@ async def process_batch2(local_path,path):
|
|
|
|
|
|
async def analysis(local_path, task_id):
|
|
async def analysis(local_path, task_id):
|
|
file_list = await get_code_files(local_path)
|
|
file_list = await get_code_files(local_path)
|
|
- print(file_list)
|
|
|
|
|
|
+ all_extensions = [
|
|
|
|
+ "adoc", "asm", "awk", "bas", "bat", "bib", "c", "cbl", "cls", "clj",
|
|
|
|
+ "cljc", "cljs", "cmd", "conf", "cpp", "cr", "cs", "css", "cxx", "dart",
|
|
|
|
+ "dockerfile", "edn", "el", "env", "erl", "ex", "exs", "f", "f90", "f95",
|
|
|
|
+ "fs", "fsscript", "fsi", "fsx", "g4", "gd", "gql", "graphql", "groovy",
|
|
|
|
+ "gsh", "gvy", "h", "hbs", "hcl", "hh", "hl", "hpp", "hrl", "hs", "htm",
|
|
|
|
+ "html", "hx", "ini", "jad", "jade", "java", "jl", "js", "json", "json5",
|
|
|
|
+ "jsx", "kt", "kts", "less", "lfe", "lgt", "lhs", "log", "ltx", "lua",
|
|
|
|
+ "m", "mjs", "ml", "mli", "mm", "nim", "nims", "nlogo", "pas", "php",
|
|
|
|
+ "pl", "plantuml", "pro", "ps1", "pug", "puml", "py", "qml", "r", "rb",
|
|
|
|
+ "re", "rei", "res", "resi", "rkt", "rs", "rst", "s", "sass", "scala",
|
|
|
|
+ "scm", "scss", "sed", "sh", "sol", "sql", "ss", "st", "squeak", "swift",
|
|
|
|
+ "tcl", "tex", "tf", "tfvars", "toml", "ts", "tsx", "txt", "v", "vb",
|
|
|
|
+ "vbs", "vh", "vhd", "vhdl", "vim", "vue", "xml", "yaml", "yang", "yml"]
|
|
|
|
+ file_list = [file for file in file_list if file.split(".")[-1] in all_extensions]
|
|
results = []
|
|
results = []
|
|
- tasks = [process_batch2(local_path, file) for file in file_list] # 假设process_batch2已改为异步函数
|
|
|
|
|
|
+ tasks = [process_batch2(local_path, file) for file in file_list]
|
|
batch_results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
batch_results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
|
|
|
|
for result in batch_results:
|
|
for result in batch_results:
|
|
@@ -223,7 +239,6 @@ async def summaryCommit(request: RequestCommit, background_tasks: BackgroundTask
|
|
repo_commit_hash=repo_commit.repo_hash
|
|
repo_commit_hash=repo_commit.repo_hash
|
|
print(f"开始提交仓库: {repo_name}")
|
|
print(f"开始提交仓库: {repo_name}")
|
|
await Commit_Summary_Tasks.filter(id=request.task_id).update(start_time=int(time.time() * 1000))
|
|
await Commit_Summary_Tasks.filter(id=request.task_id).update(start_time=int(time.time() * 1000))
|
|
- # commit_content = Repo(local_path).git.log('-1', '-p', '--pretty=format:%h %s')
|
|
|
|
commit_content = Repo(local_path).git.diff(f"{repo_commit_hash}^", repo_commit_hash)
|
|
commit_content = Repo(local_path).git.diff(f"{repo_commit_hash}^", repo_commit_hash)
|
|
background_tasks.add_task(commit_task,commit_content, request.task_id)
|
|
background_tasks.add_task(commit_task,commit_content, request.task_id)
|
|
return {"code": 200, "msg": "添加提交任务成功"}
|
|
return {"code": 200, "msg": "添加提交任务成功"}
|
|
@@ -234,6 +249,14 @@ async def summaryFile(request: RequestFile,background_tasks: BackgroundTasks):
|
|
background_tasks.add_task(file_task, request.file_path, request.task_id)
|
|
background_tasks.add_task(file_task, request.file_path, request.task_id)
|
|
return {"code": 200, "msg": "添加提交任务成功"}
|
|
return {"code": 200, "msg": "添加提交任务成功"}
|
|
|
|
|
|
|
|
+@airouter.post("/chat")
|
|
|
|
+async def chat(request: RequestChat):
|
|
|
|
+ client = MCPClient(request.uuid)
|
|
|
|
+ await client.connect_to_server()
|
|
|
|
+ response = await client.process_query(request.message)
|
|
|
|
+ await client.cleanup()
|
|
|
|
+ return {"code": 200, "msg": response}
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|