diff --git a/.gitignore b/.gitignore index 5f0f3842..7a87ec70 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ node_modules .opencode .sst app.log - +.env diff --git a/bun.lock b/bun.lock index e33d04cc..7b56e631 100644 --- a/bun.lock +++ b/bun.lock @@ -28,6 +28,7 @@ "ai": "catalog:", "cac": "6.7.14", "decimal.js": "10.5.0", + "diff": "8.0.2", "env-paths": "3.0.0", "hono": "4.7.10", "hono-openapi": "0.4.8", diff --git a/packages/function/sst-env.d.ts b/packages/function/sst-env.d.ts index a123afeb..41727ee9 100644 --- a/packages/function/sst-env.d.ts +++ b/packages/function/sst-env.d.ts @@ -6,20 +6,20 @@ import "sst" declare module "sst" { export interface Resource { - Web: { - type: "sst.cloudflare.StaticSite" - url: string + "Web": { + "type": "sst.cloudflare.StaticSite" + "url": string } } } -// cloudflare -import * as cloudflare from "@cloudflare/workers-types" +// cloudflare +import * as cloudflare from "@cloudflare/workers-types"; declare module "sst" { export interface Resource { - Api: cloudflare.Service - Bucket: cloudflare.R2Bucket + "Api": cloudflare.Service + "Bucket": cloudflare.R2Bucket } } import "sst" -export {} +export {} \ No newline at end of file diff --git a/packages/opencode/package.json b/packages/opencode/package.json index 61cccb89..25ee16a6 100644 --- a/packages/opencode/package.json +++ b/packages/opencode/package.json @@ -25,6 +25,7 @@ "ai": "catalog:", "cac": "6.7.14", "decimal.js": "10.5.0", + "diff": "8.0.2", "env-paths": "3.0.0", "hono": "4.7.10", "hono-openapi": "0.4.8", diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index c8e31f52..7f06df9e 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -3,6 +3,7 @@ import * as path from "path" import { Tool } from "./tool" import { FileTimes } from "./util/file-times" import { LSP } from "../lsp" +import { diffLines } from "diff" const DESCRIPTION = `Edits files by replacing text, creating new files, or deleting content. For moving or renaming files, use the Bash tool with the 'mv' command instead. For larger file edits, use the FileWrite tool to overwrite files. @@ -70,8 +71,11 @@ export const EditTool = Tool.define({ filePath = path.join(process.cwd(), filePath) } + let contentOld = "" + let contentNew = "" await (async () => { if (params.oldString === "") { + contentNew = params.newString await Bun.write(filePath, params.newString) return } @@ -91,26 +95,28 @@ export const EditTool = Tool.define({ `File ${filePath} has been modified since it was last read.\nLast modification: ${read.toISOString()}\nLast read: ${stats.mtime.toISOString()}\n\nPlease read the file again before modifying it.`, ) - const content = await file.text() - const index = content.indexOf(params.oldString) + contentOld = await file.text() + const index = contentOld.indexOf(params.oldString) if (index === -1) throw new Error( `oldString not found in file. Make sure it matches exactly, including whitespace and line breaks`, ) - const lastIndex = content.lastIndexOf(params.oldString) + const lastIndex = contentOld.lastIndexOf(params.oldString) if (index !== lastIndex) throw new Error( `oldString appears multiple times in the file. Please provide more context to ensure a unique match`, ) - const newContent = - content.substring(0, index) + + contentNew = + contentOld.substring(0, index) + params.newString + - content.substring(index + params.oldString.length) + contentOld.substring(index + params.oldString.length) - await file.write(newContent) + await file.write(contentNew) })() + const changes = diffLines(contentOld, contentNew) + FileTimes.write(filePath) FileTimes.read(filePath) @@ -129,6 +135,7 @@ export const EditTool = Tool.define({ return { metadata: { diagnostics, + changes, }, output, } diff --git a/packages/opencode/src/util/event.ts b/packages/opencode/src/util/event.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/packages/opencode/src/util/log.ts b/packages/opencode/src/util/log.ts index bf0a7258..003ebb77 100644 --- a/packages/opencode/src/util/log.ts +++ b/packages/opencode/src/util/log.ts @@ -53,6 +53,9 @@ export namespace Log { error(message?: any, extra?: Record) { write.err(build(message, extra)) }, + warn(message?: any, extra?: Record) { + write.err(build(message, extra)) + }, tag(key: string, value: string) { if (tags) tags[key] = value return result diff --git a/packages/opencode/src/util/scrap.ts b/packages/opencode/src/util/scrap.ts index 519f8326..554dba1c 100644 --- a/packages/opencode/src/util/scrap.ts +++ b/packages/opencode/src/util/scrap.ts @@ -1,5 +1,10 @@ export const foo: string = "42" +export const bar: number = 123 export function dummyFunction(): void { console.log("This is a dummy function") } + +export function randomHelper(): boolean { + return Math.random() > 0.5 +} diff --git a/packages/web/src/components/DiffView.tsx b/packages/web/src/components/DiffView.tsx index 4e864e06..6f89d6a9 100644 --- a/packages/web/src/components/DiffView.tsx +++ b/packages/web/src/components/DiffView.tsx @@ -1,5 +1,5 @@ -import { type Component, createSignal, onMount } from "solid-js" -import { diffLines } from "diff" +import { type Component, createMemo, createSignal, onMount } from "solid-js" +import { diffLines, type ChangeObject } from "diff" import CodeBlock from "./CodeBlock" import styles from "./diffview.module.css" @@ -10,33 +10,29 @@ type DiffRow = { } interface DiffViewProps { - oldCode: string - newCode: string + changes: ChangeObject[] lang?: string class?: string } const DiffView: Component = (props) => { - const [rows, setRows] = createSignal([]) - - onMount(() => { - const chunks = diffLines(props.oldCode, props.newCode) + const rows = createMemo(() => { const diffRows: DiffRow[] = [] - for (const chunk of chunks) { - const lines = chunk.value.split(/\r?\n/) + for (const item of props.changes) { + const lines = item.value.split(/\r?\n/) if (lines.at(-1) === "") lines.pop() for (const line of lines) { diffRows.push({ - left: chunk.removed ? line : chunk.added ? "" : line, - right: chunk.added ? line : chunk.removed ? "" : line, - type: chunk.added ? "added" : chunk.removed ? "removed" : "unchanged", + left: item.removed ? line : item.added ? "" : line, + right: item.added ? line : item.removed ? "" : line, + type: item.added ? "added" : item.removed ? "removed" : "unchanged", }) } } - setRows(diffRows) + return diffRows }) return ( diff --git a/packages/web/src/components/Share.tsx b/packages/web/src/components/Share.tsx index f4b89e1f..6d418b82 100644 --- a/packages/web/src/components/Share.tsx +++ b/packages/web/src/components/Share.tsx @@ -53,7 +53,7 @@ type SessionMessage = UIMessage<{ tool: Record< string, { - properties: Record + [key: string]: any time: { start: number end: number @@ -596,11 +596,12 @@ export default function Share(props: { api: string }) { when={ msg.role === "assistant" && part.type === "tool-invocation" && - part.toolInvocation.toolName === "edit" && + part.toolInvocation.toolName === "opencode_edit" && part } > {(part) => { + const metadata = createMemo(() => msg.metadata?.tool[part().toolInvocation.toolCallId]) const args = part().toolInvocation.args const filePath = args.filePath return ( @@ -622,8 +623,7 @@ export default function Share(props: { api: string }) {
@@ -680,7 +680,7 @@ export default function Share(props: { api: string }) { diff --git a/packages/web/sst-env.d.ts b/packages/web/sst-env.d.ts index 0397645b..b6a7e906 100644 --- a/packages/web/sst-env.d.ts +++ b/packages/web/sst-env.d.ts @@ -6,4 +6,4 @@ /// import "sst" -export {} +export {} \ No newline at end of file diff --git a/sst-env.d.ts b/sst-env.d.ts index 51e6de57..7ca38b72 100644 --- a/sst-env.d.ts +++ b/sst-env.d.ts @@ -5,20 +5,20 @@ declare module "sst" { export interface Resource { - Api: { - type: "sst.cloudflare.Worker" - url: string + "Api": { + "type": "sst.cloudflare.Worker" + "url": string } - Bucket: { - type: "sst.cloudflare.Bucket" + "Bucket": { + "type": "sst.cloudflare.Bucket" } - Web: { - type: "sst.cloudflare.StaticSite" - url: string + "Web": { + "type": "sst.cloudflare.StaticSite" + "url": string } } } /// import "sst" -export {} +export {} \ No newline at end of file