(cfgPath *string)
| 176 | } |
| 177 | |
| 178 | func replayCmd(cfgPath *string) *cobra.Command { |
| 179 | var mode, idsFile string |
| 180 | cmd := &cobra.Command{ |
| 181 | Use: "replay [request_id ...]", |
| 182 | Short: "Replay ledger request(s) via internal/replay (no HTTP API on gateway)", |
| 183 | Long: "With one request_id and no --ids-file, prints a single indented AIResponse JSON. With multiple IDs or --ids-file, prints one JSON object per line (NDJSON) with request_id, response or error.", |
| 184 | RunE: func(cmd *cobra.Command, args []string) error { |
| 185 | var ids []string |
| 186 | if strings.TrimSpace(idsFile) != "" { |
| 187 | f, err := os.Open(idsFile) |
| 188 | if err != nil { |
| 189 | return err |
| 190 | } |
| 191 | defer f.Close() |
| 192 | sc := bufio.NewScanner(f) |
| 193 | for sc.Scan() { |
| 194 | line := strings.TrimSpace(sc.Text()) |
| 195 | if line == "" || strings.HasPrefix(line, "#") { |
| 196 | continue |
| 197 | } |
| 198 | ids = append(ids, line) |
| 199 | } |
| 200 | if err := sc.Err(); err != nil { |
| 201 | return err |
| 202 | } |
| 203 | } |
| 204 | ids = append(ids, args...) |
| 205 | if len(ids) == 0 { |
| 206 | return fmt.Errorf("provide at least one request_id or use --ids-file") |
| 207 | } |
| 208 | |
| 209 | cfg, err := config.Load(*cfgPath) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | st, err := requests.NewFromConfig(cfg) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | if st == nil { |
| 218 | return fmt.Errorf("ledger is disabled; set ledger.enabled=true in config") |
| 219 | } |
| 220 | defer st.Close() |
| 221 | adapterMap := buildAdapterMap(cfg) |
| 222 | ret := retrieval.FromConfig(cfg) |
| 223 | deps := replay.NewDependenciesFromConfig(cfg, adapterMap, ret) |
| 224 | |
| 225 | m := replay.Mode(strings.ToLower(strings.TrimSpace(mode))) |
| 226 | if m != replay.ModeExact && m != replay.ModeCurrent { |
| 227 | return fmt.Errorf("mode must be exact or current") |
| 228 | } |
| 229 | ctx := cmd.Context() |
| 230 | if len(ids) == 1 && idsFile == "" { |
| 231 | resp, err := replay.Replay(ctx, cfg, st, ids[0], m, deps) |
| 232 | if err != nil { |
| 233 | return err |
| 234 | } |
| 235 | enc := json.NewEncoder(os.Stdout) |
no test coverage detected