This commit is contained in:
Dax Raad 2025-07-06 23:49:12 -04:00
parent b6ab75f2d4
commit 5fba41fe28
10 changed files with 197 additions and 823 deletions

View file

@ -1,246 +0,0 @@
import { type Component, createMemo } from "solid-js"
import { parsePatch } from "diff"
import CodeBlock from "./CodeBlock"
import styles from "./diffview.module.css"
type DiffRow = {
left: string
right: string
type: "added" | "removed" | "unchanged" | "modified"
}
interface DiffViewProps {
diff: string
lang?: string
class?: string
}
const DiffView: Component<DiffViewProps> = (props) => {
const rows = createMemo(() => {
const diffRows: DiffRow[] = []
try {
const patches = parsePatch(props.diff)
for (const patch of patches) {
for (const hunk of patch.hunks) {
const lines = hunk.lines
let i = 0
while (i < lines.length) {
const line = lines[i]
const content = line.slice(1)
const prefix = line[0]
if (prefix === '-') {
// Look ahead for consecutive additions to pair with removals
const removals: string[] = [content]
let j = i + 1
// Collect all consecutive removals
while (j < lines.length && lines[j][0] === '-') {
removals.push(lines[j].slice(1))
j++
}
// Collect all consecutive additions that follow
const additions: string[] = []
while (j < lines.length && lines[j][0] === '+') {
additions.push(lines[j].slice(1))
j++
}
// Pair removals with additions
const maxLength = Math.max(removals.length, additions.length)
for (let k = 0; k < maxLength; k++) {
const hasLeft = k < removals.length
const hasRight = k < additions.length
if (hasLeft && hasRight) {
// Replacement - left is removed, right is added
diffRows.push({
left: removals[k],
right: additions[k],
type: "modified"
})
} else if (hasLeft) {
// Pure removal
diffRows.push({
left: removals[k],
right: "",
type: "removed"
})
} else if (hasRight) {
// Pure addition - only create if we actually have content
diffRows.push({
left: "",
right: additions[k],
type: "added"
})
}
}
i = j
} else if (prefix === '+') {
// Standalone addition (not paired with removal)
diffRows.push({
left: "",
right: content,
type: "added"
})
i++
} else if (prefix === ' ') {
diffRows.push({
left: content,
right: content,
type: "unchanged"
})
i++
} else {
i++
}
}
}
}
} catch (error) {
console.error("Failed to parse patch:", error)
return []
}
return diffRows
})
const mobileRows = createMemo(() => {
const mobileBlocks: { type: 'removed' | 'added' | 'unchanged', lines: string[] }[] = []
const currentRows = rows()
let i = 0
while (i < currentRows.length) {
const removedLines: string[] = []
const addedLines: string[] = []
// Collect consecutive modified/removed/added rows
while (i < currentRows.length &&
(currentRows[i].type === 'modified' ||
currentRows[i].type === 'removed' ||
currentRows[i].type === 'added')) {
const row = currentRows[i]
if (row.left && (row.type === 'removed' || row.type === 'modified')) {
removedLines.push(row.left)
}
if (row.right && (row.type === 'added' || row.type === 'modified')) {
addedLines.push(row.right)
}
i++
}
// Add grouped blocks
if (removedLines.length > 0) {
mobileBlocks.push({ type: 'removed', lines: removedLines })
}
if (addedLines.length > 0) {
mobileBlocks.push({ type: 'added', lines: addedLines })
}
// Add unchanged rows as-is
if (i < currentRows.length && currentRows[i].type === 'unchanged') {
mobileBlocks.push({
type: 'unchanged',
lines: [currentRows[i].left]
})
i++
}
}
return mobileBlocks
})
return (
<div class={`${styles.diff} ${props.class ?? ""}`}>
<div class={styles.desktopView}>
{rows().map((r) => (
<div class={styles.row}>
<div class={styles.beforeColumn}>
<CodeBlock
code={r.left}
lang={props.lang}
data-section="cell"
data-diff-type={r.type === "removed" || r.type === "modified" ? "removed" : ""}
/>
</div>
<div class={styles.afterColumn}>
<CodeBlock
code={r.right}
lang={props.lang}
data-section="cell"
data-diff-type={r.type === "added" || r.type === "modified" ? "added" : ""}
/>
</div>
</div>
))}
</div>
<div class={styles.mobileView}>
{mobileRows().map((block) => (
<div class={styles.mobileBlock}>
{block.lines.map((line) => (
<CodeBlock
code={line}
lang={props.lang}
data-section="cell"
data-diff-type={block.type === 'removed' ? 'removed' :
block.type === 'added' ? 'added' : ''}
/>
))}
</div>
))}
</div>
</div>
)
}
export default DiffView
// const testDiff = `--- combined_before.txt 2025-06-24 16:38:08
// +++ combined_after.txt 2025-06-24 16:38:12
// @@ -1,21 +1,25 @@
// unchanged line
// -deleted line
// -old content
// +added line
// +new content
//
// -removed empty line below
// +added empty line above
//
// - tab indented
// -trailing spaces
// -very long line that will definitely wrap in most editors and cause potential alignment issues when displayed in a two column diff view
// -unicode content: 🚀 ✨ 中文
// -mixed content with tabs and spaces
// + space indented
// +no trailing spaces
// +short line
// +very long replacement line that will also wrap and test how the diff viewer handles long line additions after short line removals
// +different unicode: 🎉 💻 日本語
// +normalized content with consistent spacing
// +newline to content
//
// -content to remove
// -whitespace only:
// -multiple
// -consecutive
// -deletions
// -single deletion
// +
// +single addition
// +first addition
// +second addition
// +third addition
// line before addition
// +first added line
// +
// +third added line
// line after addition
// final unchanged line`

