runInteractive 交互模式:REPL 循环
(ctx context.Context, ag *agent.Agent)
| 128 | |
| 129 | // runInteractive 交互模式:REPL 循环 |
| 130 | func runInteractive(ctx context.Context, ag *agent.Agent) error { |
| 131 | slog.Info("Agent 创建成功", "id", ag.ID()) |
| 132 | |
| 133 | // 订阅事件 |
| 134 | eventCh := ag.Subscribe([]types.AgentChannel{types.ChannelProgress}, nil) |
| 135 | go handleEvents(eventCh) |
| 136 | |
| 137 | fmt.Println("\n🤖 OpenRouter Agent 演示") |
| 138 | fmt.Println("输入消息与 Agent 对话,输入 'quit' 退出") |
| 139 | fmt.Println(strings.Repeat("-", 50)) |
| 140 | |
| 141 | scanner := bufio.NewScanner(os.Stdin) |
| 142 | for { |
| 143 | fmt.Print("\n> ") |
| 144 | if !scanner.Scan() { |
| 145 | break |
| 146 | } |
| 147 | |
| 148 | input := strings.TrimSpace(scanner.Text()) |
| 149 | if input == "" { |
| 150 | continue |
| 151 | } |
| 152 | if input == "quit" || input == "exit" { |
| 153 | fmt.Println("👋 再见!") |
| 154 | break |
| 155 | } |
| 156 | |
| 157 | result, err := ag.Chat(ctx, input) |
| 158 | if err != nil { |
| 159 | slog.Error("对话失败", "error", err) |
| 160 | continue |
| 161 | } |
| 162 | |
| 163 | // 显示响应文本 |
| 164 | if result.Text != "" { |
| 165 | fmt.Printf("\n%s\n", result.Text) |
| 166 | } |
| 167 | fmt.Printf("[状态: %s]\n", result.Status) |
| 168 | } |
| 169 | |
| 170 | return nil |
| 171 | } |
| 172 | |
| 173 | // createAgent 创建并配置 Agent |
| 174 | func createAgent(apiKey, baseURL, model string, streaming bool) (*agent.Agent, error) { |