artifacts-dashboard/backend/app/engine/action_executor.py
Paweł Orzech 75313b83c0
Add multi-user workflows/pipelines and error tracking
Add multi-user automation features and per-user error tracking.

- Database migrations: add workflow_configs/workflow_runs (004), app_errors (005), pipeline_configs/pipeline_runs (006), and add user_token_hash to app_errors (007).
- Backend: introduce per-request token handling (X-API-Token) via app.api.deps and update many API routes (auth, automations, bank, characters, dashboard, events, exchange, logs) to use user-scoped Artifacts client and character scoping. Auth endpoints no longer store tokens server-side (validate-only); clear is a no-op on server.
- New Errors API and services: endpoint to list, filter, resolve, and report errors scoped to the requesting user; add error models, schemas, middleware/error handler and error_service for recording/hashing tokens.
- Pipelines & Workflows: add API routers, models, schemas and engine modules (pipeline/worker/coordinator, workflow runner/conditions) and action_executor updates to support workflow/pipeline execution.
- Logs: logs endpoint now prefers fetching recent action logs from the game API (with fallback to local DB), supports paging and filtering, and scopes results to the user.
- Frontend: add pipeline/workflow builders, lists, progress components and hooks (use-errors, use-pipelines, use-workflows), sentry client config, and updates to API client/constants/types.
- Misc: add middleware error handler, various engine strategy tweaks, tests adjusted.

Overall this change enables per-user API tokens, scopes DB queries to each user, introduces pipelines/workflows runtime support, and centralizes application error tracking.
2026-03-01 23:02:34 +01:00

150 lines
5 KiB
Python

"""Shared action execution logic.
Dispatches an ``ActionPlan`` to the appropriate ``ArtifactsClient`` method.
Used by ``AutomationRunner``, ``WorkflowRunner``, and ``CharacterWorker``
so the match statement is defined in exactly one place.
"""
from __future__ import annotations
import logging
from typing import Any
from app.engine.strategies.base import ActionPlan, ActionType
from app.services.artifacts_client import ArtifactsClient
logger = logging.getLogger(__name__)
async def execute_action(
client: ArtifactsClient,
character_name: str,
plan: ActionPlan,
) -> dict[str, Any]:
"""Execute an action plan against the game API and return the raw result."""
match plan.action_type:
case ActionType.MOVE:
return await client.move(
character_name,
plan.params["x"],
plan.params["y"],
)
case ActionType.FIGHT:
return await client.fight(character_name)
case ActionType.GATHER:
return await client.gather(character_name)
case ActionType.REST:
return await client.rest(character_name)
case ActionType.EQUIP:
return await client.equip(
character_name,
plan.params["code"],
plan.params["slot"],
plan.params.get("quantity", 1),
)
case ActionType.UNEQUIP:
return await client.unequip(
character_name,
plan.params["slot"],
plan.params.get("quantity", 1),
)
case ActionType.USE_ITEM:
return await client.use_item(
character_name,
plan.params["code"],
plan.params.get("quantity", 1),
)
case ActionType.DEPOSIT_ITEM:
return await client.deposit_item(
character_name,
plan.params["code"],
plan.params["quantity"],
)
case ActionType.WITHDRAW_ITEM:
return await client.withdraw_item(
character_name,
plan.params["code"],
plan.params["quantity"],
)
case ActionType.CRAFT:
return await client.craft(
character_name,
plan.params["code"],
plan.params.get("quantity", 1),
)
case ActionType.RECYCLE:
return await client.recycle(
character_name,
plan.params["code"],
plan.params.get("quantity", 1),
)
case ActionType.GE_BUY:
return await client.ge_buy(
character_name,
plan.params["id"],
plan.params["quantity"],
)
case ActionType.GE_CREATE_BUY:
return await client.ge_create_buy_order(
character_name,
plan.params["code"],
plan.params["quantity"],
plan.params["price"],
)
case ActionType.GE_SELL:
return await client.ge_sell_order(
character_name,
plan.params["code"],
plan.params["quantity"],
plan.params["price"],
)
case ActionType.GE_FILL:
return await client.ge_fill_buy_order(
character_name,
plan.params["id"],
plan.params["quantity"],
)
case ActionType.GE_CANCEL:
return await client.ge_cancel(
character_name,
plan.params["order_id"],
)
case ActionType.TASK_NEW:
return await client.task_new(character_name)
case ActionType.TASK_TRADE:
return await client.task_trade(
character_name,
plan.params["code"],
plan.params["quantity"],
)
case ActionType.TASK_COMPLETE:
return await client.task_complete(character_name)
case ActionType.TASK_EXCHANGE:
return await client.task_exchange(character_name)
case ActionType.TASK_CANCEL:
return await client.task_cancel(character_name)
case ActionType.DEPOSIT_GOLD:
return await client.deposit_gold(
character_name,
plan.params["quantity"],
)
case ActionType.WITHDRAW_GOLD:
return await client.withdraw_gold(
character_name,
plan.params["quantity"],
)
case ActionType.NPC_BUY:
return await client.npc_buy(
character_name,
plan.params["code"],
plan.params["quantity"],
)
case ActionType.NPC_SELL:
return await client.npc_sell(
character_name,
plan.params["code"],
plan.params["quantity"],
)
case _:
logger.warning("Unhandled action type: %s", plan.action_type)
return {}