Skip to content

feat(agent): route slack-originated tasks to slack_app gateway product#2225

Open
VojtechBartos wants to merge 1 commit into
mainfrom
vojtab/slack-bot-ai-credits-billing
Open

feat(agent): route slack-originated tasks to slack_app gateway product#2225
VojtechBartos wants to merge 1 commit into
mainfrom
vojtab/slack-bot-ai-credits-billing

Conversation

@VojtechBartos
Copy link
Copy Markdown
Member

Problem

The agent in the sandbox calls the LLM gateway as posthog_code regardless of how the task started. Slack-bot mentions are now billed differently from desktop tasks (gateway tags slack_app traffic with \$ai_billable=true), so the agent needs to send the correct product per task.

Changes

  • Add "slack_app" to the GatewayProduct type union (packages/agent/src/utils/gateway.ts).
  • Add "slack" to the Task.origin_product union (packages/agent/src/types.ts) to mirror the Python Task.OriginProduct.SLACK value already set on the backend.
  • configureEnvironment now accepts originProduct?: Task["origin_product"] | null. Mapping: "slack"slack_app, internal=truebackground_agents, else → posthog_code. Call site reads preTask?.origin_product from the Task fetched at session init.
  • Three new unit tests covering the slack → slack_app mapping, slack-over-internal precedence, and non-slack fallback.

Pairs with PostHog/posthog#59040 (gateway-side product registration + per-turn quota gate). Both ship behind the existing posthog-code-slack-availability flag — neither side has user-visible effect until the flag is enabled.

How did you test this?

I'm an agent. No manual testing.

  • pnpm --filter @posthog/agent test src/server/agent-server.configure-environment.test.ts8 passed (5 pre-existing + 3 new).
  • Typecheck error count unchanged from clean main (pre-existing module-resolution noise unrelated to this change).
  • biome format --write clean on touched files.

Publish to changelog?

no

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 21, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
packages/agent/src/server/agent-server.configure-environment.test.ts:92-127
**Prefer parameterised tests for product-routing cases**

The three new tests exercise the same `configureEnvironment` code path with different `(originProduct, isInternal, expectedProduct)` tuples — a textbook candidate for `it.each`. The codebase's own rule is to always prefer parameterised tests, and duplicating the arrange/assert boilerplate across three separate `it()` blocks makes it harder to see the full matrix at a glance and to add future cases. A single `it.each` table would also make it obvious that the `(originProduct="user_created", isInternal=true) → background_agents` cell is currently untested.

Reviews (1): Last reviewed commit: "feat(agent): route slack-originated task..." | Re-trigger Greptile

Comment on lines +92 to +127
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",
);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Prefer parameterised tests for product-routing cases

The three new tests exercise the same configureEnvironment code path with different (originProduct, isInternal, expectedProduct) tuples — a textbook candidate for it.each. The codebase's own rule is to always prefer parameterised tests, and duplicating the arrange/assert boilerplate across three separate it() blocks makes it harder to see the full matrix at a glance and to add future cases. A single it.each table would also make it obvious that the (originProduct="user_created", isInternal=true) → background_agents cell is currently untested.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/agent/src/server/agent-server.configure-environment.test.ts
Line: 92-127

Comment:
**Prefer parameterised tests for product-routing cases**

The three new tests exercise the same `configureEnvironment` code path with different `(originProduct, isInternal, expectedProduct)` tuples — a textbook candidate for `it.each`. The codebase's own rule is to always prefer parameterised tests, and duplicating the arrange/assert boilerplate across three separate `it()` blocks makes it harder to see the full matrix at a glance and to add future cases. A single `it.each` table would also make it obvious that the `(originProduct="user_created", isInternal=true) → background_agents` cell is currently untested.

**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@VojtechBartos VojtechBartos force-pushed the vojtab/slack-bot-ai-credits-billing branch from f7bc6de to 05a3c88 Compare May 25, 2026 09:29
@VojtechBartos VojtechBartos force-pushed the vojtab/slack-bot-ai-credits-billing branch from 05a3c88 to 1ebba3b Compare May 25, 2026 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant