feat: add OPC (One Person Company) management system with Kanban board
Deploy Dashboard (Dev) / deploy-dev (push) Has been cancelled
Run Tests / Backend Tests (push) Has been cancelled
Run Tests / Frontend Tests (push) Has been cancelled
Run Tests / Test Summary (push) Has been cancelled

Phase 1 MVP implementation:
- PostgreSQL database schema for tasks, agents, executions, time tracking
- Backend API: task CRUD, agent management, automatic time tracking
- Frontend: Kanban board with drag-and-drop (Parking Lot → In Progress → Done)
- Task modal for creating/editing tasks with tags, priority, assignee
- 3 core agents seeded: PM, CTO, COO
- Agent approval workflow: CxO level requires approval, others auto-execute
- Automatic time tracking: starts on in_progress, stops on done
- RBAC integration: OPC page accessible to admin/member roles

Features:
- Drag-and-drop tasks between columns
- Assign tasks to humans or AI agents
- Priority badges (low, medium, high, urgent)
- Tags and due dates
- Task history tracking
- Time entry tracking with automatic timer
- Agent execution records with approval workflow

Next steps:
- Initialize OPC database on NAS
- Implement agent executor service
- Add WebSocket for real-time updates
- Add email notifications
- Add PDF invoice generation
This commit is contained in:
Gan, Jimmy
2026-03-31 14:31:31 +08:00
parent 51c4cd1a4e
commit 1ca019fa48
16 changed files with 1771 additions and 3 deletions
+10 -1
View File
@@ -7,10 +7,19 @@ from fastapi import APIRouter
from config import CC_CONNECT_URL, DOCKER_HOST
router = APIRouter()
client = docker.DockerClient(base_url=DOCKER_HOST, timeout=5)
_client = None
def get_docker_client():
"""Get or create Docker client (lazy initialization)."""
global _client
if _client is None:
_client = docker.DockerClient(base_url=DOCKER_HOST, timeout=5)
return _client
def _get_cc_connect_container():
client = get_docker_client()
matches = client.containers.list(all=True, filters={"name": "cc-connect"})
return matches[0] if matches else None
+116
View File
@@ -0,0 +1,116 @@
"""
OPC Agents Router
"""
from fastapi import APIRouter, Depends, HTTPException
from typing import Optional, List
from pydantic import BaseModel
from db import opc_db
from rbac import require_page, User, _inject_user, require_admin
router = APIRouter()
class AgentUpdate(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
system_prompt: Optional[str] = None
enabled: Optional[bool] = None
class ExecutionApproval(BaseModel):
approved: bool
@router.get("/agents")
async def list_agents(
enabled_only: bool = True,
user: User = Depends(_inject_user)
):
"""List all agents"""
agents = await opc_db.get_agents(enabled_only=enabled_only)
return {"items": agents}
@router.get("/agents/{agent_id}")
async def get_agent(
agent_id: str,
user: User = Depends(_inject_user)
):
"""Get agent details"""
agent = await opc_db.get_agent(agent_id)
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
return agent
@router.put("/agents/{agent_id}", dependencies=[Depends(require_admin())])
async def update_agent(
agent_id: str,
updates: AgentUpdate,
user: User = Depends(_inject_user)
):
"""Update agent configuration (admin only)"""
# This would need an update_agent function in opc_db
raise HTTPException(status_code=501, detail="Not implemented yet")
@router.post("/agents/{agent_id}/execute")
async def trigger_agent(
agent_id: str,
task_id: int,
user: User = Depends(_inject_user)
):
"""Manually trigger agent execution"""
execution = await opc_db.create_agent_execution(
task_id=task_id,
agent_id=agent_id,
actor=user.username
)
return execution
@router.get("/executions")
async def list_executions(
task_id: Optional[int] = None,
agent_id: Optional[str] = None,
status: Optional[str] = None,
limit: int = 50,
user: User = Depends(_inject_user)
):
"""List agent executions"""
executions = await opc_db.get_agent_executions(
task_id=task_id,
agent_id=agent_id,
status=status,
limit=limit
)
return {"items": executions}
@router.get("/executions/{execution_id}")
async def get_execution(
execution_id: int,
user: User = Depends(_inject_user)
):
"""Get execution details"""
executions = await opc_db.get_agent_executions(limit=1000)
execution = next((e for e in executions if e["id"] == execution_id), None)
if not execution:
raise HTTPException(status_code=404, detail="Execution not found")
return execution
@router.post("/executions/{execution_id}/approve")
async def approve_execution(
execution_id: int,
approval: ExecutionApproval,
user: User = Depends(_inject_user)
):
"""Approve or reject agent execution (for CxO level actions)"""
# This would need an approve_execution function in opc_db
# For now, return placeholder
return {
"execution_id": execution_id,
"approved": approval.approved,
"approved_by": user.username
}
+259
View File
@@ -0,0 +1,259 @@
"""
OPC Task Management Router
"""
from fastapi import APIRouter, Depends, Query, HTTPException
from typing import Optional, List
from pydantic import BaseModel
from datetime import datetime
from db import opc_db
from rbac import require_page, User, _inject_user
router = APIRouter()
class TaskCreate(BaseModel):
title: str
description: Optional[str] = None
status: str = "parking_lot"
priority: str = "medium"
project_id: Optional[int] = None
assigned_to: Optional[str] = None
assigned_type: str = "human"
tags: Optional[List[str]] = None
due_date: Optional[datetime] = None
class TaskUpdate(BaseModel):
title: Optional[str] = None
description: Optional[str] = None
status: Optional[str] = None
priority: Optional[str] = None
project_id: Optional[int] = None
assigned_to: Optional[str] = None
assigned_type: Optional[str] = None
tags: Optional[List[str]] = None
due_date: Optional[datetime] = None
completed_at: Optional[datetime] = None
class TaskMove(BaseModel):
status: str
class TaskAssign(BaseModel):
assigned_to: str
assigned_type: str = "human"
@router.get("/tasks")
async def list_tasks(
status: Optional[str] = Query(None),
assigned_to: Optional[str] = Query(None),
project_id: Optional[int] = Query(None),
priority: Optional[str] = Query(None),
tags: Optional[str] = Query(None),
limit: int = Query(50, le=100),
offset: int = Query(0, ge=0),
user: User = Depends(_inject_user)
):
"""List tasks with filters"""
tag_list = tags.split(",") if tags else None
tasks = await opc_db.get_tasks(
status=status,
assigned_to=assigned_to,
project_id=project_id,
priority=priority,
tags=tag_list,
limit=limit,
offset=offset
)
return {"items": tasks, "total": len(tasks)}
@router.post("/tasks")
async def create_task(
task: TaskCreate,
user: User = Depends(_inject_user)
):
"""Create a new task"""
created_task = await opc_db.create_task(
title=task.title,
description=task.description,
status=task.status,
priority=task.priority,
project_id=task.project_id,
assigned_to=task.assigned_to,
assigned_type=task.assigned_type,
tags=task.tags,
due_date=task.due_date,
actor=user.username
)
# Start automatic timer if status is in_progress
if task.status == "in_progress":
await opc_db.start_time_entry(
task_id=created_task["id"],
project_id=task.project_id,
actor=user.username
)
return created_task
@router.get("/tasks/{task_id}")
async def get_task(
task_id: int,
user: User = Depends(_inject_user)
):
"""Get task details"""
tasks = await opc_db.get_tasks(limit=1, offset=0)
task = next((t for t in tasks if t["id"] == task_id), None)
if not task:
raise HTTPException(status_code=404, detail="Task not found")
return task
@router.put("/tasks/{task_id}")
async def update_task(
task_id: int,
task: TaskUpdate,
user: User = Depends(_inject_user)
):
"""Update a task"""
updates = task.dict(exclude_unset=True)
# Get current task status
tasks = await opc_db.get_tasks(limit=1000, offset=0)
current_task = next((t for t in tasks if t["id"] == task_id), None)
if not current_task:
raise HTTPException(status_code=404, detail="Task not found")
# Handle automatic time tracking
old_status = current_task["status"]
new_status = updates.get("status", old_status)
# Start timer when moving to in_progress
if old_status != "in_progress" and new_status == "in_progress":
await opc_db.start_time_entry(
task_id=task_id,
project_id=current_task.get("project_id"),
actor=user.username
)
# Stop timer when moving away from in_progress
if old_status == "in_progress" and new_status != "in_progress":
await opc_db.stop_time_entry(task_id=task_id)
# Set completed_at when moving to done
if new_status == "done" and old_status != "done":
updates["completed_at"] = datetime.utcnow()
updated_task = await opc_db.update_task(
task_id=task_id,
updates=updates,
actor=user.username
)
return updated_task
@router.delete("/tasks/{task_id}")
async def delete_task(
task_id: int,
user: User = Depends(_inject_user)
):
"""Delete a task"""
await opc_db.delete_task(task_id=task_id, actor=user.username)
return {"ok": True}
@router.put("/tasks/{task_id}/move")
async def move_task(
task_id: int,
move: TaskMove,
user: User = Depends(_inject_user)
):
"""Move task to a different column"""
updates = {"status": move.status}
# Get current task
tasks = await opc_db.get_tasks(limit=1000, offset=0)
current_task = next((t for t in tasks if t["id"] == task_id), None)
if not current_task:
raise HTTPException(status_code=404, detail="Task not found")
old_status = current_task["status"]
# Handle automatic time tracking
if old_status != "in_progress" and move.status == "in_progress":
await opc_db.start_time_entry(
task_id=task_id,
project_id=current_task.get("project_id"),
actor=user.username
)
if old_status == "in_progress" and move.status != "in_progress":
await opc_db.stop_time_entry(task_id=task_id)
if move.status == "done":
updates["completed_at"] = datetime.utcnow()
updated_task = await opc_db.update_task(
task_id=task_id,
updates=updates,
actor=user.username
)
return updated_task
@router.post("/tasks/{task_id}/assign")
async def assign_task(
task_id: int,
assign: TaskAssign,
user: User = Depends(_inject_user)
):
"""Assign task to human or agent"""
updates = {
"assigned_to": assign.assigned_to,
"assigned_type": assign.assigned_type
}
updated_task = await opc_db.update_task(
task_id=task_id,
updates=updates,
actor=user.username
)
# If assigning to agent, create execution record
if assign.assigned_type == "agent":
await opc_db.create_agent_execution(
task_id=task_id,
agent_id=assign.assigned_to,
actor=user.username
)
return updated_task
@router.get("/tasks/{task_id}/history")
async def get_task_history(
task_id: int,
user: User = Depends(_inject_user)
):
"""Get task history"""
history = await opc_db.get_task_history(task_id=task_id)
return {"items": history}
@router.get("/tasks/{task_id}/time")
async def get_task_time(
task_id: int,
user: User = Depends(_inject_user)
):
"""Get time entries for a task"""
entries = await opc_db.get_time_entries(task_id=task_id)
total_minutes = sum(e["duration_minutes"] for e in entries)
return {
"entries": entries,
"total_minutes": total_minutes,
"total_hours": round(total_minutes / 60, 2)
}