agent-chat 1.0.3 → 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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +120 -0
  3. data/LICENSE +21 -0
  4. data/README.md +196 -0
  5. data/config.ru +1 -6
  6. data/exe/agent-chat +3 -2
  7. data/exe/agent-chat-web +3 -6
  8. data/lib/agent_chat/chat/draft.rb +24 -0
  9. data/lib/agent_chat/chat/entrance.rb +34 -0
  10. data/lib/agent_chat/chat/missing_ids.rb +12 -0
  11. data/lib/agent_chat/chat.rb +56 -0
  12. data/lib/agent_chat/cli/arrival_notices.rb +29 -0
  13. data/lib/agent_chat/cli/command_line.rb +37 -0
  14. data/lib/agent_chat/cli/commands.rb +51 -0
  15. data/lib/agent_chat/cli/flag_values.rb +49 -0
  16. data/lib/agent_chat/cli/help.rb +62 -0
  17. data/lib/agent_chat/cli/json_output.rb +50 -0
  18. data/lib/agent_chat/cli/lookup.rb +24 -0
  19. data/lib/agent_chat/cli/notice.rb +10 -0
  20. data/lib/agent_chat/cli/parse.rb +17 -0
  21. data/lib/agent_chat/cli/request.rb +9 -0
  22. data/lib/agent_chat/cli/request_builder.rb +41 -0
  23. data/lib/agent_chat/cli/runner.rb +75 -0
  24. data/lib/agent_chat/cli/stream.rb +37 -0
  25. data/lib/agent_chat/cli/text_output.rb +48 -0
  26. data/lib/agent_chat/cli/tokenizer.rb +66 -0
  27. data/lib/agent_chat/cli/vocabulary.rb +25 -0
  28. data/lib/agent_chat/cli/wording.rb +42 -0
  29. data/lib/agent_chat/global_id.rb +10 -0
  30. data/lib/agent_chat/message.rb +6 -11
  31. data/lib/agent_chat/outcome.rb +4 -0
  32. data/lib/agent_chat/problem.rb +15 -0
  33. data/lib/agent_chat/room/arrival.rb +9 -0
  34. data/lib/agent_chat/room/directory.rb +27 -0
  35. data/lib/agent_chat/room/location.rb +21 -0
  36. data/lib/agent_chat/room/name.rb +19 -0
  37. data/lib/agent_chat/room/opener.rb +63 -0
  38. data/lib/agent_chat/room/schema.rb +50 -0
  39. data/lib/agent_chat/room/sqlite.rb +8 -0
  40. data/lib/agent_chat/room/store.rb +87 -0
  41. data/lib/agent_chat/spelling.rb +43 -0
  42. data/lib/agent_chat/version.rb +1 -1
  43. data/lib/agent_chat/web/app.rb +47 -18
  44. data/lib/agent_chat/web/post_body.rb +33 -0
  45. data/lib/agent_chat.rb +2 -8
  46. data/public/css/form.css +3 -3
  47. data/public/css/replies.css +110 -0
  48. data/public/css/style.css +1 -0
  49. data/public/index.html +31 -3
  50. data/public/js/feed/message_feed.js +24 -0
  51. data/public/js/feed/message_view.js +41 -0
  52. data/public/js/feed/reply_link.js +24 -0
  53. data/public/js/messages.js +19 -34
  54. data/public/js/page.js +13 -0
  55. data/public/js/reply_chips.js +21 -0
  56. data/public/js/reply_selection.js +23 -0
  57. metadata +67 -47
  58. data/lib/agent_chat/argument_parser.rb +0 -20
  59. data/lib/agent_chat/cli_adapter.rb +0 -82
  60. data/lib/agent_chat/message_formatter.rb +0 -14
  61. data/lib/agent_chat/persistence/database.rb +0 -112
  62. data/lib/agent_chat/persistence/file_resolver.rb +0 -14
  63. data/lib/agent_chat/persistence.rb +0 -19
  64. data/lib/agent_chat/service/main.rb +0 -70
  65. data/lib/agent_chat/web/room_discovery_service.rb +0 -18
  66. data/lib/agent_chat/web/service_factory.rb +0 -13
  67. data/public/js/message_feed.js +0 -48
