Skip to content

Commit 9895382

Browse files
committed
fix: v1.10.20
1 parent a52501f commit 9895382

File tree

2 files changed

+115
-68
lines changed

2 files changed

+115
-68
lines changed

common/constants.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package common
33
import "time"
44

55
var StartTime = time.Now().Unix() // unit: second
6-
var Version = "v1.10.19" // this hard coding will be replaced automatically when building, no need to manually change
6+
var Version = "v1.10.20" // this hard coding will be replaced automatically when building, no need to manually change
77

88
var DefaultOpenaiModelList = []string{
99
"gpt-4o",
1010
"o1",
1111
"o3-mini-high",
12-
"claude-3-5-sonnet",
1312
"claude-3-7-sonnet",
1413
"claude-3-5-haiku",
1514
"gemini-2.0-flash",
@@ -30,7 +29,6 @@ var TextModelList = []string{
3029
"gpt-4o",
3130
"o1",
3231
"o3-mini-high",
33-
"claude-3-5-sonnet",
3432
"claude-3-7-sonnet",
3533
"claude-3-5-haiku",
3634
"gemini-2.0-flash",

controller/chat.go

Lines changed: 114 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -386,31 +386,7 @@ func createRequestBody(c *gin.Context, client cycletls.CycleTLS, cookie string,
386386

387387
logger.Debug(c.Request.Context(), fmt.Sprintf("RequestBody: %v", requestBody))
388388

389-
if strings.TrimSpace(config.CheatUrl) == "" ||
390-
(!strings.HasPrefix(config.CheatUrl, "http://") &&
391-
!strings.HasPrefix(config.CheatUrl, "https://")) {
392-
return requestBody, nil
393-
} else {
394-
jsonData, err := json.Marshal(requestBody)
395-
if err != nil {
396-
return nil, fmt.Errorf("marshal request body error: %v", err)
397-
}
398-
399-
resp, err := http.Post(config.CheatUrl, "application/json", bytes.NewBuffer(jsonData))
400-
if err != nil {
401-
return nil, fmt.Errorf("send request to test api error: %v", err)
402-
}
403-
defer resp.Body.Close()
404-
405-
// 读取响应
406-
var response map[string]interface{}
407-
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
408-
return nil, fmt.Errorf("decode response error: %v", err)
409-
}
410-
logger.Debugf(c.Request.Context(), fmt.Sprintf("Cheat success!"))
411-
return response, nil
412-
}
413-
389+
return requestBody, nil
414390
}
415391

416392
func createImageRequestBody(c *gin.Context, cookie string, openAIReq *model.OpenAIImagesGenerationRequest, chatId string) (map[string]interface{}, error) {
@@ -525,7 +501,16 @@ func createImageRequestBody(c *gin.Context, cookie string, openAIReq *model.Open
525501
return nil, fmt.Errorf("marshal request body error: %v", err)
526502
}
527503

528-
resp, err := http.Post(config.CheatUrl, "application/json", bytes.NewBuffer(jsonData))
504+
req, err := http.NewRequest("POST", config.CheatUrl, bytes.NewBuffer(jsonData))
505+
if err != nil {
506+
return nil, err
507+
}
508+
req.Header.Set("Content-Type", "application/json")
509+
req.Header.Set("Cookie", cookie)
510+
511+
client := &http.Client{}
512+
resp, err := client.Do(req)
513+
529514
if err != nil {
530515
return nil, fmt.Errorf("send request to test api error: %v", err)
531516
}
@@ -858,6 +843,12 @@ func handleStreamRequest(c *gin.Context, client cycletls.CycleTLS, cookie string
858843

859844
c.Stream(func(w io.Writer) bool {
860845
for attempt := 0; attempt < maxRetries; attempt++ {
846+
847+
requestBody, err := cheat(requestBody, c, cookie)
848+
if err != nil {
849+
c.JSON(500, gin.H{"error": err.Error()})
850+
return false
851+
}
861852
jsonData, err := json.Marshal(requestBody)
862853
if err != nil {
863854
c.JSON(500, gin.H{"error": "Failed to marshal request body"})
@@ -955,6 +946,43 @@ func handleStreamRequest(c *gin.Context, client cycletls.CycleTLS, cookie string
955946
})
956947
}
957948

949+
func cheat(requestBody map[string]interface{}, c *gin.Context, cookie string) (map[string]interface{}, error) {
950+
if strings.TrimSpace(config.CheatUrl) == "" ||
951+
(!strings.HasPrefix(config.CheatUrl, "http://") &&
952+
!strings.HasPrefix(config.CheatUrl, "https://")) {
953+
return requestBody, nil
954+
} else {
955+
jsonData, err := json.Marshal(requestBody)
956+
if err != nil {
957+
return nil, fmt.Errorf("marshal request body error: %v", err)
958+
}
959+
960+
req, err := http.NewRequest("POST", config.CheatUrl, bytes.NewBuffer(jsonData))
961+
if err != nil {
962+
return nil, err
963+
}
964+
req.Header.Set("Content-Type", "application/json")
965+
req.Header.Set("Cookie", cookie)
966+
967+
client := &http.Client{}
968+
resp, err := client.Do(req)
969+
970+
//resp, err := http.Post(config.CheatUrl, "application/json", bytes.NewBuffer(jsonData))
971+
if err != nil {
972+
return nil, fmt.Errorf("send request to test api error: %v", err)
973+
}
974+
defer resp.Body.Close()
975+
976+
// 读取响应
977+
var response map[string]interface{}
978+
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
979+
return nil, fmt.Errorf("decode response error: %v", err)
980+
}
981+
logger.Debugf(c.Request.Context(), fmt.Sprintf("Cheat success!"))
982+
return response, nil
983+
}
984+
}
985+
958986
// 处理流式数据的辅助函数,返回bool表示是否继续处理
959987
func processStreamData(c *gin.Context, data string, projectId *string, cookie, responseId, model string, jsonData []byte, searchModel bool) bool {
960988
data = strings.TrimSpace(data)
@@ -1013,6 +1041,7 @@ func processStreamData(c *gin.Context, data string, projectId *string, cookie, r
10131041
}
10141042

10151043
func makeStreamRequest(c *gin.Context, client cycletls.CycleTLS, jsonData []byte, cookie string) (<-chan cycletls.SSEResponse, error) {
1044+
10161045
options := cycletls.Options{
10171046
Timeout: 10 * 60 * 60,
10181047
Proxy: config.ProxyUrl, // 在每个请求中设置代理
@@ -1135,6 +1164,11 @@ func handleNonStreamRequest(c *gin.Context, client cycletls.CycleTLS, cookie str
11351164
maxRetries := len(cookieManager.Cookies)
11361165

11371166
for attempt := 0; attempt < maxRetries; attempt++ {
1167+
requestBody, err := cheat(requestBody, c, cookie)
1168+
if err != nil {
1169+
c.JSON(500, gin.H{"error": err.Error()})
1170+
return
1171+
}
11381172
jsonData, err := json.Marshal(requestBody)
11391173
if err != nil {
11401174
c.JSON(500, gin.H{"error": "Failed to marshal request body"})
@@ -1626,51 +1660,66 @@ func extractTaskIDs(responseBody string) (string, []string) {
16261660
func pollTaskStatus(c *gin.Context, client cycletls.CycleTLS, taskIDs []string, cookie string) []string {
16271661
var imageURLs []string
16281662

1629-
for _, taskID := range taskIDs {
1630-
for {
1631-
// 构建请求URL
1632-
url := fmt.Sprintf("https://www.genspark.ai/api/spark/image_generation_task_status?task_id=%s", taskID)
1633-
1634-
// 发送请求
1635-
response, err := client.Do(url, cycletls.Options{
1636-
Timeout: 10 * 60 * 60,
1637-
Proxy: config.ProxyUrl, // 在每个请求中设置代理
1638-
Method: "GET",
1639-
Headers: map[string]string{
1640-
"Cookie": cookie,
1641-
},
1642-
}, "GET")
1663+
requestData := map[string]interface{}{
1664+
"task_ids": taskIDs,
1665+
}
16431666

1644-
if err != nil {
1645-
continue
1646-
}
1667+
jsonData, err := json.Marshal(requestData)
1668+
if err != nil {
1669+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to marshal request data"})
1670+
return imageURLs
1671+
}
16471672

1648-
var result struct {
1649-
Data struct {
1650-
ImageURLs []string `json:"image_urls"`
1651-
ImageURLsNowatermark []string `json:"image_urls_nowatermark"`
1652-
Status string `json:"status"`
1653-
}
1654-
}
1673+
sseChan, err := client.DoSSE("https://www.genspark.ai/api/ig_tasks_status", cycletls.Options{
1674+
Timeout: 10 * 60 * 60,
1675+
Proxy: config.ProxyUrl, // 在每个请求中设置代理
1676+
Body: string(jsonData),
1677+
Method: "POST",
1678+
Headers: map[string]string{
1679+
"Content-Type": "application/json",
1680+
"Accept": "*/*",
1681+
"Origin": baseURL,
1682+
"Referer": baseURL + "/",
1683+
"Cookie": cookie,
1684+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome",
1685+
},
1686+
}, "POST")
1687+
if err != nil {
1688+
logger.Errorf(c, "Failed to make stream request: %v", err)
1689+
return imageURLs
1690+
}
1691+
for response := range sseChan {
1692+
if response.Done {
1693+
//logger.Warnf(c.Request.Context(), response.Data)
1694+
return imageURLs
1695+
}
16551696

1656-
if err := json.Unmarshal([]byte(response.Body), &result); err != nil {
1657-
continue
1658-
}
1697+
data := response.Data
1698+
if data == "" {
1699+
continue
1700+
}
16591701

1660-
// 如果状态成功且有图片URL
1661-
if result.Data.Status == "SUCCESS" {
1662-
if len(result.Data.ImageURLsNowatermark) > 0 {
1663-
imageURLs = append(imageURLs, result.Data.ImageURLsNowatermark...)
1664-
break
1665-
}
1666-
if len(result.Data.ImageURLs) > 0 {
1667-
imageURLs = append(imageURLs, result.Data.ImageURLs...)
1668-
break
1702+
logger.Debug(c.Request.Context(), strings.TrimSpace(data))
1703+
1704+
var responseData map[string]interface{}
1705+
if err := json.Unmarshal([]byte(data), &responseData); err != nil {
1706+
continue
1707+
}
1708+
1709+
if responseData["type"] == "TASKS_STATUS_COMPLETE" {
1710+
if finalStatus, ok := responseData["final_status"].(map[string]interface{}); ok {
1711+
for _, taskID := range taskIDs {
1712+
if task, exists := finalStatus[taskID].(map[string]interface{}); exists {
1713+
if status, ok := task["status"].(string); ok && status == "SUCCESS" {
1714+
if urls, ok := task["image_urls"].([]interface{}); ok && len(urls) > 0 {
1715+
if imageURL, ok := urls[0].(string); ok {
1716+
imageURLs = append(imageURLs, imageURL)
1717+
}
1718+
}
1719+
}
1720+
}
16691721
}
16701722
}
1671-
1672-
// 等待1秒后重试
1673-
time.Sleep(500 * time.Millisecond)
16741723
}
16751724
}
16761725

0 commit comments

Comments
 (0)