mirror of
https://github.com/openclaw/openclaw.git
synced 2026-10-03 01:29:56 +00:00
* fix(openai-completions): apply Anthropic cache markers on the managed transport * fix(openai-completions): preserve string content and explicit long cache TTL
30 lines
1.2 KiB
TypeScript
30 lines
1.2 KiB
TypeScript
import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
|
|
import {
|
|
applyAnthropicEphemeralCacheControlMarkers,
|
|
applyCompletionsAnthropicCacheControl,
|
|
createPayloadPatchStreamWrapper,
|
|
} from "openclaw/plugin-sdk/provider-stream-shared";
|
|
|
|
export function createDeepInfraAnthropicCacheWrapper(
|
|
baseStreamFn: StreamFn,
|
|
extraParams?: Record<string, unknown>,
|
|
): StreamFn {
|
|
return (model, context, options) => {
|
|
if (!model.id.toLowerCase().startsWith("anthropic/")) {
|
|
return baseStreamFn(model, context, options);
|
|
}
|
|
const isCompletions = model.api === "openai-completions";
|
|
const configured = options?.cacheRetention ?? extraParams?.cacheRetention;
|
|
const cacheRetention = configured === "none" || configured === "long" ? configured : "short";
|
|
return createPayloadPatchStreamWrapper(baseStreamFn, ({ payload }) => {
|
|
if (isCompletions) {
|
|
applyCompletionsAnthropicCacheControl(
|
|
payload,
|
|
cacheRetention === "none" ? null : { type: "ephemeral" },
|
|
);
|
|
} else {
|
|
applyAnthropicEphemeralCacheControlMarkers(payload);
|
|
}
|
|
})(model, context, isCompletions ? { ...options, cacheRetention } : options);
|
|
};
|
|
}
|