mirror of
https://github.com/sst/opencode.git
synced 2025-07-07 16:14:59 +00:00
ui progress
This commit is contained in:
parent
8eaf25b319
commit
b6ab75f2d4
21 changed files with 2177 additions and 1667 deletions
|
@ -31,7 +31,8 @@
|
|||
},
|
||||
"license": "MIT",
|
||||
"prettier": {
|
||||
"semi": false
|
||||
"semi": false,
|
||||
"printWidth": 120
|
||||
},
|
||||
"trustedDependencies": [
|
||||
"esbuild",
|
||||
|
|
|
@ -34,11 +34,12 @@ export namespace MessageV2 {
|
|||
.openapi({
|
||||
ref: "ToolStateRunning",
|
||||
})
|
||||
export type ToolStateRunning = z.infer<typeof ToolStateRunning>
|
||||
|
||||
export const ToolStateCompleted = z
|
||||
.object({
|
||||
status: z.literal("completed"),
|
||||
input: z.any(),
|
||||
input: z.record(z.any()),
|
||||
output: z.string(),
|
||||
title: z.string(),
|
||||
metadata: z.record(z.any()),
|
||||
|
@ -50,11 +51,12 @@ export namespace MessageV2 {
|
|||
.openapi({
|
||||
ref: "ToolStateCompleted",
|
||||
})
|
||||
export type ToolStateCompleted = z.infer<typeof ToolStateCompleted>
|
||||
|
||||
export const ToolStateError = z
|
||||
.object({
|
||||
status: z.literal("error"),
|
||||
input: z.any(),
|
||||
input: z.record(z.any()),
|
||||
error: z.string(),
|
||||
time: z.object({
|
||||
start: z.number(),
|
||||
|
@ -64,6 +66,7 @@ export namespace MessageV2 {
|
|||
.openapi({
|
||||
ref: "ToolStateError",
|
||||
})
|
||||
export type ToolStateError = z.infer<typeof ToolStateError>
|
||||
|
||||
export const ToolState = z
|
||||
.discriminatedUnion("status", [
|
||||
|
|
|
@ -4,6 +4,7 @@ import { Bus } from "../bus"
|
|||
import path from "path"
|
||||
import z from "zod"
|
||||
import fs from "fs/promises"
|
||||
import { MessageV2 } from "../session/message-v2"
|
||||
|
||||
export namespace Storage {
|
||||
const log = Log.create({ service: "storage" })
|
||||
|
@ -15,33 +16,64 @@ export namespace Storage {
|
|||
),
|
||||
}
|
||||
|
||||
const state = App.state("storage", () => {
|
||||
type Migration = (dir: string) => Promise<void>
|
||||
|
||||
const MIGRATIONS: Migration[] = [
|
||||
async (dir: string) => {
|
||||
const files = new Bun.Glob("session/message/*/*.json").scanSync({
|
||||
cwd: dir,
|
||||
absolute: true,
|
||||
})
|
||||
for (const file of files) {
|
||||
const content = await Bun.file(file).json()
|
||||
if (!content.metadata) continue
|
||||
log.info("migrating to v2 message", { file })
|
||||
const result = MessageV2.fromV1(content)
|
||||
await Bun.write(file, JSON.stringify(result, null, 2))
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
const state = App.state("storage", async () => {
|
||||
const app = App.info()
|
||||
const dir = path.join(app.path.data, "storage")
|
||||
log.info("init", { path: dir })
|
||||
const migration = await Bun.file(path.join(dir, "migration"))
|
||||
.json()
|
||||
.then((x) => parseInt(x))
|
||||
.catch(() => 0)
|
||||
for (let index = migration; index < MIGRATIONS.length; index++) {
|
||||
log.info("running migration", { index })
|
||||
const migration = MIGRATIONS[index]
|
||||
await migration(dir)
|
||||
await Bun.write(path.join(dir, "migration"), (index + 1).toString())
|
||||
}
|
||||
return {
|
||||
dir,
|
||||
}
|
||||
})
|
||||
|
||||
export async function remove(key: string) {
|
||||
const target = path.join(state().dir, key + ".json")
|
||||
const dir = await state().then((x) => x.dir)
|
||||
const target = path.join(dir, key + ".json")
|
||||
await fs.unlink(target).catch(() => {})
|
||||
}
|
||||
|
||||
export async function removeDir(key: string) {
|
||||
const target = path.join(state().dir, key)
|
||||
const dir = await state().then((x) => x.dir)
|
||||
const target = path.join(dir, key)
|
||||
await fs.rm(target, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
|
||||
export async function readJSON<T>(key: string) {
|
||||
return Bun.file(path.join(state().dir, key + ".json")).json() as Promise<T>
|
||||
const dir = await state().then((x) => x.dir)
|
||||
return Bun.file(path.join(dir, key + ".json")).json() as Promise<T>
|
||||
}
|
||||
|
||||
export async function writeJSON<T>(key: string, content: T) {
|
||||
const target = path.join(state().dir, key + ".json")
|
||||
const dir = await state().then((x) => x.dir)
|
||||
const target = path.join(dir, key + ".json")
|
||||
const tmp = target + Date.now() + ".tmp"
|
||||
await Bun.write(tmp, JSON.stringify(content))
|
||||
await Bun.write(tmp, JSON.stringify(content, null, 2))
|
||||
await fs.rename(tmp, target).catch(() => {})
|
||||
await fs.unlink(tmp).catch(() => {})
|
||||
Bus.publish(Event.Write, { key, content })
|
||||
|
@ -49,9 +81,10 @@ export namespace Storage {
|
|||
|
||||
const glob = new Bun.Glob("**/*")
|
||||
export async function* list(prefix: string) {
|
||||
const dir = await state().then((x) => x.dir)
|
||||
try {
|
||||
for await (const item of glob.scan({
|
||||
cwd: path.join(state().dir, prefix),
|
||||
cwd: path.join(dir, prefix),
|
||||
onlyFiles: true,
|
||||
})) {
|
||||
const result = path.join(prefix, item.slice(0, -5))
|
||||
|
|
|
@ -20,6 +20,9 @@ export default defineConfig({
|
|||
devToolbar: {
|
||||
enabled: false,
|
||||
},
|
||||
server: {
|
||||
host: "0.0.0.0",
|
||||
},
|
||||
markdown: {
|
||||
rehypePlugins: [
|
||||
rehypeHeadingIds,
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
import {
|
||||
type JSX,
|
||||
splitProps,
|
||||
createResource,
|
||||
} from "solid-js"
|
||||
import { type JSX, splitProps, createResource } from "solid-js"
|
||||
import { codeToHtml } from "shiki"
|
||||
import styles from "./codeblock.module.css"
|
||||
import { transformerNotationDiff } from "@shikijs/transformers"
|
||||
|
@ -30,7 +26,7 @@ function CodeBlock(props: CodeBlockProps) {
|
|||
},
|
||||
)
|
||||
|
||||
return <div innerHTML={html()} class={styles.codeblock} {...rest}></div >
|
||||
return <div innerHTML={html()} class={styles.codeblock} {...rest}></div>
|
||||
}
|
||||
|
||||
export default CodeBlock
|
||||
|
|
|
@ -5,7 +5,7 @@ import { codeToHtml } from "shiki"
|
|||
import { transformerNotationDiff } from "@shikijs/transformers"
|
||||
import styles from "./markdownview.module.css"
|
||||
|
||||
interface MarkdownViewProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
||||
interface MarkdownViewProps {
|
||||
markdown: string
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ const markedWithShiki = marked.use(
|
|||
}),
|
||||
)
|
||||
|
||||
function MarkdownView(props: MarkdownViewProps) {
|
||||
export function MarkdownView(props: MarkdownViewProps) {
|
||||
const [local, rest] = splitProps(props, ["markdown"])
|
||||
const [html] = createResource(
|
||||
() => local.markdown,
|
||||
|
@ -33,7 +33,6 @@ function MarkdownView(props: MarkdownViewProps) {
|
|||
},
|
||||
)
|
||||
|
||||
return <div innerHTML={html()} class={styles["markdown-body"]} {...rest} />
|
||||
return <div innerHTML={html()} class={styles.root} {...rest} />
|
||||
}
|
||||
|
||||
export default MarkdownView
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,4 +1,4 @@
|
|||
.markdown-body {
|
||||
.root {
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5;
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
|||
list-style-position: inside;
|
||||
padding-left: 0.75rem;
|
||||
}
|
||||
|
||||
ul {
|
||||
padding-left: 1.5rem;
|
||||
}
|
||||
|
@ -35,7 +36,7 @@
|
|||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
& > *:last-child {
|
||||
&>*:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
|
@ -61,6 +62,7 @@
|
|||
content: "`";
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
&::after {
|
||||
content: "`";
|
||||
font-weight: 700;
|
||||
|
|
|
@ -15,76 +15,43 @@
|
|||
--lg-tool-width: 56rem;
|
||||
|
||||
--term-icon: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%2060%2016'%20preserveAspectRatio%3D'xMidYMid%20meet'%3E%3Ccircle%20cx%3D'8'%20cy%3D'8'%20r%3D'8'%2F%3E%3Ccircle%20cx%3D'30'%20cy%3D'8'%20r%3D'8'%2F%3E%3Ccircle%20cx%3D'52'%20cy%3D'8'%20r%3D'8'%2F%3E%3C%2Fsvg%3E");
|
||||
}
|
||||
|
||||
[data-element-button-text] {
|
||||
cursor: pointer;
|
||||
appearance: none;
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
color: var(--sl-color-text-secondary);
|
||||
|
||||
&:hover {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
|
||||
&[data-element-button-more] {
|
||||
[data-component="header"] {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.125rem;
|
||||
|
||||
span[data-button-icon] {
|
||||
line-height: 1;
|
||||
opacity: 0.85;
|
||||
svg {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-element-label] {
|
||||
text-transform: uppercase;
|
||||
letter-spacing: -0.5px;
|
||||
color: var(--sl-color-text-dimmed);
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
|
||||
@media (max-width: 30rem) {
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
|
||||
@media (max-width: 30rem) {
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[data-section="title"] {
|
||||
h1 {
|
||||
font-size: 2.75rem;
|
||||
font-weight: 500;
|
||||
line-height: 1.2;
|
||||
letter-spacing: -0.05em;
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 3;
|
||||
overflow: hidden;
|
||||
[data-component="header-title"] {
|
||||
font-size: 2.75rem;
|
||||
font-weight: 500;
|
||||
line-height: 1.2;
|
||||
letter-spacing: -0.05em;
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 3;
|
||||
line-clamp: 3;
|
||||
overflow: hidden;
|
||||
|
||||
@media (max-width: 30rem) {
|
||||
font-size: 1.75rem;
|
||||
line-height: 1.25;
|
||||
-webkit-line-clamp: 3;
|
||||
}
|
||||
@media (max-width: 30rem) {
|
||||
font-size: 1.75rem;
|
||||
line-height: 1.25;
|
||||
-webkit-line-clamp: 3;
|
||||
}
|
||||
}
|
||||
|
||||
[data-section="row"] {
|
||||
[data-component="header-details"] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
[data-section="stats"] {
|
||||
[data-component="header-stats"] {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
|
@ -92,45 +59,69 @@
|
|||
gap: 0.5rem 0.875rem;
|
||||
flex-wrap: wrap;
|
||||
|
||||
li {
|
||||
|
||||
[data-slot="item"] {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
gap: 0.3125rem;
|
||||
font-size: 0.875rem;
|
||||
|
||||
span[data-placeholder] {
|
||||
color: var(--sl-color-text-dimmed);
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="icon"] {
|
||||
flex: 0 0 auto;
|
||||
color: var(--sl-color-text-dimmed);
|
||||
opacity: 0.85;
|
||||
|
||||
svg {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="model"] {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
}
|
||||
|
||||
[data-section="stats"] {
|
||||
li {
|
||||
gap: 0.3125rem;
|
||||
[data-component="header-time"] {
|
||||
color: var(--sl-color-text-dimmed);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
[data-stat-icon] {
|
||||
flex: 0 0 auto;
|
||||
color: var(--sl-color-text-dimmed);
|
||||
[data-component="text-button"] {
|
||||
cursor: pointer;
|
||||
appearance: none;
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
color: var(--sl-color-text-secondary);
|
||||
|
||||
&:hover {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
|
||||
&[data-element-button-more] {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.125rem;
|
||||
|
||||
span[data-button-icon] {
|
||||
line-height: 1;
|
||||
opacity: 0.85;
|
||||
|
||||
svg {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
span[data-stat-model] {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-section="time"] {
|
||||
span {
|
||||
color: var(--sl-color-text-dimmed);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
.parts {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
@ -140,7 +131,7 @@
|
|||
display: flex;
|
||||
gap: 0.625rem;
|
||||
|
||||
& > [data-section="decoration"] {
|
||||
&>[data-section="decoration"] {
|
||||
flex: 0 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
@ -170,10 +161,12 @@
|
|||
svg:nth-child(3) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
svg:nth-child(1) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
svg:nth-child(2) {
|
||||
display: block;
|
||||
}
|
||||
|
@ -213,12 +206,15 @@
|
|||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
|
||||
a,
|
||||
a:hover {
|
||||
|
||||
svg:nth-child(1),
|
||||
svg:nth-child(2) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
svg:nth-child(3) {
|
||||
display: block;
|
||||
}
|
||||
|
@ -234,7 +230,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
& > [data-section="content"] {
|
||||
&>[data-section="content"] {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
padding: 0 0 0.375rem;
|
||||
|
@ -264,7 +260,7 @@
|
|||
}
|
||||
|
||||
b {
|
||||
color: var(--sl-color-text);
|
||||
color: var(--sl-color-text);
|
||||
word-break: break-all;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
@ -287,20 +283,20 @@
|
|||
max-width: var(--md-tool-width);
|
||||
gap: 0.25rem 0.375rem;
|
||||
|
||||
& > div:nth-child(3n + 1) {
|
||||
&>div:nth-child(3n + 1) {
|
||||
width: 8px;
|
||||
height: 2px;
|
||||
border-radius: 1px;
|
||||
background: var(--sl-color-divider);
|
||||
}
|
||||
|
||||
& > div:nth-child(3n + 2),
|
||||
& > div:nth-child(3n + 3) {
|
||||
&>div:nth-child(3n + 2),
|
||||
&>div:nth-child(3n + 3) {
|
||||
font-size: 0.75rem;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
& > div:nth-child(3n + 3) {
|
||||
&>div:nth-child(3n + 3) {
|
||||
padding-left: 0.125rem;
|
||||
word-break: break-word;
|
||||
color: var(--sl-color-text-secondary);
|
||||
|
@ -331,7 +327,7 @@
|
|||
[data-part-type="ai-model"],
|
||||
[data-part-type="system-text"],
|
||||
[data-part-type="fallback"] {
|
||||
& > [data-section="content"] {
|
||||
&>[data-section="content"] {
|
||||
padding-bottom: 1rem;
|
||||
}
|
||||
}
|
||||
|
@ -342,14 +338,13 @@
|
|||
[data-part-type="tool-edit"],
|
||||
[data-part-type="tool-write"],
|
||||
[data-part-type="tool-fetch"] {
|
||||
& > [data-section="content"] > [data-part-tool-body] {
|
||||
&>[data-section="content"]>[data-part-tool-body] {
|
||||
gap: 0.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
[data-part-type="tool-grep"] {
|
||||
&:not(:has([data-part-tool-args]))
|
||||
> [data-section="content"] > [data-part-tool-body] {
|
||||
&:not(:has([data-part-tool-args]))>[data-section="content"]>[data-part-tool-body] {
|
||||
gap: 0.5rem;
|
||||
}
|
||||
}
|
||||
|
@ -374,8 +369,9 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-part-type="summary"] {
|
||||
& > [data-section="decoration"] {
|
||||
&>[data-section="decoration"] {
|
||||
span:first-child {
|
||||
flex: 0 0 auto;
|
||||
display: block;
|
||||
|
@ -388,22 +384,26 @@
|
|||
&[data-status="connected"] {
|
||||
background-color: var(--sl-color-green);
|
||||
}
|
||||
|
||||
&[data-status="connecting"] {
|
||||
background-color: var(--sl-color-orange);
|
||||
}
|
||||
|
||||
&[data-status="disconnected"] {
|
||||
background-color: var(--sl-color-divider);
|
||||
}
|
||||
|
||||
&[data-status="reconnecting"] {
|
||||
background-color: var(--sl-color-orange);
|
||||
}
|
||||
|
||||
&[data-status="error"] {
|
||||
background-color: var(--sl-color-red);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
& > [data-section="content"] {
|
||||
&>[data-section="content"] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
|
@ -493,14 +493,20 @@
|
|||
}
|
||||
}
|
||||
|
||||
&[data-background="none"] { background-color: transparent; }
|
||||
&[data-background="blue"] { background-color: var(--sl-color-blue-low); }
|
||||
&[data-background="none"] {
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
&[data-background="blue"] {
|
||||
background-color: var(--sl-color-blue-low);
|
||||
}
|
||||
|
||||
&[data-expanded="true"] {
|
||||
pre {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
&[data-expanded="false"] {
|
||||
pre {
|
||||
display: -webkit-box;
|
||||
|
@ -536,20 +542,25 @@
|
|||
|
||||
span {
|
||||
margin-right: 0.25rem;
|
||||
|
||||
&:last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
}
|
||||
|
||||
span[data-color="red"] {
|
||||
color: var(--sl-color-red);
|
||||
}
|
||||
|
||||
span[data-color="dimmed"] {
|
||||
color: var(--sl-color-text-dimmed);
|
||||
}
|
||||
|
||||
span[data-marker="label"] {
|
||||
text-transform: uppercase;
|
||||
letter-spacing: -0.5px;
|
||||
}
|
||||
|
||||
span[data-separator] {
|
||||
margin-right: 0.375rem;
|
||||
}
|
||||
|
@ -561,6 +572,7 @@
|
|||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
&[data-expanded="false"] {
|
||||
[data-section="content"] {
|
||||
display: -webkit-box;
|
||||
|
@ -575,7 +587,6 @@
|
|||
padding: 2px 0;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
.message-terminal {
|
||||
|
@ -586,7 +597,7 @@
|
|||
width: 100%;
|
||||
max-width: var(--sm-tool-width);
|
||||
|
||||
& > [data-section="body"] {
|
||||
&>[data-section="body"] {
|
||||
width: 100%;
|
||||
border: 1px solid var(--sl-color-divider);
|
||||
border-radius: 0.25rem;
|
||||
|
@ -599,7 +610,7 @@
|
|||
text-align: center;
|
||||
padding: 0 3.25rem;
|
||||
|
||||
& > span {
|
||||
&>span {
|
||||
max-width: min(100%, 140ch);
|
||||
display: inline-block;
|
||||
white-space: nowrap;
|
||||
|
@ -651,6 +662,7 @@
|
|||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
&[data-expanded="false"] {
|
||||
pre {
|
||||
display: -webkit-box;
|
||||
|
@ -693,6 +705,7 @@
|
|||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
&[data-expanded="false"] {
|
||||
[data-element-markdown] {
|
||||
display: -webkit-box;
|
||||
|
@ -733,7 +746,7 @@
|
|||
border-bottom: none;
|
||||
}
|
||||
|
||||
& > span {
|
||||
&>span {
|
||||
position: absolute;
|
||||
display: inline-block;
|
||||
left: 0.5rem;
|
||||
|
@ -743,18 +756,21 @@
|
|||
border: 1px solid var(--sl-color-divider);
|
||||
border-radius: 0.15rem;
|
||||
|
||||
&::before {
|
||||
}
|
||||
&::before {}
|
||||
}
|
||||
|
||||
&[data-status="pending"] {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
|
||||
&[data-status="in_progress"] {
|
||||
color: var(--sl-color-text);
|
||||
|
||||
& > span { border-color: var(--sl-color-orange); }
|
||||
& > span::before {
|
||||
&>span {
|
||||
border-color: var(--sl-color-orange);
|
||||
}
|
||||
|
||||
&>span::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 2px;
|
||||
|
@ -764,11 +780,15 @@
|
|||
box-shadow: inset 1rem 1rem var(--sl-color-orange-low);
|
||||
}
|
||||
}
|
||||
|
||||
&[data-status="completed"] {
|
||||
color: var(--sl-color-text-secondary);
|
||||
|
||||
& > span { border-color: var(--sl-color-green-low); }
|
||||
& > span::before {
|
||||
&>span {
|
||||
border-color: var(--sl-color-green-low);
|
||||
}
|
||||
|
||||
&>span::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 2px;
|
||||
|
|
65
packages/web/src/components/share/common.tsx
Normal file
65
packages/web/src/components/share/common.tsx
Normal file
|
@ -0,0 +1,65 @@
|
|||
import { createSignal, onCleanup, splitProps } from "solid-js"
|
||||
import type { JSX } from "solid-js/jsx-runtime"
|
||||
import { IconCheckCircle, IconHashtag } from "../icons"
|
||||
|
||||
interface AnchorProps extends JSX.HTMLAttributes<HTMLDivElement> {
|
||||
id: string
|
||||
}
|
||||
export function AnchorIcon(props: AnchorProps) {
|
||||
const [local, rest] = splitProps(props, ["id", "children"])
|
||||
const [copied, setCopied] = createSignal(false)
|
||||
|
||||
return (
|
||||
<div
|
||||
{...rest}
|
||||
data-element-anchor
|
||||
title="Link to this message"
|
||||
data-status={copied() ? "copied" : ""}
|
||||
>
|
||||
<a
|
||||
href={`#${local.id}`}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
|
||||
const anchor = e.currentTarget
|
||||
const hash = anchor.getAttribute("href") || ""
|
||||
const { origin, pathname, search } = window.location
|
||||
|
||||
navigator.clipboard
|
||||
.writeText(`${origin}${pathname}${search}${hash}`)
|
||||
.catch((err) => console.error("Copy failed", err))
|
||||
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 3000)
|
||||
}}
|
||||
>
|
||||
{local.children}
|
||||
<IconHashtag width={18} height={18} />
|
||||
<IconCheckCircle width={18} height={18} />
|
||||
</a>
|
||||
<span data-element-tooltip>Copied!</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function createOverflow() {
|
||||
const [overflow, setOverflow] = createSignal(false)
|
||||
return {
|
||||
get status() {
|
||||
return overflow()
|
||||
},
|
||||
ref(el: HTMLElement) {
|
||||
const ro = new ResizeObserver(() => {
|
||||
if (el.scrollHeight > el.clientHeight + 1) {
|
||||
setOverflow(true)
|
||||
}
|
||||
return
|
||||
})
|
||||
ro.observe(el)
|
||||
|
||||
onCleanup(() => {
|
||||
ro.disconnect()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
16
packages/web/src/components/share/content-code.module.css
Normal file
16
packages/web/src/components/share/content-code.module.css
Normal file
|
@ -0,0 +1,16 @@
|
|||
.root {
|
||||
max-width: var(--md-tool-width);
|
||||
border: 1px solid var(--sl-color-divider);
|
||||
background-color: var(--sl-color-bg-surface);
|
||||
border-radius: .25rem;
|
||||
padding: .5rem calc(.5rem + 3px);
|
||||
|
||||
pre {
|
||||
--shiki-dark-bg: var(--sl-color-bg-surface) !important;
|
||||
background-color: var(--sl-color-bg-surface) !important;
|
||||
|
||||
span {
|
||||
white-space: break-spaces;
|
||||
}
|
||||
}
|
||||
}
|
28
packages/web/src/components/share/content-code.tsx
Normal file
28
packages/web/src/components/share/content-code.tsx
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { type JSX, splitProps, createResource } from "solid-js"
|
||||
import { codeToHtml } from "shiki"
|
||||
import style from "./content-code.module.css"
|
||||
import { transformerNotationDiff } from "@shikijs/transformers"
|
||||
|
||||
interface Props {
|
||||
code: string
|
||||
lang?: string
|
||||
}
|
||||
export function ContentCode(props: Props) {
|
||||
const [html] = createResource(
|
||||
() => [props.code, props.lang],
|
||||
async ([code, lang]) => {
|
||||
// TODO: For testing delays
|
||||
// await new Promise((resolve) => setTimeout(resolve, 3000))
|
||||
return (await codeToHtml(code || "", {
|
||||
lang: lang || "text",
|
||||
themes: {
|
||||
light: "github-light",
|
||||
dark: "github-dark",
|
||||
},
|
||||
transformers: [transformerNotationDiff()],
|
||||
})) as string
|
||||
},
|
||||
)
|
||||
return <div innerHTML={html()} class={style.root} />
|
||||
}
|
||||
|
121
packages/web/src/components/share/content-diff.module.css
Normal file
121
packages/web/src/components/share/content-diff.module.css
Normal file
|
@ -0,0 +1,121 @@
|
|||
.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;
|
||||
}
|
||||
}
|
246
packages/web/src/components/share/content-diff.tsx
Normal file
246
packages/web/src/components/share/content-diff.tsx
Normal file
|
@ -0,0 +1,246 @@
|
|||
import { type Component, createMemo } from "solid-js"
|
||||
import { parsePatch } from "diff"
|
||||
import { ContentCode } from "./content-code"
|
||||
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}>
|
||||
<ContentCode
|
||||
code={r.left}
|
||||
lang={props.lang}
|
||||
data-section="cell"
|
||||
data-diff-type={r.type === "removed" || r.type === "modified" ? "removed" : ""}
|
||||
/>
|
||||
</div>
|
||||
<div class={styles.afterColumn}>
|
||||
<ContentCode
|
||||
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) => (
|
||||
<ContentCode
|
||||
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`
|
140
packages/web/src/components/share/content-markdown.module.css
Normal file
140
packages/web/src/components/share/content-markdown.module.css
Normal file
|
@ -0,0 +1,140 @@
|
|||
.root {
|
||||
border: 1px solid var(--sl-color-blue-high);
|
||||
padding: 0.5rem calc(0.5rem + 3px);
|
||||
border-radius: 0.25rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 1rem;
|
||||
align-self: flex-start;
|
||||
max-width: var(--md-tool-width);
|
||||
|
||||
&[data-highlight="true"] {
|
||||
background-color: var(--sl-color-blue-low);
|
||||
}
|
||||
|
||||
[data-slot="expand-button"] {
|
||||
flex: 0 0 auto;
|
||||
padding: 2px 0;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
[data-slot="markdown"] {
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 3;
|
||||
line-clamp: 3;
|
||||
overflow: hidden;
|
||||
|
||||
[data-expanded] & {
|
||||
display: block;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
65
packages/web/src/components/share/content-markdown.tsx
Normal file
65
packages/web/src/components/share/content-markdown.tsx
Normal file
|
@ -0,0 +1,65 @@
|
|||
import style from "./content-markdown.module.css"
|
||||
import { createResource, createSignal } from "solid-js"
|
||||
import { createOverflow } from "./common"
|
||||
import { transformerNotationDiff } from "@shikijs/transformers"
|
||||
import { marked } from "marked"
|
||||
import markedShiki from "marked-shiki"
|
||||
import { codeToHtml } from "shiki"
|
||||
|
||||
const markedWithShiki = marked.use(
|
||||
markedShiki({
|
||||
highlight(code, lang) {
|
||||
return codeToHtml(code, {
|
||||
lang: lang || "text",
|
||||
themes: {
|
||||
light: "github-light",
|
||||
dark: "github-dark",
|
||||
},
|
||||
transformers: [transformerNotationDiff()],
|
||||
})
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
interface Props {
|
||||
text: string
|
||||
expand?: boolean
|
||||
highlight?: boolean
|
||||
}
|
||||
export function ContentMarkdown(props: Props) {
|
||||
const [html] = createResource(
|
||||
() => strip(props.text),
|
||||
async (markdown) => {
|
||||
return markedWithShiki.parse(markdown)
|
||||
},
|
||||
)
|
||||
const [expanded, setExpanded] = createSignal(false)
|
||||
const overflow = createOverflow()
|
||||
|
||||
return (
|
||||
<div
|
||||
class={style.root}
|
||||
data-highlight={props.highlight === true ? true : undefined}
|
||||
data-expanded={(expanded() || props.expand === true) ? true : undefined}
|
||||
>
|
||||
<div data-slot="markdown" ref={overflow.ref} innerHTML={html()} />
|
||||
|
||||
{(!props.expand && overflow.status) && (
|
||||
<button
|
||||
type="button"
|
||||
data-component="text-button"
|
||||
data-slot="expand-button"
|
||||
onClick={() => setExpanded((e) => !e)}
|
||||
>
|
||||
{expanded() ? "Show less" : "Show more"}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
58
packages/web/src/components/share/content-text.module.css
Normal file
58
packages/web/src/components/share/content-text.module.css
Normal file
|
@ -0,0 +1,58 @@
|
|||
.root {
|
||||
color: var(--sl-color-text);
|
||||
background-color: var(--sl-color-bg-surface);
|
||||
padding: 0.5rem calc(0.5rem + 3px);
|
||||
border-radius: 0.25rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 1rem;
|
||||
align-self: flex-start;
|
||||
max-width: var(--md-tool-width);
|
||||
font-size: 0.875rem;
|
||||
|
||||
&[data-compact] {
|
||||
font-size: 0.75rem;
|
||||
color: var(--sl-color-text-dimmed);
|
||||
}
|
||||
|
||||
[data-slot="text"] {
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
display: -webkit-box;
|
||||
-webkit-box-orient: vertical;
|
||||
-webkit-line-clamp: 3;
|
||||
line-clamp: 3;
|
||||
overflow: hidden;
|
||||
|
||||
[data-expanded] & {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="expand-button"] {
|
||||
flex: 0 0 auto;
|
||||
padding: 2px 0;
|
||||
font-size: 0.75rem;
|
||||
|
||||
}
|
||||
|
||||
&[data-theme="invert"] {
|
||||
background-color: var(--sl-color-blue-high);
|
||||
color: var(--sl-color-text-invert);
|
||||
|
||||
[data-slot="expand-button"] {
|
||||
opacity: 0.85;
|
||||
color: var(--sl-color-text-invert);
|
||||
|
||||
&:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&[data-theme="blue"] {
|
||||
background-color: var(--sl-color-blue-low);
|
||||
}
|
||||
}
|
33
packages/web/src/components/share/content-text.tsx
Normal file
33
packages/web/src/components/share/content-text.tsx
Normal file
|
@ -0,0 +1,33 @@
|
|||
import style from "./content-text.module.css"
|
||||
import { createSignal } from "solid-js"
|
||||
import { createOverflow } from "./common"
|
||||
|
||||
interface Props {
|
||||
text: string
|
||||
expand?: boolean
|
||||
compact?: boolean
|
||||
}
|
||||
export function ContentText(props: Props) {
|
||||
const [expanded, setExpanded] = createSignal(false)
|
||||
const overflow = createOverflow()
|
||||
|
||||
return (
|
||||
<div
|
||||
class={style.root}
|
||||
data-expanded={(expanded() || props.expand === true) ? true : undefined}
|
||||
data-compact={props.compact === true ? true : undefined}
|
||||
>
|
||||
<pre data-slot="text" ref={overflow.ref}>{props.text}</pre>
|
||||
{((!props.expand && overflow.status) || expanded()) && (
|
||||
<button
|
||||
type="button"
|
||||
data-component="text-button"
|
||||
data-slot="expand-button"
|
||||
onClick={() => setExpanded((e) => !e)}
|
||||
>
|
||||
{expanded() ? "Show less" : "Show more"}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
288
packages/web/src/components/share/part.module.css
Normal file
288
packages/web/src/components/share/part.module.css
Normal file
|
@ -0,0 +1,288 @@
|
|||
.root {
|
||||
display: flex;
|
||||
gap: 0.625rem;
|
||||
|
||||
[data-component="decoration"] {
|
||||
flex: 0 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.625rem;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
|
||||
[data-slot="anchor"] {
|
||||
position: relative;
|
||||
|
||||
a:first-child {
|
||||
display: block;
|
||||
flex: 0 0 auto;
|
||||
width: 18px;
|
||||
opacity: 0.65;
|
||||
|
||||
svg {
|
||||
color: var(--sl-color-text-secondary);
|
||||
display: block;
|
||||
|
||||
&:nth-child(3) {
|
||||
color: var(--sl-color-green-high);
|
||||
}
|
||||
}
|
||||
|
||||
svg:nth-child(2),
|
||||
svg:nth-child(3) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
svg:nth-child(1) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
svg:nth-child(2) {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-copied] & {
|
||||
|
||||
a,
|
||||
a:hover {
|
||||
|
||||
svg:nth-child(1),
|
||||
svg:nth-child(2) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
svg:nth-child(3) {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-slot="bar"] {
|
||||
width: 3px;
|
||||
height: 100%;
|
||||
border-radius: 1px;
|
||||
background-color: var(--sl-color-hairline);
|
||||
}
|
||||
|
||||
[data-slot="tooltip"] {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: calc(100% + 12px);
|
||||
transform: translate(0, -50%);
|
||||
line-height: 1.1;
|
||||
padding: 0.375em 0.5em calc(0.375em + 2px);
|
||||
background: var(--sl-color-white);
|
||||
color: var(--sl-color-text-invert);
|
||||
font-size: 0.6875rem;
|
||||
border-radius: 7px;
|
||||
white-space: nowrap;
|
||||
|
||||
z-index: 1;
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
|
||||
&::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: -15px;
|
||||
transform: translateY(-50%);
|
||||
border: 8px solid transparent;
|
||||
border-right-color: var(--sl-color-white);
|
||||
}
|
||||
|
||||
[data-copied] & {
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-component="content"] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
[data-component="spacer"] {
|
||||
height: 0rem;
|
||||
}
|
||||
|
||||
[data-component="content-footer"] {
|
||||
align-self: flex-start;
|
||||
font-size: 0.75rem;
|
||||
color: var(--sl-color-text-dimmed);
|
||||
}
|
||||
|
||||
[data-component="step-start"] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: .375rem;
|
||||
padding-bottom: 1rem;
|
||||
|
||||
[data-slot="provider"] {
|
||||
line-height: 18px;
|
||||
font-size: .875rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: -.5px;
|
||||
color: var(--sl-color-text-secondary)
|
||||
}
|
||||
|
||||
[data-slot="model"] {
|
||||
line-height: 1.5;
|
||||
}
|
||||
}
|
||||
|
||||
[data-component="button-text"] {
|
||||
cursor: pointer;
|
||||
appearance: none;
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
padding: 0;
|
||||
color: var(--sl-color-text-secondary);
|
||||
font-size: 0.75rem;
|
||||
|
||||
&:hover {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
|
||||
&[data-more] {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.125rem;
|
||||
|
||||
span[data-slot="icon"] {
|
||||
line-height: 1;
|
||||
opacity: 0.85;
|
||||
|
||||
svg {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[data-component="tool"] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0.375rem;
|
||||
padding-bottom: 1rem;
|
||||
|
||||
}
|
||||
|
||||
[data-component="tool-title"] {
|
||||
line-height: 18px;
|
||||
font-size: .875rem;
|
||||
color: var(--sl-color-text-secondary);
|
||||
max-width: var(--md-tool-width);
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: .375rem;
|
||||
|
||||
[data-slot="name"] {
|
||||
text-transform: uppercase;
|
||||
letter-spacing: -.5px;
|
||||
}
|
||||
|
||||
[data-slot="target"] {
|
||||
color: var(--sl-color-text);
|
||||
word-break: break-all;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
|
||||
[data-component="tool-result"] {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: .5rem;
|
||||
}
|
||||
|
||||
[data-component="todos"] {
|
||||
list-style-type: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
width: 100%;
|
||||
max-width: var(--sm-tool-width);
|
||||
border: 1px solid var(--sl-color-divider);
|
||||
border-radius: 0.25rem;
|
||||
|
||||
[data-slot="item"] {
|
||||
margin: 0;
|
||||
position: relative;
|
||||
padding-left: 1.5rem;
|
||||
font-size: 0.75rem;
|
||||
padding: 0.375rem 0.625rem 0.375rem 1.75rem;
|
||||
border-bottom: 1px solid var(--sl-color-divider);
|
||||
line-height: 1.5;
|
||||
word-break: break-word;
|
||||
|
||||
&:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
&>span {
|
||||
position: absolute;
|
||||
display: inline-block;
|
||||
left: 0.5rem;
|
||||
top: calc(0.5rem + 1px);
|
||||
width: 0.75rem;
|
||||
height: 0.75rem;
|
||||
border: 1px solid var(--sl-color-divider);
|
||||
border-radius: 0.15rem;
|
||||
|
||||
&::before {}
|
||||
}
|
||||
|
||||
&[data-status="pending"] {
|
||||
color: var(--sl-color-text);
|
||||
}
|
||||
|
||||
&[data-status="in_progress"] {
|
||||
color: var(--sl-color-text);
|
||||
|
||||
&>span {
|
||||
border-color: var(--sl-color-orange);
|
||||
}
|
||||
|
||||
&>span::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 2px;
|
||||
left: 2px;
|
||||
width: calc(0.75rem - 2px - 4px);
|
||||
height: calc(0.75rem - 2px - 4px);
|
||||
box-shadow: inset 1rem 1rem var(--sl-color-orange-low);
|
||||
}
|
||||
}
|
||||
|
||||
&[data-status="completed"] {
|
||||
color: var(--sl-color-text-secondary);
|
||||
|
||||
&>span {
|
||||
border-color: var(--sl-color-green-low);
|
||||
}
|
||||
|
||||
&>span::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 2px;
|
||||
left: 2px;
|
||||
width: calc(0.75rem - 2px - 4px);
|
||||
height: calc(0.75rem - 2px - 4px);
|
||||
box-shadow: inset 1rem 1rem var(--sl-color-green);
|
||||
|
||||
transform-origin: bottom left;
|
||||
clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
504
packages/web/src/components/share/part.tsx
Normal file
504
packages/web/src/components/share/part.tsx
Normal file
|
@ -0,0 +1,504 @@
|
|||
import { createEffect, 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"
|
||||
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"
|
||||
|
||||
export interface PartProps {
|
||||
index: number
|
||||
message: MessageV2.Info
|
||||
part: MessageV2.AssistantPart | MessageV2.UserPart
|
||||
last: boolean
|
||||
}
|
||||
|
||||
export function Part(props: PartProps) {
|
||||
const [copied, setCopied] = createSignal(false)
|
||||
const id = createMemo(() => props.message.id + "-" + props.index)
|
||||
|
||||
return (
|
||||
<div
|
||||
class={styles.root}
|
||||
id={id()}
|
||||
data-component="part"
|
||||
data-type={props.part.type}
|
||||
data-role={props.message.role}
|
||||
data-copied={copied() ? true : undefined}
|
||||
>
|
||||
<div data-component="decoration">
|
||||
<div data-slot="anchor" title="Link to this message">
|
||||
<a
|
||||
href={`#${id()}`}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
const anchor = e.currentTarget
|
||||
const hash = anchor.getAttribute("href") || ""
|
||||
const { origin, pathname, search } = window.location
|
||||
navigator.clipboard
|
||||
.writeText(`${origin}${pathname}${search}${hash}`)
|
||||
.catch((err) => console.error("Copy failed", err))
|
||||
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 3000)
|
||||
}}
|
||||
>
|
||||
<Switch>
|
||||
<Match when={true}>
|
||||
<IconSparkles width={18} height={18} />
|
||||
</Match>
|
||||
</Switch>
|
||||
<IconHashtag width={18} height={18} />
|
||||
<IconCheckCircle width={18} height={18} />
|
||||
</a>
|
||||
<span data-slot="tooltip">Copied!</span>
|
||||
</div>
|
||||
<div data-slot="bar"></div>
|
||||
</div>
|
||||
<div data-component="content">
|
||||
{props.message.role === "user" && props.part.type === "text" && (
|
||||
<>
|
||||
<ContentText text={props.part.text} expand={props.last} /> <Spacer />
|
||||
</>
|
||||
)}
|
||||
{props.message.role === "assistant" && props.part.type === "text" && (
|
||||
<>
|
||||
<ContentMarkdown expand={props.last} text={props.part.text} />
|
||||
{props.last && props.message.role === "assistant" && props.message.time.completed && (
|
||||
<Footer
|
||||
title={DateTime.fromMillis(props.message.time.completed).toLocaleString(
|
||||
DateTime.DATETIME_FULL_WITH_SECONDS,
|
||||
)}
|
||||
>
|
||||
{DateTime.fromMillis(props.message.time.completed).toLocaleString(DateTime.DATETIME_MED)}
|
||||
</Footer>
|
||||
)}
|
||||
<Spacer />
|
||||
</>
|
||||
)}
|
||||
{props.part.type === "step-start" && props.message.role === "assistant" && (
|
||||
<div data-component="step-start">
|
||||
<div data-slot="provider">{props.message.providerID}</div>
|
||||
<div data-slot="model">{props.message.modelID}</div>
|
||||
</div>
|
||||
)}
|
||||
{props.part.type === "tool" && props.part.state.status === "completed" && (
|
||||
<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} />
|
||||
</Match>
|
||||
<Match when={props.part.tool === "glob"}>
|
||||
<GlobTool 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} />
|
||||
</Match>
|
||||
<Match when={props.part.tool === "read"}>
|
||||
<ReadTool 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} />
|
||||
</Match>
|
||||
<Match when={props.part.tool === "edit"}>
|
||||
<EditTool 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} />
|
||||
</Match>
|
||||
<Match when={props.part.tool === "webfetch"}>
|
||||
<WebFetchTool 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} />
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
type ToolProps = {
|
||||
id: MessageV2.ToolPart["id"]
|
||||
tool: MessageV2.ToolPart["tool"]
|
||||
state: MessageV2.ToolStateCompleted
|
||||
rootDir?: string
|
||||
isLastPart?: boolean
|
||||
}
|
||||
|
||||
interface Todo {
|
||||
id: string
|
||||
content: string
|
||||
status: "pending" | "in_progress" | "completed"
|
||||
priority: "low" | "medium" | "high"
|
||||
}
|
||||
|
||||
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 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
|
||||
}
|
||||
|
||||
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>
|
||||
)
|
||||
}
|
||||
|
||||
export function TodoWriteTool(props: ToolProps) {
|
||||
const priority: Record<Todo["status"], number> = {
|
||||
in_progress: 0,
|
||||
pending: 1,
|
||||
completed: 2,
|
||||
}
|
||||
const todos = createMemo(() =>
|
||||
((props.state.input?.todos ?? []) as Todo[]).slice().sort((a, b) => priority[a.status] - priority[b.status]),
|
||||
)
|
||||
const starting = () => todos().every((t: Todo) => t.status === "pending")
|
||||
const finished = () => todos().every((t: Todo) => t.status === "completed")
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">
|
||||
<Switch fallback="Updating plan">
|
||||
<Match when={starting()}>Creating plan</Match>
|
||||
<Match when={finished()}>Completing plan</Match>
|
||||
</Switch>
|
||||
</span>
|
||||
</div>
|
||||
<Show when={todos().length > 0}>
|
||||
<ul data-component="todos">
|
||||
<For each={todos()}>
|
||||
{(todo) => (
|
||||
<li data-slot="item" data-status={todo.status}>
|
||||
<span></span>
|
||||
{todo.content}
|
||||
</li>
|
||||
)}
|
||||
</For>
|
||||
</ul>
|
||||
</Show>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function GrepTool(props: ToolProps) {
|
||||
const count = () => props.state.metadata?.matches
|
||||
const pattern = () => props.state.input.pattern
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">Grep</span>
|
||||
<span data-slot="target">“{pattern()}”</span>
|
||||
</div>
|
||||
<div data-component="tool-result">
|
||||
<Switch>
|
||||
<Match when={count() && count() > 0}>
|
||||
<ResultsButton showCopy={count() === 1 ? "1 match" : `${count()} matches`}>
|
||||
<ContentText expand compact text={props.state.output} />
|
||||
</ResultsButton>
|
||||
</Match>
|
||||
<Match when={props.state.output}>
|
||||
<ContentText expand compact text={props.state.output} data-size="sm" data-color="dimmed" />
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">LS</span>
|
||||
<span data-slot="target" title={props.state.input?.path}>
|
||||
{path()}
|
||||
</span>
|
||||
</div>
|
||||
<div data-component="tool-result">
|
||||
<Switch>
|
||||
<Match when={props.state.output}>
|
||||
<ResultsButton>
|
||||
<ContentText expand compact text={props.state.output} />
|
||||
</ResultsButton>
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function WebFetchTool(props: ToolProps) {
|
||||
const url = () => props.state.input.url
|
||||
const format = () => props.state.input.format
|
||||
const hasError = () => props.state.metadata?.error
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">Fetch</span>
|
||||
<span data-slot="target">{url()}</span>
|
||||
</div>
|
||||
<div data-component="tool-result">
|
||||
<Switch>
|
||||
<Match when={hasError()}>
|
||||
<div data-component="error">{formatErrorString(props.state.output)}</div>
|
||||
</Match>
|
||||
<Match when={props.state.output}>
|
||||
<ResultsButton>
|
||||
<div data-component="code">
|
||||
<CodeBlock lang={format() || "text"} code={props.state.output} />
|
||||
</div>
|
||||
</ResultsButton>
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function ReadTool(props: ToolProps) {
|
||||
const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.rootDir))
|
||||
const hasError = () => props.state.metadata?.error
|
||||
const preview = () => props.state.metadata?.preview
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">Read</span>
|
||||
<span data-slot="target" title={props.state.input?.filePath}>
|
||||
{filePath()}
|
||||
</span>
|
||||
</div>
|
||||
<div data-component="tool-result">
|
||||
<Switch>
|
||||
<Match when={hasError()}>
|
||||
<div data-component="error">{formatErrorString(props.state.output)}</div>
|
||||
</Match>
|
||||
<Match when={typeof preview() === "string"}>
|
||||
<ResultsButton showCopy="Show preview" hideCopy="Hide preview">
|
||||
<div data-component="code">
|
||||
<CodeBlock lang={getShikiLang(filePath() || "")} code={preview()} />
|
||||
</div>
|
||||
</ResultsButton>
|
||||
</Match>
|
||||
<Match when={typeof preview() !== "string" && props.state.output}>
|
||||
<ResultsButton>
|
||||
<ContentText expand compact text={props.state.output} />
|
||||
</ResultsButton>
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function WriteTool(props: ToolProps) {
|
||||
const filePath = createMemo(() => stripWorkingDirectory(props.state.input?.filePath, props.rootDir))
|
||||
const hasError = () => props.state.metadata?.error
|
||||
const content = () => props.state.input?.content
|
||||
const diagnostics = createMemo(() => getDiagnostics(props.state.metadata?.diagnostics, props.state.input.filePath))
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">Write</span>
|
||||
<span data-slot="target" title={props.state.input?.filePath}>
|
||||
{filePath()}
|
||||
</span>
|
||||
</div>
|
||||
<Show when={diagnostics().length > 0}>
|
||||
<div data-component="error">{diagnostics()}</div>
|
||||
</Show>
|
||||
<div data-component="tool-result">
|
||||
<Switch>
|
||||
<Match when={hasError()}>
|
||||
<div data-component="error">{formatErrorString(props.state.output)}</div>
|
||||
</Match>
|
||||
<Match when={content()}>
|
||||
<ResultsButton showCopy="Show contents" hideCopy="Hide contents">
|
||||
<ContentCode lang={getShikiLang(filePath() || "")} code={props.state.input?.content} />
|
||||
</ResultsButton>
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
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 diagnostics = createMemo(() => getDiagnostics(props.state.metadata?.diagnostics, props.state.input.filePath))
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">Edit</span>
|
||||
<span data-slot="target" title={props.state.input?.filePath}>
|
||||
{filePath()}
|
||||
</span>
|
||||
</div>
|
||||
<div data-component="tool-result">
|
||||
<Switch>
|
||||
<Match when={hasError()}>
|
||||
<div data-component="error">{formatErrorString(message() || "")}</div>
|
||||
</Match>
|
||||
<Match when={diff()}>
|
||||
<div data-component="diff">
|
||||
<DiffView class={styles.root} diff={diff()} lang={getShikiLang(filePath() || "")} />
|
||||
</div>
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
<Show when={diagnostics().length > 0}>
|
||||
<div data-component="error">{diagnostics()}</div>
|
||||
</Show>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function GlobTool(props: ToolProps) {
|
||||
const count = () => props.state.metadata?.count
|
||||
const pattern = () => props.state.input.pattern
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-component="tool-title">
|
||||
<span data-slot="name">Glob</span>
|
||||
<span data-slot="target">“{pattern()}”</span>
|
||||
</div>
|
||||
<Switch>
|
||||
<Match when={count() && count() > 0}>
|
||||
<div data-component="tool-result">
|
||||
<ResultsButton showCopy={count() === 1 ? "1 result" : `${count()} results`}>
|
||||
<ContentText expand compact text={props.state.output} />
|
||||
</ResultsButton>
|
||||
</div>
|
||||
</Match>
|
||||
<Match when={props.state.output}>
|
||||
<ContentText expand text={props.state.output} data-size="sm" data-color="dimmed" />
|
||||
</Match>
|
||||
</Switch>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
interface ResultsButtonProps extends ParentProps {
|
||||
showCopy?: string
|
||||
hideCopy?: string
|
||||
}
|
||||
function ResultsButton(props: ResultsButtonProps) {
|
||||
const [show, setShow] = createSignal(false)
|
||||
|
||||
return (
|
||||
<>
|
||||
<button type="button" data-component="button-text" data-more onClick={() => setShow((e) => !e)}>
|
||||
<span>{show() ? props.hideCopy || "Hide results" : props.showCopy || "Show results"}</span>
|
||||
<span data-slot="icon">
|
||||
<Show when={show()} fallback={<IconChevronRight width={11} height={11} />}>
|
||||
<IconChevronDown width={11} height={11} />
|
||||
</Show>
|
||||
</span>
|
||||
</button>
|
||||
<Show when={show()}>{props.children}</Show>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function Spacer() {
|
||||
return <div data-component="spacer"></div>
|
||||
}
|
||||
|
||||
function Footer(props: ParentProps<{ title: string }>) {
|
||||
return (
|
||||
<div data-component="content-footer" title={props.title}>
|
||||
{props.children}
|
||||
</div>
|
||||
)
|
||||
}
|
270
packages/web/src/components/share/tool.tsx
Normal file
270
packages/web/src/components/share/tool.tsx
Normal file
|
@ -0,0 +1,270 @@
|
|||
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 CodeBlock from "../CodeBlock"
|
||||
import DiffView from "../DiffView"
|
||||
import styles from "../share.module.css"
|
||||
|
||||
type ToolProps = {
|
||||
id: MessageV2.ToolPart["id"]
|
||||
tool: MessageV2.ToolPart["tool"]
|
||||
state: MessageV2.ToolStateCompleted
|
||||
rootDir?: string
|
||||
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
|
||||
result?: string
|
||||
desc?: string
|
||||
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
|
||||
hideCopy?: string
|
||||
results: boolean
|
||||
}
|
||||
function ResultsButton(props: ResultsButtonProps) {
|
||||
return (
|
||||
<button type="button" data-element-button-text data-element-button-more {...props}>
|
||||
<span>{props.results ? props.hideCopy || "Hide results" : props.showCopy || "Show results"}</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
function TextPart(props: TextPartProps) {
|
||||
return (
|
||||
<div class={styles["message-text"]} data-expanded={props.expand === true} {...props}>
|
||||
<pre>{props.text}</pre>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
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}>
|
||||
<div data-section="body">
|
||||
<div data-section="header">
|
||||
<span>{props.desc}</span>
|
||||
</div>
|
||||
<div data-section="content">
|
||||
<CodeBlock lang="bash" code={props.command} />
|
||||
<Switch>
|
||||
<Match when={props.error}>
|
||||
<CodeBlock lang="text" data-section="error" code={props.error || ""} />
|
||||
</Match>
|
||||
<Match when={props.result}>
|
||||
<CodeBlock lang="console" code={props.result || ""} />
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
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>“{pattern()}”</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
|
||||
const result = () => props.state.metadata?.stdout
|
||||
const error = () => props.state.metadata?.stderr
|
||||
|
||||
return (
|
||||
<>
|
||||
{command() && (
|
||||
<TerminalPart desc={desc()} data-size="sm" command={command()!} result={result()} error={error()} />
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function FallbackTool(props: ToolProps) {
|
||||
const [showResults, setShowResults] = createSignal(false)
|
||||
|
||||
return (
|
||||
<>
|
||||
<div data-part-title>{props.tool}</div>
|
||||
<div data-part-tool-args>
|
||||
<For each={flattenToolArgs(props.state.input)}>
|
||||
{(arg) => (
|
||||
<>
|
||||
<div></div>
|
||||
<div>{arg[0]}</div>
|
||||
<div>{arg[1]}</div>
|
||||
</>
|
||||
)}
|
||||
</For>
|
||||
</div>
|
||||
<Switch>
|
||||
<Match when={props.state.output}>
|
||||
<div data-part-tool-result>
|
||||
<ResultsButton results={showResults()} onClick={() => setShowResults((e) => !e)} />
|
||||
<Show when={showResults()}>
|
||||
<TextPart expand data-size="sm" data-color="dimmed" text={props.state.output} />
|
||||
</Show>
|
||||
</div>
|
||||
</Match>
|
||||
</Switch>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
// Converts nested objects/arrays into [path, value] pairs.
|
||||
// E.g. {a:{b:{c:1}}, d:[{e:2}, 3]} => [["a.b.c",1], ["d[0].e",2], ["d[1]",3]]
|
||||
function flattenToolArgs(obj: any, prefix: string = ""): Array<[string, any]> {
|
||||
const entries: Array<[string, any]> = []
|
||||
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
const path = prefix ? `${prefix}.${key}` : key
|
||||
|
||||
if (value !== null && typeof value === "object") {
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item, index) => {
|
||||
const arrayPath = `${path}[${index}]`
|
||||
if (item !== null && typeof item === "object") {
|
||||
entries.push(...flattenToolArgs(item, arrayPath))
|
||||
} else {
|
||||
entries.push([arrayPath, item])
|
||||
}
|
||||
})
|
||||
} else {
|
||||
entries.push(...flattenToolArgs(value, path))
|
||||
}
|
||||
} else {
|
||||
entries.push([path, value])
|
||||
}
|
||||
}
|
||||
|
||||
return entries
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue