Repository navigation
Strip previous_response_id for stateless Responses upstreams #3143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 3 commits into
cheaper-inference:release/v3.8.10
from
JxnLexn:fix_custom-endpoint
Jun 4, 2026
+405
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { isOpenAIResponsesStoreEnabled } from "@/lib/providers/requestDefaults"; | ||
| import { | ||
| DEFAULT_RESPONSES_PREVIOUS_RESPONSE_ID_MODE, | ||
| RESPONSES_PREVIOUS_RESPONSE_ID_MODES, | ||
| type ResponsesPreviousResponseIdMode, | ||
| } from "@/shared/constants/responsesPreviousResponseId"; | ||
| import { FORMATS } from "../translator/formats.ts"; | ||
|
|
||
| type JsonRecord = Record<string, unknown>; | ||
|
|
||
| type ApplyResponsesPreviousResponseIdPolicyOptions = { | ||
| mode: unknown; | ||
| sourceFormat?: unknown; | ||
| targetFormat?: unknown; | ||
| credentials?: unknown; | ||
| }; | ||
|
|
||
| const MODE_SET = new Set<string>(RESPONSES_PREVIOUS_RESPONSE_ID_MODES); | ||
|
|
||
| function toRecord(value: unknown): JsonRecord { | ||
| return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : {}; | ||
| } | ||
|
|
||
| export function normalizeResponsesPreviousResponseIdMode( | ||
| value: unknown | ||
| ): ResponsesPreviousResponseIdMode { | ||
| if (typeof value === "string" && MODE_SET.has(value)) { | ||
| return value as ResponsesPreviousResponseIdMode; | ||
| } | ||
| return DEFAULT_RESPONSES_PREVIOUS_RESPONSE_ID_MODE; | ||
| } | ||
|
|
||
| export function shouldStripPreviousResponseId({ | ||
| mode, | ||
| sourceFormat, | ||
| targetFormat, | ||
| credentials, | ||
| }: ApplyResponsesPreviousResponseIdPolicyOptions): boolean { | ||
| const normalizedMode = normalizeResponsesPreviousResponseIdMode(mode); | ||
| if (normalizedMode === "preserve") return false; | ||
| if (normalizedMode === "strip") return true; | ||
|
|
||
| const isResponsesSource = sourceFormat === FORMATS.OPENAI_RESPONSES; | ||
| const isResponsesTarget = targetFormat === FORMATS.OPENAI_RESPONSES; | ||
| if (!isResponsesSource && !isResponsesTarget) return false; | ||
|
|
||
| // `previous_response_id` is only safe when the upstream actually keeps | ||
| // Responses state. OmniRoute defaults to stateless upstream calls, so auto | ||
| // strips unless the connection explicitly opts into OpenAI Responses storage. | ||
| const providerSpecificData = toRecord(toRecord(credentials).providerSpecificData); | ||
| return !isOpenAIResponsesStoreEnabled(providerSpecificData); | ||
| } | ||
|
|
||
| export function applyResponsesPreviousResponseIdPolicy( | ||
| body: unknown, | ||
| options: ApplyResponsesPreviousResponseIdPolicyOptions | ||
| ): { body: unknown; stripped: boolean; mode: ResponsesPreviousResponseIdMode } { | ||
| const mode = normalizeResponsesPreviousResponseIdMode(options.mode); | ||
| if (!body || typeof body !== "object" || Array.isArray(body)) { | ||
| return { body, stripped: false, mode }; | ||
| } | ||
|
|
||
| const record = body as JsonRecord; | ||
| if (!Object.hasOwn(record, "previous_response_id")) { | ||
| return { body, stripped: false, mode }; | ||
| } | ||
|
|
||
| if (!shouldStripPreviousResponseId({ ...options, mode })) { | ||
| return { body, stripped: false, mode }; | ||
| } | ||
|
|
||
| const next = { ...record }; | ||
| delete next.previous_response_id; | ||
| return { body: next, stripped: true, mode }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/app/(dashboard)/dashboard/settings/components/ResponsesStatePolicyTab.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useState, useRef } from "react"; | ||
| import { useTranslations } from "next-intl"; | ||
| import { Card, Select } from "@/shared/components"; | ||
| import { | ||
| DEFAULT_RESPONSES_PREVIOUS_RESPONSE_ID_MODE, | ||
| RESPONSES_PREVIOUS_RESPONSE_ID_MODES, | ||
| type ResponsesPreviousResponseIdMode, | ||
| } from "@/shared/constants/responsesPreviousResponseId"; | ||
|
|
||
| const MODE_SET = new Set<string>(RESPONSES_PREVIOUS_RESPONSE_ID_MODES); | ||
|
|
||
| function normalizeMode(value: unknown): ResponsesPreviousResponseIdMode { | ||
| return typeof value === "string" && MODE_SET.has(value) | ||
| ? (value as ResponsesPreviousResponseIdMode) | ||
| : DEFAULT_RESPONSES_PREVIOUS_RESPONSE_ID_MODE; | ||
| } | ||
|
|
||
| export default function ResponsesStatePolicyTab() { | ||
| const [mode, setMode] = useState<ResponsesPreviousResponseIdMode>( | ||
| DEFAULT_RESPONSES_PREVIOUS_RESPONSE_ID_MODE | ||
| ); | ||
| const [loading, setLoading] = useState(true); | ||
| const [saving, setSaving] = useState(false); | ||
| const [status, setStatus] = useState<"" | "saved" | "error">(""); | ||
| const t = useTranslations("settings"); | ||
|
|
||
| useEffect(() => { | ||
| let cancelled = false; | ||
| fetch("/api/settings") | ||
| .then((res) => res.json()) | ||
| .then((data) => { | ||
| if (cancelled) return; | ||
| setMode(normalizeMode(data?.responsesPreviousResponseIdMode)); | ||
| setLoading(false); | ||
| }) | ||
| .catch(() => { | ||
| if (!cancelled) setLoading(false); | ||
| }); | ||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, []); | ||
|
|
||
| const save = async (nextMode: ResponsesPreviousResponseIdMode) => { | ||
| if (saving || loading || nextMode === mode) return; | ||
| const previousMode = mode; | ||
| setMode(nextMode); | ||
| setSaving(true); | ||
| setStatus(""); | ||
|
|
||
| try { | ||
| const res = await fetch("/api/settings", { | ||
| method: "PATCH", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ responsesPreviousResponseIdMode: nextMode }), | ||
| }); | ||
| if (!res.ok) { | ||
| setMode(previousMode); | ||
| setStatus("error"); | ||
| return; | ||
| } | ||
| setStatus("saved"); | ||
| setTimeout(() => setStatus(""), 2000); | ||
| } catch { | ||
| setMode(previousMode); | ||
| setStatus("error"); | ||
| } finally { | ||
| setSaving(false); | ||
| } | ||
| }; | ||
|
Comment on lines
+20
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Manage the status timeout using a |
||
|
|
||
| return ( | ||
| <Card> | ||
| <div className="flex items-center gap-3 mb-3"> | ||
| <div className="p-2 rounded-lg bg-emerald-500/10 text-emerald-500"> | ||
| <span className="material-symbols-outlined text-[20px]" aria-hidden="true"> | ||
| account_tree | ||
| </span> | ||
| </div> | ||
| <div className="flex-1"> | ||
| <h3 className="text-lg font-semibold">{t("responsesStateTitle")}</h3> | ||
| <p className="text-sm text-text-muted">{t("responsesStateDesc")}</p> | ||
| </div> | ||
| <div className="min-w-[7rem] flex justify-end"> | ||
| {status === "saved" && ( | ||
| <span className="text-xs font-medium text-emerald-500 flex items-center gap-1"> | ||
| <span className="material-symbols-outlined text-[14px]">check_circle</span>{" "} | ||
| {t("saved")} | ||
| </span> | ||
| )} | ||
| {status === "error" && ( | ||
| <span className="text-xs font-medium text-rose-500 flex items-center gap-1"> | ||
| <span className="material-symbols-outlined text-[14px]">error</span>{" "} | ||
| {t("responsesStateSaveError")} | ||
| </span> | ||
| )} | ||
| </div> | ||
| </div> | ||
|
|
||
| <Select | ||
| label={t("responsesStateModeLabel")} | ||
| value={mode} | ||
| disabled={loading || saving} | ||
| onChange={(e) => save(normalizeMode(e.target.value))} | ||
| options={[ | ||
| { value: "auto", label: t("responsesStateModeAuto") }, | ||
| { value: "strip", label: t("responsesStateModeStrip") }, | ||
| { value: "preserve", label: t("responsesStateModePreserve") }, | ||
| ]} | ||
| /> | ||
|
|
||
| <p className="mt-4 text-xs text-text-muted/80 flex items-start gap-1.5 leading-relaxed"> | ||
| <span className="material-symbols-outlined text-[14px] mt-0.5">info</span> | ||
| <span>{t("responsesStateHint")}</span> | ||
| </p> | ||
| </Card> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const RESPONSES_PREVIOUS_RESPONSE_ID_MODES = ["auto", "strip", "preserve"] as const; | ||
|
|
||
| export type ResponsesPreviousResponseIdMode = (typeof RESPONSES_PREVIOUS_RESPONSE_ID_MODES)[number]; | ||
|
|
||
| export const DEFAULT_RESPONSES_PREVIOUS_RESPONSE_ID_MODE: ResponsesPreviousResponseIdMode = "auto"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Unused import
useRef- imported from React but never referenced in this component. Unused imports increase bundle size and can confuse developers. Should remove.