Skip to content

Commit 8e71e45

Browse files
Fix bugs (#226)
* test fix * fix quip errors * catch soql error * security bug --------- Co-authored-by: Vijay Swamidass <[email protected]>
1 parent 6e0a079 commit 8e71e45

File tree

9 files changed

+65
-24
lines changed

9 files changed

+65
-24
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,4 @@ gem 'omniauth-google-oauth2'
135135
gem 'google-api-client'
136136
gem 'googleauth'
137137

138-
gem 'uri', '>= 1.0.3'
138+
gem 'uri', '>= 1.0.4'

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ GEM
497497
unicode-display_width (3.1.4)
498498
unicode-emoji (~> 4.0, >= 4.0.4)
499499
unicode-emoji (4.0.4)
500-
uri (1.0.3)
500+
uri (1.0.4)
501501
useragent (0.16.11)
502502
version_gem (1.1.8)
503503
wasabi (5.1.0)
@@ -582,7 +582,7 @@ DEPENDENCIES
582582
tiktoken_ruby
583583
turbo-rails
584584
tzinfo-data
585-
uri (>= 1.0.3)
585+
uri (>= 1.0.4)
586586
web-console
587587
webdrivers
588588
websocket-client-simple

app/jobs/generate_message_response_job.rb

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,20 @@ def perform(_message_id)
133133

134134
# QUIP Doc
135135
if assistant.quip_url.present?
136-
quip_client = Quip::Client.new(access_token: ENV.fetch('QUIP_TOKEN'))
137-
138136
uri = URI.parse(assistant.quip_url)
139-
path = uri.path.sub(%r{^/}, '') # Removes the leading /
140-
quip_thread = quip_client.get_thread(path)
141-
142-
prompt += "# QUIP DOCUMENT\n\n"
143-
# The quip api only returns html which has too much extra junk.
144-
# Convert to md for smaller size
145-
markdown_quip = ReverseMarkdown.convert quip_thread['html']
146-
prompt += markdown_quip
137+
# Some quip_urls have google and other things in the host. skip this if it doesn't have quip.com in the host
138+
if uri.host&.include?('quip.com')
139+
quip_client = Quip::Client.new(access_token: ENV.fetch('QUIP_TOKEN'))
140+
path = uri.path.sub(%r{^/}, '') # Removes the leading /
141+
142+
quip_thread = quip_client.get_thread(path)
143+
144+
prompt += "# QUIP DOCUMENT\n\n"
145+
# The quip api only returns html which has too much extra junk.
146+
# Convert to md for smaller size
147+
markdown_quip = ReverseMarkdown.convert quip_thread['html']
148+
prompt += markdown_quip
149+
end
147150
end
148151

149152
if assistant.confluence_spaces.present?
@@ -164,7 +167,11 @@ def perform(_message_id)
164167
salesforce_client = Salesforce::Client.new
165168

166169
salesforce_results = salesforce_client.query(assistant.soql)
167-
prompt += JSON.pretty_generate(salesforce_results.map(&:to_h))
170+
prompt += if salesforce_results&.any?
171+
JSON.pretty_generate(salesforce_results.map(&:to_h))
172+
else
173+
'No results from query.'
174+
end
168175
prompt += '</SOQL>'
169176
end
170177

@@ -217,12 +224,8 @@ def perform(_message_id)
217224
llm_message.from = :assistant
218225

219226
begin
220-
start_time = Time.now
221227
llm_message.generating!
222228
llm_message.content = get_generation(llm_message.prompt)
223-
end_time = Time.now
224-
225-
generation_time = end_time - start_time
226229

227230
llm_message.save
228231
llm_message.ready!

app/models/assistant.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ class Assistant < ApplicationRecord
77

88
has_many :assistant_users, dependent: :destroy
99
has_many :users, through: :assistant_users
10-
enum status: { development: 0, ready: 1 }
10+
enum :status, { development: 0, ready: 1 }
1111
validates :name, presence: true
1212
validates :slack_channel_name, uniqueness: true, allow_blank: true
1313
validate :slack_channel_name_starts_with_unique
14+
validate :quip_url_must_contain_quip_domain
1415

1516
validate :libraries_must_be_csv_with_numbers
1617

@@ -61,4 +62,12 @@ def libraries_must_be_csv_with_numbers
6162

6263
errors.add(:libraries, 'must be a valid CSV format with only numbers')
6364
end
65+
66+
def quip_url_must_contain_quip_domain
67+
return if quip_url.blank?
68+
69+
return if quip_url.include?('quip.com')
70+
71+
errors.add(:quip_url, 'Only quip urls are supported.')
72+
end
6473
end

app/models/library_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ class LibraryUser < ApplicationRecord
22
belongs_to :user
33
belongs_to :library
44

5-
enum role: { admin: 0, editor: 1 }
5+
enum :role, { admin: 0, editor: 1 }
66
end

app/models/message.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class Message < ApplicationRecord
66
validates :from, presence: true
77
validates :content, presence: true
88

9-
enum from: { user: 0, assistant: 1 }
10-
enum status: { ready: 0, generating: 1 }
9+
enum :from, { user: 0, assistant: 1 }
10+
enum :status, { ready: 0, generating: 1 }
1111

1212
after_commit :broadcast_message, on: %i[create update]
1313

app/models/question.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Question < ApplicationRecord
1111
# Tracking helpful answers
1212
acts_as_votable
1313

14-
enum status: { pending: 0, generating: 1, generated: 2, failed: 3 }
14+
enum :status, { pending: 0, generating: 1, generated: 2, failed: 3 }
1515

1616
validates :question, presence: true
1717
belongs_to :user, optional: true

app/models/webhook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Webhook < ApplicationRecord
33
belongs_to :library
44
has_many :chats
55

6-
enum hook_type: { pagerduty: 0 }
6+
enum :hook_type, { pagerduty: 0 }
77
validates :hook_type, presence: true
88

99
before_create :generate_secret_key

spec/models/assistant_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,34 @@
2222
expect(assistant.errors[:libraries]).to include('must be a valid CSV format with only numbers')
2323
end
2424
end
25+
26+
context 'quip_url validation' do
27+
it 'is valid with a quip.com URL' do
28+
assistant = Assistant.new(name: 'test', quip_url: 'https://example.quip.com/document/123', input: 'input', user:, instructions: 'instructions', output: 'output')
29+
expect(assistant).to be_valid
30+
end
31+
32+
it 'is valid with blank quip_url' do
33+
assistant = Assistant.new(name: 'test', quip_url: '', input: 'input', user:, instructions: 'instructions', output: 'output')
34+
expect(assistant).to be_valid
35+
end
36+
37+
it 'is valid with nil quip_url' do
38+
assistant = Assistant.new(name: 'test', quip_url: nil, input: 'input', user:, instructions: 'instructions', output: 'output')
39+
expect(assistant).to be_valid
40+
end
41+
42+
it 'is not valid with non-quip URL' do
43+
assistant = Assistant.new(name: 'test', quip_url: 'https://google.com/document/123', input: 'input', user:, instructions: 'instructions', output: 'output')
44+
expect(assistant).not_to be_valid
45+
expect(assistant.errors[:quip_url]).to include('Only quip urls are supported.')
46+
end
47+
48+
it 'is not valid with URL that does not contain quip.com' do
49+
assistant = Assistant.new(name: 'test', quip_url: 'https://example.com/quip/document/123', input: 'input', user:, instructions: 'instructions', output: 'output')
50+
expect(assistant).not_to be_valid
51+
expect(assistant.errors[:quip_url]).to include('Only quip urls are supported.')
52+
end
53+
end
2554
end
2655
end

0 commit comments

Comments
 (0)