JoeYing
Add ReScopeBench cases and task preview
f726fd6
Raw History Blame Contribute Delete
9.57 kB
"""Write tool implementations."""
from __future__ import annotations
from mcp_servers.course_logic import *
def register_student_in_section(state_manager, student_id=None, section_id=None, grading_basis="letter"):
if _missing(student_id):
return error_response("MISSING_STUDENT_ID", "student_id is required")
if _missing(section_id):
return error_response("MISSING_SECTION_ID", "section_id is required")
if grading_basis not in GRADING_BASES:
return error_response("INVALID_GRADING_BASIS", "grading_basis is not supported")
student, section, course, err = _validate_registration_eligibility(state_manager, student_id, section_id)
if err:
return err
if int(section.get("available_seats", 0) or 0) <= 0:
return error_response("SECTION_FULL", "section has no available seats")
stamp = now_iso()
registration = {"registration_id": _new_registration_id(state_manager, student_id, section), "student_id": student_id, "section_id": section_id, "course_id": section["course_id"], "term_id": section["term_id"], "status": "registered", "grading_basis": grading_basis, "source": "self_service", "registered_at": stamp, "dropped_at": None, "created_at": stamp, "updated_at": stamp}
updated_section = safe_copy(section)
updated_section["enrolled_count"] = int(updated_section.get("enrolled_count", 0) or 0) + 1
updated_section["available_seats"] = max(0, int(updated_section.get("seat_capacity", 0) or 0) - updated_section["enrolled_count"])
updated_section["status"] = _derive_section_status(updated_section)
updated_section["updated_at"] = stamp
_add(state_manager, "registration", registration)
_add(state_manager, "course_section", updated_section)
_update(state_manager, "student", student_id, {"updated_at": stamp})
return success_response({"registration_id": registration["registration_id"], "student_id": student_id, "section_id": section_id, "course_id": section["course_id"], "term_id": section["term_id"], "status": "registered", "grading_basis": grading_basis, "source": "self_service", "registered_at": stamp, "section_seat_summary": _section_summary(updated_section)})
def add_student_to_waitlist(state_manager, student_id=None, section_id=None):
if _missing(student_id):
return error_response("MISSING_STUDENT_ID", "student_id is required")
if _missing(section_id):
return error_response("MISSING_SECTION_ID", "section_id is required")
student, section, course, err = _validate_registration_eligibility(state_manager, student_id, section_id)
if err:
return err
if int(section.get("available_seats", 0) or 0) != 0:
return error_response("SECTION_HAS_OPEN_SEAT", "section still has an open seat")
if int(section.get("waitlist_count", 0) or 0) >= int(section.get("waitlist_capacity", 0) or 0):
return error_response("WAITLIST_FULL", "section waitlist is already full")
stamp = now_iso()
waiting = _current_waiting_entries(state_manager, section_id)
position = max([entry.get("position_number", 0) for entry in waiting] or [0]) + 1
entry = {"waitlist_entry_id": _new_waitlist_id(state_manager, student_id, section), "student_id": student_id, "section_id": section_id, "course_id": section["course_id"], "term_id": section["term_id"], "status": "waiting", "position_number": position, "joined_at": stamp, "promoted_at": None, "removed_at": None, "removal_reason": "none", "created_at": stamp, "updated_at": stamp}
updated_section = safe_copy(section)
updated_section["waitlist_count"] = int(updated_section.get("waitlist_count", 0) or 0) + 1
updated_section["updated_at"] = stamp
_add(state_manager, "waitlist_entry", entry)
_add(state_manager, "course_section", updated_section)
_update(state_manager, "student", student_id, {"updated_at": stamp})
return success_response({"waitlist_entry_id": entry["waitlist_entry_id"], "student_id": student_id, "section_id": section_id, "course_id": section["course_id"], "term_id": section["term_id"], "status": "waiting", "position_number": position, "joined_at": stamp, "queue_summary": _queue_summary(state_manager, section_id)})
def change_registration_grading_basis(state_manager, registration_id=None, grading_basis=None):
if _missing(registration_id):
return error_response("MISSING_REGISTRATION_ID", "registration_id is required")
if grading_basis not in GRADING_BASES:
return error_response("INVALID_GRADING_BASIS", "grading_basis is not supported")
registration = _get(state_manager, "registration", registration_id)
if registration is None:
return error_response("REGISTRATION_NOT_FOUND", "no registration matches registration_id")
if registration.get("status") != "registered":
return error_response("REGISTRATION_NOT_ACTIVE", "registration is not active")
if registration.get("grading_basis") == grading_basis:
return error_response("GRADING_BASIS_UNCHANGED", "requested grading basis matches the current value")
stamp = now_iso()
old_basis = registration.get("grading_basis")
updated = safe_copy(registration)
updated["grading_basis"] = grading_basis
updated["updated_at"] = stamp
_add(state_manager, "registration", updated)
return success_response({"registration_id": registration_id, "student_id": updated["student_id"], "section_id": updated["section_id"], "course_id": updated["course_id"], "term_id": updated["term_id"], "status": updated["status"], "old_grading_basis": old_basis, "grading_basis": grading_basis, "updated_at": stamp})
def cancel_student_registration(state_manager, registration_id=None, drop_reason="student_request"):
if _missing(registration_id):
return error_response("MISSING_REGISTRATION_ID", "registration_id is required")
if drop_reason not in DROP_REASONS:
return error_response("INVALID_DROP_REASON", "drop_reason is not supported")
registration = _get(state_manager, "registration", registration_id)
if registration is None:
return error_response("REGISTRATION_NOT_FOUND", "no registration matches registration_id")
if registration.get("status") != "registered":
return error_response("REGISTRATION_NOT_ACTIVE", "registration is not active")
term = _get(state_manager, "term", registration["term_id"])
if term and term.get("status") == "completed":
return error_response("TERM_ALREADY_COMPLETED", "related term is already completed")
section = _get(state_manager, "course_section", registration["section_id"])
stamp = now_iso()
updated_registration = safe_copy(registration)
updated_registration.update({"status": "dropped", "dropped_at": stamp, "updated_at": stamp})
_add(state_manager, "registration", updated_registration)
updated_section = safe_copy(section) if section else None
if updated_section:
updated_section["enrolled_count"] = max(0, int(updated_section.get("enrolled_count", 0) or 0) - 1)
updated_section["available_seats"] = max(0, int(updated_section.get("seat_capacity", 0) or 0) - updated_section["enrolled_count"])
updated_section["status"] = _derive_section_status(updated_section)
updated_section["updated_at"] = stamp
_add(state_manager, "course_section", updated_section)
_update(state_manager, "student", registration["student_id"], {"updated_at": stamp})
return success_response({"registration_id": registration_id, "student_id": registration["student_id"], "section_id": registration["section_id"], "course_id": registration["course_id"], "term_id": registration["term_id"], "status": "dropped", "dropped_at": stamp, "drop_reason": drop_reason, "section_seat_summary": _section_summary(updated_section) if updated_section else None})
def remove_student_from_waitlist(state_manager, waitlist_entry_id=None, removal_reason=None):
if _missing(waitlist_entry_id):
return error_response("MISSING_WAITLIST_ENTRY_ID", "waitlist_entry_id is required")
if removal_reason not in REMOVAL_REASONS:
return error_response("INVALID_REMOVAL_REASON", "removal_reason is not supported")
entry = _get(state_manager, "waitlist_entry", waitlist_entry_id)
if entry is None:
return error_response("WAITLIST_ENTRY_NOT_FOUND", "no waitlist entry matches waitlist_entry_id")
if entry.get("status") != "waiting":
return error_response("WAITLIST_ENTRY_NOT_WAITING", "waitlist entry is not waiting")
stamp = now_iso()
removed_position = int(entry.get("position_number", 0) or 0)
updated_entry = safe_copy(entry)
updated_entry.update({"status": "removed", "removed_at": stamp, "removal_reason": removal_reason, "updated_at": stamp})
_add(state_manager, "waitlist_entry", updated_entry)
resequenced = _resequence_after(state_manager, entry["section_id"], removed_position)
section = _get(state_manager, "course_section", entry["section_id"])
if section:
updated_section = safe_copy(section)
updated_section["waitlist_count"] = max(0, int(updated_section.get("waitlist_count", 0) or 0) - 1)
updated_section["updated_at"] = stamp
_add(state_manager, "course_section", updated_section)
_update(state_manager, "student", entry["student_id"], {"updated_at": stamp})
return success_response({"waitlist_entry_id": waitlist_entry_id, "student_id": entry["student_id"], "section_id": entry["section_id"], "status": "removed", "removed_at": stamp, "removal_reason": removal_reason, "resequenced_entries": resequenced})