@@ -0,0 +1,49 @@
1
+ require_relative 'parse'
2
+ require_relative 'vocabulary'
3
+ require_relative 'wording'
4
+
5
+ module AgentChat
6
+ module CLI
7
+ class FlagValues
8
+ LIST_FLAG = '--reply-to'.freeze
9
+
10
+ def initialize(command, uses)
11
+ @command = command
12
+ @uses = uses
13
+ end
14
+
15
+ def collect
16
+ given = @uses.group_by(&:flag).transform_values { |uses| uses.map(&:value) }
17
+ conflicts = given.select { |flag, values| flag != LIST_FLAG && values.uniq.size > 1 }
18
+ return Parse.rejected(conflicting(conflicts)) unless conflicts.empty?
19
+
20
+ settle(given.to_h { |flag, values| [flag, merge(flag, values)] })
21
+ end
22
+
23
+ private
24
+
25
+ def conflicting(conflicts)
26
+ flag, values = conflicts.first
27
+ Wording.conflicting_values(flag, values.uniq)
28
+ end
29
+
30
+ def merge(flag, values)
31
+ flag == LIST_FLAG ? values.flat_map { |value| value.split(',') } : values.first
32
+ end
33
+
34
+ def settle(values)
35
+ reading_all = values.key?('--all')
36
+ notices = reading_all && values.key?('--consumer') ? [Wording.ignored('--consumer', 'is not used with --all')] : []
37
+ kept = reading_all ? values.except('--consumer') : values
38
+ missing = required(reading_all).reject { |flag| kept.key?(flag) }
39
+ return Parse.rejected(Wording.missing_flag(@command, missing.first), notices) unless missing.empty?
40
+
41
+ Parse.accepted(kept, notices)
42
+ end
43
+
44
+ def required(reading_all)
45
+ reading_all ? Vocabulary.required(@command) - ['--consumer'] : Vocabulary.required(@command)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,62 @@
1
+ module AgentChat
2
+ module CLI
3
+ HELP = <<~HELP.freeze
4
+ agent-chat sends and reads messages in shared chat rooms, for programs,
5
+ scripts, agents and people.
6
+
7
+ Usage:
8
+ agent-chat <command> [options]
9
+
10
+ Commands:
11
+ send Send a message. The content is read from standard input.
12
+ Prints the id the new message was given.
13
+ receive Print the messages this consumer has not seen yet, then exit.
14
+ stream Like receive, but keeps running and prints new messages as
15
+ they arrive. Stop it with Ctrl-C.
16
+ show Print the messages with the given ids, in the order given:
17
+ agent-chat show --room <name> <id> [<id>...]
18
+
19
+ Options:
20
+ --room <name> Chat room name. Every command needs it.
21
+ --author <name> Who the message is from (send).
22
+ --consumer <name> Reader name. Each consumer keeps its own read
23
+ position (receive, stream).
24
+ --reply-to <id,...> Ids of the messages this one answers (send).
25
+ Repeat the flag or separate ids with commas.
26
+ --all Print the whole history instead of unread
27
+ messages, without moving any read position
28
+ (receive, stream).
29
+ --json Structured output, one JSON object per line.
30
+ Recommended for scripts and agents.
31
+ -h, --help Show this help.
32
+ -v, --version Show the version.
33
+
34
+ JSON output (--json)
35
+ Every message is one line, the same for receive, stream and show:
36
+ {"id":"7xKqR2m","author":"Alice","timestamp":"2026-11-22T13:30:00Z",
37
+ "content":"Hello","reply_to":["3fGhJ9k"]}
38
+ receive prints nothing when there is nothing new.
39
+ send prints {"status":"accepted","assigned_id":"7xKqR2m"} and adds
40
+ "room_created":true when the message created the room.
41
+ A refused request prints {"status":"rejected","error":{"code":"...",
42
+ "message":"..."}} on standard output.
43
+ Notes and warnings from receive, stream and show go to standard error
44
+ as {"info":{...}} or {"warning":{...}} lines. For send and for refused
45
+ requests they appear in a "warnings" list instead.
46
+
47
+ A misspelt flag or command one edit away from exactly one known name is
48
+ read as that name, with a warning saying so. Anything less certain is
49
+ refused.
50
+
51
+ Exit status is 0 on success, 1 when a request is refused (an unknown
52
+ message id, for example) and 2 when the command line cannot be read.
53
+
54
+ Examples:
55
+ echo 'Hello!' | agent-chat send --room general --author Alice
56
+ echo 'Agreed' | agent-chat send --json --room general --author Bob --reply-to 7xKqR2m
57
+ agent-chat receive --json --room general --consumer Bob
58
+ agent-chat stream --json --room general --consumer Bob
59
+ agent-chat show --json --room general 7xKqR2m
60
+ HELP
61
+ end
62
+ end
@@ -0,0 +1,50 @@
1
+ require 'json'
2
+ require_relative 'notice'
3
+ require_relative '../message'
4
+
5
+ module AgentChat
6
+ module CLI
7
+ class JsonOutput
8
+ def initialize(stdout:, stderr:)
9
+ @stdout = stdout
10
+ @stderr = stderr
11
+ end
12
+
13
+ def messages(messages)
14
+ messages.each { |message| @stdout.puts(JSON.generate(message.as_json)) }
15
+ @stdout.flush
16
+ end
17
+
18
+ def notices(notices)
19
+ notices.each { |notice| @stderr.puts(JSON.generate({ notice.level => notice.problem.as_json })) }
20
+ end
21
+
22
+ def posted(id, notices)
23
+ created = notices.find(&:room_created?)
24
+ envelope = { status: 'accepted', assigned_id: id, **room_fields(created), **warnings(notices) }
25
+ @stdout.puts(JSON.generate(envelope))
26
+ end
27
+
28
+ def rejected(problem, notices)
29
+ @stdout.puts(JSON.generate({ status: 'rejected', error: problem.as_json, **warnings(notices) }))
30
+ end
31
+
32
+ def version(number)
33
+ @stdout.puts(JSON.generate({ version: number }))
34
+ end
35
+
36
+ private
37
+
38
+ def room_fields(created)
39
+ return {} if created.nil?
40
+
41
+ { room_created: true, **created.problem.details }
42
+ end
43
+
44
+ def warnings(notices)
45
+ warnings = notices.select(&:warning?)
46
+ warnings.empty? ? {} : { warnings: warnings.map { |notice| notice.problem.as_json } }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../spelling'
2
+
3
+ module AgentChat
4
+ module CLI
5
+ module Lookup
6
+ Exact = Data.define(:word)
7
+ Interpreted = Data.define(:word)
8
+ Unknown = Data.define(:suggestions)
9
+
10
+ def self.find(word, known)
11
+ return Exact.new(word) if known.include?(word)
12
+
13
+ close = within(1, word, known)
14
+ return Interpreted.new(close.first) if close.one?
15
+
16
+ Unknown.new(close.empty? ? within(2, word, known) : close)
17
+ end
18
+
19
+ def self.within(edits, word, known)
20
+ known.select { |candidate| Spelling.distance(word, candidate) <= edits }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ require_relative '../problem'
2
+
3
+ module AgentChat
4
+ module CLI
5
+ Notice = Data.define(:level, :problem) do
6
+ def warning? = level == :warning
7
+ def room_created? = problem.code == :room_created
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ require_relative '../outcome'
2
+
3
+ module AgentChat
4
+ module CLI
5
+ Parse = Data.define(:outcome, :notices) do
6
+ def self.accepted(value, notices = []) = new(Accepted.new(value), notices)
7
+ def self.rejected(problem, notices = []) = new(Rejected.new(problem), notices)
8
+
9
+ def and_then
10
+ return self if outcome.is_a?(Rejected)
11
+
12
+ following = yield outcome.value
13
+ Parse.new(following.outcome, notices + following.notices)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module AgentChat
2
+ module CLI
3
+ Request = Data.define(:command, :room, :author, :consumer, :reply_to, :ids, :all, :json) do
4
+ def self.special(command, json:)
5
+ new(command:, room: nil, author: nil, consumer: nil, reply_to: [], ids: [], all: false, json:)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'flag_values'
2
+ require_relative 'request'
3
+
4
+ module AgentChat
5
+ module CLI
6
+ class RequestBuilder
7
+ def initialize(command, tokens)
8
+ @command = command
9
+ @tokens = tokens
10
+ end
11
+
12
+ def build
13
+ applicable, ignored = @tokens.flags.partition { |use| Vocabulary.applies?(@command, use.flag) }
14
+ notices = ignored.map(&:flag).uniq.map { |flag| Wording.ignored(flag, "does not apply to #{@command}") }
15
+ parse = FlagValues.new(@command, applicable).collect.and_then { |values| finish(values) }
16
+ Parse.new(parse.outcome, notices + parse.notices)
17
+ end
18
+
19
+ private
20
+
21
+ def finish(values)
22
+ arguments = @tokens.positionals.drop(1)
23
+ return ids(arguments).and_then { |ids| Parse.accepted(request(values, ids)) } if @command == 'show'
24
+ return Parse.rejected(Wording.unexpected_argument(@command, arguments.first)) unless arguments.empty?
25
+
26
+ Parse.accepted(request(values, []))
27
+ end
28
+
29
+ def ids(arguments)
30
+ ids = arguments.flat_map { |argument| argument.split(',') }
31
+ ids.empty? ? Parse.rejected(Wording.missing_ids) : Parse.accepted(ids)
32
+ end
33
+
34
+ def request(values, ids)
35
+ Request.new(command: @command.to_sym, room: values['--room'], author: values['--author'],
36
+ consumer: values['--consumer'], reply_to: values.fetch('--reply-to', []), ids:,
37
+ all: values.fetch('--all', false), json: values.fetch('--json', false))
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,75 @@
1
+ require 'tmpdir'
2
+ require_relative 'command_line'
3
+ require_relative 'arrival_notices'
4
+ require_relative 'commands'
5
+ require_relative 'help'
6
+ require_relative 'json_output'
7
+ require_relative 'stream'
8
+ require_relative 'text_output'
9
+ require_relative '../chat/entrance'
10
+ require_relative '../version'
11
+
12
+ module AgentChat
13
+ module CLI
14
+ Terminal = Data.define(:stdin, :stdout, :stderr)
15
+
16
+ class Runner
17
+ OK = 0
18
+ REFUSED = 1
19
+ UNREADABLE = 2
20
+
21
+ def initialize(terminal, root: Dir.tmpdir, pause: -> { sleep 1 })
22
+ @terminal = terminal
23
+ @root = root
24
+ @pause = pause
25
+ end
26
+
27
+ def run(args)
28
+ command_line = CommandLine.new(args)
29
+ output = output_for(command_line.json_requested?)
30
+ respond(command_line.parse, output)
31
+ rescue Interrupt, Errno::EPIPE
32
+ OK
33
+ end
34
+
35
+ private
36
+
37
+ def respond(parse, output)
38
+ notices = parse.notices.map { |problem| Notice.new(:warning, problem) }
39
+ return UNREADABLE.tap { output.rejected(parse.outcome.problem, notices) } if parse.outcome.is_a?(Rejected)
40
+
41
+ dispatch(parse.outcome.value, notices, output)
42
+ end
43
+
44
+ def output_for(json)
45
+ streams = { stdout: @terminal.stdout, stderr: @terminal.stderr }
46
+ json ? JsonOutput.new(**streams) : TextOutput.new(**streams)
47
+ end
48
+
49
+ def dispatch(request, notices, output)
50
+ case request.command
51
+ when :help then OK.tap { @terminal.stdout.puts(HELP) }
52
+ when :version then OK.tap { output.version(VERSION) }
53
+ else enter(request, notices, output)
54
+ end
55
+ end
56
+
57
+ def enter(request, notices, output)
58
+ outcome = Chat::Entrance.new(@root).enter(request.room)
59
+ return REFUSED.tap { output.rejected(outcome.problem, notices) } if outcome.is_a?(Rejected)
60
+
61
+ visit = outcome.value
62
+ command(request.command, visit.chat, output).run(request, notices + ArrivalNotices.for(visit, request.room))
63
+ end
64
+
65
+ def command(name, chat, output)
66
+ case name
67
+ when :send then Commands::Send.new(chat:, output:, stdin: @terminal.stdin)
68
+ when :receive then Commands::Receive.new(chat:, output:)
69
+ when :show then Commands::Show.new(chat:, output:)
70
+ when :stream then Commands::Stream.new(chat:, output:, pause: @pause)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,37 @@
1
+ module AgentChat
2
+ module CLI
3
+ module Commands
4
+ class Stream
5
+ def initialize(chat:, output:, pause:)
6
+ @chat = chat
7
+ @output = output
8
+ @pause = pause
9
+ end
10
+
11
+ def run(request, notices)
12
+ @output.notices(notices)
13
+ request.all ? follow_everything : follow_unread(request.consumer)
14
+ end
15
+
16
+ private
17
+
18
+ def follow_unread(consumer)
19
+ loop do
20
+ @output.messages(@chat.unread(consumer))
21
+ @pause.call
22
+ end
23
+ end
24
+
25
+ def follow_everything
26
+ last_id = nil
27
+ loop do
28
+ batch = @chat.history_after(last_id)
29
+ @output.messages(batch)
30
+ last_id = batch.last.id unless batch.empty?
31
+ @pause.call
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'notice'
2
+ require_relative '../message'
3
+
4
+ module AgentChat
5
+ module CLI
6
+ class TextOutput
7
+ LABELS = { info: 'note', warning: 'warning' }.freeze
8
+
9
+ def initialize(stdout:, stderr:)
10
+ @stdout = stdout
11
+ @stderr = stderr
12
+ end
13
+
14
+ def messages(messages)
15
+ return if messages.empty?
16
+
17
+ @stdout.puts(messages.map { |message| block(message) }.join("\n\n\n\n"))
18
+ @stdout.flush
19
+ end
20
+
21
+ def notices(notices)
22
+ notices.each { |notice| @stderr.puts("#{LABELS.fetch(notice.level)}: #{notice.problem.message}") }
23
+ end
24
+
25
+ def posted(id, notices)
26
+ notices(notices)
27
+ @stdout.puts(id)
28
+ end
29
+
30
+ def rejected(problem, notices)
31
+ notices(notices)
32
+ @stderr.puts("error: #{problem.message}")
33
+ end
34
+
35
+ def version(number)
36
+ @stdout.puts("agent-chat #{number}")
37
+ end
38
+
39
+ private
40
+
41
+ def block(message)
42
+ time = message.timestamp.localtime.strftime('%Y-%m-%d %H:%M:%S')
43
+ replies = message.reply_to.empty? ? '' : " | re: #{message.reply_to.join(', ')}"
44
+ "<<< #{message.author} | #{time} | #{message.id}#{replies} >>>\n#{message.content}"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,66 @@
1
+ require_relative 'lookup'
2
+ require_relative 'parse'
3
+ require_relative 'vocabulary'
4
+ require_relative 'wording'
5
+
6
+ module AgentChat
7
+ module CLI
8
+ FlagUse = Data.define(:flag, :value)
9
+ Tokens = Data.define(:flags, :positionals)
10
+
11
+ class Tokenizer
12
+ def initialize(args)
13
+ @remaining = args.dup
14
+ @flags = []
15
+ @positionals = []
16
+ @notices = []
17
+ end
18
+
19
+ def tokenize
20
+ until @remaining.empty?
21
+ problems = take(@remaining.shift)
22
+ return Parse.rejected(problems.first, @notices) unless problems.empty?
23
+ end
24
+ Parse.accepted(Tokens.new(flags: @flags, positionals: @positionals), @notices)
25
+ end
26
+
27
+ private
28
+
29
+ def take(token)
30
+ return take_flag(*token.split('=', 2)) if token.start_with?('-')
31
+
32
+ @positionals << token
33
+ []
34
+ end
35
+
36
+ def take_flag(given, joined_value = nil)
37
+ match = Lookup.find(Vocabulary.flag_name(given), Vocabulary::FLAGS)
38
+ return [Wording.unknown_flag(given, match.suggestions)] if match.is_a?(Lookup::Unknown)
39
+
40
+ @notices << Wording.interpreted(:interpreted_flag, given, match.word) unless given == match.word
41
+ Vocabulary.takes_value?(match.word) ? take_value(match.word, joined_value) : take_switch(match.word, joined_value)
42
+ end
43
+
44
+ def take_value(flag, joined_value)
45
+ value = joined_value || next_value
46
+ return [Wording.missing_value(flag)] if value.nil?
47
+
48
+ @flags << FlagUse.new(flag:, value:)
49
+ []
50
+ end
51
+
52
+ def next_value
53
+ return nil if @remaining.empty? || @remaining.first.start_with?('-')
54
+
55
+ @remaining.shift
56
+ end
57
+
58
+ def take_switch(flag, joined_value)
59
+ return [Wording.unexpected_value(flag)] unless joined_value.nil?
60
+
61
+ @flags << FlagUse.new(flag:, value: true)
62
+ []
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,25 @@
1
+ module AgentChat
2
+ module CLI
3
+ module Vocabulary
4
+ VALUE_FLAGS = %w[--room --author --consumer --reply-to].freeze
5
+ SWITCHES = %w[--all --json].freeze
6
+ FLAGS = (VALUE_FLAGS + SWITCHES).freeze
7
+ HELP = %w[-h --help].freeze
8
+ VERSION = %w[-v --version].freeze
9
+
10
+ READING = { flags: %w[--room --consumer --all --json], required: %w[--room --consumer] }.freeze
11
+ COMMANDS = {
12
+ 'send' => { flags: %w[--room --author --reply-to --json], required: %w[--room --author] },
13
+ 'receive' => READING,
14
+ 'stream' => READING,
15
+ 'show' => { flags: %w[--room --json], required: %w[--room] }
16
+ }.freeze
17
+
18
+ def self.takes_value?(flag) = VALUE_FLAGS.include?(flag)
19
+ def self.applies?(command, flag) = COMMANDS.fetch(command)[:flags].include?(flag)
20
+ def self.required(command) = COMMANDS.fetch(command)[:required]
21
+ def self.flag_name(given) = "--#{given.sub(/\A-+/, '').downcase.tr('_', '-')}"
22
+ def self.command_names = COMMANDS.keys
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ require_relative '../problem'
2
+
3
+ module AgentChat
4
+ module CLI
5
+ module Wording
6
+ COMMAND_LIST = 'Commands: send, receive, stream, show.'.freeze
7
+ HELP_HINT = 'Run agent-chat --help for usage.'.freeze
8
+
9
+ def self.interpreted(code, given, interpreted_as)
10
+ Problem.new(code, "Read #{given} as #{interpreted_as}", given:, interpreted_as:)
11
+ end
12
+
13
+ def self.unknown_flag(flag, suggestions)
14
+ Problem.new(:unknown_flag, "Unknown flag #{flag}.#{did_you_mean(suggestions)} #{HELP_HINT}", flag:, **listed(suggestions))
15
+ end
16
+
17
+ def self.unknown_command(command, suggestions)
18
+ Problem.new(:unknown_command, "Unknown command #{command}.#{did_you_mean(suggestions)} #{COMMAND_LIST}",
19
+ command:, **listed(suggestions))
20
+ end
21
+
22
+ def self.missing_command = Problem.new(:missing_command, "No command given. #{COMMAND_LIST}")
23
+ def self.missing_value(flag) = Problem.new(:missing_value, "#{flag} needs a value", flag:)
24
+ def self.unexpected_value(flag) = Problem.new(:unexpected_value, "#{flag} takes no value", flag:)
25
+ def self.missing_flag(command, flag) = Problem.new(:missing_flag, "#{command} needs #{flag}", flag:)
26
+ def self.missing_ids = Problem.new(:missing_argument, 'show needs at least one message id')
27
+ def self.ignored(flag, reason) = Problem.new(:ignored_flag, "#{flag} #{reason}; ignored it", flag:)
28
+
29
+ def self.unexpected_argument(command, argument)
30
+ extra = command == 'send' ? '; the message content is read from standard input' : ''
31
+ Problem.new(:unexpected_argument, "#{command} takes no argument #{argument}#{extra}", argument:)
32
+ end
33
+
34
+ def self.conflicting_values(flag, values)
35
+ Problem.new(:conflicting_values, "#{flag} was given different values: #{values.join(', ')}", flag:, values:)
36
+ end
37
+
38
+ def self.did_you_mean(suggestions) = suggestions.empty? ? '' : " Did you mean #{suggestions.join(' or ')}?"
39
+ def self.listed(suggestions) = suggestions.empty? ? {} : { suggestions: }
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,10 @@
1
+ module AgentChat
2
+ module GlobalId
3
+ ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.freeze
4
+ LENGTH = 7
5
+
6
+ def self.generate(random = Random)
7
+ Array.new(LENGTH) { ALPHABET[random.rand(ALPHABET.length)] }.join
8
+ end
9
+ end
10
+ end
@@ -1,15 +1,10 @@
1
- module AgentChat
2
- class Message
3
- attr_reader :author, :content, :timestamp
4
-
5
- def initialize(author:, content:, timestamp: Time.now)
6
- @author = author
7
- @content = content
8
- @timestamp = timestamp
9
- end
1
+ require 'json'
2
+ require 'time'
10
3
 
