handleSlashCommand 处理 slash command
(ctx context.Context, text string)
| 1276 | |
| 1277 | // handleSlashCommand 处理 slash command |
| 1278 | func (a *Agent) handleSlashCommand(ctx context.Context, text string) error { |
| 1279 | if a.commandExecutor == nil { |
| 1280 | agentLog.Error(ctx, "slash commands not enabled", map[string]any{"agent_id": a.id}) |
| 1281 | return errors.New("slash commands not enabled") |
| 1282 | } |
| 1283 | |
| 1284 | // 解析命令和参数 |
| 1285 | parts := strings.Fields(text) |
| 1286 | commandName := strings.TrimPrefix(parts[0], "/") |
| 1287 | |
| 1288 | args := make(map[string]string) |
| 1289 | if len(parts) > 1 { |
| 1290 | args["argument"] = strings.Join(parts[1:], " ") |
| 1291 | } |
| 1292 | |
| 1293 | agentLog.Debug(ctx, "executing command", map[string]any{"agent_id": a.id, "command": commandName, "args": args}) |
| 1294 | |
| 1295 | // 执行命令并获取消息 |
| 1296 | message, err := a.commandExecutor.Execute(ctx, commandName, args) |
| 1297 | if err != nil { |
| 1298 | agentLog.Error(ctx, "failed to execute command", map[string]any{"agent_id": a.id, "command": commandName, "error": err}) |
| 1299 | return fmt.Errorf("execute command: %w", err) |
| 1300 | } |
| 1301 | |
| 1302 | agentLog.Debug(ctx, "command executed successfully", map[string]any{"agent_id": a.id, "command": commandName, "message_length": len(message)}) |
| 1303 | |
| 1304 | // 将命令消息作为用户消息发送 |
| 1305 | userMessage := types.Message{ |
| 1306 | Role: types.MessageRoleUser, |
| 1307 | ContentBlocks: []types.ContentBlock{ |
| 1308 | &types.TextBlock{Text: message}, |
| 1309 | }, |
| 1310 | } |
| 1311 | |
| 1312 | a.messages = append(a.messages, userMessage) |
| 1313 | a.stepCount++ |
| 1314 | |
| 1315 | // 持久化 |
| 1316 | if err := a.deps.Store.SaveMessages(ctx, a.id, a.messages); err != nil { |
| 1317 | return fmt.Errorf("save messages: %w", err) |
| 1318 | } |
| 1319 | |
| 1320 | agentLog.Debug(ctx, "command processing started", map[string]any{"agent_id": a.id, "command": commandName}) |
| 1321 | |
| 1322 | // 触发处理 |
| 1323 | go a.processMessages(ctx) |
| 1324 | |
| 1325 | return nil |
| 1326 | } |
| 1327 | |
| 1328 | // getRecentFiles 获取最近访问的文件列表 |
| 1329 | func (a *Agent) getRecentFiles() []string { |
no test coverage detected