View file

@ -1,38 +0,0 @@
import { type JSX, splitProps, createResource } from "solid-js"
import { marked } from "marked"
import markedShiki from "marked-shiki"
import { codeToHtml } from "shiki"
import { transformerNotationDiff } from "@shikijs/transformers"
import styles from "./markdownview.module.css"
interface MarkdownViewProps {
markdown: string
}
const markedWithShiki = marked.use(
markedShiki({
highlight(code, lang) {
return codeToHtml(code, {
lang: lang || "text",
themes: {
light: "github-light",
dark: "github-dark",
},
transformers: [transformerNotationDiff()],
})
},
}),
)
export function MarkdownView(props: MarkdownViewProps) {
const [local, rest] = splitProps(props, ["markdown"])
const [html] = createResource(
() => local.markdown,
async (markdown) => {
return markedWithShiki.parse(markdown)
},
)
return <div innerHTML={html()} class={styles.root} {...rest} />
}

View file

@ -1,121 +0,0 @@
.diff {
display: flex;
flex-direction: column;
border: 1px solid var(--sl-color-divider);
background-color: var(--sl-color-bg-surface);
border-radius: 0.25rem;
}
.desktopView {
display: block;
}
.mobileView {
display: none;
}
.mobileBlock {
display: flex;
flex-direction: column;
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
align-items: stretch;
}
.beforeColumn,
.afterColumn {
display: flex;
flex-direction: column;
overflow-x: visible;
min-width: 0;
align-items: stretch;
}
.beforeColumn {
border-right: 1px solid var(--sl-color-divider);
}
.diff > .row:first-child [data-section="cell"]:first-child {
padding-top: 0.5rem;
}
.diff > .row:last-child [data-section="cell"]:last-child {
padding-bottom: 0.5rem;
}
[data-section="cell"] {
position: relative;
flex: 1;
display: flex;
flex-direction: column;
width: 100%;
padding: 0.1875rem 0.5rem 0.1875rem 2.2ch;
margin: 0;
&[data-display-mobile="true"] {
display: none;
}
pre {
--shiki-dark-bg: var(--sl-color-bg-surface) !important;
background-color: var(--sl-color-bg-surface) !important;
white-space: pre-wrap;
word-break: break-word;
code > span:empty::before {
content: "\00a0";
white-space: pre;
display: inline-block;
width: 0;
}
}
}
[data-diff-type="removed"] {
background-color: var(--sl-color-red-low);
pre {
--shiki-dark-bg: var(--sl-color-red-low) !important;
background-color: var(--sl-color-red-low) !important;
}
&::before {
content: "-";
position: absolute;
left: 0.5ch;
user-select: none;
color: var(--sl-color-red-high);
}
}
[data-diff-type="added"] {
background-color: var(--sl-color-green-low);
pre {
--shiki-dark-bg: var(--sl-color-green-low) !important;
background-color: var(--sl-color-green-low) !important;
}
&::before {
content: "+";
position: absolute;
left: 0.6ch;
user-select: none;
color: var(--sl-color-green-high);
}
}
@media (max-width: 40rem) {
.desktopView {
display: none;
}
.mobileView {
display: block;
}
}

