2025-04-16 12:56:08 -04:00
|
|
|
|
import type { TerminalHeaderProps } from "./terminal-header.js";
|
|
|
|
|
|
import type { GroupedResponseItem } from "./use-message-grouping.js";
|
|
|
|
|
|
import type { ResponseItem } from "openai/resources/responses/responses.mjs";
|
|
|
|
|
|
|
|
|
|
|
|
import TerminalChatResponseItem from "./terminal-chat-response-item.js";
|
|
|
|
|
|
import TerminalHeader from "./terminal-header.js";
|
|
|
|
|
|
import { Box, Static, Text } from "ink";
|
|
|
|
|
|
import React, { useMemo } from "react";
|
|
|
|
|
|
|
|
|
|
|
|
// A batch entry can either be a standalone response item or a grouped set of
|
|
|
|
|
|
// items (e.g. auto‑approved tool‑call batches) that should be rendered
|
|
|
|
|
|
// together.
|
|
|
|
|
|
type BatchEntry = { item?: ResponseItem; group?: GroupedResponseItem };
|
|
|
|
|
|
type MessageHistoryProps = {
|
|
|
|
|
|
batch: Array<BatchEntry>;
|
|
|
|
|
|
groupCounts: Record<string, number>;
|
|
|
|
|
|
items: Array<ResponseItem>;
|
|
|
|
|
|
userMsgCount: number;
|
|
|
|
|
|
confirmationPrompt: React.ReactNode;
|
|
|
|
|
|
loading: boolean;
|
|
|
|
|
|
thinkingSeconds: number;
|
|
|
|
|
|
headerProps: TerminalHeaderProps;
|
|
|
|
|
|
fullStdout: boolean;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const MessageHistory: React.FC<MessageHistoryProps> = ({
|
|
|
|
|
|
batch,
|
|
|
|
|
|
headerProps,
|
|
|
|
|
|
loading,
|
|
|
|
|
|
thinkingSeconds,
|
|
|
|
|
|
fullStdout,
|
|
|
|
|
|
}) => {
|
2025-04-17 08:12:38 -07:00
|
|
|
|
// Flatten batch entries to response items.
|
|
|
|
|
|
const messages = useMemo(() => batch.map(({ item }) => item!), [batch]);
|
2025-04-16 12:56:08 -04:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<Box flexDirection="column">
|
2025-04-17 08:12:38 -07:00
|
|
|
|
{loading && (
|
2025-04-16 12:56:08 -04:00
|
|
|
|
<Box marginTop={1}>
|
2025-04-17 08:12:38 -07:00
|
|
|
|
<Text color="yellow">{`thinking for ${thinkingSeconds}s`}</Text>
|
2025-04-16 12:56:08 -04:00
|
|
|
|
</Box>
|
|
|
|
|
|
)}
|
|
|
|
|
|
<Static items={["header", ...messages]}>
|
|
|
|
|
|
{(item, index) => {
|
|
|
|
|
|
if (item === "header") {
|
|
|
|
|
|
return <TerminalHeader key="header" {...headerProps} />;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-17 08:12:38 -07:00
|
|
|
|
// After the guard above, item is a ResponseItem
|
2025-04-16 12:56:08 -04:00
|
|
|
|
const message = item as ResponseItem;
|
2025-04-17 08:12:38 -07:00
|
|
|
|
// Suppress empty reasoning updates (i.e. items with an empty summary).
|
|
|
|
|
|
const msg = message as unknown as { summary?: Array<unknown> };
|
|
|
|
|
|
if (msg.summary?.length === 0) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-04-16 12:56:08 -04:00
|
|
|
|
return (
|
|
|
|
|
|
<Box
|
|
|
|
|
|
key={`${message.id}-${index}`}
|
|
|
|
|
|
flexDirection="column"
|
|
|
|
|
|
marginLeft={
|
|
|
|
|
|
message.type === "message" && message.role === "user" ? 0 : 4
|
|
|
|
|
|
}
|
|
|
|
|
|
marginTop={
|
|
|
|
|
|
message.type === "message" && message.role === "user" ? 0 : 1
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
<TerminalChatResponseItem
|
|
|
|
|
|
item={message}
|
|
|
|
|
|
fullStdout={fullStdout}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
);
|
|
|
|
|
|
}}
|
|
|
|
|
|
</Static>
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default React.memo(MessageHistory);
|