Class: Legate::CLI::SessionCommands

Inherits:
BaseCommand show all
Defined in:
lib/legate/cli/session_commands.rb

Overview

CLI commands for session management

Instance Method Summary collapse

Methods inherited from BaseCommand

#tree

Instance Method Details

#delete(session_id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/legate/cli/session_commands.rb', line 73

def delete(session_id)
  session_service = Legate::SessionService::InMemory.new

  # Check if session exists first
  session = session_service.get_session(session_id: session_id)
  unless session
    say "Session '#{session_id}' not found.", :red
    return
  end

  # Confirm deletion
  if yes?("Are you sure you want to delete session '#{session_id}'? (y/n)")
    if session_service.delete_session(session_id: session_id)
      say "Session '#{session_id}' deleted successfully.", :green
    else
      say "Failed to delete session '#{session_id}'.", :red
    end
  else
    say 'Deletion cancelled.', :yellow
  end
end

#execute(agent_name, task) ⇒ Object



99
100
101
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
127
128
129
130
131
132
133
134
135
# File 'lib/legate/cli/session_commands.rb', line 99

def execute(agent_name, task)
  # First, check if the agent exists in the GlobalDefinitionRegistry
  name_sym = agent_name.to_sym
  agent_definition = Legate::GlobalDefinitionRegistry.find(name_sym)

  unless agent_definition
    say "Error: Agent definition '#{agent_name}' not found.", :red
    exit(1)
  end

  # Use in-memory session service
  session_service = Legate::SessionService::InMemory.new
  session_id = options[:session_id]
  legate_session = nil

  if session_id
    legate_session = session_service.get_session(session_id: session_id)
    if legate_session
      say "Using existing session: #{session_id}", :cyan
    else
      say "Warning: Session ID '#{session_id}' provided but not found. Starting a new session.", :yellow
      session_id = nil # Force creation below
    end
  end

  unless legate_session
    # Create a new session
    legate_session = session_service.create_session(app_name: agent_name, user_id: options[:user_id])
    session_id = legate_session.id
    say "Started new session: #{session_id}", :cyan
  end

  # Now execute the task using the agent_commands implementation
  agent_commands = Legate::CLI::AgentCommands.new
  agent_commands.invoke(:execute, [agent_name, task],
                        { session_id: session_id, session_service: session_service })
end

#list(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legate/cli/session_commands.rb', line 17

def list(*args)
  # Parse positional arguments if provided
  app_name = options[:app_name] || args[0]
  user_id = options[:user_id] || args[1]

  session_service = Legate::SessionService::InMemory.new
  sessions = session_service.list_sessions(app_name: app_name, user_id: user_id)

  if sessions.empty?
    say "No sessions found#{app_name ? " for app '#{app_name}'" : ''}#{user_id ? " and user '#{user_id}'" : ''}.",
        :yellow
    return
  end

  say "Found #{sessions.length} session(s):", :bold
  sessions.each do |session|
    created_at = session.created_at.strftime('%Y-%m-%d %H:%M:%S')
    say "  ID: #{session.id}", :cyan
    say "    App: #{session.app_name}"
    say "    User: #{session.user_id}"
    say "    Created: #{created_at}"
    say "    Events: #{session.events.length}"
    say ''
  end
end

#show(session_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/legate/cli/session_commands.rb', line 44

def show(session_id)
  session_service = Legate::SessionService::InMemory.new
  session = session_service.get_session(session_id: session_id)

  unless session
    say "Session '#{session_id}' not found.", :red
    return
  end

  created_at = session.created_at.strftime('%Y-%m-%d %H:%M:%S')
  say 'Session Details:', :bold
  say "  ID: #{session.id}", :cyan
  say "  App: #{session.app_name}"
  say "  User: #{session.user_id}"
  say "  Created: #{created_at}"
  say "  Events: #{session.events.length}"

  if session.events.empty?
    say "\nNo events in this session.", :yellow
    return
  end

  say "\nEvents:", :bold
  session.events.each_with_index do |event, index|
    say "  #{index + 1}. [#{event.role}] #{event.content.inspect}"
  end
end