Skip to content

Commit 7e85902

Browse files
authored
🐛 Fix ID verification exception when updating the model #1311
2 parents a28385d + 8e59434 commit 7e85902

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

backend/services/model_management_service.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,15 @@ async def list_provider_models_for_tenant(tenant_id: str, provider: str, model_t
195195
async def update_single_model_for_tenant(user_id: str, tenant_id: str, model_data: Dict[str, Any]):
196196
"""Update a single model by its model_id, ensuring display_name uniqueness."""
197197
try:
198-
existing_model_by_display = get_model_by_display_name(
199-
model_data["display_name"], tenant_id)
200-
if existing_model_by_display and existing_model_by_display["model_id"] != model_data["model_id"]:
198+
existing_model_by_display = get_model_by_display_name(model_data["display_name"], tenant_id)
199+
current_model_id = int(model_data["model_id"])
200+
existing_model_id = existing_model_by_display["model_id"] if existing_model_by_display else None
201+
202+
if existing_model_by_display and existing_model_id != current_model_id:
201203
raise ValueError(
202204
f"Name {model_data['display_name']} is already in use, please choose another display name")
203205

204-
update_model_record(model_data["model_id"], model_data, user_id)
206+
update_model_record(current_model_id, model_data, user_id)
205207
logging.debug(
206208
f"Model {model_data['display_name']} updated successfully")
207209
except Exception as e:

frontend/app/[locale]/chat/internal/chatInterface.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ export function ChatInterface() {
14021402
);
14031403
if (preprocessStep) {
14041404
const stoppedMessage =
1405-
(t("chatInterface.filePreprocessingStopped") as string) ||
1405+
(t("chatInterface.fileProcessingStopped") as string) ||
14061406
"File preprocessing stopped";
14071407
preprocessStep.content = stoppedMessage;
14081408
if (

frontend/public/locales/en/common.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"chatInterface.truncationSeparator": "; ",
3030
"chatInterface.fileParsingFailed": "File parsing failed",
3131
"chatInterface.fileSizeExceeded": "File size exceeds the limit, please upload smaller files",
32-
"chatInterface.filePreprocessingStopped": "File parsing stopped",
32+
"chatInterface.fileProcessingStopped": "File parsing stopped",
3333
"chatInterface.conversationStopped": "Conversation stopped",
3434
"chatInterface.errorProcessingRequest": "An error occurred while processing the request",
3535
"chatInterface.errorFetchingConversationList": "Failed to fetch conversation list during initialization:",

frontend/public/locales/zh/common.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"chatInterface.truncationSeparator": "",
3030
"chatInterface.fileParsingFailed": "文件解析失败",
3131
"chatInterface.fileSizeExceeded": "文件大小超出限制,请上传更小的文件",
32-
"chatInterface.filePreprocessingStopped": "文件解析已停止",
32+
"chatInterface.fileProcessingStopped": "文件解析已停止",
3333
"chatInterface.conversationStopped": "对话已停止",
3434
"chatInterface.errorProcessingRequest": "处理请求时发生错误",
3535
"chatInterface.errorFetchingConversationList": "初始化时获取对话列表失败:",

test/backend/services/test_model_management_service.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,12 @@ async def test_list_provider_models_for_tenant_exception():
484484
async def test_update_single_model_for_tenant_success():
485485
svc = import_svc()
486486

487-
model = {"model_id": "m1", "display_name": "name"}
487+
model = {"model_id": "1", "display_name": "name"}
488488
with mock.patch.object(svc, "get_model_by_display_name", return_value=None) as mock_get, \
489489
mock.patch.object(svc, "update_model_record") as mock_update:
490490
await svc.update_single_model_for_tenant("u1", "t1", model)
491491
mock_get.assert_called_once_with("name", "t1")
492-
mock_update.assert_called_once_with("m1", model, "u1")
492+
mock_update.assert_called_once_with(1, model, "u1")
493493

494494

495495
async def test_update_single_model_for_tenant_conflict():
@@ -502,6 +502,45 @@ async def test_update_single_model_for_tenant_conflict():
502502
assert "Failed to update model" in str(exc.value)
503503

504504

505+
async def test_update_single_model_for_tenant_same_model_no_conflict():
506+
"""Test that updating the same model with same display name doesn't raise conflict."""
507+
svc = import_svc()
508+
509+
model = {"model_id": "123", "display_name": "existing_name"}
510+
# Return the same model_id (as int) to simulate updating the same model
511+
with mock.patch.object(svc, "get_model_by_display_name", return_value={"model_id": 123}) as mock_get, \
512+
mock.patch.object(svc, "update_model_record") as mock_update:
513+
await svc.update_single_model_for_tenant("u1", "t1", model)
514+
mock_get.assert_called_once_with("existing_name", "t1")
515+
mock_update.assert_called_once_with(123, model, "u1")
516+
517+
518+
async def test_update_single_model_for_tenant_type_conversion():
519+
"""Test that string model_id is properly converted to int for comparison."""
520+
svc = import_svc()
521+
522+
model = {"model_id": "456", "display_name": "test_name"}
523+
# Return the same model_id as int to test type conversion
524+
with mock.patch.object(svc, "get_model_by_display_name", return_value={"model_id": 456}) as mock_get, \
525+
mock.patch.object(svc, "update_model_record") as mock_update:
526+
await svc.update_single_model_for_tenant("u1", "t1", model)
527+
mock_get.assert_called_once_with("test_name", "t1")
528+
mock_update.assert_called_once_with(456, model, "u1")
529+
530+
531+
async def test_update_single_model_for_tenant_different_model_conflict():
532+
"""Test that updating with a display name used by a different model raises conflict."""
533+
svc = import_svc()
534+
535+
model = {"model_id": "789", "display_name": "conflict_name"}
536+
# Return a different model_id to simulate name conflict
537+
with mock.patch.object(svc, "get_model_by_display_name", return_value={"model_id": 999}):
538+
with pytest.raises(Exception) as exc:
539+
await svc.update_single_model_for_tenant("u1", "t1", model)
540+
assert "Failed to update model" in str(exc.value)
541+
assert "Name conflict_name is already in use" in str(exc.value)
542+
543+
505544
async def test_batch_update_models_for_tenant_success():
506545
svc = import_svc()
507546

0 commit comments

Comments
 (0)