@@ -1193,6 +1193,70 @@ def update_tasks_models(self, request, pk=None):
11931193 {"message" : f"Error: { str (e )} " },
11941194 status = status .HTTP_400_BAD_REQUEST
11951195 )
1196+
1197+ @action (detail = True , methods = ["POST" ], url_path = "update_idc_tasks_model" )
1198+ def update_idc_tasks_model (self , request , pk = None ):
1199+ """
1200+ Update the model of all incomplete tasks in a Single IDC project if the current model is inactive.
1201+ """
1202+ from tasks .models import Task , INCOMPLETE
1203+ from dataset .models import ACTIVE_LLM_MODELS
1204+
1205+ try :
1206+ project = self .get_object ()
1207+
1208+ # Check permissions
1209+ if not (request .user .role == User .ORGANIZATION_OWNER or
1210+ request .user .is_superuser or
1211+ request .user .role == User .WORKSPACE_MANAGER ):
1212+ return Response (
1213+ {"message" : "You are not authorized to perform this action" },
1214+ status = status .HTTP_403_FORBIDDEN
1215+ )
1216+
1217+ if project .project_type != "InstructionDrivenChat" :
1218+ return Response (
1219+ {"message" : "This operation is only allowed for InstructionDrivenChat projects." },
1220+ status = status .HTTP_400_BAD_REQUEST
1221+ )
1222+
1223+ new_model = request .data .get ("new_model" )
1224+ if not new_model :
1225+ return Response (
1226+ {"message" : "new_model is required." },
1227+ status = status .HTTP_400_BAD_REQUEST
1228+ )
1229+
1230+ if new_model not in ACTIVE_LLM_MODELS :
1231+ return Response (
1232+ {"message" : f"{ new_model } is not an active LLM model." },
1233+ status = status .HTTP_400_BAD_REQUEST
1234+ )
1235+
1236+ tasks = Task .objects .filter (project_id = project , task_status = INCOMPLETE )
1237+ updated_count = 0
1238+ tasks_list = []
1239+
1240+ for task in tasks :
1241+ current_model = task .data .get ("model" )
1242+ if current_model not in ACTIVE_LLM_MODELS :
1243+ task .data ["model" ] = new_model
1244+ tasks_list .append (task )
1245+ updated_count += 1
1246+
1247+ if tasks_list :
1248+ Task .objects .bulk_update (tasks_list , ['data' ])
1249+
1250+ return Response ({
1251+ "message" : f"Updated { updated_count } incomplete tasks to model { new_model } ." ,
1252+ "updated_count" : updated_count
1253+ }, status = status .HTTP_200_OK )
1254+
1255+ except Exception as e :
1256+ return Response (
1257+ {"message" : f"Error: { str (e )} " },
1258+ status = status .HTTP_400_BAD_REQUEST
1259+ )
11961260
11971261 @swagger_auto_schema (
11981262 method = "get" ,
0 commit comments