refactor(core): remove duplicated code in built-in tools (#61175)

This commit is contained in:
Aiden Cline
2026-11-01 21:14:44 -06:00
committed by GitHub
Unverified
parent 70551b511f
commit 598c52f058
6 changed files with 128 additions and 165 deletions
+17 -24
View File
@@ -129,7 +129,7 @@ export function convertHTMLToMarkdown(html: string) {
if (last && last !== "\n" && last !== " ") append(" ")
pendingSpace = false
}
const inline = (value: string, open = false) => {
const inline = (value: string, open = false, content = false) => {
if (open) flushSpace()
prefixQuote()
if (pendingIndent) {
@@ -140,11 +140,11 @@ export function convertHTMLToMarkdown(html: string) {
append("[")
linkOpen = true
}
append(value)
append(value, content)
}
const block = () => {
if (activeLink && linkOpen) {
append(`](${destination(activeLink.href)}${title(activeLink.title)})`)
closeLink(activeLink)
linkOpen = false
}
pendingSpace = false
@@ -171,22 +171,15 @@ export function convertHTMLToMarkdown(html: string) {
pendingSpace = true
continue
}
flushSpace()
prefixQuote()
if (pendingIndent) {
append(pendingIndent)
pendingIndent = ""
}
if (activeLink && !linkOpen) {
append("[")
linkOpen = true
}
const escaped = part
.replace(/([\\`*_[\]<>|])/g, "\\$1")
.replace(/~/g, "\\~")
.replace(/^([#+-])/, "\\$1")
.replace(/^(\d+)\./, "$1\\.")
append(escaped, true)
inline(
part
.replace(/([\\`*_[\]<>|])/g, "\\$1")
.replace(/~/g, "\\~")
.replace(/^([#+-])/, "\\$1")
.replace(/^(\d+)\./, "$1\\."),
true,
true,
)
}
}
const destination = (value: string) => value.replace(/([\\()])/g, "\\$1").replace(/[\t\n\r ]+/g, "%20")
@@ -197,6 +190,7 @@ export function convertHTMLToMarkdown(html: string) {
.trim()
.replace(/([\\"])/g, "\\$1")}"`
: ""
const closeLink = (link: NonNullable<Frame["link"]>) => append(`](${destination(link.href)}${title(link.title)})`)
const inlineMarker = (name: string) => {
if (name === "strong" || name === "b") return "**"
if (name === "em" || name === "i") return "*"
@@ -350,7 +344,7 @@ export function convertHTMLToMarkdown(html: string) {
)
if (parent) {
finishCode(activeCode)
if (linkOpen) append(`](${destination(activeLink.href)}${title(activeLink.title)})`)
if (linkOpen) closeLink(activeLink)
activeCode = parent.resumedCode
parent.link = undefined
parent.linkCode = undefined
@@ -372,7 +366,7 @@ export function convertHTMLToMarkdown(html: string) {
return
}
if (activeLink) {
if (linkOpen) append(`](${destination(activeLink.href)}${title(activeLink.title)})`)
if (linkOpen) closeLink(activeLink)
const parent = stack.findLast((candidate) => candidate.link === activeLink)
if (parent) parent.link = undefined
activeLink = undefined
@@ -487,7 +481,7 @@ export function convertHTMLToMarkdown(html: string) {
}
activeCode = undefined
finishCode(frame.linkCode)
if (frame.link && linkOpen) append(`](${destination(frame.link.href)}${title(frame.link.title)})`)
if (frame.link && linkOpen) closeLink(frame.link)
activeLink = undefined
linkOpen = false
activeCode = frame.resumedCode
@@ -530,8 +524,7 @@ export function convertHTMLToMarkdown(html: string) {
if (name === "a") {
if (frame.link && (activeLink === frame.link || !activeLink)) {
activeLink = frame.link
if (linkOpen || (last && last !== "\n"))
append(`](${destination(frame.link.href)}${title(frame.link.title)})`)
if (linkOpen || (last && last !== "\n")) closeLink(frame.link)
linkOpen = false
activeLink = undefined
}
+13 -32
View File
@@ -5,6 +5,7 @@ import type { Context } from "@opencode/plugin/effect/plugin"
import { Effect, Schema } from "effect"
import path from "path"
import { toolFailure } from "../runtime.js"
import { ToolSearch } from "../search.js"
import { Environment } from "../../environment/index.js"
import { FileSystem } from "../../filesystem.js"
import { Location } from "../../location.js"
@@ -80,44 +81,24 @@ export const Plugin = {
agent: context.agent,
source,
})
const type = yield* Environment.typeFollowing(environment.files, target.absolute).pipe(
Effect.catchTag("Environment.NotFound", () =>
Effect.fail(new ToolFailure({ message: `Search path does not exist: ${searchPath ?? "."}` })),
),
)
const type = yield* ToolSearch.pathType(environment.files, target.absolute, searchPath ?? ".")
if (type !== "directory")
return yield* Effect.fail(
new ToolFailure({ message: `Search path is not a directory: ${searchPath ?? "."}` }),
)
const root = target.absolute
const limit = input.limit ?? FileSystem.DEFAULT_SEARCH_LIMIT
const entries = yield* ripgrep
.glob({
cwd: root,
pattern: input.pattern,
hidden: input.hidden,
limit: limit + 1,
})
.pipe(
Effect.timeoutOrElse({
duration: FileSystem.DEFAULT_SEARCH_TIMEOUT_MS,
orElse: () =>
Effect.fail(
new ToolFailure({
message: `Search timed out after ${FileSystem.DEFAULT_SEARCH_TIMEOUT_MS / 1_000} seconds. Consider using a more specific path or pattern.`,
}),
),
const result = yield* ToolSearch.bounded(input.limit ?? FileSystem.DEFAULT_SEARCH_LIMIT, (limit) =>
ripgrep.glob({ cwd: root, pattern: input.pattern, hidden: input.hidden, limit }),
)
return {
entries: result.items.map((entry) =>
FileSystem.Entry.make({
...entry,
path: RelativePath.make(path.relative(location.directory, path.resolve(root, entry.path))),
}),
Effect.map((result) =>
result.map((entry) =>
FileSystem.Entry.make({
...entry,
path: RelativePath.make(path.relative(location.directory, path.resolve(root, entry.path))),
}),
),
),
)
return { entries: entries.slice(0, limit), truncated: entries.length > limit }
),
truncated: result.truncated,
}
}).pipe(
Effect.map((result) => ({
output: result.entries,
+18 -34
View File
@@ -11,6 +11,7 @@ import { FileAccess } from "../../file-access.js"
import { Permission } from "../../permission.js"
import { Ripgrep } from "../../ripgrep.js"
import { RelativePath } from "../../schema.js"
import { ToolSearch } from "../search.js"
export const name = "grep"
@@ -101,48 +102,31 @@ export const Plugin = {
source,
})
const root = target.absolute
const type = yield* Environment.typeFollowing(environment.files, root).pipe(
Effect.catchTag("Environment.NotFound", () =>
Effect.fail(new ToolFailure({ message: `Search path does not exist: ${input.path ?? "."}` })),
),
)
const type = yield* ToolSearch.pathType(environment.files, root, input.path ?? ".")
const cwd = type === "directory" ? root : path.dirname(root)
const limit = input.limit ?? FileSystem.DEFAULT_SEARCH_LIMIT
const matches = yield* ripgrep
.grep({
const result = yield* ToolSearch.bounded(input.limit ?? FileSystem.DEFAULT_SEARCH_LIMIT, (limit) =>
ripgrep.grep({
cwd,
pattern: input.pattern,
file: type === "file" ? path.basename(root) : undefined,
include: input.include,
literal: input.literal,
caseSensitive: input.caseSensitive,
limit: limit + 1,
})
.pipe(
Effect.timeoutOrElse({
duration: FileSystem.DEFAULT_SEARCH_TIMEOUT_MS,
orElse: () =>
Effect.fail(
new ToolFailure({
message: `Search timed out after ${FileSystem.DEFAULT_SEARCH_TIMEOUT_MS / 1_000} seconds. Consider using a more specific path or pattern.`,
}),
),
limit,
}),
)
return {
matches: result.items.map((match) =>
FileSystem.Match.make({
...match,
entry: FileSystem.Entry.make({
...match.entry,
path: RelativePath.make(path.relative(location.directory, path.resolve(cwd, match.entry.path))),
}),
}),
Effect.map((result) =>
result.map((match) =>
FileSystem.Match.make({
...match,
entry: FileSystem.Entry.make({
...match.entry,
path: RelativePath.make(
path.relative(location.directory, path.resolve(cwd, match.entry.path)),
),
}),
}),
),
),
)
return { matches: matches.slice(0, limit), truncated: matches.length > limit }
),
truncated: result.truncated,
}
}).pipe(
Effect.map((result) => ({
output: result.matches,
+44 -48
View File
@@ -156,48 +156,56 @@ export const Plugin = {
const readImage = Effect.fn("ImageGenerateTool.readImage")(function* (
source: string,
context: FileAccess.Invocation,
fetchAllowed: (url: string) => ReturnType<typeof permission.assert>,
) {
const bytes = yield* readBytes(source, context, fetchAllowed)
const bytes = /^https?:\/\//i.test(source)
? yield* Effect.gen(function* () {
// Downloads follow the same rule as the webfetch tool, so denying web access also covers image URLs.
yield* permission.assert({
action: "webfetch",
resources: [source],
save: ["*"],
metadata: { url: source },
sessionID: context.sessionID,
agent: context.agent,
source: { type: "tool", messageID: context.messageID, id: context.id },
})
return yield* Effect.gen(function* () {
const response = yield* http
.execute(HttpClientRequest.get(source))
.pipe(Effect.flatMap(HttpClientResponse.filterStatusOk))
return yield* collectBoundedResponseBody(
response,
MAX_INPUT_BYTES,
() => new ToolFailure({ message: `${source} is larger than 20 MiB.` }),
)
}).pipe(
Effect.timeoutOrElse({
duration: DOWNLOAD_TIMEOUT,
orElse: () => Effect.fail(new ToolFailure({ message: `Timed out downloading ${source}.` })),
}),
Effect.mapError(toolFailure(`Unable to download ${source}`)),
)
})
: yield* Effect.gen(function* () {
const target = yield* access.authorizeRead(source, context)
const info = yield* environment.files
.stat(target.absolute)
.pipe(
Effect.catchTag(
"Environment.NotFound",
() => new ToolFailure({ message: `File not found: ${source}` }),
),
)
if (info.size > MAX_INPUT_BYTES)
return yield* new ToolFailure({ message: `${source} is larger than 20 MiB.` })
return (yield* environment.files.read(target.absolute)).bytes
})
const mime = Mime.detect(bytes)
if (!INPUT_TYPES.includes(mime))
return yield* new ToolFailure({ message: `${source} is not a PNG, JPEG, WebP or GIF image.` })
return { data: Buffer.from(bytes).toString("base64"), mime, name: path.basename(source) }
})
const readBytes = Effect.fn("ImageGenerateTool.readBytes")(function* (
source: string,
context: FileAccess.Invocation,
fetchAllowed: (url: string) => ReturnType<typeof permission.assert>,
) {
if (/^https?:\/\//i.test(source)) {
// Downloads follow the same rule as the webfetch tool, so denying web access also covers image URLs.
yield* fetchAllowed(source)
return yield* Effect.gen(function* () {
const response = yield* http
.execute(HttpClientRequest.get(source))
.pipe(Effect.flatMap(HttpClientResponse.filterStatusOk))
return yield* collectBoundedResponseBody(
response,
MAX_INPUT_BYTES,
() => new ToolFailure({ message: `${source} is larger than 20 MiB.` }),
)
}).pipe(
Effect.timeoutOrElse({
duration: DOWNLOAD_TIMEOUT,
orElse: () => Effect.fail(new ToolFailure({ message: `Timed out downloading ${source}.` })),
}),
Effect.mapError(toolFailure(`Unable to download ${source}`)),
)
}
const target = yield* access.authorizeRead(source, context)
const info = yield* environment.files
.stat(target.absolute)
.pipe(Effect.catchTag("Environment.NotFound", () => new ToolFailure({ message: `File not found: ${source}` })))
if (info.size > MAX_INPUT_BYTES) return yield* new ToolFailure({ message: `${source} is larger than 20 MiB.` })
return (yield* environment.files.read(target.absolute)).bytes
})
const notifyWhenDone = Effect.fn("ImageGenerateTool.notifyWhenDone")(
function* (input: { sessionID: SessionSchema.ID; id: string; notificationID: SessionMessage.ID; file: string }) {
const info = (yield* jobs.wait({ id: input.id })).info
@@ -288,19 +296,7 @@ export const Plugin = {
})
}
const images = input.images
? yield* Effect.forEach(input.images, (image) =>
readImage(image, context, (url) =>
permission.assert({
action: "webfetch",
resources: [url],
save: ["*"],
metadata: { url },
sessionID: context.sessionID,
agent: context.agent,
source,
}),
),
)
? yield* Effect.forEach(input.images, (image) => readImage(image, context))
: undefined
// Everything that needs the user or can fail fast happened above; the paid call runs as a background job.
+8 -27
View File
@@ -231,22 +231,17 @@ export const Plugin = {
prepared,
(change) =>
Effect.gen(function* () {
if (change.type === "delete") {
const moveTarget = change.type === "update" ? change.moveTarget : undefined
const target = moveTarget ?? change.target
if (change.type === "delete")
yield* environment.files
.remove(change.target.absolute)
.pipe(Effect.mapError((error) => fail(`Failed to delete ${change.target.resource}`, error)))
applied.push({
type: change.type,
resource: change.target.resource,
target: change.target.absolute,
})
return
}
if (change.type === "update" && change.moveTarget) {
const moveTarget = change.moveTarget
if (change.type !== "delete")
yield* environment.files
.write(moveTarget.absolute, new TextEncoder().encode(change.content))
.pipe(Effect.mapError((error) => fail(`Failed to write ${moveTarget.resource}`, error)))
.write(target.absolute, new TextEncoder().encode(change.content))
.pipe(Effect.mapError((error) => fail(`Failed to write ${target.resource}`, error)))
if (moveTarget)
yield* environment.files
.remove(change.target.absolute)
.pipe(
@@ -254,21 +249,7 @@ export const Plugin = {
fail(`Wrote ${moveTarget.resource} but failed to remove ${change.target.resource}`, error),
),
)
applied.push({
type: change.type,
resource: change.moveTarget.resource,
target: change.moveTarget.absolute,
})
return
}
yield* environment.files
.write(change.target.absolute, new TextEncoder().encode(change.content))
.pipe(Effect.mapError((error) => fail(`Failed to write ${change.target.resource}`, error)))
applied.push({
type: change.type,
resource: change.target.resource,
target: change.target.absolute,
})
applied.push({ type: change.type, resource: target.resource, target: target.absolute })
}),
{ discard: true },
)
+28
View File
@@ -0,0 +1,28 @@
export * as ToolSearch from "./search.js"
import { ToolFailure } from "@opencode/ai/effect"
import { Effect } from "effect"
import { Environment } from "../environment/index.js"
import { FileSystem } from "../filesystem.js"
export const pathType = (files: Environment.Files, absolute: string, display: string) =>
Environment.typeFollowing(files, absolute).pipe(
Effect.catchTag("Environment.NotFound", () =>
Effect.fail(new ToolFailure({ message: `Search path does not exist: ${display}` })),
),
)
// Fetches one result past the limit so truncation can be reported without a second search.
export const bounded = <A, E, R>(limit: number, search: (limit: number) => Effect.Effect<readonly A[], E, R>) =>
search(limit + 1).pipe(
Effect.timeoutOrElse({
duration: FileSystem.DEFAULT_SEARCH_TIMEOUT_MS,
orElse: () =>
Effect.fail(
new ToolFailure({
message: `Search timed out after ${FileSystem.DEFAULT_SEARCH_TIMEOUT_MS / 1_000} seconds. Consider using a more specific path or pattern.`,
}),
),
}),
Effect.map((items) => ({ items: items.slice(0, limit), truncated: items.length > limit })),
)