tooluniverse 1.0.4__py3-none-any.whl → 1.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tooluniverse might be problematic. Click here for more details.

@@ -0,0 +1,288 @@
1
+ """
2
+ Tests for OpenRouter client integration.
3
+
4
+ These tests verify that the OpenRouter client is properly integrated
5
+ with the ToolUniverse system.
6
+ """
7
+
8
+ import os
9
+ import pytest
10
+ from unittest.mock import Mock, patch, MagicMock
11
+ from tooluniverse.llm_clients import OpenRouterClient
12
+ from tooluniverse.agentic_tool import AgenticTool
13
+
14
+
15
+ class TestOpenRouterClient:
16
+ """Test suite for OpenRouterClient."""
17
+
18
+ def test_client_initialization_without_api_key(self):
19
+ """Test that client raises error when API key is not set."""
20
+ # Remove API key if present
21
+ old_key = os.environ.pop("OPENROUTER_API_KEY", None)
22
+
23
+ try:
24
+ with pytest.raises(ValueError, match="OPENROUTER_API_KEY not set"):
25
+ logger = Mock()
26
+ OpenRouterClient("openai/gpt-4o", logger)
27
+ finally:
28
+ # Restore old key if it existed
29
+ if old_key:
30
+ os.environ["OPENROUTER_API_KEY"] = old_key
31
+
32
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "test_key"})
33
+ @patch("tooluniverse.llm_clients.OpenRouterClient._OpenAI")
34
+ def test_client_initialization_with_api_key(self, mock_openai_class):
35
+ """Test that client initializes correctly with API key."""
36
+ logger = Mock()
37
+ mock_client = Mock()
38
+ mock_openai_class.return_value = mock_client
39
+
40
+ client = OpenRouterClient("openai/gpt-4o", logger)
41
+
42
+ assert client.model_name == "openai/gpt-4o"
43
+ assert client.logger == logger
44
+ mock_openai_class.assert_called_once()
45
+
46
+ # Verify base_url and api_key
47
+ call_kwargs = mock_openai_class.call_args[1]
48
+ assert call_kwargs["base_url"] == "https://openrouter.ai/api/v1"
49
+ assert call_kwargs["api_key"] == "test_key"
50
+
51
+ @patch.dict(
52
+ os.environ,
53
+ {
54
+ "OPENROUTER_API_KEY": "test_key",
55
+ "OPENROUTER_SITE_URL": "https://example.com",
56
+ "OPENROUTER_SITE_NAME": "Test App"
57
+ }
58
+ )
59
+ @patch("tooluniverse.llm_clients.OpenRouterClient._OpenAI")
60
+ def test_client_with_optional_headers(self, mock_openai_class):
61
+ """Test that optional headers are set correctly."""
62
+ logger = Mock()
63
+ mock_client = Mock()
64
+ mock_openai_class.return_value = mock_client
65
+
66
+ client = OpenRouterClient("openai/gpt-4o", logger)
67
+
68
+ call_kwargs = mock_openai_class.call_args[1]
69
+ assert "default_headers" in call_kwargs
70
+ headers = call_kwargs["default_headers"]
71
+ assert headers["HTTP-Referer"] == "https://example.com"
72
+ assert headers["X-Title"] == "Test App"
73
+
74
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "test_key"})
75
+ @patch("tooluniverse.llm_clients.OpenRouterClient._OpenAI")
76
+ def test_resolve_default_max_tokens(self, mock_openai_class):
77
+ """Test max tokens resolution for known models."""
78
+ logger = Mock()
79
+ mock_client = Mock()
80
+ mock_openai_class.return_value = mock_client
81
+
82
+ client = OpenRouterClient("openai/gpt-4o", logger)
83
+
84
+ # Test known model
85
+ max_tokens = client._resolve_default_max_tokens("openai/gpt-4o")
86
+ assert max_tokens == 64000
87
+
88
+ # Test another known model
89
+ max_tokens = client._resolve_default_max_tokens("anthropic/claude-3.5-sonnet")
90
+ assert max_tokens == 8192
91
+
92
+ # Test unknown model
93
+ max_tokens = client._resolve_default_max_tokens("unknown/model")
94
+ assert max_tokens is None
95
+
96
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "test_key"})
97
+ @patch("tooluniverse.llm_clients.OpenRouterClient._OpenAI")
98
+ def test_infer_basic(self, mock_openai_class):
99
+ """Test basic inference functionality."""
100
+ logger = Mock()
101
+ mock_client = Mock()
102
+ mock_openai_class.return_value = mock_client
103
+
104
+ # Mock the completion response
105
+ mock_response = Mock()
106
+ mock_response.choices = [Mock()]
107
+ mock_response.choices[0].message.content = "Test response"
108
+ mock_client.chat.completions.create.return_value = mock_response
109
+
110
+ client = OpenRouterClient("openai/gpt-4o", logger)
111
+
112
+ messages = [{"role": "user", "content": "Test prompt"}]
113
+ result = client.infer(
114
+ messages=messages,
115
+ temperature=0.7,
116
+ max_tokens=100,
117
+ return_json=False
118
+ )
119
+
120
+ assert result == "Test response"
121
+ mock_client.chat.completions.create.assert_called_once()
122
+
123
+ # Verify call arguments
124
+ call_kwargs = mock_client.chat.completions.create.call_args[1]
125
+ assert call_kwargs["model"] == "openai/gpt-4o"
126
+ assert call_kwargs["messages"] == messages
127
+ assert call_kwargs["temperature"] == 0.7
128
+ assert call_kwargs["max_tokens"] == 100
129
+
130
+
131
+ class TestAgenticToolWithOpenRouter:
132
+ """Test AgenticTool integration with OpenRouter."""
133
+
134
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "test_key"})
135
+ @patch("tooluniverse.agentic_tool.OpenRouterClient")
136
+ def test_agentic_tool_with_openrouter(self, mock_client_class):
137
+ """Test that AgenticTool can use OpenRouter."""
138
+ # Mock the client
139
+ mock_client = Mock()
140
+ mock_client_class.return_value = mock_client
141
+ mock_client.test_api = Mock()
142
+ mock_client.infer = Mock(return_value="Test result")
143
+
144
+ # Create tool config
145
+ tool_config = {
146
+ "name": "Test_Tool",
147
+ "prompt": "Test prompt: {input}",
148
+ "input_arguments": ["input"],
149
+ "parameter": {
150
+ "type": "object",
151
+ "properties": {
152
+ "input": {"type": "string", "required": True}
153
+ },
154
+ "required": ["input"]
155
+ },
156
+ "configs": {
157
+ "api_type": "OPENROUTER",
158
+ "model_id": "openai/gpt-4o",
159
+ "temperature": 0.5,
160
+ "validate_api_key": True,
161
+ "return_metadata": False
162
+ }
163
+ }
164
+
165
+ # Create tool
166
+ tool = AgenticTool(tool_config)
167
+
168
+ # Verify initialization
169
+ assert tool._is_available
170
+ assert tool._current_api_type == "OPENROUTER"
171
+ assert tool._current_model_id == "openai/gpt-4o"
172
+ mock_client.test_api.assert_called_once()
173
+
174
+ # Test execution
175
+ result = tool.run({"input": "test data"})
176
+ assert result == "Test result"
177
+ mock_client.infer.assert_called_once()
178
+
179
+ def test_openrouter_in_supported_types(self):
180
+ """Test that OPENROUTER is in supported API types."""
181
+ tool_config = {
182
+ "name": "Test_Tool",
183
+ "prompt": "Test: {x}",
184
+ "input_arguments": ["x"],
185
+ "parameter": {
186
+ "type": "object",
187
+ "properties": {"x": {"type": "string"}},
188
+ "required": ["x"]
189
+ },
190
+ "configs": {
191
+ "api_type": "OPENROUTER",
192
+ "model_id": "openai/gpt-4o",
193
+ "validate_api_key": False
194
+ }
195
+ }
196
+
197
+ # This should not raise an error
198
+ try:
199
+ tool = AgenticTool(tool_config)
200
+ # Validation should pass
201
+ validation = tool.validate_configuration()
202
+ assert validation["valid"]
203
+ except ValueError as e:
204
+ if "Unsupported API type" in str(e):
205
+ pytest.fail("OPENROUTER should be a supported API type")
206
+
207
+
208
+ class TestOpenRouterModels:
209
+ """Test model configuration and limits."""
210
+
211
+ @patch.dict(os.environ, {"OPENROUTER_API_KEY": "test_key"})
212
+ @patch("tooluniverse.llm_clients.OpenRouterClient._OpenAI")
213
+ def test_model_limits_configuration(self, mock_openai_class):
214
+ """Test that model limits are correctly configured."""
215
+ logger = Mock()
216
+ mock_client = Mock()
217
+ mock_openai_class.return_value = mock_client
218
+
219
+ client = OpenRouterClient("openai/gpt-4o", logger)
220
+
221
+ # Check some key models
222
+ expected_models = {
223
+ "openai/gpt-4o": {"max_output": 64000, "context_window": 1_048_576},
224
+ "anthropic/claude-3.7-sonnet": {"max_output": 8192, "context_window": 200_000},
225
+ "google/gemini-2.0-flash-exp": {"max_output": 8192, "context_window": 1_048_576},
226
+ "qwen/qwq-32b-preview": {"max_output": 8192, "context_window": 32_768},
227
+ }
228
+
229
+ for model_id, expected_limits in expected_models.items():
230
+ assert model_id in client.DEFAULT_MODEL_LIMITS
231
+ assert client.DEFAULT_MODEL_LIMITS[model_id] == expected_limits
232
+
233
+ @patch.dict(
234
+ os.environ,
235
+ {
236
+ "OPENROUTER_API_KEY": "test_key",
237
+ "OPENROUTER_MAX_TOKENS_BY_MODEL": '{"openai/gpt-4o": 32000}'
238
+ }
239
+ )
240
+ @patch("tooluniverse.llm_clients.OpenRouterClient._OpenAI")
241
+ def test_env_override_max_tokens(self, mock_openai_class):
242
+ """Test that environment variables can override max tokens."""
243
+ logger = Mock()
244
+ mock_client = Mock()
245
+ mock_openai_class.return_value = mock_client
246
+
247
+ client = OpenRouterClient("openai/gpt-4o", logger)
248
+
249
+ # Should return the overridden value
250
+ max_tokens = client._resolve_default_max_tokens("openai/gpt-4o")
251
+ assert max_tokens == 32000
252
+
253
+
254
+ class TestOpenRouterFallback:
255
+ """Test fallback configuration with OpenRouter."""
256
+
257
+ def test_openrouter_in_default_fallback_chain(self):
258
+ """Test that OpenRouter is in the default fallback chain."""
259
+ from tooluniverse.agentic_tool import DEFAULT_FALLBACK_CHAIN
260
+
261
+ # Check that OPENROUTER is in the default chain
262
+ openrouter_configs = [
263
+ config for config in DEFAULT_FALLBACK_CHAIN
264
+ if config["api_type"] == "OPENROUTER"
265
+ ]
266
+
267
+ assert len(openrouter_configs) > 0, "OPENROUTER should be in default fallback chain"
268
+
269
+ # Verify it has a model_id
270
+ for config in openrouter_configs:
271
+ assert "model_id" in config
272
+ assert config["model_id"].startswith("openai/") or \
273
+ config["model_id"].startswith("anthropic/") or \
274
+ config["model_id"].startswith("google/") or \
275
+ config["model_id"].startswith("qwen/")
276
+
277
+ def test_openrouter_in_api_key_env_vars(self):
278
+ """Test that OPENROUTER is in API key environment variables mapping."""
279
+ from tooluniverse.agentic_tool import API_KEY_ENV_VARS
280
+
281
+ assert "OPENROUTER" in API_KEY_ENV_VARS
282
+ assert "OPENROUTER_API_KEY" in API_KEY_ENV_VARS["OPENROUTER"]
283
+
284
+
285
+ if __name__ == "__main__":
286
+ pytest.main([__file__, "-v"])
287
+
288
+
@@ -50,7 +50,7 @@ run_stdio_server()
50
50
  if not line:
51
51
  break
52
52
  print(f"启动日志: {line.strip()}")
53
- if "Starting SMCP ToolUniverse Server" in line:
53
+ if "Starting ToolUniverse SMCP Server" in line:
54
54
  break
55
55
 
56
56
  # 发送初始化请求
@@ -14,7 +14,7 @@ test_queries = [
14
14
  "return_call_result": False,
15
15
  },
16
16
  },
17
- {"name": "Tool_Finder_Keyword", "arguments": {"query": "disease", "limit": 5}},
17
+ {"name": "Tool_Finder_Keyword", "arguments": {"description": "disease", "limit": 5}},
18
18
  ]
19
19
 
20
20
  for idx, query in enumerate(test_queries):