diff options
| author | David Hill <[email protected]> | 2025-10-30 22:53:00 +0000 |
|---|---|---|
| committer | David Hill <[email protected]> | 2025-10-30 22:53:00 +0000 |
| commit | 2f9f189f390401b3314c74bd9f83e64b2420cd65 (patch) | |
| tree | b275ca785719a54115752a79d4b66984ba125670 /packages/desktop/src/components | |
| parent | f3c70f4ea8818bde5b1228482f72af9b10c71796 (diff) | |
| parent | a3ba740de41eb1e4825a99dc8f519e1225357e55 (diff) | |
| download | opencode-2f9f189f390401b3314c74bd9f83e64b2420cd65.tar.gz opencode-2f9f189f390401b3314c74bd9f83e64b2420cd65.zip | |
Merge branch 'dev' of https://github.com/sst/opencode into dev
Diffstat (limited to 'packages/desktop/src/components')
| -rw-r--r-- | packages/desktop/src/components/code.tsx | 2 | ||||
| -rw-r--r-- | packages/desktop/src/components/diff-changes.tsx | 20 | ||||
| -rw-r--r-- | packages/desktop/src/components/markdown.tsx | 23 | ||||
| -rw-r--r-- | packages/desktop/src/components/message.tsx | 459 | ||||
| -rw-r--r-- | packages/desktop/src/components/progress-circle.tsx | 48 | ||||
| -rw-r--r-- | packages/desktop/src/components/prompt-input.tsx | 15 | ||||
| -rw-r--r-- | packages/desktop/src/components/session-timeline.tsx | 536 |
7 files changed, 13 insertions, 1090 deletions
diff --git a/packages/desktop/src/components/code.tsx b/packages/desktop/src/components/code.tsx index 11518e73a..c214fd5e6 100644 --- a/packages/desktop/src/components/code.tsx +++ b/packages/desktop/src/components/code.tsx @@ -2,7 +2,7 @@ import { bundledLanguages, type BundledLanguage, type ShikiTransformer } from "s import { splitProps, type ComponentProps, createEffect, onMount, onCleanup, createMemo, createResource } from "solid-js" import { useLocal, type TextSelection } from "@/context/local" import { getFileExtension, getNodeOffsetInLine, getSelectionInContainer } from "@/utils" -import { useShiki } from "@/context/shiki" +import { useShiki } from "@opencode-ai/ui" type DefinedSelection = Exclude<TextSelection, undefined> diff --git a/packages/desktop/src/components/diff-changes.tsx b/packages/desktop/src/components/diff-changes.tsx deleted file mode 100644 index 3b633f70f..000000000 --- a/packages/desktop/src/components/diff-changes.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import { FileDiff } from "@opencode-ai/sdk" -import { createMemo, Show } from "solid-js" - -export function DiffChanges(props: { diff: FileDiff | FileDiff[] }) { - const additions = createMemo(() => - Array.isArray(props.diff) ? props.diff.reduce((acc, diff) => acc + (diff.additions ?? 0), 0) : props.diff.additions, - ) - const deletions = createMemo(() => - Array.isArray(props.diff) ? props.diff.reduce((acc, diff) => acc + (diff.deletions ?? 0), 0) : props.diff.deletions, - ) - const total = createMemo(() => additions() + deletions()) - return ( - <Show when={total() > 0}> - <div class="flex gap-2 justify-end items-center"> - <span class="text-12-mono text-right text-text-diff-add-base">{`+${additions()}`}</span> - <span class="text-12-mono text-right text-text-diff-delete-base">{`-${deletions()}`}</span> - </div> - </Show> - ) -} diff --git a/packages/desktop/src/components/markdown.tsx b/packages/desktop/src/components/markdown.tsx deleted file mode 100644 index e0f185f5f..000000000 --- a/packages/desktop/src/components/markdown.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import { useMarked } from "@/context/marked" -import { createResource } from "solid-js" - -function strip(text: string): string { - const wrappedRe = /^\s*<([A-Za-z]\w*)>\s*([\s\S]*?)\s*<\/\1>\s*$/ - const match = text.match(wrappedRe) - return match ? match[2] : text -} -export function Markdown(props: { text: string; class?: string }) { - const marked = useMarked() - const [html] = createResource( - () => strip(props.text), - async (markdown) => { - return marked.parse(markdown) - }, - ) - return ( - <div - class={`min-w-0 max-w-full overflow-auto no-scrollbar text-14-regular text-text-base ${props.class ?? ""}`} - innerHTML={html()} - /> - ) -} diff --git a/packages/desktop/src/components/message.tsx b/packages/desktop/src/components/message.tsx deleted file mode 100644 index 589ca3118..000000000 --- a/packages/desktop/src/components/message.tsx +++ /dev/null @@ -1,459 +0,0 @@ -import type { Part, ReasoningPart, TextPart, ToolPart, Message, AssistantMessage, UserMessage } from "@opencode-ai/sdk" -import { children, Component, createMemo, For, Match, Show, Switch, type JSX } from "solid-js" -import { Dynamic } from "solid-js/web" -import { Markdown } from "./markdown" -import { Checkbox, Collapsible, Diff, Icon, IconProps } from "@opencode-ai/ui" -import { getDirectory, getFilename } from "@/utils" -import type { Tool } from "opencode/tool/tool" -import type { ReadTool } from "opencode/tool/read" -import type { ListTool } from "opencode/tool/ls" -import type { GlobTool } from "opencode/tool/glob" -import type { GrepTool } from "opencode/tool/grep" -import type { WebFetchTool } from "opencode/tool/webfetch" -import type { TaskTool } from "opencode/tool/task" -import type { BashTool } from "opencode/tool/bash" -import type { EditTool } from "opencode/tool/edit" -import type { WriteTool } from "opencode/tool/write" -import type { TodoWriteTool } from "opencode/tool/todo" -import { DiffChanges } from "./diff-changes" - -export function Message(props: { message: Message; parts: Part[] }) { - return ( - <Switch> - <Match when={props.message.role === "user" && props.message}> - {(userMessage) => <UserMessage message={userMessage()} parts={props.parts} />} - </Match> - <Match when={props.message.role === "assistant" && props.message}> - {(assistantMessage) => <AssistantMessage message={assistantMessage()} parts={props.parts} />} - </Match> - </Switch> - ) -} - -function AssistantMessage(props: { message: AssistantMessage; parts: Part[] }) { - const filteredParts = createMemo(() => { - return props.parts?.filter((x) => { - if (x.type === "reasoning") return false - return x.type !== "tool" || x.tool !== "todoread" - }) - }) - return ( - <div class="w-full flex flex-col items-start gap-4"> - <For each={filteredParts()}>{(part) => <Part part={part} message={props.message} />}</For> - </div> - ) -} - -function UserMessage(props: { message: UserMessage; parts: Part[] }) { - const text = createMemo(() => - props.parts - ?.filter((p) => p.type === "text" && !p.synthetic) - ?.map((p) => (p as TextPart).text) - ?.join(""), - ) - return <div class="text-12-regular text-text-base line-clamp-3">{text()}</div> -} - -export function Part(props: { part: Part; message: Message; hideDetails?: boolean }) { - const component = createMemo(() => PART_MAPPING[props.part.type as keyof typeof PART_MAPPING]) - return ( - <Show when={component()}> - <Dynamic - component={component()} - part={props.part as any} - message={props.message} - hideDetails={props.hideDetails} - /> - </Show> - ) -} - -const PART_MAPPING = { - text: TextPart, - tool: ToolPart, - reasoning: ReasoningPart, -} - -function ReasoningPart(props: { part: ReasoningPart; message: Message }) { - return ( - <Show when={props.part.text.trim()}> - <Markdown text={props.part.text.trim()} /> - </Show> - ) -} - -function TextPart(props: { part: TextPart; message: Message }) { - return ( - <Show when={props.part.text.trim()}> - <Markdown text={props.part.text.trim()} /> - </Show> - ) -} - -function ToolPart(props: { part: ToolPart; message: Message; hideDetails?: boolean }) { - const component = createMemo(() => { - const render = ToolRegistry.render(props.part.tool) ?? GenericTool - const metadata = props.part.state.status === "pending" ? {} : (props.part.state.metadata ?? {}) - const input = props.part.state.status === "completed" ? props.part.state.input : {} - - return ( - <Dynamic - component={render} - input={input} - tool={props.part.tool} - metadata={metadata} - output={props.part.state.status === "completed" ? props.part.state.output : undefined} - hideDetails={props.hideDetails} - /> - ) - }) - - return <Show when={component()}>{component()}</Show> -} - -type TriggerTitle = { - title: string - titleClass?: string - subtitle?: string - subtitleClass?: string - args?: string[] - argsClass?: string - action?: JSX.Element -} - -const isTriggerTitle = (val: any): val is TriggerTitle => { - return typeof val === "object" && val !== null && "title" in val && !(val instanceof Node) -} - -function BasicTool(props: { - icon: IconProps["name"] - trigger: TriggerTitle | JSX.Element - children?: JSX.Element - hideDetails?: boolean -}) { - const resolved = children(() => props.children) - return ( - <Collapsible> - <Collapsible.Trigger> - <div class="w-full flex items-center self-stretch gap-5 justify-between"> - <div class="w-full flex items-center self-stretch gap-5"> - <Icon name={props.icon} size="small" class="shrink-0" /> - <div class="grow min-w-0"> - <Switch> - <Match when={isTriggerTitle(props.trigger) && props.trigger}> - {(trigger) => ( - <div class="w-full flex items-center gap-2 justify-between"> - <div class="flex items-center gap-2 whitespace-nowrap truncate"> - <span - classList={{ - "text-12-medium text-text-base": true, - [trigger().titleClass ?? ""]: !!trigger().titleClass, - }} - > - {trigger().title} - </span> - <Show when={trigger().subtitle}> - <span - classList={{ - "text-12-medium text-text-weak": true, - [trigger().subtitleClass ?? ""]: !!trigger().subtitleClass, - }} - > - {trigger().subtitle} - </span> - </Show> - <Show when={trigger().args?.length}> - <For each={trigger().args}> - {(arg) => ( - <span - classList={{ - "text-12-regular text-text-weak": true, - [trigger().argsClass ?? ""]: !!trigger().argsClass, - }} - > - {arg} - </span> - )} - </For> - </Show> - </div> - <Show when={trigger().action}>{trigger().action}</Show> - </div> - )} - </Match> - <Match when={true}>{props.trigger as JSX.Element}</Match> - </Switch> - </div> - </div> - <Show when={resolved() && !props.hideDetails}> - <Collapsible.Arrow /> - </Show> - </div> - </Collapsible.Trigger> - <Show when={resolved() && !props.hideDetails}> - <Collapsible.Content>{resolved()}</Collapsible.Content> - </Show> - </Collapsible> - // <> - // <Show when={props.part.state.status === "error"}>{props.part.state.error.replace("Error: ", "")}</Show> - // </> - ) -} - -function GenericTool(props: ToolProps<any>) { - return <BasicTool icon="mcp" trigger={{ title: props.tool }} hideDetails={props.hideDetails} /> -} - -type ToolProps<T extends Tool.Info> = { - input: Partial<Tool.InferParameters<T>> - metadata: Partial<Tool.InferMetadata<T>> - tool: string - output?: string - hideDetails?: boolean -} - -const ToolRegistry = (() => { - const state: Record< - string, - { - name: string - render?: Component<ToolProps<any>> - } - > = {} - function register<T extends Tool.Info>(input: { name: string; render?: Component<ToolProps<T>> }) { - state[input.name] = input - return input - } - return { - register, - render(name: string) { - return state[name]?.render - }, - } -})() - -ToolRegistry.register<typeof ReadTool>({ - name: "read", - render(props) { - return ( - <BasicTool - icon="glasses" - trigger={{ title: "Read", subtitle: props.input.filePath ? getFilename(props.input.filePath) : "" }} - /> - ) - }, -}) - -ToolRegistry.register<typeof ListTool>({ - name: "list", - render(props) { - return ( - <BasicTool icon="bullet-list" trigger={{ title: "List", subtitle: getDirectory(props.input.path || "/") }}> - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof GlobTool>({ - name: "glob", - render(props) { - return ( - <BasicTool - icon="magnifying-glass-menu" - trigger={{ - title: "Glob", - subtitle: getDirectory(props.input.path || "/"), - args: props.input.pattern ? ["pattern=" + props.input.pattern] : [], - }} - > - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof GrepTool>({ - name: "grep", - render(props) { - const args = [] - if (props.input.pattern) args.push("pattern=" + props.input.pattern) - if (props.input.include) args.push("include=" + props.input.include) - return ( - <BasicTool - icon="magnifying-glass-menu" - trigger={{ - title: "Grep", - subtitle: getDirectory(props.input.path || "/"), - args, - }} - > - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof WebFetchTool>({ - name: "webfetch", - render(props) { - return ( - <BasicTool - icon="window-cursor" - trigger={{ - title: "Webfetch", - subtitle: props.input.url || "", - args: props.input.format ? ["format=" + props.input.format] : [], - action: ( - <div class="size-6 flex items-center justify-center"> - <Icon name="square-arrow-top-right" size="small" /> - </div> - ), - }} - > - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof TaskTool>({ - name: "task", - render(props) { - return ( - <BasicTool - icon="task" - trigger={{ - title: `${props.input.subagent_type || props.tool} Agent`, - titleClass: "capitalize", - subtitle: props.input.description, - }} - > - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof BashTool>({ - name: "bash", - render(props) { - return ( - <BasicTool - icon="console" - trigger={{ - title: "Shell", - subtitle: "Ran " + props.input.command, - }} - > - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof EditTool>({ - name: "edit", - render(props) { - return ( - <BasicTool - icon="code-lines" - trigger={ - <div class="flex items-center justify-between w-full"> - <div class="flex items-center gap-2"> - <div class="text-12-medium text-text-base capitalize">Edit</div> - <div class="flex"> - <Show when={props.input.filePath?.includes("/")}> - <span class="text-text-weak">{getDirectory(props.input.filePath!)}</span> - </Show> - <span class="text-text-strong">{getFilename(props.input.filePath ?? "")}</span> - </div> - </div> - <div class="flex gap-4 items-center justify-end"> - <Show when={props.metadata.filediff}> - <DiffChanges diff={props.metadata.filediff} /> - </Show> - </div> - </div> - } - > - <Show when={props.metadata.filediff}> - <div class="border-t border-border-weaker-base"> - <Diff - before={{ name: getFilename(props.metadata.filediff.path), contents: props.metadata.filediff.before }} - after={{ name: getFilename(props.metadata.filediff.path), contents: props.metadata.filediff.after }} - /> - </div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof WriteTool>({ - name: "write", - render(props) { - return ( - <BasicTool - icon="code-lines" - trigger={ - <div class="flex items-center justify-between w-full"> - <div class="flex items-center gap-2"> - <div class="text-12-medium text-text-base capitalize">Write</div> - <div class="flex"> - <Show when={props.input.filePath?.includes("/")}> - <span class="text-text-weak">{getDirectory(props.input.filePath!)}</span> - </Show> - <span class="text-text-strong">{getFilename(props.input.filePath ?? "")}</span> - </div> - </div> - <div class="flex gap-4 items-center justify-end">{/* <DiffChanges diff={diff} /> */}</div> - </div> - } - > - <Show when={false && props.output}> - <div class="whitespace-pre">{props.output}</div> - </Show> - </BasicTool> - ) - }, -}) - -ToolRegistry.register<typeof TodoWriteTool>({ - name: "todowrite", - render(props) { - return ( - <BasicTool - icon="checklist" - trigger={{ - title: "To-dos", - subtitle: `${props.input.todos?.filter((t) => t.status === "completed").length}/${props.input.todos?.length}`, - }} - > - <Show when={props.input.todos?.length}> - <div class="px-12 pt-2.5 pb-6 flex flex-col gap-2"> - <For each={props.input.todos}> - {(todo) => ( - <Checkbox readOnly checked={todo.status === "completed"}> - <div classList={{ "line-through text-text-weaker": todo.status === "completed" }}>{todo.content}</div> - </Checkbox> - )} - </For> - </div> - </Show> - </BasicTool> - ) - }, -}) diff --git a/packages/desktop/src/components/progress-circle.tsx b/packages/desktop/src/components/progress-circle.tsx deleted file mode 100644 index d56197ed3..000000000 --- a/packages/desktop/src/components/progress-circle.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import { Component, createMemo } from "solid-js" - -interface ProgressCircleProps { - percentage: number - size?: number - strokeWidth?: number -} - -export const ProgressCircle: Component<ProgressCircleProps> = (props) => { - // --- Set default values for props --- - const size = () => props.size || 16 - const strokeWidth = () => props.strokeWidth || 3 - - // --- Constants for SVG calculation --- - const viewBoxSize = 16 - const center = viewBoxSize / 2 - const radius = () => center - strokeWidth() / 2 - const circumference = createMemo(() => 2 * Math.PI * radius()) - - // --- Reactive Calculation for the progress offset --- - const offset = createMemo(() => { - const clampedPercentage = Math.max(0, Math.min(100, props.percentage || 0)) - const progress = clampedPercentage / 100 - return circumference() * (1 - progress) - }) - - return ( - <svg - width={size()} - height={size()} - viewBox={`0 0 ${viewBoxSize} ${viewBoxSize}`} - fill="none" - class="transform -rotate-90" - > - <circle cx={center} cy={center} r={radius()} class="stroke-border-weak-base" stroke-width={strokeWidth()} /> - <circle - cx={center} - cy={center} - r={radius()} - class="stroke-border-active" - stroke-width={strokeWidth()} - stroke-dasharray={circumference().toString()} - stroke-dashoffset={offset()} - style={{ transition: "stroke-dashoffset 0.35s cubic-bezier(0.65, 0, 0.35, 1)" }} - /> - </svg> - ) -} diff --git a/packages/desktop/src/components/prompt-input.tsx b/packages/desktop/src/components/prompt-input.tsx index d6276c158..f1c19388e 100644 --- a/packages/desktop/src/components/prompt-input.tsx +++ b/packages/desktop/src/components/prompt-input.tsx @@ -71,7 +71,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => { } }) - const { flat, active, onInput, onKeyDown } = useFilteredList<string>({ + const { flat, active, onInput, onKeyDown, refetch } = useFilteredList<string>({ items: local.file.search, key: (x) => x, onSelect: (path) => { @@ -81,6 +81,11 @@ export const PromptInput: Component<PromptInputProps> = (props) => { }, }) + createEffect(() => { + local.model.recent() + refetch() + }) + createEffect( on( () => store.contentParts, @@ -369,16 +374,20 @@ export const PromptInput: Component<PromptInputProps> = (props) => { items={local.model.list()} current={local.model.current()} filterKeys={["provider.name", "name", "id"]} - groupBy={(x) => x.provider.name} + groupBy={(x) => (local.model.recent().includes(x) ? "Recent" : x.provider.name)} sortGroupsBy={(a, b) => { const order = ["opencode", "anthropic", "github-copilot", "openai", "google", "openrouter", "vercel"] + if (a.category === "Recent" && b.category !== "Recent") return -1 + if (b.category === "Recent" && a.category !== "Recent") return 1 const aProvider = a.items[0].provider.id const bProvider = b.items[0].provider.id if (order.includes(aProvider) && !order.includes(bProvider)) return -1 if (!order.includes(aProvider) && order.includes(bProvider)) return 1 return order.indexOf(aProvider) - order.indexOf(bProvider) }} - onSelect={(x) => local.model.set(x ? { modelID: x.id, providerID: x.provider.id } : undefined)} + onSelect={(x) => + local.model.set(x ? { modelID: x.id, providerID: x.provider.id } : undefined, { recent: true }) + } trigger={ <Button as="div" variant="ghost"> {local.model.current()?.name ?? "Select model"} diff --git a/packages/desktop/src/components/session-timeline.tsx b/packages/desktop/src/components/session-timeline.tsx deleted file mode 100644 index e1f3beae4..000000000 --- a/packages/desktop/src/components/session-timeline.tsx +++ /dev/null @@ -1,536 +0,0 @@ -import { Icon, Tooltip } from "@opencode-ai/ui" -import { Collapsible } from "@/ui" -import type { AssistantMessage, Message, Part, ToolPart } from "@opencode-ai/sdk" -import { DateTime } from "luxon" -import { - createSignal, - For, - Match, - splitProps, - Switch, - type ComponentProps, - type ParentProps, - createEffect, - createMemo, - Show, -} from "solid-js" -import { getFilename } from "@/utils" -import { Markdown } from "./markdown" -import { Code } from "./code" -import { createElementSize } from "@solid-primitives/resize-observer" -import { createScrollPosition } from "@solid-primitives/scroll" -import { ProgressCircle } from "./progress-circle" -import { pipe, sumBy } from "remeda" -import { useSync } from "@/context/sync" -import { useLocal } from "@/context/local" - -function Part(props: ParentProps & ComponentProps<"div">) { - const [local, others] = splitProps(props, ["class", "classList", "children"]) - return ( - <div - classList={{ - ...(local.classList ?? {}), - "h-6 flex items-center": true, - [local.class ?? ""]: !!local.class, - }} - {...others} - > - <p class="text-12-medium text-left">{local.children}</p> - </div> - ) -} - -function CollapsiblePart(props: { title: ParentProps["children"] } & ParentProps & ComponentProps<typeof Collapsible>) { - return ( - <Collapsible {...props}> - <Collapsible.Trigger class="peer/collapsible"> - <Part>{props.title}</Part> - </Collapsible.Trigger> - <Collapsible.Content> - <p class="flex-auto min-w-0 text-pretty"> - <span class="text-12-medium text-text-weak break-words">{props.children}</span> - </p> - </Collapsible.Content> - </Collapsible> - ) -} - -function ReadToolPart(props: { part: ToolPart }) { - const sync = useSync() - const local = useLocal() - return ( - <Switch> - <Match when={props.part.state.status === "pending"}> - <Part>Reading file...</Part> - </Match> - <Match when={props.part.state.status === "completed" && props.part.state}> - {(state) => { - const path = state().input["filePath"] as string - return ( - <Part onClick={() => local.file.open(path)}> - <span class="">Read</span> {getFilename(path)} - </Part> - ) - }} - </Match> - <Match when={props.part.state.status === "error" && props.part.state}> - {(state) => ( - <div> - <Part> - <span class="">Read</span> {getFilename(state().input["filePath"] as string)} - </Part> - <div class="text-icon-critical-active">{sync.sanitize(state().error)}</div> - </div> - )} - </Match> - </Switch> - ) -} - -function EditToolPart(props: { part: ToolPart }) { - const sync = useSync() - return ( - <Switch> - <Match when={props.part.state.status === "pending"}> - <Part>Preparing edit...</Part> - </Match> - <Match when={props.part.state.status === "completed" && props.part.state}> - {(state) => ( - <CollapsiblePart - title={ - <> - <span class="">Edit</span> {getFilename(state().input["filePath"] as string)} - </> - } - > - <Code path={state().input["filePath"] as string} code={state().metadata["diff"] as string} /> - </CollapsiblePart> - )} - </Match> - <Match when={props.part.state.status === "error" && props.part.state}> - {(state) => ( - <CollapsiblePart - title={ - <> - <span class="">Edit</span> {getFilename(state().input["filePath"] as string)} - </> - } - > - <div class="text-icon-critical-active">{sync.sanitize(state().error)}</div> - </CollapsiblePart> - )} - </Match> - </Switch> - ) -} - -function WriteToolPart(props: { part: ToolPart }) { - const sync = useSync() - return ( - <Switch> - <Match when={props.part.state.status === "pending"}> - <Part>Preparing write...</Part> - </Match> - <Match when={props.part.state.status === "completed" && props.part.state}> - {(state) => ( - <CollapsiblePart - title={ - <> - <span class="">Write</span> {getFilename(state().input["filePath"] as string)} - </> - } - > - <div class="p-2 bg-background-panel rounded-md border border-border-subtle"></div> - </CollapsiblePart> - )} - </Match> - <Match when={props.part.state.status === "error" && props.part.state}> - {(state) => ( - <div> - <Part> - <span class="">Write</span> {getFilename(state().input["filePath"] as string)} - </Part> - <div class="text-icon-critical-active">{sync.sanitize(state().error)}</div> - </div> - )} - </Match> - </Switch> - ) -} - -function BashToolPart(props: { part: ToolPart }) { - const sync = useSync() - return ( - <Switch> - <Match when={props.part.state.status === "pending"}> - <Part>Writing shell command...</Part> - </Match> - <Match when={props.part.state.status === "completed" && props.part.state}> - {(state) => ( - <CollapsiblePart - defaultOpen - title={ - <> - <span class="">Run command:</span> {state().input["command"]} - </> - } - > - <Markdown text={`\`\`\`command\n${state().input["command"]}\n${state().output}\`\`\``} /> - </CollapsiblePart> - )} - </Match> - <Match when={props.part.state.status === "error" && props.part.state}> - {(state) => ( - <CollapsiblePart - title={ - <> - <span class="">Shell</span> {state().input["command"]} - </> - } - > - <div class="text-icon-critical-active">{sync.sanitize(state().error)}</div> - </CollapsiblePart> - )} - </Match> - </Switch> - ) -} - -function ToolPart(props: { part: ToolPart }) { - // read - // edit - // write - // bash - // ls - // glob - // grep - // todowrite - // todoread - // webfetch - // websearch - // patch - // task - return ( - <div class="min-w-0 flex-auto text-12-medium"> - <Switch - fallback={ - <span> - {props.part.type}:{props.part.tool} - </span> - } - > - <Match when={props.part.tool === "read"}> - <ReadToolPart part={props.part} /> - </Match> - <Match when={props.part.tool === "edit"}> - <EditToolPart part={props.part} /> - </Match> - <Match when={props.part.tool === "write"}> - <WriteToolPart part={props.part} /> - </Match> - <Match when={props.part.tool === "bash"}> - <BashToolPart part={props.part} /> - </Match> - </Switch> - </div> - ) -} - -export default function SessionTimeline(props: { session: string; class?: string }) { - const sync = useSync() - const [scrollElement, setScrollElement] = createSignal<HTMLElement | undefined>(undefined) - const [root, setRoot] = createSignal<HTMLDivElement | undefined>(undefined) - const [tail, setTail] = createSignal(true) - const size = createElementSize(root) - const scroll = createScrollPosition(scrollElement) - - const valid = (part: Part) => { - if (!part) return false - switch (part.type) { - case "step-start": - case "step-finish": - case "file": - case "patch": - return false - case "text": - return !part.synthetic && part.text.trim() - case "reasoning": - return part.text.trim() - case "tool": - switch (part.tool) { - case "todoread": - case "todowrite": - case "list": - case "grep": - return false - } - return true - default: - return true - } - } - - const hasValidParts = (message: Message) => { - return sync.data.part[message.id]?.filter(valid).length > 0 - } - - const hasTextPart = (message: Message) => { - return !!sync.data.part[message.id]?.filter(valid).find((p) => p.type === "text") - } - - const session = createMemo(() => sync.session.get(props.session)) - const messages = createMemo(() => sync.data.message[props.session] ?? []) - const messagesWithValidParts = createMemo(() => sync.data.message[props.session]?.filter(hasValidParts) ?? []) - const working = createMemo(() => { - const last = messages()[messages().length - 1] - if (!last) return false - if (last.role === "user") return true - return !last.time.completed - }) - - const cost = createMemo(() => { - const total = pipe( - messages(), - sumBy((x) => (x.role === "assistant" ? x.cost : 0)), - ) - return new Intl.NumberFormat("en-US", { - style: "currency", - currency: "USD", - }).format(total) - }) - - const last = createMemo(() => { - return messages().findLast((x) => x.role === "assistant") as AssistantMessage - }) - - const model = createMemo(() => { - if (!last()) return - const model = sync.data.provider.find((x) => x.id === last().providerID)?.models[last().modelID] - return model - }) - - const tokens = createMemo(() => { - if (!last()) return - const tokens = last().tokens - const total = tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write - return new Intl.NumberFormat("en-US", { - notation: "compact", - compactDisplay: "short", - }).format(total) - }) - - const context = createMemo(() => { - if (!last()) return - if (!model()?.limit.context) return 0 - const tokens = last().tokens - const total = tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write - return Math.round((total / model()!.limit.context) * 100) - }) - - const getScrollParent = (el: HTMLElement | null): HTMLElement | undefined => { - let p = el?.parentElement - while (p && p !== document.body) { - const s = getComputedStyle(p) - if (s.overflowY === "auto" || s.overflowY === "scroll") return p - p = p.parentElement - } - return undefined - } - - createEffect(() => { - if (!root()) return - setScrollElement(getScrollParent(root()!)) - }) - - const scrollToBottom = () => { - const element = scrollElement() - if (!element) return - element.scrollTop = element.scrollHeight - } - - createEffect(() => { - size.height - if (tail()) scrollToBottom() - }) - - createEffect(() => { - if (working()) { - setTail(true) - scrollToBottom() - } - }) - - let lastScrollY = 0 - createEffect(() => { - if (scroll.y < lastScrollY) { - setTail(false) - } - lastScrollY = scroll.y - }) - - const duration = (part: Part) => { - switch (part.type) { - default: - if ( - "time" in part && - part.time && - "start" in part.time && - part.time.start && - "end" in part.time && - part.time.end - ) { - const start = DateTime.fromMillis(part.time.start) - const end = DateTime.fromMillis(part.time.end) - return end.diff(start).toFormat("s") - } - return "" - } - } - - createEffect(() => { - console.log("WHAT") - console.log(JSON.stringify(messagesWithValidParts())) - }) - - return ( - <div - ref={setRoot} - classList={{ - "select-text flex flex-col text-text-weak": true, - [props.class ?? ""]: !!props.class, - }} - > - <div class="flex justify-end items-center self-stretch"> - <div class="flex items-center gap-6"> - <Tooltip value={`${tokens()} Tokens`} class="flex items-center gap-1.5"> - <Show when={context()}> - <ProgressCircle percentage={context()!} /> - </Show> - <div class="text-14-regular text-text-weak text-right">{context()}%</div> - </Tooltip> - <div class="text-14-regular text-text-strong text-right">{cost()}</div> - </div> - </div> - <ul role="list" class="flex flex-col items-start self-stretch"> - <For each={messagesWithValidParts()}> - {(message) => ( - <div - classList={{ - "flex flex-col gap-1 justify-center items-start self-stretch": true, - "mt-6": hasTextPart(message), - }} - > - <For each={sync.data.part[message.id]?.filter(valid) ?? []}> - {(part) => ( - <li class="group/li"> - <Switch fallback={<div class="">{part.type}</div>}> - <Match when={part.type === "text" && part}> - {(part) => ( - <Switch> - <Match when={message.role === "user"}> - <div class="w-fit flex items-center px-3 py-1 rounded-md bg-surface-weak"> - <span class="text-14-regular text-text-strong whitespace-pre-wrap break-words"> - {part().text} - </span> - </div> - </Match> - <Match when={message.role === "assistant"}> - <Markdown text={sync.sanitize(part().text)} /> - </Match> - </Switch> - )} - </Match> - <Match when={part.type === "reasoning" && part}> - {(part) => ( - <CollapsiblePart - title={ - <Switch fallback={<span class="text-text-weak">Thinking</span>}> - <Match when={part().time.end}> - <span class="text-12-medium text-text-weak">Thought</span> for {duration(part())}s - </Match> - </Switch> - } - > - <Markdown text={part().text} /> - </CollapsiblePart> - )} - </Match> - <Match when={part.type === "tool" && part}>{(part) => <ToolPart part={part()} />}</Match> - </Switch> - </li> - )} - </For> - </div> - )} - </For> - </ul> - <Show when={false}> - <Collapsible defaultOpen={false}> - <Collapsible.Trigger> - <div class="mt-12 ml-1 flex items-center gap-x-2 text-xs text-text-muted"> - <Icon name="file-code" /> - <span>Raw Session Data</span> - <Collapsible.Arrow class="text-text-muted" /> - </div> - </Collapsible.Trigger> - <Collapsible.Content class="mt-5"> - <ul role="list" class="space-y-2"> - <li> - <Collapsible> - <Collapsible.Trigger> - <div class="flex items-center gap-x-2 text-xs text-text-muted ml-1"> - <Icon name="file-code" /> - <span>session</span> - <Collapsible.Arrow class="text-text-muted" /> - </div> - </Collapsible.Trigger> - <Collapsible.Content> - <Code path="session.json" code={JSON.stringify(session(), null, 2)} /> - </Collapsible.Content> - </Collapsible> - </li> - <For each={messages()}> - {(message) => ( - <> - <li> - <Collapsible> - <Collapsible.Trigger> - <div class="flex items-center gap-x-2 text-xs text-text-muted ml-1"> - <Icon name="file-code" /> - <span>{message.role === "user" ? "user" : "assistant"}</span> - <Collapsible.Arrow class="text-text-muted" /> - </div> - </Collapsible.Trigger> - <Collapsible.Content> - <Code path={message.id + ".json"} code={JSON.stringify(message, null, 2)} /> - </Collapsible.Content> - </Collapsible> - </li> - <For each={sync.data.part[message.id]}> - {(part) => ( - <li> - <Collapsible> - <Collapsible.Trigger> - <div class="flex items-center gap-x-2 text-xs text-text-muted ml-1"> - <Icon name="file-code" /> - <span>{part.type}</span> - <Collapsible.Arrow class="text-text-muted" /> - </div> - </Collapsible.Trigger> - <Collapsible.Content> - <Code path={message.id + "." + part.id + ".json"} code={JSON.stringify(part, null, 2)} /> - </Collapsible.Content> - </Collapsible> - </li> - )} - </For> - </> - )} - </For> - </ul> - </Collapsible.Content> - </Collapsible> - </Show> - </div> - ) -} |
