Class: Kward::ToolRegistry

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

Overview

Exposes local workspace, search, skill, and interaction tools to the model and dispatches approved tool calls into the active conversation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workspace: Workspace.new, prompt: nil, web_search: WebSearch.new, code_search: CodeSearch.new, web_search_enabled: nil, skills: nil, ask_user_question_enabled: nil) ⇒ ToolRegistry

Returns a new instance of ToolRegistry.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kward/tools/registry.rb', line 25

def initialize(workspace: Workspace.new, prompt: nil, web_search: WebSearch.new, code_search: CodeSearch.new, web_search_enabled: nil, skills: nil, ask_user_question_enabled: nil)
  @workspace = workspace
  @prompt = prompt
  @web_search = web_search
  @code_search = code_search
  @skills = skills
  @web_search_enabled = web_search_enabled
  @ask_user_question_enabled = ask_user_question_enabled
  @tools = build_tools.freeze
  @schemas = build_schema_tools.map(&:schema).freeze
end

Instance Attribute Details

#schemasArray<Hash> (readonly)

Tool schemas advertised to the model for the current frontend and config.

Returns:

  • (Array<Hash>)


23
24
25
# File 'lib/kward/tools/registry.rb', line 23

def schemas
  @schemas
end

Instance Method Details

#dispatch(tool_call, conversation, cancellation: nil) ⇒ String

Executes a model-requested tool call and appends the result to the conversation transcript.

Parameters:

  • tool_call (Hash)

    model tool call payload

  • conversation (Conversation)

    active conversation

Returns:

  • (String)

    tool output content appended to the conversation



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kward/tools/registry.rb', line 43

def dispatch(tool_call, conversation, cancellation: nil)
  cancellation&.raise_if_cancelled!
  name = ToolCall.name(tool_call)
  args = ToolCall.arguments(tool_call)
  tool = @tools[name]

  content = if tool
              tool.call(args, conversation, cancellation: cancellation)
            else
              "Unknown tool: #{name}"
            end

  conversation.append_tool(
    tool_call_id: tool_call["id"] || tool_call[:id],
    name: name,
    content: content
  )
  conversation.append_tool_execution(tool_call: tool_call, content: content)

  content
end