Skip to content

Commit a4c735a

Browse files
committed
[REL-11697] resolve linting errors
1 parent 3ff3b8b commit a4c735a

4 files changed

Lines changed: 91 additions & 55 deletions

File tree

packages/sdk/server-ai/src/ldai/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
from ldclient import log
44

5+
from ldai.agent_graph import AgentGraphDefinition
56
from ldai.chat import Chat
67
from ldai.client import LDAIClient
7-
from ldai.agent_graph import AgentGraphDefinition
88
from ldai.judge import Judge
99
from ldai.models import ( # Deprecated aliases for backward compatibility
10-
AIAgentConfig, AIAgentConfigDefault, AIAgentConfigRequest, AIAgents,
11-
AICompletionConfig, AICompletionConfigDefault, AIConfig, AIJudgeConfig,
12-
AIJudgeConfigDefault, JudgeConfiguration, LDAIAgent, LDAIAgentConfig,
13-
LDAIAgentDefaults, LDMessage, ModelConfig, ProviderConfig, AIAgentGraphConfig, Edge)
10+
AIAgentConfig, AIAgentConfigDefault, AIAgentConfigRequest,
11+
AIAgentGraphConfig, AIAgentGraphResponse, AIAgents, AICompletionConfig,
12+
AICompletionConfigDefault, AIConfig, AIJudgeConfig, AIJudgeConfigDefault,
13+
Edge, JudgeConfiguration, LDAIAgent, LDAIAgentConfig, LDAIAgentDefaults,
14+
LDMessage, ModelConfig, ProviderConfig)
1415
from ldai.providers.types import EvalScore, JudgeResponse
1516

