"""Space demo: independent visitor databases, separate admin token audience and API prefix. The public demo never loads local model credentials and resets after idle expiry/restart. """ import asyncio import json import os import secrets import shutil import time from contextlib import asynccontextmanager from pathlib import Path from http.cookies import SimpleCookie from starlette.applications import Starlette from starlette.responses import JSONResponse, Response, FileResponse ROOT=Path(__file__).resolve().parents[2] BUILD_ID=json.loads((ROOT/'deployment-manifest.json').read_text())['build_id'] if (ROOT/'deployment-manifest.json').exists() else 'local-source' DATA=Path(os.getenv('WORKOS_DEMO_DATA_DIR','/tmp/workos-public-demo')).resolve() DATA.mkdir(parents=True,exist_ok=True) os.environ['WORKOS_WORKSPACE_DATA_DIR']=str(DATA/'bootstrap') MODEL_MODE=os.getenv('WORKOS_PUBLIC_MODEL_MODE','real') if MODEL_MODE not in {'real','fixture'}:raise ValueError('Invalid public model mode') MODULES=ROOT/('APIGateway/modules.real.json' if MODEL_MODE=='real' else 'APIGateway/modules.json') os.environ['WORKOS_MODULES_CONFIG']=str(MODULES) os.environ['WORKOS_PUBLIC_DEMO']='1' os.environ.setdefault('DASHSCOPE_BASE_URL','https://dashscope.aliyuncs.com/compatible-mode/v1') os.environ.setdefault('WORKOS_LLM_HOURLY_LIMIT','120') os.environ['WORKOS_ALLOWED_HOSTS']=os.getenv('SPACE_HOST','dellweb3-workos-office.hf.space') os.environ['WORKOS_SURFACE']='employee' # The real key is supplied only by Space Secrets; no offline fallback. from APIGateway.app.main import create_app, app as bootstrap from APIGateway.app.stubs import Providers static_admin=create_app(bootstrap.state.providers,surface='admin') MAX_VISITORS=8 TTL=15*60 MAX_BODY=512*1024 class Demo: def __init__(self): self.sessions={};self.active=0;self.bucket=[];self.create_lock=asyncio.Lock() async def expire(self): for sid,v in list(self.sessions.items()): if not v['active'] and not v['p'].platform.busy and time.monotonic()-v['seen']>TTL: self.sessions.pop(sid) await v['p'].platform.close();await v['p'].runtime.shutdown();await v['p'].llm.close();v['p'].workspace.engine.dispose() # Only generated visitor folders inside DATA; never a user supplied path. target=v['root'].resolve() if target.parent==DATA and target.name.startswith('visitor-'):shutil.rmtree(target,ignore_errors=True) async def new(self): async with self.create_lock: await self.expire() if len(self.sessions)>=MAX_VISITORS:return None,None sid=secrets.token_urlsafe(32);folder=DATA/('visitor-'+secrets.token_hex(16)) old=os.environ['WORKOS_WORKSPACE_DATA_DIR'];os.environ['WORKOS_WORKSPACE_DATA_DIR']=str(folder) try:p=Providers(config_path=MODULES) finally:os.environ['WORKOS_WORKSPACE_DATA_DIR']=old p.llm.admission_scope=sid p.platform.start() v={'p':p,'employee':create_app(p,surface='employee'),'admin':create_app(p,surface='admin'), 'seen':time.monotonic(),'active':0,'mutations':0,'root':folder,'calls':[]} self.sessions[sid]=v;return sid,v async def __call__(self,scope,receive,send): if scope['type']!='http':return path=scope['path'] if path=='/health/live':return await JSONResponse({'status':'alive','mode':'public-isolated-'+MODEL_MODE,'build_id':BUILD_ID})(scope,receive,send) if path=='/deployment.js': script="window.WORKOS_ADMIN_API_PREFIX='/manage/api/v1';window.WORKOS_PUBLIC_DEMO=true;" return await Response(script,media_type='text/javascript')(scope,receive,send) # Assets and landing pages never consume a visitor seat. HF probes often don't retain cookies. if scope['method']=='GET' and path in {'/','/manage','/manage/','/admin'}: folder='EmployeeWeb' if path=='/' else 'AdminWeb' return await FileResponse(ROOT/folder/'index.html',media_type='text/html')(scope,receive,send) if scope['method']=='GET' and path.startswith(('/admin/','/employee/')): return await (static_admin if path.startswith('/admin/') else bootstrap)(scope,receive,send) now=time.monotonic();self.bucket=[t for t in self.bucket if now-t<60] if self.active>=24 or len(self.bucket)>=360: return await JSONResponse({'message':'展示服务繁忙,请稍后重试。'},429)(scope,receive,send) self.bucket.append(now) cookies=SimpleCookie();headers=dict(scope['headers']) try:cookies.load(headers.get(b'cookie',b'').decode('latin1'));sid=cookies.get('workos_demo');sid=sid.value if sid else '' except Exception:sid='' await self.expire();v=self.sessions.get(sid);fresh=False if v is None: if path not in {'/api/v1/auth/login','/manage/api/v1/auth/login'}: return await JSONResponse({'message':'演示会话已失效,请刷新页面重新进入。'},401)(scope,receive,send) sid,v=await self.new();fresh=True if v is None:return await JSONResponse({'message':'8个演示席位已占满,请15分钟后再试。'},429)(scope,receive,send) v['calls']=[t for t in v['calls'] if now-t<60] if len(v['calls'])>=180: return await JSONResponse({'message':'演示请求过于频繁,请稍后重试。'},429)(scope,receive,send) v['calls'].append(now) if scope['method'] not in {'GET','HEAD','OPTIONS'}: if v['mutations']>=120 or sum(p.stat().st_size for p in v['root'].rglob('*') if p.is_file())>32*1024*1024: return await JSONResponse({'message':'当前演示会话已达到操作或存储上限,请会话过期后重试。'},429)(scope,receive,send) v['mutations']+=1 # Enforce the actual streamed body size, including chunked requests without Content-Length. chunks=[];size=0 while True: item=await receive() if item['type']=='http.disconnect':return size+=len(item.get('body',b'')) if size>MAX_BODY:return await JSONResponse({'message':'公开展示版单次请求或附件上限512KiB。'},413)(scope,receive,send) chunks.append(item) if not item.get('more_body',False):break async def replay(): if chunks:return chunks.pop(0) return await receive() is_admin=path=='/manage' or path.startswith(('/manage/','/admin')) new_scope={**scope} if path.startswith('/manage/api/v1'): new_scope['path']=path.removeprefix('/manage') elif path.startswith('/manage'): new_scope['path']='/admin' if path in {'/manage','/manage/'} else path.removeprefix('/manage') new_scope['raw_path']=new_scope['path'].encode() async def decorate(message): if message['type']=='http.response.start': h=list(message['headers']);h.extend([(b'cache-control',b'no-store'),(b'x-workos-demo',('isolated-'+MODEL_MODE).encode()), (b'x-content-type-options',b'nosniff'),(b'referrer-policy',b'same-origin')]) if fresh: attributes='; Secure; SameSite=None; Partitioned' if os.getenv('WORKOS_DEMO_SECURE_COOKIE','1')=='1' else '; SameSite=Lax' h.append((b'set-cookie',('workos_demo='+sid+'; Path=/; HttpOnly; Max-Age=3600'+attributes).encode())) message={**message,'headers':h} await send(message) self.active+=1;v['active']+=1;v['seen']=time.monotonic() try:await v['admin' if is_admin else 'employee'](new_scope,replay,decorate) finally:self.active-=1;v['active']-=1;v['seen']=time.monotonic() async def close(self): for v in self.sessions.values():await v['p'].platform.close();await v['p'].runtime.shutdown();await v['p'].llm.close();v['p'].workspace.engine.dispose() self.sessions.clear();await bootstrap.state.providers.llm.close();bootstrap.state.providers.workspace.engine.dispose() demo=Demo() @asynccontextmanager async def lifespan(app): yield await demo.close() app=Starlette(lifespan=lifespan) app.mount('/',demo)