Spaces:
Sleeping
Sleeping
Download PlatformService/service.py from dellweb3/workos-office: direct link, hf CLI and curl.
- Browser
- Download file 15.1 kB
-
https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/PlatformService/service.py
- Command line
-
hf download hf://spaces/dellweb3/workos-office/PlatformService/service.py
-
curl -L -o service.py https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/PlatformService/service.py
15.1 kB
| import asyncio, copy, hashlib, json, os, uuid | |
| from datetime import datetime, timedelta, timezone | |
| from sqlalchemy import select, update, delete, func | |
| from sqlalchemy.exc import IntegrityError | |
| from KnowledgeService.models import Base | |
| from APIGateway.app.errors import GatewayError | |
| from observability import Trace, scope, traced | |
| from .models import Resource, Schedule, ScheduledRun | |
| from .catalog import CATALOG | |
| def now():return datetime.now(timezone.utc) | |
| def stamp(dt=None):return (dt or now()).isoformat() | |
| def next_time(body, after=None): | |
| current=after or now(); local=current.astimezone(timezone(timedelta(hours=8))) | |
| if body['frequency']=='once': | |
| try:target=datetime.fromisoformat(body['run_at'].replace('Z','+00:00')) | |
| except (ValueError,AttributeError) as e:raise GatewayError(422,'SCHEDULE_INVALID','执行时间格式不正确。') from e | |
| if target.tzinfo is None:raise GatewayError(422,'TIMEZONE_REQUIRED','单次任务时间必须包含时区。') | |
| return stamp(target.astimezone(timezone.utc)) if target>current else None | |
| hour,minute=map(int,body['time'].split(':')) | |
| for days in range(8): | |
| candidate=(local+timedelta(days=days)).replace(hour=hour,minute=minute,second=0,microsecond=0) | |
| if candidate>local and (body['frequency']=='daily' or candidate.weekday()==body['weekday']): | |
| return stamp(candidate.astimezone(timezone.utc)) | |
| raise GatewayError(422,'SCHEDULE_INVALID','无法计算下一次执行时间。') | |
| class Platform: | |
| def __init__(self,p): | |
| self.p=p;self.Session=p.workspace.Session;self.worker=None;self.busy=False;self.guard=asyncio.Lock() | |
| if os.getenv('WORKOS_WORKSPACE_AUTO_SCHEMA','1')=='1':Base.metadata.create_all(p.workspace.engine) | |
| def resources(self,u,kind): | |
| with self.Session() as db: | |
| return {'items':[{'id':r.id,'revision':r.revision,'updated_at':r.updated_at,**copy.deepcopy(r.body)} for r in db.scalars(select(Resource).where(Resource.tenant==u['tenant_id'],Resource.owner==u['user_id'],Resource.kind==kind).order_by(Resource.updated_at.desc()))]} | |
| def save(self,u,kind,data,ident=None): | |
| ident=ident or uuid.uuid4().hex;expected=data.pop('expected_revision',None) | |
| with self.Session.begin() as db: | |
| pk=(u['tenant_id'],u['user_id'],kind,ident);r=db.get(Resource,pk) | |
| if ident and expected is not None and not r:raise GatewayError(404,'RESOURCE_NOT_FOUND','资源不存在。') | |
| rev=r.revision if r else 0 | |
| if r and expected!=rev:raise GatewayError(409,'VERSION_CONFLICT','内容已改变,请刷新。') | |
| if r: | |
| n=db.execute(update(Resource).where(Resource.tenant==pk[0],Resource.owner==pk[1],Resource.kind==kind,Resource.id==ident,Resource.revision==rev).values(body=data,revision=rev+1,updated_at=stamp())).rowcount | |
| if n!=1:raise GatewayError(409,'VERSION_CONFLICT','内容已改变,请刷新。') | |
| else: | |
| if db.scalar(select(func.count()).select_from(Resource).where(Resource.tenant==pk[0],Resource.owner==pk[1],Resource.kind==kind))>=100:raise GatewayError(429,'RESOURCE_LIMIT','同类资源最多100项。') | |
| db.add(Resource(tenant=pk[0],owner=pk[1],kind=kind,id=ident,revision=1,body=data,updated_at=stamp())) | |
| return {'id':ident,'revision':rev+1,**data} | |
| def remove(self,u,kind,ident,revision): | |
| with self.Session.begin() as db: | |
| r=db.get(Resource,(u['tenant_id'],u['user_id'],kind,ident)) | |
| if not r:raise GatewayError(404,'RESOURCE_NOT_FOUND','资源不存在。') | |
| if r.revision!=revision:raise GatewayError(409,'VERSION_CONFLICT','内容已改变。') | |
| n=db.execute(delete(Resource).where(Resource.tenant==u['tenant_id'],Resource.owner==u['user_id'],Resource.kind==kind,Resource.id==ident,Resource.revision==revision)).rowcount | |
| if n!=1:raise GatewayError(409,'VERSION_CONFLICT','内容已改变。') | |
| return {'removed':True} | |
| def market(self,u): | |
| installed={x.get('template_id'):x for x in self.resources(u,'installed')['items']} | |
| return {'items':[{**x,'installed_skill_id':installed.get(x['id'],{}).get('skill_id')} for x in CATALOG]} | |
| def install(self,u,ident): | |
| item=next((x for x in CATALOG if x['id']==ident),None) | |
| if not item:raise GatewayError(404,'RESOURCE_NOT_FOUND','模板不存在。') | |
| value,_=self.p.catalog.create_skill(u,{'name':item['name'],'category':'knowledge' if item['category']=='知识管理' else 'document','content':item['content']},'market-v1-'+ident) | |
| with self.Session.begin() as db: | |
| pk=(u['tenant_id'],u['user_id'],'installed',ident) | |
| if not db.get(Resource,pk):db.add(Resource(tenant=pk[0],owner=pk[1],kind=pk[2],id=pk[3],revision=1,body={'template_id':ident,'skill_id':value['skill_id']},updated_at=stamp())) | |
| return {**value,'starter':item['starter']} | |
| def save_draft(self,u,data): | |
| self.p.organization.admin(u) | |
| # Validate DTO structure, but unfinished DAGs are allowed as drafts. | |
| from APIGateway.app.admin_api import WorkflowInput | |
| flow=WorkflowInput.model_validate(data['flow']).model_dump(exclude={'expected_version'}) | |
| if data.get('workflow_id'):self.p.runtime.get_workflow(u,data['workflow_id']) | |
| return self.save(u,'canvas-draft',{'name':data['name'],'workflow_id':data.get('workflow_id'),'source_version':data.get('source_version'), 'flow':flow}) | |
| def restore_draft(self,u,ident,mode,expected): | |
| self.p.organization.admin(u) | |
| with self.Session() as db: | |
| r=db.get(Resource,(u['tenant_id'],u['user_id'],'canvas-draft',ident)) | |
| if not r:raise GatewayError(404,'RESOURCE_NOT_FOUND','草稿不存在。') | |
| data=copy.deepcopy(r.body) | |
| if mode=='copy':return self.p.runtime.save_workflow(u,{**data['flow'],'name':data['name']+'(草稿副本)'}) | |
| if not data['workflow_id']:raise GatewayError(422,'DRAFT_SOURCE_MISSING','该草稿仅能另存为数字员工。') | |
| return self.p.runtime.save_workflow(u,{**data['flow'],'expected_version':expected},data['workflow_id']) | |
| def schedules(self,u): | |
| with self.Session() as db: | |
| rows=db.scalars(select(Schedule).where(Schedule.tenant==u['tenant_id'],Schedule.owner==u['user_id']).order_by(Schedule.updated_at.desc())) | |
| items=[] | |
| for r in rows: | |
| runs=db.scalars(select(ScheduledRun).where(ScheduledRun.schedule_id==r.id).order_by(ScheduledRun.created_at.desc()).limit(10)) | |
| items.append({'id':r.id,'revision':r.revision,'status':r.status,'next_at':r.next_at,**r.body,'runs':[{'id':x.id,'status':x.status,'created_at':x.created_at,**x.result} for x in runs]}) | |
| return {'items':items,'timezone':'Asia/Shanghai (UTC+08:00)','execution':'server polling, single process','public_lifetime_minutes':15 if os.getenv('WORKOS_PUBLIC_DEMO')=='1' else None} | |
| def _owned(self,db,u,ident): | |
| r=db.get(Schedule,ident) | |
| if not r or (r.tenant,r.owner)!=(u['tenant_id'],u['user_id']):raise GatewayError(404,'RESOURCE_NOT_FOUND','定时任务不存在。') | |
| return r | |
| def save_schedule(self,u,data,ident=None): | |
| data=copy.deepcopy(data);expected=data.pop('expected_revision',None) | |
| self.p.workspace.assert_access(u,data['workspace_id']) | |
| for sid in data['skill_ids']:self.p.state.skill(u,sid) | |
| visible={b['base_id'] for b in self.p.knowledge.bases(u)['items']} | |
| if not set(data['base_ids'])<=visible:raise GatewayError(404,'RESOURCE_NOT_FOUND','知识库不存在或无权使用。') | |
| eid=data.get('employee_id') | |
| if eid: | |
| if not any(e['employee_id']==eid for e in self.p.catalog.employees(u)['items']):raise GatewayError(422,'EMPLOYEE_INVALID','数字员工不存在。') | |
| if eid.startswith('workflow:'):data['workflow_version']=self.p.runtime.published(u,eid[9:])['published_version'] | |
| nxt=next_time(data) | |
| if not nxt:raise GatewayError(422,'SCHEDULE_PAST','请选择未来的执行时间。') | |
| with self.Session.begin() as db: | |
| r=self._owned(db,u,ident) if ident else None | |
| if r and r.revision!=expected:raise GatewayError(409,'VERSION_CONFLICT','定时任务已改变。') | |
| if not r: | |
| count=db.scalar(select(func.count()).select_from(Schedule).where(Schedule.tenant==u['tenant_id'],Schedule.owner==u['user_id'])) | |
| if count>=10:raise GatewayError(429,'SCHEDULE_LIMIT','每人最多10个定时任务。') | |
| r=Schedule(id='schedule-'+uuid.uuid4().hex,tenant=u['tenant_id'],owner=u['user_id'],revision=0,body={},status='active',next_at=None,updated_at=stamp());db.add(r) | |
| ident=r.id | |
| if expected is not None: | |
| n=db.execute(update(Schedule).where(Schedule.id==ident,Schedule.revision==expected).values(body=data,revision=expected+1,next_at=nxt,status='active',updated_at=stamp())).rowcount | |
| if n!=1:raise GatewayError(409,'VERSION_CONFLICT','定时任务已改变。') | |
| else:r.body=data;r.revision=1;r.next_at=nxt;r.status='active';r.updated_at=stamp() | |
| return next(x for x in self.schedules(u)['items'] if x['id']==ident) | |
| def state_schedule(self,u,ident,status,expected): | |
| with self.Session.begin() as db: | |
| r=self._owned(db,u,ident) | |
| if r.revision!=expected:raise GatewayError(409,'VERSION_CONFLICT','定时任务已改变。') | |
| nxt=next_time(r.body) if status=='active' else None | |
| if status=='active' and not nxt:raise GatewayError(422,'SCHEDULE_PAST','单次任务时间已过,请修改时间后重启。') | |
| n=db.execute(update(Schedule).where(Schedule.id==ident,Schedule.revision==expected).values(status=status,next_at=nxt,revision=expected+1,updated_at=stamp())).rowcount | |
| if n!=1:raise GatewayError(409,'VERSION_CONFLICT','定时任务已改变。') | |
| return self.schedules(u) | |
| async def execute(self,u,ident,slot,manual=False): | |
| async with self.guard: | |
| with self.Session.begin() as db: | |
| r=self._owned(db,u,ident) | |
| old=db.scalar(select(ScheduledRun).where(ScheduledRun.schedule_id==ident,ScheduledRun.slot==slot)) | |
| if old:return {'id':old.id,'status':old.status,**old.result,'replayed':True} | |
| if r.status=='archived':raise GatewayError(409,'SCHEDULE_ARCHIVED','请先启用或编辑已归档任务。') | |
| if not manual and (r.status!='active' or r.next_at!=slot):return {'status':'skipped'} | |
| active=db.scalar(select(ScheduledRun.id).where(ScheduledRun.schedule_id==ident,ScheduledRun.status=='running')) | |
| if active:raise GatewayError(409,'SCHEDULE_RUNNING','该任务正在执行;中断的任务需核对记录。') | |
| body=copy.deepcopy(r.body);rid='scheduled-'+uuid.uuid4().hex | |
| db.add(ScheduledRun(id=rid,schedule_id=ident,tenant=u['tenant_id'],owner=u['user_id'],slot=slot,status='running',result={},created_at=stamp())) | |
| if not manual:r.next_at=next_time(body);r.status='active' if r.next_at else 'completed' | |
| self.busy=True | |
| try: | |
| self.p.organization.check(u);wid=body['workspace_id'];self.p.workspace.assert_access(u,wid) | |
| scope(u,wid) | |
| eid=body.get('employee_id') | |
| if eid and (eid.startswith('workflow:') or eid=='official-document-writer'): | |
| msg,_=self.p.workspace.message(u,wid,body['prompt'],rid+'-input') | |
| result,_=self.p.runtime.start(u,wid,{'employee_id':eid,'input_message_id':msg['message_id'],'base_ids':body['base_ids'],'skill_ids':body['skill_ids'],'model_route':body['model_route'],'workflow_version':body.get('workflow_version'),'form':{}},rid) | |
| result={'workspace_id':wid,'run_id':result['run_id'],'note':'已提交数字员工;人工确认节点仍须用户确认。'};status='dispatched' | |
| else: | |
| from APIGateway.app.chat import DirectChatService | |
| async with asyncio.timeout(120): | |
| answer=await DirectChatService(self.p.workspace,self.p.llm,self.p.knowledge,self.p.catalog,self.p.state,self.p.office,self.p.context,self.p.tool).reply(u,wid,body['prompt'],rid,{'model_route':body['model_route'],'skill_ids':body['skill_ids'],'base_ids':body['base_ids'],'employee_id':eid}) | |
| result={'workspace_id':wid,'message_id':answer['assistant_message']['message_id'],'model_id':answer['model_id'],'summary':answer['assistant_message']['content'][:1200]};status='succeeded' | |
| except asyncio.CancelledError: | |
| self._finish(rid,'interrupted',{'message':'服务停止,结果需核对;不会自动重试。'});raise | |
| except Exception as e:status='failed';result={'code':getattr(e,'code','SCHEDULE_FAILED'),'message':getattr(e,'message','执行失败或超时,请查看消息流与模型配置。')} | |
| finally:self.busy=False | |
| self._finish(rid,status,result) | |
| return {'id':rid,'status':status,**result} | |
| def _finish(self,rid,status,result): | |
| with self.Session.begin() as db: | |
| r=db.get(ScheduledRun,rid);r.status=status;r.result=result | |
| async def tick(self): | |
| with self.Session() as db: | |
| rows=[(r.id,r.tenant,r.owner,r.next_at) for r in db.scalars(select(Schedule).where(Schedule.status=='active',Schedule.next_at<=stamp()).order_by(Schedule.next_at).limit(2))] | |
| for ident,tenant,owner,slot in rows: | |
| u={'tenant_id':tenant,'user_id':owner,'roles':['employee'],'display_name':owner} | |
| with Trace(self.Session,'定时任务执行'): | |
| scope(u) | |
| try:await self.execute(u,ident,slot) | |
| except Exception:pass # Failure is persisted by execute; no implicit retries. | |
| def start(self): | |
| if self.worker and not self.worker.done():return | |
| # A claim surviving process restart is uncertain, never silently replay it. | |
| with self.Session.begin() as db: | |
| db.execute(update(ScheduledRun).where(ScheduledRun.status=='running').values(status='interrupted',result={'message':'服务重启,执行结果需核对;不会自动重试。'})) | |
| async def loop(): | |
| while True: | |
| await self.tick();await asyncio.sleep(10) | |
| self.worker=asyncio.create_task(loop()) | |
| async def close(self): | |
| if self.worker: | |
| self.worker.cancel();await asyncio.gather(self.worker,return_exceptions=True);self.worker=None | |