更新chatgpt协议

This commit is contained in:
2026-07-06 10:21:44 +08:00
parent c96bee4094
commit e517c56225
5 changed files with 78 additions and 21 deletions
+1 -1
View File
@@ -223,7 +223,7 @@ func readMultipartImages(c *gin.Context, keys ...string) []string {
if e != nil {
continue
}
b, _ := io.ReadAll(io.LimitReader(f, 8<<20+1))
b, _ := io.ReadAll(io.LimitReader(f, 20<<20+1))
f.Close()
if len(b) > 0 {
out = append(out, base64.StdEncoding.EncodeToString(b))
+34 -6
View File
@@ -73,6 +73,15 @@ func (c *Client) GenerateImage(ctx context.Context, accessToken, prompt, model,
return nil, nil, err
}
// Fail over immediately when the account's image_gen allowance is spent:
// submitting anyway just burns the whole poll budget and surfaces as
// "image poll timeout". Unknown quota (init failed) proceeds as before.
if quota, qErr := c.fetchImageQuota(ctx, session, accessToken); qErr == nil && quota["unknown"] == false {
if remaining, ok := quota["remaining"].(int); ok && remaining <= 0 {
return nil, nil, fmt.Errorf("%w: image_gen remaining 0 (resets %s)", ErrQuotaExhausted, stringValue(quota["reset_after"]))
}
}
scriptSources, dataBuild, err := c.bootstrap(ctx, session)
if err != nil {
return nil, nil, err
@@ -142,6 +151,10 @@ func (c *Client) FetchImageQuota(ctx context.Context, accessToken string) (map[s
if err != nil {
return nil, err
}
return c.fetchImageQuota(ctx, session, accessToken)
}
func (c *Client) fetchImageQuota(ctx context.Context, session tlsclient.HttpClient, accessToken string) (map[string]any, error) {
path := "/backend-api/conversation/init"
body, _ := json.Marshal(map[string]any{
"gizmo_id": nil,
@@ -949,12 +962,27 @@ func (c *Client) pollForImage(ctx context.Context, session tlsclient.HttpClient,
}
func (c *Client) getFileDownloadURL(ctx context.Context, session tlsclient.HttpClient, accessToken, conversationID, fileID string, inline bool) (string, error) {
// Python parity (_get_file_download_url): GET /backend-api/files/{id}/download
// with NO query params. The old /files/download/{id}?conversation_id&inline
// form returned an inline stream URL that 403s with "File stream access denied".
_ = conversationID
_ = inline
path := "/backend-api/files/" + fileID + "/download"
// Current web client form: GET /backend-api/files/download/{id}
// ?conversation_id=...&inline=false → {"status":"success","download_url":...}.
// Falls back to the legacy /files/{id}/download form if the new one fails.
paths := []string{
"/backend-api/files/download/" + fileID + "?conversation_id=" + conversationID + "&inline=" + strconv.FormatBool(inline),
"/backend-api/files/" + fileID + "/download",
}
var lastErr error
for _, path := range paths {
rawURL, err := c.fetchDownloadURL(ctx, session, accessToken, path)
if err == nil && rawURL != "" {
return rawURL, nil
}
if err != nil {
lastErr = err
}
}
return "", lastErr
}
func (c *Client) fetchDownloadURL(ctx context.Context, session tlsclient.HttpClient, accessToken, path string) (string, error) {
req, err := http.NewRequest(http.MethodGet, baseURL+path, nil)
if err != nil {
return "", err
+16
View File
@@ -43,6 +43,22 @@ var (
"this request may violate our content polic",
"this prompt may violate our content polic",
"may violate our content policies",
"i can't help with",
"i can\u2019t help with",
"i cannot help with",
"i can't assist with",
"i can\u2019t assist with",
"i cannot assist with",
"i'm unable to help with",
"i\u2019m unable to help with",
"can't create images",
"can\u2019t create images",
"cannot create images",
"can't generate images",
"can\u2019t generate images",
"cannot generate images",
"unable to create images",
"unable to generate images",
}
)
+2 -2
View File
@@ -58,10 +58,10 @@ var (
ErrVideoNotReady = errors.New("video is not ready yet")
)
// maxReferenceImageBytes bounds a single decoded reference image. 8 MB
// maxReferenceImageBytes bounds a single decoded reference image. 20 MB
// comfortably covers real photos/screenshots; anything larger is almost
// certainly abuse or a mistake. Mirrors Python core/refs.py.
const maxReferenceImageBytes = 8 * 1024 * 1024
const maxReferenceImageBytes = 20 * 1024 * 1024
type V1Service struct {
cfg *config.Config