collavre_completion_api 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d657f80e16c1705fa79850aa8445aa53d90112de021c9640221f71b15d73f71e
4
- data.tar.gz: 4cef77f5d3433dcb90d9c1a1c82c97a8b9c0d56ec727f61e73fab68a3fa68de1
3
+ metadata.gz: f0766b36abd1a87f89283ed02a814e37d11c74cbda7fc4d4d8d3e22725bcd571
4
+ data.tar.gz: 3e020fecc799cd736ca0ed1b53d65347ddc6fab30992f942c751d492c1669c42
5
5
  SHA512:
6
- metadata.gz: 4a4d8cbd25e4f17e953085529b075b590e4dd390c1eb2d24cce0bd5a09d4c72ba25434a70b4a85e022a2e2ac8792a2c36adca9efdf657f6ec9565674836ef2a4
7
- data.tar.gz: adc77be5cc324e467f24438d3677777259a4474fd44a151c4f7ea7642fe3b65cfaec60dbe41069104802b843ccfa521b1d48625a3c926f770ff0206f4bf2fabb
6
+ metadata.gz: 98a13c15455b1881b8b758868768a9e9aa1a7331f659c6e9b469dfcf68a89bc0d9f8c455e0518a988ea2cbeee625637f44b16df28b73de9ce1a60288f74fdf33
7
+ data.tar.gz: dcade7f6d2f1d3d0c2c258228c0a0c1ff358cccf628d6342bbd8155af8380b53afb7216fbb2e0759a28ed09637d875641a919dfa867f5d6ba509c55421a91b66
@@ -47,6 +47,36 @@ module CollavreCompletionApi
47
47
  Collavre::Current.user
48
48
  end
49
49
 
50
+ # Model ID format: email username (before @)
51
+ # e.g., "devbot" for devbot@collavre.local
52
+ # Falls back to "collavre/{id}" if email is blank
53
+ def agent_model_id(agent)
54
+ if agent.email.present?
55
+ agent.email.split("@").first
56
+ else
57
+ "collavre/#{agent.id}"
58
+ end
59
+ end
60
+
61
+ # Resolve agent from model parameter (email username or legacy collavre/id format)
62
+ def resolve_agent_by_model(model_param)
63
+ return nil if model_param.blank?
64
+
65
+ agent = if model_param.start_with?("collavre/")
66
+ # Legacy format: collavre/{id}
67
+ Collavre::User.find_by(id: model_param.sub("collavre/", ""))
68
+ else
69
+ # Email username format — sanitize to prevent LIKE wildcard injection
70
+ sanitized = ActiveRecord::Base.sanitize_sql_like(model_param)
71
+ Collavre::User.where("email LIKE ?", "#{sanitized}@%").first
72
+ end
73
+
74
+ return nil unless agent&.ai_user?
75
+ return nil unless agent.created_by_id == current_user.id || agent.searchable?
76
+
77
+ agent
78
+ end
79
+
50
80
  def collavre_creative
51
81
  @collavre_creative ||= begin
52
82
  creative_id = request.headers["X-Collavre-Creative"]
@@ -39,7 +39,7 @@ module CollavreCompletionApi
39
39
  context: { creative: collavre_creative, user: current_user }
40
40
  )
41
41
 
42
- model_name = params[:model] || "collavre/#{agent.id}"
42
+ model_name = params[:model] || agent_model_id(agent)
43
43
 
44
44
  if stream
45
45
  stream_response(client, contents, model_name)
@@ -51,20 +51,7 @@ module CollavreCompletionApi
51
51
  private
52
52
 
53
53
  def resolve_agent
54
- model_param = params[:model].to_s
55
-
56
- return nil unless model_param.start_with?("collavre/")
57
-
58
- ai_id = model_param.sub("collavre/", "")
59
- agent = Collavre::User.find_by(id: ai_id)
60
- return nil unless agent&.ai_user?
61
- return nil unless agent_accessible?(agent)
62
-
63
- agent
64
- end
65
-
66
- def agent_accessible?(agent)
67
- agent.created_by_id == current_user.id || agent.searchable?
54
+ resolve_agent_by_model(params[:model].to_s)
68
55
  end
69
56
 
70
57
  def build_system_prompt(messages, agent)
@@ -9,7 +9,7 @@ module CollavreCompletionApi
9
9
 
10
10
  models = ai_agents.map do |agent|
11
11
  {
12
- id: "collavre/#{agent.id}",
12
+ id: agent_model_id(agent),
13
13
  object: "model",
14
14
  created: agent.created_at.to_i,
15
15
  owned_by: "collavre",
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module CollavreCompletionApi
4
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
5
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collavre_completion_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collavre