View file

@ -1,108 +0,0 @@
.root {
font-size: 0.875rem;
line-height: 1.5;
p,
blockquote,
ul,
ol,
dl,
table,
pre {
margin-bottom: 1rem;
}
strong {
font-weight: 600;
}
ol {
list-style-position: inside;
padding-left: 0.75rem;
}
ul {
padding-left: 1.5rem;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-size: 0.875rem;
font-weight: 600;
margin-bottom: 0.5rem;
}
&>*:last-child {
margin-bottom: 0;
}
pre {
--shiki-dark-bg: var(--sl-color-bg-surface) !important;
background-color: var(--sl-color-bg-surface) !important;
padding: 0.5rem 0.75rem;
line-height: 1.6;
font-size: 0.75rem;
white-space: pre-wrap;
word-break: break-word;
span {
white-space: break-spaces;
}
}
code {
font-weight: 500;
&:not(pre code) {
&::before {
content: "`";
font-weight: 700;
}
&::after {
content: "`";
font-weight: 700;
}
}
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid var(--sl-color-border);
padding: 0.5rem 0.75rem;
text-align: left;
}
th {
border-bottom: 1px solid var(--sl-color-border);
}
/* Remove outer borders */
table tr:first-child th,
table tr:first-child td {
border-top: none;
}
table tr:last-child td {
border-bottom: none;
}
table th:first-child,
table td:first-child {
border-left: none;
}
table th:last-child,
table td:last-child {
border-right: none;
}
}

View file

@ -5,9 +5,17 @@
border-radius: .25rem;
padding: .5rem calc(.5rem + 3px);
&[data-flush="true"] {
border: none;
padding: 0
}
pre {
--shiki-dark-bg: var(--sl-color-bg-surface) !important;
background-color: var(--sl-color-bg-surface) !important;
line-height: 1.6;
font-size: 0.75rem;
white-space: pre-wrap;
word-break: break-word;
span {
white-space: break-spaces;

View file

@ -1,4 +1,4 @@
import { type JSX, splitProps, createResource } from "solid-js"
import { type JSX, splitProps, createResource, Suspense } from "solid-js"
import { codeToHtml } from "shiki"
import style from "./content-code.module.css"
import { transformerNotationDiff } from "@shikijs/transformers"
@ -6,6 +6,7 @@ import { transformerNotationDiff } from "@shikijs/transformers"
interface Props {
code: string
lang?: string
flush?: boolean
}
export function ContentCode(props: Props) {
const [html] = createResource(
@ -23,6 +24,10 @@ export function ContentCode(props: Props) {
})) as string
},
)
return <div innerHTML={html()} class={style.root} />
return (
<Suspense>
<div innerHTML={html()} class={style.root} data-flush={props.flush === true ? true : undefined} />
</Suspense>
)
}

View file

