44 */
55
66import { promises as fs } from 'fs' ;
7+ import type { ChatCompletionMessageToolCall } from 'openai/resources/chat' ;
78import * as path from 'path' ;
89import { Agent } from '../agent/Agent.js' ;
910import { getState , sessionActions } from '../store/vanilla.js' ;
11+ import type { ToolResult } from '../tools/types/index.js' ;
12+ import { formatToolCallSummary } from '../ui/utils/toolFormatters.js' ;
1013import type { SlashCommand , SlashCommandContext , SlashCommandResult } from './types.js' ;
1114
1215const initCommand : SlashCommand = {
@@ -20,6 +23,7 @@ const initCommand: SlashCommand = {
2023 try {
2124 const { cwd } = context ;
2225 const addMessage = sessionActions ( ) . addAssistantMessage ;
26+ const addToolMessage = sessionActions ( ) . addToolMessage ;
2327
2428 // 从 store 获取 sessionId
2529 const sessionId = getState ( ) . session . sessionId ;
@@ -79,12 +83,42 @@ const initCommand: SlashCommand = {
7983**Final output**: Return your analysis and suggestions as plain text. Do NOT use Write tool.` ;
8084
8185 // 使用 chat 方法让 Agent 可以调用工具
82- const result = await agent . chat ( analysisPrompt , {
83- messages : [ ] ,
84- userId : 'cli-user' ,
85- sessionId : sessionId || 'init-session' ,
86- workspaceRoot : cwd ,
87- } ) ;
86+ const result = await agent . chat (
87+ analysisPrompt ,
88+ {
89+ messages : [ ] ,
90+ userId : 'cli-user' ,
91+ sessionId : sessionId || 'init-session' ,
92+ workspaceRoot : cwd ,
93+ } ,
94+ {
95+ onToolStart : ( toolCall : ChatCompletionMessageToolCall ) => {
96+ if ( toolCall . type !== 'function' ) return ;
97+ try {
98+ const params = JSON . parse ( toolCall . function . arguments ) ;
99+ const summary = formatToolCallSummary ( toolCall . function . name , params ) ;
100+ addToolMessage ( summary , {
101+ toolName : toolCall . function . name ,
102+ phase : 'start' ,
103+ summary,
104+ params,
105+ } ) ;
106+ } catch {
107+ // 静默处理解析错误
108+ }
109+ } ,
110+ onToolResult : async ( toolCall : ChatCompletionMessageToolCall , result : ToolResult ) => {
111+ if ( toolCall . type !== 'function' ) return ;
112+ if ( result ?. metadata ?. summary ) {
113+ addToolMessage ( result . metadata . summary , {
114+ toolName : toolCall . function . name ,
115+ phase : 'complete' ,
116+ summary : result . metadata . summary ,
117+ } ) ;
118+ }
119+ } ,
120+ }
121+ ) ;
88122
89123 addMessage ( result ) ;
90124
@@ -138,12 +172,42 @@ const initCommand: SlashCommand = {
138172**Final output**: Return ONLY the complete BLADE.md content (markdown format), ready to be written to the file.` ;
139173
140174 // 使用 chat 方法让 Agent 可以调用工具
141- const generatedContent = await agent . chat ( analysisPrompt , {
142- messages : [ ] ,
143- userId : 'cli-user' ,
144- sessionId : sessionId || 'init-session' ,
145- workspaceRoot : cwd ,
146- } ) ;
175+ const generatedContent = await agent . chat (
176+ analysisPrompt ,
177+ {
178+ messages : [ ] ,
179+ userId : 'cli-user' ,
180+ sessionId : sessionId || 'init-session' ,
181+ workspaceRoot : cwd ,
182+ } ,
183+ {
184+ onToolStart : ( toolCall : ChatCompletionMessageToolCall ) => {
185+ if ( toolCall . type !== 'function' ) return ;
186+ try {
187+ const params = JSON . parse ( toolCall . function . arguments ) ;
188+ const summary = formatToolCallSummary ( toolCall . function . name , params ) ;
189+ addToolMessage ( summary , {
190+ toolName : toolCall . function . name ,
191+ phase : 'start' ,
192+ summary,
193+ params,
194+ } ) ;
195+ } catch {
196+ // 静默处理解析错误
197+ }
198+ } ,
199+ onToolResult : async ( toolCall : ChatCompletionMessageToolCall , result : ToolResult ) => {
200+ if ( toolCall . type !== 'function' ) return ;
201+ if ( result ?. metadata ?. summary ) {
202+ addToolMessage ( result . metadata . summary , {
203+ toolName : toolCall . function . name ,
204+ phase : 'complete' ,
205+ summary : result . metadata . summary ,
206+ } ) ;
207+ }
208+ } ,
209+ }
210+ ) ;
147211
148212 // 验证生成内容的有效性(至少应该有基本的标题和内容)
149213 if ( ! generatedContent || generatedContent . trim ( ) . length === 0 ) {
0 commit comments