Class: Rasti::AI::Assistant

Inherits:
Object
  • Object
show all
Defined in:
lib/rasti/ai/assistant.rb

Constant Summary collapse

VALID_THINKING_LEVELS =
%w[low medium high].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, json_schema: nil, state: nil, model: nil, thinking: nil, tools: [], mcp_servers: {}, logger: nil) ⇒ Assistant

Returns a new instance of Assistant.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rasti/ai/assistant.rb', line 9

def initialize(client:nil, json_schema:nil, state:nil, model:nil, thinking:nil, tools:[], mcp_servers:{}, logger:nil)
  raise ArgumentError, "Invalid thinking level '#{thinking}'. Valid: #{VALID_THINKING_LEVELS.join(', ')}" if thinking && !VALID_THINKING_LEVELS.include?(thinking)

  @client = client || build_default_client
  @json_schema = json_schema
  @state = state || AssistantState.new
  @model = model
  @thinking = thinking
  @tools = {}
  @serialized_tools = []
  @logger = logger || Rasti::AI.logger

  register_tools(tools)
  register_mcp_servers(mcp_servers)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/rasti/ai/assistant.rb', line 5

def model
  @model
end

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/rasti/ai/assistant.rb', line 5

def state
  @state
end

#thinkingObject (readonly)

Returns the value of attribute thinking.



5
6
7
# File 'lib/rasti/ai/assistant.rb', line 5

def thinking
  @thinking
end

Instance Method Details

#call(prompt) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rasti/ai/assistant.rb', line 25

def call(prompt)
  messages << build_user_message(prompt)

  loop do
    response = request_completion

    tool_calls = parse_tool_calls(response)

    if tool_calls.any?
      messages << build_assistant_tool_calls_message(response)

      tool_calls.each do |tool_call|
        name, args = extract_tool_call_info(tool_call)
        result = call_tool(name, args)
        messages << build_tool_result_message(tool_call, name, result)
      end
    else
      content = parse_content(response)

      messages << build_assistant_message(content)

      return content if finished?(response)
    end
  end
end