Skip to content

Commit d27dca3

Browse files
committed
fix(chat): render step titles and compaction detail from real server fields
The opencode server's StepStartPart/StepFinishPart and CompactionPart carry no `text`/`state` fields, so the client was reading non-existent fields and every step rendered as "Untitled step" while compaction banners were blank. - StepBlock now titles steps positionally ("Step N") and shows finish outcome (reason · tokens · cost) from the fields the server actually sends; the expanded view is built from titles so it always has content. - CompactionBanner is now a status chip with Auto/Manual/Overflow badges and an expand gate that only appears when the server attached detail. - Added optional reason/cost/tokens/auto/overflow/tail_start_id to the Part interface, matching the server schema. - Extracted pairStepParts into src/lib/step-pairs.ts so it can be unit-tested without react-native; added regression tests for textless server parts. Tests: 398 pass, 0 fail. Typecheck and lint clean.
1 parent 3948e8a commit d27dca3

7 files changed

Lines changed: 242 additions & 77 deletions

File tree

‎src/components/chat/CompactionBanner.tsx‎

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,24 @@ export function CompactionBanner({
2626
const density = useDensity();
2727
const [expanded, setExpanded] = useState(false);
2828

29+
// The server's CompactionPart carries no `text`/`state` — just flags.
30+
// The actual compaction summary arrives as a separate text part in the
31+
// same message (rendered by MessageBubble), so this banner is a status
32+
// chip: label + badges. part.text is kept as a legacy fallback.
2933
const summary =
3034
part.text || t("chat.compactionBanner.default", "Conversation compacted");
35+
36+
// Expand is only meaningful when an older/newer server actually attached
37+
// detail — otherwise the banner is honestly non-expandable instead of
38+
// opening onto a blank "No additional details" view.
39+
const hasDetail =
40+
(typeof part.state?.input === "string" && part.state.input.length > 0) ||
41+
(typeof part.state?.output === "string" && part.state.output.length > 0) ||
42+
typeof part.state?.error?.message === "string";
43+
44+
const toggle = useCallback(() => {
45+
if (hasDetail) setExpanded((v) => !v);
46+
}, [hasDetail]);
3147
const elapsed =
3248
part.time?.start && part.time?.end
3349
? (() => {
@@ -37,10 +53,6 @@ export function CompactionBanner({
3753
})()
3854
: null;
3955

40-
const toggle = useCallback(() => {
41-
setExpanded((v) => !v);
42-
}, []);
43-
4456
return (
4557
<TouchableOpacity
4658
style={[
@@ -49,7 +61,7 @@ export function CompactionBanner({
4961
{ ...ds({ padding: 10, marginBottom: 8 }, density) },
5062
]}
5163
onPress={toggle}
52-
activeOpacity={0.7}
64+
activeOpacity={hasDetail ? 0.7 : 1}
5365
>
5466
<View style={[s.header, { ...ds({ gap: 6, marginBottom: 4 }, density) }]}>
5567
<Ionicons name="archive-outline" size={14} color="#6366f1" />
@@ -62,6 +74,30 @@ export function CompactionBanner({
6274
>
6375
{t("chat.compactionBanner.label", "Compacted")}
6476
</Text>
77+
{part.auto !== undefined && (
78+
<Text
79+
style={[
80+
s.badge,
81+
isDark && s.badgeDark,
82+
{ ...ds({ fontSize: 10 }, density) },
83+
]}
84+
>
85+
{part.auto
86+
? t("chat.compactionBanner.auto", "Auto")
87+
: t("chat.compactionBanner.manual", "Manual")}
88+
</Text>
89+
)}
90+
{part.overflow === true && (
91+
<Text
92+
style={[
93+
s.badge,
94+
isDark && s.badgeDark,
95+
{ ...ds({ fontSize: 10 }, density) },
96+
]}
97+
>
98+
{t("chat.compactionBanner.overflow", "Overflow")}
99+
</Text>
100+
)}
65101
{elapsed && (
66102
<Text
67103
style={[
@@ -73,11 +109,13 @@ export function CompactionBanner({
73109
{elapsed}
74110
</Text>
75111
)}
76-
<Ionicons
77-
name={expanded ? "chevron-up" : "chevron-down"}
78-
size={14}
79-
color={isDark ? "#666666" : "#999999"}
80-
/>
112+
{hasDetail && (
113+
<Ionicons
114+
name={expanded ? "chevron-up" : "chevron-down"}
115+
size={14}
116+
color={isDark ? "#666666" : "#999999"}
117+
/>
118+
)}
81119
</View>
82120
<Text
83121
style={[
@@ -90,7 +128,7 @@ export function CompactionBanner({
90128
>
91129
{summary}
92130
</Text>
93-
{expanded && (
131+
{expanded && hasDetail && (
94132
<ScrollView
95133
{...WIDE_CONTENT_SCROLL_CONFIG}
96134
nestedScrollEnabled
@@ -186,22 +224,6 @@ export function CompactionBanner({
186224
</Text>
187225
</View>
188226
)}
189-
{!part.state?.input &&
190-
!part.state?.output &&
191-
!part.state?.error?.message && (
192-
<Text
193-
style={[
194-
s.emptyHint,
195-
isDark && s.emptyHintDark,
196-
{ ...ds({ fontSize: 11 }, density) },
197-
]}
198-
>
199-
{t(
200-
"chat.compactionBanner.noDetails",
201-
"No additional details",
202-
)}
203-
</Text>
204-
)}
205227
</View>
206228
</ScrollView>
207229
)}
@@ -236,6 +258,17 @@ const s = StyleSheet.create({
236258
textTransform: "uppercase",
237259
},
238260
labelDark: { color: "#a78bfa" },
261+
badge: {
262+
fontSize: 10,
263+
fontWeight: "600",
264+
color: "#6366f1",
265+
backgroundColor: "#ede9fe",
266+
borderRadius: 8,
267+
paddingHorizontal: 6,
268+
paddingVertical: 1,
269+
overflow: "hidden",
270+
},
271+
badgeDark: { color: "#a78bfa", backgroundColor: "#2a2440" },
239272
elapsed: {
240273
fontSize: 10,
241274
color: "#999999",
@@ -286,7 +319,4 @@ const s = StyleSheet.create({
286319
errorBannerDark: { backgroundColor: "#1a0a0a" },
287320
errorText: { fontSize: 12, color: "#dc2626", flex: 1, lineHeight: 18 },
288321
errorTextDark: { color: "#ff6b6b" },
289-
290-
emptyHint: { fontSize: 11, color: "#999999", fontStyle: "italic" },
291-
emptyHintDark: { color: "#666666" },
292322
});

‎src/components/chat/StepBlock.tsx‎

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,52 @@ import {
88
} from "react-native";
99
import { Ionicons } from "@expo/vector-icons";
1010
import { useTranslation } from "react-i18next";
11+
import type { TFunction } from "i18next";
1112
import { WIDE_CONTENT_SCROLL_CONFIG } from "../../lib/scroll-config";
1213
import { useDensity, ds } from "../../lib/density";
1314
import type { Part } from "../../lib/sdk";
15+
import { pairStepParts, type StepPair } from "../../lib/step-pairs";
1416

1517
const STEP_ICONS: Record<string, string> = {
1618
"step-start": "play-outline",
1719
"step-finish": "checkmark-outline",
1820
};
1921

20-
export interface StepPair {
21-
start: Part;
22-
finish: Part | null;
23-
index: number;
24-
}
22+
// Re-exported so existing importers (MessageBubble) keep working; the
23+
// implementation lives in lib/ so plain `node --test` can cover it without
24+
// pulling in react-native.
25+
export { pairStepParts, type StepPair };
2526

26-
export function pairStepParts(parts: Part[]): StepPair[] {
27-
const pairs: StepPair[] = [];
28-
let currentStart: Part | null = null;
29-
let index = 0;
27+
// Display title for one step. The server's StepStartPart carries no `text`
28+
// (only an optional snapshot ref), so titles are positional — "Step N" —
29+
// with any legacy text a server does send kept as-is for compatibility.
30+
function stepTitle(pair: StepPair, t: TFunction): string {
31+
return (
32+
pair.start.text ||
33+
pair.finish?.text ||
34+
t("chat.stepBlock.stepN", "Step {{n}}", { n: pair.index + 1 })
35+
);
36+
}
3037

31-
for (const part of parts) {
32-
if (part.type === "step-start") {
33-
if (currentStart) {
34-
// Orphaned start (previous one had no matching finish) — store it
35-
pairs.push({ start: currentStart, finish: null, index: index++ });
36-
}
37-
currentStart = part;
38-
} else if (part.type === "step-finish" && currentStart) {
39-
pairs.push({ start: currentStart, finish: part, index: index++ });
40-
currentStart = null;
41-
}
38+
// One-line outcome for a finished step, built from the fields
39+
// StepFinishPart actually carries (reason/cost/tokens — never `text`).
40+
// Returns null when the finish carries nothing displayable.
41+
function finishMetaLine(finish: Part | null): string | null {
42+
if (!finish) return null;
43+
const bits: string[] = [];
44+
if (finish.reason) bits.push(finish.reason);
45+
const tokens = finish.tokens;
46+
if (
47+
tokens &&
48+
typeof tokens.input === "number" &&
49+
typeof tokens.output === "number"
50+
) {
51+
bits.push(`${(tokens.input + tokens.output).toLocaleString()} tokens`);
4252
}
43-
44-
// Any trailing unpaired start
45-
if (currentStart) {
46-
pairs.push({ start: currentStart, finish: null, index: index });
53+
if (typeof finish.cost === "number" && finish.cost > 0) {
54+
bits.push(`$${finish.cost.toFixed(4)}`);
4755
}
48-
49-
return pairs;
56+
return bits.length > 0 ? bits.join(" · ") : null;
5057
}
5158

5259
interface Props {
@@ -61,32 +68,27 @@ export function StepBlock({ parts, isDark }: Props) {
6168

6269
const stepCount = parts.length;
6370

64-
// When expanded shows all steps; when collapsed shows only the first step title
71+
// Collapsed: single step shows its title; multiple steps show the count.
72+
// Expanded: every step shows its title plus any finish outcome.
6573
const summaryText = useMemo(() => {
6674
if (stepCount === 0) return "";
67-
if (stepCount === 1) {
68-
return (
69-
parts[0]?.start.text ||
70-
t("chat.stepBlock.untitledStep", "Untitled step")
71-
);
75+
if (stepCount === 1 && parts[0]) {
76+
return stepTitle(parts[0], t);
7277
}
7378
return t("chat.stepBlock.summary", "{{count}} steps", { count: stepCount });
7479
}, [parts, t, stepCount]);
7580

76-
// Combine all step text for the expanded view
81+
// Titles always exist (positional "Step N" fallback), so the expanded view
82+
// is never blank when steps exist — it doubles as the has-content gate.
7783
const expandedText = useMemo(() => {
7884
return parts
7985
.map((pair) => {
80-
const startText = pair.start.text || "";
81-
const finishText = pair.finish?.text || "";
82-
if (startText && finishText) {
83-
return `→ ${startText}\n ${finishText}`;
84-
}
85-
return `→ ${startText}`;
86+
const title = stepTitle(pair, t);
87+
const meta = finishMetaLine(pair.finish);
88+
return meta ? `→ ${title}\n ${meta}` : `→ ${title}`;
8689
})
87-
.filter((s) => s.trim())
8890
.join("\n\n");
89-
}, [parts]);
91+
}, [parts, t]);
9092

9193
if (stepCount === 0) return null;
9294

@@ -184,11 +186,10 @@ export function StepBlock({ parts, isDark }: Props) {
184186
]}
185187
selectable
186188
>
187-
{pair.start.text ||
188-
t("chat.stepBlock.untitledStep", "Untitled step")}
189+
{stepTitle(pair, t)}
189190
</Text>
190191
</View>
191-
{pair.finish && pair.finish.text && (
192+
{pair.finish && finishMetaLine(pair.finish) && (
192193
<Text
193194
style={[
194195
s.stepResult,
@@ -207,7 +208,7 @@ export function StepBlock({ parts, isDark }: Props) {
207208
]}
208209
selectable
209210
>
210-
{pair.finish.text}
211+
{finishMetaLine(pair.finish)}
211212
</Text>
212213
)}
213214
</View>

‎src/lib/i18n/en.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@
634634
"stepBlock": {
635635
"title": "Steps",
636636
"summary": "{{count}} steps",
637-
"untitledStep": "Untitled step"
637+
"stepN": "Step {{n}}"
638638
},
639639
"patchPartCard": {
640640
"defaultTitle": "File Patch",
@@ -650,6 +650,9 @@
650650
"compactionBanner": {
651651
"default": "Conversation compacted",
652652
"label": "Compacted",
653+
"auto": "Auto",
654+
"manual": "Manual",
655+
"overflow": "Overflow",
653656
"inputLabel": "Prompt sent",
654657
"outputLabel": "Response"
655658
},

‎src/lib/i18n/zh-Hans.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@
634634
"stepBlock": {
635635
"title": "步骤",
636636
"summary": "{{count}} 个步骤",
637-
"untitledStep": "未命名步骤"
637+
"stepN": "第 {{n}} 步"
638638
},
639639
"patchPartCard": {
640640
"defaultTitle": "文件补丁",
@@ -650,6 +650,9 @@
650650
"compactionBanner": {
651651
"default": "对话已压缩",
652652
"label": "已压缩",
653+
"auto": "自动",
654+
"manual": "手动",
655+
"overflow": "溢出",
653656
"inputLabel": "发送的提示词",
654657
"outputLabel": "回复"
655658
},

‎src/lib/sdk.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ export interface Part {
117117
// Tool part
118118
tool?: string;
119119
callID?: string;
120+
// Step-finish part (matches the server's StepFinishPart schema — step
121+
// parts carry no `text`; the outcome lives in these fields)
122+
reason?: string;
123+
cost?: number;
124+
tokens?: {
125+
total?: number;
126+
input: number;
127+
output: number;
128+
reasoning?: number;
129+
cache?: { read: number; write: number };
130+
};
131+
// Compaction part (matches the server's CompactionPart schema — also no
132+
// `text`/`state`; the summary arrives as a separate text part)
133+
auto?: boolean;
134+
overflow?: boolean;
135+
tail_start_id?: string;
120136
state?: {
121137
status: "pending" | "running" | "completed" | "error";
122138
input?: unknown;

0 commit comments

Comments
 (0)