(cfgPath *string)
| 101 | } |
| 102 | |
| 103 | func traceCmd(cfgPath *string) *cobra.Command { |
| 104 | return &cobra.Command{ |
| 105 | Use: "trace [request_id]", |
| 106 | Short: "Print ledger record and steps as JSON", |
| 107 | Args: cobra.ExactArgs(1), |
| 108 | RunE: func(cmd *cobra.Command, args []string) error { |
| 109 | cfg, err := config.Load(*cfgPath) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | st, err := requests.NewFromConfig(cfg) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | if st == nil { |
| 118 | return fmt.Errorf("ledger is disabled; set ledger.enabled=true in config") |
| 119 | } |
| 120 | defer st.Close() |
| 121 | ctx := context.Background() |
| 122 | id := args[0] |
| 123 | row, err := st.GetRequest(ctx, id) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | steps, err := st.ListSteps(ctx, id) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | out := map[string]any{ |
| 132 | "request": map[string]any{ |
| 133 | "request_id": row.RequestID, |
| 134 | "trace_id": row.TraceID, |
| 135 | "request_type": row.RequestType, |
| 136 | "tenant_id": row.TenantID, |
| 137 | "task_type": row.TaskType, |
| 138 | "priority": row.Priority, |
| 139 | "pipeline_ref": row.PipelineRef, |
| 140 | "input_json": json.RawMessage(row.InputJSON), |
| 141 | "context_json": json.RawMessage(row.ContextJSON), |
| 142 | "ai_request_json": json.RawMessage(row.AIRequestJSON), |
| 143 | "policy_snapshot": json.RawMessage(row.PolicySnapshot), |
| 144 | "status": row.Status, |
| 145 | "selected_backend": row.SelectedBackend, |
| 146 | "route_reason": row.RouteReason, |
| 147 | "created_at": row.CreatedAt, |
| 148 | "updated_at": row.UpdatedAt, |
| 149 | }, |
| 150 | "steps": stepsToJSON(steps), |
| 151 | } |
| 152 | enc := json.NewEncoder(os.Stdout) |
| 153 | enc.SetIndent("", " ") |
| 154 | return enc.Encode(out) |
| 155 | }, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func stepsToJSON(steps []requests.StepRow) []map[string]any { |
| 160 | out := make([]map[string]any, 0, len(steps)) |
no test coverage detected