Class: NitroIntelligence::Assistants

Inherits:
Object
  • Object
show all
Defined in:
lib/nitro_intelligence/assistants.rb

Defined Under Namespace

Classes: ConfigurationError, RunError, ThreadInitializationError, ThreadResumptionError, ThreadStateError

Constant Summary collapse

THREAD_CONFLICT_CODE =

Assistants answers with a conflict when ifExists: "raise" is sent for a thread that already exists.

409

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key:, user_id: "default-user") ⇒ Assistants

Returns a new instance of Assistants.

Raises:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/nitro_intelligence/assistants.rb', line 19

def initialize(base_url:, api_key:, user_id: "default-user")
  raise ConfigurationError, "base_url is required" if base_url.blank?
  raise ConfigurationError, "api_key is required" if api_key.blank?
  raise ConfigurationError, "user_id is required" if user_id.blank?

  @base_url = base_url
  @api_key = api_key
  @user_id = user_id
  @tool_call_review_validator = ToolCallReviewValidator.new
  @graph_ids = {}
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



17
18
19
# File 'lib/nitro_intelligence/assistants.rb', line 17

def base_url
  @base_url
end

#user_idObject (readonly)

Returns the value of attribute user_id.



17
18
19
# File 'lib/nitro_intelligence/assistants.rb', line 17

def user_id
  @user_id
end

Instance Method Details

#await_run(thread_id:, assistant_id:, messages:, context: {}) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
# File 'lib/nitro_intelligence/assistants.rb', line 31

def await_run(thread_id:, assistant_id:, messages:, context: {})
  raise RunError, "messages cannot be empty" if messages.blank?

  initial_state = messages[0..-2]
  last_message = messages.last

  initialize_thread_if_needed(thread_id:, assistant_id:, initial_state:)
  trigger_run(thread_id:, assistant_id:, context:, last_message:)
end

#review_tool_calls(thread_id:, assistant_id:, reviewer_id:, tool_calls:, reviewed_at: DateTime.current.iso8601) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/nitro_intelligence/assistants.rb', line 72

def review_tool_calls(thread_id:, assistant_id:, reviewer_id:, tool_calls:, reviewed_at: DateTime.current.iso8601)
  resume = { reviewer_id:, reviewed_at:, tool_calls: }.with_indifferent_access
  thread = get_thread(thread_id:)
  raise ThreadResumptionError, "Thread #{thread_id} is not in the interrupted state" unless interrupted?(thread)

  thread_state = get_thread_state(thread_id:)

  @tool_call_review_validator.validate!(
    thread_state:,
    tool_calls: resume[:tool_calls],
    pending_tool_calls: tool_calls_pending_review(thread_id:)
  )

  resume_run(
    thread_id:,
    assistant_id:,
    resume:,
    context: interrupt_context(thread_state)
  )

  nil
end

#thread_messages(thread_id:) ⇒ Object

The thread's messages as Assistants reports them, unformatted, oldest first. Each message carries its own type ("human", "ai", "tool", ...), which callers map to their own roles.



49
50
51
# File 'lib/nitro_intelligence/assistants.rb', line 49

def thread_messages(thread_id:)
  messages_in(thread_state(thread_id:))
end

#thread_state(thread_id:) ⇒ Object

The thread's state as Assistants reports it, unformatted. Callers that only want the conversation should reach for #thread_messages instead.



43
44
45
# File 'lib/nitro_intelligence/assistants.rb', line 43

def thread_state(thread_id:)
  get_thread_state(thread_id:, error: ThreadStateError)
end

#tool_calls_pending_review(thread_id:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nitro_intelligence/assistants.rb', line 53

def tool_calls_pending_review(thread_id:)
  thread_state = get_thread_state(thread_id:)
  messages = messages_in(thread_state)
  reviewed_tool_call_ids = tool_messages(messages).map { |message| message["tool_call_id"] }

  messages.each_with_index.flat_map do |message, index|
    next [] unless message["type"] == "ai"

    pending_tool_calls(message, reviewed_tool_call_ids).map do |tool_call|
      {
        "previous_message_id" => index.zero? ? nil : messages[index - 1]&.dig("id"),
        "id" => tool_call["id"],
        "name" => tool_call["name"],
        "args" => tool_call["args"] || {},
      }
    end
  end
end