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
44 changes: 43 additions & 1 deletion studio-web/components/chat/opencode-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { memo, useEffect, useMemo, useRef, useState } from "react";
import type { AssistantMessage } from "@opencode-ai/sdk";
import { formatTokens, outputLimitInfo } from "@/lib/opencode/output-limit";
import {
MessageLiveContext,
PartView,
Expand Down Expand Up @@ -906,6 +907,46 @@ function assistantError(message: RenderedMessage) {
: undefined;
}

function outputLimit(message: RenderedMessage) {
return outputLimitInfo(message.info as AssistantMessage, message.parts);
}

/**
* Says that a step stopped at the provider's output-token cap (#665).
*
* Not an error box: nothing failed, and `info.error` is unset. Amber because
* the user has to do something (retry, or pick a model with more room), which
* is the difference between this and a silent empty bubble.
*/
function OutputLimitNotice({ message }: { message: RenderedMessage }) {
const limit = outputLimit(message);
if (!limit) return null;
const spent = limit.reasoning + limit.output;
const budget = spent > 0 ? ` (${formatTokens(spent)} tokens)` : "";
return (
<div
role="status"
className="rounded border border-amber-500/40 bg-amber-500/10 px-2 py-1.5 text-xs text-amber-700 dark:text-amber-300"
>
{limit.kind === "empty" ? (
<>
<span className="font-semibold">No reply:</span> the model used its
whole output budget{budget}
{limit.reasoning > 0 && limit.output === 0 ? " thinking" : ""} and was
cut off before answering. Send the message again, or pick another
model.
</>
) : (
<>
<span className="font-semibold">Cut off:</span> this reply reached the
model&apos;s output limit{budget}, so it may be incomplete. Ask it to
continue.
</>
)}
</div>
);
}

/**
* True when two turns hold the same messages and the same parts.
*
Expand Down Expand Up @@ -944,7 +985,7 @@ const TurnBubble = memo(function TurnBubble({
}) {
const isUser = turn.role === "user";
const shown = turn.messages.filter(
(m) => hasRenderableContent(m) || assistantError(m)
(m) => hasRenderableContent(m) || assistantError(m) || outputLimit(m)
);
if (shown.length === 0) return null;

Expand Down Expand Up @@ -1002,6 +1043,7 @@ const TurnBubble = memo(function TurnBubble({
{/* A stop the user asked for is not a failure. OpenCode records it
as MessageAbortedError, so without this a red "Error:" box is
the reward for pressing the Stop button. */}
<OutputLimitNotice message={message} />
{assistantError(message) && isAbortError(assistantError(message)) && (
<div className="text-xs text-fgsubtle">Stopped</div>
)}
Expand Down
97 changes: 97 additions & 0 deletions studio-web/lib/opencode/output-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, it } from "vitest";
import type { Part } from "@opencode-ai/sdk";
import { formatTokens, outputLimitInfo } from "./output-limit";

const done = { completed: 2 };

// Only the fields the rule reads; the cast keeps the fixtures readable.
const text = (t: string) => ({ type: "text", text: t }) as unknown as Part;
const reasoning = (t: string) => ({ type: "reasoning", text: t }) as unknown as Part;
const tool = () => ({ type: "tool", tool: "read" }) as unknown as Part;
const stepFinish = () => ({ type: "step-finish" }) as unknown as Part;

