chat_sdk 1.0.1 → 1.2.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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/chat_sdk/adapter/base.rb +33 -5
  3. data/lib/chat_sdk/adapter/capabilities.rb +1 -1
  4. data/lib/chat_sdk/adapter/invocation.rb +18 -0
  5. data/lib/chat_sdk/ai/conversation_scope.rb +44 -0
  6. data/lib/chat_sdk/ai/converter.rb +16 -5
  7. data/lib/chat_sdk/ai/tool_builder.rb +18 -2
  8. data/lib/chat_sdk/ai/tool_executor.rb +64 -10
  9. data/lib/chat_sdk/ai.rb +2 -2
  10. data/lib/chat_sdk/author.rb +13 -2
  11. data/lib/chat_sdk/cards/actions_context.rb +9 -3
  12. data/lib/chat_sdk/cards/builder.rb +38 -1
  13. data/lib/chat_sdk/cards/node.rb +36 -0
  14. data/lib/chat_sdk/cards/renderer.rb +20 -0
  15. data/lib/chat_sdk/channel.rb +8 -0
  16. data/lib/chat_sdk/chat.rb +35 -2
  17. data/lib/chat_sdk/config.rb +48 -1
  18. data/lib/chat_sdk/dispatcher.rb +280 -33
  19. data/lib/chat_sdk/event_registry.rb +7 -2
  20. data/lib/chat_sdk/event_serializer.rb +192 -0
  21. data/lib/chat_sdk/events/action.rb +3 -2
  22. data/lib/chat_sdk/events/installed.rb +19 -0
  23. data/lib/chat_sdk/events/message_deleted.rb +19 -0
  24. data/lib/chat_sdk/events/message_updated.rb +17 -0
  25. data/lib/chat_sdk/events/uninstalled.rb +19 -0
  26. data/lib/chat_sdk/format/mentions.rb +61 -0
  27. data/lib/chat_sdk/history.rb +151 -0
  28. data/lib/chat_sdk/message.rb +8 -2
  29. data/lib/chat_sdk/modals/builder.rb +16 -0
  30. data/lib/chat_sdk/postable_message.rb +3 -2
  31. data/lib/chat_sdk/state/base.rb +16 -0
  32. data/lib/chat_sdk/state/memory.rb +31 -0
  33. data/lib/chat_sdk/streaming/stream.rb +6 -2
  34. data/lib/chat_sdk/testing/fake_adapter.rb +32 -8
  35. data/lib/chat_sdk/thread.rb +35 -6
  36. data/lib/chat_sdk/version.rb +1 -1
  37. data/lib/chat_sdk/webhook/endpoint.rb +3 -0
  38. data/lib/chat_sdk.rb +2 -2
  39. metadata +10 -1
@@ -5,11 +5,13 @@ module ChatSDK
5
5
  class FakeAdapter < Adapter::Base
6
6
  capabilities :edit_messages, :delete_messages, :ephemeral_messages,
7
7
  :file_uploads, :reactions, :modals, :typing_indicator,
8
- :streaming_edit, :threads, :direct_messages, :message_history
8
+ :streaming_edit, :threads, :direct_messages, :message_history,
9
+ :replies, :read_receipts
9
10
 
10
11
  attr_reader :posted_messages, :edited_messages, :deleted_messages,
11
12
  :ephemeral_messages_sent, :reactions_added, :reactions_removed,
12
- :files_uploaded, :modals_opened, :typing_started, :dm_channels
13
+ :files_uploaded, :modals_opened, :typing_started, :dm_channels,
14
+ :replies_sent, :read_receipts_sent
13
15
 
14
16
  def initialize
15
17
  reset!
@@ -48,13 +50,25 @@ module ChatSDK
48
50
  )
49
51
  end
50
52
 
51
- def edit_message(channel_id:, message_id:, message:)
53
+ def edit_message(channel_id:, message_id:, message:, **_options)
52
54
  record = RecordedCall.new(:edit_message, channel_id: channel_id, message_id: message_id, message: message)
53
55
  @edited_messages << record
54
56
  record
55
57
  end
56
58
 
57
- def delete_message(channel_id:, message_id:)
59
+ def reply_message(channel_id:, message_id:, message:, thread_id: nil)
60
+ record = RecordedCall.new(:reply_message, channel_id: channel_id, message_id: message_id, message: message, thread_id: thread_id)
61
+ @replies_sent << record
62
+ record
63
+ end
64
+
65
+ def mark_as_read(channel_id:, message_id:, thread_id: nil, message: nil)
66
+ record = RecordedCall.new(:mark_as_read, channel_id: channel_id, message_id: message_id, thread_id: thread_id, message: message)
67
+ @read_receipts_sent << record
68
+ record
69
+ end
70
+
71
+ def delete_message(channel_id:, message_id:, **_options)
58
72
  record = RecordedCall.new(:delete_message, channel_id: channel_id, message_id: message_id)
