(ctx context.Context, cmd *cli.Command)
| 54 | } |
| 55 | |
| 56 | func run(ctx context.Context, cmd *cli.Command) error { |
| 57 | // 检查 API Key |
| 58 | apiKey := os.Getenv("OPENROUTER_API_KEY") |
| 59 | if apiKey == "" { |
| 60 | return errors.New("需要设置 OPENROUTER_API_KEY 环境变量") |
| 61 | } |
| 62 | |
| 63 | baseURL := os.Getenv("OPENROUTER_BASE_URL") |
| 64 | if baseURL == "" { |
| 65 | baseURL = "https://openrouter.ai/api/v1" |
| 66 | } |
| 67 | |
| 68 | // 获取参数 |
| 69 | prompt := cmd.String("print") |
| 70 | streaming := cmd.Bool("stream") |
| 71 | model := cmd.String("model") |
| 72 | |
| 73 | // 创建 Agent |
| 74 | ag, err := createAgent(apiKey, baseURL, model, streaming) |
| 75 | if err != nil { |
| 76 | return fmt.Errorf("创建 Agent 失败: %w", err) |
| 77 | } |
| 78 | defer func() { _ = ag.Close() }() |
| 79 | |
| 80 | // 非交互模式 |
| 81 | if prompt != "" { |
| 82 | return runOnce(ctx, ag, prompt) |
| 83 | } |
| 84 | |
| 85 | // 交互模式 |
| 86 | return runInteractive(ctx, ag) |
| 87 | } |
| 88 | |
| 89 | // runOnce 非交互模式:执行单次对话并退出 |
| 90 | func runOnce(ctx context.Context, ag *agent.Agent, prompt string) error { |
nothing calls this directly
no test coverage detected