Skip to content

Commit ffdea9f

Browse files
committed
fix test and linting
1 parent 97aa9f4 commit ffdea9f

File tree

2 files changed

+40
-59
lines changed

2 files changed

+40
-59
lines changed

ldai/client.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LDMessage:
1313
role: Literal['system', 'user', 'assistant']
1414
content: str
1515

16-
def to_dict(self) -> dict:
16+
def to_dict(self) -> Dict[str, Any]:
1717
"""
1818
Render the given message as a dictionary object.
1919
"""
@@ -69,7 +69,7 @@ def get_custom(self, key: str) -> Any:
6969

7070
return self._custom.get(key)
7171

72-
def to_dict(self) -> dict:
72+
def to_dict(self) -> Dict[str, Any]:
7373
"""
7474
Render the given model config as a dictionary object.
7575
"""
@@ -95,7 +95,7 @@ def name(self) -> str:
9595
"""
9696
return self._name
9797

98-
def to_dict(self) -> dict:
98+
def to_dict(self) -> Dict[str, Any]:
9999
"""
100100
Render the given provider config as a dictionary object.
101101
"""
@@ -111,7 +111,7 @@ class AIConfig:
111111
messages: Optional[List[LDMessage]] = None
112112
provider: Optional[ProviderConfig] = None
113113

114-
def to_dict(self) -> dict:
114+
def to_dict(self) -> Dict[str, Any]:
115115
"""
116116
Render the given default values as an AIConfig-compatible dictionary object.
117117
"""
@@ -129,7 +129,7 @@ def to_dict(self) -> dict:
129129
class LDAIAgent:
130130
"""
131131
Represents an AI agent configuration with instructions and model settings.
132-
132+
133133
An agent is similar to an AIConfig but focuses on instructions rather than messages,
134134
making it suitable for AI assistant/agent use cases.
135135
"""
@@ -139,11 +139,11 @@ class LDAIAgent:
139139
instructions: Optional[str] = None
140140
tracker: Optional[LDAIConfigTracker] = None
141141

142-
def to_dict(self) -> dict:
142+
def to_dict(self) -> Dict[str, Any]:
143143
"""
144144
Render the given agent as a dictionary object.
145145
"""
146-
result = {
146+
result: Dict[str, Any] = {
147147
'_ldMeta': {
148148
'enabled': self.enabled or False,
149149
},
@@ -159,7 +159,7 @@ def to_dict(self) -> dict:
159159
class LDAIAgentDefaults:
160160
"""
161161
Default values for AI agent configurations.
162-
162+
163163
Similar to LDAIAgent but without tracker and with optional enabled field,
164164
used as fallback values when agent configurations are not available.
165165
"""
@@ -168,11 +168,11 @@ class LDAIAgentDefaults:
168168
provider: Optional[ProviderConfig] = None
169169
instructions: Optional[str] = None
170170

171-
def to_dict(self) -> dict:
171+
def to_dict(self) -> Dict[str, Any]:
172172
"""
173173
Render the given agent defaults as a dictionary object.
174174
"""
175-
result = {
175+
result: Dict[str, Any] = {
176176
'_ldMeta': {
177177
'enabled': self.enabled or False,
178178
},
@@ -210,7 +210,7 @@ def config(
210210
:param variables: Additional variables for the model configuration.
211211
:return: The value of the model configuration along with a tracker used for gathering metrics.
212212
"""
213-
model, provider, messages, tracker, enabled = self.__evaluate(key, context, default_value.to_dict(), variables)
213+
model, provider, messages, instructions, tracker, enabled = self.__evaluate(key, context, default_value.to_dict(), variables)
214214

