Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ gem "after_commit_everywhere", "~> 1.0"

# AI
gem "ruby-openai"
gem "anthropic", "~> 1.0"
gem "langfuse-ruby", "~> 0.1.4", require: "langfuse"

group :development, :test do
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ GEM
activerecord (>= 4.2)
activesupport
android_key_attestation (0.3.0)
anthropic (1.43.0)
cgi
connection_pool
standardwebhooks
ast (2.4.3)
attr_required (1.0.2)
aws-eventstream (1.4.0)
Expand Down Expand Up @@ -759,6 +763,7 @@ GEM
faraday (>= 1.0.1, < 3.0)
faraday-multipart (~> 1.0, >= 1.0.4)
stackprof (0.2.27)
standardwebhooks (1.1.0)
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.7)
Expand Down Expand Up @@ -859,6 +864,7 @@ DEPENDENCIES
aasm
activerecord-import
after_commit_everywhere (~> 1.0)
anthropic (~> 1.0)
aws-sdk-s3 (~> 1.208.0)
bcrypt (~> 3.1)
benchmark-ips
Expand Down
2 changes: 1 addition & 1 deletion app/models/assistant/builtin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def respond_to(message, assistant_message: nil)
if assistant_message.content.blank?
assistant_message.destroy
else
# Demote partially-streamed turns to `failed` so `Responder#conversation_history` excludes them.
# Demote partially-streamed turns to `failed` so the responder's history builders (`#openai_messages_payload`, `#chat_message_records`) exclude them.
assistant_message.update_columns(status: "failed")
end
end
Expand Down
41 changes: 32 additions & 9 deletions app/models/assistant/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def get_llm_response(streamer:, function_results: [], previous_response_id: nil)
instructions: instructions,
functions: function_tool_caller.function_definitions,
function_results: function_results,
messages: conversation_history,
messages: openai_messages_payload,
conversation_history: chat_message_records,
streamer: streamer,
previous_response_id: previous_response_id,
session_id: chat_session_id,
Expand Down Expand Up @@ -116,15 +117,37 @@ def chat
@chat ||= message.chat
end

def conversation_history
messages = []
return messages unless chat&.messages
# Memoized fetch — both `chat_message_records` and `openai_messages_payload`
# derive their shape from this one in-memory array so a single chat turn
# fires one history query instead of two.
def complete_chat_messages
return @complete_chat_messages if defined?(@complete_chat_messages)

@complete_chat_messages =
if chat&.messages
chat.messages
.where(type: [ "UserMessage", "AssistantMessage" ], status: "complete")
.includes(:tool_calls)
.ordered
.to_a
else
[]
end
end

chat.messages
.where(type: [ "UserMessage", "AssistantMessage" ], status: "complete")
.includes(:tool_calls)
.ordered
.each do |chat_message|
# Raw Message records preceding the current turn — providers that build
# their own native message shape (Anthropic) consume this directly so they
# do not have to round-trip through the OpenAI-shaped payload below.
def chat_message_records
complete_chat_messages.reject { |m| m.id == message.id }
end

# Builds the OpenAI-shaped messages payload (role: "user" | "assistant" |
# "tool"; tool_call_id pairing) consumed by Provider::Openai's generic
# chat path. Anthropic uses chat_message_records instead.
def openai_messages_payload
messages = []
complete_chat_messages.each do |chat_message|
if chat_message.tool_calls.any?
messages << {
role: chat_message.role,
Expand Down
20 changes: 17 additions & 3 deletions app/models/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,24 @@ def generate_title(prompt)
prompt.first(80)
end

# Returns the default AI model to use for chats
# Priority: AI Config > Setting
# Returns the default AI model to use for chats.
# Resolved from the configured llm_provider so installs that swap providers
# don't have to manually update every chat default. Falls through to a
# provider that actually has credentials configured, otherwise the chosen
# provider's classes would later raise "no LLM provider supports model …"
# even when the other provider is configured.
def default_model
Provider::Openai.effective_model.presence || Setting.openai_model
prefers_anthropic = Setting.llm_provider == "anthropic"

if prefers_anthropic && Provider::Anthropic.configured?
Provider::Anthropic.effective_model.presence || Setting.anthropic_model
elsif Provider::Openai.configured?
Provider::Openai.effective_model.presence || Setting.openai_model
elsif Provider::Anthropic.configured?
Provider::Anthropic.effective_model.presence || Setting.anthropic_model
else
Provider::Openai.effective_model.presence || Setting.openai_model
end
end
end

Expand Down
Loading
Loading