Skip to content
Kward Search API index

Class: Kward::RPC::PluginChatManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/rpc/plugin_chat_manager.rb

Overview

Exposes the frontend-neutral plugin chat runtime over JSON-RPC.

Constant Summary collapse

EVENT_LIMIT =
PluginChatRuntime::EVENT_LIMIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server:, client: Client.new, plugin_registry_provider: nil) ⇒ PluginChatManager

Returns a new instance of PluginChatManager.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 15

def initialize(server:, client: Client.new, plugin_registry_provider: nil)
  @server = server
  @runtime = PluginChatRuntime.new(
    client: client,
    plugin_registry_provider: plugin_registry_provider,
    message_normalizer: ->(message) { TranscriptNormalizer.new([message]).normalize.first }
  )
  @subscriptions = {}
  @mutex = Mutex.new
  @runtime.subscribe_events { |event| notify_event(event) }
end

Instance Attribute Details

#runtimeObject (readonly)

Returns the value of attribute runtime.



27
28
29
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 27

def runtime
  @runtime
end

Instance Method Details

#cancel_turn(turn_id:) ⇒ Object



79
80
81
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 79

def cancel_turn(turn_id:)
  turn_payload(@runtime.cancel_turn(turn_id: turn_id))
end

#listObject



33
34
35
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 33

def list
  { chats: supported_types.map { |type| type_payload(type) } }
end

#list_turns(chat_id: nil, active: false) ⇒ Object



95
96
97
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 95

def list_turns(chat_id: nil, active: false)
  { turns: @runtime.list_turns(chat_id: chat_id, active: active).map { |turn| turn_payload(turn) } }
end

#open(type_id:) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 37

def open(type_id:)
  chat = @runtime.open(type_id: type_id, surface: :rpc, scope_key: "owner")
  payload = chat_payload(chat)
  return payload if chat.driver.respond_to?(:transcript_page)

  payload.merge(messages: TranscriptNormalizer.new(chat.driver.messages).normalize)
end

#shutdownObject



99
100
101
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 99

def shutdown
  @runtime.shutdown
end

#start_turn(chat_id:, input:, attachments: []) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 68

def start_turn(chat_id:, input:, attachments: [])
  chat = fetch_chat(chat_id)
  normalized_attachments = AttachmentNormalizer.new.normalize(attachments)
  turn = @runtime.start_turn(
    chat_id: chat.id,
    input: @runtime.input_with_attachments(input, normalized_attachments),
    display_input: input.to_s
  )
  turn_payload(turn)
end

#subscribe(chat_id:) ⇒ Object



56
57
58
59
60
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 56

def subscribe(chat_id:)
  chat = fetch_chat(chat_id)
  @mutex.synchronize { @subscriptions[chat.id] = true }
  { chat: chat_payload(chat), subscribed: true }
end

#supported_typesObject



29
30
31
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 29

def supported_types
  @runtime.supported_types(surface: :rpc)
end

#transcript(chat_id:, limit: nil, before: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 45

def transcript(chat_id:, limit: nil, before: nil)
  chat = fetch_chat(chat_id)
  page = transcript_page(chat.driver, limit: limit, before: before)
  {
    chat: chat_payload(chat),
    messages: TranscriptNormalizer.new(page.fetch(:messages)).normalize,
    hasMore: page.fetch(:has_more),
    nextBefore: page[:next_before]
  }.compact
end

#turn_events(turn_id:, after_sequence: 0) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 87

def turn_events(turn_id:, after_sequence: 0)
  turn = @runtime.turn_status(turn_id: turn_id)
  {
    turn: turn_payload(turn),
    events: @runtime.turn_events(turn_id: turn_id, after_sequence: after_sequence)
  }
end

#turn_status(turn_id:) ⇒ Object



83
84
85
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 83

def turn_status(turn_id:)
  turn_payload(@runtime.turn_status(turn_id: turn_id))
end

#unsubscribe(chat_id:) ⇒ Object



62
63
64
65
66
# File 'lib/kward/rpc/plugin_chat_manager.rb', line 62

def unsubscribe(chat_id:)
  chat = fetch_chat(chat_id)
  @mutex.synchronize { @subscriptions.delete(chat.id) }
  { chat: chat_payload(chat), subscribed: false }
end