Module: Gemlings

Defined in:
lib/gemlings.rb,
lib/gemlings/ui.rb,
lib/gemlings/cli.rb,
lib/gemlings/mcp.rb,
lib/gemlings/tool.rb,
lib/gemlings/agent.rb,
lib/gemlings/model.rb,
lib/gemlings/errors.rb,
lib/gemlings/memory.rb,
lib/gemlings/prompt.rb,
lib/gemlings/sandbox.rb,
lib/gemlings/version.rb,
lib/gemlings/callback.rb,
lib/gemlings/ruby_llm.rb,
lib/gemlings/code_agent.rb,
lib/gemlings/tools/file_read.rb,
lib/gemlings/tools/list_gems.rb,
lib/gemlings/tools/file_write.rb,
lib/gemlings/tools/user_input.rb,
lib/gemlings/tools/web_search.rb,
lib/gemlings/tool_calling_agent.rb,
lib/gemlings/tools/visit_webpage.rb,
lib/gemlings/models/ruby_llm_adapter.rb

Defined Under Namespace

Modules: MCP, Models, Prompt, UI Classes: ActionStep, Agent, AgentError, CLI, Callback, ChatMessage, CodeAgent, Error, ExecutionError, FileRead, FileWrite, FinalAnswerException, FinalAnswerTool, GenerationError, InterruptError, ListGems, ManagedAgentTool, MaxStepsError, Memory, Model, ParsingError, PlanningStep, PromptTemplates, RubyLLMAgentWrapper, RunResult, Sandbox, TokenUsage, Tool, ToolCall, ToolCallFunction, ToolCallingAgent, UserInput, UserMessage, VisitWebpage, WebSearch

Constant Summary collapse

LIPGLOSS_AVAILABLE =
defined?(Lipgloss)
VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.agent_from_ruby_llm(agent, name: nil, description: nil) ⇒ Object

Wraps a RubyLLM::Agent (class or instance) as a managed Gemlings agent. The agent can be passed in the agents: array of a Gemlings agent.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gemlings/ruby_llm.rb', line 36

def self.agent_from_ruby_llm(agent, name: nil, description: nil)
  require "ruby_llm"

  chat = if agent.is_a?(Class) && agent < RubyLLM::Agent
    agent.chat
  elsif agent.is_a?(RubyLLM::Agent)
    agent.chat
  elsif agent.respond_to?(:ask)
    agent
  else
    raise ArgumentError, "Expected a RubyLLM::Agent class, instance, or RubyLLM::Chat, got #{agent.class}"
  end

  agent_name = name || agent.class.name&.split("::")&.last&.downcase || "ruby_llm_agent"
  agent_desc = description || "A RubyLLM agent"

  RubyLLMAgentWrapper.new(chat, name: agent_name, description: agent_desc)
end

.tool(tool_name, desc, output: :string, **inputs, &block) ⇒ Object

Block-based tool shorthand Usage:

greet = Gemlings.tool(:greet, "Greets a person", name: "The person's name") { |name:| "Hello, #{name}!" }


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gemlings/tool.rb', line 103

def self.tool(tool_name, desc, output: :string, **inputs, &block)
  klass = Class.new(Tool) do
    self.tool_name(tool_name.to_s)
    self.description(desc)

    inputs.each do |param_name, param_desc|
      type, description = if param_desc.is_a?(Hash)
        [param_desc[:type] || :string, param_desc[:description] || param_desc.to_s]
      else
        [:string, param_desc.to_s]
      end
      input param_name, type: type, description: description
    end

    self.output_type(output)

    define_method(:call, &block)
  end

  klass.new
end

.tool_from_mcp(command:, tool_name:) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gemlings/mcp.rb', line 116

def self.tool_from_mcp(command:, tool_name:)
  transport = MCP::StdioTransport.new(command: command)
  response = transport.send_request(request: { method: "tools/list", params: {} })
  tools = response.dig("result", "tools") || []
  tool_def = tools.find { |t| t["name"] == tool_name }

  unless tool_def
    transport.close
    available = tools.map { |t| t["name"] }.join(", ")
    raise ArgumentError, "Tool #{tool_name.inspect} not found. Available: #{available}"
  end

  MCP::MCPToolWrapper.for(tool_def, transport)
end

.tool_from_ruby_llm(tool) ⇒ Object

Wraps a RubyLLM::Tool (class or instance) as a Gemlings::Tool.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gemlings/ruby_llm.rb', line 5

def self.tool_from_ruby_llm(tool)
  require "ruby_llm"
  instance = tool.is_a?(Class) ? tool.new : tool

  raise ArgumentError, "Expected a RubyLLM::Tool, got #{instance.class}" unless instance.is_a?(RubyLLM::Tool)

  tool_name = instance.name
  tool_desc = instance.description || ""
  params = instance.parameters || {}

  klass = Class.new(Tool) do
    self.tool_name(tool_name)
    description(tool_desc)

    params.each do |pname, param|
      input pname.to_sym,
            type: (param.type || "string").to_sym,
            description: param.description || "",
            required: param.required != false
    end
  end

  wrapper = klass.new
  wrapper.define_singleton_method(:call) do |**kwargs|
    instance.call(kwargs)
  end
  wrapper
end

.tools_from_mcp(command:) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/gemlings/mcp.rb', line 108

def self.tools_from_mcp(command:)
  transport = MCP::StdioTransport.new(command: command)
  response = transport.send_request(request: { method: "tools/list", params: {} })
  tools = response.dig("result", "tools") || []
  return (transport.close; []) if tools.empty?
  tools.map { |t| MCP::MCPToolWrapper.for(t, transport) }
end