File size: 6,546 Bytes
3201ca6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /**
* Inline markdown artifact preview for chat tool cards.
*
* Shows a height-limited scrollable rich preview so long reports stay compact
* in the stream; the footer View action deep-links into the Files drawer for
* the full document. Markdown *create* file-editor events stay expanded and
* ungrouped so the preview is visible without an extra click.
*/
import { useTranslation } from "react-i18next";
import { ArrowUpRight } from "lucide-react";
import FileIcon from "#/icons/file.svg?react";
import { I18nKey } from "#/i18n/declaration";
import { MarkdownRenderer } from "#/components/features/markdown/markdown-renderer";
import { planComponents } from "#/components/features/markdown/plan-components";
import { Typography } from "#/ui/typography";
import { isMarkdownFilePath } from "#/utils/is-markdown-file-path";
import type {
ActionEvent,
ObservationEvent,
OpenHandsEvent,
} from "#/types/agent-server/core";
import {
isActionEvent,
isObservationEvent,
} from "#/types/agent-server/type-guards";
import type {
FileEditorAction,
StrReplaceEditorAction,
} from "#/types/agent-server/core/base/action";
import type {
FileEditorObservation,
StrReplaceEditorObservation,
} from "#/types/agent-server/core/base/observation";
export { isMarkdownFilePath } from "#/utils/is-markdown-file-path";
const FILE_EDITOR_ACTION_KINDS = new Set([
"FileEditorAction",
"StrReplaceEditorAction",
]);
const FILE_EDITOR_OBSERVATION_KINDS = new Set([
"FileEditorObservation",
"StrReplaceEditorObservation",
]);
interface MarkdownFilePreviewProps {
content: string;
path: string;
/** When omitted (e.g. in-flight create), the View affordance is hidden. */
onView?: () => void;
}
/**
* Resolves the file path for a file-editor action/observation, including the
* observation's originating action when the observation omits `path`.
*/
function getFileEditorEventPath(
event: OpenHandsEvent,
correspondingAction?: ActionEvent,
): string | null {
if (isActionEvent(event) && FILE_EDITOR_ACTION_KINDS.has(event.action.kind)) {
return (
(event as ActionEvent<FileEditorAction | StrReplaceEditorAction>).action
.path || null
);
}
if (
isObservationEvent(event) &&
FILE_EDITOR_OBSERVATION_KINDS.has(event.observation.kind)
) {
const path = (
event as ObservationEvent<
FileEditorObservation | StrReplaceEditorObservation
>
).observation.path;
if (path) return path;
if (
correspondingAction &&
FILE_EDITOR_ACTION_KINDS.has(correspondingAction.action.kind)
) {
return (
(
correspondingAction as ActionEvent<
FileEditorAction | StrReplaceEditorAction
>
).action.path || null
);
}
}
return null;
}
/**
* Resolves the file-editor command (`create` / `view` / …), including the
* observation's originating action when the observation omits `command`.
*/
function getFileEditorEventCommand(
event: OpenHandsEvent,
correspondingAction?: ActionEvent,
): string | null {
if (isActionEvent(event) && FILE_EDITOR_ACTION_KINDS.has(event.action.kind)) {
return (
(event as ActionEvent<FileEditorAction | StrReplaceEditorAction>).action
.command || null
);
}
if (
isObservationEvent(event) &&
FILE_EDITOR_OBSERVATION_KINDS.has(event.observation.kind)
) {
const command = (
event as ObservationEvent<
FileEditorObservation | StrReplaceEditorObservation
>
).observation.command;
if (command) return command;
if (
correspondingAction &&
FILE_EDITOR_ACTION_KINDS.has(correspondingAction.action.kind)
) {
return (
(
correspondingAction as ActionEvent<
FileEditorAction | StrReplaceEditorAction
>
).action.command || null
);
}
}
return null;
}
/**
* True for file-editor *create* events whose path is a markdown artifact.
*
* Used to keep those cards expanded and outside collapsed action groups so
* the clipped preview is visible by default. Reads/edits of `.md` files stay
* on the normal groupable path.
*/
export function isMarkdownFileEditorEvent(
event: OpenHandsEvent,
correspondingAction?: ActionEvent,
): boolean {
const path = getFileEditorEventPath(event, correspondingAction);
const command = getFileEditorEventCommand(event, correspondingAction);
return Boolean(path && command === "create" && isMarkdownFilePath(path));
}
/**
* Height-clipped markdown card with an optional View bar that opens the file.
*/
export function MarkdownFilePreview({
content,
path,
onView,
}: MarkdownFilePreviewProps) {
const { t } = useTranslation("openhands");
const fileName = path.split("/").pop() || path;
return (
<div
className="w-full overflow-hidden rounded-[12px] border border-[var(--oh-border)] bg-[var(--oh-surface)]"
data-testid="markdown-file-preview"
>
<div
data-testid="markdown-file-preview-content"
className="max-h-40 overflow-y-auto px-4 py-3 text-white custom-scrollbar-always [--oh-scroll-fade-from:var(--oh-surface)]"
>
{/* Deliberately compact: the clipped in-stream card reuses the plan
preview's small typography; the Files drawer renders the same
artifact full-size with the default components. */}
<MarkdownRenderer
content={content}
includeStandard
includeHeadings
components={planComponents}
/>
</div>
<div className="flex h-10 items-center justify-between gap-2 border-t border-[var(--oh-border)] px-3">
<div className="flex min-w-0 items-center gap-1.5">
<FileIcon className="h-3.5 w-3.5 flex-shrink-0 text-[var(--oh-muted)]" />
<Typography.Text className="truncate font-mono text-[11px] leading-4 tracking-[0.11px] text-[var(--oh-muted)]">
{fileName}
</Typography.Text>
</div>
{onView ? (
<button
type="button"
onClick={onView}
className="flex shrink-0 cursor-pointer items-center gap-1 transition-opacity hover:opacity-80"
data-testid="markdown-file-preview-view"
>
<Typography.Text className="text-[11px] leading-4 tracking-[0.11px] text-white">
{t(I18nKey.COMMON$VIEW)}
</Typography.Text>
<ArrowUpRight className="text-white" size={16} />
</button>
) : null}
</div>
</div>
);
}
|