Files
windmill/frontend/src/lib/components/copilot/chat/AssistantMessage.svelte
T
Ruben FiszelandClaude Opus 5.5 767d2d6f91 fix(frontend): use the shared markdown renderer in more views
AI instruction/skill views and markdown results now use the shared markdown plugins; small approval result display fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 18:52:44 +00:00

197 lines
7.2 KiB
Svelte

<script lang="ts">
import Markdown from 'svelte-exmarkdown'
import { gfmPlugin } from 'svelte-exmarkdown/gfm'
import type { DisplayMessage } from './shared'
import ChatCollapsibleCard from './ChatCollapsibleCard.svelte'
import { thinkingPreferences } from './thinkingPreferences.svelte'
import CodeDisplay from './script/CodeDisplay.svelte'
import LinkRenderer from './LinkRenderer.svelte'
import MarkdownImage from './MarkdownImage.svelte'
import { messageS3Object } from './messageS3Object'
import {
extractCandidatePaths,
remarkWindmillPaths,
workspaceItemRegistry
} from './workspaceItems.svelte'
import { markdownProse } from '$lib/components/markdownProse'
import DisplayResult from '$lib/components/DisplayResult.svelte'
import { Bot, ExternalLink } from 'lucide-svelte'
import CopyButton from '$lib/components/common/button/CopyButton.svelte'
import { displayDate, truncateRev } from '$lib/utils'
interface Props {
message: DisplayMessage
// Workspace the message's paths are resolved against: the one the chat
// operates on, which is not always the one being navigated.
workspace: string | undefined
// Only the last answer of a run (a copilot turn, or a flow step's run) gets the copy /
// timestamp / run row: it stays in the layout while invisible, so on an answer between
// tool calls it would double the gap below it.
showActions?: boolean
}
let { message, workspace, showActions = true }: Props = $props()
// The run this answer came out of. Only a flow chat has one — a copilot turn runs in
// the browser — so the job link is absent rather than empty elsewhere.
const jobId = $derived(message.role === 'assistant' ? message.jobId : undefined)
const createdAt = $derived(message.role === 'assistant' ? message.createdAt : undefined)
const runHref = $derived(jobId ? `/run/${jobId}?workspace=${workspace}` : undefined)
// Today's answers show the time alone; the day earns its place only on a conversation
// read back later. Resolved at render, so a chat left open across midnight keeps
// yesterday's format until it is reopened.
const timestamp = $derived.by(() => {
if (!createdAt) return undefined
const at = new Date(createdAt)
const today = new Date().toDateString() === at.toDateString()
return displayDate(at, false, !today)
})
const reasoning = $derived(
message.role === 'assistant' ? message.reasoning?.trim() || undefined : undefined
)
// Set the moment thinking ends, which is mid-turn on the live message — the
// answer streams on afterwards.
const reasoningDurationMs = $derived(
message.role === 'assistant' ? message.reasoningDurationMs : undefined
)
// Shimmer while the reasoning text streams before the answer. Only the live
// synthetic message carries `streaming` — a finalized reasoning-only message
// (thinking that led straight to a tool call) must not look in-progress.
const reasoningStreaming = $derived(
!!reasoning &&
message.role === 'assistant' &&
!!message.streaming &&
!message.content &&
reasoningDurationMs === undefined
)
// Undefined until this block is toggled by hand, so flipping the preference
// reaches every block the reader hasn't already made a decision about.
let reasoningToggled = $state<boolean | undefined>(undefined)
const reasoningExpanded = $derived(reasoningToggled ?? thinkingPreferences.expandByDefault)
const reasoningLabel = $derived(
reasoningDurationMs !== undefined
? `Thought for ${formatThinkingDuration(reasoningDurationMs)}`
: reasoningStreaming
? 'Thinking...'
: 'Thinking'
)
function formatThinkingDuration(ms: number): string {
const seconds = Math.max(1, Math.round(ms / 1000))
if (seconds < 60) return `${seconds}s`
const minutes = Math.floor(seconds / 60)
const rest = seconds % 60
return rest === 0 ? `${minutes}m` : `${minutes}m ${rest}s`
}
const stepName = $derived(message.role === 'assistant' ? message.stepName : undefined)
// A flow step can return a file rather than text; the raw JSON would be
// unreadable, so hand it to the result viewer instead of the markdown renderer.
const s3Object = $derived(messageS3Object(message.content))
const candidatePaths = $derived(extractCandidatePaths(message.content))
const reasoningPlugins = [gfmPlugin(), { renderer: { a: LinkRenderer, img: MarkdownImage } }]
const rendererPlugin = {
renderer: {
pre: CodeDisplay,
a: LinkRenderer,
img: MarkdownImage
}
}
// Only populate the registry for messages that contain path-shaped tokens. The
// registry still dedups concurrent calls across messages and workspaces.
$effect(() => {
if (workspace && candidatePaths.length > 0) workspaceItemRegistry.ensureLoaded(workspace)
})
const plugins = $derived.by(() => {
const ws = workspace ?? ''
if (!ws || candidatePaths.length === 0) {
return [gfmPlugin(), rendererPlugin]
}
if (!workspaceItemRegistry.isLoaded(ws)) {
return [gfmPlugin(), rendererPlugin]
}
return [
gfmPlugin(),
{
remarkPlugin: remarkWindmillPaths({
resolve: (path) => workspaceItemRegistry.resolve(ws, path),
workspace: ws || undefined
}),
renderer: {}
},
rendererPlugin
]
})
</script>
<!-- An agent step's answer is headed by the agent's own icon, hung in the margin so the
answer itself stays on the same left edge as the reader's messages. The icon sits in
the padding the message column already carries. -->
{#if stepName}
<div
class="flex items-center gap-2 -ml-6 pt-1 text-2xs text-tertiary"
title="Answered by {stepName}"
>
<Bot size={16} class="shrink-0" />
<span class="font-mono truncate">{stepName}</span>
</div>
{/if}
{#if reasoning}
<ChatCollapsibleCard
label={reasoningLabel}
expanded={reasoningExpanded}
onToggle={() => (reasoningToggled = !reasoningExpanded)}
shimmer={reasoningStreaming}
class={message.content ? 'mb-1' : ''}
labelClass="truncate"
contentClass="font-main text-secondary {markdownProse.xs}"
>
<Markdown md={reasoning} plugins={reasoningPlugins} />
</ChatCollapsibleCard>
{/if}
{#if s3Object}
<div class="py-1">
<DisplayResult result={s3Object} workspaceId={workspace} noControls={true} />
</div>
{:else if message.content}
<div class="w-full space-y-2 py-1 {markdownProse.sm}">
<Markdown md={message.content} {plugins} />
</div>
{/if}
{#if message.content && showActions}
<!-- Zero height in the layout (h-4 = -mt-1 + -mb-3): it draws in the answer's bottom
padding and the gap below the turn, so a turn's last answer is spaced like any other
message. A thinking-only row has no answer to copy, and its run's last answer links
the run. -->
<div
class="flex items-center gap-2 h-4 -mt-1 -mb-3 text-2xs text-tertiary opacity-0 transition-opacity duration-150 group-hover/answer:opacity-100 focus-within:opacity-100"
>
<CopyButton value={message.content} title="Copy answer" class="-ml-1" btnClasses="h-4" />
{#if timestamp}
<span>{timestamp}</span>
{/if}
{#if runHref}
<a
href={runHref}
target="_blank"
rel="noopener noreferrer"
class="inline-flex items-center gap-1 hover:text-primary hover:underline"
title="Open this run"
>
<span>job <span class="font-mono">{truncateRev(jobId ?? '', 8)}</span></span>
<ExternalLink size={11} class="shrink-0" />
</a>
{/if}
</div>
{/if}