runREPL runs the read-eval-print loop
(ctx context.Context, ag *agent.Agent, sessionStore session.Service, sessionID string, useColor bool)
| 317 | |
| 318 | // runREPL runs the read-eval-print loop |
| 319 | func runREPL(ctx context.Context, ag *agent.Agent, sessionStore session.Service, sessionID string, useColor bool) error { |
| 320 | reader := bufio.NewReader(os.Stdin) |
| 321 | |
| 322 | for { |
| 323 | // Print prompt |
| 324 | printColored(useColor, colorBold+colorBlue, "\naster> ") |
| 325 | |
| 326 | // Read input |
| 327 | input, err := reader.ReadString('\n') |
| 328 | if err != nil { |
| 329 | if err == io.EOF { |
| 330 | printColored(useColor, colorYellow, "\n\n👋 Goodbye!\n") |
| 331 | return nil |
| 332 | } |
| 333 | return fmt.Errorf("read input: %w", err) |
| 334 | } |
| 335 | |
| 336 | input = strings.TrimSpace(input) |
| 337 | if input == "" { |
| 338 | continue |
| 339 | } |
| 340 | |
| 341 | // Handle commands |
| 342 | if strings.HasPrefix(input, "/") { |
| 343 | handled, err := handleCommand(ctx, input, ag, sessionStore, sessionID, useColor) |
| 344 | if err != nil { |
| 345 | printColored(useColor, colorYellow, "Error: %s\n", err) |
| 346 | } |
| 347 | if handled { |
| 348 | if input == "/exit" || input == "/quit" { |
| 349 | return nil |
| 350 | } |
| 351 | continue |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Record user message to session |
| 356 | _ = sessionStore.AppendEvent(ctx, sessionID, &session.Event{ |
| 357 | Author: "user", |
| 358 | Content: types.Message{ |
| 359 | Role: types.RoleUser, |
| 360 | Content: input, |
| 361 | }, |
| 362 | }) |
| 363 | |
| 364 | // Send to agent |
| 365 | fmt.Println() |
| 366 | if err := ag.Send(ctx, input); err != nil { |
| 367 | printColored(useColor, colorYellow, "Error: %s\n", err) |
| 368 | continue |
| 369 | } |
| 370 | |
| 371 | // Wait for response to complete |
| 372 | waitForCompletion(ctx, ag) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // handleCommand handles slash commands |
no test coverage detected