Spaces:
Running
Running
Update project_controller.py
Browse files- project_controller.py +54 -0
project_controller.py
CHANGED
@@ -102,6 +102,60 @@ async def publish_project(request: Request):
|
|
102 |
|
103 |
return {"message": f"Project {project_name} version published and new draft version {new_version_number} created"}
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
@router.post("/seed/test_data")
|
106 |
def seed_test_data():
|
107 |
service_config.projects = {
|
|
|
102 |
|
103 |
return {"message": f"Project {project_name} version published and new draft version {new_version_number} created"}
|
104 |
|
105 |
+
@router.post("/add_intent")
|
106 |
+
async def add_intent(request: Request):
|
107 |
+
data = await request.json()
|
108 |
+
project_name = data.get("project_name")
|
109 |
+
intent_name = data.get("intent_name")
|
110 |
+
|
111 |
+
project = service_config.projects.get(project_name)
|
112 |
+
if not project:
|
113 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
114 |
+
|
115 |
+
latest = max(project["versions"], key=lambda v: v["version_number"])
|
116 |
+
if any(intent["name"] == intent_name for intent in latest.get("intents", [])):
|
117 |
+
raise HTTPException(status_code=400, detail="Intent with this name already exists.")
|
118 |
+
|
119 |
+
latest.setdefault("intents", []).append({
|
120 |
+
"name": intent_name,
|
121 |
+
"examples": [],
|
122 |
+
"parameters": [],
|
123 |
+
"action": "",
|
124 |
+
"fallback_timeout_message": "",
|
125 |
+
"fallback_error_message": "",
|
126 |
+
"humanization_prompt": ""
|
127 |
+
})
|
128 |
+
project["last_updated"] = get_utc_now()
|
129 |
+
|
130 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
131 |
+
json.dump(service_config, f, indent=2)
|
132 |
+
|
133 |
+
return {"message": f"Intent {intent_name} added to project {project_name}"}
|
134 |
+
|
135 |
+
@router.post("/delete_intent")
|
136 |
+
async def delete_intent(request: Request):
|
137 |
+
data = await request.json()
|
138 |
+
project_name = data.get("project_name")
|
139 |
+
intent_name = data.get("intent_name")
|
140 |
+
|
141 |
+
project = service_config.projects.get(project_name)
|
142 |
+
if not project:
|
143 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
144 |
+
|
145 |
+
latest = max(project["versions"], key=lambda v: v["version_number"])
|
146 |
+
intents = latest.get("intents", [])
|
147 |
+
new_intents = [intent for intent in intents if intent["name"] != intent_name]
|
148 |
+
if len(new_intents) == len(intents):
|
149 |
+
raise HTTPException(status_code=404, detail="Intent not found.")
|
150 |
+
|
151 |
+
latest["intents"] = new_intents
|
152 |
+
project["last_updated"] = get_utc_now()
|
153 |
+
|
154 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
155 |
+
json.dump(service_config, f, indent=2)
|
156 |
+
|
157 |
+
return {"message": f"Intent {intent_name} deleted from project {project_name}"}
|
158 |
+
|
159 |
@router.post("/seed/test_data")
|
160 |
def seed_test_data():
|
161 |
service_config.projects = {
|