neh 0.0.24 → 0.0.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/neh/cli/o.rb +143 -6
- data/lib/neh/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 550819e91c6cd6dc8c57c7fa1cff4698652e77333a7bac1379697ebef5a7b314
|
4
|
+
data.tar.gz: 25526b39861ddd276647f446aacea145743757a254613d0245236a79237c157a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 670d5fa4c6d5188462e66ec1de7f7d9449d1f568762c0da7618b3f37603520d7386c0b4998991ff8237690e59e836c26262d5d76ec41158662b3714e61dcd781
|
7
|
+
data.tar.gz: 6b00589c49832231863c3e2282b9a5e45cd89c24889c11a13dbb6a04c7eb5e4bd1b9ce7157bd682041d03dbe0776eb7edb35d6c2c3b9319050a0a4674cdc3cd7
|
data/lib/neh/cli/o.rb
CHANGED
@@ -1,12 +1,149 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
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
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.
|
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-
|
11
|
+
date: 2024-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|