Top Level Namespace
Defined Under Namespace
Modules: AICallable, GenericParams, GlimAI, GlimHelpers
Classes: APILimiter, AnthropicRequestDetails, AnthropicResponse, ChatRequestDetails, ChatResponse, GlimContext, GlimRequest, GlimResponse, GlimResponseError, LLMError, RateLimitExceededError
Constant Summary
collapse
- PROJECT_HOME =
Bundler.root.to_s
- CACHE_PATH =
ENV['LLM_CACHE_DIRECTORY'] || 'glim_cache'
Instance Method Summary
collapse
-
#deep_copy_with_mods(object, string_cutoff = 80, array_cutoff = 10) ⇒ Object
-
#extract_and_save_files(input_string, base_path = nil) ⇒ Object
-
#extract_between_markers(str, start_marker, end_marker) ⇒ Object
-
#extract_json(json_string) ⇒ Object
-
#extract_with_markers(str, start_marker, end_marker) ⇒ Object
-
#levenshtein_distance(str1, str2) ⇒ Object
-
#putt(topic, s, options = []) ⇒ Object
-
#save_file(filename, content, base_path) ⇒ Object
Instance Method Details
#deep_copy_with_mods(object, string_cutoff = 80, array_cutoff = 10) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/globals.rb', line 202
def deep_copy_with_mods(object, string_cutoff=80, array_cutoff=10)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[key] = deep_copy_with_mods(value, string_cutoff, array_cutoff)
end
when Array
if object.length > array_cutoff
object[0...array_cutoff].map do |value|
deep_copy_with_mods(value, string_cutoff, array_cutoff)
end << ["... (#{object.length - array_cutoff} more)"]
else
object.dup
end
when String
if object.length > string_cutoff
truncated_string = object[0...string_cutoff]
digest = Digest::SHA1.hexdigest(object)[0..7]
"#{truncated_string}..#{digest} #{object.length}b}"
else
object
end
else
object
end
end
|
#extract_and_save_files(input_string, base_path = nil) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/globals.rb', line 66
def extract_and_save_files(input_string, base_path=nil)
files = {}
info_text = ""
parts = input_string.split(/(<file pathname="[^"]+">|<\/file>)/)
in_file = false
parts.each_with_index do |part, idx|
if part =~ /<file pathname="([^"]+)">/
pathname = $1.strip
content = parts[idx + 1]
files[pathname] = content.strip
if base_path
save_file(pathname, content, base_path)
end
in_file = true
info_text += part
elsif part == "</file>"
in_file = false
else
info_text += part unless in_file
end
end
return info_text, files
end
|
230
231
232
|
# File 'lib/globals.rb', line 230
def (str, start_marker, end_marker)
str[/#{Regexp.escape(start_marker)}(.*?)#{Regexp.escape(end_marker)}/m, 1]
end
|
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/globals.rb', line 239
def (json_string)
str = json_string.dup
begin
return JSON.parse(str)
rescue JSON::ParserError => e
putt :extract_json, "JSON parse error 1: #{e}"
end
str = (str, "{","}")
begin
return JSON.parse(str)
rescue JSON::ParserError => e
putt :extract_json, "JSON parse error 2: #{e}"
end
str.gsub!(/,\s*}/, "}")
return JSON.parse(str)
end
|
234
235
236
|
# File 'lib/globals.rb', line 234
def (str, start_marker, end_marker)
str[/#{Regexp.escape(start_marker)}.*?#{Regexp.escape(end_marker)}/m]
end
|
#levenshtein_distance(str1, str2) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/globals.rb', line 134
def levenshtein_distance(str1, str2)
raise ArgumentError, "str1 must be a String" unless str1.is_a?(String)
raise ArgumentError, "str2 must be a String" unless str2.is_a?(String)
matrix = Array.new(str1.length + 1) { Array.new(str2.length + 1, 0) }
edits = ""
(1..str1.length).each { |i| matrix[i][0] = i }
(1..str2.length).each { |j| matrix[0][j] = j }
(1..str1.length).each do |i|
(1..str2.length).each do |j|
cost = str1[i - 1] == str2[j - 1] ? 0 : 1
matrix[i][j] = [
matrix[i - 1][j] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j - 1] + cost
].min
end
end
i, j = str1.length, str2.length
while i > 0 && j > 0
min_val = [matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1]].min
if min_val == matrix[i-1][j-1]
edits << (str1[i - 1] == str2[j - 1] ? "M" : "S")
i -= 1
j -= 1
elsif min_val == matrix[i-1][j]
edits << "D"
i -= 1
else
edits << "I"
j -= 1
end
end
while i > 0
edits << "D"
i -= 1
end
while j > 0
edits << "I"
j -= 1
end
explanation = "#{str1}\n#{str2}\n#{edits.reverse}\n}"
putt :levenshtein_distance, explanation
[matrix[str1.length][str2.length], edits.reverse]
end
|
#putt(topic, s, options = []) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/globals.rb', line 186
def putt(topic, s, options = [])
return unless [:warning, :include_files].include?(topic)
t = Time.now - $putt_timestamp
line = "#{t.round(3)}: " + "#{s}"
puts(line)
options = []
if options.include?(:trace)
for i in 1..([caller.length, 20].min)
puts "\t"+caller[i]
end
end
end
|
#save_file(filename, content, base_path) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/globals.rb', line 115
def save_file(filename, content, base_path)
putt :file_helper, "Saving file #{filename} to #{base_path}, #{content.length} chars"
relative_path = filename
path = File.join(base_path, relative_path)
directory = File.dirname(path)
begin
if path && (File.exist?(path) || Dir.exist?(path))
timestamp = Time.now.strftime('%Y%m%d%H%M%S')
FileUtils.mv(path, File.join(directory, "old-#{timestamp}-#{File.basename(path)}"))
end
FileUtils.mkdir_p(directory) unless Dir.exist?(directory)
File.open(path, 'w') { |file| file.write(content) } unless content.to_s.empty?
rescue StandardError => e
puts "An error occurred while processing file at #{path}: #{e.message} - #{e.backtrace.join("\n")}"
end
end
|