59
73
  @deleted_messages << record
60
74
  record
@@ -72,13 +86,13 @@ module ChatSDK
72
86
  record
73
87
  end
74
88
 
75
- def add_reaction(channel_id:, message_id:, emoji:)
89
+ def add_reaction(channel_id:, message_id:, emoji:, **_options)
76
90
  record = RecordedCall.new(:add_reaction, channel_id: channel_id, message_id: message_id, emoji: emoji)
77
91
  @reactions_added << record
78
92
  record
79
93
  end
80
94
 
81
- def remove_reaction(channel_id:, message_id:, emoji:)
95
+ def remove_reaction(channel_id:, message_id:, emoji:, **_options)
82
96
  record = RecordedCall.new(:remove_reaction, channel_id: channel_id, message_id: message_id, emoji: emoji)
83
97
  @reactions_removed << record
84
98
  record
@@ -94,14 +108,22 @@ module ChatSDK
94
108
  [[], nil]
95
109
  end
96
110
 
111
+ def fetch_channel_messages(channel_id:, cursor: nil, limit: 50)
112
+ [[], nil]
113
+ end
114
+
115
+ def list_threads(channel_id:, cursor: nil, limit: 50)
116
+ [[], nil]
117
+ end
118
+
97
119
  def open_modal(trigger_id:, modal:)
98
120
  record = RecordedCall.new(:open_modal, trigger_id: trigger_id, modal: modal)
99
121
  @modals_opened << record
100
122
  record
101
123
  end
102
124
 
103
- def start_typing(channel_id:, thread_id: nil)
104
- record = RecordedCall.new(:start_typing, channel_id: channel_id, thread_id: thread_id)
125
+ def start_typing(channel_id:, thread_id: nil, message_id: nil)
126
+ record = RecordedCall.new(:start_typing, channel_id: channel_id, thread_id: thread_id, message_id: message_id)
105
127
  @typing_started << record
106
128
  record
107
129
  end
@@ -189,6 +211,8 @@ module ChatSDK
189
211
  @modals_opened = []
190
212
  @typing_started = []
191
213
  @dm_channels = []
214
+ @replies_sent = []
215
+ @read_receipts_sent = []
192
216
  end
193
217
  end
194
218
  end
@@ -2,13 +2,16 @@
2
2
 
3
3
  module ChatSDK
4
4
  class Thread
5
- attr_reader :id, :channel_id, :adapter, :chat
5
+ include Adapter::Invocation
6
6
 
7
- def initialize(id:, channel_id:, adapter:, chat:)
7
+ attr_reader :id, :channel_id, :adapter, :chat, :current_message
8
+
9
+ def initialize(id:, channel_id:, adapter:, chat:, current_message: nil)
8
10
  @id = id
9
11
  @channel_id = channel_id
10
12
  @adapter = adapter
11
13
  @chat = chat
14
+ @current_message = current_message
12
15
  end
13
16
 
14
17
  def subscribe
@@ -28,6 +31,32 @@ module ChatSDK
28
31
  adapter.post_message(channel_id: channel_id, message: message, thread_id: id)
29
32
  end
30
33
 
34
+ def reply(target, content)
35
+ target_id = target.is_a?(ChatSDK::Message) ? target.id : target.to_s
36
+ if target.is_a?(ChatSDK::Message) && target.thread_id != id
37
+ raise ArgumentError, "cannot reply to a message from another thread"
38
+ end
39
+
40
+ message = PostableMessage.from(content)
41
+ adapter.reply_message(channel_id: channel_id, thread_id: id, message_id: target_id, message: message)
42
+ end
43
+
44
+ def mark_as_read(message = nil)
45
+ target = message
46
+ target ||= current_message
47
+ raise ArgumentError, "a message or message ID is required" unless target
48
+ if target.is_a?(ChatSDK::Message) && target.thread_id != id
49
+ raise ArgumentError, "cannot mark a message from another thread as read"
50
+ end
51
+
52
+ message_id = target.is_a?(ChatSDK::Message) ? target.id : target.to_s
53
+ adapter.mark_as_read(channel_id: channel_id, thread_id: id, message_id: message_id, message: target.is_a?(ChatSDK::Message) ? target : nil)
54
+ end
55
+
56
+ def start_typing
57
+ adapter.start_typing(channel_id: channel_id, thread_id: id, message_id: current_message&.id)
58
+ end
59
+
31
60
  def post_ephemeral(content, user_id:)
32
61
  message = PostableMessage.from(content)