1617
__all__ = [
@@ -20,6 +21,7 @@
2021
'AIAgentConfigRequest',
2122
'AIAgents',
2223
'AIAgentGraphConfig',
24+
'AIAgentGraphResponse',
2325
'Edge',
2426
'AICompletionConfig',
2527
'AICompletionConfigDefault',

packages/sdk/server-ai/src/ldai/agent_graph/__init__.py

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
"""Graph implementation for managing AI agent graphs."""
22

3-
from typing import Any, Callable, Dict, List, Set
4-
from ldai.models import AIAgentGraphConfig, AIAgentConfig, Edge
3+
from typing import Any, Callable, Dict, List, Optional, Set
4+
55
from ldclient import Context
66

7+
from ldai.models import AIAgentConfig, AIAgentGraphConfig, Edge
8+
79
DEFAULT_FALSE = AIAgentConfig(key="", enabled=False)
10+
11+
812
class AgentGraphNode:
913
"""
1014
Node in an agent graph.
@@ -36,10 +40,12 @@ def get_edges(self) -> List[Edge]:
3640
"""Get the edges of the node."""
3741
return self._children
3842

43+
3944
class AgentGraphDefinition:
4045
"""
4146
Graph implementation for managing AI agent graphs.
4247
"""
48+
4349
def __init__(
4450
self,
4551
agent_graph: AIAgentGraphConfig,
@@ -72,42 +78,40 @@ def build_nodes(
7278
nodes[edge.target_config] = AgentGraphNode(
7379
edge.target_config,
7480
graph_nodes[edge.target_config],
75-
[
76-
e
77-
for e in agent_graph.edges
78-
if e.source_config == edge.target_config
79-
],
81+
[e for e in agent_graph.edges if e.source_config == edge.target_config],
8082
)
8183

8284
return nodes
8385

84-
def get_node(self, key: str) -> AgentGraphNode | None:
86+
def get_node(self, key: str) -> Optional[AgentGraphNode]:
8587
"""Get a node by its key."""
8688
return self._nodes.get(key)
8789

8890
def _get_child_edges(self, config_key: str) -> List[Edge]:
8991
"""Get the child edges of the given config."""
9092
return [
91-
edge
92-
for edge in self._agent_graph.edges
93-
if edge.source_config == config_key
93+
edge for edge in self._agent_graph.edges if edge.source_config == config_key
9494
]
9595

9696
def get_child_nodes(self, node_key: str) -> List[AgentGraphNode]:
9797
"""Get the child nodes of the given node key as AgentGraphNode objects."""
98-
return [
99-
self.get_node(edge.target_config)
100-
for edge in self._agent_graph.edges
101-
if edge.source_config == node_key and self.get_node(edge.target_config) is not None
102-
]
98+
nodes: List[AgentGraphNode] = []
99+
for edge in self._agent_graph.edges:
100+
if edge.source_config == node_key:
101+
node = self.get_node(edge.target_config)
102+
if node is not None:
103+
nodes.append(node)
104+
return nodes
103105

104106
def get_parent_nodes(self, node_key: str) -> List[AgentGraphNode]:
105107
"""Get the parent nodes of the given node key as AgentGraphNode objects."""
106-
return [
107-
self.get_node(edge.source_config)
108-
for edge in self._agent_graph.edges
109-
if edge.target_config == node_key and self.get_node(edge.source_config) is not None
110-
]
108+
nodes: List[AgentGraphNode] = []
109+
for edge in self._agent_graph.edges:
110+
if edge.target_config == node_key:
111+
node = self.get_node(edge.source_config)
112+
if node is not None:
113+
nodes.append(node)
114+
return nodes
111115

112116
def _collect_nodes(
113117
self,
@@ -133,14 +137,20 @@ def _collect_nodes(
133137
def terminal_nodes(self) -> List[AgentGraphNode]:
134138
"""Get the terminal nodes of the graph, meaning any nodes without children."""
135139
return [
136-
node for node in self._nodes.values() if len(self.get_child_nodes(node.get_key())) == 0
140+
node
141+
for node in self._nodes.values()
142+
if len(self.get_child_nodes(node.get_key())) == 0
137143
]
138144

139-
def root(self) -> AgentGraphNode | None:
145+
def root(self) -> Optional[AgentGraphNode]:
140146
"""Get the root node of the graph."""
141147
return self._nodes[self._agent_graph.root_config_key]
142148

143-
def traverse(self, fn: Callable[["AgentGraphNode", Dict[str, Any]], None], execution_context: Dict[str, Any] = {}) -> None:
149+
def traverse(
150+
self,
151+
fn: Callable[["AgentGraphNode", Dict[str, Any]], Any],
152+
execution_context: Dict[str, Any] = {},
153+
) -> None:
144154
"""Traverse from the root down to terminal nodes, visiting nodes in order of depth.
145155
Nodes with the longest paths from the root (deepest nodes) will always be visited last."""
146156
root_node = self.root()
@@ -181,7 +191,11 @@ def traverse(self, fn: Callable[["AgentGraphNode", Dict[str, Any]], None], execu
181191

182192
return execution_context[self._agent_graph.root_config_key]
183193

184-
def reverse_traverse(self, fn: Callable[["AgentGraphNode", Dict[str, Any]], Any], execution_context: Dict[str, Any] = {}) -> None:
194+
def reverse_traverse(
195+
self,
196+
fn: Callable[["AgentGraphNode", Dict[str, Any]], Any],
197+
execution_context: Dict[str, Any] = {},
198+
) -> None:
185199
"""Traverse from terminal nodes up to the root, visiting nodes level by level.
186200
The root node will always be visited last, even if multiple paths converge at it."""
187201
terminal_nodes = self.terminal_nodes()
@@ -208,7 +222,7 @@ def reverse_traverse(self, fn: Callable[["AgentGraphNode", Dict[str, Any]], Any]
208222
continue
209223

210224
execution_context[node_key] = fn(node, execution_context)
211-
225+
212226
for parent in self.get_parent_nodes(node_key):
213227
parent_key = parent.get_key()
214228
if parent_key not in visited:
@@ -221,8 +235,8 @@ def reverse_traverse(self, fn: Callable[["AgentGraphNode", Dict[str, Any]], Any]
221235
if root_node_seen:
222236
root_node = self.root()
223237
if root_node is not None:
224-
execution_context[root_node.get_key()] = fn(root_node, execution_context)
238+
execution_context[root_node.get_key()] = fn(
239+
root_node, execution_context
240+
)
225241

226242
return execution_context[self._agent_graph.root_config_key]
227-
228-

packages/sdk/server-ai/src/ldai/client.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
from ldclient.client import LDClient
66

77
from ldai import log
8-
from ldai.chat import Chat
98
from ldai.agent_graph import AgentGraphDefinition
9+
from ldai.chat import Chat
1010
from ldai.judge import Judge
1111
from ldai.models import (AIAgentConfig, AIAgentConfigDefault,
12-
AIAgentConfigRequest, AIAgentGraphConfig, AIAgents, AICompletionConfig,
12+
AIAgentConfigRequest, AIAgentGraphConfig,
13+
AIAgentGraphResponse, AIAgents, AICompletionConfig,
1314
AICompletionConfigDefault, AIJudgeConfig,
14-
AIJudgeConfigDefault, JudgeConfiguration, LDMessage,
15-
ModelConfig, ProviderConfig, Edge)
15+
AIJudgeConfigDefault, Edge, JudgeConfiguration,
16+
LDMessage, ModelConfig, ProviderConfig)
1617
from ldai.providers.ai_provider_factory import AIProviderFactory
1718
from ldai.tracker import LDAIConfigTracker
1819

@@ -424,29 +425,29 @@ def agent_graph(
424425
self,
425426
key: str,
426427
context: Context,
427-
) -> AgentGraphDefinition:
428-
"""
428+
) -> AIAgentGraphResponse:
429+
"""`
429430
Retrieve an AI agent graph.
430431
"""
431432
variation = self._client.variation(key, context, {})
432433

