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.
fix(backend): Include sub-graphs in graph-level credentials support (#9862)
Reinier van der Leer0726a00fb7f8
diff --git a/autogpt_platform/backend/backend/blocks/agent.py b/autogpt_platform/backend/backend/blocks/agent.pyindex 1b8344d5aa..a68cb05204 100644--- a/autogpt_platform/backend/backend/blocks/agent.py+++ b/autogpt_platform/backend/backend/blocks/agent.py@@ -1,5 +1,5 @@import logging-from typing import Any+from typing import Any, Optionalfrom backend.data.block import (Block,@@ -11,7 +11,7 @@ from backend.data.block import (get_block,)from backend.data.execution import ExecutionStatus-from backend.data.model import SchemaField+from backend.data.model import CredentialsMetaInput, SchemaFieldfrom backend.util import jsonlogger = logging.getLogger(__name__)@@ -23,17 +23,21 @@ class AgentExecutorBlock(Block):graph_id: str = SchemaField(description="Graph ID")graph_version: int = SchemaField(description="Graph Version")- data: BlockInput = SchemaField(description="Input data for the graph")+ inputs: BlockInput = SchemaField(description="Input data for the graph")input_schema: dict = SchemaField(description="Input schema for the graph")output_schema: dict = SchemaField(description="Output schema for the graph")+ node_credentials_input_map: Optional[+ dict[str, dict[str, CredentialsMetaInput]]+ ] = SchemaField(default=None, hidden=True)+@classmethoddef get_input_schema(cls, data: BlockInput) -> dict[str, Any]:return data.get("input_schema", {})@classmethoddef get_input_defaults(cls, data: BlockInput) -> BlockInput:- return data.get("data", {})+ return data.get("inputs", {})@classmethoddef get_missing_input(cls, data: BlockInput) -> set[str]:@@ -67,7 +71,8 @@ class AgentExecutorBlock(Block):graph_id=input_data.graph_id,graph_version=input_data.graph_version,user_id=input_data.user_id,- inputs=input_data.data,+ inputs=input_data.inputs,+ node_credentials_input_map=input_data.node_credentials_input_map,)log_id = f"Graph #{input_data.graph_id}-V{input_data.graph_version}, exec-id: {graph_exec.id}"logger.info(f"Starting execution of {log_id}")diff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.pyindex d6caf5e711..d8ba344039 100644--- a/autogpt_platform/backend/backend/data/execution.py+++ b/autogpt_platform/backend/backend/data/execution.py@@ -276,7 +276,7 @@ class NodeExecutionResult(BaseModel):node_exec_id=self.node_exec_id,node_id=self.node_id,block_id=self.block_id,- data=self.input_data,+ inputs=self.input_data,)@@ -691,7 +691,7 @@ class NodeExecutionEntry(BaseModel):node_exec_id: strnode_id: strblock_id: str- data: BlockInput+ inputs: BlockInputclass ExecutionQueue(Generic[T]):diff --git a/autogpt_platform/backend/backend/data/graph.py b/autogpt_platform/backend/backend/data/graph.pyindex cfc9d53073..71491c6ae1 100644--- a/autogpt_platform/backend/backend/data/graph.py+++ b/autogpt_platform/backend/backend/data/graph.py@@ -199,11 +199,6 @@ class BaseGraph(BaseDbModel):))- @computed_field- @property- def credentials_input_schema(self) -> dict[str, Any]:- return self._credentials_input_schema.jsonschema()-@staticmethoddef _generate_schema(*props: tuple[type[AgentInputBlock.Input] | type[AgentOutputBlock.Input], dict],@@ -236,6 +231,15 @@ class BaseGraph(BaseDbModel):"required": [p.name for p in schema_fields if p.value is None],}++class Graph(BaseGraph):+ sub_graphs: list[BaseGraph] = [] # Flattened sub-graphs++ @computed_field+ @property+ def credentials_input_schema(self) -> dict[str, Any]:+ return self._credentials_input_schema.jsonschema()+@propertydef _credentials_input_schema(self) -> type[BlockSchema]:graph_credentials_inputs = self.aggregate_credentials_inputs()@@ -314,17 +318,14 @@ class BaseGraph(BaseDbModel):),(node.id, field_name),)- for node in self.nodes+ for graph in [self] + self.sub_graphs+ for node in graph.nodesfor field_name, field_info in node.block.input_schema.get_credentials_fields_info().items()))}-class Graph(BaseGraph):- sub_graphs: list[BaseGraph] = [] # Flattened sub-graphs, only used in export--class GraphModel(Graph):user_id: strnodes: list[NodeModel] = [] # type: ignore@@ -400,7 +401,7 @@ class GraphModel(Graph):if node.block_id != AgentExecutorBlock().id:continuenode.input_default["user_id"] = user_id- node.input_default.setdefault("data", {})+ node.input_default.setdefault("inputs", {})if (graph_id := node.input_default.get("graph_id")) in graph_id_map:node.input_default["graph_id"] = graph_id_map[graph_id]@@ -689,6 +690,7 @@ async def get_graph(version: int | None = None,user_id: str | None = None,for_export: bool = False,+ include_subgraphs: bool = False,) -> GraphModel | None:"""Retrieves a graph from the DB.@@ -725,7 +727,7 @@ async def get_graph():return None- if for_export:+ if include_subgraphs or for_export:sub_graphs = await get_sub_graphs(graph)return GraphModel.from_db(graph=graph,diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex ff467e9b44..b55518ad23 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -8,14 +8,18 @@ import threadingfrom concurrent.futures import Future, ProcessPoolExecutorfrom contextlib import contextmanagerfrom multiprocessing.pool import AsyncResult, Pool-from typing import TYPE_CHECKING, Any, Generator, TypeVar, cast+from typing import TYPE_CHECKING, Any, Generator, Optional, TypeVar, castfrom pika.adapters.blocking_connection import BlockingChannelfrom pika.spec import Basic, BasicPropertiesfrom redis.lock import Lock as RedisLockfrom backend.blocks.io import AgentOutputBlock-from backend.data.model import GraphExecutionStats, NodeExecutionStats+from backend.data.model import (+ CredentialsMetaInput,+ GraphExecutionStats,+ NodeExecutionStats,+)from backend.data.notifications import (AgentRunData,LowBalanceData,@@ -139,6 +143,9 @@ def execute_node(creds_manager: IntegrationCredentialsManager,data: NodeExecutionEntry,execution_stats: NodeExecutionStats | None = None,+ node_credentials_input_map: Optional[+ dict[str, dict[str, CredentialsMetaInput]]+ ] = None,) -> ExecutionStream:"""Execute a node in the graph. This will trigger a block execution on a node,@@ -186,7 +193,7 @@ def execute_node()# Sanity check: validate the execution input.- input_data, error = validate_exec(node, data.data, resolve_input=False)+ input_data, error = validate_exec(node, data.inputs, resolve_input=False)if input_data is None:log_metadata.error(f"Skip execution, input validation error: {error}")push_output("error", error)@@ -196,8 +203,12 @@ def execute_node(# Re-shape the input data for agent block.# AgentExecutorBlock specially separate the node input_data & its input_default.if isinstance(node_block, AgentExecutorBlock):- input_data = {**node.input_default, "data": input_data}- data.data = input_data+ _input_data = AgentExecutorBlock.Input(**node.input_default)+ _input_data.inputs = input_data+ if node_credentials_input_map:+ _input_data.node_credentials_input_map = node_credentials_input_map+ input_data = _input_data.model_dump()+ data.inputs = input_data# Execute the nodeinput_data_str = json.dumps(input_data)@@ -244,6 +255,7 @@ def execute_node(graph_exec_id=graph_exec_id,graph_id=graph_id,log_metadata=log_metadata,+ node_credentials_input_map=node_credentials_input_map,):yield execution@@ -262,6 +274,7 @@ def execute_node(graph_exec_id=graph_exec_id,graph_id=graph_id,log_metadata=log_metadata,+ node_credentials_input_map=node_credentials_input_map,):yield execution@@ -291,6 +304,7 @@ def _enqueue_next_nodes(graph_exec_id: str,graph_id: str,log_metadata: LogMetadata,+ node_credentials_input_map: Optional[dict[str, dict[str, CredentialsMetaInput]]],) -> list[NodeExecutionEntry]:def add_enqueued_execution(node_exec_id: str, node_id: str, block_id: str, data: BlockInput@@ -306,7 +320,7 @@ def _enqueue_next_nodes(node_exec_id=node_exec_id,node_id=node_id,block_id=block_id,- data=data,+ inputs=data,)def register_next_executions(node_link: Link) -> list[NodeExecutionEntry]:@@ -347,6 +361,15 @@ def _enqueue_next_nodes(for name in static_link_names:next_node_input[name] = latest_execution.input_data.get(name)+ # Apply node credentials overrides+ node_credentials = None+ if node_credentials_input_map and (+ node_credentials := node_credentials_input_map.get(next_node.id)+ ):+ next_node_input.update(+ {k: v.model_dump() for k, v in node_credentials.items()}+ )+# Validate the input data for the next node.next_node_input, validation_msg = validate_exec(next_node, next_node_input)suffix = f"{next_output_name}>{next_input_name}~{next_node_exec_id}:{validation_msg}"@@ -389,6 +412,12 @@ def _enqueue_next_nodes(for input_name in static_link_names:idata[input_name] = next_node_input[input_name]+ # Apply node credentials overrides+ if node_credentials:+ idata.update(+ {k: v.model_dump() for k, v in node_credentials.items()}+ )+idata, msg = validate_exec(next_node, idata)suffix = f"{next_output_name}>{next_input_name}~{ineid}:{msg}"if not idata:@@ -478,6 +507,9 @@ class Executor:cls,q: ExecutionQueue[NodeExecutionEntry],node_exec: NodeExecutionEntry,+ node_credentials_input_map: Optional[+ dict[str, dict[str, CredentialsMetaInput]]+ ] = None,) -> NodeExecutionStats:log_metadata = LogMetadata(user_id=node_exec.user_id,@@ -490,7 +522,7 @@ class Executor:execution_stats = NodeExecutionStats()timing_info, _ = cls._on_node_execution(- q, node_exec, log_metadata, execution_stats+ q, node_exec, log_metadata, execution_stats, node_credentials_input_map)execution_stats.walltime = timing_info.wall_timeexecution_stats.cputime = timing_info.cpu_time@@ -510,6 +542,9 @@ class Executor:node_exec: NodeExecutionEntry,log_metadata: LogMetadata,stats: NodeExecutionStats | None = None,+ node_credentials_input_map: Optional[+ dict[str, dict[str, CredentialsMetaInput]]+ ] = None,):try:log_metadata.info(f"Start node execution {node_exec.node_exec_id}")@@ -518,6 +553,7 @@ class Executor:creds_manager=cls.creds_manager,data=node_exec,execution_stats=stats,+ node_credentials_input_map=node_credentials_input_map,):q.add(execution)log_metadata.info(f"Finished node execution {node_exec.node_exec_id}")@@ -625,7 +661,9 @@ class Executor:logger.error(f"Block {node_exec.block_id} not found.")return- cost, matching_filter = block_usage_cost(block=block, input_data=node_exec.data)+ cost, matching_filter = block_usage_cost(+ block=block, input_data=node_exec.inputs+ )if cost > 0:cls.db_client.spend_credits(user_id=node_exec.user_id,@@ -797,7 +835,7 @@ class Executor:if (node_creds_map := graph_exec.node_credentials_input_map) and (node_field_creds_map := node_creds_map.get(node_id)):- queued_node_exec.data.update(+ queued_node_exec.inputs.update({field_name: creds_meta.model_dump()for field_name, creds_meta in node_field_creds_map.items()@@ -807,7 +845,7 @@ class Executor:# Initiate node executionrunning_executions[queued_node_exec.node_id] = cls.executor.apply_async(cls.on_node_execution,- (queue, queued_node_exec),+ (queue, queued_node_exec, node_creds_map),callback=make_exec_callback(queued_node_exec),)diff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.pyindex 05bac14d2d..986eec31aa 100644--- a/autogpt_platform/backend/backend/executor/utils.py+++ b/autogpt_platform/backend/backend/executor/utils.py@@ -258,7 +258,7 @@ def validate_exec(If the data is valid, the first element will be the resolved input data, andthe second element will be the block name."""- node_block: Block | None = get_block(node.block_id)+ node_block = get_block(node.block_id)if not node_block:return None, f"Block for {node.block_id} not found."schema = node_block.input_schema@@ -608,7 +608,10 @@ async def add_graph_execution_async(ValueError: If the graph is not found or if there are validation errors.""" # noqagraph: GraphModel | None = await get_graph(- graph_id=graph_id, user_id=user_id, version=graph_version+ graph_id=graph_id,+ user_id=user_id,+ version=graph_version,+ include_subgraphs=True,)if not graph:raise NotFoundError(f"Graph #{graph_id} not found.")@@ -668,6 +671,9 @@ def add_graph_execution(preset_id: Optional[str] = None,graph_version: Optional[int] = None,graph_credentials_inputs: Optional[dict[str, CredentialsMetaInput]] = None,+ node_credentials_input_map: Optional[+ dict[str, dict[str, CredentialsMetaInput]]+ ] = None,) -> GraphExecutionWithNodes:"""Adds a graph execution to the queue and returns the execution entry.@@ -680,6 +686,7 @@ def add_graph_execution(graph_version: The version of the graph to execute.graph_credentials_inputs: Credentials inputs to use in the execution.Keys should map to the keys generated by `GraphModel.aggregate_credentials_inputs`.+ node_credentials_input_map: Credentials inputs to use in the execution, mapped to specific nodes.Returns:GraphExecutionEntry: The entry for the graph execution.Raises:@@ -687,12 +694,15 @@ def add_graph_execution("""db = get_db_client()graph: GraphModel | None = db.get_graph(- graph_id=graph_id, user_id=user_id, version=graph_version+ graph_id=graph_id,+ user_id=user_id,+ version=graph_version,+ include_subgraphs=True,)if not graph:raise NotFoundError(f"Graph #{graph_id} not found.")- node_credentials_input_map = (+ node_credentials_input_map = node_credentials_input_map or (make_node_credentials_input_map(graph, graph_credentials_inputs)if graph_credentials_inputselse Nonediff --git a/autogpt_platform/backend/backend/server/routers/v1.py b/autogpt_platform/backend/backend/server/routers/v1.pyindex 883615c08e..65d4262290 100644--- a/autogpt_platform/backend/backend/server/routers/v1.py+++ b/autogpt_platform/backend/backend/server/routers/v1.py@@ -422,7 +422,11 @@ async def get_graph(for_export: bool = False,) -> graph_db.GraphModel:graph = await graph_db.get_graph(- graph_id, version, user_id=user_id, for_export=for_export+ graph_id,+ version,+ user_id=user_id,+ for_export=for_export,+ include_subgraphs=True, # needed to construct full credentials input schema)if not graph:raise HTTPException(status_code=404, detail=f"Graph #{graph_id} not found.")diff --git a/autogpt_platform/backend/test/data/test_credit.py b/autogpt_platform/backend/test/data/test_credit.pyindex b8d5e5fce6..d4bf1a3a4d 100644--- a/autogpt_platform/backend/test/data/test_credit.py+++ b/autogpt_platform/backend/test/data/test_credit.py@@ -34,7 +34,7 @@ async def spend_credits(entry: NodeExecutionEntry) -> int:if not block:raise RuntimeError(f"Block {entry.block_id} not found")- cost, matching_filter = block_usage_cost(block=block, input_data=entry.data)+ cost, matching_filter = block_usage_cost(block=block, input_data=entry.inputs)await user_credit.spend_credits(entry.user_id,cost,@@ -67,7 +67,7 @@ async def test_block_credit_usage(server: SpinTestServer):graph_exec_id="test_graph_exec",node_exec_id="test_node_exec",block_id=AITextGeneratorBlock().id,- data={+ inputs={"model": "gpt-4-turbo","credentials": {"id": openai_credentials.id,@@ -87,7 +87,7 @@ async def test_block_credit_usage(server: SpinTestServer):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"},+ inputs={"model": "gpt-4-turbo", "api_key": "owned_api_key"},),)assert spending_amount_2 == 0