@@ -136,17 +136,11 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
136136 session .setHeaders (this .headers );
137137
138138 // Set multiple cookies
139- session .getCookies ().put ("__Secure-next-auth.session-token" , config .get (
140- "session_token" ));
139+ session .getCookies ().put ("__Secure-next-auth.session-token" , config .get ("session_token" ));
141140 session .getCookies ().put ("__Secure-next-auth.callback-url" , "https://chat.openai.com/" );
142141
143142 // Set proxies
144- if (config .get ("proxy" ) != null && !config .get ("proxy" ).equals ("" )) {
145- Map <String , String > proxies = new HashMap <>();
146- proxies .put ("http" , config .get ("proxy" ));
147- proxies .put ("https" , config .get ("proxy" ));
148- session .setProxies (proxies );
149- }
143+ setupProxy (session );
150144
151145 HttpResponse response = session .post2 ("https://chat.openai.com/backend-api/conversation" ,
152146 data );
@@ -193,81 +187,15 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
193187
194188 }
195189 return chatData ;
190+ }
196191
197- // try {
198- // JSONObject responseObject = JSON.parseObject(body);
199- //
200- //
201- // this.parentId = (String) responseObject.getJSONObject("message").get("id");
202- // this.conversationId = (String) responseObject.get("conversation_id");
203- //
204- // JSONArray jsonArray = responseObject.getJSONObject(
205- // "message")
206- // .getJSONObject("content")
207- // .getJSONArray("parts");
208- // if (jsonArray.size() == 0) {
209- //// continue;
210- // }
211- // Map<String, Object> message = (Map<String, Object>) jsonArray
212- // .get(0);
213- // Map<String, Object> result = new HashMap<>();
214- // result.put("message", message);
215- // result.put("conversation_id", this.conversationId);
216- // result.put("parent_id", this.parentId);
217- // return result;
218- //
219- // } catch (Exception e) {
220- // e.printStackTrace();
221- // try {
222- // // Get the title text
223- // System.out.println(body);
224- //
225- // String titleText = Pattern.compile("<title>(.*)</title>")
226- // .matcher(response.toString())
227- // .group();
228- //
229- //// // Find all div elements and capture the id attribute and the contents of the
230- //// // element
231- //// String divPattern = "<div[^>]*id=\"([^\"]*)\">(.*)</div>";
232- //// Matcher matcher = Pattern.compile(divPattern)
233- //// .matcher(response.toString());
234- //// List<String[]> divElements = (List<String[]>) matcher;
235- ////
236- ////
237- //// for (int i = 1; i <= matcher.groupCount(); i++) {
238- //// String group = matcher.group(i);
239- //// // 对匹配的组进行操作
240- ////
241- //// }
242- ////
243- //// .results()
244- //// .map(m -> new String[]{m.group(1), m.group(2)})
245- //// .collect(Collectors.toList());
246- ////
247- //// // Loop through the div elements and find the one with the "message" id
248- //// String messageText = "";
249- //// for (String[] div : divElements) {
250- //// String divId = div[0];
251- //// String divContent = div[1];
252- //// if (divId.equals("message")) {
253- //// messageText = divContent;
254- //// break;
255- //// }
256- //// }
257- // // Concatenate the title and message text
258- // errorDesc = titleText;
259- // } catch (Exception ex) {
260- // ex.printStackTrace();
261- //// errorDesc = (String) ((Map) JSON.parse(response.toString())).get("detail");
262- //// if (errorDesc.containsKey("message")) {
263- //// errorDesc = (String) errorDesc.get("message");
264- //// }
265- // } finally {
266- // System.out.println(response.toString());
267- // throw new RuntimeException("Response is not in the correct format " + errorDesc);
268- // }
269- // }
270-
192+ private void setupProxy (Session session ) {
193+ if (config .get ("proxy" ) != null && !config .get ("proxy" ).equals ("" )) {
194+ Map <String , String > proxies = new HashMap <>();
195+ proxies .put ("http" , config .get ("proxy" ));
196+ proxies .put ("https" , config .get ("proxy" ));
197+ session .setProxies (proxies );
198+ }
271199 }
272200
273201 public Map <String , Object > getChatResponse (String prompt , String output ) {
@@ -316,12 +244,7 @@ public void refreshSession() {
316244 Session session = new Session ();
317245
318246 // Set proxies
319- if (config .get ("proxy" ) != null && !config .get ("proxy" ).equals ("" )) {
320- Map <String , String > proxies = new HashMap <>();
321- proxies .put ("http" , config .get ("proxy" ));
322- proxies .put ("https" , config .get ("proxy" ));
323- session .setProxies (proxies );
324- }
247+ setupProxy (session );
325248
326249 // Set cookies
327250 session .getCookies ().put ("__Secure-next-auth.session-token" , config .get (
@@ -350,19 +273,9 @@ public void refreshSession() {
350273
351274 config .put ("Authorization" , accessToken );
352275
353-
354- // List<Cookie> cookies = Cookie.parseAll(HttpUrl.parse(urlSession),
355- // response.headers());
356- // for (Cookie cookie1 : cookies) {
357- // if (cookie1.name().equals(name)) {
358- // config.put("session_token", response.getCookieValue(name));
359- // }
360- // }
361-
362276 this .refreshHeaders ();
363277 } catch (Exception e ) {
364278 System .out .println ("Error refreshing session" );
365- // System.out.println(response.toString());
366279 throw new Exception ("Error refreshing session" , e );
367280 }
368281 } else if (config .containsKey ("email" ) && config .containsKey ("password" )) {
0 commit comments