Method: MyAsk::Api::Project#initialize

Defined in:
lib/myask/api/project.rb

#initialize(c) ⇒ Project

Returns a new instance of Project.



5
6
7
8
9
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
74
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
# File 'lib/myask/api/project.rb', line 5

def initialize(c)
  c.desc "Show MyAsk Project Details"
  c.arg_name "project"
  c.command :show do |c|
    c.desc "List projects and select to view"
    c.switch [:l, :list]

    c.flag [:project], desc: "ID of project to display"

    c.action do |global_options, options, args|
      config = global_options
      project = options[:project] || config[:project]

      helper = MyAsk::Api::ProjectHelper.new(global_options, options, args)

      project = if options[:list]
                     helper.project_id_from_selection
                   else
                     args[0] || project
                   end

      helper.show_project_info(project)
    end
  end

  c.desc "Set MyAsk Project"
  c.arg_name "project"
  c.command :set do |c|
    c.desc "Set current MyAsk project"
    c.switch [:l, :list]

    c.flag [:project], desc: "ID of MyAsk Project to display"

    c.action do |global_options, options, args|
      config = global_options
      project = options[:project] || config[:project]

      helper = MyAsk::Api::ProjectHelper.new(global_options, options, args)

      project = if options[:list]
                     helper.project_id_from_selection
                   else
                     args[0] || project
                   end

      helper = MyAsk::ConfigHelper.new(global_options, options.merge(project: project), args)
      response = helper.save_config
    end
  end

  c.desc "Edit MyAsk Project"
  c.arg_name "project"
  c.command :edit do |c|
    c.desc "Edit MyAsk project"
    c.switch [:l, :list]

    c.flag [:project], desc: "ID of MyAsk Project to Edit"

    c.action do |global_options, options, args|
      config = global_options
      project = options[:project] || config[:project]

      helper = MyAsk::Api::ProjectHelper.new(global_options, options, args)

      project = if options[:list]
                     helper.project_id_from_selection
                   else
                     args[0] || project
                   end

      payload = helper.show_project_info(project)

      ai_models = ["gpt-4o-mini", "o3-mini", "o1-mini", "gpt-3.5-turbo", "gpt-4o", "chatgpt-4o-latest"]

      prompt = TTY::Prompt.new

      payload["project"]["name"] = prompt.read_line("Enter project name:", value: payload["project"]["name"]).strip
      payload["project"]["description"] = prompt.read_line("Enter project description:", value: payload["project"]["description"]).strip

      payload["project"]["ai_model"] = prompt.enum_select(
        "Select an AI model for this project:",
        ai_models,
        default: payload["project"]["ai_model"]
      )

      payload["project"]["system_message"] = prompt.read_line(
        "Enter project system_message (optional):",
        value: payload["project"]["metadata"]["system_message"]["content"]
      ).strip

      puts payload

      uri = URI("#{global_options[:api_host]}/api/projects/#{payload["project"]["id"]}")
      response = MyAsk::Api::Helper.make_request("Patch", uri, global_options[:api_key], payload)

      if response.is_a?(Net::HTTPSuccess)
        puts "Project successfully edited:" unless global_options[:json]
        puts JSON.pretty_generate(JSON.parse(response.body))
      else
        raise "Failed to edit project. Response code: #{response.code}, Body: #{response.body}"
      end
    end
  end

  c.desc "Create MyAsk Project"
  c.command :create do |c|
    c.flag [:name], desc: "Project Name"
    c.flag [:description], desc: "Project Description"
    c.flag [:ai_model], desc: "Project AI Model"
    c.flag [:system_message], desc: "Project System Message"

    c.action do |global_options, options, _args|
      ai_models = ["gpt-4o-mini", "o3-mini", "o1-mini", "gpt-3.5-turbo", "gpt-4o", "chatgpt-4o-latest"]

      prompt = TTY::Prompt.new

      project_name = options[:name] || prompt.read_line("Enter project name:")
      project_description = options[:description] || prompt.read_line("Enter project description:")

      project_ai_model = options[:ai_model] || prompt.enum_select(
        "Select an AI model for this project:",
        ai_models
      )

      project_system_message = options[:system_message] || prompt.read_line(
        "Enter project system_message (optional):"
      )

      payload = {
        project: {
          name: project_name.strip,
          description: project_description.strip,
          ai_model: project_ai_model,
          system_message: { role: "developer", content: project_system_message.strip }
        }
      }

      uri = URI("#{global_options[:api_host]}/api/projects")
      response = MyAsk::Api::Helper.make_request("Post", uri, global_options[:api_key], payload)

      if response.is_a?(Net::HTTPSuccess)
        puts "Project created successfully:" unless global_options[:json]
        puts JSON.pretty_generate(JSON.parse(response.body))
      else
        raise "Failed to create project. Response code: #{response.code}, Body: #{response.body}"
      end
    end
  end

  c.desc "Delete MyAsk Project"
  c.command :delete do |c|
    c.desc "List projects and select to view"
    c.switch [:l, :list]

    c.flag [:project], desc: "Project ID"

    c.action do |global_options, options, args|
      helper = MyAsk::Api::ProjectHelper.new(global_options, options, args)

      project = if options[:list]
                     helper.project_id_from_selection
                   else
                     args[0] || project
                   end

      payload = {
        project: {
          id: project
        }
      }

      uri = URI("#{global_options[:api_host]}/api/projects/#{project}")
      response = MyAsk::Api::Helper.make_request("Delete", uri, global_options[:api_key], payload)

      if response.is_a?(Net::HTTPSuccess)
        puts "Project deleted successfully:" unless global_options[:json]
        puts JSON.pretty_generate(JSON.parse(response.body))
      else
        raise "Failed to delete project. Response code: #{response.code}, Body: #{response.body}"
      end
    end
  end

  c
end