Skip to content
Open
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
240 changes: 240 additions & 0 deletions console/src/components/call-renderers/openai-chat.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import { describe, expect, it } from "bun:test"
import { createElement } from "react"
import { renderToStaticMarkup } from "react-dom/server"
import { FinishBadge } from "../ui/finish-badge"
import { SummaryCards } from "../llm-call-detail/summary-cards"
import { OpenAiChatCallView, OpenAiChatOutputBlocks } from "./openai-chat"
import { parseOpenAiChatCall } from "../../lib/wire-apis/openai-chat"
import type { LlmCallDetail } from "../../types/api"

function detail(overrides: Partial<LlmCallDetail> = {}): LlmCallDetail {
return {
id: "sample-call",
source_id: "sample-source",
request_time: 0,
response_time: 1,
complete_time: 2,
wire_api: "openai-chat",
model: "sample-model",
api_type: "chat",
is_stream: true,
request_path: "/v1/chat/completions",
status_code: 200,
finish_reason: null,
input_tokens: null,
output_tokens: null,
total_tokens: null,
ttft_ms: 1,
e2e_latency_ms: 2,
response_id: null,
client_ip: "",
client_port: 0,
server_ip: "",
server_port: 0,
request_body: null,
response_body: null,
request_headers: null,
response_headers: null,
is_agent_request: false,
tool_surface: null,
agent_topology: null,
tool_call_count: 0,
tool_names: [],
process: null,
...overrides,
}
}

function renderChat(responseBody: string | null): string {
return renderToStaticMarkup(createElement(OpenAiChatCallView, {
requestBody: null,
responseBody,
hasRequestBody: false,
}))
}

function body(message: unknown, finishReason: string | null = null): string {
return JSON.stringify({ choices: [{ index: 0, message, finish_reason: finishReason }] })
}

describe("finish evidence display", () => {
it("visibly labels streaming missing finish without guessing an error", () => {
const markup = renderToStaticMarkup(createElement(FinishBadge, { reason: null, isStream: true, statusCode: 200 }))
expect(markup).toContain("Incomplete")
expect(markup).not.toContain("Stream error")
expect(markup).not.toContain("bg-red-")
})

it("preserves raw provider finish values", () => {
const markup = renderToStaticMarkup(createElement(FinishBadge, { reason: "provider_terminal", isStream: true, statusCode: 200 }))
expect(markup).toContain("provider_terminal")
expect(markup).not.toContain("Incomplete")
})

for (const props of [
{ reason: null },
{ reason: null, isStream: false, statusCode: 200 },
{ reason: null, isStream: true, statusCode: 429 },
]) {
it(`keeps missing finish neutral outside an unfinished HTTP-success stream ${JSON.stringify(props)}`, () => {
const markup = renderToStaticMarkup(createElement(FinishBadge, props))
expect(markup).toContain("—")
expect(markup).not.toContain("Incomplete")
expect(markup).not.toContain("Stream error")
})
}

it("marks HTTP 200 without finish as incomplete in the detail summary", () => {
const markup = renderToStaticMarkup(createElement(SummaryCards, { detail: detail() }))
expect(markup).toContain("200")
expect(markup).toContain("Incomplete")
expect(markup).not.toContain("Stream error")
})

it("shows the full parsed stream error alongside preserved HTTP status", () => {
const response_body = JSON.stringify({ error: {
message: "Synthetic stream failure",
type: "sample_error",
code: "sample_overload",
extra: { detail: "preserved nested value" },
} })
const markup = renderToStaticMarkup(createElement(SummaryCards, { detail: detail({ response_body }) }))
expect(markup).toContain("200")
expect(markup).toContain("Stream error")
expect(markup).toContain('role="alert"')
expect(markup).toContain("Synthetic stream failure")
expect(markup).toContain("sample_error")
expect(markup).toContain("sample_overload")
expect(markup).toContain("preserved nested value")
})

it("uses shared stream error display for Responses details", () => {
const markup = renderToStaticMarkup(createElement(SummaryCards, { detail: detail({
wire_api: "openai-responses",
response_body: '{"error":{"message":"Synthetic response failure","code":"sample_error"}}',
}) }))
expect(markup).toContain("Stream error")
expect(markup).toContain("Synthetic response failure")
})

it("keeps raw stop visible even when a stream error is also captured", () => {
const markup = renderToStaticMarkup(createElement(SummaryCards, { detail: detail({
finish_reason: "stop",
response_body: '{"error":{"message":"Synthetic late failure"}}',
}) }))
expect(markup).toContain("stop")
expect(markup).toContain("Stream error")
expect(markup).toContain("Synthetic late failure")
})

for (const overrides of [
{ is_stream: false },
{ status_code: 400 },
]) {
it(`does not relabel existing HTTP/non-streaming errors ${JSON.stringify(overrides)}`, () => {
const markup = renderToStaticMarkup(createElement(SummaryCards, { detail: detail({
response_body: '{"error":{"message":"Synthetic request failure"}}',
...overrides,
}) }))
expect(markup).not.toContain("Stream error")
expect(markup).not.toContain("Incomplete")
})
}
})

