collavre_completion_api 0.2.1 → 0.2.3
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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dc50b74db9b7b24ba288392fb8c5a4b269727228829dcf91a0196236f0a5c352
|
|
4
|
+
data.tar.gz: 5bdad75265afa2f8eae040d22069e8370cb771afd5bc2abccc380a47d2a9def5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2abccff570987512417cdc2d41f22a7b05177b7bf4bcdb6e44bd47f7b87560d35aa9fe8332ecf7f7abf60d9f3e9f8083605a209394cc915e7362f24188b34605
|
|
7
|
+
data.tar.gz: 96578d0b13a218be1f72a1086d0e7ecf6fd7ce8a3fb391750b24073602fa29403e251e5c1163d2f084a866404ee8f2dc905fcb908504e43d3c2f005d4657fe86
|
data/README.md
CHANGED
|
@@ -32,6 +32,13 @@ Pass optional headers to inject Collavre context into AI prompts:
|
|
|
32
32
|
- `X-Collavre-Creative: <creative_id>` — Include creative context
|
|
33
33
|
- `X-Collavre-Topic: <topic_id>` — Filter context to specific topic
|
|
34
34
|
|
|
35
|
+
## Session Tracking
|
|
36
|
+
|
|
37
|
+
When both `X-Collavre-Creative` and `X-Collavre-Topic` headers are present, the
|
|
38
|
+
completion API forwards an `X-Session-Id` header (`creative_{id}_topic_{id}`) to
|
|
39
|
+
the upstream LLM provider. This allows the gateway to group requests by
|
|
40
|
+
creative/topic pair.
|
|
41
|
+
|
|
35
42
|
## Installation
|
|
36
43
|
|
|
37
44
|
Add to your host app's `Gemfile`:
|
|
@@ -17,7 +17,7 @@ module CollavreCompletionApi
|
|
|
17
17
|
return
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
unless
|
|
20
|
+
unless find_user_by_bearer_token(token)
|
|
21
21
|
render json: { error: { message: "Invalid authentication token", type: "invalid_request_error",
|
|
22
22
|
code: "invalid_token" } },
|
|
23
23
|
status: :unauthorized
|
|
@@ -25,17 +25,10 @@ module CollavreCompletionApi
|
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
user = Collavre::User.find_by(id: access_token.resource_owner_id)
|
|
33
|
-
return false unless user
|
|
34
|
-
|
|
35
|
-
Collavre::Current.user = user
|
|
36
|
-
true
|
|
37
|
-
end
|
|
38
|
-
|
|
28
|
+
# Bearer-auth helpers are kept inline here (rather than shared via the
|
|
29
|
+
# core Collavre::Api::Authenticatable concern) so this engine stays
|
|
30
|
+
# loadable under USE_COLLAVRE_GEM=true, where core resolves to a
|
|
31
|
+
# published collavre gem that may predate the concern.
|
|
39
32
|
def extract_bearer_token
|
|
40
33
|
auth_header = request.headers["Authorization"]
|
|
41
34
|
return nil unless auth_header&.start_with?("Bearer ")
|
|
@@ -43,6 +36,19 @@ module CollavreCompletionApi
|
|
|
43
36
|
auth_header.sub("Bearer ", "")
|
|
44
37
|
end
|
|
45
38
|
|
|
39
|
+
def find_user_by_bearer_token(token)
|
|
40
|
+
return nil if token.blank?
|
|
41
|
+
|
|
42
|
+
access_token = Doorkeeper::AccessToken.by_token(token)
|
|
43
|
+
return nil unless access_token&.accessible?
|
|
44
|
+
|
|
45
|
+
user = Collavre::User.find_by(id: access_token.resource_owner_id)
|
|
46
|
+
return nil unless user
|
|
47
|
+
|
|
48
|
+
Collavre::Current.user = user
|
|
49
|
+
user
|
|
50
|
+
end
|
|
51
|
+
|
|
46
52
|
def current_user
|
|
47
53
|
Collavre::Current.user
|
|
48
54
|
end
|
|
@@ -37,7 +37,7 @@ module CollavreCompletionApi
|
|
|
37
37
|
system_prompt: system_prompt,
|
|
38
38
|
llm_api_key: api_key,
|
|
39
39
|
gateway_url: agent.respond_to?(:gateway_url) ? agent.gateway_url : nil,
|
|
40
|
-
context: { creative: collavre_creative, user: current_user }
|
|
40
|
+
context: { creative: collavre_creative, user: current_user, topic_id: collavre_topic&.id }
|
|
41
41
|
)
|
|
42
42
|
|
|
43
43
|
model_name = params[:model] || agent_model_id(agent)
|
|
@@ -80,7 +80,7 @@ module CollavreCompletionApi
|
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
scope = collavre_creative.comments
|
|
83
|
-
.
|
|
83
|
+
.public_only
|
|
84
84
|
.order(created_at: :desc)
|
|
85
85
|
scope = scope.where(topic_id: collavre_topic.id) if collavre_topic
|
|
86
86
|
recent_comments = scope.limit(CREATIVE_CONTEXT_COMMENT_LIMIT)
|