travel-agent-cli 0.4.2 → 0.4.4
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/interactive.jsx +73 -7
- package/package.json +1 -1
package/interactive.jsx
CHANGED
|
@@ -7,6 +7,15 @@ import React, { useState, useEffect } from 'react';
|
|
|
7
7
|
import { render, Box, Text, useApp, useInput } from 'ink';
|
|
8
8
|
import TextInput from 'ink-text-input';
|
|
9
9
|
import chalk from 'chalk';
|
|
10
|
+
import { spawn } from 'child_process';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import { fileURLToPath } from 'url';
|
|
13
|
+
|
|
14
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
+
const __dirname = path.dirname(__filename);
|
|
16
|
+
const packagePath = path.join(__dirname, '..');
|
|
17
|
+
const pythonDir = path.join(packagePath, 'python');
|
|
18
|
+
const mainPy = path.join(pythonDir, 'main.py');
|
|
10
19
|
|
|
11
20
|
// 命令选项
|
|
12
21
|
const commands = [
|
|
@@ -20,6 +29,52 @@ const commands = [
|
|
|
20
29
|
{ label: 'help - 帮助信息', value: 'help' },
|
|
21
30
|
];
|
|
22
31
|
|
|
32
|
+
// 查找 Python 命令
|
|
33
|
+
function findPython() {
|
|
34
|
+
const { execSync } = require('child_process');
|
|
35
|
+
const candidates = ['python3', 'python'];
|
|
36
|
+
|
|
37
|
+
for (const cmd of candidates) {
|
|
38
|
+
try {
|
|
39
|
+
execSync(`${cmd} --version`, { stdio: 'ignore' });
|
|
40
|
+
return cmd;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return 'python3'; // 默认使用 python3
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 执行 Python 命令
|
|
50
|
+
const runPythonCommand = (cmd, args) => {
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
let output = '';
|
|
53
|
+
let error = '';
|
|
54
|
+
|
|
55
|
+
const pythonCmd = findPython();
|
|
56
|
+
const proc = spawn(pythonCmd, [mainPy, cmd, ...args], {
|
|
57
|
+
cwd: pythonDir,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
proc.stdout.on('data', (data) => {
|
|
61
|
+
output += data.toString();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
proc.stderr.on('data', (data) => {
|
|
65
|
+
error += data.toString();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
proc.on('close', (code) => {
|
|
69
|
+
if (code === 0) {
|
|
70
|
+
resolve(output.trim());
|
|
71
|
+
} else {
|
|
72
|
+
reject(error.trim() || '命令执行失败');
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
|
|
23
78
|
// 主应用组件
|
|
24
79
|
function App() {
|
|
25
80
|
const { exit } = useApp();
|
|
@@ -106,14 +161,25 @@ function App() {
|
|
|
106
161
|
setInput('');
|
|
107
162
|
setIsProcessing(true);
|
|
108
163
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
164
|
+
try {
|
|
165
|
+
let result;
|
|
166
|
+
if (value.startsWith('/')) {
|
|
167
|
+
// 处理斜杠命令
|
|
168
|
+
const parts = value.slice(1).split(' ');
|
|
169
|
+
const cmd = parts[0];
|
|
170
|
+
const args = parts.slice(1);
|
|
171
|
+
result = await runPythonCommand(cmd, args);
|
|
172
|
+
} else {
|
|
173
|
+
// 默认使用 flyai
|
|
174
|
+
result = await runPythonCommand('flyai', value.split(' '));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
setMessages(prev => [...prev, { type: 'result', content: result }]);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
setMessages(prev => [...prev, { type: 'error', content: err.message || '命令执行失败' }]);
|
|
180
|
+
} finally {
|
|
115
181
|
setIsProcessing(false);
|
|
116
|
-
}
|
|
182
|
+
}
|
|
117
183
|
};
|
|
118
184
|
|
|
119
185
|
// 命令面板过滤
|