chore: initialize project repository

This commit is contained in:
Codex
2026-09-09 21:55:37 +08:00
commit b1497fd2c6
167 changed files with 17690 additions and 0 deletions
@@ -0,0 +1,66 @@
from datetime import UTC, datetime
from uuid import uuid4
from sqlalchemy import delete, text
from app.core.contracts import AgentDefinition, AgentRequest, CoreResult, RequestContext
from app.infrastructure.db import SessionFactory
from app.model.memory import MemoryUnit
from app.service.agent.base import BaseAgent
from app.service.agent.factory import AgentFactory
async def test_real_memory_and_reviewed_negative_rule_are_used():
customer_id = uuid4().int % 10**15 + 10**15
agent_type = f"test_{uuid4().hex[:20]}"
memory_uuid = str(uuid4())
rule_code = f"test-{uuid4()}"
now = datetime.now(UTC).replace(tzinfo=None)
async with SessionFactory() as session, session.begin():
for user_id in (customer_id, customer_id + 1):
await session.execute(text("""
INSERT INTO sys_user
(id,user_no,username,password_hash,user_type,professional_investor_status,
fund_account_status,status,created_at,updated_at)
VALUES (:id,:name,:name,'test-only','员工','未申请','未开户','正常',UTC_TIMESTAMP(),
UTC_TIMESTAMP())
"""), {"id": user_id, "name": f"gov-{user_id}"})
session.add(MemoryUnit(
memory_uuid=memory_uuid, customer_id=customer_id, memory_key="test_preference",
content="测试低风险偏好", memory_type="fact", source_type="conversation",
source_confidence=0.8, confidence=0.8, status="active", valid_from=now,
version=1, created_at=now, updated_at=now,
))
await session.execute(text("""
INSERT INTO agent_negative_word
(rule_code,word_pattern,match_type,category,severity,applicable_agents,status,
created_by,reviewer_id,reviewed_at)
VALUES (:code,'测试禁止词','contains','test','block',JSON_ARRAY(:agent),'active',:c,:r,
UTC_TIMESTAMP())
"""), {"code": rule_code, "agent": agent_type, "c": customer_id, "r": customer_id + 1})
try:
class TestAgent(BaseAgent):
async def handle(self, request, context):
assert self.config is not None and self.config.config_version
assert self.memories[0].memory_uuid == memory_uuid
assert self.memories[0].customer_id == str(customer_id)
return CoreResult(text="测试禁止词")
definition = AgentDefinition(agent_type=agent_type, version="1",
allowed_roles=("customer",), allowed_portals=("api",))
factory = AgentFactory()
factory.register(definition, lambda _: TestAgent(definition))
context = RequestContext(user_id=str(customer_id), trace_id="test",
roles=("customer",), permissions=("agent:run",))
result = [event async for event in factory.create(agent_type, context).execute(
AgentRequest(agent_type=agent_type, message="test", session_id="test",
idempotency_key=str(uuid4())), context, "test-run")]
assert result[-1].payload["result"]["result"]["transfer_required"] is True
assert "测试禁止词" not in result[-1].payload["result"]["result"]["text"]
finally:
async with SessionFactory() as session, session.begin():
await session.execute(delete(MemoryUnit).where(MemoryUnit.memory_uuid == memory_uuid))
await session.execute(text("DELETE FROM agent_negative_word WHERE rule_code=:code"),
{"code": rule_code})
await session.execute(text("DELETE FROM sys_user WHERE id IN (:c,:r)"),
{"c": customer_id, "r": customer_id + 1})