llm_memory_gmail_loader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e5d1d336263122a16697d534d73ea268ca47e77488a2d4260541530bc3a18521
4
+ data.tar.gz: 6a057cf21fccf27ac53777a123f9db086fb1aa61dd6588745e9896b07291708c
5
+ SHA512:
6
+ metadata.gz: 521bfe0d89d342cff1c0ffb6632460e089b52561f501ceb636f83f9cd825f94e2c9303a2ab182649ebbb7ce019104e4d9846ae9a896505a2fe21fddd4c107333
7
+ data.tar.gz: 34682aa18c7d5f367ce07712af0e521efeb91f5e0bd1bb0f37f830ea9cf6056fe80b6854db9a255fea524444e460bf2926feeeb466d6af690e24ca2546fa50d7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
@@ -0,0 +1,4 @@
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "standardRuby.autofix": true
4
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-08
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at shoheik@cpan.org. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in llm_memory_gmail_loader.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "standard", "~> 1.3"
11
+ gem "googleauth"
12
+ gem "google-apis-gmail_v1"
13
+ gem "llm_memory"
14
+ gem "nokogiri"
15
+ # dev
16
+ gem "pry"
17
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,135 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ llm_memory_gmail_loader (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.4)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ ast (2.4.2)
12
+ coderay (1.1.3)
13
+ declarative (0.0.20)
14
+ diff-lcs (1.5.0)
15
+ dotenv (2.8.1)
16
+ faraday (2.7.4)
17
+ faraday-net_http (>= 2.0, < 3.1)
18
+ ruby2_keywords (>= 0.0.4)
19
+ faraday-net_http (3.0.2)
20
+ google-apis-core (0.11.0)
21
+ addressable (~> 2.5, >= 2.5.1)
22
+ googleauth (>= 0.16.2, < 2.a)
23
+ httpclient (>= 2.8.1, < 3.a)
24
+ mini_mime (~> 1.0)
25
+ representable (~> 3.0)
26
+ retriable (>= 2.0, < 4.a)
27
+ rexml
28
+ webrick
29
+ google-apis-gmail_v1 (0.27.0)
30
+ google-apis-core (>= 0.11.0, < 2.a)
31
+ googleauth (1.5.2)
32
+ faraday (>= 0.17.3, < 3.a)
33
+ jwt (>= 1.4, < 3.0)
34
+ memoist (~> 0.16)
35
+ multi_json (~> 1.11)
36
+ os (>= 0.9, < 2.0)
37
+ signet (>= 0.16, < 2.a)
38
+ httpclient (2.8.3)
39
+ json (2.6.3)
40
+ jwt (2.7.0)
41
+ language_server-protocol (3.17.0.3)
42
+ lint_roller (1.0.0)
43
+ llm_memory (0.1.0)
44
+ memoist (0.16.2)
45
+ method_source (1.0.0)
46
+ mini_mime (1.1.2)
47
+ multi_json (1.15.0)
48
+ nokogiri (1.14.2-arm64-darwin)
49
+ racc (~> 1.4)
50
+ os (1.1.4)
51
+ parallel (1.23.0)
52
+ parser (3.2.2.1)
53
+ ast (~> 2.4.1)
54
+ pry (0.14.2)
55
+ coderay (~> 1.1)
56
+ method_source (~> 1.0)
57
+ public_suffix (5.0.1)
58
+ racc (1.6.2)
59
+ rainbow (3.1.1)
60
+ rake (13.0.6)
61
+ regexp_parser (2.8.0)
62
+ representable (3.2.0)
63
+ declarative (< 0.1.0)
64
+ trailblazer-option (>= 0.1.1, < 0.2.0)
65
+ uber (< 0.2.0)
66
+ retriable (3.1.2)
67
+ rexml (3.2.5)
68
+ rspec (3.12.0)
69
+ rspec-core (~> 3.12.0)
70
+ rspec-expectations (~> 3.12.0)
71
+ rspec-mocks (~> 3.12.0)
72
+ rspec-core (3.12.2)
73
+ rspec-support (~> 3.12.0)
74
+ rspec-expectations (3.12.3)
75
+ diff-lcs (>= 1.2.0, < 2.0)
76
+ rspec-support (~> 3.12.0)
77
+ rspec-mocks (3.12.5)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.12.0)
80
+ rspec-support (3.12.0)
81
+ rubocop (1.50.2)
82
+ json (~> 2.3)
83
+ parallel (~> 1.10)
84
+ parser (>= 3.2.0.0)
85
+ rainbow (>= 2.2.2, < 4.0)
86
+ regexp_parser (>= 1.8, < 3.0)
87
+ rexml (>= 3.2.5, < 4.0)
88
+ rubocop-ast (>= 1.28.0, < 2.0)
89
+ ruby-progressbar (~> 1.7)
90
+ unicode-display_width (>= 2.4.0, < 3.0)
91
+ rubocop-ast (1.28.1)
92
+ parser (>= 3.2.1.0)
93
+ rubocop-performance (1.16.0)
94
+ rubocop (>= 1.7.0, < 2.0)
95
+ rubocop-ast (>= 0.4.0)
96
+ ruby-progressbar (1.13.0)
97
+ ruby2_keywords (0.0.5)
98
+ signet (0.17.0)
99
+ addressable (~> 2.8)
100
+ faraday (>= 0.17.5, < 3.a)
101
+ jwt (>= 1.5, < 3.0)
102
+ multi_json (~> 1.10)
103
+ standard (1.28.2)
104
+ language_server-protocol (~> 3.17.0.2)
105
+ lint_roller (~> 1.0)
106
+ rubocop (~> 1.50.2)
107
+ standard-custom (~> 1.0.0)
108
+ standard-performance (~> 1.0.1)
109
+ standard-custom (1.0.0)
110
+ lint_roller (~> 1.0)
111
+ standard-performance (1.0.1)
112
+ lint_roller (~> 1.0)
113
+ rubocop-performance (~> 1.16.0)
114
+ trailblazer-option (0.1.2)
115
+ uber (0.1.0)
116
+ unicode-display_width (2.4.2)
117
+ webrick (1.8.1)
118
+
119
+ PLATFORMS
120
+ arm64-darwin-22
121
+
122
+ DEPENDENCIES
123
+ dotenv
124
+ google-apis-gmail_v1
125
+ googleauth
126
+ llm_memory
127
+ llm_memory_gmail_loader!
128
+ nokogiri
129
+ pry
130
+ rake (~> 13.0)
131
+ rspec (~> 3.0)
132
+ standard (~> 1.3)
133
+
134
+ BUNDLED WITH
135
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Shohei Kameda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # LLM Memory Gmail Loader
2
+
3
+ The LLM Memory Gmail Loader is a loader plugin designed for the `llm_memory` gem. It enables the retrieval of emails using the Gmail API. To use this plugin, please ensure you have the `llm_memory` gem installed.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ The following syntax is used to load emails:
20
+
21
+ - `emails`: (required) An array of email addresses you want to retrieve.
22
+ - `limit`: (optional) The maximum number of emails to retrieve for each email address. The default value is 100.
23
+ - `query`: A text query to filter emails in Gmail. The default value is "label:sent"
24
+
25
+ ```ruby
26
+ documents = LlmMemory::Wernicke.load(
27
+ :gmail,
28
+ emails: ["my@example.com"],
29
+ query: "label:sent",
30
+ limit: 10
31
+ )
32
+ # [{
33
+ # content: "subject\nbody",
34
+ # metadata: {
35
+ # subject: subject,
36
+ # from: "xx@example.com"
37
+ # }
38
+ # },,,]
39
+ ```
40
+
41
+ ## Prepare Auth keys
42
+
43
+ ### 1. Enable the Gmail API:
44
+
45
+ 1. Go to the Google API Console: https://console.developers.google.com/
46
+ 2. Create a new project or select an existing one.
47
+ 3. Click on "Enable APIs and Services" and search for "Gmail API".
48
+ 4. Enable the Gmail API for your project.
49
+
50
+ ### 2. Create credentials:
51
+
52
+ 1. In the API Console, go to the "Credentials" tab.
53
+ 2. Click on "Create credentials" and select "Service account".
54
+ 3. Fill in the required information and click "Create".
55
+ 4. Grant the necessary roles, such as "Google Workspace Domain-wide Delegation".
56
+ 5. Download the JSON key file for the service account.
57
+
58
+ ### 3. Delegate domain-wide authority to the service account:
59
+
60
+ Follow the instructions in this guide to delegate domain-wide authority: https://developers.google.com/identity/protocols/oauth2/service-account#delegatingauthority
61
+
62
+ ## Configuration
63
+
64
+ To configure the Gmail Loader, you'll need to set up an initializer. By default, it uses the following environment variables:
65
+
66
+ ```
67
+ GOOGLE_PROJECT_ID
68
+ GOOGLE_PRIVATE_KEY_ID
69
+ GOOGLE_PRIVATE_KEY
70
+ GOOGLE_CLIENT_EMAIL
71
+ GOOGLE_CLIENT_ID
72
+ ```
73
+
74
+ ### Setting up the Gmail Loader
75
+
76
+ 1. Download the service account JSON file and copy the keys.
77
+ 2. Add the following configuration block in your initializer:
78
+
79
+ ```ruby
80
+ LlmMemoryGmailLoader.configure do |config|
81
+ config.google_project_id = "<your_google_project_id>"
82
+ config.google_private_key_id = "<your_google_private_key_id>"
83
+ config.google_private_key = "<your_google_private_key>"
84
+ config.google_client_email = "<your_google_client_email>"
85
+ config.google_client_id = "<your_google_client_id>"
86
+ end
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
92
+
93
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/llm_memory_gmail_loader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/llm_memory_gmail_loader/blob/master/CODE_OF_CONDUCT.md).
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
102
+
103
+ ## Code of Conduct
104
+
105
+ Everyone interacting in the LlmMemoryGmailLoader project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/llm_memory_gmail_loader/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,13 @@
1
+ module LlmMemoryGmailLoader
2
+ class Configuration
3
+ attr_accessor :google_project_id, :google_private_key_id, :google_private_key, :google_client_email, :google_client_id
4
+
5
+ def initialize
6
+ @google_project_id = ENV["GOOGLE_PROJECT_ID"]
7
+ @google_private_key_id = ENV["GOOGLE_PRIVATE_KEY_ID"]
8
+ @google_private_key = ENV["GOOGLE_PRIVATE_KEY"]
9
+ @google_client_email = ENV["GOOGLE_CLIENT_EMAIL"]
10
+ @google_client_id = ENV["GOOGLE_CLIENT_ID"]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LlmMemoryGmailLoader
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "google/apis/gmail_v1"
4
+ require "googleauth"
5
+ require "nokogiri"
6
+
7
+ require_relative "llm_memory_gmail_loader/version"
8
+ require_relative "llm_memory_gmail_loader/configuration"
9
+ require "llm_memory/loader"
10
+
11
+ module LlmMemoryGmailLoader
12
+ class Error < StandardError; end
13
+
14
+ class << self
15
+ attr_accessor :configuration
16
+ end
17
+
18
+ def self.configure
19
+ self.configuration ||= Configuration.new
20
+ yield(configuration) if block_given?
21
+ end
22
+
23
+ class GmailLoader
24
+ include LlmMemory::Loader
25
+
26
+ register_loader :gmail
27
+
28
+ def initialize
29
+ LlmMemoryGmailLoader.configure
30
+ end
31
+
32
+ def authorize
33
+ key_content = {
34
+ "type" => "service_account",
35
+ "project_id" => LlmMemoryGmailLoader.configuration.google_project_id,
36
+ "private_key_id" => LlmMemoryGmailLoader.configuration.google_private_key_id,
37
+ "private_key" => LlmMemoryGmailLoader.configuration.google_private_key,
38
+ "client_email" => LlmMemoryGmailLoader.configuration.google_client_email,
39
+ "client_id" => LlmMemoryGmailLoader.configuration.google_client_id,
40
+ "auth_uri" => "https://accounts.google.com/o/oauth2/auth",
41
+ "token_uri" => "https://oauth2.googleapis.com/token",
42
+ "auth_provider_x509_cert_url" => "https://www.googleapis.com/oauth2/v1/certs",
43
+ "client_x509_cert_url" => "https://www.googleapis.com/robot/v1/metadata/x509/#{LlmMemoryGmailLoader.configuration.google_client_email}"
44
+ }
45
+
46
+ Google::Auth::ServiceAccountCredentials.make_creds(
47
+ json_key_io: StringIO.new(JSON.dump(key_content)),
48
+ scope: Google::Apis::GmailV1::AUTH_GMAIL_READONLY
49
+ )
50
+ # authorizer.sub = "user@example.com" # Replace with the user's email address
51
+ end
52
+
53
+ def list_emails(email:, limit: 100, query: "label:sent")
54
+ @service.authorization.sub = email
55
+ next_page_token = nil
56
+ sent_emails = []
57
+ count = 0
58
+ max_results = (limit < 100) ? limit : 100
59
+ loop do
60
+ result = @service.list_user_messages("me", q: query, page_token: next_page_token, max_results: max_results)
61
+ sent_emails.concat(result.messages) if result.messages
62
+ count += result.messages.length
63
+ next_page_token = result.next_page_token
64
+ break if next_page_token.nil?
65
+ break if count > limit
66
+ end
67
+ sent_emails
68
+ end
69
+
70
+ def get_email_subject(headers)
71
+ subject_header = headers.find { |header| header.name == "Subject" }
72
+ subject_header&.value
73
+ end
74
+
75
+ def get_email_from(headers)
76
+ from_header = headers.find { |header| header.name == "From" }
77
+ if from_header
78
+ from_header.value
79
+ else
80
+ ""
81
+ end
82
+ end
83
+
84
+ def extract_email_bodies(payload)
85
+ if payload.mime_type == "text/plain" || payload.mime_type == "text/html"
86
+ {
87
+ text: (payload.mime_type == "text/plain") ? payload.body.data : nil,
88
+ html: (payload.mime_type == "text/html") ? payload.body.data : nil
89
+ }
90
+ elsif payload.mime_type.start_with?("multipart/")
91
+ text_body = nil
92
+ html_body = nil
93
+
94
+ payload.parts.each do |part|
95
+ if part.mime_type == "text/plain"
96
+ text_body = part.body.data
97
+ elsif part.mime_type == "text/html"
98
+ html_body = part.body.data
99
+ end
100
+ end
101
+
102
+ {
103
+ text: text_body,
104
+ html: html_body
105
+ }
106
+ else
107
+ {
108
+ text: nil,
109
+ html: nil
110
+ }
111
+ end
112
+ end
113
+
114
+ def extract_text_from_html(html)
115
+ utf8_html = html.force_encoding("UTF-8")
116
+ document = Nokogiri::HTML(utf8_html)
117
+ document.css("style").remove
118
+ document.css("br").each { |br| br.replace("\n") }
119
+ document.css("a").each do |a|
120
+ link = a["href"]
121
+ text = a.text
122
+ a.replace("#{text} (#{link})") # Replace the <a> tag with its text content and link
123
+ end
124
+ document.text.gsub(/ /, " ").gsub(/\s+/, " ").gsub(/(\r\n)+|\n+/, "\n").strip
125
+ end
126
+
127
+ def create_documents(emails)
128
+ docs = []
129
+ emails.each do |email|
130
+ message = @service.get_user_message("me", email.id)
131
+ subject = get_email_subject(message.payload.headers)
132
+ from = get_email_from(message.payload.headers)
133
+ bodies = extract_email_bodies(message.payload)
134
+ if bodies[:text]
135
+ body = bodies[:text].force_encoding("UTF-8").gsub(/ /, " ").gsub(/\s+/, " ").strip
136
+ elsif bodies[:html]
137
+ body = extract_text_from_html(bodies[:html])
138
+ else
139
+ next
140
+ end
141
+
142
+ docs.push({
143
+ content: "#{subject}\n#{body}",
144
+ metadata: {
145
+ subject: subject,
146
+ from: from
147
+ }
148
+ })
149
+ end
150
+ docs
151
+ end
152
+
153
+ def load(args)
154
+ emails = args[:emails]
155
+ limit = args[:limit]
156
+ raise "emails is required" if emails.nil? || emails.length == 0
157
+
158
+ @service ||= Google::Apis::GmailV1::GmailService.new
159
+ @service.authorization = authorize
160
+
161
+ results = []
162
+ emails.each do |email|
163
+ results += if limit.nil?
164
+ list_emails(email: email)
165
+ else
166
+ list_emails(email: email, limit: limit)
167
+ end
168
+ end
169
+
170
+ create_documents(results)
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/llm_memory_gmail_loader/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "llm_memory_gmail_loader"
7
+ spec.version = LlmMemoryGmailLoader::VERSION
8
+ spec.authors = ["Shohei Kameda"]
9
+ spec.email = ["shoheik@cpan.org"]
10
+
11
+ spec.summary = "A LLM Memory plugin to load data from GMail"
12
+ spec.description = "Loading data from GMail using API"
13
+ spec.homepage = "https://github.com/shohey1226/llm_memory_gmail_loader"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/shohey1226/llm_memory_gmail_loader"
19
+ spec.metadata["changelog_uri"] = "https://github.com/shohey1226/llm_memory_gmail_loader/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, check out our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
@@ -0,0 +1,4 @@
1
+ module LlmMemoryGmailLoader
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: llm_memory_gmail_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shohei Kameda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Loading data from GMail using API
14
+ email:
15
+ - shoheik@cpan.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".standard.yml"
22
+ - ".vscode/settings.json"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - lib/llm_memory_gmail_loader.rb
31
+ - lib/llm_memory_gmail_loader/configuration.rb
32
+ - lib/llm_memory_gmail_loader/version.rb
33
+ - llm_memory_gmail_loader.gemspec
34
+ - sig/llm_memory_gmail_loader.rbs
35
+ homepage: https://github.com/shohey1226/llm_memory_gmail_loader
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/shohey1226/llm_memory_gmail_loader
40
+ source_code_uri: https://github.com/shohey1226/llm_memory_gmail_loader
41
+ changelog_uri: https://github.com/shohey1226/llm_memory_gmail_loader/CHANGELOG.md
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.6.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.4.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A LLM Memory plugin to load data from GMail
61
+ test_files: []