@ -1,121 +1,130 @@
.diff {
.root {
display: flex;
flex-direction: column;
border: 1px solid var(--sl-color-divider);
background-color: var(--sl-color-bg-surface);
border-radius: 0.25rem;
}
.desktopView {
display: block;
}
[data-component="desktop"] {
display: block;
}
.mobileView {
display: none;
}
.mobileBlock {
display: flex;
flex-direction: column;
}
.row {
display: grid;
grid-template-columns: 1fr 1fr;
align-items: stretch;
}
.beforeColumn,
.afterColumn {
display: flex;
flex-direction: column;
overflow-x: visible;
min-width: 0;
align-items: stretch;
}
.beforeColumn {
border-right: 1px solid var(--sl-color-divider);
}
.diff > .row:first-child [data-section="cell"]:first-child {
padding-top: 0.5rem;
}
.diff > .row:last-child [data-section="cell"]:last-child {
padding-bottom: 0.5rem;
}
[data-section="cell"] {
position: relative;
flex: 1;
display: flex;
flex-direction: column;
width: 100%;
padding: 0.1875rem 0.5rem 0.1875rem 2.2ch;
margin: 0;
&[data-display-mobile="true"] {
[data-component="mobile"] {
display: none;
}
pre {
--shiki-dark-bg: var(--sl-color-bg-surface) !important;
background-color: var(--sl-color-bg-surface) !important;
[data-component="diff-block"] {
display: flex;
flex-direction: column;
}
white-space: pre-wrap;
word-break: break-word;
[data-component="diff-row"] {
display: grid;
grid-template-columns: 1fr 1fr;
align-items: stretch;
code > span:empty::before {
content: "\00a0";
white-space: pre;
display: inline-block;
width: 0;
[data-slot="before"],
[data-slot="after"] {
position: relative;
display: flex;
flex-direction: column;
overflow-x: visible;
min-width: 0;
align-items: stretch;
padding: 0 1rem;
&[data-diff-type="removed"] {
background-color: var(--sl-color-red-low);
pre {
--shiki-dark-bg: var(--sl-color-red-low) !important;
background-color: var(--sl-color-red-low) !important;
}
&::before {
content: "-";
position: absolute;
left: 0.5ch;
top: 1px;
user-select: none;
color: var(--sl-color-red-high);
}
}
&[data-diff-type="added"] {
background-color: var(--sl-color-green-low);
pre {
--shiki-dark-bg: var(--sl-color-green-low) !important;
background-color: var(--sl-color-green-low) !important;
}
&::before {
content: "+";
position: absolute;
user-select: none;
color: var(--sl-color-green-high);
left: 0.5ch;
top: 1px;
}
}
}
[data-slot="before"] {
border-right: 1px solid var(--sl-color-divider);
}
}
.diff>.row:first-child [data-section="cell"]:first-child {
padding-top: 0.5rem;
}
.diff>.row:last-child [data-section="cell"]:last-child {
padding-bottom: 0.5rem;
}
[data-section="cell"] {
position: relative;
flex: 1;
display: flex;
flex-direction: column;
width: 100%;
padding: 0.1875rem 0.5rem 0.1875rem 2.2ch;
margin: 0;
&[data-display-mobile="true"] {
display: none;
}
pre {
--shiki-dark-bg: var(--sl-color-bg-surface) !important;
background-color: var(--sl-color-bg-surface) !important;
white-space: pre-wrap;
word-break: break-word;
code>span:empty::before {
content: "\00a0";
white-space: pre;
display: inline-block;
width: 0;
}
}
}
@media (max-width: 40rem) {
[data-slot="desktop"] {
display: none;
}
[data-slot="mobile"] {
display: block;
}
}
}
[data-diff-type="removed"] {
background-color: var(--sl-color-red-low);
pre {
--shiki-dark-bg: var(--sl-color-red-low) !important;
background-color: var(--sl-color-red-low) !important;
}
&::before {
content: "-";
position: absolute;
left: 0.5ch;
user-select: none;
color: var(--sl-color-red-high);
}
}
[data-diff-type="added"] {
background-color: var(--sl-color-green-low);
pre {
--shiki-dark-bg: var(--sl-color-green-low) !important;
background-color: var(--sl-color-green-low) !important;
}
&::before {
content: "+";
position: absolute;
left: 0.6ch;
user-select: none;
color: var(--sl-color-green-high);
}
}
@media (max-width: 40rem) {
.desktopView {
display: none;
}
.mobileView {
display: block;
}
}

View file

@ -1,7 +1,7 @@
import { type Component, createMemo } from "solid-js"
import { parsePatch } from "diff"
import { ContentCode } from "./content-code"
import styles from "./diffview.module.css"
import styles from "./content-diff.module.css"
type DiffRow = {
left: string
@ -9,14 +9,12 @@ type DiffRow = {
type: "added" | "removed" | "unchanged" | "modified"
}
interface DiffViewProps {
interface Props {
diff: string
lang?: string
class?: string
}
const DiffView: Component<DiffViewProps> = (props) => {
export function ContentDiff(props: Props) {
const rows = createMemo(() => {
const diffRows: DiffRow[] = []
@ -33,20 +31,20 @@ const DiffView: Component<DiffViewProps> = (props) => {
const content = line.slice(1)
const prefix = line[0]
if (prefix === '-') {
if (prefix === "-") {
// Look ahead for consecutive additions to pair with removals
const removals: string[] = [content]
let j = i + 1
// Collect all consecutive removals
while (j < lines.length && lines[j][0] === '-') {
while (j < lines.length && lines[j][0] === "-") {
removals.push(lines[j].slice(1))
j++
}
// Collect all consecutive additions that follow
const additions: string[] = []
while (j < lines.length && lines[j][0] === '+') {
while (j < lines.length && lines[j][0] === "+") {
additions.push(lines[j].slice(1))
j++
}
@ -62,39 +60,39 @@ const DiffView: Component<DiffViewProps> = (props) => {
diffRows.push({
left: removals[k],
right: additions[k],
type: "modified"
type: "modified",
})
} else if (hasLeft) {
// Pure removal
diffRows.push({
left: removals[k],
right: "",
type: "removed"
type: "removed",
})
} else if (hasRight) {
// Pure addition - only create if we actually have content
diffRows.push({
left: "",
right: additions[k],
type: "added"
type: "added",
})
}
}
i = j
} else if (prefix === '+') {
} else if (prefix === "+") {
// Standalone addition (not paired with removal)
diffRows.push({
left: "",
right: content,
type: "added"
type: "added",
})
i++
} else if (prefix === ' ') {
} else if (prefix === " ") {
diffRows.push({
left: content,
right: content,
type: "unchanged"
type: "unchanged",
})
i++
} else {
@ -112,7 +110,7 @@ const DiffView: Component<DiffViewProps> = (props) => {
})
const mobileRows = createMemo(() => {
const mobileBlocks: { type: 'removed' | 'added' | 'unchanged', lines: string[] }[] = []
const mobileBlocks: { type: "removed" | "added" | "unchanged"; lines: string[] }[] = []
const currentRows = rows()
let i = 0
@ -121,15 +119,15 @@ const DiffView: Component<DiffViewProps> = (props) => {
const addedLines: string[] = []
// Collect consecutive modified/removed/added rows
while (i < currentRows.length &&
(currentRows[i].type === 'modified' ||
currentRows[i].type === 'removed' ||
currentRows[i].type === 'added')) {
while (
i < currentRows.length &&
(currentRows[i].type === "modified" || currentRows[i].type === "removed" || currentRows[i].type === "added")
) {
const row = currentRows[i]
if (row.left && (row.type === 'removed' || row.type === 'modified')) {
if (row.left && (row.type === "removed" || row.type === "modified")) {
removedLines.push(row.left)
}
if (row.right && (row.type === 'added' || row.type === 'modified')) {
if (row.right && (row.type === "added" || row.type === "modified")) {
addedLines.push(row.right)
}
i++
@ -137,17 +135,17 @@ const DiffView: Component<DiffViewProps> = (props) => {
// Add grouped blocks
if (removedLines.length > 0) {
mobileBlocks.push({ type: 'removed', lines: removedLines })
mobileBlocks.push({ type: "removed", lines: removedLines })
}
if (addedLines.length > 0) {
mobileBlocks.push({ type: 'added', lines: addedLines })
mobileBlocks.push({ type: "added", lines: addedLines })
}
// Add unchanged rows as-is
if (i < currentRows.length && currentRows[i].type === 'unchanged') {
if (i < currentRows.length && currentRows[i].type === "unchanged") {
mobileBlocks.push({
type: 'unchanged',
lines: [currentRows[i].left]
type: "unchanged",
lines: [currentRows[i].left],
})
i++
}
@ -157,40 +155,37 @@ const DiffView: Component<DiffViewProps> = (props) => {
})
return (
<div class={`${styles.diff} ${props.class ?? ""}`}>
<div class={styles.desktopView}>
<div class={styles.root}>
<div data-component="desktop">
{rows().map((r) => (
<div class={styles.row}>
<div class={styles.beforeColumn}>
<div data-component="diff-row" data-type={r.type}>
<div data-slot="before" data-diff-type={r.type === "removed" || r.type === "modified" ? "removed" : ""}>
<ContentCode
code={r.left}
flush
lang={props.lang}
data-section="cell"
data-diff-type={r.type === "removed" || r.type === "modified" ? "removed" : ""}
/>
</div>
<div class={styles.afterColumn}>
<div data-slot="after" data-diff-type={r.type === "added" || r.type === "modified" ? "added" : ""}>
<ContentCode
code={r.right}
lang={props.lang}
data-section="cell"
data-diff-type={r.type === "added" || r.type === "modified" ? "added" : ""}
flush
/>
</div>
</div>
))}
</div>
<div class={styles.mobileView}>
<div data-component="mobile">
{mobileRows().map((block) => (
<div class={styles.mobileBlock}>
<div data-component="diff-block" data-type={block.type}>
{block.lines.map((line) => (
<ContentCode
code={line}
lang={props.lang}
data-section="cell"
data-diff-type={block.type === 'removed' ? 'removed' :
block.type === 'added' ? 'added' : ''}
data-diff-type={block.type === "removed" ? "removed" : block.type === "added" ? "added" : ""}
/>
))}
</div>
@ -200,7 +195,6 @@ const DiffView: Component<DiffViewProps> = (props) => {
)
}
export default DiffView
// const testDiff = `--- combined_before.txt 2025-06-24 16:38:08
// +++ combined_after.txt 2025-06-24 16:38:12
@ -210,12 +204,12 @@ export default DiffView
// -old content
// +added line
// +new content
//
//
// -removed empty line below
// +added empty line above
//
//
// - tab indented
// -trailing spaces
// -trailing spaces
// -very long line that will definitely wrap in most editors and cause potential alignment issues when displayed in a two column diff view
// -unicode content: 🚀 ✨ 中文
// -mixed content with tabs and spaces
@ -226,14 +220,14 @@ export default DiffView
// +different unicode: 🎉 💻 日本語
// +normalized content with consistent spacing
// +newline to content
//
//
// -content to remove
// -whitespace only:
// -whitespace only:
// -multiple
// -consecutive
// -deletions
// -single deletion
// +
// +
// +single addition
// +first addition
// +second addition

View file

@ -1,4 +1,4 @@
import { createEffect, createMemo, createSignal, For, Match, Show, Switch, type JSX, type ParentProps } from "solid-js"
import { createMemo, createSignal, For, Match, Show, Switch, type JSX, type ParentProps } from "solid-js"
import { IconCheckCircle, IconChevronDown, IconChevronRight, IconHashtag, IconSparkles } from "../icons"
import styles from "./part.module.css"
import type { MessageV2 } from "opencode/session/message-v2"
@ -6,11 +6,11 @@ import { ContentText } from "./content-text"
import { ContentMarkdown } from "./content-markdown"
import { DateTime } from "luxon"
import CodeBlock from "../CodeBlock"
import DiffView from "../DiffView"
import map from "lang-map"
import type { Diagnostic } from "vscode-languageserver-types"
import { BashTool, FallbackTool } from "./tool"
import { ContentCode } from "./content-code"
import { ContentDiff } from "./content-diff"
export interface PartProps {
index: number
@ -88,35 +88,35 @@ export function Part(props: PartProps) {
<div data-slot="model">{props.message.modelID}</div>
</div>
)}
{props.part.type === "tool" && props.part.state.status === "completed" && (
{props.part.type === "tool" && props.part.state.status === "completed" && props.message.role === "assistant" && (
<div data-component="tool" data-tool={props.part.tool}>
<Switch>
<Match when={props.part.tool === "grep"}>
<GrepTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<GrepTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "glob"}>
<GlobTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<GlobTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "list"}>
<ListTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<ListTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "read"}>
<ReadTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<ReadTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "write"}>
<WriteTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<WriteTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "edit"}>
<EditTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<EditTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "bash"}>
<BashTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "todowrite"}>
<TodoWriteTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<TodoWriteTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={props.part.tool === "webfetch"}>
<WebFetchTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
<WebFetchTool message={props.message} id={props.part.id} tool={props.part.tool} state={props.part.state} />
</Match>
<Match when={true}>
<FallbackTool id={props.part.id} tool={props.part.tool} state={props.part.state} />
@ -133,7 +133,7 @@ type ToolProps = {
id: MessageV2.ToolPart["id"]
tool: MessageV2.ToolPart["tool"]
state: MessageV2.ToolStateCompleted
rootDir?: string
message: MessageV2.Assistant
isLastPart?: boolean
}
@ -285,8 +285,8 @@ export function GrepTool(props: ToolProps) {
export function ListTool(props: ToolProps) {
const path = createMemo(() =>
props.state.input?.path !== props.rootDir
? stripWorkingDirectory(props.state.input?.path, props.rootDir)
props.state.input?.path !== props.message.path.cwd
? stripWorkingDirectory(props.state.input?.path, props.message.path.cwd)
: props.state.input?.path,
)
@ -329,9 +329,7 @@ export function WebFetchTool(props: ToolProps) {
</Match>
<Match when={props.state.output}>
<ResultsButton>
<div data-component="code">
<CodeBlock lang={format() || "text"} code={props.state.output} />
</div>
<CodeBlock lang={format() || "text"} code={props.state.output} />
</ResultsButton>
</Match>
</Switch>
@ -341,7 +339,7 @@ export function WebFetchTool(props: ToolProps) {
}
export function ReadTool(props: ToolProps) {
const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.rootDir))
const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.message.path.cwd))
const hasError = () => props.state.metadata?.error
const preview = () => props.state.metadata?.preview
@ -360,9 +358,7 @@ export function ReadTool(props: ToolProps) {
</Match>
<Match when={typeof preview() === "string"}>
<ResultsButton showCopy="Show preview" hideCopy="Hide preview">
<div data-component="code">
<CodeBlock lang={getShikiLang(filePath() || "")} code={preview()} />
</div>
<ContentCode lang={getShikiLang(filePath() || "")} code={preview()} />
</ResultsButton>
</Match>
<Match when={typeof preview() !== "string" && props.state.output}>
@ -377,7 +373,7 @@ export function ReadTool(props: ToolProps) {
}
export function WriteTool(props: ToolProps) {
const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.rootDir))
const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.message.path.cwd))
const hasError = () => props.state.metadata?.error
const content = () => props.state.input?.content
const diagnostics = createMemo(() => getDiagnostics(props.state.metadata?.diagnostics, props.state.input.filePath))
@ -413,7 +409,7 @@ export function EditTool(props: ToolProps) {
const diff = () => props.state.metadata?.diff
const message = () => props.state.metadata?.message
const hasError = () => props.state.metadata?.error
const filePath = createMemo(() => stripWorkingDirectory(props.state.input.filePath, props.rootDir))
const filePath = createMemo(() => stripWorkingDirectory(props.state.input.filePath, props.message.path.cwd))
const diagnostics = createMemo(() => getDiagnostics(props.state.metadata?.diagnostics, props.state.input.filePath))
return (
@ -431,7 +427,7 @@ export function EditTool(props: ToolProps) {
</Match>
<Match when={diff()}>
<div data-component="diff">
<DiffView class={styles.root} diff={diff()} lang={getShikiLang(filePath() || "")} />
<ContentDiff diff={diff()} lang={getShikiLang(filePath() || "")} />
</div>
</Match>
</Switch>

View file

@ -1,10 +1,7 @@
import type { MessageV2 } from "opencode/session/message-v2"
import { Show, For, Switch, Match, createSignal, createMemo, type JSX } from "solid-js"
import type { Diagnostic } from "vscode-languageserver-types"
import map from "lang-map"
import { Show, For, Switch, Match, createSignal, type JSX } from "solid-js"
import CodeBlock from "../CodeBlock"
import DiffView from "../DiffView"
import styles from "../share.module.css"
type ToolProps = {
@ -15,15 +12,6 @@ type ToolProps = {
isLastPart?: boolean
}
interface TextPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
text: string
expand?: boolean
}
interface ErrorPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
expand?: boolean
}
interface TerminalPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
command: string
error?: string
@ -32,80 +20,6 @@ interface TerminalPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
expand?: boolean
}
function stripWorkingDirectory(filePath?: string, workingDir?: string) {
if (filePath === undefined || workingDir === undefined) return filePath
const prefix = workingDir.endsWith("/") ? workingDir : workingDir + "/"
if (filePath === workingDir) {
return ""
}
if (filePath.startsWith(prefix)) {
return filePath.slice(prefix.length)
}
return filePath
}
function getShikiLang(filename: string) {
const ext = filename.split(".").pop()?.toLowerCase() ?? ""
const langs = map.languages(ext)
const type = langs?.[0]?.toLowerCase()
const overrides: Record<string, string> = {
conf: "shellscript",
}
return type ? (overrides[type] ?? type) : "plaintext"
}
function formatErrorString(error: string): JSX.Element {
const errorMarker = "Error: "
const startsWithError = error.startsWith(errorMarker)
return startsWithError ? (
<pre>
<span data-color="red" data-marker="label" data-separator>
Error
</span>
<span>{error.slice(errorMarker.length)}</span>
</pre>
) : (
<pre>
<span data-color="dimmed">{error}</span>
</pre>
)
}
function getDiagnostics(diagnosticsByFile: Record<string, Diagnostic[]>, currentFile: string): JSX.Element[] {
const result: JSX.Element[] = []
if (diagnosticsByFile === undefined || diagnosticsByFile[currentFile] === undefined) return result
for (const diags of Object.values(diagnosticsByFile)) {
for (const d of diags) {
if (d.severity !== 1) continue
const line = d.range.start.line + 1
const column = d.range.start.character + 1
result.push(
<pre>
<span data-color="red" data-marker="label">
Error
</span>
<span data-color="dimmed" data-separator>
[{line}:{column}]
</span>
<span>{d.message}</span>
</pre>,
)
}
}
return result
}
interface ResultsButtonProps extends JSX.HTMLAttributes<HTMLButtonElement> {
showCopy?: string
@ -120,6 +34,10 @@ function ResultsButton(props: ResultsButtonProps) {
)
}
interface TextPartProps extends JSX.HTMLAttributes<HTMLDivElement> {
text: string
expand?: boolean
}
function TextPart(props: TextPartProps) {
return (
<div class={styles["message-text"]} data-expanded={props.expand === true} {...props}>
@ -128,14 +46,6 @@ function TextPart(props: TextPartProps) {
)
}
function ErrorPart(props: ErrorPartProps) {
return (
<div class={styles["message-error"]} data-expanded={props.expand === true} {...props}>
<div data-section="content">{props.children}</div>
</div>
)
}
function TerminalPart(props: TerminalPartProps) {
return (
<div class={styles["message-terminal"]} data-expanded={props.expand === true} {...props}>
@ -159,41 +69,6 @@ function TerminalPart(props: TerminalPartProps) {
)
}
export function GlobTool(props: ToolProps) {
const [showResults, setShowResults] = createSignal(false)
const count = () => props.state.metadata?.count
const pattern = () => props.state.input.pattern
return (
<>
<div data-part-title>
<span data-element-label>Glob</span>
<b>&ldquo;{pattern()}&rdquo;</b>
</div>
<Switch>
<Match when={count() && count() > 0}>
<div data-part-tool-result>
<ResultsButton
showCopy={count() === 1 ? "1 result" : `${count()} results`}
results={showResults()}
onClick={() => setShowResults((e) => !e)}
/>
<Show when={showResults()}>
<TextPart expand text={props.state.output} data-size="sm" data-color="dimmed" />
</Show>
</div>
</Match>
<Match when={props.state.output}>
<div data-part-tool-result>
<TextPart expand text={props.state.output} data-size="sm" data-color="dimmed" />
</div>
</Match>
</Switch>
</>
)
}
export function BashTool(props: ToolProps) {
const command = () => props.state.metadata?.title
const desc = () => props.state.metadata?.description