create_data.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import json, urllib.request, urllib.error
  2. BASE='http://localhost:8980/js/a'
  3. def call(method,path,body=None,token=None):
  4. data=json.dumps(body).encode() if body is not None else None
  5. req=urllib.request.Request(BASE+path,data=data,method=method)
  6. req.add_header('Content-Type','application/json')
  7. if token: req.add_header('x-token',token)
  8. try:
  9. with urllib.request.urlopen(req,timeout=30) as r: txt=r.read().decode()
  10. except urllib.error.HTTPError as e:
  11. return {'http':e.code,'body':e.read().decode()[:500]}
  12. try: return json.loads(txt)
  13. except Exception: return {'raw':txt[:500]}
  14. def login(u,p):
  15. r=call('POST','/auth/login',{'username':u,'password':p,'force':True})
  16. if r.get('code')!=200: print('LOGIN FAIL',u,r); return None
  17. return r['data']['token']
  18. env=json.load(open('env.json'))
  19. dA,dB=env['deptA'],env['deptB']
  20. admin=login('admin','admin123'); A=login('vsuserA','Vs1118pass'); B=login('vsuserB','Vs1118pass'); C=login('vsuserC','Vs1118pass')
  21. print('tokens',bool(admin),bool(A),bool(B),bool(C))
  22. for u,t in [('admin',admin),('A',A),('B',B),('C',C)]:
  23. r=call('GET','/auth/info',token=t)
  24. di=r.get('data') or {}
  25. print(u,'userId',di.get('userId'),'dept',di.get('deptId'),'isAdmin',di.get('isAdmin'),'roles',di.get('roles'),'perms',len(di.get('permissions') or []))
  26. if u!='admin':
  27. print(' scope-options:',json.dumps(call('GET','/ai/agent/scope-options',token=t),ensure_ascii=False)[:300])
  28. CHAT=1761400000000020400; EMB=1761400000000020401
  29. def tagname(x): return 'VS1118'+x
  30. # admin public records
  31. res={}
  32. res['expert_pub']=call('POST','/ai/agent',{'agentName':'VS公开专家','agentCode':'vs_expert_pub','description':'public expert','modelId':CHAT,'status':'0','visibleScope':'0','autoRoute':'1','memoryEnabled':'0','expertWriteEnabled':'1'},admin)
  33. res['skill_pub']=call('POST','/ai/skill',{'name':'vs_skill_pub','description':'public skill','status':'0','visibleScope':'0'},admin)
  34. res['pack_pub']=call('POST','/ai/pack',{'packCode':'vs_pack_pub','packName':'VS公开场景包','description':'public pack','status':'0','visibleScope':'0'},admin)
  35. res['kb_pub']=call('POST','/ai/kb',{'kbName':'VS公开知识库','kbCode':'vs_kb_pub','description':'public kb','embeddingModelId':EMB,'retrievalMode':'vector','status':'0','visibleScope':'0'},admin)
  36. # A dept records (ownerDept omitted for skill to test fallback to creator dept)
  37. res['expert_dept']=call('POST','/ai/agent',{'agentName':'VS部门专家','agentCode':'vs_expert_dept','description':'dept expert','modelId':CHAT,'status':'0','visibleScope':'1','ownerDeptId':dA,'autoRoute':'1','memoryEnabled':'0','expertWriteEnabled':'1'},A)
  38. res['skill_dept']=call('POST','/ai/skill',{'name':'vs_skill_dept','description':'dept skill','status':'0','visibleScope':'1'},A)
  39. res['pack_dept']=call('POST','/ai/pack',{'packCode':'vs_pack_dept','packName':'VS部门场景包','description':'dept pack','status':'0','visibleScope':'1','ownerDeptId':dA},A)
  40. res['kb_dept']=call('POST','/ai/kb',{'kbName':'VS部门知识库','kbCode':'vs_kb_dept','description':'dept kb','embeddingModelId':EMB,'retrievalMode':'vector','status':'0','visibleScope':'1'},A)
  41. # A self records
  42. res['expert_self']=call('POST','/ai/agent',{'agentName':'VS自建专家','agentCode':'vs_expert_self','description':'self expert','modelId':CHAT,'status':'0','visibleScope':'2','autoRoute':'1','memoryEnabled':'0','expertWriteEnabled':'1'},A)
  43. res['skill_self']=call('POST','/ai/skill',{'name':'vs_skill_self','description':'self skill','status':'0','visibleScope':'2'},A)
  44. res['pack_self']=call('POST','/ai/pack',{'packCode':'vs_pack_self','packName':'VS自建场景包','description':'self pack','status':'0','visibleScope':'2'},A)
  45. res['kb_self']=call('POST','/ai/kb',{'kbName':'VS自建知识库','kbCode':'vs_kb_self','description':'self kb','embeddingModelId':EMB,'retrievalMode':'vector','status':'0','visibleScope':'2'},A)
  46. # A tries public (must fail)
  47. res['A_expert_pub_try']=call('POST','/ai/agent',{'agentName':'VS越权专家','agentCode':'vs_expert_pub_try','description':'x','modelId':CHAT,'status':'0','visibleScope':'0','autoRoute':'1','expertWriteEnabled':'1'},A)
  48. res['A_skill_pub_try']=call('POST','/ai/skill',{'name':'vs_skill_pub_try','description':'x','status':'0','visibleScope':'0'},A)
  49. res['A_pack_pub_try']=call('POST','/ai/pack',{'packCode':'vs_pack_pub_try','packName':'VS越权包','status':'0','visibleScope':'0'},A)
  50. res['A_kb_pub_try']=call('POST','/ai/kb',{'kbName':'VS越权库','kbCode':'vs_kb_pub_try','embeddingModelId':EMB,'retrievalMode':'vector','status':'0','visibleScope':'0'},A)
  51. print(json.dumps(res,ensure_ascii=False,indent=1))
  52. json.dump(res,open('create_res.json','w'),ensure_ascii=False,indent=2)