mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 01:04:36 +00:00
Better proxy handling.
This commit is contained in:
parent
ec50ede6c2
commit
b6b64dff86
6 changed files with 298 additions and 160 deletions
|
|
@ -6,11 +6,12 @@ import {
|
|||
type UserMessage,
|
||||
} from "@mariozechner/pi-ai";
|
||||
import { getAppStorage } from "../../storage/app-storage.js";
|
||||
import { applyProxyIfNeeded } from "../../utils/proxy-utils.js";
|
||||
import type { AgentRunConfig, AgentTransport } from "./types.js";
|
||||
|
||||
/**
|
||||
* Transport that calls LLM providers directly.
|
||||
* Optionally routes calls through a CORS proxy if enabled in settings.
|
||||
* Uses CORS proxy only for providers that require it (Anthropic OAuth, Z-AI).
|
||||
*/
|
||||
export class ProviderTransport implements AgentTransport {
|
||||
async *run(messages: Message[], userMessage: Message, cfg: AgentRunConfig, signal?: AbortSignal) {
|
||||
|
|
@ -20,18 +21,12 @@ export class ProviderTransport implements AgentTransport {
|
|||
throw new Error("no-api-key");
|
||||
}
|
||||
|
||||
// Check if CORS proxy is enabled
|
||||
// Get proxy URL from settings (if available)
|
||||
const proxyEnabled = await getAppStorage().settings.get<boolean>("proxy.enabled");
|
||||
const proxyUrl = await getAppStorage().settings.get<string>("proxy.url");
|
||||
|
||||
// Clone model and modify baseUrl if proxy is enabled
|
||||
let model = cfg.model;
|
||||
if (proxyEnabled && proxyUrl && cfg.model.baseUrl) {
|
||||
model = {
|
||||
...cfg.model,
|
||||
baseUrl: `${proxyUrl}/?url=${encodeURIComponent(cfg.model.baseUrl)}`,
|
||||
};
|
||||
}
|
||||
// Apply proxy only if this provider/key combination requires it
|
||||
const model = applyProxyIfNeeded(cfg.model, apiKey, proxyEnabled ? proxyUrl || undefined : undefined);
|
||||
|
||||
// Messages are already LLM-compatible (filtered by Agent)
|
||||
const context: AgentContext = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue