Skip to content

Commit 35bba89

Browse files
committed
cursor commit
1 parent a5244b4 commit 35bba89

6 files changed

Lines changed: 133 additions & 2 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from ..models.register_nodes_request import RegisterNodesRequestModel
2+
from ..models.register_nodes_response import RegisterNodesResponseModel, RegisteredNodeModel
3+
from ..models.db.registered_node import RegisteredNode
4+
5+
from app.singletons.logs_manager import LogsManager
6+
from beanie.operators import Set
7+
8+
logger = LogsManager().get_logger()
9+
10+
11+
async def register_nodes(namespace_name: str, body: RegisterNodesRequestModel, x_exosphere_request_id: str) -> RegisterNodesResponseModel:
12+
13+
try:
14+
logger.info(f"Registering nodes for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
15+
16+
# Check if nodes already exist and update them, or create new ones
17+
registered_nodes = []
18+
19+
for node_data in body.nodes:
20+
# Check if node already exists
21+
existing_node = await RegisteredNode.find_one(
22+
RegisteredNode.name == node_data.name,
23+
RegisteredNode.namespace == node_data.namespace
24+
)
25+
26+
if existing_node:
27+
# Update existing node
28+
await existing_node.update(
29+
Set({
30+
RegisteredNode.runtime_name: body.runtime_name,
31+
RegisteredNode.runtime_namespace: body.runtime_namespace,
32+
RegisteredNode.inputs_schema: node_data.inputs_schema, # type: ignore
33+
RegisteredNode.outputs_schema: node_data.outputs_schema # type: ignore
34+
}))
35+
logger.info(f"Updated existing node {node_data.name} in namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
36+
else:
37+
# Create new node
38+
new_node = RegisteredNode(
39+
name=node_data.name,
40+
namespace=node_data.namespace,
41+
runtime_name=body.runtime_name,
42+
runtime_namespace=body.runtime_namespace,
43+
inputs_schema=node_data.inputs_schema,
44+
outputs_schema=node_data.outputs_schema
45+
)
46+
inserted_node = await new_node.insert()
47+
logger.info(f"Created new node {node_data.name} in namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
48+
49+
registered_nodes.append(
50+
RegisteredNodeModel(
51+
name=node_data.name,
52+
namespace=node_data.namespace
53+
)
54+
)
55+
56+
response = RegisterNodesResponseModel(
57+
runtime_name=body.runtime_name,
58+
runtime_namespace=body.runtime_namespace,
59+
registered_nodes=registered_nodes,
60+
)
61+
62+
logger.info(f"Successfully registered {len(registered_nodes)} nodes for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
63+
return response
64+
65+
except Exception as e:
66+
logger.error(f"Error registering nodes for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id, error=e)
67+
raise e

state-manager/app/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .models.db.state import State
2222
from .models.db.namespace import Namespace
2323
from .models.db.graph_template_model import GraphTemplate
24+
from .models.db.registered_node import RegisteredNode
2425

2526
# injecting routes
2627
from .routes import router
@@ -36,7 +37,7 @@ async def lifespan(app: FastAPI):
3637
# initializing beanie
3738
client = AsyncMongoClient(os.getenv("MONGO_URI"))
3839
db = client[os.getenv("MONGO_DATABASE_NAME", "exosphere-state-manager")]
39-
await init_beanie(db, document_models=[State, Namespace, GraphTemplate])
40+
await init_beanie(db, document_models=[State, Namespace, GraphTemplate, RegisteredNode])
4041
logger.info("beanie dbs initialized")
4142

4243
# initialize secret
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .base import BaseDatabaseModel
2+
from pydantic import Field
3+
from typing import Any
4+
5+
6+
class RegisteredNode(BaseDatabaseModel):
7+
name: str = Field(..., description="Unique name of the registered node")
8+
namespace: str = Field(..., description="Namespace of the registered node")
9+
runtime_name: str = Field(..., description="Name of the runtime that registered this node")
10+
runtime_namespace: str = Field(..., description="Namespace of the runtime that registered this node")
11+
inputs_schema: dict[str, Any] = Field(..., description="JSON schema for node inputs")
12+
outputs_schema: dict[str, Any] = Field(..., description="JSON schema for node outputs")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pydantic import BaseModel, Field
2+
from typing import Any, List
3+
4+
5+
class NodeRegistrationModel(BaseModel):
6+
name: str = Field(..., description="Unique name of the node")
7+
namespace: str = Field(..., description="Namespace of the node")
8+
inputs_schema: dict[str, Any] = Field(..., description="JSON schema for node inputs")
9+
outputs_schema: dict[str, Any] = Field(..., description="JSON schema for node outputs")
10+
11+
12+
class RegisterNodesRequestModel(BaseModel):
13+
runtime_name: str = Field(..., description="Name of the runtime registering the nodes")
14+
runtime_namespace: str = Field(..., description="Namespace of the runtime")
15+
nodes: List[NodeRegistrationModel] = Field(..., description="List of nodes to register")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pydantic import BaseModel, Field
2+
from typing import List
3+
4+
5+
class RegisteredNodeModel(BaseModel):
6+
name: str = Field(..., description="Name of the registered node")
7+
namespace: str = Field(..., description="Namespace of the registered node")
8+
9+
10+
class RegisterNodesResponseModel(BaseModel):
11+
runtime_name: str = Field(..., description="Name of the runtime that registered the nodes")
12+
runtime_namespace: str = Field(..., description="Namespace of the runtime")
13+
registered_nodes: List[RegisteredNodeModel] = Field(..., description="List of successfully registered nodes")

state-manager/app/routes.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
from .models.graph_models import UpsertGraphTemplateRequest, UpsertGraphTemplateResponse
2222
from .controller.upsert_graph_template import upsert_graph_template as upsert_graph_template_controller
2323

24+
from .models.register_nodes_request import RegisterNodesRequestModel
25+
from .models.register_nodes_response import RegisterNodesResponseModel
26+
from .controller.register_nodes import register_nodes
27+
2428

2529

2630
logger = LogsManager().get_logger()
@@ -124,4 +128,23 @@ async def upsert_graph_template(namespace_name: str, graph_name: str, body: Upse
124128
logger.error(f"API key is invalid for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
125129
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid API key")
126130

127-
return await upsert_graph_template_controller(namespace_name, graph_name, body, x_exosphere_request_id)
131+
return await upsert_graph_template_controller(namespace_name, graph_name, body, x_exosphere_request_id)
132+
133+
134+
@router.put(
135+
"/nodes/",
136+
response_model=RegisterNodesResponseModel,
137+
status_code=status.HTTP_200_OK,
138+
response_description="Nodes registered successfully",
139+
tags=["nodes"]
140+
)
141+
async def register_nodes_route(namespace_name: str, body: RegisterNodesRequestModel, request: Request, api_key: str = Depends(check_api_key)):
142+
x_exosphere_request_id = getattr(request.state, "x_exosphere_request_id", str(uuid4()))
143+
144+
if api_key:
145+
logger.info(f"API key is valid for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
146+
else:
147+
logger.error(f"API key is invalid for namespace {namespace_name}", x_exosphere_request_id=x_exosphere_request_id)
148+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid API key")
149+
150+
return await register_nodes(namespace_name, body, x_exosphere_request_id)

0 commit comments

Comments
 (0)