Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat: persist tool_calls extra_content in jsonb and normalize google/ prefix in find_pricing

Teju Nareddy93fdfe1bbb0e
diff --git a/app/models/llm_usage.rb b/app/models/llm_usage.rb
index 8954262990..bc0814fc48 100644
--- a/app/models/llm_usage.rb
+++ b/app/models/llm_usage.rb
@@ -172,6 +172,8 @@ class LlmUsage < ApplicationRecord
def self.find_pricing(provider, model)
return nil unless PRICING.key?(provider)
+ model = model.delete_prefix("google/") if provider == "google"
+
provider_pricing = PRICING[provider]
# Try exact match first
diff --git a/app/models/tool_call/function.rb b/app/models/tool_call/function.rb
index 756bdc8d7c..1c065718c7 100644
--- a/app/models/tool_call/function.rb
+++ b/app/models/tool_call/function.rb
@@ -2,8 +2,6 @@ class ToolCall::Function < ToolCall
validates :function_name, :function_result, presence: true
validates :function_arguments, presence: true, allow_blank: true
- attr_accessor :extra_content
-
class << self
# Translates an "LLM Concept" provider's FunctionRequest into a ToolCall::Function
def from_function_request(function_request, result)
@@ -12,10 +10,9 @@ class ToolCall::Function < ToolCall
provider_call_id: function_request.call_id,
function_name: function_request.function_name,
function_arguments: function_request.function_args,
- function_result: result
- ).tap do |call|
- call.extra_content = function_request.extra_content if function_request.respond_to?(:extra_content)
- end
+ function_result: result,
+ extra_content: function_request.respond_to?(:extra_content) ? function_request.extra_content : nil
+ )
end
# Serializes tool-call arguments to the JSON-encoded string OpenAI requires.
diff --git a/db/migrate/20261005120000_add_extra_content_to_tool_calls.rb b/db/migrate/20261005120000_add_extra_content_to_tool_calls.rb
new file mode 100644
index 0000000000..9ba37e9f6b
--- /dev/null
+++ b/db/migrate/20261005120000_add_extra_content_to_tool_calls.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddExtraContentToToolCalls < ActiveRecord::Migration[8.1]
+ def change
+ add_column :tool_calls, :extra_content, :jsonb
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 61fad25e68..ac9def769e 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[8.1].define(version: 2026_09_29_120000) do
+ActiveRecord::Schema[8.1].define(version: 2026_10_05_120000) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pgcrypto"
@@ -1593,7 +1593,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_09_29_120000) do
t.jsonb "variable_rate_schedule", default: {}, null: false
t.check_constraint "down_payment IS NULL OR down_payment >= 0::numeric", name: "chk_loans_down_payment_non_negative"
t.check_constraint "insurance_rate IS NULL OR insurance_rate >= 0::numeric", name: "chk_loans_insurance_rate_non_negative"
- t.check_constraint "insurance_rate_type IS NULL OR (insurance_rate_type::text = ANY (ARRAY['level_term'::character varying, 'decreasing_life'::character varying]::text[]))", name: "chk_loans_insurance_rate_type"
+ t.check_constraint "insurance_rate_type IS NULL OR (insurance_rate_type::text = ANY (ARRAY['level_term'::character varying::text, 'decreasing_life'::character varying::text]))", name: "chk_loans_insurance_rate_type"
end
create_table "lunchflow_accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
@@ -2625,6 +2625,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_09_29_120000) do
create_table "tool_calls", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.datetime "created_at", null: false
+ t.jsonb "extra_content"
t.jsonb "function_arguments"
t.string "function_name"
t.jsonb "function_result"
diff --git a/test/models/llm_usage_test.rb b/test/models/llm_usage_test.rb
index b5f631d188..66ed1efdd7 100644
--- a/test/models/llm_usage_test.rb
+++ b/test/models/llm_usage_test.rb
@@ -222,6 +222,20 @@ class LlmUsageTest < ActiveSupport::TestCase
assert_in_delta 0.80, cost_3_flash, 0.0001
end
+ test "find_pricing and calculate_cost normalize google/ prefix for OpenRouter models" do
+ pricing = LlmUsage.find_pricing("google", "google/gemini-3.8-flash")
+ assert_not_nil pricing
+ assert_equal 0.75, pricing[:prompt]
+ assert_equal 3.75, pricing[:completion]
+
+ cost = LlmUsage.calculate_cost(
+ model: "google/gemini-3.8-flash",
+ prompt_tokens: 1_000_000,
+ completion_tokens: 100_000
+ )
+ assert_in_delta 1.125, cost, 0.0001
+ end
+
test "calculate_cost uses lower pricing for Haiku" do
cost = LlmUsage.calculate_cost(model: "claude-haiku-4-5", prompt_tokens: 1_000_000, completion_tokens: 1_000_000)
diff --git a/test/models/tool_call/function_test.rb b/test/models/tool_call/function_test.rb
index 692a1c26ee..7fadad0b82 100644
--- a/test/models/tool_call/function_test.rb
+++ b/test/models/tool_call/function_test.rb
@@ -83,4 +83,24 @@ class ToolCall::FunctionTest < ActiveSupport::TestCase
assert_nil tool_call.extra_content
assert_not_includes tool_call.to_tool_call.keys, :extra_content
end
+
+ test "extra_content is persisted in the database and reloaded across turns" do
+ extra = { "google" => { "thought_signature" => "sig_persisted_123" } }
+ message = messages(:chat1_assistant_response)
+
+ tool_call = ToolCall::Function.create!(
+ message: message,
+ provider_id: "resp_1",
+ provider_call_id: "call_1",
+ function_name: "get_net_worth",
+ function_arguments: '{"currency":"USD"}',
+ function_result: { "amount" => 10000 },
+ extra_content: extra
+ )
+
+ reloaded = ToolCall::Function.find(tool_call.id)
+ assert_equal extra, reloaded.extra_content
+ assert_equal extra, reloaded.to_tool_call[:extra_content]
+ assert_equal extra, reloaded.to_result[:extra_content]
+ end
end