66 fab! ( :user )
77 fab! ( :bot_user ) { Fabricate ( :user ) }
88
9- fab! ( :llm_model ) do
10- Fabricate ( :llm_model , provider : "anthropic" , name : "claude-3-opus" , enabled_chat_bot : true )
11- end
9+ fab! ( :llm_model ) { Fabricate ( :anthropic_model , name : "claude-3-opus" , enabled_chat_bot : true ) }
1210
1311 fab! ( :ai_persona ) do
1412 persona =
2523 persona
2624 end
2725
28- let ( :automation ) { Fabricate ( :automation , script : "llm_persona_triage" , enabled : true ) }
26+ let ( :automation ) do
27+ Fabricate ( :automation , name : "my automation" , script : "llm_persona_triage" , enabled : true )
28+ end
2929
3030 def add_automation_field ( name , value , type : "text" )
3131 automation . fields . create! (
@@ -49,12 +49,40 @@ def add_automation_field(name, value, type: "text")
4949 it "can respond to a post using the specified persona" do
5050 post = Fabricate ( :post , raw : "This is a test post that needs triage" )
5151
52- response_text = "I've analyzed your post and can help with that."
52+ response_text = "I analyzed your post and can help with that."
5353
54- DiscourseAi ::Completions ::Llm . with_prepared_responses ( [ response_text ] ) do
55- automation . running_in_background!
56- automation . trigger! ( { "post" => post } )
57- end
54+ body = ( <<~STRING ) . strip
55+ event: message_start
56+ data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}
57+
58+ event: content_block_start
59+ data: {"type": "content_block_start", "index":0, "content_block": {"type": "text", "text": ""}}
60+
61+ event: ping
62+ data: {"type": "ping"}
63+
64+ event: content_block_delta
65+ data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "#{ response_text } "}}
66+
67+ event: content_block_stop
68+ data: {"type": "content_block_stop", "index": 0}
69+
70+ event: message_delta
71+ data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}
72+
73+ event: message_stop
74+ data: {"type": "message_stop"}
75+ STRING
76+
77+ stub_request ( :post , "https://api.anthropic.com/v1/messages" ) . to_return ( body : body )
78+
79+ automation . running_in_background!
80+ automation . trigger! ( { "post" => post } )
81+
82+ log = AiApiAuditLog . last
83+ expect ( log ) . to be_present
84+ expect ( log . user_id ) . to eq ( post . user_id )
85+ expect ( log . feature_name ) . to eq ( "automation - #{ automation . name } " )
5886
5987 topic = post . topic . reload
6088 last_post = topic . posts . order ( :post_number ) . last
0 commit comments