describe("Chat effective output display", () => {
for (const responseBody of [null, "", '{"choices":', "null", "[]"]) {
it(`does not infer no valid output from an unavailable or unparsed full-view body ${String(responseBody)}`, () => {
const markup = renderChat(responseBody)
expect(markup).not.toContain("No valid output")
expect(markup).toContain("Input")
expect(markup).toContain("Output")
})
}

for (const responseBody of [null, '{"choices":', '{"choices":[],"usage":{"completion_tokens":3}}']) {
it(`keeps output-only zero choices neutral without raw body evidence ${String(responseBody)}`, () => {
const response = parseOpenAiChatCall(null, responseBody).response
const markup = renderToStaticMarkup(createElement(OpenAiChatOutputBlocks, { response }))
expect(markup).not.toContain("No valid output")
expect(markup).toContain("No response content.")
})
}

for (const message of [
{ role: "assistant" },
{ role: "assistant", content: "" },
{ role: "assistant", content: null },
{ role: "assistant", content: [] },
]) {
it(`shows no valid output for a synthetic shell ${JSON.stringify(message)}`, () => {
const markup = renderChat(body(message))
expect(markup).toContain("No valid output")
})
}

it("shows no valid output for a usage-only response", () => {
expect(renderChat('{"choices":[],"usage":{"completion_tokens":3}}')).toContain("No valid output")
})

it("does not mistake normal stop plus empty content for a stream error", () => {
const response_body = body({ role: "assistant", content: "" }, "stop")
const markup = renderToStaticMarkup(createElement("div", null,
createElement(SummaryCards, { detail: detail({ finish_reason: "stop", response_body }) }),
createElement(OpenAiChatCallView, { requestBody: null, responseBody: response_body, hasRequestBody: false }),
))
expect(markup).toContain("No valid output")
expect(markup).toContain("stop")
expect(markup).not.toContain("Stream error")
expect(markup).not.toContain("Incomplete")
})

it("retains partial text and tools when a stream error is present", () => {
const response_body = JSON.stringify({
choices: [{ index: 0, message: {
role: "assistant",
content: "Synthetic partial answer",
tool_calls: [{ id: "sample-tool-call", type: "function", function: { name: "sample_tool", arguments: '{"partial":' } }],
}, finish_reason: null }],
error: { message: "Synthetic stream failure", extra: { detail: "complete error detail" } },
})
const markup = renderToStaticMarkup(createElement("div", null,
createElement(SummaryCards, { detail: detail({ response_body }) }),
createElement(OpenAiChatCallView, { requestBody: null, responseBody: response_body, hasRequestBody: false }),
))
expect(markup).toContain("Stream error")
expect(markup).toContain("complete error detail")
expect(markup).toContain("Synthetic partial answer")
expect(markup).toContain("sample_tool")
expect(markup).toContain("partial")
expect(markup).not.toContain("No valid output")
})

for (const message of [
{ role: "assistant", content: "Synthetic complete answer" },
{ role: "assistant", reasoning_content: "Synthetic reasoning" },
{ role: "assistant", tool_calls: [{ id: "sample-tool-call", type: "function", function: { name: "sample_tool", arguments: "{}" } }] },
]) {
it(`keeps actual output free of empty-output warnings ${JSON.stringify(message)}`, () => {
const markup = renderChat(body(message, "stop"))
expect(markup).not.toContain("No valid output")
expect(markup).not.toContain("Stream error")
})
}

it("renders reasoning alias output instead of calling it empty", () => {
const response_body = body({ role: "assistant", reasoning: "Synthetic reasoning alias" }, "stop")
const markup = renderChat(response_body)
expect(markup).toContain("Synthetic reasoning alias")
expect(markup).not.toContain("No valid output")
const response = parseOpenAiChatCall(null, response_body).response
const outputMarkup = renderToStaticMarkup(createElement(OpenAiChatOutputBlocks, { response }))
expect(outputMarkup).toContain("Synthetic reasoning alias")
expect(outputMarkup).not.toContain("No valid output")
})

it("uses the same empty-output check in output-only cards", () => {
const response = parseOpenAiChatCall(null, body({ role: "assistant", content: "" }, "stop")).response
expect(renderToStaticMarkup(createElement(OpenAiChatOutputBlocks, { response }))).toContain("No valid output")
})
})
28 changes: 24 additions & 4 deletions console/src/components/call-renderers/openai-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChevronRight, ChevronDown, FileJson, Activity } from "lucide-react"
import { cn } from "@/lib/utils"
import { Markdown } from "@/components/ui/markdown"
import { parseOpenAiChatCall } from "@/lib/wire-apis/openai-chat"
import { deriveCallOutcome, hasOpenAiChatOutput } from "@/lib/call-outcome"
import type {
OpenAiChatChoice,
OpenAiChatMessage,
Expand Down Expand Up @@ -519,12 +520,19 @@ function LogprobsPanel({ choice }: { choice: OpenAiChatChoice }) {
)
}

