Class: PromptObjects::CLI::MessageCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/cli/message_command.rb

Overview

message command: send a message to a PO and print the response. Routes through a running server when one exists, standalone otherwise.

Instance Method Summary collapse

Instance Method Details

#helpObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/prompt_objects/cli/message_command.rb', line 75

def help
  prog = Helpers.program_name
  puts <<~HELP
    Usage: #{prog} message [options] <environment> <po_name> "message"

    Send a message to a prompt object and print the response.

    Arguments:
      environment  Environment name or path
      po_name      Name of the prompt object to message
      message      The message to send (quoted string)

    Options:
      --json           Output response as JSON
      --events         Also print the event log for this interaction
      --new-thread     Start a new thread for this message
      --session ID     Continue a specific thread ID (session alias)
      --help, -h       Show this help

    If a server is running for the environment, the message is sent via
    HTTP to the running server (and streams to the web UI in real time).
    Otherwise, a standalone runtime is started for the request.

    Examples:
      #{prog} message my-env solver "What patterns do you see?"
      #{prog} message my-env solver "Solve task 1" --json
      #{prog} message my-env solver "Try again" --new-thread
  HELP
end

#run(args) ⇒ Object



10
11
12
13
14
15
16
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
42
43
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
71
72
73
# File 'lib/prompt_objects/cli/message_command.rb', line 10

def run(args)
  options = {
    json: false,
    events: false,
    new_thread: false,
    session_id: nil
  }

  positional = []
  skip_next = false

  args.each_with_index do |arg, i|
    if skip_next
      skip_next = false
      next
    end

    case arg
    when "--json"
      options[:json] = true
    when "--events"
      options[:events] = true
    when "--new-thread"
      options[:new_thread] = true
    when "--session"
      options[:session_id] = args[i + 1]
      skip_next = true
    when "--help", "-h"
      help
      exit 0
    else
      positional << arg
    end
  end

  env_name = positional[0]
  po_name = positional[1]
  message_text = positional[2]

  unless env_name && po_name && message_text
    puts "Error: environment, po_name, and message are required"
    puts "Run '#{Helpers.program_name} message --help' for usage"
    exit 1
  end

  # Resolve environment
  env_path = Helpers.resolve_environment(env_name)
  unless env_path
    $stderr.puts "Error: environment '#{env_name}' not found"
    exit 1
  end

  # Check for running server
  require "prompt_objects/server"
  server_info = PromptObjects::Server.read_server_file(env_path)

  if server_info
    # Send via HTTP to running server
    send_via_http(server_info, po_name, message_text, options)
  else
    # Standalone mode
    send_standalone(env_path, po_name, message_text, options)
  end
end