Files
windmill/frontend/src/lib/components/copilot/chat/AIChatDisplay.svelte
T

1473 lines
56 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { createBottomSticker } from '$lib/components/stickToBottom'
import AIChatMessage from './AIChatMessage.svelte'
import AppAvailableContextList from './AppAvailableContextList.svelte'
import ChatContextPicker from './ChatContextPicker.svelte'
import WorkspaceMentionPicker from './WorkspaceMentionPicker.svelte'
import { type Snippet } from 'svelte'
import {
AlertTriangle,
ArrowDown,
AtSign,
BookOpen,
CheckIcon,
FileText,
Folder,
HistoryIcon,
KeyRound,
MousePointer2,
Plug,
Plus,
TextSelect,
X,
XIcon
} from 'lucide-svelte'
import Button from '$lib/components/common/button/Button.svelte'
import { fade } from 'svelte/transition'
import Popover from '$lib/components/meltComponents/Popover.svelte'
import DropdownV2 from '$lib/components/DropdownV2.svelte'
import { pendingUserAction, pendingUserActionDetail, type DisplayMessage } from './shared'
import AutonomyModePicker from './AutonomyModePicker.svelte'
import { availableAutonomyModeOptions, resolveAutonomyMode } from './autonomyModes'
import type { ContextElement } from './context'
import ChatQuickActions from './ChatQuickActions.svelte'
import ContextUsageIndicator from './ContextUsageIndicator.svelte'
import AIChatModelSettings from './AIChatModelSettings.svelte'
import ScrollFade from '$lib/components/ScrollFade.svelte'
import AssistantSettingsModal from './AssistantSettingsModal.svelte'
import WorkingInPicker from './WorkingInPicker.svelte'
import { scopeLabel } from './workingScope'
import { SkillsMenu } from './skills/skillsMenu.svelte'
import { McpMenu } from '$lib/components/mcp/mcpMenu.svelte'
import ChatMode from './ChatMode.svelte'
import DatatableCreationPolicy from './DatatableCreationPolicy.svelte'
import Tooltip from '$lib/components/meltComponents/Tooltip.svelte'
import Markdown from 'svelte-exmarkdown'
import { twMerge } from 'tailwind-merge'
import { AIAutonomyMode, AIMode } from './AIChatManager.svelte'
import { AIWorkMode, type WorkModeLock } from './workMode'
import { getChatViewHost } from './chatViewHost'
import { getAiChatManager } from './aiChatManagerContext'
import ChatTypingIndicator from './ChatTypingIndicator.svelte'
import ToolGroupDisplay from './ToolGroupDisplay.svelte'
import { chatItemKey, createChatItemGrouper } from './toolGroups'
import AIChatInput from './AIChatInput.svelte'
import {
STARTER_PLACEHOLDER,
USE_STARTER_PLACEHOLDER,
type ComposerSelection
} from './composerBox'
import AttachedFilesBar from './files/AttachedFilesBar.svelte'
import QueuedMessageChip from './QueuedMessageChip.svelte'
import JobsSegment from './JobsSegment.svelte'
import { getModifierKey } from '$lib/utils'
import type { SelectedContext } from './app/core'
import { type FileToAttach } from './files/attachedFiles.svelte'
import { isImageFile } from './imageUtils'
import {
hasFileSystemAccess,
pickDirectory,
handlesFromDataTransfer,
isDirectoryHandle,
isFileHandle,
readDroppedEntries
} from './files/fsAccess'
import { sendUserToast } from '$lib/toast'
import Alert from '$lib/components/common/alert/Alert.svelte'
import { copilotInfo } from '$lib/aiStore'
import ComposerSuggestions from './starter/ComposerSuggestions.svelte'
import type { DraftAttachments } from './messageDraft.svelte'
import type { ChatStarter } from './starter/starterPrompts.svelte'
import { asksForComposer, isComposerOverlay } from './starter/composerCollapse'
import { smoothHeight } from './starter/smoothHeight'
const MAX_YOLO_TOOLTIP_TOOLS = 8
const chatHost = getChatViewHost()
// The skill and MCP menus take an AIChatManager itself, which the seam deliberately
// doesn't carry. They render only under GLOBAL, which a non-copilot host never sets.
const aiChatManager = getAiChatManager()
// The free grant pays for the copilot's own model, so its banners belong only to a host
// that sends to that model. A flow chat's turn runs on the flow's provider.
const freeTier = $derived(chatHost.supportsModelSettings ? $copilotInfo.freeTier : undefined)
// The user spent their one-time free Windmill AI grant: there is no model left to send
// to, so say so in the thread itself rather than only failing on send.
let freeTierExhausted = $derived(freeTier?.exhausted === true)
// Still on the free grant: keep how much is left in view right above the composer, so
// running out isn't a surprise. Once spent, the exhausted banner replaces it.
let freeTierUsedPct = $derived(Math.min(100, Math.round((freeTier?.used_ratio ?? 0) * 100)))
let showFreeTierUsage = $derived(!!freeTier && !freeTier.exhausted)
let {
messages,
pastChats,
hasDiff,
diffMode = false, // todo: remove default
selectedContext = $bindable([]), // todo: remove default
availableContext = [], // todo: remove default
loadPastChat,
deletePastChat,
saveAndClear,
askAi = () => {}, // todo: remove default,
headerLeft,
headerRight,
disabled = false,
disabledMessage = '',
suggestions = [],
hideHeader = false,
hideModeSelector = false,
wideLayout = false,
emptyHint,
starter = undefined,
onSent = undefined,
inputPreface,
workModeLock = undefined,
footerSettings,
initialInstructions = undefined,
onDraftChange = undefined,
initialAttachments = undefined,
onAttachmentsChange = undefined,
placeholder = undefined,
scrollElement = $bindable(),
onTranscriptScroll = undefined
}: {
messages: DisplayMessage[]
pastChats: { id: string; title: string }[]
hasDiff?: boolean
diffMode: boolean
selectedContext: ContextElement[]
availableContext: ContextElement[]
loadPastChat: (id: string) => void
deletePastChat: (id: string) => void
saveAndClear: () => void
askAi?: (instructions: string, options?: { withCode?: boolean; withDiff?: boolean }) => void
headerLeft?: Snippet
headerRight?: Snippet
disabled?: boolean
disabledMessage?: string
suggestions?: string[]
hideHeader?: boolean
hideModeSelector?: boolean
// Center the messages + input columns inside a max-w-3xl px-8
// inner container. The session pane uses this for breathing
// room; the right-hand global chat panel is narrow enough that
// the inner padding eats too much horizontal space, so it's
// off there.
wideLayout?: boolean
emptyHint?: Snippet
/** Open an empty chat as the block that starts a session: Home's composer, collapsed
* to one line at rest. Replaces `emptyHint`. */
starter?: ChatStarter
/** After the composer hands a new turn to the host. */
onSent?: () => void
inputPreface?: Snippet
/** Holds a session chat in Use, and says why in the picker. */
workModeLock?: WorkModeLock
/** The settings control at the footer's right edge, where the copilot puts its
* model picker. A host that configures its turn elsewhere replaces it here. */
footerSettings?: Snippet
// Seed / observe the main composer's draft (see AIChatInput).
initialInstructions?: string
onDraftChange?: (text: string) => void
initialAttachments?: DraftAttachments
onAttachmentsChange?: (attachments: DraftAttachments) => void
/** Composer placeholder. Falls back to the per-AI-mode wording. */
placeholder?: string
/** The transcript's scroll container. A host that paginates older messages
* needs it to measure and restore the scroll position. */
scrollElement?: HTMLDivElement | undefined
onTranscriptScroll?: () => void
} = $props()
let aiChatInput: AIChatInput | undefined = $state()
let assistantSettings: AssistantSettingsModal | undefined = $state()
// The "+" menu's skill and MCP rows: enough state to check and flip one, with
// everything else about them behind the assistant settings modal.
const skillsMenu = new SkillsMenu(aiChatManager, () => assistantSettings?.open('skills'))
const mcpMenu = new McpMenu(aiChatManager, () => assistantSettings?.open('mcp'))
let plusMenuOpen = $state(false)
let editingMessageIndex = $state<number | null>(null)
const groupChatItems = createChatItemGrouper()
const chatItems = $derived(groupChatItems(messages))
// Escape stops the generation when focus is on the chat (or parked on
// body), but stays with other widgets (e.g. the session's Monaco editor).
// Capture phase is required: Monaco and mounted-but-closed Modal2
// instances consume window Escapes before they bubble.
let panelEl: HTMLDivElement | undefined = $state()
$effect(() => {
function onWindowKeydownCapture(e: KeyboardEvent) {
if (e.key !== 'Escape' || !chatHost.loading) return
const active = document.activeElement
// Focus parked on the body answers for the panel on screen only: the flow chat keeps
// a panel per conversation mounted, several of which can be loading, and a hidden
// one's listener would otherwise swallow the press (immediate form, below) and stop
// its own turn instead. `inert` marks the panels behind, and is matched as an
// attribute so the browsers without `checkVisibility` read it too.
const hidden =
panelEl?.closest('[inert]') != null ||
panelEl?.checkVisibility?.({ visibilityProperty: true }) === false
const focusOnChat =
!hidden && (!active || active === document.body || panelEl?.contains(active) === true)
// An Escape while a run form is open must not discard what the user typed, so the action
// row alone stops the turn — wherever it is mounted, since the preview panel holds the
// form outside `panelEl`. Matched by call: two chats can be loading at once, and one's
// row must not answer for the other.
if (chatHost.hasPendingRunForm) {
const row = active?.closest('[data-run-form-actions]')
const toolCallId = row?.getAttribute('data-run-form-actions')
if (!toolCallId || !chatHost.isRunFormPending(toolCallId)) return
} else if (!focusOnChat) return
e.preventDefault()
// Immediate form: other chat panels' identical listeners must not
// also cancel on body focus, nor a drawer/modal close on this press.
e.stopImmediatePropagation()
chatHost.cancel()
}
window.addEventListener('keydown', onWindowKeydownCapture, true)
return () => window.removeEventListener('keydown', onWindowKeydownCapture, true)
})
// Shared with the agent run viewer, which needs the same programmatic-scroll
// guard for the same reason.
const sticker = createBottomSticker()
// Per message: whether it is the last answer of its turn — per flow step run, since each
// run's last answer carries the only link to that run. Keyed on the answer's own job (a step
// label repeats when a step runs in a loop), and on answers rather than the next row: a
// flow's tool rows have their own job, and a stopped turn can end on a tool row.
// None in a turn still running, paused on the user included (their reply lands as a tool
// result, so a row shown during the pause would vanish again): which answer ends the turn is
// unknown until it does, and one followed by a tool call would leave the row's blank gap above
// it. A manual compaction loads without a turn of its own, so the last turn keeps its row.
const showsAnswerActions = $derived.by(() => {
const shows: boolean[] = new Array(messages.length).fill(false)
const answeredLater = new Set<string | undefined>()
let inRunningTurn = chatHost.loading && !chatHost.compacting
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i]
if (message.role === 'user' || message.role === 'summary') {
answeredLater.clear()
inRunningTurn = false
} else if (message.role === 'assistant' && message.content) {
const run = message.jobId ?? message.stepName
shows[i] = !inRunningTurn && !answeredLater.has(run)
answeredLater.add(run)
}
}
return shows
})
function scrollDown() {
sticker.scrollToEnd(scrollElement)
}
let height = $state(0)
$effect(() => {
if (chatHost.automaticScroll && height) {
scrollDown()
}
// Recompute the scroll-to-latest visibility on every content-height
// change. `onScroll` only fires for actual scroll events, so without
// this the arrow can go stale when content grows past the threshold
// while auto-scroll is disabled (user scrolled up mid-stream).
if (scrollElement && height) {
const distance =
scrollElement.scrollHeight - scrollElement.scrollTop - scrollElement.clientHeight
showScrollToLatest = distance > SCROLL_TO_LATEST_THRESHOLD_PX
}
})
// Show the "scroll to latest" arrow only once the user has scrolled
// meaningfully away from the tail — a couple of message-heights up. Avoids
// flicker when the auto-scroll lags by a few px during streaming.
const SCROLL_TO_LATEST_THRESHOLD_PX = 200
let showScrollToLatest = $state(false)
function onScroll() {
if (!scrollElement) return
const distance =
scrollElement.scrollHeight - scrollElement.scrollTop - scrollElement.clientHeight
// Always refresh the arrow visibility — even during the cooldown,
// because clicking the arrow itself triggers a programmatic scroll
// whose only event would otherwise be swallowed, leaving the arrow
// stuck visible after we already reached the bottom.
showScrollToLatest = distance > SCROLL_TO_LATEST_THRESHOLD_PX
if (sticker.isOwnScroll()) {
return
}
if (sticker.isAtEnd(scrollElement)) {
chatHost.enableAutomaticScroll()
} else {
chatHost.disableAutomaticScroll()
}
onTranscriptScroll?.()
}
function submitSuggestion(suggestion: string) {
chatHost.sendRequest({ instructions: suggestion })
}
export function focusInput(selection?: ComposerSelection) {
aiChatInput?.focusInput(selection)
}
/** Mention a workspace item in the draft, as "+" → Mention file does, and hand the user
* the caret after it. Focusing is what opens Home's one-line composer. */
export async function attachContext(element: ContextElement) {
await mentionWorkspaceItem(element)
aiChatInput?.focusInput()
}
$effect(() => {
const input = aiChatInput
if (!input) return
chatHost.setAiChatInput(input)
return () => chatHost.releaseAiChatInput(input)
})
// Also shown for a run held by another tab, labeled with where it is: the
// dots say a turn is in flight even before the reader reaches the footer
// note. Remote runs pause nothing and offer no Stop — this tab can't cancel.
const showTypingIndicator = $derived(chatHost.loading || chatHost.runHeldElsewhere)
// The manual `@` context-picker button. Shown in SCRIPT/FLOW (workspace items +
// code blocks) and APP (datatables, frontend files). Hidden in GLOBAL — there
// `@`-context is still invoked inline by typing `@` in the input, so the button
// is redundant. NAVIGATOR/ASK/API don't take @-context at all.
const showContextPicker = $derived(
chatHost.mode === AIMode.SCRIPT || chatHost.mode === AIMode.FLOW || chatHost.mode === AIMode.APP
)
// Why attaching is off, when this chat takes attachments but cannot right now. The `+` is
// kept and disabled rather than dropped: the input is the composer's either way, so the
// reader has to be able to see here why nothing can be attached.
const attachmentsOffReason = $derived(
chatHost.supportsMessageAttachments ? chatHost.attachmentsUnavailableReason : undefined
)
const canAttachFiles = $derived(
chatHost.supportsMessageAttachments && !disabled && !attachmentsOffReason
)
// Folders are linked as session-wide assets, which only a host that reads files in
// the browser can do — a host running the turn server-side takes attachments only.
const canLinkFolders = $derived(chatHost.supportsLinkedFolders && !disabled)
let fileInputEl = $state<HTMLInputElement | null>(null)
let folderInputEl = $state<HTMLInputElement | null>(null)
let dragDepth = $state(0)
const isDraggingFiles = $derived(dragDepth > 0)
// File System Access API → live re-grantable folder handles (refreshed each turn).
// Otherwise folders are snapshotted into the browser (via webkitdirectory / dropped-entry
// walk), same as files. Either way folders display identically.
const canUseFsAccess = hasFileSystemAccess()
function reportAddResult(added: string[], rejected: { name: string; reason: string }[]) {
if (rejected.length === 0) return
// Single rejected file (e.g. one dropped image): show the precise reason.
if (added.length === 0 && rejected.length === 1) {
sendUserToast(`Could not attach "${rejected[0].name}": ${rejected[0].reason}`, true)
return
}
// Otherwise (folders / multi-select): summarize to avoid a flood of toasts. The only
// per-file rejection left is non-text content (binary files are skipped).
const lead = added.length
? `Attached ${added.length}, skipped ${rejected.length}`
: `Skipped ${rejected.length} file${rejected.length === 1 ? '' : 's'}`
sendUserToast(`${lead} (non-text).`, added.length === 0)
}
async function handleAddFiles(files: FileList | FileToAttach[]) {
const { added, rejected } = await chatHost.attachedFiles.addFiles(files)
reportAddResult(added, rejected)
}
async function addDirHandle(dir: FileSystemDirectoryHandle) {
const { added, rejected } = await chatHost.attachedFiles.addFolder(dir)
reportAddResult(added, rejected)
}
function linkFiles() {
// Files are always snapshotted (every browser), so the universal picker is fine.
fileInputEl?.click()
}
async function linkFolder() {
if (!canUseFsAccess) {
// No File System Access API → pick a folder via the directory input; its files are
// snapshotted into the browser (no live handle), grouped under the folder name.
folderInputEl?.click()
return
}
let dir: FileSystemDirectoryHandle | undefined
try {
dir = await pickDirectory()
} catch (e) {
// The picker threw instead of opening — surface why (e.g. a browser/enterprise
// policy blocking the File System Access API) rather than appearing to do nothing.
sendUserToast(
`Couldn't open the folder picker: ${e instanceof Error ? e.message : String(e)}`,
true
)
return
}
if (dir) await addDirHandle(dir)
}
function dragHasFiles(e: DragEvent): boolean {
return Array.from(e.dataTransfer?.types ?? []).includes('Files')
}
// A drop is claimed while attaching is off for a stated reason, too: the browser would
// otherwise navigate to the dropped file, and the reader is owed the reason instead.
const panelTakesDrops = $derived(canAttachFiles || attachmentsOffReason !== undefined)
function onPanelDragEnter(e: DragEvent) {
if (!panelTakesDrops || !dragHasFiles(e)) return
e.preventDefault()
dragDepth++
}
function onPanelDragOver(e: DragEvent) {
if (!panelTakesDrops || !dragHasFiles(e)) return
e.preventDefault()
if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
}
function onPanelDragLeave(_e: DragEvent) {
if (!panelTakesDrops) return
dragDepth = Math.max(0, dragDepth - 1)
}
async function onPanelDrop(e: DragEvent) {
dragDepth = 0
if (!panelTakesDrops || !dragHasFiles(e)) return
e.preventDefault()
if (attachmentsOffReason) {
sendUserToast(attachmentsOffReason, true)
return
}
const dt = e.dataTransfer
if (!dt) return
// Images and loose text files attach to the message; folders link as session
// assets. Images are reserved from dt.files BEFORE any await (a send
// mid-ingestion would land them on the next message), and dt.files is the
// only place a disk-less drag exists — a cross-tab image resolves every
// getAsFileSystemHandle() to null.
const flatFiles = Array.from(dt.files ?? [])
const topLevelImages = flatFiles.filter(isImageFile)
const imageWork: Promise<unknown>[] = []
// The composer this drop landed on, held for the whole routing: a panel destroyed
// while a file is being read clears the binding, and the file would then reach no
// composer at all.
const dropped = aiChatInput
if (topLevelImages.length > 0) {
imageWork.push(dropped?.addImages(topLevelImages) ?? Promise.resolve())
}
// Text-file routing must await handle/entry resolution before it can call
// addTextFiles — hold sending across that window (taken BEFORE the first
// await) or a send mid-resolution would land the drop on the next message.
const releaseSendHold = dropped?.holdSendForIngestion()
try {
await routeDroppedTextAndFolders(dt, flatFiles, dropped)
} finally {
releaseSendHold?.()
}
await Promise.all(imageWork)
}
async function routeDroppedTextAndFolders(
dt: DataTransfer,
flatFiles: File[],
input: typeof aiChatInput
) {
if (canUseFsAccess) {
// getAsFileSystemHandle calls are kicked off synchronously inside this call.
const handles = await handlesFromDataTransfer(dt)
// No handles → nothing beyond dt.files exists; its text files are all there is.
// Handle-backed files are top-level by definition, so their images are
// already reserved above — only text files remain to route.
const looseFiles =
handles.length === 0
? flatFiles
: await Promise.all(handles.filter(isFileHandle).map((h) => h.getFile()))
// Loose files attach to the message, like images.
await attachNonImageFiles(
looseFiles.filter((f) => !isImageFile(f)),
input
)
// Folders link as a live handle.
const dirs = handles.filter(isDirectoryHandle)
if (dirs.length > 0 && !canLinkFolders) {
sendUserToast('Folders cannot be attached in this chat — drop individual files.', true)
} else {
for (const h of dirs) {
await addDirHandle(h)
}
}
} else {
// Fallback (no File System Access API): snapshot dropped files AND folders by walking
// the legacy webkitGetAsEntry tree. readDroppedEntries reads the entries synchronously
// (they're only valid during this event) before its first await; if it yields nothing
// (no entry API), fall back to the flat dt.files.
const entries = await readDroppedEntries(Array.from(dt.items ?? []))
const source: FileToAttach[] = entries.length > 0 ? entries : flatFiles
// Top-level files attach to the message (images were already reserved
// from dt.files before the walk). Folder children keep riding the
// session store as a snapshot — including nested images, which are
// deliberately NOT attached (the FSA path never extracts folder
// contents either); they are summarized as skipped there.
const topLevelText: File[] = []
const folderEntries: FileToAttach[] = []
for (const entry of source) {
const file = entry instanceof File ? entry : entry.file
const nested = !(entry instanceof File) && !!entry.path?.includes('/')
if (nested) {
folderEntries.push(entry)
} else if (!isImageFile(file)) {
topLevelText.push(file)
}
}
if (folderEntries.length > 0) {
if (canLinkFolders) await handleAddFiles(folderEntries)
else sendUserToast('Folders cannot be attached in this chat — drop individual files.', true)
}
await attachNonImageFiles(topLevelText, input)
}
}
async function onFileInputChange(e: Event) {
const input = e.currentTarget as HTMLInputElement
if (input.files && input.files.length > 0) {
await attachPickedFiles(Array.from(input.files))
}
input.value = '' // allow re-selecting the same file
}
/** `input` is the composer the files are for, captured before any await: read off the
* binding afterwards it would be null once the panel has been destroyed. */
async function attachNonImageFiles(files: File[], input: typeof aiChatInput) {
await input?.addNonImageFiles(files)
}
async function attachPickedFiles(picked: File[]) {
const imageFiles = picked.filter(isImageFile)
const others = picked.filter((f) => !isImageFile(f))
// Reserved before the other work is awaited — see onPanelDrop.
const imageWork = imageFiles.length > 0 ? aiChatInput?.addImages(imageFiles) : undefined
await attachNonImageFiles(others, aiChatInput)
await imageWork
}
function mentionWorkspaceItem(element: ContextElement) {
void aiChatInput?.addContextToSelection(element)
return aiChatInput?.insertMention(element.title)
}
function onFolderInputChange(e: Event) {
const input = e.currentTarget as HTMLInputElement
// webkitdirectory files carry webkitRelativePath (`folder/sub/file`); addFiles groups
// them under the folder and skips junk paths. Snapshot, like a dropped folder.
if (input.files && input.files.length > 0) void handleAddFiles(input.files)
input.value = ''
}
const autonomyAvailability = $derived({
autoAcceptEditsAvailable: chatHost.autoAcceptEditsAvailable,
autoAcceptToolConfirmationsAvailable: chatHost.autoAcceptToolConfirmationsAvailable
})
const effectiveAutonomyMode = $derived(
resolveAutonomyMode(chatHost.autonomyMode, autonomyAvailability)
)
const showAutonomyModeSelector = $derived(
!disabled &&
(chatHost.workModesAvailable || availableAutonomyModeOptions(autonomyAvailability).length > 1)
)
// The typing-dots indicator implies the AI is busy, which is misleading while
// the loop is parked on the user; surface a text pill instead so users know to
// act on the tool above.
// A step name hangs its icon in the column's left padding (see AssistantMessage), so a
// transcript carrying one widens the padding, on both sides to keep the column centred.
const agentGutter = $derived(messages.some((m) => m.role === 'assistant' && m.stepName))
// The block's layout holds while the chat is empty, usable or not; only the taller box
// and the suggestions need a composer that can send.
const starterLayout = $derived(!!starter && messages.length === 0)
const heldInUse = $derived(
chatHost.workModesAvailable && (!!workModeLock || chatHost.workMode === AIWorkMode.USE)
)
// Home's composer at rest is one line. Focusing the field opens it; a press outside it or
// Escape puts it back, but only with nothing in the draft: leaving it open then means
// clearing the text later cannot fold it up under the caret. Opened by the field rather
// than any focus inside: the one-line row's model button would otherwise unmount under the
// menu it is opening.
let composerEl: HTMLDivElement | undefined = $state()
let composerOpened = $state(false)
// Anything the user has started (text, an attachment) or is in the middle of (they opened it
// and have not left) keeps it open, and so does a host that has it rest open.
// Until the input is bound, the draft it starts with is the one it is seeded with: reading
// it as empty collapses a remounted composer that holds text for a frame, and the box then
// animates open as if the user had opened it.
const seededDraftEmpty = $derived(
!initialInstructions?.trim() &&
!initialAttachments?.pastes.length &&
!initialAttachments?.images.length &&
!initialAttachments?.files.length &&
!initialAttachments?.blobs.length
)
const composerCollapsed = $derived(
starterLayout &&
!disabled &&
!starter?.restsOpen &&
!composerOpened &&
(aiChatInput ? aiChatInput.isDraftEmpty() : seededDraftEmpty)
)
function closeComposer() {
if (aiChatInput?.isDraftEmpty() ?? true) composerOpened = false
}
function onComposerPointerDown(e: PointerEvent) {
if (!starterLayout || !composerOpened || composerEl?.contains(e.target as Node)) return
if (!isComposerOverlay(e.target) && !asksForComposer(e.target)) closeComposer()
}
// While the box plays its accent outline (app.css, `data-composer-flash`).
let flashing = $state(false)
/** Points the eye at a composer the user was sent to without it moving: its box's outline
* lights up in the accent color and fades. */
export function flashComposer() {
// Off for a frame first, so a flash asked for while one plays starts over.
flashing = false
requestAnimationFrame(() => (flashing = true))
}
// Home's composer is one block of the page and takes the page's column as it is.
const columnClass = $derived(
starter
? 'w-full'
: wideLayout
? `w-full max-w-3xl mx-auto ${agentGutter ? 'px-8' : 'px-7'}`
: `w-full max-w-2xl mx-auto ${agentGutter ? 'px-8' : 'px-3'}`
)
const waitingForUserAction = $derived(chatHost.loading && !!pendingUserAction(messages))
// The out-of-scope card appears in the transcript without taking focus, so a screen reader
// hears about it from a live region that stays mounted and changes its text.
const outOfScopeAnnouncement = $derived.by(() => {
const held = [...messages]
.reverse()
.find((m) => m.role === 'tool' && m.isLoading && m.outOfScope && !m.outOfScope.decision)
if (held?.role !== 'tool' || !held.outOfScope) return ''
const { path, scope, verb } = held.outOfScope
return `This would ${verb} ${path}, outside ${scope}. Allow once, or allow writes in ${held.outOfScope.root}.`
})
// The turns sent in another scope than the turn before, with the scope they took.
const scopeChanges = $derived.by(() => {
const changes = new Map<number, string | null>()
let previous: string | undefined
messages.forEach((m, i) => {
// Read back from saved chats, so only a list or `null` counts.
if (m.role !== 'user' || (m.workingIn !== null && !Array.isArray(m.workingIn))) return
const label = m.workingIn ? scopeLabel(m.workingIn) : ''
if (previous !== undefined && label !== previous) changes.set(i, label || null)
previous = label
})
return changes
})
// Gated on `loading` because a card restored from history still looks parked:
// its resolver left with the old page, so the composer must not advertise an
// answer it cannot deliver.
const pendingQuestionToolCallId = $derived.by(() => {
if (!chatHost.loading) {
return undefined
}
const pending = pendingUserActionDetail(messages)
return pending?.action === 'question' ? pending.toolCallId : undefined
})
// Get app context for display when in APP mode
const appContext = $derived.by((): SelectedContext | undefined => {
if (chatHost.mode !== AIMode.APP || !chatHost.appAiChatHelpers) {
return undefined
}
return chatHost.appAiChatHelpers.getSelectedContext()
})
const yoloBypassedTools = $derived.by(() => {
return chatHost.tools
.filter((tool) => tool.requiresConfirmation === true || tool.bypassedByAutoAccept === true)
.map((tool) => ({
name: tool.def.function.name,
// confirmationMessage may be a function of the call args, which we don't
// have here — fall back to the tool name rather than render its source.
label:
typeof tool.confirmationMessage === 'string'
? tool.confirmationMessage
: tool.def.function.name
}))
})
const visibleYoloBypassedTools = $derived(yoloBypassedTools.slice(0, MAX_YOLO_TOOLTIP_TOOLS))
const hiddenYoloBypassedToolCount = $derived(
Math.max(0, yoloBypassedTools.length - visibleYoloBypassedTools.length)
)
const showFlowPendingActionControls = $derived(
(chatHost.flowAiChatHelpers?.hasPendingChanges() ?? false) && !chatHost.autoAcceptEditsActive
)
// A disabled state with no message (a remote hold, a spent free grant) keeps
// the footer toolbar in place — swapping it for an empty strip would make
// the model/mode row flash out and back on every remote turn. A state with
// a real message (archived, AI off) still shows it, hold or not, matching
// the precedence disabledMessage itself encodes.
const footerMessageShown = $derived(disabled && disabledMessage !== '')
// `canAttachFiles` belongs in the group too: in GLOBAL mode the `+` always has the
// context picker or the autonomy selector beside it, but a host with attachments and
// nothing else would lose the group and the `+` with it.
const showFooterLeftControls = $derived(
!footerMessageShown &&
(canAttachFiles ||
attachmentsOffReason !== undefined ||
showContextPicker ||
showAutonomyModeSelector ||
(chatHost.mode === AIMode.SCRIPT && hasDiff))
)
</script>
{#snippet freeTierExhaustedBanner()}
<div class="my-2">
<Alert type="info" size="xs" title="Free Windmill AI used up">
<div class="flex flex-col items-start gap-2">
<span>
You have used all of your free Windmill AI tokens. Add your own API key to keep using AI.
</span>
<Button
unifiedSize="2xs"
variant="accent"
startIcon={{ icon: KeyRound }}
href="/workspace_settings?tab=ai"
>
Add your own API key
</Button>
</div>
</Alert>
</div>
{/snippet}
{#snippet freeTierUsageBanner()}
<div
class="my-1 flex items-center justify-between gap-2 rounded-md border bg-surface-secondary px-2 py-1"
>
<span class="text-xs text-secondary tabular-nums">
{freeTierUsedPct}% of your free Windmill AI used
</span>
<Button
unifiedSize="2xs"
variant="default"
startIcon={{ icon: KeyRound }}
href="/workspace_settings?tab=ai"
>
Configure your API key
</Button>
</div>
{/snippet}
<svelte:document onpointerdown={onComposerPointerDown} />
<!-- tabindex="-1": clicks on non-focusable chat content must move focus into
the panel, or the Escape-to-stop focus check would wrongly reject them. -->
<div
class="flex flex-col h-full relative outline-none"
tabindex="-1"
bind:this={panelEl}
ondragenter={onPanelDragEnter}
ondragover={onPanelDragOver}
ondragleave={onPanelDragLeave}
ondrop={onPanelDrop}
role="region"
aria-label="AI chat"
>
{#if isDraggingFiles}
<div
class="absolute inset-0 z-50 flex items-center justify-center pointer-events-none rounded-md border-2 border-dashed border-blue-400 bg-blue-500/10"
transition:fade={{ duration: 100 }}
>
<div class="flex flex-col items-center gap-1 text-blue-600 dark:text-blue-300">
<Plus size={24} />
<span class="text-sm font-medium">Drop files or images to attach</span>
</div>
</div>
{/if}
{#if !hideHeader}
<div
class="flex flex-row items-center justify-between gap-2 p-2 border-b border-gray-200 dark:border-gray-600"
>
<div class="flex flex-row items-center gap-2">
{@render headerLeft?.()}
<p class="text-sm font-semibold">Chat</p>
</div>
<div class="flex flex-row items-center gap-2">
<Popover>
{#snippet trigger()}
<Button
on:click={() => {}}
title="History"
size="md"
btnClasses="!p-1"
startIcon={{ icon: HistoryIcon }}
iconOnly
variant="border"
color="light"
propagateEvent
/>
{/snippet}
{#snippet content({ close })}
<div class="p-1 overflow-y-auto max-h-[300px]">
{#if pastChats.length === 0}
<div class="text-center text-primary text-xs">No history</div>
{:else}
<div class="flex flex-col">
{#each pastChats as chat (chat.id)}
<button
class="text-left flex flex-row items-center gap-2 justify-between hover:bg-gray-100 dark:hover:bg-gray-700 rounded-md p-1 disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent dark:disabled:hover:bg-transparent"
disabled={chatHost.loading ||
chatHost.sendInFlight ||
chatHost.runHeldElsewhere}
title={chatHost.runHeldElsewhere
? 'Wait for the turn in the other tab to switch conversation'
: chatHost.loading || chatHost.sendInFlight
? 'Stop the current answer to switch conversation'
: undefined}
onclick={() => {
loadPastChat(chat.id)
close()
}}
>
<div
class="text-xs font-medium w-48 text-ellipsis overflow-hidden whitespace-nowrap flex-1"
title={chat.title}
>
{chat.title}
</div>
<Button
iconOnly
size="xs2"
btnClasses="!p-1"
variant="default"
startIcon={{ icon: X }}
on:click={() => {
deletePastChat(chat.id)
}}
/>
</button>
{/each}
</div>
{/if}
</div>
{/snippet}
</Popover>
<Button
title={chatHost.runHeldElsewhere
? 'Wait for the turn in the other tab to start a new chat'
: 'New chat'}
disabled={chatHost.runHeldElsewhere}
on:click={() => {
saveAndClear()
}}
size="md"
btnClasses="!p-1"
startIcon={{ icon: Plus }}
iconOnly
variant="border"
color="light"
/>
{@render headerRight?.()}
</div>
</div>
{/if}
{#if messages.length === 0}
{#if starter}
<!-- Home's composer is one control among the page's: nothing above it. -->
{:else if emptyHint}
{@render emptyHint()}
{:else}
<span class="text-2xs text-gray-500 dark:text-gray-400 text-center px-2 my-2"
>You can use {getModifierKey()}L to open or close this chat, and {getModifierKey()}K in the
script editor to modify selected lines.</span
>
{/if}
{#if freeTierExhausted}
<div class={wideLayout ? 'w-full max-w-3xl mx-auto px-7' : 'w-full max-w-2xl mx-auto px-3'}>
{@render freeTierExhaustedBanner()}
</div>
{/if}
{/if}
{#if messages.length > 0}
<div class="flex-1 min-h-0 relative">
<div
class="absolute inset-0 overflow-y-scroll pt-2 scrollbar-subtle"
bind:this={scrollElement}
onscroll={onScroll}
>
<div class="{columnClass} flex flex-col pb-2" bind:clientHeight={height}>
{#snippet messageRow(
message: DisplayMessage,
messageIndex: number,
isLast: boolean,
inGroup: boolean
)}
{#if scopeChanges.has(messageIndex)}
{@const root = scopeChanges.get(messageIndex)}
<p class="mt-4 text-center text-2xs text-tertiary">
Now working in {#if root}<span class="font-mono">{root}</span>{:else}the whole
workspace{/if}
</p>
{/if}
<AIChatMessage
{message}
{messageIndex}
{availableContext}
bind:editingMessageIndex
{isLast}
{inGroup}
showAnswerActions={showsAnswerActions[messageIndex]}
/>
{/snippet}
{#snippet groupRow(message: DisplayMessage, messageIndex: number)}
{@render messageRow(message, messageIndex, false, true)}
{/snippet}
{#each chatItems as item (chatItemKey(item))}
{#if item.kind === 'group'}
<div
class={twMerge(
'mb-1 min-w-0 text-sm px-2 text-primary',
item.entries.at(-1)?.index === messages.length - 1 && '!mb-12'
)}
>
<div class="px-[1px]">
<ToolGroupDisplay group={item} entry={groupRow} />
</div>
</div>
{:else}
{@render messageRow(
item.message,
item.index,
item.index === messages.length - 1,
false
)}
{/if}
{/each}
{#if freeTierExhausted}
{@render freeTierExhaustedBanner()}
{/if}
{#if showTypingIndicator}
<div
class={twMerge(
'sticky z-10 -mt-10 ml-2 self-start pointer-events-none',
showFlowPendingActionControls ? 'bottom-14' : 'bottom-2'
)}
>
<ChatTypingIndicator
loading={showTypingIndicator}
paused={waitingForUserAction}
label={chatHost.runHeldElsewhere
? 'Running in another tab'
: chatHost.loadingLabel
? chatHost.loadingLabel
: chatHost.compacting
? 'Compacting conversation'
: chatHost.currentReasoningActive &&
!chatHost.currentReply &&
!chatHost.currentReasoning
? (chatHost.reasoningHiddenIndicatorLabel ?? 'Thinking')
: undefined}
/>
</div>
{/if}
<div class="sr-only" role="status">{outOfScopeAnnouncement}</div>
</div>
</div>
<!-- Sits below the scroll-to-latest button, which carries z-10. -->
<ScrollFade scroller={scrollElement} />
{#if showScrollToLatest}
<div
transition:fade={{ duration: 120 }}
class={twMerge(
'absolute left-1/2 -translate-x-1/2 z-10 rounded-md bg-surface shadow-md',
showFlowPendingActionControls ? 'bottom-12' : 'bottom-2'
)}
>
<Button
variant="default"
unifiedSize="sm"
iconOnly
title="Scroll to latest"
aria-label="Scroll to latest message"
startIcon={{ icon: ArrowDown }}
on:click={() => {
chatHost.enableAutomaticScroll()
scrollDown()
}}
/>
</div>
{/if}
</div>
{/if}
<!-- Same horizontal padding as the transcript above: the composer's edges line up with
the messages rather than sitting closer to the panel edge. -->
<!-- The focus and key handlers take events bubbling from the field inside, which is the
interactive element; the group itself is never focused. -->
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
<!-- `data-composer-open`: while the reader is in Home's composer, Home keeps it at the top
and quiets its cards. -->
<div
data-session-composer
data-composer-open={starterLayout && composerOpened ? '' : undefined}
data-composer-flash={flashing ? '' : undefined}
role="group"
aria-label="Message composer"
bind:this={composerEl}
class="relative {columnClass} pb-2"
onanimationend={(e) => {
if (e.animationName === 'wm-composer-flash') flashing = false
}}
onfocusin={(e) => {
if (starterLayout && e.target instanceof HTMLTextAreaElement) composerOpened = true
}}
onkeydown={(e) => {
// A prevented Escape closed something inside first, such as the @-mention picker.
if (e.key !== 'Escape' || e.defaultPrevented) return
if (!starterLayout || !composerOpened) return
closeComposer()
if (aiChatInput?.isDraftEmpty() ?? true) (document.activeElement as HTMLElement)?.blur()
}}
>
{#if showFlowPendingActionControls}
<div class="absolute -top-10 w-full flex flex-row justify-center gap-2">
<Button
startIcon={{ icon: CheckIcon }}
unifiedSize="sm"
variant="default"
btnClasses="bg-green-500 hover:bg-green-600 text-white hover:text-white"
onclick={() => {
chatHost.flowAiChatHelpers?.acceptAllModuleActions()
}}
>
Accept all
</Button>
<div class="rounded bg-surface">
<Button
startIcon={{ icon: XIcon }}
unifiedSize="sm"
variant="default"
btnClasses="dark:opacity-50 opacity-60 hover:opacity-100"
onclick={() => {
chatHost.flowAiChatHelpers?.rejectAllModuleActions()
}}
>
Reject all
</Button>
</div>
</div>
{/if}
<div>
<QueuedMessageChip />
{#if chatHost.mode === AIMode.GLOBAL && !chatHost.isSessionChat}
<!-- Standalone Jobs bar for the global side-panel chat. In /sessions the
Jobs segment lives inside the session bar (SessionChangesBar). -->
<div class="mb-1">
<JobsSegment standalone />
</div>
{/if}
<!-- Message-scoped chips (selected-context / DOM-selector / images) render
inside the input box via AIChatInput → ContextTextarea's `leading` snippet;
selected context also appears as @mentions in the input (deleting the
mention deselects). Hence showContext={false} below. Session-scoped
assets (attached files/folders) render in the footer row instead. -->
{#if inputPreface}
{@render inputPreface()}
{/if}
{#if showFreeTierUsage && !starterLayout}
{@render freeTierUsageBanner()}
{/if}
<AIChatInput
bind:this={aiChatInput}
bind:selectedContext
{availableContext}
placeholder={starterLayout
? heldInUse
? USE_STARTER_PLACEHOLDER
: STARTER_PLACEHOLDER
: placeholder}
{initialInstructions}
{onDraftChange}
{initialAttachments}
{onAttachmentsChange}
showContext={chatHost.mode !== AIMode.GLOBAL}
{disabled}
{pendingQuestionToolCallId}
isFirstMessage={messages.length === 0}
hero={starterLayout}
collapsed={composerCollapsed}
chatIcon={starterLayout}
{onSent}
>
{#snippet collapsedControls()}
{#if chatHost.mode === AIMode.GLOBAL}
<WorkingInPicker ws={aiChatManager.operatingWorkspace ?? ''} onlyWhenScoped />
{/if}
{#if chatHost.supportsModelSettings}
<AIChatModelSettings promptSettings={false} />
{/if}
{/snippet}
{#snippet toolbar()}
<div
class="flex flex-row flex-wrap items-center gap-x-1.5 gap-y-1"
class:justify-between={showFooterLeftControls}
class:justify-end={!showFooterLeftControls}
>
{#if showFooterLeftControls}
<div class="flex flex-row items-center gap-x-1.5 min-w-0 flex-wrap">
{#if showContextPicker && !disabled}
<Popover placement="bottom-start">
{#snippet trigger()}
<Button
nonCaptureEvent
unifiedSize="2xs"
variant="default"
title="Add context"
iconOnly
startIcon={{ icon: AtSign }}
/>
{/snippet}
{#snippet content({ close })}
{#if chatHost.mode === AIMode.APP}
<AppAvailableContextList
{availableContext}
{selectedContext}
onSelect={(element) => {
void aiChatInput?.addContextToSelection(element)
close()
}}
/>
{:else}
<ChatContextPicker
{availableContext}
{selectedContext}
onSelect={(element) => {
void mentionWorkspaceItem(element)
close()
aiChatInput?.focusInput()
}}
onSelectWorkspaceItem={(element) => {
void mentionWorkspaceItem(element)
close()
aiChatInput?.focusInput()
}}
setShowing={(showing) => {
if (!showing) close()
}}
onSelectFile={(name) => {
aiChatInput?.insertFileMention(name)
close()
}}
/>
{/if}
{/snippet}
</Popover>
{/if}
{#if attachmentsOffReason}
<Tooltip small placement="top">
<Button
nonCaptureEvent
unifiedSize="2xs"
variant="subtle"
iconOnly
disabled
startIcon={{ icon: Plus }}
/>
{#snippet text()}
<div class="max-w-64 text-xs">{attachmentsOffReason}</div>
{/snippet}
</Tooltip>
{:else if canAttachFiles}
<DropdownV2
items={() => {
const closeMenu = () => (plusMenuOpen = false)
const inGlobal = chatHost.mode === AIMode.GLOBAL
if (inGlobal) {
skillsMenu.refresh()
mcpMenu.refresh()
}
return [
{
displayName: 'Attach file or image',
icon: FileText,
action: () => {
plusMenuOpen = false
linkFiles()
}
},
...(canLinkFolders
? [
{
// A real (live) link needs the File System Access API; without it the
// folder is only snapshotted, so call it "Add folder", not "Link folder".
displayName: canUseFsAccess ? 'Link folder' : 'Add folder',
icon: Folder,
tooltip: canUseFsAccess
? 'Linked live — the assistant reads the folder’s current files from disk and refreshes each turn.'
: 'Loaded as a snapshot — the folder’s files are copied into your browser (they won’t auto-update). For a live link that refreshes from disk, use a Chromium-based browser (Chrome, Edge).',
action: () => {
plusMenuOpen = false
linkFolder()
}
}
]
: []),
...(inGlobal
? [
{
displayName: 'Mention file',
icon: AtSign,
customSubmenu: WorkspaceMentionPicker,
customSubmenuProps: {
onSelect: mentionWorkspaceItem,
onAfterClose: () => aiChatInput?.focusInput()
}
}
]
: []),
...(inGlobal
? [
{
displayName: 'Skills',
icon: BookOpen,
separatorTop: true,
// Getters so the submenus fill in when their fetch lands
// rather than the whole menu waiting on it.
get submenuItems() {
return skillsMenu.items(closeMenu)
}
},
{
displayName: 'MCP connections',
icon: Plug,
get submenuItems() {
return mcpMenu.items(closeMenu)
}
}
]
: [])
]
}}
placement="bottom-start"
fixedHeight={false}
closeOnItemClick={false}
bind:open={plusMenuOpen}
>
{#snippet buttonReplacement()}
<Tooltip small placement="top">
<Button
nonCaptureEvent
unifiedSize="2xs"
variant="subtle"
iconOnly
startIcon={{ icon: Plus }}
/>
{#snippet text()}
<div class="max-w-64 text-xs">
<p class="font-semibold">Attach files or link a folder</p>
<p class="mt-1">
Files and images attach to your next message. Images are seen
directly; file contents stay in your browser and are read on demand.
</p>
<p class="mt-1">
A linked folder is a session-wide resource: the assistant lists,
searches, and reads its files whenever it needs them.
</p>
</div>
{/snippet}
</Tooltip>
{/snippet}
</DropdownV2>
<!-- Fallback file picker (used when the File System Access API is unavailable).
`accept` only steers the picker; the content sniff at attach is authoritative. -->
<input
bind:this={fileInputEl}
type="file"
multiple
accept={chatHost.attachmentAccept}
class="hidden no-default-style"
onchange={onFileInputChange}
/>
<!-- Fallback folder picker (no File System Access API): webkitdirectory selects a
whole folder; its files carry webkitRelativePath and are snapshotted. -->
<input
bind:this={folderInputEl}
type="file"
multiple
webkitdirectory
class="hidden no-default-style"
onchange={onFolderInputChange}
/>
{/if}
{#if showAutonomyModeSelector}
<AutonomyModePicker
mode={chatHost.autonomyMode}
availability={autonomyAvailability}
onChange={chatHost.setAutonomyMode}
work={chatHost.workModesAvailable
? {
mode: chatHost.workMode,
planAvailable: chatHost.planModeAvailable,
lock: workModeLock,
onChange: chatHost.setWorkMode
}
: undefined}
/>
{/if}
{#if effectiveAutonomyMode === AIAutonomyMode.YOLO && chatHost.autoAcceptToolConfirmationsAvailable}
<Tooltip small placement="top">
<AlertTriangle class="w-3 h-3 text-red-500" />
{#snippet text()}
<div class="max-w-64 text-xs">
<p class="font-semibold">
{chatHost.autoAcceptEditsAvailable
? 'Bypass permissions auto-accepts edits and tool usage.'
: 'Bypass permissions auto-accepts tool usage.'}
</p>
<p class="mt-1">
{chatHost.autoAcceptEditsAvailable
? 'This can result in edits being applied or tools being called without user confirmation.'
: 'This can result in tools being called without user confirmation.'}
</p>
{#if yoloBypassedTools.length > 0}
<p class="mt-2 font-semibold">Bypassed in current mode:</p>
<ul class="mt-1 list-disc pl-4 space-y-0.5">
{#each visibleYoloBypassedTools as tool (tool.name)}
<li class="break-words">{tool.label}</li>
{/each}
</ul>
{#if hiddenYoloBypassedToolCount > 0}
<p class="mt-1">+ {hiddenYoloBypassedToolCount} more</p>
{/if}
{:else}
<p class="mt-2">No tools in the current mode require confirmation.</p>
{/if}
</div>
{/snippet}
</Tooltip>
{/if}
{#if chatHost.mode === AIMode.GLOBAL}
<WorkingInPicker ws={aiChatManager.operatingWorkspace ?? ''} />
{/if}
{#if chatHost.mode === AIMode.SCRIPT && hasDiff && !disabled}
<ChatQuickActions {askAi} {diffMode} />
{/if}
</div>
{/if}
{#if footerMessageShown}
<div class="text-primary text-xs my-2 px-2">
<Markdown md={disabledMessage} />
</div>
{:else}
<div class="flex flex-row gap-x-1.5 min-w-0 flex-wrap items-center">
{#if chatHost.mode === AIMode.GLOBAL}
<AttachedFilesBar />
{/if}
{#if !hideModeSelector}
<ChatMode />
{/if}
{#if chatHost.mode === AIMode.APP}
<DatatableCreationPolicy />
{/if}
<ContextUsageIndicator />
{#if chatHost.supportsModelSettings}
<!-- `promptSettings={false}`: in a session, GLOBAL, the settings modal's
Instructions section owns the prompt entries. -->
<AIChatModelSettings promptSettings={false} />
{/if}
{@render footerSettings?.()}
{#if chatHost.mode === AIMode.GLOBAL}
<AssistantSettingsModal bind:this={assistantSettings} />
{/if}
{#if chatHost.mode === AIMode.APP && appContext && (appContext.inspectorElement || appContext.codeSelection)}
{#if appContext.inspectorElement}
<div
class="inline-flex items-center gap-1 px-1.5 py-0.5 rounded bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-300 text-2xs"
title={appContext.inspectorElement.path}
>
<MousePointer2 class="w-3 h-3" />
<span class="truncate max-w-[60px]">
{appContext.inspectorElement.tagName.toLowerCase()}{appContext
.inspectorElement.id
? `#${appContext.inspectorElement.id}`
: ''}{appContext.inspectorElement.className
? `.${appContext.inspectorElement.className.split(' ')[0]}`
: ''}
</span>
<button
class="hover:bg-purple-200 dark:hover:bg-purple-800/50 rounded p-0.5 -mr-0.5"
onclick={() => appContext.clearInspector?.()}
title="Clear element selection"
>
<X class="w-2.5 h-2.5" />
</button>
</div>
{/if}
{#if appContext.codeSelection}
<div
class="inline-flex items-center gap-1 px-1.5 py-0.5 rounded bg-amber-100 dark:bg-amber-900/30 text-amber-700 dark:text-amber-300 text-2xs"
title={`${appContext.codeSelection.source}: lines ${appContext.codeSelection.startLine}-${appContext.codeSelection.endLine}`}
>
<TextSelect class="w-3 h-3" />
<span class="truncate max-w-[80px]">
L{appContext.codeSelection.startLine}-{appContext.codeSelection.endLine}
</span>
<button
class="hover:bg-amber-200 dark:hover:bg-amber-800/50 rounded p-0.5 -mr-0.5"
onclick={() => appContext.clearCodeSelection?.()}
title="Clear code selection"
>
<X class="w-2.5 h-2.5" />
</button>
</div>
{/if}
{/if}
</div>
{/if}
</div>
{/snippet}
</AIChatInput>
</div>
<!-- Opens and closes with the box, so the page under it slides rather than jumps. -->
<div use:smoothHeight={{ enabled: starterLayout, key: composerCollapsed }}>
<div>
{#if starterLayout && starter && !disabled && !composerCollapsed && starter.suggestions.length > 0}
<div class="pt-2">
<ComposerSuggestions
suggestions={starter.suggestions}
onPick={(prompt) => aiChatInput?.setText(prompt)}
/>
</div>
{/if}
</div>
</div>
{#if (chatHost.mode === AIMode.NAVIGATOR || chatHost.mode === AIMode.ASK) && suggestions.length > 0 && messages.filter((m) => m.role === 'user').length === 0 && !disabled}
<div class="px-2 mt-4">
<div class="flex flex-col gap-2">
{#each suggestions as suggestion (suggestion)}
<Button
on:click={() => submitSuggestion(suggestion)}
variant="subtle"
size="xs2"
btnClasses="whitespace-normal text-center font-normal"
>
{suggestion}
</Button>
{/each}
</div>
</div>
{/if}
</div>
</div>