agent-chat 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +120 -0
- data/LICENSE +21 -0
- data/README.md +196 -0
- data/config.ru +1 -6
- data/exe/agent-chat +3 -2
- data/exe/agent-chat-web +4 -7
- data/lib/agent_chat/chat/draft.rb +24 -0
- data/lib/agent_chat/chat/entrance.rb +34 -0
- data/lib/agent_chat/chat/missing_ids.rb +12 -0
- data/lib/agent_chat/chat.rb +56 -0
- data/lib/agent_chat/cli/arrival_notices.rb +29 -0
- data/lib/agent_chat/cli/command_line.rb +37 -0
- data/lib/agent_chat/cli/commands.rb +51 -0
- data/lib/agent_chat/cli/flag_values.rb +49 -0
- data/lib/agent_chat/cli/help.rb +62 -0
- data/lib/agent_chat/cli/json_output.rb +50 -0
- data/lib/agent_chat/cli/lookup.rb +24 -0
- data/lib/agent_chat/cli/notice.rb +10 -0
- data/lib/agent_chat/cli/parse.rb +17 -0
- data/lib/agent_chat/cli/request.rb +9 -0
- data/lib/agent_chat/cli/request_builder.rb +41 -0
- data/lib/agent_chat/cli/runner.rb +75 -0
- data/lib/agent_chat/cli/stream.rb +37 -0
- data/lib/agent_chat/cli/text_output.rb +48 -0
- data/lib/agent_chat/cli/tokenizer.rb +66 -0
- data/lib/agent_chat/cli/vocabulary.rb +25 -0
- data/lib/agent_chat/cli/wording.rb +42 -0
- data/lib/agent_chat/global_id.rb +10 -0
- data/lib/agent_chat/message.rb +6 -11
- data/lib/agent_chat/outcome.rb +4 -0
- data/lib/agent_chat/problem.rb +15 -0
- data/lib/agent_chat/room/arrival.rb +9 -0
- data/lib/agent_chat/room/directory.rb +27 -0
- data/lib/agent_chat/room/location.rb +21 -0
- data/lib/agent_chat/room/name.rb +19 -0
- data/lib/agent_chat/room/opener.rb +63 -0
- data/lib/agent_chat/room/schema.rb +50 -0
- data/lib/agent_chat/room/sqlite.rb +8 -0
- data/lib/agent_chat/room/store.rb +87 -0
- data/lib/agent_chat/spelling.rb +43 -0
- data/lib/agent_chat/version.rb +1 -1
- data/lib/agent_chat/web/app.rb +53 -18
- data/lib/agent_chat/web/post_body.rb +33 -0
- data/lib/agent_chat.rb +2 -8
- data/public/css/form.css +3 -3
- data/public/css/replies.css +110 -0
- data/public/css/style.css +1 -0
- data/public/index.html +31 -3
- data/public/js/feed/message_feed.js +24 -0
- data/public/js/feed/message_view.js +41 -0
- data/public/js/feed/reply_link.js +24 -0
- data/public/js/messages.js +19 -34
- data/public/js/page.js +13 -0
- data/public/js/reply_chips.js +21 -0
- data/public/js/reply_selection.js +23 -0
- metadata +70 -46
- data/lib/agent_chat/argument_parser.rb +0 -20
- data/lib/agent_chat/cli_adapter.rb +0 -82
- data/lib/agent_chat/message_formatter.rb +0 -14
- data/lib/agent_chat/persistence/database.rb +0 -112
- data/lib/agent_chat/persistence/file_resolver.rb +0 -14
- data/lib/agent_chat/persistence.rb +0 -19
- data/lib/agent_chat/service/main.rb +0 -70
- data/lib/agent_chat/web/room_discovery_service.rb +0 -18
- data/lib/agent_chat/web/service_factory.rb +0 -13
- data/public/js/message_feed.js +0 -48
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module AgentChat
|
|
2
|
+
module Room
|
|
3
|
+
class Location
|
|
4
|
+
attr_reader :name
|
|
5
|
+
|
|
6
|
+
def initialize(root:, name:)
|
|
7
|
+
@root = root
|
|
8
|
+
@name = name
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def folder = File.join(@root, 'agent-chat', 'rooms', @name)
|
|
12
|
+
def database = File.join(folder, 'room.db')
|
|
13
|
+
def lock = File.join(folder, 'room.lock')
|
|
14
|
+
def database_files = [database, "#{database}-wal", "#{database}-shm"]
|
|
15
|
+
|
|
16
|
+
def backup_folder(at:)
|
|
17
|
+
File.join(@root, 'agent-chat', 'backups', at.getutc.strftime('%Y%m%dT%H%M%SZ'), @name)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require_relative '../problem'
|
|
2
|
+
|
|
3
|
+
module AgentChat
|
|
4
|
+
module Room
|
|
5
|
+
module Name
|
|
6
|
+
FORBIDDEN = %r{[/\\\x00-\x1f]}
|
|
7
|
+
|
|
8
|
+
def self.problems_with(text)
|
|
9
|
+
return [] if acceptable?(text)
|
|
10
|
+
|
|
11
|
+
[Problem.new(:invalid_room, "Room name #{text.inspect} cannot be used: it must be a plain folder name")]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.acceptable?(text)
|
|
15
|
+
!text.strip.empty? && !FORBIDDEN.match?(text) && !%w[. ..].include?(text)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require_relative 'sqlite'
|
|
3
|
+
require_relative 'arrival'
|
|
4
|
+
require_relative 'directory'
|
|
5
|
+
require_relative 'schema'
|
|
6
|
+
|
|
7
|
+
module AgentChat
|
|
8
|
+
module Room
|
|
9
|
+
class Opener
|
|
10
|
+
def initialize(location, clock: -> { Time.now })
|
|
11
|
+
@location = location
|
|
12
|
+
@clock = clock
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def open
|
|
16
|
+
FileUtils.mkdir_p(@location.folder)
|
|
17
|
+
File.open(@location.lock, File::RDWR | File::CREAT) do |lock|
|
|
18
|
+
lock.flock(File::LOCK_EX)
|
|
19
|
+
open_while_locked
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def open_while_locked
|
|
26
|
+
arrival = arrive
|
|
27
|
+
db = connect
|
|
28
|
+
Schema.apply(db)
|
|
29
|
+
[db, arrival]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def arrive
|
|
33
|
+
return Arrival.created unless File.exist?(@location.database)
|
|
34
|
+
|
|
35
|
+
state = inspect_schema
|
|
36
|
+
return Arrival.reset(back_up) if state == :outdated
|
|
37
|
+
|
|
38
|
+
state == :blank ? Arrival.created : Arrival.existing
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def inspect_schema
|
|
42
|
+
db = SQLite3::Database.new(@location.database)
|
|
43
|
+
Schema.state(db)
|
|
44
|
+
ensure
|
|
45
|
+
db&.close
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def back_up
|
|
49
|
+
folder = @location.backup_folder(at: @clock.call)
|
|
50
|
+
FileUtils.mkdir_p(folder)
|
|
51
|
+
@location.database_files.select { |file| File.exist?(file) }.each { |file| File.rename(file, File.join(folder, File.basename(file))) }
|
|
52
|
+
folder
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def connect
|
|
56
|
+
db = SQLite3::Database.new(@location.database)
|
|
57
|
+
db.busy_timeout = 5000
|
|
58
|
+
db.execute('PRAGMA journal_mode = WAL')
|
|
59
|
+
db
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require_relative 'sqlite'
|
|
2
|
+
|
|
3
|
+
module AgentChat
|
|
4
|
+
module Room
|
|
5
|
+
module Schema
|
|
6
|
+
VERSION = 1
|
|
7
|
+
|
|
8
|
+
TABLES = [
|
|
9
|
+
<<~SQL,
|
|
10
|
+
CREATE TABLE IF NOT EXISTS messages (
|
|
11
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
12
|
+
id TEXT NOT NULL UNIQUE,
|
|
13
|
+
author TEXT NOT NULL,
|
|
14
|
+
content TEXT NOT NULL,
|
|
15
|
+
timestamp TEXT NOT NULL
|
|
16
|
+
)
|
|
17
|
+
SQL
|
|
18
|
+
<<~SQL,
|
|
19
|
+
CREATE TABLE IF NOT EXISTS replies (
|
|
20
|
+
message_seq INTEGER NOT NULL REFERENCES messages(seq),
|
|
21
|
+
position INTEGER NOT NULL,
|
|
22
|
+
parent_id TEXT NOT NULL,
|
|
23
|
+
PRIMARY KEY (message_seq, position)
|
|
24
|
+
)
|
|
25
|
+
SQL
|
|
26
|
+
<<~SQL
|
|
27
|
+
CREATE TABLE IF NOT EXISTS read_positions (
|
|
28
|
+
consumer TEXT PRIMARY KEY,
|
|
29
|
+
last_seq INTEGER NOT NULL
|
|
30
|
+
)
|
|
31
|
+
SQL
|
|
32
|
+
].freeze
|
|
33
|
+
|
|
34
|
+
def self.apply(db)
|
|
35
|
+
db.transaction(:immediate) do
|
|
36
|
+
TABLES.each { |sql| db.execute(sql) }
|
|
37
|
+
db.execute("PRAGMA user_version = #{VERSION}")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.state(db)
|
|
42
|
+
tables = db.get_first_value("SELECT count(*) FROM sqlite_master WHERE type = 'table'")
|
|
43
|
+
return :blank if tables.zero?
|
|
44
|
+
|
|
45
|
+
version = db.get_first_value('PRAGMA user_version')
|
|
46
|
+
version == VERSION ? :current : :outdated
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
require 'sqlite3'
|
|
2
|
+
|
|
3
|
+
# sqlite3 looks up the UTF-16 encodings on its first query that binds a
|
|
4
|
+
# string, and Ruby loads them on demand. A Ctrl-C that lands during that load,
|
|
5
|
+
# inside the extension's C code, crashes Ruby with a [BUG] report instead of
|
|
6
|
+
# raising Interrupt. Loading them here, before any query, closes that window.
|
|
7
|
+
require 'enc/utf_16le'
|
|
8
|
+
require 'enc/utf_16be'
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'set'
|
|
3
|
+
require 'time'
|
|
4
|
+
require_relative 'schema'
|
|
5
|
+
require_relative '../message'
|
|
6
|
+
|
|
7
|
+
module AgentChat
|
|
8
|
+
module Room
|
|
9
|
+
class IdTaken < StandardError; end
|
|
10
|
+
|
|
11
|
+
class Store
|
|
12
|
+
SELECT_MESSAGES = <<~SQL.freeze
|
|
13
|
+
SELECT m.id, m.author, m.content, m.timestamp,
|
|
14
|
+
(SELECT json_group_array(parent_id ORDER BY position) FROM replies WHERE message_seq = m.seq)
|
|
15
|
+
FROM messages m
|
|
16
|
+
SQL
|
|
17
|
+
SEQ_OF_ID = 'COALESCE((SELECT seq FROM messages WHERE id = ?), 0)'.freeze
|
|
18
|
+
IN_JSON_ARRAY = 'IN (SELECT value FROM json_each(?))'.freeze
|
|
19
|
+
|
|
20
|
+
def initialize(db)
|
|
21
|
+
@db = db
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def insert(message)
|
|
25
|
+
write(message)
|
|
26
|
+
rescue SQLite3::ConstraintException => e
|
|
27
|
+
raise IdTaken, message.id if e.message.include?('messages.id')
|
|
28
|
+
|
|
29
|
+
raise
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def messages_after(id)
|
|
33
|
+
rows = @db.execute("#{SELECT_MESSAGES} WHERE m.seq > #{SEQ_OF_ID} ORDER BY m.seq", [id])
|
|
34
|
+
rows.map { |row| to_message(row) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def messages_with_ids(ids)
|
|
38
|
+
rows = @db.execute("#{SELECT_MESSAGES} WHERE m.id #{IN_JSON_ARRAY} ORDER BY m.seq", [JSON.generate(ids)])
|
|
39
|
+
rows.map { |row| to_message(row) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def existing_ids(ids)
|
|
43
|
+
rows = @db.execute("SELECT id FROM messages WHERE id #{IN_JSON_ARRAY}", [JSON.generate(ids)])
|
|
44
|
+
rows.flatten.to_set
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def read_position(consumer)
|
|
48
|
+
@db.get_first_value(<<~SQL, [consumer])
|
|
49
|
+
SELECT m.id FROM read_positions r JOIN messages m ON m.seq = r.last_seq WHERE r.consumer = ?
|
|
50
|
+
SQL
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def move_read_position(consumer, to:)
|
|
54
|
+
@db.execute(<<~SQL, [consumer, to])
|
|
55
|
+
INSERT INTO read_positions (consumer, last_seq) SELECT ?, seq FROM messages WHERE id = ?
|
|
56
|
+
ON CONFLICT (consumer) DO UPDATE SET last_seq = excluded.last_seq
|
|
57
|
+
SQL
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def write(message)
|
|
63
|
+
@db.transaction do
|
|
64
|
+
insert_message(message)
|
|
65
|
+
insert_replies(@db.last_insert_row_id, message.reply_to)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def insert_message(message)
|
|
70
|
+
@db.execute('INSERT INTO messages (id, author, content, timestamp) VALUES (?, ?, ?, ?)',
|
|
71
|
+
[message.id, message.author, message.content, message.timestamp.getutc.iso8601])
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def insert_replies(seq, parent_ids)
|
|
75
|
+
parent_ids.each_with_index do |parent_id, position|
|
|
76
|
+
@db.execute('INSERT INTO replies (message_seq, position, parent_id) VALUES (?, ?, ?)',
|
|
77
|
+
[seq, position, parent_id])
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def to_message(row)
|
|
82
|
+
id, author, content, timestamp, reply_to = row
|
|
83
|
+
Message.new(id:, author:, content:, timestamp: Time.iso8601(timestamp), reply_to: JSON.parse(reply_to))
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module AgentChat
|
|
2
|
+
module Spelling
|
|
3
|
+
def self.distance(given, known)
|
|
4
|
+
EditTable.new(given, known).distance
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def self.one_edit_apart?(given, known)
|
|
8
|
+
distance(given, known) == 1
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class EditTable
|
|
12
|
+
def initialize(given, known)
|
|
13
|
+
@given = given
|
|
14
|
+
@known = known
|
|
15
|
+
@cells = Array.new(given.length + 1) { |row| Array.new(known.length + 1) { |column| edge(row, column) } }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def distance
|
|
19
|
+
(1..@given.length).each do |row|
|
|
20
|
+
(1..@known.length).each { |column| fill(row, column) }
|
|
21
|
+
end
|
|
22
|
+
@cells[@given.length][@known.length]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def edge(row, column)
|
|
28
|
+
[row, column].min.zero? ? row + column : nil
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def fill(row, column)
|
|
32
|
+
changed = @given[row - 1] == @known[column - 1] ? 0 : 1
|
|
33
|
+
best = [@cells[row - 1][column] + 1, @cells[row][column - 1] + 1, @cells[row - 1][column - 1] + changed].min
|
|
34
|
+
best = [best, @cells[row - 2][column - 2] + 1].min if swapped?(row, column)
|
|
35
|
+
@cells[row][column] = best
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def swapped?(row, column)
|
|
39
|
+
row > 1 && column > 1 && @given[row - 1] == @known[column - 2] && @given[row - 2] == @known[column - 1]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
data/lib/agent_chat/version.rb
CHANGED
data/lib/agent_chat/web/app.rb
CHANGED
|
@@ -1,38 +1,73 @@
|
|
|
1
|
+
require 'json'
|
|
1
2
|
require 'sinatra/base'
|
|
2
|
-
require '
|
|
3
|
+
require 'tmpdir'
|
|
4
|
+
require_relative 'post_body'
|
|
5
|
+
require_relative '../chat/entrance'
|
|
3
6
|
|
|
4
7
|
module AgentChat
|
|
5
8
|
module Web
|
|
6
9
|
class App < Sinatra::Base
|
|
7
|
-
|
|
10
|
+
UNPROCESSABLE = %i[unknown_reply_to unknown_id].freeze
|
|
8
11
|
|
|
9
|
-
set :public_folder, File.expand_path('
|
|
12
|
+
set :public_folder, File.expand_path('../../../public', __dir__)
|
|
13
|
+
set :chat_root, Dir.tmpdir
|
|
14
|
+
|
|
15
|
+
# Sinatra serves the public folder but does not map / to its index, and
|
|
16
|
+
# the README and the executable's help both point people at the bare URL.
|
|
17
|
+
get '/' do
|
|
18
|
+
send_file File.join(settings.public_folder, 'index.html')
|
|
19
|
+
end
|
|
10
20
|
|
|
11
21
|
get '/api/rooms' do
|
|
12
|
-
|
|
22
|
+
respond(200, Room::Directory.new(settings.chat_root).names)
|
|
13
23
|
end
|
|
14
24
|
|
|
15
25
|
get '/api/rooms/:room/messages' do
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
with_visit do |visit|
|
|
27
|
+
messages = visit.chat.history
|
|
28
|
+
visit.chat.mark_read(params[:consumer], through: messages.last.id) if params[:consumer] && !messages.empty?
|
|
29
|
+
respond(200, messages.map(&:as_json))
|
|
30
|
+
end
|
|
19
31
|
end
|
|
20
32
|
|
|
21
33
|
get '/api/rooms/:room/messages/new' do
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
return refuse(Problem.new(:missing_parameter, 'consumer is required', parameter: 'consumer')) if params[:consumer].to_s.empty?
|
|
35
|
+
|
|
36
|
+
with_visit { |visit| respond(200, visit.chat.unread(params[:consumer]).map(&:as_json)) }
|
|
25
37
|
end
|
|
26
38
|
|
|
27
39
|
post '/api/rooms/:room/messages' do
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
return refuse(PostBody.unsupported(request.media_type), 415) unless request.media_type == 'application/json'
|
|
41
|
+
|
|
42
|
+
body = PostBody.parse(request.body.read)
|
|
43
|
+
return refuse(body.problem) if body.is_a?(Rejected)
|
|
44
|
+
|
|
45
|
+
with_visit { |visit| posted(visit, visit.chat.post(**body.value)) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
helpers do
|
|
49
|
+
def with_visit
|
|
50
|
+
outcome = Chat::Entrance.new(settings.chat_root).enter(params[:room])
|
|
51
|
+
outcome.is_a?(Rejected) ? refuse(outcome.problem) : yield(outcome.value)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def posted(visit, outcome)
|
|
55
|
+
return refuse(outcome.problem) if outcome.is_a?(Rejected)
|
|
56
|
+
|
|
57
|
+
created = visit.arrival.kind == :created ? { room_created: true } : {}
|
|
58
|
+
similar = visit.similar_rooms.empty? ? {} : { similar_rooms: visit.similar_rooms }
|
|
59
|
+
respond(201, { status: 'accepted', assigned_id: outcome.value, **created, **similar })
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def respond(code, data)
|
|
63
|
+
status code
|
|
64
|
+
content_type :json
|
|
65
|
+
JSON.generate(data)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def refuse(problem, code = UNPROCESSABLE.include?(problem.code) ? 422 : 400)
|
|
69
|
+
respond(code, { status: 'rejected', error: problem.as_json })
|
|
70
|
+
end
|
|
36
71
|
end
|
|
37
72
|
end
|
|
38
73
|
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require_relative '../outcome'
|
|
3
|
+
require_relative '../problem'
|
|
4
|
+
|
|
5
|
+
module AgentChat
|
|
6
|
+
module Web
|
|
7
|
+
module PostBody
|
|
8
|
+
def self.parse(text)
|
|
9
|
+
data = JSON.parse(text)
|
|
10
|
+
return Rejected.new(Problem.new(:invalid_json, 'The body must be a JSON object')) unless data.is_a?(Hash)
|
|
11
|
+
|
|
12
|
+
fields(data)
|
|
13
|
+
rescue JSON::ParserError => e
|
|
14
|
+
Rejected.new(Problem.new(:invalid_json, "The body is not valid JSON: #{e.message}"))
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.fields(data)
|
|
18
|
+
reply_to = data.fetch('reply_to', [])
|
|
19
|
+
return Rejected.new(invalid_reply_to) unless reply_to.is_a?(Array) && reply_to.all?(String)
|
|
20
|
+
|
|
21
|
+
Accepted.new({ author: data['author'].to_s, content: data['content'].to_s, reply_to: })
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.unsupported(media_type)
|
|
25
|
+
Problem.new(:unsupported_media_type, "Send the body as application/json, not #{media_type || 'without a Content-Type'}")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.invalid_reply_to
|
|
29
|
+
Problem.new(:invalid_field, 'reply_to must be a list of message ids', field: 'reply_to')
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/lib/agent_chat.rb
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
require_relative 'agent_chat/version'
|
|
2
|
-
require_relative 'agent_chat/
|
|
3
|
-
require_relative 'agent_chat/
|
|
4
|
-
require_relative 'agent_chat/argument_parser'
|
|
5
|
-
require_relative 'agent_chat/persistence'
|
|
6
|
-
require_relative 'agent_chat/cli_adapter'
|
|
7
|
-
require_relative 'agent_chat/service/main'
|
|
8
|
-
require_relative 'agent_chat/web/room_discovery_service'
|
|
9
|
-
require_relative 'agent_chat/web/service_factory'
|
|
2
|
+
require_relative 'agent_chat/chat/entrance'
|
|
3
|
+
require_relative 'agent_chat/cli/runner'
|
|
10
4
|
require_relative 'agent_chat/web/app'
|
data/public/css/form.css
CHANGED
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
color: #666;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
#message-form button {
|
|
66
|
+
#message-form button[type="submit"] {
|
|
67
67
|
padding: 1rem 1.5rem;
|
|
68
68
|
background: #e94560;
|
|
69
69
|
color: white;
|
|
@@ -77,11 +77,11 @@
|
|
|
77
77
|
min-height: 60px;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
#message-form button:hover {
|
|
80
|
+
#message-form button[type="submit"]:hover {
|
|
81
81
|
background: #d63a55;
|
|
82
82
|
transform: translateY(-1px);
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
#message-form button:active {
|
|
85
|
+
#message-form button[type="submit"]:active {
|
|
86
86
|
transform: translateY(0);
|
|
87
87
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
.message-header {
|
|
2
|
+
display: flex;
|
|
3
|
+
align-items: baseline;
|
|
4
|
+
gap: 0.75rem;
|
|
5
|
+
margin-bottom: 0.5rem;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.message-header .message-author {
|
|
9
|
+
margin-bottom: 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.message-id {
|
|
13
|
+
font-family: ui-monospace, Menlo, monospace;
|
|
14
|
+
font-size: 0.75rem;
|
|
15
|
+
color: #666;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.reply-button {
|
|
19
|
+
margin-left: auto;
|
|
20
|
+
background: none;
|
|
21
|
+
border: 1px solid #0f3460;
|
|
22
|
+
border-radius: 6px;
|
|
23
|
+
color: #888;
|
|
24
|
+
font-size: 0.75rem;
|
|
25
|
+
padding: 0.15rem 0.6rem;
|
|
26
|
+
cursor: pointer;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.reply-button:hover {
|
|
30
|
+
color: #e94560;
|
|
31
|
+
border-color: #e94560;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.message-replies {
|
|
35
|
+
display: flex;
|
|
36
|
+
flex-direction: column;
|
|
37
|
+
align-items: flex-start;
|
|
38
|
+
gap: 0.2rem;
|
|
39
|
+
margin-bottom: 0.5rem;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.reply-link {
|
|
43
|
+
background: none;
|
|
44
|
+
border: none;
|
|
45
|
+
padding: 0;
|
|
46
|
+
color: #888;
|
|
47
|
+
font-size: 0.8rem;
|
|
48
|
+
text-align: left;
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.reply-link::before {
|
|
53
|
+
content: "\21B3 ";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.reply-link:hover {
|
|
57
|
+
color: #e94560;
|
|
58
|
+
text-decoration: underline;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.message.highlighted {
|
|
62
|
+
outline: 2px solid #e94560;
|
|
63
|
+
transition: outline-color 0.3s;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
#reply-selection {
|
|
67
|
+
display: flex;
|
|
68
|
+
flex-wrap: wrap;
|
|
69
|
+
gap: 0.5rem;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#reply-selection:not(:empty) {
|
|
73
|
+
margin-bottom: 0.75rem;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.reply-chip {
|
|
77
|
+
display: inline-flex;
|
|
78
|
+
align-items: center;
|
|
79
|
+
gap: 0.4rem;
|
|
80
|
+
background: #0f3460;
|
|
81
|
+
border-radius: 999px;
|
|
82
|
+
padding: 0.2rem 0.4rem 0.2rem 0.75rem;
|
|
83
|
+
font-size: 0.8rem;
|
|
84
|
+
color: #ccc;
|
|
85
|
+
max-width: 100%;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.reply-chip-label {
|
|
89
|
+
overflow: hidden;
|
|
90
|
+
text-overflow: ellipsis;
|
|
91
|
+
white-space: nowrap;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.reply-chip-remove {
|
|
95
|
+
background: none;
|
|
96
|
+
border: none;
|
|
97
|
+
color: #888;
|
|
98
|
+
cursor: pointer;
|
|
99
|
+
font-size: 0.8rem;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.reply-chip-remove:hover {
|
|
103
|
+
color: #e94560;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#post-error:not(:empty) {
|
|
107
|
+
color: #ff8a8a;
|
|
108
|
+
font-size: 0.85rem;
|
|
109
|
+
margin-bottom: 0.75rem;
|
|
110
|
+
}
|
data/public/css/style.css
CHANGED
data/public/index.html
CHANGED
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
<meta charset="UTF-8">
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
6
|
<title>Agent Chat</title>
|
|
7
|
+
<link rel="icon" href="data:,">
|
|
7
8
|
<link rel="stylesheet" href="/css/style.css">
|
|
8
9
|
<script type="importmap">
|
|
9
10
|
{
|
|
10
11
|
"imports": {
|
|
11
|
-
"marked": "https://esm.sh/marked@
|
|
12
|
+
"marked": "https://esm.sh/marked@18.2.1",
|
|
13
|
+
"dompurify": "https://esm.sh/dompurify@3.4.18"
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
</script>
|
|
@@ -30,6 +32,8 @@
|
|
|
30
32
|
<label for="message-author">Posting as</label>
|
|
31
33
|
<input id="message-author" type="text" />
|
|
32
34
|
</div>
|
|
35
|
+
<div id="reply-selection"></div>
|
|
36
|
+
<div id="post-error" role="alert"></div>
|
|
33
37
|
<div class="form-row">
|
|
34
38
|
<textarea id="message-content" placeholder="Type your message..." rows="1"></textarea>
|
|
35
39
|
<button type="submit">Send</button>
|
|
@@ -40,16 +44,35 @@
|
|
|
40
44
|
|
|
41
45
|
<script type="module">
|
|
42
46
|
import { fetchAndRenderRooms } from './js/rooms.js';
|
|
43
|
-
import { fetchAndRenderMessages, fetchAndAppendNewMessages,
|
|
47
|
+
import { fetchAndRenderMessages, fetchAndAppendNewMessages, postMessage } from './js/messages.js';
|
|
48
|
+
import { getConsumer, createKeyDownHandler } from './js/page.js';
|
|
49
|
+
import { ReplySelection } from './js/reply_selection.js';
|
|
50
|
+
import { renderReplySelection } from './js/reply_chips.js';
|
|
44
51
|
|
|
45
52
|
let pollingInterval = null;
|
|
46
53
|
let currentRoom = null;
|
|
54
|
+
const replies = new ReplySelection();
|
|
55
|
+
const replyContainer = document.getElementById('reply-selection');
|
|
56
|
+
const postError = document.getElementById('post-error');
|
|
57
|
+
|
|
58
|
+
function resetReplies() {
|
|
59
|
+
replies.clear();
|
|
60
|
+
renderReplySelection(replies, replyContainer);
|
|
61
|
+
postError.textContent = '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
document.getElementById('messages').addEventListener('reply', (event) => {
|
|
65
|
+
replies.add(event.detail);
|
|
66
|
+
renderReplySelection(replies, replyContainer);
|
|
67
|
+
document.getElementById('message-content').focus();
|
|
68
|
+
});
|
|
47
69
|
|
|
48
70
|
async function onRoomSelected(room) {
|
|
49
71
|
currentRoom = room;
|
|
50
72
|
|
|
51
73
|
// Stop previous polling
|
|
52
74
|
if (pollingInterval) clearInterval(pollingInterval);
|
|
75
|
+
resetReplies();
|
|
53
76
|
|
|
54
77
|
document.getElementById('room-title').textContent = room;
|
|
55
78
|
document.querySelectorAll('#rooms-list li').forEach(li => {
|
|
@@ -78,9 +101,14 @@
|
|
|
78
101
|
const content = document.getElementById('message-content').value;
|
|
79
102
|
if (!content.trim()) return;
|
|
80
103
|
|
|
81
|
-
const count = await postMessage(currentRoom, author, content);
|
|
104
|
+
const { count, error } = await postMessage(currentRoom, author, content, replies.ids);
|
|
82
105
|
document.getElementById('room-title').textContent = `${currentRoom} (${count})`;
|
|
106
|
+
if (error) {
|
|
107
|
+
postError.textContent = error;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
83
110
|
document.getElementById('message-content').value = '';
|
|
111
|
+
resetReplies();
|
|
84
112
|
});
|
|
85
113
|
|
|
86
114
|
document.getElementById('message-content').addEventListener('keydown', createKeyDownHandler(form));
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class MessageFeed {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.messages = [];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
setMessages(messages) {
|
|
7
|
+
this.messages = messages;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
appendMessages(messages) {
|
|
11
|
+
const known = new Set(this.messages.map(m => m.id));
|
|
12
|
+
const added = messages.filter(m => !known.has(m.id));
|
|
13
|
+
this.messages = [...this.messages, ...added];
|
|
14
|
+
return added;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
find(id) {
|
|
18
|
+
return this.messages.find(m => m.id === id);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get count() {
|
|
22
|
+
return this.messages.length;
|
|
23
|
+
}
|
|
24
|
+
}
|