Skip to content

Commit b2b25c6

Browse files
committed
fixed ruff
1 parent 6409546 commit b2b25c6

9 files changed

Lines changed: 123 additions & 60 deletions

File tree

lightspeed_stack_providers/providers/inline/agents/lightspeed_inline_agent/agents.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,9 @@ async def _filter_tools_for_response(
222222
model=tools_filter_model_id,
223223
messages=[
224224
OpenAISystemMessageParam(
225-
role="system",
226-
content=self.config.tools_filter.system_prompt
227-
),
228-
OpenAIUserMessageParam(
229-
role="user",
230-
content=filter_prompt
225+
role="system", content=self.config.tools_filter.system_prompt
231226
),
227+
OpenAIUserMessageParam(role="user", content=filter_prompt),
232228
],
233229
stream=False,
234230
temperature=0.1,
@@ -289,7 +285,11 @@ async def _get_previously_called_tools(self, conversation_id: str) -> set[str]:
289285
tool_names: set[str] = set()
290286
try:
291287
items_response = await self.conversations_api.list_items(conversation_id)
292-
items = items_response.data if hasattr(items_response, "data") else items_response
288+
items = (
289+
items_response.data
290+
if hasattr(items_response, "data")
291+
else items_response
292+
)
293293
for item in items:
294294
item_type = getattr(item, "type", None)
295295
if item_type == "function_call":
@@ -330,23 +330,24 @@ async def _extract_tool_definitions(
330330
mcp_tools = await self._get_mcp_tool_definitions(tool_dict)
331331
tool_defs.extend(mcp_tools)
332332
elif tool_type == "file_search":
333-
tool_defs.append({
334-
"tool_name": "file_search",
335-
"description": "Search through uploaded files and knowledge base"
336-
})
333+
tool_defs.append(
334+
{
335+
"tool_name": "file_search",
336+
"description": "Search through uploaded files and knowledge base",
337+
}
338+
)
337339
elif tool_type == "function":
338340
name = tool_dict.get("name", f"function_{i}")
339341
description = tool_dict.get("description", "")
340-
tool_defs.append({
341-
"tool_name": name,
342-
"description": description
343-
})
342+
tool_defs.append({"tool_name": name, "description": description})
344343
else:
345344
logger.warning("Unknown tool type: %s", tool_type)
346-
tool_defs.append({
347-
"tool_name": f"{tool_type}_{i}",
348-
"description": f"Tool of type {tool_type}"
349-
})
345+
tool_defs.append(
346+
{
347+
"tool_name": f"{tool_type}_{i}",
348+
"description": f"Tool of type {tool_type}",
349+
}
350+
)
350351

351352
return tool_defs
352353

@@ -380,10 +381,12 @@ async def _get_mcp_tool_definitions(
380381
)
381382

382383
for tool_def in tools_response.data:
383-
tool_defs.append({
384-
"tool_name": tool_def.name,
385-
"description": tool_def.description or ""
386-
})
384+
tool_defs.append(
385+
{
386+
"tool_name": tool_def.name,
387+
"description": tool_def.description or "",
388+
}
389+
)
387390

388391
logger.debug(
389392
"Retrieved %d tools from MCP server %s",
@@ -392,10 +395,12 @@ async def _get_mcp_tool_definitions(
392395
)
393396
except Exception as e:
394397
logger.error("Failed to get MCP tool definitions: %s", e)
395-
tool_defs.append({
396-
"tool_name": mcp_tool_config.get("server_label", "mcp_tool"),
397-
"description": "MCP tool server"
398-
})
398+
tool_defs.append(
399+
{
400+
"tool_name": mcp_tool_config.get("server_label", "mcp_tool"),
401+
"description": "MCP tool server",
402+
}
403+
)
399404

400405
return tool_defs
401406

lightspeed_stack_providers/providers/inline/safety/lightspeed_question_validity/safety.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,15 @@ async def run_shield(
113113
) -> RunShieldResponse:
114114
# Take last user message (can be UserMessage or OpenAIUserMessageParam)
115115
user_messages = [
116-
m for m in messages
117-
if isinstance(m, (UserMessage, OpenAIUserMessageParam)) or
118-
(hasattr(m, 'role') and m.role == 'user')
116+
m
117+
for m in messages
118+
if isinstance(m, (UserMessage, OpenAIUserMessageParam))
119+
or (hasattr(m, "role") and m.role == "user")
119120
]
120121
if not user_messages:
121122
return RunShieldResponse(violation=None)
122123
last_msg = user_messages[-1]
123-
content = last_msg.content if hasattr(last_msg, 'content') else str(last_msg)
124+
content = last_msg.content if hasattr(last_msg, "content") else str(last_msg)
124125
message = UserMessage(content=content)
125126
log.debug(f"Shield UserMessage: {message.content}")
126127

@@ -179,7 +180,9 @@ async def run(self, message: UserMessage) -> RunShieldResponse:
179180
request = OpenAIChatCompletionRequestWithExtraBody(
180181
model=self.model_id,
181182
messages=[
182-
OpenAIUserMessageParam(role="user", content=shield_input_message.content),
183+
OpenAIUserMessageParam(
184+
role="user", content=shield_input_message.content
185+
),
183186
],
184187
stream=False,
185188
)

lightspeed_stack_providers/providers/inline/safety/lightspeed_redaction/lightspeed_redaction/redaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def run_shield(
9090
"""Run the redaction shield - mutates messages directly."""
9191

9292
for message in messages:
93-
if hasattr(message, 'content') and isinstance(message.content, str):
93+
if hasattr(message, "content") and isinstance(message.content, str):
9494
original_content: str = message.content
9595
redacted_content: str = self._apply_redaction_rules(original_content)
9696

lightspeed_stack_providers/providers/remote/agents/lightspeed_agent/lightspeed.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ def __init__(self, config: LightspeedAgentConfig):
3636

3737
async def initialize(self) -> None:
3838
"""Initialize the remote agent provider."""
39-
logger.info("Initializing LightspeedRemoteAgentProvider with URL: %s", self.lightspeed_agent_url)
39+
logger.info(
40+
"Initializing LightspeedRemoteAgentProvider with URL: %s",
41+
self.lightspeed_agent_url,
42+
)
4043

4144
async def shutdown(self) -> None:
4245
"""Shutdown the remote agent provider."""
@@ -90,16 +93,19 @@ async def create_openai_response(
9093
if tools:
9194
# Convert tools to dict format if they're objects
9295
payload["tools"] = [
93-
t if isinstance(t, dict) else t.model_dump()
94-
for t in tools
96+
t if isinstance(t, dict) else t.model_dump() for t in tools
9597
]
9698
if instructions:
9799
payload["instructions"] = instructions
98100

99101
# Add any additional kwargs
100102
payload.update(kwargs)
101103

102-
logger.debug("Proxying request to %s with payload: %s", self.lightspeed_agent_url, payload)
104+
logger.debug(
105+
"Proxying request to %s with payload: %s",
106+
self.lightspeed_agent_url,
107+
payload,
108+
)
103109

104110
if stream:
105111
return self._stream_response(payload)
@@ -118,7 +124,9 @@ async def _get_response(self, payload: dict) -> OpenAIResponseObject:
118124
data = response.json()
119125
return OpenAIResponseObject.model_validate(data)
120126

121-
async def _stream_response(self, payload: dict) -> AsyncIterator[OpenAIResponseObjectStream]:
127+
async def _stream_response(
128+
self, payload: dict
129+
) -> AsyncIterator[OpenAIResponseObjectStream]:
122130
"""Stream response from the remote agent."""
123131
async with httpx.AsyncClient() as client:
124132
async with client.stream(

tests/unit/providers/inline/agents/lightspeed_inline_agent/test_agent_instance_inline_agent.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ def lightspeed_agents_impl(
4444
"""Fixture for creating a LightspeedAgentsImpl instance."""
4545
persistence = AgentPersistenceConfig(
4646
agent_state=KVStoreReference(namespace="test", backend="in_memory"),
47-
responses=ResponsesStoreReference(table_name="test_responses", backend="in_memory"),
47+
responses=ResponsesStoreReference(
48+
table_name="test_responses", backend="in_memory"
49+
),
4850
)
4951
config = LightspeedAgentsImplConfig(
50-
persistence=persistence,
51-
tools_filter=ToolsFilter(enabled=True, min_tools=0)
52+
persistence=persistence, tools_filter=ToolsFilter(enabled=True, min_tools=0)
5253
)
5354
impl = LightspeedAgentsImpl(
5455
config=config,
@@ -99,8 +100,16 @@ async def test_filter_tools_for_response_filters_correctly(
99100

100101
# Create MCP tools
101102
tools = [
102-
{"type": "mcp", "server_label": "mcp_server1", "server_url": "http://test1.com"},
103-
{"type": "mcp", "server_label": "mcp_server2", "server_url": "http://test2.com"},
103+
{
104+
"type": "mcp",
105+
"server_label": "mcp_server1",
106+
"server_url": "http://test1.com",
107+
},
108+
{
109+
"type": "mcp",
110+
"server_label": "mcp_server2",
111+
"server_url": "http://test2.com",
112+
},
104113
]
105114

106115
# Call the filtering method
@@ -138,8 +147,16 @@ async def test_filter_tools_for_response_includes_always_included_tools(
138147

139148
# Create MCP tools
140149
tools = [
141-
{"type": "mcp", "server_label": "mcp_server1", "server_url": "http://test1.com"},
142-
{"type": "mcp", "server_label": "mcp_server2", "server_url": "http://test2.com"},
150+
{
151+
"type": "mcp",
152+
"server_label": "mcp_server1",
153+
"server_url": "http://test1.com",
154+
},
155+
{
156+
"type": "mcp",
157+
"server_label": "mcp_server2",
158+
"server_url": "http://test2.com",
159+
},
143160
]
144161

145162
# Call the filtering method

tests/unit/providers/inline/agents/lightspeed_inline_agent/test_agents_inline_agent.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ def lightspeed_agents_impl(mock_inference_api, mock_conversations_api, mocker):
3434
"""Fixture for creating a LightspeedAgentsImpl instance."""
3535
persistence = AgentPersistenceConfig(
3636
agent_state=KVStoreReference(namespace="test", backend="in_memory"),
37-
responses=ResponsesStoreReference(table_name="test_responses", backend="in_memory"),
37+
responses=ResponsesStoreReference(
38+
table_name="test_responses", backend="in_memory"
39+
),
3840
)
3941
config = LightspeedAgentsImplConfig(
40-
persistence=persistence,
41-
tools_filter=ToolsFilter(enabled=True, min_tools=5)
42+
persistence=persistence, tools_filter=ToolsFilter(enabled=True, min_tools=5)
4243
)
4344
impl = LightspeedAgentsImpl(
4445
config=config,
@@ -66,7 +67,9 @@ def test_lightspeed_agents_impl_config_defaults():
6667
"""Test that LightspeedAgentsImplConfig has correct defaults."""
6768
persistence = AgentPersistenceConfig(
6869
agent_state=KVStoreReference(namespace="test", backend="in_memory"),
69-
responses=ResponsesStoreReference(table_name="test_responses", backend="in_memory"),
70+
responses=ResponsesStoreReference(
71+
table_name="test_responses", backend="in_memory"
72+
),
7073
)
7174
config = LightspeedAgentsImplConfig(persistence=persistence)
7275
assert config.tools_filter is not None

tests/unit/providers/inline/agents/lightspeed_inline_agent/test_config_inline_agent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import os
44

55
from llama_stack.core.storage.datatypes import KVStoreReference, ResponsesStoreReference
6-
from llama_stack.providers.inline.agents.meta_reference.config import AgentPersistenceConfig
6+
from llama_stack.providers.inline.agents.meta_reference.config import (
7+
AgentPersistenceConfig,
8+
)
79

810
from lightspeed_stack_providers.providers.inline.agents.lightspeed_inline_agent.config import (
911
ToolsFilter,
@@ -59,7 +61,9 @@ def test_lightspeed_agents_impl_config_defaults():
5961
"""Test that the LightspeedAgentsImplConfig model can be instantiated with default values."""
6062
persistence = AgentPersistenceConfig(
6163
agent_state=KVStoreReference(namespace="test", backend="in_memory"),
62-
responses=ResponsesStoreReference(table_name="test_responses", backend="in_memory"),
64+
responses=ResponsesStoreReference(
65+
table_name="test_responses", backend="in_memory"
66+
),
6367
)
6468
config = LightspeedAgentsImplConfig(persistence=persistence)
6569
assert isinstance(config.tools_filter, ToolsFilter)

tests/unit/providers/inline/safety/lightspeed_question_validity/test_safety_question_validity.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
def mock_inference_api():
1717
"""Fixture for mocking the Inference API."""
1818
from unittest.mock import AsyncMock
19+
1920
return AsyncMock()
2021

2122

@@ -133,7 +134,12 @@ async def test_run_shield_allowed(question_validity_shield_impl, mocker):
133134
)
134135
# Use OpenAI message format
135136
from llama_stack_api.inference import OpenAIUserMessageParam
136-
messages = [OpenAIUserMessageParam(role="user", content="How do I create a Kubernetes service?")]
137+
138+
messages = [
139+
OpenAIUserMessageParam(
140+
role="user", content="How do I create a Kubernetes service?"
141+
)
142+
]
137143

138144
response = await question_validity_shield_impl.run_shield("test_shield", messages)
139145

@@ -157,7 +163,10 @@ async def test_run_shield_rejected(question_validity_shield_impl, mocker):
157163
)
158164
# Use OpenAI message format
159165
from llama_stack_api.inference import OpenAIUserMessageParam
160-
messages = [OpenAIUserMessageParam(role="user", content="What is the weather today?")]
166+
167+
messages = [
168+
OpenAIUserMessageParam(role="user", content="What is the weather today?")
169+
]
161170

162171
response = await question_validity_shield_impl.run_shield("test_shield", messages)
163172

tests/unit/providers/remote/agents/lightspeed_agent/test_lightspeed_remote_agent.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,29 @@ def lightspeed_remote_agent_provider():
2020

2121
def test_provider_initialization(lightspeed_remote_agent_provider):
2222
"""Test that the provider initializes correctly."""
23-
assert lightspeed_remote_agent_provider.lightspeed_agent_url == "http://test.com/agent"
23+
assert (
24+
lightspeed_remote_agent_provider.lightspeed_agent_url == "http://test.com/agent"
25+
)
2426
assert lightspeed_remote_agent_provider.config is not None
2527

2628

2729
@pytest.mark.asyncio
28-
async def test_create_openai_response_non_streaming(lightspeed_remote_agent_provider, mocker):
30+
async def test_create_openai_response_non_streaming(
31+
lightspeed_remote_agent_provider, mocker
32+
):
2933
"""Test the create_openai_response method with non-streaming."""
3034
mock_response_data = {
3135
"id": "resp_123",
3236
"object": "response",
3337
"created_at": 1234567890,
3438
"model": "test_model",
35-
"output": [{"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "Hello!"}]}],
39+
"output": [
40+
{
41+
"type": "message",
42+
"role": "assistant",
43+
"content": [{"type": "output_text", "text": "Hello!"}],
44+
}
45+
],
3646
"status": "completed",
3747
}
3848

@@ -57,7 +67,9 @@ async def test_create_openai_response_non_streaming(lightspeed_remote_agent_prov
5767

5868

5969
@pytest.mark.asyncio
60-
async def test_create_openai_response_streaming(lightspeed_remote_agent_provider, mocker):
70+
async def test_create_openai_response_streaming(
71+
lightspeed_remote_agent_provider, mocker
72+
):
6173
"""Test the create_openai_response method with streaming."""
6274
# Create valid response.created stream chunks
6375
response_obj = {
@@ -79,8 +91,8 @@ async def mock_stream(*args, **kwargs):
7991
mock_response.raise_for_status = mocker.MagicMock()
8092

8193
async def async_generator():
82-
yield f'data: {json.dumps(chunk1)}'
83-
yield f'data: {json.dumps(chunk2)}'
94+
yield f"data: {json.dumps(chunk1)}"
95+
yield f"data: {json.dumps(chunk2)}"
8496
yield "data: [DONE]"
8597

8698
mock_response.aiter_lines = async_generator
@@ -100,7 +112,9 @@ async def async_generator():
100112

101113

102114
@pytest.mark.asyncio
103-
async def test_create_openai_response_with_tools(lightspeed_remote_agent_provider, mocker):
115+
async def test_create_openai_response_with_tools(
116+
lightspeed_remote_agent_provider, mocker
117+
):
104118
"""Test the create_openai_response method with tools."""
105119
mock_response_data = {
106120
"id": "resp_123",

0 commit comments

Comments
 (0)