Add createStreamFn for CORS proxy support
- createStreamFn(proxyUrl?) returns a sync streamFn that applies proxy - Example reads proxy settings once when creating Agent - Matches old ProviderTransport behavior
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Api, Model } from "@mariozechner/pi-ai";
|
||||
import type { Api, Context, Model, SimpleStreamOptions } from "@mariozechner/pi-ai";
|
||||
import { streamSimple } from "@mariozechner/pi-ai";
|
||||
|
||||
/**
|
||||
* Centralized proxy decision logic.
|
||||
@@ -110,3 +111,21 @@ export function isCorsError(error: unknown): boolean {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a streamFn that applies CORS proxy when needed.
|
||||
*
|
||||
* @param proxyUrl - CORS proxy URL, or undefined to disable
|
||||
* @returns A streamFn compatible with Agent's streamFn option
|
||||
*/
|
||||
export function createStreamFn(proxyUrl?: string) {
|
||||
return (model: Model<any>, context: Context, options?: SimpleStreamOptions) => {
|
||||
const apiKey = options?.apiKey;
|
||||
if (!apiKey || !proxyUrl) {
|
||||
return streamSimple(model, context, options);
|
||||
}
|
||||
|
||||
const proxiedModel = applyProxyIfNeeded(model, apiKey, proxyUrl);
|
||||
return streamSimple(proxiedModel, context, options);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user