Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ List recent AI conversations
| Field | Type | Description |
|-------|------|-------------|
| `conversationId` | string | |
| `title` | string \| null | |
| `flow` | array | |
| `errors` | number | |
| `llmCalls` | number | |
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/conversation/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ export const viewCommand = buildCommand({
}
const org = resolved.org;

const { spans, truncated } = await withProgress(
const { spans, truncated, title } = await withProgress(
{
message: "Fetching conversation spans...",
json: flags.json,
},
() => getConversationSpans(org, conversationId)
);

const result = buildTranscriptResult(conversationId, org, spans);
const result = buildTranscriptResult(conversationId, org, spans, title);
result.truncated = truncated;
yield new CommandOutput<TranscriptResult>(result);
},
Expand Down
36 changes: 21 additions & 15 deletions packages/cli/src/lib/api/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
* Functions for listing and retrieving AI conversation data from the Sentry
* Explore AI-conversations endpoints.
*
* The `/organizations/{org}/ai-conversations/` endpoints are not yet in
* `@sentry/api` (getsentry/sentry-api-schema): they are a new AI-monitoring
* feature that has not been published to the API schema, so no generated SDK
* function or response type exists for them. Following the same pattern as the
* experimental endpoints in `logs.ts`/`traces.ts`, they are called directly via
* `apiRequestToRegion` with local Zod schemas. Pagination still goes through the
* shared `parseLinkHeader` helper, which wraps `@sentry/api`'s
* `parseSentryLinkHeader`. Revisit once these endpoints land in `@sentry/api`.
* The `/organizations/{org}/ai-conversations/` endpoints are PRIVATE and not
* yet in `@sentry/api` (getsentry/sentry-api-schema). Call them via
* `apiRequestToRegion` with local Zod schemas (same pattern as `logs.ts` /
* `traces.ts`). Details response shape is documented on
* `AIConversationDetailsSchema`. Pagination uses `parseLinkHeader`. Revisit
* once these endpoints land in `@sentry/api`.
*/

import { z } from "zod";

import {
type AIConversationDetails,
AIConversationDetailsSchema,
type AIConversationSpan,
AIConversationSpanSchema,
type ConversationListItem,
ConversationListItemSchema,
} from "../../types/conversation.js";
Expand Down Expand Up @@ -90,9 +89,12 @@ export async function getConversationSpans(
project?: string;
perPage?: number;
} = {}
): Promise<{ spans: AIConversationSpan[]; truncated: boolean }> {
): Promise<{
spans: AIConversationSpan[];
truncated: boolean;
title: string | null;
}> {
const regionUrl = await resolveOrgRegion(orgSlug);
const pageSchema = z.array(AIConversationSpanSchema);

const params: Record<string, string> = {
per_page: String(options.perPage ?? 1000),
Expand All @@ -103,20 +105,24 @@ export async function getConversationSpans(
}

const spans: AIConversationSpan[] = [];
let title: string | null = null;
let cursor: string | undefined;

for (let page = 0; page < MAX_PAGINATION_PAGES; page++) {
if (cursor) {
params.cursor = cursor;
}

const { data, headers } = await apiRequestToRegion<AIConversationSpan[]>(
const { data, headers } = await apiRequestToRegion<AIConversationDetails>(
regionUrl,
`/organizations/${orgSlug}/ai-conversations/${encodeURIComponent(conversationId)}/`,
{ params, schema: pageSchema }
{ params, schema: AIConversationDetailsSchema }
);

spans.push(...data);
if (page === 0) {
title = data.title;
}
spans.push(...data.spans);
const parsed = parseLinkHeader(headers.get("link") ?? null);
cursor = parsed.nextCursor;
if (!cursor) {
Expand All @@ -131,5 +137,5 @@ export async function getConversationSpans(
);
}

return { spans, truncated };
return { spans, truncated, title };
}
41 changes: 34 additions & 7 deletions packages/cli/src/lib/formatters/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function formatTimestamp(epochSeconds: number): string {
}

/**
* Minimum terminal width (columns) to show the full 7-column conversation
* table. Below this, the lower-value Tools/Errs/User columns are dropped so
* the core ID/Started/Tokens/First Input columns don't truncate aggressively.
* Minimum terminal width (columns) to show the full conversation table.
* Below this, Tools/Errs/User are dropped so the core ID/Title/Started/
* Tokens/First Input columns don't truncate aggressively.
*/
const WIDE_TABLE_MIN_TERM_WIDTH = 100;

Expand All @@ -50,6 +50,12 @@ const ID_COLUMN: Column<ConversationListItem> = {
truncate: true,
};

const TITLE_COLUMN: Column<ConversationListItem> = {
header: "Title",
value: (c) => (c.title ? escapeMarkdownCell(truncate(c.title, 40)) : "—"),
truncate: true,
};

const STARTED_COLUMN: Column<ConversationListItem> = {
header: "Started",
value: (c) => formatTimestamp(c.startTimestamp),
Expand Down Expand Up @@ -88,13 +94,15 @@ const FIRST_INPUT_COLUMN: Column<ConversationListItem> = {
/**
* Select conversation table columns for the current terminal width. Wide
* terminals get the full set; narrow ones (piped output defaults to 80) drop
* Tools/Errs/User to keep the remaining columns readable.
* Tools/Errs/User to keep the remaining columns readable. Title stays on
* both widths so stored conversation names remain visible.
*/
function selectConversationColumns(): Column<ConversationListItem>[] {
const termWidth = process.stdout.columns || 80;
if (termWidth >= WIDE_TABLE_MIN_TERM_WIDTH) {
return [
ID_COLUMN,
TITLE_COLUMN,
STARTED_COLUMN,
TOKENS_COLUMN,
TOOLS_COLUMN,
Expand All @@ -103,7 +111,13 @@ function selectConversationColumns(): Column<ConversationListItem>[] {
FIRST_INPUT_COLUMN,
];
}
return [ID_COLUMN, STARTED_COLUMN, TOKENS_COLUMN, FIRST_INPUT_COLUMN];
return [
ID_COLUMN,
TITLE_COLUMN,
STARTED_COLUMN,
TOKENS_COLUMN,
FIRST_INPUT_COLUMN,
];
}

export function formatConversationTable(items: ConversationListItem[]): string {
Expand Down Expand Up @@ -416,6 +430,8 @@ function formatTurnHuman(turn: ConversationTurn): string {
export type TranscriptResult = {
conversationId: string;
org: string;
/** Stored conversation title, or null when none has been titled yet. */
title: string | null;
turns: ConversationTurn[];
totalTokens: number;
spanCount: number;
Expand All @@ -427,9 +443,15 @@ export type TranscriptResult = {

export function formatTranscriptResult(result: TranscriptResult): string {
if (result.spanCount === 0) {
return `No spans found for conversation ${escapeMarkdownInline(result.conversationId)} in the last 30 days.`;
const id = escapeMarkdownInline(result.conversationId);
const titled = result.title
? `${id} (${escapeMarkdownInline(result.title)})`
: id;
return `No spans found for conversation ${titled} in the last 30 days.`;
}

// Keep the conversation ID in the heading (needed to re-run `conversation
// view`). Surface title as a table row so it is not duplicated in the heading.
const rows: [string, string][] = [
["Org", escapeMarkdownCell(result.org)],
["Projects", result.projects.map(escapeMarkdownCell).join(", ") || "—"],
Expand All @@ -439,6 +461,9 @@ export function formatTranscriptResult(result: TranscriptResult): string {
["Spans", String(result.spanCount)],
["Tokens", String(result.totalTokens)],
];
if (result.title) {
rows.unshift(["Title", escapeMarkdownCell(result.title)]);
}

const lines: string[] = [
`# AI Conversation: ${escapeMarkdownInline(result.conversationId)}`,
Expand All @@ -461,12 +486,14 @@ export function formatTranscriptResult(result: TranscriptResult): string {
export function buildTranscriptResult(
conversationId: string,
org: string,
spans: AIConversationSpan[]
spans: AIConversationSpan[],
title: string | null = null
): TranscriptResult {
const turns = extractTurns(spans);
return {
conversationId,
org,
title,
turns,
// Sum tokens per turn (ai_client spans only). Summing every span would
// double-count parent invoke_agent spans that aggregate child usage.
Expand Down
25 changes: 23 additions & 2 deletions packages/cli/src/types/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* AI Conversation types and Zod schemas.
*
* Schemas for the conversation list items and the raw conversation spans
* returned by the Sentry Explore AI-conversations endpoints.
* Schemas for conversation list items, the details envelope, and raw
* conversation spans returned by the Sentry Explore AI-conversations endpoints.
*/

import { z } from "zod";

export const ConversationListItemSchema = z.object({
conversationId: z.string(),
/** Stored conversation title when available (added by ai-monitoring list API). */
title: z.string().nullable().optional(),
flow: z.array(z.string()),
errors: z.number(),
llmCalls: z.number(),
Expand Down Expand Up @@ -80,3 +82,22 @@ export const AIConversationSpanSchema = z
.passthrough();

export type AIConversationSpan = z.infer<typeof AIConversationSpanSchema>;

/**
* Conversation details envelope returned by
* `GET /organizations/{org}/ai-conversations/{conversationId}/`.
*
* As of getsentry/sentry#121143 the endpoint always returns this object
* (conversation-level metadata + a page of spans) instead of a bare span
* array. Pagination still uses the `Link` header; each page is an envelope
* whose `spans` field holds that page's rows.
*/
export const AIConversationDetailsSchema = z
.object({
conversationId: z.string(),
title: z.string().nullable(),
spans: z.array(AIConversationSpanSchema),
})
.passthrough();

export type AIConversationDetails = z.infer<typeof AIConversationDetailsSchema>;
2 changes: 2 additions & 0 deletions packages/cli/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export {
} from "./config.js";
// AI Conversations types
export type {
AIConversationDetails,
AIConversationSpan,
ConversationListItem,
} from "./conversation.js";
export {
AIConversationDetailsSchema,
AIConversationSpanSchema,
ConversationListItemSchema,
} from "./conversation.js";
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/test/commands/conversation/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: false,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand All @@ -218,6 +219,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: false,
title: null,
});

const { context } = createMockContext();
Expand Down Expand Up @@ -286,6 +288,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: false,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand All @@ -305,6 +308,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: false,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand All @@ -320,6 +324,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: true,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand All @@ -335,6 +340,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: false,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand All @@ -352,6 +358,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: sampleSpans,
truncated: true,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand All @@ -366,6 +373,7 @@ describe("viewCommand.func", () => {
getConversationSpansSpy.mockResolvedValue({
spans: [],
truncated: false,
title: null,
});

const { context, stdoutWrite } = createMockContext();
Expand Down
Loading
Loading