Skip to content

fix: adapt to ChatGPT API v2 metadata format + Turnstile solver support#6

Open
abczsl520 wants to merge 1 commit into432539:mainfrom
abczsl520:fix/chatgpt-api-v2-compat
Open

fix: adapt to ChatGPT API v2 metadata format + Turnstile solver support#6
abczsl520 wants to merge 1 commit into432539:mainfrom
abczsl520:fix/chatgpt-api-v2-compat

Conversation

@abczsl520
Copy link
Copy Markdown

Problem

ChatGPT upstream API changed the metadata format for image generation tool messages:

  • Old format: metadata.async_task_type = "image_gen"
  • New format: metadata.image_gen_title = "..." (final image) or metadata.async_source = "..." (preview)

This caused ExtractImageToolMsgs to match zero tool messages during poll, making image generation always timeout with no results.

Additionally, the upstream now requires Turnstile challenge solving for the two-step chat-requirements flow (prepare → finalize).

Changes

1. Core fix: metadata compatibility (image.go)

ExtractImageToolMsgs now matches tool messages by any of:

  • async_task_type = "image_gen" (old format, backward compatible)
  • image_gen_title != "" (new format, final image)
  • async_source != "" (new format, preview image)

2. Turnstile solver support

  • TurnstileSolver interface: added token parameter to Solve(ctx, dx, token) — the token is the p_value / requirements_token needed for Turnstile bytecode decryption
  • New HTTPTurnstileSolver: delegates to an external HTTP microservice (e.g. a Python VM decompiler)
  • New config: upstream.turnstile_solver_url — when set, enables the two-step prepare → solve → finalize flow
  • Falls back to single-step chat-requirements when solver is not configured or fails

3. Tuning

  • Poll interval: 3s → 15s (upstream returns 429 at higher frequencies)
  • Consecutive 429 tolerance: 3 → 10 (prevents premature abort)
  • PerAttemptTimeout: 6min → 10min (image generation can take 3-5 min with retries)
  • PollMaxWait: 300s → 480s

Testing

Tested with a ChatGPT Plus account:

  • Turnstile two-step flow: ✅ (token_len ~2900)
  • Image generation via /v1/images/generations: ✅ (HTTP 200, 1-2 images returned)
  • Image generation via /api/me/playground/image: ✅
  • Poll correctly extracts sediment IDs from new metadata format
  • Proxy image download via /p/img/ endpoint: ✅

…upport

## Problem

ChatGPT upstream API changed the metadata format for image generation
tool messages:

- Old format: `metadata.async_task_type = "image_gen"`
- New format: `metadata.image_gen_title = "..."` (final) or
  `metadata.async_source = "..."` (preview)

This caused `ExtractImageToolMsgs` to match zero tool messages,
making poll always timeout with no images returned.

Additionally, the upstream now requires Turnstile challenge solving
for the two-step chat-requirements flow (prepare → finalize).

## Changes

### Core fix: metadata compatibility (image.go)
- `ExtractImageToolMsgs` now matches tool messages by any of:
  `async_task_type=image_gen` (old), `image_gen_title` (new final),
  or `async_source` (new preview)

### Turnstile solver support
- New `TurnstileSolver` interface signature: `Solve(ctx, dx, token)`
  adds the `token` (p_value) parameter needed for bytecode decryption
- New `HTTPTurnstileSolver`: delegates to an external HTTP service
- New config field: `upstream.turnstile_solver_url`
- Wiring in `runner.go` and `main.go`
- `ChatRequirementsPrepareResp.PToken` field to pass the
  requirements_token through to the solver

### Tuning
- Poll interval: 3s → 15s (avoids 429 rate limiting from upstream)
- Consecutive 429 tolerance: 3 → 10
- PerAttemptTimeout: 6min → 10min (image gen can take 3-5 min)
- PollMaxWait: 300s → 480s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant