modis-py-tools 0.1.0__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.
- modis_py_tools-0.1.0.dist-info/METADATA +281 -0
- modis_py_tools-0.1.0.dist-info/RECORD +25 -0
- modis_py_tools-0.1.0.dist-info/WHEEL +5 -0
- modis_py_tools-0.1.0.dist-info/top_level.txt +1 -0
- modis_tools/__init__.py +65 -0
- modis_tools/_version.py +3 -0
- modis_tools/builtins/__init__.py +25 -0
- modis_tools/builtins/python.py +119 -0
- modis_tools/builtins/shell.py +227 -0
- modis_tools/builtins/shell_policy.py +240 -0
- modis_tools/builtins/web.py +281 -0
- modis_tools/conversion.py +175 -0
- modis_tools/providers/__init__.py +13 -0
- modis_tools/providers/valyu.py +268 -0
- modis_tools/providers/web.py +166 -0
- modis_tools/py.typed +1 -0
- modis_tools/registry.py +165 -0
- modis_tools/results.py +48 -0
- modis_tools/schemas.py +121 -0
- modis_tools/serialization.py +59 -0
- modis_tools/skills/__init__.py +22 -0
- modis_tools/skills/models.py +45 -0
- modis_tools/skills/prompt.py +60 -0
- modis_tools/skills/registry.py +153 -0
- modis_tools/skills/tools.py +128 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: modis-py-tools
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Common MoDIS-compatible Python tools and tool-call execution helpers
|
|
5
|
+
Requires-Python: >=3.11
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: pydantic>=2.7.0
|
|
8
|
+
Provides-Extra: web
|
|
9
|
+
Requires-Dist: valyu>=2.0.0; extra == "web"
|
|
10
|
+
Provides-Extra: browser
|
|
11
|
+
Requires-Dist: markdownify>=0.12.0; extra == "browser"
|
|
12
|
+
Requires-Dist: playwright>=1.45.0; extra == "browser"
|
|
13
|
+
Requires-Dist: readability-lxml>=0.8.1; extra == "browser"
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: build>=1.2.0; extra == "dev"
|
|
16
|
+
Requires-Dist: mypy>=1.10.0; extra == "dev"
|
|
17
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
18
|
+
Requires-Dist: ruff>=0.8.0; extra == "dev"
|
|
19
|
+
|
|
20
|
+
# MoDIS Python Tools
|
|
21
|
+
|
|
22
|
+
`modis-py-tools` provides MoDIS-compatible function tool definitions, tool
|
|
23
|
+
groups, and tool-call execution helpers for Python applications.
|
|
24
|
+
|
|
25
|
+
The distribution name is currently `modis-py-tools`; the import package is
|
|
26
|
+
`modis_tools`.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
python -m pip install modis-py-tools
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Optional web provider dependency:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python -m pip install "modis-py-tools[web]"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Development:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
python -m pip install -e ".[dev,web]"
|
|
44
|
+
.venv/bin/python -m pytest
|
|
45
|
+
.venv/bin/python -m ruff check .
|
|
46
|
+
.venv/bin/python -m ruff format --check .
|
|
47
|
+
.venv/bin/python -m mypy src
|
|
48
|
+
.venv/bin/python -m build
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Function Conversion
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from modis_tools import function_to_tool
|
|
55
|
+
|
|
56
|
+
def lookup_order(order_id: str, include_history: bool = False) -> dict:
|
|
57
|
+
"""Look up an order.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
order_id: Order identifier.
|
|
61
|
+
include_history: Whether to include status history.
|
|
62
|
+
"""
|
|
63
|
+
return {"order_id": order_id, "status": "processing"}
|
|
64
|
+
|
|
65
|
+
tool = function_to_tool(lookup_order, name="orders.lookup")
|
|
66
|
+
tool_definition = tool.to_wire()
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Registry Execution
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from modis_tools import ToolRegistry
|
|
73
|
+
from modis_tools.builtins import python_group, shell_group
|
|
74
|
+
|
|
75
|
+
registry = ToolRegistry()
|
|
76
|
+
registry.include(shell_group())
|
|
77
|
+
registry.include(python_group())
|
|
78
|
+
|
|
79
|
+
tool_definitions = registry.definitions()
|
|
80
|
+
results = registry.execute_tool_calls(model_tool_calls)
|
|
81
|
+
messages = [result.to_chat_message() for result in results]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`execute_tool_call()` and `execute_tool_calls()` return failed `ToolResult`
|
|
85
|
+
objects by default for unknown tools, invalid arguments, and tool exceptions.
|
|
86
|
+
Pass `raise_on_error=True` when the host application should handle exceptions.
|
|
87
|
+
|
|
88
|
+
For a fuller host integration guide, including unattended pipeline setup and
|
|
89
|
+
future image generation extension notes, see `docs/INTEGRATION.md`.
|
|
90
|
+
|
|
91
|
+
## Built-In Tools
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from modis_tools.builtins import python_group, shell_group, skills_group, web_group
|
|
95
|
+
|
|
96
|
+
shell_tools = shell_group()
|
|
97
|
+
python_tools = python_group()
|
|
98
|
+
skill_tools = skills_group(skill_home="./skills")
|
|
99
|
+
web_tools = web_group(api_key="...")
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`shell.run` captures `stdout`, `stderr`, exit code, duration, timeout state,
|
|
103
|
+
working directory, policy decision metadata, and truncation metadata.
|
|
104
|
+
`python.run` executes Python source in a subprocess using the current
|
|
105
|
+
interpreter by default.
|
|
106
|
+
|
|
107
|
+
Shell and Python tools execute local code. Only expose them in trusted contexts
|
|
108
|
+
where the caller owns policy, sandboxing, and approval decisions.
|
|
109
|
+
|
|
110
|
+
Shell tools support host-owned policies. Policies are bound by the application
|
|
111
|
+
when registering the group and are not exposed as model tool-call parameters.
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
from modis_tools import ToolRegistry
|
|
115
|
+
from modis_tools.builtins import ShellPolicy, shell_group
|
|
116
|
+
|
|
117
|
+
registry = ToolRegistry()
|
|
118
|
+
registry.include(shell_group(policy=ShellPolicy.readonly_project("./")))
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Built-in shell policy profiles:
|
|
122
|
+
|
|
123
|
+
| Profile | Purpose |
|
|
124
|
+
| --- | --- |
|
|
125
|
+
| `trusted_local` | Backward-compatible default for trusted local use. |
|
|
126
|
+
| `disabled` | Denies every shell request. |
|
|
127
|
+
| `restricted` | Deterministic allowlist for unattended pipelines. |
|
|
128
|
+
| `readonly_project` | Read-oriented `argv` commands inside one project root. |
|
|
129
|
+
|
|
130
|
+
`shell.run` accepts exactly one of `cmd` or `argv`. `cmd` executes with
|
|
131
|
+
`shell=True`; `argv` executes with `shell=False`. Restricted policies should
|
|
132
|
+
prefer `argv` mode. Policy controls are guardrails, not a strong OS sandbox; use
|
|
133
|
+
containers, VMs, or other isolation for untrusted execution.
|
|
134
|
+
|
|
135
|
+
## Skill Tools
|
|
136
|
+
|
|
137
|
+
Skill support is split into runtime visibility, optional host-owned matching, and
|
|
138
|
+
tool-based skill use. `modis-tools` provides the use layer: a registry, a prompt
|
|
139
|
+
generator, and `skills.*` tools for progressive loading.
|
|
140
|
+
|
|
141
|
+
`skill_home` is required in every mode because all skill files must resolve from
|
|
142
|
+
a trusted root.
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
from modis_tools import ToolRegistry
|
|
146
|
+
from modis_tools.builtins import skills_group
|
|
147
|
+
from modis_tools.skills import SkillRegistry, build_skill_system_prompt
|
|
148
|
+
|
|
149
|
+
skill_home = "./skills"
|
|
150
|
+
registry = ToolRegistry()
|
|
151
|
+
registry.include(skills_group(skill_home=skill_home, mode="hybrid"))
|
|
152
|
+
|
|
153
|
+
skill_registry = SkillRegistry.from_home(skill_home)
|
|
154
|
+
system_prompt = build_skill_system_prompt(skill_registry, mode="hybrid")
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
The skills group exposes:
|
|
158
|
+
|
|
159
|
+
- `skills.list()`
|
|
160
|
+
- `skills.read(name)`
|
|
161
|
+
- `skills.list_resources(name)`
|
|
162
|
+
- `skills.read_resource(name, path)`
|
|
163
|
+
- `skills.system_prompt(active_skill=None)`
|
|
164
|
+
|
|
165
|
+
Execution modes:
|
|
166
|
+
|
|
167
|
+
| Mode | Behavior |
|
|
168
|
+
| --- | --- |
|
|
169
|
+
| `tools_only` | Models use only `skills.*` tools to read instructions and resources. |
|
|
170
|
+
| `shell_only` | Prompt tells the model where `skill_home` is; host shell/file policy must handle access. |
|
|
171
|
+
| `hybrid` | Models use `skills.*` for reads and policy-gated shell for commands only when needed. |
|
|
172
|
+
|
|
173
|
+
Skill matching is intentionally optional and host-owned. The package defines
|
|
174
|
+
`PromptSkillEvaluator` as a protocol, but does not decide when a skill should be
|
|
175
|
+
used.
|
|
176
|
+
|
|
177
|
+
## Web Tools
|
|
178
|
+
|
|
179
|
+
The web group exposes:
|
|
180
|
+
|
|
181
|
+
- `web.search(query, num_results=10, search_type="all", relevance_threshold=0.5, included_sources=None, excluded_sources=None, country_code=None, response_length=None, category=None, start_date=None, end_date=None, max_price=None, fast_mode=False, url_only=False, source_biases=None, instructions=None)`
|
|
182
|
+
- `web.open(id=None, cursor=-1, loc=-1, num_lines=-1)`
|
|
183
|
+
- `web.find(pattern, cursor=-1, context_lines=3)`
|
|
184
|
+
|
|
185
|
+
Valyu is the default provider when no provider is supplied. Search uses Valyu
|
|
186
|
+
search, and direct URL opens use Valyu contents extraction.
|
|
187
|
+
Opened pages and find results include source ids, URLs, and one-based line
|
|
188
|
+
ranges so responses can be cited or re-opened later in the same tool session.
|
|
189
|
+
|
|
190
|
+
```python
|
|
191
|
+
from modis_tools import ToolRegistry
|
|
192
|
+
from modis_tools.builtins import web_group
|
|
193
|
+
|
|
194
|
+
registry = ToolRegistry()
|
|
195
|
+
registry.include(web_group(api_key="..."))
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Search options map to Valyu search controls. For example:
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
result = registry.execute_tool_call({
|
|
202
|
+
"id": "search_1",
|
|
203
|
+
"type": "function",
|
|
204
|
+
"function": {
|
|
205
|
+
"name": "web.search",
|
|
206
|
+
"arguments": """
|
|
207
|
+
{
|
|
208
|
+
"query": "recent multimodal retrieval papers",
|
|
209
|
+
"num_results": 5,
|
|
210
|
+
"relevance_threshold": 0.45,
|
|
211
|
+
"included_sources": ["valyu/valyu-arxiv"],
|
|
212
|
+
"response_length": "medium",
|
|
213
|
+
"start_date": "2026-04-29",
|
|
214
|
+
"end_date": "2026-05-06",
|
|
215
|
+
"country_code": "US"
|
|
216
|
+
}
|
|
217
|
+
"""
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Default search values are copied from the installed Valyu SDK where exposed:
|
|
223
|
+
|
|
224
|
+
| Option | Default | Notes |
|
|
225
|
+
| --- | --- | --- |
|
|
226
|
+
| `num_results` | `10` | Sent to Valyu as `max_num_results`. |
|
|
227
|
+
| `search_type` | `"all"` | Valyu scopes are `web`, `proprietary`, `all`, and `news`. |
|
|
228
|
+
| `relevance_threshold` | `0.5` | Set to `None` to omit the threshold parameter. |
|
|
229
|
+
| `included_sources` | `None` | Valyu source ids or arbitrary URLs. |
|
|
230
|
+
| `excluded_sources` | `None` | Valyu source ids or arbitrary URLs. |
|
|
231
|
+
| `country_code` | `None` | Optional ISO country code. |
|
|
232
|
+
| `response_length` | `None` | Supports `short`, `medium`, `large`, `max`, integer, or numeric string. |
|
|
233
|
+
| `category` | `None` | Provider-specific category. |
|
|
234
|
+
| `start_date` / `end_date` | `None` | Optional `YYYY-MM-DD` bounds. |
|
|
235
|
+
| `max_price` | `None` | Provider cost limit if used. |
|
|
236
|
+
| `fast_mode` | `False` | Sent explicitly. |
|
|
237
|
+
| `url_only` | `False` | Sent explicitly. |
|
|
238
|
+
| `source_biases` | `None` | Optional per-source integer biases. |
|
|
239
|
+
| `instructions` | `None` | Optional provider instructions. |
|
|
240
|
+
|
|
241
|
+
`is_tool_call` is not exposed to the model; the Valyu provider sends it as
|
|
242
|
+
`True`.
|
|
243
|
+
|
|
244
|
+
Valyu provider metadata that is not part of the normalized result shape, such as
|
|
245
|
+
scores or ranking fields when returned by the SDK, is preserved in each result's
|
|
246
|
+
`metadata` object.
|
|
247
|
+
|
|
248
|
+
Custom providers implement `SearchProvider`:
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
from modis_tools.providers.web import Page, SearchResult
|
|
252
|
+
|
|
253
|
+
class MyProvider:
|
|
254
|
+
def search(self, query: str, *, num_results: int = 10, **kwargs: object) -> list[SearchResult]:
|
|
255
|
+
return [SearchResult(title="Example", url="https://example.test", content="Page text")]
|
|
256
|
+
|
|
257
|
+
def fetch(self, url: str) -> Page:
|
|
258
|
+
return Page(title="Fetched", url=url, markdown="Fetched markdown")
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
`web.open` can open prior search results by index/result id/URL, or an arbitrary
|
|
262
|
+
direct URL. Browser-backed fetching is still kept behind the optional `browser`
|
|
263
|
+
extra for future extension.
|
|
264
|
+
|
|
265
|
+
## Live Web Tests
|
|
266
|
+
|
|
267
|
+
Live web tests are skipped unless `VALYU_API_KEY` is set and the `web` extra is
|
|
268
|
+
installed:
|
|
269
|
+
|
|
270
|
+
```bash
|
|
271
|
+
python -m pip install -e ".[dev,web]"
|
|
272
|
+
VALYU_API_KEY=... pytest -m live
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
The broader web quality eval is opt-in because it consumes live provider quota:
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
VALYU_API_KEY=... MODIS_TOOLS_RUN_WEB_EVAL=1 pytest -m "live and eval" -vv
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
See `docs/WEB_EVAL.md` for the current query set and comparison checklist.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
modis_tools/__init__.py,sha256=Ye_fTAwo4SkDe2fcWXxBrAfLfgMH6wx2EW50ibHOJko,1512
|
|
2
|
+
modis_tools/_version.py,sha256=JeE2NWwTR2llH1GLrPqAzny93cCPCuOdnjwEs4d_sHA,55
|
|
3
|
+
modis_tools/conversion.py,sha256=YwowtmyRrHtDsGdOk-4MJKES0I_hCqsP9FuqcZoWIvw,5789
|
|
4
|
+
modis_tools/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
5
|
+
modis_tools/registry.py,sha256=np8DvLrreIsrzu1Jp3oO5QNr8uaNfCurECDrOmV-EHM,5577
|
|
6
|
+
modis_tools/results.py,sha256=E95RVzYvLMVIOocV3bMDTViJlz0yF41ss2T9PoPWxDw,1150
|
|
7
|
+
modis_tools/schemas.py,sha256=UPBsUp_fOT-KzWgfhOI_Vfmd0a_Mt_u6U4-SoAkcTT0,3363
|
|
8
|
+
modis_tools/serialization.py,sha256=AzlP1tbVLrLyckUdgQIvNr7ODCbhuEWK5PwMMrdynBU,1888
|
|
9
|
+
modis_tools/builtins/__init__.py,sha256=DsVV9LOcCV1J_zGekpnDBBOQ-CoGZmFfFiMiIixo7Ck,607
|
|
10
|
+
modis_tools/builtins/python.py,sha256=z8vb1b3yfubVhxYxjLCd-Dz7DsvlJtj5ZLq0nIPCHj8,3786
|
|
11
|
+
modis_tools/builtins/shell.py,sha256=Lks6PLRMTZAHKqYZJBvozO-K3f5FQNmW8tNDidGGwJc,7611
|
|
12
|
+
modis_tools/builtins/shell_policy.py,sha256=6tG6b6JoBFx3pB4XghZLstdTGwWYK1gZBuEQ05BrbG4,9060
|
|
13
|
+
modis_tools/builtins/web.py,sha256=v_bxA5dkobFddakd9CkdhLmcu0wcMd2OiKAK5vDTEGg,10638
|
|
14
|
+
modis_tools/providers/__init__.py,sha256=VtLp_ZUsjA8u18Tk3Gz8GjGdBVD-PLMIPF3Xi8uVFnk,296
|
|
15
|
+
modis_tools/providers/valyu.py,sha256=TJeekS6jRRxWhFoRhJYsgon4I774oaXRU3XBwGme1rg,9291
|
|
16
|
+
modis_tools/providers/web.py,sha256=yvhKKwlRmrDulJ9aS_ogXehjWuhjVvu7l3KU-32NOqY,5833
|
|
17
|
+
modis_tools/skills/__init__.py,sha256=sBv8-HLoGsfh9GhT-vybrOi2nZA5J0wI-X-gw4xiyC0,660
|
|
18
|
+
modis_tools/skills/models.py,sha256=dvsyrojQmElWiLAk_Lz-JcC6OrISTExonzIztX9cI60,1320
|
|
19
|
+
modis_tools/skills/prompt.py,sha256=dYk_3tYM-dBp4RNUAq6MTQ698lUzMXW5EWjMzt87eN0,1980
|
|
20
|
+
modis_tools/skills/registry.py,sha256=TLa-uAEMIpXeoGLjvTIDcbAfBuF10ACPbl15UlLSKOY,5807
|
|
21
|
+
modis_tools/skills/tools.py,sha256=Ryq4HHYwQS9UF8tRQAp5rucvdbNOENI-IWuXxOKF_cU,3861
|
|
22
|
+
modis_py_tools-0.1.0.dist-info/METADATA,sha256=IFJLwMvNE41HM-9RfoEcsLFhFMpZ2XyiDZFyL1e_fts,9526
|
|
23
|
+
modis_py_tools-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
24
|
+
modis_py_tools-0.1.0.dist-info/top_level.txt,sha256=Ledtj3ITdO0GXQpLc1HE7Un8N-XVo3sPskg8bxAjzfw,12
|
|
25
|
+
modis_py_tools-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
modis_tools
|
modis_tools/__init__.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""Public API for MoDIS-compatible Python tools."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from ._version import __version__
|
|
6
|
+
from .conversion import function_to_tool
|
|
7
|
+
from .registry import ToolExecutor, ToolGroup, ToolRegistry
|
|
8
|
+
from .results import failed_result, successful_result
|
|
9
|
+
from .schemas import (
|
|
10
|
+
ProcessResult,
|
|
11
|
+
PythonResult,
|
|
12
|
+
ShellResult,
|
|
13
|
+
ToolCall,
|
|
14
|
+
ToolCallFunction,
|
|
15
|
+
ToolDefinition,
|
|
16
|
+
ToolError,
|
|
17
|
+
ToolFunctionDefinition,
|
|
18
|
+
ToolResult,
|
|
19
|
+
)
|
|
20
|
+
from .serialization import parse_arguments, to_content_string, to_json_compatible, truncate_text
|
|
21
|
+
from .skills import (
|
|
22
|
+
PromptSkillEvaluator,
|
|
23
|
+
SkillExecutionMode,
|
|
24
|
+
SkillMetadata,
|
|
25
|
+
SkillNotFoundError,
|
|
26
|
+
SkillRegistry,
|
|
27
|
+
SkillResource,
|
|
28
|
+
SkillResourceError,
|
|
29
|
+
SkillRuntimeConfig,
|
|
30
|
+
build_skill_system_prompt,
|
|
31
|
+
skills_group,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"__version__",
|
|
36
|
+
"PromptSkillEvaluator",
|
|
37
|
+
"ProcessResult",
|
|
38
|
+
"PythonResult",
|
|
39
|
+
"ShellResult",
|
|
40
|
+
"SkillExecutionMode",
|
|
41
|
+
"SkillMetadata",
|
|
42
|
+
"SkillNotFoundError",
|
|
43
|
+
"SkillRegistry",
|
|
44
|
+
"SkillResource",
|
|
45
|
+
"SkillResourceError",
|
|
46
|
+
"SkillRuntimeConfig",
|
|
47
|
+
"ToolCall",
|
|
48
|
+
"ToolCallFunction",
|
|
49
|
+
"ToolDefinition",
|
|
50
|
+
"ToolError",
|
|
51
|
+
"ToolExecutor",
|
|
52
|
+
"ToolFunctionDefinition",
|
|
53
|
+
"ToolGroup",
|
|
54
|
+
"ToolRegistry",
|
|
55
|
+
"ToolResult",
|
|
56
|
+
"build_skill_system_prompt",
|
|
57
|
+
"failed_result",
|
|
58
|
+
"function_to_tool",
|
|
59
|
+
"parse_arguments",
|
|
60
|
+
"skills_group",
|
|
61
|
+
"successful_result",
|
|
62
|
+
"to_content_string",
|
|
63
|
+
"to_json_compatible",
|
|
64
|
+
"truncate_text",
|
|
65
|
+
]
|
modis_tools/_version.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Built-in MoDIS tool groups."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from ..skills import skills_group
|
|
6
|
+
from .python import python_group
|
|
7
|
+
from .python import run as run_python
|
|
8
|
+
from .shell import run as run_shell
|
|
9
|
+
from .shell import shell_group
|
|
10
|
+
from .shell_policy import ShellDecision, ShellPolicy, ShellPolicyDeniedError, ShellRequest
|
|
11
|
+
from .web import WebBrowser, web_group
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"ShellDecision",
|
|
15
|
+
"ShellPolicy",
|
|
16
|
+
"ShellPolicyDeniedError",
|
|
17
|
+
"ShellRequest",
|
|
18
|
+
"WebBrowser",
|
|
19
|
+
"python_group",
|
|
20
|
+
"run_python",
|
|
21
|
+
"run_shell",
|
|
22
|
+
"shell_group",
|
|
23
|
+
"skills_group",
|
|
24
|
+
"web_group",
|
|
25
|
+
]
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Python interpreter execution tool."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import subprocess
|
|
7
|
+
import sys
|
|
8
|
+
import time
|
|
9
|
+
|
|
10
|
+
from ..conversion import function_to_tool
|
|
11
|
+
from ..registry import ToolExecutor, ToolGroup
|
|
12
|
+
from ..schemas import PythonResult
|
|
13
|
+
from ..serialization import truncate_text
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def run(
|
|
17
|
+
code: str,
|
|
18
|
+
python_executable: str | None = None,
|
|
19
|
+
workdir: str | None = None,
|
|
20
|
+
timeout_seconds: float = 30.0,
|
|
21
|
+
env: dict[str, str] | None = None,
|
|
22
|
+
max_output_chars: int = 20_000,
|
|
23
|
+
) -> PythonResult:
|
|
24
|
+
"""Execute Python code in a subprocess and capture its process result.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
code: Python source code passed to the interpreter with `-c`.
|
|
28
|
+
python_executable: Optional interpreter path. Defaults to the current interpreter.
|
|
29
|
+
workdir: Optional working directory for the interpreter process.
|
|
30
|
+
timeout_seconds: Maximum execution time in seconds.
|
|
31
|
+
env: Optional environment variable overrides.
|
|
32
|
+
max_output_chars: Maximum characters retained for stdout and stderr.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
Captured stdout, stderr, exit code, timeout state, duration, and truncation metadata.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
if timeout_seconds <= 0:
|
|
39
|
+
raise ValueError("timeout_seconds must be greater than 0.")
|
|
40
|
+
|
|
41
|
+
executable = python_executable or sys.executable
|
|
42
|
+
start = time.monotonic()
|
|
43
|
+
merged_env = _merged_env(env)
|
|
44
|
+
try:
|
|
45
|
+
completed = subprocess.run(
|
|
46
|
+
[executable, "-c", code],
|
|
47
|
+
cwd=workdir,
|
|
48
|
+
env=merged_env,
|
|
49
|
+
timeout=timeout_seconds,
|
|
50
|
+
capture_output=True,
|
|
51
|
+
text=True,
|
|
52
|
+
check=False,
|
|
53
|
+
)
|
|
54
|
+
duration_seconds = time.monotonic() - start
|
|
55
|
+
stdout, stdout_truncated = truncate_text(completed.stdout or "", max_output_chars)
|
|
56
|
+
stderr, stderr_truncated = truncate_text(completed.stderr or "", max_output_chars)
|
|
57
|
+
return PythonResult(
|
|
58
|
+
command=executable,
|
|
59
|
+
code=code,
|
|
60
|
+
stdout=stdout,
|
|
61
|
+
stderr=stderr,
|
|
62
|
+
exitCode=completed.returncode,
|
|
63
|
+
durationSeconds=duration_seconds,
|
|
64
|
+
timedOut=False,
|
|
65
|
+
truncated=stdout_truncated or stderr_truncated,
|
|
66
|
+
workdir=workdir,
|
|
67
|
+
)
|
|
68
|
+
except subprocess.TimeoutExpired as exc:
|
|
69
|
+
duration_seconds = time.monotonic() - start
|
|
70
|
+
stdout, stdout_truncated = truncate_text(_coerce_output(exc.stdout), max_output_chars)
|
|
71
|
+
stderr, stderr_truncated = truncate_text(_coerce_output(exc.stderr), max_output_chars)
|
|
72
|
+
return PythonResult(
|
|
73
|
+
command=executable,
|
|
74
|
+
code=code,
|
|
75
|
+
stdout=stdout,
|
|
76
|
+
stderr=stderr,
|
|
77
|
+
exitCode=-1,
|
|
78
|
+
durationSeconds=duration_seconds,
|
|
79
|
+
timedOut=True,
|
|
80
|
+
truncated=stdout_truncated or stderr_truncated,
|
|
81
|
+
workdir=workdir,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def python_group(**defaults: object) -> ToolGroup:
|
|
86
|
+
"""Return the built-in Python interpreter tool group.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
**defaults: Default arguments applied to every `python.run` invocation.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
A tool group containing `python.run`.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
return ToolGroup(
|
|
96
|
+
name="python",
|
|
97
|
+
executors=(
|
|
98
|
+
ToolExecutor(
|
|
99
|
+
name="python.run",
|
|
100
|
+
exec=run,
|
|
101
|
+
definition=function_to_tool(run, name="python.run"),
|
|
102
|
+
defaults=defaults,
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _merged_env(env: dict[str, str] | None) -> dict[str, str] | None:
|
|
109
|
+
if env is None:
|
|
110
|
+
return None
|
|
111
|
+
return {**os.environ, **{str(key): str(value) for key, value in env.items()}}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _coerce_output(value: str | bytes | None) -> str:
|
|
115
|
+
if value is None:
|
|
116
|
+
return ""
|
|
117
|
+
if isinstance(value, bytes):
|
|
118
|
+
return value.decode(errors="replace")
|
|
119
|
+
return value
|