import type { StreamFn } from "@mariozechner/pi-agent-core"; import { streamSimple } from "@mariozechner/pi-ai"; import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry"; import { applyAnthropicEphemeralCacheControlMarkers, buildCopilotDynamicHeaders, hasCopilotVisionInput, streamWithPayloadPatch, } from "openclaw/plugin-sdk/provider-stream"; type StreamContext = Parameters[1]; export function wrapCopilotAnthropicStream(baseStreamFn: StreamFn | undefined): StreamFn { const underlying = baseStreamFn ?? streamSimple; return (model, context, options) => { if (model.provider !== "github-copilot" || model.api !== "anthropic-messages") { return underlying(model, context, options); } return streamWithPayloadPatch( underlying, model, context, { ...options, headers: { ...buildCopilotDynamicHeaders({ messages: context.messages as StreamContext["messages"], hasImages: hasCopilotVisionInput(context.messages as StreamContext["messages"]), }), ...(options?.headers ?? {}), }, }, applyAnthropicEphemeralCacheControlMarkers, ); }; } export function wrapCopilotProviderStream(ctx: ProviderWrapStreamFnContext): StreamFn { return wrapCopilotAnthropicStream(ctx.streamFn); }