Skip to content
Kward Search API index

Module: Kward::Transport

Defined in:
lib/kward/transport.rb,
lib/kward/transport/host.rb,
lib/kward/transport/store.rb,
lib/kward/transport/gateway.rb,
lib/kward/transport/manager.rb,
lib/kward/transport/runtime.rb,
lib/kward/transport/plugin_chat_gateway.rb

Defined Under Namespace

Classes: Actor, Attachment, Capabilities, ConversationKey, ExecutionProfile, Gateway, Host, InboundMessage, InteractionRequest, Manager, PluginChatGateway, Runtime, Store, TurnEvent

Constant Summary collapse

STREAMING_MODES =
%i[native edit aggregate none].freeze
TOOL_MODES =
%i[all none allowlist].freeze
APPROVAL_MODES =
%i[default ask deny].freeze
MEMORY_MODES =
%i[default none].freeze
WORKSPACE_MODES =
%i[session fixed].freeze

Class Method Summary collapse

Class Method Details

.actor(id:, display_name: nil, metadata: {}) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/kward/transport.rb', line 54

def actor(id:, display_name: nil, metadata: {})
  Actor.new(
    id: required_string(id, "actor id"),
    display_name: optional_string(display_name),
    metadata: frozen_copy()
  )
end

.attachment(name: nil, mime_type:, data: nil, url: nil, metadata: {}) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kward/transport.rb', line 62

def attachment(name: nil, mime_type:, data: nil, url: nil, metadata: {})
  raise ArgumentError, "attachment requires data or url" if data.nil? && url.nil?
  raise ArgumentError, "attachment cannot contain both data and url" unless data.nil? || url.nil?

  Attachment.new(
    name: optional_string(name),
    mime_type: required_string(mime_type, "attachment MIME type"),
    data: data&.dup&.freeze,
    url: url && required_string(url, "attachment URL"),
    metadata: frozen_copy()
  )
end

.capabilities(inbound: [], outbound: [], streaming: :none, limits: {}) ⇒ Object

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
173
174
# File 'lib/kward/transport.rb', line 164

def capabilities(inbound: [], outbound: [], streaming: :none, limits: {})
  streaming = streaming.to_sym
  raise ArgumentError, "unsupported streaming mode: #{streaming}" unless STREAMING_MODES.include?(streaming)

  Capabilities.new(
    inbound: frozen_symbols(inbound),
    outbound: frozen_symbols(outbound),
    streaming: streaming,
    limits: frozen_copy(limits)
  )
end

.conversation_key(transport_id:, external_id:) ⇒ Object



75
76
77
78
79
80
# File 'lib/kward/transport.rb', line 75

def conversation_key(transport_id:, external_id:)
  ConversationKey.new(
    transport_id: required_string(transport_id, "transport id"),
    external_id: required_string(external_id, "external conversation id")
  )
end

.execution_profile(id:, tool_mode: :all, allowed_tools: nil, disabled_tools: nil, plugin_commands: true, approval_mode: :default, memory: :default, attachments: true, workspace_mode: :session, prompt_context: nil) ⇒ Object

Raises:

  • (ArgumentError)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/kward/transport.rb', line 125

def execution_profile(
  id:,
  tool_mode: :all,
  allowed_tools: nil,
  disabled_tools: nil,
  plugin_commands: true,
  approval_mode: :default,
  memory: :default,
  attachments: true,
  workspace_mode: :session,
  prompt_context: nil
)
  id = required_string(id, "execution profile id")
  tool_mode = tool_mode.to_sym
  approval_mode = approval_mode.to_sym
  memory = memory.to_sym
  workspace_mode = workspace_mode.to_sym
  raise ArgumentError, "unsupported tool mode: #{tool_mode}" unless TOOL_MODES.include?(tool_mode)
  raise ArgumentError, "unsupported approval mode: #{approval_mode}" unless APPROVAL_MODES.include?(approval_mode)
  raise ArgumentError, "unsupported memory mode: #{memory}" unless MEMORY_MODES.include?(memory)
  raise ArgumentError, "unsupported workspace mode: #{workspace_mode}" unless WORKSPACE_MODES.include?(workspace_mode)
  raise ArgumentError, "allowlist tool mode requires allowed_tools" if tool_mode == :allowlist && Array(allowed_tools).empty?
  raise ArgumentError, "none tool mode cannot define allowed_tools" if tool_mode == :none && allowed_tools
  raise ArgumentError, "allowed_tools and disabled_tools cannot both be set" if allowed_tools && disabled_tools

  ExecutionProfile.new(
    id: id,
    tool_mode: tool_mode,
    allowed_tools: frozen_symbols(allowed_tools),
    disabled_tools: frozen_symbols(disabled_tools),
    plugin_commands: !!plugin_commands,
    approval_mode: approval_mode,
    memory: memory,
    attachments: !!attachments,
    workspace_mode: workspace_mode,
    prompt_context: prompt_context.nil? ? nil : prompt_context.to_s.freeze
  )
end

.inbound_message(conversation:, message_id:, actor:, text: "", attachments: [], reply_context: {}, idempotency_key:, metadata: {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kward/transport.rb', line 82

def inbound_message(conversation:, message_id:, actor:, text: "", attachments: [], reply_context: {}, idempotency_key:, metadata: {})
  unless conversation.is_a?(ConversationKey)
    raise ArgumentError, "conversation must be a Transport::ConversationKey"
  end
  unless actor.is_a?(Actor)
    raise ArgumentError, "actor must be a Transport::Actor"
  end

  InboundMessage.new(
    conversation: conversation,
    message_id: required_string(message_id, "message id"),
    actor: actor,
    text: text.to_s.freeze,
    attachments: frozen_copy(Array(attachments)),
    reply_context: frozen_copy(reply_context),
    idempotency_key: required_string(idempotency_key, "idempotency key"),
    metadata: frozen_copy()
  )
end

.interaction_request(id:, session_id:, turn_id:, kind:, prompt:, choices: [], expires_at: nil, metadata: {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kward/transport.rb', line 112

def interaction_request(id:, session_id:, turn_id:, kind:, prompt:, choices: [], expires_at: nil, metadata: {})
  InteractionRequest.new(
    id: required_string(id, "interaction id"),
    session_id: required_string(session_id, "session id"),
    turn_id: required_string(turn_id, "turn id"),
    kind: required_string(kind, "interaction kind"),
    prompt: required_string(prompt, "interaction prompt"),
    choices: frozen_copy(Array(choices)),
    expires_at: expires_at,
    metadata: frozen_copy()
  )
end

.turn_event(type:, session_id:, turn_id:, sequence:, payload: {}) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/kward/transport.rb', line 102

def turn_event(type:, session_id:, turn_id:, sequence:, payload: {})
  TurnEvent.new(
    type: required_string(type, "turn event type"),
    session_id: required_string(session_id, "session id"),
    turn_id: required_string(turn_id, "turn id"),
    sequence: Integer(sequence),
    payload: frozen_copy(payload)
  )
end