AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): Add graph/node id & execution id on CreditTransaction table (#9217)
Zamil Majdyc36c239dd566
diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.pyindex c408a7cf02..2adab8c402 100644--- a/autogpt_platform/backend/backend/data/credit.py+++ b/autogpt_platform/backend/backend/data/credit.py@@ -12,6 +12,7 @@ from backend.data import dbfrom backend.data.block import Block, BlockInput, get_blockfrom backend.data.block_cost_config import BLOCK_COSTSfrom backend.data.cost import BlockCost, BlockCostType+from backend.data.execution import NodeExecutionEntryfrom backend.data.user import get_user_by_idfrom backend.util.settings import Settings@@ -33,9 +34,7 @@ class UserCreditBase(ABC):@abstractmethodasync def spend_credits(self,- user_id: str,- block_id: str,- input_data: BlockInput,+ entry: NodeExecutionEntry,data_size: float,run_time: float,) -> int:@@ -43,9 +42,7 @@ class UserCreditBase(ABC):Spend the credits for the user based on the block usage.Args:- user_id (str): The user ID.- block_id (str): The block ID.- input_data (BlockInput): The input data for the block.+ entry (NodeExecutionEntry): The node execution identifiers & data.data_size (float): The size of the data being processed.run_time (float): The time taken to run the block.@@ -168,7 +165,6 @@ class UserCreditBase(ABC):transaction_type: CreditTransactionType,is_active: bool = True,transaction_key: str | None = None,- block_id: str | None = None,metadata: Json = Json({}),):async with db.locked_transaction(f"usr_trx_{user_id}"):@@ -185,7 +181,6 @@ class UserCreditBase(ABC):"amount": amount,"runningBalance": user_balance + amount,"type": transaction_type,- "blockId": block_id,"metadata": metadata,"isActive": is_active,"createdAt": self.time_now(),@@ -251,29 +246,31 @@ class UserCredit(UserCreditBase):async def spend_credits(self,- user_id: str,- block_id: str,- input_data: BlockInput,+ entry: NodeExecutionEntry,data_size: float,run_time: float,) -> int:- block = get_block(block_id)+ block = get_block(entry.block_id)if not block:- raise ValueError(f"Block not found: {block_id}")+ raise ValueError(f"Block not found: {entry.block_id}")cost, matching_filter = self._block_usage_cost(- block=block, input_data=input_data, data_size=data_size, run_time=run_time+ block=block, input_data=entry.data, data_size=data_size, run_time=run_time)if cost == 0:return 0await self._add_transaction(- user_id=user_id,+ user_id=entry.user_id,amount=-cost,transaction_type=CreditTransactionType.USAGE,- block_id=block.id,metadata=Json({+ "graph_exec_id": entry.graph_exec_id,+ "graph_id": entry.graph_id,+ "node_id": entry.node_id,+ "node_exec_id": entry.node_exec_id,+ "block_id": entry.block_id,"block": block.name,"input": matching_filter,}diff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.pyindex 230c8e4970..8df102fca2 100644--- a/autogpt_platform/backend/backend/data/execution.py+++ b/autogpt_platform/backend/backend/data/execution.py@@ -31,6 +31,7 @@ class NodeExecutionEntry(BaseModel):graph_id: strnode_exec_id: strnode_id: str+ block_id: strdata: BlockInputdiff --git a/autogpt_platform/backend/backend/executor/database.py b/autogpt_platform/backend/backend/executor/database.pyindex 108a089aca..1dee046ccc 100644--- a/autogpt_platform/backend/backend/executor/database.py+++ b/autogpt_platform/backend/backend/executor/database.py@@ -4,6 +4,7 @@ from typing import Any, Callable, Concatenate, Coroutine, ParamSpec, TypeVar, cafrom backend.data.credit import get_user_credit_modelfrom backend.data.execution import (ExecutionResult,+ NodeExecutionEntry,RedisExecutionEventBus,create_graph_execution,get_execution_results,@@ -79,7 +80,7 @@ class DatabaseManager(AppService):# Creditsuser_credit_model = get_user_credit_model()spend_credits = cast(- Callable[[Any, str, str, dict[str, str], float, float], int],+ Callable[[Any, NodeExecutionEntry, float, float], int],exposed_run_and_wait(user_credit_model.spend_credits),)diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 33e3ecf67a..50469b50b7 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -238,7 +238,8 @@ def execute_node(if res.end_time and res.start_timeelse 0)- db_client.spend_credits(user_id, node_block.id, input_data, s, t)+ data.data = input_data+ db_client.spend_credits(data, s, t)# Update execution statsif execution_stats is not None:@@ -257,7 +258,7 @@ def _enqueue_next_nodes(log_metadata: LogMetadata,) -> list[NodeExecutionEntry]:def add_enqueued_execution(- node_exec_id: str, node_id: str, data: BlockInput+ node_exec_id: str, node_id: str, block_id: str, data: BlockInput) -> NodeExecutionEntry:exec_update = db_client.update_execution_status(node_exec_id, ExecutionStatus.QUEUED, data@@ -269,6 +270,7 @@ def _enqueue_next_nodes(graph_id=graph_id,node_exec_id=node_exec_id,node_id=node_id,+ block_id=block_id,data=data,)@@ -322,7 +324,12 @@ def _enqueue_next_nodes(# Input is complete, enqueue the execution.log_metadata.info(f"Enqueued {suffix}")enqueued_executions.append(- add_enqueued_execution(next_node_exec_id, next_node_id, next_node_input)+ add_enqueued_execution(+ node_exec_id=next_node_exec_id,+ node_id=next_node_id,+ block_id=next_node.block_id,+ data=next_node_input,+ ))# Next execution stops here if the link is not static.@@ -352,7 +359,12 @@ def _enqueue_next_nodes(continuelog_metadata.info(f"Enqueueing static-link execution {suffix}")enqueued_executions.append(- add_enqueued_execution(iexec.node_exec_id, next_node_id, idata)+ add_enqueued_execution(+ node_exec_id=iexec.node_exec_id,+ node_id=next_node_id,+ block_id=next_node.block_id,+ data=idata,+ ))return enqueued_executions@@ -837,6 +849,7 @@ class ExecutionManager(AppService):graph_id=node_exec.graph_id,node_exec_id=node_exec.node_exec_id,node_id=node_exec.node_id,+ block_id=node_exec.block_id,data=node_exec.input_data,))diff --git a/autogpt_platform/backend/migrations/20250115382614_reshape_transaction_metadata_column/migration.sql b/autogpt_platform/backend/migrations/20250115382614_reshape_transaction_metadata_column/migration.sqlnew file mode 100644index 0000000000..c6952d769b--- /dev/null+++ b/autogpt_platform/backend/migrations/20250115382614_reshape_transaction_metadata_column/migration.sql@@ -0,0 +1,35 @@+/*+ Warnings:++ - You are about to drop the column `blockId` on the `CreditTransaction` table. All the data in the column will be moved to metadata->block_id.++*/+BEGIN;++-- DropForeignKey blockId+ALTER TABLE "CreditTransaction" DROP CONSTRAINT "CreditTransaction_blockId_fkey";++-- Update migrate blockId into metadata->"block_id"+UPDATE "CreditTransaction"+SET "metadata" = jsonb_set(+ COALESCE("metadata"::jsonb, '{}'),+ '{block_id}',+ to_jsonb("blockId")+)+WHERE "blockId" IS NOT NULL;++-- AlterTable drop blockId+ALTER TABLE "CreditTransaction" DROP COLUMN "blockId";++COMMIT;++/*+ These indices dropped below were part of the cleanup during the schema change applied above.+ These indexes were not useful and will not impact anything upon their removal.+*/++-- DropIndex+DROP INDEX "StoreListingReview_storeListingVersionId_idx";++-- DropIndex+DROP INDEX "StoreListingSubmission_Status_idx";diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 6d3322240d..a284886d94 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -32,12 +32,12 @@ model User {AgentPreset AgentPreset[]UserAgent UserAgent[]- Profile Profile[]- StoreListing StoreListing[]- StoreListingReview StoreListingReview[]- StoreListingSubmission StoreListingSubmission[]- APIKeys APIKey[]- IntegrationWebhooks IntegrationWebhook[]+ Profile Profile[]+ StoreListing StoreListing[]+ StoreListingReview StoreListingReview[]+ StoreListingSubmission StoreListingSubmission[]+ APIKeys APIKey[]+ IntegrationWebhooks IntegrationWebhook[]@@index([id])@@index([email])@@ -64,23 +64,23 @@ model AgentGraph {AgentNodes AgentNode[]AgentGraphExecution AgentGraphExecution[]- AgentPreset AgentPreset[]- UserAgent UserAgent[]- StoreListing StoreListing[]- StoreListingVersion StoreListingVersion?+ AgentPreset AgentPreset[]+ UserAgent UserAgent[]+ StoreListing StoreListing[]+ StoreListingVersion StoreListingVersion?@@id(name: "graphVersionId", [id, version])@@index([userId, isActive])}-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+//////////////////////////////////////////////////////////////////////////// USER SPECIFIC DATA ////////////////////-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////// An AgentPrest is an Agent + User Configuration of that agent.-// For example, if someone has created a weather agent and they want to set it up to+// For example, if someone has created a weather agent and they want to set it up to// Inform them of extreme weather warnings in Texas, the agent with the configuration to set it to// monitor texas, along with the cron setup or webhook tiggers, is an AgentPresetmodel AgentPreset {@@ -102,9 +102,9 @@ model AgentPreset {agentVersion IntAgent AgentGraph @relation(fields: [agentId, agentVersion], references: [id, version], onDelete: Cascade)- InputPresets AgentNodeExecutionInputOutput[] @relation("AgentPresetsInputData")- UserAgents UserAgent[]- AgentExecution AgentGraphExecution[]+ InputPresets AgentNodeExecutionInputOutput[] @relation("AgentPresetsInputData")+ UserAgents UserAgent[]+ AgentExecution AgentGraphExecution[]@@index([userId])}@@ -134,11 +134,11 @@ model UserAgent {@@index([userId])}-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+//////////////////////////////////////////////////////////////////// AGENT DEFINITION AND EXECUTION TABLES ////////-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////// This model describes a single node in the Agent Graph/Flow (Multi Agent System).model AgentNode {@@ -207,7 +207,6 @@ model AgentBlock {// Prisma requires explicit back-references.ReferencedByAgentNode AgentNode[]- CreditTransaction CreditTransaction[]}// This model describes the status of an AgentGraphExecution or AgentNodeExecution.@@ -345,11 +344,11 @@ model AnalyticsDetails {@@index([type])}-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////////////////// METRICS TRACKING TABLES ////////////////-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////model AnalyticsMetrics {id String @id @default(uuid())createdAt DateTime @default(now())@@ -375,11 +374,11 @@ enum CreditTransactionType {USAGE}-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+//////////////////////////////////////////////////////////////////// ACCOUNTING AND CREDIT SYSTEM TABLES //////////-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////model CreditTransaction {transactionKey String @default(uuid())createdAt DateTime @default(now())@@ -387,9 +386,6 @@ model CreditTransaction {userId Stringuser User @relation(fields: [userId], references: [id], onDelete: Cascade)- blockId String?- block AgentBlock? @relation(fields: [blockId], references: [id])-amount Inttype CreditTransactionType@@ -402,11 +398,11 @@ model CreditTransaction {@@index([userId, createdAt])}-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////////////////// Store TABLES ///////////////////////////-////////////////////////////////////////////////////////////-////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////model Profile {id String @id @default(uuid())@@ -414,7 +410,7 @@ model Profile {updatedAt DateTime @default(now()) @updatedAt// Only 1 of user or group can be set.- // The user this profile belongs to, if any.+ // The user this profile belongs to, if any.userId String?User User? @relation(fields: [userId], references: [id], onDelete: Cascade)@@ -528,7 +524,7 @@ model StoreListingVersion {agentVersion IntAgent AgentGraph @relation(fields: [agentId, agentVersion], references: [id, version])- // The detials for this version of the agent, this allows the author to update the details of the agent,+ // The details for this version of the agent, this allows the author to update the details of the agent,// But still allow using old versions of the agent with there original details.// TODO: Create a database view that shows only the latest version of each store listing.slug String@@ -573,7 +569,6 @@ model StoreListingReview {comments String?@@unique([storeListingVersionId, reviewByUserId])- @@index([storeListingVersionId])}enum SubmissionStatus {@@ -601,7 +596,6 @@ model StoreListingSubmission {reviewComments String?@@index([storeListingId])- @@index([Status])}enum APIKeyPermission {diff --git a/autogpt_platform/backend/test/data/test_credit.py b/autogpt_platform/backend/test/data/test_credit.pyindex 007c382d51..9007cfea52 100644--- a/autogpt_platform/backend/test/data/test_credit.py+++ b/autogpt_platform/backend/test/data/test_credit.py@@ -5,6 +5,7 @@ from prisma.models import CreditTransactionfrom backend.blocks.llm import AITextGeneratorBlockfrom backend.data.credit import BetaUserCredit+from backend.data.execution import NodeExecutionEntryfrom backend.data.user import DEFAULT_USER_IDfrom backend.integrations.credentials_store import openai_credentialsfrom backend.util.test import SpinTestServer@@ -24,25 +25,37 @@ async def test_block_credit_usage(server: SpinTestServer):current_credit = await user_credit.get_credits(DEFAULT_USER_ID)spending_amount_1 = await user_credit.spend_credits(- DEFAULT_USER_ID,- AITextGeneratorBlock().id,- {- "model": "gpt-4-turbo",- "credentials": {- "id": openai_credentials.id,- "provider": openai_credentials.provider,- "type": openai_credentials.type,+ NodeExecutionEntry(+ user_id=DEFAULT_USER_ID,+ graph_id="test_graph",+ node_id="test_node",+ graph_exec_id="test_graph_exec",+ node_exec_id="test_node_exec",+ block_id=AITextGeneratorBlock().id,+ data={+ "model": "gpt-4-turbo",+ "credentials": {+ "id": openai_credentials.id,+ "provider": openai_credentials.provider,+ "type": openai_credentials.type,+ },},- },+ ),0.0,0.0,)assert spending_amount_1 > 0spending_amount_2 = await user_credit.spend_credits(- DEFAULT_USER_ID,- AITextGeneratorBlock().id,- {"model": "gpt-4-turbo", "api_key": "owned_api_key"},+ NodeExecutionEntry(+ user_id=DEFAULT_USER_ID,+ graph_id="test_graph",+ node_id="test_node",+ graph_exec_id="test_graph_exec",+ node_exec_id="test_node_exec",+ block_id=AITextGeneratorBlock().id,+ data={"model": "gpt-4-turbo", "api_key": "owned_api_key"},+ ),0.0,0.0,)diff --git a/autogpt_platform/backend/test/test_data_creator.py b/autogpt_platform/backend/test/test_data_creator.pyindex 1f79386cc6..d5235043d3 100644--- a/autogpt_platform/backend/test/test_data_creator.py+++ b/autogpt_platform/backend/test/test_data_creator.py@@ -298,7 +298,6 @@ async def main():data={"transactionKey": str(faker.uuid4()),"userId": user.id,- "blockId": block.id,"amount": random.randint(1, 100),"type": (prisma.enums.CreditTransactionType.TOP_UP