From f7bc6debc0d6b657fa0fef4427d4917d70aa5cd6 Mon Sep 17 00:00:00 2001 From: Vojta Bartos Date: Tue, 19 May 2026 17:27:45 +0200 Subject: [PATCH] feat(agent): route slack-originated tasks to slack_app gateway product --- ...agent-server.configure-environment.test.ts | 43 ++++++++++++++++++- packages/agent/src/server/agent-server.ts | 17 ++++++-- packages/agent/src/types.ts | 3 +- packages/agent/src/utils/gateway.ts | 2 +- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/packages/agent/src/server/agent-server.configure-environment.test.ts b/packages/agent/src/server/agent-server.configure-environment.test.ts index e7d903b86..647a255e4 100644 --- a/packages/agent/src/server/agent-server.configure-environment.test.ts +++ b/packages/agent/src/server/agent-server.configure-environment.test.ts @@ -1,8 +1,12 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import type { Task } from "../types"; import { AgentServer } from "./agent-server"; interface TestableServer { - configureEnvironment(args?: { isInternal?: boolean }): void; + configureEnvironment(args?: { + isInternal?: boolean; + originProduct?: Task["origin_product"] | null; + }): void; } const ENV_KEYS_UNDER_TEST = [ @@ -85,6 +89,43 @@ describe("AgentServer.configureEnvironment", () => { expect(fromBackground).toBe("https://gateway.us.posthog.com/posthog_code"); }); + it("tags as slack_app when the task was initiated from Slack", () => { + buildServer("interactive").configureEnvironment({ + originProduct: "slack", + }); + + expect(process.env.LLM_GATEWAY_URL).toBe( + "https://gateway.us.posthog.com/slack_app", + ); + expect(process.env.ANTHROPIC_BASE_URL).toBe( + "https://gateway.us.posthog.com/slack_app", + ); + expect(process.env.OPENAI_BASE_URL).toBe( + "https://gateway.us.posthog.com/slack_app/v1", + ); + }); + + it("prefers slack_app over background_agents when both signals are present", () => { + buildServer("interactive").configureEnvironment({ + isInternal: true, + originProduct: "slack", + }); + + expect(process.env.LLM_GATEWAY_URL).toBe( + "https://gateway.us.posthog.com/slack_app", + ); + }); + + it("falls back to posthog_code for non-slack origin products", () => { + buildServer("background").configureEnvironment({ + originProduct: "user_created", + }); + + expect(process.env.LLM_GATEWAY_URL).toBe( + "https://gateway.us.posthog.com/posthog_code", + ); + }); + it("respects the LLM_GATEWAY_URL override regardless of internal flag", () => { process.env.LLM_GATEWAY_URL = "http://ngrok.test/proxy"; diff --git a/packages/agent/src/server/agent-server.ts b/packages/agent/src/server/agent-server.ts index 12ea84c47..f53a76e28 100644 --- a/packages/agent/src/server/agent-server.ts +++ b/packages/agent/src/server/agent-server.ts @@ -42,6 +42,7 @@ import type { GitCheckpointEvent, HandoffLocalGitState, LogLevel, + Task, TaskRun, TaskRunArtifact, } from "../types"; @@ -833,7 +834,10 @@ export class AgentServer { }), ]); - this.configureEnvironment({ isInternal: preTask?.internal === true }); + this.configureEnvironment({ + isInternal: preTask?.internal === true, + originProduct: preTask?.origin_product, + }); const prUrl = getTaskRunStateString(preTaskRun, "slack_notified_pr_url"); @@ -1785,13 +1789,18 @@ ${attributionInstructions} private configureEnvironment({ isInternal = false, + originProduct, }: { isInternal?: boolean; + originProduct?: Task["origin_product"] | null; } = {}): void { const { apiKey, apiUrl, projectId } = this.config; - const product: GatewayProduct = isInternal - ? "background_agents" - : "posthog_code"; + const product: GatewayProduct = + originProduct === "slack" + ? "slack_app" + : isInternal + ? "background_agents" + : "posthog_code"; const gatewayUrl = process.env.LLM_GATEWAY_URL || getLlmGatewayUrl(apiUrl, product); const openaiBaseUrl = gatewayUrl.endsWith("/v1") diff --git a/packages/agent/src/types.ts b/packages/agent/src/types.ts index 18e5572c0..fb670ffb4 100644 --- a/packages/agent/src/types.ts +++ b/packages/agent/src/types.ts @@ -37,7 +37,8 @@ export interface Task { | "eval_clusters" | "user_created" | "support_queue" - | "session_summaries"; + | "session_summaries" + | "slack"; github_integration?: number | null; repository: string; // Format: "organization/repository" (e.g., "posthog/posthog-js") json_schema?: Record | null; // JSON schema for task output validation diff --git a/packages/agent/src/utils/gateway.ts b/packages/agent/src/utils/gateway.ts index 161247538..082020e7d 100644 --- a/packages/agent/src/utils/gateway.ts +++ b/packages/agent/src/utils/gateway.ts @@ -1,4 +1,4 @@ -export type GatewayProduct = "posthog_code" | "background_agents"; +export type GatewayProduct = "posthog_code" | "background_agents" | "slack_app"; function getGatewayBaseUrl(posthogHost: string): string { const url = new URL(posthogHost);