2026-03-11 17:16:11 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
import base64
|
2026-03-16 18:32:09 +08:00
|
|
|
from typing import Any, cast
|
2026-03-11 17:16:11 +08:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
from agentscope.message import Msg
|
2026-03-11 17:16:11 +08:00
|
|
|
from core.agentscope.events import (
|
|
|
|
|
AgentScopeAgUiCodec,
|
|
|
|
|
AgentScopeEventPipeline,
|
|
|
|
|
RedisStreamBus,
|
2026-03-11 20:51:56 +08:00
|
|
|
SqlAlchemyEventStore,
|
|
|
|
|
)
|
2026-03-15 17:14:15 +08:00
|
|
|
from core.agentscope.runtime.orchestrator import AgentScopeRuntimeOrchestrator
|
|
|
|
|
from core.agentscope.schemas.agui_input import parse_run_input
|
|
|
|
|
from core.auth.models import CurrentUser
|
2026-03-11 17:16:11 +08:00
|
|
|
from core.config.settings import config
|
|
|
|
|
from core.db.session import AsyncSessionLocal
|
|
|
|
|
from core.logging import get_logger
|
|
|
|
|
from core.taskiq.app import bulk_broker, critical_broker, default_broker
|
2026-03-15 17:14:15 +08:00
|
|
|
from schemas.user import UserContext
|
2026-03-11 17:16:11 +08:00
|
|
|
from services.base.redis import get_or_init_redis_client
|
2026-03-15 17:14:15 +08:00
|
|
|
from services.base.supabase import supabase_service
|
2026-03-16 18:32:09 +08:00
|
|
|
from schemas.messages.chat_message import extract_user_message_attachments
|
2026-03-15 17:14:15 +08:00
|
|
|
from v1.agent.dependencies import get_agent_service
|
|
|
|
|
from v1.users.dependencies import get_user_service
|
2026-03-11 17:16:11 +08:00
|
|
|
|
|
|
|
|
logger = get_logger("core.agentscope.runtime.tasks")
|
2026-03-16 18:32:09 +08:00
|
|
|
_MAX_CONTEXT_ATTACHMENTS = 3
|
2026-03-11 17:16:11 +08:00
|
|
|
|
2026-03-11 20:51:56 +08:00
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
def _load_runtime() -> type[Any]:
|
|
|
|
|
return AgentScopeRuntimeOrchestrator
|
2026-03-11 20:51:56 +08:00
|
|
|
|
|
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
async def _build_user_context(
|
|
|
|
|
*,
|
|
|
|
|
owner_id: UUID,
|
|
|
|
|
session: Any,
|
|
|
|
|
) -> UserContext:
|
|
|
|
|
current_user = CurrentUser(id=owner_id)
|
|
|
|
|
user_service = get_user_service(session=session, user=current_user)
|
|
|
|
|
return await user_service.get_me()
|
2026-03-11 17:16:11 +08:00
|
|
|
|
|
|
|
|
|
2026-03-12 00:18:45 +08:00
|
|
|
async def _build_recent_context_messages(
|
|
|
|
|
*,
|
|
|
|
|
session: Any,
|
|
|
|
|
thread_id: str,
|
2026-03-15 17:14:15 +08:00
|
|
|
) -> list[Msg]:
|
|
|
|
|
agent_service = get_agent_service(session)
|
|
|
|
|
result = await agent_service.load_agent_input_messages(thread_id=thread_id)
|
|
|
|
|
if not result:
|
2026-03-12 00:18:45 +08:00
|
|
|
return []
|
|
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
raw_messages: list[dict[str, Any]] = result.get("messages") or []
|
|
|
|
|
if not raw_messages:
|
|
|
|
|
return []
|
2026-03-12 00:18:45 +08:00
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
converted: list[Msg] = []
|
|
|
|
|
|
|
|
|
|
for msg in raw_messages:
|
|
|
|
|
role = msg.get("role")
|
|
|
|
|
content = msg.get("content", "")
|
|
|
|
|
metadata = msg.get("metadata")
|
|
|
|
|
|
|
|
|
|
if role == "user" and metadata:
|
2026-03-16 18:32:09 +08:00
|
|
|
image_blocks: list[dict[str, Any]] = []
|
|
|
|
|
attachments = extract_user_message_attachments(metadata)[
|
|
|
|
|
:_MAX_CONTEXT_ATTACHMENTS
|
|
|
|
|
]
|
|
|
|
|
for attachment in attachments:
|
|
|
|
|
try:
|
|
|
|
|
image_bytes = await supabase_service.download_bytes(
|
|
|
|
|
bucket=attachment.bucket,
|
|
|
|
|
path=attachment.path,
|
|
|
|
|
)
|
|
|
|
|
except Exception:
|
|
|
|
|
continue
|
|
|
|
|
b64_data = base64.b64encode(image_bytes).decode("utf-8")
|
|
|
|
|
image_blocks.append(
|
|
|
|
|
{
|
|
|
|
|
"type": "image",
|
|
|
|
|
"source": {
|
|
|
|
|
"type": "base64",
|
|
|
|
|
"media_type": attachment.mime_type or "image/png",
|
|
|
|
|
"data": b64_data,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if image_blocks:
|
|
|
|
|
multimodal_content: list[dict[str, Any]] = []
|
|
|
|
|
if isinstance(content, str) and content:
|
|
|
|
|
multimodal_content.append({"type": "text", "text": content})
|
|
|
|
|
multimodal_content.extend(image_blocks)
|
|
|
|
|
converted.append(
|
|
|
|
|
Msg(
|
|
|
|
|
name="user",
|
|
|
|
|
role="user",
|
|
|
|
|
content=cast(Any, multimodal_content),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
continue
|
2026-03-15 17:14:15 +08:00
|
|
|
|
|
|
|
|
if role == "tool":
|
|
|
|
|
role = "assistant"
|
|
|
|
|
|
|
|
|
|
converted.append(
|
|
|
|
|
Msg(
|
|
|
|
|
name=role or "user",
|
|
|
|
|
role=role if role in ("user", "assistant", "system") else "user",
|
|
|
|
|
content=content,
|
|
|
|
|
)
|
2026-03-12 00:18:45 +08:00
|
|
|
)
|
|
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
return converted
|
2026-03-12 00:18:45 +08:00
|
|
|
|
|
|
|
|
|
2026-03-11 17:16:11 +08:00
|
|
|
async def run_agentscope_task(command: dict[str, Any]) -> dict[str, object]:
|
|
|
|
|
command_type = str(command.get("command", "run")).strip().lower()
|
|
|
|
|
raw_owner_id = command.get("owner_id")
|
2026-03-15 17:14:15 +08:00
|
|
|
run_input_raw = command.get("run_input")
|
2026-03-11 17:16:11 +08:00
|
|
|
|
|
|
|
|
if not isinstance(raw_owner_id, str) or not raw_owner_id.strip():
|
|
|
|
|
raise ValueError("owner_id is required")
|
2026-03-15 17:14:15 +08:00
|
|
|
if run_input_raw is None:
|
|
|
|
|
raise ValueError("run_input is required")
|
2026-03-11 17:16:11 +08:00
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
run_input = parse_run_input(run_input_raw)
|
|
|
|
|
thread_id = run_input.thread_id
|
|
|
|
|
run_id = run_input.run_id
|
2026-03-11 17:16:11 +08:00
|
|
|
owner_id = UUID(raw_owner_id)
|
2026-03-15 17:14:15 +08:00
|
|
|
|
|
|
|
|
if command_type != "run":
|
2026-03-11 20:51:56 +08:00
|
|
|
raise ValueError("invalid command type")
|
|
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
orchestrator = _load_runtime()
|
2026-03-11 17:16:11 +08:00
|
|
|
|
|
|
|
|
async with AsyncSessionLocal() as session:
|
2026-03-15 17:14:15 +08:00
|
|
|
user_context = await _build_user_context(owner_id=owner_id, session=session)
|
|
|
|
|
|
|
|
|
|
redis_client = await get_or_init_redis_client()
|
|
|
|
|
bus = RedisStreamBus(
|
|
|
|
|
client=redis_client,
|
|
|
|
|
stream_prefix=config.agent_runtime.redis_stream_prefix,
|
|
|
|
|
read_count=config.agent_runtime.redis_stream_read_count,
|
|
|
|
|
block_ms=config.agent_runtime.redis_stream_block_ms,
|
|
|
|
|
)
|
|
|
|
|
pipeline = AgentScopeEventPipeline(
|
|
|
|
|
codec=AgentScopeAgUiCodec(),
|
|
|
|
|
store=SqlAlchemyEventStore(
|
|
|
|
|
session_factory=AsyncSessionLocal,
|
|
|
|
|
),
|
|
|
|
|
bus=bus,
|
|
|
|
|
)
|
|
|
|
|
runtime = orchestrator(
|
|
|
|
|
pipeline=pipeline,
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-13 15:42:01 +08:00
|
|
|
context_messages = await _build_recent_context_messages(
|
|
|
|
|
session=session,
|
2026-03-15 17:14:15 +08:00
|
|
|
thread_id=thread_id,
|
2026-03-13 15:42:01 +08:00
|
|
|
)
|
2026-03-12 00:18:45 +08:00
|
|
|
|
2026-03-15 17:14:15 +08:00
|
|
|
await runtime.run(
|
|
|
|
|
run_input=run_input,
|
|
|
|
|
context_messages=context_messages,
|
|
|
|
|
user_context=user_context,
|
|
|
|
|
)
|
2026-03-11 17:16:11 +08:00
|
|
|
logger.info(
|
|
|
|
|
"agentscope runtime task completed",
|
|
|
|
|
command_type=command_type,
|
2026-03-15 17:14:15 +08:00
|
|
|
thread_id=thread_id,
|
|
|
|
|
run_id=run_id,
|
2026-03-11 17:16:11 +08:00
|
|
|
)
|
|
|
|
|
return {
|
2026-03-15 17:14:15 +08:00
|
|
|
"thread_id": thread_id,
|
|
|
|
|
"run_id": run_id,
|
2026-03-11 17:16:11 +08:00
|
|
|
"status": "completed",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@default_broker.task(task_name="tasks.agentscope.run_command")
|
|
|
|
|
async def run_command_task(command: dict[str, Any]) -> dict[str, object]:
|
|
|
|
|
return await run_agentscope_task(command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@critical_broker.task(task_name="tasks.agentscope.run_command.critical")
|
|
|
|
|
async def run_command_task_critical(command: dict[str, Any]) -> dict[str, object]:
|
|
|
|
|
return await run_agentscope_task(command)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bulk_broker.task(task_name="tasks.agentscope.run_command.bulk")
|
|
|
|
|
async def run_command_task_bulk(command: dict[str, Any]) -> dict[str, object]:
|
|
|
|
|
return await run_agentscope_task(command)
|