@@ -38,6 +38,19 @@ def self.signature
3838 }
3939 end
4040
41+ def self . allow_partial_tool_calls?
42+ true
43+ end
44+
45+ def partial_invoke
46+ @selected_tab = :html
47+ if @prev_parameters
48+ @selected_tab = parameters . keys . find { |k | @prev_parameters [ k ] != parameters [ k ] }
49+ end
50+ update_custom_html
51+ @prev_parameters = parameters . dup
52+ end
53+
4154 def invoke
4255 yield parameters [ :name ] || "Web Artifact"
4356 # Get the current post from context
@@ -61,34 +74,73 @@ def invoke
6174 )
6275
6376 if artifact . save
64- tabs = {
65- css : [ css , "CSS" ] ,
66- js : [ js , "JavaScript" ] ,
67- html : [ html , "HTML" ] ,
68- preview : [
69- "<iframe src=\" #{ Discourse . base_url } /discourse-ai/ai-bot/artifacts/#{ artifact . id } \" width=\" 100%\" height=\" 500\" frameborder=\" 0\" ></iframe>" ,
70- "Preview" ,
71- ] ,
72- }
73-
74- first = true
75- html_tabs =
76- tabs . map do |tab , ( content , name ) |
77- selected = " data-selected" if first
78- first = false
79- ( <<~HTML ) . strip
77+ update_custom_html ( artifact )
78+ success_response ( artifact )
79+ else
80+ error_response ( artifact . errors . full_messages . join ( ", " ) )
81+ end
82+ end
83+
84+ def chain_next_response?
85+ @chain_next_response
86+ end
87+
88+ private
89+
90+ def update_custom_html ( artifact = nil )
91+ html = parameters [ :html_body ] . to_s
92+ css = parameters [ :css ] . to_s
93+ js = parameters [ :js ] . to_s
94+
95+ iframe =
96+ "<iframe src=\" #{ Discourse . base_url } /discourse-ai/ai-bot/artifacts/#{ artifact . id } \" width=\" 100%\" height=\" 500\" frameborder=\" 0\" ></iframe>" if artifact
97+
98+ content = [ ]
99+
100+ content << [ :html , "### HTML\n \n ```html\n #{ html } \n ```" ] if html . present?
101+
102+ content << [ :css , "### CSS\n \n ```css\n #{ css } \n ```" ] if css . present?
103+
104+ content << [ :js , "### JavaScript\n \n ```javascript\n #{ js } \n ```" ] if js . present?
105+
106+ content << [ :preview , "### Preview\n \n #{ iframe } " ] if iframe
107+
108+ content . sort_by! { |c | c [ 0 ] === @selected_tab ? 0 : 1 } if !artifact
109+
110+ self . custom_raw = content . map { |c | c [ 1 ] } . join ( "\n \n " )
111+ end
112+
113+ def update_custom_html_old ( artifact = nil )
114+ html = parameters [ :html_body ] . to_s
115+ css = parameters [ :css ] . to_s
116+ js = parameters [ :js ] . to_s
117+
118+ tabs = { css : [ css , "CSS" ] , js : [ js , "JavaScript" ] , html : [ html , "HTML" ] }
119+
120+ if artifact
121+ iframe =
122+ "<iframe src=\" #{ Discourse . base_url } /discourse-ai/ai-bot/artifacts/#{ artifact . id } \" width=\" 100%\" height=\" 500\" frameborder=\" 0\" ></iframe>"
123+ tabs [ :preview ] = [ iframe , "Preview" ]
124+ end
125+
126+ first = true
127+ html_tabs =
128+ tabs . map do |tab , ( content , name ) |
129+ selected = " data-selected" if first
130+ first = false
131+ ( <<~HTML ) . strip
80132 < div class ="ai-artifact-tab " data- #{ tab } #{ selected } >
81133 < a > #{ name } </ a>
82134 </ div>
83135 HTML
84- end
85-
86- first = true
87- html_panels =
88- tabs . map do |tab , ( content , name ) |
89- selected = " data-selected" if first
90- first = false
91- inner_content =
136+ end
137+
138+ first = true
139+ html_panels =
140+ tabs . map do |tab , ( content , name ) |
141+ selected = " data-selected" if ( first || ( ! artifact && tab == @selected_tab ) )
142+ first = false
143+ inner_content =
92144 if tab == :preview
93145 content
94146 else
@@ -99,15 +151,15 @@ def invoke
99151 ```
100152 HTML
101153 end
102- ( <<~HTML ) . strip
154+ ( <<~HTML ) . strip
103155 < div class ="ai-artifact-panel " data- #{ tab } #{ selected } >
104156
105157 #{ inner_content }
106158 </ div>
107159 HTML
108- end
160+ end
109161
110- self . custom_raw = <<~RAW
162+ self . custom_raw = <<~RAW
111163 <div class="ai-artifact">
112164 <div class="ai-artifact-tabs">
113165 #{ html_tabs . join ( "\n " ) }
@@ -117,19 +169,8 @@ def invoke
117169 </div>
118170 </div>
119171 RAW
120-
121- success_response ( artifact )
122- else
123- error_response ( artifact . errors . full_messages . join ( ", " ) )
124- end
125172 end
126173
127- def chain_next_response?
128- @chain_next_response
129- end
130-
131- private
132-
133174 def success_response ( artifact )
134175 @chain_next_response = false
135176 iframe_url = "#{ Discourse . base_url } /discourse-ai/ai-bot/artifacts/#{ artifact . id } "
0 commit comments