feat: Integrate recommended workflows display

- Added fs, os imports and promisify for async file reading
- Implemented async method to load and parse the configuration file
- Extended CommandEntry interface and adjusted command logic to include recommendations
This commit is contained in:
bobo.yang 2024-03-26 16:18:03 +08:00
parent 2a5fb709fb
commit 7c03eb81da

View File

@ -1,5 +1,8 @@
import * as dotenv from 'dotenv';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import { promisify } from 'util';
import { logger } from '../util/logger';
import { CommandRun, saveModelSettings } from "../util/commonUtil";
@ -7,7 +10,9 @@ import { UiUtilWrapper } from '../util/uiUtil';
import { ApiKeyManager } from '../util/apiKey';
import { assertValue } from '../util/check';
import { getFileContent } from '../util/commonUtil';
import * as toml from '@iarna/toml';
const readFileAsync = promisify(fs.readFile);
const envPath = path.join(__dirname, '..', '.env');
dotenv.config({ path: envPath });
@ -42,6 +47,7 @@ export interface CommandEntry {
name: string;
description: string;
path: string;
recommand: number;
}
export interface TopicEntry {
@ -414,6 +420,43 @@ class DevChat {
}
}
async loadRecommandCommands(): Promise<string[]> {
try {
// 获取用户的主目录
const userHomeDir = os.homedir();
// 构建配置文件路径
const configFilePath = path.join(userHomeDir, '.chat', 'workflows', 'sys', 'configuration.toml');
// 异步读取配置文件内容
const configFileContent = await readFileAsync(configFilePath, { encoding: 'utf8' });
// 解析TOML配置文件内容并返回命令列表
return this.parseConfigFile(configFileContent);
} catch (err) {
console.error('Failed to load recommend commands:', err);
return [];
}
}
// 解析TOML配置文件内容
private parseConfigFile(content: string): string[] {
interface Config {
recommend?: {
workflows?: string[];
};
}
try {
const parsedData = toml.parse(content) as Config;
if (parsedData.recommend && parsedData.recommend.workflows) {
return parsedData.recommend.workflows;
}
} catch (err) {
console.error('Error parsing TOML content:', err);
}
return [];
}
async commands(): Promise<CommandEntry[]> {
try {
const args = ["-m", "devchat", "run", "--list"];
@ -425,7 +468,22 @@ class DevChat {
logger.channel()?.warn(`${stderr}`);
}
const commands = JSON.parse(stdout.trim());
let commands = JSON.parse(stdout.trim());
// 确保每个CommandEntry对象的recommand字段默认为-1
commands = commands.map((cmd: CommandEntry) => ({
...cmd,
recommand: cmd.recommand ?? -1,
}));
const recommandCommands = await this.loadRecommandCommands();
recommandCommands.forEach((recommandName, index) => {
const foundIndex = commands.findIndex(cmd => cmd.name === recommandName);
if (foundIndex !== -1) {
commands[foundIndex].recommand = index;
}
});
return commands;
} catch (error: any) {