Class: Legion::CLI::CaptureCommand

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/knowledge_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/legion/cli/knowledge_command.rb', line 94

def self.exit_on_failure?
  true
end

Instance Method Details

#commitObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/legion/cli/knowledge_command.rb', line 102

def commit
  log_line = `git log -1 --format='%H %s' 2>/dev/null`.strip
  diff_stat = `git diff HEAD~1 --stat 2>/dev/null`.strip

  if log_line.empty?
    formatter.warn('No git commit found')
    return
  end

  sha, *subject_parts = log_line.split
  subject = subject_parts.join(' ')
  content = "Git commit: #{sha}\nSubject: #{subject}\n\nDiff stat:\n#{diff_stat}"
  tags    = %w[git commit knowledge-capture]

  result = api_post('/api/knowledge/ingest', content: content, tags: tags, source: "git:#{sha}")

  out = formatter
  if options[:json]
    out.json(result)
  elsif result[:success]
    out.success("Captured commit #{sha[0, 8]}: #{subject}")
  else
    out.warn("Capture failed: #{result[:error]}")
  end
end

#sessionObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/legion/cli/knowledge_command.rb', line 129

def session
  input = $stdin.gets(nil) if $stdin.ready? rescue nil # rubocop:disable Style/RescueModifier
  input = input.to_s.strip

  if input.empty?
    formatter.warn('No session input provided (pipe text to stdin)')
    return
  end

  repo = `git rev-parse --show-toplevel 2>/dev/null`.strip.split('/').last
  content = "Session note (#{::Time.now.strftime('%Y-%m-%d')}):\n\n#{input}"
  tags    = ['session', 'knowledge-capture', ::Time.now.strftime('%Y-%m-%d')]
  tags   << "repo:#{repo}" unless repo.empty?

  result = api_post('/api/knowledge/ingest',
                    content: content, tags: tags, source: "session:#{::Time.now.iso8601}")

  out = formatter
  if options[:json]
    out.json(result)
  elsif result[:success]
    out.success('Session captured')
  else
    out.warn("Capture failed: #{result[:error]}")
  end
end

#transcriptObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/legion/cli/knowledge_command.rb', line 160

def transcript
  session_id = options[:session_id] || ENV.fetch('CLAUDE_SESSION_ID', nil)
  cwd        = options[:cwd] || ENV.fetch('CLAUDE_CWD', nil) || ::Dir.pwd

  unless session_id
    formatter.warn('No session ID provided (set CLAUDE_SESSION_ID or --session-id)')
    return
  end

  jsonl_path = resolve_transcript_path(session_id, cwd)
  unless jsonl_path && ::File.exist?(jsonl_path)
    formatter.warn("Transcript not found: #{jsonl_path || 'could not resolve path'}")
    return
  end

  turns = extract_turns(jsonl_path)
  if turns.empty?
    formatter.warn('No conversation turns found in transcript')
    return
  end

  repo      = `git -C #{Shellwords.escape(cwd)} rev-parse --show-toplevel 2>/dev/null`.strip.split('/').last
  base_tags = ['claude-code', 'transcript', "session:#{session_id}", ::Time.now.strftime('%Y-%m-%d')]
  base_tags << "repo:#{repo}" unless repo.to_s.empty?

  ingested = 0
  turns.first(options[:max_chunks]).each_with_index do |turn, idx|
    content = format_turn(turn, idx + 1)
    next if content.strip.empty?

    result = api_post('/api/knowledge/ingest',
                      content: content,
                      tags:    base_tags + ["turn:#{idx + 1}"],
                      source:  "claude-code:#{session_id}:turn-#{idx + 1}")
    ingested += 1 if result[:success]
  end

  out = formatter
  if options[:json]
    out.json(success: true, session_id: session_id, turns: turns.size, ingested: ingested)
  else
    out.success("Captured #{ingested}/#{[turns.size, options[:max_chunks]].min} turns from session #{session_id[0, 8]}")
  end
end