Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 9fafaa4

Browse files
committed
try to add ... for elided lines, it appears to work well
1 parent 8e2ffae commit 9fafaa4

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

evals/lib/eval.rb

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ def initialize(path:)
2828
@expected_tool_call = @yaml[:expected_tool_call]
2929
@expected_tool_call.symbolize_keys! if @expected_tool_call
3030

31-
@args[:path] = File.expand_path(File.join(File.dirname(path), @args[:path])) if @args&.key?(
32-
:path,
33-
)
31+
@args.each do |key, value|
32+
if (key.to_s.include?("_path") || key.to_s == "path") && value.is_a?(String)
33+
@args[key] = File.expand_path(File.join(File.dirname(path), value))
34+
end
35+
end
3436
end
3537

3638
def run(llm:)
@@ -44,6 +46,8 @@ def run(llm:)
4446
image_to_text(llm, **args)
4547
when "prompt"
4648
prompt_call(llm, **args)
49+
when "edit_artifact"
50+
edit_artifact(llm, **args)
4751
end
4852

4953
if expected_output
@@ -53,7 +57,7 @@ def run(llm:)
5357
{ result: :fail, expected_output: expected_output, actual_output: result }
5458
end
5559
elsif expected_output_regex
56-
if result.match?(expected_output_regex)
60+
if result.to_s.match?(expected_output_regex)
5761
{ result: :pass }
5862
else
5963
{ result: :fail, expected_output: expected_output_regex, actual_output: result }
@@ -169,4 +173,36 @@ def prompt_call(llm, system_prompt:, message:, tools: nil, stream: false)
169173
end
170174
result
171175
end
176+
177+
def edit_artifact(llm, css_path:, js_path:, html_path:, instructions_path:)
178+
css = File.read(css_path)
179+
js = File.read(js_path)
180+
html = File.read(html_path)
181+
instructions = File.read(instructions_path)
182+
artifact =
183+
AiArtifact.create!(
184+
css: css,
185+
js: js,
186+
html: html,
187+
user_id: Discourse.system_user.id,
188+
post_id: 1,
189+
name: "eval artifact",
190+
)
191+
192+
post = Post.new(topic_id: 1, id: 1)
193+
DiscourseAi::AiBot::ArtifactUpdateStrategies::Diff.new(
194+
llm: llm.llm_model.to_llm,
195+
post: post,
196+
user: Discourse.system_user,
197+
artifact: artifact,
198+
artifact_version: nil,
199+
instructions: instructions,
200+
).apply
201+
202+
version = artifact.versions.last
203+
output = { css: version.css, js: version.js, html: version.html }
204+
205+
artifact.destroy
206+
output
207+
end
172208
end

lib/ai_bot/artifact_update_strategies/diff.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def system_prompt
110110
5. Keep changes minimal and focused
111111
6. HTML should not include <html>, <head>, or <body> tags, it is injected into a template
112112
7. When specifying a SEARCH block, ALWAYS keep it 8 lines or less, you will be interrupted and a retry will be required if you exceed this limit
113+
8. NEVER EVER ask followup questions, ALL changes must be performed in a single response, you are consumed via an API, there is no opportunity for humans in the loop
114+
9. When performing a non-contiguous search, ALWAYS use ... to denote the skipped lines
113115
114116
JavaScript libraries must be sourced from the following CDNs, otherwise CSP will reject it:
115117
#{AiArtifact::ALLOWED_CDN_SOURCES.join("\n")}
@@ -154,6 +156,36 @@ def system_prompt
154156
.text { font-size: 16px; }
155157
>>>>>>> REPLACE
156158
[/CSS]
159+
160+
Example - Non contiguous search in CSS (replace all CSS with new CSS)
161+
162+
Original CSS:
163+
[CSS]
164+
body {
165+
color: red;
166+
}
167+
.button {
168+
color: blue;
169+
}
170+
.alert {
171+
background-color: green;
172+
}
173+
[/CSS]
174+
175+
[CSS]
176+
<<<<<<< SEARCH
177+
body {
178+
...
179+
background-color: green;
180+
}
181+
=======
182+
body {
183+
color: red;
184+
}
185+
>>>>>>> REPLACE
186+
187+
This will replace the entire CSS block with the new CSS block, given that the search block is non-contiguous and unambiguous.
188+
157189
PROMPT
158190
end
159191

lib/utils/diff_utils/simple_diff.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def non_contiguous_match_range(lines, search_lines)
8686
search_index = 0
8787

8888
lines.each_with_index do |line, idx|
89+
search_index += 1 if search_lines[search_index].strip == "..."
8990
if line.strip == search_lines[search_index].strip
9091
first_idx ||= idx
9192
last_idx = idx

spec/lib/utils/diff_utils/simple_diff_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@
185185

186186
search = <<~TEXT
187187
line1
188+
...
188189
line3
190+
...
189191
line1
190192
TEXT
191193

0 commit comments

Comments
 (0)