|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/safedep/dry/log" |
| 10 | + "github.com/safedep/gryph/core/events" |
| 11 | + "github.com/safedep/gryph/tui" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func NewCatCmd() *cobra.Command { |
| 16 | + var format string |
| 17 | + |
| 18 | + cmd := &cobra.Command{ |
| 19 | + Use: "cat <event-id> [event-id...]", |
| 20 | + Short: "Show full details of one or more events", |
| 21 | + Long: `Show full details of one or more events. |
| 22 | +
|
| 23 | +Displays all fields including payload, diff content, raw event, |
| 24 | +and conversation context. Accepts full UUIDs or ID prefixes.`, |
| 25 | + Example: ` gryph cat a1b2c3d4 |
| 26 | + gryph cat a1b2c3d4-e5f6-7890-abcd-ef1234567890 |
| 27 | + gryph cat a1b2c3d4 f5e6d7c8 --format json |
| 28 | + gryph cat a1b2c3d4 --format jsonl`, |
| 29 | + Args: cobra.MinimumNArgs(1), |
| 30 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + app, err := loadApp() |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + app.Presenter = tui.NewPresenter(getFormat(format), tui.PresenterOptions{ |
| 39 | + Writer: cmd.OutOrStdout(), |
| 40 | + UseColors: app.Config.ShouldUseColors(), |
| 41 | + }) |
| 42 | + |
| 43 | + if err := app.InitStore(ctx); err != nil { |
| 44 | + return ErrDatabase("failed to open database", err) |
| 45 | + } |
| 46 | + defer func() { |
| 47 | + err := app.Close() |
| 48 | + if err != nil { |
| 49 | + log.Errorf("failed to close app: %w", err) |
| 50 | + } |
| 51 | + }() |
| 52 | + |
| 53 | + views := make([]*tui.EventDetailView, 0, len(args)) |
| 54 | + for _, idArg := range args { |
| 55 | + event, err := resolveEvent(ctx, app, idArg) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + views = append(views, eventToDetailView(event)) |
| 60 | + } |
| 61 | + |
| 62 | + return app.Presenter.RenderEventDetails(views) |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + cmd.Flags().StringVar(&format, "format", "table", "output format: table, json, jsonl, csv") |
| 67 | + |
| 68 | + return cmd |
| 69 | +} |
| 70 | + |
| 71 | +func resolveEvent(ctx context.Context, app *App, idArg string) (*events.Event, error) { |
| 72 | + eventID, err := uuid.Parse(idArg) |
| 73 | + if err != nil { |
| 74 | + e, err := app.Store.GetEventByPrefix(ctx, idArg) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("event not found: %s", idArg) |
| 77 | + } |
| 78 | + if e == nil { |
| 79 | + return nil, fmt.Errorf("event not found: %s", idArg) |
| 80 | + } |
| 81 | + return e, nil |
| 82 | + } |
| 83 | + |
| 84 | + event, err := app.Store.GetEvent(ctx, eventID) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("failed to get event: %w", err) |
| 87 | + } |
| 88 | + if event == nil { |
| 89 | + return nil, fmt.Errorf("event not found: %s", idArg) |
| 90 | + } |
| 91 | + return event, nil |
| 92 | +} |
| 93 | + |
| 94 | +func eventToDetailView(e *events.Event) *tui.EventDetailView { |
| 95 | + view := &tui.EventDetailView{ |
| 96 | + ID: e.ID.String(), |
| 97 | + SessionID: e.SessionID.String(), |
| 98 | + AgentSessionID: e.AgentSessionID, |
| 99 | + Sequence: e.Sequence, |
| 100 | + Timestamp: e.Timestamp, |
| 101 | + DurationMs: e.DurationMs, |
| 102 | + AgentName: e.AgentName, |
| 103 | + AgentDisplayName: getAgentDisplayName(e.AgentName), |
| 104 | + AgentVersion: e.AgentVersion, |
| 105 | + WorkingDirectory: e.WorkingDirectory, |
| 106 | + ActionType: string(e.ActionType), |
| 107 | + ActionDisplay: e.ActionType.DisplayName(), |
| 108 | + ToolName: e.ToolName, |
| 109 | + ResultStatus: string(e.ResultStatus), |
| 110 | + ErrorMessage: e.ErrorMessage, |
| 111 | + IsSensitive: e.IsSensitive, |
| 112 | + DiffContent: e.DiffContent, |
| 113 | + RawEvent: e.RawEvent, |
| 114 | + ConvContext: e.ConversationContext, |
| 115 | + } |
| 116 | + |
| 117 | + switch e.ActionType { |
| 118 | + case events.ActionFileRead: |
| 119 | + if p, err := e.GetFileReadPayload(); err == nil && p != nil { |
| 120 | + view.Payload = p |
| 121 | + } |
| 122 | + case events.ActionFileWrite: |
| 123 | + if p, err := e.GetFileWritePayload(); err == nil && p != nil { |
| 124 | + view.Payload = p |
| 125 | + } |
| 126 | + case events.ActionCommandExec: |
| 127 | + if p, err := e.GetCommandExecPayload(); err == nil && p != nil { |
| 128 | + view.Payload = p |
| 129 | + } |
| 130 | + case events.ActionToolUse: |
| 131 | + if p, err := e.GetToolUsePayload(); err == nil && p != nil { |
| 132 | + view.Payload = p |
| 133 | + } |
| 134 | + default: |
| 135 | + if len(e.Payload) > 0 { |
| 136 | + var raw any |
| 137 | + if json.Unmarshal(e.Payload, &raw) == nil { |
| 138 | + view.Payload = raw |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return view |
| 144 | +} |
0 commit comments