migrate_postgres.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from tool_agent.registry.registry import ToolRegistry
  2. from tool_agent.tool.tool_store import PostgreSQLToolStore
  3. from tool_agent.models import ToolStatus
  4. def migrate():
  5. registry = ToolRegistry()
  6. store = PostgreSQLToolStore()
  7. local_tools = registry.list_all()
  8. remote_tools = {kt["id"]: kt for kt in store.list_all(limit=1000)}
  9. print(f"Loaded {len(local_tools)} local tools, {len(remote_tools)} remote tools")
  10. for tool in local_tools:
  11. tid = tool.tool_id
  12. # Merge logic: favor remote's name/intro if it exists
  13. remote_t = remote_tools.get(tid, {})
  14. tool_dict = {
  15. "id": tid,
  16. "name": remote_t.get("name") or tool.name,
  17. "version": remote_t.get("version") or tool.version,
  18. "introduction": remote_t.get("introduction") or tool.description,
  19. "input": tool.input_schema, # overwrite with local JSON schema
  20. "output": tool.output_schema,
  21. "status": "已接入" if tool.status == ToolStatus.ACTIVE else "未接入"
  22. }
  23. store.insert_or_update(tool_dict)
  24. print(f"Migrated / Synced tool: {tid}")
  25. print("Migration complete!")
  26. if __name__ == '__main__':
  27. migrate()