Class: NitroIntelligence::Assistants
- Inherits:
-
Object
- Object
- NitroIntelligence::Assistants
- 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- DEFAULT_BASE_URL =
Every assistant this gem is built for is served by the same deployment, so a client told nothing about where to reach one gets that deployment. A host talking to a different one -- a review environment, a local server -- still says so.
"https://assistants.powerhome.ai".freeze
- DEFAULT_USER_ID =
"default-user".freeze
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #await_run(thread_id:, assistant_id:, messages:, context: {}) ⇒ Object
-
#initialize(api_key:, base_url: nil, user_id: DEFAULT_USER_ID) ⇒ Assistants
constructor
A new instance of Assistants.
- #review_tool_calls(thread_id:, assistant_id:, reviewer_id:, tool_calls:, reviewed_at: DateTime.current.iso8601) ⇒ Object
-
#thread_messages(thread_id:) ⇒ Object
The thread's messages as Assistants reports them, unformatted, oldest first.
-
#thread_state(thread_id:) ⇒ Object
The thread's state as Assistants reports it, unformatted.
- #tool_calls_pending_review(thread_id:) ⇒ Object
Constructor Details
#initialize(api_key:, base_url: nil, user_id: DEFAULT_USER_ID) ⇒ Assistants
Returns a new instance of Assistants.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/nitro_intelligence/assistants.rb', line 26 def initialize(api_key:, base_url: nil, user_id: DEFAULT_USER_ID) raise ConfigurationError, "api_key is required" if api_key.blank? raise ConfigurationError, "user_id is required" if user_id.blank? @base_url = base_url.presence || DEFAULT_BASE_URL @api_key = api_key @user_id = user_id @tool_call_review_validator = ToolCallReviewValidator.new @graph_ids = {} end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
24 25 26 |
# File 'lib/nitro_intelligence/assistants.rb', line 24 def base_url @base_url end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
24 25 26 |
# File 'lib/nitro_intelligence/assistants.rb', line 24 def user_id @user_id end |
Instance Method Details
#await_run(thread_id:, assistant_id:, messages:, context: {}) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/nitro_intelligence/assistants.rb', line 37 def await_run(thread_id:, assistant_id:, messages:, context: {}) raise RunError, "messages cannot be empty" if .blank? initial_state = [0..-2] = .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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/nitro_intelligence/assistants.rb', line 78 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.
55 56 57 |
# File 'lib/nitro_intelligence/assistants.rb', line 55 def (thread_id:) (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.
49 50 51 |
# File 'lib/nitro_intelligence/assistants.rb', line 49 def thread_state(thread_id:) get_thread_state(thread_id:, error: ThreadStateError) end |
#tool_calls_pending_review(thread_id:) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nitro_intelligence/assistants.rb', line 59 def tool_calls_pending_review(thread_id:) thread_state = get_thread_state(thread_id:) = (thread_state) reviewed_tool_call_ids = ().map { || ["tool_call_id"] } .each_with_index.flat_map do |, index| next [] unless ["type"] == "ai" pending_tool_calls(, reviewed_tool_call_ids).map do |tool_call| { "previous_message_id" => index.zero? ? nil : [index - 1]&.dig("id"), "id" => tool_call["id"], "name" => tool_call["name"], "args" => tool_call["args"] || {}, } end end end |