Files
vibemake-pcb-studio/backend/api/app/main.py
T
2026-11-05 10:26:56 +08:00

203 lines
15 KiB
Python

import asyncio,csv,hashlib,hmac,io,json,re,secrets,shutil,time,uuid
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI,Request,HTTPException,UploadFile,File,Form
from fastapi.responses import JSONResponse,StreamingResponse,FileResponse
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.trustedhost import TrustedHostMiddleware
from .config import Settings
from .store import Store,Conflict,dumps
from .engine import Engine
MAX_UPLOAD=64*1024*1024
ALLOWED_EXT={'.pdf','.png','.jpg','.jpeg','.webp','.step','.stp','.dxf','.obj','.zip','.json','.txt','.md','.csv','.xlsx','.epro2','.eprj3'}
ACTIONS={'create','message','select','advance','status','events','cancel','recover','review','accept'}
def create_app(settings=None,start_workers=True):
settings=settings or Settings.env();store=Store(settings.root);engine=Engine(settings,store);token=secrets.token_urlsafe(32)
@asynccontextmanager
async def lifespan(app):
import fcntl
lockfile=open(settings.root/'api.lock','a+')
try:fcntl.flock(lockfile,fcntl.LOCK_EX|fcntl.LOCK_NB)
except BlockingIOError:lockfile.close();raise RuntimeError('同一数据目录已有API实例运行')
if start_workers:engine.start()
try:yield
finally:engine.shutdown();lockfile.close()
app=FastAPI(title='AI PCB Service API',version='0.1.0',lifespan=lifespan)
app.state.engine=engine;app.state.store=store;app.state.settings=settings
app.add_middleware(CORSMiddleware,allow_origins=list(settings.origins),allow_credentials=True,allow_methods=['GET','POST'],allow_headers=['Content-Type','Last-Event-ID'])
extra_hosts=[h.strip() for h in __import__('os').environ.get('AI_PCB_ALLOWED_HOSTS','').split(',') if h.strip()]
app.add_middleware(TrustedHostMiddleware,allowed_hosts=['127.0.0.1','localhost','testserver',*extra_hosts])
@app.middleware('http')
async def protect(request,call_next):
if request.method=='OPTIONS':return await call_next(request)
origin=request.headers.get('origin')
if origin and origin not in settings.origins:return JSONResponse({'error':'ORIGIN_REJECTED','message':'请求来源不在本地允许列表'},403)
path=request.url.path
if path.startswith('/api/') and path not in {'/api/v1/session','/api/v1/health'}:
if not hmac.compare_digest(request.cookies.get('pcb_local_session',''),token):return JSONResponse({'error':'SESSION_REQUIRED','message':'请重新连接本地服务'},401)
if request.method=='POST' and path.startswith('/api/'):
if origin not in settings.origins:return JSONResponse({'error':'ORIGIN_REQUIRED','message':'写操作需要明确的允许来源'},403)
if request.headers.get('content-length','').isdigit() and int(request.headers['content-length'])>MAX_UPLOAD+1024*1024:
return JSONResponse({'error':'UPLOAD_TOO_LARGE','message':'上传超过64MiB限制'},413)
response=await call_next(request);response.headers['X-Content-Type-Options']='nosniff';return response
@app.exception_handler(HTTPException)
async def error(request,exc):return JSONResponse({'error':str(exc.status_code),'message':exc.detail},status_code=exc.status_code)
@app.get('/api/v1/health')
def health():return {'status':'ok','mode':settings.mode}
@app.get('/api/v1/session')
def session():
response=JSONResponse({'connected':True,'mode':settings.mode})
response.set_cookie('pcb_local_session',token,httponly=True,samesite='strict',max_age=86400)
return response
@app.get('/api/v1/capabilities')
def capabilities():
return {'protocol_version':'1','mode':settings.mode,'simulation':settings.mode=='simulation',
'actions':sorted(ACTIONS if settings.mode!='assessment' else {'create','message','status','events','cancel'}),
'uploads':{'max_file_bytes':MAX_UPLOAD,'max_files_per_command':32,'extensions':sorted(ALLOWED_EXT)},
'eda_execution_enabled':settings.mode=='real','credential_source':'private server DeepSeek profile / environment; never exposed to browser',
'models':{'selection':'server configuration','provider':'deepseek'},'eda_backend':'kicad',
'preview_formats':['image/png','image/jpeg','image/svg+xml','application/pdf','text/plain','text/csv']}
@app.post('/api/v1/commands',status_code=202,openapi_extra={'requestBody':{'required':True,'content':{'application/json':{'schema':{'type':'object','required':['request_id','action','payload'],'additionalProperties':False,'properties':{'request_id':{'type':'string','minLength':1,'maxLength':128},'action':{'type':'string','enum':sorted(ACTIONS)},'payload':{'type':'object'}}}}}}})
async def commands(request:Request):
raw=await request.body()
if len(raw)>1024*1024:raise HTTPException(413,'命令超过1MiB')
try:command=json.loads(raw)
except ValueError:raise HTTPException(422,'无效JSON')
if not isinstance(command,dict) or set(command)!={'request_id','action','payload'}:raise HTTPException(422,'需要request_id、action、payload')
rid,action,p=command['request_id'],command['action'],command['payload']
if not isinstance(rid,str) or not 1<=len(rid)<=128 or action not in ACTIONS or not isinstance(p,dict):raise HTTPException(422,'命令字段无效')
if settings.mode=='assessment' and (action not in {'create','message','status','events','cancel'} or p.get('selection_policy')=='auto'):raise HTTPException(403,'真实评估模式禁止自动选型执行和绘图;请使用propose')
tid=p.get('task_id')
if action!='create' and not store.task(tid):raise HTTPException(404,'此API实例中没有这个任务')
if action=='create' and not p.get('description') and not p.get('request'):raise HTTPException(422,'请填写需求描述或结构化需求')
if 'native_resolution' in p:raise HTTPException(403,'原生写入恢复只允许可信运维入口操作,网页不能自动确认编辑器空闲')
attachments=p.get('attachments',[])
if not isinstance(attachments,list) or len(attachments)>32:raise HTTPException(422,'附件数量无效')
translated=[]
for attachment in attachments:
if not isinstance(attachment,dict) or set(attachment)!={'upload_id'}:raise HTTPException(422,'网页附件只能使用upload_id,不能传服务器路径')
with store.db() as db:r=db.execute('SELECT * FROM uploads WHERE id=?',(attachment['upload_id'],)).fetchone()
if not r:raise HTTPException(404,'附件不存在')
if hashlib.sha256(Path(r['path']).read_bytes()).hexdigest()!=r['sha256']:raise HTTPException(409,'附件内容已变化')
translated.append({'path':r['path'],'kind':r['kind'],'description':r['name'],'source':'Local browser upload','units':'unknown'})
if attachments:command={**command,'payload':{**p,'attachments':translated}}
# The API never manufactures business approval; hashes still go through the core.
try:op,created=store.enqueue(command,tid)
except Conflict as exc:raise HTTPException(409,str(exc))
return {'operation_id':op['id'],'state':op['state'],'task_id':op['task_id'],'replayed':not created}
@app.get('/api/v1/operations/{oid}')
def operation(oid:str):
op=store.operation(oid)
if not op:raise HTTPException(404,'操作不存在')
result=json.loads(op['result']) if op['result'] else None
if result and result.get('task_id'):result=engine.public_task({**result,**(store.task(result['task_id']) or {})})
return {'operation_id':op['id'],'request_id':op['request_id'],'state':op['state'],'task_id':op['task_id'],'result':result,'created_at':op['created'],'updated_at':op['updated']}
@app.get('/api/v1/tasks')
def tasks():return {'items':[engine.public_task(t) for t in store.tasks()]}
@app.post('/api/v1/tasks/{tid}/history')
async def manage_history(tid:str,request:Request):
if not re.fullmatch(r'pcb-[a-z0-9]{12,32}',tid):raise HTTPException(422,'无效设计编号')
raw=await request.body()
if len(raw)>4096:raise HTTPException(413,'历史操作内容过长')
try:p=json.loads(raw)
except ValueError:raise HTTPException(422,'无效JSON')
if not isinstance(p,dict):raise HTTPException(422,'历史操作字段无效')
action=p.get('action')
allowed={'rename':{'action','title'},'pin':{'action','pinned'},'archive':{'action'}}
if not isinstance(action,str) or action not in allowed or set(p)!=allowed[action]:raise HTTPException(422,'历史操作字段无效')
title=p.get('title');pinned=p.get('pinned')
if action=='rename':
if not isinstance(title,str) or not 1<=len(title.strip())<=120:raise HTTPException(422,'名称须为1至120个字符')
title=title.strip()
if action=='pin' and not isinstance(pinned,bool):raise HTTPException(422,'置顶状态无效')
def cleanup():
parent=(settings.core_root/'tasks').resolve()
directory=parent/tid
if directory.is_symlink() or directory.resolve().parent!=parent:raise Conflict('设计目录不安全,无法删除')
if directory.exists():shutil.rmtree(directory)
try:await asyncio.to_thread(store.manage_history,tid,action,title=title,pinned=pinned,cleanup=cleanup if action=='archive' else None)
except KeyError:raise HTTPException(404,'设计不存在或已删除')
except Conflict as exc:raise HTTPException(409,str(exc))
except OSError:raise HTTPException(500,'工程文件删除失败,请稍后重试')
if action=='archive':return {'deleted':True,'task_id':tid}
data=store.task(tid)
if not data:raise HTTPException(404,'设计已删除')
return engine.public_task(data)
@app.get('/api/v1/tasks/{tid}')
async def task(tid:str):
data=store.task(tid)
if not data:raise HTTPException(404,'任务不存在')
try:
fresh=await asyncio.to_thread(engine.provider.call,{'request_id':'status-'+uuid.uuid4().hex,'action':'status','payload':{'task_id':tid}},None,15)
if fresh.get('task_id'):engine.update_task(fresh);data=store.task(tid)
except Exception:pass # Keep last durable snapshot; events/operation endpoint expose failures.
data=store.task(tid)
if not data:raise HTTPException(404,'设计已删除')
return engine.public_task(data)
@app.get('/api/v1/events')
async def events(request:Request,after:int=0,task_id:str|None=None):
if task_id and not store.task(task_id):raise HTTPException(404,'任务不存在')
try:cursor=max(after,int(request.headers.get('last-event-id','0')))
except ValueError:raise HTTPException(422,'无效事件游标')
async def stream():
nonlocal cursor
heartbeat=0
while not await request.is_disconnected():
rows=store.events(cursor,task_id)
for e in rows:
cursor=e['seq'];yield f'id: {cursor}\nevent: progress\ndata: {dumps(e)}\n\n'
heartbeat+=1
if heartbeat%20==0:yield ': heartbeat\n\n'
await asyncio.sleep(.5)
return StreamingResponse(stream(),media_type='text/event-stream',headers={'Cache-Control':'no-cache','X-Accel-Buffering':'no'})
@app.post('/api/v1/uploads',status_code=201)
async def upload(file:UploadFile=File(...),kind:str=Form('other')):
if kind not in {'datasheet','model_3d','symbol','footprint','mechanical','reference_design','other'}:raise HTTPException(422,'附件类型无效')
name=Path(file.filename or 'upload').name;suffix=Path(name).suffix.lower()
if suffix not in ALLOWED_EXT:raise HTTPException(422,'暂不支持此文件类型')
uid='upload-'+uuid.uuid4().hex;directory=settings.root/'uploads';directory.mkdir(exist_ok=True)
path=directory/(uid+suffix);size=0;h=hashlib.sha256()
try:
with path.open('xb') as out:
while chunk:=await file.read(1024*1024):
size+=len(chunk)
if size>MAX_UPLOAD:raise HTTPException(413,'单文件不能超过64MiB')
h.update(chunk);out.write(chunk)
if not size:raise HTTPException(422,'空文件不能上传')
with store.db() as db:db.execute('INSERT INTO uploads VALUES(?,?,?,?,?,?)',(uid,str(path),name,kind,h.hexdigest(),size))
except BaseException:path.unlink(missing_ok=True);raise
finally:await file.close()
return {'upload_id':uid,'name':name,'kind':kind,'byte_size':size,'sha256':h.hexdigest()}
@app.get('/api/v1/tasks/{tid}/artifacts')
def artifacts(tid:str):
if not store.task(tid):raise HTTPException(404,'任务不存在')
return {'items':engine.artifacts(tid)}
def authorized_file(tid,aid):
data=store.task(tid)
with store.db() as db:r=db.execute('SELECT * FROM artifacts WHERE id=? AND task_id=?',(aid,tid)).fetchone()
if not data or not r:raise HTTPException(404,'成果不存在')
if r['release_only'] and data['state']!='completed':raise HTTPException(403,'制造包尚未放行')
path=Path(r['path']).resolve()
if not path.is_relative_to(settings.core_root.resolve()) or not path.is_file():raise HTTPException(404,'成果路径不可用')
if hashlib.sha256(path.read_bytes()).hexdigest()!=r['sha256']:raise HTTPException(409,'成果摘要变化,拒绝提供旧修订文件')
return r,path
@app.get('/api/v1/tasks/{tid}/artifacts/{aid}')
def artifact(tid:str,aid:str,preview:bool=False):
r,path=authorized_file(tid,aid)
inline=preview and r['mime'] in {'image/png','image/jpeg','image/webp','image/svg+xml','application/pdf','text/plain'}
headers={'Content-Security-Policy':"sandbox; default-src 'none'; style-src 'unsafe-inline'"} if r['mime']=='image/svg+xml' else None
return FileResponse(path,filename=path.name,media_type=r['mime'],content_disposition_type='inline' if inline else 'attachment',headers=headers)
@app.get('/api/v1/tasks/{tid}/artifacts/{aid}/table')
def table(tid:str,aid:str):
r,path=authorized_file(tid,aid)
if r['kind']!='bom':raise HTTPException(422,'不是BOM文件')
raw=path.read_bytes()
if len(raw)>8*1024*1024:raise HTTPException(413,'BOM预览超过8MiB,请下载原件')
if path.suffix=='.json':return json.loads(raw)
text=raw.decode('utf-16') if raw.startswith((b'\xff\xfe',b'\xfe\xff')) else raw.decode('utf-8-sig')
rows=list(csv.reader(io.StringIO(text),delimiter='\t' if '\t' in text.splitlines()[0] else ','))
return {'columns':rows[0] if rows else [],'rows':rows[1:501],'truncated':len(rows)>501,'simulation':settings.mode=='simulation'}
return app