| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724 |
- import {
- createContext,
- createEffect,
- createMemo,
- createSignal,
- For,
- Match,
- on,
- Show,
- Switch,
- useContext,
- type Component,
- } from "solid-js"
- import { Dynamic } from "solid-js/web"
- import path from "path"
- import { useRoute, useRouteData } from "@tui/context/route"
- import { useSync } from "@tui/context/sync"
- import { SplitBorder } from "@tui/component/border"
- import { useTheme } from "@tui/context/theme"
- import {
- BoxRenderable,
- ScrollBoxRenderable,
- addDefaultParsers,
- MacOSScrollAccel,
- type ScrollAcceleration,
- } from "@opentui/core"
- import { Prompt, type PromptRef } from "@tui/component/prompt"
- import type { AssistantMessage, Part, ToolPart, UserMessage, TextPart, ReasoningPart } from "@opencode-ai/sdk/v2"
- import { useLocal } from "@tui/context/local"
- import { Locale } from "@/util/locale"
- import type { Tool } from "@/tool/tool"
- import type { ReadTool } from "@/tool/read"
- import type { WriteTool } from "@/tool/write"
- import { BashTool } from "@/tool/bash"
- import type { GlobTool } from "@/tool/glob"
- import { TodoWriteTool } from "@/tool/todo"
- import type { GrepTool } from "@/tool/grep"
- import type { ListTool } from "@/tool/ls"
- import type { EditTool } from "@/tool/edit"
- import type { PatchTool } from "@/tool/patch"
- import type { WebFetchTool } from "@/tool/webfetch"
- import type { TaskTool } from "@/tool/task"
- import { useKeyboard, useRenderer, useTerminalDimensions, type BoxProps, type JSX } from "@opentui/solid"
- import { useSDK } from "@tui/context/sdk"
- import { useCommandDialog } from "@tui/component/dialog-command"
- import { useKeybind } from "@tui/context/keybind"
- import { Header } from "./header"
- import { parsePatch } from "diff"
- import { useDialog } from "../../ui/dialog"
- import { DialogMessage } from "./dialog-message"
- import type { PromptInfo } from "../../component/prompt/history"
- import { iife } from "@/util/iife"
- import { DialogConfirm } from "@tui/ui/dialog-confirm"
- import { DialogPrompt } from "@tui/ui/dialog-prompt"
- import { DialogTimeline } from "./dialog-timeline"
- import { DialogSessionRename } from "../../component/dialog-session-rename"
- import { Sidebar } from "./sidebar"
- import { LANGUAGE_EXTENSIONS } from "@/lsp/language"
- import parsers from "../../../../../../parsers-config.ts"
- import { Clipboard } from "../../util/clipboard"
- import { Toast, useToast } from "../../ui/toast"
- import { useKV } from "../../context/kv.tsx"
- import { Editor } from "../../util/editor"
- import stripAnsi from "strip-ansi"
- import { Footer } from "./footer.tsx"
- import { usePromptRef } from "../../context/prompt"
- import { Filesystem } from "@/util/filesystem"
- addDefaultParsers(parsers.parsers)
- class CustomSpeedScroll implements ScrollAcceleration {
- constructor(private speed: number) {}
- tick(_now?: number): number {
- return this.speed
- }
- reset(): void {}
- }
- const context = createContext<{
- width: number
- conceal: () => boolean
- showThinking: () => boolean
- showTimestamps: () => boolean
- usernameVisible: () => boolean
- showDetails: () => boolean
- diffWrapMode: () => "word" | "none"
- sync: ReturnType<typeof useSync>
- }>()
- function use() {
- const ctx = useContext(context)
- if (!ctx) throw new Error("useContext must be used within a Session component")
- return ctx
- }
- export function Session() {
- const route = useRouteData("session")
- const { navigate } = useRoute()
- const sync = useSync()
- const kv = useKV()
- const { theme } = useTheme()
- const promptRef = usePromptRef()
- const session = createMemo(() => sync.session.get(route.sessionID)!)
- const messages = createMemo(() => sync.data.message[route.sessionID] ?? [])
- const permissions = createMemo(() => sync.data.permission[route.sessionID] ?? [])
- const pending = createMemo(() => {
- return messages().findLast((x) => x.role === "assistant" && !x.time.completed)?.id
- })
- const lastAssistant = createMemo(() => {
- return messages().findLast((x) => x.role === "assistant")
- })
- const dimensions = useTerminalDimensions()
- const [sidebar, setSidebar] = createSignal<"show" | "hide" | "auto">(kv.get("sidebar", "auto"))
- const [conceal, setConceal] = createSignal(true)
- const [showThinking, setShowThinking] = createSignal(kv.get("thinking_visibility", true))
- const [showTimestamps, setShowTimestamps] = createSignal(kv.get("timestamps", "hide") === "show")
- const [usernameVisible, setUsernameVisible] = createSignal(kv.get("username_visible", true))
- const [showDetails, setShowDetails] = createSignal(kv.get("tool_details_visibility", true))
- const [showScrollbar, setShowScrollbar] = createSignal(kv.get("scrollbar_visible", false))
- const [diffWrapMode, setDiffWrapMode] = createSignal<"word" | "none">("word")
- const wide = createMemo(() => dimensions().width > 120)
- const sidebarVisible = createMemo(() => {
- if (session()?.parentID) return false
- if (sidebar() === "show") return true
- if (sidebar() === "auto" && wide()) return true
- return false
- })
- const contentWidth = createMemo(() => dimensions().width - (sidebarVisible() ? 42 : 0) - 4)
- const scrollAcceleration = createMemo(() => {
- const tui = sync.data.config.tui
- if (tui?.scroll_acceleration?.enabled) {
- return new MacOSScrollAccel()
- }
- if (tui?.scroll_speed) {
- return new CustomSpeedScroll(tui.scroll_speed)
- }
- return new CustomSpeedScroll(3)
- })
- createEffect(async () => {
- await sync.session
- .sync(route.sessionID)
- .then(() => {
- if (scroll) scroll.scrollBy(100_000)
- })
- .catch((e) => {
- console.error(e)
- toast.show({
- message: `Session not found: ${route.sessionID}`,
- variant: "error",
- })
- return navigate({ type: "home" })
- })
- })
- const toast = useToast()
- const sdk = useSDK()
- // Auto-navigate to whichever session currently needs permission input
- createEffect(() => {
- const currentSession = session()
- if (!currentSession) return
- const currentPermissions = permissions()
- let targetID = currentPermissions.length > 0 ? currentSession.id : undefined
- if (!targetID) {
- const child = sync.data.session.find(
- (x) => x.parentID === currentSession.id && (sync.data.permission[x.id]?.length ?? 0) > 0,
- )
- if (child) targetID = child.id
- }
- if (targetID && targetID !== currentSession.id) {
- navigate({
- type: "session",
- sessionID: targetID,
- })
- }
- })
- let scroll: ScrollBoxRenderable
- let prompt: PromptRef
- const keybind = useKeybind()
- useKeyboard((evt) => {
- if (dialog.stack.length > 0) return
- const first = permissions()[0]
- if (first) {
- const response = iife(() => {
- if (evt.ctrl || evt.meta) return
- if (evt.name === "return") return "once"
- if (evt.name === "a") return "always"
- if (evt.name === "d") return "reject"
- if (evt.name === "escape") return "reject"
- return
- })
- if (response) {
- sdk.client.permission.respond({
- permissionID: first.id,
- sessionID: route.sessionID,
- response: response,
- })
- }
- }
- })
- function toBottom() {
- setTimeout(() => {
- if (scroll) scroll.scrollTo(scroll.scrollHeight)
- }, 50)
- }
- const local = useLocal()
- function moveChild(direction: number) {
- const parentID = session()?.parentID ?? session()?.id
- let children = sync.data.session
- .filter((x) => x.parentID === parentID || x.id === parentID)
- .toSorted((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
- if (children.length === 1) return
- let next = children.findIndex((x) => x.id === session()?.id) + direction
- if (next >= children.length) next = 0
- if (next < 0) next = children.length - 1
- if (children[next]) {
- navigate({
- type: "session",
- sessionID: children[next].id,
- })
- }
- }
- const command = useCommandDialog()
- command.register(() => [
- ...(sync.data.config.share !== "disabled"
- ? [
- {
- title: "Share session",
- value: "session.share",
- suggested: route.type === "session",
- keybind: "session_share" as const,
- disabled: !!session()?.share?.url,
- category: "Session",
- onSelect: async (dialog: any) => {
- await sdk.client.session
- .share({
- sessionID: route.sessionID,
- })
- .then((res) =>
- Clipboard.copy(res.data!.share!.url).catch(() =>
- toast.show({ message: "Failed to copy URL to clipboard", variant: "error" }),
- ),
- )
- .then(() => toast.show({ message: "Share URL copied to clipboard!", variant: "success" }))
- .catch(() => toast.show({ message: "Failed to share session", variant: "error" }))
- dialog.clear()
- },
- },
- ]
- : []),
- {
- title: "Rename session",
- value: "session.rename",
- keybind: "session_rename",
- category: "Session",
- onSelect: (dialog) => {
- dialog.replace(() => <DialogSessionRename session={route.sessionID} />)
- },
- },
- {
- title: "Jump to message",
- value: "session.timeline",
- keybind: "session_timeline",
- category: "Session",
- onSelect: (dialog) => {
- dialog.replace(() => (
- <DialogTimeline
- onMove={(messageID) => {
- const child = scroll.getChildren().find((child) => {
- return child.id === messageID
- })
- if (child) scroll.scrollBy(child.y - scroll.y - 1)
- }}
- sessionID={route.sessionID}
- setPrompt={(promptInfo) => prompt.set(promptInfo)}
- />
- ))
- },
- },
- {
- title: "Compact session",
- value: "session.compact",
- keybind: "session_compact",
- category: "Session",
- onSelect: (dialog) => {
- const selectedModel = local.model.current()
- if (!selectedModel) {
- toast.show({
- variant: "warning",
- message: "Connect a provider to summarize this session",
- duration: 3000,
- })
- return
- }
- sdk.client.session.summarize({
- sessionID: route.sessionID,
- modelID: selectedModel.modelID,
- providerID: selectedModel.providerID,
- })
- dialog.clear()
- },
- },
- {
- title: "Unshare session",
- value: "session.unshare",
- keybind: "session_unshare",
- disabled: !session()?.share?.url,
- category: "Session",
- onSelect: async (dialog) => {
- await sdk.client.session
- .unshare({
- sessionID: route.sessionID,
- })
- .then(() => toast.show({ message: "Session unshared successfully", variant: "success" }))
- .catch(() => toast.show({ message: "Failed to unshare session", variant: "error" }))
- dialog.clear()
- },
- },
- {
- title: "Undo previous message",
- value: "session.undo",
- keybind: "messages_undo",
- category: "Session",
- onSelect: async (dialog) => {
- const status = sync.data.session_status?.[route.sessionID]
- if (status?.type !== "idle") await sdk.client.session.abort({ sessionID: route.sessionID }).catch(() => {})
- const revert = session().revert?.messageID
- const message = messages().findLast((x) => (!revert || x.id < revert) && x.role === "user")
- if (!message) return
- sdk.client.session
- .revert({
- sessionID: route.sessionID,
- messageID: message.id,
- })
- .then(() => {
- toBottom()
- })
- const parts = sync.data.part[message.id]
- prompt.set(
- parts.reduce(
- (agg, part) => {
- if (part.type === "text") {
- if (!part.synthetic) agg.input += part.text
- }
- if (part.type === "file") agg.parts.push(part)
- return agg
- },
- { input: "", parts: [] as PromptInfo["parts"] },
- ),
- )
- dialog.clear()
- },
- },
- {
- title: "Redo",
- value: "session.redo",
- keybind: "messages_redo",
- disabled: !session()?.revert?.messageID,
- category: "Session",
- onSelect: (dialog) => {
- dialog.clear()
- const messageID = session().revert?.messageID
- if (!messageID) return
- const message = messages().find((x) => x.role === "user" && x.id > messageID)
- if (!message) {
- sdk.client.session.unrevert({
- sessionID: route.sessionID,
- })
- prompt.set({ input: "", parts: [] })
- return
- }
- sdk.client.session.revert({
- sessionID: route.sessionID,
- messageID: message.id,
- })
- },
- },
- {
- title: sidebarVisible() ? "Hide sidebar" : "Show sidebar",
- value: "session.sidebar.toggle",
- keybind: "sidebar_toggle",
- category: "Session",
- onSelect: (dialog) => {
- setSidebar((prev) => {
- if (prev === "auto") return sidebarVisible() ? "hide" : "show"
- if (prev === "show") return "hide"
- return "show"
- })
- if (sidebar() === "show") kv.set("sidebar", "auto")
- if (sidebar() === "hide") kv.set("sidebar", "hide")
- dialog.clear()
- },
- },
- {
- title: usernameVisible() ? "Hide username" : "Show username",
- value: "session.username_visible.toggle",
- keybind: "username_toggle",
- category: "Session",
- onSelect: (dialog) => {
- setUsernameVisible((prev) => {
- const next = !prev
- kv.set("username_visible", next)
- return next
- })
- dialog.clear()
- },
- },
- {
- title: "Toggle code concealment",
- value: "session.toggle.conceal",
- keybind: "messages_toggle_conceal" as any,
- category: "Session",
- onSelect: (dialog) => {
- setConceal((prev) => !prev)
- dialog.clear()
- },
- },
- {
- title: showTimestamps() ? "Hide timestamps" : "Show timestamps",
- value: "session.toggle.timestamps",
- category: "Session",
- onSelect: (dialog) => {
- setShowTimestamps((prev) => {
- const next = !prev
- kv.set("timestamps", next ? "show" : "hide")
- return next
- })
- dialog.clear()
- },
- },
- {
- title: showThinking() ? "Hide thinking" : "Show thinking",
- value: "session.toggle.thinking",
- category: "Session",
- onSelect: (dialog) => {
- setShowThinking((prev) => {
- const next = !prev
- kv.set("thinking_visibility", next)
- return next
- })
- dialog.clear()
- },
- },
- {
- title: "Toggle diff wrapping",
- value: "session.toggle.diffwrap",
- category: "Session",
- onSelect: (dialog) => {
- setDiffWrapMode((prev) => (prev === "word" ? "none" : "word"))
- dialog.clear()
- },
- },
- {
- title: showDetails() ? "Hide tool details" : "Show tool details",
- value: "session.toggle.actions",
- keybind: "tool_details",
- category: "Session",
- onSelect: (dialog) => {
- const newValue = !showDetails()
- setShowDetails(newValue)
- kv.set("tool_details_visibility", newValue)
- dialog.clear()
- },
- },
- {
- title: "Toggle session scrollbar",
- value: "session.toggle.scrollbar",
- keybind: "scrollbar_toggle",
- category: "Session",
- onSelect: (dialog) => {
- setShowScrollbar((prev) => {
- const next = !prev
- kv.set("scrollbar_visible", next)
- return next
- })
- dialog.clear()
- },
- },
- {
- title: "Page up",
- value: "session.page.up",
- keybind: "messages_page_up",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- scroll.scrollBy(-scroll.height / 2)
- dialog.clear()
- },
- },
- {
- title: "Page down",
- value: "session.page.down",
- keybind: "messages_page_down",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- scroll.scrollBy(scroll.height / 2)
- dialog.clear()
- },
- },
- {
- title: "Half page up",
- value: "session.half.page.up",
- keybind: "messages_half_page_up",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- scroll.scrollBy(-scroll.height / 4)
- dialog.clear()
- },
- },
- {
- title: "Half page down",
- value: "session.half.page.down",
- keybind: "messages_half_page_down",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- scroll.scrollBy(scroll.height / 4)
- dialog.clear()
- },
- },
- {
- title: "First message",
- value: "session.first",
- keybind: "messages_first",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- scroll.scrollTo(0)
- dialog.clear()
- },
- },
- {
- title: "Last message",
- value: "session.last",
- keybind: "messages_last",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- scroll.scrollTo(scroll.scrollHeight)
- dialog.clear()
- },
- },
- {
- title: "Jump to last user message",
- value: "session.messages_last_user",
- keybind: "messages_last_user",
- category: "Session",
- onSelect: () => {
- const messages = sync.data.message[route.sessionID]
- if (!messages || !messages.length) return
- // Find the most recent user message with non-ignored, non-synthetic text parts
- for (let i = messages.length - 1; i >= 0; i--) {
- const message = messages[i]
- if (!message || message.role !== "user") continue
- const parts = sync.data.part[message.id]
- if (!parts || !Array.isArray(parts)) continue
- const hasValidTextPart = parts.some(
- (part) => part && part.type === "text" && !part.synthetic && !part.ignored,
- )
- if (hasValidTextPart) {
- const child = scroll.getChildren().find((child) => {
- return child.id === message.id
- })
- if (child) scroll.scrollBy(child.y - scroll.y - 1)
- break
- }
- }
- },
- },
- {
- title: "Copy last assistant message",
- value: "messages.copy",
- keybind: "messages_copy",
- category: "Session",
- onSelect: (dialog) => {
- const lastAssistantMessage = messages().findLast((msg) => msg.role === "assistant")
- if (!lastAssistantMessage) {
- toast.show({ message: "No assistant messages found", variant: "error" })
- dialog.clear()
- return
- }
- const parts = sync.data.part[lastAssistantMessage.id] ?? []
- const textParts = parts.filter((part) => part.type === "text")
- if (textParts.length === 0) {
- toast.show({ message: "No text parts found in last assistant message", variant: "error" })
- dialog.clear()
- return
- }
- const text = textParts
- .map((part) => part.text)
- .join("\n")
- .trim()
- if (!text) {
- toast.show({
- message: "No text content found in last assistant message",
- variant: "error",
- })
- dialog.clear()
- return
- }
- const base64 = Buffer.from(text).toString("base64")
- const osc52 = `\x1b]52;c;${base64}\x07`
- const finalOsc52 = process.env["TMUX"] ? `\x1bPtmux;\x1b${osc52}\x1b\\` : osc52
- /* @ts-expect-error */
- renderer.writeOut(finalOsc52)
- Clipboard.copy(text)
- .then(() => toast.show({ message: "Message copied to clipboard!", variant: "success" }))
- .catch(() => toast.show({ message: "Failed to copy to clipboard", variant: "error" }))
- dialog.clear()
- },
- },
- {
- title: "Copy session transcript",
- value: "session.copy",
- keybind: "session_copy",
- category: "Session",
- onSelect: async (dialog) => {
- try {
- // Format session transcript as markdown
- const sessionData = session()
- const sessionMessages = messages()
- let transcript = `# ${sessionData.title}\n\n`
- transcript += `**Session ID:** ${sessionData.id}\n`
- transcript += `**Created:** ${new Date(sessionData.time.created).toLocaleString()}\n`
- transcript += `**Updated:** ${new Date(sessionData.time.updated).toLocaleString()}\n\n`
- transcript += `---\n\n`
- for (const msg of sessionMessages) {
- const parts = sync.data.part[msg.id] ?? []
- const role = msg.role === "user" ? "User" : "Assistant"
- transcript += `## ${role}\n\n`
- for (const part of parts) {
- if (part.type === "text" && !part.synthetic) {
- transcript += `${part.text}\n\n`
- } else if (part.type === "tool") {
- transcript += `\`\`\`\nTool: ${part.tool}\n\`\`\`\n\n`
- }
- }
- transcript += `---\n\n`
- }
- // Copy to clipboard
- await Clipboard.copy(transcript)
- toast.show({ message: "Session transcript copied to clipboard!", variant: "success" })
- } catch (error) {
- toast.show({ message: "Failed to copy session transcript", variant: "error" })
- }
- dialog.clear()
- },
- },
- {
- title: "Export session transcript to file",
- value: "session.export",
- keybind: "session_export",
- category: "Session",
- onSelect: async (dialog) => {
- try {
- // Format session transcript as markdown
- const sessionData = session()
- const sessionMessages = messages()
- let transcript = `# ${sessionData.title}\n\n`
- transcript += `**Session ID:** ${sessionData.id}\n`
- transcript += `**Created:** ${new Date(sessionData.time.created).toLocaleString()}\n`
- transcript += `**Updated:** ${new Date(sessionData.time.updated).toLocaleString()}\n\n`
- transcript += `---\n\n`
- for (const msg of sessionMessages) {
- const parts = sync.data.part[msg.id] ?? []
- const role = msg.role === "user" ? "User" : "Assistant"
- transcript += `## ${role}\n\n`
- for (const part of parts) {
- if (part.type === "text" && !part.synthetic) {
- transcript += `${part.text}\n\n`
- } else if (part.type === "tool") {
- transcript += `\`\`\`\nTool: ${part.tool}\n\`\`\`\n\n`
- }
- }
- transcript += `---\n\n`
- }
- // Prompt for optional filename
- const customFilename = await DialogPrompt.show(dialog, "Export filename", {
- value: `session-${sessionData.id.slice(0, 8)}.md`,
- })
- // Cancel if user pressed escape
- if (customFilename === null) return
- // Save to file in current working directory
- const exportDir = process.cwd()
- const filename = customFilename.trim()
- const filepath = path.join(exportDir, filename)
- await Bun.write(filepath, transcript)
- // Open with EDITOR if available
- const result = await Editor.open({ value: transcript, renderer })
- if (result !== undefined) {
- // User edited the file, save the changes
- await Bun.write(filepath, result)
- }
- toast.show({ message: `Session exported to ${filename}`, variant: "success" })
- } catch (error) {
- toast.show({ message: "Failed to export session", variant: "error" })
- }
- dialog.clear()
- },
- },
- {
- title: "Next child session",
- value: "session.child.next",
- keybind: "session_child_cycle",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- moveChild(1)
- dialog.clear()
- },
- },
- {
- title: "Previous child session",
- value: "session.child.previous",
- keybind: "session_child_cycle_reverse",
- category: "Session",
- disabled: true,
- onSelect: (dialog) => {
- moveChild(-1)
- dialog.clear()
- },
- },
- ])
- const revertInfo = createMemo(() => session()?.revert)
- const revertMessageID = createMemo(() => revertInfo()?.messageID)
- const revertDiffFiles = createMemo(() => {
- const diffText = revertInfo()?.diff ?? ""
- if (!diffText) return []
- try {
- const patches = parsePatch(diffText)
- return patches.map((patch) => {
- const filename = patch.newFileName || patch.oldFileName || "unknown"
- const cleanFilename = filename.replace(/^[ab]\//, "")
- return {
- filename: cleanFilename,
- additions: patch.hunks.reduce(
- (sum, hunk) => sum + hunk.lines.filter((line) => line.startsWith("+")).length,
- 0,
- ),
- deletions: patch.hunks.reduce(
- (sum, hunk) => sum + hunk.lines.filter((line) => line.startsWith("-")).length,
- 0,
- ),
- }
- })
- } catch (error) {
- return []
- }
- })
- const revertRevertedMessages = createMemo(() => {
- const messageID = revertMessageID()
- if (!messageID) return []
- return messages().filter((x) => x.id >= messageID && x.role === "user")
- })
- const revert = createMemo(() => {
- const info = revertInfo()
- if (!info) return
- if (!info.messageID) return
- return {
- messageID: info.messageID,
- reverted: revertRevertedMessages(),
- diff: info.diff,
- diffFiles: revertDiffFiles(),
- }
- })
- const dialog = useDialog()
- const renderer = useRenderer()
- // snap to bottom when session changes
- createEffect(on(() => route.sessionID, toBottom))
- return (
- <context.Provider
- value={{
- get width() {
- return contentWidth()
- },
- conceal,
- showThinking,
- showTimestamps,
- usernameVisible,
- showDetails,
- diffWrapMode,
- sync,
- }}
- >
- <box flexDirection="row">
- <box flexGrow={1} paddingBottom={1} paddingTop={1} paddingLeft={2} paddingRight={2} gap={1}>
- <Show when={session()}>
- <Show when={!sidebarVisible()}>
- <Header />
- </Show>
- <scrollbox
- ref={(r) => (scroll = r)}
- viewportOptions={{
- paddingRight: showScrollbar() ? 1 : 0,
- }}
- verticalScrollbarOptions={{
- paddingLeft: 1,
- visible: showScrollbar(),
- trackOptions: {
- backgroundColor: theme.backgroundElement,
- foregroundColor: theme.border,
- },
- }}
- stickyScroll={true}
- stickyStart="bottom"
- flexGrow={1}
- scrollAcceleration={scrollAcceleration()}
- >
- <For each={messages()}>
- {(message, index) => (
- <Switch>
- <Match when={message.id === revert()?.messageID}>
- {(function () {
- const command = useCommandDialog()
- const [hover, setHover] = createSignal(false)
- const dialog = useDialog()
- const handleUnrevert = async () => {
- const confirmed = await DialogConfirm.show(
- dialog,
- "Confirm Redo",
- "Are you sure you want to restore the reverted messages?",
- )
- if (confirmed) {
- command.trigger("session.redo")
- }
- }
- return (
- <box
- onMouseOver={() => setHover(true)}
- onMouseOut={() => setHover(false)}
- onMouseUp={handleUnrevert}
- marginTop={1}
- flexShrink={0}
- border={["left"]}
- customBorderChars={SplitBorder.customBorderChars}
- borderColor={theme.backgroundPanel}
- >
- <box
- paddingTop={1}
- paddingBottom={1}
- paddingLeft={2}
- backgroundColor={hover() ? theme.backgroundElement : theme.backgroundPanel}
- >
- <text fg={theme.textMuted}>{revert()!.reverted.length} message reverted</text>
- <text fg={theme.textMuted}>
- <span style={{ fg: theme.text }}>{keybind.print("messages_redo")}</span> or /redo to
- restore
- </text>
- <Show when={revert()!.diffFiles?.length}>
- <box marginTop={1}>
- <For each={revert()!.diffFiles}>
- {(file) => (
- <text fg={theme.text}>
- {file.filename}
- <Show when={file.additions > 0}>
- <span style={{ fg: theme.diffAdded }}> +{file.additions}</span>
- </Show>
- <Show when={file.deletions > 0}>
- <span style={{ fg: theme.diffRemoved }}> -{file.deletions}</span>
- </Show>
- </text>
- )}
- </For>
- </box>
- </Show>
- </box>
- </box>
- )
- })()}
- </Match>
- <Match when={revert()?.messageID && message.id >= revert()!.messageID}>
- <></>
- </Match>
- <Match when={message.role === "user"}>
- <UserMessage
- index={index()}
- onMouseUp={() => {
- if (renderer.getSelection()?.getSelectedText()) return
- dialog.replace(() => (
- <DialogMessage
- messageID={message.id}
- sessionID={route.sessionID}
- setPrompt={(promptInfo) => prompt.set(promptInfo)}
- />
- ))
- }}
- message={message as UserMessage}
- parts={sync.data.part[message.id] ?? []}
- pending={pending()}
- />
- </Match>
- <Match when={message.role === "assistant"}>
- <AssistantMessage
- last={lastAssistant()?.id === message.id}
- message={message as AssistantMessage}
- parts={sync.data.part[message.id] ?? []}
- />
- </Match>
- </Switch>
- )}
- </For>
- </scrollbox>
- <box flexShrink={0}>
- <Prompt
- ref={(r) => {
- prompt = r
- promptRef.set(r)
- }}
- disabled={permissions().length > 0}
- onSubmit={() => {
- toBottom()
- }}
- sessionID={route.sessionID}
- />
- </box>
- <Show when={!sidebarVisible()}>
- <Footer />
- </Show>
- </Show>
- <Toast />
- </box>
- <Show when={sidebarVisible()}>
- <Sidebar sessionID={route.sessionID} />
- </Show>
- </box>
- </context.Provider>
- )
- }
- const MIME_BADGE: Record<string, string> = {
- "text/plain": "txt",
- "image/png": "img",
- "image/jpeg": "img",
- "image/gif": "img",
- "image/webp": "img",
- "application/pdf": "pdf",
- "application/x-directory": "dir",
- }
- function UserMessage(props: {
- message: UserMessage
- parts: Part[]
- onMouseUp: () => void
- index: number
- pending?: string
- }) {
- const ctx = use()
- const local = useLocal()
- const text = createMemo(() => props.parts.flatMap((x) => (x.type === "text" && !x.synthetic ? [x] : []))[0])
- const files = createMemo(() => props.parts.flatMap((x) => (x.type === "file" ? [x] : [])))
- const sync = useSync()
- const { theme } = useTheme()
- const [hover, setHover] = createSignal(false)
- const queued = createMemo(() => props.pending && props.message.id > props.pending)
- const color = createMemo(() => (queued() ? theme.accent : local.agent.color(props.message.agent)))
- const compaction = createMemo(() => props.parts.find((x) => x.type === "compaction"))
- return (
- <>
- <Show when={text()}>
- <box
- id={props.message.id}
- border={["left"]}
- borderColor={color()}
- customBorderChars={SplitBorder.customBorderChars}
- marginTop={props.index === 0 ? 0 : 1}
- >
- <box
- onMouseOver={() => {
- setHover(true)
- }}
- onMouseOut={() => {
- setHover(false)
- }}
- onMouseUp={props.onMouseUp}
- paddingTop={1}
- paddingBottom={1}
- paddingLeft={2}
- backgroundColor={hover() ? theme.backgroundElement : theme.backgroundPanel}
- flexShrink={0}
- >
- <text fg={theme.text}>{text()?.text}</text>
- <Show when={files().length}>
- <box flexDirection="row" paddingBottom={1} paddingTop={1} gap={1} flexWrap="wrap">
- <For each={files()}>
- {(file) => {
- const bg = createMemo(() => {
- if (file.mime.startsWith("image/")) return theme.accent
- if (file.mime === "application/pdf") return theme.primary
- return theme.secondary
- })
- return (
- <text fg={theme.text}>
- <span style={{ bg: bg(), fg: theme.background }}> {MIME_BADGE[file.mime] ?? file.mime} </span>
- <span style={{ bg: theme.backgroundElement, fg: theme.textMuted }}> {file.filename} </span>
- </text>
- )
- }}
- </For>
- </box>
- </Show>
- <text fg={theme.textMuted}>
- {ctx.usernameVisible() ? `${sync.data.config.username ?? "You "}` : "You "}
- <Show
- when={queued()}
- fallback={
- <Show when={ctx.showTimestamps()}>
- <span style={{ fg: theme.textMuted }}>
- {ctx.usernameVisible() ? " · " : " "}
- {Locale.todayTimeOrDateTime(props.message.time.created)}
- </span>
- </Show>
- }
- >
- <span> </span>
- <span style={{ bg: theme.accent, fg: theme.backgroundPanel, bold: true }}> QUEUED </span>
- </Show>
- </text>
- </box>
- </box>
- </Show>
- <Show when={compaction()}>
- <box
- marginTop={1}
- border={["top"]}
- title=" Compaction "
- titleAlignment="center"
- borderColor={theme.borderActive}
- />
- </Show>
- </>
- )
- }
- function AssistantMessage(props: { message: AssistantMessage; parts: Part[]; last: boolean }) {
- const local = useLocal()
- const { theme } = useTheme()
- const sync = useSync()
- const messages = createMemo(() => sync.data.message[props.message.sessionID] ?? [])
- const final = createMemo(() => {
- return props.message.finish && !["tool-calls", "unknown"].includes(props.message.finish)
- })
- const duration = createMemo(() => {
- if (!final()) return 0
- if (!props.message.time.completed) return 0
- const user = messages().find((x) => x.role === "user" && x.id === props.message.parentID)
- if (!user || !user.time) return 0
- return props.message.time.completed - user.time.created
- })
- return (
- <>
- <For each={props.parts}>
- {(part, index) => {
- const component = createMemo(() => PART_MAPPING[part.type as keyof typeof PART_MAPPING])
- return (
- <Show when={component()}>
- <Dynamic
- last={index() === props.parts.length - 1}
- component={component()}
- part={part as any}
- message={props.message}
- />
- </Show>
- )
- }}
- </For>
- <Show when={props.message.error}>
- <box
- border={["left"]}
- paddingTop={1}
- paddingBottom={1}
- paddingLeft={2}
- marginTop={1}
- backgroundColor={theme.backgroundPanel}
- customBorderChars={SplitBorder.customBorderChars}
- borderColor={theme.error}
- >
- <text fg={theme.textMuted}>{props.message.error?.data.message}</text>
- </box>
- </Show>
- <Switch>
- <Match when={props.last || final()}>
- <box paddingLeft={3}>
- <text marginTop={1}>
- <span style={{ fg: local.agent.color(props.message.mode) }}>▣ </span>{" "}
- <span style={{ fg: theme.text }}>{Locale.titlecase(props.message.mode)}</span>
- <span style={{ fg: theme.textMuted }}> · {props.message.modelID}</span>
- <Show when={duration()}>
- <span style={{ fg: theme.textMuted }}> · {Locale.duration(duration())}</span>
- </Show>
- </text>
- </box>
- </Match>
- </Switch>
- </>
- )
- }
- const PART_MAPPING = {
- text: TextPart,
- tool: ToolPart,
- reasoning: ReasoningPart,
- }
- function ReasoningPart(props: { last: boolean; part: ReasoningPart; message: AssistantMessage }) {
- const { theme, subtleSyntax } = useTheme()
- const ctx = use()
- const content = createMemo(() => {
- // Filter out redacted reasoning chunks from OpenRouter
- // OpenRouter sends encrypted reasoning data that appears as [REDACTED]
- return props.part.text.replace("[REDACTED]", "").trim()
- })
- return (
- <Show when={content() && ctx.showThinking()}>
- <box
- id={"text-" + props.part.id}
- paddingLeft={2}
- marginTop={1}
- flexDirection="column"
- border={["left"]}
- customBorderChars={SplitBorder.customBorderChars}
- borderColor={theme.backgroundElement}
- >
- <code
- filetype="markdown"
- drawUnstyledText={false}
- streaming={true}
- syntaxStyle={subtleSyntax()}
- content={"_Thinking:_ " + content()}
- conceal={ctx.conceal()}
- fg={theme.textMuted}
- />
- </box>
- </Show>
- )
- }
- function TextPart(props: { last: boolean; part: TextPart; message: AssistantMessage }) {
- const ctx = use()
- const { theme, syntax } = useTheme()
- return (
- <Show when={props.part.text.trim()}>
- <box id={"text-" + props.part.id} paddingLeft={3} marginTop={1} flexShrink={0}>
- <code
- filetype="markdown"
- drawUnstyledText={false}
- streaming={true}
- syntaxStyle={syntax()}
- content={props.part.text.trim()}
- conceal={ctx.conceal()}
- fg={theme.text}
- />
- </box>
- </Show>
- )
- }
- // Pending messages moved to individual tool pending functions
- function ToolPart(props: { last: boolean; part: ToolPart; message: AssistantMessage }) {
- const { theme } = useTheme()
- const { showDetails } = use()
- const sync = useSync()
- const [margin, setMargin] = createSignal(0)
- const component = createMemo(() => {
- // Hide tool if showDetails is false and tool completed successfully
- // But always show if there's an error or permission is required
- const shouldHide =
- !showDetails() &&
- props.part.state.status === "completed" &&
- !sync.data.permission[props.message.sessionID]?.some((x) => x.callID === props.part.callID)
- if (shouldHide) {
- return undefined
- }
- const render = ToolRegistry.render(props.part.tool) ?? GenericTool
- const metadata = props.part.state.status === "pending" ? {} : (props.part.state.metadata ?? {})
- const input = props.part.state.input ?? {}
- const container = ToolRegistry.container(props.part.tool)
- const permissions = sync.data.permission[props.message.sessionID] ?? []
- const permissionIndex = permissions.findIndex((x) => x.callID === props.part.callID)
- const permission = permissions[permissionIndex]
- const style: BoxProps =
- container === "block" || permission
- ? {
- border: permissionIndex === 0 ? (["left", "right"] as const) : (["left"] as const),
- paddingTop: 1,
- paddingBottom: 1,
- paddingLeft: 2,
- marginTop: 1,
- gap: 1,
- backgroundColor: theme.backgroundPanel,
- customBorderChars: SplitBorder.customBorderChars,
- borderColor: permissionIndex === 0 ? theme.warning : theme.background,
- }
- : {
- paddingLeft: 3,
- }
- return (
- <box
- marginTop={margin()}
- {...style}
- renderBefore={function () {
- const el = this as BoxRenderable
- const parent = el.parent
- if (!parent) {
- return
- }
- if (el.height > 1) {
- setMargin(1)
- return
- }
- const children = parent.getChildren()
- const index = children.indexOf(el)
- const previous = children[index - 1]
- if (!previous) {
- setMargin(0)
- return
- }
- if (previous.height > 1 || previous.id.startsWith("text-")) {
- setMargin(1)
- return
- }
- }}
- >
- <Dynamic
- component={render}
- input={input}
- tool={props.part.tool}
- metadata={metadata}
- permission={permission?.metadata ?? {}}
- output={props.part.state.status === "completed" ? props.part.state.output : undefined}
- />
- {props.part.state.status === "error" && (
- <box paddingLeft={2}>
- <text fg={theme.error}>{props.part.state.error.replace("Error: ", "")}</text>
- </box>
- )}
- {permission && (
- <box gap={1}>
- <text fg={theme.text}>Permission required to run this tool:</text>
- <box flexDirection="row" gap={2}>
- <text fg={theme.text}>
- <b>enter</b>
- <span style={{ fg: theme.textMuted }}> accept</span>
- </text>
- <text fg={theme.text}>
- <b>a</b>
- <span style={{ fg: theme.textMuted }}> accept always</span>
- </text>
- <text fg={theme.text}>
- <b>d</b>
- <span style={{ fg: theme.textMuted }}> deny</span>
- </text>
- </box>
- </box>
- )}
- </box>
- )
- })
- return <Show when={component()}>{component()}</Show>
- }
- type ToolProps<T extends Tool.Info> = {
- input: Partial<Tool.InferParameters<T>>
- metadata: Partial<Tool.InferMetadata<T>>
- permission: Record<string, any>
- tool: string
- output?: string
- }
- function GenericTool(props: ToolProps<any>) {
- return (
- <ToolTitle icon="⚙" fallback="Writing command..." when={true}>
- {props.tool} {input(props.input)}
- </ToolTitle>
- )
- }
- type ToolRegistration<T extends Tool.Info = any> = {
- name: string
- container: "inline" | "block"
- render?: Component<ToolProps<T>>
- }
- const ToolRegistry = (() => {
- const state: Record<string, ToolRegistration> = {}
- function register<T extends Tool.Info>(input: ToolRegistration<T>) {
- state[input.name] = input
- return input
- }
- return {
- register,
- container(name: string) {
- return state[name]?.container
- },
- render(name: string) {
- return state[name]?.render
- },
- }
- })()
- function ToolTitle(props: { fallback: string; when: any; icon: string; children: JSX.Element }) {
- const { theme } = useTheme()
- return (
- <text paddingLeft={3} fg={props.when ? theme.textMuted : theme.text}>
- <Show fallback={<>~ {props.fallback}</>} when={props.when}>
- <span style={{ bold: true }}>{props.icon}</span> {props.children}
- </Show>
- </text>
- )
- }
- ToolRegistry.register<typeof BashTool>({
- name: "bash",
- container: "block",
- render(props) {
- const output = createMemo(() => stripAnsi(props.metadata.output?.trim() ?? ""))
- const { theme } = useTheme()
- return (
- <>
- <ToolTitle icon="#" fallback="Writing command..." when={props.input.command}>
- {props.input.description || "Shell"}
- </ToolTitle>
- <Show when={props.input.command}>
- <text fg={theme.text}>$ {props.input.command}</text>
- </Show>
- <Show when={output()}>
- <box>
- <text fg={theme.text}>{output()}</text>
- </box>
- </Show>
- </>
- )
- },
- })
- ToolRegistry.register<typeof ReadTool>({
- name: "read",
- container: "inline",
- render(props) {
- return (
- <>
- <ToolTitle icon="→" fallback="Reading file..." when={props.input.filePath}>
- Read {normalizePath(props.input.filePath!)} {input(props.input, ["filePath"])}
- </ToolTitle>
- </>
- )
- },
- })
- ToolRegistry.register<typeof WriteTool>({
- name: "write",
- container: "block",
- render(props) {
- const { theme, syntax } = useTheme()
- const code = createMemo(() => {
- if (!props.input.content) return ""
- return props.input.content
- })
- const diagnostics = createMemo(() => {
- const filePath = Filesystem.normalizePath(props.input.filePath ?? "")
- return props.metadata.diagnostics?.[filePath] ?? []
- })
- const done = !!props.input.filePath
- return (
- <>
- <ToolTitle icon="←" fallback="Preparing write..." when={done}>
- Wrote {props.input.filePath}
- </ToolTitle>
- <Show when={done}>
- <line_number fg={theme.textMuted} minWidth={3} paddingRight={1}>
- <code
- conceal={false}
- fg={theme.text}
- filetype={filetype(props.input.filePath!)}
- syntaxStyle={syntax()}
- content={code()}
- />
- </line_number>
- </Show>
- <Show when={diagnostics().length}>
- <For each={diagnostics()}>
- {(diagnostic) => (
- <text fg={theme.error}>
- Error [{diagnostic.range.start.line}:{diagnostic.range.start.character}]: {diagnostic.message}
- </text>
- )}
- </For>
- </Show>
- </>
- )
- },
- })
- ToolRegistry.register<typeof GlobTool>({
- name: "glob",
- container: "inline",
- render(props) {
- return (
- <>
- <ToolTitle icon="✱" fallback="Finding files..." when={props.input.pattern}>
- Glob "{props.input.pattern}" <Show when={props.input.path}>in {normalizePath(props.input.path)} </Show>
- <Show when={props.metadata.count}>({props.metadata.count} matches)</Show>
- </ToolTitle>
- </>
- )
- },
- })
- ToolRegistry.register<typeof GrepTool>({
- name: "grep",
- container: "inline",
- render(props) {
- return (
- <ToolTitle icon="✱" fallback="Searching content..." when={props.input.pattern}>
- Grep "{props.input.pattern}" <Show when={props.input.path}>in {normalizePath(props.input.path)} </Show>
- <Show when={props.metadata.matches}>({props.metadata.matches} matches)</Show>
- </ToolTitle>
- )
- },
- })
- ToolRegistry.register<typeof ListTool>({
- name: "list",
- container: "inline",
- render(props) {
- const dir = createMemo(() => {
- if (props.input.path) {
- return normalizePath(props.input.path)
- }
- return ""
- })
- return (
- <>
- <ToolTitle icon="→" fallback="Listing directory..." when={props.input.path !== undefined}>
- List {dir()}
- </ToolTitle>
- </>
- )
- },
- })
- ToolRegistry.register<typeof TaskTool>({
- name: "task",
- container: "block",
- render(props) {
- const { theme } = useTheme()
- const keybind = useKeybind()
- return (
- <>
- <ToolTitle icon="◉" fallback="Delegating..." when={props.input.subagent_type ?? props.input.description}>
- {Locale.titlecase(props.input.subagent_type ?? "unknown")} Task "{props.input.description}"
- </ToolTitle>
- <Show when={props.metadata.summary?.length}>
- <box>
- <For each={props.metadata.summary ?? []}>
- {(task, index) => {
- const summary = props.metadata.summary ?? []
- return (
- <text style={{ fg: task.state.status === "error" ? theme.error : theme.textMuted }}>
- {index() === summary.length - 1 ? "└" : "├"} {Locale.titlecase(task.tool)}{" "}
- {task.state.status === "completed" ? task.state.title : ""}
- </text>
- )
- }}
- </For>
- </box>
- </Show>
- <text fg={theme.text}>
- {keybind.print("session_child_cycle")}, {keybind.print("session_child_cycle_reverse")}
- <span style={{ fg: theme.textMuted }}> to navigate between subagent sessions</span>
- </text>
- </>
- )
- },
- })
- ToolRegistry.register<typeof WebFetchTool>({
- name: "webfetch",
- container: "inline",
- render(props) {
- return (
- <ToolTitle icon="%" fallback="Fetching from the web..." when={(props.input as any).url}>
- WebFetch {(props.input as any).url}
- </ToolTitle>
- )
- },
- })
- ToolRegistry.register({
- name: "codesearch",
- container: "inline",
- render(props: ToolProps<any>) {
- const input = props.input as any
- const metadata = props.metadata as any
- return (
- <ToolTitle icon="◇" fallback="Searching code..." when={input.query}>
- Exa Code Search "{input.query}" <Show when={metadata.results}>({metadata.results} results)</Show>
- </ToolTitle>
- )
- },
- })
- ToolRegistry.register({
- name: "websearch",
- container: "inline",
- render(props: ToolProps<any>) {
- const input = props.input as any
- const metadata = props.metadata as any
- return (
- <ToolTitle icon="◈" fallback="Searching web..." when={input.query}>
- Exa Web Search "{input.query}" <Show when={metadata.numResults}>({metadata.numResults} results)</Show>
- </ToolTitle>
- )
- },
- })
- ToolRegistry.register<typeof EditTool>({
- name: "edit",
- container: "block",
- render(props) {
- const ctx = use()
- const { theme, syntax } = useTheme()
- const view = createMemo(() => {
- const diffStyle = ctx.sync.data.config.tui?.diff_style
- if (diffStyle === "stacked") return "unified"
- // Default to "auto" behavior
- return ctx.width > 120 ? "split" : "unified"
- })
- const ft = createMemo(() => filetype(props.input.filePath))
- const diffContent = createMemo(() => props.metadata.diff ?? props.permission["diff"])
- const diagnostics = createMemo(() => {
- const filePath = Filesystem.normalizePath(props.input.filePath ?? "")
- const arr = props.metadata.diagnostics?.[filePath] ?? []
- return arr.filter((x) => x.severity === 1).slice(0, 3)
- })
- return (
- <>
- <ToolTitle icon="←" fallback="Preparing edit..." when={props.input.filePath}>
- Edit {normalizePath(props.input.filePath!)}{" "}
- {input({
- replaceAll: props.input.replaceAll,
- })}
- </ToolTitle>
- <Show when={diffContent()}>
- <box paddingLeft={1}>
- <diff
- diff={diffContent()}
- view={view()}
- filetype={ft()}
- syntaxStyle={syntax()}
- showLineNumbers={true}
- width="100%"
- wrapMode={ctx.diffWrapMode()}
- fg={theme.text}
- addedBg={theme.diffAddedBg}
- removedBg={theme.diffRemovedBg}
- contextBg={theme.diffContextBg}
- addedSignColor={theme.diffHighlightAdded}
- removedSignColor={theme.diffHighlightRemoved}
- lineNumberFg={theme.diffLineNumber}
- lineNumberBg={theme.diffContextBg}
- addedLineNumberBg={theme.diffAddedLineNumberBg}
- removedLineNumberBg={theme.diffRemovedLineNumberBg}
- />
- </box>
- </Show>
- <Show when={diagnostics().length}>
- <box>
- <For each={diagnostics()}>
- {(diagnostic) => (
- <text fg={theme.error}>
- Error [{diagnostic.range.start.line + 1}:{diagnostic.range.start.character + 1}] {diagnostic.message}
- </text>
- )}
- </For>
- </box>
- </Show>
- </>
- )
- },
- })
- ToolRegistry.register<typeof PatchTool>({
- name: "patch",
- container: "block",
- render(props) {
- const { theme } = useTheme()
- return (
- <>
- <ToolTitle icon="%" fallback="Preparing patch..." when={true}>
- Patch
- </ToolTitle>
- <Show when={props.output}>
- <box>
- <text fg={theme.text}>{props.output?.trim()}</text>
- </box>
- </Show>
- </>
- )
- },
- })
- ToolRegistry.register<typeof TodoWriteTool>({
- name: "todowrite",
- container: "block",
- render(props) {
- const { theme } = useTheme()
- return (
- <>
- <Show when={!props.input.todos?.length}>
- <ToolTitle icon="⚙" fallback="Updating todos..." when={true}>
- Updating todos...
- </ToolTitle>
- </Show>
- <Show when={props.metadata.todos?.length}>
- <box>
- <For each={props.input.todos ?? []}>
- {(todo) => (
- <text style={{ fg: todo.status === "in_progress" ? theme.success : theme.textMuted }}>
- [{todo.status === "completed" ? "✓" : " "}] {todo.content}
- </text>
- )}
- </For>
- </box>
- </Show>
- </>
- )
- },
- })
- function normalizePath(input?: string) {
- if (!input) return ""
- if (path.isAbsolute(input)) {
- return path.relative(process.cwd(), input) || "."
- }
- return input
- }
- function input(input: Record<string, any>, omit?: string[]): string {
- const primitives = Object.entries(input).filter(([key, value]) => {
- if (omit?.includes(key)) return false
- return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
- })
- if (primitives.length === 0) return ""
- return `[${primitives.map(([key, value]) => `${key}=${value}`).join(", ")}]`
- }
- function filetype(input?: string) {
- if (!input) return "none"
- const ext = path.extname(input)
- const language = LANGUAGE_EXTENSIONS[ext]
- if (["typescriptreact", "javascriptreact", "javascript"].includes(language)) return "typescript"
- return language
- }
|