File size: 1,904 Bytes
aca6950
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from gradio_client import Client
import os

class AussieHubClient:
    def __init__(self, space_url="Brettapps/brettapps-aussie-mcp-server-agents-mate", hf_token=None):
        self.client = Client(space_url, token=hf_token)
        self.history = []

    def chat(self, message):
        """
        Sends a message to the Aussie Hub and returns the agent response.
        Handles the internal Gradio /user -> /bot trigger sequence.
        """
        print(f"Sending to Hub: {message}")
        
        # 1. Trigger the /user endpoint to add message to history
        # returns (empty_textbox, updated_history)
        _, self.history = self.client.predict(
            user_message=message,
            history=self.history,
            api_name="/user"
        )
        
        # 2. Trigger the /bot endpoint to generate response
        # returns updated_history with agent reply
        self.history = self.client.predict(
            history=self.history,
            api_name="/bot"
        )
        
        # Extract the last message content (agent's reply)
        # Note: In Gradio 5+, history is a list of complex dicts
        last_turn = self.history[-1]
        if last_turn['role'] == 'assistant':
            return last_turn['content'][0]['text']
        return "No response received."

    def clear(self):
        """Clears the session history."""
        self.history = []
        print("Hub session cleared.")

if __name__ == "__main__":
    # Example Usage
    # Ensure HF_TOKEN is in your environment
    token = os.environ.get("HF_TOKEN")
    
    hub = AussieHubClient(hf_token=token)
    
    # Task the Hub
    response = hub.chat("G'day! Who are the core agents in this hub?")
    print(f"\nAussie Agent: {response}")
    
    # Task the Business Manager
    response = hub.chat("Check the status of Fair Dinkum Publishing.")
    print(f"\nAussie Agent: {response}")