Repository navigation
Expand file tree
/
Copy pathcode_runner.js
More file actions
1276 lines (1273 loc) · 56.2 KB
/
Copy pathcode_runner.js
File metadata and controls
1276 lines (1273 loc) · 56.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* METADATA
{
name: code_runner
display_name: {
zh: "代码运行器"
en: "Code Runner"
}
description: { zh: "提供多语言代码执行能力,支持JavaScript、Python、Ruby、Go、Rust、C和C++脚本的运行。可直接执行代码字符串或运行外部文件,适用于快速测试、自动化脚本和教学演示。", en: "Multi-language code execution. Supports running JavaScript, Python, Ruby, Go, Rust, C and C++ scripts. You can execute code strings directly or run external files, useful for quick tests, automation, and demos." }
enabledByDefault: true
category: "Development"
// Multiple tools in this package
tools: [
{
name: run_javascript_es5
description: { zh: "运行自定义 JavaScript (ES5) 脚本。会捕获 console.log 的输出以及最终的返回值。", en: "Run custom JavaScript (ES5). Captures console.log output and the final return value." }
// This tool takes parameters
parameters: [
{
name: script
description: { zh: "要执行的 JavaScript 脚本内容", en: "JavaScript script content to execute." }
type: string
required: true
}
]
},
{
name: run_javascript_file
description: { zh: "运行 JavaScript (ES5) 文件。会捕获 console.log 的输出以及最终的返回值。", en: "Run a JavaScript (ES5) file. Captures console.log output and the final return value." }
parameters: [
{
name: file_path
description: { zh: "JavaScript 文件路径", en: "Path to the JavaScript file." }
type: string
required: true
}
]
},
{
name: run_javascript_node
description: { zh: "使用 Node.js 运行 JavaScript 脚本。返回 stdout/stderr 输出。", en: "Run JavaScript using Node.js. Returns stdout/stderr output." }
parameters: [
{
name: script
description: { zh: "要执行的 JavaScript 脚本内容", en: "JavaScript script content to execute." }
type: string
required: true
},
{
name: node_flags
description: { zh: "Node.js 解释器选项,默认为空。可自定义如 --trace-warnings、--no-warnings 等", en: "Node.js interpreter flags (default: empty). Examples: --trace-warnings, --no-warnings." }
type: string
required: false
}
]
},
{
name: run_javascript_node_file
description: { zh: "使用 Node.js 运行 JavaScript 文件。返回 stdout/stderr 输出。", en: "Run a JavaScript file using Node.js. Returns stdout/stderr output." }
parameters: [
{
name: file_path
description: { zh: "JavaScript 文件路径", en: "Path to the JavaScript file." }
type: string
required: true
},
{
name: node_flags
description: { zh: "Node.js 解释器选项,默认为空。可自定义如 --trace-warnings、--no-warnings 等", en: "Node.js interpreter flags (default: empty). Examples: --trace-warnings, --no-warnings." }
type: string
required: false
}
]
},
{
name: install_node_packages
description: { zh: "在持久 Node 工作目录($HOME/.code_runner/node)中安装 pnpm 包", en: "Install packages with pnpm in the persistent Node workspace ($HOME/.code_runner/node)." }
parameters: [
{
name: packages
description: { zh: "要安装的包名(用 | 分隔),例如 axios|lodash|@types/node", en: "Package names to install, separated by | (e.g. axios|lodash|@types/node)." }
type: string
required: true
},
{
name: save_dev
description: { zh: "是否作为开发依赖安装(pnpm add -D)", en: "Whether to install as dev dependency (pnpm add -D)." }
type: boolean
required: false
}
]
},
{
name: install_python_packages
description: { zh: "在持久虚拟环境(~/.code_runner/py)中安装 Python 包(使用 pip)", en: "Install Python packages in the persistent virtual environment (~/.code_runner/py) using pip." }
parameters: [
{
name: packages
description: { zh: "要安装的包名(用 | 分隔),例如 numpy|pydantic==2.*", en: "Package names to install, separated by | (e.g. numpy|pydantic==2.*)." }
type: string
required: true
},
{
name: upgrade
description: { zh: "是否升级已安装的包,等价于 pip -U", en: "Whether to upgrade already installed packages (equivalent to pip -U)." }
type: boolean
required: false
}
]
},
{
name: run_python
description: { zh: "运行自定义 Python 脚本。会捕获 print 函数的输出。", en: "Run custom Python scripts. Captures output from print()." }
parameters: [
{
name: script
description: { zh: "要执行的 Python 脚本内容", en: "Python script content to execute." }
type: string
required: true
},
{
name: python_flags
description: { zh: "Python 解释器选项,默认为空。可自定义如 -O(优化)、-u(无缓冲)等", en: "Python interpreter flags (default: empty). Examples: -O (optimize), -u (unbuffered)." }
type: string
required: false
},
{
name: script_args
description: { zh: "传递给 Python 脚本的参数,使用 | 分隔,例如 arg1|arg with space|--name=alice", en: "Arguments passed to the Python script, separated by | (e.g. arg1|arg with space|--name=alice)." }
type: string
required: false
}
]
},
{
name: run_python_file
description: { zh: "运行 Python 文件。会捕获 print 函数的输出。", en: "Run a Python file. Captures output from print()." }
parameters: [
{
name: file_path
description: { zh: "Python 文件路径", en: "Path to the Python file." }
type: string
required: true
},
{
name: python_flags
description: { zh: "Python 解释器选项,默认为空。可自定义如 -O(优化)、-u(无缓冲)等", en: "Python interpreter flags (default: empty). Examples: -O (optimize), -u (unbuffered)." }
type: string
required: false
},
{
name: script_args
description: { zh: "传递给 Python 文件的参数,使用 | 分隔,例如 arg1|arg with space|--name=alice", en: "Arguments passed to the Python file, separated by | (e.g. arg1|arg with space|--name=alice)." }
type: string
required: false
}
]
},
{
name: run_ruby
description: { zh: "运行自定义 Ruby 脚本", en: "Run custom Ruby scripts." }
parameters: [
{
name: script
description: { zh: "要执行的 Ruby 脚本内容", en: "Ruby script content to execute." }
type: string
required: true
},
{
name: ruby_flags
description: { zh: "Ruby 解释器选项,默认为空。可自定义如 --jit(JIT 编译)等", en: "Ruby interpreter flags (default: empty). Example: --jit." }
type: string
required: false
}
]
},
{
name: run_ruby_file
description: { zh: "运行 Ruby 文件", en: "Run a Ruby file." }
parameters: [
{
name: file_path
description: { zh: "Ruby 文件路径", en: "Path to the Ruby file." }
type: string
required: true
},
{
name: ruby_flags
description: { zh: "Ruby 解释器选项,默认为空。可自定义如 --jit(JIT 编译)等", en: "Ruby interpreter flags (default: empty). Example: --jit." }
type: string
required: false
}
]
},
{
name: run_go
description: { zh: "运行自定义 Go 代码", en: "Run custom Go code." }
parameters: [
{
name: script
description: { zh: "要执行的 Go 代码内容", en: "Go source code to execute." }
type: string
required: true
},
{
name: build_flags
description: { zh: "Go 编译选项,默认为空。可自定义如 -ldflags='-s -w'(减小二进制体积)等", en: "Go build flags (default: empty). Example: -ldflags='-s -w' (reduce binary size)." }
type: string
required: false
}
]
},
{
name: run_go_file
description: { zh: "运行 Go 文件", en: "Run a Go file." }
parameters: [
{
name: file_path
description: { zh: "Go 文件路径", en: "Path to the Go file." }
type: string
required: true
},
{
name: build_flags
description: { zh: "Go 编译选项,默认为空。可自定义如 -ldflags='-s -w'(减小二进制体积)等", en: "Go build flags (default: empty). Example: -ldflags='-s -w' (reduce binary size)." }
type: string
required: false
}
]
},
{
name: run_rust
description: { zh: "运行自定义 Rust 代码", en: "Run custom Rust code." }
parameters: [
{
name: script
description: { zh: "要执行的 Rust 代码内容", en: "Rust source code to execute." }
type: string
required: true
},
{
name: cargo_flags
description: { zh: "Cargo 构建选项,默认为 --release。可自定义如 空字符串(调试模式)、--release --features xxx 等", en: "Cargo build flags (default: --release). Examples: empty string (debug), --release --features xxx." }
type: string
required: false
}
]
},
{
name: run_rust_file
description: { zh: "运行 Rust 文件", en: "Run a Rust file." }
parameters: [
{
name: file_path
description: { zh: "Rust 文件路径", en: "Path to the Rust file." }
type: string
required: true
},
{
name: cargo_flags
description: { zh: "Cargo 构建选项,默认为 --release。可自定义如 空字符串(调试模式)、--release --features xxx 等", en: "Cargo build flags (default: --release). Examples: empty string (debug), --release --features xxx." }
type: string
required: false
}
]
},
{
name: run_c
description: { zh: "运行自定义 C 代码", en: "Run custom C code." }
parameters: [
{
name: script
description: { zh: "要执行的 C 代码内容", en: "C source code to execute." }
type: string
required: true
},
{
name: compile_flags
description: { zh: "编译选项,默认为 -O3 -march=native -fopenmp。可自定义如 -O2、-O0 -g 等", en: "Compile flags (default: -O3 -march=native -fopenmp). Examples: -O2, -O0 -g." }
type: string
required: false
}
]
},
{
name: run_c_file
description: { zh: "运行 C 文件", en: "Run a C file." }
parameters: [
{
name: file_path
description: { zh: "C 文件路径", en: "Path to the C file." }
type: string
required: true
},
{
name: compile_flags
description: { zh: "编译选项,默认为 -O3 -march=native -fopenmp。可自定义如 -O2、-O0 -g 等", en: "Compile flags (default: -O3 -march=native -fopenmp). Examples: -O2, -O0 -g." }
type: string
required: false
}
]
},
{
name: run_cpp
description: { zh: "运行自定义 C++ 代码", en: "Run custom C++ code." }
parameters: [
{
name: script
description: { zh: "要执行的 C++ 代码内容", en: "C++ source code to execute." }
type: string
required: true
},
{
name: compile_flags
description: { zh: "编译选项,默认为 -O3 -march=native -fopenmp。可自定义如 -O2、-O0 -g 等", en: "Compile flags (default: -O3 -march=native -fopenmp). Examples: -O2, -O0 -g." }
type: string
required: false
}
]
},
{
name: run_cpp_file
description: { zh: "运行 C++ 文件", en: "Run a C++ file." }
parameters: [
{
name: file_path
description: { zh: "C++ 文件路径", en: "Path to the C++ file." }
type: string
required: true
},
{
name: compile_flags
description: { zh: "编译选项,默认为 -O3 -march=native -fopenmp。可自定义如 -O2、-O0 -g 等", en: "Compile flags (default: -O3 -march=native -fopenmp). Examples: -O2, -O0 -g." }
type: string
required: false
}
]
}
]
}*/
const codeRunner = (function () {
const CARGO_MIRROR_ENV = 'export CARGO_REGISTRIES_CRATES_IO_REPLACE_WITH="ustc" && export CARGO_REGISTRIES_USTC_INDEX="https://mirrors.ustc.edu.cn/crates.io-index"';
const CODE_RUNNER_HIDDEN_EXECUTOR_KEY = "code_runner_hidden_executor";
const NODE_WORKSPACE_DIR = "$HOME/.code_runner/node";
// Use a persistent hidden executor so commands share one login context without shell wrapping.
async function executeTerminalCommand(command, timeoutMs) {
const options = {
executorKey: CODE_RUNNER_HIDDEN_EXECUTOR_KEY
};
if (timeoutMs !== undefined) {
options.timeoutMs = timeoutMs;
}
return await Tools.System.terminal.hiddenExec(command, options);
}
// Ensure a persistent Python venv under ~/.code_runner/py and return python/pip paths
async function ensurePersistentVenv() {
const venvDir = "~/.code_runner/py";
const pythonBin = `${venvDir}/bin/python`;
const pipBin = `${venvDir}/bin/pip`;
const exists = await executeTerminalCommand(`[ -x ${pythonBin} ] && echo OK || echo NO`);
if (exists.output.includes("OK")) {
return { pythonBin, pipBin };
}
const setup = await executeTerminalCommand(`python3 -m venv ${venvDir}`);
if (setup.exitCode !== 0 || hasError(setup.output)) {
throw new Error(`创建持久 venv 失败:\n${setup.output}`);
}
return { pythonBin, pipBin };
}
// Install Python packages into the persistent venv using pip
async function install_python_packages(params) {
const raw = (params.packages || "").trim();
const pkgs = raw
.split("|")
.map(s => s.trim())
.filter(s => s.length > 0);
if (!pkgs.length)
throw new Error("请提供要安装的包列表(用 | 分隔)packages");
const upgradeFlag = params.upgrade ? "-U" : "";
const { pythonBin } = await ensurePersistentVenv();
// Fallback: use python -m pip to avoid missing pip executable shims
const r2 = await executeTerminalCommand(`${pythonBin} -m pip install ${upgradeFlag} ${pkgs.join(" ")}`.trim());
if (r2.exitCode !== 0 || hasError(r2.output)) {
throw new Error(`安装依赖失败:\n${r2.output}`);
}
return `Installed with pip:\n${r2.output}`.trim();
}
async function ensureNodeAvailable() {
const nodeCheckResult = await executeTerminalCommand("node --version");
if (nodeCheckResult.exitCode !== 0 || hasError(nodeCheckResult.output)) {
throw new Error("Node.js 不可用,请确保已安装 Node.js");
}
}
async function ensurePersistentNodeWorkspace() {
await ensureNodeAvailable();
const createDirResult = await executeTerminalCommand(`mkdir -p ${NODE_WORKSPACE_DIR}`);
if (createDirResult.exitCode !== 0 || hasError(createDirResult.output)) {
throw new Error(`创建 Node 工作目录失败:\n${createDirResult.output}`);
}
const hasPackageJson = await executeTerminalCommand(`[ -f ${NODE_WORKSPACE_DIR}/package.json ] && echo OK || echo NO`);
if (!hasPackageJson.output.includes("OK")) {
const initResult = await executeTerminalCommand(`cat <<'EOF' > ${NODE_WORKSPACE_DIR}/package.json
{
"name": "code-runner-node-workspace",
"private": true
}
EOF`);
if (initResult.exitCode !== 0 || hasError(initResult.output)) {
throw new Error(`初始化 Node 工作目录失败:\n${initResult.output}`);
}
}
return { workspaceDir: NODE_WORKSPACE_DIR };
}
async function install_node_packages(params) {
const raw = (params.packages || "").trim();
const pkgs = raw
.split("|")
.map(s => s.trim())
.filter(s => s.length > 0);
if (!pkgs.length)
throw new Error("请提供要安装的包列表(用 | 分隔)packages");
const { workspaceDir } = await ensurePersistentNodeWorkspace();
const saveFlag = params.save_dev ? "-D" : "--save";
const packageArgs = pkgs.map(p => `'${escapeForShell(p)}'`).join(" ");
const result = await executeTerminalCommand(`cd ${workspaceDir} && pnpm add ${saveFlag} ${packageArgs}`);
if (result.exitCode !== 0 || hasError(result.output)) {
throw new Error(`安装 pnpm 依赖失败:\n${result.output}`);
}
return `Installed with pnpm in ${workspaceDir}:\n${result.output}`.trim();
}
// Helper function to safely escape strings for shell commands
function escapeForShell(str) {
return str.replace(/'/g, "'\\''");
}
function buildPipeSeparatedShellArgs(raw) {
if (!raw || raw.trim() === "") {
return "";
}
return raw
.split("|")
.map(part => part.trim())
.filter(part => part.length > 0)
.map(part => `'${escapeForShell(part)}'`)
.join(" ");
}
// Helper function to execute JavaScript code and capture logs/return value
async function executeJavaScript(script) {
const logs = [];
// Create a proxy for the console object
const consoleProxy = {
log: (...args) => {
const formattedArgs = args.map(arg => {
if (arg === null)
return 'null';
if (arg === undefined)
return 'undefined';
if (typeof arg === 'object') {
try {
return JSON.stringify(arg, null, 2);
}
catch (e) {
return arg.toString();
}
}
return arg.toString();
});
logs.push(formattedArgs.join(' '));
},
warn: (...args) => {
const formattedArgs = args.map(arg => {
if (arg === null)
return 'null';
if (arg === undefined)
return 'undefined';
if (typeof arg === 'object') {
try {
return JSON.stringify(arg, null, 2);
}
catch (e) {
return arg.toString();
}
}
return arg.toString();
});
logs.push(`WARN: ${formattedArgs.join(' ')}`);
},
error: (...args) => {
const formattedArgs = args.map(arg => {
if (arg === null)
return 'null';
if (arg === undefined)
return 'undefined';
if (typeof arg === 'object') {
try {
return JSON.stringify(arg, null, 2);
}
catch (e) {
return arg.toString();
}
}
return arg.toString();
});
logs.push(`ERROR: ${formattedArgs.join(' ')}`);
},
};
try {
// Use the Function constructor to create a new function in the global scope.
// Pass the console proxy to it. This is safer than eval.
const func = new Function('console', `
// Wrap in an IIFE to handle return statements properly and avoid variable leakage.
return (function(){
"use strict";
${script}
})();
`);
const returnValue = func(consoleProxy);
let output = logs.join('\n');
if (returnValue !== undefined) {
if (output) {
output += '\n';
}
let formattedReturnValue;
if (returnValue === null) {
formattedReturnValue = 'null';
}
else if (typeof returnValue === 'object') {
try {
formattedReturnValue = JSON.stringify(returnValue, null, 2);
}
catch (e) {
formattedReturnValue = returnValue.toString();
}
}
else {
formattedReturnValue = returnValue.toString();
}
output += `Return value: ${formattedReturnValue}`;
}
return output || "(No output from console.log or return value)";
}
catch (e) {
const errorOutput = logs.join('\n');
if (errorOutput) {
throw new Error(`Script execution failed: ${e.message}\n\nLogs before error:\n${errorOutput}`);
}
else {
throw new Error(`Script execution failed: ${e.message}`);
}
}
}
// Helper function to check for errors in command output when exit code is unreliable
function hasError(output) {
const errorPatterns = [
"command not found",
"No such file or directory",
"error:",
"Error:",
"failed",
"Failed",
"unable",
"Unable"
];
const lowercasedOutput = output.toLowerCase();
return errorPatterns.some(pattern => lowercasedOutput.includes(pattern));
}
async function main() {
// Ensure /tmp exists
await executeTerminalCommand("mkdir -p /tmp");
const results = {
javascript: await testJavaScript(),
python: await testPython(),
ruby: await testRuby(),
go: await testGo(),
rust: await testRust(),
c: await testC(),
cpp: await testCpp()
};
// Format results for display
let summary = "代码执行器功能测试结果:\n";
for (const [lang, result] of Object.entries(results)) {
summary += `${lang}: ${result.success ? '✅ 成功' : '❌ 失败'} - ${result.message}\n`;
}
return summary;
}
// 测试JavaScript执行功能
async function testJavaScript() {
try {
// 测试简单的JS代码
const script = "console.log('JavaScript 运行正常'); const testVar = 42; return '测试值: ' + testVar;";
const result = await executeJavaScript(script);
const expectedOutput = `JavaScript 运行正常\nReturn value: "测试值: 42"`;
if (result !== expectedOutput) {
return { success: false, message: `JavaScript执行器测试失败: 期望 "${expectedOutput}", 实际 "${result}"` };
}
return { success: true, message: "JavaScript执行器测试成功" };
}
catch (error) {
return { success: false, message: `JavaScript执行器测试失败: ${error.message}` };
}
}
// 测试Python执行功能
async function testPython() {
try {
// 检查Python是否可用
const pythonCheckResult = await executeTerminalCommand("python3 --version");
if (pythonCheckResult.exitCode !== 0 || hasError(pythonCheckResult.output)) {
return { success: false, message: "Python不可用,请确保已安装Python" };
}
// 测试简单的Python代码
const script = "print('Python运行正常')";
const tempPyFile = "/tmp/test_python.py";
await executeTerminalCommand(`cat <<'EOF' > ${tempPyFile}\n${script}\nEOF`);
const runResult = await executeTerminalCommand(`python3 ${tempPyFile}`);
await executeTerminalCommand(`rm -f ${tempPyFile}`);
if (runResult.exitCode !== 0 || hasError(runResult.output) || !runResult.output.includes("Python运行正常")) {
return { success: false, message: `Python执行器测试失败: ${runResult.output}` };
}
return { success: true, message: "Python执行器测试成功" };
}
catch (error) {
return { success: false, message: `Python执行器测试失败: ${error.message}` };
}
}
// 测试Ruby执行功能
async function testRuby() {
try {
// 检查Ruby是否可用
const rubyCheckResult = await executeTerminalCommand("ruby --version");
if (rubyCheckResult.exitCode !== 0 || hasError(rubyCheckResult.output)) {
return { success: false, message: "Ruby不可用,请确保已安装Ruby" };
}
// 测试简单的Ruby代码
const script = "puts 'Ruby运行正常'";
const tempRbFile = "/tmp/test_ruby.rb";
await executeTerminalCommand(`cat <<'EOF' > ${tempRbFile}\n${script}\nEOF`);
const runResult = await executeTerminalCommand(`ruby ${tempRbFile}`);
await executeTerminalCommand(`rm -f ${tempRbFile}`);
if (runResult.exitCode !== 0 || hasError(runResult.output) || !runResult.output.includes("Ruby运行正常")) {
return { success: false, message: `Ruby执行器测试失败: ${runResult.output}` };
}
return { success: true, message: "Ruby执行器测试成功" };
}
catch (error) {
return { success: false, message: `Ruby执行器测试失败: ${error.message}` };
}
}
// 测试Go执行功能
async function testGo() {
try {
// 检查Go是否可用
const goCheckResult = await executeTerminalCommand("go version");
if (goCheckResult.exitCode !== 0 || hasError(goCheckResult.output)) {
return { success: false, message: "Go不可用,请确保已安装Go" };
}
// 测试简单的Go代码
const script = `
package main
import "fmt"
func main() {
fmt.Println("Go运行正常")
}`;
const tempGoDir = "/tmp/test_go_project";
const tempGoFile = `${tempGoDir}/main.go`;
const tempGoExec = `${tempGoDir}/main`;
await executeTerminalCommand(`mkdir -p ${tempGoDir}`);
await executeTerminalCommand(`cat <<'EOF' > ${tempGoFile}\n${script}\nEOF`);
const compileResult = await executeTerminalCommand(`cd ${tempGoDir} && go build -o main main.go`);
if (compileResult.exitCode !== 0 || hasError(compileResult.output)) {
await executeTerminalCommand(`rm -rf ${tempGoDir}`);
return { success: false, message: `Go 编译失败: ${compileResult.output}` };
}
const runResult = await executeTerminalCommand(tempGoExec);
await executeTerminalCommand(`rm -rf ${tempGoDir}`);
if (runResult.exitCode !== 0 || hasError(runResult.output) || !runResult.output.includes("Go运行正常")) {
return { success: false, message: `Go 执行失败: ${runResult.output}` };
}
return { success: true, message: "Go执行器测试成功" };
}
catch (error) {
return { success: false, message: `Go执行器测试失败: ${error.message}` };
}
}
// 检查并配置Rust环境
async function ensureRustConfigured() {
// 在有效目录中运行,避免 "Could not locate working directory" 错误
let rustCheckResult = await executeTerminalCommand("cd /tmp && rustc --version");
if (rustCheckResult.exitCode === 0 && !hasError(rustCheckResult.output)) {
return { success: true, message: "Rust环境已配置" };
}
// 如果未配置默认工具链,则尝试设置
if (rustCheckResult.output.includes("no default is configured")) {
const setupResult = await executeTerminalCommand('export RUSTUP_DIST_SERVER="https://mirrors.ustc.edu.cn/rust-static" && export RUSTUP_UPDATE_ROOT="https://mirrors.ustc.edu.cn/rust-static/rustup" && rustup default stable');
if (setupResult.exitCode !== 0 || hasError(setupResult.output)) {
return { success: false, message: `运行 'rustup default stable' 失败: ${setupResult.output}` };
}
// 再次检查
rustCheckResult = await executeTerminalCommand("cd /tmp && rustc --version");
if (rustCheckResult.exitCode === 0 && !hasError(rustCheckResult.output)) {
return { success: true, message: "Rust环境已自动配置" };
}
}
return { success: false, message: `Rust环境检查失败: ${rustCheckResult.output}` };
}
// 测试Rust执行功能
async function testRust() {
try {
const rustConfig = await ensureRustConfigured();
if (!rustConfig.success) {
return { success: false, message: rustConfig.message };
}
// 测试简单的Rust代码
const script = `
fn main() {
println!("Rust运行正常");
}`;
const tempRustDir = "/tmp/test_rust_project";
const tempRustSrcDir = `${tempRustDir}/src`;
const tempRustFile = `${tempRustSrcDir}/main.rs`;
const cargoToml = `
[package]
name = "test_rust"
version = "0.1.0"
edition = "2021"
[dependencies]
`;
await executeTerminalCommand(`mkdir -p ${tempRustSrcDir}`);
await executeTerminalCommand(`cat <<'EOF' > ${tempRustDir}/Cargo.toml\n${cargoToml}\nEOF`);
await executeTerminalCommand(`cat <<'EOF' > ${tempRustFile}\n${script}\nEOF`);
// 在有效目录中运行 cargo
const compileResult = await executeTerminalCommand(`cd ${tempRustDir} && ${CARGO_MIRROR_ENV} && cargo build --release`);
if (compileResult.exitCode !== 0 || hasError(compileResult.output)) {
await executeTerminalCommand(`rm -rf ${tempRustDir}`);
return { success: false, message: `Rust 编译失败: ${compileResult.output}` };
}
const execPath = `${tempRustDir}/target/release/test_rust`;
const runResult = await executeTerminalCommand(execPath);
await executeTerminalCommand(`rm -rf ${tempRustDir}`);
if (runResult.exitCode !== 0 || hasError(runResult.output) || !runResult.output.includes("Rust运行正常")) {
return { success: false, message: `Rust 执行失败: ${runResult.output}` };
}
return { success: true, message: "Rust执行器测试成功" };
}
catch (error) {
return { success: false, message: `Rust执行器测试失败: ${error.message}` };
}
}
// 测试C执行功能
async function testC() {
try {
// 检查gcc是否可用
const gccCheckResult = await executeTerminalCommand("gcc --version");
if (gccCheckResult.exitCode !== 0 || hasError(gccCheckResult.output)) {
return { success: false, message: "GCC不可用,请确保已安装gcc" };
}
// 测试简单的C代码
const script = `
#include <stdio.h>
int main() {
printf("C运行正常\\n");
return 0;
}`;
const tempCFile = "/tmp/test_c.c";
const tempCExec = "/tmp/test_c";
await executeTerminalCommand(`cat <<'EOF' > ${tempCFile}\n${script}\nEOF`);
const compileResult = await executeTerminalCommand(`gcc -O3 -march=native -fopenmp ${tempCFile} -o ${tempCExec}`);
if (compileResult.exitCode !== 0 || hasError(compileResult.output)) {
await executeTerminalCommand(`rm -f ${tempCFile} ${tempCExec}`);
return { success: false, message: `C 编译失败: ${compileResult.output}` };
}
const runResult = await executeTerminalCommand(tempCExec);
await executeTerminalCommand(`rm -f ${tempCFile} ${tempCExec}`);
if (runResult.exitCode !== 0 || hasError(runResult.output) || !runResult.output.includes("C运行正常")) {
return { success: false, message: `C 执行失败: ${runResult.output}` };
}
return { success: true, message: "C执行器测试成功" };
}
catch (error) {
return { success: false, message: `C执行器测试失败: ${error.message}` };
}
}
// 测试C++执行功能
async function testCpp() {
try {
// 检查g++是否可用
const gppCheckResult = await executeTerminalCommand("g++ --version");
if (gppCheckResult.exitCode !== 0 || hasError(gppCheckResult.output)) {
return { success: false, message: "G++不可用,请确保已安装g++" };
}
// 测试简单的C++代码
const script = `
#include <iostream>
int main() {
std::cout << "C++运行正常" << std::endl;
return 0;
}`;
const tempCppFile = "/tmp/test_cpp.cpp";
const tempCppExec = "/tmp/test_cpp";
await executeTerminalCommand(`cat <<'EOF' > ${tempCppFile}\n${script}\nEOF`);
const compileResult = await executeTerminalCommand(`g++ -O3 -march=native -fopenmp ${tempCppFile} -o ${tempCppExec}`);
if (compileResult.exitCode !== 0 || hasError(compileResult.output)) {
await executeTerminalCommand(`rm -f ${tempCppFile} ${tempCppExec}`);
return { success: false, message: `C++ 编译失败: ${compileResult.output}` };
}
const runResult = await executeTerminalCommand(tempCppExec);
await executeTerminalCommand(`rm -f ${tempCppFile} ${tempCppExec}`);
if (runResult.exitCode !== 0 || hasError(runResult.output) || !runResult.output.includes("C++运行正常")) {
return { success: false, message: `C++ 执行失败: ${runResult.output}` };
}
return { success: true, message: "C++执行器测试成功" };
}
catch (error) {
return { success: false, message: `C++执行器测试失败: ${error.message}` };
}
}
async function run_javascript_es5(params) {
const script = params.script;
if (!script || script.trim() === "") {
throw new Error("请提供要执行的脚本内容");
}
return executeJavaScript(script);
}
async function run_javascript_file(params) {
const filePath = params.file_path;
if (!filePath || filePath.trim() === "") {
throw new Error("请提供要执行的 JavaScript 文件路径");
}
const fileResult = await Tools.Files.read(filePath);
if (!fileResult || !fileResult.content) {
throw new Error(`无法读取文件: ${filePath}`);
}
return executeJavaScript(fileResult.content);
}
async function run_javascript_node(params) {
const script = params.script;
if (!script || script.trim() === "") {
throw new Error("请提供要执行的 JavaScript 脚本内容");
}
const { workspaceDir } = await ensurePersistentNodeWorkspace();
const nodeFlags = params.node_flags || "";
const tempFileName = `temp_script_node_${Date.now()}.js`;
const tempFilePath = `${workspaceDir}/${tempFileName}`;
try {
await executeTerminalCommand(`cat <<'EOF' > ${tempFilePath}\n${script}\nEOF`);
const result = await executeTerminalCommand(`cd ${workspaceDir} && NODE_PATH=${workspaceDir}/node_modules node ${nodeFlags} ${tempFileName}`.trim());
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`JavaScript (Node.js) 脚本执行失败:\n${result.output}`);
}
}
finally {
await executeTerminalCommand(`rm -f ${tempFilePath}`).catch(err => console.error(`删除临时文件失败: ${err.message}`));
}
}
async function run_javascript_node_file(params) {
const filePath = params.file_path;
if (!filePath || filePath.trim() === "") {
throw new Error("请提供要执行的 JavaScript 文件路径");
}
const { workspaceDir } = await ensurePersistentNodeWorkspace();
const escapedPath = escapeForShell(filePath);
const fileExistsResult = await executeTerminalCommand(`test -f '${escapedPath}'`);
if (fileExistsResult.exitCode !== 0 || hasError(fileExistsResult.output)) {
throw new Error(`JavaScript 文件不存在或路径错误: ${filePath}`);
}
const nodeFlags = params.node_flags || "";
const result = await executeTerminalCommand(`cd ${workspaceDir} && NODE_PATH=${workspaceDir}/node_modules node ${nodeFlags} '${escapedPath}'`.trim());
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`JavaScript (Node.js) 文件执行失败:\n${result.output}`);
}
}
async function run_python(params) {
const script = params.script;
if (!script || script.trim() === "") {
throw new Error("请提供要执行的 Python 脚本内容");
}
// Use persistent venv interpreter
const { pythonBin } = await ensurePersistentVenv();
const pythonFlags = params.python_flags || "";
const scriptArgs = buildPipeSeparatedShellArgs(params.script_args);
const tempFilePath = "/tmp/temp_script.py";
const escapedTempFilePath = escapeForShell(tempFilePath);
try {
await executeTerminalCommand(`cat <<'EOF' > ${tempFilePath}\n${script}\nEOF`);
const result = await executeTerminalCommand(`${pythonBin} ${pythonFlags} '${escapedTempFilePath}' ${scriptArgs}`.trim());
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`Python 脚本执行失败:\n${result.output}`);
}
}
finally {
await executeTerminalCommand(`rm -f ${tempFilePath}`).catch(err => console.error(`删除临时文件失败: ${err.message}`));
}
}
async function run_python_file(params) {
const filePath = params.file_path;
if (!filePath || filePath.trim() === "") {
throw new Error("请提供要执行的 Python 文件路径");
}
const escapedPath = escapeForShell(filePath);
const fileExistsResult = await executeTerminalCommand(`test -f '${escapedPath}'`);
if (fileExistsResult.exitCode !== 0 || hasError(fileExistsResult.output)) {
throw new Error(`Python 文件不存在或路径错误: ${filePath}`);
}
// Use persistent venv interpreter
const { pythonBin } = await ensurePersistentVenv();
const pythonFlags = params.python_flags || "";
const scriptArgs = buildPipeSeparatedShellArgs(params.script_args);
const result = await executeTerminalCommand(`${pythonBin} ${pythonFlags} '${escapedPath}' ${scriptArgs}`.trim());
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`Python 文件执行失败:\n${result.output}`);
}
}
async function run_ruby(params) {
const script = params.script;
if (!script || script.trim() === "") {
throw new Error("请提供要执行的 Ruby 脚本内容");
}
const rubyFlags = params.ruby_flags || "";
const tempFilePath = "/tmp/temp_script.rb";
try {
await executeTerminalCommand(`cat <<'EOF' > ${tempFilePath}\n${script}\nEOF`);
const result = await executeTerminalCommand(`ruby ${rubyFlags} ${tempFilePath}`);
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`Ruby 脚本执行失败:\n${result.output}`);
}
}
finally {
await executeTerminalCommand(`rm -f ${tempFilePath}`).catch(err => console.error(`删除临时文件失败: ${err.message}`));
}
}
async function run_ruby_file(params) {
const filePath = params.file_path;
if (!filePath || filePath.trim() === "") {
throw new Error("请提供要执行的 Ruby 文件路径");
}
const fileExistsResult = await executeTerminalCommand(`test -f ${filePath}`);
if (fileExistsResult.exitCode !== 0 || hasError(fileExistsResult.output)) {
throw new Error(`Ruby 文件不存在或路径错误: ${filePath}`);
}
const rubyFlags = params.ruby_flags || "";
const result = await executeTerminalCommand(`ruby ${rubyFlags} ${filePath}`);
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`Ruby 文件执行失败:\n${result.output}`);
}
}
async function run_go(params) {
const script = params.script;
if (!script || script.trim() === "") {
throw new Error("请提供要执行的 Go 代码内容");
}
const buildFlags = params.build_flags || "";
const tempDirPath = "/tmp/temp_go";
const tempFilePath = `${tempDirPath}/main.go`;
try {
await executeTerminalCommand(`mkdir -p ${tempDirPath}`);
await executeTerminalCommand(`cat <<'EOF' > ${tempFilePath}\n${script}\nEOF`);
const compileResult = await executeTerminalCommand(`cd ${tempDirPath} && go build ${buildFlags} -o main main.go`);
if (compileResult.exitCode !== 0 || hasError(compileResult.output)) {
throw new Error(`Go 代码编译失败:\n${compileResult.output}`);
}
const result = await executeTerminalCommand(`${tempDirPath}/main`);
if (result.exitCode === 0 && !hasError(result.output)) {
return result.output.trim();
}
else {
throw new Error(`Go 代码执行失败:\n${result.output}`);
}
}
finally {
await executeTerminalCommand(`rm -rf ${tempDirPath}`).catch(err => console.error(`删除临时目录失败: ${err.message}`));
}
}
async function run_go_file(params) {
const filePath = params.file_path;
if (!filePath || filePath.trim() === "") {
throw new Error("请提供要执行的 Go 文件路径");
}
const fileExistsResult = await executeTerminalCommand(`test -f ${filePath}`);
if (fileExistsResult.exitCode !== 0 || hasError(fileExistsResult.output)) {
throw new Error(`Go 文件不存在或路径错误: ${filePath}`);
}
const buildFlags = params.build_flags || "";
const tempExecPath = "/tmp/temp_go_exec";
try {