playpath_rails 0.1.0 → 0.1.2
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 +4 -4
- data/Rakefile +2 -2
- data/examples/usage_example.rb +25 -27
- data/lib/playpath_rails/client.rb +13 -11
- data/lib/playpath_rails/errors.rb +1 -1
- data/lib/playpath_rails/generators/migration_generator.rb +5 -5
- data/lib/playpath_rails/rag.rb +1 -1
- data/lib/playpath_rails/synchronizable.rb +11 -18
- data/lib/playpath_rails/version.rb +1 -1
- data/lib/playpath_rails.rb +13 -6
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac9097b1267cb10d0bfab681f55a99b943b672aef202204d8a0af3a4be6bd6b5
|
4
|
+
data.tar.gz: d16c7e7393a6779303b3568c177a945aec790f8ad0ada4aba6393359d4fb9a1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb6decc4bfb9d6e7a17aa3cb4a594341c79641cefe65f577f782bfad1572c8da4250d0ea6d9bad1750a07e1a9f1157f7a9a149987072942c1e6d8cb6b818bbcd
|
7
|
+
data.tar.gz: '07879dd854cbd51d41c89574aa148f58f921264cd3ce09be5fae52db87447e47229258c18f1e26da8f8b277d8853b3b3533cc04ea33206068c2f351b049096ef'
|
data/Rakefile
CHANGED
data/examples/usage_example.rb
CHANGED
@@ -11,31 +11,30 @@ PlaypathRails.configure do |config|
|
|
11
11
|
config.base_url = 'https://playpath.io'
|
12
12
|
end
|
13
13
|
|
14
|
-
puts
|
15
|
-
puts
|
14
|
+
puts 'PlaypathRails Example Usage'
|
15
|
+
puts '=' * 40
|
16
16
|
|
17
17
|
# Example 1: Direct API usage
|
18
18
|
puts "\n1. Direct API Usage:"
|
19
19
|
begin
|
20
20
|
client = PlaypathRails.client
|
21
|
-
|
21
|
+
|
22
22
|
# Create an item
|
23
|
-
puts
|
23
|
+
puts 'Creating an item...'
|
24
24
|
item = client.create_item(
|
25
|
-
title:
|
26
|
-
url:
|
27
|
-
text:
|
28
|
-
tags: [
|
25
|
+
title: 'Ruby Programming Basics',
|
26
|
+
url: 'https://example.com/ruby-basics',
|
27
|
+
text: 'Learn the fundamentals of Ruby programming language',
|
28
|
+
tags: %w[ruby programming tutorial]
|
29
29
|
)
|
30
30
|
puts "Created item: #{item['title']} (ID: #{item['id']})"
|
31
|
-
|
31
|
+
|
32
32
|
# List items
|
33
33
|
puts "\nListing items..."
|
34
34
|
items = client.list_items
|
35
35
|
puts "Found #{items.length} items"
|
36
|
-
|
37
36
|
rescue PlaypathRails::AuthenticationError
|
38
|
-
puts
|
37
|
+
puts 'Error: Please configure a valid API key'
|
39
38
|
rescue PlaypathRails::APIError => e
|
40
39
|
puts "API Error: #{e.message}"
|
41
40
|
end
|
@@ -44,40 +43,39 @@ end
|
|
44
43
|
puts "\n2. RAG Chat Usage:"
|
45
44
|
begin
|
46
45
|
# Simple question
|
47
|
-
puts
|
48
|
-
response = PlaypathRails::RAG.ask(
|
46
|
+
puts 'Asking a simple question...'
|
47
|
+
response = PlaypathRails::RAG.ask('What is Ruby programming?')
|
49
48
|
puts "Response: #{response}"
|
50
|
-
|
49
|
+
|
51
50
|
# Chat with history
|
52
51
|
puts "\nChat with conversation history..."
|
53
52
|
history = PlaypathRails::RAG.build_history(
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
'What is Ruby?',
|
54
|
+
'Ruby is a dynamic programming language...',
|
55
|
+
'What are its main features?'
|
57
56
|
)
|
58
|
-
|
57
|
+
|
59
58
|
result = PlaypathRails::RAG.chat(
|
60
|
-
message:
|
59
|
+
message: 'Can you give me some examples?',
|
61
60
|
history: history
|
62
61
|
)
|
63
62
|
puts "Response: #{result['reply']}"
|
64
63
|
puts "Usage: #{result['usage']}/#{result['limit']}" if result['usage']
|
65
|
-
|
66
64
|
rescue PlaypathRails::AuthenticationError
|
67
|
-
puts
|
65
|
+
puts 'Error: Please configure a valid API key'
|
68
66
|
rescue PlaypathRails::TrialLimitError
|
69
|
-
puts
|
67
|
+
puts 'Error: Trial limit exceeded'
|
70
68
|
rescue PlaypathRails::APIError => e
|
71
69
|
puts "API Error: #{e.message}"
|
72
70
|
end
|
73
71
|
|
74
72
|
# Example 3: Model synchronization (simulated)
|
75
73
|
puts "\n3. Model Synchronization Example:"
|
76
|
-
puts
|
74
|
+
puts 'In a Rails application, you would include the Synchronizable module:'
|
77
75
|
puts <<~RUBY
|
78
76
|
class Article < ApplicationRecord
|
79
77
|
include PlaypathRails::Synchronizable
|
80
|
-
|
78
|
+
#{' '}
|
81
79
|
# Configure synchronization
|
82
80
|
playpath_sync(
|
83
81
|
title_field: :title,
|
@@ -87,7 +85,7 @@ puts <<~RUBY
|
|
87
85
|
tags: ['article', 'blog']
|
88
86
|
)
|
89
87
|
end
|
90
|
-
|
88
|
+
|
91
89
|
# Then create/update records normally:
|
92
90
|
article = Article.create!(
|
93
91
|
title: "My Blog Post",
|
@@ -95,10 +93,10 @@ puts <<~RUBY
|
|
95
93
|
permalink: "https://myblog.com/my-post",
|
96
94
|
tag_list: ["ruby", "rails"]
|
97
95
|
)
|
98
|
-
|
96
|
+
|
99
97
|
# The record will be automatically synced to PlayPath.io
|
100
98
|
# You can also manually sync:
|
101
99
|
article.sync_to_playpath!
|
102
100
|
RUBY
|
103
101
|
|
104
|
-
puts "\nExample completed!"
|
102
|
+
puts "\nExample completed!"
|
@@ -86,16 +86,14 @@ module PlaypathRails
|
|
86
86
|
|
87
87
|
request = request_class.new(uri)
|
88
88
|
request['Content-Type'] = 'application/json'
|
89
|
-
|
89
|
+
|
90
90
|
# Set authentication header
|
91
91
|
api_key = configuration.api_key_for(endpoint_type)
|
92
92
|
raise AuthenticationError, 'API key not configured' unless api_key
|
93
|
-
|
93
|
+
|
94
94
|
request['X-Api-Key'] = api_key
|
95
95
|
|
96
|
-
if body
|
97
|
-
request.body = JSON.generate(body)
|
98
|
-
end
|
96
|
+
request.body = JSON.generate(body) if body
|
99
97
|
|
100
98
|
request
|
101
99
|
end
|
@@ -104,6 +102,7 @@ module PlaypathRails
|
|
104
102
|
case response.code.to_i
|
105
103
|
when 200, 201
|
106
104
|
return nil if response.body.nil? || response.body.empty?
|
105
|
+
|
107
106
|
JSON.parse(response.body)
|
108
107
|
when 204
|
109
108
|
nil
|
@@ -116,9 +115,10 @@ module PlaypathRails
|
|
116
115
|
error_data = parse_error_response(response)
|
117
116
|
if error_data[:message]&.include?('Trial limit')
|
118
117
|
raise TrialLimitError.new(error_data[:message], status_code: 403, response_body: response.body)
|
119
|
-
else
|
120
|
-
raise APIError.new('Forbidden', status_code: 403, response_body: response.body)
|
121
118
|
end
|
119
|
+
|
120
|
+
raise APIError.new('Forbidden', status_code: 403, response_body: response.body)
|
121
|
+
|
122
122
|
when 404
|
123
123
|
raise NotFoundError.new('Resource not found', status_code: 404, response_body: response.body)
|
124
124
|
when 422
|
@@ -129,18 +129,20 @@ module PlaypathRails
|
|
129
129
|
raise RateLimitError.new('Rate limit exceeded', status_code: 429, response_body: response.body)
|
130
130
|
when 502
|
131
131
|
error_data = parse_error_response(response)
|
132
|
-
raise ExternalServiceError.new(error_data[:message] || 'Bad Gateway', status_code: 502,
|
132
|
+
raise ExternalServiceError.new(error_data[:message] || 'Bad Gateway', status_code: 502,
|
133
|
+
response_body: response.body)
|
133
134
|
else
|
134
|
-
raise APIError.new("HTTP #{response.code}: #{response.message}", status_code: response.code.to_i,
|
135
|
+
raise APIError.new("HTTP #{response.code}: #{response.message}", status_code: response.code.to_i,
|
136
|
+
response_body: response.body)
|
135
137
|
end
|
136
138
|
end
|
137
139
|
|
138
140
|
def parse_error_response(response)
|
139
141
|
return { message: 'Unknown error' } if response.body.nil? || response.body.empty?
|
140
|
-
|
142
|
+
|
141
143
|
JSON.parse(response.body, symbolize_names: true)
|
142
144
|
rescue JSON::ParserError
|
143
145
|
{ message: response.body }
|
144
146
|
end
|
145
147
|
end
|
146
|
-
end
|
148
|
+
end
|
@@ -9,12 +9,12 @@ module PlaypathRails
|
|
9
9
|
include ActiveRecord::Generators::Migration
|
10
10
|
|
11
11
|
source_root File.expand_path('templates', __dir__)
|
12
|
-
|
13
|
-
argument :model_name, type: :string, desc:
|
12
|
+
|
13
|
+
argument :model_name, type: :string, desc: 'The model to add PlayPath synchronization to'
|
14
14
|
|
15
15
|
def create_migration_file
|
16
|
-
migration_template 'add_playpath_item_id_migration.rb.erb',
|
17
|
-
|
16
|
+
migration_template 'add_playpath_item_id_migration.rb.erb',
|
17
|
+
"db/migrate/add_playpath_item_id_to_#{table_name}.rb"
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
@@ -28,4 +28,4 @@ module PlaypathRails
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
data/lib/playpath_rails/rag.rb
CHANGED
@@ -9,12 +9,8 @@ module PlaypathRails
|
|
9
9
|
|
10
10
|
included do
|
11
11
|
# Only add callbacks if the host class supports them
|
12
|
-
if respond_to?(:after_commit)
|
13
|
-
|
14
|
-
end
|
15
|
-
if respond_to?(:before_destroy)
|
16
|
-
before_destroy :playpath_delete!
|
17
|
-
end
|
12
|
+
after_commit :playpath_sync!, on: %i[create update] if respond_to?(:after_commit)
|
13
|
+
before_destroy :playpath_delete! if respond_to?(:before_destroy)
|
18
14
|
end
|
19
15
|
|
20
16
|
class_methods do
|
@@ -49,7 +45,7 @@ module PlaypathRails
|
|
49
45
|
|
50
46
|
begin
|
51
47
|
item_data = build_item_data
|
52
|
-
|
48
|
+
|
53
49
|
if playpath_item_id && playpath_item_id != 0
|
54
50
|
# Update existing item
|
55
51
|
PlaypathRails.client.update_item(playpath_item_id, **item_data)
|
@@ -57,11 +53,9 @@ module PlaypathRails
|
|
57
53
|
# Create new item
|
58
54
|
response = PlaypathRails.client.create_item(**item_data)
|
59
55
|
# Store the item ID if the model supports it
|
60
|
-
if respond_to?(:playpath_item_id=) && response&.dig('id')
|
61
|
-
update_column(:playpath_item_id, response['id'])
|
62
|
-
end
|
56
|
+
update_column(:playpath_item_id, response['id']) if respond_to?(:playpath_item_id=) && response&.dig('id')
|
63
57
|
end
|
64
|
-
|
58
|
+
|
65
59
|
true
|
66
60
|
rescue PlaypathRails::Error => e
|
67
61
|
# Log the error but don't raise it to avoid breaking the application
|
@@ -95,6 +89,7 @@ module PlaypathRails
|
|
95
89
|
# Get the PlayPath item ID for this record
|
96
90
|
def playpath_item_id
|
97
91
|
return nil unless respond_to?(:playpath_item_id)
|
92
|
+
|
98
93
|
read_attribute(:playpath_item_id)
|
99
94
|
end
|
100
95
|
|
@@ -111,9 +106,7 @@ module PlaypathRails
|
|
111
106
|
return false unless title_value && !title_value.to_s.empty?
|
112
107
|
|
113
108
|
# If 'only' fields are specified, check if any of them changed
|
114
|
-
|
115
|
-
return options[:only].any? { |field| saved_change_to_attribute?(field) }
|
116
|
-
end
|
109
|
+
return options[:only].any? { |field| saved_change_to_attribute?(field) } if options[:only]&.any?
|
117
110
|
|
118
111
|
true
|
119
112
|
end
|
@@ -140,10 +133,10 @@ module PlaypathRails
|
|
140
133
|
|
141
134
|
# Tags handling
|
142
135
|
tags = []
|
143
|
-
|
136
|
+
|
144
137
|
# Add static tags
|
145
|
-
tags.concat(options[:tags]) if options[:tags]
|
146
|
-
|
138
|
+
tags.concat(options[:tags]) if options[:tags]&.any?
|
139
|
+
|
147
140
|
# Add dynamic tags from field
|
148
141
|
if options[:tags_field] && respond_to?(options[:tags_field])
|
149
142
|
field_tags = send(options[:tags_field])
|
@@ -161,4 +154,4 @@ module PlaypathRails
|
|
161
154
|
data
|
162
155
|
end
|
163
156
|
end
|
164
|
-
end
|
157
|
+
end
|
data/lib/playpath_rails.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
3
|
+
require_relative 'playpath_rails/version'
|
4
|
+
require_relative 'playpath_rails/errors'
|
5
|
+
require_relative 'playpath_rails/client'
|
6
|
+
require_relative 'playpath_rails/synchronizable'
|
7
|
+
require_relative 'playpath_rails/rag'
|
8
|
+
|
9
|
+
# Load railtie if Rails is available
|
10
|
+
begin
|
11
|
+
require_relative 'playpath_rails/railtie' if defined?(Rails::Railtie)
|
12
|
+
rescue LoadError
|
13
|
+
# Silently ignore if Rails is not available
|
14
|
+
end
|
8
15
|
|
9
16
|
module PlaypathRails
|
10
17
|
class Error < StandardError; end
|
@@ -43,7 +50,7 @@ module PlaypathRails
|
|
43
50
|
def initialize
|
44
51
|
@api_key = nil
|
45
52
|
@embeddings_api_key = nil
|
46
|
-
@base_url =
|
53
|
+
@base_url = 'https://playpath.io'
|
47
54
|
end
|
48
55
|
|
49
56
|
# Get the appropriate API key for the request type
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: playpath_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joran Kikke
|
@@ -121,33 +121,33 @@ dependencies:
|
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '2.0'
|
123
123
|
- !ruby/object:Gem::Dependency
|
124
|
-
name:
|
124
|
+
name: simplecov
|
125
125
|
requirement: !ruby/object:Gem::Requirement
|
126
126
|
requirements:
|
127
127
|
- - "~>"
|
128
128
|
- !ruby/object:Gem::Version
|
129
|
-
version: '
|
129
|
+
version: '0.22'
|
130
130
|
type: :development
|
131
131
|
prerelease: false
|
132
132
|
version_requirements: !ruby/object:Gem::Requirement
|
133
133
|
requirements:
|
134
134
|
- - "~>"
|
135
135
|
- !ruby/object:Gem::Version
|
136
|
-
version: '
|
136
|
+
version: '0.22'
|
137
137
|
- !ruby/object:Gem::Dependency
|
138
|
-
name:
|
138
|
+
name: webmock
|
139
139
|
requirement: !ruby/object:Gem::Requirement
|
140
140
|
requirements:
|
141
141
|
- - "~>"
|
142
142
|
- !ruby/object:Gem::Version
|
143
|
-
version: '0
|
143
|
+
version: '3.0'
|
144
144
|
type: :development
|
145
145
|
prerelease: false
|
146
146
|
version_requirements: !ruby/object:Gem::Requirement
|
147
147
|
requirements:
|
148
148
|
- - "~>"
|
149
149
|
- !ruby/object:Gem::Version
|
150
|
-
version: '0
|
150
|
+
version: '3.0'
|
151
151
|
description: |-
|
152
152
|
Provides integration between ActiveRecord and the Items API at playpath.io,
|
153
153
|
enabling automatic synchronization of records between your Rails application
|
@@ -190,7 +190,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
190
190
|
requirements:
|
191
191
|
- - ">="
|
192
192
|
- !ruby/object:Gem::Version
|
193
|
-
version:
|
193
|
+
version: 2.6.0
|
194
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
195
195
|
requirements:
|
196
196
|
- - ">="
|