Share.tsx 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. import { type JSX } from "solid-js"
  2. import {
  3. For,
  4. Show,
  5. Match,
  6. Switch,
  7. onMount,
  8. Suspense,
  9. onCleanup,
  10. splitProps,
  11. createMemo,
  12. createEffect,
  13. createSignal,
  14. SuspenseList,
  15. } from "solid-js"
  16. import map from "lang-map"
  17. import { DateTime } from "luxon"
  18. import { createStore, reconcile } from "solid-js/store"
  19. import type { Diagnostic } from "vscode-languageserver-types"
  20. import {
  21. IconOpenAI,
  22. IconGemini,
  23. IconOpencode,
  24. IconAnthropic,
  25. } from "./icons/custom"
  26. import {
  27. IconHashtag,
  28. IconSparkles,
  29. IconGlobeAlt,
  30. IconDocument,
  31. IconQueueList,
  32. IconUserCircle,
  33. IconCheckCircle,
  34. IconChevronDown,
  35. IconCommandLine,
  36. IconChevronRight,
  37. IconDocumentPlus,
  38. IconPencilSquare,
  39. IconRectangleStack,
  40. IconMagnifyingGlass,
  41. IconWrenchScrewdriver,
  42. IconDocumentMagnifyingGlass,
  43. IconArrowDown,
  44. } from "./icons"
  45. import DiffView from "./DiffView"
  46. import CodeBlock from "./CodeBlock"
  47. import MarkdownView from "./MarkdownView"
  48. import styles from "./share.module.css"
  49. import type { Message } from "opencode/session/message"
  50. import type { Session } from "opencode/session/index"
  51. const MIN_DURATION = 2
  52. type Status =
  53. | "disconnected"
  54. | "connecting"
  55. | "connected"
  56. | "error"
  57. | "reconnecting"
  58. type TodoStatus = "pending" | "in_progress" | "completed"
  59. interface Todo {
  60. id: string
  61. content: string
  62. status: TodoStatus
  63. priority: "low" | "medium" | "high"
  64. }
  65. function sortTodosByStatus(todos: Todo[]) {
  66. const statusPriority: Record<TodoStatus, number> = {
  67. in_progress: 0,
  68. pending: 1,
  69. completed: 2,
  70. }
  71. return todos
  72. .slice()
  73. .sort((a, b) => statusPriority[a.status] - statusPriority[b.status])
  74. }
  75. function scrollToAnchor(id: string) {
  76. const el = document.getElementById(id)
  77. if (!el) return
  78. el.scrollIntoView({ behavior: "smooth" })
  79. }
  80. function stripWorkingDirectory(filePath?: string, workingDir?: string) {
  81. if (filePath === undefined || workingDir === undefined) return filePath
  82. const prefix = workingDir.endsWith("/") ? workingDir : workingDir + "/"
  83. if (filePath === workingDir) {
  84. return ""
  85. }
  86. if (filePath.startsWith(prefix)) {
  87. return filePath.slice(prefix.length)
  88. }
  89. return filePath
  90. }
  91. function getShikiLang(filename: string) {
  92. const ext = filename.split(".").pop()?.toLowerCase() ?? ""
  93. // map.languages(ext) returns an array of matching Linguist language names (e.g. ['TypeScript'])
  94. const langs = map.languages(ext)
  95. const type = langs?.[0]?.toLowerCase()
  96. // Overrride any specific language mappings
  97. const overrides: Record<string, string> = {
  98. conf: "shellscript",
  99. }
  100. return type ? (overrides[type] ?? type) : "plaintext"
  101. }
  102. function formatDuration(ms: number): string {
  103. const ONE_SECOND = 1000
  104. const ONE_MINUTE = 60 * ONE_SECOND
  105. if (ms >= ONE_MINUTE) {
  106. const minutes = Math.floor(ms / ONE_MINUTE)
  107. return minutes === 1 ? `1min` : `${minutes}mins`
  108. }
  109. if (ms >= ONE_SECOND) {
  110. const seconds = Math.floor(ms / ONE_SECOND)
  111. return `${seconds}s`
  112. }
  113. return `${ms}ms`
  114. }
  115. // Converts nested objects/arrays into [path, value] pairs.
  116. // E.g. {a:{b:{c:1}}, d:[{e:2}, 3]} => [["a.b.c",1], ["d[0].e",2], ["d[1]",3]]
  117. function flattenToolArgs(obj: any, prefix: string = ""): Array<[string, any]> {
  118. const entries: Array<[string, any]> = []
  119. for (const [key, value] of Object.entries(obj)) {
  120. const path = prefix ? `${prefix}.${key}` : key
  121. if (value !== null && typeof value === "object") {
  122. if (Array.isArray(value)) {
  123. value.forEach((item, index) => {
  124. const arrayPath = `${path}[${index}]`
  125. if (item !== null && typeof item === "object") {
  126. entries.push(...flattenToolArgs(item, arrayPath))
  127. } else {
  128. entries.push([arrayPath, item])
  129. }
  130. })
  131. } else {
  132. entries.push(...flattenToolArgs(value, path))
  133. }
  134. } else {
  135. entries.push([path, value])
  136. }
  137. }
  138. return entries
  139. }
  140. function formatErrorString(error: string): JSX.Element {
  141. const errorMarker = "Error: "
  142. const startsWithError = error.startsWith(errorMarker)
  143. return startsWithError ? (
  144. <pre>
  145. <span data-color="red" data-marker="label" data-separator>
  146. Error
  147. </span>
  148. <span>{error.slice(errorMarker.length)}</span>
  149. </pre>
  150. ) : (
  151. <pre>
  152. <span data-color="dimmed">{error}</span>
  153. </pre>
  154. )
  155. }
  156. function getDiagnostics(
  157. diagnosticsByFile: Record<string, Diagnostic[]>,
  158. currentFile: string,
  159. ): JSX.Element[] {
  160. // Return a flat array of error diagnostics, in the format:
  161. // "Error [65:20] Property 'x' does not exist on type 'Y'"
  162. const result: JSX.Element[] = []
  163. if (
  164. diagnosticsByFile === undefined ||
  165. diagnosticsByFile[currentFile] === undefined
  166. )
  167. return result
  168. for (const diags of Object.values(diagnosticsByFile)) {
  169. for (const d of diags) {
  170. // Only keep diagnostics explicitly marked as Error (severity === 1)
  171. if (d.severity !== 1) continue
  172. const line = d.range.start.line + 1 // 1-based
  173. const column = d.range.start.character + 1 // 1-based
  174. result.push(
  175. <pre>
  176. <span data-color="red" data-marker="label">
  177. Error
  178. </span>
  179. <span data-color="dimmed" data-separator>
  180. [{line}:{column}]
  181. </span>
  182. <span>{d.message}</span>
  183. </pre>,
  184. )
  185. }
  186. }
  187. return result
  188. }
  189. function stripEnclosingTag(text: string): string {
  190. const wrappedRe = /^\s*<([A-Za-z]\w*)>\s*([\s\S]*?)\s*<\/\1>\s*$/
  191. const match = text.match(wrappedRe)
  192. return match ? match[2] : text
  193. }
  194. function getStatusText(status: [Status, string?]): string {
  195. switch (status[0]) {
  196. case "connected":
  197. return "Connected, waiting for messages..."
  198. case "connecting":
  199. return "Connecting..."
  200. case "disconnected":
  201. return "Disconnected"
  202. case "reconnecting":
  203. return "Reconnecting..."
  204. case "error":
  205. return status[1] || "Error"
  206. default:
  207. return "Unknown"
  208. }
  209. }
  210. function ProviderIcon(props: { provider: string; size?: number }) {
  211. const size = props.size || 16
  212. return (
  213. <Switch fallback={<IconSparkles width={size} height={size} />}>
  214. <Match when={props.provider === "openai"}>
  215. <IconOpenAI width={size} height={size} />
  216. </Match>
  217. <Match when={props.provider === "anthropic"}>
  218. <IconAnthropic width={size} height={size} />
  219. </Match>
  220. <Match when={props.provider === "gemini"}>
  221. <IconGemini width={size} height={size} />
  222. </Match>
  223. </Switch>
  224. )
  225. }
  226. interface ResultsButtonProps extends JSX.HTMLAttributes<HTMLButtonElement> {
  227. showCopy?: string
  228. hideCopy?: string
  229. results: boolean
  230. }
  231. function ResultsButton(props: ResultsButtonProps) {
  232. const [local, rest] = splitProps(props, ["results", "showCopy", "hideCopy"])
  233. return (
  234. <button
  235. type="button"
  236. data-element-button-text
  237. data-element-button-more
  238. {...rest}
  239. >
  240. <span>
  241. {local.results
  242. ? local.hideCopy || "Hide results"
  243. : local.showCopy || "Show results"}
  244. </span>
  245. <span data-button-icon>
  246. <Show
  247. when={local.results}
  248. fallback={<IconChevronRight width={11} height={11} />}
  249. >
  250. <IconChevronDown width={11} height={11} />
  251. </Show>
  252. </span>
  253. </button>
  254. )
  255. }
  256. interface TextPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
  257. text: string
  258. expand?: boolean
  259. invert?: boolean
  260. highlight?: boolean
  261. }
  262. function TextPart(props: TextPartProps) {
  263. const [local, rest] = splitProps(props, [
  264. "text",
  265. "expand",
  266. "invert",
  267. "highlight",
  268. ])
  269. const [expanded, setExpanded] = createSignal(false)
  270. const [overflowed, setOverflowed] = createSignal(false)
  271. let preEl: HTMLPreElement | undefined
  272. function checkOverflow() {
  273. if (preEl && !local.expand) {
  274. setOverflowed(preEl.scrollHeight > preEl.clientHeight + 1)
  275. }
  276. }
  277. onMount(() => {
  278. checkOverflow()
  279. window.addEventListener("resize", checkOverflow)
  280. })
  281. createEffect(() => {
  282. local.text
  283. local.expand
  284. setTimeout(checkOverflow, 0)
  285. })
  286. onCleanup(() => {
  287. window.removeEventListener("resize", checkOverflow)
  288. })
  289. return (
  290. <div
  291. class={styles["message-text"]}
  292. data-invert={local.invert}
  293. data-highlight={local.highlight}
  294. data-expanded={expanded() || local.expand === true}
  295. {...rest}
  296. >
  297. <pre ref={(el) => (preEl = el)}>{local.text}</pre>
  298. {((!local.expand && overflowed()) || expanded()) && (
  299. <button
  300. type="button"
  301. data-element-button-text
  302. onClick={() => setExpanded((e) => !e)}
  303. >
  304. {expanded() ? "Show less" : "Show more"}
  305. </button>
  306. )}
  307. </div>
  308. )
  309. }
  310. interface ErrorPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
  311. expand?: boolean
  312. }
  313. function ErrorPart(props: ErrorPartProps) {
  314. const [local, rest] = splitProps(props, ["expand", "children"])
  315. const [expanded, setExpanded] = createSignal(false)
  316. const [overflowed, setOverflowed] = createSignal(false)
  317. let preEl: HTMLElement | undefined
  318. function checkOverflow() {
  319. if (preEl && !local.expand) {
  320. setOverflowed(preEl.scrollHeight > preEl.clientHeight + 1)
  321. }
  322. }
  323. onMount(() => {
  324. checkOverflow()
  325. window.addEventListener("resize", checkOverflow)
  326. })
  327. createEffect(() => {
  328. local.children
  329. local.expand
  330. setTimeout(checkOverflow, 0)
  331. })
  332. onCleanup(() => {
  333. window.removeEventListener("resize", checkOverflow)
  334. })
  335. return (
  336. <div
  337. class={styles["message-error"]}
  338. data-expanded={expanded() || local.expand === true}
  339. {...rest}
  340. >
  341. <div data-section="content" ref={(el) => (preEl = el)}>
  342. {local.children}
  343. </div>
  344. {((!local.expand && overflowed()) || expanded()) && (
  345. <button
  346. type="button"
  347. data-element-button-text
  348. onClick={() => setExpanded((e) => !e)}
  349. >
  350. {expanded() ? "Show less" : "Show more"}
  351. </button>
  352. )}
  353. </div>
  354. )
  355. }
  356. interface MarkdownPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
  357. text: string
  358. expand?: boolean
  359. highlight?: boolean
  360. }
  361. function MarkdownPart(props: MarkdownPartProps) {
  362. const [local, rest] = splitProps(props, ["text", "expand", "highlight"])
  363. const [expanded, setExpanded] = createSignal(false)
  364. const [overflowed, setOverflowed] = createSignal(false)
  365. let divEl: HTMLDivElement | undefined
  366. function checkOverflow() {
  367. if (divEl && !local.expand) {
  368. setOverflowed(divEl.scrollHeight > divEl.clientHeight + 1)
  369. }
  370. }
  371. onMount(() => {
  372. checkOverflow()
  373. window.addEventListener("resize", checkOverflow)
  374. })
  375. createEffect(() => {
  376. local.text
  377. local.expand
  378. setTimeout(checkOverflow, 0)
  379. })
  380. onCleanup(() => {
  381. window.removeEventListener("resize", checkOverflow)
  382. })
  383. return (
  384. <div
  385. class={styles["message-markdown"]}
  386. data-highlight={local.highlight}
  387. data-expanded={expanded() || local.expand === true}
  388. {...rest}
  389. >
  390. <MarkdownView
  391. data-element-markdown
  392. markdown={local.text}
  393. ref={(el) => (divEl = el)}
  394. />
  395. {((!local.expand && overflowed()) || expanded()) && (
  396. <button
  397. type="button"
  398. data-element-button-text
  399. onClick={() => setExpanded((e) => !e)}
  400. >
  401. {expanded() ? "Show less" : "Show more"}
  402. </button>
  403. )}
  404. </div>
  405. )
  406. }
  407. interface TerminalPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
  408. command: string
  409. error?: string
  410. result?: string
  411. desc?: string
  412. expand?: boolean
  413. }
  414. function TerminalPart(props: TerminalPartProps) {
  415. const [local, rest] = splitProps(props, [
  416. "command",
  417. "error",
  418. "result",
  419. "desc",
  420. "expand",
  421. ])
  422. const [expanded, setExpanded] = createSignal(false)
  423. const [overflowed, setOverflowed] = createSignal(false)
  424. let preEl: HTMLElement | undefined
  425. function checkOverflow() {
  426. if (!preEl) return
  427. const code = preEl.getElementsByTagName("code")[0]
  428. if (code && !local.expand) {
  429. setOverflowed(preEl.clientHeight < code.offsetHeight)
  430. }
  431. }
  432. createEffect(() => {
  433. local.command
  434. local.result
  435. local.error
  436. local.expand
  437. setTimeout(checkOverflow, 0)
  438. })
  439. onMount(() => {
  440. checkOverflow()
  441. window.addEventListener("resize", checkOverflow)
  442. })
  443. onCleanup(() => {
  444. window.removeEventListener("resize", checkOverflow)
  445. })
  446. return (
  447. <div
  448. class={styles["message-terminal"]}
  449. data-expanded={expanded() || local.expand === true}
  450. {...rest}
  451. >
  452. <div data-section="body">
  453. <div data-section="header">
  454. <span>{local.desc}</span>
  455. </div>
  456. <div data-section="content">
  457. <CodeBlock lang="bash" code={local.command} />
  458. <Switch>
  459. <Match when={local.error}>
  460. <CodeBlock
  461. data-section="error"
  462. lang="text"
  463. ref={(el) => (preEl = el)}
  464. code={local.error || ""}
  465. />
  466. </Match>
  467. <Match when={local.result}>
  468. <CodeBlock
  469. lang="console"
  470. ref={(el) => (preEl = el)}
  471. code={local.result || ""}
  472. />
  473. </Match>
  474. </Switch>
  475. </div>
  476. </div>
  477. {((!local.expand && overflowed()) || expanded()) && (
  478. <button
  479. type="button"
  480. data-element-button-text
  481. onClick={() => setExpanded((e) => !e)}
  482. >
  483. {expanded() ? "Show less" : "Show more"}
  484. </button>
  485. )}
  486. </div>
  487. )
  488. }
  489. function ToolFooter(props: { time: number }) {
  490. return props.time > MIN_DURATION ? (
  491. <span data-part-footer title={`${props.time}ms`}>
  492. {formatDuration(props.time)}
  493. </span>
  494. ) : (
  495. <div data-part-footer="spacer"></div>
  496. )
  497. }
  498. interface AnchorProps extends JSX.HTMLAttributes<HTMLDivElement> {
  499. id: string
  500. }
  501. function AnchorIcon(props: AnchorProps) {
  502. const [local, rest] = splitProps(props, ["id", "children"])
  503. const [copied, setCopied] = createSignal(false)
  504. return (
  505. <div
  506. {...rest}
  507. data-element-anchor
  508. title="Link to this message"
  509. data-status={copied() ? "copied" : ""}
  510. >
  511. <a
  512. href={`#${local.id}`}
  513. onClick={(e) => {
  514. e.preventDefault()
  515. const anchor = e.currentTarget
  516. const hash = anchor.getAttribute("href") || ""
  517. const { origin, pathname, search } = window.location
  518. navigator.clipboard
  519. .writeText(`${origin}${pathname}${search}${hash}`)
  520. .catch((err) => console.error("Copy failed", err))
  521. setCopied(true)
  522. setTimeout(() => setCopied(false), 3000)
  523. }}
  524. >
  525. {local.children}
  526. <IconHashtag width={18} height={18} />
  527. <IconCheckCircle width={18} height={18} />
  528. </a>
  529. <span data-element-tooltip>Copied!</span>
  530. </div>
  531. )
  532. }
  533. export default function Share(props: {
  534. id: string
  535. api: string
  536. info: Session.Info
  537. messages: Record<string, Message.Info>
  538. }) {
  539. let lastScrollY = 0
  540. let scrollTimeout: number | undefined
  541. let scrollSentinel: HTMLElement | undefined
  542. let scrollObserver: IntersectionObserver | undefined
  543. const id = props.id
  544. const params = new URLSearchParams(window.location.search)
  545. const debug = params.get("debug") === "true"
  546. const [showScrollButton, setShowScrollButton] = createSignal(false)
  547. const [isButtonHovered, setIsButtonHovered] = createSignal(false)
  548. const [isNearBottom, setIsNearBottom] = createSignal(false)
  549. const [store, setStore] = createStore<{
  550. info?: Session.Info
  551. messages: Record<string, Message.Info>
  552. }>({ info: props.info, messages: props.messages })
  553. const messages = createMemo(() =>
  554. Object.values(store.messages).toSorted((a, b) => a.id?.localeCompare(b.id)),
  555. )
  556. const [connectionStatus, setConnectionStatus] = createSignal<
  557. [Status, string?]
  558. >(["disconnected", "Disconnected"])
  559. onMount(() => {
  560. const apiUrl = props.api
  561. if (!id) {
  562. setConnectionStatus(["error", "id not found"])
  563. return
  564. }
  565. if (!apiUrl) {
  566. console.error("API URL not found in environment variables")
  567. setConnectionStatus(["error", "API URL not found"])
  568. return
  569. }
  570. let reconnectTimer: number | undefined
  571. let socket: WebSocket | null = null
  572. // Function to create and set up WebSocket with auto-reconnect
  573. const setupWebSocket = () => {
  574. // Close any existing connection
  575. if (socket) {
  576. socket.close()
  577. }
  578. setConnectionStatus(["connecting"])
  579. // Always use secure WebSocket protocol (wss)
  580. const wsBaseUrl = apiUrl.replace(/^https?:\/\//, "wss://")
  581. const wsUrl = `${wsBaseUrl}/share_poll?id=${id}`
  582. console.log("Connecting to WebSocket URL:", wsUrl)
  583. // Create WebSocket connection
  584. socket = new WebSocket(wsUrl)
  585. // Handle connection opening
  586. socket.onopen = () => {
  587. setConnectionStatus(["connected"])
  588. console.log("WebSocket connection established")
  589. }
  590. // Handle incoming messages
  591. socket.onmessage = (event) => {
  592. console.log("WebSocket message received")
  593. try {
  594. const d = JSON.parse(event.data)
  595. const [root, type, ...splits] = d.key.split("/")
  596. if (root !== "session") return
  597. if (type === "info") {
  598. setStore("info", reconcile(d.content))
  599. return
  600. }
  601. if (type === "message") {
  602. const [, messageID] = splits
  603. setStore("messages", messageID, reconcile(d.content))
  604. }
  605. } catch (error) {
  606. console.error("Error parsing WebSocket message:", error)
  607. }
  608. }
  609. // Handle errors
  610. socket.onerror = (error) => {
  611. console.error("WebSocket error:", error)
  612. setConnectionStatus(["error", "Connection failed"])
  613. }
  614. // Handle connection close and reconnection
  615. socket.onclose = (event) => {
  616. console.log(`WebSocket closed: ${event.code} ${event.reason}`)
  617. setConnectionStatus(["reconnecting"])
  618. // Try to reconnect after 2 seconds
  619. clearTimeout(reconnectTimer)
  620. reconnectTimer = window.setTimeout(
  621. setupWebSocket,
  622. 2000,
  623. ) as unknown as number
  624. }
  625. }
  626. // Initial connection
  627. setupWebSocket()
  628. // Clean up on component unmount
  629. onCleanup(() => {
  630. console.log("Cleaning up WebSocket connection")
  631. if (socket) {
  632. socket.close()
  633. }
  634. clearTimeout(reconnectTimer)
  635. })
  636. })
  637. function checkScrollNeed() {
  638. const currentScrollY = window.scrollY
  639. const isScrollingDown = currentScrollY > lastScrollY
  640. const scrolled = currentScrollY > 200 // Show after scrolling 200px
  641. // Only show when scrolling down, scrolled enough, and not near bottom
  642. const shouldShow = isScrollingDown && scrolled && !isNearBottom()
  643. // Update last scroll position
  644. lastScrollY = currentScrollY
  645. if (shouldShow) {
  646. setShowScrollButton(true)
  647. // Clear existing timeout
  648. if (scrollTimeout) {
  649. clearTimeout(scrollTimeout)
  650. }
  651. // Hide button after 3 seconds of no scrolling (unless hovered)
  652. scrollTimeout = window.setTimeout(() => {
  653. if (!isButtonHovered()) {
  654. setShowScrollButton(false)
  655. }
  656. }, 1500)
  657. } else if (!isButtonHovered()) {
  658. // Only hide if not hovered (to prevent disappearing while user is about to click)
  659. setShowScrollButton(false)
  660. if (scrollTimeout) {
  661. clearTimeout(scrollTimeout)
  662. }
  663. }
  664. }
  665. onMount(() => {
  666. lastScrollY = window.scrollY // Initialize scroll position
  667. // Create sentinel element
  668. const sentinel = document.createElement("div")
  669. sentinel.style.height = "1px"
  670. sentinel.style.position = "absolute"
  671. sentinel.style.bottom = "100px"
  672. sentinel.style.width = "100%"
  673. sentinel.style.pointerEvents = "none"
  674. document.body.appendChild(sentinel)
  675. // Create intersection observer
  676. const observer = new IntersectionObserver((entries) => {
  677. setIsNearBottom(entries[0].isIntersecting)
  678. })
  679. observer.observe(sentinel)
  680. // Store references for cleanup
  681. scrollSentinel = sentinel
  682. scrollObserver = observer
  683. checkScrollNeed()
  684. window.addEventListener("scroll", checkScrollNeed)
  685. window.addEventListener("resize", checkScrollNeed)
  686. })
  687. onCleanup(() => {
  688. window.removeEventListener("scroll", checkScrollNeed)
  689. window.removeEventListener("resize", checkScrollNeed)
  690. // Clean up observer and sentinel
  691. if (scrollObserver) {
  692. scrollObserver.disconnect()
  693. }
  694. if (scrollSentinel) {
  695. document.body.removeChild(scrollSentinel)
  696. }
  697. if (scrollTimeout) {
  698. clearTimeout(scrollTimeout)
  699. }
  700. })
  701. const data = createMemo(() => {
  702. const result = {
  703. rootDir: undefined as string | undefined,
  704. created: undefined as number | undefined,
  705. completed: undefined as number | undefined,
  706. messages: [] as Message.Info[],
  707. models: {} as Record<string, string[]>,
  708. cost: 0,
  709. tokens: {
  710. input: 0,
  711. output: 0,
  712. reasoning: 0,
  713. },
  714. }
  715. result.created = props.info.time.created
  716. for (let i = 0; i < messages().length; i++) {
  717. const msg = messages()[i]
  718. const assistant = msg.metadata?.assistant
  719. result.messages.push(msg)
  720. if (assistant) {
  721. result.cost += assistant.cost
  722. result.tokens.input += assistant.tokens.input
  723. result.tokens.output += assistant.tokens.output
  724. result.tokens.reasoning += assistant.tokens.reasoning
  725. result.models[`${assistant.providerID} ${assistant.modelID}`] = [
  726. assistant.providerID,
  727. assistant.modelID,
  728. ]
  729. if (assistant.path?.root) {
  730. result.rootDir = assistant.path.root
  731. }
  732. if (msg.metadata?.time.completed) {
  733. result.completed = msg.metadata?.time.completed
  734. }
  735. }
  736. }
  737. return result
  738. })
  739. return (
  740. <main class={`${styles.root} not-content`}>
  741. <div class={styles.header}>
  742. <div data-section="title">
  743. <h1>{store.info?.title}</h1>
  744. </div>
  745. <div data-section="row">
  746. <ul data-section="stats" data-section-models>
  747. <li title="opencode version">
  748. <div data-stat-icon title="opencode">
  749. <IconOpencode width={16} height={16} />
  750. </div>
  751. <Show when={store.info?.version} fallback="v0.0.1">
  752. <span>v{store.info?.version}</span>
  753. </Show>
  754. </li>
  755. {Object.values(data().models).length > 0 ? (
  756. <For each={Object.values(data().models)}>
  757. {([provider, model]) => (
  758. <li>
  759. <div data-stat-icon title={provider}>
  760. <ProviderIcon provider={provider} />
  761. </div>
  762. <span data-stat-model>{model}</span>
  763. </li>
  764. )}
  765. </For>
  766. ) : (
  767. <li>
  768. <span data-element-label>Models</span>
  769. <span data-placeholder>&mdash;</span>
  770. </li>
  771. )}
  772. </ul>
  773. <div data-section="time">
  774. {data().created ? (
  775. <span
  776. title={DateTime.fromMillis(data().created || 0).toLocaleString(
  777. DateTime.DATETIME_FULL_WITH_SECONDS,
  778. )}
  779. >
  780. {DateTime.fromMillis(data().created || 0).toLocaleString(
  781. DateTime.DATETIME_MED,
  782. )}
  783. </span>
  784. ) : (
  785. <span data-element-label data-placeholder>
  786. Started at &mdash;
  787. </span>
  788. )}
  789. </div>
  790. </div>
  791. </div>
  792. <div>
  793. <Show
  794. when={data().messages.length > 0}
  795. fallback={<p>Waiting for messages...</p>}
  796. >
  797. <div class={styles.parts}>
  798. <SuspenseList revealOrder="forwards">
  799. <For each={data().messages}>
  800. {(msg, msgIndex) => (
  801. <Suspense>
  802. <For each={msg.parts}>
  803. {(part, partIndex) => {
  804. if (
  805. (part.type === "step-start" &&
  806. (partIndex() > 0 || !msg.metadata?.assistant)) ||
  807. (msg.role === "assistant" &&
  808. part.type === "tool-invocation" &&
  809. part.toolInvocation.toolName === "todoread")
  810. )
  811. return null
  812. const anchor = createMemo(
  813. () => `${msg.id}-${partIndex()}`,
  814. )
  815. const [showResults, setShowResults] =
  816. createSignal(false)
  817. const isLastPart = createMemo(
  818. () =>
  819. data().messages.length === msgIndex() + 1 &&
  820. msg.parts.length === partIndex() + 1,
  821. )
  822. const toolData = createMemo(() => {
  823. if (
  824. msg.role !== "assistant" ||
  825. part.type !== "tool-invocation"
  826. )
  827. return {}
  828. const metadata =
  829. msg.metadata?.tool[part.toolInvocation.toolCallId]
  830. const args = part.toolInvocation.args
  831. const result =
  832. part.toolInvocation.state === "result" &&
  833. part.toolInvocation.result
  834. const duration = DateTime.fromMillis(
  835. metadata?.time.end || 0,
  836. )
  837. .diff(
  838. DateTime.fromMillis(metadata?.time.start || 0),
  839. )
  840. .toMillis()
  841. return { metadata, args, result, duration }
  842. })
  843. onMount(() => {
  844. const hash = window.location.hash.slice(1)
  845. if (hash !== "" && hash === anchor()) {
  846. scrollToAnchor(hash)
  847. }
  848. })
  849. return (
  850. <Switch>
  851. {/* User text */}
  852. <Match
  853. when={
  854. msg.role === "user" &&
  855. part.type === "text" &&
  856. part
  857. }
  858. >
  859. {(part) => (
  860. <div
  861. id={anchor()}
  862. data-section="part"
  863. data-part-type="user-text"
  864. >
  865. <div data-section="decoration">
  866. <AnchorIcon id={anchor()}>
  867. <IconUserCircle width={18} height={18} />
  868. </AnchorIcon>
  869. <div></div>
  870. </div>
  871. <div data-section="content">
  872. <TextPart
  873. invert
  874. text={part().text}
  875. expand={isLastPart()}
  876. />
  877. </div>
  878. </div>
  879. )}
  880. </Match>
  881. {/* AI text */}
  882. <Match
  883. when={
  884. msg.role === "assistant" &&
  885. part.type === "text" &&
  886. part
  887. }
  888. >
  889. {(part) => (
  890. <div
  891. id={anchor()}
  892. data-section="part"
  893. data-part-type="ai-text"
  894. >
  895. <div data-section="decoration">
  896. <AnchorIcon id={anchor()}>
  897. <IconSparkles width={18} height={18} />
  898. </AnchorIcon>
  899. <div></div>
  900. </div>
  901. <div data-section="content">
  902. <MarkdownPart
  903. highlight
  904. expand={isLastPart()}
  905. text={stripEnclosingTag(part().text)}
  906. />
  907. <Show
  908. when={isLastPart() && data().completed}
  909. >
  910. <span
  911. data-part-footer
  912. title={DateTime.fromMillis(
  913. data().completed || 0,
  914. ).toLocaleString(
  915. DateTime.DATETIME_FULL_WITH_SECONDS,
  916. )}
  917. >
  918. {DateTime.fromMillis(
  919. data().completed || 0,
  920. ).toLocaleString(DateTime.DATETIME_MED)}
  921. </span>
  922. </Show>
  923. </div>
  924. </div>
  925. )}
  926. </Match>
  927. {/* AI model */}
  928. <Match
  929. when={
  930. msg.role === "assistant" &&
  931. part.type === "step-start" &&
  932. msg.metadata?.assistant
  933. }
  934. >
  935. {(assistant) => {
  936. return (
  937. <div
  938. id={anchor()}
  939. data-section="part"
  940. data-part-type="ai-model"
  941. >
  942. <div data-section="decoration">
  943. <AnchorIcon id={anchor()}>
  944. <ProviderIcon
  945. size={18}
  946. provider={assistant().providerID}
  947. />
  948. </AnchorIcon>
  949. <div></div>
  950. </div>
  951. <div data-section="content">
  952. <div data-part-tool-body>
  953. <div data-part-title>
  954. <span data-element-label>
  955. {assistant().providerID}
  956. </span>
  957. </div>
  958. <span data-part-model>
  959. {assistant().modelID}
  960. </span>
  961. </div>
  962. </div>
  963. </div>
  964. )
  965. }}
  966. </Match>
  967. {/* Grep tool */}
  968. <Match
  969. when={
  970. msg.role === "assistant" &&
  971. part.type === "tool-invocation" &&
  972. part.toolInvocation.toolName === "grep" &&
  973. part
  974. }
  975. >
  976. {(_part) => {
  977. const matches = () =>
  978. toolData()?.metadata?.matches
  979. const splitArgs = () => {
  980. const { pattern, ...rest } = toolData()?.args
  981. return { pattern, rest }
  982. }
  983. return (
  984. <div
  985. id={anchor()}
  986. data-section="part"
  987. data-part-type="tool-grep"
  988. >
  989. <div data-section="decoration">
  990. <AnchorIcon id={anchor()}>
  991. <IconDocumentMagnifyingGlass
  992. width={18}
  993. height={18}
  994. />
  995. </AnchorIcon>
  996. <div></div>
  997. </div>
  998. <div data-section="content">
  999. <div data-part-tool-body>
  1000. <div data-part-title>
  1001. <span data-element-label>Grep</span>
  1002. <b>
  1003. &ldquo;{splitArgs().pattern}&rdquo;
  1004. </b>
  1005. </div>
  1006. <Show
  1007. when={
  1008. Object.keys(splitArgs().rest)
  1009. .length > 0
  1010. }
  1011. >
  1012. <div data-part-tool-args>
  1013. <For
  1014. each={flattenToolArgs(
  1015. splitArgs().rest,
  1016. )}
  1017. >
  1018. {([name, value]) => (
  1019. <>
  1020. <div></div>
  1021. <div>{name}</div>
  1022. <div>{value}</div>
  1023. </>
  1024. )}
  1025. </For>
  1026. </div>
  1027. </Show>
  1028. <Switch>
  1029. <Match when={matches() > 0}>
  1030. <div data-part-tool-result>
  1031. <ResultsButton
  1032. showCopy={
  1033. matches() === 1
  1034. ? "1 match"
  1035. : `${matches()} matches`
  1036. }
  1037. hideCopy="Hide matches"
  1038. results={showResults()}
  1039. onClick={() =>
  1040. setShowResults((e) => !e)
  1041. }
  1042. />
  1043. <Show when={showResults()}>
  1044. <TextPart
  1045. expand
  1046. data-size="sm"
  1047. data-color="dimmed"
  1048. text={toolData()?.result}
  1049. />
  1050. </Show>
  1051. </div>
  1052. </Match>
  1053. <Match when={toolData()?.result}>
  1054. <div data-part-tool-result>
  1055. <TextPart
  1056. expand
  1057. data-size="sm"
  1058. data-color="dimmed"
  1059. text={toolData()?.result}
  1060. />
  1061. </div>
  1062. </Match>
  1063. </Switch>
  1064. </div>
  1065. <ToolFooter
  1066. time={toolData()?.duration || 0}
  1067. />
  1068. </div>
  1069. </div>
  1070. )
  1071. }}
  1072. </Match>
  1073. {/* Glob tool */}
  1074. <Match
  1075. when={
  1076. msg.role === "assistant" &&
  1077. part.type === "tool-invocation" &&
  1078. part.toolInvocation.toolName === "glob" &&
  1079. part
  1080. }
  1081. >
  1082. {(_part) => {
  1083. const count = () => toolData()?.metadata?.count
  1084. const pattern = () => toolData()?.args.pattern
  1085. return (
  1086. <div
  1087. id={anchor()}
  1088. data-section="part"
  1089. data-part-type="tool-glob"
  1090. >
  1091. <div data-section="decoration">
  1092. <AnchorIcon id={anchor()}>
  1093. <IconMagnifyingGlass
  1094. width={18}
  1095. height={18}
  1096. />
  1097. </AnchorIcon>
  1098. <div></div>
  1099. </div>
  1100. <div data-section="content">
  1101. <div data-part-tool-body>
  1102. <div data-part-title>
  1103. <span data-element-label>Glob</span>
  1104. <b>&ldquo;{pattern()}&rdquo;</b>
  1105. </div>
  1106. <Switch>
  1107. <Match when={count() > 0}>
  1108. <div data-part-tool-result>
  1109. <ResultsButton
  1110. showCopy={
  1111. count() === 1
  1112. ? "1 result"
  1113. : `${count()} results`
  1114. }
  1115. results={showResults()}
  1116. onClick={() =>
  1117. setShowResults((e) => !e)
  1118. }
  1119. />
  1120. <Show when={showResults()}>
  1121. <TextPart
  1122. expand
  1123. text={toolData()?.result}
  1124. data-size="sm"
  1125. data-color="dimmed"
  1126. />
  1127. </Show>
  1128. </div>
  1129. </Match>
  1130. <Match when={toolData()?.result}>
  1131. <div data-part-tool-result>
  1132. <TextPart
  1133. expand
  1134. text={toolData()?.result}
  1135. data-size="sm"
  1136. data-color="dimmed"
  1137. />
  1138. </div>
  1139. </Match>
  1140. </Switch>
  1141. </div>
  1142. <ToolFooter
  1143. time={toolData()?.duration || 0}
  1144. />
  1145. </div>
  1146. </div>
  1147. )
  1148. }}
  1149. </Match>
  1150. {/* LS tool */}
  1151. <Match
  1152. when={
  1153. msg.role === "assistant" &&
  1154. part.type === "tool-invocation" &&
  1155. part.toolInvocation.toolName === "list" &&
  1156. part
  1157. }
  1158. >
  1159. {(_part) => {
  1160. const path = createMemo(() =>
  1161. toolData()?.args?.path !== data().rootDir
  1162. ? stripWorkingDirectory(
  1163. toolData()?.args?.path,
  1164. data().rootDir,
  1165. )
  1166. : toolData()?.args?.path,
  1167. )
  1168. return (
  1169. <div
  1170. id={anchor()}
  1171. data-section="part"
  1172. data-part-type="tool-list"
  1173. >
  1174. <div data-section="decoration">
  1175. <AnchorIcon id={anchor()}>
  1176. <IconRectangleStack
  1177. width={18}
  1178. height={18}
  1179. />
  1180. </AnchorIcon>
  1181. <div></div>
  1182. </div>
  1183. <div data-section="content">
  1184. <div data-part-tool-body>
  1185. <div data-part-title>
  1186. <span data-element-label>LS</span>
  1187. <b title={toolData()?.args?.path}>
  1188. {path()}
  1189. </b>
  1190. </div>
  1191. <Switch>
  1192. <Match when={toolData()?.result}>
  1193. <div data-part-tool-result>
  1194. <ResultsButton
  1195. results={showResults()}
  1196. onClick={() =>
  1197. setShowResults((e) => !e)
  1198. }
  1199. />
  1200. <Show when={showResults()}>
  1201. <TextPart
  1202. expand
  1203. data-size="sm"
  1204. data-color="dimmed"
  1205. text={toolData()?.result}
  1206. />
  1207. </Show>
  1208. </div>
  1209. </Match>
  1210. </Switch>
  1211. </div>
  1212. <ToolFooter
  1213. time={toolData()?.duration || 0}
  1214. />
  1215. </div>
  1216. </div>
  1217. )
  1218. }}
  1219. </Match>
  1220. {/* Read tool */}
  1221. <Match
  1222. when={
  1223. msg.role === "assistant" &&
  1224. part.type === "tool-invocation" &&
  1225. part.toolInvocation.toolName === "read" &&
  1226. part
  1227. }
  1228. >
  1229. {(_part) => {
  1230. const filePath = createMemo(() =>
  1231. stripWorkingDirectory(
  1232. toolData()?.args?.filePath,
  1233. data().rootDir,
  1234. ),
  1235. )
  1236. const hasError = () =>
  1237. toolData()?.metadata?.error
  1238. const preview = () =>
  1239. toolData()?.metadata?.preview
  1240. return (
  1241. <div
  1242. id={anchor()}
  1243. data-section="part"
  1244. data-part-type="tool-read"
  1245. >
  1246. <div data-section="decoration">
  1247. <AnchorIcon id={anchor()}>
  1248. <IconDocument width={18} height={18} />
  1249. </AnchorIcon>
  1250. <div></div>
  1251. </div>
  1252. <div data-section="content">
  1253. <div data-part-tool-body>
  1254. <div data-part-title>
  1255. <span data-element-label>Read</span>
  1256. <b title={toolData()?.args?.filePath}>
  1257. {filePath()}
  1258. </b>
  1259. </div>
  1260. <Switch>
  1261. <Match when={hasError()}>
  1262. <div data-part-tool-result>
  1263. <ErrorPart>
  1264. {formatErrorString(
  1265. toolData()?.result,
  1266. )}
  1267. </ErrorPart>
  1268. </div>
  1269. </Match>
  1270. {/* Always try to show CodeBlock if preview is available (even if empty string) */}
  1271. <Match
  1272. when={typeof preview() === "string"}
  1273. >
  1274. <div data-part-tool-result>
  1275. <ResultsButton
  1276. showCopy="Show preview"
  1277. hideCopy="Hide preview"
  1278. results={showResults()}
  1279. onClick={() =>
  1280. setShowResults((e) => !e)
  1281. }
  1282. />
  1283. <Show when={showResults()}>
  1284. <div data-part-tool-code>
  1285. <CodeBlock
  1286. lang={getShikiLang(
  1287. filePath(),
  1288. )}
  1289. code={preview()}
  1290. />
  1291. </div>
  1292. </Show>
  1293. </div>
  1294. </Match>
  1295. {/* Fallback to TextPart if preview is not a string (e.g. undefined) AND result exists */}
  1296. <Match
  1297. when={
  1298. typeof preview() !== "string" &&
  1299. toolData()?.result
  1300. }
  1301. >
  1302. <div data-part-tool-result>
  1303. <ResultsButton
  1304. results={showResults()}
  1305. onClick={() =>
  1306. setShowResults((e) => !e)
  1307. }
  1308. />
  1309. <Show when={showResults()}>
  1310. <TextPart
  1311. expand
  1312. text={toolData()?.result}
  1313. data-size="sm"
  1314. data-color="dimmed"
  1315. />
  1316. </Show>
  1317. </div>
  1318. </Match>
  1319. </Switch>
  1320. </div>
  1321. <ToolFooter
  1322. time={toolData()?.duration || 0}
  1323. />
  1324. </div>
  1325. </div>
  1326. )
  1327. }}
  1328. </Match>
  1329. {/* Write tool */}
  1330. <Match
  1331. when={
  1332. msg.role === "assistant" &&
  1333. part.type === "tool-invocation" &&
  1334. part.toolInvocation.toolName === "write" &&
  1335. part
  1336. }
  1337. >
  1338. {(_part) => {
  1339. const filePath = createMemo(() =>
  1340. stripWorkingDirectory(
  1341. toolData()?.args?.filePath,
  1342. data().rootDir,
  1343. ),
  1344. )
  1345. const hasError = () =>
  1346. toolData()?.metadata?.error
  1347. const content = () => toolData()?.args?.content
  1348. const diagnostics = createMemo(() =>
  1349. getDiagnostics(
  1350. toolData()?.metadata?.diagnostics,
  1351. toolData()?.args.filePath,
  1352. ),
  1353. )
  1354. return (
  1355. <div
  1356. id={anchor()}
  1357. data-section="part"
  1358. data-part-type="tool-write"
  1359. >
  1360. <div data-section="decoration">
  1361. <AnchorIcon id={anchor()}>
  1362. <IconDocumentPlus
  1363. width={18}
  1364. height={18}
  1365. />
  1366. </AnchorIcon>
  1367. <div></div>
  1368. </div>
  1369. <div data-section="content">
  1370. <div data-part-tool-body>
  1371. <div data-part-title>
  1372. <span data-element-label>Write</span>
  1373. <b title={toolData()?.args?.filePath}>
  1374. {filePath()}
  1375. </b>
  1376. </div>
  1377. <Show when={diagnostics().length > 0}>
  1378. <ErrorPart>{diagnostics()}</ErrorPart>
  1379. </Show>
  1380. <Switch>
  1381. <Match when={hasError()}>
  1382. <div data-part-tool-result>
  1383. <ErrorPart>
  1384. {formatErrorString(
  1385. toolData()?.result,
  1386. )}
  1387. </ErrorPart>
  1388. </div>
  1389. </Match>
  1390. <Match when={content()}>
  1391. <div data-part-tool-result>
  1392. <ResultsButton
  1393. showCopy="Show contents"
  1394. hideCopy="Hide contents"
  1395. results={showResults()}
  1396. onClick={() =>
  1397. setShowResults((e) => !e)
  1398. }
  1399. />
  1400. <Show when={showResults()}>
  1401. <div data-part-tool-code>
  1402. <CodeBlock
  1403. lang={getShikiLang(
  1404. filePath(),
  1405. )}
  1406. code={
  1407. toolData()?.args?.content
  1408. }
  1409. />
  1410. </div>
  1411. </Show>
  1412. </div>
  1413. </Match>
  1414. </Switch>
  1415. </div>
  1416. <ToolFooter
  1417. time={toolData()?.duration || 0}
  1418. />
  1419. </div>
  1420. </div>
  1421. )
  1422. }}
  1423. </Match>
  1424. {/* Edit tool */}
  1425. <Match
  1426. when={
  1427. msg.role === "assistant" &&
  1428. part.type === "tool-invocation" &&
  1429. part.toolInvocation.toolName === "edit" &&
  1430. part
  1431. }
  1432. >
  1433. {(_part) => {
  1434. const diff = () => toolData()?.metadata?.diff
  1435. const message = () =>
  1436. toolData()?.metadata?.message
  1437. const hasError = () =>
  1438. toolData()?.metadata?.error
  1439. const filePath = createMemo(() =>
  1440. stripWorkingDirectory(
  1441. toolData()?.args.filePath,
  1442. data().rootDir,
  1443. ),
  1444. )
  1445. const diagnostics = createMemo(() =>
  1446. getDiagnostics(
  1447. toolData()?.metadata?.diagnostics,
  1448. toolData()?.args.filePath,
  1449. ),
  1450. )
  1451. return (
  1452. <div
  1453. id={anchor()}
  1454. data-section="part"
  1455. data-part-type="tool-edit"
  1456. >
  1457. <div data-section="decoration">
  1458. <AnchorIcon id={anchor()}>
  1459. <IconPencilSquare
  1460. width={18}
  1461. height={18}
  1462. />
  1463. </AnchorIcon>
  1464. <div></div>
  1465. </div>
  1466. <div data-section="content">
  1467. <div data-part-tool-body>
  1468. <div data-part-title>
  1469. <span data-element-label>Edit</span>
  1470. <b title={toolData()?.args?.filePath}>
  1471. {filePath()}
  1472. </b>
  1473. </div>
  1474. <Switch>
  1475. <Match when={hasError()}>
  1476. <div data-part-tool-result>
  1477. <ErrorPart>
  1478. {formatErrorString(message())}
  1479. </ErrorPart>
  1480. </div>
  1481. </Match>
  1482. <Match when={diff()}>
  1483. <div data-part-tool-edit>
  1484. <DiffView
  1485. class={
  1486. styles["diff-code-block"]
  1487. }
  1488. diff={diff()}
  1489. lang={getShikiLang(filePath())}
  1490. />
  1491. </div>
  1492. </Match>
  1493. </Switch>
  1494. <Show when={diagnostics().length > 0}>
  1495. <ErrorPart>{diagnostics()}</ErrorPart>
  1496. </Show>
  1497. </div>
  1498. <ToolFooter
  1499. time={toolData()?.duration || 0}
  1500. />
  1501. </div>
  1502. </div>
  1503. )
  1504. }}
  1505. </Match>
  1506. {/* Bash tool */}
  1507. <Match
  1508. when={
  1509. msg.role === "assistant" &&
  1510. part.type === "tool-invocation" &&
  1511. part.toolInvocation.toolName === "bash" &&
  1512. part
  1513. }
  1514. >
  1515. {(_part) => {
  1516. const command = () =>
  1517. toolData()?.metadata?.title
  1518. const desc = () =>
  1519. toolData()?.metadata?.description
  1520. const result = () =>
  1521. toolData()?.metadata?.stdout
  1522. const error = () => toolData()?.metadata?.stderr
  1523. return (
  1524. <div
  1525. id={anchor()}
  1526. data-section="part"
  1527. data-part-type="tool-bash"
  1528. >
  1529. <div data-section="decoration">
  1530. <AnchorIcon id={anchor()}>
  1531. <IconCommandLine
  1532. width={18}
  1533. height={18}
  1534. />
  1535. </AnchorIcon>
  1536. <div></div>
  1537. </div>
  1538. <div data-section="content">
  1539. {command() && (
  1540. <div data-part-tool-body>
  1541. <TerminalPart
  1542. desc={desc()}
  1543. data-size="sm"
  1544. command={command()!}
  1545. result={result()}
  1546. error={error()}
  1547. />
  1548. </div>
  1549. )}
  1550. <ToolFooter
  1551. time={toolData()?.duration || 0}
  1552. />
  1553. </div>
  1554. </div>
  1555. )
  1556. }}
  1557. </Match>
  1558. {/* Todo write */}
  1559. <Match
  1560. when={
  1561. msg.role === "assistant" &&
  1562. part.type === "tool-invocation" &&
  1563. part.toolInvocation.toolName === "todowrite" &&
  1564. part
  1565. }
  1566. >
  1567. {(_part) => {
  1568. const todos = createMemo(() =>
  1569. sortTodosByStatus(
  1570. toolData()?.args?.todos ?? [],
  1571. ),
  1572. )
  1573. const starting = () =>
  1574. todos().every((t) => t.status === "pending")
  1575. const finished = () =>
  1576. todos().every((t) => t.status === "completed")
  1577. return (
  1578. <div
  1579. id={anchor()}
  1580. data-section="part"
  1581. data-part-type="tool-todo"
  1582. >
  1583. <div data-section="decoration">
  1584. <AnchorIcon id={anchor()}>
  1585. <IconQueueList width={18} height={18} />
  1586. </AnchorIcon>
  1587. <div></div>
  1588. </div>
  1589. <div data-section="content">
  1590. <div data-part-tool-body>
  1591. <div data-part-title>
  1592. <span data-element-label>
  1593. <Switch fallback="Updating plan">
  1594. <Match when={starting()}>
  1595. Creating plan
  1596. </Match>
  1597. <Match when={finished()}>
  1598. Completing plan
  1599. </Match>
  1600. </Switch>
  1601. </span>
  1602. </div>
  1603. <Show when={todos().length > 0}>
  1604. <ul class={styles.todos}>
  1605. <For each={todos()}>
  1606. {(todo) => (
  1607. <li data-status={todo.status}>
  1608. <span></span>
  1609. {todo.content}
  1610. </li>
  1611. )}
  1612. </For>
  1613. </ul>
  1614. </Show>
  1615. </div>
  1616. <ToolFooter
  1617. time={toolData()?.duration || 0}
  1618. />
  1619. </div>
  1620. </div>
  1621. )
  1622. }}
  1623. </Match>
  1624. {/* Fetch tool */}
  1625. <Match
  1626. when={
  1627. msg.role === "assistant" &&
  1628. part.type === "tool-invocation" &&
  1629. part.toolInvocation.toolName === "webfetch" &&
  1630. part
  1631. }
  1632. >
  1633. {(_part) => {
  1634. const url = () => toolData()?.args.url
  1635. const format = () => toolData()?.args.format
  1636. const hasError = () =>
  1637. toolData()?.metadata?.error
  1638. return (
  1639. <div
  1640. id={anchor()}
  1641. data-section="part"
  1642. data-part-type="tool-fetch"
  1643. >
  1644. <div data-section="decoration">
  1645. <AnchorIcon id={anchor()}>
  1646. <IconGlobeAlt width={18} height={18} />
  1647. </AnchorIcon>
  1648. <div></div>
  1649. </div>
  1650. <div data-section="content">
  1651. <div data-part-tool-body>
  1652. <div data-part-title>
  1653. <span data-element-label>Fetch</span>
  1654. <b>{url()}</b>
  1655. </div>
  1656. <Switch>
  1657. <Match when={hasError()}>
  1658. <div data-part-tool-result>
  1659. <ErrorPart>
  1660. {formatErrorString(
  1661. toolData()?.result,
  1662. )}
  1663. </ErrorPart>
  1664. </div>
  1665. </Match>
  1666. <Match when={toolData()?.result}>
  1667. <div data-part-tool-result>
  1668. <ResultsButton
  1669. results={showResults()}
  1670. onClick={() =>
  1671. setShowResults((e) => !e)
  1672. }
  1673. />
  1674. <Show when={showResults()}>
  1675. <div data-part-tool-code>
  1676. <CodeBlock
  1677. lang={format() || "text"}
  1678. code={toolData()?.result}
  1679. />
  1680. </div>
  1681. </Show>
  1682. </div>
  1683. </Match>
  1684. </Switch>
  1685. </div>
  1686. <ToolFooter
  1687. time={toolData()?.duration || 0}
  1688. />
  1689. </div>
  1690. </div>
  1691. )
  1692. }}
  1693. </Match>
  1694. {/* Tool call */}
  1695. <Match
  1696. when={
  1697. msg.role === "assistant" &&
  1698. part.type === "tool-invocation" &&
  1699. part
  1700. }
  1701. >
  1702. {(part) => {
  1703. return (
  1704. <div
  1705. id={anchor()}
  1706. data-section="part"
  1707. data-part-type="tool-fallback"
  1708. >
  1709. <div data-section="decoration">
  1710. <AnchorIcon id={anchor()}>
  1711. <IconWrenchScrewdriver
  1712. width={18}
  1713. height={18}
  1714. />
  1715. </AnchorIcon>
  1716. <div></div>
  1717. </div>
  1718. <div data-section="content">
  1719. <div data-part-tool-body>
  1720. <div data-part-title>
  1721. {part().toolInvocation.toolName}
  1722. </div>
  1723. <div data-part-tool-args>
  1724. <For
  1725. each={flattenToolArgs(
  1726. part().toolInvocation.args,
  1727. )}
  1728. >
  1729. {(arg) => (
  1730. <>
  1731. <div></div>
  1732. <div>{arg[0]}</div>
  1733. <div>{arg[1]}</div>
  1734. </>
  1735. )}
  1736. </For>
  1737. </div>
  1738. <Switch>
  1739. <Match when={toolData()?.result}>
  1740. <div data-part-tool-result>
  1741. <ResultsButton
  1742. results={showResults()}
  1743. onClick={() =>
  1744. setShowResults((e) => !e)
  1745. }
  1746. />
  1747. <Show when={showResults()}>
  1748. <TextPart
  1749. expand
  1750. data-size="sm"
  1751. data-color="dimmed"
  1752. text={toolData()?.result}
  1753. />
  1754. </Show>
  1755. </div>
  1756. </Match>
  1757. <Match
  1758. when={
  1759. part().toolInvocation.state ===
  1760. "call"
  1761. }
  1762. >
  1763. <TextPart
  1764. data-size="sm"
  1765. data-color="dimmed"
  1766. text="Calling..."
  1767. />
  1768. </Match>
  1769. </Switch>
  1770. </div>
  1771. <ToolFooter
  1772. time={toolData()?.duration || 0}
  1773. />
  1774. </div>
  1775. </div>
  1776. )
  1777. }}
  1778. </Match>
  1779. {/* Fallback */}
  1780. <Match when={true}>
  1781. <div
  1782. id={anchor()}
  1783. data-section="part"
  1784. data-part-type="fallback"
  1785. >
  1786. <div data-section="decoration">
  1787. <AnchorIcon id={anchor()}>
  1788. <Switch
  1789. fallback={
  1790. <IconWrenchScrewdriver
  1791. width={16}
  1792. height={16}
  1793. />
  1794. }
  1795. >
  1796. <Match
  1797. when={
  1798. msg.role === "assistant" &&
  1799. part.type !== "tool-invocation"
  1800. }
  1801. >
  1802. <IconSparkles width={18} height={18} />
  1803. </Match>
  1804. <Match when={msg.role === "user"}>
  1805. <IconUserCircle
  1806. width={18}
  1807. height={18}
  1808. />
  1809. </Match>
  1810. </Switch>
  1811. </AnchorIcon>
  1812. <div></div>
  1813. </div>
  1814. <div data-section="content">
  1815. <div data-part-tool-body>
  1816. <div data-part-title>
  1817. <span data-element-label>
  1818. {part.type}
  1819. </span>
  1820. </div>
  1821. <TextPart
  1822. text={JSON.stringify(part, null, 2)}
  1823. />
  1824. </div>
  1825. </div>
  1826. </div>
  1827. </Match>
  1828. </Switch>
  1829. )
  1830. }}
  1831. </For>
  1832. </Suspense>
  1833. )}
  1834. </For>
  1835. </SuspenseList>
  1836. <div data-section="part" data-part-type="summary">
  1837. <div data-section="decoration">
  1838. <span data-status={connectionStatus()[0]}></span>
  1839. <div></div>
  1840. </div>
  1841. <div data-section="content">
  1842. <p data-section="copy">{getStatusText(connectionStatus())}</p>
  1843. <ul data-section="stats">
  1844. <li>
  1845. <span data-element-label>Cost</span>
  1846. {data().cost !== undefined ? (
  1847. <span>${data().cost.toFixed(2)}</span>
  1848. ) : (
  1849. <span data-placeholder>&mdash;</span>
  1850. )}
  1851. </li>
  1852. <li>
  1853. <span data-element-label>Input Tokens</span>
  1854. {data().tokens.input ? (
  1855. <span>{data().tokens.input}</span>
  1856. ) : (
  1857. <span data-placeholder>&mdash;</span>
  1858. )}
  1859. </li>
  1860. <li>
  1861. <span data-element-label>Output Tokens</span>
  1862. {data().tokens.output ? (
  1863. <span>{data().tokens.output}</span>
  1864. ) : (
  1865. <span data-placeholder>&mdash;</span>
  1866. )}
  1867. </li>
  1868. <li>
  1869. <span data-element-label>Reasoning Tokens</span>
  1870. {data().tokens.reasoning ? (
  1871. <span>{data().tokens.reasoning}</span>
  1872. ) : (
  1873. <span data-placeholder>&mdash;</span>
  1874. )}
  1875. </li>
  1876. </ul>
  1877. </div>
  1878. </div>
  1879. </div>
  1880. </Show>
  1881. </div>
  1882. <Show when={debug}>
  1883. <div style={{ margin: "2rem 0" }}>
  1884. <div
  1885. style={{
  1886. border: "1px solid #ccc",
  1887. padding: "1rem",
  1888. "overflow-y": "auto",
  1889. }}
  1890. >
  1891. <Show
  1892. when={data().messages.length > 0}
  1893. fallback={<p>Waiting for messages...</p>}
  1894. >
  1895. <ul style={{ "list-style-type": "none", padding: 0 }}>
  1896. <For each={data().messages}>
  1897. {(msg) => (
  1898. <li
  1899. style={{
  1900. padding: "0.75rem",
  1901. margin: "0.75rem 0",
  1902. "box-shadow": "0 1px 3px rgba(0,0,0,0.1)",
  1903. }}
  1904. >
  1905. <div>
  1906. <strong>Key:</strong> {msg.id}
  1907. </div>
  1908. <pre>{JSON.stringify(msg, null, 2)}</pre>
  1909. </li>
  1910. )}
  1911. </For>
  1912. </ul>
  1913. </Show>
  1914. </div>
  1915. </div>
  1916. </Show>
  1917. <Show when={showScrollButton()}>
  1918. <button
  1919. type="button"
  1920. class={styles["scroll-button"]}
  1921. onClick={() =>
  1922. document.body.scrollIntoView({ behavior: "smooth", block: "end" })
  1923. }
  1924. onMouseEnter={() => {
  1925. setIsButtonHovered(true)
  1926. if (scrollTimeout) {
  1927. clearTimeout(scrollTimeout)
  1928. }
  1929. }}
  1930. onMouseLeave={() => {
  1931. setIsButtonHovered(false)
  1932. if (showScrollButton()) {
  1933. scrollTimeout = window.setTimeout(() => {
  1934. if (!isButtonHovered()) {
  1935. setShowScrollButton(false)
  1936. }
  1937. }, 3000)
  1938. }
  1939. }}
  1940. title="Scroll to bottom"
  1941. aria-label="Scroll to bottom"
  1942. >
  1943. <IconArrowDown width={20} height={20} />
  1944. </button>
  1945. </Show>
  1946. </main>
  1947. )
  1948. }