txcode-sdk 0.8.0__py3-none-any.whl → 0.9.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.
- txcode_sdk/tools/bash.py +74 -31
- txcode_sdk/tools/grep.py +18 -4
- {txcode_sdk-0.8.0.dist-info → txcode_sdk-0.9.0.dist-info}/METADATA +1 -1
- {txcode_sdk-0.8.0.dist-info → txcode_sdk-0.9.0.dist-info}/RECORD +8 -8
- {txcode_sdk-0.8.0.dist-info → txcode_sdk-0.9.0.dist-info}/WHEEL +0 -0
- {txcode_sdk-0.8.0.dist-info → txcode_sdk-0.9.0.dist-info}/entry_points.txt +0 -0
- {txcode_sdk-0.8.0.dist-info → txcode_sdk-0.9.0.dist-info}/licenses/LICENSE +0 -0
- {txcode_sdk-0.8.0.dist-info → txcode_sdk-0.9.0.dist-info}/top_level.txt +0 -0
txcode_sdk/tools/bash.py
CHANGED
|
@@ -10,6 +10,7 @@ import asyncio
|
|
|
10
10
|
import os
|
|
11
11
|
import signal
|
|
12
12
|
import sys
|
|
13
|
+
import time
|
|
13
14
|
|
|
14
15
|
from ..types import Tool, ToolContext, ToolResult
|
|
15
16
|
from .._compat.resources import get_resource
|
|
@@ -101,6 +102,36 @@ def _truncate_output(output: str) -> str:
|
|
|
101
102
|
return output
|
|
102
103
|
|
|
103
104
|
|
|
105
|
+
def _exceeds_limits(output: str) -> bool:
|
|
106
|
+
if len(output.encode("utf-8", errors="replace")) > MAX_BYTES:
|
|
107
|
+
return True
|
|
108
|
+
if len(output.split("\n")) > MAX_LINES:
|
|
109
|
+
return True
|
|
110
|
+
return False
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
async def _read_stream(stream, limit, kill_cb):
|
|
114
|
+
"""逐 chunk 读取子进程输出流,累计超过 limit 后截断并触发 kill_cb(对齐 TS 流式截断)。"""
|
|
115
|
+
chunks = []
|
|
116
|
+
total = 0
|
|
117
|
+
exceeded = False
|
|
118
|
+
try:
|
|
119
|
+
while True:
|
|
120
|
+
chunk = await stream.read(8192)
|
|
121
|
+
if not chunk:
|
|
122
|
+
break
|
|
123
|
+
if total + len(chunk) > limit:
|
|
124
|
+
chunks.append(chunk[: limit - total])
|
|
125
|
+
exceeded = True
|
|
126
|
+
await kill_cb()
|
|
127
|
+
break
|
|
128
|
+
chunks.append(chunk)
|
|
129
|
+
total += len(chunk)
|
|
130
|
+
except Exception:
|
|
131
|
+
pass
|
|
132
|
+
return b"".join(chunks), exceeded
|
|
133
|
+
|
|
134
|
+
|
|
104
135
|
async def _bash(params: dict, ctx: ToolContext) -> ToolResult:
|
|
105
136
|
command = params.get("command", "")
|
|
106
137
|
if not command:
|
|
@@ -124,38 +155,40 @@ async def _bash(params: dict, ctx: ToolContext) -> ToolResult:
|
|
|
124
155
|
except Exception as e:
|
|
125
156
|
return ToolResult(success=False, output="", error=str(e))
|
|
126
157
|
|
|
127
|
-
|
|
128
|
-
|
|
158
|
+
async def kill_cb():
|
|
159
|
+
await _kill_tree(proc)
|
|
160
|
+
|
|
161
|
+
read_out = asyncio.ensure_future(_read_stream(proc.stdout, MAX_BYTES, kill_cb))
|
|
162
|
+
read_err = asyncio.ensure_future(_read_stream(proc.stderr, MAX_BYTES, kill_cb))
|
|
163
|
+
wait_exit = asyncio.ensure_future(proc.wait())
|
|
164
|
+
|
|
129
165
|
abort_task = None
|
|
130
166
|
if ctx.abort_signal is not None:
|
|
131
167
|
abort_task = asyncio.ensure_future(_wait_abort(ctx.abort_signal))
|
|
132
|
-
tasks.append(abort_task)
|
|
133
168
|
|
|
134
169
|
timed_out = False
|
|
135
170
|
aborted = False
|
|
136
171
|
try:
|
|
172
|
+
waiters = [wait_exit]
|
|
137
173
|
if abort_task is not None:
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
try:
|
|
147
|
-
await asyncio.wait_for(comm, timeout=timeout / 1000.0)
|
|
148
|
-
except asyncio.TimeoutError:
|
|
149
|
-
timed_out = True
|
|
174
|
+
waiters.append(abort_task)
|
|
175
|
+
done, _pending = await asyncio.wait(
|
|
176
|
+
waiters, timeout=timeout / 1000.0, return_when=asyncio.FIRST_COMPLETED
|
|
177
|
+
)
|
|
178
|
+
if abort_task is not None and abort_task in done:
|
|
179
|
+
aborted = True
|
|
180
|
+
elif wait_exit not in done:
|
|
181
|
+
timed_out = True
|
|
150
182
|
except Exception:
|
|
151
183
|
aborted = True
|
|
152
184
|
|
|
153
185
|
if timed_out or aborted:
|
|
154
186
|
await _kill_tree(proc)
|
|
187
|
+
for t in (read_out, read_err):
|
|
188
|
+
if not t.done():
|
|
189
|
+
t.cancel()
|
|
155
190
|
try:
|
|
156
|
-
await asyncio.
|
|
157
|
-
except asyncio.CancelledError:
|
|
158
|
-
pass
|
|
191
|
+
await asyncio.wait([read_out, read_err, wait_exit], timeout=5)
|
|
159
192
|
except Exception:
|
|
160
193
|
pass
|
|
161
194
|
if timed_out:
|
|
@@ -171,27 +204,37 @@ async def _bash(params: dict, ctx: ToolContext) -> ToolResult:
|
|
|
171
204
|
)
|
|
172
205
|
|
|
173
206
|
try:
|
|
174
|
-
|
|
207
|
+
await asyncio.wait([read_out, read_err], timeout=10)
|
|
208
|
+
except Exception:
|
|
209
|
+
pass
|
|
210
|
+
try:
|
|
211
|
+
stdout_raw, out_exceeded = read_out.result()
|
|
212
|
+
except Exception:
|
|
213
|
+
stdout_raw, out_exceeded = b"", False
|
|
214
|
+
try:
|
|
215
|
+
stderr_raw, err_exceeded = read_err.result()
|
|
175
216
|
except Exception:
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
217
|
+
stderr_raw, err_exceeded = b"", False
|
|
218
|
+
|
|
219
|
+
out = stdout_raw.decode("utf-8", errors="replace")
|
|
220
|
+
err = stderr_raw.decode("utf-8", errors="replace")
|
|
221
|
+
raw = out
|
|
180
222
|
if err:
|
|
181
|
-
|
|
182
|
-
|
|
223
|
+
raw += ("\n" if raw else "") + err
|
|
224
|
+
truncated = out_exceeded or err_exceeded or _exceeds_limits(raw)
|
|
225
|
+
output = _truncate_output(raw)
|
|
226
|
+
if (out_exceeded or err_exceeded) and "[output truncated" not in output:
|
|
227
|
+
output += "\n\n[output truncated - exceeded {} bytes]".format(MAX_BYTES)
|
|
183
228
|
if not output:
|
|
184
229
|
output = "命令执行成功(无输出)"
|
|
185
230
|
return ToolResult(
|
|
186
|
-
success=proc.returncode == 0,
|
|
231
|
+
success=(proc.returncode == 0) or out_exceeded or err_exceeded,
|
|
187
232
|
output=output,
|
|
188
233
|
metadata={
|
|
189
234
|
"exitCode": proc.returncode,
|
|
190
|
-
"
|
|
191
|
-
"
|
|
192
|
-
"
|
|
193
|
-
"workdir": workdir,
|
|
194
|
-
"shell": shell,
|
|
235
|
+
"truncated": truncated,
|
|
236
|
+
"stdoutBytes": len(stdout_raw),
|
|
237
|
+
"stderrBytes": len(stderr_raw),
|
|
195
238
|
},
|
|
196
239
|
)
|
|
197
240
|
|
txcode_sdk/tools/grep.py
CHANGED
|
@@ -7,6 +7,10 @@ from typing import List, Tuple
|
|
|
7
7
|
from ..types import Tool, ToolContext, ToolResult
|
|
8
8
|
from .._compat.resources import get_resource
|
|
9
9
|
|
|
10
|
+
MAX_RESULTS = 100
|
|
11
|
+
MAX_LINE_LENGTH = 200
|
|
12
|
+
IGNORE_DIRS = {"node_modules", ".git", "dist", "build", ".next", "coverage", ".cache"}
|
|
13
|
+
|
|
10
14
|
|
|
11
15
|
def _grep_tool_desc() -> str:
|
|
12
16
|
return get_resource("txcode_sdk.tools", "grep.txt")
|
|
@@ -48,7 +52,7 @@ def _iter_files(base: Path, include: List[str], exclude: List[str], recursive: b
|
|
|
48
52
|
continue
|
|
49
53
|
for e in entries:
|
|
50
54
|
if e.is_dir():
|
|
51
|
-
if recursive:
|
|
55
|
+
if recursive and e.name not in IGNORE_DIRS and not e.name.startswith("."):
|
|
52
56
|
stack.append(e)
|
|
53
57
|
continue
|
|
54
58
|
name = e.name
|
|
@@ -88,6 +92,7 @@ async def _grep(params: dict, ctx: ToolContext) -> ToolResult:
|
|
|
88
92
|
exclude_list = [exclude] if isinstance(exclude, str) else (exclude or [])
|
|
89
93
|
|
|
90
94
|
matches: List[Tuple[str, int, str]] = []
|
|
95
|
+
truncated = False
|
|
91
96
|
for fp in _iter_files(base, include_list, exclude_list, recursive):
|
|
92
97
|
if _is_binary(fp):
|
|
93
98
|
continue
|
|
@@ -98,19 +103,28 @@ async def _grep(params: dict, ctx: ToolContext) -> ToolResult:
|
|
|
98
103
|
continue
|
|
99
104
|
for i, line in enumerate(lines):
|
|
100
105
|
if rx.search(line):
|
|
101
|
-
matches.append((str(fp), i + 1, line))
|
|
106
|
+
matches.append((str(fp), i + 1, line[:MAX_LINE_LENGTH]))
|
|
107
|
+
if len(matches) >= MAX_RESULTS:
|
|
108
|
+
truncated = True
|
|
109
|
+
break
|
|
110
|
+
if truncated:
|
|
111
|
+
break
|
|
102
112
|
|
|
103
113
|
if not matches:
|
|
104
114
|
return ToolResult(
|
|
105
115
|
success=True,
|
|
106
116
|
output="No matches found for pattern: {}".format(pattern),
|
|
107
|
-
metadata={"matches": 0, "pattern": pattern},
|
|
117
|
+
metadata={"matches": 0, "truncated": False, "pattern": pattern},
|
|
108
118
|
)
|
|
109
119
|
|
|
110
120
|
output = "Found {} matches:\n".format(len(matches))
|
|
111
121
|
output += "\n".join("{}:{}:{}".format(fp, ln, line) for fp, ln, line in matches)
|
|
122
|
+
if truncated:
|
|
123
|
+
output += "\n[output truncated - showing first {} matches]".format(MAX_RESULTS)
|
|
112
124
|
return ToolResult(
|
|
113
|
-
success=True,
|
|
125
|
+
success=True,
|
|
126
|
+
output=output,
|
|
127
|
+
metadata={"matches": len(matches), "truncated": truncated, "pattern": pattern},
|
|
114
128
|
)
|
|
115
129
|
|
|
116
130
|
|
|
@@ -117,7 +117,7 @@ txcode_sdk/skills/__init__.py,sha256=9_ixitGOQM8kM-bqaG6un5jdAQ61cPXZrWil3Jngp2I
|
|
|
117
117
|
txcode_sdk/specs/__init__.py,sha256=xOxygnfWXr6zwLoY1IUcbC41T-E-aKFAxeVoo6ZDEt4,5372
|
|
118
118
|
txcode_sdk/tools/__init__.py,sha256=82TvX8jTezY1Kv9v3wABI3abFoKGg-IkZrSJ-egZaR8,99
|
|
119
119
|
txcode_sdk/tools/_util.py,sha256=ciu7pFXpr23zsZcrq5Q2ihkSS5rfBC-pUnLk4RcWFfU,1335
|
|
120
|
-
txcode_sdk/tools/bash.py,sha256
|
|
120
|
+
txcode_sdk/tools/bash.py,sha256=s6FqcSz8xo7n1wb_dAeTV7F0HKUcedfICpVT_uGhHG0,8049
|
|
121
121
|
txcode_sdk/tools/bash.txt,sha256=glK3S5hFIKLNmCHQaxpRP6_Ll_1pn6xpajSYCg8DtQ4,2193
|
|
122
122
|
txcode_sdk/tools/bash_tool.py,sha256=EMVILYNezew0r202hG46bAXMJfZjUr_-_2Vw3iJfYdY,163
|
|
123
123
|
txcode_sdk/tools/code_search.py,sha256=zyN-r_rmGybyFlquDxrGngK9mfBUoqEUav_9536CVRo,2585
|
|
@@ -129,7 +129,7 @@ txcode_sdk/tools/email_tool.txt,sha256=ohgH18Sm6TyavlxLEhaRcJD5yfiYY_IsXKd90VlB2
|
|
|
129
129
|
txcode_sdk/tools/glob.py,sha256=D6E64nkO1FG9xbKoy9HZDG8yjZwGVgRrMGe3nLv2-aE,3305
|
|
130
130
|
txcode_sdk/tools/glob.txt,sha256=XQqJwf72S3CYaVs_T9zoWiZeCU46YpL0LHiZfX4JHRg,492
|
|
131
131
|
txcode_sdk/tools/glob_tool.py,sha256=RFu0rGXAW9vtyO5Jlc2US8QSN9hp_4lcysqJ44sD5DU,181
|
|
132
|
-
txcode_sdk/tools/grep.py,sha256
|
|
132
|
+
txcode_sdk/tools/grep.py,sha256=-bG-nO0ghx9izpfNOUa24yZGwDeq88HPPLJ2Liw9OHs,4963
|
|
133
133
|
txcode_sdk/tools/grep.txt,sha256=c7PJ79iXb45XE8Z-oJjMnJ77TbHB8o7tyzK4Ye88w0M,631
|
|
134
134
|
txcode_sdk/tools/grep_tool.py,sha256=tBFEQzD13nzM20Va200LU3QEonP-tLEMNvflKxWQ0Q4,181
|
|
135
135
|
txcode_sdk/tools/memory.py,sha256=zDuXWLjp1SIHxiID4OMht4-4Hb8GkVEgl6A8kx9pg0g,5058
|
|
@@ -151,9 +151,9 @@ txcode_sdk/tools/web_shell_exec.py,sha256=QTnSlDLWPvldpResrYP1RAX2SYkHIkmLDuTvsO
|
|
|
151
151
|
txcode_sdk/tools/web_shell_exec.txt,sha256=6KVGwgQDp9HnbybcsIpxqPmtr5H6X0iPRARdzTahvYo,655
|
|
152
152
|
txcode_sdk/tools/write_file.py,sha256=JqH7gbD0WixsW2eCwhYJ9eEXr74y1caa8_XLPAYDf_A,2020
|
|
153
153
|
txcode_sdk/tools/write_file.txt,sha256=b-PJMcpUf31U_eGCCgIPO7hjilPtcHJYNVR3DSbghd0,562
|
|
154
|
-
txcode_sdk-0.
|
|
155
|
-
txcode_sdk-0.
|
|
156
|
-
txcode_sdk-0.
|
|
157
|
-
txcode_sdk-0.
|
|
158
|
-
txcode_sdk-0.
|
|
159
|
-
txcode_sdk-0.
|
|
154
|
+
txcode_sdk-0.9.0.dist-info/licenses/LICENSE,sha256=MgVwuuUcdnF9JsDYM5blhGjaQrIM1SYpka3glIF0fko,1070
|
|
155
|
+
txcode_sdk-0.9.0.dist-info/METADATA,sha256=yreR2sRfNHCgRC2r49q7yNYvqnVFGvlBOAoZM97003w,11536
|
|
156
|
+
txcode_sdk-0.9.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
157
|
+
txcode_sdk-0.9.0.dist-info/entry_points.txt,sha256=DOwXTKeX9yV_FSKIecinhnIbHcxtMcJFR-3Xu4duR9Q,63
|
|
158
|
+
txcode_sdk-0.9.0.dist-info/top_level.txt,sha256=rXVjeFngkf2CR9InAdTEc68OJ2RlJZINs99Ffw1WYqY,19
|
|
159
|
+
txcode_sdk-0.9.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|