dsh-awesome-optimizer 1.8.0 → 2.0.0
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.
- package/README.md +16 -15
- package/README.zh.md +16 -15
- package/package.json +6 -2
- package/scripts/benchmark.mjs +53 -2
- package/src/custom-prompt.js +1 -0
- package/src/index.d.ts +14 -0
- package/src/index.js +15 -1
- package/src/persistent-eval/daemon-pool.js +91 -0
- package/src/persistent-eval/index.d.ts +54 -0
- package/src/persistent-eval/index.js +161 -0
- package/src/persistent-eval/stdio-runner.js +270 -0
- package/src/persistent-eval/workers/node_worker.mjs +238 -0
- package/src/persistent-eval/workers/py_worker.py +182 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
py_worker.py - Zero-dependency persistent Python worker for DSH Agent.
|
|
5
|
+
Maintains a stateful global scope across execution frames over stdio line-delimited JSON.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import sys
|
|
9
|
+
import io
|
|
10
|
+
import os
|
|
11
|
+
import ast
|
|
12
|
+
import json
|
|
13
|
+
import time
|
|
14
|
+
import traceback
|
|
15
|
+
|
|
16
|
+
# Ensure strict UTF-8 stdio streams with line buffering
|
|
17
|
+
if hasattr(sys.stdin, "reconfigure"):
|
|
18
|
+
sys.stdin.reconfigure(encoding="utf-8", errors="replace")
|
|
19
|
+
if hasattr(sys.stdout, "reconfigure"):
|
|
20
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
21
|
+
if hasattr(sys.stderr, "reconfigure"):
|
|
22
|
+
sys.stderr.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
23
|
+
|
|
24
|
+
# Keep handles to original standard streams for JSON protocol frames
|
|
25
|
+
REAL_STDIN = sys.__stdin__
|
|
26
|
+
REAL_STDOUT = sys.__stdout__
|
|
27
|
+
|
|
28
|
+
def create_fresh_scope():
|
|
29
|
+
return {
|
|
30
|
+
"__name__": "__main__",
|
|
31
|
+
"__doc__": "DSH Persistent Python Kernel",
|
|
32
|
+
"__builtins__": __builtins__,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
GLOBAL_SCOPE = create_fresh_scope()
|
|
36
|
+
|
|
37
|
+
def execute_code(code, scope):
|
|
38
|
+
"""
|
|
39
|
+
Execute python code.
|
|
40
|
+
If the last AST node is an expression, evaluate it to capture its return value.
|
|
41
|
+
Otherwise execute as statements.
|
|
42
|
+
"""
|
|
43
|
+
code_stripped = code.strip()
|
|
44
|
+
if not code_stripped:
|
|
45
|
+
return None
|
|
46
|
+
|
|
47
|
+
parsed = ast.parse(code, filename="<eval_kernel>", mode="exec")
|
|
48
|
+
if not parsed.body:
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
# Check if the last statement is an expression
|
|
52
|
+
last_stmt = parsed.body[-1]
|
|
53
|
+
if isinstance(last_stmt, ast.Expr):
|
|
54
|
+
# If there are preceding statements, execute them first
|
|
55
|
+
if len(parsed.body) > 1:
|
|
56
|
+
exec_mod = ast.Module(body=parsed.body[:-1], type_ignores=[])
|
|
57
|
+
exec(compile(exec_mod, filename="<eval_kernel>", mode="exec"), scope)
|
|
58
|
+
|
|
59
|
+
# Evaluate the trailing expression
|
|
60
|
+
eval_expr = ast.Expression(body=last_stmt.value)
|
|
61
|
+
return eval(compile(eval_expr, filename="<eval_kernel>", mode="eval"), scope)
|
|
62
|
+
else:
|
|
63
|
+
# All statements, execute directly
|
|
64
|
+
exec(compile(parsed, filename="<eval_kernel>", mode="exec"), scope)
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
def serialize_result(result):
|
|
68
|
+
if result is None:
|
|
69
|
+
return None, None
|
|
70
|
+
result_repr = repr(result)
|
|
71
|
+
try:
|
|
72
|
+
# Check if directly JSON-serializable
|
|
73
|
+
json.dumps(result)
|
|
74
|
+
return result, result_repr
|
|
75
|
+
except (TypeError, OverflowError):
|
|
76
|
+
# Fall back to string representation
|
|
77
|
+
return result_repr, result_repr
|
|
78
|
+
|
|
79
|
+
def main():
|
|
80
|
+
global GLOBAL_SCOPE
|
|
81
|
+
|
|
82
|
+
# Signal ready frame on startup
|
|
83
|
+
ready_frame = json.dumps({"status": "ready", "pid": os.getpid()}, ensure_ascii=False) + "\n"
|
|
84
|
+
REAL_STDOUT.buffer.write(ready_frame.encode("utf-8"))
|
|
85
|
+
REAL_STDOUT.buffer.flush()
|
|
86
|
+
|
|
87
|
+
for line in REAL_STDIN:
|
|
88
|
+
line_str = line.strip()
|
|
89
|
+
if not line_str:
|
|
90
|
+
continue
|
|
91
|
+
|
|
92
|
+
try:
|
|
93
|
+
req = json.loads(line_str)
|
|
94
|
+
except Exception as e:
|
|
95
|
+
err_frame = json.dumps({
|
|
96
|
+
"id": None,
|
|
97
|
+
"success": False,
|
|
98
|
+
"error": f"Invalid JSON payload: {str(e)}",
|
|
99
|
+
"stdout": "",
|
|
100
|
+
"stderr": "",
|
|
101
|
+
"executionTimeMs": 0
|
|
102
|
+
}, ensure_ascii=False) + "\n"
|
|
103
|
+
REAL_STDOUT.buffer.write(err_frame.encode("utf-8"))
|
|
104
|
+
REAL_STDOUT.buffer.flush()
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
req_id = req.get("id")
|
|
108
|
+
action = req.get("action", "eval")
|
|
109
|
+
code = req.get("code", "")
|
|
110
|
+
should_reset = bool(req.get("reset", False))
|
|
111
|
+
|
|
112
|
+
if action == "ping":
|
|
113
|
+
resp = {
|
|
114
|
+
"id": req_id,
|
|
115
|
+
"success": True,
|
|
116
|
+
"status": "pong",
|
|
117
|
+
"pid": os.getpid()
|
|
118
|
+
}
|
|
119
|
+
frame = json.dumps(resp, ensure_ascii=False) + "\n"
|
|
120
|
+
REAL_STDOUT.buffer.write(frame.encode("utf-8"))
|
|
121
|
+
REAL_STDOUT.buffer.flush()
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
if action == "reset" or should_reset:
|
|
125
|
+
GLOBAL_SCOPE = create_fresh_scope()
|
|
126
|
+
if action == "reset":
|
|
127
|
+
resp = {
|
|
128
|
+
"id": req_id,
|
|
129
|
+
"success": True,
|
|
130
|
+
"stdout": "",
|
|
131
|
+
"stderr": "",
|
|
132
|
+
"result": "Kernel namespace reset successfully.",
|
|
133
|
+
"executionTimeMs": 0
|
|
134
|
+
}
|
|
135
|
+
frame = json.dumps(resp, ensure_ascii=False) + "\n"
|
|
136
|
+
REAL_STDOUT.buffer.write(frame.encode("utf-8"))
|
|
137
|
+
REAL_STDOUT.buffer.flush()
|
|
138
|
+
continue
|
|
139
|
+
|
|
140
|
+
captured_stdout = io.StringIO()
|
|
141
|
+
captured_stderr = io.StringIO()
|
|
142
|
+
old_stdout = sys.stdout
|
|
143
|
+
old_stderr = sys.stderr
|
|
144
|
+
sys.stdout = captured_stdout
|
|
145
|
+
sys.stderr = captured_stderr
|
|
146
|
+
|
|
147
|
+
start_time = time.perf_counter()
|
|
148
|
+
success = True
|
|
149
|
+
error_msg = None
|
|
150
|
+
result_val = None
|
|
151
|
+
result_repr = None
|
|
152
|
+
|
|
153
|
+
try:
|
|
154
|
+
raw_result = execute_code(code, GLOBAL_SCOPE)
|
|
155
|
+
result_val, result_repr = serialize_result(raw_result)
|
|
156
|
+
except Exception as ex:
|
|
157
|
+
success = False
|
|
158
|
+
error_msg = str(ex)
|
|
159
|
+
# Write traceback into captured_stderr
|
|
160
|
+
traceback.print_exc(file=captured_stderr)
|
|
161
|
+
finally:
|
|
162
|
+
elapsed_ms = round((time.perf_counter() - start_time) * 1000, 3)
|
|
163
|
+
sys.stdout = old_stdout
|
|
164
|
+
sys.stderr = old_stderr
|
|
165
|
+
|
|
166
|
+
resp = {
|
|
167
|
+
"id": req_id,
|
|
168
|
+
"success": success,
|
|
169
|
+
"stdout": captured_stdout.getvalue(),
|
|
170
|
+
"stderr": captured_stderr.getvalue(),
|
|
171
|
+
"result": result_val,
|
|
172
|
+
"resultRepr": result_repr,
|
|
173
|
+
"error": error_msg,
|
|
174
|
+
"executionTimeMs": elapsed_ms
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
frame = json.dumps(resp, ensure_ascii=False) + "\n"
|
|
178
|
+
REAL_STDOUT.buffer.write(frame.encode("utf-8"))
|
|
179
|
+
REAL_STDOUT.buffer.flush()
|
|
180
|
+
|
|
181
|
+
if __name__ == "__main__":
|
|
182
|
+
main()
|