The release branch still had unresolved review comments around payload-rule atomicity, brand blocking, compact fallback controls, and response compatibility edge-cases. This patch pulls those behaviors back in line with the intended contracts and adds focused regression coverage for the fragile bridges. Constraint: The fixes must stay narrow to the PR branch so review/merge can continue without reopening the larger dev snapshot conflict set Rejected: Leave outdated-looking threads unresolved until after merge | reviewers still see concrete unfixed regressions in the current release branch Rejected: Re-run another broad merge from dev/main | too risky while the PR branch is already green and mergeable Confidence: medium Scope-risk: moderate Directive: If these release-branch fixes need to persist for future release cuts, backport them onto dev instead of re-fighting the same review threads next time Tested: Focused vitest for settings events, brand matcher, compact responses, codex compatibility, anthropic stream bridge, openai chat response bridge; typecheck; repo drift check; git diff --check Not-tested: Full npm test; live PR thread resolution state after push
88 lines
2.7 KiB
TypeScript
88 lines
2.7 KiB
TypeScript
function asTrimmedString(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : '';
|
|
}
|
|
|
|
function shouldStripCompactResponsesStore(sitePlatform?: string): boolean {
|
|
const normalized = asTrimmedString(sitePlatform).toLowerCase();
|
|
return normalized === 'codex' || normalized === 'sub2api';
|
|
}
|
|
|
|
function shouldForceCompactResponsesJsonAccept(sitePlatform?: string): boolean {
|
|
return shouldStripCompactResponsesStore(sitePlatform);
|
|
}
|
|
|
|
export function shouldForceResponsesUpstreamStream(input: {
|
|
sitePlatform?: string;
|
|
isCompactRequest?: boolean;
|
|
}): boolean {
|
|
if (input.isCompactRequest) return false;
|
|
const sitePlatform = asTrimmedString(input.sitePlatform).toLowerCase();
|
|
return sitePlatform === 'codex' || sitePlatform === 'sub2api';
|
|
}
|
|
|
|
export function sanitizeCompactResponsesRequestBody(
|
|
body: Record<string, unknown>,
|
|
options?: {
|
|
sitePlatform?: string;
|
|
},
|
|
): Record<string, unknown> {
|
|
const next = { ...body };
|
|
delete next.stream;
|
|
delete next.stream_options;
|
|
if (shouldStripCompactResponsesStore(options?.sitePlatform)) {
|
|
delete next.store;
|
|
}
|
|
return next;
|
|
}
|
|
|
|
export function ensureCompactResponsesJsonAcceptHeader(
|
|
headers: Record<string, string>,
|
|
options?: {
|
|
sitePlatform?: string;
|
|
},
|
|
): Record<string, string> {
|
|
if (!shouldForceCompactResponsesJsonAccept(options?.sitePlatform)) return headers;
|
|
const nextHeaders = { ...headers };
|
|
delete (nextHeaders as Record<string, unknown>).Accept;
|
|
delete (nextHeaders as Record<string, unknown>).accept;
|
|
return {
|
|
...nextHeaders,
|
|
accept: 'application/json',
|
|
};
|
|
}
|
|
|
|
export function shouldFallbackCompactResponsesToResponses(input: {
|
|
status?: number;
|
|
rawErrText?: string;
|
|
requestPath?: string;
|
|
}): boolean {
|
|
const status = Number.isFinite(Number(input.status)) ? Number(input.status) : 0;
|
|
const compact = asTrimmedString(input.rawErrText).toLowerCase();
|
|
const requestPath = asTrimmedString(input.requestPath).toLowerCase();
|
|
const hasRawCompactHint = (
|
|
compact.includes('/responses/compact')
|
|
|| compact.includes('responses/compact')
|
|
|| compact.includes('compact endpoint')
|
|
|| /(^|[^a-z])compact([^a-z]|$)/.test(compact)
|
|
);
|
|
const hasCompactRequestPathHint = (
|
|
requestPath.endsWith('/responses/compact')
|
|
|| requestPath.endsWith('/v1/responses/compact')
|
|
);
|
|
const isInvalidCompactUrl = compact.includes('invalid url') && hasRawCompactHint;
|
|
|
|
if (status === 404 || status === 405 || status === 501) return true;
|
|
|
|
return (
|
|
(compact.includes("unknown parameter: 'stream'") && (hasRawCompactHint || hasCompactRequestPathHint))
|
|
|| isInvalidCompactUrl
|
|
|| (
|
|
hasRawCompactHint
|
|
&& (
|
|
compact.includes('not supported')
|
|
|| compact.includes('unsupported')
|
|
)
|
|
)
|
|
);
|
|
}
|