-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_server.py
More file actions
1177 lines (1000 loc) · 48.7 KB
/
Copy pathapi_server.py
File metadata and controls
1177 lines (1000 loc) · 48.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import urllib.request
from pathlib import Path
from fastapi import FastAPI, HTTPException, Depends, Request
from fastapi.responses import JSONResponse
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
# --- 新增这两行 ---
from dotenv import load_dotenv
load_dotenv() # 自动读取同目录下的 .env 文件
# ------------------
from supabase import create_client, Client
from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime, timezone
import logging
import time
import asyncio
from concurrent.futures import ThreadPoolExecutor
import jwt
from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
# --- 1. 初始化与配置 ---
# 配置日志记录,方便我们调试
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Thread pool for running sync I/O (file writes, storage uploads) without blocking the event loop
_io_executor = ThreadPoolExecutor(max_workers=4)
# Worker health check URL (for admin status endpoint).
# Docker Compose: http://worker:8001/health; bare metal: http://localhost:8001/health
WORKER_HEALTH_URL = os.environ.get("WORKER_HEALTH_URL", "http://localhost:8001/health")
# --- User quota configuration ---
USER_STORAGE_LIMIT_BYTES = int(os.environ.get("USER_STORAGE_LIMIT_MB", "2048")) * 1024 * 1024 # default 2 GB
USER_DAILY_TASK_LIMIT = int(os.environ.get("USER_DAILY_TASK_LIMIT", "10")) # default 10 tasks/day
# Claude (claude-bridge) per-model daily limits — applies ONLY to
# llm_config.model_provider == 'claude-bridge' jobs; GPT/codex jobs are
# unaffected (they only fall under the general USER_DAILY_TASK_LIMIT above).
CLAUDE_OPUS_DAILY_LIMIT = int(os.environ.get("CLAUDE_OPUS_DAILY_LIMIT", "2"))
CLAUDE_SONNET_DAILY_LIMIT = int(os.environ.get("CLAUDE_SONNET_DAILY_LIMIT", "5"))
# Admin emails exempt from daily task limit (comma-separated)
_QUOTA_EXEMPT_EMAILS = set(
e.strip().lower() for e in os.environ.get("QUOTA_EXEMPT_EMAILS", "").split(",") if e.strip()
)
# Tasks that fail with one of these `error_category` values are refunded:
# they don't count toward the user's daily quota because the failure was on
# the platform side, not the user's prompt or BYOK credentials. Driven by the
# 2026-04-09 incident where a stale platform Codex token wedged user jobs.
PLATFORM_REFUND_CATEGORIES = [
'auth_error_platform', 'codex_quota_exceeded',
# Added 2026-08-14 after the one-month case review: transient LLM-gateway
# 5xx and empty/truncated streamed responses killed 7 jobs that Foam-Agent
# refuses to retry. The prompt was fine — don't bill the user's daily quota
# for our provider's hiccup. See docs/active/case-review-2026-08-14.md.
'llm_upstream_error', 'llm_response_error',
]
# --- Foam-Agent 目录配置 ---
FOAM_AGENT_DIR = os.environ.get("FOAM_AGENT_DIR")
if not FOAM_AGENT_DIR:
logger.error("FATAL: FOAM_AGENT_DIR is not set. Set it in .env to point to the Foam-Agent directory.")
raise RuntimeError("FOAM_AGENT_DIR is not set in the environment variables.")
FOAM_AGENT_DIR = os.path.abspath(FOAM_AGENT_DIR)
logger.info(f"FOAM_AGENT_DIR resolved to: {FOAM_AGENT_DIR}")
# 从我们之前在 Part A 设置的环境变量中加载 Supabase 的配置
SUPABASE_URL = os.environ.get("SUPABASE_URL")
SUPABASE_SERVICE_KEY = os.environ.get("SUPABASE_SERVICE_KEY")
# 检查环境变量是否已设置,如果缺失则程序无法运行
if not SUPABASE_URL or not SUPABASE_SERVICE_KEY:
logger.error("FATAL: Supabase credentials are not set in the environment variables.")
raise RuntimeError("Supabase credentials are not set in the environment variables.")
# 创建 Supabase 客户端实例
# 注意:在后端,我们使用权限更高的 service_role key
# 因为 API 服务器需要有权限无视 RLS 策略来写入数据
logger.info("Initializing Supabase client...")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
logger.info("Supabase client initialized successfully.")
# --- JWT Authentication ---
SUPABASE_JWT_SECRET = os.environ.get("SUPABASE_JWT_SECRET")
if not SUPABASE_JWT_SECRET:
logger.warning("SUPABASE_JWT_SECRET is not set. POST endpoints will reject all requests.")
security = HTTPBearer()
async def verify_jwt(credentials: HTTPAuthorizationCredentials = Depends(security)) -> str:
"""
Verify Supabase JWT from Authorization header.
Returns the authenticated user_id (from token's 'sub' claim).
"""
if not SUPABASE_JWT_SECRET:
raise HTTPException(
status_code=500,
detail="JWT verification is not configured (SUPABASE_JWT_SECRET missing)"
)
token = credentials.credentials
try:
payload = jwt.decode(
token,
SUPABASE_JWT_SECRET,
algorithms=["HS256"],
audience="authenticated"
)
user_id = payload.get("sub")
if not user_id:
raise HTTPException(status_code=401, detail="Invalid token: missing user ID")
return user_id
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token has expired")
except jwt.InvalidTokenError as e:
raise HTTPException(status_code=401, detail=f"Invalid token: {str(e)}")
# 创建 FastAPI 应用实例
app = FastAPI()
# --- Rate Limiting ---
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# --- 在这里添加 CORS 中间件 ---
# 1. 定义一个 "白名单" 列表,包含所有我们允许的来源
# 生产/本地域名写在这里;额外来源可通过环境变量 EXTRA_CORS_ORIGINS 添加(逗号分隔)
_origins_base = [
"https://foam-agent.com",
"https://www.foam-agent.com",
"https://cfdqanda.com",
"https://www.cfdqanda.com",
"http://localhost:5173",
"http://127.0.0.1:5173",
]
_extra = os.environ.get("EXTRA_CORS_ORIGINS", "")
origins = _origins_base + [o.strip() for o in _extra.split(",") if o.strip()]
# 2. 将 CORS 中间件添加到我们的 FastAPI 应用中
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # 允许 "白名单" 中的来源
allow_credentials=True, # 允许携带 cookie
allow_methods=["GET", "POST", "PATCH", "DELETE", "OPTIONS"], # Only methods our API uses
allow_headers=["Content-Type", "Authorization"], # JSON body + JWT Bearer token
)
# ----------------------------------
# --- 2. 定义数据模型 ---
# LLM 配置模型 — 通用设计,不与特定 Agent 耦合
class LLMConfig(BaseModel):
model_provider: Optional[str] = None # e.g. "openai", "openai-codex", "anthropic", "qwen", "deepseek", "claude-bridge" (subscription Claude via bridge-equipped workers)
model_version: Optional[str] = None # e.g. "gpt-5.6-sol", "gpt-5.5", "claude-sonnet-4-6"
api_key: Optional[str] = None # User-provided API key (for openai/anthropic/qwen/deepseek providers)
codex_token: Optional[str] = None # ChatGPT/Codex OAuth access token (for openai-codex provider)
base_url: Optional[str] = None # Custom OpenAI-compatible endpoint (Qwen, DeepSeek, self-hosted, etc.)
# 使用 Pydantic 定义前端发送过来的请求体(body)应该长什么样
# 这可以提供自动的数据验证和生成 API 文档
# Note: user_id is no longer in the body — it comes from JWT (verify_jwt dependency)
MESH_FILE_MAX_BYTES = 100 * 1024 * 1024 # 100 MB
class MeshFileInfo(BaseModel):
storage_path: str
original_name: str
size_bytes: int
class SimulationRequest(BaseModel):
prompt: str
llm_config: Optional[LLMConfig] = None # 用户可选的 LLM 配置
pre_run_end_time: Optional[int] = None # Pre-run timesteps: None=default(10), -1=disabled, positive=custom
pipeline_mode: str = 'auto' # 'auto' (subprocess one-shot) or 'controlled' (MCP stage-by-stage)
checkpoints: Optional[List[str]] = None # Active checkpoints: ['files_review', 'pre_run_review', 'plan_review']
mesh_file: Optional[MeshFileInfo] = None # Optional custom mesh (.msh) uploaded to Supabase Storage
timeout_minutes: Optional[int] = None # Custom timeout in minutes (default: 60)
class FeedbackRequest(BaseModel):
file_path: str # 文件路径,如 "output/log.blockMesh"
feedback_content: str # 反馈内容
class RatingRequest(BaseModel):
rating: int # 1=success, 2=partial success, 3=failed
comment: Optional[str] = None
stage: Optional[str] = None # pipeline stage (e.g. 'files_review', 'pre_run_review', 'completed')
# --- 3. 创建 API 端点 (Endpoint) ---
@app.post("/api/v1/simulations")
@limiter.limit("5/minute")
async def create_simulation_task(request: Request, sim_request: SimulationRequest, user_id: str = Depends(verify_jwt)):
"""
接收一个新的仿真请求,并将其作为任务插入数据库,状态为 'queued'
Requires a valid Supabase JWT in the Authorization header.
"""
logger.info(f"Received new simulation request for user: {user_id}")
try:
# --- Quota checks ---
# 1. Daily task limit (count tasks created today by this user)
# Check if user is exempt (admin/test accounts)
is_exempt = False
if _QUOTA_EXEMPT_EMAILS:
try:
auth_header = request.headers.get("authorization", "")
if auth_header.startswith("Bearer "):
token_payload = jwt.decode(
auth_header[7:], SUPABASE_JWT_SECRET,
algorithms=["HS256"], audience="authenticated"
)
user_email = token_payload.get("email", "").lower()
is_exempt = user_email in _QUOTA_EXEMPT_EMAILS
except Exception:
pass # if decode fails, not exempt
if not is_exempt:
quota = _count_today_billable(user_id)
if quota['billable'] >= USER_DAILY_TASK_LIMIT:
logger.warning(
f"User {user_id} hit daily task limit "
f"(billable {quota['billable']}/{USER_DAILY_TASK_LIMIT}, "
f"total {quota['total']}, refunded {quota['refunded']})"
)
raise HTTPException(
status_code=429,
detail=f"Daily task limit reached ({USER_DAILY_TASK_LIMIT} tasks/day). Please try again tomorrow."
)
# 1b. Claude (claude-bridge) per-model daily limit — Claude jobs only,
# GPT/codex jobs skip this entirely. Exempt emails reuse is_exempt above.
if (not is_exempt and sim_request.llm_config
and sim_request.llm_config.model_provider == 'claude-bridge'):
req_model = (sim_request.llm_config.model_version or 'opus').lower()
bucket = 'opus' if 'opus' in req_model else 'sonnet' # haiku/other → sonnet bucket
limit = CLAUDE_OPUS_DAILY_LIMIT if bucket == 'opus' else CLAUDE_SONNET_DAILY_LIMIT
today_start = datetime.now(timezone.utc).replace(
hour=0, minute=0, second=0, microsecond=0
).isoformat()
# Few rows per user/day — fetch and bucket in Python (keep it simple)
rows = (
supabase.table('simulations')
.select('llm_config')
.eq('user_id', user_id)
.gte('created_at', today_start)
.eq('llm_config->>model_provider', 'claude-bridge')
.execute()
).data or []
used = 0
for r in rows:
m = ((r.get('llm_config') or {}).get('model_version') or 'opus').lower()
if ('opus' if 'opus' in m else 'sonnet') == bucket:
used += 1
if used >= limit:
logger.warning(
f"User {user_id} hit Claude {bucket} daily limit ({used}/{limit})"
)
raise HTTPException(
status_code=429,
detail=f"Daily Claude {bucket} limit reached ({limit}/day). "
f"Please try again tomorrow or choose another model."
)
# 2. Storage quota (reuse cached storage calculation)
storage_total = _get_user_storage_bytes(user_id)
if storage_total >= USER_STORAGE_LIMIT_BYTES:
logger.warning(f"User {user_id} exceeded storage quota ({_format_bytes(storage_total)} / {_format_bytes(USER_STORAGE_LIMIT_BYTES)})")
raise HTTPException(
status_code=403,
detail=f"Storage quota exceeded ({_format_bytes(storage_total)} / {_format_bytes(USER_STORAGE_LIMIT_BYTES)}). "
f"Please delete old tasks to free space."
)
# Validate mesh file if provided
if sim_request.mesh_file:
if not sim_request.mesh_file.original_name.lower().endswith('.msh'):
raise HTTPException(status_code=400, detail="Only .msh mesh files are supported")
if sim_request.mesh_file.size_bytes > MESH_FILE_MAX_BYTES:
raise HTTPException(
status_code=400,
detail=f"Mesh file exceeds {MESH_FILE_MAX_BYTES // (1024*1024)} MB limit"
)
# 构建插入数据
insert_data = {
'prompt': sim_request.prompt,
'user_id': user_id,
'status': 'queued' # 将初始状态明确设置为 '排队中'
}
# 如果用户提供了 LLM 配置,写入 llm_config JSONB 列
if sim_request.llm_config:
insert_data['llm_config'] = sim_request.llm_config.model_dump(exclude_none=True)
# Pre-run end time for checkpoint mechanism
if sim_request.pre_run_end_time is not None:
insert_data['pre_run_end_time'] = sim_request.pre_run_end_time
# Pipeline mode: 'auto' (default) or 'controlled' (MCP stage-by-stage)
if sim_request.pipeline_mode != 'auto':
insert_data['pipeline_mode'] = sim_request.pipeline_mode
if sim_request.checkpoints:
insert_data['pipeline_state'] = {
'active_checkpoints': sim_request.checkpoints
}
# Custom mesh file reference
if sim_request.mesh_file:
insert_data['mesh_file'] = sim_request.mesh_file.model_dump()
# Custom timeout
if sim_request.timeout_minutes is not None:
if sim_request.timeout_minutes not in (20, 40, 60, 120):
raise HTTPException(status_code=400, detail="Invalid timeout. Choose 20, 40, 60, or 120 minutes.")
insert_data['timeout_minutes'] = sim_request.timeout_minutes
# 将新任务插入到 'simulations' 表中
response = supabase.table('simulations').insert(insert_data).execute()
# 检查 Supabase 的响应,看是否有数据被返回
if response.data:
new_task = response.data[0]
logger.info(f"Successfully queued task {new_task['id']} for user {user_id}")
# 将新创建的任务记录返回给前端,这是一个好的实践
return {"status": "success", "message": "Simulation task queued successfully.", "task": new_task}
else:
# 如果 Supabase 返回了错误(即使没有抛出异常)
error_message = response.error.message if response.error else "Unknown error from Supabase"
logger.error(f"Failed to insert task into database: {error_message}")
raise HTTPException(status_code=500, detail=f"Failed to insert task into database: {error_message}")
except HTTPException:
raise
except Exception as e:
logger.error(f"An unexpected error occurred: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"An internal server error occurred: {str(e)}")
# --- 4. (可选) 创建一个根端点用于测试 ---
@app.get("/")
def read_root():
"""
一个简单的"健康检查"端点,用于确认服务器是否正在运行。
"""
return {"message": "Foam-Agent API Server is running!"}
# --- Daily usage endpoint ---
@app.get("/api/v1/user/daily-usage")
async def get_daily_usage(request: Request, user_id: str = Depends(verify_jwt)):
"""Return the user's daily task usage and limit."""
try:
# Check if user is exempt
is_exempt = False
if _QUOTA_EXEMPT_EMAILS:
try:
auth_header = request.headers.get("authorization", "")
if auth_header.startswith("Bearer "):
token_payload = jwt.decode(
auth_header[7:], SUPABASE_JWT_SECRET,
algorithms=["HS256"], audience="authenticated"
)
is_exempt = token_payload.get("email", "").lower() in _QUOTA_EXEMPT_EMAILS
except Exception:
pass
quota = _count_today_billable(user_id)
effective_limit = 999999 if is_exempt else USER_DAILY_TASK_LIMIT
return {
"used": quota['billable'],
"limit": effective_limit,
"remaining": max(0, effective_limit - quota['billable']),
"exempt": is_exempt,
# Transparency fields: show the user when platform-side failures
# have been refunded (used = total_today - refunded_platform_failures).
"total_today": quota['total'],
"refunded_platform_failures": quota['refunded'],
}
except Exception as e:
logger.error(f"Failed to fetch daily usage: {e}")
raise HTTPException(status_code=500, detail="Failed to fetch daily usage")
# --- Queue status endpoint ---
@app.get("/api/v1/queue-status")
async def get_queue_status():
"""Return current queue depth and ordered list of queued job IDs.
Used by the frontend to show queue position for waiting tasks.
No auth required — only returns job IDs and counts, no sensitive data.
"""
try:
queued = supabase.table('simulations') \
.select('id, created_at') \
.eq('status', 'queued') \
.is_('deleted_at', None) \
.order('created_at', desc=False) \
.execute()
running = supabase.table('simulations') \
.select('id') \
.eq('status', 'running') \
.is_('deleted_at', None) \
.execute()
return {
"queued_count": len(queued.data),
"running_count": len(running.data),
"queued_ids": [str(j["id"]) for j in queued.data],
}
except Exception as e:
logger.error(f"Failed to fetch queue status: {e}")
raise HTTPException(status_code=500, detail="Failed to fetch queue status")
# --- 5. 文件浏览相关端点(可选,用于前端快速获取文件树)---
@app.get("/api/v1/simulations/{job_id}/files")
async def get_file_tree(job_id: int):
"""
获取任务的文件树结构。
这个端点是可选的,因为前端可以直接使用Supabase Storage的list() API。
但提供这个端点可以让前端更快地获取文件树结构(无需遍历Storage)。
"""
try:
# 从数据库查询任务信息
response = supabase.table('simulations').select('*').eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
# Check if task has files to browse
if job['status'] not in ['completed', 'failed', 'checkpoint', 'cancelled']:
raise HTTPException(
status_code=400,
detail=f"Simulation {job_id} has no files yet. Current status: {job['status']}"
)
# 从result_data中获取文件树
result_data = job.get('result_data', {})
if 'file_tree' not in result_data:
# 如果文件树不存在,返回错误
raise HTTPException(
status_code=404,
detail=f"File tree not found for simulation {job_id}. This might be an old task."
)
# 返回文件树和存储路径信息
return {
"job_id": job_id,
"status": job['status'],
"storage_base_path": result_data.get('storage_base_path'),
"file_tree": result_data.get('file_tree'),
"upload_stats": result_data.get('upload_stats', {})
}
except HTTPException:
raise
except Exception as e:
logger.error(f"An error occurred while getting file tree for job {job_id}: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"An internal server error occurred: {str(e)}")
# --- 修改 api_server.py 中的 submit_feedback 函数 ---
@app.post("/api/v1/simulations/{job_id}/feedback")
@limiter.limit("10/minute")
async def submit_feedback(request: Request, job_id: int, fb_request: FeedbackRequest, user_id: str = Depends(verify_jwt)):
"""
提交文件反馈。
Requires a valid Supabase JWT in the Authorization header.
1. [新增] 保存到本地 WSL 文件系统 (Foam-Agent/runs/{job_id}/...)
2. 上传到 Supabase Storage (云端备份)
"""
try:
# 1. 验证任务是否存在(只查必要字段,避免传输大量 result_data)
response = supabase.table('simulations').select('id, user_id').eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
# 2. 验证权限 (user_id from JWT, not from request body)
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
# 3. 验证大小 (限制 5KB)
feedback_size = len(fb_request.feedback_content.encode('utf-8'))
if feedback_size > 5120 or feedback_size == 0:
raise HTTPException(status_code=400, detail="Invalid feedback size")
# 4. 构建文件名
# 逻辑:原文件 "output/log.blockMesh" -> 反馈文件 "output/log.blockMesh_feedback"
feedback_file_path = f"{fb_request.file_path}_feedback"
# 5. Path traversal protection: ensure the resolved path stays within runs/{job_id}/
job_base_dir = Path(FOAM_AGENT_DIR, "runs", str(job_id)).resolve()
resolved_feedback_path = (job_base_dir / feedback_file_path).resolve()
if not str(resolved_feedback_path).startswith(str(job_base_dir) + os.sep) and resolved_feedback_path != job_base_dir:
logger.warning(f"Path traversal attempt blocked: file_path='{fb_request.file_path}' resolved to '{resolved_feedback_path}'")
raise HTTPException(status_code=400, detail="Invalid file_path: path traversal is not allowed")
# ==========================================
# 并行执行:本地写入 + 云端上传(避免串行阻塞)
# ==========================================
local_file_path = str(resolved_feedback_path)
storage_base_path = f"public/{user_id}/{job_id}"
storage_feedback_path = f"{storage_base_path}/{feedback_file_path}"
loop = asyncio.get_event_loop()
def _write_local():
os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
with open(local_file_path, "w", encoding="utf-8") as f:
f.write(fb_request.feedback_content)
def _upload_cloud():
supabase.storage.from_("simulation_results").upload(
path=storage_feedback_path,
file=fb_request.feedback_content.encode('utf-8'),
file_options={"content-type": "text/plain", "upsert": "true"}
)
local_task = loop.run_in_executor(_io_executor, _write_local)
cloud_task = loop.run_in_executor(_io_executor, _upload_cloud)
# 等待两个任务完成,收集结果
results = await asyncio.gather(local_task, cloud_task, return_exceptions=True)
# 处理本地写入结果(失败不中断)
if isinstance(results[0], Exception):
logger.error(f"Failed to write local feedback file: {results[0]}")
else:
logger.info(f"Feedback saved locally to: {local_file_path}")
# 处理云端上传结果(失败则报错)
if isinstance(results[1], Exception):
logger.error(f"Storage upload failed: {results[1]}")
raise HTTPException(status_code=500, detail=f"Storage upload failed: {str(results[1])}")
else:
logger.info(f"Feedback uploaded to Supabase: {storage_feedback_path}")
return {
"status": "success",
"message": "Feedback submitted successfully (Local & Cloud)",
"local_path": local_file_path,
"cloud_path": storage_feedback_path
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in submit_feedback: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
# --- 6. Task-level rating endpoint ---
@app.patch("/api/v1/simulations/{job_id}/rating")
@limiter.limit("10/minute")
async def submit_rating(request: Request, job_id: str, rating_request: RatingRequest, user_id: str = Depends(verify_jwt)):
"""
Submit a task-level rating (1=success, 2=partial, 3=failed) with optional comment.
Requires a valid Supabase JWT. Only the task owner can rate.
"""
if rating_request.rating not in (1, 2, 3):
raise HTTPException(status_code=400, detail="Rating must be 1 (success), 2 (partial), or 3 (failed)")
if rating_request.comment and len(rating_request.comment) > 500:
raise HTTPException(status_code=400, detail="Comment must be 500 characters or less")
try:
response = supabase.table('simulations').select(
'id, user_id, stage_ratings, pipeline_mode, pipeline_stage, status'
).eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
stage = rating_request.stage
# Auto-derive stage for controlled pipeline when frontend doesn't pass it
if not stage and job.get('pipeline_mode') == 'controlled':
if job.get('status') == 'checkpoint':
stage = job.get('pipeline_stage')
elif job.get('status') == 'completed':
stage = 'completed'
elif job.get('status') == 'failed':
stage = job.get('pipeline_stage') or 'failed'
if stage:
# Per-stage rating: store in stage_ratings JSONB (accumulative, never overwritten)
stage_ratings = job.get('stage_ratings') or {}
stage_ratings[stage] = {
'rating': rating_request.rating,
'comment': rating_request.comment or '',
}
# Also update user_rating/user_comment so the latest rating is always accessible
update_data = {
'stage_ratings': stage_ratings,
'user_rating': rating_request.rating,
'user_comment': rating_request.comment or '',
}
else:
# Legacy: store in user_rating/user_comment columns (auto mode)
update_data = {'user_rating': rating_request.rating}
if rating_request.comment is not None:
update_data['user_comment'] = rating_request.comment
supabase.table('simulations').update(update_data).eq('id', job_id).execute()
logger.info(f"Rating {rating_request.rating} submitted for job {job_id} stage={stage} by user {user_id}")
return {"status": "success", "message": "Rating submitted successfully"}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in submit_rating: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
# --- 7. Soft-delete and restore endpoints ---
@app.delete("/api/v1/simulations/{job_id}")
async def soft_delete_simulation(job_id: str, user_id: str = Depends(verify_jwt)):
"""
Soft-delete a simulation by setting deleted_at timestamp.
Cannot delete a running job. Requires ownership.
"""
try:
response = supabase.table('simulations').select('id, user_id, status').eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
if job['status'] == 'running':
raise HTTPException(status_code=409, detail="Cannot delete a running simulation")
supabase.table('simulations').update(
{'deleted_at': datetime.now(timezone.utc).isoformat()}
).eq('id', job_id).execute()
# Invalidate storage cache so quota check reflects the deletion
_storage_cache.pop(user_id, None)
logger.info(f"Soft-deleted simulation {job_id} by user {user_id}")
return {"status": "success", "message": f"Simulation {job_id} deleted"}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in soft_delete_simulation: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/v1/simulations/{job_id}/cancel")
@limiter.limit("10/minute")
async def cancel_simulation(request: Request, job_id: str, user_id: str = Depends(verify_jwt)):
"""
Cancel a queued or running simulation.
Sets status to 'cancelled'. Worker will detect this and kill the subprocess.
"""
try:
response = supabase.table('simulations').select('id, user_id, status').eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
if job['status'] not in ('queued', 'running', 'checkpoint'):
raise HTTPException(
status_code=409,
detail=f"Cannot cancel a simulation with status '{job['status']}'. Only queued, running, or checkpoint simulations can be cancelled."
)
supabase.table('simulations').update(
{'status': 'cancelled'}
).eq('id', job_id).execute()
logger.info(f"Simulation {job_id} cancelled by user {user_id}")
return {"status": "success", "message": f"Simulation {job_id} cancelled"}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in cancel_simulation: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
# --- 8. Stage confirm/reject (controlled pipeline) ---
@app.post("/api/v1/simulations/{job_id}/stage/confirm")
@limiter.limit("10/minute")
async def confirm_stage(request: Request, job_id: str, user_id: str = Depends(verify_jwt)):
"""
User confirms current pipeline stage, allowing the pipeline to advance.
Only used by controlled pipeline mode. Re-queues the job so Worker picks it up.
Accepts optional comment to record user feedback for the stage.
"""
try:
body = await request.json() if request.headers.get('content-type', '').startswith('application/json') else {}
except Exception:
body = {}
comment = (body.get('comment') or '').strip()
try:
response = supabase.table('simulations').select(
'id, user_id, status, pipeline_stage, pipeline_state'
).eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
if job['status'] != 'checkpoint':
raise HTTPException(
status_code=409,
detail=f"Cannot confirm: simulation status is '{job['status']}', expected 'checkpoint'."
)
pipeline_stage = job.get('pipeline_stage')
pipeline_state = job.get('pipeline_state') or {}
# Append stage feedback (accumulative, never overwritten)
stage_feedback = pipeline_state.setdefault('stage_feedback', [])
stage_feedback.append({
'stage': pipeline_stage,
'action': 'confirm',
'comment': comment,
'timestamp': datetime.now(timezone.utc).isoformat(),
})
# Signal the bound worker to resume by setting a flag in pipeline_state.
# IMPORTANT: Keep status='checkpoint' (NOT 'queued') so claim_next_job()
# won't let another worker steal this job. Only the bound worker's poll
# loop checks for the 'confirmed' flag.
pipeline_state['confirmed'] = True
pipeline_state['confirmed_at'] = datetime.now(timezone.utc).isoformat()
supabase.table('simulations').update({
'pipeline_state': pipeline_state,
}).eq('id', job_id).execute()
logger.info(f"Stage confirmed for job {job_id} (stage={pipeline_stage}, comment={'yes' if comment else 'none'})")
return {"status": "success", "message": f"Stage '{pipeline_stage}' confirmed. Pipeline will continue."}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in confirm_stage: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/v1/simulations/{job_id}/stage/reject")
@limiter.limit("10/minute")
async def reject_stage(request: Request, job_id: str, user_id: str = Depends(verify_jwt)):
"""
User rejects current pipeline stage. Marks the simulation as failed.
Used by controlled pipeline mode. Preserves result_data and stage feedback.
"""
try:
body = await request.json() if request.headers.get('content-type', '').startswith('application/json') else {}
except Exception:
body = {}
comment = (body.get('comment') or '').strip()
try:
response = supabase.table('simulations').select(
'id, user_id, status, pipeline_mode, pipeline_stage, pipeline_state, result_data'
).eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
if job['status'] != 'checkpoint':
raise HTTPException(
status_code=409,
detail=f"Cannot reject: simulation status is '{job['status']}', expected 'checkpoint'."
)
pipeline_stage = job.get('pipeline_stage')
pipeline_state = job.get('pipeline_state') or {}
# Append stage feedback (accumulative, never overwritten)
stage_feedback = pipeline_state.setdefault('stage_feedback', [])
stage_feedback.append({
'stage': pipeline_stage,
'action': 'reject',
'comment': comment,
'timestamp': datetime.now(timezone.utc).isoformat(),
})
existing_result = job.get('result_data') or {}
existing_result['rejected_stage'] = pipeline_stage
# Persist all stage feedback into result_data (pipeline_state may be cleared)
existing_result['stage_feedback'] = stage_feedback
supabase.table('simulations').update({
'status': 'failed',
'pipeline_state': pipeline_state,
'result_data': existing_result,
}).eq('id', job_id).execute()
logger.info(f"Stage '{pipeline_stage}' rejected for job {job_id} (comment={'yes' if comment else 'none'}). Marked as failed.")
return {"status": "success", "message": f"Stage '{pipeline_stage}' rejected. Simulation marked as failed."}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in reject_stage: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
# --- 9. Restore endpoint ---
@app.post("/api/v1/simulations/{job_id}/restore")
async def restore_simulation(job_id: str, user_id: str = Depends(verify_jwt)):
"""
Restore a soft-deleted simulation by clearing deleted_at.
"""
try:
response = supabase.table('simulations').select('id, user_id, deleted_at').eq('id', job_id).execute()
if not response.data:
raise HTTPException(status_code=404, detail=f"Simulation {job_id} not found")
job = response.data[0]
if job['user_id'] != user_id:
raise HTTPException(status_code=403, detail="Permission denied")
if not job.get('deleted_at'):
raise HTTPException(status_code=400, detail="Simulation is not deleted")
supabase.table('simulations').update(
{'deleted_at': None}
).eq('id', job_id).execute()
# Invalidate storage cache (restored task re-counts toward quota)
_storage_cache.pop(user_id, None)
logger.info(f"Restored simulation {job_id} by user {user_id}")
return {"status": "success", "message": f"Simulation {job_id} restored"}
except HTTPException:
raise
except Exception as e:
logger.error(f"Error in restore_simulation: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
# --- 10. Storage usage endpoint ---
def _format_bytes(size_bytes: int) -> str:
"""Format bytes into human-readable string."""
if size_bytes == 0:
return "0 B"
units = ["B", "KB", "MB", "GB", "TB"]
i = 0
size = float(size_bytes)
while size >= 1024 and i < len(units) - 1:
size /= 1024
i += 1
return f"{size:.1f} {units[i]}" if i > 0 else f"{int(size)} B"
def _get_storage_dir_size(prefix: str) -> int:
"""
Recursively sum file sizes under a Supabase Storage prefix.
Used as fallback when upload_stats.total_bytes is not available.
"""
total = 0
try:
listed = supabase.storage.from_('simulation_results').list(prefix)
if not listed:
return 0
for item in listed:
item_path = f"{prefix}/{item['name']}"
if item.get('id') is None:
# Directory — recurse
total += _get_storage_dir_size(item_path)
else:
# File — metadata includes size in bytes
meta = item.get('metadata') or {}
total += meta.get('size', 0)
except Exception:
pass
return total
# In-memory cache: { user_id: { "result": {...}, "expires": timestamp } }
_storage_cache = {}
_STORAGE_CACHE_TTL = 300 # 5 minutes
def _count_today_billable(user_id: str) -> dict:
"""Count today's tasks for a user, excluding platform-side failures.
Returns {'total': int, 'refunded': int, 'billable': int}.
A "refunded" task is one that failed with an error_category in
PLATFORM_REFUND_CATEGORIES — the user is not charged for platform-side
failures (e.g. stale Codex token). Used by both the create-task quota
check and the /user/daily-usage endpoint.
"""
today_start = datetime.now(timezone.utc).replace(
hour=0, minute=0, second=0, microsecond=0
).isoformat()
total_resp = (
supabase.table('simulations')
.select('id', count='exact')
.eq('user_id', user_id)
.gte('created_at', today_start)
.execute()
)
total = total_resp.count if total_resp.count is not None else len(total_resp.data)
refund_resp = (
supabase.table('simulations')
.select('id', count='exact')
.eq('user_id', user_id)
.gte('created_at', today_start)
.in_('result_data->>error_category', PLATFORM_REFUND_CATEGORIES)
.execute()
)
refunded = refund_resp.count if refund_resp.count is not None else len(refund_resp.data)
return {'total': total, 'refunded': refunded, 'billable': max(0, total - refunded)}
def _get_user_storage_bytes(user_id: str) -> int:
"""
Return total storage bytes for a user. Uses cache when available.
Called by both the storage endpoint and the quota check in create_simulation_task.
"""
cached = _storage_cache.get(user_id)
if cached and time.time() < cached["expires"]:
return cached["result"]["total_bytes"]
try:
response = (
supabase.table('simulations')
.select('id, result_data')
.eq('user_id', user_id)
.is_('deleted_at', 'null')
.execute()
)
total = 0
for row in response.data:
rd = row.get('result_data') or {}
stats = rd.get('upload_stats') or {}
recorded = stats.get('total_bytes')
if recorded is not None and recorded > 0:
total += recorded
else:
storage_base = rd.get('storage_base_path')
if storage_base:
total += _get_storage_dir_size(storage_base)
return total
except Exception as e:
logger.error(f"Error computing storage for quota check: {e}")
return 0 # fail open: allow submission if storage check fails
@app.get("/api/v1/user/storage")
@limiter.limit("10/minute")
async def get_user_storage(request: Request, user_id: str = Depends(verify_jwt)):
"""
Return storage usage summary for the authenticated user.
Uses pre-recorded total_bytes when available, falls back to
querying Supabase Storage metadata for historical tasks.
Results cached for 5 minutes to avoid flooding Supabase Storage API.
"""
# Check cache first (skip if user just deleted data)
cached = _storage_cache.get(user_id)
if cached and time.time() < cached["expires"]:
return cached["result"]
try:
response = (
supabase.table('simulations')
.select('id, status, result_data, created_at')
.eq('user_id', user_id)
.is_('deleted_at', 'null')
.execute()
)
total_bytes = 0
task_count = len(response.data)
breakdown = {}
per_task = {} # task_id -> bytes