bincode-cli 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +27 -0
- package/README.md +15 -0
- package/bin/bincode +98 -0
- package/bunfig.toml +4 -0
- package/package.json +124 -0
- package/parsers-config.ts +239 -0
- package/script/build.ts +167 -0
- package/script/postinstall.mjs +206 -0
- package/script/publish.ts +99 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +1051 -0
- package/src/acp/session.ts +101 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +398 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +10 -0
- package/src/agent/prompt/title.txt +36 -0
- package/src/auth/bineric-login.ts +506 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +114 -0
- package/src/bus/bus-event.ts +43 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +105 -0
- package/src/cli/auth-check.ts +61 -0
- package/src/cli/bootstrap.ts +21 -0
- package/src/cli/cmd/acp.ts +88 -0
- package/src/cli/cmd/agent.ts +256 -0
- package/src/cli/cmd/auth.ts +436 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/config.ts +15 -0
- package/src/cli/cmd/debug/file.ts +91 -0
- package/src/cli/cmd/debug/index.ts +43 -0
- package/src/cli/cmd/debug/lsp.ts +48 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/skill.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +48 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1399 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/login.ts +112 -0
- package/src/cli/cmd/logout.ts +38 -0
- package/src/cli/cmd/mcp.ts +654 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +368 -0
- package/src/cli/cmd/serve.ts +31 -0
- package/src/cli/cmd/session.ts +106 -0
- package/src/cli/cmd/stats.ts +298 -0
- package/src/cli/cmd/tui/app.tsx +669 -0
- package/src/cli/cmd/tui/attach.ts +30 -0
- package/src/cli/cmd/tui/component/border.tsx +21 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-command.tsx +123 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +223 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +224 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
- package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/logo.tsx +32 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +560 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +1052 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +13 -0
- package/src/cli/cmd/tui/context/exit.tsx +23 -0
- package/src/cli/cmd/tui/context/helper.tsx +25 -0
- package/src/cli/cmd/tui/context/keybind.tsx +101 -0
- package/src/cli/cmd/tui/context/kv.tsx +49 -0
- package/src/cli/cmd/tui/context/local.tsx +339 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +46 -0
- package/src/cli/cmd/tui/context/sdk.tsx +74 -0
- package/src/cli/cmd/tui/context/sync.tsx +372 -0
- package/src/cli/cmd/tui/context/theme/aura.json +69 -0
- package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
- package/src/cli/cmd/tui/context/theme/bincode.json +245 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
- package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
- package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
- package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
- package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
- package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
- package/src/cli/cmd/tui/context/theme/github.json +233 -0
- package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
- package/src/cli/cmd/tui/context/theme/material.json +235 -0
- package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
- package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
- package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
- package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
- package/src/cli/cmd/tui/context/theme/nord.json +223 -0
- package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
- package/src/cli/cmd/tui/context/theme/orng.json +245 -0
- package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
- package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
- package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
- package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
- package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
- package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
- package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
- package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
- package/src/cli/cmd/tui/context/theme.tsx +1109 -0
- package/src/cli/cmd/tui/event.ts +40 -0
- package/src/cli/cmd/tui/routes/home.tsx +105 -0
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +141 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1888 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +321 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +120 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +330 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +170 -0
- package/src/cli/cmd/tui/ui/spinner.ts +368 -0
- package/src/cli/cmd/tui/ui/toast.tsx +100 -0
- package/src/cli/cmd/tui/util/clipboard.ts +127 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/worker.ts +63 -0
- package/src/cli/cmd/uninstall.ts +344 -0
- package/src/cli/cmd/upgrade.ts +67 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +55 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +80 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +97 -0
- package/src/config/config.ts +995 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +328 -0
- package/src/file/ripgrep.ts +393 -0
- package/src/file/time.ts +64 -0
- package/src/file/watcher.ts +103 -0
- package/src/flag/flag.ts +46 -0
- package/src/format/formatter.ts +315 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +73 -0
- package/src/ide/index.ts +76 -0
- package/src/index.ts +217 -0
- package/src/installation/index.ts +196 -0
- package/src/lsp/client.ts +229 -0
- package/src/lsp/index.ts +485 -0
- package/src/lsp/language.ts +116 -0
- package/src/lsp/server.ts +1895 -0
- package/src/mcp/auth.ts +135 -0
- package/src/mcp/index.ts +654 -0
- package/src/mcp/oauth-callback.ts +200 -0
- package/src/mcp/oauth-provider.ts +154 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +199 -0
- package/src/plugin/index.ts +101 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +78 -0
- package/src/project/project.ts +221 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +76 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +106 -0
- package/src/provider/provider.ts +1071 -0
- package/src/provider/sdk/openai-compatible/src/README.md +5 -0
- package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +101 -0
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
- package/src/provider/transform.ts +455 -0
- package/src/pty/index.ts +231 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +79 -0
- package/src/server/server.ts +2642 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +223 -0
- package/src/session/index.ts +458 -0
- package/src/session/llm.ts +201 -0
- package/src/session/message-v2.ts +659 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +409 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +104 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +147 -0
- package/src/session/prompt/build-switch.txt +5 -0
- package/src/session/prompt/codex.txt +318 -0
- package/src/session/prompt/copilot-gpt-5.txt +143 -0
- package/src/session/prompt/gemini.txt +155 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/src/session/prompt/plan.txt +26 -0
- package/src/session/prompt/polaris.txt +106 -0
- package/src/session/prompt/qwen.txt +109 -0
- package/src/session/prompt.ts +1446 -0
- package/src/session/retry.ts +86 -0
- package/src/session/revert.ts +108 -0
- package/src/session/status.ts +76 -0
- package/src/session/summary.ts +194 -0
- package/src/session/system.ts +120 -0
- package/src/session/todo.ts +37 -0
- package/src/share/share-next.ts +194 -0
- package/src/share/share.ts +87 -0
- package/src/shell/shell.ts +67 -0
- package/src/skill/index.ts +1 -0
- package/src/skill/skill.ts +83 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/tool/bash.ts +306 -0
- package/src/tool/bash.txt +158 -0
- package/src/tool/batch.ts +175 -0
- package/src/tool/batch.txt +24 -0
- package/src/tool/codesearch.ts +138 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +675 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/glob.ts +65 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +121 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +110 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp-diagnostics.ts +26 -0
- package/src/tool/lsp-diagnostics.txt +1 -0
- package/src/tool/lsp-hover.ts +31 -0
- package/src/tool/lsp-hover.txt +1 -0
- package/src/tool/lsp.ts +87 -0
- package/src/tool/lsp.txt +19 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +219 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +162 -0
- package/src/tool/skill.ts +100 -0
- package/src/tool/task.ts +136 -0
- package/src/tool/task.txt +60 -0
- package/src/tool/todo.ts +39 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +71 -0
- package/src/tool/webfetch.ts +187 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +150 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +110 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +16 -0
- package/src/util/color.ts +19 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +83 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +102 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +180 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +42 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +54 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { Tool } from "./tool"
|
|
3
|
+
import TurndownService from "turndown"
|
|
4
|
+
import DESCRIPTION from "./webfetch.txt"
|
|
5
|
+
import { Config } from "../config/config"
|
|
6
|
+
import { Permission } from "../permission"
|
|
7
|
+
|
|
8
|
+
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
|
|
9
|
+
const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
|
|
10
|
+
const MAX_TIMEOUT = 120 * 1000 // 2 minutes
|
|
11
|
+
|
|
12
|
+
export const WebFetchTool = Tool.define("webfetch", {
|
|
13
|
+
description: DESCRIPTION,
|
|
14
|
+
parameters: z.object({
|
|
15
|
+
url: z.string().describe("The URL to fetch content from"),
|
|
16
|
+
format: z
|
|
17
|
+
.enum(["text", "markdown", "html"])
|
|
18
|
+
.describe("The format to return the content in (text, markdown, or html)"),
|
|
19
|
+
timeout: z.number().describe("Optional timeout in seconds (max 120)").optional(),
|
|
20
|
+
}),
|
|
21
|
+
async execute(params, ctx) {
|
|
22
|
+
// Validate URL
|
|
23
|
+
if (!params.url.startsWith("http://") && !params.url.startsWith("https://")) {
|
|
24
|
+
throw new Error("URL must start with http:// or https://")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const cfg = await Config.get()
|
|
28
|
+
if (cfg.permission?.webfetch === "ask")
|
|
29
|
+
await Permission.ask({
|
|
30
|
+
type: "webfetch",
|
|
31
|
+
sessionID: ctx.sessionID,
|
|
32
|
+
messageID: ctx.messageID,
|
|
33
|
+
callID: ctx.callID,
|
|
34
|
+
title: "Fetch content from: " + params.url,
|
|
35
|
+
metadata: {
|
|
36
|
+
url: params.url,
|
|
37
|
+
format: params.format,
|
|
38
|
+
timeout: params.timeout,
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const timeout = Math.min((params.timeout ?? DEFAULT_TIMEOUT / 1000) * 1000, MAX_TIMEOUT)
|
|
43
|
+
|
|
44
|
+
const controller = new AbortController()
|
|
45
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout)
|
|
46
|
+
|
|
47
|
+
// Build Accept header based on requested format with q parameters for fallbacks
|
|
48
|
+
let acceptHeader = "*/*"
|
|
49
|
+
switch (params.format) {
|
|
50
|
+
case "markdown":
|
|
51
|
+
acceptHeader = "text/markdown;q=1.0, text/x-markdown;q=0.9, text/plain;q=0.8, text/html;q=0.7, */*;q=0.1"
|
|
52
|
+
break
|
|
53
|
+
case "text":
|
|
54
|
+
acceptHeader = "text/plain;q=1.0, text/markdown;q=0.9, text/html;q=0.8, */*;q=0.1"
|
|
55
|
+
break
|
|
56
|
+
case "html":
|
|
57
|
+
acceptHeader = "text/html;q=1.0, application/xhtml+xml;q=0.9, text/plain;q=0.8, text/markdown;q=0.7, */*;q=0.1"
|
|
58
|
+
break
|
|
59
|
+
default:
|
|
60
|
+
acceptHeader =
|
|
61
|
+
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8"
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const response = await fetch(params.url, {
|
|
65
|
+
signal: AbortSignal.any([controller.signal, ctx.abort]),
|
|
66
|
+
headers: {
|
|
67
|
+
"User-Agent":
|
|
68
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
|
69
|
+
Accept: acceptHeader,
|
|
70
|
+
"Accept-Language": "en-US,en;q=0.9",
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
clearTimeout(timeoutId)
|
|
75
|
+
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
throw new Error(`Request failed with status code: ${response.status}`)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Check content length
|
|
81
|
+
const contentLength = response.headers.get("content-length")
|
|
82
|
+
if (contentLength && parseInt(contentLength) > MAX_RESPONSE_SIZE) {
|
|
83
|
+
throw new Error("Response too large (exceeds 5MB limit)")
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const arrayBuffer = await response.arrayBuffer()
|
|
87
|
+
if (arrayBuffer.byteLength > MAX_RESPONSE_SIZE) {
|
|
88
|
+
throw new Error("Response too large (exceeds 5MB limit)")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const content = new TextDecoder().decode(arrayBuffer)
|
|
92
|
+
const contentType = response.headers.get("content-type") || ""
|
|
93
|
+
|
|
94
|
+
const title = `${params.url} (${contentType})`
|
|
95
|
+
|
|
96
|
+
// Handle content based on requested format and actual content type
|
|
97
|
+
switch (params.format) {
|
|
98
|
+
case "markdown":
|
|
99
|
+
if (contentType.includes("text/html")) {
|
|
100
|
+
const markdown = convertHTMLToMarkdown(content)
|
|
101
|
+
return {
|
|
102
|
+
output: markdown,
|
|
103
|
+
title,
|
|
104
|
+
metadata: {},
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
output: content,
|
|
109
|
+
title,
|
|
110
|
+
metadata: {},
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
case "text":
|
|
114
|
+
if (contentType.includes("text/html")) {
|
|
115
|
+
const text = await extractTextFromHTML(content)
|
|
116
|
+
return {
|
|
117
|
+
output: text,
|
|
118
|
+
title,
|
|
119
|
+
metadata: {},
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
output: content,
|
|
124
|
+
title,
|
|
125
|
+
metadata: {},
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
case "html":
|
|
129
|
+
return {
|
|
130
|
+
output: content,
|
|
131
|
+
title,
|
|
132
|
+
metadata: {},
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
default:
|
|
136
|
+
return {
|
|
137
|
+
output: content,
|
|
138
|
+
title,
|
|
139
|
+
metadata: {},
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
async function extractTextFromHTML(html: string) {
|
|
146
|
+
let text = ""
|
|
147
|
+
let skipContent = false
|
|
148
|
+
|
|
149
|
+
const rewriter = new HTMLRewriter()
|
|
150
|
+
.on("script, style, noscript, iframe, object, embed", {
|
|
151
|
+
element() {
|
|
152
|
+
skipContent = true
|
|
153
|
+
},
|
|
154
|
+
text() {
|
|
155
|
+
// Skip text content inside these elements
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
.on("*", {
|
|
159
|
+
element(element) {
|
|
160
|
+
// Reset skip flag when entering other elements
|
|
161
|
+
if (!["script", "style", "noscript", "iframe", "object", "embed"].includes(element.tagName)) {
|
|
162
|
+
skipContent = false
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
text(input) {
|
|
166
|
+
if (!skipContent) {
|
|
167
|
+
text += input.text
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
})
|
|
171
|
+
.transform(new Response(html))
|
|
172
|
+
|
|
173
|
+
await rewriter.text()
|
|
174
|
+
return text.trim()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function convertHTMLToMarkdown(html: string): string {
|
|
178
|
+
const turndownService = new TurndownService({
|
|
179
|
+
headingStyle: "atx",
|
|
180
|
+
hr: "---",
|
|
181
|
+
bulletListMarker: "-",
|
|
182
|
+
codeBlockStyle: "fenced",
|
|
183
|
+
emDelimiter: "*",
|
|
184
|
+
})
|
|
185
|
+
turndownService.remove(["script", "style", "meta", "link"])
|
|
186
|
+
return turndownService.turndown(html)
|
|
187
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
- Fetches content from a specified URL
|
|
2
|
+
- Takes a URL and a prompt as input
|
|
3
|
+
- Fetches the URL content, converts HTML to markdown
|
|
4
|
+
- Returns the model's response about the content
|
|
5
|
+
- Use this tool when you need to retrieve and analyze web content
|
|
6
|
+
|
|
7
|
+
Usage notes:
|
|
8
|
+
- IMPORTANT: if another tool is present that offers better web fetching capabilities, is more targeted to the task, or has fewer restrictions, prefer using that tool instead of this one.
|
|
9
|
+
- The URL must be a fully-formed valid URL
|
|
10
|
+
- HTTP URLs will be automatically upgraded to HTTPS
|
|
11
|
+
- The prompt should describe what information you want to extract from the page
|
|
12
|
+
- This tool is read-only and does not modify any files
|
|
13
|
+
- Results may be summarized if the content is very large
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { Tool } from "./tool"
|
|
3
|
+
import DESCRIPTION from "./websearch.txt"
|
|
4
|
+
import { Config } from "../config/config"
|
|
5
|
+
import { Permission } from "../permission"
|
|
6
|
+
|
|
7
|
+
const API_CONFIG = {
|
|
8
|
+
BASE_URL: "https://mcp.exa.ai",
|
|
9
|
+
ENDPOINTS: {
|
|
10
|
+
SEARCH: "/mcp",
|
|
11
|
+
},
|
|
12
|
+
DEFAULT_NUM_RESULTS: 8,
|
|
13
|
+
} as const
|
|
14
|
+
|
|
15
|
+
interface McpSearchRequest {
|
|
16
|
+
jsonrpc: string
|
|
17
|
+
id: number
|
|
18
|
+
method: string
|
|
19
|
+
params: {
|
|
20
|
+
name: string
|
|
21
|
+
arguments: {
|
|
22
|
+
query: string
|
|
23
|
+
numResults?: number
|
|
24
|
+
livecrawl?: "fallback" | "preferred"
|
|
25
|
+
type?: "auto" | "fast" | "deep"
|
|
26
|
+
contextMaxCharacters?: number
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface McpSearchResponse {
|
|
32
|
+
jsonrpc: string
|
|
33
|
+
result: {
|
|
34
|
+
content: Array<{
|
|
35
|
+
type: string
|
|
36
|
+
text: string
|
|
37
|
+
}>
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const WebSearchTool = Tool.define("websearch", {
|
|
42
|
+
description: DESCRIPTION,
|
|
43
|
+
parameters: z.object({
|
|
44
|
+
query: z.string().describe("Websearch query"),
|
|
45
|
+
numResults: z.number().optional().describe("Number of search results to return (default: 8)"),
|
|
46
|
+
livecrawl: z
|
|
47
|
+
.enum(["fallback", "preferred"])
|
|
48
|
+
.optional()
|
|
49
|
+
.describe(
|
|
50
|
+
"Live crawl mode - 'fallback': use live crawling as backup if cached content unavailable, 'preferred': prioritize live crawling (default: 'fallback')",
|
|
51
|
+
),
|
|
52
|
+
type: z
|
|
53
|
+
.enum(["auto", "fast", "deep"])
|
|
54
|
+
.optional()
|
|
55
|
+
.describe("Search type - 'auto': balanced search (default), 'fast': quick results, 'deep': comprehensive search"),
|
|
56
|
+
contextMaxCharacters: z
|
|
57
|
+
.number()
|
|
58
|
+
.optional()
|
|
59
|
+
.describe("Maximum characters for context string optimized for LLMs (default: 10000)"),
|
|
60
|
+
}),
|
|
61
|
+
async execute(params, ctx) {
|
|
62
|
+
const cfg = await Config.get()
|
|
63
|
+
if (cfg.permission?.webfetch === "ask")
|
|
64
|
+
await Permission.ask({
|
|
65
|
+
type: "websearch",
|
|
66
|
+
sessionID: ctx.sessionID,
|
|
67
|
+
messageID: ctx.messageID,
|
|
68
|
+
callID: ctx.callID,
|
|
69
|
+
title: "Search web for: " + params.query,
|
|
70
|
+
metadata: {
|
|
71
|
+
query: params.query,
|
|
72
|
+
numResults: params.numResults,
|
|
73
|
+
livecrawl: params.livecrawl,
|
|
74
|
+
type: params.type,
|
|
75
|
+
contextMaxCharacters: params.contextMaxCharacters,
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const searchRequest: McpSearchRequest = {
|
|
80
|
+
jsonrpc: "2.0",
|
|
81
|
+
id: 1,
|
|
82
|
+
method: "tools/call",
|
|
83
|
+
params: {
|
|
84
|
+
name: "web_search_exa",
|
|
85
|
+
arguments: {
|
|
86
|
+
query: params.query,
|
|
87
|
+
type: params.type || "auto",
|
|
88
|
+
numResults: params.numResults || API_CONFIG.DEFAULT_NUM_RESULTS,
|
|
89
|
+
livecrawl: params.livecrawl || "fallback",
|
|
90
|
+
contextMaxCharacters: params.contextMaxCharacters,
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const controller = new AbortController()
|
|
96
|
+
const timeoutId = setTimeout(() => controller.abort(), 25000)
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const headers: Record<string, string> = {
|
|
100
|
+
accept: "application/json, text/event-stream",
|
|
101
|
+
"content-type": "application/json",
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const response = await fetch(`${API_CONFIG.BASE_URL}${API_CONFIG.ENDPOINTS.SEARCH}`, {
|
|
105
|
+
method: "POST",
|
|
106
|
+
headers,
|
|
107
|
+
body: JSON.stringify(searchRequest),
|
|
108
|
+
signal: AbortSignal.any([controller.signal, ctx.abort]),
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
clearTimeout(timeoutId)
|
|
112
|
+
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
const errorText = await response.text()
|
|
115
|
+
throw new Error(`Search error (${response.status}): ${errorText}`)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const responseText = await response.text()
|
|
119
|
+
|
|
120
|
+
// Parse SSE response
|
|
121
|
+
const lines = responseText.split("\n")
|
|
122
|
+
for (const line of lines) {
|
|
123
|
+
if (line.startsWith("data: ")) {
|
|
124
|
+
const data: McpSearchResponse = JSON.parse(line.substring(6))
|
|
125
|
+
if (data.result && data.result.content && data.result.content.length > 0) {
|
|
126
|
+
return {
|
|
127
|
+
output: data.result.content[0].text,
|
|
128
|
+
title: `Web search: ${params.query}`,
|
|
129
|
+
metadata: {},
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
output: "No search results found. Please try a different query.",
|
|
137
|
+
title: `Web search: ${params.query}`,
|
|
138
|
+
metadata: {},
|
|
139
|
+
}
|
|
140
|
+
} catch (error) {
|
|
141
|
+
clearTimeout(timeoutId)
|
|
142
|
+
|
|
143
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
144
|
+
throw new Error("Search request timed out")
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
throw error
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
- Search the web using Exa AI - performs real-time web searches and can scrape content from specific URLs
|
|
2
|
+
- Provides up-to-date information for current events and recent data
|
|
3
|
+
- Supports configurable result counts and returns the content from the most relevant websites
|
|
4
|
+
- Use this tool for accessing information beyond knowledge cutoff
|
|
5
|
+
- Searches are performed automatically within a single API call
|
|
6
|
+
|
|
7
|
+
Usage notes:
|
|
8
|
+
- Supports live crawling modes: 'fallback' (backup if cached unavailable) or 'preferred' (prioritize live crawling)
|
|
9
|
+
- Search types: 'auto' (balanced), 'fast' (quick results), 'deep' (comprehensive search)
|
|
10
|
+
- Configurable context length for optimal LLM integration
|
|
11
|
+
- Domain filtering and advanced search options available
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import * as path from "path"
|
|
3
|
+
import { Tool } from "./tool"
|
|
4
|
+
import { LSP } from "../lsp"
|
|
5
|
+
import { Permission } from "../permission"
|
|
6
|
+
import DESCRIPTION from "./write.txt"
|
|
7
|
+
import { Bus } from "../bus"
|
|
8
|
+
import { File } from "../file"
|
|
9
|
+
import { FileTime } from "../file/time"
|
|
10
|
+
import { Filesystem } from "../util/filesystem"
|
|
11
|
+
import { Instance } from "../project/instance"
|
|
12
|
+
import { Agent } from "../agent/agent"
|
|
13
|
+
|
|
14
|
+
const MAX_DIAGNOSTICS_PER_FILE = 20
|
|
15
|
+
const MAX_PROJECT_DIAGNOSTICS_FILES = 5
|
|
16
|
+
|
|
17
|
+
export const WriteTool = Tool.define("write", {
|
|
18
|
+
description: DESCRIPTION,
|
|
19
|
+
parameters: z.object({
|
|
20
|
+
content: z.string().describe("The content to write to the file"),
|
|
21
|
+
filePath: z.string().describe("The absolute path to the file to write (must be absolute, not relative)"),
|
|
22
|
+
}),
|
|
23
|
+
async execute(params, ctx) {
|
|
24
|
+
const agent = await Agent.get(ctx.agent)
|
|
25
|
+
|
|
26
|
+
const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)
|
|
27
|
+
if (!Filesystem.contains(Instance.directory, filepath)) {
|
|
28
|
+
const parentDir = path.dirname(filepath)
|
|
29
|
+
if (agent.permission.external_directory === "ask") {
|
|
30
|
+
await Permission.ask({
|
|
31
|
+
type: "external_directory",
|
|
32
|
+
pattern: [parentDir, path.join(parentDir, "*")],
|
|
33
|
+
sessionID: ctx.sessionID,
|
|
34
|
+
messageID: ctx.messageID,
|
|
35
|
+
callID: ctx.callID,
|
|
36
|
+
title: `Write file outside working directory: ${filepath}`,
|
|
37
|
+
metadata: {
|
|
38
|
+
filepath,
|
|
39
|
+
parentDir,
|
|
40
|
+
},
|
|
41
|
+
})
|
|
42
|
+
} else if (agent.permission.external_directory === "deny") {
|
|
43
|
+
throw new Permission.RejectedError(
|
|
44
|
+
ctx.sessionID,
|
|
45
|
+
"external_directory",
|
|
46
|
+
ctx.callID,
|
|
47
|
+
{
|
|
48
|
+
filepath: filepath,
|
|
49
|
+
parentDir,
|
|
50
|
+
},
|
|
51
|
+
`File ${filepath} is not in the current working directory`,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const file = Bun.file(filepath)
|
|
57
|
+
const exists = await file.exists()
|
|
58
|
+
if (exists) await FileTime.assert(ctx.sessionID, filepath)
|
|
59
|
+
|
|
60
|
+
if (agent.permission.edit === "ask")
|
|
61
|
+
await Permission.ask({
|
|
62
|
+
type: "write",
|
|
63
|
+
sessionID: ctx.sessionID,
|
|
64
|
+
messageID: ctx.messageID,
|
|
65
|
+
callID: ctx.callID,
|
|
66
|
+
title: exists ? "Overwrite this file: " + filepath : "Create new file: " + filepath,
|
|
67
|
+
metadata: {
|
|
68
|
+
filePath: filepath,
|
|
69
|
+
content: params.content,
|
|
70
|
+
exists,
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
await Bun.write(filepath, params.content)
|
|
75
|
+
await Bus.publish(File.Event.Edited, {
|
|
76
|
+
file: filepath,
|
|
77
|
+
})
|
|
78
|
+
FileTime.read(ctx.sessionID, filepath)
|
|
79
|
+
|
|
80
|
+
let output = ""
|
|
81
|
+
await LSP.touchFile(filepath, true)
|
|
82
|
+
const diagnostics = await LSP.diagnostics()
|
|
83
|
+
const normalizedFilepath = Filesystem.normalizePath(filepath)
|
|
84
|
+
let projectDiagnosticsCount = 0
|
|
85
|
+
for (const [file, issues] of Object.entries(diagnostics)) {
|
|
86
|
+
if (issues.length === 0) continue
|
|
87
|
+
const sorted = issues.toSorted((a, b) => (a.severity ?? 4) - (b.severity ?? 4))
|
|
88
|
+
const limited = sorted.slice(0, MAX_DIAGNOSTICS_PER_FILE)
|
|
89
|
+
const suffix =
|
|
90
|
+
issues.length > MAX_DIAGNOSTICS_PER_FILE ? `\n... and ${issues.length - MAX_DIAGNOSTICS_PER_FILE} more` : ""
|
|
91
|
+
if (file === normalizedFilepath) {
|
|
92
|
+
output += `\nThis file has errors, please fix\n<file_diagnostics>\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</file_diagnostics>\n`
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
if (projectDiagnosticsCount >= MAX_PROJECT_DIAGNOSTICS_FILES) continue
|
|
96
|
+
projectDiagnosticsCount++
|
|
97
|
+
output += `\n<project_diagnostics>\n${file}\n${limited.map(LSP.Diagnostic.pretty).join("\n")}${suffix}\n</project_diagnostics>\n`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
title: path.relative(Instance.worktree, filepath),
|
|
102
|
+
metadata: {
|
|
103
|
+
diagnostics,
|
|
104
|
+
filepath,
|
|
105
|
+
exists: exists,
|
|
106
|
+
},
|
|
107
|
+
output,
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
})
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Writes a file to the local filesystem.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
- This tool will overwrite the existing file if there is one at the provided path.
|
|
5
|
+
- If this is an existing file, you MUST use the Read tool first to read the file's contents. This tool will fail if you did not read the file first.
|
|
6
|
+
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
|
|
7
|
+
- NEVER proactively create documentation files (*.md) or README files. Only create documentation files if explicitly requested by the User.
|
|
8
|
+
- Only use emojis if the user explicitly requests it. Avoid writing emojis to files unless asked.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { $ } from "bun"
|
|
2
|
+
import path from "path"
|
|
3
|
+
|
|
4
|
+
export namespace Archive {
|
|
5
|
+
export async function extractZip(zipPath: string, destDir: string) {
|
|
6
|
+
if (process.platform === "win32") {
|
|
7
|
+
const winZipPath = path.resolve(zipPath)
|
|
8
|
+
const winDestDir = path.resolve(destDir)
|
|
9
|
+
// $global:ProgressPreference suppresses PowerShell's blue progress bar popup
|
|
10
|
+
const cmd = `$global:ProgressPreference = 'SilentlyContinue'; Expand-Archive -Path '${winZipPath}' -DestinationPath '${winDestDir}' -Force`
|
|
11
|
+
await $`powershell -NoProfile -NonInteractive -Command ${cmd}`.quiet()
|
|
12
|
+
} else {
|
|
13
|
+
await $`unzip -o -q ${zipPath} -d ${destDir}`.quiet()
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export namespace Color {
|
|
2
|
+
export function isValidHex(hex?: string): hex is string {
|
|
3
|
+
if (!hex) return false
|
|
4
|
+
return /^#[0-9a-fA-F]{6}$/.test(hex)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function hexToRgb(hex: string): { r: number; g: number; b: number } {
|
|
8
|
+
const r = parseInt(hex.slice(1, 3), 16)
|
|
9
|
+
const g = parseInt(hex.slice(3, 5), 16)
|
|
10
|
+
const b = parseInt(hex.slice(5, 7), 16)
|
|
11
|
+
return { r, g, b }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function hexToAnsiBold(hex?: string): string | undefined {
|
|
15
|
+
if (!isValidHex(hex)) return undefined
|
|
16
|
+
const { r, g, b } = hexToRgb(hex)
|
|
17
|
+
return `\x1b[38;2;${r};${g};${b}m\x1b[1m`
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks"
|
|
2
|
+
|
|
3
|
+
export namespace Context {
|
|
4
|
+
export class NotFound extends Error {
|
|
5
|
+
constructor(public override readonly name: string) {
|
|
6
|
+
super(`No context found for ${name}`)
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function create<T>(name: string) {
|
|
11
|
+
const storage = new AsyncLocalStorage<T>()
|
|
12
|
+
return {
|
|
13
|
+
use() {
|
|
14
|
+
const result = storage.getStore()
|
|
15
|
+
if (!result) {
|
|
16
|
+
throw new NotFound(name)
|
|
17
|
+
}
|
|
18
|
+
return result
|
|
19
|
+
},
|
|
20
|
+
provide<R>(value: T, fn: () => R) {
|
|
21
|
+
return storage.run(value, fn)
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function defer<T extends () => void | Promise<void>>(
|
|
2
|
+
fn: T,
|
|
3
|
+
): T extends () => Promise<void> ? { [Symbol.asyncDispose]: () => Promise<void> } : { [Symbol.dispose]: () => void } {
|
|
4
|
+
return {
|
|
5
|
+
[Symbol.dispose]() {
|
|
6
|
+
fn()
|
|
7
|
+
},
|
|
8
|
+
[Symbol.asyncDispose]() {
|
|
9
|
+
return Promise.resolve(fn())
|
|
10
|
+
},
|
|
11
|
+
} as any
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Log } from "./log"
|
|
2
|
+
|
|
3
|
+
export namespace EventLoop {
|
|
4
|
+
export async function wait() {
|
|
5
|
+
return new Promise<void>((resolve) => {
|
|
6
|
+
const check = () => {
|
|
7
|
+
const active = [...(process as any)._getActiveHandles(), ...(process as any)._getActiveRequests()]
|
|
8
|
+
Log.Default.info("eventloop", {
|
|
9
|
+
active,
|
|
10
|
+
})
|
|
11
|
+
if ((process as any)._getActiveHandles().length === 0 && (process as any)._getActiveRequests().length === 0) {
|
|
12
|
+
resolve()
|
|
13
|
+
} else {
|
|
14
|
+
setImmediate(check)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
check()
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { realpathSync } from "fs"
|
|
2
|
+
import { exists } from "fs/promises"
|
|
3
|
+
import { dirname, join, relative } from "path"
|
|
4
|
+
|
|
5
|
+
export namespace Filesystem {
|
|
6
|
+
/**
|
|
7
|
+
* On Windows, normalize a path to its canonical casing using the filesystem.
|
|
8
|
+
* This is needed because Windows paths are case-insensitive but LSP servers
|
|
9
|
+
* may return paths with different casing than what we send them.
|
|
10
|
+
*/
|
|
11
|
+
export function normalizePath(p: string): string {
|
|
12
|
+
if (process.platform !== "win32") return p
|
|
13
|
+
try {
|
|
14
|
+
return realpathSync.native(p)
|
|
15
|
+
} catch {
|
|
16
|
+
return p
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function overlaps(a: string, b: string) {
|
|
20
|
+
const relA = relative(a, b)
|
|
21
|
+
const relB = relative(b, a)
|
|
22
|
+
return !relA || !relA.startsWith("..") || !relB || !relB.startsWith("..")
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function contains(parent: string, child: string) {
|
|
26
|
+
return !relative(parent, child).startsWith("..")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function findUp(target: string, start: string, stop?: string) {
|
|
30
|
+
let current = start
|
|
31
|
+
const result = []
|
|
32
|
+
while (true) {
|
|
33
|
+
const search = join(current, target)
|
|
34
|
+
if (await exists(search)) result.push(search)
|
|
35
|
+
if (stop === current) break
|
|
36
|
+
const parent = dirname(current)
|
|
37
|
+
if (parent === current) break
|
|
38
|
+
current = parent
|
|
39
|
+
}
|
|
40
|
+
return result
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function* up(options: { targets: string[]; start: string; stop?: string }) {
|
|
44
|
+
const { targets, start, stop } = options
|
|
45
|
+
let current = start
|
|
46
|
+
while (true) {
|
|
47
|
+
for (const target of targets) {
|
|
48
|
+
const search = join(current, target)
|
|
49
|
+
if (await exists(search)) yield search
|
|
50
|
+
}
|
|
51
|
+
if (stop === current) break
|
|
52
|
+
const parent = dirname(current)
|
|
53
|
+
if (parent === current) break
|
|
54
|
+
current = parent
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function globUp(pattern: string, start: string, stop?: string) {
|
|
59
|
+
let current = start
|
|
60
|
+
const result = []
|
|
61
|
+
while (true) {
|
|
62
|
+
try {
|
|
63
|
+
const glob = new Bun.Glob(pattern)
|
|
64
|
+
for await (const match of glob.scan({
|
|
65
|
+
cwd: current,
|
|
66
|
+
absolute: true,
|
|
67
|
+
onlyFiles: true,
|
|
68
|
+
followSymlinks: true,
|
|
69
|
+
dot: true,
|
|
70
|
+
})) {
|
|
71
|
+
result.push(match)
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// Skip invalid glob patterns
|
|
75
|
+
}
|
|
76
|
+
if (stop === current) break
|
|
77
|
+
const parent = dirname(current)
|
|
78
|
+
if (parent === current) break
|
|
79
|
+
current = parent
|
|
80
|
+
}
|
|
81
|
+
return result
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/util/fn.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from "zod"
|
|
2
|
+
|
|
3
|
+
export function fn<T extends z.ZodType, Result>(schema: T, cb: (input: z.infer<T>) => Result) {
|
|
4
|
+
const result = (input: z.infer<T>) => {
|
|
5
|
+
const parsed = schema.parse(input)
|
|
6
|
+
return cb(parsed)
|
|
7
|
+
}
|
|
8
|
+
result.force = (input: z.infer<T>) => cb(input)
|
|
9
|
+
result.schema = schema
|
|
10
|
+
return result
|
|
11
|
+
}
|
package/src/util/iife.ts
ADDED