Skip to content
Kward Search API index

Class: Kward::Tools::PluginTool

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/plugin_tool.rb

Overview

Adapts a trusted plugin tool registration to the normal tool interface.

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Constructor Details

#initialize(registration:, context_factory:) ⇒ PluginTool

Returns a new instance of PluginTool.



11
12
13
14
15
16
17
18
# File 'lib/kward/tools/plugin_tool.rb', line 11

def initialize(registration:, context_factory:)
  @description = registration.description
  @parameters = registration.schema
  @plugin_path = registration.path
  @handler = registration.handler
  @context_factory = context_factory
  super(registration.name, registration.description)
end

Instance Attribute Details

#plugin_path ⇒ Object (readonly)

Returns the value of attribute plugin_path.



9
10
11
# File 'lib/kward/tools/plugin_tool.rb', line 9

def plugin_path
  @plugin_path
end

Instance Method Details

#call(args, conversation, cancellation: nil) ⇒ Object

Executes trusted plugin code with parsed model arguments and scoped context.



33
34
35
36
37
38
# File 'lib/kward/tools/plugin_tool.rb', line 33

def call(args, conversation, cancellation: nil)
  context = @context_factory.call(conversation, cancellation)
  result = @handler.call(args, context)
  cancellation&.raise_if_cancelled!
  result.to_s
end

#schema ⇒ Object

Returns the strict JSON schema declared by the plugin.



21
22
23
24
25
26
27
28
29
30
# File 'lib/kward/tools/plugin_tool.rb', line 21

def schema
  {
    type: "function",
    function: {
      name: name,
      description: @description,
      parameters: @parameters
    }
  }
end