MCPcopy Create free account
hub / github.com/astercloud/aster / runInteractive

Function runInteractive

examples/openrouter/main.go:130–171  ·  view source on GitHub ↗

runInteractive 交互模式:REPL 循环

(ctx context.Context, ag *agent.Agent)

Source from the content-addressed store, hash-verified

128
129// runInteractive 交互模式:REPL 循环
130func 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
174func createAgent(apiKey, baseURL, model string, streaming bool) (*agent.Agent, error) {

Callers 1

runFunction · 0.85

Calls 7

handleEventsFunction · 0.85
PrintfMethod · 0.80
InfoMethod · 0.65
IDMethod · 0.65
SubscribeMethod · 0.65
ErrorMethod · 0.65
ChatMethod · 0.45

Tested by

no test coverage detected