neh 0.0.23 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57b439f5f249bf9904c1120635eef65b477a8ad769c1f52c7a47b95d48fc6ffc
4
- data.tar.gz: a873810f8083889464e4de53f6f28468e3fa1a589b187542d435eea37f4639ab
3
+ metadata.gz: 550819e91c6cd6dc8c57c7fa1cff4698652e77333a7bac1379697ebef5a7b314
4
+ data.tar.gz: 25526b39861ddd276647f446aacea145743757a254613d0245236a79237c157a
5
5
  SHA512:
6
- metadata.gz: 1863c446c1cddcac3fac7f3bbdd8fd27b392d7c14014daf02639837dad6061058d05d23a4938e432a83007d8c01cc200a554b46635c147e0cac9aa500cd8dbf1
7
- data.tar.gz: 14411df40b0463fe1851364e8157777281164e6b9d1112f1835929f6769f1b86c33d05e50c281509d4c45089281785e03a6eebba54a51a2518b6631c21dd2f8c
6
+ metadata.gz: 670d5fa4c6d5188462e66ec1de7f7d9449d1f568762c0da7618b3f37603520d7386c0b4998991ff8237690e59e836c26262d5d76ec41158662b3714e61dcd781
7
+ data.tar.gz: 6b00589c49832231863c3e2282b9a5e45cd89c24889c11a13dbb6a04c7eb5e4bd1b9ce7157bd682041d03dbe0776eb7edb35d6c2c3b9319050a0a4674cdc3cd7
data/lib/neh/cli/base.rb CHANGED
@@ -11,22 +11,18 @@ require 'pry' if ENV['NEH_DEBUG']
11
11
  require 'active_support/all'
12
12
 
13
13
  class Neh::Cli::Base # rubocop:disable Metrics/ClassLength
14
+ attr_reader :uuid
15
+
14
16
  def initialize(*args, options:)
15
17
  @message = args.join(' ')
16
-
17
18
  @options = options
18
- server_host = ENV.fetch('NEH_SERVER_HOST', 'yoryo.gipcompany.com')
19
- server_port = ENV.fetch('NEH_SERVER_PORT', 443)
20
- @channel = 'LargeLanguageModelQueryChannel'
21
- @uuid = SecureRandom.uuid
22
- url = "ws://#{server_host}:#{server_port}/cable?uuid=#{@uuid}"
23
- @endpoint = Async::HTTP::Endpoint.parse(url)
24
19
  @message_pool = {}
25
20
  @expected_sequence_number = 1
21
+ @uuid = SecureRandom.uuid
26
22
  end
27
23
 
28
24
  def execute
29
- Async::WebSocket::Client.connect(@endpoint, headers: { 'Authorization' => "Bearer #{token}" }) do |connection|
25
+ Async::WebSocket::Client.connect(endpoint, headers: { 'Authorization' => "Bearer #{token}" }) do |connection|
30
26
  while (message = connection.read)
31
27
  parsed_message =
32
28
  JSON.parse(message, symbolize_names: true)
@@ -106,19 +102,23 @@ class Neh::Cli::Base # rubocop:disable Metrics/ClassLength
106
102
  {
107
103
  command: :subscribe,
108
104
  identifier: {
109
- channel: @channel,
110
- uuid: @uuid
105
+ channel:,
106
+ uuid:
111
107
  }.to_json
112
108
  }
113
109
  connection.write(content.to_json)
114
110
  connection.flush
115
111
  end
116
112
 
113
+ def channel
114
+ 'LargeLanguageModelQueryChannel'
115
+ end
116
+
117
117
  def on_subscribed
118
118
  response = http_connection.post("/api/neh/#{command}") do |req|
119
119
  req.body = {
120
120
  message: @message,
121
- uuid: @uuid,
121
+ uuid:,
122
122
  token:
123
123
  }.to_json
124
124
 
@@ -141,13 +141,20 @@ class Neh::Cli::Base # rubocop:disable Metrics/ClassLength
141
141
  end
142
142
 
143
143
  def http_connection
144
- Faraday.new(url: "#{protocol}://#{@endpoint.hostname}:#{@endpoint.port}") do |faraday|
144
+ Faraday.new(url:) do |faraday|
145
145
  faraday.adapter Faraday.default_adapter
146
146
  faraday.headers['Authorization'] = "Bearer #{token}"
147
147
  end
148
148
  end
149
149
 
150
- def protocol
151
- @endpoint.secure? ? 'https' : 'http'
150
+ def endpoint
151
+ Async::HTTP::Endpoint.parse(
152
+ url,
153
+ alpn_protocols: Async::HTTP::Protocol::HTTP11.names #=> ["http/1.1"]
154
+ )
155
+ end
156
+
157
+ def url
158
+ ENV['WORKING_ON_LOCALHOST'].present? ? "http://localhost:6060/cable?uuid=#{uuid}" : "https://yoryo-app.onrender.com/cable?uuid=#{uuid}"
152
159
  end
153
160
  end
