refactor(outbound,agents): extract shared payload and queue helpers

This commit is contained in:
Peter Steinberger
2026-03-07 23:07:11 +00:00
parent 7ab49a7fb7
commit 5b27b0cecf
2 changed files with 78 additions and 45 deletions

View File

@@ -297,6 +297,42 @@ function shouldEnableOpenAIResponsesServerCompaction(
return model.provider === "openai";
}
function shouldStripResponsesStore(
model: { api?: unknown; compat?: { supportsStore?: boolean } },
forceStore: boolean,
): boolean {
if (forceStore) {
return false;
}
if (typeof model.api !== "string") {
return false;
}
return OPENAI_RESPONSES_APIS.has(model.api) && model.compat?.supportsStore === false;
}
function applyOpenAIResponsesPayloadOverrides(params: {
payloadObj: Record<string, unknown>;
forceStore: boolean;
stripStore: boolean;
useServerCompaction: boolean;
compactThreshold: number;
}): void {
if (params.forceStore) {
params.payloadObj.store = true;
}
if (params.stripStore) {
delete params.payloadObj.store;
}
if (params.useServerCompaction && params.payloadObj.context_management === undefined) {
params.payloadObj.context_management = [
{
type: "compaction",
compact_threshold: params.compactThreshold,
},
];
}
}
function createOpenAIResponsesContextManagementWrapper(
baseStreamFn: StreamFn | undefined,
extraParams: Record<string, unknown> | undefined,
@@ -308,10 +344,7 @@ function createOpenAIResponsesContextManagementWrapper(
// Strip `store` from the payload when the model declares supportsStore=false.
// pi-ai upstream hardcodes `store: false` for Responses API; strict
// OpenAI-compatible endpoints (e.g. Gemini via Cloudflare) reject it.
const stripStore =
!forceStore &&
OPENAI_RESPONSES_APIS.has(String(model.api ?? "")) &&
(model as { compat?: { supportsStore?: boolean } }).compat?.supportsStore === false;
const stripStore = shouldStripResponsesStore(model, forceStore);
if (!forceStore && !useServerCompaction && !stripStore) {
return underlying(model, context, options);
}
@@ -324,21 +357,13 @@ function createOpenAIResponsesContextManagementWrapper(
...options,
onPayload: (payload) => {
if (payload && typeof payload === "object") {
const payloadObj = payload as Record<string, unknown>;
if (forceStore) {
payloadObj.store = true;
}
if (stripStore) {
delete payloadObj.store;
}
if (useServerCompaction && payloadObj.context_management === undefined) {
payloadObj.context_management = [
{
type: "compaction",
compact_threshold: compactThreshold,
},
];
}
applyOpenAIResponsesPayloadOverrides({
payloadObj: payload as Record<string, unknown>,
forceStore,
stripStore,
useServerCompaction,
compactThreshold,
});
}
originalOnPayload?.(payload);
},