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
26 changes: 12 additions & 14 deletions packages/junior/src/chat/slack/footer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { TurnThinkingSelection } from "@/chat/services/turn-thinking-level"
import type { AgentTurnUsage } from "@/chat/usage";

const SENTRY_CONVERSATION_SEARCH_STATS_PERIOD = "14d";
const ORG_ID_HOST_RE = /^o(\d+)\./;

interface SlackMrkdwnTextObject {
text: string;
Expand Down Expand Up @@ -55,19 +54,13 @@ function escapeSlackLinkUrl(url: string): string {
.replaceAll(">", "%3E");
}

function toOptionalString(value: unknown): string | undefined {
if (typeof value === "number" && Number.isFinite(value)) {
return String(value);
}
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}

function quoteSentrySearchValue(value: string): string {
return `"${value.replaceAll("\\", "\\\\").replaceAll('"', '\\"')}"`;
}

function getDsnOrgId(host: string | undefined): string | undefined {
return host?.match(ORG_ID_HOST_RE)?.[1];
function getSentryOrgSlug(): string | undefined {
const slug = process.env.SENTRY_ORG_SLUG?.trim();
return slug || undefined;
}

function isSentrySaasDsnHost(host: string): boolean {
Expand Down Expand Up @@ -98,9 +91,8 @@ function getSentryConversationSearchUrl(
return undefined;
}

const orgId =
toOptionalString(client?.getOptions().orgId) ?? getDsnOrgId(dsn.host);
if (!orgId) {
const orgSlug = getSentryOrgSlug();
if (!orgSlug) {
return undefined;
}

Expand All @@ -112,7 +104,13 @@ function getSentryConversationSearchUrl(
params.set("project", dsn.projectId);
params.set("statsPeriod", SENTRY_CONVERSATION_SEARCH_STATS_PERIOD);

return `${buildSentryWebBaseUrl(dsn)}/organizations/${orgId}/explore/traces/?${params.toString()}`;
const search = `explore/traces/?${params.toString()}`;

if (isSentrySaasDsnHost(dsn.host)) {
return `https://${orgSlug}.sentry.io/${search}`;
}

return `${buildSentryWebBaseUrl(dsn)}/organizations/${orgSlug}/${search}`;
}

function formatSlackTokenCount(value: number): string {
Expand Down
1 change: 1 addition & 0 deletions packages/junior/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ AI_VISION_MODEL=
AI_WEB_SEARCH_MODEL=
REDIS_URL=
SENTRY_DSN=
SENTRY_ORG_SLUG=
`,
);

Expand Down
34 changes: 30 additions & 4 deletions packages/junior/tests/unit/slack/footer-sentry-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ async function loadFooter() {
}

afterEach(() => {
delete process.env.SENTRY_ORG_SLUG;
vi.doUnmock("@/chat/sentry");
vi.resetModules();
});

describe("Slack footer Sentry links", () => {
it("links the ID to an Explore traces search from the active SaaS DSN", async () => {
it("links the ID to an Explore traces search using org slug subdomain for SaaS", async () => {
process.env.SENTRY_ORG_SLUG = "my-org";
mockSentryClient({
dsn: {
protocol: "https",
Expand All @@ -53,14 +55,14 @@ describe("Slack footer Sentry links", () => {
elements: [
{
type: "mrkdwn",
text: "*ID:* <https://sentry.io/organizations/123/explore/traces/?query=gen_ai.conversation.id%3A%22slack%3AC123%3A1700000000.000100%22&amp;project=4501&amp;statsPeriod=14d|slack:C123:1700000000.000100>",
text: "*ID:* <https://my-org.sentry.io/explore/traces/?query=gen_ai.conversation.id%3A%22slack%3AC123%3A1700000000.000100%22&amp;project=4501&amp;statsPeriod=14d|slack:C123:1700000000.000100>",
},
],
},
]);
});

it("uses an explicit SDK orgId before the DSN host org ID", async () => {
it("leaves the ID plain when SENTRY_ORG_SLUG is not set even with numeric org data", async () => {
mockSentryClient({
dsn: {
protocol: "https",
Expand All @@ -77,7 +79,31 @@ describe("Slack footer Sentry links", () => {
items: [
{
label: "ID",
url: "https://sentry.io/organizations/456/explore/traces/?query=gen_ai.conversation.id%3A%22conversation-1%22&project=4501&statsPeriod=14d",
value: "conversation-1",
},
],
},
);
});

it("uses /organizations/{slug}/ for self-hosted DSN", async () => {
process.env.SENTRY_ORG_SLUG = "my-org";
mockSentryClient({
dsn: {
protocol: "https",
host: "sentry.example.com",
projectId: "4501",
},
});

const { buildSlackReplyFooter } = await loadFooter();

expect(buildSlackReplyFooter({ conversationId: "conversation-1" })).toEqual(
{
items: [
{
label: "ID",
url: "https://sentry.example.com/organizations/my-org/explore/traces/?query=gen_ai.conversation.id%3A%22conversation-1%22&project=4501&statsPeriod=14d",
value: "conversation-1",
},
],
Expand Down
Loading