mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-04 03:25:37 +08:00
* feat: add opencode support
* fix: stabilize opencode session startup
* fix: /models
* fix: improveUI for commands
* fix: format commands.js
* feat: load models through provider adapters
Provider model selection had outgrown a single hardcoded service.
The old service mixed shared caching with provider catalogs and CLI lookup details.
That made stale model lists more likely as providers changed on separate schedules.
Move model discovery behind each provider so lookup lives next to the integration.
The shared service now focuses on provider resolution, caching, persistence, and dedupe.
Return cache metadata and add bypassCache because model availability changes outside the app.
The UI and /models command can show freshness and let users force a provider refresh.
Surface model descriptions while keeping fallback catalogs for unavailable CLIs or SDKs.
* feat(models): resolve active session models through provider adapters
The model inventory command was showing a mix of catalog defaults and
composer-local state instead of the model that is actually active for a
real provider session. That made /models, /cost, and /status
misleading once a session had already started, especially for providers
whose effective runtime model can differ from the optimistic model value
held in the UI.
Introduce an explicit getCurrentActiveModel() contract on
IProviderModels so model resolution lives next to each provider's
catalog logic and uses the provider-native source of truth:
- Claude reads the init event from a resumed stream-json run
- Codex reads model from ~/.codex/config.toml
- Cursor reads lastUsedModel from the chat store.db
- OpenCode reads the persisted session model from opencode.db
- Gemini intentionally returns its default because the CLI does not
provide a reliable active-session lookup
Keep the returned shape intentionally minimal ({ model }). The goal is
to expose only what downstream command consumers need and avoid leaking
provider-specific metadata into a shared transport shape that would
create extra UI coupling and future cleanup cost.
Also make command behavior session-aware: when there is no concrete
session id, do not spawn provider processes or inspect provider session
storage just to answer /models, /cost, or /status. In a new-session
view the correct answer is simply the provider default, and doing more
work there adds latency and unnecessary side effects for no user value.
As part of this, centralize two supporting concerns:
- add a shared helper for building the default current-model result from
a provider catalog so fallbacks stay aligned with DEFAULT
- move leaf-directory validation into shared utils so Cursor session
readers and model lookup code enforce the same path-safety rule
Tests were expanded to cover both the new service delegation path and
the sessionless command behavior, while keeping cache-sensitive tests
isolated from persisted host cache state.
Why this change:
- command output should reflect the model actually driving a session
- new-session views should stay fast and side-effect free
- provider-specific active-model lookup should not be scattered across
routes or UI code
- fallback behavior should be explicit, consistent, and limited to the
provider default when no true active model can be resolved
* feat: support session-scoped model overrides
Model selection was acting like a provider-level preference.
That made resumed sessions drift back to a default or request-time model.
Users expect /models changes made inside a conversation to affect that session.
Store explicit session choices in app-owned ~/.cloudcli state.
This avoids editing provider transcripts or native provider config.
Resolve the effective model before launching each provider runtime.
Claude, Cursor, Codex, Gemini, and OpenCode now honor stored resume choices.
Expose a backend active-model change endpoint for existing sessions.
The models modal can now distinguish default changes from session overrides.
It also shows when a selected model will apply on the next response.
For Claude, stop probing active model state by resuming with a dummy prompt.
Read the indexed JSONL transcript from the end instead.
This preserves provider history while honoring /model stdout or model fields.
Add service tests for adapter delegation and resume-model precedence.
The tests keep cache state, override state, and requested fallback separate.
* feat: make command modal more compact
* fix: preserve opencode session creation events
OpenCode emits the real session id asynchronously on its first JSON output. The runner
registered that id from a helper that could not see the spawned process because
the process reference was scoped inside the model-resolution callback. That
ReferenceError was swallowed by the generic JSON parse fallback, so the client
never received session_created. Without that event, a new OpenCode chat stayed
on / and the assistant stream was not attached to the new session view.
Keep the process reference in the outer spawn scope so registration can update
the active-process map and websocket writer as soon as OpenCode announces the
session id. Split JSON parsing from event processing so malformed non-JSON
output can still stream as raw text, while registration or adapter failures are
surfaced as real errors instead of being hidden as assistant content.
Add a fake opencode executable regression test to lock in the expected lifecycle
ordering: session_created must be sent before live assistant messages, and the
same session id must carry through stream_end and complete.
* fix: clarify model refresh and onboarding providers
OpenCode is now a supported chat provider, but first-run onboarding still only offered
Claude, Cursor, Codex, and Gemini. That made OpenCode harder to discover and
forced users to finish setup before finding the provider in settings or chat.
Adding it to onboarding keeps first-run setup aligned with the providers the
application already supports elsewhere.
The model refresh control was also doing too much visual work. In the new chat
model picker, the previous Hard Refresh label looked like the dialog heading,
which made the primary task unclear. Users open that dialog to choose a model;
refreshing catalogs is only a secondary maintenance action for stale cached
provider model lists.
Rename and reposition the refresh affordance so the model picker reads as a
model picker first. The copy now explains why catalogs are cached, when a refresh
is useful, and that the refresh checks every provider. The /models modal gets the
same clarification so both model-selection surfaces describe the cache behavior
consistently.
* fix: format opencode model catalog labels
OpenCode returns provider-prefixed ids directly from the CLI. Passing those ids through as
labels made the model picker hard to scan: users saw values like
anthropic/claude-3-5-sonnet-20241022 or lowercased, hyphen-split text instead
of readable model names.
Keep the exact OpenCode id as the option value because that is what the CLI
expects, but derive a presentation label for the frontend. The formatter is
intentionally generic rather than a catalog of known providers. It handles common
identifier structure such as provider/model, hyphen-delimited words, v-prefixed
versions, adjacent numeric version tokens, and 8-digit date suffixes.
This keeps OpenCode usable as its model list expands across many upstream
providers without requiring code changes for every new provider or model family.
The description keeps the raw provider-prefixed id visible so users can still
confirm the precise model being selected.
* feat: add more fallback models for cursor
* docs: move model catalog out of shared
The model catalog is no longer a frontend/backend runtime contract.
Keeping it under shared made ownership misleading. It implied the catalog was
application code shared by runtime consumers, even though it now only supports
README links and public API documentation.
Move the catalog into public so it lives beside the docs surfaces that need it.
This gives the API docs a stable, served module and gives README readers a
linkable source without suggesting frontend or backend runtime dependency.
Render the API docs model list from the exported provider registry instead of a
hardcoded Claude/Cursor/Codex subset. That keeps Gemini and OpenCode visible and
makes future provider documentation changes flow through one docs-specific file.
Update README links, provider maintenance notes, and package files so published
artifacts include the standalone docs page and model catalog without relying on
the old shared path.
* fix: simplify empty-state model selector
Keep the provider empty state focused on the setup action users need there:
choosing a model.
The refresh control, cache timestamp, and refresh explanation made the dialog feel
like a cache-management surface.
That extra action is out of place in the empty state, where the goal is to start
a chat with the selected provider and model.
Remove the refresh-specific UI from ProviderSelectionEmptyState and drop the
now-unused refresh/cache props from the ChatMessagesPane pass-through.
Refresh behavior remains available in the dedicated command result flow.
485 lines
23 KiB
TypeScript
485 lines
23 KiB
TypeScript
import { memo, useEffect, useMemo, useRef, useState } from 'react';
|
||
import { useTranslation } from 'react-i18next';
|
||
import SessionProviderLogo from '../../../llm-logo-provider/SessionProviderLogo';
|
||
import type {
|
||
ChatMessage,
|
||
ClaudePermissionSuggestion,
|
||
PermissionGrantResult,
|
||
Provider,
|
||
} from '../../types/types';
|
||
import { formatUsageLimitText } from '../../utils/chatFormatting';
|
||
import { getClaudePermissionSuggestion } from '../../utils/chatPermissions';
|
||
import type { Project } from '../../../../types/app';
|
||
import { ToolRenderer, shouldHideToolResult } from '../../tools';
|
||
import { Reasoning, ReasoningTrigger, ReasoningContent } from '../../../../shared/view/ui';
|
||
import { Markdown } from './Markdown';
|
||
import MessageCopyControl from './MessageCopyControl';
|
||
|
||
type DiffLine = {
|
||
type: string;
|
||
content: string;
|
||
lineNum: number;
|
||
};
|
||
|
||
type MessageComponentProps = {
|
||
message: ChatMessage;
|
||
prevMessage: ChatMessage | null;
|
||
createDiff: (oldStr: string, newStr: string) => DiffLine[];
|
||
onFileOpen?: (filePath: string, diffInfo?: unknown) => void;
|
||
onShowSettings?: () => void;
|
||
onGrantToolPermission?: (suggestion: ClaudePermissionSuggestion) => PermissionGrantResult | null | undefined;
|
||
autoExpandTools?: boolean;
|
||
showRawParameters?: boolean;
|
||
showThinking?: boolean;
|
||
selectedProject?: Project | null;
|
||
provider: Provider | string;
|
||
};
|
||
|
||
type InteractiveOption = {
|
||
number: string;
|
||
text: string;
|
||
isSelected: boolean;
|
||
};
|
||
|
||
type PermissionGrantState = 'idle' | 'granted' | 'error';
|
||
const COPY_HIDDEN_TOOL_NAMES = new Set(['Bash', 'Edit', 'Write', 'ApplyPatch']);
|
||
|
||
const MessageComponent = memo(({ message, prevMessage, createDiff, onFileOpen, onShowSettings, onGrantToolPermission, autoExpandTools, showRawParameters, showThinking, selectedProject, provider }: MessageComponentProps) => {
|
||
const { t } = useTranslation('chat');
|
||
const isGrouped = prevMessage && prevMessage.type === message.type &&
|
||
((prevMessage.type === 'assistant') ||
|
||
(prevMessage.type === 'user') ||
|
||
(prevMessage.type === 'tool') ||
|
||
(prevMessage.type === 'error'));
|
||
const messageRef = useRef<HTMLDivElement | null>(null);
|
||
const [isExpanded, setIsExpanded] = useState(false);
|
||
const permissionSuggestion = getClaudePermissionSuggestion(message, provider);
|
||
const [permissionGrantState, setPermissionGrantState] = useState<PermissionGrantState>('idle');
|
||
const userCopyContent = String(message.content || '');
|
||
const formattedMessageContent = useMemo(
|
||
() => formatUsageLimitText(String(message.content || '')),
|
||
[message.content]
|
||
);
|
||
const assistantCopyContent = message.isToolUse
|
||
? String(message.displayText || message.content || '')
|
||
: formattedMessageContent;
|
||
const isCommandOrFileEditToolResponse = Boolean(
|
||
message.isToolUse && COPY_HIDDEN_TOOL_NAMES.has(String(message.toolName || ''))
|
||
);
|
||
const shouldShowUserCopyControl = message.type === 'user' && userCopyContent.trim().length > 0;
|
||
const shouldShowAssistantCopyControl = message.type === 'assistant' &&
|
||
assistantCopyContent.trim().length > 0 &&
|
||
!isCommandOrFileEditToolResponse &&
|
||
!message.isThinking;
|
||
|
||
|
||
useEffect(() => {
|
||
setPermissionGrantState('idle');
|
||
}, [permissionSuggestion?.entry, message.toolId]);
|
||
|
||
useEffect(() => {
|
||
const node = messageRef.current;
|
||
if (!autoExpandTools || !node || !message.isToolUse) return;
|
||
|
||
const observer = new IntersectionObserver(
|
||
(entries) => {
|
||
entries.forEach((entry) => {
|
||
if (entry.isIntersecting && !isExpanded) {
|
||
setIsExpanded(true);
|
||
const details = node.querySelectorAll<HTMLDetailsElement>('details');
|
||
details.forEach((detail) => {
|
||
detail.open = true;
|
||
});
|
||
}
|
||
});
|
||
},
|
||
{ threshold: 0.1 }
|
||
);
|
||
|
||
observer.observe(node);
|
||
|
||
return () => {
|
||
observer.unobserve(node);
|
||
};
|
||
}, [autoExpandTools, isExpanded, message.isToolUse]);
|
||
|
||
const formattedTime = useMemo(() => new Date(message.timestamp).toLocaleTimeString(), [message.timestamp]);
|
||
const shouldHideThinkingMessage = Boolean(message.isThinking && !showThinking);
|
||
|
||
if (shouldHideThinkingMessage) {
|
||
return null;
|
||
}
|
||
|
||
return (
|
||
<div
|
||
ref={messageRef}
|
||
data-message-timestamp={message.timestamp || undefined}
|
||
className={`chat-message ${message.type} ${isGrouped ? 'grouped' : ''} ${message.type === 'user' ? 'flex justify-end px-3 sm:px-0' : 'px-3 sm:px-0'}`}
|
||
>
|
||
{message.type === 'user' ? (
|
||
/* User message bubble on the right */
|
||
<div className="flex w-full items-end space-x-0 sm:w-auto sm:max-w-[85%] sm:space-x-3 md:max-w-md lg:max-w-lg xl:max-w-xl">
|
||
<div className="group flex-1 rounded-2xl rounded-br-md bg-blue-600 px-3 py-2 text-white shadow-sm sm:flex-initial sm:px-4">
|
||
<div className="whitespace-pre-wrap break-words text-sm">
|
||
{message.content}
|
||
</div>
|
||
{message.images && message.images.length > 0 && (
|
||
<div className="mt-2 grid grid-cols-2 gap-2">
|
||
{message.images.map((img, idx) => (
|
||
<img
|
||
key={img.name || idx}
|
||
src={img.data}
|
||
alt={img.name}
|
||
className="h-auto max-w-full cursor-pointer rounded-lg transition-opacity hover:opacity-90"
|
||
onClick={() => window.open(img.data, '_blank')}
|
||
/>
|
||
))}
|
||
</div>
|
||
)}
|
||
<div className="mt-1 flex items-center justify-end gap-1 text-xs text-blue-100">
|
||
{shouldShowUserCopyControl && (
|
||
<MessageCopyControl content={userCopyContent} messageType="user" />
|
||
)}
|
||
<span>{formattedTime}</span>
|
||
</div>
|
||
</div>
|
||
{!isGrouped && (
|
||
<div className="hidden h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-blue-600 text-sm text-white sm:flex">
|
||
U
|
||
</div>
|
||
)}
|
||
</div>
|
||
) : message.isTaskNotification ? (
|
||
/* Compact task notification on the left */
|
||
<div className="w-full">
|
||
<div className="flex items-center gap-2 py-0.5">
|
||
<span className={`inline-block h-1.5 w-1.5 flex-shrink-0 rounded-full ${message.taskStatus === 'completed' ? 'bg-green-400 dark:bg-green-500' : 'bg-amber-400 dark:bg-amber-500'}`} />
|
||
<span className="text-xs text-gray-500 dark:text-gray-400">{message.content}</span>
|
||
</div>
|
||
</div>
|
||
) : (
|
||
/* Claude/Error/Tool messages on the left */
|
||
<div className="w-full">
|
||
{!isGrouped && (
|
||
<div className="mb-2 flex items-center space-x-3">
|
||
{message.type === 'error' ? (
|
||
<div className="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-red-600 text-sm text-white">
|
||
!
|
||
</div>
|
||
) : message.type === 'tool' ? (
|
||
<div className="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-gray-600 text-sm text-white dark:bg-gray-700">
|
||
🔧
|
||
</div>
|
||
) : (
|
||
<div className="flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full p-1 text-sm text-white">
|
||
<SessionProviderLogo provider={provider} className="h-full w-full" />
|
||
</div>
|
||
)}
|
||
<div className="text-sm font-medium text-gray-900 dark:text-white">
|
||
{message.type === 'error'
|
||
? t('messageTypes.error')
|
||
: message.type === 'tool'
|
||
? t('messageTypes.tool')
|
||
: (provider === 'cursor'
|
||
? t('messageTypes.cursor')
|
||
: provider === 'codex'
|
||
? t('messageTypes.codex')
|
||
: provider === 'gemini'
|
||
? t('messageTypes.gemini')
|
||
: provider === 'opencode'
|
||
? t('messageTypes.opencode', { defaultValue: 'OpenCode' })
|
||
: t('messageTypes.claude'))}
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
<div className="w-full">
|
||
|
||
{message.isToolUse ? (
|
||
<>
|
||
<div className="flex flex-col">
|
||
<div className="flex flex-col">
|
||
<Markdown className="prose prose-sm max-w-none dark:prose-invert">
|
||
{String(message.displayText || '')}
|
||
</Markdown>
|
||
</div>
|
||
</div>
|
||
|
||
{message.toolInput && (
|
||
<ToolRenderer
|
||
toolName={message.toolName || 'UnknownTool'}
|
||
toolInput={message.toolInput}
|
||
toolResult={message.toolResult}
|
||
toolId={message.toolId}
|
||
mode="input"
|
||
onFileOpen={onFileOpen}
|
||
createDiff={createDiff}
|
||
selectedProject={selectedProject}
|
||
autoExpandTools={autoExpandTools}
|
||
showRawParameters={showRawParameters}
|
||
rawToolInput={typeof message.toolInput === 'string' ? message.toolInput : undefined}
|
||
isSubagentContainer={message.isSubagentContainer}
|
||
subagentState={message.subagentState}
|
||
/>
|
||
)}
|
||
|
||
{/* Tool Result Section */}
|
||
{message.toolResult && !shouldHideToolResult(message.toolName || 'UnknownTool', message.toolResult) && (
|
||
message.toolResult.isError ? (
|
||
// Error results - red error box with content
|
||
<div
|
||
id={`tool-result-${message.toolId}`}
|
||
className="relative mt-2 scroll-mt-4 rounded border border-red-200/60 bg-red-50/50 p-3 dark:border-red-800/40 dark:bg-red-950/10"
|
||
>
|
||
<div className="relative mb-2 flex items-center gap-1.5">
|
||
<svg className="h-4 w-4 text-red-500 dark:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||
</svg>
|
||
<span className="text-xs font-medium text-red-700 dark:text-red-300">{t('messageTypes.error')}</span>
|
||
</div>
|
||
<div className="relative text-sm text-red-900 dark:text-red-100">
|
||
<Markdown className="prose prose-sm prose-red max-w-none dark:prose-invert">
|
||
{String(message.toolResult.content || '')}
|
||
</Markdown>
|
||
{permissionSuggestion && (
|
||
<div className="mt-4 border-t border-red-200/60 pt-3 dark:border-red-800/60">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
if (!onGrantToolPermission) return;
|
||
const result = onGrantToolPermission(permissionSuggestion);
|
||
if (result?.success) {
|
||
setPermissionGrantState('granted');
|
||
} else {
|
||
setPermissionGrantState('error');
|
||
}
|
||
}}
|
||
disabled={permissionSuggestion.isAllowed || permissionGrantState === 'granted'}
|
||
className={`inline-flex items-center gap-2 rounded-md border px-3 py-1.5 text-xs font-medium transition-colors ${permissionSuggestion.isAllowed || permissionGrantState === 'granted'
|
||
? 'cursor-default border-green-300/70 bg-green-100 text-green-800 dark:border-green-800/60 dark:bg-green-900/30 dark:text-green-200'
|
||
: 'border-red-300/70 bg-white/80 text-red-700 hover:bg-white dark:border-red-800/60 dark:bg-gray-900/40 dark:text-red-200 dark:hover:bg-gray-900/70'
|
||
}`}
|
||
>
|
||
{permissionSuggestion.isAllowed || permissionGrantState === 'granted'
|
||
? t('permissions.added')
|
||
: t('permissions.grant', { tool: permissionSuggestion.toolName })}
|
||
</button>
|
||
{onShowSettings && (
|
||
<button
|
||
type="button"
|
||
onClick={(e) => { e.stopPropagation(); onShowSettings(); }}
|
||
className="text-xs text-red-700 underline hover:text-red-800 dark:text-red-200 dark:hover:text-red-100"
|
||
>
|
||
{t('permissions.openSettings')}
|
||
</button>
|
||
)}
|
||
</div>
|
||
<div className="mt-2 text-xs text-red-700/90 dark:text-red-200/80">
|
||
{t('permissions.addTo', { entry: permissionSuggestion.entry })}
|
||
</div>
|
||
{permissionGrantState === 'error' && (
|
||
<div className="mt-2 text-xs text-red-700 dark:text-red-200">
|
||
{t('permissions.error')}
|
||
</div>
|
||
)}
|
||
{(permissionSuggestion.isAllowed || permissionGrantState === 'granted') && (
|
||
<div className="mt-2 text-xs text-green-700 dark:text-green-200">
|
||
{t('permissions.retry')}
|
||
</div>
|
||
)}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
) : (
|
||
// Non-error results - route through ToolRenderer (single source of truth)
|
||
<div id={`tool-result-${message.toolId}`} className="scroll-mt-4">
|
||
<ToolRenderer
|
||
toolName={message.toolName || 'UnknownTool'}
|
||
toolInput={message.toolInput}
|
||
toolResult={message.toolResult}
|
||
toolId={message.toolId}
|
||
mode="result"
|
||
onFileOpen={onFileOpen}
|
||
createDiff={createDiff}
|
||
selectedProject={selectedProject}
|
||
autoExpandTools={autoExpandTools}
|
||
/>
|
||
</div>
|
||
)
|
||
)}
|
||
</>
|
||
) : message.isInteractivePrompt ? (
|
||
// Special handling for interactive prompts
|
||
<div className="rounded-lg border border-amber-200 bg-amber-50 p-4 dark:border-amber-800 dark:bg-amber-900/20">
|
||
<div className="flex items-start gap-3">
|
||
<div className="mt-0.5 flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full bg-amber-500">
|
||
<svg className="h-5 w-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||
</svg>
|
||
</div>
|
||
<div className="flex-1">
|
||
<h4 className="mb-3 text-base font-semibold text-amber-900 dark:text-amber-100">
|
||
{t('interactive.title')}
|
||
</h4>
|
||
{(() => {
|
||
const lines = (message.content || '').split('\n').filter((line) => line.trim());
|
||
const questionLine = lines.find((line) => line.includes('?')) || lines[0] || '';
|
||
const options: InteractiveOption[] = [];
|
||
|
||
// Parse the menu options
|
||
lines.forEach((line) => {
|
||
// Match lines like "❯ 1. Yes" or " 2. No"
|
||
const optionMatch = line.match(/[❯\s]*(\d+)\.\s+(.+)/);
|
||
if (optionMatch) {
|
||
const isSelected = line.includes('❯');
|
||
options.push({
|
||
number: optionMatch[1],
|
||
text: optionMatch[2].trim(),
|
||
isSelected
|
||
});
|
||
}
|
||
});
|
||
|
||
return (
|
||
<>
|
||
<p className="mb-4 text-sm text-amber-800 dark:text-amber-200">
|
||
{questionLine}
|
||
</p>
|
||
|
||
{/* Option buttons */}
|
||
<div className="mb-4 space-y-2">
|
||
{options.map((option) => (
|
||
<button
|
||
key={option.number}
|
||
className={`w-full rounded-lg border-2 px-4 py-3 text-left transition-all ${option.isSelected
|
||
? 'border-amber-600 bg-amber-600 text-white shadow-md dark:border-amber-700 dark:bg-amber-700'
|
||
: 'border-amber-300 bg-white text-amber-900 dark:border-amber-700 dark:bg-gray-800 dark:text-amber-100'
|
||
} cursor-not-allowed opacity-75`}
|
||
disabled
|
||
>
|
||
<div className="flex items-center gap-3">
|
||
<span className={`flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-full text-sm font-bold ${option.isSelected
|
||
? 'bg-white/20'
|
||
: 'bg-amber-100 dark:bg-amber-800/50'
|
||
}`}>
|
||
{option.number}
|
||
</span>
|
||
<span className="flex-1 text-sm font-medium sm:text-base">
|
||
{option.text}
|
||
</span>
|
||
{option.isSelected && (
|
||
<span className="text-lg">❯</span>
|
||
)}
|
||
</div>
|
||
</button>
|
||
))}
|
||
</div>
|
||
|
||
<div className="rounded-lg bg-amber-100 p-3 dark:bg-amber-800/30">
|
||
<p className="mb-1 text-sm font-medium text-amber-900 dark:text-amber-100">
|
||
{t('interactive.waiting')}
|
||
</p>
|
||
<p className="text-xs text-amber-800 dark:text-amber-200">
|
||
{t('interactive.instruction')}
|
||
</p>
|
||
</div>
|
||
</>
|
||
);
|
||
})()}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
) : message.isThinking ? (
|
||
/* Thinking messages — Reasoning component (ai-elements pattern) */
|
||
<Reasoning defaultOpen={false}>
|
||
<ReasoningTrigger />
|
||
<ReasoningContent>
|
||
<Markdown className="prose prose-sm prose-gray max-w-none dark:prose-invert">
|
||
{message.content}
|
||
</Markdown>
|
||
<div className="mt-3 flex items-center text-[11px]">
|
||
<MessageCopyControl content={String(message.content || '')} messageType="assistant" />
|
||
</div>
|
||
</ReasoningContent>
|
||
</Reasoning>
|
||
) : (
|
||
<div className="text-sm text-gray-700 dark:text-gray-300">
|
||
{/* Reasoning accordion */}
|
||
{showThinking && message.reasoning && (
|
||
<Reasoning className="mb-3" defaultOpen={false}>
|
||
<ReasoningTrigger />
|
||
<ReasoningContent>
|
||
<div className="whitespace-pre-wrap">
|
||
{message.reasoning}
|
||
</div>
|
||
</ReasoningContent>
|
||
</Reasoning>
|
||
)}
|
||
|
||
{(() => {
|
||
const content = formattedMessageContent;
|
||
|
||
// Detect if content is pure JSON (starts with { or [)
|
||
const trimmedContent = content.trim();
|
||
if ((trimmedContent.startsWith('{') || trimmedContent.startsWith('[')) &&
|
||
(trimmedContent.endsWith('}') || trimmedContent.endsWith(']'))) {
|
||
try {
|
||
const parsed = JSON.parse(trimmedContent);
|
||
const formatted = JSON.stringify(parsed, null, 2);
|
||
|
||
return (
|
||
<div className="my-2">
|
||
<div className="mb-2 flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400">
|
||
<svg className="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z" />
|
||
</svg>
|
||
<span className="font-medium">{t('json.response')}</span>
|
||
</div>
|
||
<div className="overflow-hidden rounded-lg border border-gray-600/30 bg-gray-800 dark:border-gray-700 dark:bg-gray-900">
|
||
<pre className="overflow-x-auto p-4">
|
||
<code className="block whitespace-pre font-mono text-sm text-gray-100 dark:text-gray-200">
|
||
{formatted}
|
||
</code>
|
||
</pre>
|
||
</div>
|
||
</div>
|
||
);
|
||
} catch {
|
||
// Not valid JSON, fall through to normal rendering
|
||
}
|
||
}
|
||
|
||
// Normal rendering for non-JSON content
|
||
return message.type === 'assistant' ? (
|
||
<Markdown className="prose prose-sm prose-gray max-w-none dark:prose-invert">
|
||
{content}
|
||
</Markdown>
|
||
) : (
|
||
<div className="whitespace-pre-wrap">
|
||
{content}
|
||
</div>
|
||
);
|
||
})()}
|
||
</div>
|
||
)}
|
||
|
||
{(shouldShowAssistantCopyControl || !isGrouped) && (
|
||
<div className="mt-1 flex w-full items-center gap-2 text-[11px] text-gray-400 dark:text-gray-500">
|
||
{shouldShowAssistantCopyControl && (
|
||
<MessageCopyControl content={assistantCopyContent} messageType="assistant" />
|
||
)}
|
||
{!isGrouped && <span>{formattedTime}</span>}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
});
|
||
|
||
export default MessageComponent;
|
||
|