describe("outputLimitInfo", () => {
it("flags the reply that reasoned until the cap and said nothing", () => {
// The shape recorded live on 2026-11-10.
const info = {
role: "assistant",
finish: "length",
time: done,
tokens: { output: 0, reasoning: 8192 },
};
expect(outputLimitInfo(info, [reasoning("thinking…"), stepFinish()])).toEqual({
kind: "empty",
reasoning: 8192,
output: 0,
});
});

it("calls a cut-off reply with text truncated, not empty", () => {
const info = {
role: "assistant",
finish: "length",
time: done,
tokens: { output: 900, reasoning: 7292 },
};
expect(outputLimitInfo(info, [text("Here is the plan")])?.kind).toBe(
"truncated"
);
});

it("does not call a step that made a tool call empty", () => {
const info = { role: "assistant", finish: "length", time: done };
expect(outputLimitInfo(info, [tool()])?.kind).toBe("truncated");
});

it("ignores whitespace-only text", () => {
const info = { role: "assistant", finish: "length", time: done };
expect(outputLimitInfo(info, [text(" \n ")])?.kind).toBe("empty");
});

it("ignores every other finish reason", () => {
for (const finish of ["stop", "tool-calls", undefined]) {
expect(
outputLimitInfo({ role: "assistant", finish, time: done }, [])
).toBeNull();
}
});

it("leaves a step with an error to the error box", () => {
const info = {
role: "assistant",
finish: "length",
time: done,
error: { name: "APIError" },
};
expect(outputLimitInfo(info, [])).toBeNull();
});

it("waits until the step has finished", () => {
const info = { role: "assistant", finish: "length", time: {} };
expect(outputLimitInfo(info, [])).toBeNull();
});

it("never flags a user message", () => {
expect(
outputLimitInfo({ role: "user", finish: "length", time: done }, [])
).toBeNull();
});

it("treats missing token counts as zero", () => {
const info = { role: "assistant", finish: "length", time: done };
expect(outputLimitInfo(info, [])).toEqual({
kind: "empty",
reasoning: 0,
output: 0,
});
});
});

describe("formatTokens", () => {
it("abbreviates thousands", () => {
expect(formatTokens(8192)).toBe("8.2K");
expect(formatTokens(32000)).toBe("32K");
expect(formatTokens(128000)).toBe("128K");
expect(formatTokens(512)).toBe("512");
});
});
74 changes: 74 additions & 0 deletions studio-web/lib/opencode/output-limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Did this reply run out of output budget? (#665)
*
* A provider stops a reply at its output-token cap and OpenCode records that
* as `finish: "length"`. That is not an error: `info.error` stays unset, so
* the panel's error box never fires. When the cap is reached while the model
* is still reasoning, the reply has no text and no tool call, and the turn
* rendered as nothing at all — the chat looked like it had done nothing.
*
* Seen live (2026-11-10): the gateway's default model routes mostly to a
* backend capped at 8192 output tokens, reasoning included, and two first
* turns spent all of it thinking (`output: 0, reasoning: 8192`).
*
* Kept out of the component so the rule can be tested without a DOM.
*/

import type { Part } from "@opencode-ai/sdk";

export interface OutputLimitInfo {
/**
* "empty": stopped before saying or doing anything — the case that used to
* render as nothing. "truncated": stopped after some text, so what is shown
* is only the start of the reply.
*/
kind: "empty" | "truncated";
/** Tokens spent reasoning in this step, when the provider reported them. */
reasoning: number;
/** Tokens of visible output in this step. */
output: number;
}

interface MessageLike {
role: string;
finish?: string;
error?: unknown;
time: { completed?: number };
tokens?: { output?: number; reasoning?: number };
}

/**
* `null` unless `info` is a finished assistant step that the provider cut off
* at its output limit.
*
* A step with a tool call is not "empty" even with no text: the tool is what
* it did, and the next step carries on from it. A step with an error is left
* to the error box, which already says what happened.
*/
export function outputLimitInfo(
info: MessageLike,
parts: readonly Part[]
): OutputLimitInfo | null {
if (info.role !== "assistant") return null;
if (info.finish !== "length") return null;
if (info.error) return null;
if (info.time.completed === undefined) return null;

const said = parts.some(
(part) => part.type === "text" && part.text.trim().length > 0
);
const acted = parts.some((part) => part.type === "tool");

return {
kind: said || acted ? "truncated" : "empty",
reasoning: info.tokens?.reasoning ?? 0,
output: info.tokens?.output ?? 0,
};
}

/** "8192" → "8.2K"; small numbers as they are. */
export function formatTokens(n: number): string {
if (n < 1000) return String(n);
const k = n / 1000;
return `${k >= 100 ? Math.round(k) : k.toFixed(1).replace(/\.0$/, "")}K`;
}
Loading