feat: Add collaboration tool and UI

Integrates real-time document collaboration with a new tool and UI.

Co-authored-by: nicsins <nicsins@gmail.com>
This commit is contained in:
Cursor Agent 2025-12-20 20:56:50 +00:00
parent f3c41bca08
commit 8df4fc3b19
8 changed files with 225 additions and 0 deletions

32
python/collaboration.py Normal file
View file

@ -0,0 +1,32 @@
from flask_socketio import SocketIO, emit, join_room, leave_room
from flask import request
socketio = SocketIO(cors_allowed_origins="*")
# In-memory store for documents: {doc_id: content}
documents = {}
def init_collaboration(app):
socketio.init_app(app)
@socketio.on('connect')
def handle_connect():
pass
@socketio.on('join_document')
def on_join(data):
room = data.get('doc_id', 'default')
join_room(room)
# Send current document state
content = documents.get(room, "")
emit('document_state', {'content': content}, room=request.sid)
@socketio.on('update_document')
def on_update(data):
room = data.get('doc_id', 'default')
content = data.get('content', '')
documents[room] = content
# Broadcast to others in the room
emit('document_updated', {'content': content}, room=room, include_self=False)
return socketio

View file

@ -0,0 +1,24 @@
from python.helpers.tool import Tool, Response
from python.collaboration import documents, socketio
class CollaborationTool(Tool):
async def execute(self, action, content="", doc_id="default", **kwargs):
if action == "read":
text = documents.get(doc_id, "")
return Response(message=f"Document content:\n{text}", break_loop=False)
elif action == "write":
# Overwrite
documents[doc_id] = content
socketio.emit('document_updated', {'content': content}, room=doc_id)
return Response(message="Document updated.", break_loop=False)
elif action == "append":
current = documents.get(doc_id, "")
new_content = current + "\n" + content
documents[doc_id] = new_content
socketio.emit('document_updated', {'content': new_content}, room=doc_id)
return Response(message="Appended to document.", break_loop=False)
else:
return Response(message="Unknown action. Use read, write, or append.", break_loop=False)