function EmptyOutputNotice() {
return (
<div className="rounded border border-amber-200 bg-amber-50/60 px-3 py-2 text-[11px] text-amber-800 dark:border-amber-900/40 dark:bg-amber-900/10 dark:text-amber-300">
No valid output.
</div>
)
}

function ResponseCard({ response, ctx }: { response: OpenAiChatResponse; ctx?: OutputCtx }) {
if (response.choices.length === 0) {
return <div className="text-[11px] text-muted-foreground italic">No choices in response.</div>
}
if (response.choices.length === 0) return <EmptyOutputNotice />
return (
<div className="space-y-2">
{!hasOpenAiChatOutput(response) && <EmptyOutputNotice />}
{response.choices.map((c, i) => (
<ChoiceCard key={i} choice={c} ctx={ctx} />
))}
Expand Down Expand Up @@ -622,6 +630,13 @@ export function OpenAiChatCallView({
hasRequestBody,
}: OpenAiChatCallViewProps) {
const call = useMemo(() => parseOpenAiChatCall(requestBody, responseBody), [requestBody, responseBody])
const outputState = useMemo(() => deriveCallOutcome({
is_stream: false,
status_code: null,
finish_reason: null,
wire_api: "openai-chat",
response_body: responseBody,
}).outputState, [responseBody])
return (
<>
<section className="border-l-2 border-muted-foreground/30 pl-3">
Expand All @@ -643,7 +658,11 @@ export function OpenAiChatCallView({
<div className="mb-2 text-[10px] font-semibold uppercase tracking-wider text-emerald-700 dark:text-emerald-400">
Output
</div>
<ResponseCard response={call.response} />
{outputState === "unknown" ? (
<div className="text-[11px] text-muted-foreground italic">No response content.</div>
) : (
<ResponseCard response={call.response} />
)}
</section>
</>
)
Expand All @@ -655,6 +674,7 @@ export function OpenAiChatOutputBlocks({ response, ctx }: { response: OpenAiChat
}
return (
<div className="space-y-2">
{!hasOpenAiChatOutput(response) && <EmptyOutputNotice />}
{response.choices.map((c, i) => <ChoiceCard key={i} choice={c} ctx={ctx} />)}
</div>
)
Expand Down
12 changes: 11 additions & 1 deletion console/src/components/llm-call-detail/summary-cards.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StatusBadge } from "@/components/ui/status-badge"
import { FinishBadge } from "@/components/ui/finish-badge"
import { formatMs, formatNumber } from "@/lib/format"
import { deriveCallOutcome } from "@/lib/call-outcome"
import type { LlmCallDetail } from "@/types/api"

function SummaryCard({ label, children }: { label: string; children: React.ReactNode }) {
Expand All @@ -17,6 +18,7 @@ interface Props {
}

export function SummaryCards({ detail }: Props) {
const outcome = deriveCallOutcome(detail)
return (
<div className="grid grid-cols-4 gap-3">
<SummaryCard label="Wire API / Model">
Expand All @@ -28,7 +30,7 @@ export function SummaryCards({ detail }: Props) {
<SummaryCard label="Status / Finish">
<div className="flex items-center gap-2">
<StatusBadge status={detail.status_code} />
<FinishBadge reason={detail.finish_reason} />
<FinishBadge reason={detail.finish_reason} isStream={detail.is_stream} statusCode={detail.status_code} />
</div>
</SummaryCard>
<SummaryCard label={detail.is_stream ? "TTFT / E2E" : "TTFB / E2E"}>
Expand Down Expand Up @@ -75,6 +77,14 @@ export function SummaryCards({ detail }: Props) {
{formatNumber(detail.total_tokens)}
</div>
</SummaryCard>
{outcome.streamState === "stream-error" && (
<div role="alert" className="col-span-4 rounded-lg border border-red-300 bg-red-50 px-3 py-2 text-xs text-red-700 dark:border-red-900/40 dark:bg-red-900/20 dark:text-red-300">
<div className="font-medium">Stream error</div>
<pre className="mt-1 max-h-[240px] overflow-auto whitespace-pre-wrap break-words font-mono text-[11px]">
{JSON.stringify(outcome.error, null, 2)}
</pre>
</div>
)}
</div>
)
}
18 changes: 14 additions & 4 deletions console/src/components/ui/finish-badge.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { cn } from "@/lib/utils"
import { finishTone, TONE_CLASS } from "@/lib/finish-tone"
import { deriveCallOutcome } from "@/lib/call-outcome"

export function FinishBadge({ reason }: { reason: string | null }) {
if (!reason) return <span className="text-muted-foreground">—</span>
const tone = finishTone(reason)
interface Props {
reason: string | null
isStream?: boolean
statusCode?: number | null
}

export function FinishBadge({ reason, isStream = false, statusCode = null }: Props) {
const outcome = deriveCallOutcome({ is_stream: isStream, status_code: statusCode, finish_reason: reason })
if (!reason && outcome.streamState !== "incomplete") return <span className="text-muted-foreground">—</span>
const incomplete = outcome.streamState === "incomplete"
const tone = incomplete ? "warn" : finishTone(reason)
return (
<span
title={incomplete ? "Stream has no finish reason; the cause is unknown." : undefined}
className={cn(
"inline-flex items-center rounded px-1.5 py-0.5 text-xs font-medium",
TONE_CLASS[tone],
)}
>
{reason}
{incomplete ? "Incomplete" : reason}
</span>
)
}
Loading