1+ #!/usr/bin/env node
2+
3+ /**
4+ * 平铺配置CLI命令
5+ * 直接使用三要素配置驱动命令
6+ */
7+
8+ import { Command } from 'commander' ;
9+ import chalk from 'chalk' ;
10+ import { Agent } from '../agent/Agent.js' ;
11+
12+ /**
13+ * 注册Agent-LLM相关命令
14+ */
15+ export function agentLlmCommand ( program : Command ) {
16+ const llmCmd = program
17+ . command ( 'chat' )
18+ . description ( '💬 智能对话' )
19+ . argument ( '[message]' , '对话内容' )
20+ . option ( '-k, --api-key <key>' , 'API密钥' )
21+ . option ( '-u, --base-url <url>' , 'API基础URL' )
22+ . option ( '-m, --model <name>' , '模型名称' )
23+ . option ( '-s, --system <prompt>' , '系统提示词' )
24+ . option ( '-i, --interactive' , '交互式对话' )
25+ . option ( '--theme <name>' , '界面主题 (default|dark|dracula|nord|tokyo-night|github|monokai|ayu-dark|solarized-light|solarized-dark|gruvbox|one-dark|catppuccin|rose-pine|kanagawa)' )
26+ . action ( async ( message , options ) => {
27+ await handleChat ( message , options ) ;
28+ } ) ;
29+
30+ // 别名
31+ llmCmd . alias ( 'c' ) ;
32+ }
33+
34+ /**
35+ * 处理聊天命令
36+ */
37+ async function handleChat (
38+ message : string | undefined ,
39+ options : {
40+ apiKey ?: string ;
41+ baseUrl ? : string ;
42+ model ? : string ;
43+ system ? : string ;
44+ interactive ? : boolean ;
45+ theme ? : string ;
46+ }
47+ ) : Promise < void > {
48+ try {
49+ // 构建配置
50+ const configUpdates : any = { } ;
51+ if ( options . apiKey ) configUpdates . apiKey = options . apiKey ;
52+ if ( options . baseUrl ) configUpdates . baseUrl = options . baseUrl ;
53+ if ( options . model ) configUpdates . modelName = options . model ;
54+
55+ // 创建Agent实例
56+ const agent = new Agent ( configUpdates ) ;
57+
58+ // 设置主题
59+ if ( options . theme ) {
60+ const { themeManager } = await import ( '../ui/themes/index.js' ) ;
61+ themeManager . setTheme ( options . theme ) ;
62+ }
63+
64+ // 交互式模式
65+ if ( options . interactive || ! message ) {
66+ await interactiveChat ( agent , options . system ) ;
67+ return ;
68+ }
69+
70+ // 单次对话
71+ if ( options . system ) {
72+ const response = await agent . chatWithSystem ( options . system , message ) ;
73+ console . log ( response ) ;
74+ } else {
75+ const response = await agent . chat ( message ) ;
76+ console . log ( response ) ;
77+ }
78+ } catch ( error ) {
79+ console . error ( chalk . red ( `❌ 调用失败: ${ ( error as Error ) . message } ` ) ) ;
80+ process . exit ( 1 ) ;
81+ }
82+ }
83+
84+ /**
85+ * 交互式聊天
86+ */
87+ async function interactiveChat ( agent : Agent , systemPrompt ? : string ) : Promise < void > {
88+ const readline = require ( 'readline' ) ;
89+ const rl = readline . createInterface ( {
90+ input : process . stdin ,
91+ output : process . stdout ,
92+ } ) ;
93+
94+ console . log ( chalk . cyan ( '🚀 启动交互式对话 (输入 "exit" 或 "quit" 退出)' ) ) ;
95+ if ( systemPrompt ) {
96+ console . log ( chalk . gray ( `系统提示词: ${ systemPrompt } ` ) ) ;
97+ }
98+ console . log ( '' ) ;
99+
100+ const chatLoop = async ( ) = > {
101+ rl . question ( chalk . green ( '👤 你: ' ) , async ( input : string ) => {
102+ if ( input . toLowerCase ( ) === 'exit' || input . toLowerCase ( ) === 'quit' ) {
103+ console . log ( chalk . yellow ( '👋 再见!' ) ) ;
104+ rl . close ( ) ;
105+ return ;
106+ }
107+
108+ if ( input . trim ( ) ) {
109+ try {
110+ process . stdout . write ( chalk . blue ( '🤖 AI: ' ) ) ;
111+ const response = systemPrompt
112+ ? await agent . chatWithSystem ( systemPrompt , input )
113+ : await agent . chat ( input ) ;
114+ console . log ( response ) ;
115+ } catch ( error ) {
116+ console . error ( chalk . red ( `\n❌ 调用失败: ${ ( error as Error ) . message } ` ) ) ;
117+ }
118+ }
119+ console . log ( '' ) ;
120+ chatLoop ( ) ;
121+ } ) ;
122+ } ;
123+
124+ chatLoop ( ) ;
125+ }
0 commit comments