@@ -239,4 +239,92 @@ def add_automation_field(name, value, type: "text")
239239 # should not inject persona into allowed users
240240 expect ( topic . topic_allowed_users . pluck ( :user_id ) . sort ) . to eq ( original_user_ids . sort )
241241 end
242+
243+ describe "LLM Persona Triage with Chat Message Creation" do
244+ fab! ( :user )
245+ fab! ( :bot_user ) { Fabricate ( :user ) }
246+ fab! ( :chat_channel ) { Fabricate ( :category_channel ) }
247+
248+ fab! ( :custom_tool ) do
249+ AiTool . create! (
250+ name : "Chat Notifier" ,
251+ tool_name : "chat_notifier" ,
252+ description : "Creates a chat notification in a channel" ,
253+ parameters : [
254+ { name : "channel_id" , type : "integer" , description : "Chat channel ID" } ,
255+ { name : "message" , type : "string" , description : "Message to post" } ,
256+ ] ,
257+ script : <<~JS ,
258+ function invoke(params) {
259+ // Create a chat message using the Chat API
260+ const result = discourse.createChatMessage({
261+ channel_name: '#{ chat_channel . name } ',
262+ username: '#{ user . username } ',
263+ message: params.message
264+ });
265+
266+ chain.setCustomRaw("We are done, stopping chaing");
267+
268+ return {
269+ success: true,
270+ message_id: result.message_id,
271+ url: result.url,
272+ message: params.message
273+ };
274+ }
275+ JS
276+ summary : "Notify in chat channel" ,
277+ created_by : Discourse . system_user ,
278+ )
279+ end
280+
281+ before do
282+ SiteSetting . chat_enabled = true
283+
284+ ai_persona . update! ( tools : [ "custom-#{ custom_tool . id } " ] )
285+
286+ # Set up automation fields
287+ automation . fields . create! (
288+ component : "choices" ,
289+ name : "persona" ,
290+ metadata : {
291+ value : ai_persona . id ,
292+ } ,
293+ target : "script" ,
294+ )
295+
296+ automation . fields . create! (
297+ component : "boolean" ,
298+ name : "silent_mode" ,
299+ metadata : {
300+ value : true ,
301+ } ,
302+ target : "script" ,
303+ )
304+ end
305+
306+ it "can silently analyze a post and create a chat notification" do
307+ post = Fabricate ( :post , raw : "Please help with my billing issue" )
308+
309+ # Tool response from LLM
310+ tool_call =
311+ DiscourseAi ::Completions ::ToolCall . new (
312+ name : "chat_notifier" ,
313+ parameters : {
314+ "message" => "Hello world!" ,
315+ } ,
316+ id : "tool_call_1" ,
317+ )
318+
319+ DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ tool_call ] ) do
320+ automation . running_in_background!
321+ automation . trigger! ( { "post" => post } )
322+ end
323+
324+ expect ( post . topic . reload . posts . count ) . to eq ( 1 )
325+
326+ expect ( chat_channel . chat_messages . count ) . to eq ( 1 )
327+ expect ( chat_channel . chat_messages . last . message ) . to eq ( "Hello world!" )
328+ end
329+ end
242330end
0 commit comments