Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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";

Expand Down
17 changes: 13 additions & 4 deletions packages/agent/src/server/agent-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
GitCheckpointEvent,
HandoffLocalGitState,
LogLevel,
Task,
TaskRun,
TaskRunArtifact,
} from "../types";
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion packages/agent/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | null; // JSON schema for task output validation
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/utils/gateway.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Loading