mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 04:02:21 +00:00
Allow async streamFn for dynamic proxy settings
- StreamFn type now allows returning Promise - agent-loop awaits streamFn result - createStreamFn takes getProxyUrl callback, reads settings on each call
This commit is contained in:
parent
e0be2e650d
commit
92898f486b
4 changed files with 16 additions and 11 deletions
|
|
@ -211,7 +211,7 @@ async function streamAssistantResponse(
|
||||||
const resolvedApiKey =
|
const resolvedApiKey =
|
||||||
(config.getApiKey ? await config.getApiKey(config.model.provider) : undefined) || config.apiKey;
|
(config.getApiKey ? await config.getApiKey(config.model.provider) : undefined) || config.apiKey;
|
||||||
|
|
||||||
const response = streamFunction(config.model, llmContext, {
|
const response = await streamFunction(config.model, llmContext, {
|
||||||
...config,
|
...config,
|
||||||
apiKey: resolvedApiKey,
|
apiKey: resolvedApiKey,
|
||||||
signal,
|
signal,
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,10 @@ import type {
|
||||||
} from "@mariozechner/pi-ai";
|
} from "@mariozechner/pi-ai";
|
||||||
import type { Static, TSchema } from "@sinclair/typebox";
|
import type { Static, TSchema } from "@sinclair/typebox";
|
||||||
|
|
||||||
export type StreamFn = typeof streamSimple;
|
/** Stream function - can return sync or Promise for async config lookup */
|
||||||
|
export type StreamFn = (
|
||||||
|
...args: Parameters<typeof streamSimple>
|
||||||
|
) => ReturnType<typeof streamSimple> | Promise<ReturnType<typeof streamSimple>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for the agent loop.
|
* Configuration for the agent loop.
|
||||||
|
|
|
||||||
|
|
@ -161,10 +161,6 @@ const createAgent = async (initialState?: Partial<AgentState>) => {
|
||||||
agentUnsubscribe();
|
agentUnsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read proxy settings for streamFn
|
|
||||||
const proxyEnabled = await storage.settings.get<boolean>("proxy.enabled");
|
|
||||||
const proxyUrl = proxyEnabled ? (await storage.settings.get<string>("proxy.url")) || undefined : undefined;
|
|
||||||
|
|
||||||
agent = new Agent({
|
agent = new Agent({
|
||||||
initialState: initialState || {
|
initialState: initialState || {
|
||||||
systemPrompt: `You are a helpful AI assistant with access to various tools.
|
systemPrompt: `You are a helpful AI assistant with access to various tools.
|
||||||
|
|
@ -186,8 +182,11 @@ Feel free to use these tools when needed to provide accurate and helpful respons
|
||||||
const key = await storage.providerKeys.get(provider);
|
const key = await storage.providerKeys.get(provider);
|
||||||
return key ?? undefined;
|
return key ?? undefined;
|
||||||
},
|
},
|
||||||
// Use streamFn with CORS proxy support
|
// Use streamFn with CORS proxy support (reads settings on each call)
|
||||||
streamFn: createStreamFn(proxyUrl),
|
streamFn: createStreamFn(async () => {
|
||||||
|
const enabled = await storage.settings.get<boolean>("proxy.enabled");
|
||||||
|
return enabled ? (await storage.settings.get<string>("proxy.url")) || undefined : undefined;
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
agentUnsubscribe = agent.subscribe((event: any) => {
|
agentUnsubscribe = agent.subscribe((event: any) => {
|
||||||
|
|
|
||||||
|
|
@ -114,13 +114,16 @@ export function isCorsError(error: unknown): boolean {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a streamFn that applies CORS proxy when needed.
|
* Create a streamFn that applies CORS proxy when needed.
|
||||||
|
* Reads proxy settings from storage on each call.
|
||||||
*
|
*
|
||||||
* @param proxyUrl - CORS proxy URL, or undefined to disable
|
* @param getProxyUrl - Async function to get current proxy URL (or undefined if disabled)
|
||||||
* @returns A streamFn compatible with Agent's streamFn option
|
* @returns A streamFn compatible with Agent's streamFn option
|
||||||
*/
|
*/
|
||||||
export function createStreamFn(proxyUrl?: string) {
|
export function createStreamFn(getProxyUrl: () => Promise<string | undefined>) {
|
||||||
return (model: Model<any>, context: Context, options?: SimpleStreamOptions) => {
|
return async (model: Model<any>, context: Context, options?: SimpleStreamOptions) => {
|
||||||
const apiKey = options?.apiKey;
|
const apiKey = options?.apiKey;
|
||||||
|
const proxyUrl = await getProxyUrl();
|
||||||
|
|
||||||
if (!apiKey || !proxyUrl) {
|
if (!apiKey || !proxyUrl) {
|
||||||
return streamSimple(model, context, options);
|
return streamSimple(model, context, options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue