|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module DiscourseAi |
| 4 | + module AiBot |
| 5 | + module Tools |
| 6 | + class UpdateArtifact < Tool |
| 7 | + def self.name |
| 8 | + "update_artifact" |
| 9 | + end |
| 10 | + |
| 11 | + def self.unified_diff_tip |
| 12 | + <<~TIP |
| 13 | + When updating and artifact provied unified diffs, for example: |
| 14 | +
|
| 15 | + If editing: |
| 16 | +
|
| 17 | + <div> |
| 18 | + <p>Some text</p> |
| 19 | + </div> |
| 20 | +
|
| 21 | + You can provide a diff like: |
| 22 | +
|
| 23 | + <div> |
| 24 | + - <p>Some text</p> |
| 25 | + + <p>Some new text</p> |
| 26 | + </div> |
| 27 | +
|
| 28 | + This will result in: |
| 29 | +
|
| 30 | + <div> |
| 31 | + <p>Some new text</p> |
| 32 | + </div> |
| 33 | +
|
| 34 | + If you need to supply multiple hunks for a diff use the @@@ separator between hunks. |
| 35 | + TIP |
| 36 | + end |
| 37 | + |
| 38 | + def self.signature |
| 39 | + { |
| 40 | + name: "update_artifact", |
| 41 | + description: |
| 42 | + "Updates an existing web artifact with new HTML, CSS, or JavaScript content.\n#{unified_diff_tip}", |
| 43 | + parameters: [ |
| 44 | + { |
| 45 | + name: "artifact_id", |
| 46 | + description: "The ID of the artifact to update", |
| 47 | + type: "integer", |
| 48 | + required: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "html_diff", |
| 52 | + description: "A unified diff of HTML changes to apply", |
| 53 | + type: "string", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "css_diff", |
| 57 | + description: "A unified diff of CSS changes to apply", |
| 58 | + type: "string", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "js_diff", |
| 62 | + description: "A unified diff of JavaScript changes to apply", |
| 63 | + type: "string", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "change_description", |
| 67 | + description: "A brief description of the changes being made", |
| 68 | + type: "string", |
| 69 | + required: true, |
| 70 | + }, |
| 71 | + ], |
| 72 | + } |
| 73 | + end |
| 74 | + |
| 75 | + def self.allow_partial_tool_calls? |
| 76 | + true |
| 77 | + end |
| 78 | + |
| 79 | + def partial_invoke |
| 80 | + @selected_tab = :html_diff |
| 81 | + if @prev_parameters |
| 82 | + @selected_tab = parameters.keys.find { |k| @prev_parameters[k] != parameters[k] } |
| 83 | + end |
| 84 | + update_custom_html |
| 85 | + @prev_parameters = parameters.dup |
| 86 | + end |
| 87 | + |
| 88 | + def invoke |
| 89 | + yield "Updating Artifact" |
| 90 | + |
| 91 | + post = Post.find_by(id: context[:post_id]) |
| 92 | + return error_response("No post context found") unless post |
| 93 | + |
| 94 | + artifact = AiArtifact.find_by(id: parameters[:artifact_id]) |
| 95 | + return error_response("Artifact not found") unless artifact |
| 96 | + |
| 97 | + if artifact.post.topic.id != post.topic.id |
| 98 | + return error_response("Attempting to update an artifact you are not allowed to") |
| 99 | + end |
| 100 | + |
| 101 | + begin |
| 102 | + artifact.apply_diff( |
| 103 | + html_diff: parameters[:html_diff], |
| 104 | + css_diff: parameters[:css_diff], |
| 105 | + js_diff: parameters[:js_diff], |
| 106 | + change_description: parameters[:change_description], |
| 107 | + ) |
| 108 | + |
| 109 | + update_custom_html(artifact) |
| 110 | + success_response(artifact) |
| 111 | + rescue DiscourseAi::Utils::DiffUtils::DiffError => e |
| 112 | + error_response(e.to_llm_message) |
| 113 | + rescue => e |
| 114 | + error_response(e.message) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + private |
| 119 | + |
| 120 | + def update_custom_html(artifact = nil) |
| 121 | + content = [] |
| 122 | + |
| 123 | + if parameters[:html_diff].present? |
| 124 | + content << [:html_diff, "### HTML Changes\n\n```diff\n#{parameters[:html_diff]}\n```"] |
| 125 | + end |
| 126 | + |
| 127 | + if parameters[:css_diff].present? |
| 128 | + content << [:css_diff, "### CSS Changes\n\n```diff\n#{parameters[:css_diff]}\n```"] |
| 129 | + end |
| 130 | + |
| 131 | + if parameters[:js_diff].present? |
| 132 | + content << [:js_diff, "### JavaScript Changes\n\n```diff\n#{parameters[:js_diff]}\n```"] |
| 133 | + end |
| 134 | + |
| 135 | + if parameters[:change_description].present? |
| 136 | + content.unshift( |
| 137 | + [:description, "### Change Description\n\n#{parameters[:change_description]}"], |
| 138 | + ) |
| 139 | + end |
| 140 | + |
| 141 | + content.sort_by! { |c| c[0] === @selected_tab ? 1 : 0 } if !artifact |
| 142 | + |
| 143 | + if artifact |
| 144 | + content.unshift([nil, "[details='#{I18n.t("discourse_ai.ai_artifact.view_changes")}']"]) |
| 145 | + content << [nil, "[/details]"] |
| 146 | + content << [ |
| 147 | + :preview, |
| 148 | + "### Preview\n\n<div class=\"ai-artifact\" data-ai-artifact-id=\"#{artifact.id}\"></div>", |
| 149 | + ] |
| 150 | + end |
| 151 | + |
| 152 | + self.custom_raw = content.map { |c| c[1] }.join("\n\n") |
| 153 | + end |
| 154 | + |
| 155 | + def success_response(artifact) |
| 156 | + @chain_next_response = false |
| 157 | + |
| 158 | + { |
| 159 | + status: "success", |
| 160 | + artifact_id: artifact.id, |
| 161 | + version: artifact.versions.last.version_number, |
| 162 | + message: "Artifact updated successfully and rendered to user.", |
| 163 | + } |
| 164 | + end |
| 165 | + |
| 166 | + def error_response(message) |
| 167 | + @chain_next_response = false |
| 168 | + |
| 169 | + { status: "error", error: message } |
| 170 | + end |
| 171 | + |
| 172 | + def help |
| 173 | + "Updates an existing web artifact with changes to its HTML, CSS, or JavaScript content. " \ |
| 174 | + "Requires the artifact ID and at least one change diff. " \ |
| 175 | + "Changes are applied using unified diff format. " \ |
| 176 | + "A description of the changes is required for version history." |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + end |
| 181 | +end |
0 commit comments