11
- def to_h
12
- { author: @author, content: @content, timestamp: @timestamp }
4
+ module AgentChat
5
+ Message = Data.define(:id, :author, :content, :timestamp, :reply_to) do
6
+ def as_json
7
+ { id:, author:, timestamp: timestamp.getutc.iso8601, content:, reply_to: }
13
8
  end
14
9
  end
15
10
  end
@@ -0,0 +1,4 @@
1
+ module AgentChat
2
+ Accepted = Data.define(:value)
3
+ Rejected = Data.define(:problem)
4
+ end
@@ -0,0 +1,15 @@
1
+ module AgentChat
2
+ class Problem
3
+ attr_reader :code, :message, :details
4
+
5
+ def initialize(code, message, **details)
6
+ @code = code
7
+ @message = message
8
+ @details = details
9
+ end
10
+
11
+ def as_json
12
+ { code: code.to_s, message:, **details }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module AgentChat
2
+ module Room
3
+ Arrival = Data.define(:kind, :backup_path) do
4
+ def self.existing = new(kind: :existing, backup_path: nil)
5
+ def self.created = new(kind: :created, backup_path: nil)
6
+ def self.reset(backup_path) = new(kind: :reset, backup_path:)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'location'
2
+ require_relative '../spelling'
3
+
4
+ module AgentChat
5
+ module Room
6
+ class Directory
7
+ def initialize(root)
8
+ @root = root
9
+ end
10
+
11
+ def location(name)
12
+ Location.new(root: @root, name:)
13
+ end
14
+
15
+ def names
16
+ rooms = File.join(@root, 'agent-chat', 'rooms')
17
+ return [] unless Dir.exist?(rooms)
18
+
19
+ Dir.children(rooms).select { |name| File.exist?(location(name).database) }.sort
20
+ end
21
+
22
+ def names_near(name)
23
+ names.select { |other| Spelling.one_edit_apart?(name, other) }
24
+ end
25
+ end
26
+ end
27
+ end