| 739 | } |
| 740 | |
| 741 | func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (resp cliproxyexecutor.Response, err error) { |
| 742 | if opts.Alt == "responses/compact" { |
| 743 | return e.executeCompact(ctx, auth, req, opts) |
| 744 | } |
| 745 | if isCodexOpenAIImageRequest(opts) { |
| 746 | return e.executeOpenAIImage(ctx, auth, req, opts) |
| 747 | } |
| 748 | baseModel := thinking.ParseSuffix(req.Model).ModelName |
| 749 | |
| 750 | apiKey, baseURL := codexCreds(auth) |
| 751 | if baseURL == "" { |
| 752 | baseURL = "https://chatgpt.com/backend-api/codex" |
| 753 | } |
| 754 | |
| 755 | reporter := helps.NewExecutorUsageReporter(ctx, e, baseModel, auth) |
| 756 | defer reporter.TrackFailure(ctx, &err) |
| 757 | |
| 758 | from := opts.SourceFormat |
| 759 | responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts) |
| 760 | to := sdktranslator.FromString("codex") |
| 761 | originalPayloadSource := req.Payload |
| 762 | if len(opts.OriginalRequest) > 0 { |
| 763 | originalPayloadSource = opts.OriginalRequest |
| 764 | } |
| 765 | originalPayload := originalPayloadSource |
| 766 | originalTranslated, body := translateCodexRequestPair(from, to, baseModel, originalPayload, req.Payload, false) |
| 767 | |
| 768 | body, err = thinking.ApplyThinking(body, req.Model, from.String(), to.String(), e.Identifier()) |
| 769 | if err != nil { |
| 770 | return resp, err |
| 771 | } |
| 772 | |
| 773 | requestedModel := helps.PayloadRequestedModel(opts, req.Model) |
| 774 | requestPath := helps.PayloadRequestPath(opts) |
| 775 | body = helps.ApplyPayloadConfigWithRequest(e.cfg, baseModel, to.String(), from.String(), "", body, originalTranslated, requestedModel, requestPath, opts.Headers) |
| 776 | body, _ = sjson.SetBytes(body, "model", baseModel) |
| 777 | body, _ = sjson.SetBytes(body, "stream", true) |
| 778 | body, _ = sjson.DeleteBytes(body, "previous_response_id") |
| 779 | body, _ = sjson.DeleteBytes(body, "prompt_cache_retention") |
| 780 | body, _ = sjson.DeleteBytes(body, "safety_identifier") |
| 781 | body, _ = sjson.DeleteBytes(body, "stream_options") |
| 782 | body = normalizeCodexInstructions(body) |
| 783 | if e.cfg == nil || e.cfg.DisableImageGeneration == config.DisableImageGenerationOff { |
| 784 | body = ensureImageGenerationTool(body, baseModel, auth) |
| 785 | } |
| 786 | body = sanitizeOpenAIResponsesReasoningEncryptedContent(ctx, "codex executor", body) |
| 787 | body = normalizeCodexParallelToolCallsForTools(body) |
| 788 | body, replayScope, errReplay := applyCodexReasoningReplayCacheRequired(ctx, from, req, opts, body) |
| 789 | if errReplay != nil { |
| 790 | return resp, errReplay |
| 791 | } |
| 792 | reporter.SetTranslatedReasoningEffort(body, to.String()) |
| 793 | |
| 794 | url := strings.TrimSuffix(baseURL, "/") + "/responses" |
| 795 | var identityState codexIdentityConfuseState |
| 796 | httpReq, upstreamBody, identityState, err := e.cacheHelper(ctx, from, url, auth, req, originalPayloadSource, body) |
| 797 | if err != nil { |
| 798 | return resp, err |