data/lib/neh/cli/o.rb CHANGED
@@ -1,12 +1,149 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Neh::Cli
4
- class O < Base
5
- private
3
+ require 'securerandom'
4
+ require 'net/http'
5
+ require 'async'
6
+ require 'async/io/stream'
7
+ require 'async/http/endpoint'
8
+ require 'async/websocket/client'
9
+ require 'faraday'
10
+ require 'pry'
11
+ require 'active_support/all'
6
12
 
7
- def close(connection)
8
- puts ''
9
- super
13
+ class Neh::Cli::O # rubocop:disable Metrics/ClassLength
14
+ attr_reader :uuid
15
+
16
+ def initialize(*args, options:)
17
+ @message = args.join(' ')
18
+ @options = options
19
+ @message_pool = {}
20
+ @expected_sequence_number = 1
21
+ @uuid = SecureRandom.uuid
22
+ end
23
+
24
+ def execute
25
+ Async::WebSocket::Client.connect(endpoint, headers: { 'Authorization' => "Bearer #{token}" }) do |connection|
26
+ while (message = connection.read)
27
+ parsed_message =
28
+ JSON.parse(message, symbolize_names: true)
29
+
30
+ on_receive(connection, parsed_message)
31
+ end
10
32
  end
11
33
  end
34
+
35
+ private
36
+
37
+ def token
38
+ neh_personal_access_token = ENV.fetch('NEH_PERSONAL_ACCESS_TOKEN', nil)
39
+
40
+ puts 'Please set the environment variable NEH_PERSONAL_ACCESS_TOKEN.' if neh_personal_access_token.blank?
41
+
42
+ neh_personal_access_token
43
+ end
44
+
45
+ def on_receive(connection, body)
46
+ if body[:type]
47
+ handle_action_cable_messages(connection, body)
48
+ else
49
+ handle_broadcasted_messages(connection, body[:message])
50
+ end
51
+ end
52
+
53
+ def handle_action_cable_messages(connection, message)
54
+ type = message[:type]
55
+
56
+ case type
57
+ when 'welcome'
58
+ subscribe(connection)
59
+ when 'confirm_subscription'
60
+ on_subscribed
61
+ when 'ping'
62
+ # do nothing
63
+ when 'disconnect'
64
+ puts "Connection has been disconnected. Reason: #{message[:reason]}"
65
+ close(connection)
66
+ else
67
+ puts "Unknown message type: #{type}. Closing connection."
68
+ close(connection)
69
+ end
70
+ end
71
+
72
+ def process_message_in_order
73
+ while @message_pool.key?(@expected_sequence_number)
74
+ print @message_pool.delete(@expected_sequence_number)
75
+ @expected_sequence_number += 1
76
+ end
77
+ end
78
+
79
+ def handle_broadcasted_messages(connection, message)
80
+ type = message[:type]
81
+ body = message[:body]
82
+
83
+ case type
84
+ when 'output'
85
+ sequence_number = message[:sequence_number]
86
+ @message_pool[sequence_number] = body
87
+ process_message_in_order
88
+ when 'error'
89
+ print body
90
+ when 'worker_done'
91
+ close(connection)
92
+ end
93
+ end
94
+
95
+ def close(connection)
96
+ connection.close
97
+ end
98
+
99
+ def subscribe(connection)
100
+ content =
101
+ {
102
+ command: :subscribe,
103
+ identifier: {
104
+ channel: 'LargeLanguageModelQueryChannel',
105
+ uuid:
106
+ }.to_json
107
+ }
108
+ connection.write(content.to_json)
109
+ connection.flush
110
+ end
111
+
112
+ def on_subscribed
113
+ response = http_connection.post("/api/neh/o") do |req|
114
+ req.body = {
115
+ message: @message,
116
+ uuid:,
117
+ token:
118
+ }.to_json
119
+
120
+ req.headers['Content-Type'] = 'application/json'
121
+ end
122
+
123
+ body = JSON.parse response.body
124
+ message = body['message']
125
+ puts message if message.present?
126
+ end
127
+
128
+ def http_connection
129
+ Faraday.new(url: http_url) do |faraday|
130
+ faraday.adapter Faraday.default_adapter
131
+ faraday.headers['Authorization'] = "Bearer #{token}"
132
+ end
133
+ end
134
+
135
+ def endpoint
136
+ Async::HTTP::Endpoint.parse(
137
+ ws_url,
138
+ alpn_protocols: Async::HTTP::Protocol::HTTP11.names #=> ["http/1.1"]
139
+ )
140
+ end
141
+
142
+ def http_url
143
+ ENV['WORKING_ON_LOCALHOST'].present? ? "http://localhost:6060/cable?uuid=#{uuid}" : "https://yoryo-app.onrender.com/cable?uuid=#{uuid}"
144
+ end
145
+
146
+ def ws_url
147
+ ENV['WORKING_ON_LOCALHOST'].present? ? "ws://localhost:6060/cable?uuid=#{uuid}" : "wss://yoryo-app.onrender.com/cable?uuid=#{uuid}" # rubocop:disable Metrics/LineLength
148
+ end
12
149
  end
data/lib/neh/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Neh
4
- VERSION = "0.0.23"
4
+ VERSION = "0.0.25"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Atsushi Ishida
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-24 00:00:00.000000000 Z
11
+ date: 2024-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport