feat: add ordered agent runtime
This commit is contained in:
1
backend/apps/agents/tests/__init__.py
Normal file
1
backend/apps/agents/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
91
backend/apps/agents/tests/helpers.py
Normal file
91
backend/apps/agents/tests/helpers.py
Normal file
@@ -0,0 +1,91 @@
|
||||
from apps.agents.models import AgentDefinition, AgentStepBinding, AgentVersion
|
||||
from apps.modelhub.crypto import encrypt_secret
|
||||
from apps.modelhub.models import ModelDeployment, ModelProvider
|
||||
from apps.skills.models import SkillDefinition, SkillVersion
|
||||
|
||||
|
||||
def create_deployment():
|
||||
provider = ModelProvider.objects.create(
|
||||
name="模型渠道",
|
||||
base_url="https://models.example.com/v1",
|
||||
api_key_encrypted=encrypt_secret("secret"),
|
||||
)
|
||||
return ModelDeployment.objects.create(
|
||||
provider=provider,
|
||||
display_name="支持模型",
|
||||
model_name="support-model",
|
||||
)
|
||||
|
||||
|
||||
def create_skill(code: str, output_schema: dict, input_schema: dict | None = None):
|
||||
definition = SkillDefinition.objects.create(code=code, name=code)
|
||||
version = SkillVersion.objects.create(
|
||||
definition=definition,
|
||||
version=1,
|
||||
status="published",
|
||||
prompt_template="处理:{{ input.value }}",
|
||||
input_schema=input_schema
|
||||
or {
|
||||
"type": "object",
|
||||
"properties": {"value": {}},
|
||||
"required": ["value"],
|
||||
},
|
||||
output_schema=output_schema,
|
||||
)
|
||||
definition.active_version = version
|
||||
definition.save(update_fields=["active_version"])
|
||||
return version
|
||||
|
||||
|
||||
def create_agent(deployment=None, *, published=False, two_steps=True):
|
||||
deployment = deployment or create_deployment()
|
||||
first_skill = create_skill(
|
||||
"first-skill",
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {"summary": {"type": "string"}},
|
||||
"required": ["summary"],
|
||||
},
|
||||
)
|
||||
second_skill = None
|
||||
if two_steps:
|
||||
second_skill = create_skill(
|
||||
"second-skill",
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {"reply": {"type": "string"}},
|
||||
"required": ["reply"],
|
||||
},
|
||||
)
|
||||
definition = AgentDefinition.objects.create(code="support-agent", name="支持 Agent")
|
||||
version = AgentVersion.objects.create(
|
||||
definition=definition,
|
||||
version=1,
|
||||
status="published" if published else "draft",
|
||||
model_deployment=deployment,
|
||||
input_schema={
|
||||
"type": "object",
|
||||
"properties": {"problem": {"type": "string"}},
|
||||
"required": ["problem"],
|
||||
},
|
||||
example_input={"problem": "无法登录"},
|
||||
)
|
||||
AgentStepBinding.objects.create(
|
||||
agent_version=version,
|
||||
code="structure",
|
||||
order=1,
|
||||
skill_version=first_skill,
|
||||
input_mapping={"value": "$input.problem"},
|
||||
)
|
||||
if second_skill:
|
||||
AgentStepBinding.objects.create(
|
||||
agent_version=version,
|
||||
code="reply",
|
||||
order=2,
|
||||
skill_version=second_skill,
|
||||
input_mapping={"value": "$steps.structure.output.summary"},
|
||||
)
|
||||
if published:
|
||||
definition.active_version = version
|
||||
definition.save(update_fields=["active_version"])
|
||||
return definition, version
|
||||
76
backend/apps/agents/tests/test_agents_api.py
Normal file
76
backend/apps/agents/tests/test_agents_api.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import call_command
|
||||
|
||||
from apps.modelhub.services import CompletionResult
|
||||
|
||||
from .helpers import create_agent, create_deployment
|
||||
|
||||
|
||||
def login_admin(client):
|
||||
call_command("bootstrap_governance")
|
||||
user = get_user_model().objects.get(username="admin")
|
||||
client.force_login(user)
|
||||
return user
|
||||
|
||||
|
||||
def test_admin_publishes_and_runs_agent(client, db, monkeypatch):
|
||||
login_admin(client)
|
||||
definition, version = create_agent()
|
||||
monkeypatch.setattr(
|
||||
"apps.modelhub.services._request_completion",
|
||||
lambda *args, **kwargs: CompletionResult('{"summary":"ok","reply":"ok"}', 3, 2),
|
||||
)
|
||||
|
||||
published = client.post(f"/api/agents/{definition.id}/publish")
|
||||
assert published.status_code == 200
|
||||
assert published.json()["status"] == "published"
|
||||
|
||||
run = client.post(
|
||||
f"/api/agents/{definition.id}/runs",
|
||||
data={"version": 1, "input": {"problem": "无法登录"}},
|
||||
content_type="application/json",
|
||||
)
|
||||
assert run.status_code == 200
|
||||
assert run.json()["status"] == "succeeded"
|
||||
public_id = run.json()["public_id"]
|
||||
detail = client.get(f"/api/agent-runs/{public_id}")
|
||||
assert detail.status_code == 200
|
||||
assert detail.json()["config_snapshot"]["agent"]["version"] == 1
|
||||
|
||||
|
||||
def test_publishing_rejects_draft_skill(client, db):
|
||||
login_admin(client)
|
||||
definition, version = create_agent()
|
||||
binding = version.steps.first()
|
||||
binding.skill_version.status = "draft"
|
||||
binding.skill_version.save(update_fields=["status"])
|
||||
|
||||
response = client.post(f"/api/agents/{definition.id}/publish")
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "已发布 Skill" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_agent_run_permission_is_required(client, db):
|
||||
definition, _ = create_agent(published=True)
|
||||
user = get_user_model().objects.create_user(username="requester", password="password")
|
||||
client.force_login(user)
|
||||
|
||||
response = client.post(
|
||||
f"/api/agents/{definition.id}/runs",
|
||||
data={"version": 1, "input": {"problem": "无法登录"}},
|
||||
content_type="application/json",
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_seed_command_creates_support_agent(client, db):
|
||||
deployment = create_deployment()
|
||||
|
||||
call_command("seed_ai_demo", deployment_id=deployment.id)
|
||||
|
||||
from apps.agents.models import AgentDefinition
|
||||
|
||||
agent = AgentDefinition.objects.get(code="support-analysis")
|
||||
assert agent.versions.get(status="draft").steps.count() == 3
|
||||
114
backend/apps/agents/tests/test_runtime.py
Normal file
114
backend/apps/agents/tests/test_runtime.py
Normal file
@@ -0,0 +1,114 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.agents.models import AgentRun
|
||||
from apps.agents.services import build_config_snapshot, execute_agent_run
|
||||
from apps.agents.tasks import cleanup_expired_agent_runs
|
||||
from apps.modelhub.services import CompletionResult
|
||||
|
||||
from .helpers import create_agent
|
||||
|
||||
|
||||
def create_run(version):
|
||||
return AgentRun.objects.create(
|
||||
definition=version.definition,
|
||||
agent_version=version,
|
||||
sandbox=version.status == "draft",
|
||||
input_payload={"problem": "无法登录"},
|
||||
config_snapshot=build_config_snapshot(version),
|
||||
total_steps=version.steps.count(),
|
||||
)
|
||||
|
||||
|
||||
def test_ordered_runtime_records_steps_usage_and_final_output(db, monkeypatch):
|
||||
_, version = create_agent()
|
||||
outputs = iter(
|
||||
[
|
||||
CompletionResult('{"summary":"账号异常"}', 10, 5),
|
||||
CompletionResult('{"reply":"请重置密码"}', 12, 6),
|
||||
]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"apps.modelhub.services._request_completion",
|
||||
lambda *args, **kwargs: next(outputs),
|
||||
)
|
||||
run = create_run(version)
|
||||
|
||||
execute_agent_run(run.id)
|
||||
|
||||
run.refresh_from_db()
|
||||
assert run.status == "succeeded"
|
||||
assert run.final_output == {"reply": "请重置密码"}
|
||||
assert run.input_tokens == 22
|
||||
assert run.output_tokens == 11
|
||||
assert list(run.steps.values_list("code", flat=True)) == ["structure", "reply"]
|
||||
|
||||
|
||||
def test_invalid_structure_is_repaired_once(db, monkeypatch):
|
||||
_, version = create_agent(two_steps=False)
|
||||
outputs = iter(
|
||||
[
|
||||
CompletionResult("not-json", 4, 2),
|
||||
CompletionResult('{"summary":"已修复"}', 5, 3),
|
||||
]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"apps.modelhub.services._request_completion",
|
||||
lambda *args, **kwargs: next(outputs),
|
||||
)
|
||||
run = create_run(version)
|
||||
|
||||
execute_agent_run(run.id)
|
||||
|
||||
run.refresh_from_db()
|
||||
step = run.steps.get()
|
||||
assert run.status == "succeeded"
|
||||
assert step.attempts.count() == 2
|
||||
assert list(step.attempts.values_list("attempt_type", flat=True)) == ["initial", "repair"]
|
||||
|
||||
|
||||
def test_second_invalid_structure_fails_and_stops(db, monkeypatch):
|
||||
_, version = create_agent()
|
||||
outputs = iter(
|
||||
[
|
||||
CompletionResult("not-json", 4, 2),
|
||||
CompletionResult("still-not-json", 5, 3),
|
||||
]
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"apps.modelhub.services._request_completion",
|
||||
lambda *args, **kwargs: next(outputs),
|
||||
)
|
||||
run = create_run(version)
|
||||
|
||||
execute_agent_run(run.id)
|
||||
|
||||
run.refresh_from_db()
|
||||
assert run.status == "failed"
|
||||
assert run.steps.count() == 1
|
||||
assert run.steps.get().status == "failed"
|
||||
|
||||
|
||||
def test_cancellation_before_first_step_stops_run(db):
|
||||
_, version = create_agent()
|
||||
run = create_run(version)
|
||||
run.cancellation_requested_at = timezone.now()
|
||||
run.save(update_fields=["cancellation_requested_at"])
|
||||
|
||||
execute_agent_run(run.id)
|
||||
|
||||
run.refresh_from_db()
|
||||
assert run.status == "cancelled"
|
||||
assert run.steps.count() == 0
|
||||
|
||||
|
||||
def test_cleanup_removes_expired_runs_but_keeps_audit(db, settings):
|
||||
_, version = create_agent()
|
||||
run = create_run(version)
|
||||
AgentRun.objects.filter(id=run.id).update(created_at=timezone.now() - timedelta(days=91))
|
||||
|
||||
deleted = cleanup_expired_agent_runs()
|
||||
|
||||
assert deleted == 1
|
||||
assert not AgentRun.objects.filter(id=run.id).exists()
|
||||
Reference in New Issue
Block a user