pave_ai 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +195 -0
- data/app/assets/stylesheets/pave_ai/application.css +15 -0
- data/app/controllers/pave_ai/application_controller.rb +4 -0
- data/app/helpers/pave_ai/application_helper.rb +4 -0
- data/app/jobs/pave_ai/application_job.rb +4 -0
- data/app/mailers/pave_ai/application_mailer.rb +6 -0
- data/app/models/pave_ai/application_record.rb +5 -0
- data/app/models/pave_ai/chat.rb +49 -0
- data/app/models/pave_ai/chat_message.rb +28 -0
- data/app/models/pave_ai/intent.rb +34 -0
- data/app/models/pave_ai/persona.rb +34 -0
- data/app/models/pave_ai/persona_document.rb +35 -0
- data/app/models/pave_ai/quota.rb +77 -0
- data/app/models/pave_ai/spend_log.rb +47 -0
- data/app/services/pave_ai/client/provider.rb +50 -0
- data/app/services/pave_ai/client/providers/anthropic.rb +15 -0
- data/app/services/pave_ai/client/providers/gemini.rb +15 -0
- data/app/services/pave_ai/client/providers/ollama.rb +16 -0
- data/app/services/pave_ai/client/providers/openai.rb +14 -0
- data/app/services/pave_ai/client/providers/openai_compatible.rb +18 -0
- data/app/services/pave_ai/client/providers/xai.rb +14 -0
- data/app/services/pave_ai/client/providers/zai.rb +13 -0
- data/app/services/pave_ai/client/providers/zen.rb +12 -0
- data/app/services/pave_ai/client.rb +683 -0
- data/app/services/pave_ai/credential.rb +66 -0
- data/app/services/pave_ai/governance.rb +70 -0
- data/app/services/pave_ai/rates.rb +101 -0
- data/app/services/pave_ai/spend.rb +539 -0
- data/app/views/layouts/pave_ai/application.html.erb +17 -0
- data/config/ai_governance/ETHICS.md +20 -0
- data/config/routes.rb +2 -0
- data/db/migrate/20260404000001_create_pave_ai_tables.rb +102 -0
- data/db/migrate/20260820000002_add_billable_to_pave_ai_spend_logs.rb +5 -0
- data/db/migrate/20260827000001_add_model_to_pave_ai_spend_logs.rb +8 -0
- data/db/migrate/20260828000001_add_intent_credit_cost_and_log_credits.rb +10 -0
- data/db/migrate/20261123000001_remove_quota_limit_defaults.rb +9 -0
- data/lib/generators/pave_ai/install/install_generator.rb +61 -0
- data/lib/pave_ai/engine.rb +23 -0
- data/lib/pave_ai/middleware/request_cleanup.rb +17 -0
- data/lib/pave_ai/version.rb +3 -0
- data/lib/pave_ai.rb +247 -0
- data/lib/tasks/pave_ai_tasks.rake +4 -0
- metadata +127 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "cgi"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module PaveAi
|
|
7
|
+
# One provider credential for ONE call: a caller's own key, resolved and
|
|
8
|
+
# authorized by the host and handed to PaveAi::Client. The engine never
|
|
9
|
+
# stores it and never prints it; a call made with one is caller-funded.
|
|
10
|
+
class Credential
|
|
11
|
+
FILTERED = "[FILTERED]"
|
|
12
|
+
|
|
13
|
+
attr_reader :api_key, :api_base
|
|
14
|
+
|
|
15
|
+
def initialize(api_key:, api_base: nil)
|
|
16
|
+
@api_key = api_key.to_s.strip.freeze
|
|
17
|
+
raise ArgumentError, "A credential needs an API key" if @api_key.empty?
|
|
18
|
+
|
|
19
|
+
@api_base = normalized_base(api_base)
|
|
20
|
+
# The key as it travels: literally, URL-encoded, and inside JSON.
|
|
21
|
+
@forms = [ @api_key, CGI.escape(@api_key), @api_key.to_json[1..-2] ].uniq.freeze
|
|
22
|
+
freeze
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Vendors echo keys back in error bodies. Everything the client raises or
|
|
26
|
+
# logs, and everything Client#meter lets reach a SpendLog row, passes
|
|
27
|
+
# through here first.
|
|
28
|
+
def redact(text) = @forms.reduce(text.to_s) { |redacted, form| redacted.gsub(form, FILTERED) }
|
|
29
|
+
|
|
30
|
+
def exposed_in?(text) = @forms.any? { |form| text.to_s.include?(form) }
|
|
31
|
+
|
|
32
|
+
def inspect = "#<#{self.class.name} api_key=#{FILTERED} api_base=#{redact(api_base.inspect)}>"
|
|
33
|
+
alias_method :to_s, :inspect
|
|
34
|
+
|
|
35
|
+
def pretty_print(printer) = printer.text(inspect)
|
|
36
|
+
def as_json(*) = { "api_key" => FILTERED, "api_base" => api_base && redact(api_base) }
|
|
37
|
+
|
|
38
|
+
# A credential lives in memory for one call. It is not something to put in
|
|
39
|
+
# a job argument, a cache or a session, so it refuses to be serialized.
|
|
40
|
+
def marshal_dump = raise(TypeError, "#{self.class.name} cannot be serialized")
|
|
41
|
+
def encode_with(_coder) = raise(TypeError, "#{self.class.name} cannot be serialized")
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# A caller's base URL is somewhere WE will send a request and a key, so it
|
|
46
|
+
# is https, names one host, and carries nothing that could hide another:
|
|
47
|
+
# no userinfo, query or fragment. It is stored rebuilt from its parts.
|
|
48
|
+
# Which hosts are acceptable (no private or internal addresses) is the host
|
|
49
|
+
# application's check: only it knows its network.
|
|
50
|
+
def normalized_base(value)
|
|
51
|
+
base = value.to_s.strip
|
|
52
|
+
return nil if base.empty?
|
|
53
|
+
|
|
54
|
+
uri = URI.parse(base)
|
|
55
|
+
unless uri.is_a?(URI::HTTPS) && uri.host.present? && uri.userinfo.nil? && uri.query.nil? && uri.fragment.nil?
|
|
56
|
+
raise ArgumentError, "A credential's base URL must be a plain https URL"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
port = uri.port == URI::HTTPS::DEFAULT_PORT ? "" : ":#{uri.port}"
|
|
60
|
+
"https://#{uri.host.downcase}#{port}#{uri.path.chomp("/")}".freeze
|
|
61
|
+
rescue URI::InvalidURIError
|
|
62
|
+
# No cause: the parse error quotes the string it was given.
|
|
63
|
+
raise ArgumentError, "A credential's base URL must be a plain https URL", cause: nil
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PaveAi
|
|
4
|
+
class Governance
|
|
5
|
+
class Unauthorized < StandardError; end
|
|
6
|
+
class QuotaExceeded < StandardError; end
|
|
7
|
+
|
|
8
|
+
def self.authorize!(user:, intent:, type: :generation)
|
|
9
|
+
check_role!(user, intent)
|
|
10
|
+
check_quota!(user, type)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.check_role!(user, intent)
|
|
14
|
+
raise Unauthorized, "Authentication required" if user.nil?
|
|
15
|
+
return if user.respond_to?(:pave_ai_admin?) && user.pave_ai_admin?
|
|
16
|
+
unless intent.user_accessible?
|
|
17
|
+
raise Unauthorized, "You don't have access to this intent"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.check_quota!(user, type = :generation)
|
|
22
|
+
return if user.nil?
|
|
23
|
+
return if user.respond_to?(:ai_veto?) && user.ai_veto?
|
|
24
|
+
|
|
25
|
+
unless PaveAi::Quota.within_quota?(user, type: type)
|
|
26
|
+
quota = PaveAi::Quota.current_for(user)
|
|
27
|
+
case type
|
|
28
|
+
when :generation
|
|
29
|
+
raise QuotaExceeded, "You've used #{quota.generation_summary} AI generations this month"
|
|
30
|
+
when :export
|
|
31
|
+
raise QuotaExceeded, "You've used #{quota.export_summary} exports this month"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.check_credits!(user)
|
|
37
|
+
return if user.nil?
|
|
38
|
+
return if user.respond_to?(:ai_veto?) && user.ai_veto?
|
|
39
|
+
return if PaveAi::Quota.within_credit_quota?(user)
|
|
40
|
+
return if user.respond_to?(:tool_credits_balance) && user.tool_credits_balance > 0
|
|
41
|
+
|
|
42
|
+
quota = PaveAi::Quota.current_for(user)
|
|
43
|
+
raise QuotaExceeded, "You've used #{quota.credit_summary} AI credits this month"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
ETHICS_CACHE_TTL = 300 # 5 minutes
|
|
47
|
+
|
|
48
|
+
def self.ethics_prefix
|
|
49
|
+
docs = PaveAi::PersonaDocument.active.by_category("governance").ordered
|
|
50
|
+
if docs.any?
|
|
51
|
+
docs.map(&:content).join("\n\n---\n\n")
|
|
52
|
+
else
|
|
53
|
+
if @ethics_fallback_expires_at.nil? || Time.current > @ethics_fallback_expires_at
|
|
54
|
+
path = PaveAi.config.ethics_file_path || PaveAi::Engine.root.join("config/ai_governance/ETHICS.md")
|
|
55
|
+
@ethics_fallback = File.read(path)
|
|
56
|
+
@ethics_fallback_expires_at = Time.current + ETHICS_CACHE_TTL
|
|
57
|
+
end
|
|
58
|
+
@ethics_fallback
|
|
59
|
+
end
|
|
60
|
+
rescue Errno::ENOENT
|
|
61
|
+
Rails.logger.warn "[PaveAi::Governance] ETHICS.md not found"
|
|
62
|
+
""
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def self.reset!
|
|
66
|
+
@ethics_fallback = nil
|
|
67
|
+
@ethics_fallback_expires_at = nil
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
|
|
5
|
+
module PaveAi
|
|
6
|
+
# Model rates as data, never code. Sources, in order:
|
|
7
|
+
#
|
|
8
|
+
# 1. PaveAi.config.model_rates — host-pinned rates
|
|
9
|
+
# ("model-id" => { input:, output:, cached:, verified_on: }, USD per
|
|
10
|
+
# million tokens). Pin explicit model IDs, never floating aliases.
|
|
11
|
+
# 2. The RubyLLM model registry (ships with the gem, refreshable).
|
|
12
|
+
#
|
|
13
|
+
# A rate with no verified_on, or one older than rates_max_age_days, is
|
|
14
|
+
# stale — a stale table mis-charges every customer quietly.
|
|
15
|
+
class Rates
|
|
16
|
+
Rate = Struct.new(:input, :output, :cached, :verified_on, :source, keyword_init: true) do
|
|
17
|
+
def stale?(max_age_days: PaveAi.config.rates_max_age_days)
|
|
18
|
+
verified_on.nil? || verified_on < Date.current - max_age_days
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
def for(model:, provider: nil)
|
|
24
|
+
config_rate(model) || registry_rate(model, provider)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def known?(model:, provider: nil)
|
|
28
|
+
!self.for(model: model, provider: provider).nil?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Model ids of every pinned rate that has gone stale.
|
|
32
|
+
def stale
|
|
33
|
+
(PaveAi.config.model_rates || {}).filter_map do |model_id, data|
|
|
34
|
+
model_id if build_rate(data, :config).stale?
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Integer cents for a text operation, rounded up. Nil when unrated.
|
|
39
|
+
#
|
|
40
|
+
# Decimal, not float: (140_000 * 0.50 / 1_000_000) * 100 is exactly 7
|
|
41
|
+
# cents, but in float it evaluates to 7.000000000000001 and ceils to 8.
|
|
42
|
+
# The round-up is deliberate; that extra cent was not.
|
|
43
|
+
#
|
|
44
|
+
# cached_tokens are prompt tokens served from the provider's cache,
|
|
45
|
+
# priced at the cached rate (or the input rate when none is pinned).
|
|
46
|
+
# An input-only rate — an embedding model — prices input alone; output
|
|
47
|
+
# tokens against a rate without an output price are unrated.
|
|
48
|
+
def text_cost_cents(model:, provider: nil, input_tokens: 0, output_tokens: 0, cached_tokens: 0)
|
|
49
|
+
rate = self.for(model: model, provider: provider)
|
|
50
|
+
return nil unless rate&.input
|
|
51
|
+
return nil if output_tokens.to_i.positive? && rate.output.nil?
|
|
52
|
+
|
|
53
|
+
# rate is USD per million tokens, so cents = tokens * rate / 10_000.
|
|
54
|
+
cents = (input_tokens.to_i * decimal(rate.input) +
|
|
55
|
+
output_tokens.to_i * decimal(rate.output || 0) +
|
|
56
|
+
cached_tokens.to_i * decimal(rate.cached || rate.input)) / BigDecimal("10000")
|
|
57
|
+
cents.ceil
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
# Via to_s so a Float rate becomes the decimal a human wrote, not its
|
|
63
|
+
# binary approximation.
|
|
64
|
+
def decimal(value)
|
|
65
|
+
value.is_a?(BigDecimal) ? value : BigDecimal(value.to_s)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def config_rate(model)
|
|
69
|
+
data = (PaveAi.config.model_rates || {})[model.to_s]
|
|
70
|
+
data && build_rate(data, :config)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def build_rate(data, source)
|
|
74
|
+
data = data.transform_keys(&:to_sym)
|
|
75
|
+
Rate.new(
|
|
76
|
+
input: data[:input],
|
|
77
|
+
output: data[:output],
|
|
78
|
+
cached: data[:cached],
|
|
79
|
+
verified_on: data[:verified_on],
|
|
80
|
+
source: source
|
|
81
|
+
)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def registry_rate(model, provider)
|
|
85
|
+
info = RubyLLM.models.find(model, provider: provider&.to_sym)
|
|
86
|
+
tokens = info.pricing.text_tokens
|
|
87
|
+
return nil unless tokens.input || tokens.output
|
|
88
|
+
|
|
89
|
+
Rate.new(
|
|
90
|
+
input: tokens.input,
|
|
91
|
+
output: tokens.output,
|
|
92
|
+
cached: tokens.cache_read_input,
|
|
93
|
+
verified_on: nil, # registry data carries no local verification date
|
|
94
|
+
source: :registry
|
|
95
|
+
)
|
|
96
|
+
rescue StandardError
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|