feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持

This commit is contained in:
hugo committed 2026-07-07 11:10:32 +08:00
1 parent ac4262cd75
commit c8ce77199e
183 files changed
+11690 -2300

No files matched your search

+14 -11
View File
@@ -1,6 +1,7 @@
import pytest
from app.models import Agent, ChatLog, User, PermissionRequest
from app import db
from tests.conftest import parse_api_data, parse_api_error
def test_agent_lifecycle(client, admin_headers):
"""
@@ -17,15 +18,17 @@ def test_agent_lifecycle(client, admin_headers):
}
response = client.post('/api/admin/agents', json=new_agent, headers=admin_headers)
assert response.status_code == 201
agent_id = response.json['id']
agent_id = parse_api_data(response.json)['id']
# 2. Read Agent List
response = client.get('/api/admin/agents', headers=admin_headers)
assert response.status_code == 200
agents = response.json
agents = parse_api_data(response.json)
assert len(agents) == 1
assert agents[0]['name'] == "Test Agent"
assert agents[0]['description'] == "Initial Description"
assert 'dify_api_key' not in agents[0]
assert agents[0]['dify_api_key_masked']
# 3. Update Agent
updated_data = {
@@ -37,7 +40,7 @@ def test_agent_lifecycle(client, admin_headers):
# Verify Update
response = client.get('/api/admin/agents', headers=admin_headers)
agents = response.json
agents = parse_api_data(response.json)
assert agents[0]['name'] == "Updated Agent"
assert agents[0]['description'] == "Updated Description"
@@ -47,7 +50,7 @@ def test_agent_lifecycle(client, admin_headers):
# Verify Deletion
response = client.get('/api/admin/agents', headers=admin_headers)
assert len(response.json) == 0
assert len(parse_api_data(response.json)) == 0
def test_user_management(client, admin_headers):
"""
@@ -86,7 +89,7 @@ def test_user_management(client, admin_headers):
"dify_app_id": "p1",
"dify_api_key": "k1"
}, headers=admin_headers)
agent_id = agent_res.json['id']
agent_id = parse_api_data(agent_res.json)['id']
response = client.post(f'/api/admin/users/{user_id}/permissions', json={
"agent_ids": [agent_id]
@@ -155,7 +158,7 @@ def test_batch_delete_users_with_permissions(client, admin_headers):
'dify_api_key': 'del-key',
}, headers=admin_headers)
assert agent.status_code == 201
agent_id = agent.json['id']
agent_id = parse_api_data(agent.json)['id']
perm_res = client.post(f'/api/admin/users/{user_id}/permissions', json={
'agent_ids': [agent_id],
@@ -184,21 +187,21 @@ def test_agent_update_validation(client, admin_headers):
"is_active": True
}
response = client.post('/api/admin/agents', json=new_agent, headers=admin_headers)
agent_id = response.json['id']
agent_id = parse_api_data(response.json)['id']
# 1. Update with Valid Data
response = client.put(f'/api/admin/agents/{agent_id}', json={"dify_api_key": "new-key"}, headers=admin_headers)
assert response.status_code == 200
# 2. Try to Clear Key for API Agent (Should Fail)
# 2. 空 key 视为未提交,保留已有 key(write-only)
response = client.put(f'/api/admin/agents/{agent_id}', json={"dify_api_key": ""}, headers=admin_headers)
assert response.status_code == 400
assert "API mode requires Dify API Key" in response.json['error']
assert response.status_code == 200
# 3. Try to Clear App ID for API Agent (Should Fail)
response = client.put(f'/api/admin/agents/{agent_id}', json={"dify_app_id": ""}, headers=admin_headers)
assert response.status_code == 400
assert "API mode requires Dify App ID" in response.json['error']
error = parse_api_error(response.json)
assert 'dify_app_id' in error.get('message', '')
def test_permission_requests(client, app, admin_headers):
"""