Skip to content
代码片段 群组 项目
未验证 提交 3acfa24d 编辑于 作者: Jan Provaznik's avatar Jan Provaznik 提交者: GitLab
浏览文件

Merge branch '468460-support-commit-in-duo-chat-api' into 'master'

No related branches found
No related tags found
无相关合并请求
...@@ -23,14 +23,15 @@ Requests to this endpoint are proxied to the ...@@ -23,14 +23,15 @@ Requests to this endpoint are proxied to the
Supported attributes: Supported attributes:
| Attribute | Type | Required | Description | | Attribute | Type | Required | Description |
|--------------------------|---------|----------|-------------------------------------------------------------------------| |--------------------------|-----------------|----------|-------------------------------------------------------------------------|
| `content` | string | Yes | Question sent to Chat. | | `content` | string | Yes | Question sent to Chat. |
| `resource_type` | string | No | Type of resource that is sent with Chat question. | | `resource_type` | string | No | Type of resource that is sent with Chat question. |
| `resource_id` | string | No | ID of the resource. | | `resource_id` | string, integer | No | ID of the resource. Can be a resource ID (integer) or a commit hash (string). |
| `referer_url` | string | No | Referer URL. | | `referer_url` | string | No | Referer URL. |
| `client_subscription_id` | string | No | Client Subscription ID. | | `client_subscription_id` | string | No | Client Subscription ID. |
| `with_clean_history` | boolean | No | Indicates if we need to reset the history before and after the request. | | `with_clean_history` | boolean | No | Indicates if we need to reset the history before and after the request. |
| `project_id` | integer | No | Project ID. Required if `resource_type` is a commit. |
Example request: Example request:
......
...@@ -8,7 +8,7 @@ class Chat < ::API::Base ...@@ -8,7 +8,7 @@ class Chat < ::API::Base
allow_access_with_scope :ai_features allow_access_with_scope :ai_features
AVAILABLE_RESOURCES = %w[issue epic group project merge_request].freeze AVAILABLE_RESOURCES = %w[issue epic group project merge_request commit].freeze
before do before do
authenticate! authenticate!
...@@ -24,10 +24,16 @@ def user_allowed?(resource) ...@@ -24,10 +24,16 @@ def user_allowed?(resource)
def find_resource(parameters) def find_resource(parameters)
return current_user unless parameters[:resource_type] && parameters[:resource_id] return current_user unless parameters[:resource_type] && parameters[:resource_id]
return commit_object(parameters) if parameters[:resource_type] == 'commit'
object = parameters[:resource_type].camelize.safe_constantize object = parameters[:resource_type].camelize.safe_constantize
object.find(parameters[:resource_id]) object.find(parameters[:resource_id])
end end
def commit_object(parameters)
project = ::Project.find(parameters[:project_id])
project.commit_by(oid: parameters[:resource_id])
end
end end
namespace 'chat' do namespace 'chat' do
...@@ -35,11 +41,14 @@ def find_resource(parameters) ...@@ -35,11 +41,14 @@ def find_resource(parameters)
params do params do
requires :content, type: String, limit: 1000, desc: 'Prompt from user' requires :content, type: String, limit: 1000, desc: 'Prompt from user'
optional :resource_type, type: String, limit: 100, values: AVAILABLE_RESOURCES, desc: 'Resource type' optional :resource_type, type: String, limit: 100, values: AVAILABLE_RESOURCES, desc: 'Resource type'
optional :resource_id, type: Integer, desc: 'ID of resource.' optional :resource_id, types: [String, Integer],
desc: 'ID of resource. Can be a resource ID (integer) or a commit hash (string).'
optional :referer_url, type: String, limit: 1000, desc: 'Referer URL' optional :referer_url, type: String, limit: 1000, desc: 'Referer URL'
optional :client_subscription_id, type: String, limit: 500, desc: 'Client Subscription ID' optional :client_subscription_id, type: String, limit: 500, desc: 'Client Subscription ID'
optional :with_clean_history, type: Boolean, optional :with_clean_history, type: Boolean,
desc: 'Indicates if we need to reset the history before and after the request' desc: 'Indicates if we need to reset the history before and after the request'
optional :project_id, type: Integer,
desc: 'Project ID. Required if resource_type is a commit.'
optional :current_file, type: Hash do optional :current_file, type: Hash do
optional :file_name, type: String, limit: 1000, desc: 'The name of the current file' optional :file_name, type: String, limit: 1000, desc: 'The name of the current file'
optional :content_above_cursor, type: String, optional :content_above_cursor, type: String,
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
let_it_be(:project) { create(:project, :repository, group: group) } let_it_be(:project) { create(:project, :repository, group: group) }
let_it_be(:issue) { create(:issue, project: project) } let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:merge_request) { create(:merge_request, source_project: project) } let_it_be(:merge_request) { create(:merge_request, source_project: project) }
let_it_be(:commit) { project.commit }
let(:current_user) { nil } let(:current_user) { nil }
let(:headers) { {} } let(:headers) { {} }
...@@ -256,6 +257,22 @@ ...@@ -256,6 +257,22 @@
end end
end end
context 'with a commit' do
let!(:resource) { commit }
let(:params) do
{ content: content, resource_type: "commit", resource_id: resource.id, project_id: resource.project.id }
end
it 'sends resource to the chat' do
expect(chat_message).to receive(:save!)
expect(Gitlab::Llm::ChatMessage).to receive(:new).with(chat_message_params).and_return(chat_message)
expect(Llm::Internal::CompletionService).to receive(:new).with(chat_message, options).and_return(chat)
expect(chat).to receive(:execute)
post_api
end
end
context 'without resource' do context 'without resource' do
let(:params) { { content: content } } let(:params) { { content: content } }
let(:resource) { current_user } let(:resource) { current_user }
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册