- Update @kevisual/query to 0.0.32 and @kevisual/router to 0.0.37 - Restructure AI command interface with run and deploy subcommands - Add comprehensive logging throughout cmd-execution flow - Improve sync module with better configuration handling - Add clickable link functionality in logger - Enhance error handling and debugging capabilities 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import { app } from '../ai.ts';
|
||
import { execSync } from 'node:child_process'
|
||
import { logger } from '@/module/logger.ts';
|
||
const promptTemplate = `# CMD 结果判断器
|
||
|
||
分析上一条 CMD 命令的执行结果,判断是否需要执行下一条命令。
|
||
|
||
- 若结果中隐含或明确指示需继续执行 → 返回:\`{"cmd": "推断出的下一条命令", "type": "cmd"}\`
|
||
- 若无后续操作,甚至上一次执行的返回为空或者成功 → 返回:\`{"type": "none"}\`
|
||
|
||
1. 仅输出合法 JSON,无任何额外文本。
|
||
2. \`cmd\` 必须从执行结果中合理推断得出,非预设或猜测。
|
||
3. 禁止解释、注释、换行或格式错误。`
|
||
|
||
app.router.route({
|
||
path: 'cmd-run',
|
||
description: '执行 CMD 命令并判断下一步操作, 参数是 cmd 字符串',
|
||
}).define(async (ctx) => {
|
||
const cmd = ctx.query.cmd || '';
|
||
if (!cmd) {
|
||
ctx.throw(400, 'cmd is required');
|
||
}
|
||
let result = '';
|
||
ctx.state.steps = ctx.state?.steps || [];
|
||
|
||
try {
|
||
logger.info('执行命令:', cmd);
|
||
result = execSync(cmd, { encoding: 'utf-8' });
|
||
ctx.state.steps.push({ cmd, result });
|
||
logger.info(result);
|
||
} catch (error: any) {
|
||
result = error.message || '';
|
||
ctx.state.steps.push({ cmd, result, error: true });
|
||
ctx.body = {
|
||
steps: ctx.state.steps,
|
||
}
|
||
return;
|
||
}
|
||
await app.loadAI()
|
||
const prompt = `${promptTemplate}\n上一条命令:\n${cmd}\n执行结果:\n${result}\n`;
|
||
const response = await app.ai.question(prompt);
|
||
|
||
const msg = app.ai.utils.extractJsonFromMarkdown(app.ai.responseText);
|
||
try {
|
||
logger.debug('AI Prompt', prompt);
|
||
logger.debug('AI 分析结果:', msg);
|
||
const { cmd, type } = msg;
|
||
if (type === 'cmd' && cmd) {
|
||
await app.router.call({ path: 'cmd-run', payload: { cmd } }, { state: ctx.state });
|
||
} else {
|
||
logger.info('无后续命令,结束执行');
|
||
ctx.state.steps.push({ type: 'none' });
|
||
}
|
||
} catch (error) {
|
||
result = '执行错误,无法解析返回结果为合法 JSON' + app.ai.responseText
|
||
logger.error(result);
|
||
ctx.state.steps.push({ cmd, result, parseError: true });
|
||
}
|
||
ctx.body = {
|
||
steps: ctx.state.steps,
|
||
}
|
||
}).addTo(app.router); |