6
6
fab! ( :user )
7
7
fab! ( :bot_user ) { Fabricate ( :user ) }
8
8
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 ) }
12
10
13
11
fab! ( :ai_persona ) do
14
12
persona =
25
23
persona
26
24
end
27
25
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
29
29
30
30
def add_automation_field ( name , value , type : "text" )
31
31
automation . fields . create! (
@@ -49,12 +49,40 @@ def add_automation_field(name, value, type: "text")
49
49
it "can respond to a post using the specified persona" do
50
50
post = Fabricate ( :post , raw : "This is a test post that needs triage" )
51
51
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."
53
53
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 } " )
58
86
59
87
topic = post . topic . reload
60
88
last_post = topic . posts . order ( :post_number ) . last
0 commit comments