File size: 4,916 Bytes
ef213b3
 
17b60d4
 
ef213b3
 
 
 
 
 
 
 
 
17b60d4
ef213b3
17b60d4
 
 
ef213b3
 
 
17b60d4
 
d27a461
17b60d4
ef213b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17b60d4
d27a461
17b60d4
 
 
 
ef213b3
17b60d4
ef213b3
 
 
 
 
 
 
 
 
 
 
17b60d4
d27a461
 
 
 
 
 
ef213b3
 
 
d27a461
 
 
 
ef213b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d27a461
ef213b3
d27a461
17b60d4
 
9fda4b9
 
 
ef213b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fda4b9
 
 
 
 
 
 
ef213b3
 
9fda4b9
 
 
ef213b3
 
 
9fda4b9
17b60d4
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Re-written to use the new REST API at https://auxteam-usersyncui.hf.space
const API_BASE_URL = "https://auxteam-usersyncui.hf.space";

export class GradioService {
  // Use HF Token from environment if available
  private static getHeaders() {
    const token = (import.meta as any).env?.VITE_HF_TOKEN || null;
    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      "Accept": "application/json"
    };
    if (token) {
      headers["Authorization"] = `Bearer ${token}`;
    }
    return headers;
  }

  static async identifyPersonas(context: string) {
    // Deprecated? Just returns context for now to not break apps that might expect a string
    console.warn("identifyPersonas is no longer supported directly by the REST API.");
    return context;
  }

  static async startSimulationAsync(simulationId: string, contentText: string, format: string = "text") {
    try {
      // simulationId in the old code might have been the focus group ID.
      // Let's assume simulationId is the focus_group_id
      const payload = {
        focus_group_id: simulationId,
        content_type: format,
        content_payload: contentText,
        parameters: {}
      };
      
      const response = await fetch(`${API_BASE_URL}/api/v1/simulations`, {
        method: "POST",
        headers: this.getHeaders(),
        body: JSON.stringify(payload)
      });
      
      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }
      
      const data = await response.json();
      // Returns a SimulationResponse object: { job_id, status, message, ... }
      return data.job_id; // old code expected job/simulation id as string return? 
      // Actually old code expects the data. Let's return the job_id as string because in ChatPage it does:
      // const result = await GradioService.startSimulationAsync(simulationId, msg);
      // Wait, let's look at ChatPage lines 185-195
    } catch (error) {
      console.error("Error starting simulation:", error);
      throw error;
    }
  }

  static async getSimulationStatus(jobId: string) {
    try {
      const response = await fetch(`${API_BASE_URL}/api/v1/simulations/${jobId}`, {
        headers: this.getHeaders()
      });
      
      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }
      
      const data = await response.json();
      // Returns { job_id, status, message, progress_percentage, results }
      return data;
    } catch (error) {
      console.error("Error getting simulation status:", error);
      throw error;
    }
  }

  static async generateVariants(contentText: string, numVariants: number = 3) {
    // This endpoint doesn't exist in the openapi spec. 
    console.warn("generateVariants is no longer supported by the REST API.");
    return ["Variant generation not supported."];
  }

  static async listSimulations() {
    try {
      // Returns focus groups from the personas endpoint
      const response = await fetch(`${API_BASE_URL}/api/v1/personas`, {
        headers: this.getHeaders()
      });
      
      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }
      
      const data = await response.json();
      // The old code returned an array of strings.
      // API returns: { focus_groups: [ {id, name, agent_count} ] }
      // We will map this to an array of names or IDs.
      // We need to see how `listSimulations` is used.
      return data.focus_groups || [];
    } catch (error) {
      console.error("Error listing simulations/personas:", error);
      return [];
    }
  }

  static async generatePersonas(businessDescription: string, customerProfile: string, numPersonas: number = 1) {
    try {
      const payload = {
        business_description: businessDescription,
        customer_profile: customerProfile,
        num_personas: numPersonas
      };
      
      const response = await fetch(`${API_BASE_URL}/api/v1/personas/generate`, {
        method: "POST",
        headers: this.getHeaders(),
        body: JSON.stringify(payload)
      });
      
      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }
      
      const data = await response.json();
      // returns SimulationResponse { job_id, status }
      return data;
    } catch (error) {
      console.error("Error generating personas:", error);
      throw error;
    }
  }

  static async generateSocialNetwork(name: string, personaCount: number = 10, networkType: string = "scale_free", focusGroupName: string | null = null) {
    console.warn("generateSocialNetwork is subsumed by persona generation or not supported.");
    return { status: "Network generated" };
  }

  static async getNetworkGraph(simulationId: string) {
    // Not supported
    console.warn("getNetworkGraph is not supported by the REST API.");
    return null;
  }
}