Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/agent/src/server/agent-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,10 @@ Important:

private configureEnvironment(): void {
const { apiKey, apiUrl, projectId } = this.config;
const gatewayUrl = process.env.LLM_GATEWAY_URL || getLlmGatewayUrl(apiUrl);
const product =
this.config.mode === "background" ? "background_agents" : "twig";
const gatewayUrl =
process.env.LLM_GATEWAY_URL || getLlmGatewayUrl(apiUrl, product);
const openaiBaseUrl = gatewayUrl.endsWith("/v1")
? gatewayUrl
: `${gatewayUrl}/v1`;
Expand Down
13 changes: 9 additions & 4 deletions packages/agent/src/utils/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
export function getLlmGatewayUrl(posthogHost: string): string {
export type GatewayProduct = "twig" | "background_agents";

export function getLlmGatewayUrl(
posthogHost: string,
product: GatewayProduct = "twig",
): string {
const url = new URL(posthogHost);
const hostname = url.hostname;

// Local development (normalize 127.0.0.1 to localhost)
if (hostname === "localhost" || hostname === "127.0.0.1") {
return `${url.protocol}//localhost:3308/twig`;
return `${url.protocol}//localhost:3308/${product}`;
}

// Docker containers accessing host
if (hostname === "host.docker.internal") {
return `${url.protocol}//host.docker.internal:3308/twig`;
return `${url.protocol}//host.docker.internal:3308/${product}`;
}

// Production - extract region from hostname, default to US
const region = hostname.match(/^(us|eu)\.posthog\.com$/)?.[1] ?? "us";
return `https://gateway.${region}.posthog.com/twig`;
return `https://gateway.${region}.posthog.com/${product}`;
}
Loading