Spaces:
Sleeping
Sleeping
Download CollaborationService/catalog.py from dellweb3/workos-office: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/CollaborationService/catalog.py
- Command line
-
hf download hf://spaces/dellweb3/workos-office/CollaborationService/catalog.py
-
curl -L -o catalog.py https://huggingface.co/spaces/dellweb3/workos-office/resolve/main/CollaborationService/catalog.py
5.55 kB
| import copy,hashlib,json | |
| from sqlalchemy import select,update | |
| from .models import PromptSkill,PromptSkillVersion | |
| from APIGateway.app.stubs import CatalogStub,page,digest | |
| from APIGateway.app.errors import GatewayError | |
| from AgentRuntime.models import Published | |
| class PersistentCatalog(CatalogStub): | |
| def __init__(self,state,workspace,runtime):super().__init__(state);self.Session=workspace.Session;self.runtime=runtime | |
| def employees(self,user): | |
| items=super().employees(user)['items'] | |
| with self.Session() as db: | |
| latest={} | |
| for p in db.scalars(select(Published).where(Published.tenant==user['tenant_id'])): | |
| if p.workflow_id not in latest or p.version>latest[p.workflow_id].version:latest[p.workflow_id]=p | |
| for wid,p in latest.items(): | |
| if wid=='official-document':continue | |
| items.append({'employee_id':'workflow:'+wid,'name':p.body['name'],'description':p.body['description'],'enabled':True,'execution_type':'workflow','workflow_id':wid,'workflow_version':p.version,'default_skill_ids':p.body.get('skill_ids',[]),'content_hash':p.body['content_hash']}) | |
| return page(items) | |
| def lookup(self,user,sid): | |
| with self.Session() as db: | |
| p=db.get(PromptSkill,(user['tenant_id'],sid)) | |
| if p: | |
| if p.owner not in {user['user_id'],'__shared__'}:raise GatewayError(404,'RESOURCE_NOT_FOUND','技能不存在或无权访问。') | |
| return copy.deepcopy(p.body) | |
| return self.s._fixture_skill_lookup(user,sid) | |
| def skills(self,user): | |
| result=super().skills(user)['items'] | |
| with self.Session() as db:result.extend([{k:v for k,v in p.body.items() if k not in {'owner_user_id','request_fingerprint'}} for p in db.scalars(select(PromptSkill).where(PromptSkill.tenant==user['tenant_id'],PromptSkill.owner.in_([user['user_id'],'__shared__'])))]) | |
| return page(result) | |
| def create_skill(self,user,data,key): | |
| if len(data['content'].encode())>32768:raise GatewayError(413,'PAYLOAD_TOO_LARGE','技能内容超过32 KiB。') | |
| sid='skill-'+hashlib.sha256((user['tenant_id']+user['user_id']+key).encode()).hexdigest()[:32];fp=digest(json.dumps(data,sort_keys=True,ensure_ascii=False)) | |
| with self.Session.begin() as db: | |
| old=db.get(PromptSkill,(user['tenant_id'],sid)) | |
| if old: | |
| if old.body['request_fingerprint']!=fp:raise GatewayError(409,'IDEMPOTENCY_CONFLICT','同一请求编号对应不同技能内容。') | |
| return {k:v for k,v in old.body.items() if k not in {'owner_user_id','request_fingerprint'}},True | |
| value={**data,'skill_id':sid,'version':1,'content_hash':digest(data['content']),'owner_scope':'personal','owner_user_id':user['user_id'],'request_fingerprint':fp} | |
| db.add(PromptSkill(tenant=user['tenant_id'],id=sid,owner=user['user_id'],revision=1,body=value)) | |
| db.flush();db.add(PromptSkillVersion(tenant=user['tenant_id'],skill_id=sid,version=1,body=value)) | |
| return {k:v for k,v in value.items() if k not in {'owner_user_id','request_fingerprint'}},False | |
| def share_skill(self,user,sid,expected): | |
| if 'admin' not in user.get('roles',[]):raise GatewayError(403,'ADMIN_REQUIRED','企业技能发布需要管理员。') | |
| with self.Session.begin() as db: | |
| source=db.get(PromptSkill,(user['tenant_id'],sid)) | |
| if not source or source.owner!=user['user_id']:raise GatewayError(404,'RESOURCE_NOT_FOUND','只能发布本人准备的技能。') | |
| if source.revision!=expected:raise GatewayError(409,'VERSION_CONFLICT','技能版本已改变。') | |
| ident='enterprise-'+hashlib.sha256((sid+str(expected)).encode()).hexdigest()[:28] | |
| old=db.get(PromptSkill,(user['tenant_id'],ident)) | |
| if old:return copy.deepcopy(old.body) | |
| value={**source.body,'skill_id':ident,'owner_scope':'shared','owner_user_id':None,'version':1} | |
| db.add(PromptSkill(tenant=user['tenant_id'],id=ident,owner='__shared__',revision=1,body=value));db.flush() | |
| db.add(PromptSkillVersion(tenant=user['tenant_id'],skill_id=ident,version=1,body=value)) | |
| self.runtime._audit(db,user,'enterprise.skill.publish',ident) | |
| return {k:v for k,v in value.items() if k!='request_fingerprint'} | |
| def update_skill(self,user,sid,data): | |
| if len(data['content'].encode())>32768:raise GatewayError(413,'PAYLOAD_TOO_LARGE','技能内容超过32 KiB。') | |
| with self.Session.begin() as db: | |
| p=db.get(PromptSkill,(user['tenant_id'],sid)) | |
| if not p or (p.owner!=user['user_id'] and not (p.owner=='__shared__' and 'admin' in user.get('roles',[]))):raise GatewayError(403,'SCOPE_DENIED','仅能编辑本人创建的提示技能。') | |
| v={**p.body,**{k:data[k] for k in ['name','category','content']},'version':p.revision+1,'content_hash':digest(data['content'])} | |
| n=db.execute(update(PromptSkill).where(PromptSkill.tenant==user['tenant_id'],PromptSkill.id==sid,PromptSkill.revision==data['expected_version']).values(body=v,revision=p.revision+1)).rowcount | |
| if n!=1:raise GatewayError(409,'VERSION_CONFLICT','技能已更新。') | |
| db.add(PromptSkillVersion(tenant=user['tenant_id'],skill_id=sid,version=v['version'],body=v)) | |
| return {k:value for k,value in v.items() if k not in {'owner_user_id','request_fingerprint'}} | |