433434
if not variation.get("rootConfigKey"):
434435
log.debug(f"Agent graph {key} is disabled, no root config key found")
435-
return { "enabled": False, "graph": None }
436+
return AIAgentGraphResponse(enabled=False, graph=None)
436437

437-
all_agent_keys = [variation["rootConfigKey"]] + [edge["targetConfig"] for edge in variation["edges"]]
438+
all_agent_keys = [variation["rootConfigKey"]] + [
439+
edge["targetConfig"] for edge in variation["edges"]
440+
]
438441
agent_configs = {
439442
key: self.agent_config(key, context, AIAgentConfigDefault(enabled=False))
440443
for key in all_agent_keys
441444
}
442445

443-
444446
if not all(config.enabled for config in agent_configs.values()):
445-
log.debug(f"Agent graph {key} is disabled, not all agent configs are enabled")
446-
return {
447-
"enabled": False,
448-
"graph": None,
449-
}
447+
log.debug(
448+
f"Agent graph {key} is disabled, not all agent configs are enabled"
449+
)
450+
return AIAgentGraphResponse(enabled=False, graph=None)
450451

451452
try:
452453
agent_graph_config = AIAgentGraphConfig(
@@ -466,22 +467,21 @@ def agent_graph(
466467
)
467468
except Exception as e:
468469
log.debug(f"Agent graph {key} is disabled, invalid agent graph config")
469-
return { "enabled": False, "graph": None }
470-
470+
return AIAgentGraphResponse(enabled=False, graph=None)
471471

472472
nodes = AgentGraphDefinition.build_nodes(
473473
agent_graph_config,
474474
agent_configs,
475475
)
476476

477-
return {
478-
"enabled": True,
479-
"graph": AgentGraphDefinition(
477+
return AIAgentGraphResponse(
478+
enabled=True,
479+
graph=AgentGraphDefinition(
480480
agent_graph=agent_graph_config,
481481
nodes=nodes,
482482
context=context,
483483
),
484-
}
484+
)
485485

486486
def agents(
487487
self,

packages/sdk/server-ai/src/ldai/models.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from dataclasses import dataclass, field
33
from typing import Any, Dict, List, Literal, Optional, Union
44

5+
from ldai.agent_graph import AgentGraphDefinition
56
from ldai.tracker import LDAIConfigTracker
67

78

@@ -341,16 +342,20 @@ class AIAgentConfigRequest:
341342
# ============================================================================
342343
# AI Config Agent Graph Edge Type
343344
# ============================================================================
345+
346+
344347
@dataclass
345348
class Edge:
346349
"""
347350
Edge configuration for an agent graph.
348351
"""
352+
349353
key: str
350354
source_config: str
351355
target_config: str
352356
handoff: Optional[dict] = None
353357

358+
354359
# ============================================================================
355360
# AI Config Agent Graph
356361
# ============================================================================
@@ -359,11 +364,26 @@ class AIAgentGraphConfig:
359364
"""
360365
Agent graph configuration.
361366
"""
367+
362368
key: str
363369
name: str
364370
root_config_key: str
365371
edges: List[Edge]
366-
description: Optional[str] = ''
372+
description: Optional[str] = ""
373+
374+
375+
# ============================================================================
376+
# AI Config Agent Graph Response
377+
# ============================================================================
378+
@dataclass
379+
class AIAgentGraphResponse:
380+
"""
381+
Agent graph response.
382+
"""
383+
384+
enabled: bool
385+
graph: Optional[AgentGraphDefinition] = None
386+
367387

368388
# ============================================================================
369389
# Deprecated Type Aliases for Backward Compatibility

0 commit comments

Comments
 (0)