33
62
  adapter.post_ephemeral(channel_id: channel_id, user_id: user_id, message: message, thread_id: id)
@@ -46,19 +75,19 @@ module ChatSDK
46
75
 
47
76
  def edit(message_id, content)
48
77
  message = PostableMessage.from(content)
49
- adapter.edit_message(channel_id: channel_id, message_id: message_id, message: message)
78
+ call_adapter(:edit_message, channel_id: channel_id, message_id: message_id, message: message, thread_id: id)
50
79
  end
51
80
 
52
81
  def delete(message_id)
53
- adapter.delete_message(channel_id: channel_id, message_id: message_id)
82
+ call_adapter(:delete_message, channel_id: channel_id, message_id: message_id, thread_id: id)
54
83
  end
55
84
 
56
85
  def react(message_id, emoji)
57
- adapter.add_reaction(channel_id: channel_id, message_id: message_id, emoji: emoji)
86
+ call_adapter(:add_reaction, channel_id: channel_id, message_id: message_id, emoji: emoji, thread_id: id)
58
87
  end
59
88
 
60
89
  def unreact(message_id, emoji)
61
- adapter.remove_reaction(channel_id: channel_id, message_id: message_id, emoji: emoji)
90
+ call_adapter(:remove_reaction, channel_id: channel_id, message_id: message_id, emoji: emoji, thread_id: id)
62
91
  end
63
92
 
64
93
  def post_ai_stream(enumerable, placeholder: "Thinking...")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChatSDK
4
- VERSION = "1.0.1"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -22,6 +22,9 @@ module ChatSDK
22
22
  events = adapter.parse_events(request || env)
23
23
  events.each { |event| chat.dispatch(event, adapter_name: adapter_name) }
24
24
 
25
+ response = adapter.webhook_response(events)
26
+ return response if response
27
+
25
28
  [200, {"content-type" => "text/plain"}, [""]]
26
29
  rescue ChatSDK::SignatureVerificationError => e
27
30
  ChatSDK::Log.warn("Signature verification failed: #{e.message}")
data/lib/chat_sdk.rb CHANGED
@@ -17,8 +17,8 @@ module ChatSDK
17
17
  end
18
18
  end
19
19
 
20
- def card(title: nil, subtitle: nil, &block)
21
- Cards::Builder.new(title: title, subtitle: subtitle, &block).build
20
+ def card(title: nil, subtitle: nil, width: nil, &block)
21
+ Cards::Builder.new(title: title, subtitle: subtitle, width: width, &block).build
22
22
  end
23
23
  end
24
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chat_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Quentin Rousseau
@@ -48,9 +48,11 @@ files:
48
48
  - lib/chat_sdk.rb
49
49
  - lib/chat_sdk/adapter/base.rb
50
50
  - lib/chat_sdk/adapter/capabilities.rb
51
+ - lib/chat_sdk/adapter/invocation.rb
51
52
  - lib/chat_sdk/adapter/media_types.rb
52
53
  - lib/chat_sdk/adapter/meta_verification.rb
53
54
  - lib/chat_sdk/ai.rb
55
+ - lib/chat_sdk/ai/conversation_scope.rb
54
56
  - lib/chat_sdk/ai/converter.rb
55
57
  - lib/chat_sdk/ai/stream_handler.rb
56
58
  - lib/chat_sdk/ai/tool_builder.rb
@@ -70,14 +72,21 @@ files:
70
72
  - lib/chat_sdk/dispatcher.rb
71
73
  - lib/chat_sdk/errors.rb
72
74
  - lib/chat_sdk/event_registry.rb
75
+ - lib/chat_sdk/event_serializer.rb
73
76
  - lib/chat_sdk/events/action.rb
74
77
  - lib/chat_sdk/events/base.rb
75
78
  - lib/chat_sdk/events/direct_message.rb
79
+ - lib/chat_sdk/events/installed.rb
76
80
  - lib/chat_sdk/events/mention.rb
81
+ - lib/chat_sdk/events/message_deleted.rb
82
+ - lib/chat_sdk/events/message_updated.rb
77
83
  - lib/chat_sdk/events/reaction.rb
78
84
  - lib/chat_sdk/events/slash_command.rb
79
85
  - lib/chat_sdk/events/subscribed_message.rb
86
+ - lib/chat_sdk/events/uninstalled.rb
80
87
  - lib/chat_sdk/format/converter.rb
88
+ - lib/chat_sdk/format/mentions.rb
89
+ - lib/chat_sdk/history.rb
81
90
  - lib/chat_sdk/instrumentation.rb
82
91
  - lib/chat_sdk/log.rb
83
92
  - lib/chat_sdk/message.rb