215215
config = AIConfig(
216216
enabled=bool(enabled),
@@ -247,7 +247,7 @@ def agents(
247247
),
248248
{'company_name': 'Acme Corp'}
249249
)
250-
250+
251251
support_agent = agents['customer-support']
252252
if support_agent.enabled:
253253
print(support_agent.instructions) # Instructions with interpolated variables
@@ -261,11 +261,11 @@ def agents(
261261
:return: Dictionary mapping agent keys to their LDAIAgent configurations.
262262
"""
263263
result: LDAIAgents = {}
264-
264+
265265
for key in keys:
266266
agent = self.__evaluate_agent(key, context, default_value, variables)
267267
result[key] = agent
268-
268+
269269
return result
270270

271271
def __evaluate(
@@ -274,10 +274,10 @@ def __evaluate(
274274
context: Context,
275275
default_dict: Dict[str, Any],
276276
variables: Optional[Dict[str, Any]] = None,
277-
) -> Tuple[Optional[ModelConfig], Optional[ProviderConfig], Optional[List[LDMessage]], Optional[str], LDAIConfigTracker]:
277+
) -> Tuple[Optional[ModelConfig], Optional[ProviderConfig], Optional[List[LDMessage]], Optional[str], LDAIConfigTracker, bool]:
278278
"""
279279
Internal method to evaluate a configuration and extract components.
280-
280+
281281
:param key: The configuration key.
282282
:param context: The evaluation context.
283283
:param default_dict: Default configuration as dictionary.
@@ -350,14 +350,14 @@ def __evaluate_agent(
350350
) -> LDAIAgent:
351351
"""
352352
Internal method to evaluate an agent configuration.
353-
353+
354354
:param key: The agent configuration key.
355355
:param context: The evaluation context.
356356
:param default_value: Default agent values.
357357
:param variables: Variables for interpolation.
358358
:return: Configured LDAIAgent instance.
359359
"""
360-
model, provider, instructions, tracker, enabled = self.__evaluate(
360+
model, provider, messages, instructions, tracker, enabled = self.__evaluate(
361361
key, context, default_value.to_dict(), variables
362362
)
363363

ldai/testing/test_agents.py

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
from ldclient import Config, Context, LDClient
33
from ldclient.integrations.test_data import TestData
44

5-
from ldai.client import LDAIAgent, LDAIAgentDefaults, LDAIClient, ModelConfig, ProviderConfig
5+
from ldai.client import (LDAIAgentDefaults, LDAIClient, ModelConfig,
6+
ProviderConfig)
67

78

89
@pytest.fixture
910
def td() -> TestData:
1011
td = TestData.data_source()
11-
12+
1213
# Single agent with instructions
1314
td.update(
1415
td.flag('customer-support-agent')
@@ -116,7 +117,7 @@ def test_single_agent_basic_functionality(ldai_client: LDAIClient):
116117

117118
assert len(agents) == 1
118119
assert 'customer-support-agent' in agents
119-
120+
120121
agent = agents['customer-support-agent']
121122
assert agent.enabled is True
122123
assert agent.model is not None
@@ -159,7 +160,7 @@ def test_agent_multi_context_interpolation(ldai_client: LDAIClient):
159160
user_context = Context.builder('user-key').name('Bob').build()
160161
org_context = Context.builder('org-key').kind('org').name('LaunchDarkly').set('tier', 'Enterprise').build()
161162
context = Context.multi_builder().add(user_context).add(org_context).build()
162-
163+
163164
defaults = LDAIAgentDefaults(enabled=True, instructions="Default")
164165

165166
agents = ldai_client.agents(['multi-context-agent'], context, defaults)
@@ -180,9 +181,9 @@ def test_multiple_agents_retrieval(ldai_client: LDAIClient):
180181
variables = {'company_name': 'MultiCorp'}
181182

182183
agents = ldai_client.agents(
183-
['customer-support-agent', 'sales-assistant'],
184-
context,
185-
defaults,
184+
['customer-support-agent', 'sales-assistant'],
185+
context,
186+
defaults,
186187
variables
187188
)
188189

@@ -192,12 +193,12 @@ def test_multiple_agents_retrieval(ldai_client: LDAIClient):
192193

193194
support_agent = agents['customer-support-agent']
194195
assert support_agent.enabled is True
195-
assert 'MultiCorp' in support_agent.instructions
196+
assert support_agent.instructions is not None and 'MultiCorp' in support_agent.instructions
196197

197198
sales_agent = agents['sales-assistant']
198199
assert sales_agent.enabled is True
199-
assert 'MultiCorp' in sales_agent.instructions
200-
assert sales_agent.model.get_parameter('temperature') == 0.7
200+
assert sales_agent.instructions is not None and 'MultiCorp' in sales_agent.instructions
201+
assert sales_agent.model is not None and sales_agent.model.get_parameter('temperature') == 0.7
201202

202203

203204
def test_disabled_agent(ldai_client: LDAIClient):
@@ -244,9 +245,9 @@ def test_agent_uses_defaults_on_missing_flag(ldai_client: LDAIClient):
244245
agent = agents['non-existent-agent']
245246

246247
assert agent.enabled == defaults.enabled
247-
assert agent.model.name == 'default-gpt'
248-
assert agent.model.get_parameter('temp') == 0.5
249-
assert agent.provider.name == 'default-provider'
248+
assert agent.model is not None and agent.model.name == 'default-gpt'
249+
assert agent.model is not None and agent.model.get_parameter('temp') == 0.5
250+
assert agent.provider is not None and agent.provider.name == 'default-provider'
250251
assert agent.instructions == defaults.instructions
251252
# Tracker should still be created for non-existent flags
252253
assert agent.tracker is not None
@@ -263,13 +264,13 @@ def test_agent_error_handling(ldai_client: LDAIClient):
263264

264265
# Test with a mix of valid and invalid keys
265266
agents = ldai_client.agents(
266-
['customer-support-agent', 'invalid-flag'],
267-
context,
267+
['customer-support-agent', 'invalid-flag'],
268+
context,
268269
defaults
269270
)
270271

271272
assert len(agents) == 2
272-
273+
273274
# Valid agent should work normally
274275
valid_agent = agents['customer-support-agent']
275276
assert valid_agent.enabled is True
@@ -278,7 +279,7 @@ def test_agent_error_handling(ldai_client: LDAIClient):
278279
# Invalid agent should use defaults but still be created
279280
invalid_agent = agents['invalid-flag']
280281
assert invalid_agent.enabled == defaults.enabled
281-
assert invalid_agent.model.name == 'fallback-model'
282+
assert invalid_agent.model is not None and invalid_agent.model.name == 'fallback-model'
282283
assert invalid_agent.instructions == defaults.instructions
283284

284285

@@ -301,35 +302,15 @@ def test_agent_empty_agent_list(ldai_client: LDAIClient):
301302
defaults = LDAIAgentDefaults(enabled=True, instructions="Default")
302303

303304
agents = ldai_client.agents([], context, defaults)
304-
305+
305306
assert len(agents) == 0
306307
assert agents == {}
307308

308309

309-
def test_agent_tracker_functionality(ldai_client: LDAIClient):
310-
"""Test that agent tracker works correctly."""
311-
context = Context.create('user-key')
312-
defaults = LDAIAgentDefaults(enabled=True, instructions="Default")
313-
314-
agents = ldai_client.agents(['customer-support-agent'], context, defaults)
315-
agent = agents['customer-support-agent']
316-
317-
assert agent.tracker is not None
318-
assert hasattr(agent.tracker, 'track_success')
319-
assert hasattr(agent.tracker, 'track_duration')
320-
assert hasattr(agent.tracker, 'track_tokens')
321-
322-
# Test that tracker has correct metadata
323-
track_data = agent.tracker._LDAIConfigTracker__get_track_data()
324-
assert track_data['variationKey'] == 'agent-v1'
325-
assert track_data['configKey'] == 'customer-support-agent'
326-
assert track_data['version'] == 1
327-
328-
329310
def test_agents_backwards_compatibility_with_config(ldai_client: LDAIClient):
330311
"""Test that the existing config method still works after agent additions."""
331312
from ldai.client import AIConfig, LDMessage
332-
313+
333314
context = Context.create('user-key')
334315
default_value = AIConfig(
335316
enabled=True,
@@ -339,7 +320,7 @@ def test_agents_backwards_compatibility_with_config(ldai_client: LDAIClient):
339320

340321
# This should still work as before
341322
config, tracker = ldai_client.config('customer-support-agent', context, default_value)
342-
323+
343324
assert config.enabled is True
344325
assert config.model is not None
345-
assert tracker is not None
326+
assert tracker is not None

0 commit comments

Comments
 (0)