-
Notifications
You must be signed in to change notification settings - Fork 0
Add tool_result pruning utility and split NL/T workflow into two passes #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #!/usr/bin/env python3 | ||
| import sys, json, re | ||
|
|
||
| def summarize(txt): | ||
| try: | ||
| obj = json.loads(txt) | ||
| except Exception: | ||
| return f"tool_result: {len(txt)} bytes" | ||
| data = obj.get("data", {}) or {} | ||
| msg = obj.get("message") or obj.get("status") or "" | ||
| # Common tool shapes | ||
| if "sha256" in str(data): | ||
| sha = (data.get("sha256") or "")[:8] + "…" if data.get("sha256") else "" | ||
| ln = data.get("lengthBytes") or data.get("length") or "" | ||
| return f"sha={sha} len={ln}".strip() | ||
| if "diagnostics" in data: | ||
| diags = data["diagnostics"] or [] | ||
| w = sum(d.get("severity","" ).lower()=="warning" for d in diags) | ||
| e = sum(d.get("severity","" ).lower() in ("error","fatal") for d in diags) | ||
| ok = "OK" if not e else "FAIL" | ||
| return f"validate: {ok} (warnings={w}, errors={e})" | ||
| if "matches" in data: | ||
| m = data["matches"] or [] | ||
| if m: | ||
| first = m[0] | ||
| return f"find_in_file: {len(m)} match(es) first@{first.get('line',0)}:{first.get('col',0)}" | ||
| return "find_in_file: 0 matches" | ||
| if "lines" in data: # console | ||
| lines = data["lines"] or [] | ||
| lvls = {"info":0,"warning":0,"error":0} | ||
| for L in lines: | ||
| lvls[L.get("level","" ).lower()] = lvls.get(L.get("level","" ).lower(),0)+1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Dictionary lookup pattern |
||
| return f"console: {len(lines)} lines (info={lvls.get('info',0)},warn={lvls.get('warning',0)},err={lvls.get('error',0)})" | ||
| # Fallback: short status | ||
| return (msg or "tool_result")[:80] | ||
|
|
||
| def prune_message(msg): | ||
| if "content" not in msg: return msg | ||
| newc=[] | ||
| for c in msg["content"]: | ||
| if c.get("type")=="tool_result" and c.get("content"): | ||
| out=[] | ||
| for chunk in c["content"]: | ||
| if chunk.get("type")=="text": | ||
| out.append({"type":"text","text":summarize(chunk.get("text","" ))}) | ||
| newc.append({"type":"tool_result","tool_use_id":c.get("tool_use_id"),"content":out}) | ||
| else: | ||
| newc.append(c) | ||
| msg["content"]=newc | ||
| return msg | ||
|
|
||
| def main(): | ||
| convo=json.load(sys.stdin) | ||
| if isinstance(convo, dict) and "messages" in convo: | ||
| convo["messages"]=[prune_message(m) for m in convo["messages"]] | ||
| elif isinstance(convo, list): | ||
| convo=[prune_message(m) for m in convo] | ||
| json.dump(convo, sys.stdout, ensure_ascii=False) | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import sys | ||
| import pathlib | ||
| import importlib.util | ||
| import types | ||
| import asyncio | ||
| import pytest | ||
|
|
||
| ROOT = pathlib.Path(__file__).resolve().parents[1] | ||
| SRC = ROOT / "UnityMcpBridge" / "UnityMcpServer~" / "src" | ||
| sys.path.insert(0, str(SRC)) | ||
|
|
||
| from tools.resource_tools import register_resource_tools # type: ignore | ||
|
|
||
| class DummyMCP: | ||
| def __init__(self): | ||
| self.tools = {} | ||
|
|
||
| def tool(self, *args, **kwargs): | ||
| def deco(fn): | ||
| self.tools[fn.__name__] = fn | ||
| return fn | ||
| return deco | ||
|
|
||
| @pytest.fixture() | ||
| def resource_tools(): | ||
| mcp = DummyMCP() | ||
| register_resource_tools(mcp) | ||
| return mcp.tools | ||
|
|
||
| def test_find_in_file_returns_positions(resource_tools, tmp_path): | ||
| proj = tmp_path | ||
| assets = proj / "Assets" | ||
| assets.mkdir() | ||
| f = assets / "A.txt" | ||
| f.write_text("hello world", encoding="utf-8") | ||
| find_in_file = resource_tools["find_in_file"] | ||
| loop = asyncio.new_event_loop() | ||
| try: | ||
| resp = loop.run_until_complete( | ||
| find_in_file(uri="unity://path/Assets/A.txt", pattern="world", ctx=None, project_root=str(proj)) | ||
| ) | ||
| finally: | ||
| loop.close() | ||
|
Comment on lines
+37
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider using |
||
| assert resp["success"] is True | ||
| assert resp["data"]["matches"] == [{"startLine": 1, "startCol": 7, "endLine": 1, "endCol": 12}] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Using
str(data)for SHA256 detection could match unrelated strings containing 'sha256'. Consider checkingdata.get('sha256')directly.